blob: 197994053e234bd26c35296a49a30c8798ca8466 [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 }
jeffhaobdb76512011-09-07 11:43:16 -0700187 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
188 Method* method = klass->GetDirectMethod(i);
189 if (!VerifyMethod(method)) {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800190 error = "Verifier rejected class ";
191 error += PrettyDescriptor(klass);
192 error += " due to bad method ";
193 error += PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700194 return false;
195 }
196 }
197 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
198 Method* method = klass->GetVirtualMethod(i);
199 if (!VerifyMethod(method)) {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800200 error = "Verifier rejected class ";
201 error += PrettyDescriptor(klass);
202 error += " due to bad method ";
203 error += PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700204 return false;
205 }
206 }
207 return true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700208}
209
Ian Rogers776ac1f2012-04-13 23:36:36 -0700210bool MethodVerifier::VerifyMethod(Method* method) {
211 MethodVerifier verifier(method);
jeffhaof56197c2012-03-05 18:01:54 -0800212 bool success = verifier.VerifyAll();
Brian Carlstrom75412882012-01-18 01:26:54 -0800213 CHECK_EQ(success, verifier.failure_ == VERIFY_ERROR_NONE);
214
Ian Rogersd81871c2011-10-03 13:57:23 -0700215 // We expect either success and no verification error, or failure and a generic failure to
216 // reject the class.
217 if (success) {
218 if (verifier.failure_ != VERIFY_ERROR_NONE) {
219 LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method) << std::endl
220 << verifier.fail_messages_;
221 }
222 } else {
223 LOG(INFO) << "Verification error in " << PrettyMethod(method) << " "
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700224 << verifier.fail_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700225 if (gDebugVerify) {
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700226 std::cout << std::endl << verifier.info_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700227 verifier.Dump(std::cout);
228 }
jeffhaod5347e02012-03-22 17:25:05 -0700229 DCHECK((verifier.failure_ == VERIFY_ERROR_BAD_CLASS_SOFT) ||
230 (verifier.failure_ == VERIFY_ERROR_BAD_CLASS_HARD)) << verifier.failure_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700231 }
232 return success;
233}
234
Ian Rogers776ac1f2012-04-13 23:36:36 -0700235void MethodVerifier::VerifyMethodAndDump(Method* method) {
236 MethodVerifier verifier(method);
jeffhaof56197c2012-03-05 18:01:54 -0800237 verifier.VerifyAll();
Shih-wei Liao371814f2011-10-27 16:52:10 -0700238
Elliott Hughese0918552011-10-28 17:18:29 -0700239 LOG(INFO) << "Dump of method " << PrettyMethod(method) << " "
240 << verifier.fail_messages_.str() << std::endl
Ian Rogers776ac1f2012-04-13 23:36:36 -0700241 << verifier.info_messages_.str() << Dumpable<MethodVerifier>(verifier);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700242}
243
Ian Rogers776ac1f2012-04-13 23:36:36 -0700244bool MethodVerifier::VerifyClass(const DexFile* dex_file, DexCache* dex_cache,
jeffhaof56197c2012-03-05 18:01:54 -0800245 const ClassLoader* class_loader, uint32_t class_def_idx, std::string& error) {
246 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
247 const byte* class_data = dex_file->GetClassData(class_def);
248 ClassDataItemIterator it(*dex_file, class_data);
249 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
250 it.Next();
251 }
252 while (it.HasNextDirectMethod()) {
253 uint32_t method_idx = it.GetMemberIndex();
254 if (!VerifyMethod(method_idx, dex_file, dex_cache, class_loader, class_def_idx,
255 it.GetMethodCodeItem())) {
256 error = "Verifier rejected class";
257 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
258 error += " due to bad method ";
259 error += PrettyMethod(method_idx, *dex_file);
260 return false;
261 }
262 it.Next();
263 }
264 while (it.HasNextVirtualMethod()) {
265 uint32_t method_idx = it.GetMemberIndex();
266 if (!VerifyMethod(method_idx, dex_file, dex_cache, class_loader, class_def_idx,
267 it.GetMethodCodeItem())) {
268 error = "Verifier rejected class";
269 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
270 error += " due to bad method ";
271 error += PrettyMethod(method_idx, *dex_file);
272 return false;
273 }
274 it.Next();
275 }
276 return true;
277}
278
Ian Rogers776ac1f2012-04-13 23:36:36 -0700279bool MethodVerifier::VerifyMethod(uint32_t method_idx, const DexFile* dex_file, DexCache* dex_cache,
jeffhaof56197c2012-03-05 18:01:54 -0800280 const ClassLoader* class_loader, uint32_t class_def_idx, const DexFile::CodeItem* code_item) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700281 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item);
jeffhaof56197c2012-03-05 18:01:54 -0800282
Ian Rogers776ac1f2012-04-13 23:36:36 -0700283 // Without a method*, we can only verify the structure.
jeffhaof56197c2012-03-05 18:01:54 -0800284 bool success = verifier.VerifyStructure();
285 CHECK_EQ(success, verifier.failure_ == VERIFY_ERROR_NONE);
286
287 // We expect either success and no verification error, or failure and a generic failure to
288 // reject the class.
289 if (success) {
290 if (verifier.failure_ != VERIFY_ERROR_NONE) {
291 LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method_idx, *dex_file)
292 << std::endl << verifier.fail_messages_;
293 }
294 } else {
295 LOG(INFO) << "Verification error in " << PrettyMethod(method_idx, *dex_file) << " "
296 << verifier.fail_messages_.str();
297 if (gDebugVerify) {
298 std::cout << std::endl << verifier.info_messages_.str();
299 verifier.Dump(std::cout);
300 }
jeffhaod5347e02012-03-22 17:25:05 -0700301 DCHECK((verifier.failure_ == VERIFY_ERROR_BAD_CLASS_SOFT) ||
302 (verifier.failure_ == VERIFY_ERROR_BAD_CLASS_HARD)) << verifier.failure_;
jeffhaof56197c2012-03-05 18:01:54 -0800303 }
304 return success;
305}
306
Ian Rogers776ac1f2012-04-13 23:36:36 -0700307MethodVerifier::MethodVerifier(Method* method)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800308 : work_insn_idx_(-1),
309 method_(method),
310 failure_(VERIFY_ERROR_NONE),
311 new_instance_count_(0),
312 monitor_enter_count_(0) {
313 CHECK(method != NULL);
jeffhaof56197c2012-03-05 18:01:54 -0800314 dex_cache_ = method->GetDeclaringClass()->GetDexCache();
315 class_loader_ = method->GetDeclaringClass()->GetClassLoader();
Brian Carlstromc12a17a2012-01-17 18:02:32 -0800316 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
jeffhaof56197c2012-03-05 18:01:54 -0800317 dex_file_ = &class_linker->FindDexFile(dex_cache_);
Ian Rogersd81871c2011-10-03 13:57:23 -0700318 code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset());
jeffhaof56197c2012-03-05 18:01:54 -0800319 const DexFile::ClassDef* class_def = ClassHelper(method_->GetDeclaringClass()).GetClassDef();
320 class_def_idx_ = dex_file_->GetIndexForClassDef(*class_def);
jeffhaoba5ebb92011-08-25 17:24:37 -0700321}
322
Ian Rogers776ac1f2012-04-13 23:36:36 -0700323MethodVerifier::MethodVerifier(const DexFile* dex_file, DexCache* dex_cache,
jeffhaof56197c2012-03-05 18:01:54 -0800324 const ClassLoader* class_loader, uint32_t class_def_idx, const DexFile::CodeItem* code_item)
325 : work_insn_idx_(-1),
326 method_(NULL),
327 dex_file_(dex_file),
328 dex_cache_(dex_cache),
329 class_loader_(class_loader),
330 class_def_idx_(class_def_idx),
331 code_item_(code_item),
332 failure_(VERIFY_ERROR_NONE),
333 new_instance_count_(0),
334 monitor_enter_count_(0) {
335}
336
Ian Rogers776ac1f2012-04-13 23:36:36 -0700337bool MethodVerifier::VerifyAll() {
jeffhaof56197c2012-03-05 18:01:54 -0800338 CHECK(method_ != NULL);
Ian Rogersd81871c2011-10-03 13:57:23 -0700339 // If there aren't any instructions, make sure that's expected, then exit successfully.
340 if (code_item_ == NULL) {
341 if (!method_->IsNative() && !method_->IsAbstract()) {
jeffhaod5347e02012-03-22 17:25:05 -0700342 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700343 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700344 } else {
345 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700346 }
jeffhaobdb76512011-09-07 11:43:16 -0700347 }
jeffhaof56197c2012-03-05 18:01:54 -0800348 return VerifyStructure() && VerifyCodeFlow();
349}
350
Ian Rogers776ac1f2012-04-13 23:36:36 -0700351bool MethodVerifier::VerifyStructure() {
jeffhaof56197c2012-03-05 18:01:54 -0800352 if (code_item_ == NULL) {
353 return true;
354 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700355 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
356 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700357 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
358 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700359 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700360 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700361 // Allocate and initialize an array to hold instruction data.
362 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
363 // Run through the instructions and see if the width checks out.
364 bool result = ComputeWidthsAndCountOps();
365 // Flag instructions guarded by a "try" block and check exception handlers.
366 result = result && ScanTryCatchBlocks();
367 // Perform static instruction verification.
368 result = result && VerifyInstructions();
jeffhaobdb76512011-09-07 11:43:16 -0700369 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700370}
371
Ian Rogers776ac1f2012-04-13 23:36:36 -0700372std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogers47a05882012-02-03 12:23:33 -0800373 CHECK_EQ(failure_, VERIFY_ERROR_NONE);
Ian Rogers47a05882012-02-03 12:23:33 -0800374 if (Runtime::Current()->IsCompiler()) {
jeffhaod1224c72012-02-29 13:43:08 -0800375 switch (error) {
376 // If we're optimistically running verification at compile time, turn NO_xxx and ACCESS_xxx
jeffhaod5347e02012-03-22 17:25:05 -0700377 // errors into soft verification errors so that we re-verify at runtime. We may fail to find
378 // or to agree on access because of not yet available class loaders, or class loaders that
379 // will differ at runtime.
Ian Rogers47a05882012-02-03 12:23:33 -0800380 case VERIFY_ERROR_NO_CLASS:
381 case VERIFY_ERROR_NO_FIELD:
382 case VERIFY_ERROR_NO_METHOD:
Ian Rogers9ada79c2012-02-03 14:29:52 -0800383 case VERIFY_ERROR_ACCESS_CLASS:
384 case VERIFY_ERROR_ACCESS_FIELD:
385 case VERIFY_ERROR_ACCESS_METHOD:
jeffhaod5347e02012-03-22 17:25:05 -0700386 error = VERIFY_ERROR_BAD_CLASS_SOFT;
Ian Rogers47a05882012-02-03 12:23:33 -0800387 break;
jeffhaod5347e02012-03-22 17:25:05 -0700388 // Hard verification failures at compile time will still fail at runtime, so the class is
389 // marked as rejected to prevent it from being compiled.
390 case VERIFY_ERROR_BAD_CLASS_HARD: {
jeffhaof56197c2012-03-05 18:01:54 -0800391 Compiler::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800392 AddRejectedClass(ref);
393 break;
394 }
Ian Rogers47a05882012-02-03 12:23:33 -0800395 default:
396 break;
397 }
398 }
399 failure_ = error;
400 return fail_messages_ << "VFY: " << PrettyMethod(method_)
401 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
402}
403
Ian Rogers776ac1f2012-04-13 23:36:36 -0700404bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700405 const uint16_t* insns = code_item_->insns_;
406 size_t insns_size = code_item_->insns_size_in_code_units_;
407 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700408 size_t new_instance_count = 0;
409 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700410 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700411
Ian Rogersd81871c2011-10-03 13:57:23 -0700412 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700413 Instruction::Code opcode = inst->Opcode();
414 if (opcode == Instruction::NEW_INSTANCE) {
415 new_instance_count++;
416 } else if (opcode == Instruction::MONITOR_ENTER) {
417 monitor_enter_count++;
418 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700419 size_t inst_size = inst->SizeInCodeUnits();
420 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
421 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700422 inst = inst->Next();
423 }
424
Ian Rogersd81871c2011-10-03 13:57:23 -0700425 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700426 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
427 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700428 return false;
429 }
430
Ian Rogersd81871c2011-10-03 13:57:23 -0700431 new_instance_count_ = new_instance_count;
432 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700433 return true;
434}
435
Ian Rogers776ac1f2012-04-13 23:36:36 -0700436bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700437 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700438 if (tries_size == 0) {
439 return true;
440 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700441 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700442 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700443
444 for (uint32_t idx = 0; idx < tries_size; idx++) {
445 const DexFile::TryItem* try_item = &tries[idx];
446 uint32_t start = try_item->start_addr_;
447 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700448 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700449 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
450 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700451 return false;
452 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700453 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700454 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700455 return false;
456 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700457 for (uint32_t dex_pc = start; dex_pc < end;
458 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
459 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700460 }
461 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800462 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700463 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700464 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700465 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700466 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700467 CatchHandlerIterator iterator(handlers_ptr);
468 for (; iterator.HasNext(); iterator.Next()) {
469 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700470 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700471 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700472 return false;
473 }
jeffhao60f83e32012-02-13 17:16:30 -0800474 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
475 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700476 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
jeffhao60f83e32012-02-13 17:16:30 -0800477 << dex_pc << ")";
478 return false;
479 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700480 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700481 // Ensure exception types are resolved so that they don't need resolution to be delivered,
482 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700483 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
jeffhaof56197c2012-03-05 18:01:54 -0800484 Class* exception_type = linker->ResolveType(*dex_file_, iterator.GetHandlerTypeIndex(),
485 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700486 if (exception_type == NULL) {
487 DCHECK(Thread::Current()->IsExceptionPending());
488 Thread::Current()->ClearException();
489 }
490 }
jeffhaobdb76512011-09-07 11:43:16 -0700491 }
Ian Rogers0571d352011-11-03 19:51:38 -0700492 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700493 }
jeffhaobdb76512011-09-07 11:43:16 -0700494 return true;
495}
496
Ian Rogers776ac1f2012-04-13 23:36:36 -0700497bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700498 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700499
Ian Rogersd81871c2011-10-03 13:57:23 -0700500 /* Flag the start of the method as a branch target. */
501 insn_flags_[0].SetBranchTarget();
502
503 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700504 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700505 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700506 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
jeffhaod1224c72012-02-29 13:43:08 -0800507 fail_messages_ << "Rejecting opcode " << inst->Name() << " at " << dex_pc;
Ian Rogersd81871c2011-10-03 13:57:23 -0700508 return false;
509 }
510 /* Flag instructions that are garbage collection points */
511 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
512 insn_flags_[dex_pc].SetGcPoint();
513 }
514 dex_pc += inst->SizeInCodeUnits();
515 inst = inst->Next();
516 }
517 return true;
518}
519
Ian Rogers776ac1f2012-04-13 23:36:36 -0700520bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800521 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700522 bool result = true;
523 switch (inst->GetVerifyTypeArgumentA()) {
524 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800525 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700526 break;
527 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800528 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700529 break;
530 }
531 switch (inst->GetVerifyTypeArgumentB()) {
532 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800533 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700534 break;
535 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800536 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700537 break;
538 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800539 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700540 break;
541 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800542 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700543 break;
544 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800545 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700546 break;
547 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800548 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700549 break;
550 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800551 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700552 break;
553 }
554 switch (inst->GetVerifyTypeArgumentC()) {
555 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800556 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700557 break;
558 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800559 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700560 break;
561 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800562 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700563 break;
564 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800565 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700566 break;
567 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800568 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700569 break;
570 }
571 switch (inst->GetVerifyExtraFlags()) {
572 case Instruction::kVerifyArrayData:
573 result = result && CheckArrayData(code_offset);
574 break;
575 case Instruction::kVerifyBranchTarget:
576 result = result && CheckBranchTarget(code_offset);
577 break;
578 case Instruction::kVerifySwitchTargets:
579 result = result && CheckSwitchTargets(code_offset);
580 break;
581 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800582 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700583 break;
584 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800585 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700586 break;
587 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700588 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700589 result = false;
590 break;
591 }
592 return result;
593}
594
Ian Rogers776ac1f2012-04-13 23:36:36 -0700595bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700596 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700597 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
598 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700599 return false;
600 }
601 return true;
602}
603
Ian Rogers776ac1f2012-04-13 23:36:36 -0700604bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700605 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700606 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
607 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700608 return false;
609 }
610 return true;
611}
612
Ian Rogers776ac1f2012-04-13 23:36:36 -0700613bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700614 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700615 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
616 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700617 return false;
618 }
619 return true;
620}
621
Ian Rogers776ac1f2012-04-13 23:36:36 -0700622bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700623 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700624 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
625 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700626 return false;
627 }
628 return true;
629}
630
Ian Rogers776ac1f2012-04-13 23:36:36 -0700631bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700632 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700633 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
634 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700635 return false;
636 }
637 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700638 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700639 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700640 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700641 return false;
642 }
643 return true;
644}
645
Ian Rogers776ac1f2012-04-13 23:36:36 -0700646bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700647 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700648 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
649 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700650 return false;
651 }
652 return true;
653}
654
Ian Rogers776ac1f2012-04-13 23:36:36 -0700655bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700656 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700657 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
658 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700659 return false;
660 }
661 return true;
662}
663
Ian Rogers776ac1f2012-04-13 23:36:36 -0700664bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700665 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700666 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
667 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700668 return false;
669 }
670 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700671 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700672 const char* cp = descriptor;
673 while (*cp++ == '[') {
674 bracket_count++;
675 }
676 if (bracket_count == 0) {
677 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700678 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700679 return false;
680 } else if (bracket_count > 255) {
681 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700682 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 return false;
684 }
685 return true;
686}
687
Ian Rogers776ac1f2012-04-13 23:36:36 -0700688bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700689 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
690 const uint16_t* insns = code_item_->insns_ + cur_offset;
691 const uint16_t* array_data;
692 int32_t array_data_offset;
693
694 DCHECK_LT(cur_offset, insn_count);
695 /* make sure the start of the array data table is in range */
696 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
697 if ((int32_t) cur_offset + array_data_offset < 0 ||
698 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700699 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
700 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700701 return false;
702 }
703 /* offset to array data table is a relative branch-style offset */
704 array_data = insns + array_data_offset;
705 /* make sure the table is 32-bit aligned */
706 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700707 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
708 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700709 return false;
710 }
711 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700712 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700713 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
714 /* make sure the end of the switch is in range */
715 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700716 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
717 << ", data offset " << array_data_offset << ", end "
718 << cur_offset + array_data_offset + table_size
719 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700720 return false;
721 }
722 return true;
723}
724
Ian Rogers776ac1f2012-04-13 23:36:36 -0700725bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700726 int32_t offset;
727 bool isConditional, selfOkay;
728 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
729 return false;
730 }
731 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700732 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 -0700733 return false;
734 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700735 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
736 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700737 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700738 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700739 return false;
740 }
741 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
742 int32_t abs_offset = cur_offset + offset;
743 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700744 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700745 << reinterpret_cast<void*>(abs_offset) << ") at "
746 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700747 return false;
748 }
749 insn_flags_[abs_offset].SetBranchTarget();
750 return true;
751}
752
Ian Rogers776ac1f2012-04-13 23:36:36 -0700753bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700754 bool* selfOkay) {
755 const uint16_t* insns = code_item_->insns_ + cur_offset;
756 *pConditional = false;
757 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700758 switch (*insns & 0xff) {
759 case Instruction::GOTO:
760 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700761 break;
762 case Instruction::GOTO_32:
763 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700764 *selfOkay = true;
765 break;
766 case Instruction::GOTO_16:
767 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700768 break;
769 case Instruction::IF_EQ:
770 case Instruction::IF_NE:
771 case Instruction::IF_LT:
772 case Instruction::IF_GE:
773 case Instruction::IF_GT:
774 case Instruction::IF_LE:
775 case Instruction::IF_EQZ:
776 case Instruction::IF_NEZ:
777 case Instruction::IF_LTZ:
778 case Instruction::IF_GEZ:
779 case Instruction::IF_GTZ:
780 case Instruction::IF_LEZ:
781 *pOffset = (int16_t) insns[1];
782 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700783 break;
784 default:
785 return false;
786 break;
787 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700788 return true;
789}
790
Ian Rogers776ac1f2012-04-13 23:36:36 -0700791bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700792 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700793 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700794 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700795 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700796 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
797 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
799 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700800 return false;
801 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700802 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700803 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700804 /* make sure the table is 32-bit aligned */
805 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700806 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
807 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700808 return false;
809 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700810 uint32_t switch_count = switch_insns[1];
811 int32_t keys_offset, targets_offset;
812 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700813 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
814 /* 0=sig, 1=count, 2/3=firstKey */
815 targets_offset = 4;
816 keys_offset = -1;
817 expected_signature = Instruction::kPackedSwitchSignature;
818 } else {
819 /* 0=sig, 1=count, 2..count*2 = keys */
820 keys_offset = 2;
821 targets_offset = 2 + 2 * switch_count;
822 expected_signature = Instruction::kSparseSwitchSignature;
823 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700824 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700825 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700826 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
827 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700828 return false;
829 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700830 /* make sure the end of the switch is in range */
831 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700832 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
833 << switch_offset << ", end "
834 << (cur_offset + switch_offset + table_size)
835 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700836 return false;
837 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700838 /* for a sparse switch, verify the keys are in ascending order */
839 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700840 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
841 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700842 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
843 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
844 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700845 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
846 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700847 return false;
848 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700849 last_key = key;
850 }
851 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700852 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700853 for (uint32_t targ = 0; targ < switch_count; targ++) {
854 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
855 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
856 int32_t abs_offset = cur_offset + offset;
857 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700858 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700859 << reinterpret_cast<void*>(abs_offset) << ") at "
860 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700861 return false;
862 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700863 insn_flags_[abs_offset].SetBranchTarget();
864 }
865 return true;
866}
867
Ian Rogers776ac1f2012-04-13 23:36:36 -0700868bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700869 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700870 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700871 return false;
872 }
873 uint16_t registers_size = code_item_->registers_size_;
874 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800875 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700876 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
877 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700878 return false;
879 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700880 }
881
882 return true;
883}
884
Ian Rogers776ac1f2012-04-13 23:36:36 -0700885bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700886 uint16_t registers_size = code_item_->registers_size_;
887 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
888 // integer overflow when adding them here.
889 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700890 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
891 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700892 return false;
893 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700894 return true;
895}
896
Brian Carlstrom75412882012-01-18 01:26:54 -0800897const std::vector<uint8_t>* CreateLengthPrefixedGcMap(const std::vector<uint8_t>& gc_map) {
898 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
899 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
900 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
901 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
902 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
903 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
904 gc_map.begin(),
905 gc_map.end());
906 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
907 DCHECK_EQ(gc_map.size(),
908 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
909 (length_prefixed_gc_map->at(1) << 16) |
910 (length_prefixed_gc_map->at(2) << 8) |
911 (length_prefixed_gc_map->at(3) << 0)));
912 return length_prefixed_gc_map;
913}
914
Ian Rogers776ac1f2012-04-13 23:36:36 -0700915bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700916 uint16_t registers_size = code_item_->registers_size_;
917 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -0700918
Ian Rogersd81871c2011-10-03 13:57:23 -0700919 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -0800920 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
921 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700922 }
923 /* Create and initialize table holding register status */
Elliott Hughes460384f2012-04-04 16:53:10 -0700924 reg_table_.Init(kTrackRegsGcPoints, insn_flags_.get(), insns_size, registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -0700925
Ian Rogersd81871c2011-10-03 13:57:23 -0700926 work_line_.reset(new RegisterLine(registers_size, this));
927 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -0700928
Ian Rogersd81871c2011-10-03 13:57:23 -0700929 /* Initialize register types of method arguments. */
930 if (!SetTypesFromSignature()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700931 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
932 fail_messages_ << "Bad signature in " << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -0700933 return false;
934 }
935 /* Perform code flow verification. */
936 if (!CodeFlowVerifyMethod()) {
Brian Carlstrom75412882012-01-18 01:26:54 -0800937 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -0700938 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700939 }
940
Ian Rogersd81871c2011-10-03 13:57:23 -0700941 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -0800942 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
943 if (map.get() == NULL) {
944 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -0700945 return false; // Not a real failure, but a failure to encode
946 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700947#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800948 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -0700949#endif
Brian Carlstrom75412882012-01-18 01:26:54 -0800950 const std::vector<uint8_t>* gc_map = CreateLengthPrefixedGcMap(*(map.get()));
951 Compiler::MethodReference ref(dex_file_, method_->GetDexMethodIndex());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700952 verifier::MethodVerifier::SetGcMap(ref, *gc_map);
Logan Chienfca7e872011-12-20 20:08:22 +0800953
Brian Carlstrom75412882012-01-18 01:26:54 -0800954 method_->SetGcMap(&gc_map->at(0));
Logan Chiendd361c92012-04-10 23:40:37 +0800955
956#if defined(ART_USE_LLVM_COMPILER)
Logan Chienfca7e872011-12-20 20:08:22 +0800957 /* Generate Inferred Register Category for LLVM-based Code Generator */
958 const InferredRegCategoryMap* table = GenerateInferredRegCategoryMap();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700959 verifier::MethodVerifier::SetInferredRegCategoryMap(ref, *table);
Logan Chienfca7e872011-12-20 20:08:22 +0800960#endif
961
jeffhaobdb76512011-09-07 11:43:16 -0700962 return true;
963}
964
Ian Rogers776ac1f2012-04-13 23:36:36 -0700965void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -0800966 if (code_item_ == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700967 os << "Native method" << std::endl;
968 return;
jeffhaobdb76512011-09-07 11:43:16 -0700969 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700970 DCHECK(code_item_ != NULL);
971 const Instruction* inst = Instruction::At(code_item_->insns_);
972 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
973 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Elliott Hughesaa6e1cd2012-01-18 19:26:06 -0800974 os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
Ian Rogers2c8a8572011-10-24 17:11:36 -0700975 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -0700976 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
977 if (reg_line != NULL) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700978 os << reg_line->Dump() << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -0700979 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700980 inst = inst->Next();
981 }
jeffhaobdb76512011-09-07 11:43:16 -0700982}
983
Ian Rogersd81871c2011-10-03 13:57:23 -0700984static bool IsPrimitiveDescriptor(char descriptor) {
985 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -0700986 case 'I':
987 case 'C':
988 case 'S':
989 case 'B':
990 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -0700991 case 'F':
992 case 'D':
993 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -0700994 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700995 default:
996 return false;
997 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700998}
999
Ian Rogers776ac1f2012-04-13 23:36:36 -07001000bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001001 RegisterLine* reg_line = reg_table_.GetLine(0);
1002 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1003 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001004
Ian Rogersd81871c2011-10-03 13:57:23 -07001005 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1006 //Include the "this" pointer.
1007 size_t cur_arg = 0;
1008 if (!method_->IsStatic()) {
1009 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1010 // argument as uninitialized. This restricts field access until the superclass constructor is
1011 // called.
1012 Class* declaring_class = method_->GetDeclaringClass();
1013 if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
1014 reg_line->SetRegisterType(arg_start + cur_arg,
1015 reg_types_.UninitializedThisArgument(declaring_class));
1016 } else {
1017 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
jeffhaobdb76512011-09-07 11:43:16 -07001018 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001019 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001020 }
1021
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001022 const DexFile::ProtoId& proto_id =
1023 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_->GetDexMethodIndex()));
Ian Rogers0571d352011-11-03 19:51:38 -07001024 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001025
1026 for (; iterator.HasNext(); iterator.Next()) {
1027 const char* descriptor = iterator.GetDescriptor();
1028 if (descriptor == NULL) {
1029 LOG(FATAL) << "Null descriptor";
1030 }
1031 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001032 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1033 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001034 return false;
1035 }
1036 switch (descriptor[0]) {
1037 case 'L':
1038 case '[':
1039 // We assume that reference arguments are initialized. The only way it could be otherwise
1040 // (assuming the caller was verified) is if the current method is <init>, but in that case
1041 // it's effectively considered initialized the instant we reach here (in the sense that we
1042 // can return without doing anything or call virtual methods).
1043 {
1044 const RegType& reg_type =
1045 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001046 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001047 }
1048 break;
1049 case 'Z':
1050 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1051 break;
1052 case 'C':
1053 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1054 break;
1055 case 'B':
1056 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1057 break;
1058 case 'I':
1059 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1060 break;
1061 case 'S':
1062 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1063 break;
1064 case 'F':
1065 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1066 break;
1067 case 'J':
1068 case 'D': {
1069 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1070 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1071 cur_arg++;
1072 break;
1073 }
1074 default:
jeffhaod5347e02012-03-22 17:25:05 -07001075 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001076 return false;
1077 }
1078 cur_arg++;
1079 }
1080 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001081 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001082 return false;
1083 }
1084 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1085 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1086 // format. Only major difference from the method argument format is that 'V' is supported.
1087 bool result;
1088 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1089 result = descriptor[1] == '\0';
1090 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1091 size_t i = 0;
1092 do {
1093 i++;
1094 } while (descriptor[i] == '['); // process leading [
1095 if (descriptor[i] == 'L') { // object array
1096 do {
1097 i++; // find closing ;
1098 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1099 result = descriptor[i] == ';';
1100 } else { // primitive array
1101 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1102 }
1103 } else if (descriptor[0] == 'L') {
1104 // could be more thorough here, but shouldn't be required
1105 size_t i = 0;
1106 do {
1107 i++;
1108 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1109 result = descriptor[i] == ';';
1110 } else {
1111 result = false;
1112 }
1113 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001114 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1115 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001116 }
1117 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001118}
1119
Ian Rogers776ac1f2012-04-13 23:36:36 -07001120bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001121 const uint16_t* insns = code_item_->insns_;
1122 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001123
jeffhaobdb76512011-09-07 11:43:16 -07001124 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001125 insn_flags_[0].SetChanged();
1126 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001127
jeffhaobdb76512011-09-07 11:43:16 -07001128 /* Continue until no instructions are marked "changed". */
1129 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001130 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1131 uint32_t insn_idx = start_guess;
1132 for (; insn_idx < insns_size; insn_idx++) {
1133 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001134 break;
1135 }
jeffhaobdb76512011-09-07 11:43:16 -07001136 if (insn_idx == insns_size) {
1137 if (start_guess != 0) {
1138 /* try again, starting from the top */
1139 start_guess = 0;
1140 continue;
1141 } else {
1142 /* all flags are clear */
1143 break;
1144 }
1145 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001146 // We carry the working set of registers from instruction to instruction. If this address can
1147 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1148 // "changed" flags, we need to load the set of registers from the table.
1149 // Because we always prefer to continue on to the next instruction, we should never have a
1150 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1151 // target.
1152 work_insn_idx_ = insn_idx;
1153 if (insn_flags_[insn_idx].IsBranchTarget()) {
1154 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001155 } else {
1156#ifndef NDEBUG
1157 /*
1158 * Sanity check: retrieve the stored register line (assuming
1159 * a full table) and make sure it actually matches.
1160 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001161 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1162 if (register_line != NULL) {
1163 if (work_line_->CompareLine(register_line) != 0) {
1164 Dump(std::cout);
1165 std::cout << info_messages_.str();
1166 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
Elliott Hughes398f64b2012-03-26 18:05:48 -07001167 << "@" << reinterpret_cast<void*>(work_insn_idx_) << std::endl
1168 << " work_line=" << *work_line_ << std::endl
1169 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001170 }
jeffhaobdb76512011-09-07 11:43:16 -07001171 }
1172#endif
1173 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001174 if (!CodeFlowVerifyInstruction(&start_guess)) {
1175 fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
jeffhaoba5ebb92011-08-25 17:24:37 -07001176 return false;
1177 }
jeffhaobdb76512011-09-07 11:43:16 -07001178 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001179 insn_flags_[insn_idx].SetVisited();
1180 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001181 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001182
Ian Rogersd81871c2011-10-03 13:57:23 -07001183 if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001184 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001185 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001186 * (besides the wasted space), but it indicates a flaw somewhere
1187 * down the line, possibly in the verifier.
1188 *
1189 * If we've substituted "always throw" instructions into the stream,
1190 * we are almost certainly going to have some dead code.
1191 */
1192 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001193 uint32_t insn_idx = 0;
1194 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001195 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001196 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001197 * may or may not be preceded by a padding NOP (for alignment).
1198 */
1199 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1200 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1201 insns[insn_idx] == Instruction::kArrayDataSignature ||
1202 (insns[insn_idx] == Instruction::NOP &&
1203 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1204 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1205 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001206 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001207 }
1208
Ian Rogersd81871c2011-10-03 13:57:23 -07001209 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001210 if (dead_start < 0)
1211 dead_start = insn_idx;
1212 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001213 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001214 dead_start = -1;
1215 }
1216 }
1217 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001218 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001219 }
1220 }
jeffhaobdb76512011-09-07 11:43:16 -07001221 return true;
1222}
1223
Ian Rogers776ac1f2012-04-13 23:36:36 -07001224bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001225#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001226 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001227 gDvm.verifierStats.instrsReexamined++;
1228 } else {
1229 gDvm.verifierStats.instrsExamined++;
1230 }
1231#endif
1232
1233 /*
1234 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001235 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001236 * control to another statement:
1237 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001238 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001239 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001240 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001241 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001242 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001243 * throw an exception that is handled by an encompassing "try"
1244 * block.
1245 *
1246 * We can also return, in which case there is no successor instruction
1247 * from this point.
1248 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001249 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001250 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001251 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1252 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001253 DecodedInstruction dec_insn(inst);
1254 int opcode_flags = Instruction::Flags(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001255
jeffhaobdb76512011-09-07 11:43:16 -07001256 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001257 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001258 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001259 // Generate processing back trace to debug verifier
Ian Rogers5ed29bf2011-10-26 12:22:21 -07001260 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
1261 << *work_line_.get() << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001262 }
jeffhaobdb76512011-09-07 11:43:16 -07001263
1264 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001265 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001266 * can throw an exception, we will copy/merge this into the "catch"
1267 * address rather than work_line, because we don't want the result
1268 * from the "successful" code path (e.g. a check-cast that "improves"
1269 * a type) to be visible to the exception handler.
1270 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001271 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001272 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001273 } else {
1274#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001275 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001276#endif
1277 }
1278
Elliott Hughesadb8c672012-03-06 16:49:32 -08001279 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07001280 case Instruction::NOP:
1281 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001282 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001283 * a signature that looks like a NOP; if we see one of these in
1284 * the course of executing code then we have a problem.
1285 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001286 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001287 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001288 }
1289 break;
1290
1291 case Instruction::MOVE:
1292 case Instruction::MOVE_FROM16:
1293 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001294 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001295 break;
1296 case Instruction::MOVE_WIDE:
1297 case Instruction::MOVE_WIDE_FROM16:
1298 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001299 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07001300 break;
1301 case Instruction::MOVE_OBJECT:
1302 case Instruction::MOVE_OBJECT_FROM16:
1303 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001304 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001305 break;
1306
1307 /*
1308 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001309 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001310 * might want to hold the result in an actual CPU register, so the
1311 * Dalvik spec requires that these only appear immediately after an
1312 * invoke or filled-new-array.
1313 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001314 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001315 * redundant with the reset done below, but it can make the debug info
1316 * easier to read in some cases.)
1317 */
1318 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001319 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07001320 break;
1321 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001322 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001323 break;
1324 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001325 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07001326 break;
1327
Ian Rogersd81871c2011-10-03 13:57:23 -07001328 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001329 /*
jeffhao60f83e32012-02-13 17:16:30 -08001330 * This statement can only appear as the first instruction in an exception handler. We verify
1331 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001332 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001333 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08001334 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001335 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001336 }
jeffhaobdb76512011-09-07 11:43:16 -07001337 case Instruction::RETURN_VOID:
Ian Rogersd81871c2011-10-03 13:57:23 -07001338 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1339 if (!GetMethodReturnType().IsUnknown()) {
jeffhaod5347e02012-03-22 17:25:05 -07001340 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001341 }
jeffhaobdb76512011-09-07 11:43:16 -07001342 }
1343 break;
1344 case Instruction::RETURN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001345 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001346 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001347 const RegType& return_type = GetMethodReturnType();
1348 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001349 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001350 } else {
1351 // Compilers may generate synthetic functions that write byte values into boolean fields.
1352 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001353 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001354 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1355 ((return_type.IsBoolean() || return_type.IsByte() ||
1356 return_type.IsShort() || return_type.IsChar()) &&
1357 src_type.IsInteger()));
1358 /* check the register contents */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001359 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001360 if (failure_ != VERIFY_ERROR_NONE) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001361 fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07001362 }
jeffhaobdb76512011-09-07 11:43:16 -07001363 }
1364 }
1365 break;
1366 case Instruction::RETURN_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001367 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001368 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001369 const RegType& return_type = GetMethodReturnType();
1370 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001371 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001372 } else {
1373 /* check the register contents */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001374 work_line_->VerifyRegisterType(dec_insn.vA, return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001375 if (failure_ != VERIFY_ERROR_NONE) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001376 fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07001377 }
jeffhaobdb76512011-09-07 11:43:16 -07001378 }
1379 }
1380 break;
1381 case Instruction::RETURN_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001382 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1383 const RegType& return_type = GetMethodReturnType();
1384 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001385 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001386 } else {
1387 /* return_type is the *expected* return type, not register value */
1388 DCHECK(!return_type.IsZero());
1389 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08001390 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07001391 // Disallow returning uninitialized values and verify that the reference in vAA is an
1392 // instance of the "return_type"
1393 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001394 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001395 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001396 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
Ian Rogers9074b992011-10-26 17:41:55 -07001397 << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001398 }
1399 }
1400 }
1401 break;
1402
1403 case Instruction::CONST_4:
1404 case Instruction::CONST_16:
1405 case Instruction::CONST:
1406 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001407 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const((int32_t) dec_insn.vB));
jeffhaobdb76512011-09-07 11:43:16 -07001408 break;
1409 case Instruction::CONST_HIGH16:
1410 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001411 work_line_->SetRegisterType(dec_insn.vA,
1412 reg_types_.FromCat1Const((int32_t) dec_insn.vB << 16));
jeffhaobdb76512011-09-07 11:43:16 -07001413 break;
1414 case Instruction::CONST_WIDE_16:
1415 case Instruction::CONST_WIDE_32:
1416 case Instruction::CONST_WIDE:
1417 case Instruction::CONST_WIDE_HIGH16:
1418 /* could be long or double; resolved upon use */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001419 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07001420 break;
1421 case Instruction::CONST_STRING:
1422 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001423 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001424 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001425 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001426 // Get type from instruction if unresolved then we need an access check
1427 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08001428 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001429 // Register holds class, ie its type is class, but on error we keep it Unknown
Elliott Hughesadb8c672012-03-06 16:49:32 -08001430 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers28ad40d2011-10-27 15:19:26 -07001431 res_type.IsUnknown() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001432 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001433 }
jeffhaobdb76512011-09-07 11:43:16 -07001434 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001435 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001436 break;
1437 case Instruction::MONITOR_EXIT:
1438 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001439 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001440 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001441 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001442 * to the need to handle asynchronous exceptions, a now-deprecated
1443 * feature that Dalvik doesn't support.)
1444 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001445 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001446 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001447 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001448 * structured locking checks are working, the former would have
1449 * failed on the -enter instruction, and the latter is impossible.
1450 *
1451 * This is fortunate, because issue 3221411 prevents us from
1452 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001453 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001454 * some catch blocks (which will show up as "dead" code when
1455 * we skip them here); if we can't, then the code path could be
1456 * "live" so we still need to check it.
1457 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001458 opcode_flags &= ~Instruction::kThrow;
1459 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001460 break;
1461
Ian Rogers28ad40d2011-10-27 15:19:26 -07001462 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001463 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001464 /*
1465 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1466 * could be a "upcast" -- not expected, so we don't try to address it.)
1467 *
1468 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001469 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001470 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001471 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001472 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001473 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogers9f1ab122011-12-12 08:52:43 -08001474 if (res_type.IsUnknown()) {
1475 CHECK_NE(failure_, VERIFY_ERROR_NONE);
1476 break; // couldn't resolve class
1477 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001478 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1479 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001480 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001481 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001482 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001483 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001484 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07001485 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001486 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001487 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001488 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001489 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001490 }
jeffhaobdb76512011-09-07 11:43:16 -07001491 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001492 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001493 }
1494 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001495 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001496 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001497 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001498 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001499 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001500 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001501 }
1502 }
1503 break;
1504 }
1505 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001506 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
jeffhao8cd6dda2012-02-22 10:15:34 -08001507 if (res_type.IsUnknown()) {
1508 CHECK_NE(failure_, VERIFY_ERROR_NONE);
1509 break; // couldn't resolve class
1510 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001511 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1512 // can't create an instance of an interface or abstract class */
1513 if (!res_type.IsInstantiableTypes()) {
1514 Fail(VERIFY_ERROR_INSTANTIATION)
1515 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001516 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001517 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1518 // Any registers holding previous allocations from this address that have not yet been
1519 // initialized must be marked invalid.
1520 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1521 // add the new uninitialized reference to the register state
Elliott Hughesadb8c672012-03-06 16:49:32 -08001522 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001523 }
1524 break;
1525 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001526 case Instruction::NEW_ARRAY:
1527 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001528 break;
1529 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08001530 VerifyNewArray(dec_insn, true, false);
1531 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001532 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001533 case Instruction::FILLED_NEW_ARRAY_RANGE:
1534 VerifyNewArray(dec_insn, true, true);
1535 just_set_result = true; // Filled new array range sets result register
1536 break;
jeffhaobdb76512011-09-07 11:43:16 -07001537 case Instruction::CMPL_FLOAT:
1538 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001539 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001540 break;
1541 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001542 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001543 break;
1544 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001545 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001546 break;
1547 case Instruction::CMPL_DOUBLE:
1548 case Instruction::CMPG_DOUBLE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001549 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08001550 break;
1551 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001552 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08001553 break;
1554 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001555 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001556 break;
1557 case Instruction::CMP_LONG:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001558 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08001559 break;
1560 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001561 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08001562 break;
1563 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001564 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001565 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001566 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001567 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001568 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001569 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001570 }
1571 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001572 }
jeffhaobdb76512011-09-07 11:43:16 -07001573 case Instruction::GOTO:
1574 case Instruction::GOTO_16:
1575 case Instruction::GOTO_32:
1576 /* no effect on or use of registers */
1577 break;
1578
1579 case Instruction::PACKED_SWITCH:
1580 case Instruction::SPARSE_SWITCH:
1581 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001582 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001583 break;
1584
Ian Rogersd81871c2011-10-03 13:57:23 -07001585 case Instruction::FILL_ARRAY_DATA: {
1586 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001587 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08001588 /* array_type can be null if the reg type is Zero */
1589 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001590 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001591 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001592 } else {
jeffhao457cc512012-02-02 16:55:13 -08001593 const RegType& component_type = reg_types_.GetComponentType(array_type,
1594 method_->GetDeclaringClass()->GetClassLoader());
1595 DCHECK(!component_type.IsUnknown());
1596 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001597 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1598 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001599 } else {
jeffhao457cc512012-02-02 16:55:13 -08001600 // Now verify if the element width in the table matches the element width declared in
1601 // the array
1602 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1603 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001604 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001605 } else {
1606 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1607 // Since we don't compress the data in Dex, expect to see equal width of data stored
1608 // in the table and expected from the array class.
1609 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001610 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1611 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001612 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001613 }
1614 }
jeffhaobdb76512011-09-07 11:43:16 -07001615 }
1616 }
1617 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001618 }
jeffhaobdb76512011-09-07 11:43:16 -07001619 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001620 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001621 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1622 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001623 bool mismatch = false;
1624 if (reg_type1.IsZero()) { // zero then integral or reference expected
1625 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1626 } else if (reg_type1.IsReferenceTypes()) { // both references?
1627 mismatch = !reg_type2.IsReferenceTypes();
1628 } else { // both integral?
1629 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1630 }
1631 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001632 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1633 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001634 }
1635 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001636 }
jeffhaobdb76512011-09-07 11:43:16 -07001637 case Instruction::IF_LT:
1638 case Instruction::IF_GE:
1639 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001640 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001641 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1642 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001643 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001644 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1645 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001646 }
1647 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001648 }
jeffhaobdb76512011-09-07 11:43:16 -07001649 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001650 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001651 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001652 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001653 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001654 }
jeffhaobdb76512011-09-07 11:43:16 -07001655 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001656 }
jeffhaobdb76512011-09-07 11:43:16 -07001657 case Instruction::IF_LTZ:
1658 case Instruction::IF_GEZ:
1659 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001660 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001661 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001662 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001663 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1664 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001665 }
jeffhaobdb76512011-09-07 11:43:16 -07001666 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001667 }
jeffhaobdb76512011-09-07 11:43:16 -07001668 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001669 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
1670 break;
jeffhaobdb76512011-09-07 11:43:16 -07001671 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001672 VerifyAGet(dec_insn, reg_types_.Byte(), true);
1673 break;
jeffhaobdb76512011-09-07 11:43:16 -07001674 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07001675 VerifyAGet(dec_insn, reg_types_.Char(), true);
1676 break;
jeffhaobdb76512011-09-07 11:43:16 -07001677 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001678 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001679 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001680 case Instruction::AGET:
1681 VerifyAGet(dec_insn, reg_types_.Integer(), true);
1682 break;
jeffhaobdb76512011-09-07 11:43:16 -07001683 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001684 VerifyAGet(dec_insn, reg_types_.Long(), true);
1685 break;
1686 case Instruction::AGET_OBJECT:
1687 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001688 break;
1689
Ian Rogersd81871c2011-10-03 13:57:23 -07001690 case Instruction::APUT_BOOLEAN:
1691 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
1692 break;
1693 case Instruction::APUT_BYTE:
1694 VerifyAPut(dec_insn, reg_types_.Byte(), true);
1695 break;
1696 case Instruction::APUT_CHAR:
1697 VerifyAPut(dec_insn, reg_types_.Char(), true);
1698 break;
1699 case Instruction::APUT_SHORT:
1700 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001701 break;
1702 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001703 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001704 break;
1705 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001706 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001707 break;
1708 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001709 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001710 break;
1711
jeffhaobdb76512011-09-07 11:43:16 -07001712 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001713 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001714 break;
jeffhaobdb76512011-09-07 11:43:16 -07001715 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001716 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001717 break;
jeffhaobdb76512011-09-07 11:43:16 -07001718 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001719 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001720 break;
jeffhaobdb76512011-09-07 11:43:16 -07001721 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001722 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001723 break;
1724 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001725 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001726 break;
1727 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001728 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001729 break;
1730 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001731 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001732 break;
jeffhaobdb76512011-09-07 11:43:16 -07001733
Ian Rogersd81871c2011-10-03 13:57:23 -07001734 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001735 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001736 break;
1737 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001738 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001739 break;
1740 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001741 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001742 break;
1743 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001744 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001745 break;
1746 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001747 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001748 break;
1749 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001750 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001751 break;
jeffhaobdb76512011-09-07 11:43:16 -07001752 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001753 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001754 break;
1755
jeffhaobdb76512011-09-07 11:43:16 -07001756 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001757 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001758 break;
jeffhaobdb76512011-09-07 11:43:16 -07001759 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001760 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001761 break;
jeffhaobdb76512011-09-07 11:43:16 -07001762 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001763 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001764 break;
jeffhaobdb76512011-09-07 11:43:16 -07001765 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001766 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001767 break;
1768 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001769 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001770 break;
1771 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001772 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001773 break;
1774 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001775 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001776 break;
1777
1778 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001779 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001780 break;
1781 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001782 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001783 break;
1784 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001785 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001786 break;
1787 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001788 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001789 break;
1790 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001791 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001792 break;
1793 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001794 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001795 break;
1796 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001797 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001798 break;
1799
1800 case Instruction::INVOKE_VIRTUAL:
1801 case Instruction::INVOKE_VIRTUAL_RANGE:
1802 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001803 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001804 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
1805 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
1806 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
1807 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001808 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
1809 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001810 const char* descriptor;
1811 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001812 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001813 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1814 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07001815 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001816 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001817 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001818 }
Ian Rogers9074b992011-10-26 17:41:55 -07001819 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07001820 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07001821 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07001822 just_set_result = true;
1823 }
1824 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001825 }
jeffhaobdb76512011-09-07 11:43:16 -07001826 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001827 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001828 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001829 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
1830 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07001831 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001832 * Some additional checks when calling a constructor. We know from the invocation arg check
1833 * that the "this" argument is an instance of called_method->klass. Now we further restrict
1834 * that to require that called_method->klass is the same as this->klass or this->super,
1835 * allowing the latter only if the "this" argument is the same as the "this" argument to
1836 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07001837 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001838 bool is_constructor;
1839 if (called_method != NULL) {
1840 is_constructor = called_method->IsConstructor();
1841 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001842 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001843 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1844 const char* name = dex_file_->GetMethodName(method_id);
1845 is_constructor = strcmp(name, "<init>") == 0;
1846 }
1847 if (is_constructor) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001848 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1849 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07001850 break;
1851
1852 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07001853 if (this_type.IsZero()) {
jeffhaod5347e02012-03-22 17:25:05 -07001854 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07001855 break;
1856 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001857 if (called_method != NULL) {
1858 Class* this_class = this_type.GetClass();
1859 DCHECK(this_class != NULL);
1860 /* must be in same class or in superclass */
1861 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
1862 if (this_class != method_->GetDeclaringClass()) {
jeffhaod5347e02012-03-22 17:25:05 -07001863 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogers28ad40d2011-10-27 15:19:26 -07001864 << "invoke-direct <init> on super only allowed for 'this' in <init>";
1865 break;
1866 }
1867 } else if (called_method->GetDeclaringClass() != this_class) {
jeffhaod5347e02012-03-22 17:25:05 -07001868 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07001869 break;
1870 }
jeffhaobdb76512011-09-07 11:43:16 -07001871 }
1872
1873 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07001874 if (!this_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001875 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
Ian Rogersd81871c2011-10-03 13:57:23 -07001876 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07001877 break;
1878 }
1879
1880 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07001881 * Replace the uninitialized reference with an initialized one. We need to do this for all
1882 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07001883 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001884 work_line_->MarkRefsAsInitialized(this_type);
1885 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07001886 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07001887 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001888 const char* descriptor;
1889 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001890 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001891 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1892 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07001893 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001894 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001895 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001896 }
Ian Rogers9074b992011-10-26 17:41:55 -07001897 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07001898 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07001899 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07001900 just_set_result = true;
1901 }
1902 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001903 }
jeffhaobdb76512011-09-07 11:43:16 -07001904 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07001905 case Instruction::INVOKE_STATIC_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001906 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001907 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
1908 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001909 const char* descriptor;
1910 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001911 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001912 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1913 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07001914 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001915 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001916 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001917 }
Ian Rogers9074b992011-10-26 17:41:55 -07001918 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07001919 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07001920 work_line_->SetResultRegisterType(return_type);
1921 just_set_result = true;
1922 }
jeffhaobdb76512011-09-07 11:43:16 -07001923 }
1924 break;
1925 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001926 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001927 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001928 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
1929 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001930 if (abs_method != NULL) {
1931 Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersf3c1f782011-11-02 14:12:15 -07001932 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001933 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
1934 << PrettyMethod(abs_method) << "'";
1935 break;
1936 }
1937 }
1938 /* Get the type of the "this" arg, which should either be a sub-interface of called
1939 * interface or Object (see comments in RegType::JoinClass).
1940 */
1941 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1942 if (failure_ == VERIFY_ERROR_NONE) {
1943 if (this_type.IsZero()) {
1944 /* null pointer always passes (and always fails at runtime) */
1945 } else {
1946 if (this_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001947 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
Ian Rogers28ad40d2011-10-27 15:19:26 -07001948 << this_type;
1949 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001950 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001951 // In the past we have tried to assert that "called_interface" is assignable
1952 // from "this_type.GetClass()", however, as we do an imprecise Join
1953 // (RegType::JoinClass) we don't have full information on what interfaces are
1954 // implemented by "this_type". For example, two classes may implement the same
1955 // interfaces and have a common parent that doesn't implement the interface. The
1956 // join will set "this_type" to the parent class and a test that this implements
1957 // the interface will incorrectly fail.
jeffhaobdb76512011-09-07 11:43:16 -07001958 }
1959 }
jeffhaobdb76512011-09-07 11:43:16 -07001960 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001961 * We don't have an object instance, so we can't find the concrete method. However, all of
1962 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07001963 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001964 const char* descriptor;
1965 if (abs_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001966 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001967 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1968 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07001969 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001970 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001971 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001972 }
Ian Rogers9074b992011-10-26 17:41:55 -07001973 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07001974 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
1975 work_line_->SetResultRegisterType(return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001976 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07001977 just_set_result = true;
1978 }
1979 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001980 }
jeffhaobdb76512011-09-07 11:43:16 -07001981 case Instruction::NEG_INT:
1982 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001983 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001984 break;
1985 case Instruction::NEG_LONG:
1986 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07001987 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07001988 break;
1989 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001990 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07001991 break;
1992 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001993 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07001994 break;
1995 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07001996 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001997 break;
1998 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001999 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002000 break;
2001 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002002 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002003 break;
2004 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002005 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002006 break;
2007 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002008 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002009 break;
2010 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002011 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002012 break;
2013 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002014 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002015 break;
2016 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002017 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002018 break;
2019 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002020 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002021 break;
2022 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002023 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002024 break;
2025 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002026 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002027 break;
2028 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002029 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002030 break;
2031 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002032 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002033 break;
2034 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002035 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002036 break;
2037 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002038 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002039 break;
2040
2041 case Instruction::ADD_INT:
2042 case Instruction::SUB_INT:
2043 case Instruction::MUL_INT:
2044 case Instruction::REM_INT:
2045 case Instruction::DIV_INT:
2046 case Instruction::SHL_INT:
2047 case Instruction::SHR_INT:
2048 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002049 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002050 break;
2051 case Instruction::AND_INT:
2052 case Instruction::OR_INT:
2053 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002054 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002055 break;
2056 case Instruction::ADD_LONG:
2057 case Instruction::SUB_LONG:
2058 case Instruction::MUL_LONG:
2059 case Instruction::DIV_LONG:
2060 case Instruction::REM_LONG:
2061 case Instruction::AND_LONG:
2062 case Instruction::OR_LONG:
2063 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002064 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002065 break;
2066 case Instruction::SHL_LONG:
2067 case Instruction::SHR_LONG:
2068 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002069 /* shift distance is Int, making these different from other binary operations */
2070 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002071 break;
2072 case Instruction::ADD_FLOAT:
2073 case Instruction::SUB_FLOAT:
2074 case Instruction::MUL_FLOAT:
2075 case Instruction::DIV_FLOAT:
2076 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002077 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002078 break;
2079 case Instruction::ADD_DOUBLE:
2080 case Instruction::SUB_DOUBLE:
2081 case Instruction::MUL_DOUBLE:
2082 case Instruction::DIV_DOUBLE:
2083 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002084 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002085 break;
2086 case Instruction::ADD_INT_2ADDR:
2087 case Instruction::SUB_INT_2ADDR:
2088 case Instruction::MUL_INT_2ADDR:
2089 case Instruction::REM_INT_2ADDR:
2090 case Instruction::SHL_INT_2ADDR:
2091 case Instruction::SHR_INT_2ADDR:
2092 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002093 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002094 break;
2095 case Instruction::AND_INT_2ADDR:
2096 case Instruction::OR_INT_2ADDR:
2097 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002098 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002099 break;
2100 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002101 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002102 break;
2103 case Instruction::ADD_LONG_2ADDR:
2104 case Instruction::SUB_LONG_2ADDR:
2105 case Instruction::MUL_LONG_2ADDR:
2106 case Instruction::DIV_LONG_2ADDR:
2107 case Instruction::REM_LONG_2ADDR:
2108 case Instruction::AND_LONG_2ADDR:
2109 case Instruction::OR_LONG_2ADDR:
2110 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002112 break;
2113 case Instruction::SHL_LONG_2ADDR:
2114 case Instruction::SHR_LONG_2ADDR:
2115 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002116 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002117 break;
2118 case Instruction::ADD_FLOAT_2ADDR:
2119 case Instruction::SUB_FLOAT_2ADDR:
2120 case Instruction::MUL_FLOAT_2ADDR:
2121 case Instruction::DIV_FLOAT_2ADDR:
2122 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002123 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002124 break;
2125 case Instruction::ADD_DOUBLE_2ADDR:
2126 case Instruction::SUB_DOUBLE_2ADDR:
2127 case Instruction::MUL_DOUBLE_2ADDR:
2128 case Instruction::DIV_DOUBLE_2ADDR:
2129 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002130 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002131 break;
2132 case Instruction::ADD_INT_LIT16:
2133 case Instruction::RSUB_INT:
2134 case Instruction::MUL_INT_LIT16:
2135 case Instruction::DIV_INT_LIT16:
2136 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002137 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002138 break;
2139 case Instruction::AND_INT_LIT16:
2140 case Instruction::OR_INT_LIT16:
2141 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002142 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002143 break;
2144 case Instruction::ADD_INT_LIT8:
2145 case Instruction::RSUB_INT_LIT8:
2146 case Instruction::MUL_INT_LIT8:
2147 case Instruction::DIV_INT_LIT8:
2148 case Instruction::REM_INT_LIT8:
2149 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002150 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002151 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002152 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002153 break;
2154 case Instruction::AND_INT_LIT8:
2155 case Instruction::OR_INT_LIT8:
2156 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002157 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002158 break;
2159
2160 /*
2161 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002162 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002163 * inserted in the course of verification, we can expect to see it here.
2164 */
jeffhaob4df5142011-09-19 20:25:32 -07002165 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002166 break;
2167
Ian Rogersd81871c2011-10-03 13:57:23 -07002168 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002169 case Instruction::UNUSED_EE:
2170 case Instruction::UNUSED_EF:
2171 case Instruction::UNUSED_F2:
2172 case Instruction::UNUSED_F3:
2173 case Instruction::UNUSED_F4:
2174 case Instruction::UNUSED_F5:
2175 case Instruction::UNUSED_F6:
2176 case Instruction::UNUSED_F7:
2177 case Instruction::UNUSED_F8:
2178 case Instruction::UNUSED_F9:
2179 case Instruction::UNUSED_FA:
2180 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002181 case Instruction::UNUSED_F0:
2182 case Instruction::UNUSED_F1:
2183 case Instruction::UNUSED_E3:
2184 case Instruction::UNUSED_E8:
2185 case Instruction::UNUSED_E7:
2186 case Instruction::UNUSED_E4:
2187 case Instruction::UNUSED_E9:
2188 case Instruction::UNUSED_FC:
2189 case Instruction::UNUSED_E5:
2190 case Instruction::UNUSED_EA:
2191 case Instruction::UNUSED_FD:
2192 case Instruction::UNUSED_E6:
2193 case Instruction::UNUSED_EB:
2194 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002195 case Instruction::UNUSED_3E:
2196 case Instruction::UNUSED_3F:
2197 case Instruction::UNUSED_40:
2198 case Instruction::UNUSED_41:
2199 case Instruction::UNUSED_42:
2200 case Instruction::UNUSED_43:
2201 case Instruction::UNUSED_73:
2202 case Instruction::UNUSED_79:
2203 case Instruction::UNUSED_7A:
2204 case Instruction::UNUSED_EC:
2205 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002206 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002207 break;
2208
2209 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002210 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002211 * complain if an instruction is missing (which is desirable).
2212 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002213 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002214
Ian Rogersd81871c2011-10-03 13:57:23 -07002215 if (failure_ != VERIFY_ERROR_NONE) {
jeffhaod5347e02012-03-22 17:25:05 -07002216 if (failure_ == VERIFY_ERROR_BAD_CLASS_HARD || failure_ == VERIFY_ERROR_BAD_CLASS_SOFT) {
jeffhaobdb76512011-09-07 11:43:16 -07002217 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002218 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002219 return false;
2220 } else {
2221 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002222 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002223 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002224 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002225 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002226 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002227 failure_ = VERIFY_ERROR_NONE;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002228 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002229 }
2230 }
jeffhaobdb76512011-09-07 11:43:16 -07002231 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002232 * If we didn't just set the result register, clear it out. This ensures that you can only use
2233 * "move-result" immediately after the result is set. (We could check this statically, but it's
2234 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002235 */
2236 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002237 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002238 }
2239
jeffhaoa0a764a2011-09-16 10:43:38 -07002240 /* Handle "continue". Tag the next consecutive instruction. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002241 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers776ac1f2012-04-13 23:36:36 -07002242 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
Ian Rogersd81871c2011-10-03 13:57:23 -07002243 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
jeffhaod5347e02012-03-22 17:25:05 -07002244 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002245 return false;
2246 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002247 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2248 // next instruction isn't one.
jeffhaod5347e02012-03-22 17:25:05 -07002249 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002250 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002251 }
2252 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2253 if (next_line != NULL) {
2254 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2255 // needed.
2256 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002257 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002258 }
jeffhaobdb76512011-09-07 11:43:16 -07002259 } else {
2260 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002261 * We're not recording register data for the next instruction, so we don't know what the prior
2262 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002263 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002264 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002265 }
2266 }
2267
2268 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002269 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002270 *
2271 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002272 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002273 * somebody could get a reference field, check it for zero, and if the
2274 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002275 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002276 * that, and will reject the code.
2277 *
2278 * TODO: avoid re-fetching the branch target
2279 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002280 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002281 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002282 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002283 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002284 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002285 return false;
2286 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002287 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002288 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002289 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002290 }
jeffhaobdb76512011-09-07 11:43:16 -07002291 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002292 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002293 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002294 }
jeffhaobdb76512011-09-07 11:43:16 -07002295 }
2296
2297 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002298 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002299 *
2300 * We've already verified that the table is structurally sound, so we
2301 * just need to walk through and tag the targets.
2302 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002303 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002304 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2305 const uint16_t* switch_insns = insns + offset_to_switch;
2306 int switch_count = switch_insns[1];
2307 int offset_to_targets, targ;
2308
2309 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2310 /* 0 = sig, 1 = count, 2/3 = first key */
2311 offset_to_targets = 4;
2312 } else {
2313 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002314 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002315 offset_to_targets = 2 + 2 * switch_count;
2316 }
2317
2318 /* verify each switch target */
2319 for (targ = 0; targ < switch_count; targ++) {
2320 int offset;
2321 uint32_t abs_offset;
2322
2323 /* offsets are 32-bit, and only partly endian-swapped */
2324 offset = switch_insns[offset_to_targets + targ * 2] |
2325 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002326 abs_offset = work_insn_idx_ + offset;
2327 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002328 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002329 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002330 }
2331 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002332 return false;
2333 }
2334 }
2335
2336 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002337 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2338 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002339 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002340 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002341 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002342 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002343
Ian Rogers0571d352011-11-03 19:51:38 -07002344 for (; iterator.HasNext(); iterator.Next()) {
2345 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002346 within_catch_all = true;
2347 }
jeffhaobdb76512011-09-07 11:43:16 -07002348 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002349 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2350 * "work_regs", because at runtime the exception will be thrown before the instruction
2351 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002352 */
Ian Rogers0571d352011-11-03 19:51:38 -07002353 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002354 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002355 }
jeffhaobdb76512011-09-07 11:43:16 -07002356 }
2357
2358 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002359 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2360 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002361 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002362 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002363 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002364 * The state in work_line reflects the post-execution state. If the current instruction is a
2365 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002366 * it will do so before grabbing the lock).
2367 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002368 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002369 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002370 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002371 return false;
2372 }
2373 }
2374 }
2375
jeffhaod1f0fde2011-09-08 17:25:33 -07002376 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002377 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002378 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002379 return false;
2380 }
jeffhaobdb76512011-09-07 11:43:16 -07002381 }
2382
2383 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002384 * Update start_guess. Advance to the next instruction of that's
2385 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002386 * neither of those exists we're in a return or throw; leave start_guess
2387 * alone and let the caller sort it out.
2388 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002389 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002390 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002391 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002392 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002393 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002394 }
2395
Ian Rogersd81871c2011-10-03 13:57:23 -07002396 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2397 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002398
2399 return true;
2400}
2401
Ian Rogers776ac1f2012-04-13 23:36:36 -07002402const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002403 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002404 Class* referrer = method_->GetDeclaringClass();
2405 Class* klass = method_->GetDexCacheResolvedTypes()->Get(class_idx);
2406 const RegType& result =
2407 klass != NULL ? reg_types_.FromClass(klass)
2408 : reg_types_.FromDescriptor(referrer->GetClassLoader(), descriptor);
2409 if (klass == NULL && !result.IsUnresolvedTypes()) {
2410 method_->GetDexCacheResolvedTypes()->Set(class_idx, result.GetClass());
Ian Rogersd81871c2011-10-03 13:57:23 -07002411 }
jeffhaod1224c72012-02-29 13:43:08 -08002412 if (result.IsUnknown()) {
jeffhaod5347e02012-03-22 17:25:05 -07002413 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing unknown class in " << PrettyDescriptor(referrer);
jeffhaod1224c72012-02-29 13:43:08 -08002414 return result;
2415 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002416 // Check if access is allowed. Unresolved types use AllocObjectFromCodeWithAccessCheck to
2417 // check at runtime if access is allowed and so pass here.
2418 if (!result.IsUnresolvedTypes() && !referrer->CanAccess(result.GetClass())) {
2419 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002420 << PrettyDescriptor(referrer) << "' -> '"
Ian Rogers28ad40d2011-10-27 15:19:26 -07002421 << result << "'";
2422 return reg_types_.Unknown();
2423 } else {
2424 return result;
2425 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002426}
2427
Ian Rogers776ac1f2012-04-13 23:36:36 -07002428const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002429 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002430 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002431 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002432 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2433 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002434 CatchHandlerIterator iterator(handlers_ptr);
2435 for (; iterator.HasNext(); iterator.Next()) {
2436 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2437 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002438 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07002439 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002440 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002441 if (common_super == NULL) {
2442 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2443 // as that is caught at runtime
2444 common_super = &exception;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002445 } else if (!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002446 // We don't know enough about the type and the common path merge will result in
2447 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002448 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002449 return reg_types_.Unknown();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002450 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002451 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002452 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002453 common_super = &common_super->Merge(exception, &reg_types_);
2454 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002455 }
2456 }
2457 }
2458 }
Ian Rogers0571d352011-11-03 19:51:38 -07002459 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002460 }
2461 }
2462 if (common_super == NULL) {
2463 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002464 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersc4762272012-02-01 15:55:55 -08002465 return reg_types_.Unknown();
Ian Rogersd81871c2011-10-03 13:57:23 -07002466 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002467 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002468}
2469
Ian Rogers776ac1f2012-04-13 23:36:36 -07002470Method* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type) {
Ian Rogers90040192011-12-16 08:54:29 -08002471 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2472 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
2473 if (failure_ != VERIFY_ERROR_NONE) {
2474 fail_messages_ << " in attempt to access method " << dex_file_->GetMethodName(method_id);
2475 return NULL;
2476 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002477 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002478 return NULL; // Can't resolve Class so no more to do here
2479 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002480 Class* klass = klass_type.GetClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002481 Class* referrer = method_->GetDeclaringClass();
2482 DexCache* dex_cache = referrer->GetDexCache();
2483 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
2484 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002485 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002486 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002487
2488 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002489 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002490 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002491 res_method = klass->FindInterfaceMethod(name, signature);
2492 } else {
2493 res_method = klass->FindVirtualMethod(name, signature);
2494 }
2495 if (res_method != NULL) {
2496 dex_cache->SetResolvedMethod(method_idx, res_method);
2497 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002498 // If a virtual or interface method wasn't found with the expected type, look in
2499 // the direct methods. This can happen when the wrong invoke type is used or when
2500 // a class has changed, and will be flagged as an error in later checks.
2501 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2502 res_method = klass->FindDirectMethod(name, signature);
2503 }
2504 if (res_method == NULL) {
2505 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2506 << PrettyDescriptor(klass) << "." << name
2507 << " " << signature;
2508 return NULL;
2509 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002510 }
2511 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002512 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2513 // enforce them here.
2514 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002515 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2516 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002517 return NULL;
2518 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002519 // Disallow any calls to class initializers.
2520 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002521 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2522 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002523 return NULL;
2524 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002525 // Check if access is allowed.
2526 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
2527 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
2528 << " from " << PrettyDescriptor(referrer) << ")";
2529 return NULL;
2530 }
jeffhaode0d9c92012-02-27 13:58:13 -08002531 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2532 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002533 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2534 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002535 return NULL;
2536 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002537 // Check that interface methods match interface classes.
2538 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2539 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2540 << " is in an interface class " << PrettyClass(klass);
2541 return NULL;
2542 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2543 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2544 << " is in a non-interface class " << PrettyClass(klass);
2545 return NULL;
2546 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002547 // See if the method type implied by the invoke instruction matches the access flags for the
2548 // target method.
2549 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2550 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2551 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2552 ) {
Ian Rogers573db4a2011-12-13 15:30:50 -08002553 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type does not match method type of "
2554 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002555 return NULL;
2556 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002557 return res_method;
2558}
2559
Ian Rogers776ac1f2012-04-13 23:36:36 -07002560Method* MethodVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
jeffhao8cd6dda2012-02-22 10:15:34 -08002561 MethodType method_type, bool is_range, bool is_super) {
2562 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2563 // we're making.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002564 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002565 if (res_method == NULL) { // error or class is unresolved
2566 return NULL;
2567 }
2568
Ian Rogersd81871c2011-10-03 13:57:23 -07002569 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2570 // has a vtable entry for the target method.
2571 if (is_super) {
2572 DCHECK(method_type == METHOD_VIRTUAL);
2573 Class* super = method_->GetDeclaringClass()->GetSuperClass();
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002574 if (super == NULL || res_method->GetMethodIndex() >= super->GetVTable()->GetLength()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002575 if (super == NULL) { // Only Object has no super class
2576 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
2577 << " to super " << PrettyMethod(res_method);
2578 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002579 MethodHelper mh(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002580 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002581 << " to super " << PrettyDescriptor(super)
2582 << "." << mh.GetName()
2583 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002584 }
2585 return NULL;
2586 }
2587 }
2588 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2589 // match the call to the signature. Also, we might might be calling through an abstract method
2590 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08002591 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002592 /* caught by static verifier */
2593 DCHECK(is_range || expected_args <= 5);
2594 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002595 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002596 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2597 return NULL;
2598 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002599
jeffhaobdb76512011-09-07 11:43:16 -07002600 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002601 * Check the "this" argument, which must be an instance of the class
2602 * that declared the method. For an interface class, we don't do the
2603 * full interface merge, so we can't do a rigorous check here (which
2604 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002605 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002606 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002607 if (!res_method->IsStatic()) {
2608 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
2609 if (failure_ != VERIFY_ERROR_NONE) {
2610 return NULL;
2611 }
2612 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002613 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002614 return NULL;
2615 }
2616 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07002617 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
2618 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002619 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002620 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002621 return NULL;
2622 }
2623 }
2624 actual_args++;
2625 }
2626 /*
2627 * Process the target method's signature. This signature may or may not
2628 * have been verified, so we can't assume it's properly formed.
2629 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002630 MethodHelper mh(res_method);
2631 const DexFile::TypeList* params = mh.GetParameterTypeList();
2632 size_t params_size = params == NULL ? 0 : params->Size();
2633 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002634 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002635 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002636 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2637 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002638 return NULL;
2639 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002640 const char* descriptor =
2641 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2642 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002643 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002644 << " missing signature component";
2645 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002646 }
2647 const RegType& reg_type =
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002648 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002649 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002650 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
2651 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002652 }
2653 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2654 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002655 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002656 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002657 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002658 return NULL;
2659 } else {
2660 return res_method;
2661 }
2662}
2663
Ian Rogers776ac1f2012-04-13 23:36:36 -07002664const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002665 return reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
2666 MethodHelper(method_).GetReturnTypeDescriptor());
2667}
2668
Ian Rogers776ac1f2012-04-13 23:36:36 -07002669void MethodVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08002670 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002671 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002672 if (res_type.IsUnknown()) {
2673 CHECK_NE(failure_, VERIFY_ERROR_NONE);
2674 } else {
2675 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2676 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002677 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002678 } else if (!is_filled) {
2679 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002680 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002681 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002682 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002683 } else {
2684 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2685 // the list and fail. It's legal, if silly, for arg_count to be zero.
2686 const RegType& expected_type = reg_types_.GetComponentType(res_type,
2687 method_->GetDeclaringClass()->GetClassLoader());
Elliott Hughesadb8c672012-03-06 16:49:32 -08002688 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002689 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002690 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002691 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
2692 work_line_->SetResultRegisterType(reg_types_.Unknown());
2693 return;
2694 }
2695 }
2696 // filled-array result goes into "result" register
2697 work_line_->SetResultRegisterType(res_type);
2698 }
2699 }
2700}
2701
Ian Rogers776ac1f2012-04-13 23:36:36 -07002702void MethodVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002703 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002704 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002705 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002706 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002707 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002708 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002709 if (array_type.IsZero()) {
2710 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2711 // instruction type. TODO: have a proper notion of bottom here.
2712 if (!is_primitive || insn_type.IsCategory1Types()) {
2713 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08002714 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002715 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002716 // Category 2
Elliott Hughesadb8c672012-03-06 16:49:32 -08002717 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
Ian Rogers89310de2012-02-01 13:47:30 -08002718 }
jeffhaofc3144e2012-02-01 17:21:15 -08002719 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002720 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002721 } else {
2722 /* verify the class */
2723 const RegType& component_type = reg_types_.GetComponentType(array_type,
2724 method_->GetDeclaringClass()->GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08002725 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002726 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002727 << " source for aget-object";
2728 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002729 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002730 << " source for category 1 aget";
2731 } else if (is_primitive && !insn_type.Equals(component_type) &&
2732 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2733 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogersd81871c2011-10-03 13:57:23 -07002735 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002736 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002737 // Use knowledge of the field type which is stronger than the type inferred from the
2738 // instruction, which can't differentiate object types and ints from floats, longs from
2739 // doubles.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002740 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002741 }
2742 }
2743 }
2744}
2745
Ian Rogers776ac1f2012-04-13 23:36:36 -07002746void MethodVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002747 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002748 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002749 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002750 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002751 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002752 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002753 if (array_type.IsZero()) {
2754 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2755 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002756 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002757 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002758 } else {
2759 /* verify the class */
2760 const RegType& component_type = reg_types_.GetComponentType(array_type,
2761 method_->GetDeclaringClass()->GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08002762 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002763 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002764 << " source for aput-object";
2765 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002766 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002767 << " source for category 1 aput";
2768 } else if (is_primitive && !insn_type.Equals(component_type) &&
2769 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2770 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002771 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002772 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002773 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002774 // The instruction agrees with the type of array, confirm the value to be stored does too
2775 // Note: we use the instruction type (rather than the component type) for aput-object as
2776 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08002777 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002778 }
2779 }
2780 }
2781}
2782
Ian Rogers776ac1f2012-04-13 23:36:36 -07002783Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002784 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2785 // Check access to class
2786 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
2787 if (failure_ != VERIFY_ERROR_NONE) {
2788 fail_messages_ << " in attempt to access static field " << field_idx << " ("
2789 << dex_file_->GetFieldName(field_id) << ") in "
2790 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
2791 return NULL;
2792 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002793 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002794 return NULL; // Can't resolve Class so no more to do here
2795 }
Ian Rogersb067ac22011-12-13 18:05:09 -08002796 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002797 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002798 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
2799 << dex_file_->GetFieldName(field_id) << ") in "
2800 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002801 DCHECK(Thread::Current()->IsExceptionPending());
2802 Thread::Current()->ClearException();
2803 return NULL;
2804 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
2805 field->GetAccessFlags())) {
2806 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
2807 << " from " << PrettyClass(method_->GetDeclaringClass());
2808 return NULL;
2809 } else if (!field->IsStatic()) {
2810 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
2811 return NULL;
2812 } else {
2813 return field;
2814 }
2815}
2816
Ian Rogers776ac1f2012-04-13 23:36:36 -07002817Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002818 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2819 // Check access to class
2820 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
2821 if (failure_ != VERIFY_ERROR_NONE) {
2822 fail_messages_ << " in attempt to access instance field " << field_idx << " ("
2823 << dex_file_->GetFieldName(field_id) << ") in "
2824 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
2825 return NULL;
2826 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002827 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002828 return NULL; // Can't resolve Class so no more to do here
2829 }
Ian Rogersb067ac22011-12-13 18:05:09 -08002830 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002831 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002832 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
2833 << dex_file_->GetFieldName(field_id) << ") in "
2834 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002835 DCHECK(Thread::Current()->IsExceptionPending());
2836 Thread::Current()->ClearException();
2837 return NULL;
2838 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
2839 field->GetAccessFlags())) {
2840 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
2841 << " from " << PrettyClass(method_->GetDeclaringClass());
2842 return NULL;
2843 } else if (field->IsStatic()) {
2844 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
2845 << " to not be static";
2846 return NULL;
2847 } else if (obj_type.IsZero()) {
2848 // Cannot infer and check type, however, access will cause null pointer exception
2849 return field;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002850 } else if (obj_type.IsUninitializedTypes() &&
2851 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
2852 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002853 // Field accesses through uninitialized references are only allowable for constructors where
2854 // the field is declared in this class
jeffhaod5347e02012-03-22 17:25:05 -07002855 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
2856 << " of a not fully initialized object within the context of "
2857 << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002858 return NULL;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002859 } else if (!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002860 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
2861 // of C1. For resolution to occur the declared class of the field must be compatible with
2862 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
2863 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
2864 << " from object of type " << PrettyClass(obj_type.GetClass());
2865 return NULL;
2866 } else {
2867 return field;
2868 }
2869}
2870
Ian Rogers776ac1f2012-04-13 23:36:36 -07002871void MethodVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002872 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002873 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002874 Field* field;
2875 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002876 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002877 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002878 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07002879 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002880 }
Ian Rogersf4028cc2011-11-02 14:56:39 -07002881 if (failure_ != VERIFY_ERROR_NONE) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002882 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Unknown());
Ian Rogersf4028cc2011-11-02 14:56:39 -07002883 } else {
2884 const char* descriptor;
2885 const ClassLoader* loader;
2886 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002887 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogersf4028cc2011-11-02 14:56:39 -07002888 loader = field->GetDeclaringClass()->GetClassLoader();
2889 } else {
2890 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2891 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2892 loader = method_->GetDeclaringClass()->GetClassLoader();
2893 }
2894 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002895 if (is_primitive) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07002896 if (field_type.Equals(insn_type) ||
2897 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
2898 (field_type.IsDouble() && insn_type.IsLongTypes())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002899 // expected that read is of the correct primitive type or that int reads are reading
2900 // floats or long reads are reading doubles
2901 } else {
2902 // This is a global failure rather than a class change failure as the instructions and
2903 // the descriptors for the type should have been consistent within the same file at
2904 // compile time
jeffhaod5347e02012-03-22 17:25:05 -07002905 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
2906 << " to be of type '" << insn_type
2907 << "' but found type '" << field_type << "' in get";
Ian Rogersd81871c2011-10-03 13:57:23 -07002908 return;
2909 }
2910 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07002911 if (!insn_type.IsAssignableFrom(field_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002912 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
2913 << " to be compatible with type '" << insn_type
2914 << "' but found type '" << field_type
2915 << "' in get-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07002916 return;
2917 }
2918 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002919 work_line_->SetRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002920 }
2921}
2922
Ian Rogers776ac1f2012-04-13 23:36:36 -07002923void MethodVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002924 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002925 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002926 Field* field;
2927 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07002928 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002929 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002930 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07002931 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002932 }
Ian Rogers55d249f2011-11-02 16:48:09 -07002933 if (failure_ != VERIFY_ERROR_NONE) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002934 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Unknown());
Ian Rogers55d249f2011-11-02 16:48:09 -07002935 } else {
2936 const char* descriptor;
2937 const ClassLoader* loader;
2938 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002939 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogers55d249f2011-11-02 16:48:09 -07002940 loader = field->GetDeclaringClass()->GetClassLoader();
2941 } else {
2942 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2943 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2944 loader = method_->GetDeclaringClass()->GetClassLoader();
Ian Rogersd81871c2011-10-03 13:57:23 -07002945 }
Ian Rogers55d249f2011-11-02 16:48:09 -07002946 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
2947 if (field != NULL) {
2948 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
2949 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
2950 << " from other class " << PrettyClass(method_->GetDeclaringClass());
2951 return;
2952 }
2953 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002954 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07002955 // Primitive field assignability rules are weaker than regular assignability rules
2956 bool instruction_compatible;
2957 bool value_compatible;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002958 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers2c8a8572011-10-24 17:11:36 -07002959 if (field_type.IsIntegralTypes()) {
2960 instruction_compatible = insn_type.IsIntegralTypes();
2961 value_compatible = value_type.IsIntegralTypes();
2962 } else if (field_type.IsFloat()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07002963 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
Ian Rogers2c8a8572011-10-24 17:11:36 -07002964 value_compatible = value_type.IsFloatTypes();
2965 } else if (field_type.IsLong()) {
2966 instruction_compatible = insn_type.IsLong();
2967 value_compatible = value_type.IsLongTypes();
2968 } else if (field_type.IsDouble()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07002969 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
Ian Rogers2c8a8572011-10-24 17:11:36 -07002970 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07002971 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07002972 instruction_compatible = false; // reference field with primitive store
2973 value_compatible = false; // unused
2974 }
2975 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002976 // This is a global failure rather than a class change failure as the instructions and
2977 // the descriptors for the type should have been consistent within the same file at
2978 // compile time
jeffhaod5347e02012-03-22 17:25:05 -07002979 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
2980 << " to be of type '" << insn_type
2981 << "' but found type '" << field_type
2982 << "' in put";
Ian Rogersd81871c2011-10-03 13:57:23 -07002983 return;
2984 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07002985 if (!value_compatible) {
jeffhaod5347e02012-03-22 17:25:05 -07002986 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
2987 << " of type " << value_type
2988 << " but expected " << field_type
2989 << " for store to " << PrettyField(field) << " in put";
Ian Rogers2c8a8572011-10-24 17:11:36 -07002990 return;
2991 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002992 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07002993 if (!insn_type.IsAssignableFrom(field_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002994 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
2995 << " to be compatible with type '" << insn_type
2996 << "' but found type '" << field_type
2997 << "' in put-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07002998 return;
2999 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003000 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003001 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003002 }
3003}
3004
Ian Rogers776ac1f2012-04-13 23:36:36 -07003005bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003006 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003007 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003008 return false;
3009 }
3010 return true;
3011}
3012
Ian Rogers776ac1f2012-04-13 23:36:36 -07003013void MethodVerifier::ReplaceFailingInstruction() {
Ian Rogersf1864ef2011-12-09 12:39:48 -08003014 if (Runtime::Current()->IsStarted()) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003015 LOG(ERROR) << "Verification attempting to replace instructions in " << PrettyMethod(method_)
Ian Rogersf1864ef2011-12-09 12:39:48 -08003016 << " " << fail_messages_.str();
3017 return;
3018 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003019 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3020 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3021 VerifyErrorRefType ref_type;
3022 switch (inst->Opcode()) {
3023 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003024 case Instruction::CHECK_CAST:
3025 case Instruction::INSTANCE_OF:
3026 case Instruction::NEW_INSTANCE:
3027 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003028 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003029 case Instruction::FILLED_NEW_ARRAY_RANGE:
3030 ref_type = VERIFY_ERROR_REF_CLASS;
3031 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003032 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003033 case Instruction::IGET_BOOLEAN:
3034 case Instruction::IGET_BYTE:
3035 case Instruction::IGET_CHAR:
3036 case Instruction::IGET_SHORT:
3037 case Instruction::IGET_WIDE:
3038 case Instruction::IGET_OBJECT:
3039 case Instruction::IPUT:
3040 case Instruction::IPUT_BOOLEAN:
3041 case Instruction::IPUT_BYTE:
3042 case Instruction::IPUT_CHAR:
3043 case Instruction::IPUT_SHORT:
3044 case Instruction::IPUT_WIDE:
3045 case Instruction::IPUT_OBJECT:
3046 case Instruction::SGET:
3047 case Instruction::SGET_BOOLEAN:
3048 case Instruction::SGET_BYTE:
3049 case Instruction::SGET_CHAR:
3050 case Instruction::SGET_SHORT:
3051 case Instruction::SGET_WIDE:
3052 case Instruction::SGET_OBJECT:
3053 case Instruction::SPUT:
3054 case Instruction::SPUT_BOOLEAN:
3055 case Instruction::SPUT_BYTE:
3056 case Instruction::SPUT_CHAR:
3057 case Instruction::SPUT_SHORT:
3058 case Instruction::SPUT_WIDE:
3059 case Instruction::SPUT_OBJECT:
3060 ref_type = VERIFY_ERROR_REF_FIELD;
3061 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003062 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003063 case Instruction::INVOKE_VIRTUAL_RANGE:
3064 case Instruction::INVOKE_SUPER:
3065 case Instruction::INVOKE_SUPER_RANGE:
3066 case Instruction::INVOKE_DIRECT:
3067 case Instruction::INVOKE_DIRECT_RANGE:
3068 case Instruction::INVOKE_STATIC:
3069 case Instruction::INVOKE_STATIC_RANGE:
3070 case Instruction::INVOKE_INTERFACE:
3071 case Instruction::INVOKE_INTERFACE_RANGE:
3072 ref_type = VERIFY_ERROR_REF_METHOD;
3073 break;
jeffhaobdb76512011-09-07 11:43:16 -07003074 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003075 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003076 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003077 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003078 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3079 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3080 // instruction, so assert it.
3081 size_t width = inst->SizeInCodeUnits();
3082 CHECK_GT(width, 1u);
Ian Rogersf1864ef2011-12-09 12:39:48 -08003083 // If the instruction is larger than 2 code units, rewrite subsequent code unit sized chunks with
Ian Rogersd81871c2011-10-03 13:57:23 -07003084 // NOPs
3085 for (size_t i = 2; i < width; i++) {
3086 insns[work_insn_idx_ + i] = Instruction::NOP;
3087 }
3088 // Encode the opcode, with the failure code in the high byte
3089 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3090 (failure_ << 8) | // AA - component
3091 (ref_type << (8 + kVerifyErrorRefTypeShift));
3092 insns[work_insn_idx_] = new_instruction;
3093 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3094 // rewrote, so nothing to do here.
Ian Rogers9fdfc182011-10-26 23:12:52 -07003095 LOG(INFO) << "Verification error, replacing instructions in " << PrettyMethod(method_) << " "
3096 << fail_messages_.str();
3097 if (gDebugVerify) {
3098 std::cout << std::endl << info_messages_.str();
3099 Dump(std::cout);
3100 }
jeffhaobdb76512011-09-07 11:43:16 -07003101}
jeffhaoba5ebb92011-08-25 17:24:37 -07003102
Ian Rogers776ac1f2012-04-13 23:36:36 -07003103bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003104 bool changed = true;
3105 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3106 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003107 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003108 * We haven't processed this instruction before, and we haven't touched the registers here, so
3109 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3110 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003111 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003112 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003113 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003114 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3115 if (gDebugVerify) {
3116 copy->CopyFromLine(target_line);
3117 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003118 changed = target_line->MergeRegisters(merge_line);
3119 if (failure_ != VERIFY_ERROR_NONE) {
3120 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003121 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003122 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003123 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
3124 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << std::endl
Ian Rogersd81871c2011-10-03 13:57:23 -07003125 << *copy.get() << " MERGE" << std::endl
3126 << *merge_line << " ==" << std::endl
3127 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003128 }
3129 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003130 if (changed) {
3131 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003132 }
3133 return true;
3134}
3135
Ian Rogers776ac1f2012-04-13 23:36:36 -07003136InsnFlags* MethodVerifier::CurrentInsnFlags() {
3137 return &insn_flags_[work_insn_idx_];
3138}
3139
3140void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003141 size_t* log2_max_gc_pc) {
3142 size_t local_gc_points = 0;
3143 size_t max_insn = 0;
3144 size_t max_ref_reg = -1;
3145 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3146 if (insn_flags_[i].IsGcPoint()) {
3147 local_gc_points++;
3148 max_insn = i;
3149 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003150 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003151 }
3152 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003153 *gc_points = local_gc_points;
3154 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3155 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003156 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003157 i++;
3158 }
3159 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003160}
3161
Ian Rogers776ac1f2012-04-13 23:36:36 -07003162const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003163 size_t num_entries, ref_bitmap_bits, pc_bits;
3164 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3165 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003166 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003167 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003168 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003169 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003170 return NULL;
3171 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003172 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3173 // There are 2 bytes to encode the number of entries
3174 if (num_entries >= 65536) {
3175 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003176 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003177 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003178 return NULL;
3179 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003180 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003181 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003182 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003183 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003184 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003185 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003186 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003187 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003188 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003189 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003190 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003191 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3192 return NULL;
3193 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003194 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003195 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003196 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003197 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003198 return NULL;
3199 }
3200 // Write table header
Ian Rogers776ac1f2012-04-13 23:36:36 -07003201 table->push_back(format | ((ref_bitmap_bytes >> PcToReferenceMap::kRegMapFormatShift) &
3202 ~PcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003203 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003204 table->push_back(num_entries & 0xFF);
3205 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003206 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003207 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3208 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003209 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003210 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003211 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003212 }
3213 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003214 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003215 }
3216 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003217 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003218 return table;
3219}
jeffhaoa0a764a2011-09-16 10:43:38 -07003220
Ian Rogers776ac1f2012-04-13 23:36:36 -07003221void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003222 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3223 // that the table data is well formed and all references are marked (or not) in the bitmap
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003224 PcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003225 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003226 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003227 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3228 if (insn_flags_[i].IsGcPoint()) {
3229 CHECK_LT(map_index, map.NumEntries());
3230 CHECK_EQ(map.GetPC(map_index), i);
3231 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3232 map_index++;
3233 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003234 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003235 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003236 CHECK_LT(j / 8, map.RegWidth());
3237 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3238 } else if ((j / 8) < map.RegWidth()) {
3239 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3240 } else {
3241 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3242 }
3243 }
3244 } else {
3245 CHECK(reg_bitmap == NULL);
3246 }
3247 }
3248}
jeffhaoa0a764a2011-09-16 10:43:38 -07003249
Ian Rogers776ac1f2012-04-13 23:36:36 -07003250Mutex* MethodVerifier::gc_maps_lock_ = NULL;
3251MethodVerifier::GcMapTable* MethodVerifier::gc_maps_ = NULL;
jeffhaoa0a764a2011-09-16 10:43:38 -07003252
Ian Rogers776ac1f2012-04-13 23:36:36 -07003253void MethodVerifier::InitGcMaps() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003254 gc_maps_lock_ = new Mutex("verifier GC maps lock");
3255 MutexLock mu(*gc_maps_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07003256 gc_maps_ = new MethodVerifier::GcMapTable;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003257}
3258
Ian Rogers776ac1f2012-04-13 23:36:36 -07003259void MethodVerifier::DeleteGcMaps() {
Elliott Hughesf34f1742012-03-16 18:56:00 -07003260 {
3261 MutexLock mu(*gc_maps_lock_);
3262 STLDeleteValues(gc_maps_);
3263 delete gc_maps_;
3264 gc_maps_ = NULL;
3265 }
3266 delete gc_maps_lock_;
3267 gc_maps_lock_ = NULL;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003268}
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003269
Ian Rogers776ac1f2012-04-13 23:36:36 -07003270void MethodVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003271 MutexLock mu(*gc_maps_lock_);
Elliott Hughesa0e18062012-04-13 15:59:59 -07003272 GcMapTable::iterator it = gc_maps_->find(ref);
3273 if (it != gc_maps_->end()) {
3274 delete it->second;
3275 gc_maps_->erase(it);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003276 }
Elliott Hughesa0e18062012-04-13 15:59:59 -07003277 gc_maps_->Put(ref, &gc_map);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003278 CHECK(GetGcMap(ref) != NULL);
3279}
3280
Ian Rogers776ac1f2012-04-13 23:36:36 -07003281const std::vector<uint8_t>* MethodVerifier::GetGcMap(Compiler::MethodReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003282 MutexLock mu(*gc_maps_lock_);
3283 GcMapTable::const_iterator it = gc_maps_->find(ref);
3284 if (it == gc_maps_->end()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003285 return NULL;
3286 }
3287 CHECK(it->second != NULL);
3288 return it->second;
3289}
3290
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003291static Mutex& GetRejectedClassesLock() {
3292 static Mutex rejected_classes_lock("verifier rejected classes lock");
3293 return rejected_classes_lock;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003294}
3295
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003296static std::set<Compiler::ClassReference>& GetRejectedClasses() {
3297 static std::set<Compiler::ClassReference> rejected_classes;
3298 return rejected_classes;
3299}
jeffhaod1224c72012-02-29 13:43:08 -08003300
Ian Rogers776ac1f2012-04-13 23:36:36 -07003301void MethodVerifier::AddRejectedClass(Compiler::ClassReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003302 MutexLock mu(GetRejectedClassesLock());
3303 GetRejectedClasses().insert(ref);
jeffhaod1224c72012-02-29 13:43:08 -08003304 CHECK(IsClassRejected(ref));
3305}
3306
Ian Rogers776ac1f2012-04-13 23:36:36 -07003307bool MethodVerifier::IsClassRejected(Compiler::ClassReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003308 MutexLock mu(GetRejectedClassesLock());
3309 std::set<Compiler::ClassReference>& rejected_classes(GetRejectedClasses());
3310 return (rejected_classes.find(ref) != rejected_classes.end());
jeffhaod1224c72012-02-29 13:43:08 -08003311}
3312
Logan Chienfca7e872011-12-20 20:08:22 +08003313#if defined(ART_USE_LLVM_COMPILER)
Ian Rogers776ac1f2012-04-13 23:36:36 -07003314const InferredRegCategoryMap* MethodVerifier::GenerateInferredRegCategoryMap() {
Logan Chienfca7e872011-12-20 20:08:22 +08003315 uint32_t insns_size = code_item_->insns_size_in_code_units_;
3316 uint16_t regs_size = code_item_->registers_size_;
3317
3318 UniquePtr<InferredRegCategoryMap> table(
3319 new InferredRegCategoryMap(insns_size, regs_size));
3320
3321 for (size_t i = 0; i < insns_size; ++i) {
3322 if (RegisterLine* line = reg_table_.GetLine(i)) {
3323 for (size_t r = 0; r < regs_size; ++r) {
Logan Chiendd361c92012-04-10 23:40:37 +08003324 const RegType &rt = line->GetRegisterType(r);
Logan Chienfca7e872011-12-20 20:08:22 +08003325
3326 if (rt.IsZero()) {
3327 table->SetRegCategory(i, r, kRegZero);
3328 } else if (rt.IsCategory1Types()) {
3329 table->SetRegCategory(i, r, kRegCat1nr);
3330 } else if (rt.IsCategory2Types()) {
3331 table->SetRegCategory(i, r, kRegCat2);
3332 } else if (rt.IsReferenceTypes()) {
3333 table->SetRegCategory(i, r, kRegObject);
3334 } else {
3335 table->SetRegCategory(i, r, kRegUnknown);
3336 }
3337 }
3338 }
3339 }
3340
3341 return table.release();
3342}
Logan Chiendd361c92012-04-10 23:40:37 +08003343
Ian Rogers776ac1f2012-04-13 23:36:36 -07003344Mutex* MethodVerifier::inferred_reg_category_maps_lock_ = NULL;
3345MethodVerifier::InferredRegCategoryMapTable* MethodVerifier::inferred_reg_category_maps_ = NULL;
Logan Chiendd361c92012-04-10 23:40:37 +08003346
Ian Rogers776ac1f2012-04-13 23:36:36 -07003347void MethodVerifier::InitInferredRegCategoryMaps() {
Logan Chiendd361c92012-04-10 23:40:37 +08003348 inferred_reg_category_maps_lock_ = new Mutex("verifier GC maps lock");
3349 MutexLock mu(*inferred_reg_category_maps_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07003350 inferred_reg_category_maps_ = new MethodVerifier::InferredRegCategoryMapTable;
Logan Chiendd361c92012-04-10 23:40:37 +08003351}
3352
Ian Rogers776ac1f2012-04-13 23:36:36 -07003353void MethodVerifier::DeleteInferredRegCategoryMaps() {
Logan Chiendd361c92012-04-10 23:40:37 +08003354 {
3355 MutexLock mu(*inferred_reg_category_maps_lock_);
3356 STLDeleteValues(inferred_reg_category_maps_);
3357 delete inferred_reg_category_maps_;
3358 inferred_reg_category_maps_ = NULL;
3359 }
3360 delete inferred_reg_category_maps_lock_;
3361 inferred_reg_category_maps_lock_ = NULL;
3362}
3363
3364
Ian Rogers776ac1f2012-04-13 23:36:36 -07003365void MethodVerifier::SetInferredRegCategoryMap(Compiler::MethodReference ref,
3366 const InferredRegCategoryMap& inferred_reg_category_map) {
Logan Chiendd361c92012-04-10 23:40:37 +08003367 MutexLock mu(*inferred_reg_category_maps_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07003368 const InferredRegCategoryMap* existing_inferred_reg_category_map = GetInferredRegCategoryMap(ref);
Logan Chiendd361c92012-04-10 23:40:37 +08003369
3370 if (existing_inferred_reg_category_map != NULL) {
3371 CHECK(*existing_inferred_reg_category_map == inferred_reg_category_map);
3372 delete existing_inferred_reg_category_map;
3373 }
3374
Ian Rogers776ac1f2012-04-13 23:36:36 -07003375 inferred_reg_category_maps_->Put(ref, &inferred_reg_category_map);
Logan Chiendd361c92012-04-10 23:40:37 +08003376 CHECK(GetInferredRegCategoryMap(ref) != NULL);
3377}
3378
3379const InferredRegCategoryMap*
Ian Rogers776ac1f2012-04-13 23:36:36 -07003380MethodVerifier::GetInferredRegCategoryMap(Compiler::MethodReference ref) {
Logan Chiendd361c92012-04-10 23:40:37 +08003381 MutexLock mu(*inferred_reg_category_maps_lock_);
3382
3383 InferredRegCategoryMapTable::const_iterator it =
3384 inferred_reg_category_maps_->find(ref);
3385
3386 if (it == inferred_reg_category_maps_->end()) {
3387 return NULL;
3388 }
3389 CHECK(it->second != NULL);
3390 return it->second;
3391}
Logan Chienfca7e872011-12-20 20:08:22 +08003392#endif
3393
Ian Rogersd81871c2011-10-03 13:57:23 -07003394} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003395} // namespace art