blob: 30bee66d1de26d05ae32a02d50a8292d030381c0 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Ian Rogers776ac1f2012-04-13 23:36:36 -070017#include "method_verifier.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Brian Carlstrom1f870082011-08-23 16:02:11 -070021#include "class_linker.h"
Brian Carlstrome7d856b2012-01-11 18:10:55 -080022#include "compiler.h"
jeffhaob4df5142011-09-19 20:25:32 -070023#include "dex_cache.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070024#include "dex_file.h"
25#include "dex_instruction.h"
26#include "dex_instruction_visitor.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070027#include "gc_map.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070028#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070029#include "leb128.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070030#include "logging.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080031#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070032#include "runtime.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070033#include "stringpiece.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070034
Logan Chienfca7e872011-12-20 20:08:22 +080035#if defined(ART_USE_LLVM_COMPILER)
36#include "compiler_llvm/backend_types.h"
37#include "compiler_llvm/inferred_reg_category_map.h"
38using namespace art::compiler_llvm;
39#endif
40
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -070041#if defined(ART_USE_GREENLAND_COMPILER)
42#include "greenland/backend_types.h"
43#include "greenland/inferred_reg_category_map.h"
44using namespace art::greenland;
45#endif
46
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070047namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070048namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070049
Ian Rogers2c8a8572011-10-24 17:11:36 -070050static const bool gDebugVerify = false;
51
Ian Rogers776ac1f2012-04-13 23:36:36 -070052class InsnFlags {
53 public:
54 InsnFlags() : length_(0), flags_(0) {}
55
56 void SetLengthInCodeUnits(size_t length) {
57 CHECK_LT(length, 65536u);
58 length_ = length;
59 }
60 size_t GetLengthInCodeUnits() {
61 return length_;
62 }
63 bool IsOpcode() const {
64 return length_ != 0;
65 }
66
67 void SetInTry() {
68 flags_ |= 1 << kInTry;
69 }
70 void ClearInTry() {
71 flags_ &= ~(1 << kInTry);
72 }
73 bool IsInTry() const {
74 return (flags_ & (1 << kInTry)) != 0;
75 }
76
77 void SetBranchTarget() {
78 flags_ |= 1 << kBranchTarget;
79 }
80 void ClearBranchTarget() {
81 flags_ &= ~(1 << kBranchTarget);
82 }
83 bool IsBranchTarget() const {
84 return (flags_ & (1 << kBranchTarget)) != 0;
85 }
86
87 void SetGcPoint() {
88 flags_ |= 1 << kGcPoint;
89 }
90 void ClearGcPoint() {
91 flags_ &= ~(1 << kGcPoint);
92 }
93 bool IsGcPoint() const {
94 return (flags_ & (1 << kGcPoint)) != 0;
95 }
96
97 void SetVisited() {
98 flags_ |= 1 << kVisited;
99 }
100 void ClearVisited() {
101 flags_ &= ~(1 << kVisited);
102 }
103 bool IsVisited() const {
104 return (flags_ & (1 << kVisited)) != 0;
105 }
106
107 void SetChanged() {
108 flags_ |= 1 << kChanged;
109 }
110 void ClearChanged() {
111 flags_ &= ~(1 << kChanged);
112 }
113 bool IsChanged() const {
114 return (flags_ & (1 << kChanged)) != 0;
115 }
116
117 bool IsVisitedOrChanged() const {
118 return IsVisited() || IsChanged();
119 }
120
121 std::string Dump() {
122 char encoding[6];
123 if (!IsOpcode()) {
124 strncpy(encoding, "XXXXX", sizeof(encoding));
125 } else {
126 strncpy(encoding, "-----", sizeof(encoding));
127 if (IsInTry()) encoding[kInTry] = 'T';
128 if (IsBranchTarget()) encoding[kBranchTarget] = 'B';
129 if (IsGcPoint()) encoding[kGcPoint] = 'G';
130 if (IsVisited()) encoding[kVisited] = 'V';
131 if (IsChanged()) encoding[kChanged] = 'C';
132 }
133 return std::string(encoding);
134 }
Elliott Hughesa21039c2012-06-21 12:09:25 -0700135
Ian Rogers776ac1f2012-04-13 23:36:36 -0700136 private:
137 enum {
138 kInTry,
139 kBranchTarget,
140 kGcPoint,
141 kVisited,
142 kChanged,
143 };
144
145 // Size of instruction in code units
146 uint16_t length_;
147 uint8_t flags_;
Ian Rogers84fa0742011-10-25 18:13:30 -0700148};
Ian Rogersd81871c2011-10-03 13:57:23 -0700149
Ian Rogersd81871c2011-10-03 13:57:23 -0700150void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
151 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -0700152 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700153 DCHECK_GT(insns_size, 0U);
154
155 for (uint32_t i = 0; i < insns_size; i++) {
156 bool interesting = false;
157 switch (mode) {
158 case kTrackRegsAll:
159 interesting = flags[i].IsOpcode();
160 break;
161 case kTrackRegsGcPoints:
162 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
163 break;
164 case kTrackRegsBranches:
165 interesting = flags[i].IsBranchTarget();
166 break;
167 default:
168 break;
169 }
170 if (interesting) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700171 pc_to_register_line_.Put(i, new RegisterLine(registers_size, verifier));
Ian Rogersd81871c2011-10-03 13:57:23 -0700172 }
173 }
174}
175
jeffhaof1e6b7c2012-06-05 18:33:30 -0700176MethodVerifier::FailureKind MethodVerifier::VerifyClass(const Class* klass, std::string& error) {
jeffhaobdb76512011-09-07 11:43:16 -0700177 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700178 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700179 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700180 Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -0800181 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800182 error = "Verifier rejected class ";
183 error += PrettyDescriptor(klass);
184 error += " that has no super class";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700185 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -0700186 }
Ian Rogers1c5eb702012-02-01 09:18:34 -0800187 if (super != NULL && super->IsFinal()) {
188 error = "Verifier rejected class ";
189 error += PrettyDescriptor(klass);
190 error += " that attempts to sub-class final class ";
191 error += PrettyDescriptor(super);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700192 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -0700193 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700194 ClassHelper kh(klass);
195 const DexFile& dex_file = kh.GetDexFile();
196 uint32_t class_def_idx;
197 if (!dex_file.FindClassDefIndex(kh.GetDescriptor(), class_def_idx)) {
198 error = "Verifier rejected class ";
199 error += PrettyDescriptor(klass);
200 error += " that isn't present in dex file ";
201 error += dex_file.GetLocation();
jeffhaof1e6b7c2012-06-05 18:33:30 -0700202 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700203 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700204 return VerifyClass(&dex_file, kh.GetDexCache(), klass->GetClassLoader(), class_def_idx, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700205}
206
Ian Rogers365c1022012-06-22 15:05:28 -0700207MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
208 DexCache* dex_cache, ClassLoader* class_loader, uint32_t class_def_idx, std::string& error) {
jeffhaof56197c2012-03-05 18:01:54 -0800209 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
210 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700211 if (class_data == NULL) {
212 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700213 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700214 }
jeffhaof56197c2012-03-05 18:01:54 -0800215 ClassDataItemIterator it(*dex_file, class_data);
216 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
217 it.Next();
218 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700219 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700220 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700221 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaof56197c2012-03-05 18:01:54 -0800222 while (it.HasNextDirectMethod()) {
223 uint32_t method_idx = it.GetMemberIndex();
Ian Rogers08f753d2012-08-24 14:35:25 -0700224 InvokeType type = it.GetMethodInvokeType(class_def);
jeffhaoc0228b82012-08-29 18:15:05 -0700225 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700226 if (method == NULL) {
227 DCHECK(Thread::Current()->IsExceptionPending());
228 // We couldn't resolve the method, but continue regardless.
229 Thread::Current()->ClearException();
230 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700231 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
232 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags());
233 if (result != kNoFailure) {
234 if (result == kHardFailure) {
235 hard_fail = true;
236 if (error_count > 0) {
237 error += "\n";
238 }
239 error = "Verifier rejected class ";
240 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
241 error += " due to bad method ";
242 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700243 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700244 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800245 }
246 it.Next();
247 }
248 while (it.HasNextVirtualMethod()) {
249 uint32_t method_idx = it.GetMemberIndex();
Ian Rogers08f753d2012-08-24 14:35:25 -0700250 InvokeType type = it.GetMethodInvokeType(class_def);
jeffhaoc0228b82012-08-29 18:15:05 -0700251 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700252 if (method == NULL) {
253 DCHECK(Thread::Current()->IsExceptionPending());
254 // We couldn't resolve the method, but continue regardless.
255 Thread::Current()->ClearException();
256 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700257 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
258 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags());
259 if (result != kNoFailure) {
260 if (result == kHardFailure) {
261 hard_fail = true;
262 if (error_count > 0) {
263 error += "\n";
264 }
265 error = "Verifier rejected class ";
266 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
267 error += " due to bad method ";
268 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700269 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700270 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800271 }
272 it.Next();
273 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700274 if (error_count == 0) {
275 return kNoFailure;
276 } else {
277 return hard_fail ? kHardFailure : kSoftFailure;
278 }
jeffhaof56197c2012-03-05 18:01:54 -0800279}
280
jeffhaof1e6b7c2012-06-05 18:33:30 -0700281MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx, const DexFile* dex_file,
Ian Rogers365c1022012-06-22 15:05:28 -0700282 DexCache* dex_cache, ClassLoader* class_loader, uint32_t class_def_idx,
jeffhaof1e6b7c2012-06-05 18:33:30 -0700283 const DexFile::CodeItem* code_item, Method* method, uint32_t method_access_flags) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700284 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item, method_idx,
285 method, method_access_flags);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700286 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700287 // Verification completed, however failures may be pending that didn't cause the verification
288 // to hard fail.
Ian Rogerse551e952012-06-03 22:59:14 -0700289 CHECK(!verifier.have_pending_hard_failure_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700290 if (verifier.failures_.size() != 0) {
291 verifier.DumpFailures(LOG(INFO) << "Soft verification failures in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700292 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof1e6b7c2012-06-05 18:33:30 -0700293 return kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800294 }
295 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700296 // Bad method data.
297 CHECK_NE(verifier.failures_.size(), 0U);
298 CHECK(verifier.have_pending_hard_failure_);
299 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700300 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800301 if (gDebugVerify) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700302 std::cout << "\n" << verifier.info_messages_.str();
jeffhaof56197c2012-03-05 18:01:54 -0800303 verifier.Dump(std::cout);
304 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700305 return kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800306 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700307 return kNoFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800308}
309
Ian Rogersad0b3a32012-04-16 14:50:24 -0700310void MethodVerifier::VerifyMethodAndDump(Method* method) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800311 CHECK(method != NULL);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700312 MethodHelper mh(method);
313 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
314 mh.GetClassDefIndex(), mh.GetCodeItem(), method->GetDexMethodIndex(),
315 method, method->GetAccessFlags());
316 verifier.Verify();
Elliott Hughesc073b072012-05-24 19:29:17 -0700317 verifier.DumpFailures(LOG(INFO) << "Dump of method " << PrettyMethod(method) << "\n")
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700318 << verifier.info_messages_.str() << MutatorLockedDumpable<MethodVerifier>(verifier);
jeffhaoba5ebb92011-08-25 17:24:37 -0700319}
320
Ian Rogers776ac1f2012-04-13 23:36:36 -0700321MethodVerifier::MethodVerifier(const DexFile* dex_file, DexCache* dex_cache,
Ian Rogers365c1022012-06-22 15:05:28 -0700322 ClassLoader* class_loader, uint32_t class_def_idx, const DexFile::CodeItem* code_item,
Ian Rogersad0b3a32012-04-16 14:50:24 -0700323 uint32_t method_idx, Method* method, uint32_t method_access_flags)
jeffhaof56197c2012-03-05 18:01:54 -0800324 : work_insn_idx_(-1),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700325 method_idx_(method_idx),
326 foo_method_(method),
327 method_access_flags_(method_access_flags),
jeffhaof56197c2012-03-05 18:01:54 -0800328 dex_file_(dex_file),
329 dex_cache_(dex_cache),
330 class_loader_(class_loader),
331 class_def_idx_(class_def_idx),
332 code_item_(code_item),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700333 interesting_dex_pc_(-1),
334 monitor_enter_dex_pcs_(NULL),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700335 have_pending_hard_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800336 new_instance_count_(0),
337 monitor_enter_count_(0) {
338}
339
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700340void MethodVerifier::FindLocksAtDexPc(Method* m, uint32_t dex_pc, std::vector<uint32_t>& monitor_enter_dex_pcs) {
341 MethodHelper mh(m);
342 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
343 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
344 m, m->GetAccessFlags());
345 verifier.interesting_dex_pc_ = dex_pc;
346 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
347 verifier.FindLocksAtDexPc();
348}
349
350void MethodVerifier::FindLocksAtDexPc() {
351 CHECK(monitor_enter_dex_pcs_ != NULL);
352 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
353
354 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
355 // verification. In practice, the phase we want relies on data structures set up by all the
356 // earlier passes, so we just run the full method verification and bail out early when we've
357 // got what we wanted.
358 Verify();
359}
360
Ian Rogersad0b3a32012-04-16 14:50:24 -0700361bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700362 // If there aren't any instructions, make sure that's expected, then exit successfully.
363 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700364 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700365 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700366 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700367 } else {
368 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700369 }
jeffhaobdb76512011-09-07 11:43:16 -0700370 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700371 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
372 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700373 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
374 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700375 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700376 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700377 // Allocate and initialize an array to hold instruction data.
378 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
379 // Run through the instructions and see if the width checks out.
380 bool result = ComputeWidthsAndCountOps();
381 // Flag instructions guarded by a "try" block and check exception handlers.
382 result = result && ScanTryCatchBlocks();
383 // Perform static instruction verification.
384 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700385 // Perform code-flow analysis and return.
386 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700387}
388
Ian Rogers776ac1f2012-04-13 23:36:36 -0700389std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700390 switch (error) {
391 case VERIFY_ERROR_NO_CLASS:
392 case VERIFY_ERROR_NO_FIELD:
393 case VERIFY_ERROR_NO_METHOD:
394 case VERIFY_ERROR_ACCESS_CLASS:
395 case VERIFY_ERROR_ACCESS_FIELD:
396 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700397 case VERIFY_ERROR_INSTANTIATION:
398 case VERIFY_ERROR_CLASS_CHANGE:
jeffhaoe4f0b2a2012-08-30 11:18:57 -0700399 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
400 // class change and instantiation errors into soft verification errors so that we re-verify
401 // at runtime. We may fail to find or to agree on access because of not yet available class
402 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
403 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
404 // paths" that dynamically perform the verification and cause the behavior to be that akin
405 // to an interpreter.
406 error = VERIFY_ERROR_BAD_CLASS_SOFT;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700407 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700408 // Indication that verification should be retried at runtime.
409 case VERIFY_ERROR_BAD_CLASS_SOFT:
410 if (!Runtime::Current()->IsCompiler()) {
411 // It is runtime so hard fail.
412 have_pending_hard_failure_ = true;
413 }
414 break;
jeffhaod5347e02012-03-22 17:25:05 -0700415 // Hard verification failures at compile time will still fail at runtime, so the class is
416 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700417 case VERIFY_ERROR_BAD_CLASS_HARD: {
418 if (Runtime::Current()->IsCompiler()) {
jeffhaof56197c2012-03-05 18:01:54 -0800419 Compiler::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800420 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800421 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700422 have_pending_hard_failure_ = true;
423 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800424 }
425 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700426 failures_.push_back(error);
427 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(method_idx_, *dex_file_).c_str(),
428 work_insn_idx_));
429 std::ostringstream* failure_message = new std::ostringstream(location);
430 failure_messages_.push_back(failure_message);
431 return *failure_message;
432}
433
434void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
435 size_t failure_num = failure_messages_.size();
436 DCHECK_NE(failure_num, 0U);
437 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
438 prepend += last_fail_message->str();
439 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
440 delete last_fail_message;
441}
442
443void MethodVerifier::AppendToLastFailMessage(std::string append) {
444 size_t failure_num = failure_messages_.size();
445 DCHECK_NE(failure_num, 0U);
446 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
447 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800448}
449
Ian Rogers776ac1f2012-04-13 23:36:36 -0700450bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700451 const uint16_t* insns = code_item_->insns_;
452 size_t insns_size = code_item_->insns_size_in_code_units_;
453 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700454 size_t new_instance_count = 0;
455 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700456 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700457
Ian Rogersd81871c2011-10-03 13:57:23 -0700458 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700459 Instruction::Code opcode = inst->Opcode();
460 if (opcode == Instruction::NEW_INSTANCE) {
461 new_instance_count++;
462 } else if (opcode == Instruction::MONITOR_ENTER) {
463 monitor_enter_count++;
464 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700465 size_t inst_size = inst->SizeInCodeUnits();
466 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
467 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700468 inst = inst->Next();
469 }
470
Ian Rogersd81871c2011-10-03 13:57:23 -0700471 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700472 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
473 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700474 return false;
475 }
476
Ian Rogersd81871c2011-10-03 13:57:23 -0700477 new_instance_count_ = new_instance_count;
478 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700479 return true;
480}
481
Ian Rogers776ac1f2012-04-13 23:36:36 -0700482bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700483 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700484 if (tries_size == 0) {
485 return true;
486 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700487 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700488 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700489
490 for (uint32_t idx = 0; idx < tries_size; idx++) {
491 const DexFile::TryItem* try_item = &tries[idx];
492 uint32_t start = try_item->start_addr_;
493 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700494 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700495 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
496 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700497 return false;
498 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700499 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700500 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700501 return false;
502 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700503 for (uint32_t dex_pc = start; dex_pc < end;
504 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
505 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700506 }
507 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800508 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700509 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700510 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700511 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700512 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700513 CatchHandlerIterator iterator(handlers_ptr);
514 for (; iterator.HasNext(); iterator.Next()) {
515 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700516 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700517 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700518 return false;
519 }
jeffhao60f83e32012-02-13 17:16:30 -0800520 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
521 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700522 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700523 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800524 return false;
525 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700526 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700527 // Ensure exception types are resolved so that they don't need resolution to be delivered,
528 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700529 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
jeffhaof56197c2012-03-05 18:01:54 -0800530 Class* exception_type = linker->ResolveType(*dex_file_, iterator.GetHandlerTypeIndex(),
531 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700532 if (exception_type == NULL) {
533 DCHECK(Thread::Current()->IsExceptionPending());
534 Thread::Current()->ClearException();
535 }
536 }
jeffhaobdb76512011-09-07 11:43:16 -0700537 }
Ian Rogers0571d352011-11-03 19:51:38 -0700538 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700539 }
jeffhaobdb76512011-09-07 11:43:16 -0700540 return true;
541}
542
Ian Rogers776ac1f2012-04-13 23:36:36 -0700543bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700544 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700545
Ian Rogersd81871c2011-10-03 13:57:23 -0700546 /* Flag the start of the method as a branch target. */
547 insn_flags_[0].SetBranchTarget();
548
549 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700550 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700551 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700552 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700553 return false;
554 }
555 /* Flag instructions that are garbage collection points */
556 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
557 insn_flags_[dex_pc].SetGcPoint();
558 }
559 dex_pc += inst->SizeInCodeUnits();
560 inst = inst->Next();
561 }
562 return true;
563}
564
Ian Rogers776ac1f2012-04-13 23:36:36 -0700565bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800566 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700567 bool result = true;
568 switch (inst->GetVerifyTypeArgumentA()) {
569 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800570 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700571 break;
572 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800573 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700574 break;
575 }
576 switch (inst->GetVerifyTypeArgumentB()) {
577 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800578 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700579 break;
580 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800581 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700582 break;
583 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800584 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700585 break;
586 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800587 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700588 break;
589 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800590 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700591 break;
592 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800593 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700594 break;
595 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800596 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700597 break;
598 }
599 switch (inst->GetVerifyTypeArgumentC()) {
600 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800601 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700602 break;
603 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800604 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700605 break;
606 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800607 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700608 break;
609 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800610 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700611 break;
612 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800613 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700614 break;
615 }
616 switch (inst->GetVerifyExtraFlags()) {
617 case Instruction::kVerifyArrayData:
618 result = result && CheckArrayData(code_offset);
619 break;
620 case Instruction::kVerifyBranchTarget:
621 result = result && CheckBranchTarget(code_offset);
622 break;
623 case Instruction::kVerifySwitchTargets:
624 result = result && CheckSwitchTargets(code_offset);
625 break;
626 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800627 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700628 break;
629 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800630 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700631 break;
632 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700633 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700634 result = false;
635 break;
636 }
637 return result;
638}
639
Ian Rogers776ac1f2012-04-13 23:36:36 -0700640bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700641 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700642 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
643 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700644 return false;
645 }
646 return true;
647}
648
Ian Rogers776ac1f2012-04-13 23:36:36 -0700649bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700650 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700651 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
652 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700653 return false;
654 }
655 return true;
656}
657
Ian Rogers776ac1f2012-04-13 23:36:36 -0700658bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700659 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700660 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
661 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700662 return false;
663 }
664 return true;
665}
666
Ian Rogers776ac1f2012-04-13 23:36:36 -0700667bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700668 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700669 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
670 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700671 return false;
672 }
673 return true;
674}
675
Ian Rogers776ac1f2012-04-13 23:36:36 -0700676bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700677 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700678 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
679 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700680 return false;
681 }
682 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700683 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700684 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700685 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700686 return false;
687 }
688 return true;
689}
690
Ian Rogers776ac1f2012-04-13 23:36:36 -0700691bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700692 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700693 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
694 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700695 return false;
696 }
697 return true;
698}
699
Ian Rogers776ac1f2012-04-13 23:36:36 -0700700bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700701 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700702 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
703 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700704 return false;
705 }
706 return true;
707}
708
Ian Rogers776ac1f2012-04-13 23:36:36 -0700709bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700710 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700711 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
712 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700713 return false;
714 }
715 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700716 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700717 const char* cp = descriptor;
718 while (*cp++ == '[') {
719 bracket_count++;
720 }
721 if (bracket_count == 0) {
722 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700723 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700724 return false;
725 } else if (bracket_count > 255) {
726 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700727 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700728 return false;
729 }
730 return true;
731}
732
Ian Rogers776ac1f2012-04-13 23:36:36 -0700733bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700734 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
735 const uint16_t* insns = code_item_->insns_ + cur_offset;
736 const uint16_t* array_data;
737 int32_t array_data_offset;
738
739 DCHECK_LT(cur_offset, insn_count);
740 /* make sure the start of the array data table is in range */
741 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
742 if ((int32_t) cur_offset + array_data_offset < 0 ||
743 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700744 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
745 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700746 return false;
747 }
748 /* offset to array data table is a relative branch-style offset */
749 array_data = insns + array_data_offset;
750 /* make sure the table is 32-bit aligned */
751 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700752 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
753 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700754 return false;
755 }
756 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700757 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700758 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
759 /* make sure the end of the switch is in range */
760 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700761 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
762 << ", data offset " << array_data_offset << ", end "
763 << cur_offset + array_data_offset + table_size
764 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700765 return false;
766 }
767 return true;
768}
769
Ian Rogers776ac1f2012-04-13 23:36:36 -0700770bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700771 int32_t offset;
772 bool isConditional, selfOkay;
773 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
774 return false;
775 }
776 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700777 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 -0700778 return false;
779 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700780 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
781 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700782 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700783 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700784 return false;
785 }
786 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
787 int32_t abs_offset = cur_offset + offset;
788 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700789 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700790 << reinterpret_cast<void*>(abs_offset) << ") at "
791 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700792 return false;
793 }
794 insn_flags_[abs_offset].SetBranchTarget();
795 return true;
796}
797
Ian Rogers776ac1f2012-04-13 23:36:36 -0700798bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700799 bool* selfOkay) {
800 const uint16_t* insns = code_item_->insns_ + cur_offset;
801 *pConditional = false;
802 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700803 switch (*insns & 0xff) {
804 case Instruction::GOTO:
805 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700806 break;
807 case Instruction::GOTO_32:
808 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700809 *selfOkay = true;
810 break;
811 case Instruction::GOTO_16:
812 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700813 break;
814 case Instruction::IF_EQ:
815 case Instruction::IF_NE:
816 case Instruction::IF_LT:
817 case Instruction::IF_GE:
818 case Instruction::IF_GT:
819 case Instruction::IF_LE:
820 case Instruction::IF_EQZ:
821 case Instruction::IF_NEZ:
822 case Instruction::IF_LTZ:
823 case Instruction::IF_GEZ:
824 case Instruction::IF_GTZ:
825 case Instruction::IF_LEZ:
826 *pOffset = (int16_t) insns[1];
827 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700828 break;
829 default:
830 return false;
831 break;
832 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700833 return true;
834}
835
Ian Rogers776ac1f2012-04-13 23:36:36 -0700836bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700837 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700838 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700839 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700840 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700841 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
842 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700843 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
844 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700845 return false;
846 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700847 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700848 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700849 /* make sure the table is 32-bit aligned */
850 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700851 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
852 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700853 return false;
854 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700855 uint32_t switch_count = switch_insns[1];
856 int32_t keys_offset, targets_offset;
857 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700858 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
859 /* 0=sig, 1=count, 2/3=firstKey */
860 targets_offset = 4;
861 keys_offset = -1;
862 expected_signature = Instruction::kPackedSwitchSignature;
863 } else {
864 /* 0=sig, 1=count, 2..count*2 = keys */
865 keys_offset = 2;
866 targets_offset = 2 + 2 * switch_count;
867 expected_signature = Instruction::kSparseSwitchSignature;
868 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700869 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700870 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700871 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
872 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700873 return false;
874 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700875 /* make sure the end of the switch is in range */
876 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700877 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
878 << switch_offset << ", end "
879 << (cur_offset + switch_offset + table_size)
880 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700881 return false;
882 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700883 /* for a sparse switch, verify the keys are in ascending order */
884 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700885 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
886 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700887 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
888 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
889 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700890 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
891 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700892 return false;
893 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700894 last_key = key;
895 }
896 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700897 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700898 for (uint32_t targ = 0; targ < switch_count; targ++) {
899 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
900 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
901 int32_t abs_offset = cur_offset + offset;
902 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700903 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700904 << reinterpret_cast<void*>(abs_offset) << ") at "
905 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700906 return false;
907 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700908 insn_flags_[abs_offset].SetBranchTarget();
909 }
910 return true;
911}
912
Ian Rogers776ac1f2012-04-13 23:36:36 -0700913bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700914 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700915 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700916 return false;
917 }
918 uint16_t registers_size = code_item_->registers_size_;
919 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800920 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700921 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
922 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700923 return false;
924 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700925 }
926
927 return true;
928}
929
Ian Rogers776ac1f2012-04-13 23:36:36 -0700930bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700931 uint16_t registers_size = code_item_->registers_size_;
932 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
933 // integer overflow when adding them here.
934 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700935 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
936 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700937 return false;
938 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700939 return true;
940}
941
Brian Carlstrom75412882012-01-18 01:26:54 -0800942const std::vector<uint8_t>* CreateLengthPrefixedGcMap(const std::vector<uint8_t>& gc_map) {
943 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
944 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
945 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
946 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
947 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
948 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
949 gc_map.begin(),
950 gc_map.end());
951 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
952 DCHECK_EQ(gc_map.size(),
953 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
954 (length_prefixed_gc_map->at(1) << 16) |
955 (length_prefixed_gc_map->at(2) << 8) |
956 (length_prefixed_gc_map->at(3) << 0)));
957 return length_prefixed_gc_map;
958}
959
Ian Rogers776ac1f2012-04-13 23:36:36 -0700960bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700961 uint16_t registers_size = code_item_->registers_size_;
962 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -0700963
Ian Rogersd81871c2011-10-03 13:57:23 -0700964 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -0800965 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
966 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700967 }
968 /* Create and initialize table holding register status */
Elliott Hughes460384f2012-04-04 16:53:10 -0700969 reg_table_.Init(kTrackRegsGcPoints, insn_flags_.get(), insns_size, registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -0700970
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 work_line_.reset(new RegisterLine(registers_size, this));
972 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -0700973
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 /* Initialize register types of method arguments. */
975 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700976 DCHECK_NE(failures_.size(), 0U);
977 std::string prepend("Bad signature in ");
978 prepend += PrettyMethod(method_idx_, *dex_file_);
979 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 return false;
981 }
982 /* Perform code flow verification. */
983 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700984 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700985 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700986 }
987
TDYa127b2eb5c12012-05-24 15:52:10 -0700988 Compiler::MethodReference ref(dex_file_, method_idx_);
989
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700990#if !defined(ART_USE_LLVM_COMPILER) && !defined(ART_USE_GREENLAND_COMPILER)
TDYa127b2eb5c12012-05-24 15:52:10 -0700991
Ian Rogersd81871c2011-10-03 13:57:23 -0700992 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -0800993 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
994 if (map.get() == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700995 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700996 return false; // Not a real failure, but a failure to encode
997 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700998#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800999 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -07001000#endif
Brian Carlstrom75412882012-01-18 01:26:54 -08001001 const std::vector<uint8_t>* gc_map = CreateLengthPrefixedGcMap(*(map.get()));
Ian Rogers776ac1f2012-04-13 23:36:36 -07001002 verifier::MethodVerifier::SetGcMap(ref, *gc_map);
Logan Chienfca7e872011-12-20 20:08:22 +08001003
Ian Rogersad0b3a32012-04-16 14:50:24 -07001004 if (foo_method_ != NULL) {
1005 foo_method_->SetGcMap(&gc_map->at(0));
1006 }
Logan Chiendd361c92012-04-10 23:40:37 +08001007
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -07001008#else // defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Logan Chienfca7e872011-12-20 20:08:22 +08001009 /* Generate Inferred Register Category for LLVM-based Code Generator */
1010 const InferredRegCategoryMap* table = GenerateInferredRegCategoryMap();
Ian Rogers776ac1f2012-04-13 23:36:36 -07001011 verifier::MethodVerifier::SetInferredRegCategoryMap(ref, *table);
TDYa127b2eb5c12012-05-24 15:52:10 -07001012
Logan Chienfca7e872011-12-20 20:08:22 +08001013#endif
1014
jeffhaobdb76512011-09-07 11:43:16 -07001015 return true;
1016}
1017
Ian Rogersad0b3a32012-04-16 14:50:24 -07001018std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1019 DCHECK_EQ(failures_.size(), failure_messages_.size());
1020 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001021 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001022 }
1023 return os;
1024}
1025
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001026extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
1027 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001028 v->Dump(std::cerr);
1029}
1030
Ian Rogers776ac1f2012-04-13 23:36:36 -07001031void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001032 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001033 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001034 return;
jeffhaobdb76512011-09-07 11:43:16 -07001035 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001036 DCHECK(code_item_ != NULL);
1037 const Instruction* inst = Instruction::At(code_item_->insns_);
1038 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1039 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Elliott Hughesaa6e1cd2012-01-18 19:26:06 -08001040 os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
Elliott Hughesc073b072012-05-24 19:29:17 -07001041 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001042 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1043 if (reg_line != NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001044 os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001045 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001046 inst = inst->Next();
1047 }
jeffhaobdb76512011-09-07 11:43:16 -07001048}
1049
Ian Rogersd81871c2011-10-03 13:57:23 -07001050static bool IsPrimitiveDescriptor(char descriptor) {
1051 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001052 case 'I':
1053 case 'C':
1054 case 'S':
1055 case 'B':
1056 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001057 case 'F':
1058 case 'D':
1059 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001060 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001061 default:
1062 return false;
1063 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001064}
1065
Ian Rogers776ac1f2012-04-13 23:36:36 -07001066bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001067 RegisterLine* reg_line = reg_table_.GetLine(0);
1068 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1069 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001070
Ian Rogersd81871c2011-10-03 13:57:23 -07001071 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1072 //Include the "this" pointer.
1073 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001074 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001075 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1076 // argument as uninitialized. This restricts field access until the superclass constructor is
1077 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001078 const RegType& declaring_class = GetDeclaringClass();
1079 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001080 reg_line->SetRegisterType(arg_start + cur_arg,
1081 reg_types_.UninitializedThisArgument(declaring_class));
1082 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001083 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001084 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001085 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001086 }
1087
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001088 const DexFile::ProtoId& proto_id =
Ian Rogersad0b3a32012-04-16 14:50:24 -07001089 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001090 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001091
1092 for (; iterator.HasNext(); iterator.Next()) {
1093 const char* descriptor = iterator.GetDescriptor();
1094 if (descriptor == NULL) {
1095 LOG(FATAL) << "Null descriptor";
1096 }
1097 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001098 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1099 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001100 return false;
1101 }
1102 switch (descriptor[0]) {
1103 case 'L':
1104 case '[':
1105 // We assume that reference arguments are initialized. The only way it could be otherwise
1106 // (assuming the caller was verified) is if the current method is <init>, but in that case
1107 // it's effectively considered initialized the instant we reach here (in the sense that we
1108 // can return without doing anything or call virtual methods).
1109 {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001110 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001111 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001112 }
1113 break;
1114 case 'Z':
1115 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1116 break;
1117 case 'C':
1118 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1119 break;
1120 case 'B':
1121 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1122 break;
1123 case 'I':
1124 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1125 break;
1126 case 'S':
1127 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1128 break;
1129 case 'F':
1130 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1131 break;
1132 case 'J':
1133 case 'D': {
1134 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1135 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1136 cur_arg++;
1137 break;
1138 }
1139 default:
jeffhaod5347e02012-03-22 17:25:05 -07001140 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001141 return false;
1142 }
1143 cur_arg++;
1144 }
1145 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001146 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001147 return false;
1148 }
1149 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1150 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1151 // format. Only major difference from the method argument format is that 'V' is supported.
1152 bool result;
1153 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1154 result = descriptor[1] == '\0';
1155 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1156 size_t i = 0;
1157 do {
1158 i++;
1159 } while (descriptor[i] == '['); // process leading [
1160 if (descriptor[i] == 'L') { // object array
1161 do {
1162 i++; // find closing ;
1163 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1164 result = descriptor[i] == ';';
1165 } else { // primitive array
1166 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1167 }
1168 } else if (descriptor[0] == 'L') {
1169 // could be more thorough here, but shouldn't be required
1170 size_t i = 0;
1171 do {
1172 i++;
1173 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1174 result = descriptor[i] == ';';
1175 } else {
1176 result = false;
1177 }
1178 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001179 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1180 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001181 }
1182 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001183}
1184
Ian Rogers776ac1f2012-04-13 23:36:36 -07001185bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001186 const uint16_t* insns = code_item_->insns_;
1187 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001188
jeffhaobdb76512011-09-07 11:43:16 -07001189 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001190 insn_flags_[0].SetChanged();
1191 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001192
jeffhaobdb76512011-09-07 11:43:16 -07001193 /* Continue until no instructions are marked "changed". */
1194 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001195 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1196 uint32_t insn_idx = start_guess;
1197 for (; insn_idx < insns_size; insn_idx++) {
1198 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001199 break;
1200 }
jeffhaobdb76512011-09-07 11:43:16 -07001201 if (insn_idx == insns_size) {
1202 if (start_guess != 0) {
1203 /* try again, starting from the top */
1204 start_guess = 0;
1205 continue;
1206 } else {
1207 /* all flags are clear */
1208 break;
1209 }
1210 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001211 // We carry the working set of registers from instruction to instruction. If this address can
1212 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1213 // "changed" flags, we need to load the set of registers from the table.
1214 // Because we always prefer to continue on to the next instruction, we should never have a
1215 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1216 // target.
1217 work_insn_idx_ = insn_idx;
1218 if (insn_flags_[insn_idx].IsBranchTarget()) {
1219 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001220 } else {
1221#ifndef NDEBUG
1222 /*
1223 * Sanity check: retrieve the stored register line (assuming
1224 * a full table) and make sure it actually matches.
1225 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001226 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1227 if (register_line != NULL) {
1228 if (work_line_->CompareLine(register_line) != 0) {
1229 Dump(std::cout);
1230 std::cout << info_messages_.str();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001231 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001232 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1233 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001234 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001235 }
jeffhaobdb76512011-09-07 11:43:16 -07001236 }
1237#endif
1238 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001239 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001240 std::string prepend(PrettyMethod(method_idx_, *dex_file_));
1241 prepend += " failed to verify: ";
1242 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001243 return false;
1244 }
jeffhaobdb76512011-09-07 11:43:16 -07001245 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001246 insn_flags_[insn_idx].SetVisited();
1247 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001248 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001249
Ian Rogers1c849e52012-06-28 14:00:33 -07001250 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001251 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001252 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001253 * (besides the wasted space), but it indicates a flaw somewhere
1254 * down the line, possibly in the verifier.
1255 *
1256 * If we've substituted "always throw" instructions into the stream,
1257 * we are almost certainly going to have some dead code.
1258 */
1259 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001260 uint32_t insn_idx = 0;
1261 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001262 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001263 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001264 * may or may not be preceded by a padding NOP (for alignment).
1265 */
1266 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1267 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1268 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001269 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001270 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1271 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1272 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001273 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001274 }
1275
Ian Rogersd81871c2011-10-03 13:57:23 -07001276 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001277 if (dead_start < 0)
1278 dead_start = insn_idx;
1279 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001280 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001281 dead_start = -1;
1282 }
1283 }
1284 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001285 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001286 }
1287 }
jeffhaobdb76512011-09-07 11:43:16 -07001288 return true;
1289}
1290
Ian Rogers776ac1f2012-04-13 23:36:36 -07001291bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001292#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001293 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001294 gDvm.verifierStats.instrsReexamined++;
1295 } else {
1296 gDvm.verifierStats.instrsExamined++;
1297 }
1298#endif
1299
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001300 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1301 // We want the state _before_ the instruction, for the case where the dex pc we're
1302 // interested in is itself a monitor-enter instruction (which is a likely place
1303 // for a thread to be suspended).
1304 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
1305 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1306 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1307 }
1308 }
1309
jeffhaobdb76512011-09-07 11:43:16 -07001310 /*
1311 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001312 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001313 * control to another statement:
1314 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001315 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001316 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001317 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001318 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001319 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001320 * throw an exception that is handled by an encompassing "try"
1321 * block.
1322 *
1323 * We can also return, in which case there is no successor instruction
1324 * from this point.
1325 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001326 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001327 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001328 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1329 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001330 DecodedInstruction dec_insn(inst);
1331 int opcode_flags = Instruction::Flags(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001332
jeffhaobdb76512011-09-07 11:43:16 -07001333 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001334 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001335 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001336 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001337 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1338 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001339 }
jeffhaobdb76512011-09-07 11:43:16 -07001340
1341 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001342 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001343 * can throw an exception, we will copy/merge this into the "catch"
1344 * address rather than work_line, because we don't want the result
1345 * from the "successful" code path (e.g. a check-cast that "improves"
1346 * a type) to be visible to the exception handler.
1347 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001348 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001349 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001350 } else {
1351#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001352 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001353#endif
1354 }
1355
Elliott Hughesadb8c672012-03-06 16:49:32 -08001356 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07001357 case Instruction::NOP:
1358 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001359 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001360 * a signature that looks like a NOP; if we see one of these in
1361 * the course of executing code then we have a problem.
1362 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001363 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001364 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001365 }
1366 break;
1367
1368 case Instruction::MOVE:
1369 case Instruction::MOVE_FROM16:
1370 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001371 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001372 break;
1373 case Instruction::MOVE_WIDE:
1374 case Instruction::MOVE_WIDE_FROM16:
1375 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001376 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07001377 break;
1378 case Instruction::MOVE_OBJECT:
1379 case Instruction::MOVE_OBJECT_FROM16:
1380 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001381 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001382 break;
1383
1384 /*
1385 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001386 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001387 * might want to hold the result in an actual CPU register, so the
1388 * Dalvik spec requires that these only appear immediately after an
1389 * invoke or filled-new-array.
1390 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001391 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001392 * redundant with the reset done below, but it can make the debug info
1393 * easier to read in some cases.)
1394 */
1395 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001396 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07001397 break;
1398 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001399 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001400 break;
1401 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001402 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07001403 break;
1404
Ian Rogersd81871c2011-10-03 13:57:23 -07001405 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001406 /*
jeffhao60f83e32012-02-13 17:16:30 -08001407 * This statement can only appear as the first instruction in an exception handler. We verify
1408 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001409 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001410 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08001411 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001412 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001413 }
jeffhaobdb76512011-09-07 11:43:16 -07001414 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001415 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1416 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001417 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001418 }
jeffhaobdb76512011-09-07 11:43:16 -07001419 }
1420 break;
1421 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001422 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001423 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001424 const RegType& return_type = GetMethodReturnType();
1425 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001426 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001427 } else {
1428 // Compilers may generate synthetic functions that write byte values into boolean fields.
1429 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001430 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001431 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1432 ((return_type.IsBoolean() || return_type.IsByte() ||
1433 return_type.IsShort() || return_type.IsChar()) &&
1434 src_type.IsInteger()));
1435 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001436 bool success =
1437 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
1438 if (!success) {
1439 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001440 }
jeffhaobdb76512011-09-07 11:43:16 -07001441 }
1442 }
1443 break;
1444 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001445 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001446 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001447 const RegType& return_type = GetMethodReturnType();
1448 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001449 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001450 } else {
1451 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001452 bool success = work_line_->VerifyRegisterType(dec_insn.vA, return_type);
1453 if (!success) {
1454 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001455 }
jeffhaobdb76512011-09-07 11:43:16 -07001456 }
1457 }
1458 break;
1459 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001460 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001461 const RegType& return_type = GetMethodReturnType();
1462 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001463 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001464 } else {
1465 /* return_type is the *expected* return type, not register value */
1466 DCHECK(!return_type.IsZero());
1467 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08001468 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07001469 // Disallow returning uninitialized values and verify that the reference in vAA is an
1470 // instance of the "return_type"
1471 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001472 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001473 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001474 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1475 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001476 }
1477 }
1478 }
1479 break;
1480
1481 case Instruction::CONST_4:
1482 case Instruction::CONST_16:
1483 case Instruction::CONST:
1484 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001485 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const((int32_t) dec_insn.vB));
jeffhaobdb76512011-09-07 11:43:16 -07001486 break;
1487 case Instruction::CONST_HIGH16:
1488 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001489 work_line_->SetRegisterType(dec_insn.vA,
1490 reg_types_.FromCat1Const((int32_t) dec_insn.vB << 16));
jeffhaobdb76512011-09-07 11:43:16 -07001491 break;
1492 case Instruction::CONST_WIDE_16:
1493 case Instruction::CONST_WIDE_32:
1494 case Instruction::CONST_WIDE:
1495 case Instruction::CONST_WIDE_HIGH16:
1496 /* could be long or double; resolved upon use */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001497 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07001498 break;
1499 case Instruction::CONST_STRING:
1500 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001501 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001502 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001503 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001504 // Get type from instruction if unresolved then we need an access check
1505 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08001506 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001507 // Register holds class, ie its type is class, on error it will hold Conflict.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001508 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogersad0b3a32012-04-16 14:50:24 -07001509 res_type.IsConflict() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001510 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001511 }
jeffhaobdb76512011-09-07 11:43:16 -07001512 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001513 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001514 break;
1515 case Instruction::MONITOR_EXIT:
1516 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001517 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001518 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001519 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001520 * to the need to handle asynchronous exceptions, a now-deprecated
1521 * feature that Dalvik doesn't support.)
1522 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001523 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001524 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001525 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001526 * structured locking checks are working, the former would have
1527 * failed on the -enter instruction, and the latter is impossible.
1528 *
1529 * This is fortunate, because issue 3221411 prevents us from
1530 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001531 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001532 * some catch blocks (which will show up as "dead" code when
1533 * we skip them here); if we can't, then the code path could be
1534 * "live" so we still need to check it.
1535 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001536 opcode_flags &= ~Instruction::kThrow;
1537 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001538 break;
1539
Ian Rogers28ad40d2011-10-27 15:19:26 -07001540 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001541 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001542 /*
1543 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1544 * could be a "upcast" -- not expected, so we don't try to address it.)
1545 *
1546 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001547 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001548 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001549 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001550 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001551 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001552 if (res_type.IsConflict()) {
1553 DCHECK_NE(failures_.size(), 0U);
1554 if (!is_checkcast) {
1555 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
1556 }
1557 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001558 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001559 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1560 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001561 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001562 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001563 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001564 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001565 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07001566 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001567 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001568 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001569 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001570 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001571 }
jeffhaobdb76512011-09-07 11:43:16 -07001572 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001573 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001574 }
1575 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001576 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001577 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001578 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001579 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001580 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001581 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001582 }
1583 }
1584 break;
1585 }
1586 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001587 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001588 if (res_type.IsConflict()) {
1589 DCHECK_NE(failures_.size(), 0U);
1590 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001591 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001592 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1593 // can't create an instance of an interface or abstract class */
1594 if (!res_type.IsInstantiableTypes()) {
1595 Fail(VERIFY_ERROR_INSTANTIATION)
1596 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001597 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001598 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001599 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1600 // Any registers holding previous allocations from this address that have not yet been
1601 // initialized must be marked invalid.
1602 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1603 // add the new uninitialized reference to the register state
1604 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001605 break;
1606 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001607 case Instruction::NEW_ARRAY:
1608 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001609 break;
1610 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08001611 VerifyNewArray(dec_insn, true, false);
1612 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001613 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001614 case Instruction::FILLED_NEW_ARRAY_RANGE:
1615 VerifyNewArray(dec_insn, true, true);
1616 just_set_result = true; // Filled new array range sets result register
1617 break;
jeffhaobdb76512011-09-07 11:43:16 -07001618 case Instruction::CMPL_FLOAT:
1619 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001620 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001621 break;
1622 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001623 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001624 break;
1625 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001626 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001627 break;
1628 case Instruction::CMPL_DOUBLE:
1629 case Instruction::CMPG_DOUBLE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001630 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08001631 break;
1632 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001633 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08001634 break;
1635 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001636 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001637 break;
1638 case Instruction::CMP_LONG:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001639 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08001640 break;
1641 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001642 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08001643 break;
1644 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001645 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001646 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001647 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001648 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001649 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001650 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001651 }
1652 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001653 }
jeffhaobdb76512011-09-07 11:43:16 -07001654 case Instruction::GOTO:
1655 case Instruction::GOTO_16:
1656 case Instruction::GOTO_32:
1657 /* no effect on or use of registers */
1658 break;
1659
1660 case Instruction::PACKED_SWITCH:
1661 case Instruction::SPARSE_SWITCH:
1662 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001663 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001664 break;
1665
Ian Rogersd81871c2011-10-03 13:57:23 -07001666 case Instruction::FILL_ARRAY_DATA: {
1667 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001668 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08001669 /* array_type can be null if the reg type is Zero */
1670 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001671 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001672 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001673 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001674 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1675 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001676 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001677 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1678 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001679 } else {
jeffhao457cc512012-02-02 16:55:13 -08001680 // Now verify if the element width in the table matches the element width declared in
1681 // the array
1682 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1683 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001684 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001685 } else {
1686 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1687 // Since we don't compress the data in Dex, expect to see equal width of data stored
1688 // in the table and expected from the array class.
1689 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001690 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1691 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001692 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001693 }
1694 }
jeffhaobdb76512011-09-07 11:43:16 -07001695 }
1696 }
1697 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001698 }
jeffhaobdb76512011-09-07 11:43:16 -07001699 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001700 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001701 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1702 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001703 bool mismatch = false;
1704 if (reg_type1.IsZero()) { // zero then integral or reference expected
1705 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1706 } else if (reg_type1.IsReferenceTypes()) { // both references?
1707 mismatch = !reg_type2.IsReferenceTypes();
1708 } else { // both integral?
1709 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1710 }
1711 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001712 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1713 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001714 }
1715 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001716 }
jeffhaobdb76512011-09-07 11:43:16 -07001717 case Instruction::IF_LT:
1718 case Instruction::IF_GE:
1719 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001720 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001721 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1722 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001723 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1725 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001726 }
1727 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001728 }
jeffhaobdb76512011-09-07 11:43:16 -07001729 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001730 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001731 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001732 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001734 }
jeffhaobdb76512011-09-07 11:43:16 -07001735 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001736 }
jeffhaobdb76512011-09-07 11:43:16 -07001737 case Instruction::IF_LTZ:
1738 case Instruction::IF_GEZ:
1739 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001740 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001741 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001742 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001743 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1744 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001745 }
jeffhaobdb76512011-09-07 11:43:16 -07001746 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001747 }
jeffhaobdb76512011-09-07 11:43:16 -07001748 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001749 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
1750 break;
jeffhaobdb76512011-09-07 11:43:16 -07001751 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001752 VerifyAGet(dec_insn, reg_types_.Byte(), true);
1753 break;
jeffhaobdb76512011-09-07 11:43:16 -07001754 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07001755 VerifyAGet(dec_insn, reg_types_.Char(), true);
1756 break;
jeffhaobdb76512011-09-07 11:43:16 -07001757 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001758 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001759 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001760 case Instruction::AGET:
1761 VerifyAGet(dec_insn, reg_types_.Integer(), true);
1762 break;
jeffhaobdb76512011-09-07 11:43:16 -07001763 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001764 VerifyAGet(dec_insn, reg_types_.Long(), true);
1765 break;
1766 case Instruction::AGET_OBJECT:
1767 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001768 break;
1769
Ian Rogersd81871c2011-10-03 13:57:23 -07001770 case Instruction::APUT_BOOLEAN:
1771 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
1772 break;
1773 case Instruction::APUT_BYTE:
1774 VerifyAPut(dec_insn, reg_types_.Byte(), true);
1775 break;
1776 case Instruction::APUT_CHAR:
1777 VerifyAPut(dec_insn, reg_types_.Char(), true);
1778 break;
1779 case Instruction::APUT_SHORT:
1780 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001781 break;
1782 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001783 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001784 break;
1785 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001786 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001787 break;
1788 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001789 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001790 break;
1791
jeffhaobdb76512011-09-07 11:43:16 -07001792 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001793 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001794 break;
jeffhaobdb76512011-09-07 11:43:16 -07001795 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001796 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001797 break;
jeffhaobdb76512011-09-07 11:43:16 -07001798 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001799 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001800 break;
jeffhaobdb76512011-09-07 11:43:16 -07001801 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001802 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001803 break;
1804 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001805 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001806 break;
1807 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001808 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001809 break;
1810 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001811 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001812 break;
jeffhaobdb76512011-09-07 11:43:16 -07001813
Ian Rogersd81871c2011-10-03 13:57:23 -07001814 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001815 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001816 break;
1817 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001818 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001819 break;
1820 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001821 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001822 break;
1823 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001824 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001825 break;
1826 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001827 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001828 break;
1829 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001830 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001831 break;
jeffhaobdb76512011-09-07 11:43:16 -07001832 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001833 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001834 break;
1835
jeffhaobdb76512011-09-07 11:43:16 -07001836 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001837 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001838 break;
jeffhaobdb76512011-09-07 11:43:16 -07001839 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001840 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001841 break;
jeffhaobdb76512011-09-07 11:43:16 -07001842 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001843 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001844 break;
jeffhaobdb76512011-09-07 11:43:16 -07001845 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001846 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001847 break;
1848 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001849 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001850 break;
1851 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001852 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001853 break;
1854 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001855 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001856 break;
1857
1858 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001859 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001860 break;
1861 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001862 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001863 break;
1864 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001865 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001866 break;
1867 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001868 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001869 break;
1870 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001871 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001872 break;
1873 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001874 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001875 break;
1876 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001877 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001878 break;
1879
1880 case Instruction::INVOKE_VIRTUAL:
1881 case Instruction::INVOKE_VIRTUAL_RANGE:
1882 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001883 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001884 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
1885 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
1886 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
1887 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001888 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001889 const char* descriptor;
1890 if (called_method == NULL) {
1891 uint32_t method_idx = dec_insn.vB;
1892 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1893 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1894 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1895 } else {
1896 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07001897 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001898 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
1899 work_line_->SetResultRegisterType(return_type);
1900 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001901 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001902 }
jeffhaobdb76512011-09-07 11:43:16 -07001903 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001904 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001905 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001906 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07001907 const char* return_type_descriptor;
1908 bool is_constructor;
1909 if (called_method == NULL) {
1910 uint32_t method_idx = dec_insn.vB;
1911 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1912 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
1913 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1914 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1915 } else {
1916 is_constructor = called_method->IsConstructor();
1917 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
1918 }
1919 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07001920 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001921 * Some additional checks when calling a constructor. We know from the invocation arg check
1922 * that the "this" argument is an instance of called_method->klass. Now we further restrict
1923 * that to require that called_method->klass is the same as this->klass or this->super,
1924 * allowing the latter only if the "this" argument is the same as the "this" argument to
1925 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07001926 */
jeffhaob57e9522012-04-26 18:08:21 -07001927 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1928 if (this_type.IsConflict()) // failure.
1929 break;
jeffhaobdb76512011-09-07 11:43:16 -07001930
jeffhaob57e9522012-04-26 18:08:21 -07001931 /* no null refs allowed (?) */
1932 if (this_type.IsZero()) {
1933 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
1934 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07001935 }
jeffhaob57e9522012-04-26 18:08:21 -07001936
1937 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07001938 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
1939 // TODO: re-enable constructor type verification
1940 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07001941 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07001942 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
1943 // break;
1944 // }
jeffhaob57e9522012-04-26 18:08:21 -07001945
1946 /* arg must be an uninitialized reference */
1947 if (!this_type.IsUninitializedTypes()) {
1948 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
1949 << this_type;
1950 break;
1951 }
1952
1953 /*
1954 * Replace the uninitialized reference with an initialized one. We need to do this for all
1955 * registers that have the same object instance in them, not just the "this" register.
1956 */
1957 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001958 }
Ian Rogers46685432012-06-03 22:26:43 -07001959 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001960 work_line_->SetResultRegisterType(return_type);
1961 just_set_result = true;
1962 break;
1963 }
1964 case Instruction::INVOKE_STATIC:
1965 case Instruction::INVOKE_STATIC_RANGE: {
1966 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
1967 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001968 const char* descriptor;
1969 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001970 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001971 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1972 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07001973 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001974 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001975 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001976 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001977 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07001978 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07001979 just_set_result = true;
1980 }
1981 break;
jeffhaobdb76512011-09-07 11:43:16 -07001982 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001983 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001984 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001985 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001986 if (abs_method != NULL) {
1987 Class* called_interface = abs_method->GetDeclaringClass();
1988 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
1989 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
1990 << PrettyMethod(abs_method) << "'";
1991 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001992 }
Ian Rogers0d604842012-04-16 14:50:24 -07001993 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001994 /* Get the type of the "this" arg, which should either be a sub-interface of called
1995 * interface or Object (see comments in RegType::JoinClass).
1996 */
1997 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1998 if (this_type.IsZero()) {
1999 /* null pointer always passes (and always fails at runtime) */
2000 } else {
2001 if (this_type.IsUninitializedTypes()) {
2002 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2003 << this_type;
2004 break;
2005 }
2006 // In the past we have tried to assert that "called_interface" is assignable
2007 // from "this_type.GetClass()", however, as we do an imprecise Join
2008 // (RegType::JoinClass) we don't have full information on what interfaces are
2009 // implemented by "this_type". For example, two classes may implement the same
2010 // interfaces and have a common parent that doesn't implement the interface. The
2011 // join will set "this_type" to the parent class and a test that this implements
2012 // the interface will incorrectly fail.
2013 }
2014 /*
2015 * We don't have an object instance, so we can't find the concrete method. However, all of
2016 * the type information is in the abstract method, so we're good.
2017 */
2018 const char* descriptor;
2019 if (abs_method == NULL) {
2020 uint32_t method_idx = dec_insn.vB;
2021 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2022 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2023 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2024 } else {
2025 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2026 }
2027 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
2028 work_line_->SetResultRegisterType(return_type);
2029 work_line_->SetResultRegisterType(return_type);
2030 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002031 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002032 }
jeffhaobdb76512011-09-07 11:43:16 -07002033 case Instruction::NEG_INT:
2034 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002035 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002036 break;
2037 case Instruction::NEG_LONG:
2038 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002039 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002040 break;
2041 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002042 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002043 break;
2044 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002045 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002046 break;
2047 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002048 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002049 break;
2050 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002051 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002052 break;
2053 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002054 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002055 break;
2056 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002057 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002058 break;
2059 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002060 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002061 break;
2062 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002063 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002064 break;
2065 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002066 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002067 break;
2068 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002069 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002070 break;
2071 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002072 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002073 break;
2074 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002075 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002076 break;
2077 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002078 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002079 break;
2080 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002081 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002082 break;
2083 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002084 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002085 break;
2086 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002087 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002088 break;
2089 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002090 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002091 break;
2092
2093 case Instruction::ADD_INT:
2094 case Instruction::SUB_INT:
2095 case Instruction::MUL_INT:
2096 case Instruction::REM_INT:
2097 case Instruction::DIV_INT:
2098 case Instruction::SHL_INT:
2099 case Instruction::SHR_INT:
2100 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002101 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002102 break;
2103 case Instruction::AND_INT:
2104 case Instruction::OR_INT:
2105 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002106 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002107 break;
2108 case Instruction::ADD_LONG:
2109 case Instruction::SUB_LONG:
2110 case Instruction::MUL_LONG:
2111 case Instruction::DIV_LONG:
2112 case Instruction::REM_LONG:
2113 case Instruction::AND_LONG:
2114 case Instruction::OR_LONG:
2115 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002116 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002117 break;
2118 case Instruction::SHL_LONG:
2119 case Instruction::SHR_LONG:
2120 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002121 /* shift distance is Int, making these different from other binary operations */
2122 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002123 break;
2124 case Instruction::ADD_FLOAT:
2125 case Instruction::SUB_FLOAT:
2126 case Instruction::MUL_FLOAT:
2127 case Instruction::DIV_FLOAT:
2128 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002129 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002130 break;
2131 case Instruction::ADD_DOUBLE:
2132 case Instruction::SUB_DOUBLE:
2133 case Instruction::MUL_DOUBLE:
2134 case Instruction::DIV_DOUBLE:
2135 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002136 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002137 break;
2138 case Instruction::ADD_INT_2ADDR:
2139 case Instruction::SUB_INT_2ADDR:
2140 case Instruction::MUL_INT_2ADDR:
2141 case Instruction::REM_INT_2ADDR:
2142 case Instruction::SHL_INT_2ADDR:
2143 case Instruction::SHR_INT_2ADDR:
2144 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002145 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002146 break;
2147 case Instruction::AND_INT_2ADDR:
2148 case Instruction::OR_INT_2ADDR:
2149 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002150 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002151 break;
2152 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002153 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002154 break;
2155 case Instruction::ADD_LONG_2ADDR:
2156 case Instruction::SUB_LONG_2ADDR:
2157 case Instruction::MUL_LONG_2ADDR:
2158 case Instruction::DIV_LONG_2ADDR:
2159 case Instruction::REM_LONG_2ADDR:
2160 case Instruction::AND_LONG_2ADDR:
2161 case Instruction::OR_LONG_2ADDR:
2162 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002163 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002164 break;
2165 case Instruction::SHL_LONG_2ADDR:
2166 case Instruction::SHR_LONG_2ADDR:
2167 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002168 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002169 break;
2170 case Instruction::ADD_FLOAT_2ADDR:
2171 case Instruction::SUB_FLOAT_2ADDR:
2172 case Instruction::MUL_FLOAT_2ADDR:
2173 case Instruction::DIV_FLOAT_2ADDR:
2174 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002175 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002176 break;
2177 case Instruction::ADD_DOUBLE_2ADDR:
2178 case Instruction::SUB_DOUBLE_2ADDR:
2179 case Instruction::MUL_DOUBLE_2ADDR:
2180 case Instruction::DIV_DOUBLE_2ADDR:
2181 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002182 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002183 break;
2184 case Instruction::ADD_INT_LIT16:
2185 case Instruction::RSUB_INT:
2186 case Instruction::MUL_INT_LIT16:
2187 case Instruction::DIV_INT_LIT16:
2188 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002189 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002190 break;
2191 case Instruction::AND_INT_LIT16:
2192 case Instruction::OR_INT_LIT16:
2193 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002194 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002195 break;
2196 case Instruction::ADD_INT_LIT8:
2197 case Instruction::RSUB_INT_LIT8:
2198 case Instruction::MUL_INT_LIT8:
2199 case Instruction::DIV_INT_LIT8:
2200 case Instruction::REM_INT_LIT8:
2201 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002202 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002203 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002204 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002205 break;
2206 case Instruction::AND_INT_LIT8:
2207 case Instruction::OR_INT_LIT8:
2208 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002209 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002210 break;
2211
Ian Rogersd81871c2011-10-03 13:57:23 -07002212 /* These should never appear during verification. */
jeffhao9a4f0032012-08-30 16:17:40 -07002213 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002214 case Instruction::UNUSED_EE:
2215 case Instruction::UNUSED_EF:
2216 case Instruction::UNUSED_F2:
2217 case Instruction::UNUSED_F3:
2218 case Instruction::UNUSED_F4:
2219 case Instruction::UNUSED_F5:
2220 case Instruction::UNUSED_F6:
2221 case Instruction::UNUSED_F7:
2222 case Instruction::UNUSED_F8:
2223 case Instruction::UNUSED_F9:
2224 case Instruction::UNUSED_FA:
2225 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002226 case Instruction::UNUSED_F0:
2227 case Instruction::UNUSED_F1:
2228 case Instruction::UNUSED_E3:
2229 case Instruction::UNUSED_E8:
2230 case Instruction::UNUSED_E7:
2231 case Instruction::UNUSED_E4:
2232 case Instruction::UNUSED_E9:
2233 case Instruction::UNUSED_FC:
2234 case Instruction::UNUSED_E5:
2235 case Instruction::UNUSED_EA:
2236 case Instruction::UNUSED_FD:
2237 case Instruction::UNUSED_E6:
2238 case Instruction::UNUSED_EB:
2239 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002240 case Instruction::UNUSED_3E:
2241 case Instruction::UNUSED_3F:
2242 case Instruction::UNUSED_40:
2243 case Instruction::UNUSED_41:
2244 case Instruction::UNUSED_42:
2245 case Instruction::UNUSED_43:
2246 case Instruction::UNUSED_73:
2247 case Instruction::UNUSED_79:
2248 case Instruction::UNUSED_7A:
2249 case Instruction::UNUSED_EC:
2250 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002251 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002252 break;
2253
2254 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002255 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002256 * complain if an instruction is missing (which is desirable).
2257 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002258 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002259
Ian Rogersad0b3a32012-04-16 14:50:24 -07002260 if (have_pending_hard_failure_) {
2261 if (!Runtime::Current()->IsStarted()) {
jeffhaob57e9522012-04-26 18:08:21 -07002262 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002263 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002264 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002265 /* immediate failure, reject class */
2266 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2267 return false;
jeffhaobdb76512011-09-07 11:43:16 -07002268 }
jeffhaobdb76512011-09-07 11:43:16 -07002269 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002270 * If we didn't just set the result register, clear it out. This ensures that you can only use
2271 * "move-result" immediately after the result is set. (We could check this statically, but it's
2272 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002273 */
2274 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002275 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002276 }
2277
jeffhaoa0a764a2011-09-16 10:43:38 -07002278 /* Handle "continue". Tag the next consecutive instruction. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002279 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers776ac1f2012-04-13 23:36:36 -07002280 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
Ian Rogersd81871c2011-10-03 13:57:23 -07002281 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
jeffhaod5347e02012-03-22 17:25:05 -07002282 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002283 return false;
2284 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002285 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2286 // next instruction isn't one.
jeffhaod5347e02012-03-22 17:25:05 -07002287 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002288 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002289 }
2290 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2291 if (next_line != NULL) {
2292 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2293 // needed.
2294 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002295 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002296 }
jeffhaobdb76512011-09-07 11:43:16 -07002297 } else {
2298 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002299 * We're not recording register data for the next instruction, so we don't know what the prior
2300 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002301 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002302 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002303 }
2304 }
2305
2306 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002307 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002308 *
2309 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002310 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002311 * somebody could get a reference field, check it for zero, and if the
2312 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002313 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002314 * that, and will reject the code.
2315 *
2316 * TODO: avoid re-fetching the branch target
2317 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002318 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002319 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002320 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002321 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002322 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002323 return false;
2324 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002325 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002326 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002327 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002328 }
jeffhaobdb76512011-09-07 11:43:16 -07002329 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002330 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002331 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002332 }
jeffhaobdb76512011-09-07 11:43:16 -07002333 }
2334
2335 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002336 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002337 *
2338 * We've already verified that the table is structurally sound, so we
2339 * just need to walk through and tag the targets.
2340 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002341 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002342 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2343 const uint16_t* switch_insns = insns + offset_to_switch;
2344 int switch_count = switch_insns[1];
2345 int offset_to_targets, targ;
2346
2347 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2348 /* 0 = sig, 1 = count, 2/3 = first key */
2349 offset_to_targets = 4;
2350 } else {
2351 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002352 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002353 offset_to_targets = 2 + 2 * switch_count;
2354 }
2355
2356 /* verify each switch target */
2357 for (targ = 0; targ < switch_count; targ++) {
2358 int offset;
2359 uint32_t abs_offset;
2360
2361 /* offsets are 32-bit, and only partly endian-swapped */
2362 offset = switch_insns[offset_to_targets + targ * 2] |
2363 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002364 abs_offset = work_insn_idx_ + offset;
2365 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002366 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002367 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002368 }
2369 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002370 return false;
2371 }
2372 }
2373
2374 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002375 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2376 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002377 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002378 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002379 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002380 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002381
Ian Rogers0571d352011-11-03 19:51:38 -07002382 for (; iterator.HasNext(); iterator.Next()) {
2383 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002384 within_catch_all = true;
2385 }
jeffhaobdb76512011-09-07 11:43:16 -07002386 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002387 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2388 * "work_regs", because at runtime the exception will be thrown before the instruction
2389 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002390 */
Ian Rogers0571d352011-11-03 19:51:38 -07002391 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002392 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002393 }
jeffhaobdb76512011-09-07 11:43:16 -07002394 }
2395
2396 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002397 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2398 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002399 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002400 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002401 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002402 * The state in work_line reflects the post-execution state. If the current instruction is a
2403 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002404 * it will do so before grabbing the lock).
2405 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002406 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002407 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002408 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002409 return false;
2410 }
2411 }
2412 }
2413
jeffhaod1f0fde2011-09-08 17:25:33 -07002414 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002415 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002416 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002417 return false;
2418 }
jeffhaobdb76512011-09-07 11:43:16 -07002419 }
2420
2421 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002422 * Update start_guess. Advance to the next instruction of that's
2423 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002424 * neither of those exists we're in a return or throw; leave start_guess
2425 * alone and let the caller sort it out.
2426 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002427 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002428 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002429 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002430 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002431 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002432 }
2433
Ian Rogersd81871c2011-10-03 13:57:23 -07002434 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2435 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002436
2437 return true;
2438}
2439
Ian Rogers776ac1f2012-04-13 23:36:36 -07002440const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002441 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002442 const RegType& referrer = GetDeclaringClass();
2443 Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002444 const RegType& result =
2445 klass != NULL ? reg_types_.FromClass(klass)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002446 : reg_types_.FromDescriptor(class_loader_, descriptor);
2447 if (result.IsConflict()) {
2448 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2449 << "' in " << referrer;
2450 return result;
2451 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002452 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002453 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002454 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002455 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002456 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002457 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002458 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002459 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002460 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002461 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002462}
2463
Ian Rogers776ac1f2012-04-13 23:36:36 -07002464const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002465 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002466 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002467 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002468 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2469 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002470 CatchHandlerIterator iterator(handlers_ptr);
2471 for (; iterator.HasNext(); iterator.Next()) {
2472 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2473 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002474 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07002475 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002476 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002477 if (common_super == NULL) {
2478 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2479 // as that is caught at runtime
2480 common_super = &exception;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002481 } else if (!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002482 // We don't know enough about the type and the common path merge will result in
2483 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002484 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002485 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002486 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002487 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002488 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002489 common_super = &common_super->Merge(exception, &reg_types_);
2490 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002491 }
2492 }
2493 }
2494 }
Ian Rogers0571d352011-11-03 19:51:38 -07002495 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002496 }
2497 }
2498 if (common_super == NULL) {
2499 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002500 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002501 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002502 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002503 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002504}
2505
Ian Rogersad0b3a32012-04-16 14:50:24 -07002506Method* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx, MethodType method_type) {
2507 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002508 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002509 if (klass_type.IsConflict()) {
2510 std::string append(" in attempt to access method ");
2511 append += dex_file_->GetMethodName(method_id);
2512 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002513 return NULL;
2514 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002515 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002516 return NULL; // Can't resolve Class so no more to do here
2517 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002518 Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002519 const RegType& referrer = GetDeclaringClass();
2520 Method* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002521 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002522 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002523 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002524
2525 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002526 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002527 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002528 res_method = klass->FindInterfaceMethod(name, signature);
2529 } else {
2530 res_method = klass->FindVirtualMethod(name, signature);
2531 }
2532 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002533 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002534 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002535 // If a virtual or interface method wasn't found with the expected type, look in
2536 // the direct methods. This can happen when the wrong invoke type is used or when
2537 // a class has changed, and will be flagged as an error in later checks.
2538 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2539 res_method = klass->FindDirectMethod(name, signature);
2540 }
2541 if (res_method == NULL) {
2542 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2543 << PrettyDescriptor(klass) << "." << name
2544 << " " << signature;
2545 return NULL;
2546 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002547 }
2548 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002549 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2550 // enforce them here.
2551 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002552 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2553 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002554 return NULL;
2555 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002556 // Disallow any calls to class initializers.
2557 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002558 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2559 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002560 return NULL;
2561 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002562 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002563 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002564 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002565 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002566 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002567 }
jeffhaode0d9c92012-02-27 13:58:13 -08002568 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2569 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002570 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2571 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002572 return NULL;
2573 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002574 // Check that interface methods match interface classes.
2575 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2576 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2577 << " is in an interface class " << PrettyClass(klass);
2578 return NULL;
2579 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2580 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2581 << " is in a non-interface class " << PrettyClass(klass);
2582 return NULL;
2583 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002584 // See if the method type implied by the invoke instruction matches the access flags for the
2585 // target method.
2586 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2587 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2588 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2589 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002590 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2591 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002592 return NULL;
2593 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002594 return res_method;
2595}
2596
Ian Rogers776ac1f2012-04-13 23:36:36 -07002597Method* MethodVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
Ian Rogers46685432012-06-03 22:26:43 -07002598 MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002599 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2600 // we're making.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002601 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002602 if (res_method == NULL) { // error or class is unresolved
2603 return NULL;
2604 }
2605
Ian Rogersd81871c2011-10-03 13:57:23 -07002606 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2607 // has a vtable entry for the target method.
2608 if (is_super) {
2609 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002610 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002611 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002612 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
2613 << PrettyMethod(method_idx_, *dex_file_)
2614 << " to super " << PrettyMethod(res_method);
2615 return NULL;
2616 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002617 Class* super_klass = super.GetClass();
2618 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002619 MethodHelper mh(res_method);
2620 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
2621 << PrettyMethod(method_idx_, *dex_file_)
2622 << " to super " << super
2623 << "." << mh.GetName()
2624 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002625 return NULL;
2626 }
2627 }
2628 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2629 // match the call to the signature. Also, we might might be calling through an abstract method
2630 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08002631 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002632 /* caught by static verifier */
2633 DCHECK(is_range || expected_args <= 5);
2634 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002635 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002636 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2637 return NULL;
2638 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002639
jeffhaobdb76512011-09-07 11:43:16 -07002640 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002641 * Check the "this" argument, which must be an instance of the class that declared the method.
2642 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2643 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002644 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002645 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002646 if (!res_method->IsStatic()) {
2647 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002648 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002649 return NULL;
2650 }
2651 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002652 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002653 return NULL;
2654 }
2655 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07002656 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
2657 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002658 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002659 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002660 return NULL;
2661 }
2662 }
2663 actual_args++;
2664 }
2665 /*
2666 * Process the target method's signature. This signature may or may not
2667 * have been verified, so we can't assume it's properly formed.
2668 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002669 MethodHelper mh(res_method);
2670 const DexFile::TypeList* params = mh.GetParameterTypeList();
2671 size_t params_size = params == NULL ? 0 : params->Size();
2672 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002673 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002674 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002675 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2676 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002677 return NULL;
2678 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002679 const char* descriptor =
2680 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2681 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002682 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002683 << " missing signature component";
2684 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002685 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002686 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002687 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002688 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002689 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002690 }
2691 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2692 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002693 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002694 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002695 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002696 return NULL;
2697 } else {
2698 return res_method;
2699 }
2700}
2701
Ian Rogers776ac1f2012-04-13 23:36:36 -07002702void MethodVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08002703 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002704 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002705 if (res_type.IsConflict()) { // bad class
2706 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002707 } else {
2708 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2709 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002710 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002711 } else if (!is_filled) {
2712 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002713 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002714 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002715 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002716 } else {
2717 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2718 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002719 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002720 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002721 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002722 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002723 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002724 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002725 return;
2726 }
2727 }
2728 // filled-array result goes into "result" register
2729 work_line_->SetResultRegisterType(res_type);
2730 }
2731 }
2732}
2733
Ian Rogers776ac1f2012-04-13 23:36:36 -07002734void MethodVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002735 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002736 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002737 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002738 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002739 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002740 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002741 if (array_type.IsZero()) {
2742 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2743 // instruction type. TODO: have a proper notion of bottom here.
2744 if (!is_primitive || insn_type.IsCategory1Types()) {
2745 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08002746 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002747 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002748 // Category 2
Elliott Hughesadb8c672012-03-06 16:49:32 -08002749 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
Ian Rogers89310de2012-02-01 13:47:30 -08002750 }
jeffhaofc3144e2012-02-01 17:21:15 -08002751 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002752 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002753 } else {
2754 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002755 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002756 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002757 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002758 << " source for aget-object";
2759 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002760 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002761 << " source for category 1 aget";
2762 } else if (is_primitive && !insn_type.Equals(component_type) &&
2763 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2764 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002765 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogersd81871c2011-10-03 13:57:23 -07002766 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002767 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002768 // Use knowledge of the field type which is stronger than the type inferred from the
2769 // instruction, which can't differentiate object types and ints from floats, longs from
2770 // doubles.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002771 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002772 }
2773 }
2774 }
2775}
2776
Ian Rogers776ac1f2012-04-13 23:36:36 -07002777void MethodVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002778 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002779 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002780 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002781 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002782 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002783 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002784 if (array_type.IsZero()) {
2785 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2786 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002787 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002788 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002789 } else {
2790 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002791 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002792 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002793 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002794 << " source for aput-object";
2795 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002796 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002797 << " source for category 1 aput";
2798 } else if (is_primitive && !insn_type.Equals(component_type) &&
2799 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2800 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002801 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002802 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002803 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002804 // The instruction agrees with the type of array, confirm the value to be stored does too
2805 // Note: we use the instruction type (rather than the component type) for aput-object as
2806 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08002807 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002808 }
2809 }
2810 }
2811}
2812
Ian Rogers776ac1f2012-04-13 23:36:36 -07002813Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002814 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2815 // Check access to class
2816 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002817 if (klass_type.IsConflict()) { // bad class
2818 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
2819 field_idx, dex_file_->GetFieldName(field_id),
2820 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002821 return NULL;
2822 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002823 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002824 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08002825 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002826 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2827 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002828 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002829 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
2830 << dex_file_->GetFieldName(field_id) << ") in "
2831 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002832 DCHECK(Thread::Current()->IsExceptionPending());
2833 Thread::Current()->ClearException();
2834 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002835 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2836 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002837 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002838 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002839 return NULL;
2840 } else if (!field->IsStatic()) {
2841 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
2842 return NULL;
2843 } else {
2844 return field;
2845 }
2846}
2847
Ian Rogers776ac1f2012-04-13 23:36:36 -07002848Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002849 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2850 // Check access to class
2851 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002852 if (klass_type.IsConflict()) {
2853 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
2854 field_idx, dex_file_->GetFieldName(field_id),
2855 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002856 return NULL;
2857 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002858 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002859 return NULL; // Can't resolve Class so no more to do here
2860 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002861 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2862 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002863 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002864 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
2865 << dex_file_->GetFieldName(field_id) << ") in "
2866 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002867 DCHECK(Thread::Current()->IsExceptionPending());
2868 Thread::Current()->ClearException();
2869 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002870 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2871 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002872 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002873 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002874 return NULL;
2875 } else if (field->IsStatic()) {
2876 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
2877 << " to not be static";
2878 return NULL;
2879 } else if (obj_type.IsZero()) {
2880 // Cannot infer and check type, however, access will cause null pointer exception
2881 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07002882 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002883 const RegType& field_klass = reg_types_.FromClass(field->GetDeclaringClass());
2884 if (obj_type.IsUninitializedTypes() &&
2885 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
2886 !field_klass.Equals(GetDeclaringClass()))) {
2887 // Field accesses through uninitialized references are only allowable for constructors where
2888 // the field is declared in this class
2889 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
2890 << " of a not fully initialized object within the context of "
2891 << PrettyMethod(method_idx_, *dex_file_);
2892 return NULL;
2893 } else if (!field_klass.IsAssignableFrom(obj_type)) {
2894 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
2895 // of C1. For resolution to occur the declared class of the field must be compatible with
2896 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
2897 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
2898 << " from object of type " << obj_type;
2899 return NULL;
2900 } else {
2901 return field;
2902 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002903 }
2904}
2905
Ian Rogers776ac1f2012-04-13 23:36:36 -07002906void MethodVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002907 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002908 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002909 Field* field;
2910 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002911 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002912 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002913 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07002914 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002915 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002916 const char* descriptor;
Ian Rogers365c1022012-06-22 15:05:28 -07002917 ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002918 if (field != NULL) {
2919 descriptor = FieldHelper(field).GetTypeDescriptor();
2920 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07002921 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002922 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2923 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2924 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07002925 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002926 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
2927 if (is_primitive) {
2928 if (field_type.Equals(insn_type) ||
2929 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
2930 (field_type.IsDouble() && insn_type.IsLongTypes())) {
2931 // expected that read is of the correct primitive type or that int reads are reading
2932 // floats or long reads are reading doubles
2933 } else {
2934 // This is a global failure rather than a class change failure as the instructions and
2935 // the descriptors for the type should have been consistent within the same file at
2936 // compile time
2937 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
2938 << " to be of type '" << insn_type
2939 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002940 return;
2941 }
2942 } else {
2943 if (!insn_type.IsAssignableFrom(field_type)) {
2944 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
2945 << " to be compatible with type '" << insn_type
2946 << "' but found type '" << field_type
2947 << "' in get-object";
2948 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Conflict());
2949 return;
2950 }
2951 }
2952 work_line_->SetRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002953}
2954
Ian Rogers776ac1f2012-04-13 23:36:36 -07002955void MethodVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002956 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002957 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002958 Field* field;
2959 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07002960 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002961 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002962 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07002963 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002964 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002965 const char* descriptor;
Ian Rogers365c1022012-06-22 15:05:28 -07002966 ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002967 if (field != NULL) {
2968 descriptor = FieldHelper(field).GetTypeDescriptor();
2969 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07002970 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002971 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2972 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2973 loader = class_loader_;
2974 }
2975 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
2976 if (field != NULL) {
2977 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
2978 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
2979 << " from other class " << GetDeclaringClass();
2980 return;
2981 }
2982 }
2983 if (is_primitive) {
2984 // Primitive field assignability rules are weaker than regular assignability rules
2985 bool instruction_compatible;
2986 bool value_compatible;
2987 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
2988 if (field_type.IsIntegralTypes()) {
2989 instruction_compatible = insn_type.IsIntegralTypes();
2990 value_compatible = value_type.IsIntegralTypes();
2991 } else if (field_type.IsFloat()) {
2992 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
2993 value_compatible = value_type.IsFloatTypes();
2994 } else if (field_type.IsLong()) {
2995 instruction_compatible = insn_type.IsLong();
2996 value_compatible = value_type.IsLongTypes();
2997 } else if (field_type.IsDouble()) {
2998 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
2999 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003000 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003001 instruction_compatible = false; // reference field with primitive store
3002 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003003 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003004 if (!instruction_compatible) {
3005 // This is a global failure rather than a class change failure as the instructions and
3006 // the descriptors for the type should have been consistent within the same file at
3007 // compile time
3008 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3009 << " to be of type '" << insn_type
3010 << "' but found type '" << field_type
3011 << "' in put";
3012 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003013 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003014 if (!value_compatible) {
3015 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
3016 << " of type " << value_type
3017 << " but expected " << field_type
3018 << " for store to " << PrettyField(field) << " in put";
3019 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003020 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003021 } else {
3022 if (!insn_type.IsAssignableFrom(field_type)) {
3023 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3024 << " to be compatible with type '" << insn_type
3025 << "' but found type '" << field_type
3026 << "' in put-object";
3027 return;
3028 }
3029 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003030 }
3031}
3032
Ian Rogers776ac1f2012-04-13 23:36:36 -07003033bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003034 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003035 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003036 return false;
3037 }
3038 return true;
3039}
3040
Ian Rogers776ac1f2012-04-13 23:36:36 -07003041bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003042 bool changed = true;
3043 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3044 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003045 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003046 * We haven't processed this instruction before, and we haven't touched the registers here, so
3047 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3048 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003049 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003050 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003051 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003052 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3053 if (gDebugVerify) {
3054 copy->CopyFromLine(target_line);
3055 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003056 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003057 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003058 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003059 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003060 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003061 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003062 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3063 << *copy.get() << " MERGE\n"
3064 << *merge_line << " ==\n"
3065 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003066 }
3067 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003068 if (changed) {
3069 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003070 }
3071 return true;
3072}
3073
Ian Rogers776ac1f2012-04-13 23:36:36 -07003074InsnFlags* MethodVerifier::CurrentInsnFlags() {
3075 return &insn_flags_[work_insn_idx_];
3076}
3077
Ian Rogersad0b3a32012-04-16 14:50:24 -07003078const RegType& MethodVerifier::GetMethodReturnType() {
3079 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx_);
3080 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3081 uint16_t return_type_idx = proto_id.return_type_idx_;
3082 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
3083 return reg_types_.FromDescriptor(class_loader_, descriptor);
3084}
3085
3086const RegType& MethodVerifier::GetDeclaringClass() {
3087 if (foo_method_ != NULL) {
3088 return reg_types_.FromClass(foo_method_->GetDeclaringClass());
3089 } else {
3090 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx_);
3091 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
3092 return reg_types_.FromDescriptor(class_loader_, descriptor);
3093 }
3094}
3095
Ian Rogers776ac1f2012-04-13 23:36:36 -07003096void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003097 size_t* log2_max_gc_pc) {
3098 size_t local_gc_points = 0;
3099 size_t max_insn = 0;
3100 size_t max_ref_reg = -1;
3101 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3102 if (insn_flags_[i].IsGcPoint()) {
3103 local_gc_points++;
3104 max_insn = i;
3105 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003106 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003107 }
3108 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003109 *gc_points = local_gc_points;
3110 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3111 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003112 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003113 i++;
3114 }
3115 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003116}
3117
Ian Rogers776ac1f2012-04-13 23:36:36 -07003118const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003119 size_t num_entries, ref_bitmap_bits, pc_bits;
3120 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3121 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003122 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003123 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003124 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003125 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003126 return NULL;
3127 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003128 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3129 // There are 2 bytes to encode the number of entries
3130 if (num_entries >= 65536) {
3131 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003132 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003133 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003134 return NULL;
3135 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003136 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003137 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003138 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003139 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003140 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003141 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003142 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003143 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003144 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003145 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003146 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003147 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3148 return NULL;
3149 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003150 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003151 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003152 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003153 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003154 return NULL;
3155 }
3156 // Write table header
Ian Rogers776ac1f2012-04-13 23:36:36 -07003157 table->push_back(format | ((ref_bitmap_bytes >> PcToReferenceMap::kRegMapFormatShift) &
3158 ~PcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003159 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003160 table->push_back(num_entries & 0xFF);
3161 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003162 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003163 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3164 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003165 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003166 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003167 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003168 }
3169 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003170 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003171 }
3172 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003173 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003174 return table;
3175}
jeffhaoa0a764a2011-09-16 10:43:38 -07003176
Ian Rogers776ac1f2012-04-13 23:36:36 -07003177void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003178 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3179 // that the table data is well formed and all references are marked (or not) in the bitmap
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003180 PcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003181 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003182 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003183 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3184 if (insn_flags_[i].IsGcPoint()) {
3185 CHECK_LT(map_index, map.NumEntries());
3186 CHECK_EQ(map.GetPC(map_index), i);
3187 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3188 map_index++;
3189 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003190 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003191 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003192 CHECK_LT(j / 8, map.RegWidth());
3193 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3194 } else if ((j / 8) < map.RegWidth()) {
3195 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3196 } else {
3197 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3198 }
3199 }
3200 } else {
3201 CHECK(reg_bitmap == NULL);
3202 }
3203 }
3204}
jeffhaoa0a764a2011-09-16 10:43:38 -07003205
Ian Rogers776ac1f2012-04-13 23:36:36 -07003206void MethodVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003207 {
3208 MutexLock mu(*gc_maps_lock_);
3209 GcMapTable::iterator it = gc_maps_->find(ref);
3210 if (it != gc_maps_->end()) {
3211 delete it->second;
3212 gc_maps_->erase(it);
3213 }
3214 gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003215 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003216 CHECK(GetGcMap(ref) != NULL);
3217}
3218
Ian Rogers776ac1f2012-04-13 23:36:36 -07003219const std::vector<uint8_t>* MethodVerifier::GetGcMap(Compiler::MethodReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003220 MutexLock mu(*gc_maps_lock_);
3221 GcMapTable::const_iterator it = gc_maps_->find(ref);
3222 if (it == gc_maps_->end()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003223 return NULL;
3224 }
3225 CHECK(it->second != NULL);
3226 return it->second;
3227}
3228
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003229Mutex* MethodVerifier::gc_maps_lock_ = NULL;
3230MethodVerifier::GcMapTable* MethodVerifier::gc_maps_ = NULL;
3231
3232Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
3233MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
3234
3235#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
3236Mutex* MethodVerifier::inferred_reg_category_maps_lock_ = NULL;
3237MethodVerifier::InferredRegCategoryMapTable* MethodVerifier::inferred_reg_category_maps_ = NULL;
3238#endif
3239
3240void MethodVerifier::Init() {
3241 gc_maps_lock_ = new Mutex("verifier GC maps lock");
3242 {
3243 MutexLock mu(*gc_maps_lock_);
3244 gc_maps_ = new MethodVerifier::GcMapTable;
3245 }
3246
3247 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
3248 {
3249 MutexLock mu(*rejected_classes_lock_);
3250 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
3251 }
3252
3253#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
3254 inferred_reg_category_maps_lock_ = new Mutex("verifier GC maps lock");
3255 {
3256 MutexLock mu(*inferred_reg_category_maps_lock_);
3257 inferred_reg_category_maps_ = new MethodVerifier::InferredRegCategoryMapTable;
3258 }
3259#endif
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003260}
3261
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003262void MethodVerifier::Shutdown() {
3263 {
3264 MutexLock mu(*gc_maps_lock_);
3265 STLDeleteValues(gc_maps_);
3266 delete gc_maps_;
3267 gc_maps_ = NULL;
3268 }
3269 delete gc_maps_lock_;
3270 gc_maps_lock_ = NULL;
3271
3272 {
3273 MutexLock mu(*rejected_classes_lock_);
3274 delete rejected_classes_;
3275 rejected_classes_ = NULL;
3276 }
3277 delete rejected_classes_lock_;
3278 rejected_classes_lock_ = NULL;
3279
3280#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
3281 {
3282 MutexLock mu(*inferred_reg_category_maps_lock_);
3283 STLDeleteValues(inferred_reg_category_maps_);
3284 delete inferred_reg_category_maps_;
3285 inferred_reg_category_maps_ = NULL;
3286 }
3287 delete inferred_reg_category_maps_lock_;
3288 inferred_reg_category_maps_lock_ = NULL;
3289#endif
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003290}
jeffhaod1224c72012-02-29 13:43:08 -08003291
Ian Rogers776ac1f2012-04-13 23:36:36 -07003292void MethodVerifier::AddRejectedClass(Compiler::ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003293 {
3294 MutexLock mu(*rejected_classes_lock_);
3295 rejected_classes_->insert(ref);
3296 }
jeffhaod1224c72012-02-29 13:43:08 -08003297 CHECK(IsClassRejected(ref));
3298}
3299
Ian Rogers776ac1f2012-04-13 23:36:36 -07003300bool MethodVerifier::IsClassRejected(Compiler::ClassReference ref) {
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003301 MutexLock mu(*rejected_classes_lock_);
3302 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08003303}
3304
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -07003305#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Ian Rogers776ac1f2012-04-13 23:36:36 -07003306const InferredRegCategoryMap* MethodVerifier::GenerateInferredRegCategoryMap() {
Logan Chienfca7e872011-12-20 20:08:22 +08003307 uint32_t insns_size = code_item_->insns_size_in_code_units_;
3308 uint16_t regs_size = code_item_->registers_size_;
3309
3310 UniquePtr<InferredRegCategoryMap> table(
3311 new InferredRegCategoryMap(insns_size, regs_size));
3312
3313 for (size_t i = 0; i < insns_size; ++i) {
3314 if (RegisterLine* line = reg_table_.GetLine(i)) {
TDYa127526643e2012-05-26 01:01:48 -07003315 const Instruction* inst = Instruction::At(code_item_->insns_ + i);
3316
3317 // GC points
3318 if (inst->IsBranch() || inst->IsInvoke()) {
3319 for (size_t r = 0; r < regs_size; ++r) {
3320 const RegType &rt = line->GetRegisterType(r);
3321 if (rt.IsNonZeroReferenceTypes()) {
3322 table->SetRegCanBeObject(r);
3323 }
TDYa127b2eb5c12012-05-24 15:52:10 -07003324 }
3325 }
3326
TDYa127526643e2012-05-26 01:01:48 -07003327 /* We only use InferredRegCategoryMap in one case */
3328 if (inst->IsBranch()) {
TDYa127b2eb5c12012-05-24 15:52:10 -07003329 for (size_t r = 0; r < regs_size; ++r) {
3330 const RegType &rt = line->GetRegisterType(r);
3331
3332 if (rt.IsZero()) {
3333 table->SetRegCategory(i, r, kRegZero);
3334 } else if (rt.IsCategory1Types()) {
3335 table->SetRegCategory(i, r, kRegCat1nr);
3336 } else if (rt.IsCategory2Types()) {
3337 table->SetRegCategory(i, r, kRegCat2);
3338 } else if (rt.IsReferenceTypes()) {
3339 table->SetRegCategory(i, r, kRegObject);
3340 } else {
3341 table->SetRegCategory(i, r, kRegUnknown);
3342 }
Logan Chienfca7e872011-12-20 20:08:22 +08003343 }
3344 }
3345 }
3346 }
3347
3348 return table.release();
3349}
Logan Chiendd361c92012-04-10 23:40:37 +08003350
Ian Rogers776ac1f2012-04-13 23:36:36 -07003351void MethodVerifier::SetInferredRegCategoryMap(Compiler::MethodReference ref,
3352 const InferredRegCategoryMap& inferred_reg_category_map) {
Shih-wei Liaocd05a622012-08-15 00:02:05 -07003353 {
3354 MutexLock mu(*inferred_reg_category_maps_lock_);
3355 InferredRegCategoryMapTable::iterator it = inferred_reg_category_maps_->find(ref);
3356 if (it == inferred_reg_category_maps_->end()) {
3357 inferred_reg_category_maps_->Put(ref, &inferred_reg_category_map);
3358 } else {
3359 CHECK(*(it->second) == inferred_reg_category_map);
3360 delete &inferred_reg_category_map;
3361 }
Logan Chiendd361c92012-04-10 23:40:37 +08003362 }
Logan Chiendd361c92012-04-10 23:40:37 +08003363 CHECK(GetInferredRegCategoryMap(ref) != NULL);
3364}
3365
3366const InferredRegCategoryMap*
Ian Rogers776ac1f2012-04-13 23:36:36 -07003367MethodVerifier::GetInferredRegCategoryMap(Compiler::MethodReference ref) {
Logan Chiendd361c92012-04-10 23:40:37 +08003368 MutexLock mu(*inferred_reg_category_maps_lock_);
3369
3370 InferredRegCategoryMapTable::const_iterator it =
3371 inferred_reg_category_maps_->find(ref);
3372
3373 if (it == inferred_reg_category_maps_->end()) {
3374 return NULL;
3375 }
3376 CHECK(it->second != NULL);
3377 return it->second;
3378}
Logan Chienfca7e872011-12-20 20:08:22 +08003379#endif
3380
Ian Rogersd81871c2011-10-03 13:57:23 -07003381} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003382} // namespace art