blob: c196b5beb3856617dc51259c8084165d903e1b67 [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"
Ian Rogers1212a022013-03-04 10:48:41 -080024#include "compiler/driver/compiler_driver.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#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
43namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070044namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070045
Ian Rogers2c8a8572011-10-24 17:11:36 -070046static const bool gDebugVerify = false;
47
Ian Rogers7b3ddd22013-02-21 15:19:52 -080048void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070049 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070050 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070051 DCHECK_GT(insns_size, 0U);
52
53 for (uint32_t i = 0; i < insns_size; i++) {
54 bool interesting = false;
55 switch (mode) {
56 case kTrackRegsAll:
57 interesting = flags[i].IsOpcode();
58 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070059 case kTrackCompilerInterestPoints:
60 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget() ;
Ian Rogersd81871c2011-10-03 13:57:23 -070061 break;
62 case kTrackRegsBranches:
63 interesting = flags[i].IsBranchTarget();
64 break;
65 default:
66 break;
67 }
68 if (interesting) {
Elliott Hughesa0e18062012-04-13 15:59:59 -070069 pc_to_register_line_.Put(i, new RegisterLine(registers_size, verifier));
Ian Rogersd81871c2011-10-03 13:57:23 -070070 }
71 }
72}
73
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074MethodVerifier::FailureKind MethodVerifier::VerifyClass(const mirror::Class* klass,
Jeff Haoee988952013-04-16 14:23:47 -070075 std::string& error,
76 bool allow_soft_failures) {
jeffhaobdb76512011-09-07 11:43:16 -070077 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -070078 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -070079 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080 mirror::Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -080081 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -080082 error = "Verifier rejected class ";
83 error += PrettyDescriptor(klass);
84 error += " that has no super class";
jeffhaof1e6b7c2012-06-05 18:33:30 -070085 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070086 }
Ian Rogers1c5eb702012-02-01 09:18:34 -080087 if (super != NULL && super->IsFinal()) {
88 error = "Verifier rejected class ";
89 error += PrettyDescriptor(klass);
90 error += " that attempts to sub-class final class ";
91 error += PrettyDescriptor(super);
jeffhaof1e6b7c2012-06-05 18:33:30 -070092 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070093 }
Ian Rogersad0b3a32012-04-16 14:50:24 -070094 ClassHelper kh(klass);
95 const DexFile& dex_file = kh.GetDexFile();
96 uint32_t class_def_idx;
97 if (!dex_file.FindClassDefIndex(kh.GetDescriptor(), class_def_idx)) {
98 error = "Verifier rejected class ";
99 error += PrettyDescriptor(klass);
100 error += " that isn't present in dex file ";
101 error += dex_file.GetLocation();
jeffhaof1e6b7c2012-06-05 18:33:30 -0700102 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700103 }
Jeff Haoee988952013-04-16 14:23:47 -0700104 return VerifyClass(&dex_file, kh.GetDexCache(), klass->GetClassLoader(), class_def_idx, error, allow_soft_failures);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700105}
106
Ian Rogers365c1022012-06-22 15:05:28 -0700107MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800108 mirror::DexCache* dex_cache,
109 mirror::ClassLoader* class_loader,
110 uint32_t class_def_idx,
Jeff Haoee988952013-04-16 14:23:47 -0700111 std::string& error,
112 bool allow_soft_failures) {
jeffhaof56197c2012-03-05 18:01:54 -0800113 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
114 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700115 if (class_data == NULL) {
116 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700117 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700118 }
jeffhaof56197c2012-03-05 18:01:54 -0800119 ClassDataItemIterator it(*dex_file, class_data);
120 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
121 it.Next();
122 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700123 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700124 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700125 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700126 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800127 while (it.HasNextDirectMethod()) {
128 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700129 if (method_idx == previous_direct_method_idx) {
130 // smali can create dex files with two encoded_methods sharing the same method_idx
131 // http://code.google.com/p/smali/issues/detail?id=119
132 it.Next();
133 continue;
134 }
135 previous_direct_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700136 InvokeType type = it.GetMethodInvokeType(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800137 mirror::AbstractMethod* method =
138 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700139 if (method == NULL) {
140 DCHECK(Thread::Current()->IsExceptionPending());
141 // We couldn't resolve the method, but continue regardless.
142 Thread::Current()->ClearException();
143 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700144 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
Jeff Haoee988952013-04-16 14:23:47 -0700145 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags(), allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700146 if (result != kNoFailure) {
147 if (result == kHardFailure) {
148 hard_fail = true;
149 if (error_count > 0) {
150 error += "\n";
151 }
152 error = "Verifier rejected class ";
153 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
154 error += " due to bad method ";
155 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700156 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700157 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800158 }
159 it.Next();
160 }
jeffhao9b0b1882012-10-01 16:51:22 -0700161 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800162 while (it.HasNextVirtualMethod()) {
163 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700164 if (method_idx == previous_virtual_method_idx) {
165 // smali can create dex files with two encoded_methods sharing the same method_idx
166 // http://code.google.com/p/smali/issues/detail?id=119
167 it.Next();
168 continue;
169 }
170 previous_virtual_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700171 InvokeType type = it.GetMethodInvokeType(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800172 mirror::AbstractMethod* method =
173 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700174 if (method == NULL) {
175 DCHECK(Thread::Current()->IsExceptionPending());
176 // We couldn't resolve the method, but continue regardless.
177 Thread::Current()->ClearException();
178 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700179 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
Jeff Haoee988952013-04-16 14:23:47 -0700180 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags(), allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700181 if (result != kNoFailure) {
182 if (result == kHardFailure) {
183 hard_fail = true;
184 if (error_count > 0) {
185 error += "\n";
186 }
187 error = "Verifier rejected class ";
188 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
189 error += " due to bad method ";
190 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700191 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700192 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800193 }
194 it.Next();
195 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700196 if (error_count == 0) {
197 return kNoFailure;
198 } else {
199 return hard_fail ? kHardFailure : kSoftFailure;
200 }
jeffhaof56197c2012-03-05 18:01:54 -0800201}
202
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800203MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
204 const DexFile* dex_file,
205 mirror::DexCache* dex_cache,
206 mirror::ClassLoader* class_loader,
207 uint32_t class_def_idx,
208 const DexFile::CodeItem* code_item,
209 mirror::AbstractMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700210 uint32_t method_access_flags,
211 bool allow_soft_failures) {
Ian Rogersc8982582012-09-07 16:53:25 -0700212 MethodVerifier::FailureKind result = kNoFailure;
213 uint64_t start_ns = NanoTime();
214
Ian Rogersad0b3a32012-04-16 14:50:24 -0700215 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item, method_idx,
Jeff Haoee988952013-04-16 14:23:47 -0700216 method, method_access_flags, true, allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700217 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700218 // Verification completed, however failures may be pending that didn't cause the verification
219 // to hard fail.
Ian Rogerse551e952012-06-03 22:59:14 -0700220 CHECK(!verifier.have_pending_hard_failure_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700221 if (verifier.failures_.size() != 0) {
222 verifier.DumpFailures(LOG(INFO) << "Soft verification failures in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700223 << PrettyMethod(method_idx, *dex_file) << "\n");
Ian Rogersc8982582012-09-07 16:53:25 -0700224 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800225 }
226 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700227 // Bad method data.
228 CHECK_NE(verifier.failures_.size(), 0U);
229 CHECK(verifier.have_pending_hard_failure_);
230 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700231 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800232 if (gDebugVerify) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700233 std::cout << "\n" << verifier.info_messages_.str();
jeffhaof56197c2012-03-05 18:01:54 -0800234 verifier.Dump(std::cout);
235 }
Ian Rogersc8982582012-09-07 16:53:25 -0700236 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800237 }
Ian Rogersc8982582012-09-07 16:53:25 -0700238 uint64_t duration_ns = NanoTime() - start_ns;
239 if (duration_ns > MsToNs(100)) {
240 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
241 << " took " << PrettyDuration(duration_ns);
242 }
243 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800244}
245
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800246void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247 const DexFile* dex_file, mirror::DexCache* dex_cache,
248 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
249 const DexFile::CodeItem* code_item,
250 mirror::AbstractMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800251 uint32_t method_access_flags) {
252 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item,
Jeff Haoee988952013-04-16 14:23:47 -0700253 dex_method_idx, method, method_access_flags, true, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700254 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800255 verifier.DumpFailures(os);
256 os << verifier.info_messages_.str();
257 verifier.Dump(os);
258}
259
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800260MethodVerifier::MethodVerifier(const DexFile* dex_file, mirror::DexCache* dex_cache,
261 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
262 const DexFile::CodeItem* code_item,
263 uint32_t dex_method_idx, mirror::AbstractMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700264 uint32_t method_access_flags, bool can_load_classes,
265 bool allow_soft_failures)
Elliott Hughes80537bb2013-01-04 16:37:26 -0800266 : reg_types_(can_load_classes),
267 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800268 dex_method_idx_(dex_method_idx),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700269 foo_method_(method),
270 method_access_flags_(method_access_flags),
jeffhaof56197c2012-03-05 18:01:54 -0800271 dex_file_(dex_file),
272 dex_cache_(dex_cache),
273 class_loader_(class_loader),
274 class_def_idx_(class_def_idx),
275 code_item_(code_item),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700276 interesting_dex_pc_(-1),
277 monitor_enter_dex_pcs_(NULL),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700278 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700279 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800280 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800281 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700282 can_load_classes_(can_load_classes),
283 allow_soft_failures_(allow_soft_failures) {
jeffhaof56197c2012-03-05 18:01:54 -0800284}
285
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800286void MethodVerifier::FindLocksAtDexPc(mirror::AbstractMethod* m, uint32_t dex_pc,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800287 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700288 MethodHelper mh(m);
289 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
290 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Jeff Haoee988952013-04-16 14:23:47 -0700291 m, m->GetAccessFlags(), false, true);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700292 verifier.interesting_dex_pc_ = dex_pc;
293 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
294 verifier.FindLocksAtDexPc();
295}
296
297void MethodVerifier::FindLocksAtDexPc() {
298 CHECK(monitor_enter_dex_pcs_ != NULL);
299 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
300
301 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
302 // verification. In practice, the phase we want relies on data structures set up by all the
303 // earlier passes, so we just run the full method verification and bail out early when we've
304 // got what we wanted.
305 Verify();
306}
307
Ian Rogersad0b3a32012-04-16 14:50:24 -0700308bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700309 // If there aren't any instructions, make sure that's expected, then exit successfully.
310 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700311 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700312 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700313 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700314 } else {
315 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700316 }
jeffhaobdb76512011-09-07 11:43:16 -0700317 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700318 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
319 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700320 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
321 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700322 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700323 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700324 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800325 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700326 // Run through the instructions and see if the width checks out.
327 bool result = ComputeWidthsAndCountOps();
328 // Flag instructions guarded by a "try" block and check exception handlers.
329 result = result && ScanTryCatchBlocks();
330 // Perform static instruction verification.
331 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700332 // Perform code-flow analysis and return.
333 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700334}
335
Ian Rogers776ac1f2012-04-13 23:36:36 -0700336std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700337 switch (error) {
338 case VERIFY_ERROR_NO_CLASS:
339 case VERIFY_ERROR_NO_FIELD:
340 case VERIFY_ERROR_NO_METHOD:
341 case VERIFY_ERROR_ACCESS_CLASS:
342 case VERIFY_ERROR_ACCESS_FIELD:
343 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700344 case VERIFY_ERROR_INSTANTIATION:
345 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800346 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700347 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
348 // class change and instantiation errors into soft verification errors so that we re-verify
349 // at runtime. We may fail to find or to agree on access because of not yet available class
350 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
351 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
352 // paths" that dynamically perform the verification and cause the behavior to be that akin
353 // to an interpreter.
354 error = VERIFY_ERROR_BAD_CLASS_SOFT;
355 } else {
356 have_pending_runtime_throw_failure_ = true;
357 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700358 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700359 // Indication that verification should be retried at runtime.
360 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700361 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700362 have_pending_hard_failure_ = true;
363 }
364 break;
jeffhaod5347e02012-03-22 17:25:05 -0700365 // Hard verification failures at compile time will still fail at runtime, so the class is
366 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700367 case VERIFY_ERROR_BAD_CLASS_HARD: {
368 if (Runtime::Current()->IsCompiler()) {
Ian Rogers1212a022013-03-04 10:48:41 -0800369 CompilerDriver::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800370 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800371 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700372 have_pending_hard_failure_ = true;
373 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800374 }
375 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700376 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800377 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700378 work_insn_idx_));
379 std::ostringstream* failure_message = new std::ostringstream(location);
380 failure_messages_.push_back(failure_message);
381 return *failure_message;
382}
383
384void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
385 size_t failure_num = failure_messages_.size();
386 DCHECK_NE(failure_num, 0U);
387 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
388 prepend += last_fail_message->str();
389 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
390 delete last_fail_message;
391}
392
393void MethodVerifier::AppendToLastFailMessage(std::string append) {
394 size_t failure_num = failure_messages_.size();
395 DCHECK_NE(failure_num, 0U);
396 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
397 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800398}
399
Ian Rogers776ac1f2012-04-13 23:36:36 -0700400bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700401 const uint16_t* insns = code_item_->insns_;
402 size_t insns_size = code_item_->insns_size_in_code_units_;
403 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700404 size_t new_instance_count = 0;
405 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700406 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700407
Ian Rogersd81871c2011-10-03 13:57:23 -0700408 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700409 Instruction::Code opcode = inst->Opcode();
410 if (opcode == Instruction::NEW_INSTANCE) {
411 new_instance_count++;
412 } else if (opcode == Instruction::MONITOR_ENTER) {
413 monitor_enter_count++;
414 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700415 size_t inst_size = inst->SizeInCodeUnits();
416 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
417 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700418 inst = inst->Next();
419 }
420
Ian Rogersd81871c2011-10-03 13:57:23 -0700421 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700422 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
423 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700424 return false;
425 }
426
Ian Rogersd81871c2011-10-03 13:57:23 -0700427 new_instance_count_ = new_instance_count;
428 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700429 return true;
430}
431
Ian Rogers776ac1f2012-04-13 23:36:36 -0700432bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700433 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700434 if (tries_size == 0) {
435 return true;
436 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700437 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700438 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700439
440 for (uint32_t idx = 0; idx < tries_size; idx++) {
441 const DexFile::TryItem* try_item = &tries[idx];
442 uint32_t start = try_item->start_addr_;
443 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700444 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700445 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
446 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700447 return false;
448 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700449 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700450 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700451 return false;
452 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700453 for (uint32_t dex_pc = start; dex_pc < end;
454 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
455 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700456 }
457 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800458 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700459 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700460 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700461 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700462 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700463 CatchHandlerIterator iterator(handlers_ptr);
464 for (; iterator.HasNext(); iterator.Next()) {
465 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700466 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700467 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700468 return false;
469 }
jeffhao60f83e32012-02-13 17:16:30 -0800470 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
471 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700472 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700473 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800474 return false;
475 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700476 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700477 // Ensure exception types are resolved so that they don't need resolution to be delivered,
478 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700479 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800480 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
481 iterator.GetHandlerTypeIndex(),
482 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700483 if (exception_type == NULL) {
484 DCHECK(Thread::Current()->IsExceptionPending());
485 Thread::Current()->ClearException();
486 }
487 }
jeffhaobdb76512011-09-07 11:43:16 -0700488 }
Ian Rogers0571d352011-11-03 19:51:38 -0700489 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700490 }
jeffhaobdb76512011-09-07 11:43:16 -0700491 return true;
492}
493
Ian Rogers776ac1f2012-04-13 23:36:36 -0700494bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700495 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700496
Ian Rogers0c7abda2012-09-19 13:33:42 -0700497 /* 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 -0700498 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700499 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700500
501 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700502 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700503 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700504 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700505 return false;
506 }
507 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700508 // All invoke points are marked as "Throw" points already.
509 // We are relying on this to also count all the invokes as interesting.
Ian Rogersd81871c2011-10-03 13:57:23 -0700510 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700511 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700512 }
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 */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700923 reg_table_.Init(kTrackCompilerInterestPoints, insn_flags_.get(), insns_size, registers_size, this);
924
jeffhaobdb76512011-09-07 11:43:16 -0700925
Ian Rogersd81871c2011-10-03 13:57:23 -0700926 work_line_.reset(new RegisterLine(registers_size, this));
927 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -0700928
Ian Rogersd81871c2011-10-03 13:57:23 -0700929 /* Initialize register types of method arguments. */
930 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700931 DCHECK_NE(failures_.size(), 0U);
932 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800933 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700934 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -0700935 return false;
936 }
937 /* Perform code flow verification. */
938 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700939 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700940 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700941 }
942
Ian Rogers1212a022013-03-04 10:48:41 -0800943 CompilerDriver::MethodReference ref(dex_file_, dex_method_idx_);
TDYa127b2eb5c12012-05-24 15:52:10 -0700944
TDYa127b2eb5c12012-05-24 15:52:10 -0700945
Ian Rogersd81871c2011-10-03 13:57:23 -0700946 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -0800947 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
948 if (map.get() == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700949 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700950 return false; // Not a real failure, but a failure to encode
951 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700952#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800953 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -0700954#endif
Ian Rogers0c7abda2012-09-19 13:33:42 -0700955 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
956 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
Logan Chiendd361c92012-04-10 23:40:37 +0800957
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700958 MethodVerifier::PcToConreteMethod* pc_to_conrete_method = GenerateDevirtMap();
959 if(pc_to_conrete_method != NULL ) {
960 SetDevirtMap(ref, pc_to_conrete_method);
961 }
jeffhaobdb76512011-09-07 11:43:16 -0700962 return true;
963}
964
Ian Rogersad0b3a32012-04-16 14:50:24 -0700965std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
966 DCHECK_EQ(failures_.size(), failure_messages_.size());
967 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700968 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -0700969 }
970 return os;
971}
972
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700973extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700974 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700975 v->Dump(std::cerr);
976}
977
Ian Rogers776ac1f2012-04-13 23:36:36 -0700978void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -0800979 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700980 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -0700981 return;
jeffhaobdb76512011-09-07 11:43:16 -0700982 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800983 {
984 os << "Register Types:\n";
985 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
986 std::ostream indent_os(&indent_filter);
987 reg_types_.Dump(indent_os);
988 }
Ian Rogersb4903572012-10-11 11:52:56 -0700989 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800990 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
991 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -0700992 const Instruction* inst = Instruction::At(code_item_->insns_);
993 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
994 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700995 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
996 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800997 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -0700998 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800999 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001000 const bool kDumpHexOfInstruction = false;
1001 if (kDumpHexOfInstruction) {
1002 indent_os << inst->DumpHex(5) << " ";
1003 }
1004 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001005 inst = inst->Next();
1006 }
jeffhaobdb76512011-09-07 11:43:16 -07001007}
1008
Ian Rogersd81871c2011-10-03 13:57:23 -07001009static bool IsPrimitiveDescriptor(char descriptor) {
1010 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001011 case 'I':
1012 case 'C':
1013 case 'S':
1014 case 'B':
1015 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001016 case 'F':
1017 case 'D':
1018 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001019 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001020 default:
1021 return false;
1022 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001023}
1024
Ian Rogers776ac1f2012-04-13 23:36:36 -07001025bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001026 RegisterLine* reg_line = reg_table_.GetLine(0);
1027 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1028 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001029
Ian Rogersd81871c2011-10-03 13:57:23 -07001030 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1031 //Include the "this" pointer.
1032 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001033 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001034 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1035 // argument as uninitialized. This restricts field access until the superclass constructor is
1036 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001037 const RegType& declaring_class = GetDeclaringClass();
1038 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 reg_line->SetRegisterType(arg_start + cur_arg,
1040 reg_types_.UninitializedThisArgument(declaring_class));
1041 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001042 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001043 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001044 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001045 }
1046
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001047 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001048 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001049 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001050
1051 for (; iterator.HasNext(); iterator.Next()) {
1052 const char* descriptor = iterator.GetDescriptor();
1053 if (descriptor == NULL) {
1054 LOG(FATAL) << "Null descriptor";
1055 }
1056 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001057 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1058 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001059 return false;
1060 }
1061 switch (descriptor[0]) {
1062 case 'L':
1063 case '[':
1064 // We assume that reference arguments are initialized. The only way it could be otherwise
1065 // (assuming the caller was verified) is if the current method is <init>, but in that case
1066 // it's effectively considered initialized the instant we reach here (in the sense that we
1067 // can return without doing anything or call virtual methods).
1068 {
Ian Rogersb4903572012-10-11 11:52:56 -07001069 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001070 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001071 }
1072 break;
1073 case 'Z':
1074 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1075 break;
1076 case 'C':
1077 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1078 break;
1079 case 'B':
1080 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1081 break;
1082 case 'I':
1083 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1084 break;
1085 case 'S':
1086 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1087 break;
1088 case 'F':
1089 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1090 break;
1091 case 'J':
1092 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001093 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1094 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1095 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001096 cur_arg++;
1097 break;
1098 }
1099 default:
jeffhaod5347e02012-03-22 17:25:05 -07001100 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001101 return false;
1102 }
1103 cur_arg++;
1104 }
1105 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001106 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001107 return false;
1108 }
1109 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1110 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1111 // format. Only major difference from the method argument format is that 'V' is supported.
1112 bool result;
1113 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1114 result = descriptor[1] == '\0';
1115 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1116 size_t i = 0;
1117 do {
1118 i++;
1119 } while (descriptor[i] == '['); // process leading [
1120 if (descriptor[i] == 'L') { // object array
1121 do {
1122 i++; // find closing ;
1123 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1124 result = descriptor[i] == ';';
1125 } else { // primitive array
1126 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1127 }
1128 } else if (descriptor[0] == 'L') {
1129 // could be more thorough here, but shouldn't be required
1130 size_t i = 0;
1131 do {
1132 i++;
1133 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1134 result = descriptor[i] == ';';
1135 } else {
1136 result = false;
1137 }
1138 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001139 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1140 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001141 }
1142 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001143}
1144
Ian Rogers776ac1f2012-04-13 23:36:36 -07001145bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001146 const uint16_t* insns = code_item_->insns_;
1147 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001148
jeffhaobdb76512011-09-07 11:43:16 -07001149 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001150 insn_flags_[0].SetChanged();
1151 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001152
jeffhaobdb76512011-09-07 11:43:16 -07001153 /* Continue until no instructions are marked "changed". */
1154 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001155 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1156 uint32_t insn_idx = start_guess;
1157 for (; insn_idx < insns_size; insn_idx++) {
1158 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001159 break;
1160 }
jeffhaobdb76512011-09-07 11:43:16 -07001161 if (insn_idx == insns_size) {
1162 if (start_guess != 0) {
1163 /* try again, starting from the top */
1164 start_guess = 0;
1165 continue;
1166 } else {
1167 /* all flags are clear */
1168 break;
1169 }
1170 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001171 // We carry the working set of registers from instruction to instruction. If this address can
1172 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1173 // "changed" flags, we need to load the set of registers from the table.
1174 // Because we always prefer to continue on to the next instruction, we should never have a
1175 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1176 // target.
1177 work_insn_idx_ = insn_idx;
1178 if (insn_flags_[insn_idx].IsBranchTarget()) {
1179 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001180 } else {
1181#ifndef NDEBUG
1182 /*
1183 * Sanity check: retrieve the stored register line (assuming
1184 * a full table) and make sure it actually matches.
1185 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001186 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1187 if (register_line != NULL) {
1188 if (work_line_->CompareLine(register_line) != 0) {
1189 Dump(std::cout);
1190 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001191 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001192 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1193 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001194 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001195 }
jeffhaobdb76512011-09-07 11:43:16 -07001196 }
1197#endif
1198 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001199 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001200 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001201 prepend += " failed to verify: ";
1202 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001203 return false;
1204 }
jeffhaobdb76512011-09-07 11:43:16 -07001205 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001206 insn_flags_[insn_idx].SetVisited();
1207 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001208 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001209
Ian Rogers1c849e52012-06-28 14:00:33 -07001210 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001211 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001212 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001213 * (besides the wasted space), but it indicates a flaw somewhere
1214 * down the line, possibly in the verifier.
1215 *
1216 * If we've substituted "always throw" instructions into the stream,
1217 * we are almost certainly going to have some dead code.
1218 */
1219 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001220 uint32_t insn_idx = 0;
1221 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001222 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001223 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001224 * may or may not be preceded by a padding NOP (for alignment).
1225 */
1226 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1227 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1228 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001229 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001230 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1231 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1232 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001233 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001234 }
1235
Ian Rogersd81871c2011-10-03 13:57:23 -07001236 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001237 if (dead_start < 0)
1238 dead_start = insn_idx;
1239 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001240 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001241 dead_start = -1;
1242 }
1243 }
1244 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001245 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001246 }
1247 }
jeffhaobdb76512011-09-07 11:43:16 -07001248 return true;
1249}
1250
Ian Rogers776ac1f2012-04-13 23:36:36 -07001251bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001252 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1253 // We want the state _before_ the instruction, for the case where the dex pc we're
1254 // interested in is itself a monitor-enter instruction (which is a likely place
1255 // for a thread to be suspended).
1256 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001257 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001258 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1259 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1260 }
1261 }
1262
jeffhaobdb76512011-09-07 11:43:16 -07001263 /*
1264 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001265 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001266 * control to another statement:
1267 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001268 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001269 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001270 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001271 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001272 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001273 * throw an exception that is handled by an encompassing "try"
1274 * block.
1275 *
1276 * We can also return, in which case there is no successor instruction
1277 * from this point.
1278 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001279 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001280 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001281 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1282 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001283 DecodedInstruction dec_insn(inst);
Ian Rogersa75a0132012-09-28 11:41:42 -07001284 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001285
jeffhaobdb76512011-09-07 11:43:16 -07001286 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001287 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001288 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001289 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001290 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1291 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001292 }
jeffhaobdb76512011-09-07 11:43:16 -07001293
1294 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001295 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001296 * can throw an exception, we will copy/merge this into the "catch"
1297 * address rather than work_line, because we don't want the result
1298 * from the "successful" code path (e.g. a check-cast that "improves"
1299 * a type) to be visible to the exception handler.
1300 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001301 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001302 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001303 } else {
1304#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001305 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001306#endif
1307 }
1308
Dragos Sbirleaef7162e2013-05-28 14:14:12 -07001309 // We need to ensure the work line is consistent while performing validation. When we spot a
1310 // peephole pattern we compute a new line for either the fallthrough instruction or the
1311 // branch target.
1312 UniquePtr<RegisterLine> branch_line;
1313 UniquePtr<RegisterLine> fallthrough_line;
1314
Elliott Hughesadb8c672012-03-06 16:49:32 -08001315 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07001316 case Instruction::NOP:
1317 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001318 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001319 * a signature that looks like a NOP; if we see one of these in
1320 * the course of executing code then we have a problem.
1321 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001322 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001323 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001324 }
1325 break;
1326
1327 case Instruction::MOVE:
1328 case Instruction::MOVE_FROM16:
1329 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001330 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001331 break;
1332 case Instruction::MOVE_WIDE:
1333 case Instruction::MOVE_WIDE_FROM16:
1334 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001335 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07001336 break;
1337 case Instruction::MOVE_OBJECT:
1338 case Instruction::MOVE_OBJECT_FROM16:
1339 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001340 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001341 break;
1342
1343 /*
1344 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001345 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001346 * might want to hold the result in an actual CPU register, so the
1347 * Dalvik spec requires that these only appear immediately after an
1348 * invoke or filled-new-array.
1349 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001350 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001351 * redundant with the reset done below, but it can make the debug info
1352 * easier to read in some cases.)
1353 */
1354 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001355 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07001356 break;
1357 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001358 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001359 break;
1360 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001361 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07001362 break;
1363
Ian Rogersd81871c2011-10-03 13:57:23 -07001364 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001365 /*
jeffhao60f83e32012-02-13 17:16:30 -08001366 * This statement can only appear as the first instruction in an exception handler. We verify
1367 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001368 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001369 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08001370 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001371 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001372 }
jeffhaobdb76512011-09-07 11:43:16 -07001373 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001374 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1375 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001376 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001377 }
jeffhaobdb76512011-09-07 11:43:16 -07001378 }
1379 break;
1380 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001381 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001382 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001383 const RegType& return_type = GetMethodReturnType();
1384 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001385 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001386 } else {
1387 // Compilers may generate synthetic functions that write byte values into boolean fields.
1388 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001389 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001390 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1391 ((return_type.IsBoolean() || return_type.IsByte() ||
1392 return_type.IsShort() || return_type.IsChar()) &&
1393 src_type.IsInteger()));
1394 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001395 bool success =
1396 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
1397 if (!success) {
1398 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001399 }
jeffhaobdb76512011-09-07 11:43:16 -07001400 }
1401 }
1402 break;
1403 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001404 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001405 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001406 const RegType& return_type = GetMethodReturnType();
1407 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001408 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001409 } else {
1410 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001411 bool success = work_line_->VerifyRegisterType(dec_insn.vA, return_type);
1412 if (!success) {
1413 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001414 }
jeffhaobdb76512011-09-07 11:43:16 -07001415 }
1416 }
1417 break;
1418 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001419 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001420 const RegType& return_type = GetMethodReturnType();
1421 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001422 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001423 } else {
1424 /* return_type is the *expected* return type, not register value */
1425 DCHECK(!return_type.IsZero());
1426 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08001427 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07001428 // Disallow returning uninitialized values and verify that the reference in vAA is an
1429 // instance of the "return_type"
1430 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001431 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001432 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001433 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1434 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001435 }
1436 }
1437 }
1438 break;
1439
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001440 /* could be boolean, int, float, or a null reference */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001441 case Instruction::CONST_4:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001442 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001443 reg_types_.FromCat1Const(static_cast<int32_t>(dec_insn.vB << 28) >> 28, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001444 break;
jeffhaobdb76512011-09-07 11:43:16 -07001445 case Instruction::CONST_16:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001446 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001447 reg_types_.FromCat1Const(static_cast<int16_t>(dec_insn.vB), true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001448 break;
jeffhaobdb76512011-09-07 11:43:16 -07001449 case Instruction::CONST:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001450 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const(dec_insn.vB, true));
jeffhaobdb76512011-09-07 11:43:16 -07001451 break;
1452 case Instruction::CONST_HIGH16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001453 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001454 reg_types_.FromCat1Const(dec_insn.vB << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001455 break;
jeffhaobdb76512011-09-07 11:43:16 -07001456 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001457 case Instruction::CONST_WIDE_16: {
1458 int64_t val = static_cast<int16_t>(dec_insn.vB);
1459 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1460 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1461 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001462 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001463 }
1464 case Instruction::CONST_WIDE_32: {
1465 int64_t val = static_cast<int32_t>(dec_insn.vB);
1466 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1467 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1468 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1469 break;
1470 }
1471 case Instruction::CONST_WIDE: {
1472 int64_t val = dec_insn.vB_wide;
1473 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1474 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1475 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1476 break;
1477 }
1478 case Instruction::CONST_WIDE_HIGH16: {
1479 int64_t val = static_cast<uint64_t>(dec_insn.vB) << 48;
1480 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1481 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1482 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1483 break;
1484 }
jeffhaobdb76512011-09-07 11:43:16 -07001485 case Instruction::CONST_STRING:
1486 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001487 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001488 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001489 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001490 // Get type from instruction if unresolved then we need an access check
1491 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08001492 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001493 // Register holds class, ie its type is class, on error it will hold Conflict.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001494 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogersb4903572012-10-11 11:52:56 -07001495 res_type.IsConflict() ? res_type
1496 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001497 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001498 }
jeffhaobdb76512011-09-07 11:43:16 -07001499 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001500 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001501 break;
1502 case Instruction::MONITOR_EXIT:
1503 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001504 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001505 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001506 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001507 * to the need to handle asynchronous exceptions, a now-deprecated
1508 * feature that Dalvik doesn't support.)
1509 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001510 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001511 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001512 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001513 * structured locking checks are working, the former would have
1514 * failed on the -enter instruction, and the latter is impossible.
1515 *
1516 * This is fortunate, because issue 3221411 prevents us from
1517 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001518 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001519 * some catch blocks (which will show up as "dead" code when
1520 * we skip them here); if we can't, then the code path could be
1521 * "live" so we still need to check it.
1522 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001523 opcode_flags &= ~Instruction::kThrow;
1524 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001525 break;
1526
Ian Rogers28ad40d2011-10-27 15:19:26 -07001527 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001528 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001529 /*
1530 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1531 * could be a "upcast" -- not expected, so we don't try to address it.)
1532 *
1533 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001534 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001535 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001536 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001537 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001538 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001539 if (res_type.IsConflict()) {
1540 DCHECK_NE(failures_.size(), 0U);
1541 if (!is_checkcast) {
1542 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
1543 }
1544 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001545 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001546 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1547 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001548 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001549 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001550 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001551 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001552 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07001553 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001554 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001555 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001556 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001557 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001558 }
jeffhaobdb76512011-09-07 11:43:16 -07001559 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001560 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001561 }
1562 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001563 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001564 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001565 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001566 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001567 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001568 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001569 }
1570 }
1571 break;
1572 }
1573 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001574 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001575 if (res_type.IsConflict()) {
1576 DCHECK_NE(failures_.size(), 0U);
1577 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001578 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001579 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1580 // can't create an instance of an interface or abstract class */
1581 if (!res_type.IsInstantiableTypes()) {
1582 Fail(VERIFY_ERROR_INSTANTIATION)
1583 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001584 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001585 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001586 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1587 // Any registers holding previous allocations from this address that have not yet been
1588 // initialized must be marked invalid.
1589 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1590 // add the new uninitialized reference to the register state
1591 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001592 break;
1593 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001594 case Instruction::NEW_ARRAY:
1595 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001596 break;
1597 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08001598 VerifyNewArray(dec_insn, true, false);
1599 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001600 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001601 case Instruction::FILLED_NEW_ARRAY_RANGE:
1602 VerifyNewArray(dec_insn, true, true);
1603 just_set_result = true; // Filled new array range sets result register
1604 break;
jeffhaobdb76512011-09-07 11:43:16 -07001605 case Instruction::CMPL_FLOAT:
1606 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001607 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001608 break;
1609 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001610 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001611 break;
1612 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001613 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001614 break;
1615 case Instruction::CMPL_DOUBLE:
1616 case Instruction::CMPG_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001617 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vB, reg_types_.DoubleLo(),
1618 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001619 break;
1620 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001621 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vC, reg_types_.DoubleLo(),
1622 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001623 break;
1624 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001625 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001626 break;
1627 case Instruction::CMP_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001628 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vB, reg_types_.LongLo(),
1629 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001630 break;
1631 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001632 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vC, reg_types_.LongLo(),
1633 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001634 break;
1635 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001636 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001637 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001638 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001639 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersb4903572012-10-11 11:52:56 -07001640 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001641 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001642 }
1643 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001644 }
jeffhaobdb76512011-09-07 11:43:16 -07001645 case Instruction::GOTO:
1646 case Instruction::GOTO_16:
1647 case Instruction::GOTO_32:
1648 /* no effect on or use of registers */
1649 break;
1650
1651 case Instruction::PACKED_SWITCH:
1652 case Instruction::SPARSE_SWITCH:
1653 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001654 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001655 break;
1656
Ian Rogersd81871c2011-10-03 13:57:23 -07001657 case Instruction::FILL_ARRAY_DATA: {
1658 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001659 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08001660 /* array_type can be null if the reg type is Zero */
1661 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001662 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001663 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001664 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001665 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1666 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001667 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001668 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1669 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001670 } else {
jeffhao457cc512012-02-02 16:55:13 -08001671 // Now verify if the element width in the table matches the element width declared in
1672 // the array
1673 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1674 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001675 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001676 } else {
1677 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1678 // Since we don't compress the data in Dex, expect to see equal width of data stored
1679 // in the table and expected from the array class.
1680 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001681 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1682 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001683 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001684 }
1685 }
jeffhaobdb76512011-09-07 11:43:16 -07001686 }
1687 }
1688 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001689 }
jeffhaobdb76512011-09-07 11:43:16 -07001690 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001691 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001692 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1693 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001694 bool mismatch = false;
1695 if (reg_type1.IsZero()) { // zero then integral or reference expected
1696 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1697 } else if (reg_type1.IsReferenceTypes()) { // both references?
1698 mismatch = !reg_type2.IsReferenceTypes();
1699 } else { // both integral?
1700 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1701 }
1702 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001703 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1704 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001705 }
1706 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001707 }
jeffhaobdb76512011-09-07 11:43:16 -07001708 case Instruction::IF_LT:
1709 case Instruction::IF_GE:
1710 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001711 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001712 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1713 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001714 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001715 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1716 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001717 }
1718 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001719 }
jeffhaobdb76512011-09-07 11:43:16 -07001720 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001721 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001722 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001723 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001725 }
Dragos Sbirleaef7162e2013-05-28 14:14:12 -07001726
1727 // Find previous instruction - its existence is a precondition to peephole optimization.
1728 uint32_t prev_idx = 0;
1729 if (0 != work_insn_idx_) {
1730 prev_idx = work_insn_idx_ - 1;
1731 while(0 != prev_idx && !insn_flags_[prev_idx].IsOpcode()) {
1732 prev_idx--;
1733 }
1734 CHECK(true == insn_flags_[prev_idx].IsOpcode());
1735 } else {
1736 break;
1737 }
1738
1739 const Instruction* prev_inst = Instruction::At(code_item_->insns_+prev_idx);
1740
1741 /* Check for peep-hole pattern of:
1742 * ...;
1743 * instance-of vX, vO, T;
1744 * ifXXX vX, b ;
1745 * ...;
1746 * b: INST;
1747 * ...;
1748 * and sharpen the type for either the fall-through or the branch case.
1749 */
1750 if (!CurrentInsnFlags()->IsBranchTarget()) {
1751 DecodedInstruction prev_dec_insn(prev_inst);
1752 if ((Instruction::INSTANCE_OF == prev_inst->Opcode())
1753 && (dec_insn.vA == prev_dec_insn.vA)) {
1754 // Check that the we are not attempting conversion to interface types,
1755 // which is not done because of the multiple inheritance implications.
1756 const RegType& cast_type =
1757 ResolveClassAndCheckAccess(prev_dec_insn.vC);
1758
1759 if(false == cast_type.GetClass()->IsInterface()) {
1760 if (dec_insn.opcode == Instruction::IF_EQZ) {
1761 fallthrough_line.reset(new RegisterLine(code_item_->registers_size_, this));
1762 fallthrough_line->CopyFromLine(work_line_.get());
1763 fallthrough_line->SetRegisterType(prev_dec_insn.vB , cast_type);
1764 } else {
1765 branch_line.reset(new RegisterLine(code_item_->registers_size_, this));
1766 branch_line->CopyFromLine(work_line_.get());
1767 branch_line->SetRegisterType(prev_dec_insn.vB , cast_type);
1768 }
1769 }
1770 }
1771 }
1772
jeffhaobdb76512011-09-07 11:43:16 -07001773 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001774 }
jeffhaobdb76512011-09-07 11:43:16 -07001775 case Instruction::IF_LTZ:
1776 case Instruction::IF_GEZ:
1777 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001778 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001779 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001780 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001781 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1782 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001783 }
jeffhaobdb76512011-09-07 11:43:16 -07001784 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001785 }
jeffhaobdb76512011-09-07 11:43:16 -07001786 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
1788 break;
jeffhaobdb76512011-09-07 11:43:16 -07001789 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001790 VerifyAGet(dec_insn, reg_types_.Byte(), true);
1791 break;
jeffhaobdb76512011-09-07 11:43:16 -07001792 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07001793 VerifyAGet(dec_insn, reg_types_.Char(), true);
1794 break;
jeffhaobdb76512011-09-07 11:43:16 -07001795 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001796 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001797 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001798 case Instruction::AGET:
1799 VerifyAGet(dec_insn, reg_types_.Integer(), true);
1800 break;
jeffhaobdb76512011-09-07 11:43:16 -07001801 case Instruction::AGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001802 VerifyAGet(dec_insn, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001803 break;
1804 case Instruction::AGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001805 VerifyAGet(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001806 break;
1807
Ian Rogersd81871c2011-10-03 13:57:23 -07001808 case Instruction::APUT_BOOLEAN:
1809 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
1810 break;
1811 case Instruction::APUT_BYTE:
1812 VerifyAPut(dec_insn, reg_types_.Byte(), true);
1813 break;
1814 case Instruction::APUT_CHAR:
1815 VerifyAPut(dec_insn, reg_types_.Char(), true);
1816 break;
1817 case Instruction::APUT_SHORT:
1818 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001819 break;
1820 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001821 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001822 break;
1823 case Instruction::APUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001824 VerifyAPut(dec_insn, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001825 break;
1826 case Instruction::APUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001827 VerifyAPut(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001828 break;
1829
jeffhaobdb76512011-09-07 11:43:16 -07001830 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001831 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001832 break;
jeffhaobdb76512011-09-07 11:43:16 -07001833 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001834 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001835 break;
jeffhaobdb76512011-09-07 11:43:16 -07001836 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001837 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001838 break;
jeffhaobdb76512011-09-07 11:43:16 -07001839 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001840 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001841 break;
1842 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001843 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001844 break;
1845 case Instruction::IGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001846 VerifyISGet(dec_insn, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001847 break;
1848 case Instruction::IGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001849 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001850 break;
jeffhaobdb76512011-09-07 11:43:16 -07001851
Ian Rogersd81871c2011-10-03 13:57:23 -07001852 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001853 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001854 break;
1855 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001856 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001857 break;
1858 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001859 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001860 break;
1861 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001862 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001863 break;
1864 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001865 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001866 break;
1867 case Instruction::IPUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001868 VerifyISPut(dec_insn, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001869 break;
jeffhaobdb76512011-09-07 11:43:16 -07001870 case Instruction::IPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001871 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001872 break;
1873
jeffhaobdb76512011-09-07 11:43:16 -07001874 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001875 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001876 break;
jeffhaobdb76512011-09-07 11:43:16 -07001877 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001878 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001879 break;
jeffhaobdb76512011-09-07 11:43:16 -07001880 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001881 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001882 break;
jeffhaobdb76512011-09-07 11:43:16 -07001883 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001884 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001885 break;
1886 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001887 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001888 break;
1889 case Instruction::SGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001890 VerifyISGet(dec_insn, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001891 break;
1892 case Instruction::SGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001893 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001894 break;
1895
1896 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001897 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001898 break;
1899 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001900 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001901 break;
1902 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001903 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001904 break;
1905 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001906 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001907 break;
1908 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001909 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001910 break;
1911 case Instruction::SPUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001912 VerifyISPut(dec_insn, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001913 break;
1914 case Instruction::SPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001915 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001916 break;
1917
1918 case Instruction::INVOKE_VIRTUAL:
1919 case Instruction::INVOKE_VIRTUAL_RANGE:
1920 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001921 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001922 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
1923 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
1924 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
1925 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001926 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL,
1927 is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001928 const char* descriptor;
1929 if (called_method == NULL) {
1930 uint32_t method_idx = dec_insn.vB;
1931 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1932 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1933 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1934 } else {
1935 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07001936 }
Ian Rogersb4903572012-10-11 11:52:56 -07001937 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001938 if (!return_type.IsLowHalf()) {
1939 work_line_->SetResultRegisterType(return_type);
1940 } else {
1941 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
1942 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001943 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001944 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001945 }
jeffhaobdb76512011-09-07 11:43:16 -07001946 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001947 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001948 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001949 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT,
1950 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07001951 const char* return_type_descriptor;
1952 bool is_constructor;
1953 if (called_method == NULL) {
1954 uint32_t method_idx = dec_insn.vB;
1955 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1956 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
1957 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1958 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1959 } else {
1960 is_constructor = called_method->IsConstructor();
1961 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
1962 }
1963 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07001964 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001965 * Some additional checks when calling a constructor. We know from the invocation arg check
1966 * that the "this" argument is an instance of called_method->klass. Now we further restrict
1967 * that to require that called_method->klass is the same as this->klass or this->super,
1968 * allowing the latter only if the "this" argument is the same as the "this" argument to
1969 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07001970 */
jeffhaob57e9522012-04-26 18:08:21 -07001971 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1972 if (this_type.IsConflict()) // failure.
1973 break;
jeffhaobdb76512011-09-07 11:43:16 -07001974
jeffhaob57e9522012-04-26 18:08:21 -07001975 /* no null refs allowed (?) */
1976 if (this_type.IsZero()) {
1977 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
1978 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07001979 }
jeffhaob57e9522012-04-26 18:08:21 -07001980
1981 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07001982 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
1983 // TODO: re-enable constructor type verification
1984 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07001985 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07001986 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
1987 // break;
1988 // }
jeffhaob57e9522012-04-26 18:08:21 -07001989
1990 /* arg must be an uninitialized reference */
1991 if (!this_type.IsUninitializedTypes()) {
1992 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
1993 << this_type;
1994 break;
1995 }
1996
1997 /*
1998 * Replace the uninitialized reference with an initialized one. We need to do this for all
1999 * registers that have the same object instance in them, not just the "this" register.
2000 */
2001 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002002 }
Ian Rogersb4903572012-10-11 11:52:56 -07002003 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2004 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002005 if (!return_type.IsLowHalf()) {
2006 work_line_->SetResultRegisterType(return_type);
2007 } else {
2008 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2009 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002010 just_set_result = true;
2011 break;
2012 }
2013 case Instruction::INVOKE_STATIC:
2014 case Instruction::INVOKE_STATIC_RANGE: {
2015 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002016 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002017 const char* descriptor;
2018 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002019 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002020 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2021 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002022 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002023 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002024 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002025 }
Ian Rogersb4903572012-10-11 11:52:56 -07002026 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002027 if (!return_type.IsLowHalf()) {
2028 work_line_->SetResultRegisterType(return_type);
2029 } else {
2030 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2031 }
jeffhaobdb76512011-09-07 11:43:16 -07002032 just_set_result = true;
2033 }
2034 break;
jeffhaobdb76512011-09-07 11:43:16 -07002035 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002036 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002037 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002038 mirror::AbstractMethod* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002039 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002040 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002041 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2042 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2043 << PrettyMethod(abs_method) << "'";
2044 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002045 }
Ian Rogers0d604842012-04-16 14:50:24 -07002046 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002047 /* Get the type of the "this" arg, which should either be a sub-interface of called
2048 * interface or Object (see comments in RegType::JoinClass).
2049 */
2050 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2051 if (this_type.IsZero()) {
2052 /* null pointer always passes (and always fails at runtime) */
2053 } else {
2054 if (this_type.IsUninitializedTypes()) {
2055 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2056 << this_type;
2057 break;
2058 }
2059 // In the past we have tried to assert that "called_interface" is assignable
2060 // from "this_type.GetClass()", however, as we do an imprecise Join
2061 // (RegType::JoinClass) we don't have full information on what interfaces are
2062 // implemented by "this_type". For example, two classes may implement the same
2063 // interfaces and have a common parent that doesn't implement the interface. The
2064 // join will set "this_type" to the parent class and a test that this implements
2065 // the interface will incorrectly fail.
2066 }
2067 /*
2068 * We don't have an object instance, so we can't find the concrete method. However, all of
2069 * the type information is in the abstract method, so we're good.
2070 */
2071 const char* descriptor;
2072 if (abs_method == NULL) {
2073 uint32_t method_idx = dec_insn.vB;
2074 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2075 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2076 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2077 } else {
2078 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2079 }
Ian Rogersb4903572012-10-11 11:52:56 -07002080 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002081 if (!return_type.IsLowHalf()) {
2082 work_line_->SetResultRegisterType(return_type);
2083 } else {
2084 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2085 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002086 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002087 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002088 }
jeffhaobdb76512011-09-07 11:43:16 -07002089 case Instruction::NEG_INT:
2090 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002091 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002092 break;
2093 case Instruction::NEG_LONG:
2094 case Instruction::NOT_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002095 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2096 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002097 break;
2098 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002099 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002100 break;
2101 case Instruction::NEG_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002102 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2103 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002104 break;
2105 case Instruction::INT_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002106 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2107 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002108 break;
2109 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002110 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002111 break;
2112 case Instruction::INT_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002113 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2114 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002115 break;
2116 case Instruction::LONG_TO_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002117 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Integer(),
2118 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002119 break;
2120 case Instruction::LONG_TO_FLOAT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002121 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Float(),
2122 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002123 break;
2124 case Instruction::LONG_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002125 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2126 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002127 break;
2128 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002129 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002130 break;
2131 case Instruction::FLOAT_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002132 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2133 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002134 break;
2135 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002136 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2137 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002138 break;
2139 case Instruction::DOUBLE_TO_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002140 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Integer(),
2141 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002142 break;
2143 case Instruction::DOUBLE_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002144 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2145 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002146 break;
2147 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002148 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Float(),
2149 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002150 break;
2151 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002152 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002153 break;
2154 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002155 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002156 break;
2157 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002158 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002159 break;
2160
2161 case Instruction::ADD_INT:
2162 case Instruction::SUB_INT:
2163 case Instruction::MUL_INT:
2164 case Instruction::REM_INT:
2165 case Instruction::DIV_INT:
2166 case Instruction::SHL_INT:
2167 case Instruction::SHR_INT:
2168 case Instruction::USHR_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002169 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(),
2170 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002171 break;
2172 case Instruction::AND_INT:
2173 case Instruction::OR_INT:
2174 case Instruction::XOR_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002175 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(),
2176 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002177 break;
2178 case Instruction::ADD_LONG:
2179 case Instruction::SUB_LONG:
2180 case Instruction::MUL_LONG:
2181 case Instruction::DIV_LONG:
2182 case Instruction::REM_LONG:
2183 case Instruction::AND_LONG:
2184 case Instruction::OR_LONG:
2185 case Instruction::XOR_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002186 work_line_->CheckBinaryOpWide(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:
2191 case Instruction::SHR_LONG:
2192 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002193 /* shift distance is Int, making these different from other binary operations */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002194 work_line_->CheckBinaryOpWideShift(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2195 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002196 break;
2197 case Instruction::ADD_FLOAT:
2198 case Instruction::SUB_FLOAT:
2199 case Instruction::MUL_FLOAT:
2200 case Instruction::DIV_FLOAT:
2201 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002202 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002203 break;
2204 case Instruction::ADD_DOUBLE:
2205 case Instruction::SUB_DOUBLE:
2206 case Instruction::MUL_DOUBLE:
2207 case Instruction::DIV_DOUBLE:
2208 case Instruction::REM_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002209 work_line_->CheckBinaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2210 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2211 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002212 break;
2213 case Instruction::ADD_INT_2ADDR:
2214 case Instruction::SUB_INT_2ADDR:
2215 case Instruction::MUL_INT_2ADDR:
2216 case Instruction::REM_INT_2ADDR:
2217 case Instruction::SHL_INT_2ADDR:
2218 case Instruction::SHR_INT_2ADDR:
2219 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002220 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002221 break;
2222 case Instruction::AND_INT_2ADDR:
2223 case Instruction::OR_INT_2ADDR:
2224 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002225 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002226 break;
2227 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002228 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002229 break;
2230 case Instruction::ADD_LONG_2ADDR:
2231 case Instruction::SUB_LONG_2ADDR:
2232 case Instruction::MUL_LONG_2ADDR:
2233 case Instruction::DIV_LONG_2ADDR:
2234 case Instruction::REM_LONG_2ADDR:
2235 case Instruction::AND_LONG_2ADDR:
2236 case Instruction::OR_LONG_2ADDR:
2237 case Instruction::XOR_LONG_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002238 work_line_->CheckBinaryOp2addrWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2239 reg_types_.LongLo(), reg_types_.LongHi(),
2240 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002241 break;
2242 case Instruction::SHL_LONG_2ADDR:
2243 case Instruction::SHR_LONG_2ADDR:
2244 case Instruction::USHR_LONG_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002245 work_line_->CheckBinaryOp2addrWideShift(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2246 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002247 break;
2248 case Instruction::ADD_FLOAT_2ADDR:
2249 case Instruction::SUB_FLOAT_2ADDR:
2250 case Instruction::MUL_FLOAT_2ADDR:
2251 case Instruction::DIV_FLOAT_2ADDR:
2252 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002253 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002254 break;
2255 case Instruction::ADD_DOUBLE_2ADDR:
2256 case Instruction::SUB_DOUBLE_2ADDR:
2257 case Instruction::MUL_DOUBLE_2ADDR:
2258 case Instruction::DIV_DOUBLE_2ADDR:
2259 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002260 work_line_->CheckBinaryOp2addrWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2261 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2262 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002263 break;
2264 case Instruction::ADD_INT_LIT16:
2265 case Instruction::RSUB_INT:
2266 case Instruction::MUL_INT_LIT16:
2267 case Instruction::DIV_INT_LIT16:
2268 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002269 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002270 break;
2271 case Instruction::AND_INT_LIT16:
2272 case Instruction::OR_INT_LIT16:
2273 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002274 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002275 break;
2276 case Instruction::ADD_INT_LIT8:
2277 case Instruction::RSUB_INT_LIT8:
2278 case Instruction::MUL_INT_LIT8:
2279 case Instruction::DIV_INT_LIT8:
2280 case Instruction::REM_INT_LIT8:
2281 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002282 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002283 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002284 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002285 break;
2286 case Instruction::AND_INT_LIT8:
2287 case Instruction::OR_INT_LIT8:
2288 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002289 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002290 break;
2291
Ian Rogersd81871c2011-10-03 13:57:23 -07002292 /* These should never appear during verification. */
jeffhao9a4f0032012-08-30 16:17:40 -07002293 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002294 case Instruction::UNUSED_EE:
2295 case Instruction::UNUSED_EF:
2296 case Instruction::UNUSED_F2:
2297 case Instruction::UNUSED_F3:
2298 case Instruction::UNUSED_F4:
2299 case Instruction::UNUSED_F5:
2300 case Instruction::UNUSED_F6:
2301 case Instruction::UNUSED_F7:
2302 case Instruction::UNUSED_F8:
2303 case Instruction::UNUSED_F9:
2304 case Instruction::UNUSED_FA:
2305 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002306 case Instruction::UNUSED_F0:
2307 case Instruction::UNUSED_F1:
2308 case Instruction::UNUSED_E3:
2309 case Instruction::UNUSED_E8:
2310 case Instruction::UNUSED_E7:
2311 case Instruction::UNUSED_E4:
2312 case Instruction::UNUSED_E9:
2313 case Instruction::UNUSED_FC:
2314 case Instruction::UNUSED_E5:
2315 case Instruction::UNUSED_EA:
2316 case Instruction::UNUSED_FD:
2317 case Instruction::UNUSED_E6:
2318 case Instruction::UNUSED_EB:
2319 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002320 case Instruction::UNUSED_3E:
2321 case Instruction::UNUSED_3F:
2322 case Instruction::UNUSED_40:
2323 case Instruction::UNUSED_41:
2324 case Instruction::UNUSED_42:
2325 case Instruction::UNUSED_43:
2326 case Instruction::UNUSED_73:
2327 case Instruction::UNUSED_79:
2328 case Instruction::UNUSED_7A:
2329 case Instruction::UNUSED_EC:
2330 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002331 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002332 break;
2333
2334 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002335 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002336 * complain if an instruction is missing (which is desirable).
2337 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002338 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002339
Ian Rogersad0b3a32012-04-16 14:50:24 -07002340 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002341 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002342 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002343 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002344 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002345 /* immediate failure, reject class */
2346 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2347 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002348 } else if (have_pending_runtime_throw_failure_) {
2349 /* slow path will throw, mark following code as unreachable */
2350 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002351 }
jeffhaobdb76512011-09-07 11:43:16 -07002352 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002353 * If we didn't just set the result register, clear it out. This ensures that you can only use
2354 * "move-result" immediately after the result is set. (We could check this statically, but it's
2355 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002356 */
2357 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002358 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002359 }
2360
Dragos Sbirleaef7162e2013-05-28 14:14:12 -07002361
jeffhaobdb76512011-09-07 11:43:16 -07002362
2363 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002364 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002365 *
2366 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002367 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002368 * somebody could get a reference field, check it for zero, and if the
2369 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002370 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002371 * that, and will reject the code.
2372 *
2373 * TODO: avoid re-fetching the branch target
2374 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002375 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002376 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002377 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002378 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002379 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002380 return false;
2381 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002382 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002383 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002384 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002385 }
jeffhaobdb76512011-09-07 11:43:16 -07002386 /* update branch target, set "changed" if appropriate */
Dragos Sbirleaef7162e2013-05-28 14:14:12 -07002387 if (NULL != branch_line.get()) {
2388 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2389 return false;
2390 }
2391 } else {
2392 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2393 return false;
2394 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002395 }
jeffhaobdb76512011-09-07 11:43:16 -07002396 }
2397
2398 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002399 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002400 *
2401 * We've already verified that the table is structurally sound, so we
2402 * just need to walk through and tag the targets.
2403 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002404 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002405 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2406 const uint16_t* switch_insns = insns + offset_to_switch;
2407 int switch_count = switch_insns[1];
2408 int offset_to_targets, targ;
2409
2410 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2411 /* 0 = sig, 1 = count, 2/3 = first key */
2412 offset_to_targets = 4;
2413 } else {
2414 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002415 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002416 offset_to_targets = 2 + 2 * switch_count;
2417 }
2418
2419 /* verify each switch target */
2420 for (targ = 0; targ < switch_count; targ++) {
2421 int offset;
2422 uint32_t abs_offset;
2423
2424 /* offsets are 32-bit, and only partly endian-swapped */
2425 offset = switch_insns[offset_to_targets + targ * 2] |
2426 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002427 abs_offset = work_insn_idx_ + offset;
2428 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002429 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002430 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002431 }
2432 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002433 return false;
2434 }
2435 }
2436
2437 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002438 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2439 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002440 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002441 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002442 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002443 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002444
Ian Rogers0571d352011-11-03 19:51:38 -07002445 for (; iterator.HasNext(); iterator.Next()) {
2446 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002447 within_catch_all = true;
2448 }
jeffhaobdb76512011-09-07 11:43:16 -07002449 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002450 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2451 * "work_regs", because at runtime the exception will be thrown before the instruction
2452 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002453 */
Ian Rogers0571d352011-11-03 19:51:38 -07002454 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002455 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002456 }
jeffhaobdb76512011-09-07 11:43:16 -07002457 }
2458
2459 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002460 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2461 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002462 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002463 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002464 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002465 * The state in work_line reflects the post-execution state. If the current instruction is a
2466 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002467 * it will do so before grabbing the lock).
2468 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002469 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002470 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002471 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002472 return false;
2473 }
2474 }
2475 }
2476
Dragos Sbirleaef7162e2013-05-28 14:14:12 -07002477 /* Handle "continue". Tag the next consecutive instruction.
2478 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2479 * because it changes work_line_ when performing peephole optimization
2480 * and this change should not be used in those cases.
2481 */
2482 if ((opcode_flags & Instruction::kContinue) != 0) {
2483 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2484 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2485 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2486 return false;
2487 }
2488 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2489 // next instruction isn't one.
2490 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2491 return false;
2492 }
2493 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2494 if (next_line != NULL) {
2495 if (NULL != fallthrough_line.get()) {
2496 // Make workline consistent with fallthrough computed from peephole optimization.
2497 work_line_->CopyFromLine(fallthrough_line.get());
2498 }
2499 // Merge registers into what we have for the next instruction,
2500 // and set the "changed" flag if needed.
2501 if (!UpdateRegisters(next_insn_idx, fallthrough_line.get())) {
2502 return false;
2503 }
2504 } else {
2505 /*
2506 * We're not recording register data for the next instruction, so we don't know what the
2507 * prior state was. We have to assume that something has changed and re-evaluate it.
2508 */
2509 insn_flags_[next_insn_idx].SetChanged();
2510 }
2511 }
2512
jeffhaod1f0fde2011-09-08 17:25:33 -07002513 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002514 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002515 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002516 return false;
2517 }
jeffhaobdb76512011-09-07 11:43:16 -07002518 }
2519
2520 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002521 * Update start_guess. Advance to the next instruction of that's
2522 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002523 * neither of those exists we're in a return or throw; leave start_guess
2524 * alone and let the caller sort it out.
2525 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002526 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002527 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002528 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002529 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002530 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002531 }
2532
Ian Rogersd81871c2011-10-03 13:57:23 -07002533 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2534 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002535
2536 return true;
2537}
2538
Ian Rogers776ac1f2012-04-13 23:36:36 -07002539const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002540 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002541 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002542 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002543 const RegType& result =
Ian Rogersb4903572012-10-11 11:52:56 -07002544 klass != NULL ? reg_types_.FromClass(klass, klass->IsFinal())
2545 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002546 if (result.IsConflict()) {
2547 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2548 << "' in " << referrer;
2549 return result;
2550 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002551 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002552 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002553 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002554 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002555 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002556 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002557 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002558 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002559 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002560 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002561}
2562
Ian Rogers776ac1f2012-04-13 23:36:36 -07002563const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002564 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002565 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002566 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002567 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2568 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002569 CatchHandlerIterator iterator(handlers_ptr);
2570 for (; iterator.HasNext(); iterator.Next()) {
2571 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2572 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002573 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002574 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002575 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002576 if (common_super == NULL) {
2577 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2578 // as that is caught at runtime
2579 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002580 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002581 // We don't know enough about the type and the common path merge will result in
2582 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002583 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002584 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002585 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002586 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002587 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002588 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002589 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002590 }
2591 }
2592 }
2593 }
Ian Rogers0571d352011-11-03 19:51:38 -07002594 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002595 }
2596 }
2597 if (common_super == NULL) {
2598 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002599 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002600 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002601 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002602 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002603}
2604
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002605mirror::AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
2606 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002607 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002608 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002609 if (klass_type.IsConflict()) {
2610 std::string append(" in attempt to access method ");
2611 append += dex_file_->GetMethodName(method_id);
2612 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002613 return NULL;
2614 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002615 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002616 return NULL; // Can't resolve Class so no more to do here
2617 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002618 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002619 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002620 mirror::AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002621 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002622 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002623 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002624
2625 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002626 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002627 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002628 res_method = klass->FindInterfaceMethod(name, signature);
2629 } else {
2630 res_method = klass->FindVirtualMethod(name, signature);
2631 }
2632 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002633 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002634 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002635 // If a virtual or interface method wasn't found with the expected type, look in
2636 // the direct methods. This can happen when the wrong invoke type is used or when
2637 // a class has changed, and will be flagged as an error in later checks.
2638 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2639 res_method = klass->FindDirectMethod(name, signature);
2640 }
2641 if (res_method == NULL) {
2642 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2643 << PrettyDescriptor(klass) << "." << name
2644 << " " << signature;
2645 return NULL;
2646 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002647 }
2648 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002649 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2650 // enforce them here.
2651 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002652 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2653 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002654 return NULL;
2655 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002656 // Disallow any calls to class initializers.
2657 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002658 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2659 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002660 return NULL;
2661 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002662 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002663 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002664 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002665 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002666 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002667 }
jeffhaode0d9c92012-02-27 13:58:13 -08002668 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2669 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002670 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2671 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002672 return NULL;
2673 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002674 // Check that interface methods match interface classes.
2675 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2676 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2677 << " is in an interface class " << PrettyClass(klass);
2678 return NULL;
2679 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2680 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2681 << " is in a non-interface class " << PrettyClass(klass);
2682 return NULL;
2683 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002684 // See if the method type implied by the invoke instruction matches the access flags for the
2685 // target method.
2686 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2687 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2688 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2689 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002690 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2691 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002692 return NULL;
2693 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002694 return res_method;
2695}
2696
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002697mirror::AbstractMethod* MethodVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
2698 MethodType method_type, bool is_range,
2699 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002700 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2701 // we're making.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002702 mirror::AbstractMethod* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002703 if (res_method == NULL) { // error or class is unresolved
2704 return NULL;
2705 }
2706
Ian Rogersd81871c2011-10-03 13:57:23 -07002707 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2708 // has a vtable entry for the target method.
2709 if (is_super) {
2710 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002711 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002712 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002713 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002714 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002715 << " to super " << PrettyMethod(res_method);
2716 return NULL;
2717 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002718 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002719 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002720 MethodHelper mh(res_method);
2721 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002722 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002723 << " to super " << super
2724 << "." << mh.GetName()
2725 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002726 return NULL;
2727 }
2728 }
2729 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2730 // match the call to the signature. Also, we might might be calling through an abstract method
2731 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08002732 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002733 /* caught by static verifier */
2734 DCHECK(is_range || expected_args <= 5);
2735 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002736 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002737 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2738 return NULL;
2739 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002740
jeffhaobdb76512011-09-07 11:43:16 -07002741 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002742 * Check the "this" argument, which must be an instance of the class that declared the method.
2743 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2744 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002745 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002746 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002747 if (!res_method->IsStatic()) {
2748 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002749 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002750 return NULL;
2751 }
2752 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002753 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002754 return NULL;
2755 }
2756 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002757 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogersb4903572012-10-11 11:52:56 -07002758 const RegType& res_method_class = reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogers9074b992011-10-26 17:41:55 -07002759 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002760 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002761 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002762 return NULL;
2763 }
2764 }
2765 actual_args++;
2766 }
2767 /*
2768 * Process the target method's signature. This signature may or may not
2769 * have been verified, so we can't assume it's properly formed.
2770 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002771 MethodHelper mh(res_method);
2772 const DexFile::TypeList* params = mh.GetParameterTypeList();
2773 size_t params_size = params == NULL ? 0 : params->Size();
2774 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002775 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002776 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002777 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2778 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002779 return NULL;
2780 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002781 const char* descriptor =
2782 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2783 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002784 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002785 << " missing signature component";
2786 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002787 }
Ian Rogersb4903572012-10-11 11:52:56 -07002788 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002789 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002790 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002791 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002792 }
2793 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2794 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002795 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002796 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002797 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002798 return NULL;
2799 } else {
2800 return res_method;
2801 }
2802}
2803
Ian Rogers776ac1f2012-04-13 23:36:36 -07002804void MethodVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08002805 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002806 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002807 if (res_type.IsConflict()) { // bad class
2808 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002809 } else {
2810 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2811 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002812 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002813 } else if (!is_filled) {
2814 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002815 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002816 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002817 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002818 } else {
2819 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2820 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002821 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002822 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002823 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002824 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002825 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002826 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002827 return;
2828 }
2829 }
2830 // filled-array result goes into "result" register
2831 work_line_->SetResultRegisterType(res_type);
2832 }
2833 }
2834}
2835
Ian Rogers776ac1f2012-04-13 23:36:36 -07002836void MethodVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002837 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002838 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002839 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002840 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002842 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002843 if (array_type.IsZero()) {
2844 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2845 // instruction type. TODO: have a proper notion of bottom here.
2846 if (!is_primitive || insn_type.IsCategory1Types()) {
2847 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08002848 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002849 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002850 // Category 2
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002851 work_line_->SetRegisterTypeWide(dec_insn.vA, reg_types_.FromCat2ConstLo(0, false),
2852 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08002853 }
jeffhaofc3144e2012-02-01 17:21:15 -08002854 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002855 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002856 } else {
2857 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002858 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002859 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002860 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002861 << " source for aget-object";
2862 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002863 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002864 << " source for category 1 aget";
2865 } else if (is_primitive && !insn_type.Equals(component_type) &&
2866 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002867 (insn_type.IsLong() && component_type.IsDouble()))) {
2868 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
2869 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002870 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002871 // Use knowledge of the field type which is stronger than the type inferred from the
2872 // instruction, which can't differentiate object types and ints from floats, longs from
2873 // doubles.
2874 if (!component_type.IsLowHalf()) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002875 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002876 } else {
2877 work_line_->SetRegisterTypeWide(dec_insn.vA, component_type,
2878 component_type.HighHalf(&reg_types_));
2879 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002880 }
2881 }
2882 }
2883}
2884
Ian Rogers776ac1f2012-04-13 23:36:36 -07002885void MethodVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002886 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002887 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002888 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002889 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002890 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002891 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002892 if (array_type.IsZero()) {
2893 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2894 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002895 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002896 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002897 } else {
2898 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002899 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002900 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002901 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002902 << " source for aput-object";
2903 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002904 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002905 << " source for category 1 aput";
2906 } else if (is_primitive && !insn_type.Equals(component_type) &&
2907 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2908 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002909 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002910 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002911 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002912 // The instruction agrees with the type of array, confirm the value to be stored does too
2913 // Note: we use the instruction type (rather than the component type) for aput-object as
2914 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08002915 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002916 }
2917 }
2918 }
2919}
2920
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002921mirror::Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002922 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2923 // Check access to class
2924 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002925 if (klass_type.IsConflict()) { // bad class
2926 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
2927 field_idx, dex_file_->GetFieldName(field_id),
2928 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002929 return NULL;
2930 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002931 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002932 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08002933 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002934 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07002935 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002936 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002937 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
2938 << dex_file_->GetFieldName(field_id) << ") in "
2939 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002940 DCHECK(Thread::Current()->IsExceptionPending());
2941 Thread::Current()->ClearException();
2942 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002943 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2944 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002945 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002946 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002947 return NULL;
2948 } else if (!field->IsStatic()) {
2949 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
2950 return NULL;
2951 } else {
2952 return field;
2953 }
2954}
2955
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002956mirror::Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002957 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2958 // Check access to class
2959 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002960 if (klass_type.IsConflict()) {
2961 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
2962 field_idx, dex_file_->GetFieldName(field_id),
2963 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002964 return NULL;
2965 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002966 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002967 return NULL; // Can't resolve Class so no more to do here
2968 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002969 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07002970 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002971 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002972 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
2973 << dex_file_->GetFieldName(field_id) << ") in "
2974 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002975 DCHECK(Thread::Current()->IsExceptionPending());
2976 Thread::Current()->ClearException();
2977 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002978 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2979 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002980 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002981 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002982 return NULL;
2983 } else if (field->IsStatic()) {
2984 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
2985 << " to not be static";
2986 return NULL;
2987 } else if (obj_type.IsZero()) {
2988 // Cannot infer and check type, however, access will cause null pointer exception
2989 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07002990 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002991 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersb4903572012-10-11 11:52:56 -07002992 const RegType& field_klass = reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002993 if (obj_type.IsUninitializedTypes() &&
2994 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
2995 !field_klass.Equals(GetDeclaringClass()))) {
2996 // Field accesses through uninitialized references are only allowable for constructors where
2997 // the field is declared in this class
2998 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
2999 << " of a not fully initialized object within the context of "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003000 << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003001 return NULL;
3002 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3003 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3004 // of C1. For resolution to occur the declared class of the field must be compatible with
3005 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3006 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3007 << " from object of type " << obj_type;
3008 return NULL;
3009 } else {
3010 return field;
3011 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003012 }
3013}
3014
Ian Rogers776ac1f2012-04-13 23:36:36 -07003015void MethodVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003016 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003017 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003018 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003019 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003020 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003021 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003022 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003023 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003024 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003025 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003026 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003027 if (field != NULL) {
3028 descriptor = FieldHelper(field).GetTypeDescriptor();
3029 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003030 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003031 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3032 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3033 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07003034 }
Ian Rogersb4903572012-10-11 11:52:56 -07003035 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003036 if (is_primitive) {
3037 if (field_type.Equals(insn_type) ||
3038 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3039 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3040 // expected that read is of the correct primitive type or that int reads are reading
3041 // floats or long reads are reading doubles
3042 } else {
3043 // This is a global failure rather than a class change failure as the instructions and
3044 // the descriptors for the type should have been consistent within the same file at
3045 // compile time
3046 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3047 << " to be of type '" << insn_type
3048 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003049 return;
3050 }
3051 } else {
3052 if (!insn_type.IsAssignableFrom(field_type)) {
3053 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3054 << " to be compatible with type '" << insn_type
3055 << "' but found type '" << field_type
3056 << "' in get-object";
3057 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Conflict());
3058 return;
3059 }
3060 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003061 if (!field_type.IsLowHalf()) {
3062 work_line_->SetRegisterType(dec_insn.vA, field_type);
3063 } else {
3064 work_line_->SetRegisterTypeWide(dec_insn.vA, field_type, field_type.HighHalf(&reg_types_));
3065 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003066}
3067
Ian Rogers776ac1f2012-04-13 23:36:36 -07003068void MethodVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003069 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003070 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003071 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003072 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003073 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003074 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003075 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07003076 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003077 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003078 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003079 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003080 if (field != NULL) {
3081 descriptor = FieldHelper(field).GetTypeDescriptor();
3082 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003083 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003084 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3085 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3086 loader = class_loader_;
3087 }
Ian Rogersb4903572012-10-11 11:52:56 -07003088 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003089 if (field != NULL) {
3090 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3091 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3092 << " from other class " << GetDeclaringClass();
3093 return;
3094 }
3095 }
3096 if (is_primitive) {
3097 // Primitive field assignability rules are weaker than regular assignability rules
3098 bool instruction_compatible;
3099 bool value_compatible;
3100 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
3101 if (field_type.IsIntegralTypes()) {
3102 instruction_compatible = insn_type.IsIntegralTypes();
3103 value_compatible = value_type.IsIntegralTypes();
3104 } else if (field_type.IsFloat()) {
3105 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3106 value_compatible = value_type.IsFloatTypes();
3107 } else if (field_type.IsLong()) {
3108 instruction_compatible = insn_type.IsLong();
3109 value_compatible = value_type.IsLongTypes();
3110 } else if (field_type.IsDouble()) {
3111 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3112 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003113 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003114 instruction_compatible = false; // reference field with primitive store
3115 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003116 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003117 if (!instruction_compatible) {
3118 // This is a global failure rather than a class change failure as the instructions and
3119 // the descriptors for the type should have been consistent within the same file at
3120 // compile time
3121 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3122 << " to be of type '" << insn_type
3123 << "' but found type '" << field_type
3124 << "' in put";
3125 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003126 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003127 if (!value_compatible) {
3128 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
3129 << " of type " << value_type
3130 << " but expected " << field_type
3131 << " for store to " << PrettyField(field) << " in put";
3132 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003133 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003134 } else {
3135 if (!insn_type.IsAssignableFrom(field_type)) {
3136 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3137 << " to be compatible with type '" << insn_type
3138 << "' but found type '" << field_type
3139 << "' in put-object";
3140 return;
3141 }
3142 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003143 }
3144}
3145
Ian Rogers776ac1f2012-04-13 23:36:36 -07003146bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003147 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003148 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003149 return false;
3150 }
3151 return true;
3152}
3153
Ian Rogers776ac1f2012-04-13 23:36:36 -07003154bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003155 bool changed = true;
3156 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3157 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003158 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003159 * We haven't processed this instruction before, and we haven't touched the registers here, so
3160 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3161 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003162 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003163 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003164 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003165 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3166 if (gDebugVerify) {
3167 copy->CopyFromLine(target_line);
3168 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003169 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003170 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003171 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003172 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003173 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003174 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003175 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3176 << *copy.get() << " MERGE\n"
3177 << *merge_line << " ==\n"
3178 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003179 }
3180 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003181 if (changed) {
3182 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003183 }
3184 return true;
3185}
3186
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003187InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003188 return &insn_flags_[work_insn_idx_];
3189}
3190
Ian Rogersad0b3a32012-04-16 14:50:24 -07003191const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003192 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003193 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3194 uint16_t return_type_idx = proto_id.return_type_idx_;
3195 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003196 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003197}
3198
3199const RegType& MethodVerifier::GetDeclaringClass() {
3200 if (foo_method_ != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003201 mirror::Class* klass = foo_method_->GetDeclaringClass();
Ian Rogersb4903572012-10-11 11:52:56 -07003202 return reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003203 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003204 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003205 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogersb4903572012-10-11 11:52:56 -07003206 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003207 }
3208}
3209
Ian Rogers776ac1f2012-04-13 23:36:36 -07003210void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003211 size_t* log2_max_gc_pc) {
3212 size_t local_gc_points = 0;
3213 size_t max_insn = 0;
3214 size_t max_ref_reg = -1;
3215 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003216 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003217 local_gc_points++;
3218 max_insn = i;
3219 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003220 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003221 }
3222 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003223 *gc_points = local_gc_points;
3224 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3225 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003226 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003227 i++;
3228 }
3229 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003230}
3231
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003232MethodVerifier::PcToConreteMethod* MethodVerifier::GenerateDevirtMap() {
3233
3234 // It is risky to rely on reg_types for sharpening in cases of soft
3235 // verification, we might end up sharpening to a wrong implementation. Just abort.
3236 if (!failure_messages_.empty()) {
3237 return NULL;
3238 }
3239
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003240 UniquePtr<PcToConreteMethod> pc_to_concrete_method(new PcToConreteMethod());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003241 uint32_t dex_pc = 0;
3242 const uint16_t* insns = code_item_->insns_ ;
3243 const Instruction* inst = Instruction::At(insns);
3244
3245 for (; dex_pc < code_item_->insns_size_in_code_units_;
3246 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits(), inst = inst->Next()) {
3247
3248 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3249 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3250 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3251 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3252
3253 if(!(is_interface || is_virtual))
3254 continue;
3255
3256 // Check if vC ("this" pointer in the instruction) has a precise type.
3257 RegisterLine* line = reg_table_.GetLine(dex_pc);
3258 DecodedInstruction dec_insn(inst);
3259 const RegType& reg_type(line->GetRegisterType(dec_insn.vC));
3260
3261 if (!reg_type.IsPreciseReference()) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003262 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003263 }
3264
3265 CHECK(!(reg_type.GetClass()->IsInterface()));
3266 // If the class is an array class, it can be both Abstract and final and so
3267 // the reg_type will be created as precise.
3268 CHECK(!(reg_type.GetClass()->IsAbstract()) || reg_type.GetClass()->IsArrayClass());
3269 // Find the abstract method.
3270 // vB has the method index.
3271 mirror::AbstractMethod* abstract_method = NULL ;
3272 abstract_method = dex_cache_->GetResolvedMethod(dec_insn.vB);
3273 if(abstract_method == NULL) {
3274 // If the method is not found in the cache this means that it was never found
3275 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
3276 continue;
3277 }
3278 // Find the concrete method.
3279 mirror::AbstractMethod* concrete_method = NULL;
3280 if (is_interface) {
3281 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
3282 }
3283 if (is_virtual) {
3284 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
3285 }
3286
3287 if(concrete_method == NULL) {
3288 // In cases where concrete_method is not found continue to the next invoke instead
3289 // of crashing.
3290 continue;
3291 }
3292
3293 CHECK(!concrete_method->IsAbstract()) << PrettyMethod(concrete_method);
3294 // Build method reference.
3295 CompilerDriver::MethodReference concrete_ref(
3296 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
3297 concrete_method->GetDexMethodIndex());
3298 // Now Save the current PC and the concrete method reference to be used
3299 // in compiler driver.
3300 pc_to_concrete_method->Put(dex_pc, concrete_ref );
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003301 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003302
3303 if (pc_to_concrete_method->size() == 0) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003304 return NULL ;
3305 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003306 return pc_to_concrete_method.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003307}
3308
Ian Rogers776ac1f2012-04-13 23:36:36 -07003309const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003310 size_t num_entries, ref_bitmap_bits, pc_bits;
3311 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3312 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003313 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003314 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003315 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003316 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003317 return NULL;
3318 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003319 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3320 // There are 2 bytes to encode the number of entries
3321 if (num_entries >= 65536) {
3322 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003323 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003324 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003325 return NULL;
3326 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003327 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003328 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003329 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003330 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003331 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003332 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003333 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003334 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003335 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003336 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003337 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003338 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3339 return NULL;
3340 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003341 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003342 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003343 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003344 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003345 return NULL;
3346 }
3347 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003348 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3349 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003350 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003351 table->push_back(num_entries & 0xFF);
3352 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003353 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003354 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003355 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003356 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003357 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003358 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003359 }
3360 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003361 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003362 }
3363 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003364 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003365 return table;
3366}
jeffhaoa0a764a2011-09-16 10:43:38 -07003367
Ian Rogers776ac1f2012-04-13 23:36:36 -07003368void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003369 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3370 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003371 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003372 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003373 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003374 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003375 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003376 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003377 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003378 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3379 map_index++;
3380 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003381 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003382 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003383 CHECK_LT(j / 8, map.RegWidth());
3384 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3385 } else if ((j / 8) < map.RegWidth()) {
3386 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3387 } else {
3388 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3389 }
3390 }
3391 } else {
3392 CHECK(reg_bitmap == NULL);
3393 }
3394 }
3395}
jeffhaoa0a764a2011-09-16 10:43:38 -07003396
Ian Rogers1212a022013-03-04 10:48:41 -08003397void MethodVerifier::SetDexGcMap(CompilerDriver::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003398 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003399 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003400 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
3401 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003402 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003403 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003404 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003405 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003406 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003407 CHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003408}
3409
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003410void MethodVerifier::SetDevirtMap(CompilerDriver::MethodReference ref, const PcToConreteMethod* devirt_map) {
3411
3412 MutexLock mu(Thread::Current(), *devirt_maps_lock_);
3413 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
3414 if (it != devirt_maps_->end()) {
3415 delete it->second;
3416 devirt_maps_->erase(it);
3417 }
3418
3419 devirt_maps_->Put(ref, devirt_map);
3420 CHECK(devirt_maps_->find(ref) != devirt_maps_->end());
3421}
3422
Ian Rogers1212a022013-03-04 10:48:41 -08003423const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(CompilerDriver::MethodReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003424 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003425 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
3426 if (it == dex_gc_maps_->end()) {
Ian Rogerse3cd2f02013-05-24 15:32:56 -07003427 LOG(WARNING) << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003428 return NULL;
3429 }
3430 CHECK(it->second != NULL);
3431 return it->second;
3432}
3433
Ian Rogerse3cd2f02013-05-24 15:32:56 -07003434const CompilerDriver::MethodReference* MethodVerifier::GetDevirtMap(const CompilerDriver::MethodReference& ref,
3435 uint32_t dex_pc) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003436 MutexLock mu(Thread::Current(), *devirt_maps_lock_);
3437 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
3438 if (it == devirt_maps_->end()) {
3439 return NULL;
3440 }
3441
3442 // Look up the PC in the map, get the concrete method to execute and return its reference.
Ian Rogerse3cd2f02013-05-24 15:32:56 -07003443 MethodVerifier::PcToConreteMethod::const_iterator pc_to_concrete_method = it->second->find(dex_pc);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003444 if(pc_to_concrete_method != it->second->end()) {
3445 return &(pc_to_concrete_method->second);
3446 } else {
3447 return NULL;
3448 }
3449}
3450
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003451std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3452 RegisterLine* line = reg_table_.GetLine(dex_pc);
3453 std::vector<int32_t> result;
3454 for (size_t i = 0; i < line->NumRegs(); ++i) {
3455 const RegType& type = line->GetRegisterType(i);
3456 if (type.IsConstant()) {
3457 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3458 result.push_back(type.ConstantValue());
3459 } else if (type.IsConstantLo()) {
3460 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3461 result.push_back(type.ConstantValueLo());
3462 } else if (type.IsConstantHi()) {
3463 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3464 result.push_back(type.ConstantValueHi());
3465 } else if (type.IsIntegralTypes()) {
3466 result.push_back(kIntVReg);
3467 result.push_back(0);
3468 } else if (type.IsFloat()) {
3469 result.push_back(kFloatVReg);
3470 result.push_back(0);
3471 } else if (type.IsLong()) {
3472 result.push_back(kLongLoVReg);
3473 result.push_back(0);
3474 result.push_back(kLongHiVReg);
3475 result.push_back(0);
3476 ++i;
3477 } else if (type.IsDouble()) {
3478 result.push_back(kDoubleLoVReg);
3479 result.push_back(0);
3480 result.push_back(kDoubleHiVReg);
3481 result.push_back(0);
3482 ++i;
3483 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
3484 result.push_back(kUndefined);
3485 result.push_back(0);
3486 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003487 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003488 result.push_back(kReferenceVReg);
3489 result.push_back(0);
3490 }
3491 }
3492 return result;
3493}
3494
Ian Rogers0c7abda2012-09-19 13:33:42 -07003495Mutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
3496MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003497
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003498Mutex* MethodVerifier::devirt_maps_lock_ = NULL;
3499MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
3500
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003501Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
3502MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
3503
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003504void MethodVerifier::Init() {
Ian Rogers0c7abda2012-09-19 13:33:42 -07003505 dex_gc_maps_lock_ = new Mutex("verifier GC maps lock");
Ian Rogers50b35e22012-10-04 10:09:15 -07003506 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003507 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003508 MutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003509 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003510 }
3511
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003512 devirt_maps_lock_ = new Mutex("verifier Devirtualization lock");
3513 {
3514 MutexLock mu(self, *devirt_maps_lock_);
3515 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
3516 }
3517
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003518 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
3519 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003520 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003521 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
3522 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003523 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003524}
3525
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003526void MethodVerifier::Shutdown() {
Ian Rogers50b35e22012-10-04 10:09:15 -07003527 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003528 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003529 MutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003530 STLDeleteValues(dex_gc_maps_);
3531 delete dex_gc_maps_;
3532 dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003533 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003534 delete dex_gc_maps_lock_;
3535 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003536
3537 {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003538 MutexLock mu(self, *devirt_maps_lock_);
3539 STLDeleteValues(devirt_maps_);
3540 delete devirt_maps_;
3541 devirt_maps_ = NULL;
3542 }
3543 delete devirt_maps_lock_;
3544 devirt_maps_lock_ = NULL;
3545
3546 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003547 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003548 delete rejected_classes_;
3549 rejected_classes_ = NULL;
3550 }
3551 delete rejected_classes_lock_;
3552 rejected_classes_lock_ = NULL;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003553 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003554}
jeffhaod1224c72012-02-29 13:43:08 -08003555
Ian Rogers1212a022013-03-04 10:48:41 -08003556void MethodVerifier::AddRejectedClass(CompilerDriver::ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003557 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003558 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003559 rejected_classes_->insert(ref);
3560 }
jeffhaod1224c72012-02-29 13:43:08 -08003561 CHECK(IsClassRejected(ref));
3562}
3563
Ian Rogers1212a022013-03-04 10:48:41 -08003564bool MethodVerifier::IsClassRejected(CompilerDriver::ClassReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003565 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003566 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08003567}
3568
Ian Rogersd81871c2011-10-03 13:57:23 -07003569} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003570} // namespace art