blob: ad4d6ed183c51065066cfa8e06b541715f639971 [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
Brian Carlstrom1f870082011-08-23 16:02:11 -070021#include "class_linker.h"
Brian Carlstrome7d856b2012-01-11 18:10:55 -080022#include "compiler.h"
jeffhaob4df5142011-09-19 20:25:32 -070023#include "dex_cache.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070024#include "dex_file.h"
25#include "dex_instruction.h"
26#include "dex_instruction_visitor.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070027#include "gc_map.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070028#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070029#include "leb128.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070030#include "logging.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080031#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070032#include "runtime.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070033#include "stringpiece.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070034
Logan Chienfca7e872011-12-20 20:08:22 +080035#if defined(ART_USE_LLVM_COMPILER)
36#include "compiler_llvm/backend_types.h"
37#include "compiler_llvm/inferred_reg_category_map.h"
38using namespace art::compiler_llvm;
39#endif
40
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070041namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070042namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070043
Ian Rogers2c8a8572011-10-24 17:11:36 -070044static const bool gDebugVerify = false;
45
Ian Rogers776ac1f2012-04-13 23:36:36 -070046class InsnFlags {
47 public:
48 InsnFlags() : length_(0), flags_(0) {}
49
50 void SetLengthInCodeUnits(size_t length) {
51 CHECK_LT(length, 65536u);
52 length_ = length;
53 }
54 size_t GetLengthInCodeUnits() {
55 return length_;
56 }
57 bool IsOpcode() const {
58 return length_ != 0;
59 }
60
61 void SetInTry() {
62 flags_ |= 1 << kInTry;
63 }
64 void ClearInTry() {
65 flags_ &= ~(1 << kInTry);
66 }
67 bool IsInTry() const {
68 return (flags_ & (1 << kInTry)) != 0;
69 }
70
71 void SetBranchTarget() {
72 flags_ |= 1 << kBranchTarget;
73 }
74 void ClearBranchTarget() {
75 flags_ &= ~(1 << kBranchTarget);
76 }
77 bool IsBranchTarget() const {
78 return (flags_ & (1 << kBranchTarget)) != 0;
79 }
80
81 void SetGcPoint() {
82 flags_ |= 1 << kGcPoint;
83 }
84 void ClearGcPoint() {
85 flags_ &= ~(1 << kGcPoint);
86 }
87 bool IsGcPoint() const {
88 return (flags_ & (1 << kGcPoint)) != 0;
89 }
90
91 void SetVisited() {
92 flags_ |= 1 << kVisited;
93 }
94 void ClearVisited() {
95 flags_ &= ~(1 << kVisited);
96 }
97 bool IsVisited() const {
98 return (flags_ & (1 << kVisited)) != 0;
99 }
100
101 void SetChanged() {
102 flags_ |= 1 << kChanged;
103 }
104 void ClearChanged() {
105 flags_ &= ~(1 << kChanged);
106 }
107 bool IsChanged() const {
108 return (flags_ & (1 << kChanged)) != 0;
109 }
110
111 bool IsVisitedOrChanged() const {
112 return IsVisited() || IsChanged();
113 }
114
115 std::string Dump() {
116 char encoding[6];
117 if (!IsOpcode()) {
118 strncpy(encoding, "XXXXX", sizeof(encoding));
119 } else {
120 strncpy(encoding, "-----", sizeof(encoding));
121 if (IsInTry()) encoding[kInTry] = 'T';
122 if (IsBranchTarget()) encoding[kBranchTarget] = 'B';
123 if (IsGcPoint()) encoding[kGcPoint] = 'G';
124 if (IsVisited()) encoding[kVisited] = 'V';
125 if (IsChanged()) encoding[kChanged] = 'C';
126 }
127 return std::string(encoding);
128 }
129 private:
130 enum {
131 kInTry,
132 kBranchTarget,
133 kGcPoint,
134 kVisited,
135 kChanged,
136 };
137
138 // Size of instruction in code units
139 uint16_t length_;
140 uint8_t flags_;
Ian Rogers84fa0742011-10-25 18:13:30 -0700141};
Ian Rogersd81871c2011-10-03 13:57:23 -0700142
Ian Rogersd81871c2011-10-03 13:57:23 -0700143void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
144 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -0700145 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700146 DCHECK_GT(insns_size, 0U);
147
148 for (uint32_t i = 0; i < insns_size; i++) {
149 bool interesting = false;
150 switch (mode) {
151 case kTrackRegsAll:
152 interesting = flags[i].IsOpcode();
153 break;
154 case kTrackRegsGcPoints:
155 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
156 break;
157 case kTrackRegsBranches:
158 interesting = flags[i].IsBranchTarget();
159 break;
160 default:
161 break;
162 }
163 if (interesting) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700164 pc_to_register_line_.Put(i, new RegisterLine(registers_size, verifier));
Ian Rogersd81871c2011-10-03 13:57:23 -0700165 }
166 }
167}
168
Ian Rogers776ac1f2012-04-13 23:36:36 -0700169bool MethodVerifier::VerifyClass(const Class* klass, std::string& error) {
jeffhaobdb76512011-09-07 11:43:16 -0700170 if (klass->IsVerified()) {
171 return true;
172 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700173 Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -0800174 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800175 error = "Verifier rejected class ";
176 error += PrettyDescriptor(klass);
177 error += " that has no super class";
Ian Rogersd81871c2011-10-03 13:57:23 -0700178 return false;
179 }
Ian Rogers1c5eb702012-02-01 09:18:34 -0800180 if (super != NULL && super->IsFinal()) {
181 error = "Verifier rejected class ";
182 error += PrettyDescriptor(klass);
183 error += " that attempts to sub-class final class ";
184 error += PrettyDescriptor(super);
185 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700186 }
Ian Rogers0d604842012-04-16 14:50:24 -0700187 ClassHelper kh(klass);
188 const DexFile& dex_file = kh.GetDexFile();
189 uint32_t class_def_idx;
190 if (!dex_file.FindClassDefIndex(kh.GetDescriptor(), class_def_idx)) {
191 error = "Verifier rejected class ";
192 error += PrettyDescriptor(klass);
193 error += " that isn't present in dex file ";
194 error += dex_file.GetLocation();
195 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700196 }
Ian Rogers0d604842012-04-16 14:50:24 -0700197 return VerifyClass(&dex_file, kh.GetDexCache(), klass->GetClassLoader(), class_def_idx, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700198}
199
Ian Rogers776ac1f2012-04-13 23:36:36 -0700200bool MethodVerifier::VerifyClass(const DexFile* dex_file, DexCache* dex_cache,
jeffhaof56197c2012-03-05 18:01:54 -0800201 const ClassLoader* class_loader, uint32_t class_def_idx, std::string& error) {
202 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
203 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogers0d604842012-04-16 14:50:24 -0700204 if (class_data == NULL) {
205 // empty class, probably a marker interface
206 return true;
207 }
jeffhaof56197c2012-03-05 18:01:54 -0800208 ClassDataItemIterator it(*dex_file, class_data);
209 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
210 it.Next();
211 }
Ian Rogers0d604842012-04-16 14:50:24 -0700212 size_t error_count = 0;
213 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaof56197c2012-03-05 18:01:54 -0800214 while (it.HasNextDirectMethod()) {
215 uint32_t method_idx = it.GetMemberIndex();
Ian Rogers0d604842012-04-16 14:50:24 -0700216 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, true);
217 if (method == NULL) {
218 DCHECK(Thread::Current()->IsExceptionPending());
219 // We couldn't resolve the method, but continue regardless.
220 Thread::Current()->ClearException();
221 }
jeffhaof56197c2012-03-05 18:01:54 -0800222 if (!VerifyMethod(method_idx, dex_file, dex_cache, class_loader, class_def_idx,
Ian Rogers0d604842012-04-16 14:50:24 -0700223 it.GetMethodCodeItem(), method, it.GetMemberAccessFlags())) {
224 if (error_count > 0) {
225 error += "\n";
226 }
227 error = "Verifier rejected class ";
jeffhaof56197c2012-03-05 18:01:54 -0800228 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
229 error += " due to bad method ";
230 error += PrettyMethod(method_idx, *dex_file);
Ian Rogers0d604842012-04-16 14:50:24 -0700231 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800232 }
233 it.Next();
234 }
235 while (it.HasNextVirtualMethod()) {
236 uint32_t method_idx = it.GetMemberIndex();
Ian Rogers0d604842012-04-16 14:50:24 -0700237 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, false);
238 if (method == NULL) {
239 DCHECK(Thread::Current()->IsExceptionPending());
240 // We couldn't resolve the method, but continue regardless.
241 Thread::Current()->ClearException();
242 }
jeffhaof56197c2012-03-05 18:01:54 -0800243 if (!VerifyMethod(method_idx, dex_file, dex_cache, class_loader, class_def_idx,
Ian Rogers0d604842012-04-16 14:50:24 -0700244 it.GetMethodCodeItem(), method, it.GetMemberAccessFlags())) {
245 if (error_count > 0) {
246 error += "\n";
247 }
248 error = "Verifier rejected class ";
jeffhaof56197c2012-03-05 18:01:54 -0800249 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
250 error += " due to bad method ";
251 error += PrettyMethod(method_idx, *dex_file);
Ian Rogers0d604842012-04-16 14:50:24 -0700252 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800253 }
254 it.Next();
255 }
Ian Rogers0d604842012-04-16 14:50:24 -0700256 return error_count == 0;
jeffhaof56197c2012-03-05 18:01:54 -0800257}
258
Ian Rogers776ac1f2012-04-13 23:36:36 -0700259bool MethodVerifier::VerifyMethod(uint32_t method_idx, const DexFile* dex_file, DexCache* dex_cache,
Ian Rogers0d604842012-04-16 14:50:24 -0700260 const ClassLoader* class_loader, uint32_t class_def_idx, const DexFile::CodeItem* code_item,
261 Method* method, uint32_t method_access_flags) {
262 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item, method_idx,
263 method, method_access_flags);
264 bool success = verifier.Verify();
jeffhaof56197c2012-03-05 18:01:54 -0800265 if (success) {
Ian Rogers0d604842012-04-16 14:50:24 -0700266 // Verification completed, however failures may be pending that didn't cause the verification
267 // to hard fail.
268 if (verifier.failures_.size() != 0) {
269 verifier.DumpFailures(LOG(INFO) << "Soft verification failures in "
270 << PrettyMethod(method_idx, *dex_file) << std::endl);
271 success = false;
jeffhaof56197c2012-03-05 18:01:54 -0800272 }
273 } else {
Ian Rogers0d604842012-04-16 14:50:24 -0700274 // Bad method data.
275 CHECK_NE(verifier.failures_.size(), 0U);
276 CHECK(verifier.have_pending_hard_failure_);
277 verifier.DumpFailures(LOG(INFO) << "Verification error in "
278 << PrettyMethod(method_idx, *dex_file) << std::endl);
jeffhaof56197c2012-03-05 18:01:54 -0800279 if (gDebugVerify) {
280 std::cout << std::endl << verifier.info_messages_.str();
281 verifier.Dump(std::cout);
282 }
jeffhaof56197c2012-03-05 18:01:54 -0800283 }
284 return success;
285}
286
Ian Rogers0d604842012-04-16 14:50:24 -0700287void MethodVerifier::VerifyMethodAndDump(Method* method) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800288 CHECK(method != NULL);
Ian Rogers0d604842012-04-16 14:50:24 -0700289 MethodHelper mh(method);
290 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
291 mh.GetClassDefIndex(), mh.GetCodeItem(), method->GetDexMethodIndex(),
292 method, method->GetAccessFlags());
293 verifier.Verify();
294 verifier.DumpFailures(LOG(INFO) << "Dump of method " << PrettyMethod(method) << std::endl)
295 << verifier.info_messages_.str() << Dumpable<MethodVerifier>(verifier);
jeffhaoba5ebb92011-08-25 17:24:37 -0700296}
297
Ian Rogers776ac1f2012-04-13 23:36:36 -0700298MethodVerifier::MethodVerifier(const DexFile* dex_file, DexCache* dex_cache,
Ian Rogers0d604842012-04-16 14:50:24 -0700299 const ClassLoader* class_loader, uint32_t class_def_idx, const DexFile::CodeItem* code_item,
300 uint32_t method_idx, Method* method, uint32_t method_access_flags)
jeffhaof56197c2012-03-05 18:01:54 -0800301 : work_insn_idx_(-1),
Ian Rogers0d604842012-04-16 14:50:24 -0700302 method_idx_(method_idx),
303 foo_method_(method),
304 method_access_flags_(method_access_flags),
jeffhaof56197c2012-03-05 18:01:54 -0800305 dex_file_(dex_file),
306 dex_cache_(dex_cache),
307 class_loader_(class_loader),
308 class_def_idx_(class_def_idx),
309 code_item_(code_item),
Ian Rogers0d604842012-04-16 14:50:24 -0700310 have_pending_hard_failure_(false),
311 have_pending_rewrite_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800312 new_instance_count_(0),
313 monitor_enter_count_(0) {
314}
315
Ian Rogers0d604842012-04-16 14:50:24 -0700316bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700317 // If there aren't any instructions, make sure that's expected, then exit successfully.
318 if (code_item_ == NULL) {
Ian Rogers0d604842012-04-16 14:50:24 -0700319 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700320 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700321 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700322 } else {
323 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700324 }
jeffhaobdb76512011-09-07 11:43:16 -0700325 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700326 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
327 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700328 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
329 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700330 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700331 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700332 // Allocate and initialize an array to hold instruction data.
333 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
334 // Run through the instructions and see if the width checks out.
335 bool result = ComputeWidthsAndCountOps();
336 // Flag instructions guarded by a "try" block and check exception handlers.
337 result = result && ScanTryCatchBlocks();
338 // Perform static instruction verification.
339 result = result && VerifyInstructions();
Ian Rogers0d604842012-04-16 14:50:24 -0700340 // Perform code-flow analysis and return.
341 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700342}
343
Ian Rogers776ac1f2012-04-13 23:36:36 -0700344std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogers0d604842012-04-16 14:50:24 -0700345 switch (error) {
346 case VERIFY_ERROR_NO_CLASS:
347 case VERIFY_ERROR_NO_FIELD:
348 case VERIFY_ERROR_NO_METHOD:
349 case VERIFY_ERROR_ACCESS_CLASS:
350 case VERIFY_ERROR_ACCESS_FIELD:
351 case VERIFY_ERROR_ACCESS_METHOD:
352 if (Runtime::Current()->IsCompiler()) {
353 // If we're optimistically running verification at compile time, turn NO_xxx and ACCESS_xxx
354 // errors into soft verification errors so that we re-verify at runtime. We may fail to find
355 // or to agree on access because of not yet available class loaders, or class loaders that
356 // will differ at runtime.
jeffhaod5347e02012-03-22 17:25:05 -0700357 error = VERIFY_ERROR_BAD_CLASS_SOFT;
Ian Rogers0d604842012-04-16 14:50:24 -0700358 } else {
359 have_pending_rewrite_failure_ = true;
360 }
361 break;
362 // Errors that are bad at both compile and runtime, but don't cause rejection of the class.
363 case VERIFY_ERROR_CLASS_CHANGE:
364 case VERIFY_ERROR_INSTANTIATION:
365 have_pending_rewrite_failure_ = true;
366 break;
367 // Indication that verification should be retried at runtime.
368 case VERIFY_ERROR_BAD_CLASS_SOFT:
369 if (!Runtime::Current()->IsCompiler()) {
370 // It is runtime so hard fail.
371 have_pending_hard_failure_ = true;
372 }
373 break;
jeffhaod5347e02012-03-22 17:25:05 -0700374 // Hard verification failures at compile time will still fail at runtime, so the class is
375 // marked as rejected to prevent it from being compiled.
Ian Rogers0d604842012-04-16 14:50:24 -0700376 case VERIFY_ERROR_BAD_CLASS_HARD: {
377 if (Runtime::Current()->IsCompiler()) {
jeffhaof56197c2012-03-05 18:01:54 -0800378 Compiler::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800379 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800380 }
Ian Rogers0d604842012-04-16 14:50:24 -0700381 have_pending_hard_failure_ = true;
382 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800383 }
384 }
Ian Rogers0d604842012-04-16 14:50:24 -0700385 failures_.push_back(error);
386 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(method_idx_, *dex_file_).c_str(),
387 work_insn_idx_));
388 std::ostringstream* failure_message = new std::ostringstream(location);
389 failure_messages_.push_back(failure_message);
390 return *failure_message;
391}
392
393void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
394 size_t failure_num = failure_messages_.size();
395 DCHECK_NE(failure_num, 0U);
396 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
397 prepend += last_fail_message->str();
398 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
399 delete last_fail_message;
400}
401
402void MethodVerifier::AppendToLastFailMessage(std::string append) {
403 size_t failure_num = failure_messages_.size();
404 DCHECK_NE(failure_num, 0U);
405 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
406 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800407}
408
Ian Rogers776ac1f2012-04-13 23:36:36 -0700409bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700410 const uint16_t* insns = code_item_->insns_;
411 size_t insns_size = code_item_->insns_size_in_code_units_;
412 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700413 size_t new_instance_count = 0;
414 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700415 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700416
Ian Rogersd81871c2011-10-03 13:57:23 -0700417 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700418 Instruction::Code opcode = inst->Opcode();
419 if (opcode == Instruction::NEW_INSTANCE) {
420 new_instance_count++;
421 } else if (opcode == Instruction::MONITOR_ENTER) {
422 monitor_enter_count++;
423 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700424 size_t inst_size = inst->SizeInCodeUnits();
425 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
426 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700427 inst = inst->Next();
428 }
429
Ian Rogersd81871c2011-10-03 13:57:23 -0700430 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700431 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
432 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700433 return false;
434 }
435
Ian Rogersd81871c2011-10-03 13:57:23 -0700436 new_instance_count_ = new_instance_count;
437 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700438 return true;
439}
440
Ian Rogers776ac1f2012-04-13 23:36:36 -0700441bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700442 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700443 if (tries_size == 0) {
444 return true;
445 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700446 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700447 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700448
449 for (uint32_t idx = 0; idx < tries_size; idx++) {
450 const DexFile::TryItem* try_item = &tries[idx];
451 uint32_t start = try_item->start_addr_;
452 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700453 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700454 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
455 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700456 return false;
457 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700458 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700459 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700460 return false;
461 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700462 for (uint32_t dex_pc = start; dex_pc < end;
463 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
464 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700465 }
466 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800467 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700468 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700469 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700470 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700471 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700472 CatchHandlerIterator iterator(handlers_ptr);
473 for (; iterator.HasNext(); iterator.Next()) {
474 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700475 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700476 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700477 return false;
478 }
jeffhao60f83e32012-02-13 17:16:30 -0800479 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
480 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700481 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
jeffhao60f83e32012-02-13 17:16:30 -0800482 << dex_pc << ")";
483 return false;
484 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700485 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700486 // Ensure exception types are resolved so that they don't need resolution to be delivered,
487 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700488 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
jeffhaof56197c2012-03-05 18:01:54 -0800489 Class* exception_type = linker->ResolveType(*dex_file_, iterator.GetHandlerTypeIndex(),
490 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700491 if (exception_type == NULL) {
492 DCHECK(Thread::Current()->IsExceptionPending());
493 Thread::Current()->ClearException();
494 }
495 }
jeffhaobdb76512011-09-07 11:43:16 -0700496 }
Ian Rogers0571d352011-11-03 19:51:38 -0700497 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700498 }
jeffhaobdb76512011-09-07 11:43:16 -0700499 return true;
500}
501
Ian Rogers776ac1f2012-04-13 23:36:36 -0700502bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700503 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700504
Ian Rogersd81871c2011-10-03 13:57:23 -0700505 /* Flag the start of the method as a branch target. */
506 insn_flags_[0].SetBranchTarget();
507
508 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700509 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700510 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogers0d604842012-04-16 14:50:24 -0700511 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700512 return false;
513 }
514 /* Flag instructions that are garbage collection points */
515 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
516 insn_flags_[dex_pc].SetGcPoint();
517 }
518 dex_pc += inst->SizeInCodeUnits();
519 inst = inst->Next();
520 }
521 return true;
522}
523
Ian Rogers776ac1f2012-04-13 23:36:36 -0700524bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800525 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700526 bool result = true;
527 switch (inst->GetVerifyTypeArgumentA()) {
528 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800529 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700530 break;
531 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800532 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700533 break;
534 }
535 switch (inst->GetVerifyTypeArgumentB()) {
536 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800537 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700538 break;
539 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800540 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700541 break;
542 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800543 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700544 break;
545 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800546 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700547 break;
548 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800549 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700550 break;
551 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800552 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700553 break;
554 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800555 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700556 break;
557 }
558 switch (inst->GetVerifyTypeArgumentC()) {
559 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800560 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700561 break;
562 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800563 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700564 break;
565 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800566 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700567 break;
568 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800569 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700570 break;
571 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800572 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700573 break;
574 }
575 switch (inst->GetVerifyExtraFlags()) {
576 case Instruction::kVerifyArrayData:
577 result = result && CheckArrayData(code_offset);
578 break;
579 case Instruction::kVerifyBranchTarget:
580 result = result && CheckBranchTarget(code_offset);
581 break;
582 case Instruction::kVerifySwitchTargets:
583 result = result && CheckSwitchTargets(code_offset);
584 break;
585 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800586 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700587 break;
588 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800589 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700590 break;
591 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700592 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700593 result = false;
594 break;
595 }
596 return result;
597}
598
Ian Rogers776ac1f2012-04-13 23:36:36 -0700599bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700600 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700601 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
602 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700603 return false;
604 }
605 return true;
606}
607
Ian Rogers776ac1f2012-04-13 23:36:36 -0700608bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700609 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700610 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
611 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700612 return false;
613 }
614 return true;
615}
616
Ian Rogers776ac1f2012-04-13 23:36:36 -0700617bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700618 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700619 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
620 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700621 return false;
622 }
623 return true;
624}
625
Ian Rogers776ac1f2012-04-13 23:36:36 -0700626bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700627 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700628 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
629 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700630 return false;
631 }
632 return true;
633}
634
Ian Rogers776ac1f2012-04-13 23:36:36 -0700635bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700636 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700637 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
638 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700639 return false;
640 }
641 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700642 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700643 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700644 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700645 return false;
646 }
647 return true;
648}
649
Ian Rogers776ac1f2012-04-13 23:36:36 -0700650bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700651 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700652 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
653 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700654 return false;
655 }
656 return true;
657}
658
Ian Rogers776ac1f2012-04-13 23:36:36 -0700659bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700660 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700661 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
662 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700663 return false;
664 }
665 return true;
666}
667
Ian Rogers776ac1f2012-04-13 23:36:36 -0700668bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700669 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700670 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
671 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700672 return false;
673 }
674 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700675 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700676 const char* cp = descriptor;
677 while (*cp++ == '[') {
678 bracket_count++;
679 }
680 if (bracket_count == 0) {
681 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700682 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 return false;
684 } else if (bracket_count > 255) {
685 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700686 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700687 return false;
688 }
689 return true;
690}
691
Ian Rogers776ac1f2012-04-13 23:36:36 -0700692bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700693 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
694 const uint16_t* insns = code_item_->insns_ + cur_offset;
695 const uint16_t* array_data;
696 int32_t array_data_offset;
697
698 DCHECK_LT(cur_offset, insn_count);
699 /* make sure the start of the array data table is in range */
700 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
701 if ((int32_t) cur_offset + array_data_offset < 0 ||
702 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700703 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
704 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700705 return false;
706 }
707 /* offset to array data table is a relative branch-style offset */
708 array_data = insns + array_data_offset;
709 /* make sure the table is 32-bit aligned */
710 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700711 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
712 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700713 return false;
714 }
715 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700716 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700717 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
718 /* make sure the end of the switch is in range */
719 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700720 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
721 << ", data offset " << array_data_offset << ", end "
722 << cur_offset + array_data_offset + table_size
723 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700724 return false;
725 }
726 return true;
727}
728
Ian Rogers776ac1f2012-04-13 23:36:36 -0700729bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700730 int32_t offset;
731 bool isConditional, selfOkay;
732 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
733 return false;
734 }
735 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700736 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 -0700737 return false;
738 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700739 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
740 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700741 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700742 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700743 return false;
744 }
745 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
746 int32_t abs_offset = cur_offset + offset;
747 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700748 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700749 << reinterpret_cast<void*>(abs_offset) << ") at "
750 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700751 return false;
752 }
753 insn_flags_[abs_offset].SetBranchTarget();
754 return true;
755}
756
Ian Rogers776ac1f2012-04-13 23:36:36 -0700757bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700758 bool* selfOkay) {
759 const uint16_t* insns = code_item_->insns_ + cur_offset;
760 *pConditional = false;
761 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700762 switch (*insns & 0xff) {
763 case Instruction::GOTO:
764 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700765 break;
766 case Instruction::GOTO_32:
767 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700768 *selfOkay = true;
769 break;
770 case Instruction::GOTO_16:
771 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700772 break;
773 case Instruction::IF_EQ:
774 case Instruction::IF_NE:
775 case Instruction::IF_LT:
776 case Instruction::IF_GE:
777 case Instruction::IF_GT:
778 case Instruction::IF_LE:
779 case Instruction::IF_EQZ:
780 case Instruction::IF_NEZ:
781 case Instruction::IF_LTZ:
782 case Instruction::IF_GEZ:
783 case Instruction::IF_GTZ:
784 case Instruction::IF_LEZ:
785 *pOffset = (int16_t) insns[1];
786 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700787 break;
788 default:
789 return false;
790 break;
791 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700792 return true;
793}
794
Ian Rogers776ac1f2012-04-13 23:36:36 -0700795bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700796 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700797 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700798 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700799 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700800 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
801 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700802 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
803 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700804 return false;
805 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700806 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700807 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700808 /* make sure the table is 32-bit aligned */
809 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700810 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
811 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700812 return false;
813 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700814 uint32_t switch_count = switch_insns[1];
815 int32_t keys_offset, targets_offset;
816 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700817 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
818 /* 0=sig, 1=count, 2/3=firstKey */
819 targets_offset = 4;
820 keys_offset = -1;
821 expected_signature = Instruction::kPackedSwitchSignature;
822 } else {
823 /* 0=sig, 1=count, 2..count*2 = keys */
824 keys_offset = 2;
825 targets_offset = 2 + 2 * switch_count;
826 expected_signature = Instruction::kSparseSwitchSignature;
827 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700828 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700829 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700830 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
831 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700832 return false;
833 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700834 /* make sure the end of the switch is in range */
835 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700836 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
837 << switch_offset << ", end "
838 << (cur_offset + switch_offset + table_size)
839 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700840 return false;
841 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700842 /* for a sparse switch, verify the keys are in ascending order */
843 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700844 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
845 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700846 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
847 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
848 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700849 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
850 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700851 return false;
852 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700853 last_key = key;
854 }
855 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700856 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700857 for (uint32_t targ = 0; targ < switch_count; targ++) {
858 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
859 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
860 int32_t abs_offset = cur_offset + offset;
861 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700862 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700863 << reinterpret_cast<void*>(abs_offset) << ") at "
864 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700865 return false;
866 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700867 insn_flags_[abs_offset].SetBranchTarget();
868 }
869 return true;
870}
871
Ian Rogers776ac1f2012-04-13 23:36:36 -0700872bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700873 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700874 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700875 return false;
876 }
877 uint16_t registers_size = code_item_->registers_size_;
878 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800879 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700880 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
881 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700882 return false;
883 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700884 }
885
886 return true;
887}
888
Ian Rogers776ac1f2012-04-13 23:36:36 -0700889bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700890 uint16_t registers_size = code_item_->registers_size_;
891 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
892 // integer overflow when adding them here.
893 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700894 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
895 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700896 return false;
897 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700898 return true;
899}
900
Brian Carlstrom75412882012-01-18 01:26:54 -0800901const std::vector<uint8_t>* CreateLengthPrefixedGcMap(const std::vector<uint8_t>& gc_map) {
902 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
903 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
904 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
905 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
906 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
907 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
908 gc_map.begin(),
909 gc_map.end());
910 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
911 DCHECK_EQ(gc_map.size(),
912 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
913 (length_prefixed_gc_map->at(1) << 16) |
914 (length_prefixed_gc_map->at(2) << 8) |
915 (length_prefixed_gc_map->at(3) << 0)));
916 return length_prefixed_gc_map;
917}
918
Ian Rogers776ac1f2012-04-13 23:36:36 -0700919bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700920 uint16_t registers_size = code_item_->registers_size_;
921 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -0700922
Ian Rogersd81871c2011-10-03 13:57:23 -0700923 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -0800924 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
925 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700926 }
927 /* Create and initialize table holding register status */
Elliott Hughes460384f2012-04-04 16:53:10 -0700928 reg_table_.Init(kTrackRegsGcPoints, insn_flags_.get(), insns_size, registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -0700929
Ian Rogersd81871c2011-10-03 13:57:23 -0700930 work_line_.reset(new RegisterLine(registers_size, this));
931 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -0700932
Ian Rogersd81871c2011-10-03 13:57:23 -0700933 /* Initialize register types of method arguments. */
934 if (!SetTypesFromSignature()) {
Ian Rogers0d604842012-04-16 14:50:24 -0700935 DCHECK_NE(failures_.size(), 0U);
936 std::string prepend("Bad signature in ");
937 prepend += PrettyMethod(method_idx_, *dex_file_);
938 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -0700939 return false;
940 }
941 /* Perform code flow verification. */
942 if (!CodeFlowVerifyMethod()) {
Ian Rogers0d604842012-04-16 14:50:24 -0700943 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700945 }
946
Ian Rogersd81871c2011-10-03 13:57:23 -0700947 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -0800948 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
949 if (map.get() == NULL) {
Ian Rogers0d604842012-04-16 14:50:24 -0700950 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700951 return false; // Not a real failure, but a failure to encode
952 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700953#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800954 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -0700955#endif
Brian Carlstrom75412882012-01-18 01:26:54 -0800956 const std::vector<uint8_t>* gc_map = CreateLengthPrefixedGcMap(*(map.get()));
Ian Rogers0d604842012-04-16 14:50:24 -0700957 Compiler::MethodReference ref(dex_file_, method_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700958 verifier::MethodVerifier::SetGcMap(ref, *gc_map);
Logan Chienfca7e872011-12-20 20:08:22 +0800959
Ian Rogers0d604842012-04-16 14:50:24 -0700960 if (foo_method_ != NULL) {
961 foo_method_->SetGcMap(&gc_map->at(0));
962 }
Logan Chiendd361c92012-04-10 23:40:37 +0800963
964#if defined(ART_USE_LLVM_COMPILER)
Logan Chienfca7e872011-12-20 20:08:22 +0800965 /* Generate Inferred Register Category for LLVM-based Code Generator */
966 const InferredRegCategoryMap* table = GenerateInferredRegCategoryMap();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700967 verifier::MethodVerifier::SetInferredRegCategoryMap(ref, *table);
Logan Chienfca7e872011-12-20 20:08:22 +0800968#endif
969
jeffhaobdb76512011-09-07 11:43:16 -0700970 return true;
971}
972
Ian Rogers0d604842012-04-16 14:50:24 -0700973std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
974 DCHECK_EQ(failures_.size(), failure_messages_.size());
975 for (size_t i = 0; i < failures_.size(); ++i) {
976 os << failure_messages_[i]->str() << std::endl;
977 }
978 return os;
979}
980
981extern "C" void MethodVerifierGdbDump(MethodVerifier* v) {
982 v->Dump(std::cerr);
983}
984
Ian Rogers776ac1f2012-04-13 23:36:36 -0700985void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -0800986 if (code_item_ == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700987 os << "Native method" << std::endl;
988 return;
jeffhaobdb76512011-09-07 11:43:16 -0700989 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700990 DCHECK(code_item_ != NULL);
991 const Instruction* inst = Instruction::At(code_item_->insns_);
992 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
993 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Elliott Hughesaa6e1cd2012-01-18 19:26:06 -0800994 os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
Ian Rogers2c8a8572011-10-24 17:11:36 -0700995 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -0700996 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
997 if (reg_line != NULL) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700998 os << reg_line->Dump() << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -0700999 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001000 inst = inst->Next();
1001 }
jeffhaobdb76512011-09-07 11:43:16 -07001002}
1003
Ian Rogersd81871c2011-10-03 13:57:23 -07001004static bool IsPrimitiveDescriptor(char descriptor) {
1005 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001006 case 'I':
1007 case 'C':
1008 case 'S':
1009 case 'B':
1010 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001011 case 'F':
1012 case 'D':
1013 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001014 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001015 default:
1016 return false;
1017 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001018}
1019
Ian Rogers776ac1f2012-04-13 23:36:36 -07001020bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001021 RegisterLine* reg_line = reg_table_.GetLine(0);
1022 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1023 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001024
Ian Rogersd81871c2011-10-03 13:57:23 -07001025 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1026 //Include the "this" pointer.
1027 size_t cur_arg = 0;
Ian Rogers0d604842012-04-16 14:50:24 -07001028 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001029 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1030 // argument as uninitialized. This restricts field access until the superclass constructor is
1031 // called.
Ian Rogers0d604842012-04-16 14:50:24 -07001032 const RegType& declaring_class = GetDeclaringClass();
1033 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001034 reg_line->SetRegisterType(arg_start + cur_arg,
1035 reg_types_.UninitializedThisArgument(declaring_class));
1036 } else {
Ian Rogers0d604842012-04-16 14:50:24 -07001037 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001038 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001040 }
1041
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001042 const DexFile::ProtoId& proto_id =
Ian Rogers0d604842012-04-16 14:50:24 -07001043 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001044 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001045
1046 for (; iterator.HasNext(); iterator.Next()) {
1047 const char* descriptor = iterator.GetDescriptor();
1048 if (descriptor == NULL) {
1049 LOG(FATAL) << "Null descriptor";
1050 }
1051 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001052 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1053 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001054 return false;
1055 }
1056 switch (descriptor[0]) {
1057 case 'L':
1058 case '[':
1059 // We assume that reference arguments are initialized. The only way it could be otherwise
1060 // (assuming the caller was verified) is if the current method is <init>, but in that case
1061 // it's effectively considered initialized the instant we reach here (in the sense that we
1062 // can return without doing anything or call virtual methods).
1063 {
Ian Rogers0d604842012-04-16 14:50:24 -07001064 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001065 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001066 }
1067 break;
1068 case 'Z':
1069 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1070 break;
1071 case 'C':
1072 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1073 break;
1074 case 'B':
1075 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1076 break;
1077 case 'I':
1078 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1079 break;
1080 case 'S':
1081 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1082 break;
1083 case 'F':
1084 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1085 break;
1086 case 'J':
1087 case 'D': {
1088 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1089 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1090 cur_arg++;
1091 break;
1092 }
1093 default:
jeffhaod5347e02012-03-22 17:25:05 -07001094 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001095 return false;
1096 }
1097 cur_arg++;
1098 }
1099 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001100 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001101 return false;
1102 }
1103 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1104 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1105 // format. Only major difference from the method argument format is that 'V' is supported.
1106 bool result;
1107 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1108 result = descriptor[1] == '\0';
1109 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1110 size_t i = 0;
1111 do {
1112 i++;
1113 } while (descriptor[i] == '['); // process leading [
1114 if (descriptor[i] == 'L') { // object array
1115 do {
1116 i++; // find closing ;
1117 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1118 result = descriptor[i] == ';';
1119 } else { // primitive array
1120 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1121 }
1122 } else if (descriptor[0] == 'L') {
1123 // could be more thorough here, but shouldn't be required
1124 size_t i = 0;
1125 do {
1126 i++;
1127 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1128 result = descriptor[i] == ';';
1129 } else {
1130 result = false;
1131 }
1132 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001133 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1134 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001135 }
1136 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001137}
1138
Ian Rogers776ac1f2012-04-13 23:36:36 -07001139bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001140 const uint16_t* insns = code_item_->insns_;
1141 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001142
jeffhaobdb76512011-09-07 11:43:16 -07001143 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001144 insn_flags_[0].SetChanged();
1145 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001146
jeffhaobdb76512011-09-07 11:43:16 -07001147 /* Continue until no instructions are marked "changed". */
1148 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001149 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1150 uint32_t insn_idx = start_guess;
1151 for (; insn_idx < insns_size; insn_idx++) {
1152 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001153 break;
1154 }
jeffhaobdb76512011-09-07 11:43:16 -07001155 if (insn_idx == insns_size) {
1156 if (start_guess != 0) {
1157 /* try again, starting from the top */
1158 start_guess = 0;
1159 continue;
1160 } else {
1161 /* all flags are clear */
1162 break;
1163 }
1164 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001165 // We carry the working set of registers from instruction to instruction. If this address can
1166 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1167 // "changed" flags, we need to load the set of registers from the table.
1168 // Because we always prefer to continue on to the next instruction, we should never have a
1169 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1170 // target.
1171 work_insn_idx_ = insn_idx;
1172 if (insn_flags_[insn_idx].IsBranchTarget()) {
1173 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001174 } else {
1175#ifndef NDEBUG
1176 /*
1177 * Sanity check: retrieve the stored register line (assuming
1178 * a full table) and make sure it actually matches.
1179 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001180 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1181 if (register_line != NULL) {
1182 if (work_line_->CompareLine(register_line) != 0) {
1183 Dump(std::cout);
1184 std::cout << info_messages_.str();
Ian Rogers0d604842012-04-16 14:50:24 -07001185 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_idx_, *dex_file_)
Elliott Hughes398f64b2012-03-26 18:05:48 -07001186 << "@" << reinterpret_cast<void*>(work_insn_idx_) << std::endl
1187 << " work_line=" << *work_line_ << std::endl
1188 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001189 }
jeffhaobdb76512011-09-07 11:43:16 -07001190 }
1191#endif
1192 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001193 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers0d604842012-04-16 14:50:24 -07001194 std::string prepend(PrettyMethod(method_idx_, *dex_file_));
1195 prepend += " failed to verify: ";
1196 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001197 return false;
1198 }
jeffhaobdb76512011-09-07 11:43:16 -07001199 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001200 insn_flags_[insn_idx].SetVisited();
1201 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001202 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001203
Ian Rogers0d604842012-04-16 14:50:24 -07001204 if (DEAD_CODE_SCAN && ((method_access_flags_ & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001205 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001206 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001207 * (besides the wasted space), but it indicates a flaw somewhere
1208 * down the line, possibly in the verifier.
1209 *
1210 * If we've substituted "always throw" instructions into the stream,
1211 * we are almost certainly going to have some dead code.
1212 */
1213 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001214 uint32_t insn_idx = 0;
1215 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001216 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001217 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001218 * may or may not be preceded by a padding NOP (for alignment).
1219 */
1220 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1221 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1222 insns[insn_idx] == Instruction::kArrayDataSignature ||
1223 (insns[insn_idx] == Instruction::NOP &&
1224 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1225 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1226 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001227 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001228 }
1229
Ian Rogersd81871c2011-10-03 13:57:23 -07001230 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001231 if (dead_start < 0)
1232 dead_start = insn_idx;
1233 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001234 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001235 dead_start = -1;
1236 }
1237 }
1238 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001239 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001240 }
1241 }
jeffhaobdb76512011-09-07 11:43:16 -07001242 return true;
1243}
1244
Ian Rogers776ac1f2012-04-13 23:36:36 -07001245bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001246#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001247 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001248 gDvm.verifierStats.instrsReexamined++;
1249 } else {
1250 gDvm.verifierStats.instrsExamined++;
1251 }
1252#endif
1253
1254 /*
1255 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001256 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001257 * control to another statement:
1258 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001259 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001260 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001261 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001262 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001263 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001264 * throw an exception that is handled by an encompassing "try"
1265 * block.
1266 *
1267 * We can also return, in which case there is no successor instruction
1268 * from this point.
1269 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001270 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001271 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001272 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1273 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001274 DecodedInstruction dec_insn(inst);
1275 int opcode_flags = Instruction::Flags(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001276
jeffhaobdb76512011-09-07 11:43:16 -07001277 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001278 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001279 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001280 // Generate processing back trace to debug verifier
Ian Rogers5ed29bf2011-10-26 12:22:21 -07001281 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
1282 << *work_line_.get() << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001283 }
jeffhaobdb76512011-09-07 11:43:16 -07001284
1285 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001286 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001287 * can throw an exception, we will copy/merge this into the "catch"
1288 * address rather than work_line, because we don't want the result
1289 * from the "successful" code path (e.g. a check-cast that "improves"
1290 * a type) to be visible to the exception handler.
1291 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001292 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001293 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001294 } else {
1295#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001296 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001297#endif
1298 }
1299
Elliott Hughesadb8c672012-03-06 16:49:32 -08001300 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07001301 case Instruction::NOP:
1302 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001303 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001304 * a signature that looks like a NOP; if we see one of these in
1305 * the course of executing code then we have a problem.
1306 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001307 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001308 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001309 }
1310 break;
1311
1312 case Instruction::MOVE:
1313 case Instruction::MOVE_FROM16:
1314 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001315 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001316 break;
1317 case Instruction::MOVE_WIDE:
1318 case Instruction::MOVE_WIDE_FROM16:
1319 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001320 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07001321 break;
1322 case Instruction::MOVE_OBJECT:
1323 case Instruction::MOVE_OBJECT_FROM16:
1324 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001325 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001326 break;
1327
1328 /*
1329 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001330 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001331 * might want to hold the result in an actual CPU register, so the
1332 * Dalvik spec requires that these only appear immediately after an
1333 * invoke or filled-new-array.
1334 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001335 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001336 * redundant with the reset done below, but it can make the debug info
1337 * easier to read in some cases.)
1338 */
1339 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001340 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07001341 break;
1342 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001343 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001344 break;
1345 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001346 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07001347 break;
1348
Ian Rogersd81871c2011-10-03 13:57:23 -07001349 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001350 /*
jeffhao60f83e32012-02-13 17:16:30 -08001351 * This statement can only appear as the first instruction in an exception handler. We verify
1352 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001353 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001354 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08001355 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001356 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001357 }
jeffhaobdb76512011-09-07 11:43:16 -07001358 case Instruction::RETURN_VOID:
Ian Rogers0d604842012-04-16 14:50:24 -07001359 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1360 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001361 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001362 }
jeffhaobdb76512011-09-07 11:43:16 -07001363 }
1364 break;
1365 case Instruction::RETURN:
Ian Rogers0d604842012-04-16 14:50:24 -07001366 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001367 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001368 const RegType& return_type = GetMethodReturnType();
1369 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001370 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001371 } else {
1372 // Compilers may generate synthetic functions that write byte values into boolean fields.
1373 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001374 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001375 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1376 ((return_type.IsBoolean() || return_type.IsByte() ||
1377 return_type.IsShort() || return_type.IsChar()) &&
1378 src_type.IsInteger()));
1379 /* check the register contents */
Ian Rogers0d604842012-04-16 14:50:24 -07001380 bool success =
1381 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
1382 if (!success) {
1383 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001384 }
jeffhaobdb76512011-09-07 11:43:16 -07001385 }
1386 }
1387 break;
1388 case Instruction::RETURN_WIDE:
Ian Rogers0d604842012-04-16 14:50:24 -07001389 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001390 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001391 const RegType& return_type = GetMethodReturnType();
1392 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001393 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001394 } else {
1395 /* check the register contents */
Ian Rogers0d604842012-04-16 14:50:24 -07001396 bool success = work_line_->VerifyRegisterType(dec_insn.vA, return_type);
1397 if (!success) {
1398 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001399 }
jeffhaobdb76512011-09-07 11:43:16 -07001400 }
1401 }
1402 break;
1403 case Instruction::RETURN_OBJECT:
Ian Rogers0d604842012-04-16 14:50:24 -07001404 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001405 const RegType& return_type = GetMethodReturnType();
1406 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001407 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001408 } else {
1409 /* return_type is the *expected* return type, not register value */
1410 DCHECK(!return_type.IsZero());
1411 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08001412 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07001413 // Disallow returning uninitialized values and verify that the reference in vAA is an
1414 // instance of the "return_type"
1415 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001416 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001417 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001418 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
Ian Rogers9074b992011-10-26 17:41:55 -07001419 << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001420 }
1421 }
1422 }
1423 break;
1424
1425 case Instruction::CONST_4:
1426 case Instruction::CONST_16:
1427 case Instruction::CONST:
1428 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001429 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const((int32_t) dec_insn.vB));
jeffhaobdb76512011-09-07 11:43:16 -07001430 break;
1431 case Instruction::CONST_HIGH16:
1432 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001433 work_line_->SetRegisterType(dec_insn.vA,
1434 reg_types_.FromCat1Const((int32_t) dec_insn.vB << 16));
jeffhaobdb76512011-09-07 11:43:16 -07001435 break;
1436 case Instruction::CONST_WIDE_16:
1437 case Instruction::CONST_WIDE_32:
1438 case Instruction::CONST_WIDE:
1439 case Instruction::CONST_WIDE_HIGH16:
1440 /* could be long or double; resolved upon use */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001441 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07001442 break;
1443 case Instruction::CONST_STRING:
1444 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001445 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001446 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001447 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001448 // Get type from instruction if unresolved then we need an access check
1449 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08001450 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogers0d604842012-04-16 14:50:24 -07001451 // Register holds class, ie its type is class, on error it will hold Conflict.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001452 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers0d604842012-04-16 14:50:24 -07001453 res_type.IsConflict() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001454 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001455 }
jeffhaobdb76512011-09-07 11:43:16 -07001456 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001457 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001458 break;
1459 case Instruction::MONITOR_EXIT:
1460 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001461 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001462 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001463 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001464 * to the need to handle asynchronous exceptions, a now-deprecated
1465 * feature that Dalvik doesn't support.)
1466 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001467 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001468 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001469 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001470 * structured locking checks are working, the former would have
1471 * failed on the -enter instruction, and the latter is impossible.
1472 *
1473 * This is fortunate, because issue 3221411 prevents us from
1474 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001475 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001476 * some catch blocks (which will show up as "dead" code when
1477 * we skip them here); if we can't, then the code path could be
1478 * "live" so we still need to check it.
1479 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001480 opcode_flags &= ~Instruction::kThrow;
1481 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001482 break;
1483
Ian Rogers28ad40d2011-10-27 15:19:26 -07001484 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001485 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001486 /*
1487 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1488 * could be a "upcast" -- not expected, so we don't try to address it.)
1489 *
1490 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001491 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001492 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001493 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001494 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001495 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogers0d604842012-04-16 14:50:24 -07001496 if (res_type.IsConflict()) {
1497 DCHECK_NE(failures_.size(), 0U);
1498 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001499 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001500 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1501 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001502 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001503 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001504 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001505 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001506 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07001507 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001508 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001509 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001510 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001511 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001512 }
jeffhaobdb76512011-09-07 11:43:16 -07001513 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001514 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001515 }
1516 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001517 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001518 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001519 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001520 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001521 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001522 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001523 }
1524 }
1525 break;
1526 }
1527 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001528 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogers0d604842012-04-16 14:50:24 -07001529 if (res_type.IsConflict()) {
1530 DCHECK_NE(failures_.size(), 0U);
1531 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001532 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001533 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1534 // can't create an instance of an interface or abstract class */
1535 if (!res_type.IsInstantiableTypes()) {
1536 Fail(VERIFY_ERROR_INSTANTIATION)
1537 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001538 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001539 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1540 // Any registers holding previous allocations from this address that have not yet been
1541 // initialized must be marked invalid.
1542 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1543 // add the new uninitialized reference to the register state
Elliott Hughesadb8c672012-03-06 16:49:32 -08001544 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001545 }
1546 break;
1547 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001548 case Instruction::NEW_ARRAY:
1549 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001550 break;
1551 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08001552 VerifyNewArray(dec_insn, true, false);
1553 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001554 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001555 case Instruction::FILLED_NEW_ARRAY_RANGE:
1556 VerifyNewArray(dec_insn, true, true);
1557 just_set_result = true; // Filled new array range sets result register
1558 break;
jeffhaobdb76512011-09-07 11:43:16 -07001559 case Instruction::CMPL_FLOAT:
1560 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001561 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001562 break;
1563 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001564 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001565 break;
1566 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001567 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001568 break;
1569 case Instruction::CMPL_DOUBLE:
1570 case Instruction::CMPG_DOUBLE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001571 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08001572 break;
1573 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001574 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08001575 break;
1576 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001577 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001578 break;
1579 case Instruction::CMP_LONG:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001580 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08001581 break;
1582 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001583 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08001584 break;
1585 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001586 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001587 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001588 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001589 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001590 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001591 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001592 }
1593 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001594 }
jeffhaobdb76512011-09-07 11:43:16 -07001595 case Instruction::GOTO:
1596 case Instruction::GOTO_16:
1597 case Instruction::GOTO_32:
1598 /* no effect on or use of registers */
1599 break;
1600
1601 case Instruction::PACKED_SWITCH:
1602 case Instruction::SPARSE_SWITCH:
1603 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001604 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001605 break;
1606
Ian Rogersd81871c2011-10-03 13:57:23 -07001607 case Instruction::FILL_ARRAY_DATA: {
1608 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001609 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08001610 /* array_type can be null if the reg type is Zero */
1611 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001612 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001613 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001614 } else {
Ian Rogers0d604842012-04-16 14:50:24 -07001615 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1616 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001617 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001618 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1619 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001620 } else {
jeffhao457cc512012-02-02 16:55:13 -08001621 // Now verify if the element width in the table matches the element width declared in
1622 // the array
1623 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1624 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001625 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001626 } else {
1627 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1628 // Since we don't compress the data in Dex, expect to see equal width of data stored
1629 // in the table and expected from the array class.
1630 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001631 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1632 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001633 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001634 }
1635 }
jeffhaobdb76512011-09-07 11:43:16 -07001636 }
1637 }
1638 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001639 }
jeffhaobdb76512011-09-07 11:43:16 -07001640 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001641 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001642 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1643 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001644 bool mismatch = false;
1645 if (reg_type1.IsZero()) { // zero then integral or reference expected
1646 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1647 } else if (reg_type1.IsReferenceTypes()) { // both references?
1648 mismatch = !reg_type2.IsReferenceTypes();
1649 } else { // both integral?
1650 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1651 }
1652 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001653 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1654 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001655 }
1656 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001657 }
jeffhaobdb76512011-09-07 11:43:16 -07001658 case Instruction::IF_LT:
1659 case Instruction::IF_GE:
1660 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001661 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001662 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1663 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001664 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001665 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1666 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001667 }
1668 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001669 }
jeffhaobdb76512011-09-07 11:43:16 -07001670 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001671 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001672 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001673 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001674 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001675 }
jeffhaobdb76512011-09-07 11:43:16 -07001676 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001677 }
jeffhaobdb76512011-09-07 11:43:16 -07001678 case Instruction::IF_LTZ:
1679 case Instruction::IF_GEZ:
1680 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001681 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001682 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001683 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001684 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1685 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001686 }
jeffhaobdb76512011-09-07 11:43:16 -07001687 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001688 }
jeffhaobdb76512011-09-07 11:43:16 -07001689 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001690 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
1691 break;
jeffhaobdb76512011-09-07 11:43:16 -07001692 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001693 VerifyAGet(dec_insn, reg_types_.Byte(), true);
1694 break;
jeffhaobdb76512011-09-07 11:43:16 -07001695 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07001696 VerifyAGet(dec_insn, reg_types_.Char(), true);
1697 break;
jeffhaobdb76512011-09-07 11:43:16 -07001698 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001699 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001700 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001701 case Instruction::AGET:
1702 VerifyAGet(dec_insn, reg_types_.Integer(), true);
1703 break;
jeffhaobdb76512011-09-07 11:43:16 -07001704 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001705 VerifyAGet(dec_insn, reg_types_.Long(), true);
1706 break;
1707 case Instruction::AGET_OBJECT:
1708 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001709 break;
1710
Ian Rogersd81871c2011-10-03 13:57:23 -07001711 case Instruction::APUT_BOOLEAN:
1712 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
1713 break;
1714 case Instruction::APUT_BYTE:
1715 VerifyAPut(dec_insn, reg_types_.Byte(), true);
1716 break;
1717 case Instruction::APUT_CHAR:
1718 VerifyAPut(dec_insn, reg_types_.Char(), true);
1719 break;
1720 case Instruction::APUT_SHORT:
1721 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001722 break;
1723 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001724 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001725 break;
1726 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001727 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001728 break;
1729 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001730 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001731 break;
1732
jeffhaobdb76512011-09-07 11:43:16 -07001733 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001734 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001735 break;
jeffhaobdb76512011-09-07 11:43:16 -07001736 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001737 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001738 break;
jeffhaobdb76512011-09-07 11:43:16 -07001739 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001740 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001741 break;
jeffhaobdb76512011-09-07 11:43:16 -07001742 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001743 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001744 break;
1745 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001746 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001747 break;
1748 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001749 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001750 break;
1751 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001752 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001753 break;
jeffhaobdb76512011-09-07 11:43:16 -07001754
Ian Rogersd81871c2011-10-03 13:57:23 -07001755 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001756 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001757 break;
1758 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001759 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001760 break;
1761 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001762 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001763 break;
1764 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001765 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001766 break;
1767 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001768 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001769 break;
1770 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001771 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001772 break;
jeffhaobdb76512011-09-07 11:43:16 -07001773 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001774 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001775 break;
1776
jeffhaobdb76512011-09-07 11:43:16 -07001777 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001778 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001779 break;
jeffhaobdb76512011-09-07 11:43:16 -07001780 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001781 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001782 break;
jeffhaobdb76512011-09-07 11:43:16 -07001783 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001784 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001785 break;
jeffhaobdb76512011-09-07 11:43:16 -07001786 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001787 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001788 break;
1789 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001790 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001791 break;
1792 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001793 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001794 break;
1795 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001796 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001797 break;
1798
1799 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001800 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001801 break;
1802 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001803 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001804 break;
1805 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001806 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001807 break;
1808 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001809 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001810 break;
1811 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001812 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001813 break;
1814 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001815 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001816 break;
1817 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001818 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001819 break;
1820
1821 case Instruction::INVOKE_VIRTUAL:
1822 case Instruction::INVOKE_VIRTUAL_RANGE:
1823 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001824 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001825 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
1826 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
1827 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
1828 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001829 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
Ian Rogers0d604842012-04-16 14:50:24 -07001830 const char* descriptor;
1831 if (called_method == NULL) {
1832 uint32_t method_idx = dec_insn.vB;
1833 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1834 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1835 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1836 } else {
1837 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07001838 }
Ian Rogers0d604842012-04-16 14:50:24 -07001839 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
1840 work_line_->SetResultRegisterType(return_type);
1841 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001842 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001843 }
jeffhaobdb76512011-09-07 11:43:16 -07001844 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001845 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001846 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001847 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
Ian Rogers0d604842012-04-16 14:50:24 -07001848 if (called_method != NULL) {
jeffhaobdb76512011-09-07 11:43:16 -07001849 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001850 * Some additional checks when calling a constructor. We know from the invocation arg check
1851 * that the "this" argument is an instance of called_method->klass. Now we further restrict
1852 * that to require that called_method->klass is the same as this->klass or this->super,
1853 * allowing the latter only if the "this" argument is the same as the "this" argument to
1854 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07001855 */
Ian Rogers0d604842012-04-16 14:50:24 -07001856 if (called_method->IsConstructor()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001857 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
Ian Rogers0d604842012-04-16 14:50:24 -07001858 if (this_type.IsConflict()) // failure.
jeffhaobdb76512011-09-07 11:43:16 -07001859 break;
1860
1861 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07001862 if (this_type.IsZero()) {
jeffhaod5347e02012-03-22 17:25:05 -07001863 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07001864 break;
1865 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001866 if (called_method != NULL) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001867 /* must be in same class or in superclass */
Ian Rogers0d604842012-04-16 14:50:24 -07001868 const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
1869 if (this_super_klass.IsConflict()) {
1870 // Unknown super class, fail so we re-check at runtime.
1871 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
1872 break;
1873 } else {
1874 if (!this_super_klass.IsZero() &&
1875 called_method->GetDeclaringClass() == this_super_klass.GetClass()) {
1876 if (this_type.GetClass() != GetDeclaringClass().GetClass()) {
1877 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1878 << "invoke-direct <init> on super only allowed for 'this' in <init>"
1879 << " (this class '" << this_type << "', called class '"
1880 << PrettyDescriptor(called_method->GetDeclaringClass()) << "')";
1881 break;
1882 }
1883 } else if (this_type.GetClass() != called_method->GetDeclaringClass()) {
jeffhaod5347e02012-03-22 17:25:05 -07001884 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogers0d604842012-04-16 14:50:24 -07001885 << "invoke-direct <init> must be on current class or super"
1886 << " (current class '" << this_type << "', called class '"
1887 << PrettyDescriptor(called_method->GetDeclaringClass()) << "')";
Ian Rogers28ad40d2011-10-27 15:19:26 -07001888 break;
1889 }
jeffhaobdb76512011-09-07 11:43:16 -07001890 }
jeffhaobdb76512011-09-07 11:43:16 -07001891 }
1892
1893 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07001894 if (!this_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001895 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
Ian Rogersd81871c2011-10-03 13:57:23 -07001896 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07001897 break;
1898 }
1899
1900 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07001901 * Replace the uninitialized reference with an initialized one. We need to do this for all
1902 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07001903 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001904 work_line_->MarkRefsAsInitialized(this_type);
jeffhao2a8a90e2011-09-26 14:25:31 -07001905 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001906 const char* descriptor;
1907 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001908 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001909 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1910 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07001911 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001912 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001913 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001914 }
Ian Rogers0d604842012-04-16 14:50:24 -07001915 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07001916 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07001917 just_set_result = true;
1918 }
1919 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001920 }
jeffhaobdb76512011-09-07 11:43:16 -07001921 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07001922 case Instruction::INVOKE_STATIC_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001923 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001924 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
Ian Rogers0d604842012-04-16 14:50:24 -07001925 const char* descriptor;
1926 if (called_method == NULL) {
1927 uint32_t method_idx = dec_insn.vB;
1928 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1929 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1930 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1931 } else {
1932 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogersd81871c2011-10-03 13:57:23 -07001933 }
Ian Rogers0d604842012-04-16 14:50:24 -07001934 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
1935 work_line_->SetResultRegisterType(return_type);
1936 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001937 }
1938 break;
1939 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001940 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001941 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001942 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
Ian Rogers0d604842012-04-16 14:50:24 -07001943 if (abs_method != NULL) {
1944 Class* called_interface = abs_method->GetDeclaringClass();
1945 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
1946 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
1947 << PrettyMethod(abs_method) << "'";
1948 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001949 }
jeffhaobdb76512011-09-07 11:43:16 -07001950 }
Ian Rogers0d604842012-04-16 14:50:24 -07001951 /* Get the type of the "this" arg, which should either be a sub-interface of called
1952 * interface or Object (see comments in RegType::JoinClass).
1953 */
1954 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1955 if (this_type.IsZero()) {
1956 /* null pointer always passes (and always fails at runtime) */
1957 } else {
1958 if (this_type.IsUninitializedTypes()) {
1959 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
1960 << this_type;
1961 break;
1962 }
1963 // In the past we have tried to assert that "called_interface" is assignable
1964 // from "this_type.GetClass()", however, as we do an imprecise Join
1965 // (RegType::JoinClass) we don't have full information on what interfaces are
1966 // implemented by "this_type". For example, two classes may implement the same
1967 // interfaces and have a common parent that doesn't implement the interface. The
1968 // join will set "this_type" to the parent class and a test that this implements
1969 // the interface will incorrectly fail.
1970 }
1971 /*
1972 * We don't have an object instance, so we can't find the concrete method. However, all of
1973 * the type information is in the abstract method, so we're good.
1974 */
1975 const char* descriptor;
1976 if (abs_method == NULL) {
1977 uint32_t method_idx = dec_insn.vB;
1978 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1979 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1980 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1981 } else {
1982 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
1983 }
1984 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
1985 work_line_->SetResultRegisterType(return_type);
1986 work_line_->SetResultRegisterType(return_type);
1987 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001988 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001989 }
jeffhaobdb76512011-09-07 11:43:16 -07001990 case Instruction::NEG_INT:
1991 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001992 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001993 break;
1994 case Instruction::NEG_LONG:
1995 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07001996 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07001997 break;
1998 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001999 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002000 break;
2001 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002002 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002003 break;
2004 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002005 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002006 break;
2007 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002008 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002009 break;
2010 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002011 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002012 break;
2013 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002014 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002015 break;
2016 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002017 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002018 break;
2019 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002020 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002021 break;
2022 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002023 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002024 break;
2025 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002026 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002027 break;
2028 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002029 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002030 break;
2031 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002032 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002033 break;
2034 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002035 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002036 break;
2037 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002038 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002039 break;
2040 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002041 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002042 break;
2043 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002044 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002045 break;
2046 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002047 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002048 break;
2049
2050 case Instruction::ADD_INT:
2051 case Instruction::SUB_INT:
2052 case Instruction::MUL_INT:
2053 case Instruction::REM_INT:
2054 case Instruction::DIV_INT:
2055 case Instruction::SHL_INT:
2056 case Instruction::SHR_INT:
2057 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002058 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002059 break;
2060 case Instruction::AND_INT:
2061 case Instruction::OR_INT:
2062 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002063 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002064 break;
2065 case Instruction::ADD_LONG:
2066 case Instruction::SUB_LONG:
2067 case Instruction::MUL_LONG:
2068 case Instruction::DIV_LONG:
2069 case Instruction::REM_LONG:
2070 case Instruction::AND_LONG:
2071 case Instruction::OR_LONG:
2072 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002073 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002074 break;
2075 case Instruction::SHL_LONG:
2076 case Instruction::SHR_LONG:
2077 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002078 /* shift distance is Int, making these different from other binary operations */
2079 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002080 break;
2081 case Instruction::ADD_FLOAT:
2082 case Instruction::SUB_FLOAT:
2083 case Instruction::MUL_FLOAT:
2084 case Instruction::DIV_FLOAT:
2085 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002086 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002087 break;
2088 case Instruction::ADD_DOUBLE:
2089 case Instruction::SUB_DOUBLE:
2090 case Instruction::MUL_DOUBLE:
2091 case Instruction::DIV_DOUBLE:
2092 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002093 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002094 break;
2095 case Instruction::ADD_INT_2ADDR:
2096 case Instruction::SUB_INT_2ADDR:
2097 case Instruction::MUL_INT_2ADDR:
2098 case Instruction::REM_INT_2ADDR:
2099 case Instruction::SHL_INT_2ADDR:
2100 case Instruction::SHR_INT_2ADDR:
2101 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002102 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002103 break;
2104 case Instruction::AND_INT_2ADDR:
2105 case Instruction::OR_INT_2ADDR:
2106 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002107 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002108 break;
2109 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002110 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002111 break;
2112 case Instruction::ADD_LONG_2ADDR:
2113 case Instruction::SUB_LONG_2ADDR:
2114 case Instruction::MUL_LONG_2ADDR:
2115 case Instruction::DIV_LONG_2ADDR:
2116 case Instruction::REM_LONG_2ADDR:
2117 case Instruction::AND_LONG_2ADDR:
2118 case Instruction::OR_LONG_2ADDR:
2119 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002120 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002121 break;
2122 case Instruction::SHL_LONG_2ADDR:
2123 case Instruction::SHR_LONG_2ADDR:
2124 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002125 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002126 break;
2127 case Instruction::ADD_FLOAT_2ADDR:
2128 case Instruction::SUB_FLOAT_2ADDR:
2129 case Instruction::MUL_FLOAT_2ADDR:
2130 case Instruction::DIV_FLOAT_2ADDR:
2131 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002132 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002133 break;
2134 case Instruction::ADD_DOUBLE_2ADDR:
2135 case Instruction::SUB_DOUBLE_2ADDR:
2136 case Instruction::MUL_DOUBLE_2ADDR:
2137 case Instruction::DIV_DOUBLE_2ADDR:
2138 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002139 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002140 break;
2141 case Instruction::ADD_INT_LIT16:
2142 case Instruction::RSUB_INT:
2143 case Instruction::MUL_INT_LIT16:
2144 case Instruction::DIV_INT_LIT16:
2145 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002146 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002147 break;
2148 case Instruction::AND_INT_LIT16:
2149 case Instruction::OR_INT_LIT16:
2150 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002151 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002152 break;
2153 case Instruction::ADD_INT_LIT8:
2154 case Instruction::RSUB_INT_LIT8:
2155 case Instruction::MUL_INT_LIT8:
2156 case Instruction::DIV_INT_LIT8:
2157 case Instruction::REM_INT_LIT8:
2158 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002159 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002160 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002161 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002162 break;
2163 case Instruction::AND_INT_LIT8:
2164 case Instruction::OR_INT_LIT8:
2165 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002166 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002167 break;
2168
2169 /*
2170 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002171 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002172 * inserted in the course of verification, we can expect to see it here.
2173 */
jeffhaob4df5142011-09-19 20:25:32 -07002174 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002175 break;
2176
Ian Rogersd81871c2011-10-03 13:57:23 -07002177 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002178 case Instruction::UNUSED_EE:
2179 case Instruction::UNUSED_EF:
2180 case Instruction::UNUSED_F2:
2181 case Instruction::UNUSED_F3:
2182 case Instruction::UNUSED_F4:
2183 case Instruction::UNUSED_F5:
2184 case Instruction::UNUSED_F6:
2185 case Instruction::UNUSED_F7:
2186 case Instruction::UNUSED_F8:
2187 case Instruction::UNUSED_F9:
2188 case Instruction::UNUSED_FA:
2189 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002190 case Instruction::UNUSED_F0:
2191 case Instruction::UNUSED_F1:
2192 case Instruction::UNUSED_E3:
2193 case Instruction::UNUSED_E8:
2194 case Instruction::UNUSED_E7:
2195 case Instruction::UNUSED_E4:
2196 case Instruction::UNUSED_E9:
2197 case Instruction::UNUSED_FC:
2198 case Instruction::UNUSED_E5:
2199 case Instruction::UNUSED_EA:
2200 case Instruction::UNUSED_FD:
2201 case Instruction::UNUSED_E6:
2202 case Instruction::UNUSED_EB:
2203 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002204 case Instruction::UNUSED_3E:
2205 case Instruction::UNUSED_3F:
2206 case Instruction::UNUSED_40:
2207 case Instruction::UNUSED_41:
2208 case Instruction::UNUSED_42:
2209 case Instruction::UNUSED_43:
2210 case Instruction::UNUSED_73:
2211 case Instruction::UNUSED_79:
2212 case Instruction::UNUSED_7A:
2213 case Instruction::UNUSED_EC:
2214 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002215 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002216 break;
2217
2218 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002219 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002220 * complain if an instruction is missing (which is desirable).
2221 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002222 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002223
Ian Rogers0d604842012-04-16 14:50:24 -07002224 if (have_pending_hard_failure_) {
2225 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
2226 /* immediate failure, reject class */
2227 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2228 return false;
2229 } else if (have_pending_rewrite_failure_) {
2230 /* replace opcode and continue on */
2231 std::string append("Replacing opcode ");
2232 append += inst->DumpString(dex_file_);
2233 AppendToLastFailMessage(append);
2234 ReplaceFailingInstruction();
2235 /* IMPORTANT: method->insns may have been changed */
2236 insns = code_item_->insns_ + work_insn_idx_;
2237 /* continue on as if we just handled a throw-verification-error */
2238 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002239 }
jeffhaobdb76512011-09-07 11:43:16 -07002240 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002241 * If we didn't just set the result register, clear it out. This ensures that you can only use
2242 * "move-result" immediately after the result is set. (We could check this statically, but it's
2243 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002244 */
2245 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002246 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002247 }
2248
jeffhaoa0a764a2011-09-16 10:43:38 -07002249 /* Handle "continue". Tag the next consecutive instruction. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002250 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers776ac1f2012-04-13 23:36:36 -07002251 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
Ian Rogersd81871c2011-10-03 13:57:23 -07002252 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
jeffhaod5347e02012-03-22 17:25:05 -07002253 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002254 return false;
2255 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002256 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2257 // next instruction isn't one.
jeffhaod5347e02012-03-22 17:25:05 -07002258 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002259 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002260 }
2261 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2262 if (next_line != NULL) {
2263 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2264 // needed.
2265 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002266 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002267 }
jeffhaobdb76512011-09-07 11:43:16 -07002268 } else {
2269 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002270 * We're not recording register data for the next instruction, so we don't know what the prior
2271 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002272 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002273 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002274 }
2275 }
2276
2277 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002278 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002279 *
2280 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002281 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002282 * somebody could get a reference field, check it for zero, and if the
2283 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002284 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002285 * that, and will reject the code.
2286 *
2287 * TODO: avoid re-fetching the branch target
2288 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002289 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002290 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002291 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002292 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002293 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002294 return false;
2295 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002296 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002297 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002298 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002299 }
jeffhaobdb76512011-09-07 11:43:16 -07002300 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002301 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002302 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002303 }
jeffhaobdb76512011-09-07 11:43:16 -07002304 }
2305
2306 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002307 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002308 *
2309 * We've already verified that the table is structurally sound, so we
2310 * just need to walk through and tag the targets.
2311 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002312 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002313 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2314 const uint16_t* switch_insns = insns + offset_to_switch;
2315 int switch_count = switch_insns[1];
2316 int offset_to_targets, targ;
2317
2318 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2319 /* 0 = sig, 1 = count, 2/3 = first key */
2320 offset_to_targets = 4;
2321 } else {
2322 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002323 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002324 offset_to_targets = 2 + 2 * switch_count;
2325 }
2326
2327 /* verify each switch target */
2328 for (targ = 0; targ < switch_count; targ++) {
2329 int offset;
2330 uint32_t abs_offset;
2331
2332 /* offsets are 32-bit, and only partly endian-swapped */
2333 offset = switch_insns[offset_to_targets + targ * 2] |
2334 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002335 abs_offset = work_insn_idx_ + offset;
2336 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002337 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002338 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002339 }
2340 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002341 return false;
2342 }
2343 }
2344
2345 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002346 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2347 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002348 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002349 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002350 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002351 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002352
Ian Rogers0571d352011-11-03 19:51:38 -07002353 for (; iterator.HasNext(); iterator.Next()) {
2354 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002355 within_catch_all = true;
2356 }
jeffhaobdb76512011-09-07 11:43:16 -07002357 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002358 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2359 * "work_regs", because at runtime the exception will be thrown before the instruction
2360 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002361 */
Ian Rogers0571d352011-11-03 19:51:38 -07002362 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002363 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002364 }
jeffhaobdb76512011-09-07 11:43:16 -07002365 }
2366
2367 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002368 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2369 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002370 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002371 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002372 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002373 * The state in work_line reflects the post-execution state. If the current instruction is a
2374 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002375 * it will do so before grabbing the lock).
2376 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002377 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002378 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002379 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002380 return false;
2381 }
2382 }
2383 }
2384
jeffhaod1f0fde2011-09-08 17:25:33 -07002385 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002386 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002387 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002388 return false;
2389 }
jeffhaobdb76512011-09-07 11:43:16 -07002390 }
2391
2392 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002393 * Update start_guess. Advance to the next instruction of that's
2394 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002395 * neither of those exists we're in a return or throw; leave start_guess
2396 * alone and let the caller sort it out.
2397 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002398 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002399 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002400 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002401 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002402 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002403 }
2404
Ian Rogersd81871c2011-10-03 13:57:23 -07002405 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2406 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002407
2408 return true;
2409}
2410
Ian Rogers776ac1f2012-04-13 23:36:36 -07002411const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002412 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogers0d604842012-04-16 14:50:24 -07002413 const RegType& referrer = GetDeclaringClass();
2414 Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002415 const RegType& result =
2416 klass != NULL ? reg_types_.FromClass(klass)
Ian Rogers0d604842012-04-16 14:50:24 -07002417 : reg_types_.FromDescriptor(class_loader_, descriptor);
2418 if (result.IsConflict()) {
2419 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2420 << "' in " << referrer;
jeffhaod1224c72012-02-29 13:43:08 -08002421 return result;
2422 }
Ian Rogers0d604842012-04-16 14:50:24 -07002423 if (klass == NULL && !result.IsUnresolvedTypes()) {
2424 dex_cache_->SetResolvedType(class_idx, result.GetClass());
2425 }
2426 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002427 // check at runtime if access is allowed and so pass here.
Ian Rogers0d604842012-04-16 14:50:24 -07002428 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002429 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogers0d604842012-04-16 14:50:24 -07002430 << referrer << "' -> '" << result << "'";
2431 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002432 } else {
2433 return result;
2434 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002435}
2436
Ian Rogers776ac1f2012-04-13 23:36:36 -07002437const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002438 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002439 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002440 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002441 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2442 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002443 CatchHandlerIterator iterator(handlers_ptr);
2444 for (; iterator.HasNext(); iterator.Next()) {
2445 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2446 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002447 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07002448 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002449 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002450 if (common_super == NULL) {
2451 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2452 // as that is caught at runtime
2453 common_super = &exception;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002454 } else if (!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002455 // We don't know enough about the type and the common path merge will result in
2456 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002457 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogers0d604842012-04-16 14:50:24 -07002458 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002459 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002460 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002461 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002462 common_super = &common_super->Merge(exception, &reg_types_);
2463 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002464 }
2465 }
2466 }
2467 }
Ian Rogers0571d352011-11-03 19:51:38 -07002468 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002469 }
2470 }
2471 if (common_super == NULL) {
2472 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002473 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogers0d604842012-04-16 14:50:24 -07002474 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002475 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002476 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002477}
2478
Ian Rogers0d604842012-04-16 14:50:24 -07002479Method* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx, MethodType method_type) {
2480 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002481 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogers0d604842012-04-16 14:50:24 -07002482 if (klass_type.IsConflict()) {
2483 std::string append(" in attempt to access method ");
2484 append += dex_file_->GetMethodName(method_id);
2485 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002486 return NULL;
2487 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002488 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002489 return NULL; // Can't resolve Class so no more to do here
2490 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002491 Class* klass = klass_type.GetClass();
Ian Rogers0d604842012-04-16 14:50:24 -07002492 const RegType& referrer = GetDeclaringClass();
2493 Method* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002494 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002495 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002496 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002497
2498 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002499 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002500 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002501 res_method = klass->FindInterfaceMethod(name, signature);
2502 } else {
2503 res_method = klass->FindVirtualMethod(name, signature);
2504 }
2505 if (res_method != NULL) {
Ian Rogers0d604842012-04-16 14:50:24 -07002506 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002507 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002508 // If a virtual or interface method wasn't found with the expected type, look in
2509 // the direct methods. This can happen when the wrong invoke type is used or when
2510 // a class has changed, and will be flagged as an error in later checks.
2511 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2512 res_method = klass->FindDirectMethod(name, signature);
2513 }
2514 if (res_method == NULL) {
2515 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2516 << PrettyDescriptor(klass) << "." << name
2517 << " " << signature;
2518 return NULL;
2519 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002520 }
2521 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002522 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2523 // enforce them here.
2524 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002525 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2526 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002527 return NULL;
2528 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002529 // Disallow any calls to class initializers.
2530 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002531 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2532 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002533 return NULL;
2534 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002535 // Check if access is allowed.
Ian Rogers0d604842012-04-16 14:50:24 -07002536 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002537 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogers0d604842012-04-16 14:50:24 -07002538 << " from " << referrer << ")";
jeffhao8cd6dda2012-02-22 10:15:34 -08002539 return NULL;
2540 }
jeffhaode0d9c92012-02-27 13:58:13 -08002541 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2542 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002543 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2544 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002545 return NULL;
2546 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002547 // Check that interface methods match interface classes.
2548 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2549 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2550 << " is in an interface class " << PrettyClass(klass);
2551 return NULL;
2552 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2553 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2554 << " is in a non-interface class " << PrettyClass(klass);
2555 return NULL;
2556 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002557 // See if the method type implied by the invoke instruction matches the access flags for the
2558 // target method.
2559 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2560 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2561 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2562 ) {
Ian Rogers573db4a2011-12-13 15:30:50 -08002563 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type does not match method type of "
2564 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002565 return NULL;
2566 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002567 return res_method;
2568}
2569
Ian Rogers776ac1f2012-04-13 23:36:36 -07002570Method* MethodVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
jeffhao8cd6dda2012-02-22 10:15:34 -08002571 MethodType method_type, bool is_range, bool is_super) {
2572 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2573 // we're making.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002574 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002575 if (res_method == NULL) { // error or class is unresolved
2576 return NULL;
2577 }
2578
Ian Rogersd81871c2011-10-03 13:57:23 -07002579 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2580 // has a vtable entry for the target method.
2581 if (is_super) {
2582 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogers0d604842012-04-16 14:50:24 -07002583 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
2584 Class* super_klass = super.GetClass();
2585 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
2586 if (super.IsConflict()) { // Only Object has no super class
2587 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
2588 << PrettyMethod(method_idx_, *dex_file_)
Ian Rogersd81871c2011-10-03 13:57:23 -07002589 << " to super " << PrettyMethod(res_method);
2590 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002591 MethodHelper mh(res_method);
Ian Rogers0d604842012-04-16 14:50:24 -07002592 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
2593 << PrettyMethod(method_idx_, *dex_file_)
2594 << " to super " << super
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002595 << "." << mh.GetName()
2596 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002597 }
2598 return NULL;
2599 }
2600 }
2601 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2602 // match the call to the signature. Also, we might might be calling through an abstract method
2603 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08002604 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002605 /* caught by static verifier */
2606 DCHECK(is_range || expected_args <= 5);
2607 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002608 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002609 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2610 return NULL;
2611 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002612
jeffhaobdb76512011-09-07 11:43:16 -07002613 /*
Ian Rogers0d604842012-04-16 14:50:24 -07002614 * Check the "this" argument, which must be an instance of the class that declared the method.
2615 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2616 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002617 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002618 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002619 if (!res_method->IsStatic()) {
2620 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
Ian Rogers0d604842012-04-16 14:50:24 -07002621 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002622 return NULL;
2623 }
2624 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002625 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002626 return NULL;
2627 }
2628 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07002629 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
2630 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002631 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002632 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002633 return NULL;
2634 }
2635 }
2636 actual_args++;
2637 }
2638 /*
2639 * Process the target method's signature. This signature may or may not
2640 * have been verified, so we can't assume it's properly formed.
2641 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002642 MethodHelper mh(res_method);
2643 const DexFile::TypeList* params = mh.GetParameterTypeList();
2644 size_t params_size = params == NULL ? 0 : params->Size();
2645 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002646 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002647 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002648 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2649 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002650 return NULL;
2651 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002652 const char* descriptor =
2653 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2654 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002655 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002656 << " missing signature component";
2657 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002658 }
Ian Rogers0d604842012-04-16 14:50:24 -07002659 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002660 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002661 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
2662 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002663 }
2664 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2665 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002666 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002667 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002668 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002669 return NULL;
2670 } else {
2671 return res_method;
2672 }
2673}
2674
Ian Rogers776ac1f2012-04-13 23:36:36 -07002675void MethodVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08002676 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002677 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogers0d604842012-04-16 14:50:24 -07002678 if (res_type.IsConflict()) { // bad class
2679 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002680 } else {
2681 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2682 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002683 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002684 } else if (!is_filled) {
2685 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002686 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002687 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002688 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002689 } else {
2690 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2691 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogers0d604842012-04-16 14:50:24 -07002692 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002693 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002694 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002695 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002696 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogers0d604842012-04-16 14:50:24 -07002697 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002698 return;
2699 }
2700 }
2701 // filled-array result goes into "result" register
2702 work_line_->SetResultRegisterType(res_type);
2703 }
2704 }
2705}
2706
Ian Rogers776ac1f2012-04-13 23:36:36 -07002707void MethodVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002708 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002709 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002710 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002711 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002712 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002713 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002714 if (array_type.IsZero()) {
2715 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2716 // instruction type. TODO: have a proper notion of bottom here.
2717 if (!is_primitive || insn_type.IsCategory1Types()) {
2718 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08002719 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002721 // Category 2
Elliott Hughesadb8c672012-03-06 16:49:32 -08002722 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
Ian Rogers89310de2012-02-01 13:47:30 -08002723 }
jeffhaofc3144e2012-02-01 17:21:15 -08002724 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002725 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002726 } else {
2727 /* verify the class */
Ian Rogers0d604842012-04-16 14:50:24 -07002728 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002729 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002730 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002731 << " source for aget-object";
2732 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002734 << " source for category 1 aget";
2735 } else if (is_primitive && !insn_type.Equals(component_type) &&
2736 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2737 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002738 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogersd81871c2011-10-03 13:57:23 -07002739 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002740 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002741 // Use knowledge of the field type which is stronger than the type inferred from the
2742 // instruction, which can't differentiate object types and ints from floats, longs from
2743 // doubles.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002744 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002745 }
2746 }
2747 }
2748}
2749
Ian Rogers776ac1f2012-04-13 23:36:36 -07002750void MethodVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002751 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002752 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002753 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002754 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002755 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002756 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002757 if (array_type.IsZero()) {
2758 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2759 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002760 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002761 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002762 } else {
2763 /* verify the class */
Ian Rogers0d604842012-04-16 14:50:24 -07002764 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002765 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002766 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002767 << " source for aput-object";
2768 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002769 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002770 << " source for category 1 aput";
2771 } else if (is_primitive && !insn_type.Equals(component_type) &&
2772 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2773 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002774 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002775 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002776 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002777 // The instruction agrees with the type of array, confirm the value to be stored does too
2778 // Note: we use the instruction type (rather than the component type) for aput-object as
2779 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08002780 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002781 }
2782 }
2783 }
2784}
2785
Ian Rogers776ac1f2012-04-13 23:36:36 -07002786Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002787 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2788 // Check access to class
2789 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogers0d604842012-04-16 14:50:24 -07002790 if (klass_type.IsConflict()) { // bad class
2791 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
2792 field_idx, dex_file_->GetFieldName(field_id),
2793 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002794 return NULL;
2795 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002796 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers0d604842012-04-16 14:50:24 -07002797 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08002798 }
Ian Rogers0d604842012-04-16 14:50:24 -07002799 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2800 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002801 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002802 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
2803 << dex_file_->GetFieldName(field_id) << ") in "
2804 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002805 DCHECK(Thread::Current()->IsExceptionPending());
2806 Thread::Current()->ClearException();
2807 return NULL;
Ian Rogers0d604842012-04-16 14:50:24 -07002808 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2809 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002810 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogers0d604842012-04-16 14:50:24 -07002811 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002812 return NULL;
2813 } else if (!field->IsStatic()) {
2814 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
2815 return NULL;
2816 } else {
2817 return field;
2818 }
2819}
2820
Ian Rogers776ac1f2012-04-13 23:36:36 -07002821Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002822 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2823 // Check access to class
2824 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogers0d604842012-04-16 14:50:24 -07002825 if (klass_type.IsConflict()) {
2826 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
2827 field_idx, dex_file_->GetFieldName(field_id),
2828 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002829 return NULL;
2830 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002831 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002832 return NULL; // Can't resolve Class so no more to do here
2833 }
Ian Rogers0d604842012-04-16 14:50:24 -07002834 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2835 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002836 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002837 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
2838 << dex_file_->GetFieldName(field_id) << ") in "
2839 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002840 DCHECK(Thread::Current()->IsExceptionPending());
2841 Thread::Current()->ClearException();
2842 return NULL;
Ian Rogers0d604842012-04-16 14:50:24 -07002843 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2844 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002845 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogers0d604842012-04-16 14:50:24 -07002846 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002847 return NULL;
2848 } else if (field->IsStatic()) {
2849 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
2850 << " to not be static";
2851 return NULL;
2852 } else if (obj_type.IsZero()) {
2853 // Cannot infer and check type, however, access will cause null pointer exception
2854 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07002855 } else {
Ian Rogers0d604842012-04-16 14:50:24 -07002856 const RegType& field_klass = reg_types_.FromClass(field->GetDeclaringClass());
2857 if (obj_type.IsUninitializedTypes() &&
2858 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
2859 !field_klass.Equals(GetDeclaringClass()))) {
2860 // Field accesses through uninitialized references are only allowable for constructors where
2861 // the field is declared in this class
2862 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
2863 << " of a not fully initialized object within the context of "
2864 << PrettyMethod(method_idx_, *dex_file_);
2865 return NULL;
2866 } else if (!field_klass.IsAssignableFrom(obj_type)) {
2867 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
2868 // of C1. For resolution to occur the declared class of the field must be compatible with
2869 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
2870 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
2871 << " from object of type " << obj_type;
2872 return NULL;
2873 } else {
2874 return field;
2875 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002876 }
2877}
2878
Ian Rogers776ac1f2012-04-13 23:36:36 -07002879void MethodVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002880 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002881 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002882 Field* field;
2883 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002884 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002885 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002886 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07002887 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002888 }
Ian Rogers0d604842012-04-16 14:50:24 -07002889 const char* descriptor;
2890 const ClassLoader* loader;
2891 if (field != NULL) {
2892 descriptor = FieldHelper(field).GetTypeDescriptor();
2893 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07002894 } else {
Ian Rogers0d604842012-04-16 14:50:24 -07002895 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2896 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2897 loader = class_loader_;
Ian Rogersd81871c2011-10-03 13:57:23 -07002898 }
Ian Rogers0d604842012-04-16 14:50:24 -07002899 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
2900 if (is_primitive) {
2901 if (field_type.Equals(insn_type) ||
2902 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
2903 (field_type.IsDouble() && insn_type.IsLongTypes())) {
2904 // expected that read is of the correct primitive type or that int reads are reading
2905 // floats or long reads are reading doubles
2906 } else {
2907 // This is a global failure rather than a class change failure as the instructions and
2908 // the descriptors for the type should have been consistent within the same file at
2909 // compile time
2910 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
2911 << " to be of type '" << insn_type
2912 << "' but found type '" << field_type << "' in get";
2913 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Conflict());
2914 return;
2915 }
2916 } else {
2917 if (!insn_type.IsAssignableFrom(field_type)) {
2918 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
2919 << " to be compatible with type '" << insn_type
2920 << "' but found type '" << field_type
2921 << "' in get-object";
2922 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Conflict());
2923 return;
2924 }
2925 }
2926 work_line_->SetRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002927}
2928
Ian Rogers776ac1f2012-04-13 23:36:36 -07002929void MethodVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002930 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002931 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002932 Field* field;
2933 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07002934 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002935 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002936 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07002937 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002938 }
Ian Rogers0d604842012-04-16 14:50:24 -07002939 const char* descriptor;
2940 const ClassLoader* loader;
2941 if (field != NULL) {
2942 descriptor = FieldHelper(field).GetTypeDescriptor();
2943 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07002944 } else {
Ian Rogers0d604842012-04-16 14:50:24 -07002945 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2946 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2947 loader = class_loader_;
2948 }
2949 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
2950 if (field != NULL) {
2951 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
2952 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
2953 << " from other class " << GetDeclaringClass();
2954 return;
2955 }
2956 }
2957 if (is_primitive) {
2958 // Primitive field assignability rules are weaker than regular assignability rules
2959 bool instruction_compatible;
2960 bool value_compatible;
2961 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
2962 if (field_type.IsIntegralTypes()) {
2963 instruction_compatible = insn_type.IsIntegralTypes();
2964 value_compatible = value_type.IsIntegralTypes();
2965 } else if (field_type.IsFloat()) {
2966 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
2967 value_compatible = value_type.IsFloatTypes();
2968 } else if (field_type.IsLong()) {
2969 instruction_compatible = insn_type.IsLong();
2970 value_compatible = value_type.IsLongTypes();
2971 } else if (field_type.IsDouble()) {
2972 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
2973 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07002974 } else {
Ian Rogers0d604842012-04-16 14:50:24 -07002975 instruction_compatible = false; // reference field with primitive store
2976 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07002977 }
Ian Rogers0d604842012-04-16 14:50:24 -07002978 if (!instruction_compatible) {
2979 // This is a global failure rather than a class change failure as the instructions and
2980 // the descriptors for the type should have been consistent within the same file at
2981 // compile time
2982 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
2983 << " to be of type '" << insn_type
2984 << "' but found type '" << field_type
2985 << "' in put";
2986 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07002987 }
Ian Rogers0d604842012-04-16 14:50:24 -07002988 if (!value_compatible) {
2989 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
2990 << " of type " << value_type
2991 << " but expected " << field_type
2992 << " for store to " << PrettyField(field) << " in put";
2993 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07002994 }
Ian Rogers0d604842012-04-16 14:50:24 -07002995 } else {
2996 if (!insn_type.IsAssignableFrom(field_type)) {
2997 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
2998 << " to be compatible with type '" << insn_type
2999 << "' but found type '" << field_type
3000 << "' in put-object";
3001 return;
3002 }
3003 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003004 }
3005}
3006
Ian Rogers776ac1f2012-04-13 23:36:36 -07003007bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003008 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003009 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003010 return false;
3011 }
3012 return true;
3013}
3014
Ian Rogers776ac1f2012-04-13 23:36:36 -07003015void MethodVerifier::ReplaceFailingInstruction() {
Ian Rogers0d604842012-04-16 14:50:24 -07003016 // Pop the failure and clear the need for rewriting.
3017 size_t failure_number = failures_.size();
3018 CHECK_NE(failure_number, 0U);
3019 DCHECK_EQ(failure_messages_.size(), failure_number);
3020 std::ostringstream* failure_message = failure_messages_[failure_number - 1];
3021 VerifyError failure = failures_[failure_number - 1];
3022 failures_.pop_back();
3023 failure_messages_.pop_back();
3024 have_pending_rewrite_failure_ = false;
3025
Ian Rogersf1864ef2011-12-09 12:39:48 -08003026 if (Runtime::Current()->IsStarted()) {
Ian Rogers0d604842012-04-16 14:50:24 -07003027 LOG(ERROR) << "Verification attempting to replace instructions at runtime in "
3028 << PrettyMethod(method_idx_, *dex_file_) << " " << failure_message->str();
Ian Rogersf1864ef2011-12-09 12:39:48 -08003029 return;
3030 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003031 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3032 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3033 VerifyErrorRefType ref_type;
3034 switch (inst->Opcode()) {
3035 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003036 case Instruction::CHECK_CAST:
3037 case Instruction::INSTANCE_OF:
3038 case Instruction::NEW_INSTANCE:
3039 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003040 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003041 case Instruction::FILLED_NEW_ARRAY_RANGE:
3042 ref_type = VERIFY_ERROR_REF_CLASS;
3043 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003044 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003045 case Instruction::IGET_BOOLEAN:
3046 case Instruction::IGET_BYTE:
3047 case Instruction::IGET_CHAR:
3048 case Instruction::IGET_SHORT:
3049 case Instruction::IGET_WIDE:
3050 case Instruction::IGET_OBJECT:
3051 case Instruction::IPUT:
3052 case Instruction::IPUT_BOOLEAN:
3053 case Instruction::IPUT_BYTE:
3054 case Instruction::IPUT_CHAR:
3055 case Instruction::IPUT_SHORT:
3056 case Instruction::IPUT_WIDE:
3057 case Instruction::IPUT_OBJECT:
3058 case Instruction::SGET:
3059 case Instruction::SGET_BOOLEAN:
3060 case Instruction::SGET_BYTE:
3061 case Instruction::SGET_CHAR:
3062 case Instruction::SGET_SHORT:
3063 case Instruction::SGET_WIDE:
3064 case Instruction::SGET_OBJECT:
3065 case Instruction::SPUT:
3066 case Instruction::SPUT_BOOLEAN:
3067 case Instruction::SPUT_BYTE:
3068 case Instruction::SPUT_CHAR:
3069 case Instruction::SPUT_SHORT:
3070 case Instruction::SPUT_WIDE:
3071 case Instruction::SPUT_OBJECT:
3072 ref_type = VERIFY_ERROR_REF_FIELD;
3073 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003074 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003075 case Instruction::INVOKE_VIRTUAL_RANGE:
3076 case Instruction::INVOKE_SUPER:
3077 case Instruction::INVOKE_SUPER_RANGE:
3078 case Instruction::INVOKE_DIRECT:
3079 case Instruction::INVOKE_DIRECT_RANGE:
3080 case Instruction::INVOKE_STATIC:
3081 case Instruction::INVOKE_STATIC_RANGE:
3082 case Instruction::INVOKE_INTERFACE:
3083 case Instruction::INVOKE_INTERFACE_RANGE:
3084 ref_type = VERIFY_ERROR_REF_METHOD;
3085 break;
jeffhaobdb76512011-09-07 11:43:16 -07003086 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003087 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003088 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003089 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003090 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3091 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3092 // instruction, so assert it.
3093 size_t width = inst->SizeInCodeUnits();
3094 CHECK_GT(width, 1u);
Ian Rogersf1864ef2011-12-09 12:39:48 -08003095 // If the instruction is larger than 2 code units, rewrite subsequent code unit sized chunks with
Ian Rogersd81871c2011-10-03 13:57:23 -07003096 // NOPs
3097 for (size_t i = 2; i < width; i++) {
3098 insns[work_insn_idx_ + i] = Instruction::NOP;
3099 }
3100 // Encode the opcode, with the failure code in the high byte
3101 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
Ian Rogers0d604842012-04-16 14:50:24 -07003102 (failure << 8) | // AA - component
Ian Rogersd81871c2011-10-03 13:57:23 -07003103 (ref_type << (8 + kVerifyErrorRefTypeShift));
3104 insns[work_insn_idx_] = new_instruction;
3105 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3106 // rewrote, so nothing to do here.
Ian Rogers0d604842012-04-16 14:50:24 -07003107 LOG(INFO) << "Verification error, replacing instructions in "
3108 << PrettyMethod(method_idx_, *dex_file_) << " "
3109 << failure_message->str();
Ian Rogers9fdfc182011-10-26 23:12:52 -07003110 if (gDebugVerify) {
3111 std::cout << std::endl << info_messages_.str();
3112 Dump(std::cout);
3113 }
jeffhaobdb76512011-09-07 11:43:16 -07003114}
jeffhaoba5ebb92011-08-25 17:24:37 -07003115
Ian Rogers776ac1f2012-04-13 23:36:36 -07003116bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003117 bool changed = true;
3118 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3119 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003120 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003121 * We haven't processed this instruction before, and we haven't touched the registers here, so
3122 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3123 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003124 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003125 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003126 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003127 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3128 if (gDebugVerify) {
3129 copy->CopyFromLine(target_line);
3130 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003131 changed = target_line->MergeRegisters(merge_line);
Ian Rogers0d604842012-04-16 14:50:24 -07003132 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003133 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003134 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003135 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003136 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
3137 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << std::endl
Ian Rogersd81871c2011-10-03 13:57:23 -07003138 << *copy.get() << " MERGE" << std::endl
3139 << *merge_line << " ==" << std::endl
3140 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003141 }
3142 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003143 if (changed) {
3144 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003145 }
3146 return true;
3147}
3148
Ian Rogers776ac1f2012-04-13 23:36:36 -07003149InsnFlags* MethodVerifier::CurrentInsnFlags() {
3150 return &insn_flags_[work_insn_idx_];
3151}
3152
Ian Rogers0d604842012-04-16 14:50:24 -07003153const RegType& MethodVerifier::GetMethodReturnType() {
3154 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx_);
3155 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3156 uint16_t return_type_idx = proto_id.return_type_idx_;
3157 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
3158 return reg_types_.FromDescriptor(class_loader_, descriptor);
3159}
3160
3161const RegType& MethodVerifier::GetDeclaringClass() {
3162 if (foo_method_ != NULL) {
3163 return reg_types_.FromClass(foo_method_->GetDeclaringClass());
3164 } else {
3165 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx_);
3166 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
3167 return reg_types_.FromDescriptor(class_loader_, descriptor);
3168 }
3169}
3170
Ian Rogers776ac1f2012-04-13 23:36:36 -07003171void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003172 size_t* log2_max_gc_pc) {
3173 size_t local_gc_points = 0;
3174 size_t max_insn = 0;
3175 size_t max_ref_reg = -1;
3176 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3177 if (insn_flags_[i].IsGcPoint()) {
3178 local_gc_points++;
3179 max_insn = i;
3180 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003181 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003182 }
3183 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003184 *gc_points = local_gc_points;
3185 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3186 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003187 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003188 i++;
3189 }
3190 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003191}
3192
Ian Rogers776ac1f2012-04-13 23:36:36 -07003193const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003194 size_t num_entries, ref_bitmap_bits, pc_bits;
3195 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3196 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003197 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003198 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003199 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003200 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003201 return NULL;
3202 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003203 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3204 // There are 2 bytes to encode the number of entries
3205 if (num_entries >= 65536) {
3206 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003207 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003208 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003209 return NULL;
3210 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003211 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003212 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003213 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003214 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003215 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003216 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003217 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003218 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003219 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003220 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003221 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003222 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3223 return NULL;
3224 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003225 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003226 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003227 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003228 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003229 return NULL;
3230 }
3231 // Write table header
Ian Rogers776ac1f2012-04-13 23:36:36 -07003232 table->push_back(format | ((ref_bitmap_bytes >> PcToReferenceMap::kRegMapFormatShift) &
3233 ~PcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003234 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003235 table->push_back(num_entries & 0xFF);
3236 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003237 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003238 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3239 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003240 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003241 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003242 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003243 }
3244 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003245 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003246 }
3247 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003248 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003249 return table;
3250}
jeffhaoa0a764a2011-09-16 10:43:38 -07003251
Ian Rogers776ac1f2012-04-13 23:36:36 -07003252void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003253 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3254 // that the table data is well formed and all references are marked (or not) in the bitmap
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003255 PcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003256 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003257 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003258 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3259 if (insn_flags_[i].IsGcPoint()) {
3260 CHECK_LT(map_index, map.NumEntries());
3261 CHECK_EQ(map.GetPC(map_index), i);
3262 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3263 map_index++;
3264 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003265 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003266 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003267 CHECK_LT(j / 8, map.RegWidth());
3268 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3269 } else if ((j / 8) < map.RegWidth()) {
3270 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3271 } else {
3272 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3273 }
3274 }
3275 } else {
3276 CHECK(reg_bitmap == NULL);
3277 }
3278 }
3279}
jeffhaoa0a764a2011-09-16 10:43:38 -07003280
Ian Rogers776ac1f2012-04-13 23:36:36 -07003281Mutex* MethodVerifier::gc_maps_lock_ = NULL;
3282MethodVerifier::GcMapTable* MethodVerifier::gc_maps_ = NULL;
jeffhaoa0a764a2011-09-16 10:43:38 -07003283
Ian Rogers776ac1f2012-04-13 23:36:36 -07003284void MethodVerifier::InitGcMaps() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003285 gc_maps_lock_ = new Mutex("verifier GC maps lock");
3286 MutexLock mu(*gc_maps_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07003287 gc_maps_ = new MethodVerifier::GcMapTable;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003288}
3289
Ian Rogers776ac1f2012-04-13 23:36:36 -07003290void MethodVerifier::DeleteGcMaps() {
Elliott Hughesf34f1742012-03-16 18:56:00 -07003291 {
3292 MutexLock mu(*gc_maps_lock_);
3293 STLDeleteValues(gc_maps_);
3294 delete gc_maps_;
3295 gc_maps_ = NULL;
3296 }
3297 delete gc_maps_lock_;
3298 gc_maps_lock_ = NULL;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003299}
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003300
Ian Rogers776ac1f2012-04-13 23:36:36 -07003301void MethodVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003302 MutexLock mu(*gc_maps_lock_);
Elliott Hughesa0e18062012-04-13 15:59:59 -07003303 GcMapTable::iterator it = gc_maps_->find(ref);
3304 if (it != gc_maps_->end()) {
3305 delete it->second;
3306 gc_maps_->erase(it);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003307 }
Elliott Hughesa0e18062012-04-13 15:59:59 -07003308 gc_maps_->Put(ref, &gc_map);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003309 CHECK(GetGcMap(ref) != NULL);
3310}
3311
Ian Rogers776ac1f2012-04-13 23:36:36 -07003312const std::vector<uint8_t>* MethodVerifier::GetGcMap(Compiler::MethodReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003313 MutexLock mu(*gc_maps_lock_);
3314 GcMapTable::const_iterator it = gc_maps_->find(ref);
3315 if (it == gc_maps_->end()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003316 return NULL;
3317 }
3318 CHECK(it->second != NULL);
3319 return it->second;
3320}
3321
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003322static Mutex& GetRejectedClassesLock() {
3323 static Mutex rejected_classes_lock("verifier rejected classes lock");
3324 return rejected_classes_lock;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003325}
3326
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003327static std::set<Compiler::ClassReference>& GetRejectedClasses() {
3328 static std::set<Compiler::ClassReference> rejected_classes;
3329 return rejected_classes;
3330}
jeffhaod1224c72012-02-29 13:43:08 -08003331
Ian Rogers776ac1f2012-04-13 23:36:36 -07003332void MethodVerifier::AddRejectedClass(Compiler::ClassReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003333 MutexLock mu(GetRejectedClassesLock());
3334 GetRejectedClasses().insert(ref);
jeffhaod1224c72012-02-29 13:43:08 -08003335 CHECK(IsClassRejected(ref));
3336}
3337
Ian Rogers776ac1f2012-04-13 23:36:36 -07003338bool MethodVerifier::IsClassRejected(Compiler::ClassReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003339 MutexLock mu(GetRejectedClassesLock());
3340 std::set<Compiler::ClassReference>& rejected_classes(GetRejectedClasses());
3341 return (rejected_classes.find(ref) != rejected_classes.end());
jeffhaod1224c72012-02-29 13:43:08 -08003342}
3343
Logan Chienfca7e872011-12-20 20:08:22 +08003344#if defined(ART_USE_LLVM_COMPILER)
Ian Rogers776ac1f2012-04-13 23:36:36 -07003345const InferredRegCategoryMap* MethodVerifier::GenerateInferredRegCategoryMap() {
Logan Chienfca7e872011-12-20 20:08:22 +08003346 uint32_t insns_size = code_item_->insns_size_in_code_units_;
3347 uint16_t regs_size = code_item_->registers_size_;
3348
3349 UniquePtr<InferredRegCategoryMap> table(
3350 new InferredRegCategoryMap(insns_size, regs_size));
3351
3352 for (size_t i = 0; i < insns_size; ++i) {
3353 if (RegisterLine* line = reg_table_.GetLine(i)) {
3354 for (size_t r = 0; r < regs_size; ++r) {
Logan Chiendd361c92012-04-10 23:40:37 +08003355 const RegType &rt = line->GetRegisterType(r);
Logan Chienfca7e872011-12-20 20:08:22 +08003356
3357 if (rt.IsZero()) {
3358 table->SetRegCategory(i, r, kRegZero);
3359 } else if (rt.IsCategory1Types()) {
3360 table->SetRegCategory(i, r, kRegCat1nr);
3361 } else if (rt.IsCategory2Types()) {
3362 table->SetRegCategory(i, r, kRegCat2);
3363 } else if (rt.IsReferenceTypes()) {
3364 table->SetRegCategory(i, r, kRegObject);
3365 } else {
3366 table->SetRegCategory(i, r, kRegUnknown);
3367 }
3368 }
3369 }
3370 }
3371
3372 return table.release();
3373}
Logan Chiendd361c92012-04-10 23:40:37 +08003374
Ian Rogers776ac1f2012-04-13 23:36:36 -07003375Mutex* MethodVerifier::inferred_reg_category_maps_lock_ = NULL;
3376MethodVerifier::InferredRegCategoryMapTable* MethodVerifier::inferred_reg_category_maps_ = NULL;
Logan Chiendd361c92012-04-10 23:40:37 +08003377
Ian Rogers776ac1f2012-04-13 23:36:36 -07003378void MethodVerifier::InitInferredRegCategoryMaps() {
Logan Chiendd361c92012-04-10 23:40:37 +08003379 inferred_reg_category_maps_lock_ = new Mutex("verifier GC maps lock");
3380 MutexLock mu(*inferred_reg_category_maps_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07003381 inferred_reg_category_maps_ = new MethodVerifier::InferredRegCategoryMapTable;
Logan Chiendd361c92012-04-10 23:40:37 +08003382}
3383
Ian Rogers776ac1f2012-04-13 23:36:36 -07003384void MethodVerifier::DeleteInferredRegCategoryMaps() {
Logan Chiendd361c92012-04-10 23:40:37 +08003385 {
3386 MutexLock mu(*inferred_reg_category_maps_lock_);
3387 STLDeleteValues(inferred_reg_category_maps_);
3388 delete inferred_reg_category_maps_;
3389 inferred_reg_category_maps_ = NULL;
3390 }
3391 delete inferred_reg_category_maps_lock_;
3392 inferred_reg_category_maps_lock_ = NULL;
3393}
3394
3395
Ian Rogers776ac1f2012-04-13 23:36:36 -07003396void MethodVerifier::SetInferredRegCategoryMap(Compiler::MethodReference ref,
3397 const InferredRegCategoryMap& inferred_reg_category_map) {
Logan Chiendd361c92012-04-10 23:40:37 +08003398 MutexLock mu(*inferred_reg_category_maps_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07003399 const InferredRegCategoryMap* existing_inferred_reg_category_map = GetInferredRegCategoryMap(ref);
Logan Chiendd361c92012-04-10 23:40:37 +08003400
3401 if (existing_inferred_reg_category_map != NULL) {
3402 CHECK(*existing_inferred_reg_category_map == inferred_reg_category_map);
3403 delete existing_inferred_reg_category_map;
3404 }
3405
Ian Rogers776ac1f2012-04-13 23:36:36 -07003406 inferred_reg_category_maps_->Put(ref, &inferred_reg_category_map);
Logan Chiendd361c92012-04-10 23:40:37 +08003407 CHECK(GetInferredRegCategoryMap(ref) != NULL);
3408}
3409
3410const InferredRegCategoryMap*
Ian Rogers776ac1f2012-04-13 23:36:36 -07003411MethodVerifier::GetInferredRegCategoryMap(Compiler::MethodReference ref) {
Logan Chiendd361c92012-04-10 23:40:37 +08003412 MutexLock mu(*inferred_reg_category_maps_lock_);
3413
3414 InferredRegCategoryMapTable::const_iterator it =
3415 inferred_reg_category_maps_->find(ref);
3416
3417 if (it == inferred_reg_category_maps_->end()) {
3418 return NULL;
3419 }
3420 CHECK(it->second != NULL);
3421 return it->second;
3422}
Logan Chienfca7e872011-12-20 20:08:22 +08003423#endif
3424
Ian Rogersd81871c2011-10-03 13:57:23 -07003425} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003426} // namespace art