blob: 78bc2e9339d904c4224a9c83cd59ae95833368b7 [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 }
135 private:
136 enum {
137 kInTry,
138 kBranchTarget,
139 kGcPoint,
140 kVisited,
141 kChanged,
142 };
143
144 // Size of instruction in code units
145 uint16_t length_;
146 uint8_t flags_;
Ian Rogers84fa0742011-10-25 18:13:30 -0700147};
Ian Rogersd81871c2011-10-03 13:57:23 -0700148
Ian Rogersd81871c2011-10-03 13:57:23 -0700149void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
150 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -0700151 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700152 DCHECK_GT(insns_size, 0U);
153
154 for (uint32_t i = 0; i < insns_size; i++) {
155 bool interesting = false;
156 switch (mode) {
157 case kTrackRegsAll:
158 interesting = flags[i].IsOpcode();
159 break;
160 case kTrackRegsGcPoints:
161 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
162 break;
163 case kTrackRegsBranches:
164 interesting = flags[i].IsBranchTarget();
165 break;
166 default:
167 break;
168 }
169 if (interesting) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700170 pc_to_register_line_.Put(i, new RegisterLine(registers_size, verifier));
Ian Rogersd81871c2011-10-03 13:57:23 -0700171 }
172 }
173}
174
jeffhaof1e6b7c2012-06-05 18:33:30 -0700175MethodVerifier::FailureKind MethodVerifier::VerifyClass(const Class* klass, std::string& error) {
jeffhaobdb76512011-09-07 11:43:16 -0700176 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700177 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700178 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700179 Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -0800180 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800181 error = "Verifier rejected class ";
182 error += PrettyDescriptor(klass);
183 error += " that has no super class";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700184 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -0700185 }
Ian Rogers1c5eb702012-02-01 09:18:34 -0800186 if (super != NULL && super->IsFinal()) {
187 error = "Verifier rejected class ";
188 error += PrettyDescriptor(klass);
189 error += " that attempts to sub-class final class ";
190 error += PrettyDescriptor(super);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700191 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -0700192 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700193 ClassHelper kh(klass);
194 const DexFile& dex_file = kh.GetDexFile();
195 uint32_t class_def_idx;
196 if (!dex_file.FindClassDefIndex(kh.GetDescriptor(), class_def_idx)) {
197 error = "Verifier rejected class ";
198 error += PrettyDescriptor(klass);
199 error += " that isn't present in dex file ";
200 error += dex_file.GetLocation();
jeffhaof1e6b7c2012-06-05 18:33:30 -0700201 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700202 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700203 return VerifyClass(&dex_file, kh.GetDexCache(), klass->GetClassLoader(), class_def_idx, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700204}
205
jeffhaof1e6b7c2012-06-05 18:33:30 -0700206MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file, DexCache* dex_cache,
jeffhaof56197c2012-03-05 18:01:54 -0800207 const ClassLoader* class_loader, uint32_t class_def_idx, std::string& error) {
208 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
209 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700210 if (class_data == NULL) {
211 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700212 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700213 }
jeffhaof56197c2012-03-05 18:01:54 -0800214 ClassDataItemIterator it(*dex_file, class_data);
215 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
216 it.Next();
217 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700218 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700219 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700220 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaof56197c2012-03-05 18:01:54 -0800221 while (it.HasNextDirectMethod()) {
222 uint32_t method_idx = it.GetMemberIndex();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700223 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, true);
224 if (method == NULL) {
225 DCHECK(Thread::Current()->IsExceptionPending());
226 // We couldn't resolve the method, but continue regardless.
227 Thread::Current()->ClearException();
228 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700229 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
230 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags());
231 if (result != kNoFailure) {
232 if (result == kHardFailure) {
233 hard_fail = true;
234 if (error_count > 0) {
235 error += "\n";
236 }
237 error = "Verifier rejected class ";
238 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
239 error += " due to bad method ";
240 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700241 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700242 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800243 }
244 it.Next();
245 }
246 while (it.HasNextVirtualMethod()) {
247 uint32_t method_idx = it.GetMemberIndex();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700248 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, false);
249 if (method == NULL) {
250 DCHECK(Thread::Current()->IsExceptionPending());
251 // We couldn't resolve the method, but continue regardless.
252 Thread::Current()->ClearException();
253 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700254 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
255 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags());
256 if (result != kNoFailure) {
257 if (result == kHardFailure) {
258 hard_fail = true;
259 if (error_count > 0) {
260 error += "\n";
261 }
262 error = "Verifier rejected class ";
263 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
264 error += " due to bad method ";
265 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700266 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700267 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800268 }
269 it.Next();
270 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700271 if (error_count == 0) {
272 return kNoFailure;
273 } else {
274 return hard_fail ? kHardFailure : kSoftFailure;
275 }
jeffhaof56197c2012-03-05 18:01:54 -0800276}
277
jeffhaof1e6b7c2012-06-05 18:33:30 -0700278MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx, const DexFile* dex_file,
279 DexCache* dex_cache, const ClassLoader* class_loader, uint32_t class_def_idx,
280 const DexFile::CodeItem* code_item, Method* method, uint32_t method_access_flags) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700281 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item, method_idx,
282 method, method_access_flags);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700283 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700284 // Verification completed, however failures may be pending that didn't cause the verification
285 // to hard fail.
Ian Rogerse551e952012-06-03 22:59:14 -0700286 CHECK(!verifier.have_pending_hard_failure_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700287 if (verifier.failures_.size() != 0) {
288 verifier.DumpFailures(LOG(INFO) << "Soft verification failures in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700289 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof1e6b7c2012-06-05 18:33:30 -0700290 return kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800291 }
292 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700293 // Bad method data.
294 CHECK_NE(verifier.failures_.size(), 0U);
295 CHECK(verifier.have_pending_hard_failure_);
296 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700297 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800298 if (gDebugVerify) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700299 std::cout << "\n" << verifier.info_messages_.str();
jeffhaof56197c2012-03-05 18:01:54 -0800300 verifier.Dump(std::cout);
301 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700302 return kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800303 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700304 return kNoFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800305}
306
Ian Rogersad0b3a32012-04-16 14:50:24 -0700307void MethodVerifier::VerifyMethodAndDump(Method* method) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800308 CHECK(method != NULL);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700309 MethodHelper mh(method);
310 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
311 mh.GetClassDefIndex(), mh.GetCodeItem(), method->GetDexMethodIndex(),
312 method, method->GetAccessFlags());
313 verifier.Verify();
Elliott Hughesc073b072012-05-24 19:29:17 -0700314 verifier.DumpFailures(LOG(INFO) << "Dump of method " << PrettyMethod(method) << "\n")
Ian Rogersad0b3a32012-04-16 14:50:24 -0700315 << verifier.info_messages_.str() << Dumpable<MethodVerifier>(verifier);
jeffhaoba5ebb92011-08-25 17:24:37 -0700316}
317
Ian Rogers776ac1f2012-04-13 23:36:36 -0700318MethodVerifier::MethodVerifier(const DexFile* dex_file, DexCache* dex_cache,
Ian Rogersad0b3a32012-04-16 14:50:24 -0700319 const ClassLoader* class_loader, uint32_t class_def_idx, const DexFile::CodeItem* code_item,
320 uint32_t method_idx, Method* method, uint32_t method_access_flags)
jeffhaof56197c2012-03-05 18:01:54 -0800321 : work_insn_idx_(-1),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700322 method_idx_(method_idx),
323 foo_method_(method),
324 method_access_flags_(method_access_flags),
jeffhaof56197c2012-03-05 18:01:54 -0800325 dex_file_(dex_file),
326 dex_cache_(dex_cache),
327 class_loader_(class_loader),
328 class_def_idx_(class_def_idx),
329 code_item_(code_item),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700330 have_pending_hard_failure_(false),
331 have_pending_rewrite_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800332 new_instance_count_(0),
333 monitor_enter_count_(0) {
334}
335
Ian Rogersad0b3a32012-04-16 14:50:24 -0700336bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700337 // If there aren't any instructions, make sure that's expected, then exit successfully.
338 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700339 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700340 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700341 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700342 } else {
343 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700344 }
jeffhaobdb76512011-09-07 11:43:16 -0700345 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700346 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
347 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700348 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
349 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700350 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700351 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700352 // Allocate and initialize an array to hold instruction data.
353 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
354 // Run through the instructions and see if the width checks out.
355 bool result = ComputeWidthsAndCountOps();
356 // Flag instructions guarded by a "try" block and check exception handlers.
357 result = result && ScanTryCatchBlocks();
358 // Perform static instruction verification.
359 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700360 // Perform code-flow analysis and return.
361 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700362}
363
Ian Rogers776ac1f2012-04-13 23:36:36 -0700364std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700365 switch (error) {
366 case VERIFY_ERROR_NO_CLASS:
367 case VERIFY_ERROR_NO_FIELD:
368 case VERIFY_ERROR_NO_METHOD:
369 case VERIFY_ERROR_ACCESS_CLASS:
370 case VERIFY_ERROR_ACCESS_FIELD:
371 case VERIFY_ERROR_ACCESS_METHOD:
372 if (Runtime::Current()->IsCompiler()) {
373 // If we're optimistically running verification at compile time, turn NO_xxx and ACCESS_xxx
374 // errors into soft verification errors so that we re-verify at runtime. We may fail to find
375 // or to agree on access because of not yet available class loaders, or class loaders that
376 // will differ at runtime.
jeffhaod5347e02012-03-22 17:25:05 -0700377 error = VERIFY_ERROR_BAD_CLASS_SOFT;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700378 } else {
379 have_pending_rewrite_failure_ = true;
380 }
381 break;
382 // Errors that are bad at both compile and runtime, but don't cause rejection of the class.
383 case VERIFY_ERROR_CLASS_CHANGE:
384 case VERIFY_ERROR_INSTANTIATION:
385 have_pending_rewrite_failure_ = true;
386 break;
387 // Indication that verification should be retried at runtime.
388 case VERIFY_ERROR_BAD_CLASS_SOFT:
389 if (!Runtime::Current()->IsCompiler()) {
390 // It is runtime so hard fail.
391 have_pending_hard_failure_ = true;
392 }
393 break;
jeffhaod5347e02012-03-22 17:25:05 -0700394 // Hard verification failures at compile time will still fail at runtime, so the class is
395 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700396 case VERIFY_ERROR_BAD_CLASS_HARD: {
397 if (Runtime::Current()->IsCompiler()) {
jeffhaof56197c2012-03-05 18:01:54 -0800398 Compiler::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800399 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800400 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700401 have_pending_hard_failure_ = true;
402 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800403 }
404 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700405 failures_.push_back(error);
406 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(method_idx_, *dex_file_).c_str(),
407 work_insn_idx_));
408 std::ostringstream* failure_message = new std::ostringstream(location);
409 failure_messages_.push_back(failure_message);
410 return *failure_message;
411}
412
413void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
414 size_t failure_num = failure_messages_.size();
415 DCHECK_NE(failure_num, 0U);
416 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
417 prepend += last_fail_message->str();
418 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
419 delete last_fail_message;
420}
421
422void MethodVerifier::AppendToLastFailMessage(std::string append) {
423 size_t failure_num = failure_messages_.size();
424 DCHECK_NE(failure_num, 0U);
425 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
426 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800427}
428
Ian Rogers776ac1f2012-04-13 23:36:36 -0700429bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700430 const uint16_t* insns = code_item_->insns_;
431 size_t insns_size = code_item_->insns_size_in_code_units_;
432 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700433 size_t new_instance_count = 0;
434 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700435 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700436
Ian Rogersd81871c2011-10-03 13:57:23 -0700437 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700438 Instruction::Code opcode = inst->Opcode();
439 if (opcode == Instruction::NEW_INSTANCE) {
440 new_instance_count++;
441 } else if (opcode == Instruction::MONITOR_ENTER) {
442 monitor_enter_count++;
443 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700444 size_t inst_size = inst->SizeInCodeUnits();
445 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
446 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700447 inst = inst->Next();
448 }
449
Ian Rogersd81871c2011-10-03 13:57:23 -0700450 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700451 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
452 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700453 return false;
454 }
455
Ian Rogersd81871c2011-10-03 13:57:23 -0700456 new_instance_count_ = new_instance_count;
457 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700458 return true;
459}
460
Ian Rogers776ac1f2012-04-13 23:36:36 -0700461bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700462 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700463 if (tries_size == 0) {
464 return true;
465 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700466 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700467 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700468
469 for (uint32_t idx = 0; idx < tries_size; idx++) {
470 const DexFile::TryItem* try_item = &tries[idx];
471 uint32_t start = try_item->start_addr_;
472 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700473 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700474 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
475 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700476 return false;
477 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700478 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700479 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700480 return false;
481 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700482 for (uint32_t dex_pc = start; dex_pc < end;
483 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
484 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700485 }
486 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800487 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700488 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700489 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700490 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700491 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700492 CatchHandlerIterator iterator(handlers_ptr);
493 for (; iterator.HasNext(); iterator.Next()) {
494 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700495 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700496 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700497 return false;
498 }
jeffhao60f83e32012-02-13 17:16:30 -0800499 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
500 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700501 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700502 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800503 return false;
504 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700505 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700506 // Ensure exception types are resolved so that they don't need resolution to be delivered,
507 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700508 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
jeffhaof56197c2012-03-05 18:01:54 -0800509 Class* exception_type = linker->ResolveType(*dex_file_, iterator.GetHandlerTypeIndex(),
510 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700511 if (exception_type == NULL) {
512 DCHECK(Thread::Current()->IsExceptionPending());
513 Thread::Current()->ClearException();
514 }
515 }
jeffhaobdb76512011-09-07 11:43:16 -0700516 }
Ian Rogers0571d352011-11-03 19:51:38 -0700517 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700518 }
jeffhaobdb76512011-09-07 11:43:16 -0700519 return true;
520}
521
Ian Rogers776ac1f2012-04-13 23:36:36 -0700522bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700523 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700524
Ian Rogersd81871c2011-10-03 13:57:23 -0700525 /* Flag the start of the method as a branch target. */
526 insn_flags_[0].SetBranchTarget();
527
528 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700529 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700530 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700531 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700532 return false;
533 }
534 /* Flag instructions that are garbage collection points */
535 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
536 insn_flags_[dex_pc].SetGcPoint();
537 }
538 dex_pc += inst->SizeInCodeUnits();
539 inst = inst->Next();
540 }
541 return true;
542}
543
Ian Rogers776ac1f2012-04-13 23:36:36 -0700544bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800545 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700546 bool result = true;
547 switch (inst->GetVerifyTypeArgumentA()) {
548 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800549 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700550 break;
551 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800552 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700553 break;
554 }
555 switch (inst->GetVerifyTypeArgumentB()) {
556 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800557 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700558 break;
559 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800560 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700561 break;
562 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800563 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700564 break;
565 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800566 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700567 break;
568 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800569 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700570 break;
571 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800572 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700573 break;
574 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800575 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700576 break;
577 }
578 switch (inst->GetVerifyTypeArgumentC()) {
579 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800580 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700581 break;
582 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800583 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700584 break;
585 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800586 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700587 break;
588 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800589 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700590 break;
591 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800592 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700593 break;
594 }
595 switch (inst->GetVerifyExtraFlags()) {
596 case Instruction::kVerifyArrayData:
597 result = result && CheckArrayData(code_offset);
598 break;
599 case Instruction::kVerifyBranchTarget:
600 result = result && CheckBranchTarget(code_offset);
601 break;
602 case Instruction::kVerifySwitchTargets:
603 result = result && CheckSwitchTargets(code_offset);
604 break;
605 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800606 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700607 break;
608 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800609 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700610 break;
611 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700612 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700613 result = false;
614 break;
615 }
616 return result;
617}
618
Ian Rogers776ac1f2012-04-13 23:36:36 -0700619bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700620 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700621 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
622 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700623 return false;
624 }
625 return true;
626}
627
Ian Rogers776ac1f2012-04-13 23:36:36 -0700628bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700629 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700630 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
631 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700632 return false;
633 }
634 return true;
635}
636
Ian Rogers776ac1f2012-04-13 23:36:36 -0700637bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700638 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700639 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
640 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700641 return false;
642 }
643 return true;
644}
645
Ian Rogers776ac1f2012-04-13 23:36:36 -0700646bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700647 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700648 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
649 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700650 return false;
651 }
652 return true;
653}
654
Ian Rogers776ac1f2012-04-13 23:36:36 -0700655bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700656 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700657 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
658 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700659 return false;
660 }
661 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700662 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700663 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700664 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700665 return false;
666 }
667 return true;
668}
669
Ian Rogers776ac1f2012-04-13 23:36:36 -0700670bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700671 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700672 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
673 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 return false;
675 }
676 return true;
677}
678
Ian Rogers776ac1f2012-04-13 23:36:36 -0700679bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700680 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700681 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
682 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 return false;
684 }
685 return true;
686}
687
Ian Rogers776ac1f2012-04-13 23:36:36 -0700688bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700689 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700690 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
691 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700692 return false;
693 }
694 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700695 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700696 const char* cp = descriptor;
697 while (*cp++ == '[') {
698 bracket_count++;
699 }
700 if (bracket_count == 0) {
701 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700702 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700703 return false;
704 } else if (bracket_count > 255) {
705 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700706 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700707 return false;
708 }
709 return true;
710}
711
Ian Rogers776ac1f2012-04-13 23:36:36 -0700712bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700713 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
714 const uint16_t* insns = code_item_->insns_ + cur_offset;
715 const uint16_t* array_data;
716 int32_t array_data_offset;
717
718 DCHECK_LT(cur_offset, insn_count);
719 /* make sure the start of the array data table is in range */
720 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
721 if ((int32_t) cur_offset + array_data_offset < 0 ||
722 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700723 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
724 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700725 return false;
726 }
727 /* offset to array data table is a relative branch-style offset */
728 array_data = insns + array_data_offset;
729 /* make sure the table is 32-bit aligned */
730 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700731 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
732 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700733 return false;
734 }
735 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700736 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700737 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
738 /* make sure the end of the switch is in range */
739 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700740 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
741 << ", data offset " << array_data_offset << ", end "
742 << cur_offset + array_data_offset + table_size
743 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700744 return false;
745 }
746 return true;
747}
748
Ian Rogers776ac1f2012-04-13 23:36:36 -0700749bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700750 int32_t offset;
751 bool isConditional, selfOkay;
752 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
753 return false;
754 }
755 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700756 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 -0700757 return false;
758 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700759 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
760 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700761 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700762 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700763 return false;
764 }
765 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
766 int32_t abs_offset = cur_offset + offset;
767 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700768 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700769 << reinterpret_cast<void*>(abs_offset) << ") at "
770 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700771 return false;
772 }
773 insn_flags_[abs_offset].SetBranchTarget();
774 return true;
775}
776
Ian Rogers776ac1f2012-04-13 23:36:36 -0700777bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700778 bool* selfOkay) {
779 const uint16_t* insns = code_item_->insns_ + cur_offset;
780 *pConditional = false;
781 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700782 switch (*insns & 0xff) {
783 case Instruction::GOTO:
784 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700785 break;
786 case Instruction::GOTO_32:
787 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700788 *selfOkay = true;
789 break;
790 case Instruction::GOTO_16:
791 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700792 break;
793 case Instruction::IF_EQ:
794 case Instruction::IF_NE:
795 case Instruction::IF_LT:
796 case Instruction::IF_GE:
797 case Instruction::IF_GT:
798 case Instruction::IF_LE:
799 case Instruction::IF_EQZ:
800 case Instruction::IF_NEZ:
801 case Instruction::IF_LTZ:
802 case Instruction::IF_GEZ:
803 case Instruction::IF_GTZ:
804 case Instruction::IF_LEZ:
805 *pOffset = (int16_t) insns[1];
806 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700807 break;
808 default:
809 return false;
810 break;
811 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700812 return true;
813}
814
Ian Rogers776ac1f2012-04-13 23:36:36 -0700815bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700816 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700817 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700818 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700819 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700820 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
821 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700822 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
823 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700824 return false;
825 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700826 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700827 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700828 /* make sure the table is 32-bit aligned */
829 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700830 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
831 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700832 return false;
833 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700834 uint32_t switch_count = switch_insns[1];
835 int32_t keys_offset, targets_offset;
836 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700837 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
838 /* 0=sig, 1=count, 2/3=firstKey */
839 targets_offset = 4;
840 keys_offset = -1;
841 expected_signature = Instruction::kPackedSwitchSignature;
842 } else {
843 /* 0=sig, 1=count, 2..count*2 = keys */
844 keys_offset = 2;
845 targets_offset = 2 + 2 * switch_count;
846 expected_signature = Instruction::kSparseSwitchSignature;
847 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700848 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700849 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700850 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
851 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700852 return false;
853 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700854 /* make sure the end of the switch is in range */
855 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700856 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
857 << switch_offset << ", end "
858 << (cur_offset + switch_offset + table_size)
859 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700860 return false;
861 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700862 /* for a sparse switch, verify the keys are in ascending order */
863 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700864 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
865 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700866 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
867 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
868 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700869 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
870 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700871 return false;
872 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700873 last_key = key;
874 }
875 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700876 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700877 for (uint32_t targ = 0; targ < switch_count; targ++) {
878 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
879 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
880 int32_t abs_offset = cur_offset + offset;
881 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700882 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700883 << reinterpret_cast<void*>(abs_offset) << ") at "
884 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700885 return false;
886 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700887 insn_flags_[abs_offset].SetBranchTarget();
888 }
889 return true;
890}
891
Ian Rogers776ac1f2012-04-13 23:36:36 -0700892bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700893 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700894 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700895 return false;
896 }
897 uint16_t registers_size = code_item_->registers_size_;
898 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800899 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700900 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
901 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700902 return false;
903 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700904 }
905
906 return true;
907}
908
Ian Rogers776ac1f2012-04-13 23:36:36 -0700909bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700910 uint16_t registers_size = code_item_->registers_size_;
911 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
912 // integer overflow when adding them here.
913 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700914 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
915 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700916 return false;
917 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700918 return true;
919}
920
Brian Carlstrom75412882012-01-18 01:26:54 -0800921const std::vector<uint8_t>* CreateLengthPrefixedGcMap(const std::vector<uint8_t>& gc_map) {
922 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
923 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
924 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
925 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
926 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
927 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
928 gc_map.begin(),
929 gc_map.end());
930 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
931 DCHECK_EQ(gc_map.size(),
932 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
933 (length_prefixed_gc_map->at(1) << 16) |
934 (length_prefixed_gc_map->at(2) << 8) |
935 (length_prefixed_gc_map->at(3) << 0)));
936 return length_prefixed_gc_map;
937}
938
Ian Rogers776ac1f2012-04-13 23:36:36 -0700939bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700940 uint16_t registers_size = code_item_->registers_size_;
941 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -0700942
Ian Rogersd81871c2011-10-03 13:57:23 -0700943 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -0800944 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
945 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700946 }
947 /* Create and initialize table holding register status */
Elliott Hughes460384f2012-04-04 16:53:10 -0700948 reg_table_.Init(kTrackRegsGcPoints, insn_flags_.get(), insns_size, registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -0700949
Ian Rogersd81871c2011-10-03 13:57:23 -0700950 work_line_.reset(new RegisterLine(registers_size, this));
951 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -0700952
Ian Rogersd81871c2011-10-03 13:57:23 -0700953 /* Initialize register types of method arguments. */
954 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700955 DCHECK_NE(failures_.size(), 0U);
956 std::string prepend("Bad signature in ");
957 prepend += PrettyMethod(method_idx_, *dex_file_);
958 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -0700959 return false;
960 }
961 /* Perform code flow verification. */
962 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700963 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700964 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700965 }
966
TDYa127b2eb5c12012-05-24 15:52:10 -0700967 Compiler::MethodReference ref(dex_file_, method_idx_);
968
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700969#if !defined(ART_USE_LLVM_COMPILER) && !defined(ART_USE_GREENLAND_COMPILER)
TDYa127b2eb5c12012-05-24 15:52:10 -0700970
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -0800972 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
973 if (map.get() == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700974 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700975 return false; // Not a real failure, but a failure to encode
976 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700977#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800978 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -0700979#endif
Brian Carlstrom75412882012-01-18 01:26:54 -0800980 const std::vector<uint8_t>* gc_map = CreateLengthPrefixedGcMap(*(map.get()));
Ian Rogers776ac1f2012-04-13 23:36:36 -0700981 verifier::MethodVerifier::SetGcMap(ref, *gc_map);
Logan Chienfca7e872011-12-20 20:08:22 +0800982
Ian Rogersad0b3a32012-04-16 14:50:24 -0700983 if (foo_method_ != NULL) {
984 foo_method_->SetGcMap(&gc_map->at(0));
985 }
Logan Chiendd361c92012-04-10 23:40:37 +0800986
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700987#else // defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Logan Chienfca7e872011-12-20 20:08:22 +0800988 /* Generate Inferred Register Category for LLVM-based Code Generator */
989 const InferredRegCategoryMap* table = GenerateInferredRegCategoryMap();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700990 verifier::MethodVerifier::SetInferredRegCategoryMap(ref, *table);
TDYa127b2eb5c12012-05-24 15:52:10 -0700991
Logan Chienfca7e872011-12-20 20:08:22 +0800992#endif
993
jeffhaobdb76512011-09-07 11:43:16 -0700994 return true;
995}
996
Ian Rogersad0b3a32012-04-16 14:50:24 -0700997std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
998 DCHECK_EQ(failures_.size(), failure_messages_.size());
999 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001000 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001001 }
1002 return os;
1003}
1004
1005extern "C" void MethodVerifierGdbDump(MethodVerifier* v) {
1006 v->Dump(std::cerr);
1007}
1008
Ian Rogers776ac1f2012-04-13 23:36:36 -07001009void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001010 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001011 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001012 return;
jeffhaobdb76512011-09-07 11:43:16 -07001013 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001014 DCHECK(code_item_ != NULL);
1015 const Instruction* inst = Instruction::At(code_item_->insns_);
1016 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1017 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Elliott Hughesaa6e1cd2012-01-18 19:26:06 -08001018 os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
Elliott Hughesc073b072012-05-24 19:29:17 -07001019 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001020 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1021 if (reg_line != NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001022 os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001023 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001024 inst = inst->Next();
1025 }
jeffhaobdb76512011-09-07 11:43:16 -07001026}
1027
Ian Rogersd81871c2011-10-03 13:57:23 -07001028static bool IsPrimitiveDescriptor(char descriptor) {
1029 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001030 case 'I':
1031 case 'C':
1032 case 'S':
1033 case 'B':
1034 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001035 case 'F':
1036 case 'D':
1037 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001038 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001039 default:
1040 return false;
1041 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001042}
1043
Ian Rogers776ac1f2012-04-13 23:36:36 -07001044bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001045 RegisterLine* reg_line = reg_table_.GetLine(0);
1046 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1047 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001048
Ian Rogersd81871c2011-10-03 13:57:23 -07001049 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1050 //Include the "this" pointer.
1051 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001052 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001053 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1054 // argument as uninitialized. This restricts field access until the superclass constructor is
1055 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001056 const RegType& declaring_class = GetDeclaringClass();
1057 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001058 reg_line->SetRegisterType(arg_start + cur_arg,
1059 reg_types_.UninitializedThisArgument(declaring_class));
1060 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001061 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001062 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001063 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001064 }
1065
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001066 const DexFile::ProtoId& proto_id =
Ian Rogersad0b3a32012-04-16 14:50:24 -07001067 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001068 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001069
1070 for (; iterator.HasNext(); iterator.Next()) {
1071 const char* descriptor = iterator.GetDescriptor();
1072 if (descriptor == NULL) {
1073 LOG(FATAL) << "Null descriptor";
1074 }
1075 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001076 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1077 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001078 return false;
1079 }
1080 switch (descriptor[0]) {
1081 case 'L':
1082 case '[':
1083 // We assume that reference arguments are initialized. The only way it could be otherwise
1084 // (assuming the caller was verified) is if the current method is <init>, but in that case
1085 // it's effectively considered initialized the instant we reach here (in the sense that we
1086 // can return without doing anything or call virtual methods).
1087 {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001088 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001089 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001090 }
1091 break;
1092 case 'Z':
1093 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1094 break;
1095 case 'C':
1096 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1097 break;
1098 case 'B':
1099 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1100 break;
1101 case 'I':
1102 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1103 break;
1104 case 'S':
1105 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1106 break;
1107 case 'F':
1108 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1109 break;
1110 case 'J':
1111 case 'D': {
1112 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1113 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1114 cur_arg++;
1115 break;
1116 }
1117 default:
jeffhaod5347e02012-03-22 17:25:05 -07001118 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001119 return false;
1120 }
1121 cur_arg++;
1122 }
1123 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001124 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001125 return false;
1126 }
1127 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1128 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1129 // format. Only major difference from the method argument format is that 'V' is supported.
1130 bool result;
1131 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1132 result = descriptor[1] == '\0';
1133 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1134 size_t i = 0;
1135 do {
1136 i++;
1137 } while (descriptor[i] == '['); // process leading [
1138 if (descriptor[i] == 'L') { // object array
1139 do {
1140 i++; // find closing ;
1141 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1142 result = descriptor[i] == ';';
1143 } else { // primitive array
1144 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1145 }
1146 } else if (descriptor[0] == 'L') {
1147 // could be more thorough here, but shouldn't be required
1148 size_t i = 0;
1149 do {
1150 i++;
1151 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1152 result = descriptor[i] == ';';
1153 } else {
1154 result = false;
1155 }
1156 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001157 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1158 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001159 }
1160 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001161}
1162
Ian Rogers776ac1f2012-04-13 23:36:36 -07001163bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001164 const uint16_t* insns = code_item_->insns_;
1165 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001166
jeffhaobdb76512011-09-07 11:43:16 -07001167 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001168 insn_flags_[0].SetChanged();
1169 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001170
jeffhaobdb76512011-09-07 11:43:16 -07001171 /* Continue until no instructions are marked "changed". */
1172 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001173 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1174 uint32_t insn_idx = start_guess;
1175 for (; insn_idx < insns_size; insn_idx++) {
1176 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001177 break;
1178 }
jeffhaobdb76512011-09-07 11:43:16 -07001179 if (insn_idx == insns_size) {
1180 if (start_guess != 0) {
1181 /* try again, starting from the top */
1182 start_guess = 0;
1183 continue;
1184 } else {
1185 /* all flags are clear */
1186 break;
1187 }
1188 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001189 // We carry the working set of registers from instruction to instruction. If this address can
1190 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1191 // "changed" flags, we need to load the set of registers from the table.
1192 // Because we always prefer to continue on to the next instruction, we should never have a
1193 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1194 // target.
1195 work_insn_idx_ = insn_idx;
1196 if (insn_flags_[insn_idx].IsBranchTarget()) {
1197 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001198 } else {
1199#ifndef NDEBUG
1200 /*
1201 * Sanity check: retrieve the stored register line (assuming
1202 * a full table) and make sure it actually matches.
1203 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001204 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1205 if (register_line != NULL) {
1206 if (work_line_->CompareLine(register_line) != 0) {
1207 Dump(std::cout);
1208 std::cout << info_messages_.str();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001209 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001210 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1211 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001212 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001213 }
jeffhaobdb76512011-09-07 11:43:16 -07001214 }
1215#endif
1216 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001217 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001218 std::string prepend(PrettyMethod(method_idx_, *dex_file_));
1219 prepend += " failed to verify: ";
1220 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001221 return false;
1222 }
jeffhaobdb76512011-09-07 11:43:16 -07001223 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001224 insn_flags_[insn_idx].SetVisited();
1225 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001226 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001227
Ian Rogersad0b3a32012-04-16 14:50:24 -07001228 if (DEAD_CODE_SCAN && ((method_access_flags_ & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001229 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001230 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001231 * (besides the wasted space), but it indicates a flaw somewhere
1232 * down the line, possibly in the verifier.
1233 *
1234 * If we've substituted "always throw" instructions into the stream,
1235 * we are almost certainly going to have some dead code.
1236 */
1237 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001238 uint32_t insn_idx = 0;
1239 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001240 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001241 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001242 * may or may not be preceded by a padding NOP (for alignment).
1243 */
1244 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1245 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1246 insns[insn_idx] == Instruction::kArrayDataSignature ||
1247 (insns[insn_idx] == Instruction::NOP &&
1248 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1249 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1250 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001251 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001252 }
1253
Ian Rogersd81871c2011-10-03 13:57:23 -07001254 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001255 if (dead_start < 0)
1256 dead_start = insn_idx;
1257 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001258 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001259 dead_start = -1;
1260 }
1261 }
1262 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001263 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001264 }
1265 }
jeffhaobdb76512011-09-07 11:43:16 -07001266 return true;
1267}
1268
Ian Rogers776ac1f2012-04-13 23:36:36 -07001269bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001270#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001271 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001272 gDvm.verifierStats.instrsReexamined++;
1273 } else {
1274 gDvm.verifierStats.instrsExamined++;
1275 }
1276#endif
1277
1278 /*
1279 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001280 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001281 * control to another statement:
1282 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001283 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001284 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001285 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001286 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001287 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001288 * throw an exception that is handled by an encompassing "try"
1289 * block.
1290 *
1291 * We can also return, in which case there is no successor instruction
1292 * from this point.
1293 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001294 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001295 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001296 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1297 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001298 DecodedInstruction dec_insn(inst);
1299 int opcode_flags = Instruction::Flags(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001300
jeffhaobdb76512011-09-07 11:43:16 -07001301 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001302 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001303 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001304 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001305 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1306 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001307 }
jeffhaobdb76512011-09-07 11:43:16 -07001308
1309 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001310 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001311 * can throw an exception, we will copy/merge this into the "catch"
1312 * address rather than work_line, because we don't want the result
1313 * from the "successful" code path (e.g. a check-cast that "improves"
1314 * a type) to be visible to the exception handler.
1315 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001316 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001317 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001318 } else {
1319#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001320 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001321#endif
1322 }
1323
Elliott Hughesadb8c672012-03-06 16:49:32 -08001324 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07001325 case Instruction::NOP:
1326 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001327 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001328 * a signature that looks like a NOP; if we see one of these in
1329 * the course of executing code then we have a problem.
1330 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001331 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001332 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001333 }
1334 break;
1335
1336 case Instruction::MOVE:
1337 case Instruction::MOVE_FROM16:
1338 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001339 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001340 break;
1341 case Instruction::MOVE_WIDE:
1342 case Instruction::MOVE_WIDE_FROM16:
1343 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001344 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07001345 break;
1346 case Instruction::MOVE_OBJECT:
1347 case Instruction::MOVE_OBJECT_FROM16:
1348 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001349 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001350 break;
1351
1352 /*
1353 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001354 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001355 * might want to hold the result in an actual CPU register, so the
1356 * Dalvik spec requires that these only appear immediately after an
1357 * invoke or filled-new-array.
1358 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001359 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001360 * redundant with the reset done below, but it can make the debug info
1361 * easier to read in some cases.)
1362 */
1363 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001364 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07001365 break;
1366 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001367 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001368 break;
1369 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001370 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07001371 break;
1372
Ian Rogersd81871c2011-10-03 13:57:23 -07001373 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001374 /*
jeffhao60f83e32012-02-13 17:16:30 -08001375 * This statement can only appear as the first instruction in an exception handler. We verify
1376 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001377 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001378 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08001379 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001380 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001381 }
jeffhaobdb76512011-09-07 11:43:16 -07001382 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001383 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1384 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001385 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001386 }
jeffhaobdb76512011-09-07 11:43:16 -07001387 }
1388 break;
1389 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001390 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001391 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001392 const RegType& return_type = GetMethodReturnType();
1393 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001394 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001395 } else {
1396 // Compilers may generate synthetic functions that write byte values into boolean fields.
1397 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001398 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001399 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1400 ((return_type.IsBoolean() || return_type.IsByte() ||
1401 return_type.IsShort() || return_type.IsChar()) &&
1402 src_type.IsInteger()));
1403 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001404 bool success =
1405 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
1406 if (!success) {
1407 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001408 }
jeffhaobdb76512011-09-07 11:43:16 -07001409 }
1410 }
1411 break;
1412 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001413 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001414 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001415 const RegType& return_type = GetMethodReturnType();
1416 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001417 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001418 } else {
1419 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001420 bool success = work_line_->VerifyRegisterType(dec_insn.vA, return_type);
1421 if (!success) {
1422 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001423 }
jeffhaobdb76512011-09-07 11:43:16 -07001424 }
1425 }
1426 break;
1427 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001428 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001429 const RegType& return_type = GetMethodReturnType();
1430 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001431 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001432 } else {
1433 /* return_type is the *expected* return type, not register value */
1434 DCHECK(!return_type.IsZero());
1435 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08001436 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07001437 // Disallow returning uninitialized values and verify that the reference in vAA is an
1438 // instance of the "return_type"
1439 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001440 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001441 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001442 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
Ian Rogers9074b992011-10-26 17:41:55 -07001443 << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001444 }
1445 }
1446 }
1447 break;
1448
1449 case Instruction::CONST_4:
1450 case Instruction::CONST_16:
1451 case Instruction::CONST:
1452 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001453 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const((int32_t) dec_insn.vB));
jeffhaobdb76512011-09-07 11:43:16 -07001454 break;
1455 case Instruction::CONST_HIGH16:
1456 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001457 work_line_->SetRegisterType(dec_insn.vA,
1458 reg_types_.FromCat1Const((int32_t) dec_insn.vB << 16));
jeffhaobdb76512011-09-07 11:43:16 -07001459 break;
1460 case Instruction::CONST_WIDE_16:
1461 case Instruction::CONST_WIDE_32:
1462 case Instruction::CONST_WIDE:
1463 case Instruction::CONST_WIDE_HIGH16:
1464 /* could be long or double; resolved upon use */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001465 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07001466 break;
1467 case Instruction::CONST_STRING:
1468 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001469 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001470 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001471 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001472 // Get type from instruction if unresolved then we need an access check
1473 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08001474 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001475 // Register holds class, ie its type is class, on error it will hold Conflict.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001476 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogersad0b3a32012-04-16 14:50:24 -07001477 res_type.IsConflict() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001478 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001479 }
jeffhaobdb76512011-09-07 11:43:16 -07001480 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001481 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001482 break;
1483 case Instruction::MONITOR_EXIT:
1484 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001485 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001486 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001487 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001488 * to the need to handle asynchronous exceptions, a now-deprecated
1489 * feature that Dalvik doesn't support.)
1490 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001491 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001492 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001493 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001494 * structured locking checks are working, the former would have
1495 * failed on the -enter instruction, and the latter is impossible.
1496 *
1497 * This is fortunate, because issue 3221411 prevents us from
1498 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001499 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001500 * some catch blocks (which will show up as "dead" code when
1501 * we skip them here); if we can't, then the code path could be
1502 * "live" so we still need to check it.
1503 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001504 opcode_flags &= ~Instruction::kThrow;
1505 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001506 break;
1507
Ian Rogers28ad40d2011-10-27 15:19:26 -07001508 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001509 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001510 /*
1511 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1512 * could be a "upcast" -- not expected, so we don't try to address it.)
1513 *
1514 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001515 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001516 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001517 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001518 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001519 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001520 if (res_type.IsConflict()) {
1521 DCHECK_NE(failures_.size(), 0U);
1522 if (!is_checkcast) {
1523 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
1524 }
1525 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001526 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001527 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1528 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001529 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001530 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001531 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001532 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001533 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07001534 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001535 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001536 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001537 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001538 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001539 }
jeffhaobdb76512011-09-07 11:43:16 -07001540 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001541 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001542 }
1543 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001544 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001545 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001546 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001547 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001548 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001549 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001550 }
1551 }
1552 break;
1553 }
1554 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001555 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001556 if (res_type.IsConflict()) {
1557 DCHECK_NE(failures_.size(), 0U);
1558 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001559 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001560 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1561 // can't create an instance of an interface or abstract class */
1562 if (!res_type.IsInstantiableTypes()) {
1563 Fail(VERIFY_ERROR_INSTANTIATION)
1564 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001565 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001566 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1567 // Any registers holding previous allocations from this address that have not yet been
1568 // initialized must be marked invalid.
1569 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1570 // add the new uninitialized reference to the register state
Elliott Hughesadb8c672012-03-06 16:49:32 -08001571 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001572 }
1573 break;
1574 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001575 case Instruction::NEW_ARRAY:
1576 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001577 break;
1578 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08001579 VerifyNewArray(dec_insn, true, false);
1580 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001581 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001582 case Instruction::FILLED_NEW_ARRAY_RANGE:
1583 VerifyNewArray(dec_insn, true, true);
1584 just_set_result = true; // Filled new array range sets result register
1585 break;
jeffhaobdb76512011-09-07 11:43:16 -07001586 case Instruction::CMPL_FLOAT:
1587 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001588 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001589 break;
1590 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001591 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001592 break;
1593 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001594 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001595 break;
1596 case Instruction::CMPL_DOUBLE:
1597 case Instruction::CMPG_DOUBLE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001598 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08001599 break;
1600 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001601 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08001602 break;
1603 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001604 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001605 break;
1606 case Instruction::CMP_LONG:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001607 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08001608 break;
1609 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001610 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08001611 break;
1612 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001613 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001614 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001615 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001616 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001617 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001618 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001619 }
1620 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001621 }
jeffhaobdb76512011-09-07 11:43:16 -07001622 case Instruction::GOTO:
1623 case Instruction::GOTO_16:
1624 case Instruction::GOTO_32:
1625 /* no effect on or use of registers */
1626 break;
1627
1628 case Instruction::PACKED_SWITCH:
1629 case Instruction::SPARSE_SWITCH:
1630 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001631 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001632 break;
1633
Ian Rogersd81871c2011-10-03 13:57:23 -07001634 case Instruction::FILL_ARRAY_DATA: {
1635 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001636 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08001637 /* array_type can be null if the reg type is Zero */
1638 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001639 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001640 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001641 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001642 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1643 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001644 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001645 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1646 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001647 } else {
jeffhao457cc512012-02-02 16:55:13 -08001648 // Now verify if the element width in the table matches the element width declared in
1649 // the array
1650 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1651 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001652 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001653 } else {
1654 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1655 // Since we don't compress the data in Dex, expect to see equal width of data stored
1656 // in the table and expected from the array class.
1657 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001658 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1659 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001660 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001661 }
1662 }
jeffhaobdb76512011-09-07 11:43:16 -07001663 }
1664 }
1665 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001666 }
jeffhaobdb76512011-09-07 11:43:16 -07001667 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001668 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001669 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1670 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001671 bool mismatch = false;
1672 if (reg_type1.IsZero()) { // zero then integral or reference expected
1673 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1674 } else if (reg_type1.IsReferenceTypes()) { // both references?
1675 mismatch = !reg_type2.IsReferenceTypes();
1676 } else { // both integral?
1677 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1678 }
1679 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001680 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1681 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001682 }
1683 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001684 }
jeffhaobdb76512011-09-07 11:43:16 -07001685 case Instruction::IF_LT:
1686 case Instruction::IF_GE:
1687 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001688 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001689 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1690 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001691 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001692 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1693 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001694 }
1695 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001696 }
jeffhaobdb76512011-09-07 11:43:16 -07001697 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001698 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001699 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001700 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001701 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001702 }
jeffhaobdb76512011-09-07 11:43:16 -07001703 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001704 }
jeffhaobdb76512011-09-07 11:43:16 -07001705 case Instruction::IF_LTZ:
1706 case Instruction::IF_GEZ:
1707 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001708 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001709 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001710 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001711 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1712 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001713 }
jeffhaobdb76512011-09-07 11:43:16 -07001714 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001715 }
jeffhaobdb76512011-09-07 11:43:16 -07001716 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001717 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
1718 break;
jeffhaobdb76512011-09-07 11:43:16 -07001719 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001720 VerifyAGet(dec_insn, reg_types_.Byte(), true);
1721 break;
jeffhaobdb76512011-09-07 11:43:16 -07001722 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07001723 VerifyAGet(dec_insn, reg_types_.Char(), true);
1724 break;
jeffhaobdb76512011-09-07 11:43:16 -07001725 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001726 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001727 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001728 case Instruction::AGET:
1729 VerifyAGet(dec_insn, reg_types_.Integer(), true);
1730 break;
jeffhaobdb76512011-09-07 11:43:16 -07001731 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001732 VerifyAGet(dec_insn, reg_types_.Long(), true);
1733 break;
1734 case Instruction::AGET_OBJECT:
1735 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001736 break;
1737
Ian Rogersd81871c2011-10-03 13:57:23 -07001738 case Instruction::APUT_BOOLEAN:
1739 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
1740 break;
1741 case Instruction::APUT_BYTE:
1742 VerifyAPut(dec_insn, reg_types_.Byte(), true);
1743 break;
1744 case Instruction::APUT_CHAR:
1745 VerifyAPut(dec_insn, reg_types_.Char(), true);
1746 break;
1747 case Instruction::APUT_SHORT:
1748 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001749 break;
1750 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001751 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001752 break;
1753 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001754 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001755 break;
1756 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001757 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001758 break;
1759
jeffhaobdb76512011-09-07 11:43:16 -07001760 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001761 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001762 break;
jeffhaobdb76512011-09-07 11:43:16 -07001763 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001764 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001765 break;
jeffhaobdb76512011-09-07 11:43:16 -07001766 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001767 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001768 break;
jeffhaobdb76512011-09-07 11:43:16 -07001769 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001770 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001771 break;
1772 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001773 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001774 break;
1775 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001776 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001777 break;
1778 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001779 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001780 break;
jeffhaobdb76512011-09-07 11:43:16 -07001781
Ian Rogersd81871c2011-10-03 13:57:23 -07001782 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001783 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001784 break;
1785 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001786 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 break;
1788 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001789 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001790 break;
1791 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001792 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001793 break;
1794 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001795 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001796 break;
1797 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001798 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001799 break;
jeffhaobdb76512011-09-07 11:43:16 -07001800 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001801 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001802 break;
1803
jeffhaobdb76512011-09-07 11:43:16 -07001804 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001805 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001806 break;
jeffhaobdb76512011-09-07 11:43:16 -07001807 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001808 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001809 break;
jeffhaobdb76512011-09-07 11:43:16 -07001810 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001811 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001812 break;
jeffhaobdb76512011-09-07 11:43:16 -07001813 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001814 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001815 break;
1816 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001817 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001818 break;
1819 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001820 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001821 break;
1822 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001823 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001824 break;
1825
1826 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001827 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001828 break;
1829 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001830 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001831 break;
1832 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001833 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001834 break;
1835 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001836 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001837 break;
1838 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001839 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001840 break;
1841 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001842 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001843 break;
1844 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001845 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001846 break;
1847
1848 case Instruction::INVOKE_VIRTUAL:
1849 case Instruction::INVOKE_VIRTUAL_RANGE:
1850 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001851 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001852 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
1853 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
1854 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
1855 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001856 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001857 const char* descriptor;
1858 if (called_method == NULL) {
1859 uint32_t method_idx = dec_insn.vB;
1860 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1861 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1862 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1863 } else {
1864 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07001865 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001866 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
1867 work_line_->SetResultRegisterType(return_type);
1868 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001869 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001870 }
jeffhaobdb76512011-09-07 11:43:16 -07001871 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001872 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001873 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001874 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07001875 const char* return_type_descriptor;
1876 bool is_constructor;
1877 if (called_method == NULL) {
1878 uint32_t method_idx = dec_insn.vB;
1879 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1880 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
1881 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1882 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1883 } else {
1884 is_constructor = called_method->IsConstructor();
1885 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
1886 }
1887 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07001888 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001889 * Some additional checks when calling a constructor. We know from the invocation arg check
1890 * that the "this" argument is an instance of called_method->klass. Now we further restrict
1891 * that to require that called_method->klass is the same as this->klass or this->super,
1892 * allowing the latter only if the "this" argument is the same as the "this" argument to
1893 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07001894 */
jeffhaob57e9522012-04-26 18:08:21 -07001895 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1896 if (this_type.IsConflict()) // failure.
1897 break;
jeffhaobdb76512011-09-07 11:43:16 -07001898
jeffhaob57e9522012-04-26 18:08:21 -07001899 /* no null refs allowed (?) */
1900 if (this_type.IsZero()) {
1901 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
1902 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07001903 }
jeffhaob57e9522012-04-26 18:08:21 -07001904
1905 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07001906 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
1907 // TODO: re-enable constructor type verification
1908 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07001909 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07001910 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
1911 // break;
1912 // }
jeffhaob57e9522012-04-26 18:08:21 -07001913
1914 /* arg must be an uninitialized reference */
1915 if (!this_type.IsUninitializedTypes()) {
1916 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
1917 << this_type;
1918 break;
1919 }
1920
1921 /*
1922 * Replace the uninitialized reference with an initialized one. We need to do this for all
1923 * registers that have the same object instance in them, not just the "this" register.
1924 */
1925 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001926 }
Ian Rogers46685432012-06-03 22:26:43 -07001927 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001928 work_line_->SetResultRegisterType(return_type);
1929 just_set_result = true;
1930 break;
1931 }
1932 case Instruction::INVOKE_STATIC:
1933 case Instruction::INVOKE_STATIC_RANGE: {
1934 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
1935 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001936 const char* descriptor;
1937 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001938 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001939 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1940 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07001941 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001942 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001943 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001944 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001945 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07001946 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07001947 just_set_result = true;
1948 }
1949 break;
jeffhaobdb76512011-09-07 11:43:16 -07001950 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001951 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001952 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001953 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001954 if (abs_method != NULL) {
1955 Class* called_interface = abs_method->GetDeclaringClass();
1956 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
1957 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
1958 << PrettyMethod(abs_method) << "'";
1959 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001960 }
Ian Rogers0d604842012-04-16 14:50:24 -07001961 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001962 /* Get the type of the "this" arg, which should either be a sub-interface of called
1963 * interface or Object (see comments in RegType::JoinClass).
1964 */
1965 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1966 if (this_type.IsZero()) {
1967 /* null pointer always passes (and always fails at runtime) */
1968 } else {
1969 if (this_type.IsUninitializedTypes()) {
1970 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
1971 << this_type;
1972 break;
1973 }
1974 // In the past we have tried to assert that "called_interface" is assignable
1975 // from "this_type.GetClass()", however, as we do an imprecise Join
1976 // (RegType::JoinClass) we don't have full information on what interfaces are
1977 // implemented by "this_type". For example, two classes may implement the same
1978 // interfaces and have a common parent that doesn't implement the interface. The
1979 // join will set "this_type" to the parent class and a test that this implements
1980 // the interface will incorrectly fail.
1981 }
1982 /*
1983 * We don't have an object instance, so we can't find the concrete method. However, all of
1984 * the type information is in the abstract method, so we're good.
1985 */
1986 const char* descriptor;
1987 if (abs_method == NULL) {
1988 uint32_t method_idx = dec_insn.vB;
1989 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1990 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1991 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1992 } else {
1993 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
1994 }
1995 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
1996 work_line_->SetResultRegisterType(return_type);
1997 work_line_->SetResultRegisterType(return_type);
1998 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001999 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002000 }
jeffhaobdb76512011-09-07 11:43:16 -07002001 case Instruction::NEG_INT:
2002 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002003 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002004 break;
2005 case Instruction::NEG_LONG:
2006 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002007 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002008 break;
2009 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002010 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002011 break;
2012 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002013 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002014 break;
2015 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002016 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002017 break;
2018 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002019 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002020 break;
2021 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002022 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002023 break;
2024 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002025 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002026 break;
2027 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002028 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002029 break;
2030 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002031 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002032 break;
2033 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002034 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002035 break;
2036 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002037 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002038 break;
2039 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002040 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002041 break;
2042 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002043 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002044 break;
2045 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002046 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002047 break;
2048 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002049 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002050 break;
2051 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002052 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002053 break;
2054 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002055 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002056 break;
2057 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002058 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002059 break;
2060
2061 case Instruction::ADD_INT:
2062 case Instruction::SUB_INT:
2063 case Instruction::MUL_INT:
2064 case Instruction::REM_INT:
2065 case Instruction::DIV_INT:
2066 case Instruction::SHL_INT:
2067 case Instruction::SHR_INT:
2068 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002069 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002070 break;
2071 case Instruction::AND_INT:
2072 case Instruction::OR_INT:
2073 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002074 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002075 break;
2076 case Instruction::ADD_LONG:
2077 case Instruction::SUB_LONG:
2078 case Instruction::MUL_LONG:
2079 case Instruction::DIV_LONG:
2080 case Instruction::REM_LONG:
2081 case Instruction::AND_LONG:
2082 case Instruction::OR_LONG:
2083 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002084 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002085 break;
2086 case Instruction::SHL_LONG:
2087 case Instruction::SHR_LONG:
2088 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002089 /* shift distance is Int, making these different from other binary operations */
2090 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002091 break;
2092 case Instruction::ADD_FLOAT:
2093 case Instruction::SUB_FLOAT:
2094 case Instruction::MUL_FLOAT:
2095 case Instruction::DIV_FLOAT:
2096 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002097 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002098 break;
2099 case Instruction::ADD_DOUBLE:
2100 case Instruction::SUB_DOUBLE:
2101 case Instruction::MUL_DOUBLE:
2102 case Instruction::DIV_DOUBLE:
2103 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002104 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002105 break;
2106 case Instruction::ADD_INT_2ADDR:
2107 case Instruction::SUB_INT_2ADDR:
2108 case Instruction::MUL_INT_2ADDR:
2109 case Instruction::REM_INT_2ADDR:
2110 case Instruction::SHL_INT_2ADDR:
2111 case Instruction::SHR_INT_2ADDR:
2112 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002113 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002114 break;
2115 case Instruction::AND_INT_2ADDR:
2116 case Instruction::OR_INT_2ADDR:
2117 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002118 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002119 break;
2120 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002121 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002122 break;
2123 case Instruction::ADD_LONG_2ADDR:
2124 case Instruction::SUB_LONG_2ADDR:
2125 case Instruction::MUL_LONG_2ADDR:
2126 case Instruction::DIV_LONG_2ADDR:
2127 case Instruction::REM_LONG_2ADDR:
2128 case Instruction::AND_LONG_2ADDR:
2129 case Instruction::OR_LONG_2ADDR:
2130 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002131 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002132 break;
2133 case Instruction::SHL_LONG_2ADDR:
2134 case Instruction::SHR_LONG_2ADDR:
2135 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002136 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002137 break;
2138 case Instruction::ADD_FLOAT_2ADDR:
2139 case Instruction::SUB_FLOAT_2ADDR:
2140 case Instruction::MUL_FLOAT_2ADDR:
2141 case Instruction::DIV_FLOAT_2ADDR:
2142 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002143 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002144 break;
2145 case Instruction::ADD_DOUBLE_2ADDR:
2146 case Instruction::SUB_DOUBLE_2ADDR:
2147 case Instruction::MUL_DOUBLE_2ADDR:
2148 case Instruction::DIV_DOUBLE_2ADDR:
2149 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002150 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002151 break;
2152 case Instruction::ADD_INT_LIT16:
2153 case Instruction::RSUB_INT:
2154 case Instruction::MUL_INT_LIT16:
2155 case Instruction::DIV_INT_LIT16:
2156 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002157 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002158 break;
2159 case Instruction::AND_INT_LIT16:
2160 case Instruction::OR_INT_LIT16:
2161 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002163 break;
2164 case Instruction::ADD_INT_LIT8:
2165 case Instruction::RSUB_INT_LIT8:
2166 case Instruction::MUL_INT_LIT8:
2167 case Instruction::DIV_INT_LIT8:
2168 case Instruction::REM_INT_LIT8:
2169 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002170 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002171 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002172 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002173 break;
2174 case Instruction::AND_INT_LIT8:
2175 case Instruction::OR_INT_LIT8:
2176 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002177 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002178 break;
2179
2180 /*
2181 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002182 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002183 * inserted in the course of verification, we can expect to see it here.
2184 */
jeffhaob4df5142011-09-19 20:25:32 -07002185 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002186 break;
2187
Ian Rogersd81871c2011-10-03 13:57:23 -07002188 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002189 case Instruction::UNUSED_EE:
2190 case Instruction::UNUSED_EF:
2191 case Instruction::UNUSED_F2:
2192 case Instruction::UNUSED_F3:
2193 case Instruction::UNUSED_F4:
2194 case Instruction::UNUSED_F5:
2195 case Instruction::UNUSED_F6:
2196 case Instruction::UNUSED_F7:
2197 case Instruction::UNUSED_F8:
2198 case Instruction::UNUSED_F9:
2199 case Instruction::UNUSED_FA:
2200 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002201 case Instruction::UNUSED_F0:
2202 case Instruction::UNUSED_F1:
2203 case Instruction::UNUSED_E3:
2204 case Instruction::UNUSED_E8:
2205 case Instruction::UNUSED_E7:
2206 case Instruction::UNUSED_E4:
2207 case Instruction::UNUSED_E9:
2208 case Instruction::UNUSED_FC:
2209 case Instruction::UNUSED_E5:
2210 case Instruction::UNUSED_EA:
2211 case Instruction::UNUSED_FD:
2212 case Instruction::UNUSED_E6:
2213 case Instruction::UNUSED_EB:
2214 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002215 case Instruction::UNUSED_3E:
2216 case Instruction::UNUSED_3F:
2217 case Instruction::UNUSED_40:
2218 case Instruction::UNUSED_41:
2219 case Instruction::UNUSED_42:
2220 case Instruction::UNUSED_43:
2221 case Instruction::UNUSED_73:
2222 case Instruction::UNUSED_79:
2223 case Instruction::UNUSED_7A:
2224 case Instruction::UNUSED_EC:
2225 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002226 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002227 break;
2228
2229 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002230 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002231 * complain if an instruction is missing (which is desirable).
2232 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002233 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002234
Ian Rogersad0b3a32012-04-16 14:50:24 -07002235 if (have_pending_hard_failure_) {
2236 if (!Runtime::Current()->IsStarted()) {
jeffhaob57e9522012-04-26 18:08:21 -07002237 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002238 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002239 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002240 /* immediate failure, reject class */
2241 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2242 return false;
2243 } else if (have_pending_rewrite_failure_) {
2244 /* replace opcode and continue on */
2245 std::string append("Replacing opcode ");
2246 append += inst->DumpString(dex_file_);
2247 AppendToLastFailMessage(append);
2248 ReplaceFailingInstruction();
2249 /* IMPORTANT: method->insns may have been changed */
2250 insns = code_item_->insns_ + work_insn_idx_;
2251 /* continue on as if we just handled a throw-verification-error */
2252 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002253 }
jeffhaobdb76512011-09-07 11:43:16 -07002254 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002255 * If we didn't just set the result register, clear it out. This ensures that you can only use
2256 * "move-result" immediately after the result is set. (We could check this statically, but it's
2257 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002258 */
2259 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002260 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002261 }
2262
jeffhaoa0a764a2011-09-16 10:43:38 -07002263 /* Handle "continue". Tag the next consecutive instruction. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002264 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers776ac1f2012-04-13 23:36:36 -07002265 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
Ian Rogersd81871c2011-10-03 13:57:23 -07002266 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
jeffhaod5347e02012-03-22 17:25:05 -07002267 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002268 return false;
2269 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002270 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2271 // next instruction isn't one.
jeffhaod5347e02012-03-22 17:25:05 -07002272 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002273 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002274 }
2275 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2276 if (next_line != NULL) {
2277 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2278 // needed.
2279 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002280 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002281 }
jeffhaobdb76512011-09-07 11:43:16 -07002282 } else {
2283 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002284 * We're not recording register data for the next instruction, so we don't know what the prior
2285 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002286 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002287 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002288 }
2289 }
2290
2291 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002292 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002293 *
2294 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002295 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002296 * somebody could get a reference field, check it for zero, and if the
2297 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002298 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002299 * that, and will reject the code.
2300 *
2301 * TODO: avoid re-fetching the branch target
2302 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002303 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002304 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002305 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002306 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002307 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002308 return false;
2309 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002310 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002311 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002312 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002313 }
jeffhaobdb76512011-09-07 11:43:16 -07002314 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002315 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002316 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002317 }
jeffhaobdb76512011-09-07 11:43:16 -07002318 }
2319
2320 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002321 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002322 *
2323 * We've already verified that the table is structurally sound, so we
2324 * just need to walk through and tag the targets.
2325 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002326 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002327 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2328 const uint16_t* switch_insns = insns + offset_to_switch;
2329 int switch_count = switch_insns[1];
2330 int offset_to_targets, targ;
2331
2332 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2333 /* 0 = sig, 1 = count, 2/3 = first key */
2334 offset_to_targets = 4;
2335 } else {
2336 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002337 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002338 offset_to_targets = 2 + 2 * switch_count;
2339 }
2340
2341 /* verify each switch target */
2342 for (targ = 0; targ < switch_count; targ++) {
2343 int offset;
2344 uint32_t abs_offset;
2345
2346 /* offsets are 32-bit, and only partly endian-swapped */
2347 offset = switch_insns[offset_to_targets + targ * 2] |
2348 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002349 abs_offset = work_insn_idx_ + offset;
2350 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002351 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002352 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002353 }
2354 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002355 return false;
2356 }
2357 }
2358
2359 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002360 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2361 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002362 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002363 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002364 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002365 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002366
Ian Rogers0571d352011-11-03 19:51:38 -07002367 for (; iterator.HasNext(); iterator.Next()) {
2368 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002369 within_catch_all = true;
2370 }
jeffhaobdb76512011-09-07 11:43:16 -07002371 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002372 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2373 * "work_regs", because at runtime the exception will be thrown before the instruction
2374 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002375 */
Ian Rogers0571d352011-11-03 19:51:38 -07002376 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002377 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002378 }
jeffhaobdb76512011-09-07 11:43:16 -07002379 }
2380
2381 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002382 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2383 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002384 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002385 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002386 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002387 * The state in work_line reflects the post-execution state. If the current instruction is a
2388 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002389 * it will do so before grabbing the lock).
2390 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002391 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002392 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002393 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002394 return false;
2395 }
2396 }
2397 }
2398
jeffhaod1f0fde2011-09-08 17:25:33 -07002399 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002400 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002401 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002402 return false;
2403 }
jeffhaobdb76512011-09-07 11:43:16 -07002404 }
2405
2406 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002407 * Update start_guess. Advance to the next instruction of that's
2408 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002409 * neither of those exists we're in a return or throw; leave start_guess
2410 * alone and let the caller sort it out.
2411 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002412 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002413 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002414 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002415 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002416 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002417 }
2418
Ian Rogersd81871c2011-10-03 13:57:23 -07002419 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2420 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002421
2422 return true;
2423}
2424
Ian Rogers776ac1f2012-04-13 23:36:36 -07002425const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002426 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002427 const RegType& referrer = GetDeclaringClass();
2428 Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002429 const RegType& result =
2430 klass != NULL ? reg_types_.FromClass(klass)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002431 : reg_types_.FromDescriptor(class_loader_, descriptor);
2432 if (result.IsConflict()) {
2433 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2434 << "' in " << referrer;
2435 return result;
2436 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002437 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002438 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002439 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002440 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002441 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002442 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002443 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002444 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002445 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002446 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002447}
2448
Ian Rogers776ac1f2012-04-13 23:36:36 -07002449const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002450 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002451 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002452 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002453 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2454 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002455 CatchHandlerIterator iterator(handlers_ptr);
2456 for (; iterator.HasNext(); iterator.Next()) {
2457 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2458 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002459 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07002460 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002461 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002462 if (common_super == NULL) {
2463 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2464 // as that is caught at runtime
2465 common_super = &exception;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002466 } else if (!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002467 // We don't know enough about the type and the common path merge will result in
2468 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002469 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002470 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002471 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002472 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002473 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002474 common_super = &common_super->Merge(exception, &reg_types_);
2475 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002476 }
2477 }
2478 }
2479 }
Ian Rogers0571d352011-11-03 19:51:38 -07002480 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002481 }
2482 }
2483 if (common_super == NULL) {
2484 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002485 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002486 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002487 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002488 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002489}
2490
Ian Rogersad0b3a32012-04-16 14:50:24 -07002491Method* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx, MethodType method_type) {
2492 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002493 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002494 if (klass_type.IsConflict()) {
2495 std::string append(" in attempt to access method ");
2496 append += dex_file_->GetMethodName(method_id);
2497 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002498 return NULL;
2499 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002500 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002501 return NULL; // Can't resolve Class so no more to do here
2502 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002503 Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002504 const RegType& referrer = GetDeclaringClass();
2505 Method* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002506 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002507 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002508 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002509
2510 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002511 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002512 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002513 res_method = klass->FindInterfaceMethod(name, signature);
2514 } else {
2515 res_method = klass->FindVirtualMethod(name, signature);
2516 }
2517 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002518 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002519 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002520 // If a virtual or interface method wasn't found with the expected type, look in
2521 // the direct methods. This can happen when the wrong invoke type is used or when
2522 // a class has changed, and will be flagged as an error in later checks.
2523 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2524 res_method = klass->FindDirectMethod(name, signature);
2525 }
2526 if (res_method == NULL) {
2527 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2528 << PrettyDescriptor(klass) << "." << name
2529 << " " << signature;
2530 return NULL;
2531 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002532 }
2533 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002534 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2535 // enforce them here.
2536 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002537 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2538 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002539 return NULL;
2540 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002541 // Disallow any calls to class initializers.
2542 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002543 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2544 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002545 return NULL;
2546 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002547 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002548 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002549 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002550 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002551 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002552 }
jeffhaode0d9c92012-02-27 13:58:13 -08002553 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2554 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002555 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2556 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002557 return NULL;
2558 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002559 // Check that interface methods match interface classes.
2560 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2561 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2562 << " is in an interface class " << PrettyClass(klass);
2563 return NULL;
2564 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2565 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2566 << " is in a non-interface class " << PrettyClass(klass);
2567 return NULL;
2568 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002569 // See if the method type implied by the invoke instruction matches the access flags for the
2570 // target method.
2571 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2572 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2573 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2574 ) {
Ian Rogers573db4a2011-12-13 15:30:50 -08002575 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type does not match method type of "
2576 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002577 return NULL;
2578 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002579 return res_method;
2580}
2581
Ian Rogers776ac1f2012-04-13 23:36:36 -07002582Method* MethodVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
Ian Rogers46685432012-06-03 22:26:43 -07002583 MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002584 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2585 // we're making.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002586 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002587 if (res_method == NULL) { // error or class is unresolved
2588 return NULL;
2589 }
2590
Ian Rogersd81871c2011-10-03 13:57:23 -07002591 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2592 // has a vtable entry for the target method.
2593 if (is_super) {
2594 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002595 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
jeffhao4d8df822012-04-24 17:09:36 -07002596 if (super.IsConflict()) { // unknown super class
2597 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
2598 << PrettyMethod(method_idx_, *dex_file_)
2599 << " to super " << PrettyMethod(res_method);
2600 return NULL;
2601 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002602 Class* super_klass = super.GetClass();
2603 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002604 MethodHelper mh(res_method);
2605 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
2606 << PrettyMethod(method_idx_, *dex_file_)
2607 << " to super " << super
2608 << "." << mh.GetName()
2609 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002610 return NULL;
2611 }
2612 }
2613 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2614 // match the call to the signature. Also, we might might be calling through an abstract method
2615 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08002616 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002617 /* caught by static verifier */
2618 DCHECK(is_range || expected_args <= 5);
2619 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002620 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002621 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2622 return NULL;
2623 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002624
jeffhaobdb76512011-09-07 11:43:16 -07002625 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002626 * Check the "this" argument, which must be an instance of the class that declared the method.
2627 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2628 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002629 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002630 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002631 if (!res_method->IsStatic()) {
2632 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002633 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002634 return NULL;
2635 }
2636 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002637 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002638 return NULL;
2639 }
2640 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07002641 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
2642 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002643 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002644 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002645 return NULL;
2646 }
2647 }
2648 actual_args++;
2649 }
2650 /*
2651 * Process the target method's signature. This signature may or may not
2652 * have been verified, so we can't assume it's properly formed.
2653 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002654 MethodHelper mh(res_method);
2655 const DexFile::TypeList* params = mh.GetParameterTypeList();
2656 size_t params_size = params == NULL ? 0 : params->Size();
2657 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002658 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002659 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002660 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2661 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002662 return NULL;
2663 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002664 const char* descriptor =
2665 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2666 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002667 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002668 << " missing signature component";
2669 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002670 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002671 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002672 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002673 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002674 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002675 }
2676 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2677 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002678 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002679 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002680 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002681 return NULL;
2682 } else {
2683 return res_method;
2684 }
2685}
2686
Ian Rogers776ac1f2012-04-13 23:36:36 -07002687void MethodVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08002688 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002689 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002690 if (res_type.IsConflict()) { // bad class
2691 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002692 } else {
2693 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2694 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002695 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002696 } else if (!is_filled) {
2697 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002698 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002699 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002700 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002701 } else {
2702 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2703 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002704 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002705 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002706 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002707 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002708 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002709 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002710 return;
2711 }
2712 }
2713 // filled-array result goes into "result" register
2714 work_line_->SetResultRegisterType(res_type);
2715 }
2716 }
2717}
2718
Ian Rogers776ac1f2012-04-13 23:36:36 -07002719void MethodVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002721 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002722 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002723 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002724 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002725 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002726 if (array_type.IsZero()) {
2727 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2728 // instruction type. TODO: have a proper notion of bottom here.
2729 if (!is_primitive || insn_type.IsCategory1Types()) {
2730 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08002731 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002732 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002733 // Category 2
Elliott Hughesadb8c672012-03-06 16:49:32 -08002734 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
Ian Rogers89310de2012-02-01 13:47:30 -08002735 }
jeffhaofc3144e2012-02-01 17:21:15 -08002736 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002737 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002738 } else {
2739 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002740 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002741 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002742 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002743 << " source for aget-object";
2744 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002745 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002746 << " source for category 1 aget";
2747 } else if (is_primitive && !insn_type.Equals(component_type) &&
2748 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2749 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002750 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogersd81871c2011-10-03 13:57:23 -07002751 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002752 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002753 // Use knowledge of the field type which is stronger than the type inferred from the
2754 // instruction, which can't differentiate object types and ints from floats, longs from
2755 // doubles.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002756 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002757 }
2758 }
2759 }
2760}
2761
Ian Rogers776ac1f2012-04-13 23:36:36 -07002762void MethodVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002763 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002764 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002765 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002766 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002767 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002768 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002769 if (array_type.IsZero()) {
2770 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2771 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002772 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002773 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002774 } else {
2775 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002776 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002777 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002778 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002779 << " source for aput-object";
2780 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002781 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002782 << " source for category 1 aput";
2783 } else if (is_primitive && !insn_type.Equals(component_type) &&
2784 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2785 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002786 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002787 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002788 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002789 // The instruction agrees with the type of array, confirm the value to be stored does too
2790 // Note: we use the instruction type (rather than the component type) for aput-object as
2791 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08002792 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002793 }
2794 }
2795 }
2796}
2797
Ian Rogers776ac1f2012-04-13 23:36:36 -07002798Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002799 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2800 // Check access to class
2801 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002802 if (klass_type.IsConflict()) { // bad class
2803 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
2804 field_idx, dex_file_->GetFieldName(field_id),
2805 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002806 return NULL;
2807 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002808 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002809 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08002810 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002811 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2812 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002813 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002814 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
2815 << dex_file_->GetFieldName(field_id) << ") in "
2816 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 DCHECK(Thread::Current()->IsExceptionPending());
2818 Thread::Current()->ClearException();
2819 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002820 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2821 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002822 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002823 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002824 return NULL;
2825 } else if (!field->IsStatic()) {
2826 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
2827 return NULL;
2828 } else {
2829 return field;
2830 }
2831}
2832
Ian Rogers776ac1f2012-04-13 23:36:36 -07002833Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002834 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2835 // Check access to class
2836 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002837 if (klass_type.IsConflict()) {
2838 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
2839 field_idx, dex_file_->GetFieldName(field_id),
2840 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002841 return NULL;
2842 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002843 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002844 return NULL; // Can't resolve Class so no more to do here
2845 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002846 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2847 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002848 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002849 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
2850 << dex_file_->GetFieldName(field_id) << ") in "
2851 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002852 DCHECK(Thread::Current()->IsExceptionPending());
2853 Thread::Current()->ClearException();
2854 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002855 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2856 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002857 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002858 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002859 return NULL;
2860 } else if (field->IsStatic()) {
2861 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
2862 << " to not be static";
2863 return NULL;
2864 } else if (obj_type.IsZero()) {
2865 // Cannot infer and check type, however, access will cause null pointer exception
2866 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07002867 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002868 const RegType& field_klass = reg_types_.FromClass(field->GetDeclaringClass());
2869 if (obj_type.IsUninitializedTypes() &&
2870 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
2871 !field_klass.Equals(GetDeclaringClass()))) {
2872 // Field accesses through uninitialized references are only allowable for constructors where
2873 // the field is declared in this class
2874 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
2875 << " of a not fully initialized object within the context of "
2876 << PrettyMethod(method_idx_, *dex_file_);
2877 return NULL;
2878 } else if (!field_klass.IsAssignableFrom(obj_type)) {
2879 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
2880 // of C1. For resolution to occur the declared class of the field must be compatible with
2881 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
2882 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
2883 << " from object of type " << obj_type;
2884 return NULL;
2885 } else {
2886 return field;
2887 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002888 }
2889}
2890
Ian Rogers776ac1f2012-04-13 23:36:36 -07002891void MethodVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002892 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002893 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002894 Field* field;
2895 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002896 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002897 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002898 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07002899 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002900 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002901 const char* descriptor;
2902 const ClassLoader* loader;
2903 if (field != NULL) {
2904 descriptor = FieldHelper(field).GetTypeDescriptor();
2905 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07002906 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002907 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2908 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2909 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07002910 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002911 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
2912 if (is_primitive) {
2913 if (field_type.Equals(insn_type) ||
2914 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
2915 (field_type.IsDouble() && insn_type.IsLongTypes())) {
2916 // expected that read is of the correct primitive type or that int reads are reading
2917 // floats or long reads are reading doubles
2918 } else {
2919 // This is a global failure rather than a class change failure as the instructions and
2920 // the descriptors for the type should have been consistent within the same file at
2921 // compile time
2922 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
2923 << " to be of type '" << insn_type
2924 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002925 return;
2926 }
2927 } else {
2928 if (!insn_type.IsAssignableFrom(field_type)) {
2929 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
2930 << " to be compatible with type '" << insn_type
2931 << "' but found type '" << field_type
2932 << "' in get-object";
2933 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Conflict());
2934 return;
2935 }
2936 }
2937 work_line_->SetRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002938}
2939
Ian Rogers776ac1f2012-04-13 23:36:36 -07002940void MethodVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002941 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002942 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002943 Field* field;
2944 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07002945 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002946 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002947 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07002948 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002949 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002950 const char* descriptor;
2951 const ClassLoader* loader;
2952 if (field != NULL) {
2953 descriptor = FieldHelper(field).GetTypeDescriptor();
2954 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07002955 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002956 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2957 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2958 loader = class_loader_;
2959 }
2960 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
2961 if (field != NULL) {
2962 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
2963 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
2964 << " from other class " << GetDeclaringClass();
2965 return;
2966 }
2967 }
2968 if (is_primitive) {
2969 // Primitive field assignability rules are weaker than regular assignability rules
2970 bool instruction_compatible;
2971 bool value_compatible;
2972 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
2973 if (field_type.IsIntegralTypes()) {
2974 instruction_compatible = insn_type.IsIntegralTypes();
2975 value_compatible = value_type.IsIntegralTypes();
2976 } else if (field_type.IsFloat()) {
2977 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
2978 value_compatible = value_type.IsFloatTypes();
2979 } else if (field_type.IsLong()) {
2980 instruction_compatible = insn_type.IsLong();
2981 value_compatible = value_type.IsLongTypes();
2982 } else if (field_type.IsDouble()) {
2983 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
2984 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07002985 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002986 instruction_compatible = false; // reference field with primitive store
2987 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07002988 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002989 if (!instruction_compatible) {
2990 // This is a global failure rather than a class change failure as the instructions and
2991 // the descriptors for the type should have been consistent within the same file at
2992 // compile time
2993 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
2994 << " to be of type '" << insn_type
2995 << "' but found type '" << field_type
2996 << "' in put";
2997 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07002998 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002999 if (!value_compatible) {
3000 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
3001 << " of type " << value_type
3002 << " but expected " << field_type
3003 << " for store to " << PrettyField(field) << " in put";
3004 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003005 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003006 } else {
3007 if (!insn_type.IsAssignableFrom(field_type)) {
3008 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3009 << " to be compatible with type '" << insn_type
3010 << "' but found type '" << field_type
3011 << "' in put-object";
3012 return;
3013 }
3014 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003015 }
3016}
3017
Ian Rogers776ac1f2012-04-13 23:36:36 -07003018bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003019 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003020 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003021 return false;
3022 }
3023 return true;
3024}
3025
Ian Rogers776ac1f2012-04-13 23:36:36 -07003026void MethodVerifier::ReplaceFailingInstruction() {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003027 // Pop the failure and clear the need for rewriting.
3028 size_t failure_number = failures_.size();
3029 CHECK_NE(failure_number, 0U);
3030 DCHECK_EQ(failure_messages_.size(), failure_number);
jeffhaob57e9522012-04-26 18:08:21 -07003031 std::ostringstream* failure_message = failure_messages_[0];
3032 VerifyError failure = failures_[0];
3033 failures_.clear();
3034 failure_messages_.clear();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003035 have_pending_rewrite_failure_ = false;
3036
Ian Rogersf1864ef2011-12-09 12:39:48 -08003037 if (Runtime::Current()->IsStarted()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003038 LOG(ERROR) << "Verification attempting to replace instructions at runtime in "
3039 << PrettyMethod(method_idx_, *dex_file_) << " " << failure_message->str();
Ian Rogersf1864ef2011-12-09 12:39:48 -08003040 return;
3041 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003042 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3043 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3044 VerifyErrorRefType ref_type;
3045 switch (inst->Opcode()) {
3046 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003047 case Instruction::CHECK_CAST:
3048 case Instruction::INSTANCE_OF:
3049 case Instruction::NEW_INSTANCE:
3050 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003051 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003052 case Instruction::FILLED_NEW_ARRAY_RANGE:
3053 ref_type = VERIFY_ERROR_REF_CLASS;
3054 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003055 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003056 case Instruction::IGET_BOOLEAN:
3057 case Instruction::IGET_BYTE:
3058 case Instruction::IGET_CHAR:
3059 case Instruction::IGET_SHORT:
3060 case Instruction::IGET_WIDE:
3061 case Instruction::IGET_OBJECT:
3062 case Instruction::IPUT:
3063 case Instruction::IPUT_BOOLEAN:
3064 case Instruction::IPUT_BYTE:
3065 case Instruction::IPUT_CHAR:
3066 case Instruction::IPUT_SHORT:
3067 case Instruction::IPUT_WIDE:
3068 case Instruction::IPUT_OBJECT:
3069 case Instruction::SGET:
3070 case Instruction::SGET_BOOLEAN:
3071 case Instruction::SGET_BYTE:
3072 case Instruction::SGET_CHAR:
3073 case Instruction::SGET_SHORT:
3074 case Instruction::SGET_WIDE:
3075 case Instruction::SGET_OBJECT:
3076 case Instruction::SPUT:
3077 case Instruction::SPUT_BOOLEAN:
3078 case Instruction::SPUT_BYTE:
3079 case Instruction::SPUT_CHAR:
3080 case Instruction::SPUT_SHORT:
3081 case Instruction::SPUT_WIDE:
3082 case Instruction::SPUT_OBJECT:
3083 ref_type = VERIFY_ERROR_REF_FIELD;
3084 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003085 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003086 case Instruction::INVOKE_VIRTUAL_RANGE:
3087 case Instruction::INVOKE_SUPER:
3088 case Instruction::INVOKE_SUPER_RANGE:
3089 case Instruction::INVOKE_DIRECT:
3090 case Instruction::INVOKE_DIRECT_RANGE:
3091 case Instruction::INVOKE_STATIC:
3092 case Instruction::INVOKE_STATIC_RANGE:
3093 case Instruction::INVOKE_INTERFACE:
3094 case Instruction::INVOKE_INTERFACE_RANGE:
3095 ref_type = VERIFY_ERROR_REF_METHOD;
3096 break;
jeffhaobdb76512011-09-07 11:43:16 -07003097 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003098 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003099 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003100 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003101 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3102 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3103 // instruction, so assert it.
3104 size_t width = inst->SizeInCodeUnits();
3105 CHECK_GT(width, 1u);
Ian Rogersf1864ef2011-12-09 12:39:48 -08003106 // If the instruction is larger than 2 code units, rewrite subsequent code unit sized chunks with
Ian Rogersd81871c2011-10-03 13:57:23 -07003107 // NOPs
3108 for (size_t i = 2; i < width; i++) {
3109 insns[work_insn_idx_ + i] = Instruction::NOP;
3110 }
3111 // Encode the opcode, with the failure code in the high byte
3112 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
Ian Rogersad0b3a32012-04-16 14:50:24 -07003113 (failure << 8) | // AA - component
Ian Rogersd81871c2011-10-03 13:57:23 -07003114 (ref_type << (8 + kVerifyErrorRefTypeShift));
3115 insns[work_insn_idx_] = new_instruction;
3116 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3117 // rewrote, so nothing to do here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003118 LOG(INFO) << "Verification error, replacing instructions in "
3119 << PrettyMethod(method_idx_, *dex_file_) << " "
3120 << failure_message->str();
Ian Rogers9fdfc182011-10-26 23:12:52 -07003121 if (gDebugVerify) {
Elliott Hughesc073b072012-05-24 19:29:17 -07003122 std::cout << "\n" << info_messages_.str();
Ian Rogers9fdfc182011-10-26 23:12:52 -07003123 Dump(std::cout);
3124 }
jeffhaobdb76512011-09-07 11:43:16 -07003125}
jeffhaoba5ebb92011-08-25 17:24:37 -07003126
Ian Rogers776ac1f2012-04-13 23:36:36 -07003127bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003128 bool changed = true;
3129 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3130 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003131 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003132 * We haven't processed this instruction before, and we haven't touched the registers here, so
3133 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3134 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003135 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003136 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003137 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003138 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3139 if (gDebugVerify) {
3140 copy->CopyFromLine(target_line);
3141 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003142 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003143 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003144 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003145 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003146 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003147 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003148 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3149 << *copy.get() << " MERGE\n"
3150 << *merge_line << " ==\n"
3151 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003152 }
3153 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003154 if (changed) {
3155 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003156 }
3157 return true;
3158}
3159
Ian Rogers776ac1f2012-04-13 23:36:36 -07003160InsnFlags* MethodVerifier::CurrentInsnFlags() {
3161 return &insn_flags_[work_insn_idx_];
3162}
3163
Ian Rogersad0b3a32012-04-16 14:50:24 -07003164const RegType& MethodVerifier::GetMethodReturnType() {
3165 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx_);
3166 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3167 uint16_t return_type_idx = proto_id.return_type_idx_;
3168 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
3169 return reg_types_.FromDescriptor(class_loader_, descriptor);
3170}
3171
3172const RegType& MethodVerifier::GetDeclaringClass() {
3173 if (foo_method_ != NULL) {
3174 return reg_types_.FromClass(foo_method_->GetDeclaringClass());
3175 } else {
3176 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx_);
3177 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
3178 return reg_types_.FromDescriptor(class_loader_, descriptor);
3179 }
3180}
3181
Ian Rogers776ac1f2012-04-13 23:36:36 -07003182void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003183 size_t* log2_max_gc_pc) {
3184 size_t local_gc_points = 0;
3185 size_t max_insn = 0;
3186 size_t max_ref_reg = -1;
3187 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3188 if (insn_flags_[i].IsGcPoint()) {
3189 local_gc_points++;
3190 max_insn = i;
3191 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003192 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003193 }
3194 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003195 *gc_points = local_gc_points;
3196 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3197 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003198 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003199 i++;
3200 }
3201 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003202}
3203
Ian Rogers776ac1f2012-04-13 23:36:36 -07003204const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003205 size_t num_entries, ref_bitmap_bits, pc_bits;
3206 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3207 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003208 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003209 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003210 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003211 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003212 return NULL;
3213 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003214 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3215 // There are 2 bytes to encode the number of entries
3216 if (num_entries >= 65536) {
3217 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003218 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003219 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003220 return NULL;
3221 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003222 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003223 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003224 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003225 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003226 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003227 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003228 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003229 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003230 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003231 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003232 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003233 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3234 return NULL;
3235 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003236 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003237 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003238 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003239 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003240 return NULL;
3241 }
3242 // Write table header
Ian Rogers776ac1f2012-04-13 23:36:36 -07003243 table->push_back(format | ((ref_bitmap_bytes >> PcToReferenceMap::kRegMapFormatShift) &
3244 ~PcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003245 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003246 table->push_back(num_entries & 0xFF);
3247 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003248 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003249 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3250 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003251 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003252 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003253 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003254 }
3255 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003256 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003257 }
3258 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003259 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003260 return table;
3261}
jeffhaoa0a764a2011-09-16 10:43:38 -07003262
Ian Rogers776ac1f2012-04-13 23:36:36 -07003263void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003264 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3265 // that the table data is well formed and all references are marked (or not) in the bitmap
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003266 PcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003267 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003268 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003269 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3270 if (insn_flags_[i].IsGcPoint()) {
3271 CHECK_LT(map_index, map.NumEntries());
3272 CHECK_EQ(map.GetPC(map_index), i);
3273 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3274 map_index++;
3275 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003276 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003277 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003278 CHECK_LT(j / 8, map.RegWidth());
3279 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3280 } else if ((j / 8) < map.RegWidth()) {
3281 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3282 } else {
3283 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3284 }
3285 }
3286 } else {
3287 CHECK(reg_bitmap == NULL);
3288 }
3289 }
3290}
jeffhaoa0a764a2011-09-16 10:43:38 -07003291
Ian Rogers776ac1f2012-04-13 23:36:36 -07003292Mutex* MethodVerifier::gc_maps_lock_ = NULL;
3293MethodVerifier::GcMapTable* MethodVerifier::gc_maps_ = NULL;
jeffhaoa0a764a2011-09-16 10:43:38 -07003294
Ian Rogers776ac1f2012-04-13 23:36:36 -07003295void MethodVerifier::InitGcMaps() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003296 gc_maps_lock_ = new Mutex("verifier GC maps lock");
3297 MutexLock mu(*gc_maps_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07003298 gc_maps_ = new MethodVerifier::GcMapTable;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003299}
3300
Ian Rogers776ac1f2012-04-13 23:36:36 -07003301void MethodVerifier::DeleteGcMaps() {
Elliott Hughesf34f1742012-03-16 18:56:00 -07003302 {
3303 MutexLock mu(*gc_maps_lock_);
3304 STLDeleteValues(gc_maps_);
3305 delete gc_maps_;
3306 gc_maps_ = NULL;
3307 }
3308 delete gc_maps_lock_;
3309 gc_maps_lock_ = NULL;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003310}
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003311
Ian Rogers776ac1f2012-04-13 23:36:36 -07003312void MethodVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003313 MutexLock mu(*gc_maps_lock_);
Elliott Hughesa0e18062012-04-13 15:59:59 -07003314 GcMapTable::iterator it = gc_maps_->find(ref);
3315 if (it != gc_maps_->end()) {
3316 delete it->second;
3317 gc_maps_->erase(it);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003318 }
Elliott Hughesa0e18062012-04-13 15:59:59 -07003319 gc_maps_->Put(ref, &gc_map);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003320 CHECK(GetGcMap(ref) != NULL);
3321}
3322
Ian Rogers776ac1f2012-04-13 23:36:36 -07003323const std::vector<uint8_t>* MethodVerifier::GetGcMap(Compiler::MethodReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003324 MutexLock mu(*gc_maps_lock_);
3325 GcMapTable::const_iterator it = gc_maps_->find(ref);
3326 if (it == gc_maps_->end()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003327 return NULL;
3328 }
3329 CHECK(it->second != NULL);
3330 return it->second;
3331}
3332
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003333static Mutex& GetRejectedClassesLock() {
3334 static Mutex rejected_classes_lock("verifier rejected classes lock");
3335 return rejected_classes_lock;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003336}
3337
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003338static std::set<Compiler::ClassReference>& GetRejectedClasses() {
3339 static std::set<Compiler::ClassReference> rejected_classes;
3340 return rejected_classes;
3341}
jeffhaod1224c72012-02-29 13:43:08 -08003342
Ian Rogers776ac1f2012-04-13 23:36:36 -07003343void MethodVerifier::AddRejectedClass(Compiler::ClassReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003344 MutexLock mu(GetRejectedClassesLock());
3345 GetRejectedClasses().insert(ref);
jeffhaod1224c72012-02-29 13:43:08 -08003346 CHECK(IsClassRejected(ref));
3347}
3348
Ian Rogers776ac1f2012-04-13 23:36:36 -07003349bool MethodVerifier::IsClassRejected(Compiler::ClassReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003350 MutexLock mu(GetRejectedClassesLock());
3351 std::set<Compiler::ClassReference>& rejected_classes(GetRejectedClasses());
3352 return (rejected_classes.find(ref) != rejected_classes.end());
jeffhaod1224c72012-02-29 13:43:08 -08003353}
3354
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -07003355#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Ian Rogers776ac1f2012-04-13 23:36:36 -07003356const InferredRegCategoryMap* MethodVerifier::GenerateInferredRegCategoryMap() {
Logan Chienfca7e872011-12-20 20:08:22 +08003357 uint32_t insns_size = code_item_->insns_size_in_code_units_;
3358 uint16_t regs_size = code_item_->registers_size_;
3359
3360 UniquePtr<InferredRegCategoryMap> table(
3361 new InferredRegCategoryMap(insns_size, regs_size));
3362
3363 for (size_t i = 0; i < insns_size; ++i) {
3364 if (RegisterLine* line = reg_table_.GetLine(i)) {
TDYa127526643e2012-05-26 01:01:48 -07003365 const Instruction* inst = Instruction::At(code_item_->insns_ + i);
3366
3367 // GC points
3368 if (inst->IsBranch() || inst->IsInvoke()) {
3369 for (size_t r = 0; r < regs_size; ++r) {
3370 const RegType &rt = line->GetRegisterType(r);
3371 if (rt.IsNonZeroReferenceTypes()) {
3372 table->SetRegCanBeObject(r);
3373 }
TDYa127b2eb5c12012-05-24 15:52:10 -07003374 }
3375 }
3376
TDYa127526643e2012-05-26 01:01:48 -07003377 /* We only use InferredRegCategoryMap in one case */
3378 if (inst->IsBranch()) {
TDYa127b2eb5c12012-05-24 15:52:10 -07003379 for (size_t r = 0; r < regs_size; ++r) {
3380 const RegType &rt = line->GetRegisterType(r);
3381
3382 if (rt.IsZero()) {
3383 table->SetRegCategory(i, r, kRegZero);
3384 } else if (rt.IsCategory1Types()) {
3385 table->SetRegCategory(i, r, kRegCat1nr);
3386 } else if (rt.IsCategory2Types()) {
3387 table->SetRegCategory(i, r, kRegCat2);
3388 } else if (rt.IsReferenceTypes()) {
3389 table->SetRegCategory(i, r, kRegObject);
3390 } else {
3391 table->SetRegCategory(i, r, kRegUnknown);
3392 }
Logan Chienfca7e872011-12-20 20:08:22 +08003393 }
3394 }
3395 }
3396 }
3397
3398 return table.release();
3399}
Logan Chiendd361c92012-04-10 23:40:37 +08003400
Ian Rogers776ac1f2012-04-13 23:36:36 -07003401Mutex* MethodVerifier::inferred_reg_category_maps_lock_ = NULL;
3402MethodVerifier::InferredRegCategoryMapTable* MethodVerifier::inferred_reg_category_maps_ = NULL;
Logan Chiendd361c92012-04-10 23:40:37 +08003403
Ian Rogers776ac1f2012-04-13 23:36:36 -07003404void MethodVerifier::InitInferredRegCategoryMaps() {
Logan Chiendd361c92012-04-10 23:40:37 +08003405 inferred_reg_category_maps_lock_ = new Mutex("verifier GC maps lock");
3406 MutexLock mu(*inferred_reg_category_maps_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07003407 inferred_reg_category_maps_ = new MethodVerifier::InferredRegCategoryMapTable;
Logan Chiendd361c92012-04-10 23:40:37 +08003408}
3409
Ian Rogers776ac1f2012-04-13 23:36:36 -07003410void MethodVerifier::DeleteInferredRegCategoryMaps() {
Logan Chiendd361c92012-04-10 23:40:37 +08003411 {
3412 MutexLock mu(*inferred_reg_category_maps_lock_);
3413 STLDeleteValues(inferred_reg_category_maps_);
3414 delete inferred_reg_category_maps_;
3415 inferred_reg_category_maps_ = NULL;
3416 }
3417 delete inferred_reg_category_maps_lock_;
3418 inferred_reg_category_maps_lock_ = NULL;
3419}
3420
3421
Ian Rogers776ac1f2012-04-13 23:36:36 -07003422void MethodVerifier::SetInferredRegCategoryMap(Compiler::MethodReference ref,
3423 const InferredRegCategoryMap& inferred_reg_category_map) {
Logan Chiendd361c92012-04-10 23:40:37 +08003424 MutexLock mu(*inferred_reg_category_maps_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07003425 const InferredRegCategoryMap* existing_inferred_reg_category_map = GetInferredRegCategoryMap(ref);
Logan Chiendd361c92012-04-10 23:40:37 +08003426
3427 if (existing_inferred_reg_category_map != NULL) {
3428 CHECK(*existing_inferred_reg_category_map == inferred_reg_category_map);
3429 delete existing_inferred_reg_category_map;
3430 }
3431
Ian Rogers776ac1f2012-04-13 23:36:36 -07003432 inferred_reg_category_maps_->Put(ref, &inferred_reg_category_map);
Logan Chiendd361c92012-04-10 23:40:37 +08003433 CHECK(GetInferredRegCategoryMap(ref) != NULL);
3434}
3435
3436const InferredRegCategoryMap*
Ian Rogers776ac1f2012-04-13 23:36:36 -07003437MethodVerifier::GetInferredRegCategoryMap(Compiler::MethodReference ref) {
Logan Chiendd361c92012-04-10 23:40:37 +08003438 MutexLock mu(*inferred_reg_category_maps_lock_);
3439
3440 InferredRegCategoryMapTable::const_iterator it =
3441 inferred_reg_category_maps_->find(ref);
3442
3443 if (it == inferred_reg_category_maps_->end()) {
3444 return NULL;
3445 }
3446 CHECK(it->second != NULL);
3447 return it->second;
3448}
Logan Chienfca7e872011-12-20 20:08:22 +08003449#endif
3450
Ian Rogersd81871c2011-10-03 13:57:23 -07003451} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003452} // namespace art