blob: fb60c904a1780d1e89b08fee4485c3ddb52c4007 [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"
Brian Carlstrom1f870082011-08-23 16:02:11 -070025#include "dex_file.h"
26#include "dex_instruction.h"
27#include "dex_instruction_visitor.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "gc/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080029#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070030#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070031#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "mirror/abstract_method-inl.h"
33#include "mirror/class.h"
34#include "mirror/class-inl.h"
35#include "mirror/dex_cache.h"
36#include "mirror/field-inl.h"
37#include "mirror/object-inl.h"
38#include "mirror/object_array-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080039#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070040#include "runtime.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080041#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070042
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,
75 std::string& error) {
jeffhaobdb76512011-09-07 11:43:16 -070076 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -070077 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -070078 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079 mirror::Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -080080 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -080081 error = "Verifier rejected class ";
82 error += PrettyDescriptor(klass);
83 error += " that has no super class";
jeffhaof1e6b7c2012-06-05 18:33:30 -070084 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070085 }
Ian Rogers1c5eb702012-02-01 09:18:34 -080086 if (super != NULL && super->IsFinal()) {
87 error = "Verifier rejected class ";
88 error += PrettyDescriptor(klass);
89 error += " that attempts to sub-class final class ";
90 error += PrettyDescriptor(super);
jeffhaof1e6b7c2012-06-05 18:33:30 -070091 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070092 }
Ian Rogersad0b3a32012-04-16 14:50:24 -070093 ClassHelper kh(klass);
94 const DexFile& dex_file = kh.GetDexFile();
95 uint32_t class_def_idx;
96 if (!dex_file.FindClassDefIndex(kh.GetDescriptor(), class_def_idx)) {
97 error = "Verifier rejected class ";
98 error += PrettyDescriptor(klass);
99 error += " that isn't present in dex file ";
100 error += dex_file.GetLocation();
jeffhaof1e6b7c2012-06-05 18:33:30 -0700101 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700102 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700103 return VerifyClass(&dex_file, kh.GetDexCache(), klass->GetClassLoader(), class_def_idx, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700104}
105
Ian Rogers365c1022012-06-22 15:05:28 -0700106MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107 mirror::DexCache* dex_cache,
108 mirror::ClassLoader* class_loader,
109 uint32_t class_def_idx,
110 std::string& error) {
jeffhaof56197c2012-03-05 18:01:54 -0800111 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
112 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700113 if (class_data == NULL) {
114 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700115 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700116 }
jeffhaof56197c2012-03-05 18:01:54 -0800117 ClassDataItemIterator it(*dex_file, class_data);
118 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
119 it.Next();
120 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700121 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700122 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700123 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700124 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800125 while (it.HasNextDirectMethod()) {
126 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700127 if (method_idx == previous_direct_method_idx) {
128 // smali can create dex files with two encoded_methods sharing the same method_idx
129 // http://code.google.com/p/smali/issues/detail?id=119
130 it.Next();
131 continue;
132 }
133 previous_direct_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700134 InvokeType type = it.GetMethodInvokeType(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800135 mirror::AbstractMethod* method =
136 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700137 if (method == NULL) {
138 DCHECK(Thread::Current()->IsExceptionPending());
139 // We couldn't resolve the method, but continue regardless.
140 Thread::Current()->ClearException();
141 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700142 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
143 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags());
144 if (result != kNoFailure) {
145 if (result == kHardFailure) {
146 hard_fail = true;
147 if (error_count > 0) {
148 error += "\n";
149 }
150 error = "Verifier rejected class ";
151 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
152 error += " due to bad method ";
153 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700154 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700155 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800156 }
157 it.Next();
158 }
jeffhao9b0b1882012-10-01 16:51:22 -0700159 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800160 while (it.HasNextVirtualMethod()) {
161 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700162 if (method_idx == previous_virtual_method_idx) {
163 // smali can create dex files with two encoded_methods sharing the same method_idx
164 // http://code.google.com/p/smali/issues/detail?id=119
165 it.Next();
166 continue;
167 }
168 previous_virtual_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700169 InvokeType type = it.GetMethodInvokeType(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170 mirror::AbstractMethod* method =
171 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700172 if (method == NULL) {
173 DCHECK(Thread::Current()->IsExceptionPending());
174 // We couldn't resolve the method, but continue regardless.
175 Thread::Current()->ClearException();
176 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700177 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
178 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags());
179 if (result != kNoFailure) {
180 if (result == kHardFailure) {
181 hard_fail = true;
182 if (error_count > 0) {
183 error += "\n";
184 }
185 error = "Verifier rejected class ";
186 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
187 error += " due to bad method ";
188 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700189 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700190 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800191 }
192 it.Next();
193 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700194 if (error_count == 0) {
195 return kNoFailure;
196 } else {
197 return hard_fail ? kHardFailure : kSoftFailure;
198 }
jeffhaof56197c2012-03-05 18:01:54 -0800199}
200
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800201MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
202 const DexFile* dex_file,
203 mirror::DexCache* dex_cache,
204 mirror::ClassLoader* class_loader,
205 uint32_t class_def_idx,
206 const DexFile::CodeItem* code_item,
207 mirror::AbstractMethod* method,
208 uint32_t method_access_flags) {
Ian Rogersc8982582012-09-07 16:53:25 -0700209 MethodVerifier::FailureKind result = kNoFailure;
210 uint64_t start_ns = NanoTime();
211
Ian Rogersad0b3a32012-04-16 14:50:24 -0700212 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item, method_idx,
Elliott Hughes80537bb2013-01-04 16:37:26 -0800213 method, method_access_flags, true);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700214 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700215 // Verification completed, however failures may be pending that didn't cause the verification
216 // to hard fail.
Ian Rogerse551e952012-06-03 22:59:14 -0700217 CHECK(!verifier.have_pending_hard_failure_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700218 if (verifier.failures_.size() != 0) {
219 verifier.DumpFailures(LOG(INFO) << "Soft verification failures in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700220 << PrettyMethod(method_idx, *dex_file) << "\n");
Ian Rogersc8982582012-09-07 16:53:25 -0700221 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800222 }
223 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700224 // Bad method data.
225 CHECK_NE(verifier.failures_.size(), 0U);
226 CHECK(verifier.have_pending_hard_failure_);
227 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700228 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800229 if (gDebugVerify) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700230 std::cout << "\n" << verifier.info_messages_.str();
jeffhaof56197c2012-03-05 18:01:54 -0800231 verifier.Dump(std::cout);
232 }
Ian Rogersc8982582012-09-07 16:53:25 -0700233 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800234 }
Ian Rogersc8982582012-09-07 16:53:25 -0700235 uint64_t duration_ns = NanoTime() - start_ns;
236 if (duration_ns > MsToNs(100)) {
237 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
238 << " took " << PrettyDuration(duration_ns);
239 }
240 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800241}
242
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800243void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800244 const DexFile* dex_file, mirror::DexCache* dex_cache,
245 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
246 const DexFile::CodeItem* code_item,
247 mirror::AbstractMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800248 uint32_t method_access_flags) {
249 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item,
Elliott Hughes80537bb2013-01-04 16:37:26 -0800250 dex_method_idx, method, method_access_flags, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700251 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800252 verifier.DumpFailures(os);
253 os << verifier.info_messages_.str();
254 verifier.Dump(os);
255}
256
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800257MethodVerifier::MethodVerifier(const DexFile* dex_file, mirror::DexCache* dex_cache,
258 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
259 const DexFile::CodeItem* code_item,
260 uint32_t dex_method_idx, mirror::AbstractMethod* method,
261 uint32_t method_access_flags,
Elliott Hughes80537bb2013-01-04 16:37:26 -0800262 bool can_load_classes)
263 : reg_types_(can_load_classes),
264 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800265 dex_method_idx_(dex_method_idx),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700266 foo_method_(method),
267 method_access_flags_(method_access_flags),
jeffhaof56197c2012-03-05 18:01:54 -0800268 dex_file_(dex_file),
269 dex_cache_(dex_cache),
270 class_loader_(class_loader),
271 class_def_idx_(class_def_idx),
272 code_item_(code_item),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700273 interesting_dex_pc_(-1),
274 monitor_enter_dex_pcs_(NULL),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700275 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700276 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800277 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800278 monitor_enter_count_(0),
279 can_load_classes_(can_load_classes) {
jeffhaof56197c2012-03-05 18:01:54 -0800280}
281
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800282void MethodVerifier::FindLocksAtDexPc(mirror::AbstractMethod* m, uint32_t dex_pc,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800283 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700284 MethodHelper mh(m);
285 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
286 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800287 m, m->GetAccessFlags(), false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700288 verifier.interesting_dex_pc_ = dex_pc;
289 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
290 verifier.FindLocksAtDexPc();
291}
292
293void MethodVerifier::FindLocksAtDexPc() {
294 CHECK(monitor_enter_dex_pcs_ != NULL);
295 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
296
297 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
298 // verification. In practice, the phase we want relies on data structures set up by all the
299 // earlier passes, so we just run the full method verification and bail out early when we've
300 // got what we wanted.
301 Verify();
302}
303
Ian Rogersad0b3a32012-04-16 14:50:24 -0700304bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700305 // If there aren't any instructions, make sure that's expected, then exit successfully.
306 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700307 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700308 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700309 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700310 } else {
311 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700312 }
jeffhaobdb76512011-09-07 11:43:16 -0700313 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700314 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
315 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700316 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
317 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700318 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700319 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700320 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800321 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700322 // Run through the instructions and see if the width checks out.
323 bool result = ComputeWidthsAndCountOps();
324 // Flag instructions guarded by a "try" block and check exception handlers.
325 result = result && ScanTryCatchBlocks();
326 // Perform static instruction verification.
327 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700328 // Perform code-flow analysis and return.
329 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700330}
331
Ian Rogers776ac1f2012-04-13 23:36:36 -0700332std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700333 switch (error) {
334 case VERIFY_ERROR_NO_CLASS:
335 case VERIFY_ERROR_NO_FIELD:
336 case VERIFY_ERROR_NO_METHOD:
337 case VERIFY_ERROR_ACCESS_CLASS:
338 case VERIFY_ERROR_ACCESS_FIELD:
339 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700340 case VERIFY_ERROR_INSTANTIATION:
341 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800342 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700343 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
344 // class change and instantiation errors into soft verification errors so that we re-verify
345 // at runtime. We may fail to find or to agree on access because of not yet available class
346 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
347 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
348 // paths" that dynamically perform the verification and cause the behavior to be that akin
349 // to an interpreter.
350 error = VERIFY_ERROR_BAD_CLASS_SOFT;
351 } else {
352 have_pending_runtime_throw_failure_ = true;
353 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700354 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700355 // Indication that verification should be retried at runtime.
356 case VERIFY_ERROR_BAD_CLASS_SOFT:
357 if (!Runtime::Current()->IsCompiler()) {
358 // It is runtime so hard fail.
359 have_pending_hard_failure_ = true;
360 }
361 break;
jeffhaod5347e02012-03-22 17:25:05 -0700362 // Hard verification failures at compile time will still fail at runtime, so the class is
363 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700364 case VERIFY_ERROR_BAD_CLASS_HARD: {
365 if (Runtime::Current()->IsCompiler()) {
Ian Rogers1212a022013-03-04 10:48:41 -0800366 CompilerDriver::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800367 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800368 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700369 have_pending_hard_failure_ = true;
370 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800371 }
372 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700373 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800374 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700375 work_insn_idx_));
376 std::ostringstream* failure_message = new std::ostringstream(location);
377 failure_messages_.push_back(failure_message);
378 return *failure_message;
379}
380
381void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
382 size_t failure_num = failure_messages_.size();
383 DCHECK_NE(failure_num, 0U);
384 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
385 prepend += last_fail_message->str();
386 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
387 delete last_fail_message;
388}
389
390void MethodVerifier::AppendToLastFailMessage(std::string append) {
391 size_t failure_num = failure_messages_.size();
392 DCHECK_NE(failure_num, 0U);
393 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
394 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800395}
396
Ian Rogers776ac1f2012-04-13 23:36:36 -0700397bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700398 const uint16_t* insns = code_item_->insns_;
399 size_t insns_size = code_item_->insns_size_in_code_units_;
400 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700401 size_t new_instance_count = 0;
402 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700403 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700404
Ian Rogersd81871c2011-10-03 13:57:23 -0700405 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700406 Instruction::Code opcode = inst->Opcode();
407 if (opcode == Instruction::NEW_INSTANCE) {
408 new_instance_count++;
409 } else if (opcode == Instruction::MONITOR_ENTER) {
410 monitor_enter_count++;
411 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700412 size_t inst_size = inst->SizeInCodeUnits();
413 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
414 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700415 inst = inst->Next();
416 }
417
Ian Rogersd81871c2011-10-03 13:57:23 -0700418 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700419 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
420 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700421 return false;
422 }
423
Ian Rogersd81871c2011-10-03 13:57:23 -0700424 new_instance_count_ = new_instance_count;
425 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700426 return true;
427}
428
Ian Rogers776ac1f2012-04-13 23:36:36 -0700429bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700430 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700431 if (tries_size == 0) {
432 return true;
433 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700434 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700435 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700436
437 for (uint32_t idx = 0; idx < tries_size; idx++) {
438 const DexFile::TryItem* try_item = &tries[idx];
439 uint32_t start = try_item->start_addr_;
440 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700441 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700442 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
443 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700444 return false;
445 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700446 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700447 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700448 return false;
449 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700450 for (uint32_t dex_pc = start; dex_pc < end;
451 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
452 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700453 }
454 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800455 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700456 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700457 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700458 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700459 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700460 CatchHandlerIterator iterator(handlers_ptr);
461 for (; iterator.HasNext(); iterator.Next()) {
462 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700463 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700464 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700465 return false;
466 }
jeffhao60f83e32012-02-13 17:16:30 -0800467 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
468 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700469 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700470 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800471 return false;
472 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700473 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700474 // Ensure exception types are resolved so that they don't need resolution to be delivered,
475 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700476 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800477 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
478 iterator.GetHandlerTypeIndex(),
479 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700480 if (exception_type == NULL) {
481 DCHECK(Thread::Current()->IsExceptionPending());
482 Thread::Current()->ClearException();
483 }
484 }
jeffhaobdb76512011-09-07 11:43:16 -0700485 }
Ian Rogers0571d352011-11-03 19:51:38 -0700486 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700487 }
jeffhaobdb76512011-09-07 11:43:16 -0700488 return true;
489}
490
Ian Rogers776ac1f2012-04-13 23:36:36 -0700491bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700492 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700493
Ian Rogers0c7abda2012-09-19 13:33:42 -0700494 /* 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 -0700495 insn_flags_[0].SetBranchTarget();
Ian Rogers0c7abda2012-09-19 13:33:42 -0700496 insn_flags_[0].SetGcPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700497
498 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700499 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700500 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700501 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700502 return false;
503 }
504 /* Flag instructions that are garbage collection points */
505 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
506 insn_flags_[dex_pc].SetGcPoint();
507 }
508 dex_pc += inst->SizeInCodeUnits();
509 inst = inst->Next();
510 }
511 return true;
512}
513
Ian Rogers776ac1f2012-04-13 23:36:36 -0700514bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800515 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700516 bool result = true;
517 switch (inst->GetVerifyTypeArgumentA()) {
518 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800519 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700520 break;
521 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800522 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700523 break;
524 }
525 switch (inst->GetVerifyTypeArgumentB()) {
526 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800527 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700528 break;
529 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800530 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700531 break;
532 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800533 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700534 break;
535 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800536 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700537 break;
538 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800539 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700540 break;
541 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800542 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700543 break;
544 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800545 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700546 break;
547 }
548 switch (inst->GetVerifyTypeArgumentC()) {
549 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800550 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700551 break;
552 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800553 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700554 break;
555 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800556 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700557 break;
558 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800559 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700560 break;
561 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800562 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700563 break;
564 }
565 switch (inst->GetVerifyExtraFlags()) {
566 case Instruction::kVerifyArrayData:
567 result = result && CheckArrayData(code_offset);
568 break;
569 case Instruction::kVerifyBranchTarget:
570 result = result && CheckBranchTarget(code_offset);
571 break;
572 case Instruction::kVerifySwitchTargets:
573 result = result && CheckSwitchTargets(code_offset);
574 break;
575 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800576 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700577 break;
578 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800579 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700580 break;
581 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700582 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700583 result = false;
584 break;
585 }
586 return result;
587}
588
Ian Rogers776ac1f2012-04-13 23:36:36 -0700589bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700590 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700591 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
592 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700593 return false;
594 }
595 return true;
596}
597
Ian Rogers776ac1f2012-04-13 23:36:36 -0700598bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700599 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700600 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
601 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700602 return false;
603 }
604 return true;
605}
606
Ian Rogers776ac1f2012-04-13 23:36:36 -0700607bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700608 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700609 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
610 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700611 return false;
612 }
613 return true;
614}
615
Ian Rogers776ac1f2012-04-13 23:36:36 -0700616bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700617 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700618 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
619 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700620 return false;
621 }
622 return true;
623}
624
Ian Rogers776ac1f2012-04-13 23:36:36 -0700625bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700626 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700627 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
628 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700629 return false;
630 }
631 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700632 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700633 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700634 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700635 return false;
636 }
637 return true;
638}
639
Ian Rogers776ac1f2012-04-13 23:36:36 -0700640bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700641 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700642 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
643 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700644 return false;
645 }
646 return true;
647}
648
Ian Rogers776ac1f2012-04-13 23:36:36 -0700649bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700650 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700651 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
652 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700653 return false;
654 }
655 return true;
656}
657
Ian Rogers776ac1f2012-04-13 23:36:36 -0700658bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700659 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700660 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
661 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700662 return false;
663 }
664 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700665 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700666 const char* cp = descriptor;
667 while (*cp++ == '[') {
668 bracket_count++;
669 }
670 if (bracket_count == 0) {
671 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700672 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700673 return false;
674 } else if (bracket_count > 255) {
675 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700676 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700677 return false;
678 }
679 return true;
680}
681
Ian Rogers776ac1f2012-04-13 23:36:36 -0700682bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
684 const uint16_t* insns = code_item_->insns_ + cur_offset;
685 const uint16_t* array_data;
686 int32_t array_data_offset;
687
688 DCHECK_LT(cur_offset, insn_count);
689 /* make sure the start of the array data table is in range */
690 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
691 if ((int32_t) cur_offset + array_data_offset < 0 ||
692 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700693 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
694 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700695 return false;
696 }
697 /* offset to array data table is a relative branch-style offset */
698 array_data = insns + array_data_offset;
699 /* make sure the table is 32-bit aligned */
700 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700701 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
702 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700703 return false;
704 }
705 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700706 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700707 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
708 /* make sure the end of the switch is in range */
709 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700710 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
711 << ", data offset " << array_data_offset << ", end "
712 << cur_offset + array_data_offset + table_size
713 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700714 return false;
715 }
716 return true;
717}
718
Ian Rogers776ac1f2012-04-13 23:36:36 -0700719bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700720 int32_t offset;
721 bool isConditional, selfOkay;
722 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
723 return false;
724 }
725 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700726 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 -0700727 return false;
728 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700729 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
730 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700731 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700732 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700733 return false;
734 }
735 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
736 int32_t abs_offset = cur_offset + offset;
737 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700738 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700739 << reinterpret_cast<void*>(abs_offset) << ") at "
740 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700741 return false;
742 }
743 insn_flags_[abs_offset].SetBranchTarget();
744 return true;
745}
746
Ian Rogers776ac1f2012-04-13 23:36:36 -0700747bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700748 bool* selfOkay) {
749 const uint16_t* insns = code_item_->insns_ + cur_offset;
750 *pConditional = false;
751 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700752 switch (*insns & 0xff) {
753 case Instruction::GOTO:
754 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700755 break;
756 case Instruction::GOTO_32:
757 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700758 *selfOkay = true;
759 break;
760 case Instruction::GOTO_16:
761 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700762 break;
763 case Instruction::IF_EQ:
764 case Instruction::IF_NE:
765 case Instruction::IF_LT:
766 case Instruction::IF_GE:
767 case Instruction::IF_GT:
768 case Instruction::IF_LE:
769 case Instruction::IF_EQZ:
770 case Instruction::IF_NEZ:
771 case Instruction::IF_LTZ:
772 case Instruction::IF_GEZ:
773 case Instruction::IF_GTZ:
774 case Instruction::IF_LEZ:
775 *pOffset = (int16_t) insns[1];
776 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700777 break;
778 default:
779 return false;
780 break;
781 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700782 return true;
783}
784
Ian Rogers776ac1f2012-04-13 23:36:36 -0700785bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700786 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700787 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700788 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700789 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700790 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
791 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700792 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
793 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700794 return false;
795 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700796 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700797 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700798 /* make sure the table is 32-bit aligned */
799 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700800 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
801 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700802 return false;
803 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700804 uint32_t switch_count = switch_insns[1];
805 int32_t keys_offset, targets_offset;
806 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700807 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
808 /* 0=sig, 1=count, 2/3=firstKey */
809 targets_offset = 4;
810 keys_offset = -1;
811 expected_signature = Instruction::kPackedSwitchSignature;
812 } else {
813 /* 0=sig, 1=count, 2..count*2 = keys */
814 keys_offset = 2;
815 targets_offset = 2 + 2 * switch_count;
816 expected_signature = Instruction::kSparseSwitchSignature;
817 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700818 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700819 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700820 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
821 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700822 return false;
823 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700824 /* make sure the end of the switch is in range */
825 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700826 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
827 << switch_offset << ", end "
828 << (cur_offset + switch_offset + table_size)
829 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700830 return false;
831 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700832 /* for a sparse switch, verify the keys are in ascending order */
833 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700834 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
835 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700836 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
837 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
838 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700839 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
840 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700841 return false;
842 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700843 last_key = key;
844 }
845 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700846 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700847 for (uint32_t targ = 0; targ < switch_count; targ++) {
848 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
849 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
850 int32_t abs_offset = cur_offset + offset;
851 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700852 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700853 << reinterpret_cast<void*>(abs_offset) << ") at "
854 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700855 return false;
856 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700857 insn_flags_[abs_offset].SetBranchTarget();
858 }
859 return true;
860}
861
Ian Rogers776ac1f2012-04-13 23:36:36 -0700862bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700863 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700864 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 return false;
866 }
867 uint16_t registers_size = code_item_->registers_size_;
868 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800869 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700870 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
871 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700872 return false;
873 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700874 }
875
876 return true;
877}
878
Ian Rogers776ac1f2012-04-13 23:36:36 -0700879bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700880 uint16_t registers_size = code_item_->registers_size_;
881 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
882 // integer overflow when adding them here.
883 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700884 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
885 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700886 return false;
887 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700888 return true;
889}
890
Ian Rogers0c7abda2012-09-19 13:33:42 -0700891static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -0800892 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
893 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
894 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
895 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
896 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
897 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
898 gc_map.begin(),
899 gc_map.end());
900 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
901 DCHECK_EQ(gc_map.size(),
902 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
903 (length_prefixed_gc_map->at(1) << 16) |
904 (length_prefixed_gc_map->at(2) << 8) |
905 (length_prefixed_gc_map->at(3) << 0)));
906 return length_prefixed_gc_map;
907}
908
Ian Rogers776ac1f2012-04-13 23:36:36 -0700909bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700910 uint16_t registers_size = code_item_->registers_size_;
911 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -0700912
Ian Rogersd81871c2011-10-03 13:57:23 -0700913 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -0800914 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
915 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700916 }
917 /* Create and initialize table holding register status */
Elliott Hughes460384f2012-04-04 16:53:10 -0700918 reg_table_.Init(kTrackRegsGcPoints, insn_flags_.get(), insns_size, registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -0700919
Ian Rogersd81871c2011-10-03 13:57:23 -0700920 work_line_.reset(new RegisterLine(registers_size, this));
921 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -0700922
Ian Rogersd81871c2011-10-03 13:57:23 -0700923 /* Initialize register types of method arguments. */
924 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700925 DCHECK_NE(failures_.size(), 0U);
926 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800927 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700928 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -0700929 return false;
930 }
931 /* Perform code flow verification. */
932 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700933 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700934 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700935 }
936
Ian Rogers1212a022013-03-04 10:48:41 -0800937 CompilerDriver::MethodReference ref(dex_file_, dex_method_idx_);
TDYa127b2eb5c12012-05-24 15:52:10 -0700938
TDYa127b2eb5c12012-05-24 15:52:10 -0700939
Ian Rogersd81871c2011-10-03 13:57:23 -0700940 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -0800941 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
942 if (map.get() == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700943 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 return false; // Not a real failure, but a failure to encode
945 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700946#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800947 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -0700948#endif
Ian Rogers0c7abda2012-09-19 13:33:42 -0700949 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
950 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
Logan Chiendd361c92012-04-10 23:40:37 +0800951
jeffhaobdb76512011-09-07 11:43:16 -0700952 return true;
953}
954
Ian Rogersad0b3a32012-04-16 14:50:24 -0700955std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
956 DCHECK_EQ(failures_.size(), failure_messages_.size());
957 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700958 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -0700959 }
960 return os;
961}
962
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700963extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700964 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700965 v->Dump(std::cerr);
966}
967
Ian Rogers776ac1f2012-04-13 23:36:36 -0700968void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -0800969 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700970 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 return;
jeffhaobdb76512011-09-07 11:43:16 -0700972 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800973 {
974 os << "Register Types:\n";
975 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
976 std::ostream indent_os(&indent_filter);
977 reg_types_.Dump(indent_os);
978 }
Ian Rogersb4903572012-10-11 11:52:56 -0700979 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800980 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
981 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -0700982 const Instruction* inst = Instruction::At(code_item_->insns_);
983 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
984 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700985 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
986 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800987 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -0700988 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800989 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800990 const bool kDumpHexOfInstruction = false;
991 if (kDumpHexOfInstruction) {
992 indent_os << inst->DumpHex(5) << " ";
993 }
994 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -0700995 inst = inst->Next();
996 }
jeffhaobdb76512011-09-07 11:43:16 -0700997}
998
Ian Rogersd81871c2011-10-03 13:57:23 -0700999static bool IsPrimitiveDescriptor(char descriptor) {
1000 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001001 case 'I':
1002 case 'C':
1003 case 'S':
1004 case 'B':
1005 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001006 case 'F':
1007 case 'D':
1008 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001009 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001010 default:
1011 return false;
1012 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001013}
1014
Ian Rogers776ac1f2012-04-13 23:36:36 -07001015bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001016 RegisterLine* reg_line = reg_table_.GetLine(0);
1017 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1018 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001019
Ian Rogersd81871c2011-10-03 13:57:23 -07001020 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1021 //Include the "this" pointer.
1022 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001023 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001024 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1025 // argument as uninitialized. This restricts field access until the superclass constructor is
1026 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001027 const RegType& declaring_class = GetDeclaringClass();
1028 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001029 reg_line->SetRegisterType(arg_start + cur_arg,
1030 reg_types_.UninitializedThisArgument(declaring_class));
1031 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001032 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001033 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001034 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001035 }
1036
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001037 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001038 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001039 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001040
1041 for (; iterator.HasNext(); iterator.Next()) {
1042 const char* descriptor = iterator.GetDescriptor();
1043 if (descriptor == NULL) {
1044 LOG(FATAL) << "Null descriptor";
1045 }
1046 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001047 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1048 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001049 return false;
1050 }
1051 switch (descriptor[0]) {
1052 case 'L':
1053 case '[':
1054 // We assume that reference arguments are initialized. The only way it could be otherwise
1055 // (assuming the caller was verified) is if the current method is <init>, but in that case
1056 // it's effectively considered initialized the instant we reach here (in the sense that we
1057 // can return without doing anything or call virtual methods).
1058 {
Ian Rogersb4903572012-10-11 11:52:56 -07001059 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001060 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001061 }
1062 break;
1063 case 'Z':
1064 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1065 break;
1066 case 'C':
1067 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1068 break;
1069 case 'B':
1070 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1071 break;
1072 case 'I':
1073 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1074 break;
1075 case 'S':
1076 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1077 break;
1078 case 'F':
1079 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1080 break;
1081 case 'J':
1082 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001083 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1084 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1085 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001086 cur_arg++;
1087 break;
1088 }
1089 default:
jeffhaod5347e02012-03-22 17:25:05 -07001090 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001091 return false;
1092 }
1093 cur_arg++;
1094 }
1095 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001096 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001097 return false;
1098 }
1099 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1100 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1101 // format. Only major difference from the method argument format is that 'V' is supported.
1102 bool result;
1103 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1104 result = descriptor[1] == '\0';
1105 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1106 size_t i = 0;
1107 do {
1108 i++;
1109 } while (descriptor[i] == '['); // process leading [
1110 if (descriptor[i] == 'L') { // object array
1111 do {
1112 i++; // find closing ;
1113 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1114 result = descriptor[i] == ';';
1115 } else { // primitive array
1116 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1117 }
1118 } else if (descriptor[0] == 'L') {
1119 // could be more thorough here, but shouldn't be required
1120 size_t i = 0;
1121 do {
1122 i++;
1123 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1124 result = descriptor[i] == ';';
1125 } else {
1126 result = false;
1127 }
1128 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001129 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1130 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001131 }
1132 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001133}
1134
Ian Rogers776ac1f2012-04-13 23:36:36 -07001135bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001136 const uint16_t* insns = code_item_->insns_;
1137 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001138
jeffhaobdb76512011-09-07 11:43:16 -07001139 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001140 insn_flags_[0].SetChanged();
1141 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001142
jeffhaobdb76512011-09-07 11:43:16 -07001143 /* Continue until no instructions are marked "changed". */
1144 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001145 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1146 uint32_t insn_idx = start_guess;
1147 for (; insn_idx < insns_size; insn_idx++) {
1148 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001149 break;
1150 }
jeffhaobdb76512011-09-07 11:43:16 -07001151 if (insn_idx == insns_size) {
1152 if (start_guess != 0) {
1153 /* try again, starting from the top */
1154 start_guess = 0;
1155 continue;
1156 } else {
1157 /* all flags are clear */
1158 break;
1159 }
1160 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001161 // We carry the working set of registers from instruction to instruction. If this address can
1162 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1163 // "changed" flags, we need to load the set of registers from the table.
1164 // Because we always prefer to continue on to the next instruction, we should never have a
1165 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1166 // target.
1167 work_insn_idx_ = insn_idx;
1168 if (insn_flags_[insn_idx].IsBranchTarget()) {
1169 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001170 } else {
1171#ifndef NDEBUG
1172 /*
1173 * Sanity check: retrieve the stored register line (assuming
1174 * a full table) and make sure it actually matches.
1175 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001176 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1177 if (register_line != NULL) {
1178 if (work_line_->CompareLine(register_line) != 0) {
1179 Dump(std::cout);
1180 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001181 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001182 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1183 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001184 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001185 }
jeffhaobdb76512011-09-07 11:43:16 -07001186 }
1187#endif
1188 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001189 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001190 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001191 prepend += " failed to verify: ";
1192 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001193 return false;
1194 }
jeffhaobdb76512011-09-07 11:43:16 -07001195 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001196 insn_flags_[insn_idx].SetVisited();
1197 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001198 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001199
Ian Rogers1c849e52012-06-28 14:00:33 -07001200 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001201 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001202 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001203 * (besides the wasted space), but it indicates a flaw somewhere
1204 * down the line, possibly in the verifier.
1205 *
1206 * If we've substituted "always throw" instructions into the stream,
1207 * we are almost certainly going to have some dead code.
1208 */
1209 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001210 uint32_t insn_idx = 0;
1211 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001212 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001213 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001214 * may or may not be preceded by a padding NOP (for alignment).
1215 */
1216 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1217 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1218 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001219 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001220 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1221 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1222 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001223 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001224 }
1225
Ian Rogersd81871c2011-10-03 13:57:23 -07001226 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001227 if (dead_start < 0)
1228 dead_start = insn_idx;
1229 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001230 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001231 dead_start = -1;
1232 }
1233 }
1234 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001235 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001236 }
1237 }
jeffhaobdb76512011-09-07 11:43:16 -07001238 return true;
1239}
1240
Ian Rogers776ac1f2012-04-13 23:36:36 -07001241bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001242 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1243 // We want the state _before_ the instruction, for the case where the dex pc we're
1244 // interested in is itself a monitor-enter instruction (which is a likely place
1245 // for a thread to be suspended).
1246 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001247 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001248 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1249 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1250 }
1251 }
1252
jeffhaobdb76512011-09-07 11:43:16 -07001253 /*
1254 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001255 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001256 * control to another statement:
1257 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001258 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001259 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001260 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001261 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001262 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001263 * throw an exception that is handled by an encompassing "try"
1264 * block.
1265 *
1266 * We can also return, in which case there is no successor instruction
1267 * from this point.
1268 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001269 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001270 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001271 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1272 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001273 DecodedInstruction dec_insn(inst);
Ian Rogersa75a0132012-09-28 11:41:42 -07001274 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001275
jeffhaobdb76512011-09-07 11:43:16 -07001276 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001277 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001278 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001279 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001280 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1281 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001282 }
jeffhaobdb76512011-09-07 11:43:16 -07001283
1284 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001285 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001286 * can throw an exception, we will copy/merge this into the "catch"
1287 * address rather than work_line, because we don't want the result
1288 * from the "successful" code path (e.g. a check-cast that "improves"
1289 * a type) to be visible to the exception handler.
1290 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001291 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001292 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001293 } else {
1294#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001295 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001296#endif
1297 }
1298
Elliott Hughesadb8c672012-03-06 16:49:32 -08001299 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07001300 case Instruction::NOP:
1301 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001302 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001303 * a signature that looks like a NOP; if we see one of these in
1304 * the course of executing code then we have a problem.
1305 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001306 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001307 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001308 }
1309 break;
1310
1311 case Instruction::MOVE:
1312 case Instruction::MOVE_FROM16:
1313 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001314 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001315 break;
1316 case Instruction::MOVE_WIDE:
1317 case Instruction::MOVE_WIDE_FROM16:
1318 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001319 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07001320 break;
1321 case Instruction::MOVE_OBJECT:
1322 case Instruction::MOVE_OBJECT_FROM16:
1323 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001324 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001325 break;
1326
1327 /*
1328 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001329 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001330 * might want to hold the result in an actual CPU register, so the
1331 * Dalvik spec requires that these only appear immediately after an
1332 * invoke or filled-new-array.
1333 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001334 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001335 * redundant with the reset done below, but it can make the debug info
1336 * easier to read in some cases.)
1337 */
1338 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001339 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07001340 break;
1341 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001342 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001343 break;
1344 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001345 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07001346 break;
1347
Ian Rogersd81871c2011-10-03 13:57:23 -07001348 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001349 /*
jeffhao60f83e32012-02-13 17:16:30 -08001350 * This statement can only appear as the first instruction in an exception handler. We verify
1351 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001352 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001353 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08001354 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001355 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001356 }
jeffhaobdb76512011-09-07 11:43:16 -07001357 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001358 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1359 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001360 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001361 }
jeffhaobdb76512011-09-07 11:43:16 -07001362 }
1363 break;
1364 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001365 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001366 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001367 const RegType& return_type = GetMethodReturnType();
1368 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001369 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001370 } else {
1371 // Compilers may generate synthetic functions that write byte values into boolean fields.
1372 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001373 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001374 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1375 ((return_type.IsBoolean() || return_type.IsByte() ||
1376 return_type.IsShort() || return_type.IsChar()) &&
1377 src_type.IsInteger()));
1378 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001379 bool success =
1380 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
1381 if (!success) {
1382 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001383 }
jeffhaobdb76512011-09-07 11:43:16 -07001384 }
1385 }
1386 break;
1387 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001388 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001389 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001390 const RegType& return_type = GetMethodReturnType();
1391 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001392 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001393 } else {
1394 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001395 bool success = work_line_->VerifyRegisterType(dec_insn.vA, return_type);
1396 if (!success) {
1397 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001398 }
jeffhaobdb76512011-09-07 11:43:16 -07001399 }
1400 }
1401 break;
1402 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001403 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001404 const RegType& return_type = GetMethodReturnType();
1405 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001406 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001407 } else {
1408 /* return_type is the *expected* return type, not register value */
1409 DCHECK(!return_type.IsZero());
1410 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08001411 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07001412 // Disallow returning uninitialized values and verify that the reference in vAA is an
1413 // instance of the "return_type"
1414 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001415 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001416 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001417 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1418 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001419 }
1420 }
1421 }
1422 break;
1423
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001424 /* could be boolean, int, float, or a null reference */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001425 case Instruction::CONST_4:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001426 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001427 reg_types_.FromCat1Const(static_cast<int32_t>(dec_insn.vB << 28) >> 28, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001428 break;
jeffhaobdb76512011-09-07 11:43:16 -07001429 case Instruction::CONST_16:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001430 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001431 reg_types_.FromCat1Const(static_cast<int16_t>(dec_insn.vB), true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001432 break;
jeffhaobdb76512011-09-07 11:43:16 -07001433 case Instruction::CONST:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001434 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const(dec_insn.vB, true));
jeffhaobdb76512011-09-07 11:43:16 -07001435 break;
1436 case Instruction::CONST_HIGH16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001437 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001438 reg_types_.FromCat1Const(dec_insn.vB << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001439 break;
jeffhaobdb76512011-09-07 11:43:16 -07001440 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001441 case Instruction::CONST_WIDE_16: {
1442 int64_t val = static_cast<int16_t>(dec_insn.vB);
1443 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1444 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1445 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001446 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001447 }
1448 case Instruction::CONST_WIDE_32: {
1449 int64_t val = static_cast<int32_t>(dec_insn.vB);
1450 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1451 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1452 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1453 break;
1454 }
1455 case Instruction::CONST_WIDE: {
1456 int64_t val = dec_insn.vB_wide;
1457 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1458 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1459 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1460 break;
1461 }
1462 case Instruction::CONST_WIDE_HIGH16: {
1463 int64_t val = static_cast<uint64_t>(dec_insn.vB) << 48;
1464 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1465 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1466 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1467 break;
1468 }
jeffhaobdb76512011-09-07 11:43:16 -07001469 case Instruction::CONST_STRING:
1470 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001471 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001472 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001473 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001474 // Get type from instruction if unresolved then we need an access check
1475 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08001476 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001477 // Register holds class, ie its type is class, on error it will hold Conflict.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001478 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogersb4903572012-10-11 11:52:56 -07001479 res_type.IsConflict() ? res_type
1480 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001481 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001482 }
jeffhaobdb76512011-09-07 11:43:16 -07001483 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001484 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001485 break;
1486 case Instruction::MONITOR_EXIT:
1487 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001488 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001489 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001490 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001491 * to the need to handle asynchronous exceptions, a now-deprecated
1492 * feature that Dalvik doesn't support.)
1493 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001494 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001495 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001496 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001497 * structured locking checks are working, the former would have
1498 * failed on the -enter instruction, and the latter is impossible.
1499 *
1500 * This is fortunate, because issue 3221411 prevents us from
1501 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001502 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001503 * some catch blocks (which will show up as "dead" code when
1504 * we skip them here); if we can't, then the code path could be
1505 * "live" so we still need to check it.
1506 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001507 opcode_flags &= ~Instruction::kThrow;
1508 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001509 break;
1510
Ian Rogers28ad40d2011-10-27 15:19:26 -07001511 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001512 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001513 /*
1514 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1515 * could be a "upcast" -- not expected, so we don't try to address it.)
1516 *
1517 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001518 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001519 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001520 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001521 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001522 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001523 if (res_type.IsConflict()) {
1524 DCHECK_NE(failures_.size(), 0U);
1525 if (!is_checkcast) {
1526 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
1527 }
1528 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001529 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001530 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1531 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001532 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001533 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001534 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001535 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001536 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07001537 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001538 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001539 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001540 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001541 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001542 }
jeffhaobdb76512011-09-07 11:43:16 -07001543 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001544 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001545 }
1546 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001547 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001548 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001549 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001550 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001551 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001552 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001553 }
1554 }
1555 break;
1556 }
1557 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001558 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001559 if (res_type.IsConflict()) {
1560 DCHECK_NE(failures_.size(), 0U);
1561 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001562 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001563 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1564 // can't create an instance of an interface or abstract class */
1565 if (!res_type.IsInstantiableTypes()) {
1566 Fail(VERIFY_ERROR_INSTANTIATION)
1567 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001568 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001569 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001570 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1571 // Any registers holding previous allocations from this address that have not yet been
1572 // initialized must be marked invalid.
1573 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1574 // add the new uninitialized reference to the register state
1575 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001576 break;
1577 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001578 case Instruction::NEW_ARRAY:
1579 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001580 break;
1581 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08001582 VerifyNewArray(dec_insn, true, false);
1583 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001584 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001585 case Instruction::FILLED_NEW_ARRAY_RANGE:
1586 VerifyNewArray(dec_insn, true, true);
1587 just_set_result = true; // Filled new array range sets result register
1588 break;
jeffhaobdb76512011-09-07 11:43:16 -07001589 case Instruction::CMPL_FLOAT:
1590 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001591 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001592 break;
1593 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001594 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001595 break;
1596 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001597 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001598 break;
1599 case Instruction::CMPL_DOUBLE:
1600 case Instruction::CMPG_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001601 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vB, reg_types_.DoubleLo(),
1602 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001603 break;
1604 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001605 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vC, reg_types_.DoubleLo(),
1606 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001607 break;
1608 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001609 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001610 break;
1611 case Instruction::CMP_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001612 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vB, reg_types_.LongLo(),
1613 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001614 break;
1615 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001616 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vC, reg_types_.LongLo(),
1617 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001618 break;
1619 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001620 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001621 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001622 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001623 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersb4903572012-10-11 11:52:56 -07001624 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001625 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001626 }
1627 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001628 }
jeffhaobdb76512011-09-07 11:43:16 -07001629 case Instruction::GOTO:
1630 case Instruction::GOTO_16:
1631 case Instruction::GOTO_32:
1632 /* no effect on or use of registers */
1633 break;
1634
1635 case Instruction::PACKED_SWITCH:
1636 case Instruction::SPARSE_SWITCH:
1637 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001638 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001639 break;
1640
Ian Rogersd81871c2011-10-03 13:57:23 -07001641 case Instruction::FILL_ARRAY_DATA: {
1642 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001643 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08001644 /* array_type can be null if the reg type is Zero */
1645 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001646 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001647 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001648 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001649 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1650 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001651 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001652 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1653 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001654 } else {
jeffhao457cc512012-02-02 16:55:13 -08001655 // Now verify if the element width in the table matches the element width declared in
1656 // the array
1657 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1658 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001659 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001660 } else {
1661 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1662 // Since we don't compress the data in Dex, expect to see equal width of data stored
1663 // in the table and expected from the array class.
1664 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001665 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1666 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001667 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001668 }
1669 }
jeffhaobdb76512011-09-07 11:43:16 -07001670 }
1671 }
1672 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001673 }
jeffhaobdb76512011-09-07 11:43:16 -07001674 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001675 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001676 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1677 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001678 bool mismatch = false;
1679 if (reg_type1.IsZero()) { // zero then integral or reference expected
1680 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1681 } else if (reg_type1.IsReferenceTypes()) { // both references?
1682 mismatch = !reg_type2.IsReferenceTypes();
1683 } else { // both integral?
1684 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1685 }
1686 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001687 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1688 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001689 }
1690 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001691 }
jeffhaobdb76512011-09-07 11:43:16 -07001692 case Instruction::IF_LT:
1693 case Instruction::IF_GE:
1694 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001695 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001696 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1697 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001698 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001699 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1700 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001701 }
1702 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001703 }
jeffhaobdb76512011-09-07 11:43:16 -07001704 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001705 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001706 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001707 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001708 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001709 }
jeffhaobdb76512011-09-07 11:43:16 -07001710 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001711 }
jeffhaobdb76512011-09-07 11:43:16 -07001712 case Instruction::IF_LTZ:
1713 case Instruction::IF_GEZ:
1714 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001715 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001716 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001717 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1719 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001720 }
jeffhaobdb76512011-09-07 11:43:16 -07001721 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001722 }
jeffhaobdb76512011-09-07 11:43:16 -07001723 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001724 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
1725 break;
jeffhaobdb76512011-09-07 11:43:16 -07001726 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001727 VerifyAGet(dec_insn, reg_types_.Byte(), true);
1728 break;
jeffhaobdb76512011-09-07 11:43:16 -07001729 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07001730 VerifyAGet(dec_insn, reg_types_.Char(), true);
1731 break;
jeffhaobdb76512011-09-07 11:43:16 -07001732 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001733 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001734 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001735 case Instruction::AGET:
1736 VerifyAGet(dec_insn, reg_types_.Integer(), true);
1737 break;
jeffhaobdb76512011-09-07 11:43:16 -07001738 case Instruction::AGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001739 VerifyAGet(dec_insn, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001740 break;
1741 case Instruction::AGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001742 VerifyAGet(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001743 break;
1744
Ian Rogersd81871c2011-10-03 13:57:23 -07001745 case Instruction::APUT_BOOLEAN:
1746 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
1747 break;
1748 case Instruction::APUT_BYTE:
1749 VerifyAPut(dec_insn, reg_types_.Byte(), true);
1750 break;
1751 case Instruction::APUT_CHAR:
1752 VerifyAPut(dec_insn, reg_types_.Char(), true);
1753 break;
1754 case Instruction::APUT_SHORT:
1755 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001756 break;
1757 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001758 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001759 break;
1760 case Instruction::APUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001761 VerifyAPut(dec_insn, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001762 break;
1763 case Instruction::APUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001764 VerifyAPut(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001765 break;
1766
jeffhaobdb76512011-09-07 11:43:16 -07001767 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001768 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001769 break;
jeffhaobdb76512011-09-07 11:43:16 -07001770 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001771 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001772 break;
jeffhaobdb76512011-09-07 11:43:16 -07001773 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001774 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001775 break;
jeffhaobdb76512011-09-07 11:43:16 -07001776 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001777 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001778 break;
1779 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001780 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001781 break;
1782 case Instruction::IGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001783 VerifyISGet(dec_insn, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001784 break;
1785 case Instruction::IGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001786 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 break;
jeffhaobdb76512011-09-07 11:43:16 -07001788
Ian Rogersd81871c2011-10-03 13:57:23 -07001789 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001790 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001791 break;
1792 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001793 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001794 break;
1795 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001796 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001797 break;
1798 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001799 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001800 break;
1801 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001802 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001803 break;
1804 case Instruction::IPUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001805 VerifyISPut(dec_insn, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001806 break;
jeffhaobdb76512011-09-07 11:43:16 -07001807 case Instruction::IPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001808 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001809 break;
1810
jeffhaobdb76512011-09-07 11:43:16 -07001811 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001812 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001813 break;
jeffhaobdb76512011-09-07 11:43:16 -07001814 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001815 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001816 break;
jeffhaobdb76512011-09-07 11:43:16 -07001817 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001818 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001819 break;
jeffhaobdb76512011-09-07 11:43:16 -07001820 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001821 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001822 break;
1823 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001824 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001825 break;
1826 case Instruction::SGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001827 VerifyISGet(dec_insn, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001828 break;
1829 case Instruction::SGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001830 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001831 break;
1832
1833 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001834 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001835 break;
1836 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001837 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001838 break;
1839 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001840 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001841 break;
1842 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001843 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001844 break;
1845 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001846 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001847 break;
1848 case Instruction::SPUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001849 VerifyISPut(dec_insn, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001850 break;
1851 case Instruction::SPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001852 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001853 break;
1854
1855 case Instruction::INVOKE_VIRTUAL:
1856 case Instruction::INVOKE_VIRTUAL_RANGE:
1857 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001858 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001859 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
1860 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
1861 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
1862 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001863 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL,
1864 is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001865 const char* descriptor;
1866 if (called_method == NULL) {
1867 uint32_t method_idx = dec_insn.vB;
1868 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1869 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1870 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1871 } else {
1872 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07001873 }
Ian Rogersb4903572012-10-11 11:52:56 -07001874 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001875 if (!return_type.IsLowHalf()) {
1876 work_line_->SetResultRegisterType(return_type);
1877 } else {
1878 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
1879 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001880 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001881 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001882 }
jeffhaobdb76512011-09-07 11:43:16 -07001883 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001884 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001885 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001886 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT,
1887 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07001888 const char* return_type_descriptor;
1889 bool is_constructor;
1890 if (called_method == NULL) {
1891 uint32_t method_idx = dec_insn.vB;
1892 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1893 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
1894 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1895 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1896 } else {
1897 is_constructor = called_method->IsConstructor();
1898 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
1899 }
1900 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07001901 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001902 * Some additional checks when calling a constructor. We know from the invocation arg check
1903 * that the "this" argument is an instance of called_method->klass. Now we further restrict
1904 * that to require that called_method->klass is the same as this->klass or this->super,
1905 * allowing the latter only if the "this" argument is the same as the "this" argument to
1906 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07001907 */
jeffhaob57e9522012-04-26 18:08:21 -07001908 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1909 if (this_type.IsConflict()) // failure.
1910 break;
jeffhaobdb76512011-09-07 11:43:16 -07001911
jeffhaob57e9522012-04-26 18:08:21 -07001912 /* no null refs allowed (?) */
1913 if (this_type.IsZero()) {
1914 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
1915 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07001916 }
jeffhaob57e9522012-04-26 18:08:21 -07001917
1918 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07001919 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
1920 // TODO: re-enable constructor type verification
1921 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07001922 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07001923 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
1924 // break;
1925 // }
jeffhaob57e9522012-04-26 18:08:21 -07001926
1927 /* arg must be an uninitialized reference */
1928 if (!this_type.IsUninitializedTypes()) {
1929 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
1930 << this_type;
1931 break;
1932 }
1933
1934 /*
1935 * Replace the uninitialized reference with an initialized one. We need to do this for all
1936 * registers that have the same object instance in them, not just the "this" register.
1937 */
1938 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001939 }
Ian Rogersb4903572012-10-11 11:52:56 -07001940 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
1941 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001942 if (!return_type.IsLowHalf()) {
1943 work_line_->SetResultRegisterType(return_type);
1944 } else {
1945 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
1946 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001947 just_set_result = true;
1948 break;
1949 }
1950 case Instruction::INVOKE_STATIC:
1951 case Instruction::INVOKE_STATIC_RANGE: {
1952 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001953 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001954 const char* descriptor;
1955 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001956 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001957 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1958 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07001959 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001960 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001961 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001962 }
Ian Rogersb4903572012-10-11 11:52:56 -07001963 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001964 if (!return_type.IsLowHalf()) {
1965 work_line_->SetResultRegisterType(return_type);
1966 } else {
1967 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
1968 }
jeffhaobdb76512011-09-07 11:43:16 -07001969 just_set_result = true;
1970 }
1971 break;
jeffhaobdb76512011-09-07 11:43:16 -07001972 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001973 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001974 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001975 mirror::AbstractMethod* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001976 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001977 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001978 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
1979 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
1980 << PrettyMethod(abs_method) << "'";
1981 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001982 }
Ian Rogers0d604842012-04-16 14:50:24 -07001983 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001984 /* Get the type of the "this" arg, which should either be a sub-interface of called
1985 * interface or Object (see comments in RegType::JoinClass).
1986 */
1987 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1988 if (this_type.IsZero()) {
1989 /* null pointer always passes (and always fails at runtime) */
1990 } else {
1991 if (this_type.IsUninitializedTypes()) {
1992 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
1993 << this_type;
1994 break;
1995 }
1996 // In the past we have tried to assert that "called_interface" is assignable
1997 // from "this_type.GetClass()", however, as we do an imprecise Join
1998 // (RegType::JoinClass) we don't have full information on what interfaces are
1999 // implemented by "this_type". For example, two classes may implement the same
2000 // interfaces and have a common parent that doesn't implement the interface. The
2001 // join will set "this_type" to the parent class and a test that this implements
2002 // the interface will incorrectly fail.
2003 }
2004 /*
2005 * We don't have an object instance, so we can't find the concrete method. However, all of
2006 * the type information is in the abstract method, so we're good.
2007 */
2008 const char* descriptor;
2009 if (abs_method == NULL) {
2010 uint32_t method_idx = dec_insn.vB;
2011 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2012 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2013 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2014 } else {
2015 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2016 }
Ian Rogersb4903572012-10-11 11:52:56 -07002017 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002018 if (!return_type.IsLowHalf()) {
2019 work_line_->SetResultRegisterType(return_type);
2020 } else {
2021 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2022 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002023 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002024 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002025 }
jeffhaobdb76512011-09-07 11:43:16 -07002026 case Instruction::NEG_INT:
2027 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002028 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002029 break;
2030 case Instruction::NEG_LONG:
2031 case Instruction::NOT_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002032 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2033 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002034 break;
2035 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002036 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002037 break;
2038 case Instruction::NEG_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002039 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2040 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002041 break;
2042 case Instruction::INT_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002043 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2044 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002045 break;
2046 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002047 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002048 break;
2049 case Instruction::INT_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002050 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2051 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002052 break;
2053 case Instruction::LONG_TO_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002054 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Integer(),
2055 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002056 break;
2057 case Instruction::LONG_TO_FLOAT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002058 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Float(),
2059 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002060 break;
2061 case Instruction::LONG_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002062 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2063 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002064 break;
2065 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002066 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002067 break;
2068 case Instruction::FLOAT_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002069 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2070 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002071 break;
2072 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002073 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2074 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002075 break;
2076 case Instruction::DOUBLE_TO_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002077 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Integer(),
2078 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002079 break;
2080 case Instruction::DOUBLE_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002081 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2082 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002083 break;
2084 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002085 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Float(),
2086 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002087 break;
2088 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002089 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002090 break;
2091 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002092 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002093 break;
2094 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002095 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002096 break;
2097
2098 case Instruction::ADD_INT:
2099 case Instruction::SUB_INT:
2100 case Instruction::MUL_INT:
2101 case Instruction::REM_INT:
2102 case Instruction::DIV_INT:
2103 case Instruction::SHL_INT:
2104 case Instruction::SHR_INT:
2105 case Instruction::USHR_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002106 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(),
2107 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002108 break;
2109 case Instruction::AND_INT:
2110 case Instruction::OR_INT:
2111 case Instruction::XOR_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002112 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(),
2113 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002114 break;
2115 case Instruction::ADD_LONG:
2116 case Instruction::SUB_LONG:
2117 case Instruction::MUL_LONG:
2118 case Instruction::DIV_LONG:
2119 case Instruction::REM_LONG:
2120 case Instruction::AND_LONG:
2121 case Instruction::OR_LONG:
2122 case Instruction::XOR_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002123 work_line_->CheckBinaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2124 reg_types_.LongLo(), reg_types_.LongHi(),
2125 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002126 break;
2127 case Instruction::SHL_LONG:
2128 case Instruction::SHR_LONG:
2129 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002130 /* shift distance is Int, making these different from other binary operations */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002131 work_line_->CheckBinaryOpWideShift(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2132 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002133 break;
2134 case Instruction::ADD_FLOAT:
2135 case Instruction::SUB_FLOAT:
2136 case Instruction::MUL_FLOAT:
2137 case Instruction::DIV_FLOAT:
2138 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002139 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002140 break;
2141 case Instruction::ADD_DOUBLE:
2142 case Instruction::SUB_DOUBLE:
2143 case Instruction::MUL_DOUBLE:
2144 case Instruction::DIV_DOUBLE:
2145 case Instruction::REM_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002146 work_line_->CheckBinaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2147 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2148 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002149 break;
2150 case Instruction::ADD_INT_2ADDR:
2151 case Instruction::SUB_INT_2ADDR:
2152 case Instruction::MUL_INT_2ADDR:
2153 case Instruction::REM_INT_2ADDR:
2154 case Instruction::SHL_INT_2ADDR:
2155 case Instruction::SHR_INT_2ADDR:
2156 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002157 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002158 break;
2159 case Instruction::AND_INT_2ADDR:
2160 case Instruction::OR_INT_2ADDR:
2161 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002163 break;
2164 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002165 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002166 break;
2167 case Instruction::ADD_LONG_2ADDR:
2168 case Instruction::SUB_LONG_2ADDR:
2169 case Instruction::MUL_LONG_2ADDR:
2170 case Instruction::DIV_LONG_2ADDR:
2171 case Instruction::REM_LONG_2ADDR:
2172 case Instruction::AND_LONG_2ADDR:
2173 case Instruction::OR_LONG_2ADDR:
2174 case Instruction::XOR_LONG_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002175 work_line_->CheckBinaryOp2addrWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2176 reg_types_.LongLo(), reg_types_.LongHi(),
2177 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002178 break;
2179 case Instruction::SHL_LONG_2ADDR:
2180 case Instruction::SHR_LONG_2ADDR:
2181 case Instruction::USHR_LONG_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002182 work_line_->CheckBinaryOp2addrWideShift(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2183 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002184 break;
2185 case Instruction::ADD_FLOAT_2ADDR:
2186 case Instruction::SUB_FLOAT_2ADDR:
2187 case Instruction::MUL_FLOAT_2ADDR:
2188 case Instruction::DIV_FLOAT_2ADDR:
2189 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002190 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002191 break;
2192 case Instruction::ADD_DOUBLE_2ADDR:
2193 case Instruction::SUB_DOUBLE_2ADDR:
2194 case Instruction::MUL_DOUBLE_2ADDR:
2195 case Instruction::DIV_DOUBLE_2ADDR:
2196 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002197 work_line_->CheckBinaryOp2addrWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2198 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2199 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002200 break;
2201 case Instruction::ADD_INT_LIT16:
2202 case Instruction::RSUB_INT:
2203 case Instruction::MUL_INT_LIT16:
2204 case Instruction::DIV_INT_LIT16:
2205 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002206 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002207 break;
2208 case Instruction::AND_INT_LIT16:
2209 case Instruction::OR_INT_LIT16:
2210 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002211 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002212 break;
2213 case Instruction::ADD_INT_LIT8:
2214 case Instruction::RSUB_INT_LIT8:
2215 case Instruction::MUL_INT_LIT8:
2216 case Instruction::DIV_INT_LIT8:
2217 case Instruction::REM_INT_LIT8:
2218 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002219 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002220 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002221 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002222 break;
2223 case Instruction::AND_INT_LIT8:
2224 case Instruction::OR_INT_LIT8:
2225 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002226 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002227 break;
2228
Ian Rogersd81871c2011-10-03 13:57:23 -07002229 /* These should never appear during verification. */
jeffhao9a4f0032012-08-30 16:17:40 -07002230 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002231 case Instruction::UNUSED_EE:
2232 case Instruction::UNUSED_EF:
2233 case Instruction::UNUSED_F2:
2234 case Instruction::UNUSED_F3:
2235 case Instruction::UNUSED_F4:
2236 case Instruction::UNUSED_F5:
2237 case Instruction::UNUSED_F6:
2238 case Instruction::UNUSED_F7:
2239 case Instruction::UNUSED_F8:
2240 case Instruction::UNUSED_F9:
2241 case Instruction::UNUSED_FA:
2242 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002243 case Instruction::UNUSED_F0:
2244 case Instruction::UNUSED_F1:
2245 case Instruction::UNUSED_E3:
2246 case Instruction::UNUSED_E8:
2247 case Instruction::UNUSED_E7:
2248 case Instruction::UNUSED_E4:
2249 case Instruction::UNUSED_E9:
2250 case Instruction::UNUSED_FC:
2251 case Instruction::UNUSED_E5:
2252 case Instruction::UNUSED_EA:
2253 case Instruction::UNUSED_FD:
2254 case Instruction::UNUSED_E6:
2255 case Instruction::UNUSED_EB:
2256 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002257 case Instruction::UNUSED_3E:
2258 case Instruction::UNUSED_3F:
2259 case Instruction::UNUSED_40:
2260 case Instruction::UNUSED_41:
2261 case Instruction::UNUSED_42:
2262 case Instruction::UNUSED_43:
2263 case Instruction::UNUSED_73:
2264 case Instruction::UNUSED_79:
2265 case Instruction::UNUSED_7A:
2266 case Instruction::UNUSED_EC:
2267 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002268 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002269 break;
2270
2271 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002272 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002273 * complain if an instruction is missing (which is desirable).
2274 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002275 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002276
Ian Rogersad0b3a32012-04-16 14:50:24 -07002277 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002278 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002279 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002280 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002281 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002282 /* immediate failure, reject class */
2283 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2284 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002285 } else if (have_pending_runtime_throw_failure_) {
2286 /* slow path will throw, mark following code as unreachable */
2287 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002288 }
jeffhaobdb76512011-09-07 11:43:16 -07002289 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002290 * If we didn't just set the result register, clear it out. This ensures that you can only use
2291 * "move-result" immediately after the result is set. (We could check this statically, but it's
2292 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002293 */
2294 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002295 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002296 }
2297
jeffhaoa0a764a2011-09-16 10:43:38 -07002298 /* Handle "continue". Tag the next consecutive instruction. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002299 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers776ac1f2012-04-13 23:36:36 -07002300 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
Ian Rogersd81871c2011-10-03 13:57:23 -07002301 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
jeffhaod5347e02012-03-22 17:25:05 -07002302 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002303 return false;
2304 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002305 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2306 // next instruction isn't one.
jeffhaod5347e02012-03-22 17:25:05 -07002307 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002308 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002309 }
2310 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2311 if (next_line != NULL) {
2312 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2313 // needed.
2314 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002315 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002316 }
jeffhaobdb76512011-09-07 11:43:16 -07002317 } else {
2318 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002319 * We're not recording register data for the next instruction, so we don't know what the prior
2320 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002321 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002322 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002323 }
2324 }
2325
2326 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002327 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002328 *
2329 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002330 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002331 * somebody could get a reference field, check it for zero, and if the
2332 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002333 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002334 * that, and will reject the code.
2335 *
2336 * TODO: avoid re-fetching the branch target
2337 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002338 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002339 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002340 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002341 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002342 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002343 return false;
2344 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002345 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002346 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002347 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002348 }
jeffhaobdb76512011-09-07 11:43:16 -07002349 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002350 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002351 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002352 }
jeffhaobdb76512011-09-07 11:43:16 -07002353 }
2354
2355 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002356 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002357 *
2358 * We've already verified that the table is structurally sound, so we
2359 * just need to walk through and tag the targets.
2360 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002361 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002362 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2363 const uint16_t* switch_insns = insns + offset_to_switch;
2364 int switch_count = switch_insns[1];
2365 int offset_to_targets, targ;
2366
2367 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2368 /* 0 = sig, 1 = count, 2/3 = first key */
2369 offset_to_targets = 4;
2370 } else {
2371 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002372 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002373 offset_to_targets = 2 + 2 * switch_count;
2374 }
2375
2376 /* verify each switch target */
2377 for (targ = 0; targ < switch_count; targ++) {
2378 int offset;
2379 uint32_t abs_offset;
2380
2381 /* offsets are 32-bit, and only partly endian-swapped */
2382 offset = switch_insns[offset_to_targets + targ * 2] |
2383 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002384 abs_offset = work_insn_idx_ + offset;
2385 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002386 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002387 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002388 }
2389 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002390 return false;
2391 }
2392 }
2393
2394 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002395 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2396 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002397 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002398 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002399 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002400 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002401
Ian Rogers0571d352011-11-03 19:51:38 -07002402 for (; iterator.HasNext(); iterator.Next()) {
2403 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002404 within_catch_all = true;
2405 }
jeffhaobdb76512011-09-07 11:43:16 -07002406 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002407 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2408 * "work_regs", because at runtime the exception will be thrown before the instruction
2409 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002410 */
Ian Rogers0571d352011-11-03 19:51:38 -07002411 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002412 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002413 }
jeffhaobdb76512011-09-07 11:43:16 -07002414 }
2415
2416 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002417 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2418 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002419 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002420 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002421 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002422 * The state in work_line reflects the post-execution state. If the current instruction is a
2423 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002424 * it will do so before grabbing the lock).
2425 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002426 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002427 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002428 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002429 return false;
2430 }
2431 }
2432 }
2433
jeffhaod1f0fde2011-09-08 17:25:33 -07002434 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002435 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002436 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002437 return false;
2438 }
jeffhaobdb76512011-09-07 11:43:16 -07002439 }
2440
2441 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002442 * Update start_guess. Advance to the next instruction of that's
2443 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002444 * neither of those exists we're in a return or throw; leave start_guess
2445 * alone and let the caller sort it out.
2446 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002447 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002448 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002449 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002450 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002451 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002452 }
2453
Ian Rogersd81871c2011-10-03 13:57:23 -07002454 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2455 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002456
2457 return true;
2458}
2459
Ian Rogers776ac1f2012-04-13 23:36:36 -07002460const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002461 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002462 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002463 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002464 const RegType& result =
Ian Rogersb4903572012-10-11 11:52:56 -07002465 klass != NULL ? reg_types_.FromClass(klass, klass->IsFinal())
2466 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002467 if (result.IsConflict()) {
2468 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2469 << "' in " << referrer;
2470 return result;
2471 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002472 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002473 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002474 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002475 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002476 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002477 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002478 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002479 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002480 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002481 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002482}
2483
Ian Rogers776ac1f2012-04-13 23:36:36 -07002484const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002485 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002486 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002487 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002488 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2489 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002490 CatchHandlerIterator iterator(handlers_ptr);
2491 for (; iterator.HasNext(); iterator.Next()) {
2492 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2493 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002494 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002495 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002496 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002497 if (common_super == NULL) {
2498 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2499 // as that is caught at runtime
2500 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002501 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002502 // We don't know enough about the type and the common path merge will result in
2503 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002504 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002505 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002506 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002507 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002508 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002509 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002510 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002511 }
2512 }
2513 }
2514 }
Ian Rogers0571d352011-11-03 19:51:38 -07002515 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002516 }
2517 }
2518 if (common_super == NULL) {
2519 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002520 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002521 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002522 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002523 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002524}
2525
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002526mirror::AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
2527 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002528 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002529 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002530 if (klass_type.IsConflict()) {
2531 std::string append(" in attempt to access method ");
2532 append += dex_file_->GetMethodName(method_id);
2533 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002534 return NULL;
2535 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002536 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002537 return NULL; // Can't resolve Class so no more to do here
2538 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002539 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002540 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002541 mirror::AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002542 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002543 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002544 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002545
2546 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002547 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002548 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002549 res_method = klass->FindInterfaceMethod(name, signature);
2550 } else {
2551 res_method = klass->FindVirtualMethod(name, signature);
2552 }
2553 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002554 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002555 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002556 // If a virtual or interface method wasn't found with the expected type, look in
2557 // the direct methods. This can happen when the wrong invoke type is used or when
2558 // a class has changed, and will be flagged as an error in later checks.
2559 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2560 res_method = klass->FindDirectMethod(name, signature);
2561 }
2562 if (res_method == NULL) {
2563 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2564 << PrettyDescriptor(klass) << "." << name
2565 << " " << signature;
2566 return NULL;
2567 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002568 }
2569 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002570 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2571 // enforce them here.
2572 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002573 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2574 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002575 return NULL;
2576 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002577 // Disallow any calls to class initializers.
2578 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002579 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2580 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002581 return NULL;
2582 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002583 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002584 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002585 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002586 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002587 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002588 }
jeffhaode0d9c92012-02-27 13:58:13 -08002589 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2590 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002591 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2592 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002593 return NULL;
2594 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002595 // Check that interface methods match interface classes.
2596 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2597 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2598 << " is in an interface class " << PrettyClass(klass);
2599 return NULL;
2600 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2601 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2602 << " is in a non-interface class " << PrettyClass(klass);
2603 return NULL;
2604 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002605 // See if the method type implied by the invoke instruction matches the access flags for the
2606 // target method.
2607 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2608 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2609 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2610 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002611 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2612 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002613 return NULL;
2614 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002615 return res_method;
2616}
2617
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002618mirror::AbstractMethod* MethodVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
2619 MethodType method_type, bool is_range,
2620 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002621 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2622 // we're making.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002623 mirror::AbstractMethod* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002624 if (res_method == NULL) { // error or class is unresolved
2625 return NULL;
2626 }
2627
Ian Rogersd81871c2011-10-03 13:57:23 -07002628 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2629 // has a vtable entry for the target method.
2630 if (is_super) {
2631 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002632 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002633 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002634 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002635 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002636 << " to super " << PrettyMethod(res_method);
2637 return NULL;
2638 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002639 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002640 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002641 MethodHelper mh(res_method);
2642 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002643 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002644 << " to super " << super
2645 << "." << mh.GetName()
2646 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002647 return NULL;
2648 }
2649 }
2650 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2651 // match the call to the signature. Also, we might might be calling through an abstract method
2652 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08002653 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002654 /* caught by static verifier */
2655 DCHECK(is_range || expected_args <= 5);
2656 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002657 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002658 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2659 return NULL;
2660 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002661
jeffhaobdb76512011-09-07 11:43:16 -07002662 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002663 * Check the "this" argument, which must be an instance of the class that declared the method.
2664 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2665 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002666 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002667 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002668 if (!res_method->IsStatic()) {
2669 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002670 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002671 return NULL;
2672 }
2673 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002674 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002675 return NULL;
2676 }
2677 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002678 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogersb4903572012-10-11 11:52:56 -07002679 const RegType& res_method_class = reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogers9074b992011-10-26 17:41:55 -07002680 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002681 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002682 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002683 return NULL;
2684 }
2685 }
2686 actual_args++;
2687 }
2688 /*
2689 * Process the target method's signature. This signature may or may not
2690 * have been verified, so we can't assume it's properly formed.
2691 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002692 MethodHelper mh(res_method);
2693 const DexFile::TypeList* params = mh.GetParameterTypeList();
2694 size_t params_size = params == NULL ? 0 : params->Size();
2695 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002696 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002697 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002698 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2699 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002700 return NULL;
2701 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002702 const char* descriptor =
2703 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2704 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002705 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002706 << " missing signature component";
2707 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002708 }
Ian Rogersb4903572012-10-11 11:52:56 -07002709 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002710 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002711 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002712 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002713 }
2714 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2715 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002716 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002717 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002718 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002719 return NULL;
2720 } else {
2721 return res_method;
2722 }
2723}
2724
Ian Rogers776ac1f2012-04-13 23:36:36 -07002725void MethodVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08002726 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002727 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002728 if (res_type.IsConflict()) { // bad class
2729 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002730 } else {
2731 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2732 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002734 } else if (!is_filled) {
2735 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002736 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002737 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002738 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002739 } else {
2740 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2741 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002742 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002743 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002744 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002745 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002746 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002747 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002748 return;
2749 }
2750 }
2751 // filled-array result goes into "result" register
2752 work_line_->SetResultRegisterType(res_type);
2753 }
2754 }
2755}
2756
Ian Rogers776ac1f2012-04-13 23:36:36 -07002757void MethodVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002758 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002759 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002760 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002761 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002762 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002763 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002764 if (array_type.IsZero()) {
2765 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2766 // instruction type. TODO: have a proper notion of bottom here.
2767 if (!is_primitive || insn_type.IsCategory1Types()) {
2768 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08002769 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002770 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002771 // Category 2
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002772 work_line_->SetRegisterTypeWide(dec_insn.vA, reg_types_.FromCat2ConstLo(0, false),
2773 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08002774 }
jeffhaofc3144e2012-02-01 17:21:15 -08002775 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002776 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002777 } else {
2778 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002779 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002780 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002781 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002782 << " source for aget-object";
2783 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002784 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002785 << " source for category 1 aget";
2786 } else if (is_primitive && !insn_type.Equals(component_type) &&
2787 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002788 (insn_type.IsLong() && component_type.IsDouble()))) {
2789 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
2790 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002791 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002792 // Use knowledge of the field type which is stronger than the type inferred from the
2793 // instruction, which can't differentiate object types and ints from floats, longs from
2794 // doubles.
2795 if (!component_type.IsLowHalf()) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002796 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002797 } else {
2798 work_line_->SetRegisterTypeWide(dec_insn.vA, component_type,
2799 component_type.HighHalf(&reg_types_));
2800 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002801 }
2802 }
2803 }
2804}
2805
Ian Rogers776ac1f2012-04-13 23:36:36 -07002806void MethodVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002807 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002808 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002809 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002810 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002811 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002812 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002813 if (array_type.IsZero()) {
2814 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2815 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002816 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002817 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002818 } else {
2819 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002820 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002821 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002822 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002823 << " source for aput-object";
2824 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002825 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002826 << " source for category 1 aput";
2827 } else if (is_primitive && !insn_type.Equals(component_type) &&
2828 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2829 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002830 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002831 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002832 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002833 // The instruction agrees with the type of array, confirm the value to be stored does too
2834 // Note: we use the instruction type (rather than the component type) for aput-object as
2835 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08002836 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002837 }
2838 }
2839 }
2840}
2841
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002842mirror::Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002843 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2844 // Check access to class
2845 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002846 if (klass_type.IsConflict()) { // bad class
2847 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
2848 field_idx, dex_file_->GetFieldName(field_id),
2849 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002850 return NULL;
2851 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002852 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002853 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08002854 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002855 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07002856 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002857 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002858 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
2859 << dex_file_->GetFieldName(field_id) << ") in "
2860 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002861 DCHECK(Thread::Current()->IsExceptionPending());
2862 Thread::Current()->ClearException();
2863 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002864 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2865 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002866 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002867 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002868 return NULL;
2869 } else if (!field->IsStatic()) {
2870 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
2871 return NULL;
2872 } else {
2873 return field;
2874 }
2875}
2876
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002877mirror::Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002878 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2879 // Check access to class
2880 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002881 if (klass_type.IsConflict()) {
2882 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
2883 field_idx, dex_file_->GetFieldName(field_id),
2884 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002885 return NULL;
2886 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002887 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002888 return NULL; // Can't resolve Class so no more to do here
2889 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002890 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07002891 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002892 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002893 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
2894 << dex_file_->GetFieldName(field_id) << ") in "
2895 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002896 DCHECK(Thread::Current()->IsExceptionPending());
2897 Thread::Current()->ClearException();
2898 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002899 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2900 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002901 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002902 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002903 return NULL;
2904 } else if (field->IsStatic()) {
2905 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
2906 << " to not be static";
2907 return NULL;
2908 } else if (obj_type.IsZero()) {
2909 // Cannot infer and check type, however, access will cause null pointer exception
2910 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07002911 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002912 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersb4903572012-10-11 11:52:56 -07002913 const RegType& field_klass = reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002914 if (obj_type.IsUninitializedTypes() &&
2915 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
2916 !field_klass.Equals(GetDeclaringClass()))) {
2917 // Field accesses through uninitialized references are only allowable for constructors where
2918 // the field is declared in this class
2919 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
2920 << " of a not fully initialized object within the context of "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002921 << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002922 return NULL;
2923 } else if (!field_klass.IsAssignableFrom(obj_type)) {
2924 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
2925 // of C1. For resolution to occur the declared class of the field must be compatible with
2926 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
2927 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
2928 << " from object of type " << obj_type;
2929 return NULL;
2930 } else {
2931 return field;
2932 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002933 }
2934}
2935
Ian Rogers776ac1f2012-04-13 23:36:36 -07002936void MethodVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002937 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002938 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002939 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002940 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002941 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002942 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002943 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07002944 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002945 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002946 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002947 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002948 if (field != NULL) {
2949 descriptor = FieldHelper(field).GetTypeDescriptor();
2950 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07002951 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002952 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2953 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2954 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07002955 }
Ian Rogersb4903572012-10-11 11:52:56 -07002956 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002957 if (is_primitive) {
2958 if (field_type.Equals(insn_type) ||
2959 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
2960 (field_type.IsDouble() && insn_type.IsLongTypes())) {
2961 // expected that read is of the correct primitive type or that int reads are reading
2962 // floats or long reads are reading doubles
2963 } else {
2964 // This is a global failure rather than a class change failure as the instructions and
2965 // the descriptors for the type should have been consistent within the same file at
2966 // compile time
2967 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
2968 << " to be of type '" << insn_type
2969 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002970 return;
2971 }
2972 } else {
2973 if (!insn_type.IsAssignableFrom(field_type)) {
2974 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
2975 << " to be compatible with type '" << insn_type
2976 << "' but found type '" << field_type
2977 << "' in get-object";
2978 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Conflict());
2979 return;
2980 }
2981 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002982 if (!field_type.IsLowHalf()) {
2983 work_line_->SetRegisterType(dec_insn.vA, field_type);
2984 } else {
2985 work_line_->SetRegisterTypeWide(dec_insn.vA, field_type, field_type.HighHalf(&reg_types_));
2986 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002987}
2988
Ian Rogers776ac1f2012-04-13 23:36:36 -07002989void MethodVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002990 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002991 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002992 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002993 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07002994 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002995 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002996 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07002997 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002998 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002999 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003000 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003001 if (field != NULL) {
3002 descriptor = FieldHelper(field).GetTypeDescriptor();
3003 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003004 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003005 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3006 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3007 loader = class_loader_;
3008 }
Ian Rogersb4903572012-10-11 11:52:56 -07003009 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003010 if (field != NULL) {
3011 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3012 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3013 << " from other class " << GetDeclaringClass();
3014 return;
3015 }
3016 }
3017 if (is_primitive) {
3018 // Primitive field assignability rules are weaker than regular assignability rules
3019 bool instruction_compatible;
3020 bool value_compatible;
3021 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
3022 if (field_type.IsIntegralTypes()) {
3023 instruction_compatible = insn_type.IsIntegralTypes();
3024 value_compatible = value_type.IsIntegralTypes();
3025 } else if (field_type.IsFloat()) {
3026 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3027 value_compatible = value_type.IsFloatTypes();
3028 } else if (field_type.IsLong()) {
3029 instruction_compatible = insn_type.IsLong();
3030 value_compatible = value_type.IsLongTypes();
3031 } else if (field_type.IsDouble()) {
3032 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3033 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003034 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003035 instruction_compatible = false; // reference field with primitive store
3036 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003037 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003038 if (!instruction_compatible) {
3039 // This is a global failure rather than a class change failure as the instructions and
3040 // the descriptors for the type should have been consistent within the same file at
3041 // compile time
3042 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3043 << " to be of type '" << insn_type
3044 << "' but found type '" << field_type
3045 << "' in put";
3046 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003047 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003048 if (!value_compatible) {
3049 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
3050 << " of type " << value_type
3051 << " but expected " << field_type
3052 << " for store to " << PrettyField(field) << " in put";
3053 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003054 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003055 } else {
3056 if (!insn_type.IsAssignableFrom(field_type)) {
3057 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3058 << " to be compatible with type '" << insn_type
3059 << "' but found type '" << field_type
3060 << "' in put-object";
3061 return;
3062 }
3063 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003064 }
3065}
3066
Ian Rogers776ac1f2012-04-13 23:36:36 -07003067bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003068 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003069 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003070 return false;
3071 }
3072 return true;
3073}
3074
Ian Rogers776ac1f2012-04-13 23:36:36 -07003075bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003076 bool changed = true;
3077 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3078 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003079 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003080 * We haven't processed this instruction before, and we haven't touched the registers here, so
3081 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3082 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003083 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003084 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003085 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003086 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3087 if (gDebugVerify) {
3088 copy->CopyFromLine(target_line);
3089 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003090 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003091 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003092 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003093 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003094 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003095 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003096 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3097 << *copy.get() << " MERGE\n"
3098 << *merge_line << " ==\n"
3099 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003100 }
3101 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003102 if (changed) {
3103 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003104 }
3105 return true;
3106}
3107
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003108InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003109 return &insn_flags_[work_insn_idx_];
3110}
3111
Ian Rogersad0b3a32012-04-16 14:50:24 -07003112const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003113 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003114 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3115 uint16_t return_type_idx = proto_id.return_type_idx_;
3116 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003117 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003118}
3119
3120const RegType& MethodVerifier::GetDeclaringClass() {
3121 if (foo_method_ != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003122 mirror::Class* klass = foo_method_->GetDeclaringClass();
Ian Rogersb4903572012-10-11 11:52:56 -07003123 return reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003124 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003125 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003126 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogersb4903572012-10-11 11:52:56 -07003127 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003128 }
3129}
3130
Ian Rogers776ac1f2012-04-13 23:36:36 -07003131void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003132 size_t* log2_max_gc_pc) {
3133 size_t local_gc_points = 0;
3134 size_t max_insn = 0;
3135 size_t max_ref_reg = -1;
3136 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3137 if (insn_flags_[i].IsGcPoint()) {
3138 local_gc_points++;
3139 max_insn = i;
3140 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003141 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003142 }
3143 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003144 *gc_points = local_gc_points;
3145 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3146 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003147 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003148 i++;
3149 }
3150 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003151}
3152
Ian Rogers776ac1f2012-04-13 23:36:36 -07003153const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003154 size_t num_entries, ref_bitmap_bits, pc_bits;
3155 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3156 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003157 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003158 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003159 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003160 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003161 return NULL;
3162 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003163 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3164 // There are 2 bytes to encode the number of entries
3165 if (num_entries >= 65536) {
3166 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003167 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003168 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003169 return NULL;
3170 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003171 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003172 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003173 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003174 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003175 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003176 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003177 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003178 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003179 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003180 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003181 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003182 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3183 return NULL;
3184 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003185 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003186 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003187 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003188 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003189 return NULL;
3190 }
3191 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003192 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3193 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003194 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003195 table->push_back(num_entries & 0xFF);
3196 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003197 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003198 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3199 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003200 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003201 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003202 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003203 }
3204 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003205 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003206 }
3207 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003208 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003209 return table;
3210}
jeffhaoa0a764a2011-09-16 10:43:38 -07003211
Ian Rogers776ac1f2012-04-13 23:36:36 -07003212void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003213 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3214 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003215 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003216 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003217 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003218 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3219 if (insn_flags_[i].IsGcPoint()) {
3220 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003221 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003222 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3223 map_index++;
3224 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003225 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003226 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003227 CHECK_LT(j / 8, map.RegWidth());
3228 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3229 } else if ((j / 8) < map.RegWidth()) {
3230 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3231 } else {
3232 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3233 }
3234 }
3235 } else {
3236 CHECK(reg_bitmap == NULL);
3237 }
3238 }
3239}
jeffhaoa0a764a2011-09-16 10:43:38 -07003240
Ian Rogers1212a022013-03-04 10:48:41 -08003241void MethodVerifier::SetDexGcMap(CompilerDriver::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003242 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003243 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003244 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
3245 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003246 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003247 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003248 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003249 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003250 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003251 CHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003252}
3253
Ian Rogers1212a022013-03-04 10:48:41 -08003254const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(CompilerDriver::MethodReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003255 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003256 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
3257 if (it == dex_gc_maps_->end()) {
Ian Rogers64b6d142012-10-29 16:34:15 -07003258 LOG(WARNING) << "Didn't find GC map for: " << PrettyMethod(ref.second, *ref.first);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003259 return NULL;
3260 }
3261 CHECK(it->second != NULL);
3262 return it->second;
3263}
3264
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003265std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3266 RegisterLine* line = reg_table_.GetLine(dex_pc);
3267 std::vector<int32_t> result;
3268 for (size_t i = 0; i < line->NumRegs(); ++i) {
3269 const RegType& type = line->GetRegisterType(i);
3270 if (type.IsConstant()) {
3271 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3272 result.push_back(type.ConstantValue());
3273 } else if (type.IsConstantLo()) {
3274 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3275 result.push_back(type.ConstantValueLo());
3276 } else if (type.IsConstantHi()) {
3277 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3278 result.push_back(type.ConstantValueHi());
3279 } else if (type.IsIntegralTypes()) {
3280 result.push_back(kIntVReg);
3281 result.push_back(0);
3282 } else if (type.IsFloat()) {
3283 result.push_back(kFloatVReg);
3284 result.push_back(0);
3285 } else if (type.IsLong()) {
3286 result.push_back(kLongLoVReg);
3287 result.push_back(0);
3288 result.push_back(kLongHiVReg);
3289 result.push_back(0);
3290 ++i;
3291 } else if (type.IsDouble()) {
3292 result.push_back(kDoubleLoVReg);
3293 result.push_back(0);
3294 result.push_back(kDoubleHiVReg);
3295 result.push_back(0);
3296 ++i;
3297 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
3298 result.push_back(kUndefined);
3299 result.push_back(0);
3300 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003301 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003302 result.push_back(kReferenceVReg);
3303 result.push_back(0);
3304 }
3305 }
3306 return result;
3307}
3308
Ian Rogers0c7abda2012-09-19 13:33:42 -07003309Mutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
3310MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003311
3312Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
3313MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
3314
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003315void MethodVerifier::Init() {
Ian Rogers0c7abda2012-09-19 13:33:42 -07003316 dex_gc_maps_lock_ = new Mutex("verifier GC maps lock");
Ian Rogers50b35e22012-10-04 10:09:15 -07003317 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003318 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003319 MutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003320 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003321 }
3322
3323 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
3324 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003325 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003326 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
3327 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003328}
3329
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003330void MethodVerifier::Shutdown() {
Ian Rogers50b35e22012-10-04 10:09:15 -07003331 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003332 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003333 MutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003334 STLDeleteValues(dex_gc_maps_);
3335 delete dex_gc_maps_;
3336 dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003337 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003338 delete dex_gc_maps_lock_;
3339 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003340
3341 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003342 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003343 delete rejected_classes_;
3344 rejected_classes_ = NULL;
3345 }
3346 delete rejected_classes_lock_;
3347 rejected_classes_lock_ = NULL;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003348}
jeffhaod1224c72012-02-29 13:43:08 -08003349
Ian Rogers1212a022013-03-04 10:48:41 -08003350void MethodVerifier::AddRejectedClass(CompilerDriver::ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003351 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003352 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003353 rejected_classes_->insert(ref);
3354 }
jeffhaod1224c72012-02-29 13:43:08 -08003355 CHECK(IsClassRejected(ref));
3356}
3357
Ian Rogers1212a022013-03-04 10:48:41 -08003358bool MethodVerifier::IsClassRejected(CompilerDriver::ClassReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003359 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003360 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08003361}
3362
Ian Rogersd81871c2011-10-03 13:57:23 -07003363} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003364} // namespace art