blob: 2b70e265d43b959397e162491815227f890c1f1f [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;
59 case kTrackRegsGcPoints:
60 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
61 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();
Ian Rogers0c7abda2012-09-19 13:33:42 -0700499 insn_flags_[0].SetGcPoint();
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 */
508 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
509 insn_flags_[dex_pc].SetGcPoint();
510 }
511 dex_pc += inst->SizeInCodeUnits();
512 inst = inst->Next();
513 }
514 return true;
515}
516
Ian Rogers776ac1f2012-04-13 23:36:36 -0700517bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800518 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700519 bool result = true;
520 switch (inst->GetVerifyTypeArgumentA()) {
521 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800522 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700523 break;
524 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800525 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700526 break;
527 }
528 switch (inst->GetVerifyTypeArgumentB()) {
529 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800530 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700531 break;
532 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800533 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700534 break;
535 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800536 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700537 break;
538 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800539 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700540 break;
541 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800542 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700543 break;
544 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800545 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700546 break;
547 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800548 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700549 break;
550 }
551 switch (inst->GetVerifyTypeArgumentC()) {
552 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800553 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700554 break;
555 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800556 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700557 break;
558 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800559 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700560 break;
561 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800562 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700563 break;
564 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800565 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700566 break;
567 }
568 switch (inst->GetVerifyExtraFlags()) {
569 case Instruction::kVerifyArrayData:
570 result = result && CheckArrayData(code_offset);
571 break;
572 case Instruction::kVerifyBranchTarget:
573 result = result && CheckBranchTarget(code_offset);
574 break;
575 case Instruction::kVerifySwitchTargets:
576 result = result && CheckSwitchTargets(code_offset);
577 break;
578 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800579 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700580 break;
581 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800582 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700583 break;
584 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700585 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700586 result = false;
587 break;
588 }
589 return result;
590}
591
Ian Rogers776ac1f2012-04-13 23:36:36 -0700592bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700593 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700594 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
595 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700596 return false;
597 }
598 return true;
599}
600
Ian Rogers776ac1f2012-04-13 23:36:36 -0700601bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700602 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700603 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
604 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700605 return false;
606 }
607 return true;
608}
609
Ian Rogers776ac1f2012-04-13 23:36:36 -0700610bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700611 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700612 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
613 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700614 return false;
615 }
616 return true;
617}
618
Ian Rogers776ac1f2012-04-13 23:36:36 -0700619bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700620 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700621 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
622 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700623 return false;
624 }
625 return true;
626}
627
Ian Rogers776ac1f2012-04-13 23:36:36 -0700628bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700629 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700630 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
631 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700632 return false;
633 }
634 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700635 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700636 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700637 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700638 return false;
639 }
640 return true;
641}
642
Ian Rogers776ac1f2012-04-13 23:36:36 -0700643bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700644 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700645 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
646 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700647 return false;
648 }
649 return true;
650}
651
Ian Rogers776ac1f2012-04-13 23:36:36 -0700652bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700653 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700654 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
655 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700656 return false;
657 }
658 return true;
659}
660
Ian Rogers776ac1f2012-04-13 23:36:36 -0700661bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700662 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700663 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
664 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700665 return false;
666 }
667 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700668 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700669 const char* cp = descriptor;
670 while (*cp++ == '[') {
671 bracket_count++;
672 }
673 if (bracket_count == 0) {
674 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700675 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700676 return false;
677 } else if (bracket_count > 255) {
678 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700679 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700680 return false;
681 }
682 return true;
683}
684
Ian Rogers776ac1f2012-04-13 23:36:36 -0700685bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700686 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
687 const uint16_t* insns = code_item_->insns_ + cur_offset;
688 const uint16_t* array_data;
689 int32_t array_data_offset;
690
691 DCHECK_LT(cur_offset, insn_count);
692 /* make sure the start of the array data table is in range */
693 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
694 if ((int32_t) cur_offset + array_data_offset < 0 ||
695 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700696 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
697 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700698 return false;
699 }
700 /* offset to array data table is a relative branch-style offset */
701 array_data = insns + array_data_offset;
702 /* make sure the table is 32-bit aligned */
703 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700704 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
705 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700706 return false;
707 }
708 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700709 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700710 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
711 /* make sure the end of the switch is in range */
712 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700713 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
714 << ", data offset " << array_data_offset << ", end "
715 << cur_offset + array_data_offset + table_size
716 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700717 return false;
718 }
719 return true;
720}
721
Ian Rogers776ac1f2012-04-13 23:36:36 -0700722bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700723 int32_t offset;
724 bool isConditional, selfOkay;
725 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
726 return false;
727 }
728 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700729 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 -0700730 return false;
731 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700732 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
733 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700734 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700735 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700736 return false;
737 }
738 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
739 int32_t abs_offset = cur_offset + offset;
740 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700741 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700742 << reinterpret_cast<void*>(abs_offset) << ") at "
743 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700744 return false;
745 }
746 insn_flags_[abs_offset].SetBranchTarget();
747 return true;
748}
749
Ian Rogers776ac1f2012-04-13 23:36:36 -0700750bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700751 bool* selfOkay) {
752 const uint16_t* insns = code_item_->insns_ + cur_offset;
753 *pConditional = false;
754 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700755 switch (*insns & 0xff) {
756 case Instruction::GOTO:
757 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700758 break;
759 case Instruction::GOTO_32:
760 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700761 *selfOkay = true;
762 break;
763 case Instruction::GOTO_16:
764 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700765 break;
766 case Instruction::IF_EQ:
767 case Instruction::IF_NE:
768 case Instruction::IF_LT:
769 case Instruction::IF_GE:
770 case Instruction::IF_GT:
771 case Instruction::IF_LE:
772 case Instruction::IF_EQZ:
773 case Instruction::IF_NEZ:
774 case Instruction::IF_LTZ:
775 case Instruction::IF_GEZ:
776 case Instruction::IF_GTZ:
777 case Instruction::IF_LEZ:
778 *pOffset = (int16_t) insns[1];
779 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700780 break;
781 default:
782 return false;
783 break;
784 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700785 return true;
786}
787
Ian Rogers776ac1f2012-04-13 23:36:36 -0700788bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700789 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700790 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700791 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700792 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700793 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
794 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700795 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
796 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700797 return false;
798 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700799 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700800 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700801 /* make sure the table is 32-bit aligned */
802 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700803 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
804 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700805 return false;
806 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700807 uint32_t switch_count = switch_insns[1];
808 int32_t keys_offset, targets_offset;
809 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700810 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
811 /* 0=sig, 1=count, 2/3=firstKey */
812 targets_offset = 4;
813 keys_offset = -1;
814 expected_signature = Instruction::kPackedSwitchSignature;
815 } else {
816 /* 0=sig, 1=count, 2..count*2 = keys */
817 keys_offset = 2;
818 targets_offset = 2 + 2 * switch_count;
819 expected_signature = Instruction::kSparseSwitchSignature;
820 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700821 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700822 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700823 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
824 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700825 return false;
826 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700827 /* make sure the end of the switch is in range */
828 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700829 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
830 << switch_offset << ", end "
831 << (cur_offset + switch_offset + table_size)
832 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700833 return false;
834 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700835 /* for a sparse switch, verify the keys are in ascending order */
836 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700837 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
838 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700839 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
840 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
841 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700842 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
843 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700844 return false;
845 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700846 last_key = key;
847 }
848 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700849 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700850 for (uint32_t targ = 0; targ < switch_count; targ++) {
851 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
852 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
853 int32_t abs_offset = cur_offset + offset;
854 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700855 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700856 << reinterpret_cast<void*>(abs_offset) << ") at "
857 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700858 return false;
859 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700860 insn_flags_[abs_offset].SetBranchTarget();
861 }
862 return true;
863}
864
Ian Rogers776ac1f2012-04-13 23:36:36 -0700865bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700866 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700867 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700868 return false;
869 }
870 uint16_t registers_size = code_item_->registers_size_;
871 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800872 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700873 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
874 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700875 return false;
876 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700877 }
878
879 return true;
880}
881
Ian Rogers776ac1f2012-04-13 23:36:36 -0700882bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700883 uint16_t registers_size = code_item_->registers_size_;
884 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
885 // integer overflow when adding them here.
886 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700887 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
888 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700889 return false;
890 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700891 return true;
892}
893
Ian Rogers0c7abda2012-09-19 13:33:42 -0700894static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -0800895 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
896 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
897 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
898 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
899 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
900 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
901 gc_map.begin(),
902 gc_map.end());
903 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
904 DCHECK_EQ(gc_map.size(),
905 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
906 (length_prefixed_gc_map->at(1) << 16) |
907 (length_prefixed_gc_map->at(2) << 8) |
908 (length_prefixed_gc_map->at(3) << 0)));
909 return length_prefixed_gc_map;
910}
911
Ian Rogers776ac1f2012-04-13 23:36:36 -0700912bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700913 uint16_t registers_size = code_item_->registers_size_;
914 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -0700915
Ian Rogersd81871c2011-10-03 13:57:23 -0700916 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -0800917 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
918 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700919 }
920 /* Create and initialize table holding register status */
Elliott Hughes460384f2012-04-04 16:53:10 -0700921 reg_table_.Init(kTrackRegsGcPoints, insn_flags_.get(), insns_size, registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -0700922
Ian Rogersd81871c2011-10-03 13:57:23 -0700923 work_line_.reset(new RegisterLine(registers_size, this));
924 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -0700925
Ian Rogersd81871c2011-10-03 13:57:23 -0700926 /* Initialize register types of method arguments. */
927 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700928 DCHECK_NE(failures_.size(), 0U);
929 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800930 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700931 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -0700932 return false;
933 }
934 /* Perform code flow verification. */
935 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700936 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700937 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700938 }
939
Ian Rogers1212a022013-03-04 10:48:41 -0800940 CompilerDriver::MethodReference ref(dex_file_, dex_method_idx_);
TDYa127b2eb5c12012-05-24 15:52:10 -0700941
TDYa127b2eb5c12012-05-24 15:52:10 -0700942
Ian Rogersd81871c2011-10-03 13:57:23 -0700943 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -0800944 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
945 if (map.get() == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700946 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700947 return false; // Not a real failure, but a failure to encode
948 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700949#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800950 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -0700951#endif
Ian Rogers0c7abda2012-09-19 13:33:42 -0700952 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
953 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
Logan Chiendd361c92012-04-10 23:40:37 +0800954
jeffhaobdb76512011-09-07 11:43:16 -0700955 return true;
956}
957
Ian Rogersad0b3a32012-04-16 14:50:24 -0700958std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
959 DCHECK_EQ(failures_.size(), failure_messages_.size());
960 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700961 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -0700962 }
963 return os;
964}
965
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700966extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700967 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700968 v->Dump(std::cerr);
969}
970
Ian Rogers776ac1f2012-04-13 23:36:36 -0700971void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -0800972 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700973 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 return;
jeffhaobdb76512011-09-07 11:43:16 -0700975 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800976 {
977 os << "Register Types:\n";
978 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
979 std::ostream indent_os(&indent_filter);
980 reg_types_.Dump(indent_os);
981 }
Ian Rogersb4903572012-10-11 11:52:56 -0700982 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800983 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
984 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -0700985 const Instruction* inst = Instruction::At(code_item_->insns_);
986 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
987 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700988 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
989 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800990 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -0700991 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800992 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800993 const bool kDumpHexOfInstruction = false;
994 if (kDumpHexOfInstruction) {
995 indent_os << inst->DumpHex(5) << " ";
996 }
997 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -0700998 inst = inst->Next();
999 }
jeffhaobdb76512011-09-07 11:43:16 -07001000}
1001
Ian Rogersd81871c2011-10-03 13:57:23 -07001002static bool IsPrimitiveDescriptor(char descriptor) {
1003 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001004 case 'I':
1005 case 'C':
1006 case 'S':
1007 case 'B':
1008 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001009 case 'F':
1010 case 'D':
1011 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001012 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001013 default:
1014 return false;
1015 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001016}
1017
Ian Rogers776ac1f2012-04-13 23:36:36 -07001018bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001019 RegisterLine* reg_line = reg_table_.GetLine(0);
1020 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1021 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001022
Ian Rogersd81871c2011-10-03 13:57:23 -07001023 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1024 //Include the "this" pointer.
1025 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001026 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001027 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1028 // argument as uninitialized. This restricts field access until the superclass constructor is
1029 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001030 const RegType& declaring_class = GetDeclaringClass();
1031 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001032 reg_line->SetRegisterType(arg_start + cur_arg,
1033 reg_types_.UninitializedThisArgument(declaring_class));
1034 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001035 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001036 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001037 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001038 }
1039
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001040 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001041 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001042 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001043
1044 for (; iterator.HasNext(); iterator.Next()) {
1045 const char* descriptor = iterator.GetDescriptor();
1046 if (descriptor == NULL) {
1047 LOG(FATAL) << "Null descriptor";
1048 }
1049 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001050 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1051 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001052 return false;
1053 }
1054 switch (descriptor[0]) {
1055 case 'L':
1056 case '[':
1057 // We assume that reference arguments are initialized. The only way it could be otherwise
1058 // (assuming the caller was verified) is if the current method is <init>, but in that case
1059 // it's effectively considered initialized the instant we reach here (in the sense that we
1060 // can return without doing anything or call virtual methods).
1061 {
Ian Rogersb4903572012-10-11 11:52:56 -07001062 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001063 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001064 }
1065 break;
1066 case 'Z':
1067 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1068 break;
1069 case 'C':
1070 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1071 break;
1072 case 'B':
1073 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1074 break;
1075 case 'I':
1076 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1077 break;
1078 case 'S':
1079 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1080 break;
1081 case 'F':
1082 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1083 break;
1084 case 'J':
1085 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001086 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1087 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1088 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001089 cur_arg++;
1090 break;
1091 }
1092 default:
jeffhaod5347e02012-03-22 17:25:05 -07001093 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001094 return false;
1095 }
1096 cur_arg++;
1097 }
1098 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001099 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001100 return false;
1101 }
1102 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1103 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1104 // format. Only major difference from the method argument format is that 'V' is supported.
1105 bool result;
1106 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1107 result = descriptor[1] == '\0';
1108 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1109 size_t i = 0;
1110 do {
1111 i++;
1112 } while (descriptor[i] == '['); // process leading [
1113 if (descriptor[i] == 'L') { // object array
1114 do {
1115 i++; // find closing ;
1116 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1117 result = descriptor[i] == ';';
1118 } else { // primitive array
1119 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1120 }
1121 } else if (descriptor[0] == 'L') {
1122 // could be more thorough here, but shouldn't be required
1123 size_t i = 0;
1124 do {
1125 i++;
1126 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1127 result = descriptor[i] == ';';
1128 } else {
1129 result = false;
1130 }
1131 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001132 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1133 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001134 }
1135 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001136}
1137
Ian Rogers776ac1f2012-04-13 23:36:36 -07001138bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001139 const uint16_t* insns = code_item_->insns_;
1140 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001141
jeffhaobdb76512011-09-07 11:43:16 -07001142 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001143 insn_flags_[0].SetChanged();
1144 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001145
jeffhaobdb76512011-09-07 11:43:16 -07001146 /* Continue until no instructions are marked "changed". */
1147 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001148 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1149 uint32_t insn_idx = start_guess;
1150 for (; insn_idx < insns_size; insn_idx++) {
1151 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001152 break;
1153 }
jeffhaobdb76512011-09-07 11:43:16 -07001154 if (insn_idx == insns_size) {
1155 if (start_guess != 0) {
1156 /* try again, starting from the top */
1157 start_guess = 0;
1158 continue;
1159 } else {
1160 /* all flags are clear */
1161 break;
1162 }
1163 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001164 // We carry the working set of registers from instruction to instruction. If this address can
1165 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1166 // "changed" flags, we need to load the set of registers from the table.
1167 // Because we always prefer to continue on to the next instruction, we should never have a
1168 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1169 // target.
1170 work_insn_idx_ = insn_idx;
1171 if (insn_flags_[insn_idx].IsBranchTarget()) {
1172 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001173 } else {
1174#ifndef NDEBUG
1175 /*
1176 * Sanity check: retrieve the stored register line (assuming
1177 * a full table) and make sure it actually matches.
1178 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001179 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1180 if (register_line != NULL) {
1181 if (work_line_->CompareLine(register_line) != 0) {
1182 Dump(std::cout);
1183 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001184 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001185 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1186 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001187 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001188 }
jeffhaobdb76512011-09-07 11:43:16 -07001189 }
1190#endif
1191 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001192 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001193 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001194 prepend += " failed to verify: ";
1195 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001196 return false;
1197 }
jeffhaobdb76512011-09-07 11:43:16 -07001198 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001199 insn_flags_[insn_idx].SetVisited();
1200 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001201 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001202
Ian Rogers1c849e52012-06-28 14:00:33 -07001203 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001204 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001205 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001206 * (besides the wasted space), but it indicates a flaw somewhere
1207 * down the line, possibly in the verifier.
1208 *
1209 * If we've substituted "always throw" instructions into the stream,
1210 * we are almost certainly going to have some dead code.
1211 */
1212 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001213 uint32_t insn_idx = 0;
1214 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001215 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001216 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001217 * may or may not be preceded by a padding NOP (for alignment).
1218 */
1219 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1220 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1221 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001222 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001223 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1224 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1225 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001226 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001227 }
1228
Ian Rogersd81871c2011-10-03 13:57:23 -07001229 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001230 if (dead_start < 0)
1231 dead_start = insn_idx;
1232 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001233 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001234 dead_start = -1;
1235 }
1236 }
1237 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001238 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001239 }
1240 }
jeffhaobdb76512011-09-07 11:43:16 -07001241 return true;
1242}
1243
Ian Rogers776ac1f2012-04-13 23:36:36 -07001244bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001245 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1246 // We want the state _before_ the instruction, for the case where the dex pc we're
1247 // interested in is itself a monitor-enter instruction (which is a likely place
1248 // for a thread to be suspended).
1249 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001250 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001251 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1252 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1253 }
1254 }
1255
jeffhaobdb76512011-09-07 11:43:16 -07001256 /*
1257 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001258 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001259 * control to another statement:
1260 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001261 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001262 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001263 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001264 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001265 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001266 * throw an exception that is handled by an encompassing "try"
1267 * block.
1268 *
1269 * We can also return, in which case there is no successor instruction
1270 * from this point.
1271 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001272 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001273 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001274 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1275 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001276 DecodedInstruction dec_insn(inst);
Ian Rogersa75a0132012-09-28 11:41:42 -07001277 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001278
jeffhaobdb76512011-09-07 11:43:16 -07001279 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001280 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001281 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001282 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001283 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1284 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001285 }
jeffhaobdb76512011-09-07 11:43:16 -07001286
1287 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001288 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001289 * can throw an exception, we will copy/merge this into the "catch"
1290 * address rather than work_line, because we don't want the result
1291 * from the "successful" code path (e.g. a check-cast that "improves"
1292 * a type) to be visible to the exception handler.
1293 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001294 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001295 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001296 } else {
1297#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001298 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001299#endif
1300 }
1301
Elliott Hughesadb8c672012-03-06 16:49:32 -08001302 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07001303 case Instruction::NOP:
1304 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001305 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001306 * a signature that looks like a NOP; if we see one of these in
1307 * the course of executing code then we have a problem.
1308 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001309 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001310 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001311 }
1312 break;
1313
1314 case Instruction::MOVE:
1315 case Instruction::MOVE_FROM16:
1316 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001317 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001318 break;
1319 case Instruction::MOVE_WIDE:
1320 case Instruction::MOVE_WIDE_FROM16:
1321 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001322 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07001323 break;
1324 case Instruction::MOVE_OBJECT:
1325 case Instruction::MOVE_OBJECT_FROM16:
1326 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001327 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001328 break;
1329
1330 /*
1331 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001332 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001333 * might want to hold the result in an actual CPU register, so the
1334 * Dalvik spec requires that these only appear immediately after an
1335 * invoke or filled-new-array.
1336 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001337 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001338 * redundant with the reset done below, but it can make the debug info
1339 * easier to read in some cases.)
1340 */
1341 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001342 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07001343 break;
1344 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001345 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001346 break;
1347 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001348 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07001349 break;
1350
Ian Rogersd81871c2011-10-03 13:57:23 -07001351 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001352 /*
jeffhao60f83e32012-02-13 17:16:30 -08001353 * This statement can only appear as the first instruction in an exception handler. We verify
1354 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001355 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001356 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08001357 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001358 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 }
jeffhaobdb76512011-09-07 11:43:16 -07001360 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001361 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1362 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001363 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001364 }
jeffhaobdb76512011-09-07 11:43:16 -07001365 }
1366 break;
1367 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001368 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001369 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001370 const RegType& return_type = GetMethodReturnType();
1371 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001372 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001373 } else {
1374 // Compilers may generate synthetic functions that write byte values into boolean fields.
1375 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001376 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001377 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1378 ((return_type.IsBoolean() || return_type.IsByte() ||
1379 return_type.IsShort() || return_type.IsChar()) &&
1380 src_type.IsInteger()));
1381 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001382 bool success =
1383 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
1384 if (!success) {
1385 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001386 }
jeffhaobdb76512011-09-07 11:43:16 -07001387 }
1388 }
1389 break;
1390 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001391 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001392 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001393 const RegType& return_type = GetMethodReturnType();
1394 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001395 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001396 } else {
1397 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001398 bool success = work_line_->VerifyRegisterType(dec_insn.vA, return_type);
1399 if (!success) {
1400 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001401 }
jeffhaobdb76512011-09-07 11:43:16 -07001402 }
1403 }
1404 break;
1405 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001406 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001407 const RegType& return_type = GetMethodReturnType();
1408 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001409 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001410 } else {
1411 /* return_type is the *expected* return type, not register value */
1412 DCHECK(!return_type.IsZero());
1413 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08001414 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07001415 // Disallow returning uninitialized values and verify that the reference in vAA is an
1416 // instance of the "return_type"
1417 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001418 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001419 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001420 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1421 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001422 }
1423 }
1424 }
1425 break;
1426
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001427 /* could be boolean, int, float, or a null reference */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001428 case Instruction::CONST_4:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001429 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001430 reg_types_.FromCat1Const(static_cast<int32_t>(dec_insn.vB << 28) >> 28, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001431 break;
jeffhaobdb76512011-09-07 11:43:16 -07001432 case Instruction::CONST_16:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001433 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001434 reg_types_.FromCat1Const(static_cast<int16_t>(dec_insn.vB), true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001435 break;
jeffhaobdb76512011-09-07 11:43:16 -07001436 case Instruction::CONST:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001437 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const(dec_insn.vB, true));
jeffhaobdb76512011-09-07 11:43:16 -07001438 break;
1439 case Instruction::CONST_HIGH16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001440 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001441 reg_types_.FromCat1Const(dec_insn.vB << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001442 break;
jeffhaobdb76512011-09-07 11:43:16 -07001443 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001444 case Instruction::CONST_WIDE_16: {
1445 int64_t val = static_cast<int16_t>(dec_insn.vB);
1446 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1447 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1448 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001449 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001450 }
1451 case Instruction::CONST_WIDE_32: {
1452 int64_t val = static_cast<int32_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);
1456 break;
1457 }
1458 case Instruction::CONST_WIDE: {
1459 int64_t val = dec_insn.vB_wide;
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_HIGH16: {
1466 int64_t val = static_cast<uint64_t>(dec_insn.vB) << 48;
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 }
jeffhaobdb76512011-09-07 11:43:16 -07001472 case Instruction::CONST_STRING:
1473 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001474 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001475 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001476 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001477 // Get type from instruction if unresolved then we need an access check
1478 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08001479 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001480 // Register holds class, ie its type is class, on error it will hold Conflict.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001481 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogersb4903572012-10-11 11:52:56 -07001482 res_type.IsConflict() ? res_type
1483 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001484 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001485 }
jeffhaobdb76512011-09-07 11:43:16 -07001486 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001487 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001488 break;
1489 case Instruction::MONITOR_EXIT:
1490 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001491 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001492 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001493 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001494 * to the need to handle asynchronous exceptions, a now-deprecated
1495 * feature that Dalvik doesn't support.)
1496 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001497 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001498 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001499 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001500 * structured locking checks are working, the former would have
1501 * failed on the -enter instruction, and the latter is impossible.
1502 *
1503 * This is fortunate, because issue 3221411 prevents us from
1504 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001505 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001506 * some catch blocks (which will show up as "dead" code when
1507 * we skip them here); if we can't, then the code path could be
1508 * "live" so we still need to check it.
1509 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001510 opcode_flags &= ~Instruction::kThrow;
1511 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001512 break;
1513
Ian Rogers28ad40d2011-10-27 15:19:26 -07001514 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001515 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001516 /*
1517 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1518 * could be a "upcast" -- not expected, so we don't try to address it.)
1519 *
1520 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001521 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001522 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001523 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001524 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001525 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001526 if (res_type.IsConflict()) {
1527 DCHECK_NE(failures_.size(), 0U);
1528 if (!is_checkcast) {
1529 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
1530 }
1531 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001532 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001533 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1534 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001535 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001536 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001537 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001538 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001539 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07001540 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001541 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001542 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001543 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001544 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001545 }
jeffhaobdb76512011-09-07 11:43:16 -07001546 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001547 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001548 }
1549 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001550 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001551 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001552 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001553 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001554 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001555 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001556 }
1557 }
1558 break;
1559 }
1560 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001561 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001562 if (res_type.IsConflict()) {
1563 DCHECK_NE(failures_.size(), 0U);
1564 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001565 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001566 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1567 // can't create an instance of an interface or abstract class */
1568 if (!res_type.IsInstantiableTypes()) {
1569 Fail(VERIFY_ERROR_INSTANTIATION)
1570 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001571 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001572 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001573 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1574 // Any registers holding previous allocations from this address that have not yet been
1575 // initialized must be marked invalid.
1576 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1577 // add the new uninitialized reference to the register state
1578 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001579 break;
1580 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001581 case Instruction::NEW_ARRAY:
1582 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001583 break;
1584 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08001585 VerifyNewArray(dec_insn, true, false);
1586 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001587 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001588 case Instruction::FILLED_NEW_ARRAY_RANGE:
1589 VerifyNewArray(dec_insn, true, true);
1590 just_set_result = true; // Filled new array range sets result register
1591 break;
jeffhaobdb76512011-09-07 11:43:16 -07001592 case Instruction::CMPL_FLOAT:
1593 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001594 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001595 break;
1596 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001597 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001598 break;
1599 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001600 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001601 break;
1602 case Instruction::CMPL_DOUBLE:
1603 case Instruction::CMPG_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001604 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vB, reg_types_.DoubleLo(),
1605 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001606 break;
1607 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001608 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vC, reg_types_.DoubleLo(),
1609 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001610 break;
1611 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001612 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001613 break;
1614 case Instruction::CMP_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001615 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vB, reg_types_.LongLo(),
1616 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001617 break;
1618 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001619 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vC, reg_types_.LongLo(),
1620 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001621 break;
1622 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001623 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001624 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001625 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001626 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersb4903572012-10-11 11:52:56 -07001627 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001628 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001629 }
1630 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001631 }
jeffhaobdb76512011-09-07 11:43:16 -07001632 case Instruction::GOTO:
1633 case Instruction::GOTO_16:
1634 case Instruction::GOTO_32:
1635 /* no effect on or use of registers */
1636 break;
1637
1638 case Instruction::PACKED_SWITCH:
1639 case Instruction::SPARSE_SWITCH:
1640 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001641 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001642 break;
1643
Ian Rogersd81871c2011-10-03 13:57:23 -07001644 case Instruction::FILL_ARRAY_DATA: {
1645 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001646 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08001647 /* array_type can be null if the reg type is Zero */
1648 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001649 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001650 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001651 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001652 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1653 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001654 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001655 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1656 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001657 } else {
jeffhao457cc512012-02-02 16:55:13 -08001658 // Now verify if the element width in the table matches the element width declared in
1659 // the array
1660 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1661 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001662 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001663 } else {
1664 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1665 // Since we don't compress the data in Dex, expect to see equal width of data stored
1666 // in the table and expected from the array class.
1667 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001668 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1669 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001670 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001671 }
1672 }
jeffhaobdb76512011-09-07 11:43:16 -07001673 }
1674 }
1675 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001676 }
jeffhaobdb76512011-09-07 11:43:16 -07001677 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001678 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001679 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1680 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001681 bool mismatch = false;
1682 if (reg_type1.IsZero()) { // zero then integral or reference expected
1683 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1684 } else if (reg_type1.IsReferenceTypes()) { // both references?
1685 mismatch = !reg_type2.IsReferenceTypes();
1686 } else { // both integral?
1687 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1688 }
1689 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001690 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1691 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001692 }
1693 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001694 }
jeffhaobdb76512011-09-07 11:43:16 -07001695 case Instruction::IF_LT:
1696 case Instruction::IF_GE:
1697 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001698 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001699 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1700 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001701 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001702 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1703 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001704 }
1705 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001706 }
jeffhaobdb76512011-09-07 11:43:16 -07001707 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001708 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001709 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001710 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001711 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001712 }
jeffhaobdb76512011-09-07 11:43:16 -07001713 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001714 }
jeffhaobdb76512011-09-07 11:43:16 -07001715 case Instruction::IF_LTZ:
1716 case Instruction::IF_GEZ:
1717 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001718 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001719 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001720 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001721 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1722 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001723 }
jeffhaobdb76512011-09-07 11:43:16 -07001724 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001725 }
jeffhaobdb76512011-09-07 11:43:16 -07001726 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001727 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
1728 break;
jeffhaobdb76512011-09-07 11:43:16 -07001729 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001730 VerifyAGet(dec_insn, reg_types_.Byte(), true);
1731 break;
jeffhaobdb76512011-09-07 11:43:16 -07001732 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07001733 VerifyAGet(dec_insn, reg_types_.Char(), true);
1734 break;
jeffhaobdb76512011-09-07 11:43:16 -07001735 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001736 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001737 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001738 case Instruction::AGET:
1739 VerifyAGet(dec_insn, reg_types_.Integer(), true);
1740 break;
jeffhaobdb76512011-09-07 11:43:16 -07001741 case Instruction::AGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001742 VerifyAGet(dec_insn, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001743 break;
1744 case Instruction::AGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001745 VerifyAGet(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001746 break;
1747
Ian Rogersd81871c2011-10-03 13:57:23 -07001748 case Instruction::APUT_BOOLEAN:
1749 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
1750 break;
1751 case Instruction::APUT_BYTE:
1752 VerifyAPut(dec_insn, reg_types_.Byte(), true);
1753 break;
1754 case Instruction::APUT_CHAR:
1755 VerifyAPut(dec_insn, reg_types_.Char(), true);
1756 break;
1757 case Instruction::APUT_SHORT:
1758 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001759 break;
1760 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001761 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001762 break;
1763 case Instruction::APUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001764 VerifyAPut(dec_insn, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001765 break;
1766 case Instruction::APUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001767 VerifyAPut(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001768 break;
1769
jeffhaobdb76512011-09-07 11:43:16 -07001770 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001771 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001772 break;
jeffhaobdb76512011-09-07 11:43:16 -07001773 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001774 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001775 break;
jeffhaobdb76512011-09-07 11:43:16 -07001776 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001777 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001778 break;
jeffhaobdb76512011-09-07 11:43:16 -07001779 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001780 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001781 break;
1782 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001783 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001784 break;
1785 case Instruction::IGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001786 VerifyISGet(dec_insn, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001787 break;
1788 case Instruction::IGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001789 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001790 break;
jeffhaobdb76512011-09-07 11:43:16 -07001791
Ian Rogersd81871c2011-10-03 13:57:23 -07001792 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001793 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001794 break;
1795 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001796 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001797 break;
1798 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001799 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001800 break;
1801 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001802 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001803 break;
1804 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001805 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001806 break;
1807 case Instruction::IPUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001808 VerifyISPut(dec_insn, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001809 break;
jeffhaobdb76512011-09-07 11:43:16 -07001810 case Instruction::IPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001811 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001812 break;
1813
jeffhaobdb76512011-09-07 11:43:16 -07001814 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001815 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001816 break;
jeffhaobdb76512011-09-07 11:43:16 -07001817 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001818 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001819 break;
jeffhaobdb76512011-09-07 11:43:16 -07001820 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001821 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001822 break;
jeffhaobdb76512011-09-07 11:43:16 -07001823 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001824 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001825 break;
1826 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001827 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001828 break;
1829 case Instruction::SGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001830 VerifyISGet(dec_insn, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001831 break;
1832 case Instruction::SGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001833 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001834 break;
1835
1836 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001837 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001838 break;
1839 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001840 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001841 break;
1842 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001843 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001844 break;
1845 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001846 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001847 break;
1848 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001849 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001850 break;
1851 case Instruction::SPUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001852 VerifyISPut(dec_insn, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001853 break;
1854 case Instruction::SPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001855 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001856 break;
1857
1858 case Instruction::INVOKE_VIRTUAL:
1859 case Instruction::INVOKE_VIRTUAL_RANGE:
1860 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001861 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001862 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
1863 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
1864 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
1865 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001866 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL,
1867 is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001868 const char* descriptor;
1869 if (called_method == NULL) {
1870 uint32_t method_idx = dec_insn.vB;
1871 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1872 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1873 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1874 } else {
1875 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07001876 }
Ian Rogersb4903572012-10-11 11:52:56 -07001877 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001878 if (!return_type.IsLowHalf()) {
1879 work_line_->SetResultRegisterType(return_type);
1880 } else {
1881 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
1882 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001883 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001884 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001885 }
jeffhaobdb76512011-09-07 11:43:16 -07001886 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001887 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001888 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001889 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT,
1890 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07001891 const char* return_type_descriptor;
1892 bool is_constructor;
1893 if (called_method == NULL) {
1894 uint32_t method_idx = dec_insn.vB;
1895 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1896 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
1897 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1898 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1899 } else {
1900 is_constructor = called_method->IsConstructor();
1901 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
1902 }
1903 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07001904 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001905 * Some additional checks when calling a constructor. We know from the invocation arg check
1906 * that the "this" argument is an instance of called_method->klass. Now we further restrict
1907 * that to require that called_method->klass is the same as this->klass or this->super,
1908 * allowing the latter only if the "this" argument is the same as the "this" argument to
1909 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07001910 */
jeffhaob57e9522012-04-26 18:08:21 -07001911 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1912 if (this_type.IsConflict()) // failure.
1913 break;
jeffhaobdb76512011-09-07 11:43:16 -07001914
jeffhaob57e9522012-04-26 18:08:21 -07001915 /* no null refs allowed (?) */
1916 if (this_type.IsZero()) {
1917 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
1918 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07001919 }
jeffhaob57e9522012-04-26 18:08:21 -07001920
1921 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07001922 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
1923 // TODO: re-enable constructor type verification
1924 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07001925 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07001926 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
1927 // break;
1928 // }
jeffhaob57e9522012-04-26 18:08:21 -07001929
1930 /* arg must be an uninitialized reference */
1931 if (!this_type.IsUninitializedTypes()) {
1932 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
1933 << this_type;
1934 break;
1935 }
1936
1937 /*
1938 * Replace the uninitialized reference with an initialized one. We need to do this for all
1939 * registers that have the same object instance in them, not just the "this" register.
1940 */
1941 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001942 }
Ian Rogersb4903572012-10-11 11:52:56 -07001943 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
1944 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001945 if (!return_type.IsLowHalf()) {
1946 work_line_->SetResultRegisterType(return_type);
1947 } else {
1948 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
1949 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001950 just_set_result = true;
1951 break;
1952 }
1953 case Instruction::INVOKE_STATIC:
1954 case Instruction::INVOKE_STATIC_RANGE: {
1955 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001956 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001957 const char* descriptor;
1958 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001959 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001960 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1961 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07001962 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001963 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001964 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001965 }
Ian Rogersb4903572012-10-11 11:52:56 -07001966 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001967 if (!return_type.IsLowHalf()) {
1968 work_line_->SetResultRegisterType(return_type);
1969 } else {
1970 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
1971 }
jeffhaobdb76512011-09-07 11:43:16 -07001972 just_set_result = true;
1973 }
1974 break;
jeffhaobdb76512011-09-07 11:43:16 -07001975 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001976 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001977 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001978 mirror::AbstractMethod* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001979 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001980 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001981 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
1982 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
1983 << PrettyMethod(abs_method) << "'";
1984 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001985 }
Ian Rogers0d604842012-04-16 14:50:24 -07001986 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001987 /* Get the type of the "this" arg, which should either be a sub-interface of called
1988 * interface or Object (see comments in RegType::JoinClass).
1989 */
1990 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1991 if (this_type.IsZero()) {
1992 /* null pointer always passes (and always fails at runtime) */
1993 } else {
1994 if (this_type.IsUninitializedTypes()) {
1995 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
1996 << this_type;
1997 break;
1998 }
1999 // In the past we have tried to assert that "called_interface" is assignable
2000 // from "this_type.GetClass()", however, as we do an imprecise Join
2001 // (RegType::JoinClass) we don't have full information on what interfaces are
2002 // implemented by "this_type". For example, two classes may implement the same
2003 // interfaces and have a common parent that doesn't implement the interface. The
2004 // join will set "this_type" to the parent class and a test that this implements
2005 // the interface will incorrectly fail.
2006 }
2007 /*
2008 * We don't have an object instance, so we can't find the concrete method. However, all of
2009 * the type information is in the abstract method, so we're good.
2010 */
2011 const char* descriptor;
2012 if (abs_method == NULL) {
2013 uint32_t method_idx = dec_insn.vB;
2014 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2015 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2016 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2017 } else {
2018 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2019 }
Ian Rogersb4903572012-10-11 11:52:56 -07002020 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002021 if (!return_type.IsLowHalf()) {
2022 work_line_->SetResultRegisterType(return_type);
2023 } else {
2024 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2025 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002026 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002027 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002028 }
jeffhaobdb76512011-09-07 11:43:16 -07002029 case Instruction::NEG_INT:
2030 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002031 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002032 break;
2033 case Instruction::NEG_LONG:
2034 case Instruction::NOT_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002035 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2036 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002037 break;
2038 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002039 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002040 break;
2041 case Instruction::NEG_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002042 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2043 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002044 break;
2045 case Instruction::INT_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002046 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2047 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002048 break;
2049 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002050 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002051 break;
2052 case Instruction::INT_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002053 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2054 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002055 break;
2056 case Instruction::LONG_TO_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002057 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Integer(),
2058 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002059 break;
2060 case Instruction::LONG_TO_FLOAT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002061 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Float(),
2062 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002063 break;
2064 case Instruction::LONG_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002065 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2066 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002067 break;
2068 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002069 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002070 break;
2071 case Instruction::FLOAT_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002072 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2073 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002074 break;
2075 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002076 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2077 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002078 break;
2079 case Instruction::DOUBLE_TO_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002080 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Integer(),
2081 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002082 break;
2083 case Instruction::DOUBLE_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002084 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2085 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002086 break;
2087 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002088 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Float(),
2089 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002090 break;
2091 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002092 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002093 break;
2094 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002095 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002096 break;
2097 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002098 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002099 break;
2100
2101 case Instruction::ADD_INT:
2102 case Instruction::SUB_INT:
2103 case Instruction::MUL_INT:
2104 case Instruction::REM_INT:
2105 case Instruction::DIV_INT:
2106 case Instruction::SHL_INT:
2107 case Instruction::SHR_INT:
2108 case Instruction::USHR_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002109 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(),
2110 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002111 break;
2112 case Instruction::AND_INT:
2113 case Instruction::OR_INT:
2114 case Instruction::XOR_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002115 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(),
2116 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002117 break;
2118 case Instruction::ADD_LONG:
2119 case Instruction::SUB_LONG:
2120 case Instruction::MUL_LONG:
2121 case Instruction::DIV_LONG:
2122 case Instruction::REM_LONG:
2123 case Instruction::AND_LONG:
2124 case Instruction::OR_LONG:
2125 case Instruction::XOR_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002126 work_line_->CheckBinaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2127 reg_types_.LongLo(), reg_types_.LongHi(),
2128 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002129 break;
2130 case Instruction::SHL_LONG:
2131 case Instruction::SHR_LONG:
2132 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002133 /* shift distance is Int, making these different from other binary operations */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002134 work_line_->CheckBinaryOpWideShift(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2135 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002136 break;
2137 case Instruction::ADD_FLOAT:
2138 case Instruction::SUB_FLOAT:
2139 case Instruction::MUL_FLOAT:
2140 case Instruction::DIV_FLOAT:
2141 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002142 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002143 break;
2144 case Instruction::ADD_DOUBLE:
2145 case Instruction::SUB_DOUBLE:
2146 case Instruction::MUL_DOUBLE:
2147 case Instruction::DIV_DOUBLE:
2148 case Instruction::REM_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002149 work_line_->CheckBinaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2150 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2151 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002152 break;
2153 case Instruction::ADD_INT_2ADDR:
2154 case Instruction::SUB_INT_2ADDR:
2155 case Instruction::MUL_INT_2ADDR:
2156 case Instruction::REM_INT_2ADDR:
2157 case Instruction::SHL_INT_2ADDR:
2158 case Instruction::SHR_INT_2ADDR:
2159 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002160 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002161 break;
2162 case Instruction::AND_INT_2ADDR:
2163 case Instruction::OR_INT_2ADDR:
2164 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002165 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002166 break;
2167 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002168 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002169 break;
2170 case Instruction::ADD_LONG_2ADDR:
2171 case Instruction::SUB_LONG_2ADDR:
2172 case Instruction::MUL_LONG_2ADDR:
2173 case Instruction::DIV_LONG_2ADDR:
2174 case Instruction::REM_LONG_2ADDR:
2175 case Instruction::AND_LONG_2ADDR:
2176 case Instruction::OR_LONG_2ADDR:
2177 case Instruction::XOR_LONG_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002178 work_line_->CheckBinaryOp2addrWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2179 reg_types_.LongLo(), reg_types_.LongHi(),
2180 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002181 break;
2182 case Instruction::SHL_LONG_2ADDR:
2183 case Instruction::SHR_LONG_2ADDR:
2184 case Instruction::USHR_LONG_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002185 work_line_->CheckBinaryOp2addrWideShift(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2186 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002187 break;
2188 case Instruction::ADD_FLOAT_2ADDR:
2189 case Instruction::SUB_FLOAT_2ADDR:
2190 case Instruction::MUL_FLOAT_2ADDR:
2191 case Instruction::DIV_FLOAT_2ADDR:
2192 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002193 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002194 break;
2195 case Instruction::ADD_DOUBLE_2ADDR:
2196 case Instruction::SUB_DOUBLE_2ADDR:
2197 case Instruction::MUL_DOUBLE_2ADDR:
2198 case Instruction::DIV_DOUBLE_2ADDR:
2199 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002200 work_line_->CheckBinaryOp2addrWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2201 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2202 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002203 break;
2204 case Instruction::ADD_INT_LIT16:
2205 case Instruction::RSUB_INT:
2206 case Instruction::MUL_INT_LIT16:
2207 case Instruction::DIV_INT_LIT16:
2208 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002209 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002210 break;
2211 case Instruction::AND_INT_LIT16:
2212 case Instruction::OR_INT_LIT16:
2213 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002214 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002215 break;
2216 case Instruction::ADD_INT_LIT8:
2217 case Instruction::RSUB_INT_LIT8:
2218 case Instruction::MUL_INT_LIT8:
2219 case Instruction::DIV_INT_LIT8:
2220 case Instruction::REM_INT_LIT8:
2221 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002222 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002223 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002224 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002225 break;
2226 case Instruction::AND_INT_LIT8:
2227 case Instruction::OR_INT_LIT8:
2228 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002229 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002230 break;
2231
Ian Rogersd81871c2011-10-03 13:57:23 -07002232 /* These should never appear during verification. */
jeffhao9a4f0032012-08-30 16:17:40 -07002233 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002234 case Instruction::UNUSED_EE:
2235 case Instruction::UNUSED_EF:
2236 case Instruction::UNUSED_F2:
2237 case Instruction::UNUSED_F3:
2238 case Instruction::UNUSED_F4:
2239 case Instruction::UNUSED_F5:
2240 case Instruction::UNUSED_F6:
2241 case Instruction::UNUSED_F7:
2242 case Instruction::UNUSED_F8:
2243 case Instruction::UNUSED_F9:
2244 case Instruction::UNUSED_FA:
2245 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002246 case Instruction::UNUSED_F0:
2247 case Instruction::UNUSED_F1:
2248 case Instruction::UNUSED_E3:
2249 case Instruction::UNUSED_E8:
2250 case Instruction::UNUSED_E7:
2251 case Instruction::UNUSED_E4:
2252 case Instruction::UNUSED_E9:
2253 case Instruction::UNUSED_FC:
2254 case Instruction::UNUSED_E5:
2255 case Instruction::UNUSED_EA:
2256 case Instruction::UNUSED_FD:
2257 case Instruction::UNUSED_E6:
2258 case Instruction::UNUSED_EB:
2259 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002260 case Instruction::UNUSED_3E:
2261 case Instruction::UNUSED_3F:
2262 case Instruction::UNUSED_40:
2263 case Instruction::UNUSED_41:
2264 case Instruction::UNUSED_42:
2265 case Instruction::UNUSED_43:
2266 case Instruction::UNUSED_73:
2267 case Instruction::UNUSED_79:
2268 case Instruction::UNUSED_7A:
2269 case Instruction::UNUSED_EC:
2270 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002271 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002272 break;
2273
2274 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002275 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002276 * complain if an instruction is missing (which is desirable).
2277 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002278 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002279
Ian Rogersad0b3a32012-04-16 14:50:24 -07002280 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002281 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002282 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002283 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002284 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002285 /* immediate failure, reject class */
2286 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2287 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002288 } else if (have_pending_runtime_throw_failure_) {
2289 /* slow path will throw, mark following code as unreachable */
2290 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002291 }
jeffhaobdb76512011-09-07 11:43:16 -07002292 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002293 * If we didn't just set the result register, clear it out. This ensures that you can only use
2294 * "move-result" immediately after the result is set. (We could check this statically, but it's
2295 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002296 */
2297 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002298 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002299 }
2300
jeffhaoa0a764a2011-09-16 10:43:38 -07002301 /* Handle "continue". Tag the next consecutive instruction. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002302 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers776ac1f2012-04-13 23:36:36 -07002303 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
Ian Rogersd81871c2011-10-03 13:57:23 -07002304 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
jeffhaod5347e02012-03-22 17:25:05 -07002305 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002306 return false;
2307 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002308 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2309 // next instruction isn't one.
jeffhaod5347e02012-03-22 17:25:05 -07002310 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002311 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002312 }
2313 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2314 if (next_line != NULL) {
2315 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2316 // needed.
2317 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002318 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002319 }
jeffhaobdb76512011-09-07 11:43:16 -07002320 } else {
2321 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002322 * We're not recording register data for the next instruction, so we don't know what the prior
2323 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002324 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002325 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002326 }
2327 }
2328
2329 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002330 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002331 *
2332 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002333 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002334 * somebody could get a reference field, check it for zero, and if the
2335 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002336 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002337 * that, and will reject the code.
2338 *
2339 * TODO: avoid re-fetching the branch target
2340 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002341 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002342 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002343 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002344 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002345 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002346 return false;
2347 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002348 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002349 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002350 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002351 }
jeffhaobdb76512011-09-07 11:43:16 -07002352 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002353 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002354 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002355 }
jeffhaobdb76512011-09-07 11:43:16 -07002356 }
2357
2358 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002359 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002360 *
2361 * We've already verified that the table is structurally sound, so we
2362 * just need to walk through and tag the targets.
2363 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002364 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002365 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2366 const uint16_t* switch_insns = insns + offset_to_switch;
2367 int switch_count = switch_insns[1];
2368 int offset_to_targets, targ;
2369
2370 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2371 /* 0 = sig, 1 = count, 2/3 = first key */
2372 offset_to_targets = 4;
2373 } else {
2374 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002375 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002376 offset_to_targets = 2 + 2 * switch_count;
2377 }
2378
2379 /* verify each switch target */
2380 for (targ = 0; targ < switch_count; targ++) {
2381 int offset;
2382 uint32_t abs_offset;
2383
2384 /* offsets are 32-bit, and only partly endian-swapped */
2385 offset = switch_insns[offset_to_targets + targ * 2] |
2386 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002387 abs_offset = work_insn_idx_ + offset;
2388 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002389 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002390 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002391 }
2392 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002393 return false;
2394 }
2395 }
2396
2397 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002398 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2399 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002400 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002401 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002402 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002403 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002404
Ian Rogers0571d352011-11-03 19:51:38 -07002405 for (; iterator.HasNext(); iterator.Next()) {
2406 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002407 within_catch_all = true;
2408 }
jeffhaobdb76512011-09-07 11:43:16 -07002409 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002410 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2411 * "work_regs", because at runtime the exception will be thrown before the instruction
2412 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002413 */
Ian Rogers0571d352011-11-03 19:51:38 -07002414 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002415 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002416 }
jeffhaobdb76512011-09-07 11:43:16 -07002417 }
2418
2419 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002420 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2421 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002422 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002423 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002424 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002425 * The state in work_line reflects the post-execution state. If the current instruction is a
2426 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002427 * it will do so before grabbing the lock).
2428 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002429 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002430 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002431 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002432 return false;
2433 }
2434 }
2435 }
2436
jeffhaod1f0fde2011-09-08 17:25:33 -07002437 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002438 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002439 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002440 return false;
2441 }
jeffhaobdb76512011-09-07 11:43:16 -07002442 }
2443
2444 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002445 * Update start_guess. Advance to the next instruction of that's
2446 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002447 * neither of those exists we're in a return or throw; leave start_guess
2448 * alone and let the caller sort it out.
2449 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002450 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002451 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002452 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002453 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002454 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002455 }
2456
Ian Rogersd81871c2011-10-03 13:57:23 -07002457 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2458 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002459
2460 return true;
2461}
2462
Ian Rogers776ac1f2012-04-13 23:36:36 -07002463const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002464 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002465 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002466 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002467 const RegType& result =
Ian Rogersb4903572012-10-11 11:52:56 -07002468 klass != NULL ? reg_types_.FromClass(klass, klass->IsFinal())
2469 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002470 if (result.IsConflict()) {
2471 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2472 << "' in " << referrer;
2473 return result;
2474 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002475 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002476 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002477 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002478 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002479 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002480 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002481 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002482 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002483 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002484 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002485}
2486
Ian Rogers776ac1f2012-04-13 23:36:36 -07002487const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002488 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002489 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002490 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002491 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2492 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002493 CatchHandlerIterator iterator(handlers_ptr);
2494 for (; iterator.HasNext(); iterator.Next()) {
2495 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2496 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002497 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002498 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002499 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002500 if (common_super == NULL) {
2501 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2502 // as that is caught at runtime
2503 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002504 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002505 // We don't know enough about the type and the common path merge will result in
2506 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002507 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002508 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002509 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002510 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002511 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002512 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002513 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002514 }
2515 }
2516 }
2517 }
Ian Rogers0571d352011-11-03 19:51:38 -07002518 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002519 }
2520 }
2521 if (common_super == NULL) {
2522 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002523 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002524 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002525 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002526 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002527}
2528
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002529mirror::AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
2530 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002531 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002532 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002533 if (klass_type.IsConflict()) {
2534 std::string append(" in attempt to access method ");
2535 append += dex_file_->GetMethodName(method_id);
2536 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002537 return NULL;
2538 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002539 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002540 return NULL; // Can't resolve Class so no more to do here
2541 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002542 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002543 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002544 mirror::AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002545 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002546 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002547 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002548
2549 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002550 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002551 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002552 res_method = klass->FindInterfaceMethod(name, signature);
2553 } else {
2554 res_method = klass->FindVirtualMethod(name, signature);
2555 }
2556 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002557 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002558 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002559 // If a virtual or interface method wasn't found with the expected type, look in
2560 // the direct methods. This can happen when the wrong invoke type is used or when
2561 // a class has changed, and will be flagged as an error in later checks.
2562 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2563 res_method = klass->FindDirectMethod(name, signature);
2564 }
2565 if (res_method == NULL) {
2566 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2567 << PrettyDescriptor(klass) << "." << name
2568 << " " << signature;
2569 return NULL;
2570 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002571 }
2572 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002573 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2574 // enforce them here.
2575 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002576 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2577 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002578 return NULL;
2579 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002580 // Disallow any calls to class initializers.
2581 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002582 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2583 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002584 return NULL;
2585 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002586 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002587 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002588 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002589 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002590 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002591 }
jeffhaode0d9c92012-02-27 13:58:13 -08002592 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2593 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002594 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2595 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002596 return NULL;
2597 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002598 // Check that interface methods match interface classes.
2599 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2600 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2601 << " is in an interface class " << PrettyClass(klass);
2602 return NULL;
2603 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2604 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2605 << " is in a non-interface class " << PrettyClass(klass);
2606 return NULL;
2607 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002608 // See if the method type implied by the invoke instruction matches the access flags for the
2609 // target method.
2610 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2611 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2612 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2613 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002614 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2615 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002616 return NULL;
2617 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002618 return res_method;
2619}
2620
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002621mirror::AbstractMethod* MethodVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
2622 MethodType method_type, bool is_range,
2623 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002624 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2625 // we're making.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002626 mirror::AbstractMethod* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002627 if (res_method == NULL) { // error or class is unresolved
2628 return NULL;
2629 }
2630
Ian Rogersd81871c2011-10-03 13:57:23 -07002631 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2632 // has a vtable entry for the target method.
2633 if (is_super) {
2634 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002635 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002636 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002637 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002638 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002639 << " to super " << PrettyMethod(res_method);
2640 return NULL;
2641 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002642 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002643 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002644 MethodHelper mh(res_method);
2645 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002646 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002647 << " to super " << super
2648 << "." << mh.GetName()
2649 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002650 return NULL;
2651 }
2652 }
2653 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2654 // match the call to the signature. Also, we might might be calling through an abstract method
2655 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08002656 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002657 /* caught by static verifier */
2658 DCHECK(is_range || expected_args <= 5);
2659 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002660 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002661 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2662 return NULL;
2663 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002664
jeffhaobdb76512011-09-07 11:43:16 -07002665 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002666 * Check the "this" argument, which must be an instance of the class that declared the method.
2667 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2668 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002669 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002670 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002671 if (!res_method->IsStatic()) {
2672 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002673 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002674 return NULL;
2675 }
2676 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002677 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002678 return NULL;
2679 }
2680 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002681 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogersb4903572012-10-11 11:52:56 -07002682 const RegType& res_method_class = reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogers9074b992011-10-26 17:41:55 -07002683 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002684 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002685 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002686 return NULL;
2687 }
2688 }
2689 actual_args++;
2690 }
2691 /*
2692 * Process the target method's signature. This signature may or may not
2693 * have been verified, so we can't assume it's properly formed.
2694 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002695 MethodHelper mh(res_method);
2696 const DexFile::TypeList* params = mh.GetParameterTypeList();
2697 size_t params_size = params == NULL ? 0 : params->Size();
2698 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002699 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002700 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002701 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2702 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002703 return NULL;
2704 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002705 const char* descriptor =
2706 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2707 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002708 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002709 << " missing signature component";
2710 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002711 }
Ian Rogersb4903572012-10-11 11:52:56 -07002712 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002713 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002714 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002715 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002716 }
2717 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2718 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002719 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002720 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002721 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002722 return NULL;
2723 } else {
2724 return res_method;
2725 }
2726}
2727
Ian Rogers776ac1f2012-04-13 23:36:36 -07002728void MethodVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08002729 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002730 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002731 if (res_type.IsConflict()) { // bad class
2732 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002733 } else {
2734 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2735 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002736 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002737 } else if (!is_filled) {
2738 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002739 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002740 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002741 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002742 } else {
2743 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2744 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002745 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002746 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002747 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002748 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002749 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002750 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002751 return;
2752 }
2753 }
2754 // filled-array result goes into "result" register
2755 work_line_->SetResultRegisterType(res_type);
2756 }
2757 }
2758}
2759
Ian Rogers776ac1f2012-04-13 23:36:36 -07002760void MethodVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002761 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002762 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002763 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002764 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002765 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002766 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002767 if (array_type.IsZero()) {
2768 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2769 // instruction type. TODO: have a proper notion of bottom here.
2770 if (!is_primitive || insn_type.IsCategory1Types()) {
2771 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08002772 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002773 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002774 // Category 2
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002775 work_line_->SetRegisterTypeWide(dec_insn.vA, reg_types_.FromCat2ConstLo(0, false),
2776 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08002777 }
jeffhaofc3144e2012-02-01 17:21:15 -08002778 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002779 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002780 } else {
2781 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002782 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002783 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002784 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002785 << " source for aget-object";
2786 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002787 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002788 << " source for category 1 aget";
2789 } else if (is_primitive && !insn_type.Equals(component_type) &&
2790 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002791 (insn_type.IsLong() && component_type.IsDouble()))) {
2792 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
2793 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002794 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002795 // Use knowledge of the field type which is stronger than the type inferred from the
2796 // instruction, which can't differentiate object types and ints from floats, longs from
2797 // doubles.
2798 if (!component_type.IsLowHalf()) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002799 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002800 } else {
2801 work_line_->SetRegisterTypeWide(dec_insn.vA, component_type,
2802 component_type.HighHalf(&reg_types_));
2803 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002804 }
2805 }
2806 }
2807}
2808
Ian Rogers776ac1f2012-04-13 23:36:36 -07002809void MethodVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002810 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002811 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002812 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002813 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002814 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002815 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002816 if (array_type.IsZero()) {
2817 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2818 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002819 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002820 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002821 } else {
2822 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002823 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002824 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002825 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002826 << " source for aput-object";
2827 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002828 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002829 << " source for category 1 aput";
2830 } else if (is_primitive && !insn_type.Equals(component_type) &&
2831 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2832 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002833 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002834 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002835 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002836 // The instruction agrees with the type of array, confirm the value to be stored does too
2837 // Note: we use the instruction type (rather than the component type) for aput-object as
2838 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08002839 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002840 }
2841 }
2842 }
2843}
2844
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002845mirror::Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002846 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2847 // Check access to class
2848 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002849 if (klass_type.IsConflict()) { // bad class
2850 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
2851 field_idx, dex_file_->GetFieldName(field_id),
2852 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002853 return NULL;
2854 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002855 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002856 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08002857 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002858 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07002859 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002860 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002861 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
2862 << dex_file_->GetFieldName(field_id) << ") in "
2863 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002864 DCHECK(Thread::Current()->IsExceptionPending());
2865 Thread::Current()->ClearException();
2866 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002867 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2868 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002869 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002870 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002871 return NULL;
2872 } else if (!field->IsStatic()) {
2873 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
2874 return NULL;
2875 } else {
2876 return field;
2877 }
2878}
2879
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002880mirror::Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002881 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2882 // Check access to class
2883 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002884 if (klass_type.IsConflict()) {
2885 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
2886 field_idx, dex_file_->GetFieldName(field_id),
2887 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002888 return NULL;
2889 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002890 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002891 return NULL; // Can't resolve Class so no more to do here
2892 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002893 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07002894 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002895 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002896 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
2897 << dex_file_->GetFieldName(field_id) << ") in "
2898 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002899 DCHECK(Thread::Current()->IsExceptionPending());
2900 Thread::Current()->ClearException();
2901 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002902 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2903 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002904 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002905 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002906 return NULL;
2907 } else if (field->IsStatic()) {
2908 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
2909 << " to not be static";
2910 return NULL;
2911 } else if (obj_type.IsZero()) {
2912 // Cannot infer and check type, however, access will cause null pointer exception
2913 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07002914 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002915 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersb4903572012-10-11 11:52:56 -07002916 const RegType& field_klass = reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002917 if (obj_type.IsUninitializedTypes() &&
2918 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
2919 !field_klass.Equals(GetDeclaringClass()))) {
2920 // Field accesses through uninitialized references are only allowable for constructors where
2921 // the field is declared in this class
2922 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
2923 << " of a not fully initialized object within the context of "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002924 << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002925 return NULL;
2926 } else if (!field_klass.IsAssignableFrom(obj_type)) {
2927 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
2928 // of C1. For resolution to occur the declared class of the field must be compatible with
2929 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
2930 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
2931 << " from object of type " << obj_type;
2932 return NULL;
2933 } else {
2934 return field;
2935 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002936 }
2937}
2938
Ian Rogers776ac1f2012-04-13 23:36:36 -07002939void MethodVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002940 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002941 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002942 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002943 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002944 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002945 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002946 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07002947 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002948 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002949 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002950 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002951 if (field != NULL) {
2952 descriptor = FieldHelper(field).GetTypeDescriptor();
2953 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07002954 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002955 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2956 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2957 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07002958 }
Ian Rogersb4903572012-10-11 11:52:56 -07002959 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002960 if (is_primitive) {
2961 if (field_type.Equals(insn_type) ||
2962 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
2963 (field_type.IsDouble() && insn_type.IsLongTypes())) {
2964 // expected that read is of the correct primitive type or that int reads are reading
2965 // floats or long reads are reading doubles
2966 } else {
2967 // This is a global failure rather than a class change failure as the instructions and
2968 // the descriptors for the type should have been consistent within the same file at
2969 // compile time
2970 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
2971 << " to be of type '" << insn_type
2972 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002973 return;
2974 }
2975 } else {
2976 if (!insn_type.IsAssignableFrom(field_type)) {
2977 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
2978 << " to be compatible with type '" << insn_type
2979 << "' but found type '" << field_type
2980 << "' in get-object";
2981 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Conflict());
2982 return;
2983 }
2984 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002985 if (!field_type.IsLowHalf()) {
2986 work_line_->SetRegisterType(dec_insn.vA, field_type);
2987 } else {
2988 work_line_->SetRegisterTypeWide(dec_insn.vA, field_type, field_type.HighHalf(&reg_types_));
2989 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002990}
2991
Ian Rogers776ac1f2012-04-13 23:36:36 -07002992void MethodVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002993 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002994 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002995 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002996 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07002997 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002998 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002999 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07003000 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003001 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003002 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003003 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003004 if (field != NULL) {
3005 descriptor = FieldHelper(field).GetTypeDescriptor();
3006 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003007 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003008 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3009 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3010 loader = class_loader_;
3011 }
Ian Rogersb4903572012-10-11 11:52:56 -07003012 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003013 if (field != NULL) {
3014 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3015 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3016 << " from other class " << GetDeclaringClass();
3017 return;
3018 }
3019 }
3020 if (is_primitive) {
3021 // Primitive field assignability rules are weaker than regular assignability rules
3022 bool instruction_compatible;
3023 bool value_compatible;
3024 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
3025 if (field_type.IsIntegralTypes()) {
3026 instruction_compatible = insn_type.IsIntegralTypes();
3027 value_compatible = value_type.IsIntegralTypes();
3028 } else if (field_type.IsFloat()) {
3029 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3030 value_compatible = value_type.IsFloatTypes();
3031 } else if (field_type.IsLong()) {
3032 instruction_compatible = insn_type.IsLong();
3033 value_compatible = value_type.IsLongTypes();
3034 } else if (field_type.IsDouble()) {
3035 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3036 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003037 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003038 instruction_compatible = false; // reference field with primitive store
3039 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003040 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003041 if (!instruction_compatible) {
3042 // This is a global failure rather than a class change failure as the instructions and
3043 // the descriptors for the type should have been consistent within the same file at
3044 // compile time
3045 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3046 << " to be of type '" << insn_type
3047 << "' but found type '" << field_type
3048 << "' in put";
3049 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003050 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003051 if (!value_compatible) {
3052 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
3053 << " of type " << value_type
3054 << " but expected " << field_type
3055 << " for store to " << PrettyField(field) << " in put";
3056 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003057 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003058 } else {
3059 if (!insn_type.IsAssignableFrom(field_type)) {
3060 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3061 << " to be compatible with type '" << insn_type
3062 << "' but found type '" << field_type
3063 << "' in put-object";
3064 return;
3065 }
3066 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003067 }
3068}
3069
Ian Rogers776ac1f2012-04-13 23:36:36 -07003070bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003071 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003072 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003073 return false;
3074 }
3075 return true;
3076}
3077
Ian Rogers776ac1f2012-04-13 23:36:36 -07003078bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003079 bool changed = true;
3080 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3081 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003082 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003083 * We haven't processed this instruction before, and we haven't touched the registers here, so
3084 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3085 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003086 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003087 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003088 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003089 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3090 if (gDebugVerify) {
3091 copy->CopyFromLine(target_line);
3092 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003093 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003094 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003095 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003096 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003097 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003098 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003099 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3100 << *copy.get() << " MERGE\n"
3101 << *merge_line << " ==\n"
3102 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003103 }
3104 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003105 if (changed) {
3106 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003107 }
3108 return true;
3109}
3110
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003111InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003112 return &insn_flags_[work_insn_idx_];
3113}
3114
Ian Rogersad0b3a32012-04-16 14:50:24 -07003115const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003116 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003117 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3118 uint16_t return_type_idx = proto_id.return_type_idx_;
3119 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003120 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003121}
3122
3123const RegType& MethodVerifier::GetDeclaringClass() {
3124 if (foo_method_ != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003125 mirror::Class* klass = foo_method_->GetDeclaringClass();
Ian Rogersb4903572012-10-11 11:52:56 -07003126 return reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003127 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003128 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003129 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogersb4903572012-10-11 11:52:56 -07003130 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003131 }
3132}
3133
Ian Rogers776ac1f2012-04-13 23:36:36 -07003134void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003135 size_t* log2_max_gc_pc) {
3136 size_t local_gc_points = 0;
3137 size_t max_insn = 0;
3138 size_t max_ref_reg = -1;
3139 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3140 if (insn_flags_[i].IsGcPoint()) {
3141 local_gc_points++;
3142 max_insn = i;
3143 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003144 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003145 }
3146 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003147 *gc_points = local_gc_points;
3148 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3149 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003150 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003151 i++;
3152 }
3153 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003154}
3155
Ian Rogers776ac1f2012-04-13 23:36:36 -07003156const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003157 size_t num_entries, ref_bitmap_bits, pc_bits;
3158 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3159 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003160 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003161 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003162 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003163 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003164 return NULL;
3165 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003166 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3167 // There are 2 bytes to encode the number of entries
3168 if (num_entries >= 65536) {
3169 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003170 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003171 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003172 return NULL;
3173 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003174 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003175 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003176 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003177 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003178 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003179 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003180 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003181 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003182 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003183 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003184 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003185 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3186 return NULL;
3187 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003188 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003189 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003190 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003191 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003192 return NULL;
3193 }
3194 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003195 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3196 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003197 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003198 table->push_back(num_entries & 0xFF);
3199 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003200 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003201 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3202 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003203 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003204 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003205 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003206 }
3207 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003208 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003209 }
3210 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003211 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003212 return table;
3213}
jeffhaoa0a764a2011-09-16 10:43:38 -07003214
Ian Rogers776ac1f2012-04-13 23:36:36 -07003215void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003216 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3217 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003218 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003219 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003220 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003221 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3222 if (insn_flags_[i].IsGcPoint()) {
3223 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003224 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003225 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3226 map_index++;
3227 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003228 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003229 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003230 CHECK_LT(j / 8, map.RegWidth());
3231 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3232 } else if ((j / 8) < map.RegWidth()) {
3233 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3234 } else {
3235 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3236 }
3237 }
3238 } else {
3239 CHECK(reg_bitmap == NULL);
3240 }
3241 }
3242}
jeffhaoa0a764a2011-09-16 10:43:38 -07003243
Ian Rogers1212a022013-03-04 10:48:41 -08003244void MethodVerifier::SetDexGcMap(CompilerDriver::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003245 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003246 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003247 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
3248 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003249 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003250 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003251 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003252 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003253 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003254 CHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003255}
3256
Ian Rogers1212a022013-03-04 10:48:41 -08003257const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(CompilerDriver::MethodReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003258 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003259 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
3260 if (it == dex_gc_maps_->end()) {
Ian Rogers64b6d142012-10-29 16:34:15 -07003261 LOG(WARNING) << "Didn't find GC map for: " << PrettyMethod(ref.second, *ref.first);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003262 return NULL;
3263 }
3264 CHECK(it->second != NULL);
3265 return it->second;
3266}
3267
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003268std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3269 RegisterLine* line = reg_table_.GetLine(dex_pc);
3270 std::vector<int32_t> result;
3271 for (size_t i = 0; i < line->NumRegs(); ++i) {
3272 const RegType& type = line->GetRegisterType(i);
3273 if (type.IsConstant()) {
3274 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3275 result.push_back(type.ConstantValue());
3276 } else if (type.IsConstantLo()) {
3277 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3278 result.push_back(type.ConstantValueLo());
3279 } else if (type.IsConstantHi()) {
3280 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3281 result.push_back(type.ConstantValueHi());
3282 } else if (type.IsIntegralTypes()) {
3283 result.push_back(kIntVReg);
3284 result.push_back(0);
3285 } else if (type.IsFloat()) {
3286 result.push_back(kFloatVReg);
3287 result.push_back(0);
3288 } else if (type.IsLong()) {
3289 result.push_back(kLongLoVReg);
3290 result.push_back(0);
3291 result.push_back(kLongHiVReg);
3292 result.push_back(0);
3293 ++i;
3294 } else if (type.IsDouble()) {
3295 result.push_back(kDoubleLoVReg);
3296 result.push_back(0);
3297 result.push_back(kDoubleHiVReg);
3298 result.push_back(0);
3299 ++i;
3300 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
3301 result.push_back(kUndefined);
3302 result.push_back(0);
3303 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003304 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003305 result.push_back(kReferenceVReg);
3306 result.push_back(0);
3307 }
3308 }
3309 return result;
3310}
3311
Ian Rogers0c7abda2012-09-19 13:33:42 -07003312Mutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
3313MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003314
3315Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
3316MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
3317
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003318void MethodVerifier::Init() {
Ian Rogers0c7abda2012-09-19 13:33:42 -07003319 dex_gc_maps_lock_ = new Mutex("verifier GC maps lock");
Ian Rogers50b35e22012-10-04 10:09:15 -07003320 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003321 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003322 MutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003323 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003324 }
3325
3326 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
3327 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003328 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003329 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
3330 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003331 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003332}
3333
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003334void MethodVerifier::Shutdown() {
Ian Rogers50b35e22012-10-04 10:09:15 -07003335 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003336 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003337 MutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003338 STLDeleteValues(dex_gc_maps_);
3339 delete dex_gc_maps_;
3340 dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003341 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003342 delete dex_gc_maps_lock_;
3343 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003344
3345 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003346 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003347 delete rejected_classes_;
3348 rejected_classes_ = NULL;
3349 }
3350 delete rejected_classes_lock_;
3351 rejected_classes_lock_ = NULL;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003352 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003353}
jeffhaod1224c72012-02-29 13:43:08 -08003354
Ian Rogers1212a022013-03-04 10:48:41 -08003355void MethodVerifier::AddRejectedClass(CompilerDriver::ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003356 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003357 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003358 rejected_classes_->insert(ref);
3359 }
jeffhaod1224c72012-02-29 13:43:08 -08003360 CHECK(IsClassRejected(ref));
3361}
3362
Ian Rogers1212a022013-03-04 10:48:41 -08003363bool MethodVerifier::IsClassRejected(CompilerDriver::ClassReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003364 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003365 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08003366}
3367
Ian Rogersd81871c2011-10-03 13:57:23 -07003368} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003369} // namespace art