blob: 18078d2c8b0b84bdd9ca2177741bca3bddfa8bb4 [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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-04-16 14:50:24 -0700252 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800253 }
254 it.Next();
255 }
Ian Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-04-16 14:50:24 -0700287void MethodVerifier::VerifyMethodAndDump(Method* method) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800288 CHECK(method != NULL);
Ian Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-04-16 14:50:24 -0700381 have_pending_hard_failure_ = true;
382 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800383 }
384 }
Ian Rogersad0b3a32012-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 ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700482 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800483 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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-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 Rogersad0b3a32012-04-16 14:50:24 -07001496 if (res_type.IsConflict()) {
1497 DCHECK_NE(failures_.size(), 0U);
1498 if (!is_checkcast) {
1499 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
1500 }
1501 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001502 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001503 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1504 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001505 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001506 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001507 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001508 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001509 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07001510 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001511 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001512 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001513 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001514 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001515 }
jeffhaobdb76512011-09-07 11:43:16 -07001516 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001517 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001518 }
1519 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001520 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001521 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001522 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001523 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001524 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001525 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001526 }
1527 }
1528 break;
1529 }
1530 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001531 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001532 if (res_type.IsConflict()) {
1533 DCHECK_NE(failures_.size(), 0U);
1534 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001535 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001536 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1537 // can't create an instance of an interface or abstract class */
1538 if (!res_type.IsInstantiableTypes()) {
1539 Fail(VERIFY_ERROR_INSTANTIATION)
1540 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001541 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001542 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1543 // Any registers holding previous allocations from this address that have not yet been
1544 // initialized must be marked invalid.
1545 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1546 // add the new uninitialized reference to the register state
Elliott Hughesadb8c672012-03-06 16:49:32 -08001547 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001548 }
1549 break;
1550 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001551 case Instruction::NEW_ARRAY:
1552 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001553 break;
1554 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08001555 VerifyNewArray(dec_insn, true, false);
1556 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001557 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001558 case Instruction::FILLED_NEW_ARRAY_RANGE:
1559 VerifyNewArray(dec_insn, true, true);
1560 just_set_result = true; // Filled new array range sets result register
1561 break;
jeffhaobdb76512011-09-07 11:43:16 -07001562 case Instruction::CMPL_FLOAT:
1563 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001564 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001565 break;
1566 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001567 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001568 break;
1569 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001570 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001571 break;
1572 case Instruction::CMPL_DOUBLE:
1573 case Instruction::CMPG_DOUBLE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001574 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08001575 break;
1576 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001577 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08001578 break;
1579 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001580 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001581 break;
1582 case Instruction::CMP_LONG:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001583 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08001584 break;
1585 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001586 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08001587 break;
1588 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001589 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001590 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001591 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001592 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001593 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001594 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001595 }
1596 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001597 }
jeffhaobdb76512011-09-07 11:43:16 -07001598 case Instruction::GOTO:
1599 case Instruction::GOTO_16:
1600 case Instruction::GOTO_32:
1601 /* no effect on or use of registers */
1602 break;
1603
1604 case Instruction::PACKED_SWITCH:
1605 case Instruction::SPARSE_SWITCH:
1606 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001607 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001608 break;
1609
Ian Rogersd81871c2011-10-03 13:57:23 -07001610 case Instruction::FILL_ARRAY_DATA: {
1611 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001612 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08001613 /* array_type can be null if the reg type is Zero */
1614 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001615 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001616 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001617 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001618 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1619 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001620 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001621 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1622 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001623 } else {
jeffhao457cc512012-02-02 16:55:13 -08001624 // Now verify if the element width in the table matches the element width declared in
1625 // the array
1626 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1627 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001628 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001629 } else {
1630 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1631 // Since we don't compress the data in Dex, expect to see equal width of data stored
1632 // in the table and expected from the array class.
1633 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001634 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1635 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001636 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001637 }
1638 }
jeffhaobdb76512011-09-07 11:43:16 -07001639 }
1640 }
1641 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001642 }
jeffhaobdb76512011-09-07 11:43:16 -07001643 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001644 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001645 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1646 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001647 bool mismatch = false;
1648 if (reg_type1.IsZero()) { // zero then integral or reference expected
1649 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1650 } else if (reg_type1.IsReferenceTypes()) { // both references?
1651 mismatch = !reg_type2.IsReferenceTypes();
1652 } else { // both integral?
1653 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1654 }
1655 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001656 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1657 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001658 }
1659 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001660 }
jeffhaobdb76512011-09-07 11:43:16 -07001661 case Instruction::IF_LT:
1662 case Instruction::IF_GE:
1663 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001664 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001665 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1666 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001667 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001668 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1669 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001670 }
1671 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001672 }
jeffhaobdb76512011-09-07 11:43:16 -07001673 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001674 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001675 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001676 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001677 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001678 }
jeffhaobdb76512011-09-07 11:43:16 -07001679 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001680 }
jeffhaobdb76512011-09-07 11:43:16 -07001681 case Instruction::IF_LTZ:
1682 case Instruction::IF_GEZ:
1683 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001684 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001685 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001686 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001687 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1688 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001689 }
jeffhaobdb76512011-09-07 11:43:16 -07001690 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001691 }
jeffhaobdb76512011-09-07 11:43:16 -07001692 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001693 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
1694 break;
jeffhaobdb76512011-09-07 11:43:16 -07001695 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001696 VerifyAGet(dec_insn, reg_types_.Byte(), true);
1697 break;
jeffhaobdb76512011-09-07 11:43:16 -07001698 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07001699 VerifyAGet(dec_insn, reg_types_.Char(), true);
1700 break;
jeffhaobdb76512011-09-07 11:43:16 -07001701 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001702 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001703 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001704 case Instruction::AGET:
1705 VerifyAGet(dec_insn, reg_types_.Integer(), true);
1706 break;
jeffhaobdb76512011-09-07 11:43:16 -07001707 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001708 VerifyAGet(dec_insn, reg_types_.Long(), true);
1709 break;
1710 case Instruction::AGET_OBJECT:
1711 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001712 break;
1713
Ian Rogersd81871c2011-10-03 13:57:23 -07001714 case Instruction::APUT_BOOLEAN:
1715 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
1716 break;
1717 case Instruction::APUT_BYTE:
1718 VerifyAPut(dec_insn, reg_types_.Byte(), true);
1719 break;
1720 case Instruction::APUT_CHAR:
1721 VerifyAPut(dec_insn, reg_types_.Char(), true);
1722 break;
1723 case Instruction::APUT_SHORT:
1724 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001725 break;
1726 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001727 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001728 break;
1729 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001730 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001731 break;
1732 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001733 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001734 break;
1735
jeffhaobdb76512011-09-07 11:43:16 -07001736 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001737 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001738 break;
jeffhaobdb76512011-09-07 11:43:16 -07001739 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001740 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001741 break;
jeffhaobdb76512011-09-07 11:43:16 -07001742 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001743 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001744 break;
jeffhaobdb76512011-09-07 11:43:16 -07001745 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001746 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001747 break;
1748 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001749 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001750 break;
1751 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001752 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001753 break;
1754 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001755 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001756 break;
jeffhaobdb76512011-09-07 11:43:16 -07001757
Ian Rogersd81871c2011-10-03 13:57:23 -07001758 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001759 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001760 break;
1761 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001762 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001763 break;
1764 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001765 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001766 break;
1767 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001768 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001769 break;
1770 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001771 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001772 break;
1773 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001774 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001775 break;
jeffhaobdb76512011-09-07 11:43:16 -07001776 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001777 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001778 break;
1779
jeffhaobdb76512011-09-07 11:43:16 -07001780 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001781 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001782 break;
jeffhaobdb76512011-09-07 11:43:16 -07001783 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001784 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001785 break;
jeffhaobdb76512011-09-07 11:43:16 -07001786 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001787 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001788 break;
jeffhaobdb76512011-09-07 11:43:16 -07001789 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001790 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001791 break;
1792 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001793 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001794 break;
1795 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001796 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001797 break;
1798 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001799 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001800 break;
1801
1802 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001803 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001804 break;
1805 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001806 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001807 break;
1808 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001809 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001810 break;
1811 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001812 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001813 break;
1814 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001815 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001816 break;
1817 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001818 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001819 break;
1820 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001821 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001822 break;
1823
1824 case Instruction::INVOKE_VIRTUAL:
1825 case Instruction::INVOKE_VIRTUAL_RANGE:
1826 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001827 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001828 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
1829 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
1830 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
1831 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001832 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001833 const char* descriptor;
1834 if (called_method == NULL) {
1835 uint32_t method_idx = dec_insn.vB;
1836 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1837 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1838 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1839 } else {
1840 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07001841 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001842 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
1843 work_line_->SetResultRegisterType(return_type);
1844 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001845 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001846 }
jeffhaobdb76512011-09-07 11:43:16 -07001847 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001848 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001849 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001850 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001851 if (called_method != NULL) {
jeffhaobdb76512011-09-07 11:43:16 -07001852 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001853 * Some additional checks when calling a constructor. We know from the invocation arg check
1854 * that the "this" argument is an instance of called_method->klass. Now we further restrict
1855 * that to require that called_method->klass is the same as this->klass or this->super,
1856 * allowing the latter only if the "this" argument is the same as the "this" argument to
1857 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07001858 */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001859 if (called_method->IsConstructor()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001860 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001861 if (this_type.IsConflict()) // failure.
jeffhaobdb76512011-09-07 11:43:16 -07001862 break;
1863
1864 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07001865 if (this_type.IsZero()) {
jeffhaod5347e02012-03-22 17:25:05 -07001866 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07001867 break;
1868 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001869 if (called_method != NULL) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001870 /* must be in same class or in superclass */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001871 const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
1872 if (this_super_klass.IsConflict()) {
1873 // Unknown super class, fail so we re-check at runtime.
1874 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
1875 break;
1876 } else {
1877 if (!this_super_klass.IsZero() &&
1878 called_method->GetDeclaringClass() == this_super_klass.GetClass()) {
1879 if (this_type.GetClass() != GetDeclaringClass().GetClass()) {
1880 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1881 << "invoke-direct <init> on super only allowed for 'this' in <init>"
1882 << " (this class '" << this_type << "', called class '"
1883 << PrettyDescriptor(called_method->GetDeclaringClass()) << "')";
1884 break;
1885 }
1886 } else if (this_type.GetClass() != called_method->GetDeclaringClass()) {
jeffhaod5347e02012-03-22 17:25:05 -07001887 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersad0b3a32012-04-16 14:50:24 -07001888 << "invoke-direct <init> must be on current class or super"
1889 << " (current class '" << this_type << "', called class '"
1890 << PrettyDescriptor(called_method->GetDeclaringClass()) << "')";
Ian Rogers28ad40d2011-10-27 15:19:26 -07001891 break;
1892 }
jeffhaobdb76512011-09-07 11:43:16 -07001893 }
jeffhaobdb76512011-09-07 11:43:16 -07001894 }
1895
1896 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07001897 if (!this_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001898 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
Ian Rogersd81871c2011-10-03 13:57:23 -07001899 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07001900 break;
1901 }
1902
1903 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07001904 * Replace the uninitialized reference with an initialized one. We need to do this for all
1905 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07001906 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001907 work_line_->MarkRefsAsInitialized(this_type);
jeffhao2a8a90e2011-09-26 14:25:31 -07001908 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001909 }
1910 const char* descriptor;
1911 if (called_method == NULL) {
1912 uint32_t method_idx = dec_insn.vB;
1913 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1914 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1915 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1916 } else {
1917 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
1918 }
1919 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
1920 work_line_->SetResultRegisterType(return_type);
1921 just_set_result = true;
1922 break;
1923 }
1924 case Instruction::INVOKE_STATIC:
1925 case Instruction::INVOKE_STATIC_RANGE: {
1926 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
1927 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001928 const char* descriptor;
1929 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001930 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001931 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1932 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07001933 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001934 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001935 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001936 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001937 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07001938 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07001939 just_set_result = true;
1940 }
1941 break;
jeffhaobdb76512011-09-07 11:43:16 -07001942 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001943 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001944 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001945 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001946 if (abs_method != NULL) {
1947 Class* called_interface = abs_method->GetDeclaringClass();
1948 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
1949 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
1950 << PrettyMethod(abs_method) << "'";
1951 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001952 }
Ian Rogers0d604842012-04-16 14:50:24 -07001953 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001954 /* Get the type of the "this" arg, which should either be a sub-interface of called
1955 * interface or Object (see comments in RegType::JoinClass).
1956 */
1957 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1958 if (this_type.IsZero()) {
1959 /* null pointer always passes (and always fails at runtime) */
1960 } else {
1961 if (this_type.IsUninitializedTypes()) {
1962 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
1963 << this_type;
1964 break;
1965 }
1966 // In the past we have tried to assert that "called_interface" is assignable
1967 // from "this_type.GetClass()", however, as we do an imprecise Join
1968 // (RegType::JoinClass) we don't have full information on what interfaces are
1969 // implemented by "this_type". For example, two classes may implement the same
1970 // interfaces and have a common parent that doesn't implement the interface. The
1971 // join will set "this_type" to the parent class and a test that this implements
1972 // the interface will incorrectly fail.
1973 }
1974 /*
1975 * We don't have an object instance, so we can't find the concrete method. However, all of
1976 * the type information is in the abstract method, so we're good.
1977 */
1978 const char* descriptor;
1979 if (abs_method == NULL) {
1980 uint32_t method_idx = dec_insn.vB;
1981 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1982 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1983 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1984 } else {
1985 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
1986 }
1987 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
1988 work_line_->SetResultRegisterType(return_type);
1989 work_line_->SetResultRegisterType(return_type);
1990 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001991 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001992 }
jeffhaobdb76512011-09-07 11:43:16 -07001993 case Instruction::NEG_INT:
1994 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001995 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001996 break;
1997 case Instruction::NEG_LONG:
1998 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07001999 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002000 break;
2001 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002002 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002003 break;
2004 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002005 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002006 break;
2007 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002008 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002009 break;
2010 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002011 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002012 break;
2013 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002014 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002015 break;
2016 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002017 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002018 break;
2019 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002020 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002021 break;
2022 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002023 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002024 break;
2025 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002026 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002027 break;
2028 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002029 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002030 break;
2031 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002032 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002033 break;
2034 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002035 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002036 break;
2037 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002038 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002039 break;
2040 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002041 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002042 break;
2043 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002044 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002045 break;
2046 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002047 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002048 break;
2049 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002050 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002051 break;
2052
2053 case Instruction::ADD_INT:
2054 case Instruction::SUB_INT:
2055 case Instruction::MUL_INT:
2056 case Instruction::REM_INT:
2057 case Instruction::DIV_INT:
2058 case Instruction::SHL_INT:
2059 case Instruction::SHR_INT:
2060 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002061 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002062 break;
2063 case Instruction::AND_INT:
2064 case Instruction::OR_INT:
2065 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002066 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002067 break;
2068 case Instruction::ADD_LONG:
2069 case Instruction::SUB_LONG:
2070 case Instruction::MUL_LONG:
2071 case Instruction::DIV_LONG:
2072 case Instruction::REM_LONG:
2073 case Instruction::AND_LONG:
2074 case Instruction::OR_LONG:
2075 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002076 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002077 break;
2078 case Instruction::SHL_LONG:
2079 case Instruction::SHR_LONG:
2080 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002081 /* shift distance is Int, making these different from other binary operations */
2082 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002083 break;
2084 case Instruction::ADD_FLOAT:
2085 case Instruction::SUB_FLOAT:
2086 case Instruction::MUL_FLOAT:
2087 case Instruction::DIV_FLOAT:
2088 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002089 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002090 break;
2091 case Instruction::ADD_DOUBLE:
2092 case Instruction::SUB_DOUBLE:
2093 case Instruction::MUL_DOUBLE:
2094 case Instruction::DIV_DOUBLE:
2095 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002096 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002097 break;
2098 case Instruction::ADD_INT_2ADDR:
2099 case Instruction::SUB_INT_2ADDR:
2100 case Instruction::MUL_INT_2ADDR:
2101 case Instruction::REM_INT_2ADDR:
2102 case Instruction::SHL_INT_2ADDR:
2103 case Instruction::SHR_INT_2ADDR:
2104 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002105 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002106 break;
2107 case Instruction::AND_INT_2ADDR:
2108 case Instruction::OR_INT_2ADDR:
2109 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002110 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002111 break;
2112 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002113 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002114 break;
2115 case Instruction::ADD_LONG_2ADDR:
2116 case Instruction::SUB_LONG_2ADDR:
2117 case Instruction::MUL_LONG_2ADDR:
2118 case Instruction::DIV_LONG_2ADDR:
2119 case Instruction::REM_LONG_2ADDR:
2120 case Instruction::AND_LONG_2ADDR:
2121 case Instruction::OR_LONG_2ADDR:
2122 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002123 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002124 break;
2125 case Instruction::SHL_LONG_2ADDR:
2126 case Instruction::SHR_LONG_2ADDR:
2127 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002128 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002129 break;
2130 case Instruction::ADD_FLOAT_2ADDR:
2131 case Instruction::SUB_FLOAT_2ADDR:
2132 case Instruction::MUL_FLOAT_2ADDR:
2133 case Instruction::DIV_FLOAT_2ADDR:
2134 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002135 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002136 break;
2137 case Instruction::ADD_DOUBLE_2ADDR:
2138 case Instruction::SUB_DOUBLE_2ADDR:
2139 case Instruction::MUL_DOUBLE_2ADDR:
2140 case Instruction::DIV_DOUBLE_2ADDR:
2141 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002142 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002143 break;
2144 case Instruction::ADD_INT_LIT16:
2145 case Instruction::RSUB_INT:
2146 case Instruction::MUL_INT_LIT16:
2147 case Instruction::DIV_INT_LIT16:
2148 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002149 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002150 break;
2151 case Instruction::AND_INT_LIT16:
2152 case Instruction::OR_INT_LIT16:
2153 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002154 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002155 break;
2156 case Instruction::ADD_INT_LIT8:
2157 case Instruction::RSUB_INT_LIT8:
2158 case Instruction::MUL_INT_LIT8:
2159 case Instruction::DIV_INT_LIT8:
2160 case Instruction::REM_INT_LIT8:
2161 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002162 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002163 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002164 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002165 break;
2166 case Instruction::AND_INT_LIT8:
2167 case Instruction::OR_INT_LIT8:
2168 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002169 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002170 break;
2171
2172 /*
2173 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002174 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002175 * inserted in the course of verification, we can expect to see it here.
2176 */
jeffhaob4df5142011-09-19 20:25:32 -07002177 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002178 break;
2179
Ian Rogersd81871c2011-10-03 13:57:23 -07002180 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002181 case Instruction::UNUSED_EE:
2182 case Instruction::UNUSED_EF:
2183 case Instruction::UNUSED_F2:
2184 case Instruction::UNUSED_F3:
2185 case Instruction::UNUSED_F4:
2186 case Instruction::UNUSED_F5:
2187 case Instruction::UNUSED_F6:
2188 case Instruction::UNUSED_F7:
2189 case Instruction::UNUSED_F8:
2190 case Instruction::UNUSED_F9:
2191 case Instruction::UNUSED_FA:
2192 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002193 case Instruction::UNUSED_F0:
2194 case Instruction::UNUSED_F1:
2195 case Instruction::UNUSED_E3:
2196 case Instruction::UNUSED_E8:
2197 case Instruction::UNUSED_E7:
2198 case Instruction::UNUSED_E4:
2199 case Instruction::UNUSED_E9:
2200 case Instruction::UNUSED_FC:
2201 case Instruction::UNUSED_E5:
2202 case Instruction::UNUSED_EA:
2203 case Instruction::UNUSED_FD:
2204 case Instruction::UNUSED_E6:
2205 case Instruction::UNUSED_EB:
2206 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002207 case Instruction::UNUSED_3E:
2208 case Instruction::UNUSED_3F:
2209 case Instruction::UNUSED_40:
2210 case Instruction::UNUSED_41:
2211 case Instruction::UNUSED_42:
2212 case Instruction::UNUSED_43:
2213 case Instruction::UNUSED_73:
2214 case Instruction::UNUSED_79:
2215 case Instruction::UNUSED_7A:
2216 case Instruction::UNUSED_EC:
2217 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002218 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002219 break;
2220
2221 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002222 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002223 * complain if an instruction is missing (which is desirable).
2224 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002225 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002226
Ian Rogersad0b3a32012-04-16 14:50:24 -07002227 if (have_pending_hard_failure_) {
2228 if (!Runtime::Current()->IsStarted()) {
2229 /* When compiling, check that the first failure is a hard failure */
2230 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002231 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002232 /* immediate failure, reject class */
2233 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2234 return false;
2235 } else if (have_pending_rewrite_failure_) {
2236 /* replace opcode and continue on */
2237 std::string append("Replacing opcode ");
2238 append += inst->DumpString(dex_file_);
2239 AppendToLastFailMessage(append);
2240 ReplaceFailingInstruction();
2241 /* IMPORTANT: method->insns may have been changed */
2242 insns = code_item_->insns_ + work_insn_idx_;
2243 /* continue on as if we just handled a throw-verification-error */
2244 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002245 }
jeffhaobdb76512011-09-07 11:43:16 -07002246 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002247 * If we didn't just set the result register, clear it out. This ensures that you can only use
2248 * "move-result" immediately after the result is set. (We could check this statically, but it's
2249 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002250 */
2251 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002252 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002253 }
2254
jeffhaoa0a764a2011-09-16 10:43:38 -07002255 /* Handle "continue". Tag the next consecutive instruction. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002256 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers776ac1f2012-04-13 23:36:36 -07002257 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
Ian Rogersd81871c2011-10-03 13:57:23 -07002258 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
jeffhaod5347e02012-03-22 17:25:05 -07002259 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002260 return false;
2261 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002262 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2263 // next instruction isn't one.
jeffhaod5347e02012-03-22 17:25:05 -07002264 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002265 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002266 }
2267 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2268 if (next_line != NULL) {
2269 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2270 // needed.
2271 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002272 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002273 }
jeffhaobdb76512011-09-07 11:43:16 -07002274 } else {
2275 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002276 * We're not recording register data for the next instruction, so we don't know what the prior
2277 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002278 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002279 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002280 }
2281 }
2282
2283 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002284 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002285 *
2286 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002287 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002288 * somebody could get a reference field, check it for zero, and if the
2289 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002290 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002291 * that, and will reject the code.
2292 *
2293 * TODO: avoid re-fetching the branch target
2294 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002295 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002296 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002297 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002298 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002299 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002300 return false;
2301 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002302 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002303 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002304 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002305 }
jeffhaobdb76512011-09-07 11:43:16 -07002306 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002307 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002308 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002309 }
jeffhaobdb76512011-09-07 11:43:16 -07002310 }
2311
2312 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002313 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002314 *
2315 * We've already verified that the table is structurally sound, so we
2316 * just need to walk through and tag the targets.
2317 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002318 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002319 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2320 const uint16_t* switch_insns = insns + offset_to_switch;
2321 int switch_count = switch_insns[1];
2322 int offset_to_targets, targ;
2323
2324 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2325 /* 0 = sig, 1 = count, 2/3 = first key */
2326 offset_to_targets = 4;
2327 } else {
2328 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002329 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002330 offset_to_targets = 2 + 2 * switch_count;
2331 }
2332
2333 /* verify each switch target */
2334 for (targ = 0; targ < switch_count; targ++) {
2335 int offset;
2336 uint32_t abs_offset;
2337
2338 /* offsets are 32-bit, and only partly endian-swapped */
2339 offset = switch_insns[offset_to_targets + targ * 2] |
2340 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002341 abs_offset = work_insn_idx_ + offset;
2342 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002343 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002344 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002345 }
2346 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002347 return false;
2348 }
2349 }
2350
2351 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002352 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2353 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002354 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002355 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002356 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002357 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002358
Ian Rogers0571d352011-11-03 19:51:38 -07002359 for (; iterator.HasNext(); iterator.Next()) {
2360 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002361 within_catch_all = true;
2362 }
jeffhaobdb76512011-09-07 11:43:16 -07002363 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002364 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2365 * "work_regs", because at runtime the exception will be thrown before the instruction
2366 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002367 */
Ian Rogers0571d352011-11-03 19:51:38 -07002368 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002369 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002370 }
jeffhaobdb76512011-09-07 11:43:16 -07002371 }
2372
2373 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002374 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2375 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002376 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002377 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002378 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002379 * The state in work_line reflects the post-execution state. If the current instruction is a
2380 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002381 * it will do so before grabbing the lock).
2382 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002383 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002384 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002385 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002386 return false;
2387 }
2388 }
2389 }
2390
jeffhaod1f0fde2011-09-08 17:25:33 -07002391 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002392 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002393 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002394 return false;
2395 }
jeffhaobdb76512011-09-07 11:43:16 -07002396 }
2397
2398 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002399 * Update start_guess. Advance to the next instruction of that's
2400 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002401 * neither of those exists we're in a return or throw; leave start_guess
2402 * alone and let the caller sort it out.
2403 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002404 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002405 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002406 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002407 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002408 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002409 }
2410
Ian Rogersd81871c2011-10-03 13:57:23 -07002411 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2412 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002413
2414 return true;
2415}
2416
Ian Rogers776ac1f2012-04-13 23:36:36 -07002417const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002418 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002419 const RegType& referrer = GetDeclaringClass();
2420 Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002421 const RegType& result =
2422 klass != NULL ? reg_types_.FromClass(klass)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002423 : reg_types_.FromDescriptor(class_loader_, descriptor);
2424 if (result.IsConflict()) {
2425 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2426 << "' in " << referrer;
2427 return result;
2428 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002429 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002430 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002431 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002432 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002433 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002434 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002435 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002436 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002437 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002438 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002439}
2440
Ian Rogers776ac1f2012-04-13 23:36:36 -07002441const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002442 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002443 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002444 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002445 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2446 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002447 CatchHandlerIterator iterator(handlers_ptr);
2448 for (; iterator.HasNext(); iterator.Next()) {
2449 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2450 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002451 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07002452 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002453 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002454 if (common_super == NULL) {
2455 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2456 // as that is caught at runtime
2457 common_super = &exception;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002458 } else if (!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002459 // We don't know enough about the type and the common path merge will result in
2460 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002461 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002462 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002463 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002464 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002465 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002466 common_super = &common_super->Merge(exception, &reg_types_);
2467 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002468 }
2469 }
2470 }
2471 }
Ian Rogers0571d352011-11-03 19:51:38 -07002472 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002473 }
2474 }
2475 if (common_super == NULL) {
2476 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002477 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002478 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002479 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002480 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002481}
2482
Ian Rogersad0b3a32012-04-16 14:50:24 -07002483Method* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx, MethodType method_type) {
2484 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002485 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002486 if (klass_type.IsConflict()) {
2487 std::string append(" in attempt to access method ");
2488 append += dex_file_->GetMethodName(method_id);
2489 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002490 return NULL;
2491 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002492 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002493 return NULL; // Can't resolve Class so no more to do here
2494 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002495 Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002496 const RegType& referrer = GetDeclaringClass();
2497 Method* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002498 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002499 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002500 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002501
2502 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002503 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002504 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002505 res_method = klass->FindInterfaceMethod(name, signature);
2506 } else {
2507 res_method = klass->FindVirtualMethod(name, signature);
2508 }
2509 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002510 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002511 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002512 // If a virtual or interface method wasn't found with the expected type, look in
2513 // the direct methods. This can happen when the wrong invoke type is used or when
2514 // a class has changed, and will be flagged as an error in later checks.
2515 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2516 res_method = klass->FindDirectMethod(name, signature);
2517 }
2518 if (res_method == NULL) {
2519 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2520 << PrettyDescriptor(klass) << "." << name
2521 << " " << signature;
2522 return NULL;
2523 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002524 }
2525 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002526 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2527 // enforce them here.
2528 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002529 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2530 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002531 return NULL;
2532 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002533 // Disallow any calls to class initializers.
2534 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002535 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2536 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002537 return NULL;
2538 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002539 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002540 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002541 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002542 << " from " << referrer << ")";
jeffhao8cd6dda2012-02-22 10:15:34 -08002543 return NULL;
2544 }
jeffhaode0d9c92012-02-27 13:58:13 -08002545 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2546 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002547 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2548 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002549 return NULL;
2550 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002551 // Check that interface methods match interface classes.
2552 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2553 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2554 << " is in an interface class " << PrettyClass(klass);
2555 return NULL;
2556 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2557 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2558 << " is in a non-interface class " << PrettyClass(klass);
2559 return NULL;
2560 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002561 // See if the method type implied by the invoke instruction matches the access flags for the
2562 // target method.
2563 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2564 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2565 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2566 ) {
Ian Rogers573db4a2011-12-13 15:30:50 -08002567 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type does not match method type of "
2568 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002569 return NULL;
2570 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002571 return res_method;
2572}
2573
Ian Rogers776ac1f2012-04-13 23:36:36 -07002574Method* MethodVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
jeffhao8cd6dda2012-02-22 10:15:34 -08002575 MethodType method_type, bool is_range, bool is_super) {
2576 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2577 // we're making.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002578 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002579 if (res_method == NULL) { // error or class is unresolved
2580 return NULL;
2581 }
2582
Ian Rogersd81871c2011-10-03 13:57:23 -07002583 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2584 // has a vtable entry for the target method.
2585 if (is_super) {
2586 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002587 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
jeffhao4d8df822012-04-24 17:09:36 -07002588 if (super.IsConflict()) { // unknown super class
2589 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
2590 << PrettyMethod(method_idx_, *dex_file_)
2591 << " to super " << PrettyMethod(res_method);
2592 return NULL;
2593 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002594 Class* super_klass = super.GetClass();
2595 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002596 MethodHelper mh(res_method);
2597 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
2598 << PrettyMethod(method_idx_, *dex_file_)
2599 << " to super " << super
2600 << "." << mh.GetName()
2601 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002602 return NULL;
2603 }
2604 }
2605 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2606 // match the call to the signature. Also, we might might be calling through an abstract method
2607 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08002608 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002609 /* caught by static verifier */
2610 DCHECK(is_range || expected_args <= 5);
2611 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002612 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002613 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2614 return NULL;
2615 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002616
jeffhaobdb76512011-09-07 11:43:16 -07002617 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002618 * Check the "this" argument, which must be an instance of the class that declared the method.
2619 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2620 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002621 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002622 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002623 if (!res_method->IsStatic()) {
2624 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002625 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002626 return NULL;
2627 }
2628 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002629 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002630 return NULL;
2631 }
2632 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07002633 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
2634 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002635 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002636 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002637 return NULL;
2638 }
2639 }
2640 actual_args++;
2641 }
2642 /*
2643 * Process the target method's signature. This signature may or may not
2644 * have been verified, so we can't assume it's properly formed.
2645 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002646 MethodHelper mh(res_method);
2647 const DexFile::TypeList* params = mh.GetParameterTypeList();
2648 size_t params_size = params == NULL ? 0 : params->Size();
2649 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002650 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002651 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002652 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2653 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002654 return NULL;
2655 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002656 const char* descriptor =
2657 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2658 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002659 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002660 << " missing signature component";
2661 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002662 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002663 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002664 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002665 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
2666 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002667 }
2668 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2669 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002670 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002671 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002672 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002673 return NULL;
2674 } else {
2675 return res_method;
2676 }
2677}
2678
Ian Rogers776ac1f2012-04-13 23:36:36 -07002679void MethodVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08002680 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002681 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002682 if (res_type.IsConflict()) { // bad class
2683 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002684 } else {
2685 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2686 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002687 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002688 } else if (!is_filled) {
2689 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002690 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002691 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002692 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002693 } else {
2694 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2695 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002696 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002697 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002698 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002699 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002700 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002701 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002702 return;
2703 }
2704 }
2705 // filled-array result goes into "result" register
2706 work_line_->SetResultRegisterType(res_type);
2707 }
2708 }
2709}
2710
Ian Rogers776ac1f2012-04-13 23:36:36 -07002711void MethodVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002712 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002713 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002714 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002715 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002716 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002717 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002718 if (array_type.IsZero()) {
2719 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2720 // instruction type. TODO: have a proper notion of bottom here.
2721 if (!is_primitive || insn_type.IsCategory1Types()) {
2722 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08002723 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002724 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002725 // Category 2
Elliott Hughesadb8c672012-03-06 16:49:32 -08002726 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
Ian Rogers89310de2012-02-01 13:47:30 -08002727 }
jeffhaofc3144e2012-02-01 17:21:15 -08002728 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002729 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002730 } else {
2731 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002732 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002733 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002735 << " source for aget-object";
2736 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002737 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002738 << " source for category 1 aget";
2739 } else if (is_primitive && !insn_type.Equals(component_type) &&
2740 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2741 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002742 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogersd81871c2011-10-03 13:57:23 -07002743 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002744 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002745 // Use knowledge of the field type which is stronger than the type inferred from the
2746 // instruction, which can't differentiate object types and ints from floats, longs from
2747 // doubles.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002748 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002749 }
2750 }
2751 }
2752}
2753
Ian Rogers776ac1f2012-04-13 23:36:36 -07002754void MethodVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002755 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002756 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002757 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002758 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002759 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002760 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002761 if (array_type.IsZero()) {
2762 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2763 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002764 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002765 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002766 } else {
2767 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002768 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002769 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002770 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002771 << " source for aput-object";
2772 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002773 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002774 << " source for category 1 aput";
2775 } else if (is_primitive && !insn_type.Equals(component_type) &&
2776 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2777 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002778 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002779 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002780 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002781 // The instruction agrees with the type of array, confirm the value to be stored does too
2782 // Note: we use the instruction type (rather than the component type) for aput-object as
2783 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08002784 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002785 }
2786 }
2787 }
2788}
2789
Ian Rogers776ac1f2012-04-13 23:36:36 -07002790Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002791 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2792 // Check access to class
2793 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002794 if (klass_type.IsConflict()) { // bad class
2795 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
2796 field_idx, dex_file_->GetFieldName(field_id),
2797 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002798 return NULL;
2799 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002800 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002801 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08002802 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002803 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2804 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002805 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002806 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
2807 << dex_file_->GetFieldName(field_id) << ") in "
2808 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002809 DCHECK(Thread::Current()->IsExceptionPending());
2810 Thread::Current()->ClearException();
2811 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002812 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2813 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002814 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002815 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002816 return NULL;
2817 } else if (!field->IsStatic()) {
2818 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
2819 return NULL;
2820 } else {
2821 return field;
2822 }
2823}
2824
Ian Rogers776ac1f2012-04-13 23:36:36 -07002825Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002826 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2827 // Check access to class
2828 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002829 if (klass_type.IsConflict()) {
2830 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
2831 field_idx, dex_file_->GetFieldName(field_id),
2832 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002833 return NULL;
2834 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002835 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002836 return NULL; // Can't resolve Class so no more to do here
2837 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002838 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2839 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002840 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002841 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
2842 << dex_file_->GetFieldName(field_id) << ") in "
2843 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002844 DCHECK(Thread::Current()->IsExceptionPending());
2845 Thread::Current()->ClearException();
2846 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002847 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2848 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002849 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002850 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002851 return NULL;
2852 } else if (field->IsStatic()) {
2853 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
2854 << " to not be static";
2855 return NULL;
2856 } else if (obj_type.IsZero()) {
2857 // Cannot infer and check type, however, access will cause null pointer exception
2858 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07002859 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002860 const RegType& field_klass = reg_types_.FromClass(field->GetDeclaringClass());
2861 if (obj_type.IsUninitializedTypes() &&
2862 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
2863 !field_klass.Equals(GetDeclaringClass()))) {
2864 // Field accesses through uninitialized references are only allowable for constructors where
2865 // the field is declared in this class
2866 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
2867 << " of a not fully initialized object within the context of "
2868 << PrettyMethod(method_idx_, *dex_file_);
2869 return NULL;
2870 } else if (!field_klass.IsAssignableFrom(obj_type)) {
2871 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
2872 // of C1. For resolution to occur the declared class of the field must be compatible with
2873 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
2874 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
2875 << " from object of type " << obj_type;
2876 return NULL;
2877 } else {
2878 return field;
2879 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002880 }
2881}
2882
Ian Rogers776ac1f2012-04-13 23:36:36 -07002883void MethodVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002884 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002885 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002886 Field* field;
2887 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002888 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002889 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002890 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07002891 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002892 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002893 const char* descriptor;
2894 const ClassLoader* loader;
2895 if (field != NULL) {
2896 descriptor = FieldHelper(field).GetTypeDescriptor();
2897 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07002898 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002899 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2900 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2901 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07002902 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002903 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
2904 if (is_primitive) {
2905 if (field_type.Equals(insn_type) ||
2906 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
2907 (field_type.IsDouble() && insn_type.IsLongTypes())) {
2908 // expected that read is of the correct primitive type or that int reads are reading
2909 // floats or long reads are reading doubles
2910 } else {
2911 // This is a global failure rather than a class change failure as the instructions and
2912 // the descriptors for the type should have been consistent within the same file at
2913 // compile time
2914 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
2915 << " to be of type '" << insn_type
2916 << "' but found type '" << field_type << "' in get";
2917 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Conflict());
2918 return;
2919 }
2920 } else {
2921 if (!insn_type.IsAssignableFrom(field_type)) {
2922 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
2923 << " to be compatible with type '" << insn_type
2924 << "' but found type '" << field_type
2925 << "' in get-object";
2926 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Conflict());
2927 return;
2928 }
2929 }
2930 work_line_->SetRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002931}
2932
Ian Rogers776ac1f2012-04-13 23:36:36 -07002933void MethodVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002934 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002935 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002936 Field* field;
2937 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07002938 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002939 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002940 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07002941 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002942 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002943 const char* descriptor;
2944 const ClassLoader* loader;
2945 if (field != NULL) {
2946 descriptor = FieldHelper(field).GetTypeDescriptor();
2947 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07002948 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002949 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2950 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2951 loader = class_loader_;
2952 }
2953 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
2954 if (field != NULL) {
2955 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
2956 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
2957 << " from other class " << GetDeclaringClass();
2958 return;
2959 }
2960 }
2961 if (is_primitive) {
2962 // Primitive field assignability rules are weaker than regular assignability rules
2963 bool instruction_compatible;
2964 bool value_compatible;
2965 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
2966 if (field_type.IsIntegralTypes()) {
2967 instruction_compatible = insn_type.IsIntegralTypes();
2968 value_compatible = value_type.IsIntegralTypes();
2969 } else if (field_type.IsFloat()) {
2970 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
2971 value_compatible = value_type.IsFloatTypes();
2972 } else if (field_type.IsLong()) {
2973 instruction_compatible = insn_type.IsLong();
2974 value_compatible = value_type.IsLongTypes();
2975 } else if (field_type.IsDouble()) {
2976 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
2977 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07002978 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002979 instruction_compatible = false; // reference field with primitive store
2980 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07002981 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002982 if (!instruction_compatible) {
2983 // This is a global failure rather than a class change failure as the instructions and
2984 // the descriptors for the type should have been consistent within the same file at
2985 // compile time
2986 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
2987 << " to be of type '" << insn_type
2988 << "' but found type '" << field_type
2989 << "' in put";
2990 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07002991 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002992 if (!value_compatible) {
2993 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
2994 << " of type " << value_type
2995 << " but expected " << field_type
2996 << " for store to " << PrettyField(field) << " in put";
2997 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07002998 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002999 } else {
3000 if (!insn_type.IsAssignableFrom(field_type)) {
3001 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3002 << " to be compatible with type '" << insn_type
3003 << "' but found type '" << field_type
3004 << "' in put-object";
3005 return;
3006 }
3007 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003008 }
3009}
3010
Ian Rogers776ac1f2012-04-13 23:36:36 -07003011bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003012 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003013 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003014 return false;
3015 }
3016 return true;
3017}
3018
Ian Rogers776ac1f2012-04-13 23:36:36 -07003019void MethodVerifier::ReplaceFailingInstruction() {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003020 // Pop the failure and clear the need for rewriting.
3021 size_t failure_number = failures_.size();
3022 CHECK_NE(failure_number, 0U);
3023 DCHECK_EQ(failure_messages_.size(), failure_number);
3024 std::ostringstream* failure_message = failure_messages_[failure_number - 1];
3025 VerifyError failure = failures_[failure_number - 1];
3026 failures_.pop_back();
3027 failure_messages_.pop_back();
3028 have_pending_rewrite_failure_ = false;
3029
Ian Rogersf1864ef2011-12-09 12:39:48 -08003030 if (Runtime::Current()->IsStarted()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003031 LOG(ERROR) << "Verification attempting to replace instructions at runtime in "
3032 << PrettyMethod(method_idx_, *dex_file_) << " " << failure_message->str();
Ian Rogersf1864ef2011-12-09 12:39:48 -08003033 return;
3034 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003035 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3036 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3037 VerifyErrorRefType ref_type;
3038 switch (inst->Opcode()) {
3039 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003040 case Instruction::CHECK_CAST:
3041 case Instruction::INSTANCE_OF:
3042 case Instruction::NEW_INSTANCE:
3043 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003044 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003045 case Instruction::FILLED_NEW_ARRAY_RANGE:
3046 ref_type = VERIFY_ERROR_REF_CLASS;
3047 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003048 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003049 case Instruction::IGET_BOOLEAN:
3050 case Instruction::IGET_BYTE:
3051 case Instruction::IGET_CHAR:
3052 case Instruction::IGET_SHORT:
3053 case Instruction::IGET_WIDE:
3054 case Instruction::IGET_OBJECT:
3055 case Instruction::IPUT:
3056 case Instruction::IPUT_BOOLEAN:
3057 case Instruction::IPUT_BYTE:
3058 case Instruction::IPUT_CHAR:
3059 case Instruction::IPUT_SHORT:
3060 case Instruction::IPUT_WIDE:
3061 case Instruction::IPUT_OBJECT:
3062 case Instruction::SGET:
3063 case Instruction::SGET_BOOLEAN:
3064 case Instruction::SGET_BYTE:
3065 case Instruction::SGET_CHAR:
3066 case Instruction::SGET_SHORT:
3067 case Instruction::SGET_WIDE:
3068 case Instruction::SGET_OBJECT:
3069 case Instruction::SPUT:
3070 case Instruction::SPUT_BOOLEAN:
3071 case Instruction::SPUT_BYTE:
3072 case Instruction::SPUT_CHAR:
3073 case Instruction::SPUT_SHORT:
3074 case Instruction::SPUT_WIDE:
3075 case Instruction::SPUT_OBJECT:
3076 ref_type = VERIFY_ERROR_REF_FIELD;
3077 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003078 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003079 case Instruction::INVOKE_VIRTUAL_RANGE:
3080 case Instruction::INVOKE_SUPER:
3081 case Instruction::INVOKE_SUPER_RANGE:
3082 case Instruction::INVOKE_DIRECT:
3083 case Instruction::INVOKE_DIRECT_RANGE:
3084 case Instruction::INVOKE_STATIC:
3085 case Instruction::INVOKE_STATIC_RANGE:
3086 case Instruction::INVOKE_INTERFACE:
3087 case Instruction::INVOKE_INTERFACE_RANGE:
3088 ref_type = VERIFY_ERROR_REF_METHOD;
3089 break;
jeffhaobdb76512011-09-07 11:43:16 -07003090 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003091 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003092 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003093 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003094 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3095 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3096 // instruction, so assert it.
3097 size_t width = inst->SizeInCodeUnits();
3098 CHECK_GT(width, 1u);
Ian Rogersf1864ef2011-12-09 12:39:48 -08003099 // If the instruction is larger than 2 code units, rewrite subsequent code unit sized chunks with
Ian Rogersd81871c2011-10-03 13:57:23 -07003100 // NOPs
3101 for (size_t i = 2; i < width; i++) {
3102 insns[work_insn_idx_ + i] = Instruction::NOP;
3103 }
3104 // Encode the opcode, with the failure code in the high byte
3105 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
Ian Rogersad0b3a32012-04-16 14:50:24 -07003106 (failure << 8) | // AA - component
Ian Rogersd81871c2011-10-03 13:57:23 -07003107 (ref_type << (8 + kVerifyErrorRefTypeShift));
3108 insns[work_insn_idx_] = new_instruction;
3109 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3110 // rewrote, so nothing to do here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003111 LOG(INFO) << "Verification error, replacing instructions in "
3112 << PrettyMethod(method_idx_, *dex_file_) << " "
3113 << failure_message->str();
Ian Rogers9fdfc182011-10-26 23:12:52 -07003114 if (gDebugVerify) {
3115 std::cout << std::endl << info_messages_.str();
3116 Dump(std::cout);
3117 }
jeffhaobdb76512011-09-07 11:43:16 -07003118}
jeffhaoba5ebb92011-08-25 17:24:37 -07003119
Ian Rogers776ac1f2012-04-13 23:36:36 -07003120bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003121 bool changed = true;
3122 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3123 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003124 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003125 * We haven't processed this instruction before, and we haven't touched the registers here, so
3126 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3127 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003128 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003129 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003130 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003131 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3132 if (gDebugVerify) {
3133 copy->CopyFromLine(target_line);
3134 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003135 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003136 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003137 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003138 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003139 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003140 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
3141 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << std::endl
Ian Rogersd81871c2011-10-03 13:57:23 -07003142 << *copy.get() << " MERGE" << std::endl
3143 << *merge_line << " ==" << std::endl
3144 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003145 }
3146 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003147 if (changed) {
3148 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003149 }
3150 return true;
3151}
3152
Ian Rogers776ac1f2012-04-13 23:36:36 -07003153InsnFlags* MethodVerifier::CurrentInsnFlags() {
3154 return &insn_flags_[work_insn_idx_];
3155}
3156
Ian Rogersad0b3a32012-04-16 14:50:24 -07003157const RegType& MethodVerifier::GetMethodReturnType() {
3158 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx_);
3159 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3160 uint16_t return_type_idx = proto_id.return_type_idx_;
3161 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
3162 return reg_types_.FromDescriptor(class_loader_, descriptor);
3163}
3164
3165const RegType& MethodVerifier::GetDeclaringClass() {
3166 if (foo_method_ != NULL) {
3167 return reg_types_.FromClass(foo_method_->GetDeclaringClass());
3168 } else {
3169 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx_);
3170 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
3171 return reg_types_.FromDescriptor(class_loader_, descriptor);
3172 }
3173}
3174
Ian Rogers776ac1f2012-04-13 23:36:36 -07003175void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003176 size_t* log2_max_gc_pc) {
3177 size_t local_gc_points = 0;
3178 size_t max_insn = 0;
3179 size_t max_ref_reg = -1;
3180 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3181 if (insn_flags_[i].IsGcPoint()) {
3182 local_gc_points++;
3183 max_insn = i;
3184 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003185 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003186 }
3187 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003188 *gc_points = local_gc_points;
3189 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3190 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003191 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003192 i++;
3193 }
3194 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003195}
3196
Ian Rogers776ac1f2012-04-13 23:36:36 -07003197const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003198 size_t num_entries, ref_bitmap_bits, pc_bits;
3199 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3200 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003201 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003202 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003203 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003204 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003205 return NULL;
3206 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003207 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3208 // There are 2 bytes to encode the number of entries
3209 if (num_entries >= 65536) {
3210 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003211 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003212 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003213 return NULL;
3214 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003215 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003216 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003217 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003218 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003219 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003220 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003221 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003222 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003223 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003224 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003225 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003226 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3227 return NULL;
3228 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003229 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003230 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003231 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003232 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003233 return NULL;
3234 }
3235 // Write table header
Ian Rogers776ac1f2012-04-13 23:36:36 -07003236 table->push_back(format | ((ref_bitmap_bytes >> PcToReferenceMap::kRegMapFormatShift) &
3237 ~PcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003238 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003239 table->push_back(num_entries & 0xFF);
3240 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003241 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003242 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3243 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003244 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003245 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003246 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003247 }
3248 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003249 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003250 }
3251 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003252 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003253 return table;
3254}
jeffhaoa0a764a2011-09-16 10:43:38 -07003255
Ian Rogers776ac1f2012-04-13 23:36:36 -07003256void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003257 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3258 // that the table data is well formed and all references are marked (or not) in the bitmap
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003259 PcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003260 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003261 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003262 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3263 if (insn_flags_[i].IsGcPoint()) {
3264 CHECK_LT(map_index, map.NumEntries());
3265 CHECK_EQ(map.GetPC(map_index), i);
3266 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3267 map_index++;
3268 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003269 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003270 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003271 CHECK_LT(j / 8, map.RegWidth());
3272 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3273 } else if ((j / 8) < map.RegWidth()) {
3274 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3275 } else {
3276 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3277 }
3278 }
3279 } else {
3280 CHECK(reg_bitmap == NULL);
3281 }
3282 }
3283}
jeffhaoa0a764a2011-09-16 10:43:38 -07003284
Ian Rogers776ac1f2012-04-13 23:36:36 -07003285Mutex* MethodVerifier::gc_maps_lock_ = NULL;
3286MethodVerifier::GcMapTable* MethodVerifier::gc_maps_ = NULL;
jeffhaoa0a764a2011-09-16 10:43:38 -07003287
Ian Rogers776ac1f2012-04-13 23:36:36 -07003288void MethodVerifier::InitGcMaps() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003289 gc_maps_lock_ = new Mutex("verifier GC maps lock");
3290 MutexLock mu(*gc_maps_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07003291 gc_maps_ = new MethodVerifier::GcMapTable;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003292}
3293
Ian Rogers776ac1f2012-04-13 23:36:36 -07003294void MethodVerifier::DeleteGcMaps() {
Elliott Hughesf34f1742012-03-16 18:56:00 -07003295 {
3296 MutexLock mu(*gc_maps_lock_);
3297 STLDeleteValues(gc_maps_);
3298 delete gc_maps_;
3299 gc_maps_ = NULL;
3300 }
3301 delete gc_maps_lock_;
3302 gc_maps_lock_ = NULL;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003303}
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003304
Ian Rogers776ac1f2012-04-13 23:36:36 -07003305void MethodVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003306 MutexLock mu(*gc_maps_lock_);
Elliott Hughesa0e18062012-04-13 15:59:59 -07003307 GcMapTable::iterator it = gc_maps_->find(ref);
3308 if (it != gc_maps_->end()) {
3309 delete it->second;
3310 gc_maps_->erase(it);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003311 }
Elliott Hughesa0e18062012-04-13 15:59:59 -07003312 gc_maps_->Put(ref, &gc_map);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003313 CHECK(GetGcMap(ref) != NULL);
3314}
3315
Ian Rogers776ac1f2012-04-13 23:36:36 -07003316const std::vector<uint8_t>* MethodVerifier::GetGcMap(Compiler::MethodReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003317 MutexLock mu(*gc_maps_lock_);
3318 GcMapTable::const_iterator it = gc_maps_->find(ref);
3319 if (it == gc_maps_->end()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003320 return NULL;
3321 }
3322 CHECK(it->second != NULL);
3323 return it->second;
3324}
3325
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003326static Mutex& GetRejectedClassesLock() {
3327 static Mutex rejected_classes_lock("verifier rejected classes lock");
3328 return rejected_classes_lock;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003329}
3330
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003331static std::set<Compiler::ClassReference>& GetRejectedClasses() {
3332 static std::set<Compiler::ClassReference> rejected_classes;
3333 return rejected_classes;
3334}
jeffhaod1224c72012-02-29 13:43:08 -08003335
Ian Rogers776ac1f2012-04-13 23:36:36 -07003336void MethodVerifier::AddRejectedClass(Compiler::ClassReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003337 MutexLock mu(GetRejectedClassesLock());
3338 GetRejectedClasses().insert(ref);
jeffhaod1224c72012-02-29 13:43:08 -08003339 CHECK(IsClassRejected(ref));
3340}
3341
Ian Rogers776ac1f2012-04-13 23:36:36 -07003342bool MethodVerifier::IsClassRejected(Compiler::ClassReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003343 MutexLock mu(GetRejectedClassesLock());
3344 std::set<Compiler::ClassReference>& rejected_classes(GetRejectedClasses());
3345 return (rejected_classes.find(ref) != rejected_classes.end());
jeffhaod1224c72012-02-29 13:43:08 -08003346}
3347
Logan Chienfca7e872011-12-20 20:08:22 +08003348#if defined(ART_USE_LLVM_COMPILER)
Ian Rogers776ac1f2012-04-13 23:36:36 -07003349const InferredRegCategoryMap* MethodVerifier::GenerateInferredRegCategoryMap() {
Logan Chienfca7e872011-12-20 20:08:22 +08003350 uint32_t insns_size = code_item_->insns_size_in_code_units_;
3351 uint16_t regs_size = code_item_->registers_size_;
3352
3353 UniquePtr<InferredRegCategoryMap> table(
3354 new InferredRegCategoryMap(insns_size, regs_size));
3355
3356 for (size_t i = 0; i < insns_size; ++i) {
3357 if (RegisterLine* line = reg_table_.GetLine(i)) {
3358 for (size_t r = 0; r < regs_size; ++r) {
Logan Chiendd361c92012-04-10 23:40:37 +08003359 const RegType &rt = line->GetRegisterType(r);
Logan Chienfca7e872011-12-20 20:08:22 +08003360
3361 if (rt.IsZero()) {
3362 table->SetRegCategory(i, r, kRegZero);
3363 } else if (rt.IsCategory1Types()) {
3364 table->SetRegCategory(i, r, kRegCat1nr);
3365 } else if (rt.IsCategory2Types()) {
3366 table->SetRegCategory(i, r, kRegCat2);
3367 } else if (rt.IsReferenceTypes()) {
3368 table->SetRegCategory(i, r, kRegObject);
3369 } else {
3370 table->SetRegCategory(i, r, kRegUnknown);
3371 }
3372 }
3373 }
3374 }
3375
3376 return table.release();
3377}
Logan Chiendd361c92012-04-10 23:40:37 +08003378
Ian Rogers776ac1f2012-04-13 23:36:36 -07003379Mutex* MethodVerifier::inferred_reg_category_maps_lock_ = NULL;
3380MethodVerifier::InferredRegCategoryMapTable* MethodVerifier::inferred_reg_category_maps_ = NULL;
Logan Chiendd361c92012-04-10 23:40:37 +08003381
Ian Rogers776ac1f2012-04-13 23:36:36 -07003382void MethodVerifier::InitInferredRegCategoryMaps() {
Logan Chiendd361c92012-04-10 23:40:37 +08003383 inferred_reg_category_maps_lock_ = new Mutex("verifier GC maps lock");
3384 MutexLock mu(*inferred_reg_category_maps_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07003385 inferred_reg_category_maps_ = new MethodVerifier::InferredRegCategoryMapTable;
Logan Chiendd361c92012-04-10 23:40:37 +08003386}
3387
Ian Rogers776ac1f2012-04-13 23:36:36 -07003388void MethodVerifier::DeleteInferredRegCategoryMaps() {
Logan Chiendd361c92012-04-10 23:40:37 +08003389 {
3390 MutexLock mu(*inferred_reg_category_maps_lock_);
3391 STLDeleteValues(inferred_reg_category_maps_);
3392 delete inferred_reg_category_maps_;
3393 inferred_reg_category_maps_ = NULL;
3394 }
3395 delete inferred_reg_category_maps_lock_;
3396 inferred_reg_category_maps_lock_ = NULL;
3397}
3398
3399
Ian Rogers776ac1f2012-04-13 23:36:36 -07003400void MethodVerifier::SetInferredRegCategoryMap(Compiler::MethodReference ref,
3401 const InferredRegCategoryMap& inferred_reg_category_map) {
Logan Chiendd361c92012-04-10 23:40:37 +08003402 MutexLock mu(*inferred_reg_category_maps_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07003403 const InferredRegCategoryMap* existing_inferred_reg_category_map = GetInferredRegCategoryMap(ref);
Logan Chiendd361c92012-04-10 23:40:37 +08003404
3405 if (existing_inferred_reg_category_map != NULL) {
3406 CHECK(*existing_inferred_reg_category_map == inferred_reg_category_map);
3407 delete existing_inferred_reg_category_map;
3408 }
3409
Ian Rogers776ac1f2012-04-13 23:36:36 -07003410 inferred_reg_category_maps_->Put(ref, &inferred_reg_category_map);
Logan Chiendd361c92012-04-10 23:40:37 +08003411 CHECK(GetInferredRegCategoryMap(ref) != NULL);
3412}
3413
3414const InferredRegCategoryMap*
Ian Rogers776ac1f2012-04-13 23:36:36 -07003415MethodVerifier::GetInferredRegCategoryMap(Compiler::MethodReference ref) {
Logan Chiendd361c92012-04-10 23:40:37 +08003416 MutexLock mu(*inferred_reg_category_maps_lock_);
3417
3418 InferredRegCategoryMapTable::const_iterator it =
3419 inferred_reg_category_maps_->find(ref);
3420
3421 if (it == inferred_reg_category_maps_->end()) {
3422 return NULL;
3423 }
3424 CHECK(it->second != NULL);
3425 return it->second;
3426}
Logan Chienfca7e872011-12-20 20:08:22 +08003427#endif
3428
Ian Rogersd81871c2011-10-03 13:57:23 -07003429} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003430} // namespace art