blob: 178c2a92ddb40033ba572ceec8fb7d471f7084f6 [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
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -070041#if defined(ART_USE_GREENLAND_COMPILER)
42#include "greenland/backend_types.h"
43#include "greenland/inferred_reg_category_map.h"
44using namespace art::greenland;
45#endif
46
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070047namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070048namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070049
Ian Rogers2c8a8572011-10-24 17:11:36 -070050static const bool gDebugVerify = false;
51
Ian Rogers776ac1f2012-04-13 23:36:36 -070052class InsnFlags {
53 public:
54 InsnFlags() : length_(0), flags_(0) {}
55
56 void SetLengthInCodeUnits(size_t length) {
57 CHECK_LT(length, 65536u);
58 length_ = length;
59 }
60 size_t GetLengthInCodeUnits() {
61 return length_;
62 }
63 bool IsOpcode() const {
64 return length_ != 0;
65 }
66
67 void SetInTry() {
68 flags_ |= 1 << kInTry;
69 }
70 void ClearInTry() {
71 flags_ &= ~(1 << kInTry);
72 }
73 bool IsInTry() const {
74 return (flags_ & (1 << kInTry)) != 0;
75 }
76
77 void SetBranchTarget() {
78 flags_ |= 1 << kBranchTarget;
79 }
80 void ClearBranchTarget() {
81 flags_ &= ~(1 << kBranchTarget);
82 }
83 bool IsBranchTarget() const {
84 return (flags_ & (1 << kBranchTarget)) != 0;
85 }
86
87 void SetGcPoint() {
88 flags_ |= 1 << kGcPoint;
89 }
90 void ClearGcPoint() {
91 flags_ &= ~(1 << kGcPoint);
92 }
93 bool IsGcPoint() const {
94 return (flags_ & (1 << kGcPoint)) != 0;
95 }
96
97 void SetVisited() {
98 flags_ |= 1 << kVisited;
99 }
100 void ClearVisited() {
101 flags_ &= ~(1 << kVisited);
102 }
103 bool IsVisited() const {
104 return (flags_ & (1 << kVisited)) != 0;
105 }
106
107 void SetChanged() {
108 flags_ |= 1 << kChanged;
109 }
110 void ClearChanged() {
111 flags_ &= ~(1 << kChanged);
112 }
113 bool IsChanged() const {
114 return (flags_ & (1 << kChanged)) != 0;
115 }
116
117 bool IsVisitedOrChanged() const {
118 return IsVisited() || IsChanged();
119 }
120
121 std::string Dump() {
122 char encoding[6];
123 if (!IsOpcode()) {
124 strncpy(encoding, "XXXXX", sizeof(encoding));
125 } else {
126 strncpy(encoding, "-----", sizeof(encoding));
127 if (IsInTry()) encoding[kInTry] = 'T';
128 if (IsBranchTarget()) encoding[kBranchTarget] = 'B';
129 if (IsGcPoint()) encoding[kGcPoint] = 'G';
130 if (IsVisited()) encoding[kVisited] = 'V';
131 if (IsChanged()) encoding[kChanged] = 'C';
132 }
133 return std::string(encoding);
134 }
Elliott Hughesa21039c2012-06-21 12:09:25 -0700135
Ian Rogers776ac1f2012-04-13 23:36:36 -0700136 private:
137 enum {
138 kInTry,
139 kBranchTarget,
140 kGcPoint,
141 kVisited,
142 kChanged,
143 };
144
145 // Size of instruction in code units
146 uint16_t length_;
147 uint8_t flags_;
Ian Rogers84fa0742011-10-25 18:13:30 -0700148};
Ian Rogersd81871c2011-10-03 13:57:23 -0700149
Ian Rogersd81871c2011-10-03 13:57:23 -0700150void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
151 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -0700152 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700153 DCHECK_GT(insns_size, 0U);
154
155 for (uint32_t i = 0; i < insns_size; i++) {
156 bool interesting = false;
157 switch (mode) {
158 case kTrackRegsAll:
159 interesting = flags[i].IsOpcode();
160 break;
161 case kTrackRegsGcPoints:
162 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
163 break;
164 case kTrackRegsBranches:
165 interesting = flags[i].IsBranchTarget();
166 break;
167 default:
168 break;
169 }
170 if (interesting) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700171 pc_to_register_line_.Put(i, new RegisterLine(registers_size, verifier));
Ian Rogersd81871c2011-10-03 13:57:23 -0700172 }
173 }
174}
175
jeffhaof1e6b7c2012-06-05 18:33:30 -0700176MethodVerifier::FailureKind MethodVerifier::VerifyClass(const Class* klass, std::string& error) {
jeffhaobdb76512011-09-07 11:43:16 -0700177 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700178 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700179 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700180 Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -0800181 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800182 error = "Verifier rejected class ";
183 error += PrettyDescriptor(klass);
184 error += " that has no super class";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700185 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -0700186 }
Ian Rogers1c5eb702012-02-01 09:18:34 -0800187 if (super != NULL && super->IsFinal()) {
188 error = "Verifier rejected class ";
189 error += PrettyDescriptor(klass);
190 error += " that attempts to sub-class final class ";
191 error += PrettyDescriptor(super);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700192 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -0700193 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700194 ClassHelper kh(klass);
195 const DexFile& dex_file = kh.GetDexFile();
196 uint32_t class_def_idx;
197 if (!dex_file.FindClassDefIndex(kh.GetDescriptor(), class_def_idx)) {
198 error = "Verifier rejected class ";
199 error += PrettyDescriptor(klass);
200 error += " that isn't present in dex file ";
201 error += dex_file.GetLocation();
jeffhaof1e6b7c2012-06-05 18:33:30 -0700202 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700203 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700204 return VerifyClass(&dex_file, kh.GetDexCache(), klass->GetClassLoader(), class_def_idx, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700205}
206
Ian Rogers365c1022012-06-22 15:05:28 -0700207MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
208 DexCache* dex_cache, ClassLoader* class_loader, uint32_t class_def_idx, std::string& error) {
jeffhaof56197c2012-03-05 18:01:54 -0800209 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
210 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700211 if (class_data == NULL) {
212 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700213 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700214 }
jeffhaof56197c2012-03-05 18:01:54 -0800215 ClassDataItemIterator it(*dex_file, class_data);
216 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
217 it.Next();
218 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700219 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700220 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700221 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaof56197c2012-03-05 18:01:54 -0800222 while (it.HasNextDirectMethod()) {
223 uint32_t method_idx = it.GetMemberIndex();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700224 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, true);
225 if (method == NULL) {
226 DCHECK(Thread::Current()->IsExceptionPending());
227 // We couldn't resolve the method, but continue regardless.
228 Thread::Current()->ClearException();
229 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700230 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
231 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags());
232 if (result != kNoFailure) {
233 if (result == kHardFailure) {
234 hard_fail = true;
235 if (error_count > 0) {
236 error += "\n";
237 }
238 error = "Verifier rejected class ";
239 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
240 error += " due to bad method ";
241 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700242 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700243 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800244 }
245 it.Next();
246 }
247 while (it.HasNextVirtualMethod()) {
248 uint32_t method_idx = it.GetMemberIndex();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700249 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, false);
250 if (method == NULL) {
251 DCHECK(Thread::Current()->IsExceptionPending());
252 // We couldn't resolve the method, but continue regardless.
253 Thread::Current()->ClearException();
254 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700255 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
256 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags());
257 if (result != kNoFailure) {
258 if (result == kHardFailure) {
259 hard_fail = true;
260 if (error_count > 0) {
261 error += "\n";
262 }
263 error = "Verifier rejected class ";
264 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
265 error += " due to bad method ";
266 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700267 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700268 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800269 }
270 it.Next();
271 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700272 if (error_count == 0) {
273 return kNoFailure;
274 } else {
275 return hard_fail ? kHardFailure : kSoftFailure;
276 }
jeffhaof56197c2012-03-05 18:01:54 -0800277}
278
jeffhaof1e6b7c2012-06-05 18:33:30 -0700279MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx, const DexFile* dex_file,
Ian Rogers365c1022012-06-22 15:05:28 -0700280 DexCache* dex_cache, ClassLoader* class_loader, uint32_t class_def_idx,
jeffhaof1e6b7c2012-06-05 18:33:30 -0700281 const DexFile::CodeItem* code_item, Method* method, uint32_t method_access_flags) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700282 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item, method_idx,
283 method, method_access_flags);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700284 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700285 // Verification completed, however failures may be pending that didn't cause the verification
286 // to hard fail.
Ian Rogerse551e952012-06-03 22:59:14 -0700287 CHECK(!verifier.have_pending_hard_failure_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700288 if (verifier.failures_.size() != 0) {
289 verifier.DumpFailures(LOG(INFO) << "Soft verification failures in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700290 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof1e6b7c2012-06-05 18:33:30 -0700291 return kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800292 }
293 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700294 // Bad method data.
295 CHECK_NE(verifier.failures_.size(), 0U);
296 CHECK(verifier.have_pending_hard_failure_);
297 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700298 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800299 if (gDebugVerify) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700300 std::cout << "\n" << verifier.info_messages_.str();
jeffhaof56197c2012-03-05 18:01:54 -0800301 verifier.Dump(std::cout);
302 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700303 return kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800304 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700305 return kNoFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800306}
307
Ian Rogersad0b3a32012-04-16 14:50:24 -0700308void MethodVerifier::VerifyMethodAndDump(Method* method) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800309 CHECK(method != NULL);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700310 MethodHelper mh(method);
311 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
312 mh.GetClassDefIndex(), mh.GetCodeItem(), method->GetDexMethodIndex(),
313 method, method->GetAccessFlags());
314 verifier.Verify();
Elliott Hughesc073b072012-05-24 19:29:17 -0700315 verifier.DumpFailures(LOG(INFO) << "Dump of method " << PrettyMethod(method) << "\n")
Ian Rogersad0b3a32012-04-16 14:50:24 -0700316 << verifier.info_messages_.str() << Dumpable<MethodVerifier>(verifier);
jeffhaoba5ebb92011-08-25 17:24:37 -0700317}
318
Ian Rogers776ac1f2012-04-13 23:36:36 -0700319MethodVerifier::MethodVerifier(const DexFile* dex_file, DexCache* dex_cache,
Ian Rogers365c1022012-06-22 15:05:28 -0700320 ClassLoader* class_loader, uint32_t class_def_idx, const DexFile::CodeItem* code_item,
Ian Rogersad0b3a32012-04-16 14:50:24 -0700321 uint32_t method_idx, Method* method, uint32_t method_access_flags)
jeffhaof56197c2012-03-05 18:01:54 -0800322 : work_insn_idx_(-1),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700323 method_idx_(method_idx),
324 foo_method_(method),
325 method_access_flags_(method_access_flags),
jeffhaof56197c2012-03-05 18:01:54 -0800326 dex_file_(dex_file),
327 dex_cache_(dex_cache),
328 class_loader_(class_loader),
329 class_def_idx_(class_def_idx),
330 code_item_(code_item),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700331 have_pending_hard_failure_(false),
332 have_pending_rewrite_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800333 new_instance_count_(0),
334 monitor_enter_count_(0) {
335}
336
Ian Rogersad0b3a32012-04-16 14:50:24 -0700337bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700338 // If there aren't any instructions, make sure that's expected, then exit successfully.
339 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700340 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700341 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700342 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700343 } else {
344 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700345 }
jeffhaobdb76512011-09-07 11:43:16 -0700346 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700347 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
348 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700349 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
350 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700351 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700352 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700353 // Allocate and initialize an array to hold instruction data.
354 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
355 // Run through the instructions and see if the width checks out.
356 bool result = ComputeWidthsAndCountOps();
357 // Flag instructions guarded by a "try" block and check exception handlers.
358 result = result && ScanTryCatchBlocks();
359 // Perform static instruction verification.
360 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700361 // Perform code-flow analysis and return.
362 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700363}
364
Ian Rogers776ac1f2012-04-13 23:36:36 -0700365std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700366 switch (error) {
367 case VERIFY_ERROR_NO_CLASS:
368 case VERIFY_ERROR_NO_FIELD:
369 case VERIFY_ERROR_NO_METHOD:
370 case VERIFY_ERROR_ACCESS_CLASS:
371 case VERIFY_ERROR_ACCESS_FIELD:
372 case VERIFY_ERROR_ACCESS_METHOD:
373 if (Runtime::Current()->IsCompiler()) {
374 // If we're optimistically running verification at compile time, turn NO_xxx and ACCESS_xxx
375 // errors into soft verification errors so that we re-verify at runtime. We may fail to find
376 // or to agree on access because of not yet available class loaders, or class loaders that
377 // will differ at runtime.
jeffhaod5347e02012-03-22 17:25:05 -0700378 error = VERIFY_ERROR_BAD_CLASS_SOFT;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700379 } else {
380 have_pending_rewrite_failure_ = true;
381 }
382 break;
383 // Errors that are bad at both compile and runtime, but don't cause rejection of the class.
384 case VERIFY_ERROR_CLASS_CHANGE:
385 case VERIFY_ERROR_INSTANTIATION:
386 have_pending_rewrite_failure_ = true;
387 break;
388 // Indication that verification should be retried at runtime.
389 case VERIFY_ERROR_BAD_CLASS_SOFT:
390 if (!Runtime::Current()->IsCompiler()) {
391 // It is runtime so hard fail.
392 have_pending_hard_failure_ = true;
393 }
394 break;
jeffhaod5347e02012-03-22 17:25:05 -0700395 // Hard verification failures at compile time will still fail at runtime, so the class is
396 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700397 case VERIFY_ERROR_BAD_CLASS_HARD: {
398 if (Runtime::Current()->IsCompiler()) {
jeffhaof56197c2012-03-05 18:01:54 -0800399 Compiler::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800400 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800401 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700402 have_pending_hard_failure_ = true;
403 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800404 }
405 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700406 failures_.push_back(error);
407 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(method_idx_, *dex_file_).c_str(),
408 work_insn_idx_));
409 std::ostringstream* failure_message = new std::ostringstream(location);
410 failure_messages_.push_back(failure_message);
411 return *failure_message;
412}
413
414void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
415 size_t failure_num = failure_messages_.size();
416 DCHECK_NE(failure_num, 0U);
417 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
418 prepend += last_fail_message->str();
419 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
420 delete last_fail_message;
421}
422
423void MethodVerifier::AppendToLastFailMessage(std::string append) {
424 size_t failure_num = failure_messages_.size();
425 DCHECK_NE(failure_num, 0U);
426 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
427 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800428}
429
Ian Rogers776ac1f2012-04-13 23:36:36 -0700430bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700431 const uint16_t* insns = code_item_->insns_;
432 size_t insns_size = code_item_->insns_size_in_code_units_;
433 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700434 size_t new_instance_count = 0;
435 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700436 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700437
Ian Rogersd81871c2011-10-03 13:57:23 -0700438 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700439 Instruction::Code opcode = inst->Opcode();
440 if (opcode == Instruction::NEW_INSTANCE) {
441 new_instance_count++;
442 } else if (opcode == Instruction::MONITOR_ENTER) {
443 monitor_enter_count++;
444 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700445 size_t inst_size = inst->SizeInCodeUnits();
446 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
447 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700448 inst = inst->Next();
449 }
450
Ian Rogersd81871c2011-10-03 13:57:23 -0700451 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700452 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
453 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700454 return false;
455 }
456
Ian Rogersd81871c2011-10-03 13:57:23 -0700457 new_instance_count_ = new_instance_count;
458 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700459 return true;
460}
461
Ian Rogers776ac1f2012-04-13 23:36:36 -0700462bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700463 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700464 if (tries_size == 0) {
465 return true;
466 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700467 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700468 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700469
470 for (uint32_t idx = 0; idx < tries_size; idx++) {
471 const DexFile::TryItem* try_item = &tries[idx];
472 uint32_t start = try_item->start_addr_;
473 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700474 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700475 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
476 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700477 return false;
478 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700479 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700480 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700481 return false;
482 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700483 for (uint32_t dex_pc = start; dex_pc < end;
484 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
485 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700486 }
487 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800488 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700489 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700490 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700491 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700492 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700493 CatchHandlerIterator iterator(handlers_ptr);
494 for (; iterator.HasNext(); iterator.Next()) {
495 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700496 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700497 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700498 return false;
499 }
jeffhao60f83e32012-02-13 17:16:30 -0800500 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
501 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700502 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700503 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800504 return false;
505 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700506 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700507 // Ensure exception types are resolved so that they don't need resolution to be delivered,
508 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700509 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
jeffhaof56197c2012-03-05 18:01:54 -0800510 Class* exception_type = linker->ResolveType(*dex_file_, iterator.GetHandlerTypeIndex(),
511 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700512 if (exception_type == NULL) {
513 DCHECK(Thread::Current()->IsExceptionPending());
514 Thread::Current()->ClearException();
515 }
516 }
jeffhaobdb76512011-09-07 11:43:16 -0700517 }
Ian Rogers0571d352011-11-03 19:51:38 -0700518 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700519 }
jeffhaobdb76512011-09-07 11:43:16 -0700520 return true;
521}
522
Ian Rogers776ac1f2012-04-13 23:36:36 -0700523bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700524 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700525
Ian Rogersd81871c2011-10-03 13:57:23 -0700526 /* Flag the start of the method as a branch target. */
527 insn_flags_[0].SetBranchTarget();
528
529 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700530 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700531 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700532 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700533 return false;
534 }
535 /* Flag instructions that are garbage collection points */
536 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
537 insn_flags_[dex_pc].SetGcPoint();
538 }
539 dex_pc += inst->SizeInCodeUnits();
540 inst = inst->Next();
541 }
542 return true;
543}
544
Ian Rogers776ac1f2012-04-13 23:36:36 -0700545bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800546 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700547 bool result = true;
548 switch (inst->GetVerifyTypeArgumentA()) {
549 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800550 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700551 break;
552 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800553 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700554 break;
555 }
556 switch (inst->GetVerifyTypeArgumentB()) {
557 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800558 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700559 break;
560 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800561 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700562 break;
563 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800564 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700565 break;
566 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800567 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700568 break;
569 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800570 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700571 break;
572 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800573 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700574 break;
575 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800576 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700577 break;
578 }
579 switch (inst->GetVerifyTypeArgumentC()) {
580 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800581 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700582 break;
583 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800584 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700585 break;
586 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800587 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700588 break;
589 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800590 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700591 break;
592 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800593 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700594 break;
595 }
596 switch (inst->GetVerifyExtraFlags()) {
597 case Instruction::kVerifyArrayData:
598 result = result && CheckArrayData(code_offset);
599 break;
600 case Instruction::kVerifyBranchTarget:
601 result = result && CheckBranchTarget(code_offset);
602 break;
603 case Instruction::kVerifySwitchTargets:
604 result = result && CheckSwitchTargets(code_offset);
605 break;
606 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800607 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700608 break;
609 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800610 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700611 break;
612 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700613 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700614 result = false;
615 break;
616 }
617 return result;
618}
619
Ian Rogers776ac1f2012-04-13 23:36:36 -0700620bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700621 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700622 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
623 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700624 return false;
625 }
626 return true;
627}
628
Ian Rogers776ac1f2012-04-13 23:36:36 -0700629bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700630 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700631 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
632 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700633 return false;
634 }
635 return true;
636}
637
Ian Rogers776ac1f2012-04-13 23:36:36 -0700638bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700639 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700640 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
641 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700642 return false;
643 }
644 return true;
645}
646
Ian Rogers776ac1f2012-04-13 23:36:36 -0700647bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700648 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700649 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
650 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700651 return false;
652 }
653 return true;
654}
655
Ian Rogers776ac1f2012-04-13 23:36:36 -0700656bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700657 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700658 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
659 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700660 return false;
661 }
662 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700663 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700664 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700665 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700666 return false;
667 }
668 return true;
669}
670
Ian Rogers776ac1f2012-04-13 23:36:36 -0700671bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700672 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700673 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
674 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700675 return false;
676 }
677 return true;
678}
679
Ian Rogers776ac1f2012-04-13 23:36:36 -0700680bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700681 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700682 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
683 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700684 return false;
685 }
686 return true;
687}
688
Ian Rogers776ac1f2012-04-13 23:36:36 -0700689bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700690 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700691 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
692 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700693 return false;
694 }
695 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700696 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700697 const char* cp = descriptor;
698 while (*cp++ == '[') {
699 bracket_count++;
700 }
701 if (bracket_count == 0) {
702 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700703 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700704 return false;
705 } else if (bracket_count > 255) {
706 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700707 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700708 return false;
709 }
710 return true;
711}
712
Ian Rogers776ac1f2012-04-13 23:36:36 -0700713bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700714 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
715 const uint16_t* insns = code_item_->insns_ + cur_offset;
716 const uint16_t* array_data;
717 int32_t array_data_offset;
718
719 DCHECK_LT(cur_offset, insn_count);
720 /* make sure the start of the array data table is in range */
721 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
722 if ((int32_t) cur_offset + array_data_offset < 0 ||
723 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
725 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700726 return false;
727 }
728 /* offset to array data table is a relative branch-style offset */
729 array_data = insns + array_data_offset;
730 /* make sure the table is 32-bit aligned */
731 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700732 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
733 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700734 return false;
735 }
736 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700737 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700738 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
739 /* make sure the end of the switch is in range */
740 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700741 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
742 << ", data offset " << array_data_offset << ", end "
743 << cur_offset + array_data_offset + table_size
744 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700745 return false;
746 }
747 return true;
748}
749
Ian Rogers776ac1f2012-04-13 23:36:36 -0700750bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700751 int32_t offset;
752 bool isConditional, selfOkay;
753 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
754 return false;
755 }
756 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700757 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 -0700758 return false;
759 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700760 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
761 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700762 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700763 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700764 return false;
765 }
766 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
767 int32_t abs_offset = cur_offset + offset;
768 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700769 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700770 << reinterpret_cast<void*>(abs_offset) << ") at "
771 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700772 return false;
773 }
774 insn_flags_[abs_offset].SetBranchTarget();
775 return true;
776}
777
Ian Rogers776ac1f2012-04-13 23:36:36 -0700778bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700779 bool* selfOkay) {
780 const uint16_t* insns = code_item_->insns_ + cur_offset;
781 *pConditional = false;
782 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700783 switch (*insns & 0xff) {
784 case Instruction::GOTO:
785 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700786 break;
787 case Instruction::GOTO_32:
788 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700789 *selfOkay = true;
790 break;
791 case Instruction::GOTO_16:
792 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700793 break;
794 case Instruction::IF_EQ:
795 case Instruction::IF_NE:
796 case Instruction::IF_LT:
797 case Instruction::IF_GE:
798 case Instruction::IF_GT:
799 case Instruction::IF_LE:
800 case Instruction::IF_EQZ:
801 case Instruction::IF_NEZ:
802 case Instruction::IF_LTZ:
803 case Instruction::IF_GEZ:
804 case Instruction::IF_GTZ:
805 case Instruction::IF_LEZ:
806 *pOffset = (int16_t) insns[1];
807 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700808 break;
809 default:
810 return false;
811 break;
812 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700813 return true;
814}
815
Ian Rogers776ac1f2012-04-13 23:36:36 -0700816bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700817 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700818 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700819 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700820 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700821 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
822 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700823 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
824 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700825 return false;
826 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700827 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700828 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700829 /* make sure the table is 32-bit aligned */
830 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700831 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
832 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700833 return false;
834 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700835 uint32_t switch_count = switch_insns[1];
836 int32_t keys_offset, targets_offset;
837 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700838 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
839 /* 0=sig, 1=count, 2/3=firstKey */
840 targets_offset = 4;
841 keys_offset = -1;
842 expected_signature = Instruction::kPackedSwitchSignature;
843 } else {
844 /* 0=sig, 1=count, 2..count*2 = keys */
845 keys_offset = 2;
846 targets_offset = 2 + 2 * switch_count;
847 expected_signature = Instruction::kSparseSwitchSignature;
848 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700849 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700850 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700851 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
852 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700853 return false;
854 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700855 /* make sure the end of the switch is in range */
856 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700857 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
858 << switch_offset << ", end "
859 << (cur_offset + switch_offset + table_size)
860 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700861 return false;
862 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700863 /* for a sparse switch, verify the keys are in ascending order */
864 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
866 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700867 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
868 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
869 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700870 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
871 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700872 return false;
873 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700874 last_key = key;
875 }
876 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700877 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700878 for (uint32_t targ = 0; targ < switch_count; targ++) {
879 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
880 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
881 int32_t abs_offset = cur_offset + offset;
882 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700883 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700884 << reinterpret_cast<void*>(abs_offset) << ") at "
885 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700886 return false;
887 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700888 insn_flags_[abs_offset].SetBranchTarget();
889 }
890 return true;
891}
892
Ian Rogers776ac1f2012-04-13 23:36:36 -0700893bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700894 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700895 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700896 return false;
897 }
898 uint16_t registers_size = code_item_->registers_size_;
899 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800900 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700901 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
902 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700903 return false;
904 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700905 }
906
907 return true;
908}
909
Ian Rogers776ac1f2012-04-13 23:36:36 -0700910bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700911 uint16_t registers_size = code_item_->registers_size_;
912 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
913 // integer overflow when adding them here.
914 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700915 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
916 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700917 return false;
918 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700919 return true;
920}
921
Brian Carlstrom75412882012-01-18 01:26:54 -0800922const std::vector<uint8_t>* CreateLengthPrefixedGcMap(const std::vector<uint8_t>& gc_map) {
923 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
924 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
925 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
926 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
927 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
928 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
929 gc_map.begin(),
930 gc_map.end());
931 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
932 DCHECK_EQ(gc_map.size(),
933 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
934 (length_prefixed_gc_map->at(1) << 16) |
935 (length_prefixed_gc_map->at(2) << 8) |
936 (length_prefixed_gc_map->at(3) << 0)));
937 return length_prefixed_gc_map;
938}
939
Ian Rogers776ac1f2012-04-13 23:36:36 -0700940bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700941 uint16_t registers_size = code_item_->registers_size_;
942 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -0700943
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -0800945 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
946 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700947 }
948 /* Create and initialize table holding register status */
Elliott Hughes460384f2012-04-04 16:53:10 -0700949 reg_table_.Init(kTrackRegsGcPoints, insn_flags_.get(), insns_size, registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -0700950
Ian Rogersd81871c2011-10-03 13:57:23 -0700951 work_line_.reset(new RegisterLine(registers_size, this));
952 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -0700953
Ian Rogersd81871c2011-10-03 13:57:23 -0700954 /* Initialize register types of method arguments. */
955 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700956 DCHECK_NE(failures_.size(), 0U);
957 std::string prepend("Bad signature in ");
958 prepend += PrettyMethod(method_idx_, *dex_file_);
959 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -0700960 return false;
961 }
962 /* Perform code flow verification. */
963 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700964 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700965 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700966 }
967
TDYa127b2eb5c12012-05-24 15:52:10 -0700968 Compiler::MethodReference ref(dex_file_, method_idx_);
969
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700970#if !defined(ART_USE_LLVM_COMPILER) && !defined(ART_USE_GREENLAND_COMPILER)
TDYa127b2eb5c12012-05-24 15:52:10 -0700971
Ian Rogersd81871c2011-10-03 13:57:23 -0700972 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -0800973 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
974 if (map.get() == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700975 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700976 return false; // Not a real failure, but a failure to encode
977 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700978#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800979 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -0700980#endif
Brian Carlstrom75412882012-01-18 01:26:54 -0800981 const std::vector<uint8_t>* gc_map = CreateLengthPrefixedGcMap(*(map.get()));
Ian Rogers776ac1f2012-04-13 23:36:36 -0700982 verifier::MethodVerifier::SetGcMap(ref, *gc_map);
Logan Chienfca7e872011-12-20 20:08:22 +0800983
Ian Rogersad0b3a32012-04-16 14:50:24 -0700984 if (foo_method_ != NULL) {
985 foo_method_->SetGcMap(&gc_map->at(0));
986 }
Logan Chiendd361c92012-04-10 23:40:37 +0800987
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700988#else // defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Logan Chienfca7e872011-12-20 20:08:22 +0800989 /* Generate Inferred Register Category for LLVM-based Code Generator */
990 const InferredRegCategoryMap* table = GenerateInferredRegCategoryMap();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700991 verifier::MethodVerifier::SetInferredRegCategoryMap(ref, *table);
TDYa127b2eb5c12012-05-24 15:52:10 -0700992
Logan Chienfca7e872011-12-20 20:08:22 +0800993#endif
994
jeffhaobdb76512011-09-07 11:43:16 -0700995 return true;
996}
997
Ian Rogersad0b3a32012-04-16 14:50:24 -0700998std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
999 DCHECK_EQ(failures_.size(), failure_messages_.size());
1000 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001001 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001002 }
1003 return os;
1004}
1005
1006extern "C" void MethodVerifierGdbDump(MethodVerifier* v) {
1007 v->Dump(std::cerr);
1008}
1009
Ian Rogers776ac1f2012-04-13 23:36:36 -07001010void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001011 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001012 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001013 return;
jeffhaobdb76512011-09-07 11:43:16 -07001014 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001015 DCHECK(code_item_ != NULL);
1016 const Instruction* inst = Instruction::At(code_item_->insns_);
1017 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1018 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Elliott Hughesaa6e1cd2012-01-18 19:26:06 -08001019 os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
Elliott Hughesc073b072012-05-24 19:29:17 -07001020 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001021 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1022 if (reg_line != NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001023 os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001024 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001025 inst = inst->Next();
1026 }
jeffhaobdb76512011-09-07 11:43:16 -07001027}
1028
Ian Rogersd81871c2011-10-03 13:57:23 -07001029static bool IsPrimitiveDescriptor(char descriptor) {
1030 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001031 case 'I':
1032 case 'C':
1033 case 'S':
1034 case 'B':
1035 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001036 case 'F':
1037 case 'D':
1038 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001040 default:
1041 return false;
1042 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001043}
1044
Ian Rogers776ac1f2012-04-13 23:36:36 -07001045bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001046 RegisterLine* reg_line = reg_table_.GetLine(0);
1047 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1048 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001049
Ian Rogersd81871c2011-10-03 13:57:23 -07001050 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1051 //Include the "this" pointer.
1052 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001053 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001054 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1055 // argument as uninitialized. This restricts field access until the superclass constructor is
1056 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001057 const RegType& declaring_class = GetDeclaringClass();
1058 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001059 reg_line->SetRegisterType(arg_start + cur_arg,
1060 reg_types_.UninitializedThisArgument(declaring_class));
1061 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001062 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001063 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001064 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001065 }
1066
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001067 const DexFile::ProtoId& proto_id =
Ian Rogersad0b3a32012-04-16 14:50:24 -07001068 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001069 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001070
1071 for (; iterator.HasNext(); iterator.Next()) {
1072 const char* descriptor = iterator.GetDescriptor();
1073 if (descriptor == NULL) {
1074 LOG(FATAL) << "Null descriptor";
1075 }
1076 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001077 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1078 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001079 return false;
1080 }
1081 switch (descriptor[0]) {
1082 case 'L':
1083 case '[':
1084 // We assume that reference arguments are initialized. The only way it could be otherwise
1085 // (assuming the caller was verified) is if the current method is <init>, but in that case
1086 // it's effectively considered initialized the instant we reach here (in the sense that we
1087 // can return without doing anything or call virtual methods).
1088 {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001089 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001090 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001091 }
1092 break;
1093 case 'Z':
1094 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1095 break;
1096 case 'C':
1097 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1098 break;
1099 case 'B':
1100 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1101 break;
1102 case 'I':
1103 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1104 break;
1105 case 'S':
1106 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1107 break;
1108 case 'F':
1109 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1110 break;
1111 case 'J':
1112 case 'D': {
1113 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1114 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1115 cur_arg++;
1116 break;
1117 }
1118 default:
jeffhaod5347e02012-03-22 17:25:05 -07001119 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001120 return false;
1121 }
1122 cur_arg++;
1123 }
1124 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001125 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001126 return false;
1127 }
1128 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1129 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1130 // format. Only major difference from the method argument format is that 'V' is supported.
1131 bool result;
1132 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1133 result = descriptor[1] == '\0';
1134 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1135 size_t i = 0;
1136 do {
1137 i++;
1138 } while (descriptor[i] == '['); // process leading [
1139 if (descriptor[i] == 'L') { // object array
1140 do {
1141 i++; // find closing ;
1142 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1143 result = descriptor[i] == ';';
1144 } else { // primitive array
1145 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1146 }
1147 } else if (descriptor[0] == 'L') {
1148 // could be more thorough here, but shouldn't be required
1149 size_t i = 0;
1150 do {
1151 i++;
1152 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1153 result = descriptor[i] == ';';
1154 } else {
1155 result = false;
1156 }
1157 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001158 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1159 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001160 }
1161 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001162}
1163
Ian Rogers776ac1f2012-04-13 23:36:36 -07001164bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001165 const uint16_t* insns = code_item_->insns_;
1166 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001167
jeffhaobdb76512011-09-07 11:43:16 -07001168 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001169 insn_flags_[0].SetChanged();
1170 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001171
jeffhaobdb76512011-09-07 11:43:16 -07001172 /* Continue until no instructions are marked "changed". */
1173 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001174 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1175 uint32_t insn_idx = start_guess;
1176 for (; insn_idx < insns_size; insn_idx++) {
1177 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001178 break;
1179 }
jeffhaobdb76512011-09-07 11:43:16 -07001180 if (insn_idx == insns_size) {
1181 if (start_guess != 0) {
1182 /* try again, starting from the top */
1183 start_guess = 0;
1184 continue;
1185 } else {
1186 /* all flags are clear */
1187 break;
1188 }
1189 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001190 // We carry the working set of registers from instruction to instruction. If this address can
1191 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1192 // "changed" flags, we need to load the set of registers from the table.
1193 // Because we always prefer to continue on to the next instruction, we should never have a
1194 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1195 // target.
1196 work_insn_idx_ = insn_idx;
1197 if (insn_flags_[insn_idx].IsBranchTarget()) {
1198 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001199 } else {
1200#ifndef NDEBUG
1201 /*
1202 * Sanity check: retrieve the stored register line (assuming
1203 * a full table) and make sure it actually matches.
1204 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001205 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1206 if (register_line != NULL) {
1207 if (work_line_->CompareLine(register_line) != 0) {
1208 Dump(std::cout);
1209 std::cout << info_messages_.str();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001210 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001211 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1212 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001213 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001214 }
jeffhaobdb76512011-09-07 11:43:16 -07001215 }
1216#endif
1217 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001218 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001219 std::string prepend(PrettyMethod(method_idx_, *dex_file_));
1220 prepend += " failed to verify: ";
1221 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001222 return false;
1223 }
jeffhaobdb76512011-09-07 11:43:16 -07001224 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001225 insn_flags_[insn_idx].SetVisited();
1226 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001227 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001228
Ian Rogersad0b3a32012-04-16 14:50:24 -07001229 if (DEAD_CODE_SCAN && ((method_access_flags_ & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001230 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001231 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001232 * (besides the wasted space), but it indicates a flaw somewhere
1233 * down the line, possibly in the verifier.
1234 *
1235 * If we've substituted "always throw" instructions into the stream,
1236 * we are almost certainly going to have some dead code.
1237 */
1238 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001239 uint32_t insn_idx = 0;
1240 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001241 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001242 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001243 * may or may not be preceded by a padding NOP (for alignment).
1244 */
1245 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1246 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1247 insns[insn_idx] == Instruction::kArrayDataSignature ||
1248 (insns[insn_idx] == Instruction::NOP &&
1249 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1250 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1251 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001252 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001253 }
1254
Ian Rogersd81871c2011-10-03 13:57:23 -07001255 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001256 if (dead_start < 0)
1257 dead_start = insn_idx;
1258 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001259 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001260 dead_start = -1;
1261 }
1262 }
1263 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001264 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001265 }
1266 }
jeffhaobdb76512011-09-07 11:43:16 -07001267 return true;
1268}
1269
Ian Rogers776ac1f2012-04-13 23:36:36 -07001270bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001271#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001272 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001273 gDvm.verifierStats.instrsReexamined++;
1274 } else {
1275 gDvm.verifierStats.instrsExamined++;
1276 }
1277#endif
1278
1279 /*
1280 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001281 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001282 * control to another statement:
1283 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001284 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001285 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001286 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001287 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001288 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001289 * throw an exception that is handled by an encompassing "try"
1290 * block.
1291 *
1292 * We can also return, in which case there is no successor instruction
1293 * from this point.
1294 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001295 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001296 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001297 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1298 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001299 DecodedInstruction dec_insn(inst);
1300 int opcode_flags = Instruction::Flags(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001301
jeffhaobdb76512011-09-07 11:43:16 -07001302 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001303 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001304 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001305 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001306 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1307 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 }
jeffhaobdb76512011-09-07 11:43:16 -07001309
1310 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001311 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001312 * can throw an exception, we will copy/merge this into the "catch"
1313 * address rather than work_line, because we don't want the result
1314 * from the "successful" code path (e.g. a check-cast that "improves"
1315 * a type) to be visible to the exception handler.
1316 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001317 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001318 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001319 } else {
1320#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001321 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001322#endif
1323 }
1324
Elliott Hughesadb8c672012-03-06 16:49:32 -08001325 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07001326 case Instruction::NOP:
1327 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001328 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001329 * a signature that looks like a NOP; if we see one of these in
1330 * the course of executing code then we have a problem.
1331 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001332 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001333 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001334 }
1335 break;
1336
1337 case Instruction::MOVE:
1338 case Instruction::MOVE_FROM16:
1339 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001340 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001341 break;
1342 case Instruction::MOVE_WIDE:
1343 case Instruction::MOVE_WIDE_FROM16:
1344 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001345 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07001346 break;
1347 case Instruction::MOVE_OBJECT:
1348 case Instruction::MOVE_OBJECT_FROM16:
1349 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001350 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001351 break;
1352
1353 /*
1354 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001355 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001356 * might want to hold the result in an actual CPU register, so the
1357 * Dalvik spec requires that these only appear immediately after an
1358 * invoke or filled-new-array.
1359 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001360 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001361 * redundant with the reset done below, but it can make the debug info
1362 * easier to read in some cases.)
1363 */
1364 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001365 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07001366 break;
1367 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001368 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001369 break;
1370 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001371 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07001372 break;
1373
Ian Rogersd81871c2011-10-03 13:57:23 -07001374 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001375 /*
jeffhao60f83e32012-02-13 17:16:30 -08001376 * This statement can only appear as the first instruction in an exception handler. We verify
1377 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001378 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001379 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08001380 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001381 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001382 }
jeffhaobdb76512011-09-07 11:43:16 -07001383 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001384 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1385 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001386 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001387 }
jeffhaobdb76512011-09-07 11:43:16 -07001388 }
1389 break;
1390 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001391 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001392 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001393 const RegType& return_type = GetMethodReturnType();
1394 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001395 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001396 } else {
1397 // Compilers may generate synthetic functions that write byte values into boolean fields.
1398 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001399 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001400 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1401 ((return_type.IsBoolean() || return_type.IsByte() ||
1402 return_type.IsShort() || return_type.IsChar()) &&
1403 src_type.IsInteger()));
1404 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001405 bool success =
1406 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
1407 if (!success) {
1408 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001409 }
jeffhaobdb76512011-09-07 11:43:16 -07001410 }
1411 }
1412 break;
1413 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001414 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001415 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001416 const RegType& return_type = GetMethodReturnType();
1417 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001418 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001419 } else {
1420 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001421 bool success = work_line_->VerifyRegisterType(dec_insn.vA, return_type);
1422 if (!success) {
1423 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001424 }
jeffhaobdb76512011-09-07 11:43:16 -07001425 }
1426 }
1427 break;
1428 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001429 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001430 const RegType& return_type = GetMethodReturnType();
1431 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001432 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001433 } else {
1434 /* return_type is the *expected* return type, not register value */
1435 DCHECK(!return_type.IsZero());
1436 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08001437 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07001438 // Disallow returning uninitialized values and verify that the reference in vAA is an
1439 // instance of the "return_type"
1440 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001441 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001442 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001443 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1444 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001445 }
1446 }
1447 }
1448 break;
1449
1450 case Instruction::CONST_4:
1451 case Instruction::CONST_16:
1452 case Instruction::CONST:
1453 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001454 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const((int32_t) dec_insn.vB));
jeffhaobdb76512011-09-07 11:43:16 -07001455 break;
1456 case Instruction::CONST_HIGH16:
1457 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001458 work_line_->SetRegisterType(dec_insn.vA,
1459 reg_types_.FromCat1Const((int32_t) dec_insn.vB << 16));
jeffhaobdb76512011-09-07 11:43:16 -07001460 break;
1461 case Instruction::CONST_WIDE_16:
1462 case Instruction::CONST_WIDE_32:
1463 case Instruction::CONST_WIDE:
1464 case Instruction::CONST_WIDE_HIGH16:
1465 /* could be long or double; resolved upon use */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001466 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07001467 break;
1468 case Instruction::CONST_STRING:
1469 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001470 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001471 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001472 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001473 // Get type from instruction if unresolved then we need an access check
1474 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08001475 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001476 // Register holds class, ie its type is class, on error it will hold Conflict.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001477 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogersad0b3a32012-04-16 14:50:24 -07001478 res_type.IsConflict() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001479 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001480 }
jeffhaobdb76512011-09-07 11:43:16 -07001481 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001482 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001483 break;
1484 case Instruction::MONITOR_EXIT:
1485 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001486 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001487 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001488 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001489 * to the need to handle asynchronous exceptions, a now-deprecated
1490 * feature that Dalvik doesn't support.)
1491 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001492 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001493 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001494 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001495 * structured locking checks are working, the former would have
1496 * failed on the -enter instruction, and the latter is impossible.
1497 *
1498 * This is fortunate, because issue 3221411 prevents us from
1499 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001500 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001501 * some catch blocks (which will show up as "dead" code when
1502 * we skip them here); if we can't, then the code path could be
1503 * "live" so we still need to check it.
1504 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001505 opcode_flags &= ~Instruction::kThrow;
1506 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001507 break;
1508
Ian Rogers28ad40d2011-10-27 15:19:26 -07001509 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001510 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001511 /*
1512 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1513 * could be a "upcast" -- not expected, so we don't try to address it.)
1514 *
1515 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001516 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001517 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001518 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001519 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001520 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001521 if (res_type.IsConflict()) {
1522 DCHECK_NE(failures_.size(), 0U);
1523 if (!is_checkcast) {
1524 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
1525 }
1526 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001527 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001528 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1529 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001530 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001531 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001532 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001533 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001534 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07001535 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001536 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001537 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001538 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001539 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001540 }
jeffhaobdb76512011-09-07 11:43:16 -07001541 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001542 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001543 }
1544 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001545 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001546 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001547 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001548 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001549 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001550 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001551 }
1552 }
1553 break;
1554 }
1555 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001556 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001557 if (res_type.IsConflict()) {
1558 DCHECK_NE(failures_.size(), 0U);
1559 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001560 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001561 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1562 // can't create an instance of an interface or abstract class */
1563 if (!res_type.IsInstantiableTypes()) {
1564 Fail(VERIFY_ERROR_INSTANTIATION)
1565 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001566 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001567 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1568 // Any registers holding previous allocations from this address that have not yet been
1569 // initialized must be marked invalid.
1570 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1571 // add the new uninitialized reference to the register state
Elliott Hughesadb8c672012-03-06 16:49:32 -08001572 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001573 }
1574 break;
1575 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001576 case Instruction::NEW_ARRAY:
1577 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001578 break;
1579 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08001580 VerifyNewArray(dec_insn, true, false);
1581 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001582 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001583 case Instruction::FILLED_NEW_ARRAY_RANGE:
1584 VerifyNewArray(dec_insn, true, true);
1585 just_set_result = true; // Filled new array range sets result register
1586 break;
jeffhaobdb76512011-09-07 11:43:16 -07001587 case Instruction::CMPL_FLOAT:
1588 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001589 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001590 break;
1591 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001592 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001593 break;
1594 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001595 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001596 break;
1597 case Instruction::CMPL_DOUBLE:
1598 case Instruction::CMPG_DOUBLE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001599 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08001600 break;
1601 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001602 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08001603 break;
1604 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001605 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001606 break;
1607 case Instruction::CMP_LONG:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001608 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08001609 break;
1610 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001611 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08001612 break;
1613 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001614 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001615 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001616 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001617 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001618 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001619 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001620 }
1621 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001622 }
jeffhaobdb76512011-09-07 11:43:16 -07001623 case Instruction::GOTO:
1624 case Instruction::GOTO_16:
1625 case Instruction::GOTO_32:
1626 /* no effect on or use of registers */
1627 break;
1628
1629 case Instruction::PACKED_SWITCH:
1630 case Instruction::SPARSE_SWITCH:
1631 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001632 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001633 break;
1634
Ian Rogersd81871c2011-10-03 13:57:23 -07001635 case Instruction::FILL_ARRAY_DATA: {
1636 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001637 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08001638 /* array_type can be null if the reg type is Zero */
1639 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001640 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001641 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001642 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001643 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1644 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001645 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001646 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1647 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001648 } else {
jeffhao457cc512012-02-02 16:55:13 -08001649 // Now verify if the element width in the table matches the element width declared in
1650 // the array
1651 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1652 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001653 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001654 } else {
1655 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1656 // Since we don't compress the data in Dex, expect to see equal width of data stored
1657 // in the table and expected from the array class.
1658 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001659 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1660 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001661 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001662 }
1663 }
jeffhaobdb76512011-09-07 11:43:16 -07001664 }
1665 }
1666 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001667 }
jeffhaobdb76512011-09-07 11:43:16 -07001668 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001669 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001670 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1671 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001672 bool mismatch = false;
1673 if (reg_type1.IsZero()) { // zero then integral or reference expected
1674 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1675 } else if (reg_type1.IsReferenceTypes()) { // both references?
1676 mismatch = !reg_type2.IsReferenceTypes();
1677 } else { // both integral?
1678 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1679 }
1680 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001681 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1682 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001683 }
1684 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001685 }
jeffhaobdb76512011-09-07 11:43:16 -07001686 case Instruction::IF_LT:
1687 case Instruction::IF_GE:
1688 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001689 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001690 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1691 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001692 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001693 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1694 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001695 }
1696 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001697 }
jeffhaobdb76512011-09-07 11:43:16 -07001698 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001699 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001700 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001701 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001702 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001703 }
jeffhaobdb76512011-09-07 11:43:16 -07001704 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001705 }
jeffhaobdb76512011-09-07 11:43:16 -07001706 case Instruction::IF_LTZ:
1707 case Instruction::IF_GEZ:
1708 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001709 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001710 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001711 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001712 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1713 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001714 }
jeffhaobdb76512011-09-07 11:43:16 -07001715 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001716 }
jeffhaobdb76512011-09-07 11:43:16 -07001717 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001718 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
1719 break;
jeffhaobdb76512011-09-07 11:43:16 -07001720 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001721 VerifyAGet(dec_insn, reg_types_.Byte(), true);
1722 break;
jeffhaobdb76512011-09-07 11:43:16 -07001723 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07001724 VerifyAGet(dec_insn, reg_types_.Char(), true);
1725 break;
jeffhaobdb76512011-09-07 11:43:16 -07001726 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001727 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001728 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001729 case Instruction::AGET:
1730 VerifyAGet(dec_insn, reg_types_.Integer(), true);
1731 break;
jeffhaobdb76512011-09-07 11:43:16 -07001732 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001733 VerifyAGet(dec_insn, reg_types_.Long(), true);
1734 break;
1735 case Instruction::AGET_OBJECT:
1736 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001737 break;
1738
Ian Rogersd81871c2011-10-03 13:57:23 -07001739 case Instruction::APUT_BOOLEAN:
1740 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
1741 break;
1742 case Instruction::APUT_BYTE:
1743 VerifyAPut(dec_insn, reg_types_.Byte(), true);
1744 break;
1745 case Instruction::APUT_CHAR:
1746 VerifyAPut(dec_insn, reg_types_.Char(), true);
1747 break;
1748 case Instruction::APUT_SHORT:
1749 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001750 break;
1751 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001752 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001753 break;
1754 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001755 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001756 break;
1757 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001758 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001759 break;
1760
jeffhaobdb76512011-09-07 11:43:16 -07001761 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001762 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001763 break;
jeffhaobdb76512011-09-07 11:43:16 -07001764 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001765 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001766 break;
jeffhaobdb76512011-09-07 11:43:16 -07001767 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001768 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001769 break;
jeffhaobdb76512011-09-07 11:43:16 -07001770 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001771 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001772 break;
1773 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001774 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001775 break;
1776 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001777 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001778 break;
1779 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001780 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001781 break;
jeffhaobdb76512011-09-07 11:43:16 -07001782
Ian Rogersd81871c2011-10-03 13:57:23 -07001783 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001784 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001785 break;
1786 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001787 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001788 break;
1789 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001790 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001791 break;
1792 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001793 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001794 break;
1795 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001796 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001797 break;
1798 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001799 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001800 break;
jeffhaobdb76512011-09-07 11:43:16 -07001801 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001802 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001803 break;
1804
jeffhaobdb76512011-09-07 11:43:16 -07001805 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001806 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001807 break;
jeffhaobdb76512011-09-07 11:43:16 -07001808 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001809 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001810 break;
jeffhaobdb76512011-09-07 11:43:16 -07001811 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001812 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001813 break;
jeffhaobdb76512011-09-07 11:43:16 -07001814 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001815 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001816 break;
1817 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001818 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001819 break;
1820 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001821 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001822 break;
1823 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001824 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001825 break;
1826
1827 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001828 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001829 break;
1830 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001831 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001832 break;
1833 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001834 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001835 break;
1836 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001837 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001838 break;
1839 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001840 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001841 break;
1842 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001843 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001844 break;
1845 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001846 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001847 break;
1848
1849 case Instruction::INVOKE_VIRTUAL:
1850 case Instruction::INVOKE_VIRTUAL_RANGE:
1851 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001852 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001853 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
1854 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
1855 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
1856 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001857 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001858 const char* descriptor;
1859 if (called_method == NULL) {
1860 uint32_t method_idx = dec_insn.vB;
1861 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1862 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1863 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1864 } else {
1865 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07001866 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001867 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
1868 work_line_->SetResultRegisterType(return_type);
1869 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001870 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001871 }
jeffhaobdb76512011-09-07 11:43:16 -07001872 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001873 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001874 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001875 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07001876 const char* return_type_descriptor;
1877 bool is_constructor;
1878 if (called_method == NULL) {
1879 uint32_t method_idx = dec_insn.vB;
1880 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1881 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
1882 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1883 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1884 } else {
1885 is_constructor = called_method->IsConstructor();
1886 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
1887 }
1888 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07001889 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001890 * Some additional checks when calling a constructor. We know from the invocation arg check
1891 * that the "this" argument is an instance of called_method->klass. Now we further restrict
1892 * that to require that called_method->klass is the same as this->klass or this->super,
1893 * allowing the latter only if the "this" argument is the same as the "this" argument to
1894 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07001895 */
jeffhaob57e9522012-04-26 18:08:21 -07001896 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1897 if (this_type.IsConflict()) // failure.
1898 break;
jeffhaobdb76512011-09-07 11:43:16 -07001899
jeffhaob57e9522012-04-26 18:08:21 -07001900 /* no null refs allowed (?) */
1901 if (this_type.IsZero()) {
1902 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
1903 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07001904 }
jeffhaob57e9522012-04-26 18:08:21 -07001905
1906 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07001907 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
1908 // TODO: re-enable constructor type verification
1909 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07001910 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07001911 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
1912 // break;
1913 // }
jeffhaob57e9522012-04-26 18:08:21 -07001914
1915 /* arg must be an uninitialized reference */
1916 if (!this_type.IsUninitializedTypes()) {
1917 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
1918 << this_type;
1919 break;
1920 }
1921
1922 /*
1923 * Replace the uninitialized reference with an initialized one. We need to do this for all
1924 * registers that have the same object instance in them, not just the "this" register.
1925 */
1926 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001927 }
Ian Rogers46685432012-06-03 22:26:43 -07001928 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001929 work_line_->SetResultRegisterType(return_type);
1930 just_set_result = true;
1931 break;
1932 }
1933 case Instruction::INVOKE_STATIC:
1934 case Instruction::INVOKE_STATIC_RANGE: {
1935 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
1936 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001937 const char* descriptor;
1938 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001939 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001940 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1941 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07001942 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001943 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001944 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001945 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001946 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07001947 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07001948 just_set_result = true;
1949 }
1950 break;
jeffhaobdb76512011-09-07 11:43:16 -07001951 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001952 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001953 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001954 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001955 if (abs_method != NULL) {
1956 Class* called_interface = abs_method->GetDeclaringClass();
1957 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
1958 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
1959 << PrettyMethod(abs_method) << "'";
1960 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001961 }
Ian Rogers0d604842012-04-16 14:50:24 -07001962 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001963 /* Get the type of the "this" arg, which should either be a sub-interface of called
1964 * interface or Object (see comments in RegType::JoinClass).
1965 */
1966 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1967 if (this_type.IsZero()) {
1968 /* null pointer always passes (and always fails at runtime) */
1969 } else {
1970 if (this_type.IsUninitializedTypes()) {
1971 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
1972 << this_type;
1973 break;
1974 }
1975 // In the past we have tried to assert that "called_interface" is assignable
1976 // from "this_type.GetClass()", however, as we do an imprecise Join
1977 // (RegType::JoinClass) we don't have full information on what interfaces are
1978 // implemented by "this_type". For example, two classes may implement the same
1979 // interfaces and have a common parent that doesn't implement the interface. The
1980 // join will set "this_type" to the parent class and a test that this implements
1981 // the interface will incorrectly fail.
1982 }
1983 /*
1984 * We don't have an object instance, so we can't find the concrete method. However, all of
1985 * the type information is in the abstract method, so we're good.
1986 */
1987 const char* descriptor;
1988 if (abs_method == NULL) {
1989 uint32_t method_idx = dec_insn.vB;
1990 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1991 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1992 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1993 } else {
1994 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
1995 }
1996 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
1997 work_line_->SetResultRegisterType(return_type);
1998 work_line_->SetResultRegisterType(return_type);
1999 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002000 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002001 }
jeffhaobdb76512011-09-07 11:43:16 -07002002 case Instruction::NEG_INT:
2003 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002004 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002005 break;
2006 case Instruction::NEG_LONG:
2007 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002008 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002009 break;
2010 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002011 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002012 break;
2013 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002014 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002015 break;
2016 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002017 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002018 break;
2019 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002020 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002021 break;
2022 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002023 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002024 break;
2025 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002026 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002027 break;
2028 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002029 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002030 break;
2031 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002032 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002033 break;
2034 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002035 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002036 break;
2037 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002038 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002039 break;
2040 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002041 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002042 break;
2043 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002044 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002045 break;
2046 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002047 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002048 break;
2049 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002050 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002051 break;
2052 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002053 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002054 break;
2055 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002056 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002057 break;
2058 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002059 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002060 break;
2061
2062 case Instruction::ADD_INT:
2063 case Instruction::SUB_INT:
2064 case Instruction::MUL_INT:
2065 case Instruction::REM_INT:
2066 case Instruction::DIV_INT:
2067 case Instruction::SHL_INT:
2068 case Instruction::SHR_INT:
2069 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002070 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002071 break;
2072 case Instruction::AND_INT:
2073 case Instruction::OR_INT:
2074 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002075 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002076 break;
2077 case Instruction::ADD_LONG:
2078 case Instruction::SUB_LONG:
2079 case Instruction::MUL_LONG:
2080 case Instruction::DIV_LONG:
2081 case Instruction::REM_LONG:
2082 case Instruction::AND_LONG:
2083 case Instruction::OR_LONG:
2084 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002085 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002086 break;
2087 case Instruction::SHL_LONG:
2088 case Instruction::SHR_LONG:
2089 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002090 /* shift distance is Int, making these different from other binary operations */
2091 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002092 break;
2093 case Instruction::ADD_FLOAT:
2094 case Instruction::SUB_FLOAT:
2095 case Instruction::MUL_FLOAT:
2096 case Instruction::DIV_FLOAT:
2097 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002098 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002099 break;
2100 case Instruction::ADD_DOUBLE:
2101 case Instruction::SUB_DOUBLE:
2102 case Instruction::MUL_DOUBLE:
2103 case Instruction::DIV_DOUBLE:
2104 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002105 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002106 break;
2107 case Instruction::ADD_INT_2ADDR:
2108 case Instruction::SUB_INT_2ADDR:
2109 case Instruction::MUL_INT_2ADDR:
2110 case Instruction::REM_INT_2ADDR:
2111 case Instruction::SHL_INT_2ADDR:
2112 case Instruction::SHR_INT_2ADDR:
2113 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002114 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002115 break;
2116 case Instruction::AND_INT_2ADDR:
2117 case Instruction::OR_INT_2ADDR:
2118 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002119 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002120 break;
2121 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002122 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002123 break;
2124 case Instruction::ADD_LONG_2ADDR:
2125 case Instruction::SUB_LONG_2ADDR:
2126 case Instruction::MUL_LONG_2ADDR:
2127 case Instruction::DIV_LONG_2ADDR:
2128 case Instruction::REM_LONG_2ADDR:
2129 case Instruction::AND_LONG_2ADDR:
2130 case Instruction::OR_LONG_2ADDR:
2131 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002132 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002133 break;
2134 case Instruction::SHL_LONG_2ADDR:
2135 case Instruction::SHR_LONG_2ADDR:
2136 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002137 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002138 break;
2139 case Instruction::ADD_FLOAT_2ADDR:
2140 case Instruction::SUB_FLOAT_2ADDR:
2141 case Instruction::MUL_FLOAT_2ADDR:
2142 case Instruction::DIV_FLOAT_2ADDR:
2143 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002144 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002145 break;
2146 case Instruction::ADD_DOUBLE_2ADDR:
2147 case Instruction::SUB_DOUBLE_2ADDR:
2148 case Instruction::MUL_DOUBLE_2ADDR:
2149 case Instruction::DIV_DOUBLE_2ADDR:
2150 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002151 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002152 break;
2153 case Instruction::ADD_INT_LIT16:
2154 case Instruction::RSUB_INT:
2155 case Instruction::MUL_INT_LIT16:
2156 case Instruction::DIV_INT_LIT16:
2157 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002158 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002159 break;
2160 case Instruction::AND_INT_LIT16:
2161 case Instruction::OR_INT_LIT16:
2162 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002163 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002164 break;
2165 case Instruction::ADD_INT_LIT8:
2166 case Instruction::RSUB_INT_LIT8:
2167 case Instruction::MUL_INT_LIT8:
2168 case Instruction::DIV_INT_LIT8:
2169 case Instruction::REM_INT_LIT8:
2170 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002171 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002172 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002173 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002174 break;
2175 case Instruction::AND_INT_LIT8:
2176 case Instruction::OR_INT_LIT8:
2177 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002178 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002179 break;
2180
2181 /*
2182 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002183 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002184 * inserted in the course of verification, we can expect to see it here.
2185 */
jeffhaob4df5142011-09-19 20:25:32 -07002186 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002187 break;
2188
Ian Rogersd81871c2011-10-03 13:57:23 -07002189 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002190 case Instruction::UNUSED_EE:
2191 case Instruction::UNUSED_EF:
2192 case Instruction::UNUSED_F2:
2193 case Instruction::UNUSED_F3:
2194 case Instruction::UNUSED_F4:
2195 case Instruction::UNUSED_F5:
2196 case Instruction::UNUSED_F6:
2197 case Instruction::UNUSED_F7:
2198 case Instruction::UNUSED_F8:
2199 case Instruction::UNUSED_F9:
2200 case Instruction::UNUSED_FA:
2201 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002202 case Instruction::UNUSED_F0:
2203 case Instruction::UNUSED_F1:
2204 case Instruction::UNUSED_E3:
2205 case Instruction::UNUSED_E8:
2206 case Instruction::UNUSED_E7:
2207 case Instruction::UNUSED_E4:
2208 case Instruction::UNUSED_E9:
2209 case Instruction::UNUSED_FC:
2210 case Instruction::UNUSED_E5:
2211 case Instruction::UNUSED_EA:
2212 case Instruction::UNUSED_FD:
2213 case Instruction::UNUSED_E6:
2214 case Instruction::UNUSED_EB:
2215 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002216 case Instruction::UNUSED_3E:
2217 case Instruction::UNUSED_3F:
2218 case Instruction::UNUSED_40:
2219 case Instruction::UNUSED_41:
2220 case Instruction::UNUSED_42:
2221 case Instruction::UNUSED_43:
2222 case Instruction::UNUSED_73:
2223 case Instruction::UNUSED_79:
2224 case Instruction::UNUSED_7A:
2225 case Instruction::UNUSED_EC:
2226 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002227 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002228 break;
2229
2230 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002231 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002232 * complain if an instruction is missing (which is desirable).
2233 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002234 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002235
Ian Rogersad0b3a32012-04-16 14:50:24 -07002236 if (have_pending_hard_failure_) {
2237 if (!Runtime::Current()->IsStarted()) {
jeffhaob57e9522012-04-26 18:08:21 -07002238 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002239 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002240 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002241 /* immediate failure, reject class */
2242 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2243 return false;
2244 } else if (have_pending_rewrite_failure_) {
2245 /* replace opcode and continue on */
2246 std::string append("Replacing opcode ");
2247 append += inst->DumpString(dex_file_);
2248 AppendToLastFailMessage(append);
2249 ReplaceFailingInstruction();
2250 /* IMPORTANT: method->insns may have been changed */
2251 insns = code_item_->insns_ + work_insn_idx_;
2252 /* continue on as if we just handled a throw-verification-error */
2253 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002254 }
jeffhaobdb76512011-09-07 11:43:16 -07002255 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002256 * If we didn't just set the result register, clear it out. This ensures that you can only use
2257 * "move-result" immediately after the result is set. (We could check this statically, but it's
2258 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002259 */
2260 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002261 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002262 }
2263
jeffhaoa0a764a2011-09-16 10:43:38 -07002264 /* Handle "continue". Tag the next consecutive instruction. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002265 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers776ac1f2012-04-13 23:36:36 -07002266 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
Ian Rogersd81871c2011-10-03 13:57:23 -07002267 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
jeffhaod5347e02012-03-22 17:25:05 -07002268 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002269 return false;
2270 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002271 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2272 // next instruction isn't one.
jeffhaod5347e02012-03-22 17:25:05 -07002273 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002274 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002275 }
2276 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2277 if (next_line != NULL) {
2278 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2279 // needed.
2280 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002281 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002282 }
jeffhaobdb76512011-09-07 11:43:16 -07002283 } else {
2284 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002285 * We're not recording register data for the next instruction, so we don't know what the prior
2286 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002287 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002288 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002289 }
2290 }
2291
2292 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002293 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002294 *
2295 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002296 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002297 * somebody could get a reference field, check it for zero, and if the
2298 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002299 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002300 * that, and will reject the code.
2301 *
2302 * TODO: avoid re-fetching the branch target
2303 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002304 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002305 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002306 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002307 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002308 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002309 return false;
2310 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002311 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002312 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002313 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002314 }
jeffhaobdb76512011-09-07 11:43:16 -07002315 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002316 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002317 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002318 }
jeffhaobdb76512011-09-07 11:43:16 -07002319 }
2320
2321 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002322 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002323 *
2324 * We've already verified that the table is structurally sound, so we
2325 * just need to walk through and tag the targets.
2326 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002327 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002328 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2329 const uint16_t* switch_insns = insns + offset_to_switch;
2330 int switch_count = switch_insns[1];
2331 int offset_to_targets, targ;
2332
2333 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2334 /* 0 = sig, 1 = count, 2/3 = first key */
2335 offset_to_targets = 4;
2336 } else {
2337 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002338 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002339 offset_to_targets = 2 + 2 * switch_count;
2340 }
2341
2342 /* verify each switch target */
2343 for (targ = 0; targ < switch_count; targ++) {
2344 int offset;
2345 uint32_t abs_offset;
2346
2347 /* offsets are 32-bit, and only partly endian-swapped */
2348 offset = switch_insns[offset_to_targets + targ * 2] |
2349 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002350 abs_offset = work_insn_idx_ + offset;
2351 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002352 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002353 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002354 }
2355 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002356 return false;
2357 }
2358 }
2359
2360 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002361 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2362 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002363 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002364 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002365 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002366 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002367
Ian Rogers0571d352011-11-03 19:51:38 -07002368 for (; iterator.HasNext(); iterator.Next()) {
2369 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002370 within_catch_all = true;
2371 }
jeffhaobdb76512011-09-07 11:43:16 -07002372 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002373 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2374 * "work_regs", because at runtime the exception will be thrown before the instruction
2375 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002376 */
Ian Rogers0571d352011-11-03 19:51:38 -07002377 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002378 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002379 }
jeffhaobdb76512011-09-07 11:43:16 -07002380 }
2381
2382 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002383 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2384 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002385 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002386 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002387 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002388 * The state in work_line reflects the post-execution state. If the current instruction is a
2389 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002390 * it will do so before grabbing the lock).
2391 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002392 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002393 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002394 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002395 return false;
2396 }
2397 }
2398 }
2399
jeffhaod1f0fde2011-09-08 17:25:33 -07002400 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002401 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002402 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002403 return false;
2404 }
jeffhaobdb76512011-09-07 11:43:16 -07002405 }
2406
2407 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002408 * Update start_guess. Advance to the next instruction of that's
2409 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002410 * neither of those exists we're in a return or throw; leave start_guess
2411 * alone and let the caller sort it out.
2412 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002413 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002414 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002415 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002416 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002417 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002418 }
2419
Ian Rogersd81871c2011-10-03 13:57:23 -07002420 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2421 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002422
2423 return true;
2424}
2425
Ian Rogers776ac1f2012-04-13 23:36:36 -07002426const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002427 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002428 const RegType& referrer = GetDeclaringClass();
2429 Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002430 const RegType& result =
2431 klass != NULL ? reg_types_.FromClass(klass)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002432 : reg_types_.FromDescriptor(class_loader_, descriptor);
2433 if (result.IsConflict()) {
2434 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2435 << "' in " << referrer;
2436 return result;
2437 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002438 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002439 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002440 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002441 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002442 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002443 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002444 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002445 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002446 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002447 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002448}
2449
Ian Rogers776ac1f2012-04-13 23:36:36 -07002450const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002451 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002452 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002453 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002454 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2455 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002456 CatchHandlerIterator iterator(handlers_ptr);
2457 for (; iterator.HasNext(); iterator.Next()) {
2458 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2459 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002460 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07002461 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002462 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002463 if (common_super == NULL) {
2464 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2465 // as that is caught at runtime
2466 common_super = &exception;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002467 } else if (!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002468 // We don't know enough about the type and the common path merge will result in
2469 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002470 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002471 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002472 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002473 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002474 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002475 common_super = &common_super->Merge(exception, &reg_types_);
2476 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002477 }
2478 }
2479 }
2480 }
Ian Rogers0571d352011-11-03 19:51:38 -07002481 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002482 }
2483 }
2484 if (common_super == NULL) {
2485 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002486 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002487 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002488 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002489 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002490}
2491
Ian Rogersad0b3a32012-04-16 14:50:24 -07002492Method* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx, MethodType method_type) {
2493 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002494 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002495 if (klass_type.IsConflict()) {
2496 std::string append(" in attempt to access method ");
2497 append += dex_file_->GetMethodName(method_id);
2498 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002499 return NULL;
2500 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002501 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002502 return NULL; // Can't resolve Class so no more to do here
2503 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002504 Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002505 const RegType& referrer = GetDeclaringClass();
2506 Method* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002507 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002508 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002509 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002510
2511 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002512 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002513 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002514 res_method = klass->FindInterfaceMethod(name, signature);
2515 } else {
2516 res_method = klass->FindVirtualMethod(name, signature);
2517 }
2518 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002519 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002520 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002521 // If a virtual or interface method wasn't found with the expected type, look in
2522 // the direct methods. This can happen when the wrong invoke type is used or when
2523 // a class has changed, and will be flagged as an error in later checks.
2524 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2525 res_method = klass->FindDirectMethod(name, signature);
2526 }
2527 if (res_method == NULL) {
2528 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2529 << PrettyDescriptor(klass) << "." << name
2530 << " " << signature;
2531 return NULL;
2532 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002533 }
2534 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002535 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2536 // enforce them here.
2537 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002538 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2539 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002540 return NULL;
2541 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002542 // Disallow any calls to class initializers.
2543 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002544 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2545 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002546 return NULL;
2547 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002548 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002549 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002550 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002551 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002552 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002553 }
jeffhaode0d9c92012-02-27 13:58:13 -08002554 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2555 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002556 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2557 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002558 return NULL;
2559 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002560 // Check that interface methods match interface classes.
2561 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2562 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2563 << " is in an interface class " << PrettyClass(klass);
2564 return NULL;
2565 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2566 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2567 << " is in a non-interface class " << PrettyClass(klass);
2568 return NULL;
2569 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002570 // See if the method type implied by the invoke instruction matches the access flags for the
2571 // target method.
2572 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2573 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2574 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2575 ) {
Ian Rogers573db4a2011-12-13 15:30:50 -08002576 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type does not match method type of "
2577 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002578 return NULL;
2579 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002580 return res_method;
2581}
2582
Ian Rogers776ac1f2012-04-13 23:36:36 -07002583Method* MethodVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
Ian Rogers46685432012-06-03 22:26:43 -07002584 MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002585 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2586 // we're making.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002587 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002588 if (res_method == NULL) { // error or class is unresolved
2589 return NULL;
2590 }
2591
Ian Rogersd81871c2011-10-03 13:57:23 -07002592 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2593 // has a vtable entry for the target method.
2594 if (is_super) {
2595 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002596 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
jeffhao4d8df822012-04-24 17:09:36 -07002597 if (super.IsConflict()) { // unknown super class
2598 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
2599 << PrettyMethod(method_idx_, *dex_file_)
2600 << " to super " << PrettyMethod(res_method);
2601 return NULL;
2602 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002603 Class* super_klass = super.GetClass();
2604 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002605 MethodHelper mh(res_method);
2606 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
2607 << PrettyMethod(method_idx_, *dex_file_)
2608 << " to super " << super
2609 << "." << mh.GetName()
2610 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002611 return NULL;
2612 }
2613 }
2614 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2615 // match the call to the signature. Also, we might might be calling through an abstract method
2616 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08002617 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002618 /* caught by static verifier */
2619 DCHECK(is_range || expected_args <= 5);
2620 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002621 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002622 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2623 return NULL;
2624 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002625
jeffhaobdb76512011-09-07 11:43:16 -07002626 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002627 * Check the "this" argument, which must be an instance of the class that declared the method.
2628 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2629 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002630 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002631 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002632 if (!res_method->IsStatic()) {
2633 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002634 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002635 return NULL;
2636 }
2637 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002638 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002639 return NULL;
2640 }
2641 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07002642 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
2643 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002644 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002645 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002646 return NULL;
2647 }
2648 }
2649 actual_args++;
2650 }
2651 /*
2652 * Process the target method's signature. This signature may or may not
2653 * have been verified, so we can't assume it's properly formed.
2654 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002655 MethodHelper mh(res_method);
2656 const DexFile::TypeList* params = mh.GetParameterTypeList();
2657 size_t params_size = params == NULL ? 0 : params->Size();
2658 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002659 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002660 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002661 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2662 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002663 return NULL;
2664 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002665 const char* descriptor =
2666 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2667 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002668 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002669 << " missing signature component";
2670 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002671 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002672 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002673 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002674 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002675 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002676 }
2677 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2678 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002679 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002680 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002681 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002682 return NULL;
2683 } else {
2684 return res_method;
2685 }
2686}
2687
Ian Rogers776ac1f2012-04-13 23:36:36 -07002688void MethodVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08002689 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002690 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002691 if (res_type.IsConflict()) { // bad class
2692 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002693 } else {
2694 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2695 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002696 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002697 } else if (!is_filled) {
2698 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002699 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002700 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002701 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002702 } else {
2703 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2704 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002705 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002706 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002707 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002708 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002709 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002710 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002711 return;
2712 }
2713 }
2714 // filled-array result goes into "result" register
2715 work_line_->SetResultRegisterType(res_type);
2716 }
2717 }
2718}
2719
Ian Rogers776ac1f2012-04-13 23:36:36 -07002720void MethodVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002721 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002722 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002723 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002725 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002726 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002727 if (array_type.IsZero()) {
2728 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2729 // instruction type. TODO: have a proper notion of bottom here.
2730 if (!is_primitive || insn_type.IsCategory1Types()) {
2731 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08002732 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002733 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002734 // Category 2
Elliott Hughesadb8c672012-03-06 16:49:32 -08002735 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
Ian Rogers89310de2012-02-01 13:47:30 -08002736 }
jeffhaofc3144e2012-02-01 17:21:15 -08002737 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002738 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002739 } else {
2740 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002741 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002742 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002743 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002744 << " source for aget-object";
2745 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002746 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002747 << " source for category 1 aget";
2748 } else if (is_primitive && !insn_type.Equals(component_type) &&
2749 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2750 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002751 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogersd81871c2011-10-03 13:57:23 -07002752 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002753 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002754 // Use knowledge of the field type which is stronger than the type inferred from the
2755 // instruction, which can't differentiate object types and ints from floats, longs from
2756 // doubles.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002757 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002758 }
2759 }
2760 }
2761}
2762
Ian Rogers776ac1f2012-04-13 23:36:36 -07002763void MethodVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002764 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002765 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002766 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002767 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002768 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002769 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002770 if (array_type.IsZero()) {
2771 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2772 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002773 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002774 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002775 } else {
2776 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002777 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002778 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002779 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002780 << " source for aput-object";
2781 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002782 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002783 << " source for category 1 aput";
2784 } else if (is_primitive && !insn_type.Equals(component_type) &&
2785 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2786 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002787 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002788 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002789 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002790 // The instruction agrees with the type of array, confirm the value to be stored does too
2791 // Note: we use the instruction type (rather than the component type) for aput-object as
2792 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08002793 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002794 }
2795 }
2796 }
2797}
2798
Ian Rogers776ac1f2012-04-13 23:36:36 -07002799Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002800 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2801 // Check access to class
2802 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002803 if (klass_type.IsConflict()) { // bad class
2804 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
2805 field_idx, dex_file_->GetFieldName(field_id),
2806 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002807 return NULL;
2808 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002809 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002810 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08002811 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002812 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2813 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002814 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002815 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
2816 << dex_file_->GetFieldName(field_id) << ") in "
2817 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002818 DCHECK(Thread::Current()->IsExceptionPending());
2819 Thread::Current()->ClearException();
2820 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002821 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2822 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002823 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002824 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002825 return NULL;
2826 } else if (!field->IsStatic()) {
2827 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
2828 return NULL;
2829 } else {
2830 return field;
2831 }
2832}
2833
Ian Rogers776ac1f2012-04-13 23:36:36 -07002834Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002835 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2836 // Check access to class
2837 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002838 if (klass_type.IsConflict()) {
2839 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
2840 field_idx, dex_file_->GetFieldName(field_id),
2841 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002842 return NULL;
2843 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002844 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002845 return NULL; // Can't resolve Class so no more to do here
2846 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002847 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2848 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002849 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002850 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
2851 << dex_file_->GetFieldName(field_id) << ") in "
2852 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002853 DCHECK(Thread::Current()->IsExceptionPending());
2854 Thread::Current()->ClearException();
2855 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002856 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2857 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002858 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002859 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002860 return NULL;
2861 } else if (field->IsStatic()) {
2862 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
2863 << " to not be static";
2864 return NULL;
2865 } else if (obj_type.IsZero()) {
2866 // Cannot infer and check type, however, access will cause null pointer exception
2867 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07002868 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002869 const RegType& field_klass = reg_types_.FromClass(field->GetDeclaringClass());
2870 if (obj_type.IsUninitializedTypes() &&
2871 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
2872 !field_klass.Equals(GetDeclaringClass()))) {
2873 // Field accesses through uninitialized references are only allowable for constructors where
2874 // the field is declared in this class
2875 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
2876 << " of a not fully initialized object within the context of "
2877 << PrettyMethod(method_idx_, *dex_file_);
2878 return NULL;
2879 } else if (!field_klass.IsAssignableFrom(obj_type)) {
2880 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
2881 // of C1. For resolution to occur the declared class of the field must be compatible with
2882 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
2883 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
2884 << " from object of type " << obj_type;
2885 return NULL;
2886 } else {
2887 return field;
2888 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002889 }
2890}
2891
Ian Rogers776ac1f2012-04-13 23:36:36 -07002892void MethodVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002893 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002894 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002895 Field* field;
2896 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002897 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002898 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002899 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07002900 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002901 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002902 const char* descriptor;
Ian Rogers365c1022012-06-22 15:05:28 -07002903 ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002904 if (field != NULL) {
2905 descriptor = FieldHelper(field).GetTypeDescriptor();
2906 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07002907 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002908 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2909 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2910 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07002911 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002912 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
2913 if (is_primitive) {
2914 if (field_type.Equals(insn_type) ||
2915 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
2916 (field_type.IsDouble() && insn_type.IsLongTypes())) {
2917 // expected that read is of the correct primitive type or that int reads are reading
2918 // floats or long reads are reading doubles
2919 } else {
2920 // This is a global failure rather than a class change failure as the instructions and
2921 // the descriptors for the type should have been consistent within the same file at
2922 // compile time
2923 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
2924 << " to be of type '" << insn_type
2925 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002926 return;
2927 }
2928 } else {
2929 if (!insn_type.IsAssignableFrom(field_type)) {
2930 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
2931 << " to be compatible with type '" << insn_type
2932 << "' but found type '" << field_type
2933 << "' in get-object";
2934 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Conflict());
2935 return;
2936 }
2937 }
2938 work_line_->SetRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002939}
2940
Ian Rogers776ac1f2012-04-13 23:36:36 -07002941void MethodVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002942 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002943 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002944 Field* field;
2945 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07002946 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002947 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002948 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07002949 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002950 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002951 const char* descriptor;
Ian Rogers365c1022012-06-22 15:05:28 -07002952 ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002953 if (field != NULL) {
2954 descriptor = FieldHelper(field).GetTypeDescriptor();
2955 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07002956 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002957 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2958 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2959 loader = class_loader_;
2960 }
2961 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
2962 if (field != NULL) {
2963 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
2964 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
2965 << " from other class " << GetDeclaringClass();
2966 return;
2967 }
2968 }
2969 if (is_primitive) {
2970 // Primitive field assignability rules are weaker than regular assignability rules
2971 bool instruction_compatible;
2972 bool value_compatible;
2973 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
2974 if (field_type.IsIntegralTypes()) {
2975 instruction_compatible = insn_type.IsIntegralTypes();
2976 value_compatible = value_type.IsIntegralTypes();
2977 } else if (field_type.IsFloat()) {
2978 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
2979 value_compatible = value_type.IsFloatTypes();
2980 } else if (field_type.IsLong()) {
2981 instruction_compatible = insn_type.IsLong();
2982 value_compatible = value_type.IsLongTypes();
2983 } else if (field_type.IsDouble()) {
2984 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
2985 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07002986 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002987 instruction_compatible = false; // reference field with primitive store
2988 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07002989 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002990 if (!instruction_compatible) {
2991 // This is a global failure rather than a class change failure as the instructions and
2992 // the descriptors for the type should have been consistent within the same file at
2993 // compile time
2994 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
2995 << " to be of type '" << insn_type
2996 << "' but found type '" << field_type
2997 << "' in put";
2998 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07002999 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003000 if (!value_compatible) {
3001 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
3002 << " of type " << value_type
3003 << " but expected " << field_type
3004 << " for store to " << PrettyField(field) << " in put";
3005 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003006 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003007 } else {
3008 if (!insn_type.IsAssignableFrom(field_type)) {
3009 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3010 << " to be compatible with type '" << insn_type
3011 << "' but found type '" << field_type
3012 << "' in put-object";
3013 return;
3014 }
3015 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003016 }
3017}
3018
Ian Rogers776ac1f2012-04-13 23:36:36 -07003019bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003020 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003021 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003022 return false;
3023 }
3024 return true;
3025}
3026
Ian Rogers776ac1f2012-04-13 23:36:36 -07003027void MethodVerifier::ReplaceFailingInstruction() {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003028 // Pop the failure and clear the need for rewriting.
3029 size_t failure_number = failures_.size();
3030 CHECK_NE(failure_number, 0U);
3031 DCHECK_EQ(failure_messages_.size(), failure_number);
jeffhaob57e9522012-04-26 18:08:21 -07003032 std::ostringstream* failure_message = failure_messages_[0];
3033 VerifyError failure = failures_[0];
3034 failures_.clear();
3035 failure_messages_.clear();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003036 have_pending_rewrite_failure_ = false;
3037
Ian Rogersf1864ef2011-12-09 12:39:48 -08003038 if (Runtime::Current()->IsStarted()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003039 LOG(ERROR) << "Verification attempting to replace instructions at runtime in "
3040 << PrettyMethod(method_idx_, *dex_file_) << " " << failure_message->str();
Ian Rogersf1864ef2011-12-09 12:39:48 -08003041 return;
3042 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003043 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3044 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3045 VerifyErrorRefType ref_type;
3046 switch (inst->Opcode()) {
3047 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003048 case Instruction::CHECK_CAST:
3049 case Instruction::INSTANCE_OF:
3050 case Instruction::NEW_INSTANCE:
3051 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003052 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003053 case Instruction::FILLED_NEW_ARRAY_RANGE:
3054 ref_type = VERIFY_ERROR_REF_CLASS;
3055 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003056 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003057 case Instruction::IGET_BOOLEAN:
3058 case Instruction::IGET_BYTE:
3059 case Instruction::IGET_CHAR:
3060 case Instruction::IGET_SHORT:
3061 case Instruction::IGET_WIDE:
3062 case Instruction::IGET_OBJECT:
3063 case Instruction::IPUT:
3064 case Instruction::IPUT_BOOLEAN:
3065 case Instruction::IPUT_BYTE:
3066 case Instruction::IPUT_CHAR:
3067 case Instruction::IPUT_SHORT:
3068 case Instruction::IPUT_WIDE:
3069 case Instruction::IPUT_OBJECT:
3070 case Instruction::SGET:
3071 case Instruction::SGET_BOOLEAN:
3072 case Instruction::SGET_BYTE:
3073 case Instruction::SGET_CHAR:
3074 case Instruction::SGET_SHORT:
3075 case Instruction::SGET_WIDE:
3076 case Instruction::SGET_OBJECT:
3077 case Instruction::SPUT:
3078 case Instruction::SPUT_BOOLEAN:
3079 case Instruction::SPUT_BYTE:
3080 case Instruction::SPUT_CHAR:
3081 case Instruction::SPUT_SHORT:
3082 case Instruction::SPUT_WIDE:
3083 case Instruction::SPUT_OBJECT:
3084 ref_type = VERIFY_ERROR_REF_FIELD;
3085 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003086 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003087 case Instruction::INVOKE_VIRTUAL_RANGE:
3088 case Instruction::INVOKE_SUPER:
3089 case Instruction::INVOKE_SUPER_RANGE:
3090 case Instruction::INVOKE_DIRECT:
3091 case Instruction::INVOKE_DIRECT_RANGE:
3092 case Instruction::INVOKE_STATIC:
3093 case Instruction::INVOKE_STATIC_RANGE:
3094 case Instruction::INVOKE_INTERFACE:
3095 case Instruction::INVOKE_INTERFACE_RANGE:
3096 ref_type = VERIFY_ERROR_REF_METHOD;
3097 break;
jeffhaobdb76512011-09-07 11:43:16 -07003098 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003099 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003100 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003101 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003102 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3103 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3104 // instruction, so assert it.
3105 size_t width = inst->SizeInCodeUnits();
3106 CHECK_GT(width, 1u);
Ian Rogersf1864ef2011-12-09 12:39:48 -08003107 // If the instruction is larger than 2 code units, rewrite subsequent code unit sized chunks with
Ian Rogersd81871c2011-10-03 13:57:23 -07003108 // NOPs
3109 for (size_t i = 2; i < width; i++) {
3110 insns[work_insn_idx_ + i] = Instruction::NOP;
3111 }
3112 // Encode the opcode, with the failure code in the high byte
3113 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
Ian Rogersad0b3a32012-04-16 14:50:24 -07003114 (failure << 8) | // AA - component
Ian Rogersd81871c2011-10-03 13:57:23 -07003115 (ref_type << (8 + kVerifyErrorRefTypeShift));
3116 insns[work_insn_idx_] = new_instruction;
3117 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3118 // rewrote, so nothing to do here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003119 LOG(INFO) << "Verification error, replacing instructions in "
3120 << PrettyMethod(method_idx_, *dex_file_) << " "
3121 << failure_message->str();
Ian Rogers9fdfc182011-10-26 23:12:52 -07003122 if (gDebugVerify) {
Elliott Hughesc073b072012-05-24 19:29:17 -07003123 std::cout << "\n" << info_messages_.str();
Ian Rogers9fdfc182011-10-26 23:12:52 -07003124 Dump(std::cout);
3125 }
jeffhaobdb76512011-09-07 11:43:16 -07003126}
jeffhaoba5ebb92011-08-25 17:24:37 -07003127
Ian Rogers776ac1f2012-04-13 23:36:36 -07003128bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003129 bool changed = true;
3130 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3131 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003132 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003133 * We haven't processed this instruction before, and we haven't touched the registers here, so
3134 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3135 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003136 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003137 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003138 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003139 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3140 if (gDebugVerify) {
3141 copy->CopyFromLine(target_line);
3142 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003143 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003144 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003145 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003146 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003147 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003148 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003149 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3150 << *copy.get() << " MERGE\n"
3151 << *merge_line << " ==\n"
3152 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003153 }
3154 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003155 if (changed) {
3156 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003157 }
3158 return true;
3159}
3160
Ian Rogers776ac1f2012-04-13 23:36:36 -07003161InsnFlags* MethodVerifier::CurrentInsnFlags() {
3162 return &insn_flags_[work_insn_idx_];
3163}
3164
Ian Rogersad0b3a32012-04-16 14:50:24 -07003165const RegType& MethodVerifier::GetMethodReturnType() {
3166 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx_);
3167 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3168 uint16_t return_type_idx = proto_id.return_type_idx_;
3169 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
3170 return reg_types_.FromDescriptor(class_loader_, descriptor);
3171}
3172
3173const RegType& MethodVerifier::GetDeclaringClass() {
3174 if (foo_method_ != NULL) {
3175 return reg_types_.FromClass(foo_method_->GetDeclaringClass());
3176 } else {
3177 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx_);
3178 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
3179 return reg_types_.FromDescriptor(class_loader_, descriptor);
3180 }
3181}
3182
Ian Rogers776ac1f2012-04-13 23:36:36 -07003183void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003184 size_t* log2_max_gc_pc) {
3185 size_t local_gc_points = 0;
3186 size_t max_insn = 0;
3187 size_t max_ref_reg = -1;
3188 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3189 if (insn_flags_[i].IsGcPoint()) {
3190 local_gc_points++;
3191 max_insn = i;
3192 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003193 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003194 }
3195 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003196 *gc_points = local_gc_points;
3197 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3198 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003199 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003200 i++;
3201 }
3202 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003203}
3204
Ian Rogers776ac1f2012-04-13 23:36:36 -07003205const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003206 size_t num_entries, ref_bitmap_bits, pc_bits;
3207 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3208 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003209 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003210 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003211 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003212 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003213 return NULL;
3214 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003215 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3216 // There are 2 bytes to encode the number of entries
3217 if (num_entries >= 65536) {
3218 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003219 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003220 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003221 return NULL;
3222 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003223 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003224 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003225 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003226 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003227 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003228 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003229 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003230 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003231 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003232 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003233 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003234 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3235 return NULL;
3236 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003237 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003238 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003239 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003240 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003241 return NULL;
3242 }
3243 // Write table header
Ian Rogers776ac1f2012-04-13 23:36:36 -07003244 table->push_back(format | ((ref_bitmap_bytes >> PcToReferenceMap::kRegMapFormatShift) &
3245 ~PcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003246 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003247 table->push_back(num_entries & 0xFF);
3248 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003249 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003250 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3251 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003252 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003253 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003254 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003255 }
3256 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003257 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003258 }
3259 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003260 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003261 return table;
3262}
jeffhaoa0a764a2011-09-16 10:43:38 -07003263
Ian Rogers776ac1f2012-04-13 23:36:36 -07003264void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003265 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3266 // that the table data is well formed and all references are marked (or not) in the bitmap
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003267 PcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003268 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003269 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003270 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3271 if (insn_flags_[i].IsGcPoint()) {
3272 CHECK_LT(map_index, map.NumEntries());
3273 CHECK_EQ(map.GetPC(map_index), i);
3274 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3275 map_index++;
3276 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003277 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003278 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003279 CHECK_LT(j / 8, map.RegWidth());
3280 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3281 } else if ((j / 8) < map.RegWidth()) {
3282 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3283 } else {
3284 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3285 }
3286 }
3287 } else {
3288 CHECK(reg_bitmap == NULL);
3289 }
3290 }
3291}
jeffhaoa0a764a2011-09-16 10:43:38 -07003292
Ian Rogers776ac1f2012-04-13 23:36:36 -07003293void MethodVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003294 MutexLock mu(*gc_maps_lock_);
Elliott Hughesa0e18062012-04-13 15:59:59 -07003295 GcMapTable::iterator it = gc_maps_->find(ref);
3296 if (it != gc_maps_->end()) {
3297 delete it->second;
3298 gc_maps_->erase(it);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003299 }
Elliott Hughesa0e18062012-04-13 15:59:59 -07003300 gc_maps_->Put(ref, &gc_map);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003301 CHECK(GetGcMap(ref) != NULL);
3302}
3303
Ian Rogers776ac1f2012-04-13 23:36:36 -07003304const std::vector<uint8_t>* MethodVerifier::GetGcMap(Compiler::MethodReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003305 MutexLock mu(*gc_maps_lock_);
3306 GcMapTable::const_iterator it = gc_maps_->find(ref);
3307 if (it == gc_maps_->end()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003308 return NULL;
3309 }
3310 CHECK(it->second != NULL);
3311 return it->second;
3312}
3313
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003314Mutex* MethodVerifier::gc_maps_lock_ = NULL;
3315MethodVerifier::GcMapTable* MethodVerifier::gc_maps_ = NULL;
3316
3317Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
3318MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
3319
3320#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
3321Mutex* MethodVerifier::inferred_reg_category_maps_lock_ = NULL;
3322MethodVerifier::InferredRegCategoryMapTable* MethodVerifier::inferred_reg_category_maps_ = NULL;
3323#endif
3324
3325void MethodVerifier::Init() {
3326 gc_maps_lock_ = new Mutex("verifier GC maps lock");
3327 {
3328 MutexLock mu(*gc_maps_lock_);
3329 gc_maps_ = new MethodVerifier::GcMapTable;
3330 }
3331
3332 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
3333 {
3334 MutexLock mu(*rejected_classes_lock_);
3335 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
3336 }
3337
3338#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
3339 inferred_reg_category_maps_lock_ = new Mutex("verifier GC maps lock");
3340 {
3341 MutexLock mu(*inferred_reg_category_maps_lock_);
3342 inferred_reg_category_maps_ = new MethodVerifier::InferredRegCategoryMapTable;
3343 }
3344#endif
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003345}
3346
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003347void MethodVerifier::Shutdown() {
3348 {
3349 MutexLock mu(*gc_maps_lock_);
3350 STLDeleteValues(gc_maps_);
3351 delete gc_maps_;
3352 gc_maps_ = NULL;
3353 }
3354 delete gc_maps_lock_;
3355 gc_maps_lock_ = NULL;
3356
3357 {
3358 MutexLock mu(*rejected_classes_lock_);
3359 delete rejected_classes_;
3360 rejected_classes_ = NULL;
3361 }
3362 delete rejected_classes_lock_;
3363 rejected_classes_lock_ = NULL;
3364
3365#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
3366 {
3367 MutexLock mu(*inferred_reg_category_maps_lock_);
3368 STLDeleteValues(inferred_reg_category_maps_);
3369 delete inferred_reg_category_maps_;
3370 inferred_reg_category_maps_ = NULL;
3371 }
3372 delete inferred_reg_category_maps_lock_;
3373 inferred_reg_category_maps_lock_ = NULL;
3374#endif
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003375}
jeffhaod1224c72012-02-29 13:43:08 -08003376
Ian Rogers776ac1f2012-04-13 23:36:36 -07003377void MethodVerifier::AddRejectedClass(Compiler::ClassReference ref) {
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003378 MutexLock mu(*rejected_classes_lock_);
3379 rejected_classes_->insert(ref);
jeffhaod1224c72012-02-29 13:43:08 -08003380 CHECK(IsClassRejected(ref));
3381}
3382
Ian Rogers776ac1f2012-04-13 23:36:36 -07003383bool MethodVerifier::IsClassRejected(Compiler::ClassReference ref) {
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003384 MutexLock mu(*rejected_classes_lock_);
3385 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08003386}
3387
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -07003388#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Ian Rogers776ac1f2012-04-13 23:36:36 -07003389const InferredRegCategoryMap* MethodVerifier::GenerateInferredRegCategoryMap() {
Logan Chienfca7e872011-12-20 20:08:22 +08003390 uint32_t insns_size = code_item_->insns_size_in_code_units_;
3391 uint16_t regs_size = code_item_->registers_size_;
3392
3393 UniquePtr<InferredRegCategoryMap> table(
3394 new InferredRegCategoryMap(insns_size, regs_size));
3395
3396 for (size_t i = 0; i < insns_size; ++i) {
3397 if (RegisterLine* line = reg_table_.GetLine(i)) {
TDYa127526643e2012-05-26 01:01:48 -07003398 const Instruction* inst = Instruction::At(code_item_->insns_ + i);
3399
3400 // GC points
3401 if (inst->IsBranch() || inst->IsInvoke()) {
3402 for (size_t r = 0; r < regs_size; ++r) {
3403 const RegType &rt = line->GetRegisterType(r);
3404 if (rt.IsNonZeroReferenceTypes()) {
3405 table->SetRegCanBeObject(r);
3406 }
TDYa127b2eb5c12012-05-24 15:52:10 -07003407 }
3408 }
3409
TDYa127526643e2012-05-26 01:01:48 -07003410 /* We only use InferredRegCategoryMap in one case */
3411 if (inst->IsBranch()) {
TDYa127b2eb5c12012-05-24 15:52:10 -07003412 for (size_t r = 0; r < regs_size; ++r) {
3413 const RegType &rt = line->GetRegisterType(r);
3414
3415 if (rt.IsZero()) {
3416 table->SetRegCategory(i, r, kRegZero);
3417 } else if (rt.IsCategory1Types()) {
3418 table->SetRegCategory(i, r, kRegCat1nr);
3419 } else if (rt.IsCategory2Types()) {
3420 table->SetRegCategory(i, r, kRegCat2);
3421 } else if (rt.IsReferenceTypes()) {
3422 table->SetRegCategory(i, r, kRegObject);
3423 } else {
3424 table->SetRegCategory(i, r, kRegUnknown);
3425 }
Logan Chienfca7e872011-12-20 20:08:22 +08003426 }
3427 }
3428 }
3429 }
3430
3431 return table.release();
3432}
Logan Chiendd361c92012-04-10 23:40:37 +08003433
Ian Rogers776ac1f2012-04-13 23:36:36 -07003434void MethodVerifier::SetInferredRegCategoryMap(Compiler::MethodReference ref,
3435 const InferredRegCategoryMap& inferred_reg_category_map) {
Logan Chiendd361c92012-04-10 23:40:37 +08003436 MutexLock mu(*inferred_reg_category_maps_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07003437 const InferredRegCategoryMap* existing_inferred_reg_category_map = GetInferredRegCategoryMap(ref);
Logan Chiendd361c92012-04-10 23:40:37 +08003438
Logan Chien6d657bf2012-06-27 16:23:27 +08003439 if (existing_inferred_reg_category_map == NULL) {
3440 inferred_reg_category_maps_->Put(ref, &inferred_reg_category_map);
3441 } else {
Logan Chiendd361c92012-04-10 23:40:37 +08003442 CHECK(*existing_inferred_reg_category_map == inferred_reg_category_map);
Logan Chien6d657bf2012-06-27 16:23:27 +08003443 delete &inferred_reg_category_map;
Logan Chiendd361c92012-04-10 23:40:37 +08003444 }
3445
Logan Chiendd361c92012-04-10 23:40:37 +08003446 CHECK(GetInferredRegCategoryMap(ref) != NULL);
3447}
3448
3449const InferredRegCategoryMap*
Ian Rogers776ac1f2012-04-13 23:36:36 -07003450MethodVerifier::GetInferredRegCategoryMap(Compiler::MethodReference ref) {
Logan Chiendd361c92012-04-10 23:40:37 +08003451 MutexLock mu(*inferred_reg_category_maps_lock_);
3452
3453 InferredRegCategoryMapTable::const_iterator it =
3454 inferred_reg_category_maps_->find(ref);
3455
3456 if (it == inferred_reg_category_maps_->end()) {
3457 return NULL;
3458 }
3459 CHECK(it->second != NULL);
3460 return it->second;
3461}
Logan Chienfca7e872011-12-20 20:08:22 +08003462#endif
3463
Ian Rogersd81871c2011-10-03 13:57:23 -07003464} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003465} // namespace art