blob: 1b2d9f35bf80531fea952b08d526e915230e4543 [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
Elliott Hughesadb8c672012-03-06 16:49:32 -08001309 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07001310 case Instruction::NOP:
1311 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001312 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001313 * a signature that looks like a NOP; if we see one of these in
1314 * the course of executing code then we have a problem.
1315 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001316 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001317 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001318 }
1319 break;
1320
1321 case Instruction::MOVE:
1322 case Instruction::MOVE_FROM16:
1323 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001324 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001325 break;
1326 case Instruction::MOVE_WIDE:
1327 case Instruction::MOVE_WIDE_FROM16:
1328 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001329 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07001330 break;
1331 case Instruction::MOVE_OBJECT:
1332 case Instruction::MOVE_OBJECT_FROM16:
1333 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001334 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001335 break;
1336
1337 /*
1338 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001339 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001340 * might want to hold the result in an actual CPU register, so the
1341 * Dalvik spec requires that these only appear immediately after an
1342 * invoke or filled-new-array.
1343 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001344 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001345 * redundant with the reset done below, but it can make the debug info
1346 * easier to read in some cases.)
1347 */
1348 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001349 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07001350 break;
1351 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001352 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001353 break;
1354 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001355 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07001356 break;
1357
Ian Rogersd81871c2011-10-03 13:57:23 -07001358 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001359 /*
jeffhao60f83e32012-02-13 17:16:30 -08001360 * This statement can only appear as the first instruction in an exception handler. We verify
1361 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001362 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001363 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08001364 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001365 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001366 }
jeffhaobdb76512011-09-07 11:43:16 -07001367 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001368 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1369 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001370 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001371 }
jeffhaobdb76512011-09-07 11:43:16 -07001372 }
1373 break;
1374 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001375 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001376 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001377 const RegType& return_type = GetMethodReturnType();
1378 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001379 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001380 } else {
1381 // Compilers may generate synthetic functions that write byte values into boolean fields.
1382 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001383 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001384 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1385 ((return_type.IsBoolean() || return_type.IsByte() ||
1386 return_type.IsShort() || return_type.IsChar()) &&
1387 src_type.IsInteger()));
1388 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001389 bool success =
1390 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
1391 if (!success) {
1392 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001393 }
jeffhaobdb76512011-09-07 11:43:16 -07001394 }
1395 }
1396 break;
1397 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001398 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001399 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001400 const RegType& return_type = GetMethodReturnType();
1401 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001402 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001403 } else {
1404 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001405 bool success = work_line_->VerifyRegisterType(dec_insn.vA, return_type);
1406 if (!success) {
1407 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001408 }
jeffhaobdb76512011-09-07 11:43:16 -07001409 }
1410 }
1411 break;
1412 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001413 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001414 const RegType& return_type = GetMethodReturnType();
1415 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001416 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001417 } else {
1418 /* return_type is the *expected* return type, not register value */
1419 DCHECK(!return_type.IsZero());
1420 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08001421 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07001422 // Disallow returning uninitialized values and verify that the reference in vAA is an
1423 // instance of the "return_type"
1424 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001425 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001426 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001427 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1428 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001429 }
1430 }
1431 }
1432 break;
1433
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001434 /* could be boolean, int, float, or a null reference */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001435 case Instruction::CONST_4:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001436 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001437 reg_types_.FromCat1Const(static_cast<int32_t>(dec_insn.vB << 28) >> 28, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001438 break;
jeffhaobdb76512011-09-07 11:43:16 -07001439 case Instruction::CONST_16:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001440 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001441 reg_types_.FromCat1Const(static_cast<int16_t>(dec_insn.vB), true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001442 break;
jeffhaobdb76512011-09-07 11:43:16 -07001443 case Instruction::CONST:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001444 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const(dec_insn.vB, true));
jeffhaobdb76512011-09-07 11:43:16 -07001445 break;
1446 case Instruction::CONST_HIGH16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001447 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001448 reg_types_.FromCat1Const(dec_insn.vB << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001449 break;
jeffhaobdb76512011-09-07 11:43:16 -07001450 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001451 case Instruction::CONST_WIDE_16: {
1452 int64_t val = static_cast<int16_t>(dec_insn.vB);
1453 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1454 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1455 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001456 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001457 }
1458 case Instruction::CONST_WIDE_32: {
1459 int64_t val = static_cast<int32_t>(dec_insn.vB);
1460 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1461 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1462 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1463 break;
1464 }
1465 case Instruction::CONST_WIDE: {
1466 int64_t val = dec_insn.vB_wide;
1467 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1468 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1469 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1470 break;
1471 }
1472 case Instruction::CONST_WIDE_HIGH16: {
1473 int64_t val = static_cast<uint64_t>(dec_insn.vB) << 48;
1474 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1475 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1476 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1477 break;
1478 }
jeffhaobdb76512011-09-07 11:43:16 -07001479 case Instruction::CONST_STRING:
1480 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001481 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001482 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001483 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001484 // Get type from instruction if unresolved then we need an access check
1485 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08001486 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001487 // Register holds class, ie its type is class, on error it will hold Conflict.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001488 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogersb4903572012-10-11 11:52:56 -07001489 res_type.IsConflict() ? res_type
1490 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001491 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001492 }
jeffhaobdb76512011-09-07 11:43:16 -07001493 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001494 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001495 break;
1496 case Instruction::MONITOR_EXIT:
1497 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001498 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001499 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001500 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001501 * to the need to handle asynchronous exceptions, a now-deprecated
1502 * feature that Dalvik doesn't support.)
1503 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001504 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001505 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001506 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001507 * structured locking checks are working, the former would have
1508 * failed on the -enter instruction, and the latter is impossible.
1509 *
1510 * This is fortunate, because issue 3221411 prevents us from
1511 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001512 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001513 * some catch blocks (which will show up as "dead" code when
1514 * we skip them here); if we can't, then the code path could be
1515 * "live" so we still need to check it.
1516 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001517 opcode_flags &= ~Instruction::kThrow;
1518 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001519 break;
1520
Ian Rogers28ad40d2011-10-27 15:19:26 -07001521 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001522 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001523 /*
1524 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1525 * could be a "upcast" -- not expected, so we don't try to address it.)
1526 *
1527 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001528 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001529 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001530 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001531 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001532 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001533 if (res_type.IsConflict()) {
1534 DCHECK_NE(failures_.size(), 0U);
1535 if (!is_checkcast) {
1536 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
1537 }
1538 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001539 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001540 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1541 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001542 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001543 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001544 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001545 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001546 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07001547 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001548 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001549 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001550 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001551 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001552 }
jeffhaobdb76512011-09-07 11:43:16 -07001553 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001554 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001555 }
1556 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001557 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001558 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001559 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001560 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001561 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001562 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001563 }
1564 }
1565 break;
1566 }
1567 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001568 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001569 if (res_type.IsConflict()) {
1570 DCHECK_NE(failures_.size(), 0U);
1571 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001572 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001573 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1574 // can't create an instance of an interface or abstract class */
1575 if (!res_type.IsInstantiableTypes()) {
1576 Fail(VERIFY_ERROR_INSTANTIATION)
1577 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001578 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001579 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001580 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1581 // Any registers holding previous allocations from this address that have not yet been
1582 // initialized must be marked invalid.
1583 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1584 // add the new uninitialized reference to the register state
1585 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001586 break;
1587 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001588 case Instruction::NEW_ARRAY:
1589 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001590 break;
1591 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08001592 VerifyNewArray(dec_insn, true, false);
1593 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001594 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001595 case Instruction::FILLED_NEW_ARRAY_RANGE:
1596 VerifyNewArray(dec_insn, true, true);
1597 just_set_result = true; // Filled new array range sets result register
1598 break;
jeffhaobdb76512011-09-07 11:43:16 -07001599 case Instruction::CMPL_FLOAT:
1600 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001601 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001602 break;
1603 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001604 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001605 break;
1606 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001607 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001608 break;
1609 case Instruction::CMPL_DOUBLE:
1610 case Instruction::CMPG_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001611 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vB, reg_types_.DoubleLo(),
1612 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001613 break;
1614 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001615 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vC, reg_types_.DoubleLo(),
1616 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001617 break;
1618 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001619 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001620 break;
1621 case Instruction::CMP_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001622 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vB, reg_types_.LongLo(),
1623 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001624 break;
1625 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001626 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vC, reg_types_.LongLo(),
1627 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001628 break;
1629 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001630 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001631 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001632 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001633 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersb4903572012-10-11 11:52:56 -07001634 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001635 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001636 }
1637 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001638 }
jeffhaobdb76512011-09-07 11:43:16 -07001639 case Instruction::GOTO:
1640 case Instruction::GOTO_16:
1641 case Instruction::GOTO_32:
1642 /* no effect on or use of registers */
1643 break;
1644
1645 case Instruction::PACKED_SWITCH:
1646 case Instruction::SPARSE_SWITCH:
1647 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001648 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001649 break;
1650
Ian Rogersd81871c2011-10-03 13:57:23 -07001651 case Instruction::FILL_ARRAY_DATA: {
1652 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001653 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08001654 /* array_type can be null if the reg type is Zero */
1655 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001656 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001657 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001658 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001659 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1660 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001661 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001662 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1663 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001664 } else {
jeffhao457cc512012-02-02 16:55:13 -08001665 // Now verify if the element width in the table matches the element width declared in
1666 // the array
1667 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1668 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001669 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001670 } else {
1671 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1672 // Since we don't compress the data in Dex, expect to see equal width of data stored
1673 // in the table and expected from the array class.
1674 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001675 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1676 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001677 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001678 }
1679 }
jeffhaobdb76512011-09-07 11:43:16 -07001680 }
1681 }
1682 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001683 }
jeffhaobdb76512011-09-07 11:43:16 -07001684 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001685 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001686 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1687 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001688 bool mismatch = false;
1689 if (reg_type1.IsZero()) { // zero then integral or reference expected
1690 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1691 } else if (reg_type1.IsReferenceTypes()) { // both references?
1692 mismatch = !reg_type2.IsReferenceTypes();
1693 } else { // both integral?
1694 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1695 }
1696 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001697 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1698 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001699 }
1700 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001701 }
jeffhaobdb76512011-09-07 11:43:16 -07001702 case Instruction::IF_LT:
1703 case Instruction::IF_GE:
1704 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001705 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001706 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1707 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001708 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001709 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1710 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001711 }
1712 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001713 }
jeffhaobdb76512011-09-07 11:43:16 -07001714 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001715 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001716 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001717 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001719 }
jeffhaobdb76512011-09-07 11:43:16 -07001720 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001721 }
jeffhaobdb76512011-09-07 11:43:16 -07001722 case Instruction::IF_LTZ:
1723 case Instruction::IF_GEZ:
1724 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001725 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001726 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001727 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001728 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1729 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001730 }
jeffhaobdb76512011-09-07 11:43:16 -07001731 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001732 }
jeffhaobdb76512011-09-07 11:43:16 -07001733 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001734 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
1735 break;
jeffhaobdb76512011-09-07 11:43:16 -07001736 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001737 VerifyAGet(dec_insn, reg_types_.Byte(), true);
1738 break;
jeffhaobdb76512011-09-07 11:43:16 -07001739 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07001740 VerifyAGet(dec_insn, reg_types_.Char(), true);
1741 break;
jeffhaobdb76512011-09-07 11:43:16 -07001742 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001743 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001744 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001745 case Instruction::AGET:
1746 VerifyAGet(dec_insn, reg_types_.Integer(), true);
1747 break;
jeffhaobdb76512011-09-07 11:43:16 -07001748 case Instruction::AGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001749 VerifyAGet(dec_insn, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001750 break;
1751 case Instruction::AGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001752 VerifyAGet(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001753 break;
1754
Ian Rogersd81871c2011-10-03 13:57:23 -07001755 case Instruction::APUT_BOOLEAN:
1756 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
1757 break;
1758 case Instruction::APUT_BYTE:
1759 VerifyAPut(dec_insn, reg_types_.Byte(), true);
1760 break;
1761 case Instruction::APUT_CHAR:
1762 VerifyAPut(dec_insn, reg_types_.Char(), true);
1763 break;
1764 case Instruction::APUT_SHORT:
1765 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001766 break;
1767 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001768 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001769 break;
1770 case Instruction::APUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001771 VerifyAPut(dec_insn, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001772 break;
1773 case Instruction::APUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001774 VerifyAPut(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001775 break;
1776
jeffhaobdb76512011-09-07 11:43:16 -07001777 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001778 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001779 break;
jeffhaobdb76512011-09-07 11:43:16 -07001780 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001781 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001782 break;
jeffhaobdb76512011-09-07 11:43:16 -07001783 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001784 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001785 break;
jeffhaobdb76512011-09-07 11:43:16 -07001786 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001787 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001788 break;
1789 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001790 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001791 break;
1792 case Instruction::IGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001793 VerifyISGet(dec_insn, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001794 break;
1795 case Instruction::IGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001796 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001797 break;
jeffhaobdb76512011-09-07 11:43:16 -07001798
Ian Rogersd81871c2011-10-03 13:57:23 -07001799 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001800 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001801 break;
1802 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001803 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001804 break;
1805 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001806 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001807 break;
1808 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001809 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001810 break;
1811 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001812 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001813 break;
1814 case Instruction::IPUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001815 VerifyISPut(dec_insn, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001816 break;
jeffhaobdb76512011-09-07 11:43:16 -07001817 case Instruction::IPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001818 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001819 break;
1820
jeffhaobdb76512011-09-07 11:43:16 -07001821 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001822 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001823 break;
jeffhaobdb76512011-09-07 11:43:16 -07001824 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001825 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001826 break;
jeffhaobdb76512011-09-07 11:43:16 -07001827 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001828 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001829 break;
jeffhaobdb76512011-09-07 11:43:16 -07001830 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001831 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001832 break;
1833 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001834 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001835 break;
1836 case Instruction::SGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001837 VerifyISGet(dec_insn, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001838 break;
1839 case Instruction::SGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001840 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001841 break;
1842
1843 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001844 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001845 break;
1846 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001847 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001848 break;
1849 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001850 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001851 break;
1852 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001853 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001854 break;
1855 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001856 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001857 break;
1858 case Instruction::SPUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001859 VerifyISPut(dec_insn, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001860 break;
1861 case Instruction::SPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001862 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001863 break;
1864
1865 case Instruction::INVOKE_VIRTUAL:
1866 case Instruction::INVOKE_VIRTUAL_RANGE:
1867 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001868 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001869 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
1870 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
1871 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
1872 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001873 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL,
1874 is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001875 const char* descriptor;
1876 if (called_method == NULL) {
1877 uint32_t method_idx = dec_insn.vB;
1878 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1879 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1880 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1881 } else {
1882 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07001883 }
Ian Rogersb4903572012-10-11 11:52:56 -07001884 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001885 if (!return_type.IsLowHalf()) {
1886 work_line_->SetResultRegisterType(return_type);
1887 } else {
1888 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
1889 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001890 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001891 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001892 }
jeffhaobdb76512011-09-07 11:43:16 -07001893 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001894 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001895 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001896 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT,
1897 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07001898 const char* return_type_descriptor;
1899 bool is_constructor;
1900 if (called_method == NULL) {
1901 uint32_t method_idx = dec_insn.vB;
1902 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1903 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
1904 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1905 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1906 } else {
1907 is_constructor = called_method->IsConstructor();
1908 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
1909 }
1910 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07001911 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001912 * Some additional checks when calling a constructor. We know from the invocation arg check
1913 * that the "this" argument is an instance of called_method->klass. Now we further restrict
1914 * that to require that called_method->klass is the same as this->klass or this->super,
1915 * allowing the latter only if the "this" argument is the same as the "this" argument to
1916 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07001917 */
jeffhaob57e9522012-04-26 18:08:21 -07001918 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1919 if (this_type.IsConflict()) // failure.
1920 break;
jeffhaobdb76512011-09-07 11:43:16 -07001921
jeffhaob57e9522012-04-26 18:08:21 -07001922 /* no null refs allowed (?) */
1923 if (this_type.IsZero()) {
1924 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
1925 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07001926 }
jeffhaob57e9522012-04-26 18:08:21 -07001927
1928 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07001929 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
1930 // TODO: re-enable constructor type verification
1931 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07001932 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07001933 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
1934 // break;
1935 // }
jeffhaob57e9522012-04-26 18:08:21 -07001936
1937 /* arg must be an uninitialized reference */
1938 if (!this_type.IsUninitializedTypes()) {
1939 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
1940 << this_type;
1941 break;
1942 }
1943
1944 /*
1945 * Replace the uninitialized reference with an initialized one. We need to do this for all
1946 * registers that have the same object instance in them, not just the "this" register.
1947 */
1948 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001949 }
Ian Rogersb4903572012-10-11 11:52:56 -07001950 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
1951 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001952 if (!return_type.IsLowHalf()) {
1953 work_line_->SetResultRegisterType(return_type);
1954 } else {
1955 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
1956 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001957 just_set_result = true;
1958 break;
1959 }
1960 case Instruction::INVOKE_STATIC:
1961 case Instruction::INVOKE_STATIC_RANGE: {
1962 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001963 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001964 const char* descriptor;
1965 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001966 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001967 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1968 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07001969 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001970 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001971 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001972 }
Ian Rogersb4903572012-10-11 11:52:56 -07001973 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001974 if (!return_type.IsLowHalf()) {
1975 work_line_->SetResultRegisterType(return_type);
1976 } else {
1977 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
1978 }
jeffhaobdb76512011-09-07 11:43:16 -07001979 just_set_result = true;
1980 }
1981 break;
jeffhaobdb76512011-09-07 11:43:16 -07001982 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001983 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001984 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001985 mirror::AbstractMethod* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001986 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001987 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001988 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
1989 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
1990 << PrettyMethod(abs_method) << "'";
1991 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001992 }
Ian Rogers0d604842012-04-16 14:50:24 -07001993 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001994 /* Get the type of the "this" arg, which should either be a sub-interface of called
1995 * interface or Object (see comments in RegType::JoinClass).
1996 */
1997 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1998 if (this_type.IsZero()) {
1999 /* null pointer always passes (and always fails at runtime) */
2000 } else {
2001 if (this_type.IsUninitializedTypes()) {
2002 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2003 << this_type;
2004 break;
2005 }
2006 // In the past we have tried to assert that "called_interface" is assignable
2007 // from "this_type.GetClass()", however, as we do an imprecise Join
2008 // (RegType::JoinClass) we don't have full information on what interfaces are
2009 // implemented by "this_type". For example, two classes may implement the same
2010 // interfaces and have a common parent that doesn't implement the interface. The
2011 // join will set "this_type" to the parent class and a test that this implements
2012 // the interface will incorrectly fail.
2013 }
2014 /*
2015 * We don't have an object instance, so we can't find the concrete method. However, all of
2016 * the type information is in the abstract method, so we're good.
2017 */
2018 const char* descriptor;
2019 if (abs_method == NULL) {
2020 uint32_t method_idx = dec_insn.vB;
2021 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2022 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2023 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2024 } else {
2025 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2026 }
Ian Rogersb4903572012-10-11 11:52:56 -07002027 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002028 if (!return_type.IsLowHalf()) {
2029 work_line_->SetResultRegisterType(return_type);
2030 } else {
2031 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2032 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002033 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002034 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002035 }
jeffhaobdb76512011-09-07 11:43:16 -07002036 case Instruction::NEG_INT:
2037 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002038 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002039 break;
2040 case Instruction::NEG_LONG:
2041 case Instruction::NOT_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002042 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2043 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002044 break;
2045 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002046 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002047 break;
2048 case Instruction::NEG_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002049 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2050 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002051 break;
2052 case Instruction::INT_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002053 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2054 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002055 break;
2056 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002057 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002058 break;
2059 case Instruction::INT_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002060 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2061 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002062 break;
2063 case Instruction::LONG_TO_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002064 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Integer(),
2065 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002066 break;
2067 case Instruction::LONG_TO_FLOAT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002068 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Float(),
2069 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002070 break;
2071 case Instruction::LONG_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002072 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2073 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002074 break;
2075 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002076 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002077 break;
2078 case Instruction::FLOAT_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002079 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2080 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002081 break;
2082 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002083 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2084 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002085 break;
2086 case Instruction::DOUBLE_TO_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002087 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Integer(),
2088 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002089 break;
2090 case Instruction::DOUBLE_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002091 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2092 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002093 break;
2094 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002095 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Float(),
2096 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002097 break;
2098 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002099 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002100 break;
2101 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002102 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002103 break;
2104 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002105 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002106 break;
2107
2108 case Instruction::ADD_INT:
2109 case Instruction::SUB_INT:
2110 case Instruction::MUL_INT:
2111 case Instruction::REM_INT:
2112 case Instruction::DIV_INT:
2113 case Instruction::SHL_INT:
2114 case Instruction::SHR_INT:
2115 case Instruction::USHR_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002116 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(),
2117 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002118 break;
2119 case Instruction::AND_INT:
2120 case Instruction::OR_INT:
2121 case Instruction::XOR_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002122 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(),
2123 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002124 break;
2125 case Instruction::ADD_LONG:
2126 case Instruction::SUB_LONG:
2127 case Instruction::MUL_LONG:
2128 case Instruction::DIV_LONG:
2129 case Instruction::REM_LONG:
2130 case Instruction::AND_LONG:
2131 case Instruction::OR_LONG:
2132 case Instruction::XOR_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002133 work_line_->CheckBinaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2134 reg_types_.LongLo(), reg_types_.LongHi(),
2135 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002136 break;
2137 case Instruction::SHL_LONG:
2138 case Instruction::SHR_LONG:
2139 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002140 /* shift distance is Int, making these different from other binary operations */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002141 work_line_->CheckBinaryOpWideShift(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2142 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002143 break;
2144 case Instruction::ADD_FLOAT:
2145 case Instruction::SUB_FLOAT:
2146 case Instruction::MUL_FLOAT:
2147 case Instruction::DIV_FLOAT:
2148 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002149 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002150 break;
2151 case Instruction::ADD_DOUBLE:
2152 case Instruction::SUB_DOUBLE:
2153 case Instruction::MUL_DOUBLE:
2154 case Instruction::DIV_DOUBLE:
2155 case Instruction::REM_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002156 work_line_->CheckBinaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2157 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2158 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002159 break;
2160 case Instruction::ADD_INT_2ADDR:
2161 case Instruction::SUB_INT_2ADDR:
2162 case Instruction::MUL_INT_2ADDR:
2163 case Instruction::REM_INT_2ADDR:
2164 case Instruction::SHL_INT_2ADDR:
2165 case Instruction::SHR_INT_2ADDR:
2166 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002167 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002168 break;
2169 case Instruction::AND_INT_2ADDR:
2170 case Instruction::OR_INT_2ADDR:
2171 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002172 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002173 break;
2174 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002175 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002176 break;
2177 case Instruction::ADD_LONG_2ADDR:
2178 case Instruction::SUB_LONG_2ADDR:
2179 case Instruction::MUL_LONG_2ADDR:
2180 case Instruction::DIV_LONG_2ADDR:
2181 case Instruction::REM_LONG_2ADDR:
2182 case Instruction::AND_LONG_2ADDR:
2183 case Instruction::OR_LONG_2ADDR:
2184 case Instruction::XOR_LONG_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002185 work_line_->CheckBinaryOp2addrWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2186 reg_types_.LongLo(), reg_types_.LongHi(),
2187 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002188 break;
2189 case Instruction::SHL_LONG_2ADDR:
2190 case Instruction::SHR_LONG_2ADDR:
2191 case Instruction::USHR_LONG_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002192 work_line_->CheckBinaryOp2addrWideShift(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2193 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002194 break;
2195 case Instruction::ADD_FLOAT_2ADDR:
2196 case Instruction::SUB_FLOAT_2ADDR:
2197 case Instruction::MUL_FLOAT_2ADDR:
2198 case Instruction::DIV_FLOAT_2ADDR:
2199 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002200 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002201 break;
2202 case Instruction::ADD_DOUBLE_2ADDR:
2203 case Instruction::SUB_DOUBLE_2ADDR:
2204 case Instruction::MUL_DOUBLE_2ADDR:
2205 case Instruction::DIV_DOUBLE_2ADDR:
2206 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002207 work_line_->CheckBinaryOp2addrWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2208 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2209 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002210 break;
2211 case Instruction::ADD_INT_LIT16:
2212 case Instruction::RSUB_INT:
2213 case Instruction::MUL_INT_LIT16:
2214 case Instruction::DIV_INT_LIT16:
2215 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002216 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002217 break;
2218 case Instruction::AND_INT_LIT16:
2219 case Instruction::OR_INT_LIT16:
2220 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002221 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002222 break;
2223 case Instruction::ADD_INT_LIT8:
2224 case Instruction::RSUB_INT_LIT8:
2225 case Instruction::MUL_INT_LIT8:
2226 case Instruction::DIV_INT_LIT8:
2227 case Instruction::REM_INT_LIT8:
2228 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002229 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002230 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002231 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002232 break;
2233 case Instruction::AND_INT_LIT8:
2234 case Instruction::OR_INT_LIT8:
2235 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002236 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002237 break;
2238
Ian Rogersd81871c2011-10-03 13:57:23 -07002239 /* These should never appear during verification. */
jeffhao9a4f0032012-08-30 16:17:40 -07002240 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002241 case Instruction::UNUSED_EE:
2242 case Instruction::UNUSED_EF:
2243 case Instruction::UNUSED_F2:
2244 case Instruction::UNUSED_F3:
2245 case Instruction::UNUSED_F4:
2246 case Instruction::UNUSED_F5:
2247 case Instruction::UNUSED_F6:
2248 case Instruction::UNUSED_F7:
2249 case Instruction::UNUSED_F8:
2250 case Instruction::UNUSED_F9:
2251 case Instruction::UNUSED_FA:
2252 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002253 case Instruction::UNUSED_F0:
2254 case Instruction::UNUSED_F1:
2255 case Instruction::UNUSED_E3:
2256 case Instruction::UNUSED_E8:
2257 case Instruction::UNUSED_E7:
2258 case Instruction::UNUSED_E4:
2259 case Instruction::UNUSED_E9:
2260 case Instruction::UNUSED_FC:
2261 case Instruction::UNUSED_E5:
2262 case Instruction::UNUSED_EA:
2263 case Instruction::UNUSED_FD:
2264 case Instruction::UNUSED_E6:
2265 case Instruction::UNUSED_EB:
2266 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002267 case Instruction::UNUSED_3E:
2268 case Instruction::UNUSED_3F:
2269 case Instruction::UNUSED_40:
2270 case Instruction::UNUSED_41:
2271 case Instruction::UNUSED_42:
2272 case Instruction::UNUSED_43:
2273 case Instruction::UNUSED_73:
2274 case Instruction::UNUSED_79:
2275 case Instruction::UNUSED_7A:
2276 case Instruction::UNUSED_EC:
2277 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002278 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002279 break;
2280
2281 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002282 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002283 * complain if an instruction is missing (which is desirable).
2284 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002285 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002286
Ian Rogersad0b3a32012-04-16 14:50:24 -07002287 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002288 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002289 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002290 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002291 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002292 /* immediate failure, reject class */
2293 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2294 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002295 } else if (have_pending_runtime_throw_failure_) {
2296 /* slow path will throw, mark following code as unreachable */
2297 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002298 }
jeffhaobdb76512011-09-07 11:43:16 -07002299 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002300 * If we didn't just set the result register, clear it out. This ensures that you can only use
2301 * "move-result" immediately after the result is set. (We could check this statically, but it's
2302 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002303 */
2304 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002305 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002306 }
2307
jeffhaoa0a764a2011-09-16 10:43:38 -07002308 /* Handle "continue". Tag the next consecutive instruction. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002309 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers776ac1f2012-04-13 23:36:36 -07002310 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
Ian Rogersd81871c2011-10-03 13:57:23 -07002311 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
jeffhaod5347e02012-03-22 17:25:05 -07002312 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002313 return false;
2314 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002315 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2316 // next instruction isn't one.
jeffhaod5347e02012-03-22 17:25:05 -07002317 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002318 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002319 }
2320 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2321 if (next_line != NULL) {
2322 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2323 // needed.
2324 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002325 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002326 }
jeffhaobdb76512011-09-07 11:43:16 -07002327 } else {
2328 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002329 * We're not recording register data for the next instruction, so we don't know what the prior
2330 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002331 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002332 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002333 }
2334 }
2335
2336 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002337 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002338 *
2339 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002340 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002341 * somebody could get a reference field, check it for zero, and if the
2342 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002343 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002344 * that, and will reject the code.
2345 *
2346 * TODO: avoid re-fetching the branch target
2347 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002348 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002349 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002350 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002351 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002352 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002353 return false;
2354 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002355 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002356 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002357 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002358 }
jeffhaobdb76512011-09-07 11:43:16 -07002359 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002360 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002361 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002362 }
jeffhaobdb76512011-09-07 11:43:16 -07002363 }
2364
2365 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002366 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002367 *
2368 * We've already verified that the table is structurally sound, so we
2369 * just need to walk through and tag the targets.
2370 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002371 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002372 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2373 const uint16_t* switch_insns = insns + offset_to_switch;
2374 int switch_count = switch_insns[1];
2375 int offset_to_targets, targ;
2376
2377 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2378 /* 0 = sig, 1 = count, 2/3 = first key */
2379 offset_to_targets = 4;
2380 } else {
2381 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002382 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002383 offset_to_targets = 2 + 2 * switch_count;
2384 }
2385
2386 /* verify each switch target */
2387 for (targ = 0; targ < switch_count; targ++) {
2388 int offset;
2389 uint32_t abs_offset;
2390
2391 /* offsets are 32-bit, and only partly endian-swapped */
2392 offset = switch_insns[offset_to_targets + targ * 2] |
2393 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002394 abs_offset = work_insn_idx_ + offset;
2395 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002396 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002397 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002398 }
2399 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002400 return false;
2401 }
2402 }
2403
2404 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002405 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2406 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002407 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002408 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002409 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002410 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002411
Ian Rogers0571d352011-11-03 19:51:38 -07002412 for (; iterator.HasNext(); iterator.Next()) {
2413 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002414 within_catch_all = true;
2415 }
jeffhaobdb76512011-09-07 11:43:16 -07002416 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002417 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2418 * "work_regs", because at runtime the exception will be thrown before the instruction
2419 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002420 */
Ian Rogers0571d352011-11-03 19:51:38 -07002421 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002422 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002423 }
jeffhaobdb76512011-09-07 11:43:16 -07002424 }
2425
2426 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002427 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2428 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002429 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002430 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002431 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002432 * The state in work_line reflects the post-execution state. If the current instruction is a
2433 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002434 * it will do so before grabbing the lock).
2435 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002436 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002437 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002438 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002439 return false;
2440 }
2441 }
2442 }
2443
jeffhaod1f0fde2011-09-08 17:25:33 -07002444 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002445 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002446 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002447 return false;
2448 }
jeffhaobdb76512011-09-07 11:43:16 -07002449 }
2450
2451 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002452 * Update start_guess. Advance to the next instruction of that's
2453 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002454 * neither of those exists we're in a return or throw; leave start_guess
2455 * alone and let the caller sort it out.
2456 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002457 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002458 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002459 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002460 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002461 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002462 }
2463
Ian Rogersd81871c2011-10-03 13:57:23 -07002464 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2465 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002466
2467 return true;
2468}
2469
Ian Rogers776ac1f2012-04-13 23:36:36 -07002470const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002471 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002472 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002473 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002474 const RegType& result =
Ian Rogersb4903572012-10-11 11:52:56 -07002475 klass != NULL ? reg_types_.FromClass(klass, klass->IsFinal())
2476 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002477 if (result.IsConflict()) {
2478 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2479 << "' in " << referrer;
2480 return result;
2481 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002482 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002483 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002484 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002485 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002486 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002487 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002488 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002489 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002490 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002491 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002492}
2493
Ian Rogers776ac1f2012-04-13 23:36:36 -07002494const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002495 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002496 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002497 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002498 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2499 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002500 CatchHandlerIterator iterator(handlers_ptr);
2501 for (; iterator.HasNext(); iterator.Next()) {
2502 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2503 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002504 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002505 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002506 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002507 if (common_super == NULL) {
2508 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2509 // as that is caught at runtime
2510 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002511 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002512 // We don't know enough about the type and the common path merge will result in
2513 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002514 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002515 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002516 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002517 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002518 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002519 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002520 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002521 }
2522 }
2523 }
2524 }
Ian Rogers0571d352011-11-03 19:51:38 -07002525 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002526 }
2527 }
2528 if (common_super == NULL) {
2529 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002530 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002531 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002532 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002533 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002534}
2535
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002536mirror::AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
2537 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002538 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002539 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002540 if (klass_type.IsConflict()) {
2541 std::string append(" in attempt to access method ");
2542 append += dex_file_->GetMethodName(method_id);
2543 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002544 return NULL;
2545 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002546 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002547 return NULL; // Can't resolve Class so no more to do here
2548 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002549 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002550 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002551 mirror::AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002552 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002553 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002554 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002555
2556 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002557 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002558 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002559 res_method = klass->FindInterfaceMethod(name, signature);
2560 } else {
2561 res_method = klass->FindVirtualMethod(name, signature);
2562 }
2563 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002564 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002565 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002566 // If a virtual or interface method wasn't found with the expected type, look in
2567 // the direct methods. This can happen when the wrong invoke type is used or when
2568 // a class has changed, and will be flagged as an error in later checks.
2569 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2570 res_method = klass->FindDirectMethod(name, signature);
2571 }
2572 if (res_method == NULL) {
2573 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2574 << PrettyDescriptor(klass) << "." << name
2575 << " " << signature;
2576 return NULL;
2577 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002578 }
2579 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002580 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2581 // enforce them here.
2582 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002583 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2584 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002585 return NULL;
2586 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002587 // Disallow any calls to class initializers.
2588 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002589 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2590 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002591 return NULL;
2592 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002593 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002594 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002595 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002596 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002597 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002598 }
jeffhaode0d9c92012-02-27 13:58:13 -08002599 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2600 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002601 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2602 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002603 return NULL;
2604 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002605 // Check that interface methods match interface classes.
2606 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2607 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2608 << " is in an interface class " << PrettyClass(klass);
2609 return NULL;
2610 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2611 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2612 << " is in a non-interface class " << PrettyClass(klass);
2613 return NULL;
2614 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002615 // See if the method type implied by the invoke instruction matches the access flags for the
2616 // target method.
2617 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2618 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2619 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2620 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002621 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2622 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002623 return NULL;
2624 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002625 return res_method;
2626}
2627
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002628mirror::AbstractMethod* MethodVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
2629 MethodType method_type, bool is_range,
2630 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002631 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2632 // we're making.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002633 mirror::AbstractMethod* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002634 if (res_method == NULL) { // error or class is unresolved
2635 return NULL;
2636 }
2637
Ian Rogersd81871c2011-10-03 13:57:23 -07002638 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2639 // has a vtable entry for the target method.
2640 if (is_super) {
2641 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002642 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002643 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002644 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002645 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002646 << " to super " << PrettyMethod(res_method);
2647 return NULL;
2648 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002649 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002650 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002651 MethodHelper mh(res_method);
2652 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002653 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002654 << " to super " << super
2655 << "." << mh.GetName()
2656 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002657 return NULL;
2658 }
2659 }
2660 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2661 // match the call to the signature. Also, we might might be calling through an abstract method
2662 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08002663 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002664 /* caught by static verifier */
2665 DCHECK(is_range || expected_args <= 5);
2666 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002667 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002668 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2669 return NULL;
2670 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002671
jeffhaobdb76512011-09-07 11:43:16 -07002672 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002673 * Check the "this" argument, which must be an instance of the class that declared the method.
2674 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2675 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002676 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002677 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002678 if (!res_method->IsStatic()) {
2679 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002680 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002681 return NULL;
2682 }
2683 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002684 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002685 return NULL;
2686 }
2687 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002688 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogersb4903572012-10-11 11:52:56 -07002689 const RegType& res_method_class = reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogers9074b992011-10-26 17:41:55 -07002690 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002691 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002692 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002693 return NULL;
2694 }
2695 }
2696 actual_args++;
2697 }
2698 /*
2699 * Process the target method's signature. This signature may or may not
2700 * have been verified, so we can't assume it's properly formed.
2701 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002702 MethodHelper mh(res_method);
2703 const DexFile::TypeList* params = mh.GetParameterTypeList();
2704 size_t params_size = params == NULL ? 0 : params->Size();
2705 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002706 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002707 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002708 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2709 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002710 return NULL;
2711 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002712 const char* descriptor =
2713 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2714 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002715 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002716 << " missing signature component";
2717 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002718 }
Ian Rogersb4903572012-10-11 11:52:56 -07002719 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002720 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002721 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002722 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002723 }
2724 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2725 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002726 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002727 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002728 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002729 return NULL;
2730 } else {
2731 return res_method;
2732 }
2733}
2734
Ian Rogers776ac1f2012-04-13 23:36:36 -07002735void MethodVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08002736 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002737 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002738 if (res_type.IsConflict()) { // bad class
2739 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002740 } else {
2741 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2742 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002743 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002744 } else if (!is_filled) {
2745 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002746 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002747 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002748 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002749 } else {
2750 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2751 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002752 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002753 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002754 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002755 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002756 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002757 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002758 return;
2759 }
2760 }
2761 // filled-array result goes into "result" register
2762 work_line_->SetResultRegisterType(res_type);
2763 }
2764 }
2765}
2766
Ian Rogers776ac1f2012-04-13 23:36:36 -07002767void MethodVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002768 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002769 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002770 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002771 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002772 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002773 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002774 if (array_type.IsZero()) {
2775 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2776 // instruction type. TODO: have a proper notion of bottom here.
2777 if (!is_primitive || insn_type.IsCategory1Types()) {
2778 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08002779 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002780 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002781 // Category 2
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002782 work_line_->SetRegisterTypeWide(dec_insn.vA, reg_types_.FromCat2ConstLo(0, false),
2783 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08002784 }
jeffhaofc3144e2012-02-01 17:21:15 -08002785 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002786 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002787 } else {
2788 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002789 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002790 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002791 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002792 << " source for aget-object";
2793 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002794 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002795 << " source for category 1 aget";
2796 } else if (is_primitive && !insn_type.Equals(component_type) &&
2797 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002798 (insn_type.IsLong() && component_type.IsDouble()))) {
2799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
2800 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002801 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002802 // Use knowledge of the field type which is stronger than the type inferred from the
2803 // instruction, which can't differentiate object types and ints from floats, longs from
2804 // doubles.
2805 if (!component_type.IsLowHalf()) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002806 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002807 } else {
2808 work_line_->SetRegisterTypeWide(dec_insn.vA, component_type,
2809 component_type.HighHalf(&reg_types_));
2810 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002811 }
2812 }
2813 }
2814}
2815
Ian Rogers776ac1f2012-04-13 23:36:36 -07002816void MethodVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002818 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002819 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002820 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002821 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002822 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002823 if (array_type.IsZero()) {
2824 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2825 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002826 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002827 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002828 } else {
2829 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002830 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002831 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002832 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002833 << " source for aput-object";
2834 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002835 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002836 << " source for category 1 aput";
2837 } else if (is_primitive && !insn_type.Equals(component_type) &&
2838 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2839 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002840 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002841 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002842 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002843 // The instruction agrees with the type of array, confirm the value to be stored does too
2844 // Note: we use the instruction type (rather than the component type) for aput-object as
2845 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08002846 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002847 }
2848 }
2849 }
2850}
2851
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002852mirror::Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002853 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2854 // Check access to class
2855 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002856 if (klass_type.IsConflict()) { // bad class
2857 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
2858 field_idx, dex_file_->GetFieldName(field_id),
2859 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002860 return NULL;
2861 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002862 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002863 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08002864 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002865 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07002866 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002867 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002868 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
2869 << dex_file_->GetFieldName(field_id) << ") in "
2870 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002871 DCHECK(Thread::Current()->IsExceptionPending());
2872 Thread::Current()->ClearException();
2873 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002874 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2875 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002876 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002877 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002878 return NULL;
2879 } else if (!field->IsStatic()) {
2880 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
2881 return NULL;
2882 } else {
2883 return field;
2884 }
2885}
2886
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002887mirror::Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002888 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2889 // Check access to class
2890 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002891 if (klass_type.IsConflict()) {
2892 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
2893 field_idx, dex_file_->GetFieldName(field_id),
2894 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002895 return NULL;
2896 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002897 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002898 return NULL; // Can't resolve Class so no more to do here
2899 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002900 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07002901 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002902 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002903 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
2904 << dex_file_->GetFieldName(field_id) << ") in "
2905 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002906 DCHECK(Thread::Current()->IsExceptionPending());
2907 Thread::Current()->ClearException();
2908 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002909 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2910 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002911 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002912 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002913 return NULL;
2914 } else if (field->IsStatic()) {
2915 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
2916 << " to not be static";
2917 return NULL;
2918 } else if (obj_type.IsZero()) {
2919 // Cannot infer and check type, however, access will cause null pointer exception
2920 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07002921 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002922 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersb4903572012-10-11 11:52:56 -07002923 const RegType& field_klass = reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002924 if (obj_type.IsUninitializedTypes() &&
2925 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
2926 !field_klass.Equals(GetDeclaringClass()))) {
2927 // Field accesses through uninitialized references are only allowable for constructors where
2928 // the field is declared in this class
2929 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
2930 << " of a not fully initialized object within the context of "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002931 << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002932 return NULL;
2933 } else if (!field_klass.IsAssignableFrom(obj_type)) {
2934 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
2935 // of C1. For resolution to occur the declared class of the field must be compatible with
2936 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
2937 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
2938 << " from object of type " << obj_type;
2939 return NULL;
2940 } else {
2941 return field;
2942 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002943 }
2944}
2945
Ian Rogers776ac1f2012-04-13 23:36:36 -07002946void MethodVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002947 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002948 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002949 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002950 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002951 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002952 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002953 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07002954 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002955 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002956 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002957 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002958 if (field != NULL) {
2959 descriptor = FieldHelper(field).GetTypeDescriptor();
2960 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07002961 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002962 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2963 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2964 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07002965 }
Ian Rogersb4903572012-10-11 11:52:56 -07002966 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002967 if (is_primitive) {
2968 if (field_type.Equals(insn_type) ||
2969 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
2970 (field_type.IsDouble() && insn_type.IsLongTypes())) {
2971 // expected that read is of the correct primitive type or that int reads are reading
2972 // floats or long reads are reading doubles
2973 } else {
2974 // This is a global failure rather than a class change failure as the instructions and
2975 // the descriptors for the type should have been consistent within the same file at
2976 // compile time
2977 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
2978 << " to be of type '" << insn_type
2979 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002980 return;
2981 }
2982 } else {
2983 if (!insn_type.IsAssignableFrom(field_type)) {
2984 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
2985 << " to be compatible with type '" << insn_type
2986 << "' but found type '" << field_type
2987 << "' in get-object";
2988 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Conflict());
2989 return;
2990 }
2991 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002992 if (!field_type.IsLowHalf()) {
2993 work_line_->SetRegisterType(dec_insn.vA, field_type);
2994 } else {
2995 work_line_->SetRegisterTypeWide(dec_insn.vA, field_type, field_type.HighHalf(&reg_types_));
2996 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002997}
2998
Ian Rogers776ac1f2012-04-13 23:36:36 -07002999void MethodVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003000 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003001 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003002 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003003 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003004 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003005 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003006 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07003007 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003008 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003009 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003010 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003011 if (field != NULL) {
3012 descriptor = FieldHelper(field).GetTypeDescriptor();
3013 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003014 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003015 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3016 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3017 loader = class_loader_;
3018 }
Ian Rogersb4903572012-10-11 11:52:56 -07003019 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003020 if (field != NULL) {
3021 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3022 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3023 << " from other class " << GetDeclaringClass();
3024 return;
3025 }
3026 }
3027 if (is_primitive) {
3028 // Primitive field assignability rules are weaker than regular assignability rules
3029 bool instruction_compatible;
3030 bool value_compatible;
3031 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
3032 if (field_type.IsIntegralTypes()) {
3033 instruction_compatible = insn_type.IsIntegralTypes();
3034 value_compatible = value_type.IsIntegralTypes();
3035 } else if (field_type.IsFloat()) {
3036 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3037 value_compatible = value_type.IsFloatTypes();
3038 } else if (field_type.IsLong()) {
3039 instruction_compatible = insn_type.IsLong();
3040 value_compatible = value_type.IsLongTypes();
3041 } else if (field_type.IsDouble()) {
3042 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3043 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003044 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003045 instruction_compatible = false; // reference field with primitive store
3046 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003047 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003048 if (!instruction_compatible) {
3049 // This is a global failure rather than a class change failure as the instructions and
3050 // the descriptors for the type should have been consistent within the same file at
3051 // compile time
3052 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3053 << " to be of type '" << insn_type
3054 << "' but found type '" << field_type
3055 << "' in put";
3056 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003057 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003058 if (!value_compatible) {
3059 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
3060 << " of type " << value_type
3061 << " but expected " << field_type
3062 << " for store to " << PrettyField(field) << " in put";
3063 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003064 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003065 } else {
3066 if (!insn_type.IsAssignableFrom(field_type)) {
3067 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3068 << " to be compatible with type '" << insn_type
3069 << "' but found type '" << field_type
3070 << "' in put-object";
3071 return;
3072 }
3073 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003074 }
3075}
3076
Ian Rogers776ac1f2012-04-13 23:36:36 -07003077bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003078 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003079 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003080 return false;
3081 }
3082 return true;
3083}
3084
Ian Rogers776ac1f2012-04-13 23:36:36 -07003085bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003086 bool changed = true;
3087 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3088 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003089 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003090 * We haven't processed this instruction before, and we haven't touched the registers here, so
3091 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3092 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003093 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003094 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003095 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003096 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3097 if (gDebugVerify) {
3098 copy->CopyFromLine(target_line);
3099 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003100 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003101 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003102 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003103 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003104 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003105 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003106 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3107 << *copy.get() << " MERGE\n"
3108 << *merge_line << " ==\n"
3109 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003110 }
3111 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003112 if (changed) {
3113 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003114 }
3115 return true;
3116}
3117
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003118InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003119 return &insn_flags_[work_insn_idx_];
3120}
3121
Ian Rogersad0b3a32012-04-16 14:50:24 -07003122const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003123 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003124 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3125 uint16_t return_type_idx = proto_id.return_type_idx_;
3126 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003127 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003128}
3129
3130const RegType& MethodVerifier::GetDeclaringClass() {
3131 if (foo_method_ != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003132 mirror::Class* klass = foo_method_->GetDeclaringClass();
Ian Rogersb4903572012-10-11 11:52:56 -07003133 return reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003134 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003135 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003136 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogersb4903572012-10-11 11:52:56 -07003137 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003138 }
3139}
3140
Ian Rogers776ac1f2012-04-13 23:36:36 -07003141void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003142 size_t* log2_max_gc_pc) {
3143 size_t local_gc_points = 0;
3144 size_t max_insn = 0;
3145 size_t max_ref_reg = -1;
3146 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003147 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003148 local_gc_points++;
3149 max_insn = i;
3150 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003151 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003152 }
3153 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003154 *gc_points = local_gc_points;
3155 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3156 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003157 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003158 i++;
3159 }
3160 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003161}
3162
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003163MethodVerifier::PcToConreteMethod* MethodVerifier::GenerateDevirtMap() {
3164
3165 // It is risky to rely on reg_types for sharpening in cases of soft
3166 // verification, we might end up sharpening to a wrong implementation. Just abort.
3167 if (!failure_messages_.empty()) {
3168 return NULL;
3169 }
3170
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003171 UniquePtr<PcToConreteMethod> pc_to_concrete_method(new PcToConreteMethod());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003172 uint32_t dex_pc = 0;
3173 const uint16_t* insns = code_item_->insns_ ;
3174 const Instruction* inst = Instruction::At(insns);
3175
3176 for (; dex_pc < code_item_->insns_size_in_code_units_;
3177 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits(), inst = inst->Next()) {
3178
3179 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3180 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3181 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3182 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3183
3184 if(!(is_interface || is_virtual))
3185 continue;
3186
3187 // Check if vC ("this" pointer in the instruction) has a precise type.
3188 RegisterLine* line = reg_table_.GetLine(dex_pc);
3189 DecodedInstruction dec_insn(inst);
3190 const RegType& reg_type(line->GetRegisterType(dec_insn.vC));
3191
3192 if (!reg_type.IsPreciseReference()) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003193 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003194 }
3195
3196 CHECK(!(reg_type.GetClass()->IsInterface()));
3197 // If the class is an array class, it can be both Abstract and final and so
3198 // the reg_type will be created as precise.
3199 CHECK(!(reg_type.GetClass()->IsAbstract()) || reg_type.GetClass()->IsArrayClass());
3200 // Find the abstract method.
3201 // vB has the method index.
3202 mirror::AbstractMethod* abstract_method = NULL ;
3203 abstract_method = dex_cache_->GetResolvedMethod(dec_insn.vB);
3204 if(abstract_method == NULL) {
3205 // If the method is not found in the cache this means that it was never found
3206 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
3207 continue;
3208 }
3209 // Find the concrete method.
3210 mirror::AbstractMethod* concrete_method = NULL;
3211 if (is_interface) {
3212 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
3213 }
3214 if (is_virtual) {
3215 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
3216 }
3217
3218 if(concrete_method == NULL) {
3219 // In cases where concrete_method is not found continue to the next invoke instead
3220 // of crashing.
3221 continue;
3222 }
3223
3224 CHECK(!concrete_method->IsAbstract()) << PrettyMethod(concrete_method);
3225 // Build method reference.
3226 CompilerDriver::MethodReference concrete_ref(
3227 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
3228 concrete_method->GetDexMethodIndex());
3229 // Now Save the current PC and the concrete method reference to be used
3230 // in compiler driver.
3231 pc_to_concrete_method->Put(dex_pc, concrete_ref );
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003232 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003233
3234 if (pc_to_concrete_method->size() == 0) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003235 return NULL ;
3236 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003237 return pc_to_concrete_method.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003238}
3239
Ian Rogers776ac1f2012-04-13 23:36:36 -07003240const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003241 size_t num_entries, ref_bitmap_bits, pc_bits;
3242 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3243 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003244 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003245 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003246 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003247 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003248 return NULL;
3249 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003250 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3251 // There are 2 bytes to encode the number of entries
3252 if (num_entries >= 65536) {
3253 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003255 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003256 return NULL;
3257 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003258 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003259 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003260 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003261 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003262 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003263 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003264 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003265 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003266 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003267 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003268 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003269 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3270 return NULL;
3271 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003272 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003273 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003274 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003275 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003276 return NULL;
3277 }
3278 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003279 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3280 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003281 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003282 table->push_back(num_entries & 0xFF);
3283 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003284 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003285 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003286 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003287 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003288 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003289 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003290 }
3291 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003292 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003293 }
3294 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003295 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003296 return table;
3297}
jeffhaoa0a764a2011-09-16 10:43:38 -07003298
Ian Rogers776ac1f2012-04-13 23:36:36 -07003299void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003300 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3301 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003302 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003303 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003304 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003305 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003306 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003307 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003308 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003309 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3310 map_index++;
3311 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003312 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003313 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003314 CHECK_LT(j / 8, map.RegWidth());
3315 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3316 } else if ((j / 8) < map.RegWidth()) {
3317 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3318 } else {
3319 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3320 }
3321 }
3322 } else {
3323 CHECK(reg_bitmap == NULL);
3324 }
3325 }
3326}
jeffhaoa0a764a2011-09-16 10:43:38 -07003327
Ian Rogers1212a022013-03-04 10:48:41 -08003328void MethodVerifier::SetDexGcMap(CompilerDriver::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003329 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003330 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003331 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
3332 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003333 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003334 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003335 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003336 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003337 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003338 CHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003339}
3340
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003341void MethodVerifier::SetDevirtMap(CompilerDriver::MethodReference ref, const PcToConreteMethod* devirt_map) {
3342
3343 MutexLock mu(Thread::Current(), *devirt_maps_lock_);
3344 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
3345 if (it != devirt_maps_->end()) {
3346 delete it->second;
3347 devirt_maps_->erase(it);
3348 }
3349
3350 devirt_maps_->Put(ref, devirt_map);
3351 CHECK(devirt_maps_->find(ref) != devirt_maps_->end());
3352}
3353
Ian Rogers1212a022013-03-04 10:48:41 -08003354const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(CompilerDriver::MethodReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003355 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003356 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
3357 if (it == dex_gc_maps_->end()) {
Ian Rogerse3cd2f02013-05-24 15:32:56 -07003358 LOG(WARNING) << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003359 return NULL;
3360 }
3361 CHECK(it->second != NULL);
3362 return it->second;
3363}
3364
Ian Rogerse3cd2f02013-05-24 15:32:56 -07003365const CompilerDriver::MethodReference* MethodVerifier::GetDevirtMap(const CompilerDriver::MethodReference& ref,
3366 uint32_t dex_pc) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003367 MutexLock mu(Thread::Current(), *devirt_maps_lock_);
3368 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
3369 if (it == devirt_maps_->end()) {
3370 return NULL;
3371 }
3372
3373 // Look up the PC in the map, get the concrete method to execute and return its reference.
Ian Rogerse3cd2f02013-05-24 15:32:56 -07003374 MethodVerifier::PcToConreteMethod::const_iterator pc_to_concrete_method = it->second->find(dex_pc);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003375 if(pc_to_concrete_method != it->second->end()) {
3376 return &(pc_to_concrete_method->second);
3377 } else {
3378 return NULL;
3379 }
3380}
3381
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003382std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3383 RegisterLine* line = reg_table_.GetLine(dex_pc);
3384 std::vector<int32_t> result;
3385 for (size_t i = 0; i < line->NumRegs(); ++i) {
3386 const RegType& type = line->GetRegisterType(i);
3387 if (type.IsConstant()) {
3388 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3389 result.push_back(type.ConstantValue());
3390 } else if (type.IsConstantLo()) {
3391 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3392 result.push_back(type.ConstantValueLo());
3393 } else if (type.IsConstantHi()) {
3394 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3395 result.push_back(type.ConstantValueHi());
3396 } else if (type.IsIntegralTypes()) {
3397 result.push_back(kIntVReg);
3398 result.push_back(0);
3399 } else if (type.IsFloat()) {
3400 result.push_back(kFloatVReg);
3401 result.push_back(0);
3402 } else if (type.IsLong()) {
3403 result.push_back(kLongLoVReg);
3404 result.push_back(0);
3405 result.push_back(kLongHiVReg);
3406 result.push_back(0);
3407 ++i;
3408 } else if (type.IsDouble()) {
3409 result.push_back(kDoubleLoVReg);
3410 result.push_back(0);
3411 result.push_back(kDoubleHiVReg);
3412 result.push_back(0);
3413 ++i;
3414 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
3415 result.push_back(kUndefined);
3416 result.push_back(0);
3417 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003418 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003419 result.push_back(kReferenceVReg);
3420 result.push_back(0);
3421 }
3422 }
3423 return result;
3424}
3425
Ian Rogers0c7abda2012-09-19 13:33:42 -07003426Mutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
3427MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003428
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003429Mutex* MethodVerifier::devirt_maps_lock_ = NULL;
3430MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
3431
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003432Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
3433MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
3434
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003435void MethodVerifier::Init() {
Ian Rogers0c7abda2012-09-19 13:33:42 -07003436 dex_gc_maps_lock_ = new Mutex("verifier GC maps lock");
Ian Rogers50b35e22012-10-04 10:09:15 -07003437 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003438 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003439 MutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003440 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003441 }
3442
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003443 devirt_maps_lock_ = new Mutex("verifier Devirtualization lock");
3444 {
3445 MutexLock mu(self, *devirt_maps_lock_);
3446 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
3447 }
3448
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003449 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
3450 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003451 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003452 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
3453 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003454 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003455}
3456
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003457void MethodVerifier::Shutdown() {
Ian Rogers50b35e22012-10-04 10:09:15 -07003458 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003459 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003460 MutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003461 STLDeleteValues(dex_gc_maps_);
3462 delete dex_gc_maps_;
3463 dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003464 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003465 delete dex_gc_maps_lock_;
3466 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003467
3468 {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003469 MutexLock mu(self, *devirt_maps_lock_);
3470 STLDeleteValues(devirt_maps_);
3471 delete devirt_maps_;
3472 devirt_maps_ = NULL;
3473 }
3474 delete devirt_maps_lock_;
3475 devirt_maps_lock_ = NULL;
3476
3477 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003478 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003479 delete rejected_classes_;
3480 rejected_classes_ = NULL;
3481 }
3482 delete rejected_classes_lock_;
3483 rejected_classes_lock_ = NULL;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003484 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003485}
jeffhaod1224c72012-02-29 13:43:08 -08003486
Ian Rogers1212a022013-03-04 10:48:41 -08003487void MethodVerifier::AddRejectedClass(CompilerDriver::ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003488 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003489 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003490 rejected_classes_->insert(ref);
3491 }
jeffhaod1224c72012-02-29 13:43:08 -08003492 CHECK(IsClassRejected(ref));
3493}
3494
Ian Rogers1212a022013-03-04 10:48:41 -08003495bool MethodVerifier::IsClassRejected(CompilerDriver::ClassReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003496 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003497 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08003498}
3499
Ian Rogersd81871c2011-10-03 13:57:23 -07003500} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003501} // namespace art