blob: a01aa8db489891c3259978cbb3345553e2a3f9e5 [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
Shih-wei Liao21d28f52012-06-12 05:55:00 -070035#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
TDYa12789f96052012-07-12 20:49:53 -070036#include "greenland/backend_types.h"
37#include "greenland/inferred_reg_category_map.h"
Logan Chienfca7e872011-12-20 20:08:22 +080038#endif
39
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070040namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070041namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070042
Ian Rogers2c8a8572011-10-24 17:11:36 -070043static const bool gDebugVerify = false;
44
Ian Rogers776ac1f2012-04-13 23:36:36 -070045class InsnFlags {
46 public:
47 InsnFlags() : length_(0), flags_(0) {}
48
49 void SetLengthInCodeUnits(size_t length) {
50 CHECK_LT(length, 65536u);
51 length_ = length;
52 }
53 size_t GetLengthInCodeUnits() {
54 return length_;
55 }
56 bool IsOpcode() const {
57 return length_ != 0;
58 }
59
60 void SetInTry() {
61 flags_ |= 1 << kInTry;
62 }
63 void ClearInTry() {
64 flags_ &= ~(1 << kInTry);
65 }
66 bool IsInTry() const {
67 return (flags_ & (1 << kInTry)) != 0;
68 }
69
70 void SetBranchTarget() {
71 flags_ |= 1 << kBranchTarget;
72 }
73 void ClearBranchTarget() {
74 flags_ &= ~(1 << kBranchTarget);
75 }
76 bool IsBranchTarget() const {
77 return (flags_ & (1 << kBranchTarget)) != 0;
78 }
79
80 void SetGcPoint() {
81 flags_ |= 1 << kGcPoint;
82 }
83 void ClearGcPoint() {
84 flags_ &= ~(1 << kGcPoint);
85 }
86 bool IsGcPoint() const {
87 return (flags_ & (1 << kGcPoint)) != 0;
88 }
89
90 void SetVisited() {
91 flags_ |= 1 << kVisited;
92 }
93 void ClearVisited() {
94 flags_ &= ~(1 << kVisited);
95 }
96 bool IsVisited() const {
97 return (flags_ & (1 << kVisited)) != 0;
98 }
99
100 void SetChanged() {
101 flags_ |= 1 << kChanged;
102 }
103 void ClearChanged() {
104 flags_ &= ~(1 << kChanged);
105 }
106 bool IsChanged() const {
107 return (flags_ & (1 << kChanged)) != 0;
108 }
109
110 bool IsVisitedOrChanged() const {
111 return IsVisited() || IsChanged();
112 }
113
114 std::string Dump() {
115 char encoding[6];
116 if (!IsOpcode()) {
117 strncpy(encoding, "XXXXX", sizeof(encoding));
118 } else {
119 strncpy(encoding, "-----", sizeof(encoding));
120 if (IsInTry()) encoding[kInTry] = 'T';
121 if (IsBranchTarget()) encoding[kBranchTarget] = 'B';
122 if (IsGcPoint()) encoding[kGcPoint] = 'G';
123 if (IsVisited()) encoding[kVisited] = 'V';
124 if (IsChanged()) encoding[kChanged] = 'C';
125 }
126 return std::string(encoding);
127 }
Elliott Hughesa21039c2012-06-21 12:09:25 -0700128
Ian Rogers776ac1f2012-04-13 23:36:36 -0700129 private:
130 enum {
131 kInTry,
132 kBranchTarget,
133 kGcPoint,
134 kVisited,
135 kChanged,
136 };
137
138 // Size of instruction in code units
139 uint16_t length_;
140 uint8_t flags_;
Ian Rogers84fa0742011-10-25 18:13:30 -0700141};
Ian Rogersd81871c2011-10-03 13:57:23 -0700142
Ian Rogersd81871c2011-10-03 13:57:23 -0700143void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
144 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -0700145 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700146 DCHECK_GT(insns_size, 0U);
147
148 for (uint32_t i = 0; i < insns_size; i++) {
149 bool interesting = false;
150 switch (mode) {
151 case kTrackRegsAll:
152 interesting = flags[i].IsOpcode();
153 break;
154 case kTrackRegsGcPoints:
155 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
156 break;
157 case kTrackRegsBranches:
158 interesting = flags[i].IsBranchTarget();
159 break;
160 default:
161 break;
162 }
163 if (interesting) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700164 pc_to_register_line_.Put(i, new RegisterLine(registers_size, verifier));
Ian Rogersd81871c2011-10-03 13:57:23 -0700165 }
166 }
167}
168
jeffhaof1e6b7c2012-06-05 18:33:30 -0700169MethodVerifier::FailureKind MethodVerifier::VerifyClass(const Class* klass, std::string& error) {
jeffhaobdb76512011-09-07 11:43:16 -0700170 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700171 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700172 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700173 Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -0800174 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800175 error = "Verifier rejected class ";
176 error += PrettyDescriptor(klass);
177 error += " that has no super class";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700178 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -0700179 }
Ian Rogers1c5eb702012-02-01 09:18:34 -0800180 if (super != NULL && super->IsFinal()) {
181 error = "Verifier rejected class ";
182 error += PrettyDescriptor(klass);
183 error += " that attempts to sub-class final class ";
184 error += PrettyDescriptor(super);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700185 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -0700186 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700187 ClassHelper kh(klass);
188 const DexFile& dex_file = kh.GetDexFile();
189 uint32_t class_def_idx;
190 if (!dex_file.FindClassDefIndex(kh.GetDescriptor(), class_def_idx)) {
191 error = "Verifier rejected class ";
192 error += PrettyDescriptor(klass);
193 error += " that isn't present in dex file ";
194 error += dex_file.GetLocation();
jeffhaof1e6b7c2012-06-05 18:33:30 -0700195 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700196 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700197 return VerifyClass(&dex_file, kh.GetDexCache(), klass->GetClassLoader(), class_def_idx, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700198}
199
Ian Rogers365c1022012-06-22 15:05:28 -0700200MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
201 DexCache* dex_cache, ClassLoader* class_loader, uint32_t class_def_idx, std::string& error) {
jeffhaof56197c2012-03-05 18:01:54 -0800202 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
203 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700204 if (class_data == NULL) {
205 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700206 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700207 }
jeffhaof56197c2012-03-05 18:01:54 -0800208 ClassDataItemIterator it(*dex_file, class_data);
209 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
210 it.Next();
211 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700212 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700213 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700214 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaof56197c2012-03-05 18:01:54 -0800215 while (it.HasNextDirectMethod()) {
216 uint32_t method_idx = it.GetMemberIndex();
Ian Rogers08f753d2012-08-24 14:35:25 -0700217 InvokeType type = it.GetMethodInvokeType(class_def);
jeffhaoc0228b82012-08-29 18:15:05 -0700218 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700219 if (method == NULL) {
220 DCHECK(Thread::Current()->IsExceptionPending());
221 // We couldn't resolve the method, but continue regardless.
222 Thread::Current()->ClearException();
223 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700224 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
225 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags());
226 if (result != kNoFailure) {
227 if (result == kHardFailure) {
228 hard_fail = true;
229 if (error_count > 0) {
230 error += "\n";
231 }
232 error = "Verifier rejected class ";
233 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
234 error += " due to bad method ";
235 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700236 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700237 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800238 }
239 it.Next();
240 }
241 while (it.HasNextVirtualMethod()) {
242 uint32_t method_idx = it.GetMemberIndex();
Ian Rogers08f753d2012-08-24 14:35:25 -0700243 InvokeType type = it.GetMethodInvokeType(class_def);
jeffhaoc0228b82012-08-29 18:15:05 -0700244 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700245 if (method == NULL) {
246 DCHECK(Thread::Current()->IsExceptionPending());
247 // We couldn't resolve the method, but continue regardless.
248 Thread::Current()->ClearException();
249 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700250 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
251 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags());
252 if (result != kNoFailure) {
253 if (result == kHardFailure) {
254 hard_fail = true;
255 if (error_count > 0) {
256 error += "\n";
257 }
258 error = "Verifier rejected class ";
259 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
260 error += " due to bad method ";
261 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700262 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700263 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800264 }
265 it.Next();
266 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700267 if (error_count == 0) {
268 return kNoFailure;
269 } else {
270 return hard_fail ? kHardFailure : kSoftFailure;
271 }
jeffhaof56197c2012-03-05 18:01:54 -0800272}
273
jeffhaof1e6b7c2012-06-05 18:33:30 -0700274MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx, const DexFile* dex_file,
Ian Rogers365c1022012-06-22 15:05:28 -0700275 DexCache* dex_cache, ClassLoader* class_loader, uint32_t class_def_idx,
jeffhaof1e6b7c2012-06-05 18:33:30 -0700276 const DexFile::CodeItem* code_item, Method* method, uint32_t method_access_flags) {
Ian Rogersc8982582012-09-07 16:53:25 -0700277 MethodVerifier::FailureKind result = kNoFailure;
278 uint64_t start_ns = NanoTime();
279
Ian Rogersad0b3a32012-04-16 14:50:24 -0700280 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item, method_idx,
281 method, method_access_flags);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700282 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700283 // Verification completed, however failures may be pending that didn't cause the verification
284 // to hard fail.
Ian Rogerse551e952012-06-03 22:59:14 -0700285 CHECK(!verifier.have_pending_hard_failure_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700286 if (verifier.failures_.size() != 0) {
287 verifier.DumpFailures(LOG(INFO) << "Soft verification failures in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700288 << PrettyMethod(method_idx, *dex_file) << "\n");
Ian Rogersc8982582012-09-07 16:53:25 -0700289 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800290 }
291 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700292 // Bad method data.
293 CHECK_NE(verifier.failures_.size(), 0U);
294 CHECK(verifier.have_pending_hard_failure_);
295 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700296 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800297 if (gDebugVerify) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700298 std::cout << "\n" << verifier.info_messages_.str();
jeffhaof56197c2012-03-05 18:01:54 -0800299 verifier.Dump(std::cout);
300 }
Ian Rogersc8982582012-09-07 16:53:25 -0700301 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800302 }
Ian Rogersc8982582012-09-07 16:53:25 -0700303 uint64_t duration_ns = NanoTime() - start_ns;
304 if (duration_ns > MsToNs(100)) {
305 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
306 << " took " << PrettyDuration(duration_ns);
307 }
308 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800309}
310
Ian Rogersad0b3a32012-04-16 14:50:24 -0700311void MethodVerifier::VerifyMethodAndDump(Method* method) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800312 CHECK(method != NULL);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700313 MethodHelper mh(method);
314 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
315 mh.GetClassDefIndex(), mh.GetCodeItem(), method->GetDexMethodIndex(),
316 method, method->GetAccessFlags());
317 verifier.Verify();
Elliott Hughesc073b072012-05-24 19:29:17 -0700318 verifier.DumpFailures(LOG(INFO) << "Dump of method " << PrettyMethod(method) << "\n")
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700319 << verifier.info_messages_.str() << MutatorLockedDumpable<MethodVerifier>(verifier);
jeffhaoba5ebb92011-08-25 17:24:37 -0700320}
321
Ian Rogers776ac1f2012-04-13 23:36:36 -0700322MethodVerifier::MethodVerifier(const DexFile* dex_file, DexCache* dex_cache,
Ian Rogers365c1022012-06-22 15:05:28 -0700323 ClassLoader* class_loader, uint32_t class_def_idx, const DexFile::CodeItem* code_item,
Ian Rogersad0b3a32012-04-16 14:50:24 -0700324 uint32_t method_idx, Method* method, uint32_t method_access_flags)
jeffhaof56197c2012-03-05 18:01:54 -0800325 : work_insn_idx_(-1),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700326 method_idx_(method_idx),
327 foo_method_(method),
328 method_access_flags_(method_access_flags),
jeffhaof56197c2012-03-05 18:01:54 -0800329 dex_file_(dex_file),
330 dex_cache_(dex_cache),
331 class_loader_(class_loader),
332 class_def_idx_(class_def_idx),
333 code_item_(code_item),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700334 interesting_dex_pc_(-1),
335 monitor_enter_dex_pcs_(NULL),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700336 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700337 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800338 new_instance_count_(0),
339 monitor_enter_count_(0) {
340}
341
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700342void MethodVerifier::FindLocksAtDexPc(Method* m, uint32_t dex_pc, std::vector<uint32_t>& monitor_enter_dex_pcs) {
343 MethodHelper mh(m);
344 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
345 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
346 m, m->GetAccessFlags());
347 verifier.interesting_dex_pc_ = dex_pc;
348 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
349 verifier.FindLocksAtDexPc();
350}
351
352void MethodVerifier::FindLocksAtDexPc() {
353 CHECK(monitor_enter_dex_pcs_ != NULL);
354 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
355
356 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
357 // verification. In practice, the phase we want relies on data structures set up by all the
358 // earlier passes, so we just run the full method verification and bail out early when we've
359 // got what we wanted.
360 Verify();
361}
362
Ian Rogersad0b3a32012-04-16 14:50:24 -0700363bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700364 // If there aren't any instructions, make sure that's expected, then exit successfully.
365 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700366 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700367 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700368 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700369 } else {
370 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700371 }
jeffhaobdb76512011-09-07 11:43:16 -0700372 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700373 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
374 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700375 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
376 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700377 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700378 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700379 // Allocate and initialize an array to hold instruction data.
380 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
381 // Run through the instructions and see if the width checks out.
382 bool result = ComputeWidthsAndCountOps();
383 // Flag instructions guarded by a "try" block and check exception handlers.
384 result = result && ScanTryCatchBlocks();
385 // Perform static instruction verification.
386 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700387 // Perform code-flow analysis and return.
388 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700389}
390
Ian Rogers776ac1f2012-04-13 23:36:36 -0700391std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700392 switch (error) {
393 case VERIFY_ERROR_NO_CLASS:
394 case VERIFY_ERROR_NO_FIELD:
395 case VERIFY_ERROR_NO_METHOD:
396 case VERIFY_ERROR_ACCESS_CLASS:
397 case VERIFY_ERROR_ACCESS_FIELD:
398 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700399 case VERIFY_ERROR_INSTANTIATION:
400 case VERIFY_ERROR_CLASS_CHANGE:
jeffhaofaf459e2012-08-31 15:32:47 -0700401 if (Runtime::Current()->IsCompiler()) {
402 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
403 // class change and instantiation errors into soft verification errors so that we re-verify
404 // at runtime. We may fail to find or to agree on access because of not yet available class
405 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
406 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
407 // paths" that dynamically perform the verification and cause the behavior to be that akin
408 // to an interpreter.
409 error = VERIFY_ERROR_BAD_CLASS_SOFT;
410 } else {
411 have_pending_runtime_throw_failure_ = true;
412 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700413 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700414 // Indication that verification should be retried at runtime.
415 case VERIFY_ERROR_BAD_CLASS_SOFT:
416 if (!Runtime::Current()->IsCompiler()) {
417 // It is runtime so hard fail.
418 have_pending_hard_failure_ = true;
419 }
420 break;
jeffhaod5347e02012-03-22 17:25:05 -0700421 // Hard verification failures at compile time will still fail at runtime, so the class is
422 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700423 case VERIFY_ERROR_BAD_CLASS_HARD: {
424 if (Runtime::Current()->IsCompiler()) {
jeffhaof56197c2012-03-05 18:01:54 -0800425 Compiler::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800426 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800427 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700428 have_pending_hard_failure_ = true;
429 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800430 }
431 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700432 failures_.push_back(error);
433 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(method_idx_, *dex_file_).c_str(),
434 work_insn_idx_));
435 std::ostringstream* failure_message = new std::ostringstream(location);
436 failure_messages_.push_back(failure_message);
437 return *failure_message;
438}
439
440void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
441 size_t failure_num = failure_messages_.size();
442 DCHECK_NE(failure_num, 0U);
443 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
444 prepend += last_fail_message->str();
445 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
446 delete last_fail_message;
447}
448
449void MethodVerifier::AppendToLastFailMessage(std::string append) {
450 size_t failure_num = failure_messages_.size();
451 DCHECK_NE(failure_num, 0U);
452 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
453 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800454}
455
Ian Rogers776ac1f2012-04-13 23:36:36 -0700456bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700457 const uint16_t* insns = code_item_->insns_;
458 size_t insns_size = code_item_->insns_size_in_code_units_;
459 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700460 size_t new_instance_count = 0;
461 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700462 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700463
Ian Rogersd81871c2011-10-03 13:57:23 -0700464 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700465 Instruction::Code opcode = inst->Opcode();
466 if (opcode == Instruction::NEW_INSTANCE) {
467 new_instance_count++;
468 } else if (opcode == Instruction::MONITOR_ENTER) {
469 monitor_enter_count++;
470 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700471 size_t inst_size = inst->SizeInCodeUnits();
472 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
473 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700474 inst = inst->Next();
475 }
476
Ian Rogersd81871c2011-10-03 13:57:23 -0700477 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700478 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
479 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700480 return false;
481 }
482
Ian Rogersd81871c2011-10-03 13:57:23 -0700483 new_instance_count_ = new_instance_count;
484 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700485 return true;
486}
487
Ian Rogers776ac1f2012-04-13 23:36:36 -0700488bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700489 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700490 if (tries_size == 0) {
491 return true;
492 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700493 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700494 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700495
496 for (uint32_t idx = 0; idx < tries_size; idx++) {
497 const DexFile::TryItem* try_item = &tries[idx];
498 uint32_t start = try_item->start_addr_;
499 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700500 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700501 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
502 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700503 return false;
504 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700505 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700506 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700507 return false;
508 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700509 for (uint32_t dex_pc = start; dex_pc < end;
510 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
511 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700512 }
513 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800514 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700515 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700516 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700517 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700518 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700519 CatchHandlerIterator iterator(handlers_ptr);
520 for (; iterator.HasNext(); iterator.Next()) {
521 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700522 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700523 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700524 return false;
525 }
jeffhao60f83e32012-02-13 17:16:30 -0800526 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
527 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700528 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700529 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800530 return false;
531 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700532 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700533 // Ensure exception types are resolved so that they don't need resolution to be delivered,
534 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700535 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
jeffhaof56197c2012-03-05 18:01:54 -0800536 Class* exception_type = linker->ResolveType(*dex_file_, iterator.GetHandlerTypeIndex(),
537 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700538 if (exception_type == NULL) {
539 DCHECK(Thread::Current()->IsExceptionPending());
540 Thread::Current()->ClearException();
541 }
542 }
jeffhaobdb76512011-09-07 11:43:16 -0700543 }
Ian Rogers0571d352011-11-03 19:51:38 -0700544 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700545 }
jeffhaobdb76512011-09-07 11:43:16 -0700546 return true;
547}
548
Ian Rogers776ac1f2012-04-13 23:36:36 -0700549bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700550 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700551
Ian Rogersd81871c2011-10-03 13:57:23 -0700552 /* Flag the start of the method as a branch target. */
553 insn_flags_[0].SetBranchTarget();
554
555 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700556 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700557 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700558 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700559 return false;
560 }
561 /* Flag instructions that are garbage collection points */
562 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
563 insn_flags_[dex_pc].SetGcPoint();
564 }
565 dex_pc += inst->SizeInCodeUnits();
566 inst = inst->Next();
567 }
568 return true;
569}
570
Ian Rogers776ac1f2012-04-13 23:36:36 -0700571bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800572 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700573 bool result = true;
574 switch (inst->GetVerifyTypeArgumentA()) {
575 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800576 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700577 break;
578 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800579 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700580 break;
581 }
582 switch (inst->GetVerifyTypeArgumentB()) {
583 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800584 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700585 break;
586 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800587 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700588 break;
589 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800590 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700591 break;
592 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800593 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700594 break;
595 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800596 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700597 break;
598 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800599 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700600 break;
601 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800602 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700603 break;
604 }
605 switch (inst->GetVerifyTypeArgumentC()) {
606 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800607 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700608 break;
609 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800610 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700611 break;
612 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800613 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700614 break;
615 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800616 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700617 break;
618 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800619 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700620 break;
621 }
622 switch (inst->GetVerifyExtraFlags()) {
623 case Instruction::kVerifyArrayData:
624 result = result && CheckArrayData(code_offset);
625 break;
626 case Instruction::kVerifyBranchTarget:
627 result = result && CheckBranchTarget(code_offset);
628 break;
629 case Instruction::kVerifySwitchTargets:
630 result = result && CheckSwitchTargets(code_offset);
631 break;
632 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800633 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700634 break;
635 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800636 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700637 break;
638 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700639 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700640 result = false;
641 break;
642 }
643 return result;
644}
645
Ian Rogers776ac1f2012-04-13 23:36:36 -0700646bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700647 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700648 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
649 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700650 return false;
651 }
652 return true;
653}
654
Ian Rogers776ac1f2012-04-13 23:36:36 -0700655bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700656 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700657 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
658 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700659 return false;
660 }
661 return true;
662}
663
Ian Rogers776ac1f2012-04-13 23:36:36 -0700664bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700665 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700666 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
667 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700668 return false;
669 }
670 return true;
671}
672
Ian Rogers776ac1f2012-04-13 23:36:36 -0700673bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700675 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
676 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700677 return false;
678 }
679 return true;
680}
681
Ian Rogers776ac1f2012-04-13 23:36:36 -0700682bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700684 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
685 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700686 return false;
687 }
688 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700689 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700690 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700691 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700692 return false;
693 }
694 return true;
695}
696
Ian Rogers776ac1f2012-04-13 23:36:36 -0700697bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700698 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700699 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
700 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700701 return false;
702 }
703 return true;
704}
705
Ian Rogers776ac1f2012-04-13 23:36:36 -0700706bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700707 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700708 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
709 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700710 return false;
711 }
712 return true;
713}
714
Ian Rogers776ac1f2012-04-13 23:36:36 -0700715bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700716 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700717 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
718 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700719 return false;
720 }
721 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700722 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700723 const char* cp = descriptor;
724 while (*cp++ == '[') {
725 bracket_count++;
726 }
727 if (bracket_count == 0) {
728 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700729 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700730 return false;
731 } else if (bracket_count > 255) {
732 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700734 return false;
735 }
736 return true;
737}
738
Ian Rogers776ac1f2012-04-13 23:36:36 -0700739bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700740 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
741 const uint16_t* insns = code_item_->insns_ + cur_offset;
742 const uint16_t* array_data;
743 int32_t array_data_offset;
744
745 DCHECK_LT(cur_offset, insn_count);
746 /* make sure the start of the array data table is in range */
747 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
748 if ((int32_t) cur_offset + array_data_offset < 0 ||
749 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700750 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
751 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700752 return false;
753 }
754 /* offset to array data table is a relative branch-style offset */
755 array_data = insns + array_data_offset;
756 /* make sure the table is 32-bit aligned */
757 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700758 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
759 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700760 return false;
761 }
762 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700763 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700764 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
765 /* make sure the end of the switch is in range */
766 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700767 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
768 << ", data offset " << array_data_offset << ", end "
769 << cur_offset + array_data_offset + table_size
770 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700771 return false;
772 }
773 return true;
774}
775
Ian Rogers776ac1f2012-04-13 23:36:36 -0700776bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700777 int32_t offset;
778 bool isConditional, selfOkay;
779 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
780 return false;
781 }
782 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700783 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 -0700784 return false;
785 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700786 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
787 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700788 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700789 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700790 return false;
791 }
792 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
793 int32_t abs_offset = cur_offset + offset;
794 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700795 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700796 << reinterpret_cast<void*>(abs_offset) << ") at "
797 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700798 return false;
799 }
800 insn_flags_[abs_offset].SetBranchTarget();
801 return true;
802}
803
Ian Rogers776ac1f2012-04-13 23:36:36 -0700804bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700805 bool* selfOkay) {
806 const uint16_t* insns = code_item_->insns_ + cur_offset;
807 *pConditional = false;
808 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700809 switch (*insns & 0xff) {
810 case Instruction::GOTO:
811 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700812 break;
813 case Instruction::GOTO_32:
814 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700815 *selfOkay = true;
816 break;
817 case Instruction::GOTO_16:
818 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700819 break;
820 case Instruction::IF_EQ:
821 case Instruction::IF_NE:
822 case Instruction::IF_LT:
823 case Instruction::IF_GE:
824 case Instruction::IF_GT:
825 case Instruction::IF_LE:
826 case Instruction::IF_EQZ:
827 case Instruction::IF_NEZ:
828 case Instruction::IF_LTZ:
829 case Instruction::IF_GEZ:
830 case Instruction::IF_GTZ:
831 case Instruction::IF_LEZ:
832 *pOffset = (int16_t) insns[1];
833 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700834 break;
835 default:
836 return false;
837 break;
838 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700839 return true;
840}
841
Ian Rogers776ac1f2012-04-13 23:36:36 -0700842bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700843 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700844 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700845 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700846 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700847 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
848 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700849 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
850 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700851 return false;
852 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700853 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700854 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700855 /* make sure the table is 32-bit aligned */
856 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700857 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
858 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700859 return false;
860 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700861 uint32_t switch_count = switch_insns[1];
862 int32_t keys_offset, targets_offset;
863 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700864 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
865 /* 0=sig, 1=count, 2/3=firstKey */
866 targets_offset = 4;
867 keys_offset = -1;
868 expected_signature = Instruction::kPackedSwitchSignature;
869 } else {
870 /* 0=sig, 1=count, 2..count*2 = keys */
871 keys_offset = 2;
872 targets_offset = 2 + 2 * switch_count;
873 expected_signature = Instruction::kSparseSwitchSignature;
874 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700875 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700876 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700877 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
878 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700879 return false;
880 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700881 /* make sure the end of the switch is in range */
882 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700883 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
884 << switch_offset << ", end "
885 << (cur_offset + switch_offset + table_size)
886 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700887 return false;
888 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700889 /* for a sparse switch, verify the keys are in ascending order */
890 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700891 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
892 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700893 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
894 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
895 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700896 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
897 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700898 return false;
899 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700900 last_key = key;
901 }
902 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700903 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700904 for (uint32_t targ = 0; targ < switch_count; targ++) {
905 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
906 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
907 int32_t abs_offset = cur_offset + offset;
908 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700909 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700910 << reinterpret_cast<void*>(abs_offset) << ") at "
911 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700912 return false;
913 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700914 insn_flags_[abs_offset].SetBranchTarget();
915 }
916 return true;
917}
918
Ian Rogers776ac1f2012-04-13 23:36:36 -0700919bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700920 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700921 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700922 return false;
923 }
924 uint16_t registers_size = code_item_->registers_size_;
925 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800926 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700927 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
928 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700929 return false;
930 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700931 }
932
933 return true;
934}
935
Ian Rogers776ac1f2012-04-13 23:36:36 -0700936bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700937 uint16_t registers_size = code_item_->registers_size_;
938 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
939 // integer overflow when adding them here.
940 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700941 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
942 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700943 return false;
944 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700945 return true;
946}
947
Brian Carlstrom75412882012-01-18 01:26:54 -0800948const std::vector<uint8_t>* CreateLengthPrefixedGcMap(const std::vector<uint8_t>& gc_map) {
949 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
950 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
951 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
952 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
953 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
954 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
955 gc_map.begin(),
956 gc_map.end());
957 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
958 DCHECK_EQ(gc_map.size(),
959 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
960 (length_prefixed_gc_map->at(1) << 16) |
961 (length_prefixed_gc_map->at(2) << 8) |
962 (length_prefixed_gc_map->at(3) << 0)));
963 return length_prefixed_gc_map;
964}
965
Ian Rogers776ac1f2012-04-13 23:36:36 -0700966bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700967 uint16_t registers_size = code_item_->registers_size_;
968 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -0700969
Ian Rogersd81871c2011-10-03 13:57:23 -0700970 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -0800971 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
972 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700973 }
974 /* Create and initialize table holding register status */
Elliott Hughes460384f2012-04-04 16:53:10 -0700975 reg_table_.Init(kTrackRegsGcPoints, insn_flags_.get(), insns_size, registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -0700976
Ian Rogersd81871c2011-10-03 13:57:23 -0700977 work_line_.reset(new RegisterLine(registers_size, this));
978 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -0700979
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 /* Initialize register types of method arguments. */
981 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700982 DCHECK_NE(failures_.size(), 0U);
983 std::string prepend("Bad signature in ");
984 prepend += PrettyMethod(method_idx_, *dex_file_);
985 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -0700986 return false;
987 }
988 /* Perform code flow verification. */
989 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700990 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700991 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700992 }
993
TDYa127b2eb5c12012-05-24 15:52:10 -0700994 Compiler::MethodReference ref(dex_file_, method_idx_);
995
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700996#if !defined(ART_USE_LLVM_COMPILER) && !defined(ART_USE_GREENLAND_COMPILER)
TDYa127b2eb5c12012-05-24 15:52:10 -0700997
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -0800999 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1000 if (map.get() == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001001 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001002 return false; // Not a real failure, but a failure to encode
1003 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001004#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001005 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -07001006#endif
Brian Carlstrom75412882012-01-18 01:26:54 -08001007 const std::vector<uint8_t>* gc_map = CreateLengthPrefixedGcMap(*(map.get()));
Ian Rogers776ac1f2012-04-13 23:36:36 -07001008 verifier::MethodVerifier::SetGcMap(ref, *gc_map);
Logan Chienfca7e872011-12-20 20:08:22 +08001009
Ian Rogersad0b3a32012-04-16 14:50:24 -07001010 if (foo_method_ != NULL) {
1011 foo_method_->SetGcMap(&gc_map->at(0));
1012 }
Logan Chiendd361c92012-04-10 23:40:37 +08001013
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -07001014#else // defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Logan Chienfca7e872011-12-20 20:08:22 +08001015 /* Generate Inferred Register Category for LLVM-based Code Generator */
1016 const InferredRegCategoryMap* table = GenerateInferredRegCategoryMap();
Ian Rogers776ac1f2012-04-13 23:36:36 -07001017 verifier::MethodVerifier::SetInferredRegCategoryMap(ref, *table);
TDYa127b2eb5c12012-05-24 15:52:10 -07001018
Logan Chienfca7e872011-12-20 20:08:22 +08001019#endif
1020
jeffhaobdb76512011-09-07 11:43:16 -07001021 return true;
1022}
1023
Ian Rogersad0b3a32012-04-16 14:50:24 -07001024std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1025 DCHECK_EQ(failures_.size(), failure_messages_.size());
1026 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001027 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001028 }
1029 return os;
1030}
1031
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001032extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001033 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001034 v->Dump(std::cerr);
1035}
1036
Ian Rogers776ac1f2012-04-13 23:36:36 -07001037void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001038 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001039 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001040 return;
jeffhaobdb76512011-09-07 11:43:16 -07001041 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001042 DCHECK(code_item_ != NULL);
1043 const Instruction* inst = Instruction::At(code_item_->insns_);
1044 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1045 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Elliott Hughesaa6e1cd2012-01-18 19:26:06 -08001046 os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
Elliott Hughesc073b072012-05-24 19:29:17 -07001047 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001048 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1049 if (reg_line != NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001050 os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001051 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001052 inst = inst->Next();
1053 }
jeffhaobdb76512011-09-07 11:43:16 -07001054}
1055
Ian Rogersd81871c2011-10-03 13:57:23 -07001056static bool IsPrimitiveDescriptor(char descriptor) {
1057 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001058 case 'I':
1059 case 'C':
1060 case 'S':
1061 case 'B':
1062 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001063 case 'F':
1064 case 'D':
1065 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001066 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001067 default:
1068 return false;
1069 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001070}
1071
Ian Rogers776ac1f2012-04-13 23:36:36 -07001072bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001073 RegisterLine* reg_line = reg_table_.GetLine(0);
1074 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1075 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001076
Ian Rogersd81871c2011-10-03 13:57:23 -07001077 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1078 //Include the "this" pointer.
1079 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001080 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001081 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1082 // argument as uninitialized. This restricts field access until the superclass constructor is
1083 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001084 const RegType& declaring_class = GetDeclaringClass();
1085 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001086 reg_line->SetRegisterType(arg_start + cur_arg,
1087 reg_types_.UninitializedThisArgument(declaring_class));
1088 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001089 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001090 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001091 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001092 }
1093
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001094 const DexFile::ProtoId& proto_id =
Ian Rogersad0b3a32012-04-16 14:50:24 -07001095 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001096 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001097
1098 for (; iterator.HasNext(); iterator.Next()) {
1099 const char* descriptor = iterator.GetDescriptor();
1100 if (descriptor == NULL) {
1101 LOG(FATAL) << "Null descriptor";
1102 }
1103 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001104 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1105 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001106 return false;
1107 }
1108 switch (descriptor[0]) {
1109 case 'L':
1110 case '[':
1111 // We assume that reference arguments are initialized. The only way it could be otherwise
1112 // (assuming the caller was verified) is if the current method is <init>, but in that case
1113 // it's effectively considered initialized the instant we reach here (in the sense that we
1114 // can return without doing anything or call virtual methods).
1115 {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001116 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001117 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001118 }
1119 break;
1120 case 'Z':
1121 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1122 break;
1123 case 'C':
1124 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1125 break;
1126 case 'B':
1127 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1128 break;
1129 case 'I':
1130 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1131 break;
1132 case 'S':
1133 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1134 break;
1135 case 'F':
1136 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1137 break;
1138 case 'J':
1139 case 'D': {
1140 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1141 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1142 cur_arg++;
1143 break;
1144 }
1145 default:
jeffhaod5347e02012-03-22 17:25:05 -07001146 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001147 return false;
1148 }
1149 cur_arg++;
1150 }
1151 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001152 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001153 return false;
1154 }
1155 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1156 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1157 // format. Only major difference from the method argument format is that 'V' is supported.
1158 bool result;
1159 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1160 result = descriptor[1] == '\0';
1161 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1162 size_t i = 0;
1163 do {
1164 i++;
1165 } while (descriptor[i] == '['); // process leading [
1166 if (descriptor[i] == 'L') { // object array
1167 do {
1168 i++; // find closing ;
1169 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1170 result = descriptor[i] == ';';
1171 } else { // primitive array
1172 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1173 }
1174 } else if (descriptor[0] == 'L') {
1175 // could be more thorough here, but shouldn't be required
1176 size_t i = 0;
1177 do {
1178 i++;
1179 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1180 result = descriptor[i] == ';';
1181 } else {
1182 result = false;
1183 }
1184 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001185 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1186 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001187 }
1188 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001189}
1190
Ian Rogers776ac1f2012-04-13 23:36:36 -07001191bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001192 const uint16_t* insns = code_item_->insns_;
1193 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001194
jeffhaobdb76512011-09-07 11:43:16 -07001195 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001196 insn_flags_[0].SetChanged();
1197 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001198
jeffhaobdb76512011-09-07 11:43:16 -07001199 /* Continue until no instructions are marked "changed". */
1200 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001201 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1202 uint32_t insn_idx = start_guess;
1203 for (; insn_idx < insns_size; insn_idx++) {
1204 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001205 break;
1206 }
jeffhaobdb76512011-09-07 11:43:16 -07001207 if (insn_idx == insns_size) {
1208 if (start_guess != 0) {
1209 /* try again, starting from the top */
1210 start_guess = 0;
1211 continue;
1212 } else {
1213 /* all flags are clear */
1214 break;
1215 }
1216 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001217 // We carry the working set of registers from instruction to instruction. If this address can
1218 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1219 // "changed" flags, we need to load the set of registers from the table.
1220 // Because we always prefer to continue on to the next instruction, we should never have a
1221 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1222 // target.
1223 work_insn_idx_ = insn_idx;
1224 if (insn_flags_[insn_idx].IsBranchTarget()) {
1225 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001226 } else {
1227#ifndef NDEBUG
1228 /*
1229 * Sanity check: retrieve the stored register line (assuming
1230 * a full table) and make sure it actually matches.
1231 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001232 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1233 if (register_line != NULL) {
1234 if (work_line_->CompareLine(register_line) != 0) {
1235 Dump(std::cout);
1236 std::cout << info_messages_.str();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001237 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001238 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1239 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001240 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001241 }
jeffhaobdb76512011-09-07 11:43:16 -07001242 }
1243#endif
1244 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001245 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001246 std::string prepend(PrettyMethod(method_idx_, *dex_file_));
1247 prepend += " failed to verify: ";
1248 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001249 return false;
1250 }
jeffhaobdb76512011-09-07 11:43:16 -07001251 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001252 insn_flags_[insn_idx].SetVisited();
1253 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001254 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001255
Ian Rogers1c849e52012-06-28 14:00:33 -07001256 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001257 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001258 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001259 * (besides the wasted space), but it indicates a flaw somewhere
1260 * down the line, possibly in the verifier.
1261 *
1262 * If we've substituted "always throw" instructions into the stream,
1263 * we are almost certainly going to have some dead code.
1264 */
1265 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001266 uint32_t insn_idx = 0;
1267 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001268 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001269 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001270 * may or may not be preceded by a padding NOP (for alignment).
1271 */
1272 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1273 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1274 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001275 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001276 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1277 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1278 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001279 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001280 }
1281
Ian Rogersd81871c2011-10-03 13:57:23 -07001282 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001283 if (dead_start < 0)
1284 dead_start = insn_idx;
1285 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001286 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001287 dead_start = -1;
1288 }
1289 }
1290 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001291 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001292 }
1293 }
jeffhaobdb76512011-09-07 11:43:16 -07001294 return true;
1295}
1296
Ian Rogers776ac1f2012-04-13 23:36:36 -07001297bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001298#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001299 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001300 gDvm.verifierStats.instrsReexamined++;
1301 } else {
1302 gDvm.verifierStats.instrsExamined++;
1303 }
1304#endif
1305
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001306 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1307 // We want the state _before_ the instruction, for the case where the dex pc we're
1308 // interested in is itself a monitor-enter instruction (which is a likely place
1309 // for a thread to be suspended).
1310 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
1311 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1312 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1313 }
1314 }
1315
jeffhaobdb76512011-09-07 11:43:16 -07001316 /*
1317 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001318 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001319 * control to another statement:
1320 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001321 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001322 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001323 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001324 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001325 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001326 * throw an exception that is handled by an encompassing "try"
1327 * block.
1328 *
1329 * We can also return, in which case there is no successor instruction
1330 * from this point.
1331 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001332 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001333 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001334 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1335 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001336 DecodedInstruction dec_insn(inst);
1337 int opcode_flags = Instruction::Flags(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001338
jeffhaobdb76512011-09-07 11:43:16 -07001339 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001340 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001341 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001342 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001343 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1344 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001345 }
jeffhaobdb76512011-09-07 11:43:16 -07001346
1347 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001348 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001349 * can throw an exception, we will copy/merge this into the "catch"
1350 * address rather than work_line, because we don't want the result
1351 * from the "successful" code path (e.g. a check-cast that "improves"
1352 * a type) to be visible to the exception handler.
1353 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001354 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001355 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001356 } else {
1357#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001358 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001359#endif
1360 }
1361
Elliott Hughesadb8c672012-03-06 16:49:32 -08001362 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07001363 case Instruction::NOP:
1364 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001365 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001366 * a signature that looks like a NOP; if we see one of these in
1367 * the course of executing code then we have a problem.
1368 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001369 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001370 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001371 }
1372 break;
1373
1374 case Instruction::MOVE:
1375 case Instruction::MOVE_FROM16:
1376 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001377 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001378 break;
1379 case Instruction::MOVE_WIDE:
1380 case Instruction::MOVE_WIDE_FROM16:
1381 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001382 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07001383 break;
1384 case Instruction::MOVE_OBJECT:
1385 case Instruction::MOVE_OBJECT_FROM16:
1386 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001387 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001388 break;
1389
1390 /*
1391 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001392 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001393 * might want to hold the result in an actual CPU register, so the
1394 * Dalvik spec requires that these only appear immediately after an
1395 * invoke or filled-new-array.
1396 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001397 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001398 * redundant with the reset done below, but it can make the debug info
1399 * easier to read in some cases.)
1400 */
1401 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001402 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07001403 break;
1404 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001405 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001406 break;
1407 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001408 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07001409 break;
1410
Ian Rogersd81871c2011-10-03 13:57:23 -07001411 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001412 /*
jeffhao60f83e32012-02-13 17:16:30 -08001413 * This statement can only appear as the first instruction in an exception handler. We verify
1414 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001415 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001416 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08001417 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001418 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001419 }
jeffhaobdb76512011-09-07 11:43:16 -07001420 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001421 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1422 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001423 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001424 }
jeffhaobdb76512011-09-07 11:43:16 -07001425 }
1426 break;
1427 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001428 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001429 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001430 const RegType& return_type = GetMethodReturnType();
1431 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001432 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001433 } else {
1434 // Compilers may generate synthetic functions that write byte values into boolean fields.
1435 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001436 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001437 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1438 ((return_type.IsBoolean() || return_type.IsByte() ||
1439 return_type.IsShort() || return_type.IsChar()) &&
1440 src_type.IsInteger()));
1441 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001442 bool success =
1443 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
1444 if (!success) {
1445 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001446 }
jeffhaobdb76512011-09-07 11:43:16 -07001447 }
1448 }
1449 break;
1450 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001451 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001452 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001453 const RegType& return_type = GetMethodReturnType();
1454 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001455 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001456 } else {
1457 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001458 bool success = work_line_->VerifyRegisterType(dec_insn.vA, return_type);
1459 if (!success) {
1460 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001461 }
jeffhaobdb76512011-09-07 11:43:16 -07001462 }
1463 }
1464 break;
1465 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001466 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001467 const RegType& return_type = GetMethodReturnType();
1468 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001469 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001470 } else {
1471 /* return_type is the *expected* return type, not register value */
1472 DCHECK(!return_type.IsZero());
1473 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08001474 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07001475 // Disallow returning uninitialized values and verify that the reference in vAA is an
1476 // instance of the "return_type"
1477 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001478 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001479 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001480 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1481 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001482 }
1483 }
1484 }
1485 break;
1486
1487 case Instruction::CONST_4:
1488 case Instruction::CONST_16:
1489 case Instruction::CONST:
1490 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001491 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const((int32_t) dec_insn.vB));
jeffhaobdb76512011-09-07 11:43:16 -07001492 break;
1493 case Instruction::CONST_HIGH16:
1494 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001495 work_line_->SetRegisterType(dec_insn.vA,
1496 reg_types_.FromCat1Const((int32_t) dec_insn.vB << 16));
jeffhaobdb76512011-09-07 11:43:16 -07001497 break;
1498 case Instruction::CONST_WIDE_16:
1499 case Instruction::CONST_WIDE_32:
1500 case Instruction::CONST_WIDE:
1501 case Instruction::CONST_WIDE_HIGH16:
1502 /* could be long or double; resolved upon use */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001503 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07001504 break;
1505 case Instruction::CONST_STRING:
1506 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001507 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001508 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001509 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001510 // Get type from instruction if unresolved then we need an access check
1511 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08001512 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001513 // Register holds class, ie its type is class, on error it will hold Conflict.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001514 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogersad0b3a32012-04-16 14:50:24 -07001515 res_type.IsConflict() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001516 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001517 }
jeffhaobdb76512011-09-07 11:43:16 -07001518 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001519 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001520 break;
1521 case Instruction::MONITOR_EXIT:
1522 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001523 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001524 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001525 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001526 * to the need to handle asynchronous exceptions, a now-deprecated
1527 * feature that Dalvik doesn't support.)
1528 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001529 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001530 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001531 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001532 * structured locking checks are working, the former would have
1533 * failed on the -enter instruction, and the latter is impossible.
1534 *
1535 * This is fortunate, because issue 3221411 prevents us from
1536 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001537 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001538 * some catch blocks (which will show up as "dead" code when
1539 * we skip them here); if we can't, then the code path could be
1540 * "live" so we still need to check it.
1541 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001542 opcode_flags &= ~Instruction::kThrow;
1543 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001544 break;
1545
Ian Rogers28ad40d2011-10-27 15:19:26 -07001546 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001547 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001548 /*
1549 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1550 * could be a "upcast" -- not expected, so we don't try to address it.)
1551 *
1552 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001553 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001554 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001555 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001556 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001557 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001558 if (res_type.IsConflict()) {
1559 DCHECK_NE(failures_.size(), 0U);
1560 if (!is_checkcast) {
1561 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
1562 }
1563 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001564 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001565 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1566 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001567 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001568 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001569 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001570 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001571 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07001572 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001573 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001574 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001575 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001576 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001577 }
jeffhaobdb76512011-09-07 11:43:16 -07001578 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001579 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001580 }
1581 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001582 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001583 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001584 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001585 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001586 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001587 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001588 }
1589 }
1590 break;
1591 }
1592 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001593 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001594 if (res_type.IsConflict()) {
1595 DCHECK_NE(failures_.size(), 0U);
1596 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001597 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001598 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1599 // can't create an instance of an interface or abstract class */
1600 if (!res_type.IsInstantiableTypes()) {
1601 Fail(VERIFY_ERROR_INSTANTIATION)
1602 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001603 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001604 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001605 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1606 // Any registers holding previous allocations from this address that have not yet been
1607 // initialized must be marked invalid.
1608 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1609 // add the new uninitialized reference to the register state
1610 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001611 break;
1612 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001613 case Instruction::NEW_ARRAY:
1614 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001615 break;
1616 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08001617 VerifyNewArray(dec_insn, true, false);
1618 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001619 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001620 case Instruction::FILLED_NEW_ARRAY_RANGE:
1621 VerifyNewArray(dec_insn, true, true);
1622 just_set_result = true; // Filled new array range sets result register
1623 break;
jeffhaobdb76512011-09-07 11:43:16 -07001624 case Instruction::CMPL_FLOAT:
1625 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001626 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001627 break;
1628 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001629 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001630 break;
1631 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001632 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001633 break;
1634 case Instruction::CMPL_DOUBLE:
1635 case Instruction::CMPG_DOUBLE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001636 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08001637 break;
1638 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001639 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08001640 break;
1641 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001642 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001643 break;
1644 case Instruction::CMP_LONG:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001645 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08001646 break;
1647 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001648 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08001649 break;
1650 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001651 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001652 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001653 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001654 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001655 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001656 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001657 }
1658 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001659 }
jeffhaobdb76512011-09-07 11:43:16 -07001660 case Instruction::GOTO:
1661 case Instruction::GOTO_16:
1662 case Instruction::GOTO_32:
1663 /* no effect on or use of registers */
1664 break;
1665
1666 case Instruction::PACKED_SWITCH:
1667 case Instruction::SPARSE_SWITCH:
1668 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001669 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001670 break;
1671
Ian Rogersd81871c2011-10-03 13:57:23 -07001672 case Instruction::FILL_ARRAY_DATA: {
1673 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001674 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08001675 /* array_type can be null if the reg type is Zero */
1676 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001677 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001678 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001679 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001680 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1681 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001682 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001683 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1684 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001685 } else {
jeffhao457cc512012-02-02 16:55:13 -08001686 // Now verify if the element width in the table matches the element width declared in
1687 // the array
1688 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1689 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001690 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001691 } else {
1692 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1693 // Since we don't compress the data in Dex, expect to see equal width of data stored
1694 // in the table and expected from the array class.
1695 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001696 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1697 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001698 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001699 }
1700 }
jeffhaobdb76512011-09-07 11:43:16 -07001701 }
1702 }
1703 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001704 }
jeffhaobdb76512011-09-07 11:43:16 -07001705 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001706 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001707 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1708 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001709 bool mismatch = false;
1710 if (reg_type1.IsZero()) { // zero then integral or reference expected
1711 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1712 } else if (reg_type1.IsReferenceTypes()) { // both references?
1713 mismatch = !reg_type2.IsReferenceTypes();
1714 } else { // both integral?
1715 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1716 }
1717 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1719 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001720 }
1721 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001722 }
jeffhaobdb76512011-09-07 11:43:16 -07001723 case Instruction::IF_LT:
1724 case Instruction::IF_GE:
1725 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001726 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001727 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1728 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001729 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001730 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1731 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001732 }
1733 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001734 }
jeffhaobdb76512011-09-07 11:43:16 -07001735 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001736 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001737 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001738 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001739 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001740 }
jeffhaobdb76512011-09-07 11:43:16 -07001741 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001742 }
jeffhaobdb76512011-09-07 11:43:16 -07001743 case Instruction::IF_LTZ:
1744 case Instruction::IF_GEZ:
1745 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001746 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001747 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001748 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001749 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1750 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001751 }
jeffhaobdb76512011-09-07 11:43:16 -07001752 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001753 }
jeffhaobdb76512011-09-07 11:43:16 -07001754 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001755 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
1756 break;
jeffhaobdb76512011-09-07 11:43:16 -07001757 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001758 VerifyAGet(dec_insn, reg_types_.Byte(), true);
1759 break;
jeffhaobdb76512011-09-07 11:43:16 -07001760 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07001761 VerifyAGet(dec_insn, reg_types_.Char(), true);
1762 break;
jeffhaobdb76512011-09-07 11:43:16 -07001763 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001764 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001765 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001766 case Instruction::AGET:
1767 VerifyAGet(dec_insn, reg_types_.Integer(), true);
1768 break;
jeffhaobdb76512011-09-07 11:43:16 -07001769 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001770 VerifyAGet(dec_insn, reg_types_.Long(), true);
1771 break;
1772 case Instruction::AGET_OBJECT:
1773 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001774 break;
1775
Ian Rogersd81871c2011-10-03 13:57:23 -07001776 case Instruction::APUT_BOOLEAN:
1777 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
1778 break;
1779 case Instruction::APUT_BYTE:
1780 VerifyAPut(dec_insn, reg_types_.Byte(), true);
1781 break;
1782 case Instruction::APUT_CHAR:
1783 VerifyAPut(dec_insn, reg_types_.Char(), true);
1784 break;
1785 case Instruction::APUT_SHORT:
1786 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001787 break;
1788 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001789 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001790 break;
1791 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001792 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001793 break;
1794 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001795 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001796 break;
1797
jeffhaobdb76512011-09-07 11:43:16 -07001798 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001799 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001800 break;
jeffhaobdb76512011-09-07 11:43:16 -07001801 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001802 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001803 break;
jeffhaobdb76512011-09-07 11:43:16 -07001804 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001805 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001806 break;
jeffhaobdb76512011-09-07 11:43:16 -07001807 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001808 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001809 break;
1810 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001811 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001812 break;
1813 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001814 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001815 break;
1816 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001817 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001818 break;
jeffhaobdb76512011-09-07 11:43:16 -07001819
Ian Rogersd81871c2011-10-03 13:57:23 -07001820 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001821 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001822 break;
1823 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001824 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001825 break;
1826 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001827 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001828 break;
1829 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001830 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001831 break;
1832 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001833 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001834 break;
1835 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001836 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001837 break;
jeffhaobdb76512011-09-07 11:43:16 -07001838 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001839 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001840 break;
1841
jeffhaobdb76512011-09-07 11:43:16 -07001842 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001843 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001844 break;
jeffhaobdb76512011-09-07 11:43:16 -07001845 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001846 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001847 break;
jeffhaobdb76512011-09-07 11:43:16 -07001848 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001849 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001850 break;
jeffhaobdb76512011-09-07 11:43:16 -07001851 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001852 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001853 break;
1854 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001855 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001856 break;
1857 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001858 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001859 break;
1860 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001861 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001862 break;
1863
1864 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001865 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001866 break;
1867 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001868 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001869 break;
1870 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001871 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001872 break;
1873 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001874 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001875 break;
1876 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001877 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001878 break;
1879 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001880 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001881 break;
1882 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001883 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001884 break;
1885
1886 case Instruction::INVOKE_VIRTUAL:
1887 case Instruction::INVOKE_VIRTUAL_RANGE:
1888 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001889 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001890 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
1891 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
1892 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
1893 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001894 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001895 const char* descriptor;
1896 if (called_method == NULL) {
1897 uint32_t method_idx = dec_insn.vB;
1898 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1899 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1900 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1901 } else {
1902 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07001903 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001904 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
1905 work_line_->SetResultRegisterType(return_type);
1906 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001907 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001908 }
jeffhaobdb76512011-09-07 11:43:16 -07001909 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001910 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001911 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001912 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07001913 const char* return_type_descriptor;
1914 bool is_constructor;
1915 if (called_method == NULL) {
1916 uint32_t method_idx = dec_insn.vB;
1917 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1918 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
1919 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1920 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1921 } else {
1922 is_constructor = called_method->IsConstructor();
1923 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
1924 }
1925 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07001926 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001927 * Some additional checks when calling a constructor. We know from the invocation arg check
1928 * that the "this" argument is an instance of called_method->klass. Now we further restrict
1929 * that to require that called_method->klass is the same as this->klass or this->super,
1930 * allowing the latter only if the "this" argument is the same as the "this" argument to
1931 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07001932 */
jeffhaob57e9522012-04-26 18:08:21 -07001933 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1934 if (this_type.IsConflict()) // failure.
1935 break;
jeffhaobdb76512011-09-07 11:43:16 -07001936
jeffhaob57e9522012-04-26 18:08:21 -07001937 /* no null refs allowed (?) */
1938 if (this_type.IsZero()) {
1939 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
1940 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07001941 }
jeffhaob57e9522012-04-26 18:08:21 -07001942
1943 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07001944 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
1945 // TODO: re-enable constructor type verification
1946 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07001947 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07001948 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
1949 // break;
1950 // }
jeffhaob57e9522012-04-26 18:08:21 -07001951
1952 /* arg must be an uninitialized reference */
1953 if (!this_type.IsUninitializedTypes()) {
1954 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
1955 << this_type;
1956 break;
1957 }
1958
1959 /*
1960 * Replace the uninitialized reference with an initialized one. We need to do this for all
1961 * registers that have the same object instance in them, not just the "this" register.
1962 */
1963 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001964 }
Ian Rogers46685432012-06-03 22:26:43 -07001965 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001966 work_line_->SetResultRegisterType(return_type);
1967 just_set_result = true;
1968 break;
1969 }
1970 case Instruction::INVOKE_STATIC:
1971 case Instruction::INVOKE_STATIC_RANGE: {
1972 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
1973 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001974 const char* descriptor;
1975 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001976 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001977 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1978 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07001979 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001980 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001981 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001982 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001983 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07001984 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07001985 just_set_result = true;
1986 }
1987 break;
jeffhaobdb76512011-09-07 11:43:16 -07001988 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001989 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001990 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001991 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001992 if (abs_method != NULL) {
1993 Class* called_interface = abs_method->GetDeclaringClass();
1994 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
1995 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
1996 << PrettyMethod(abs_method) << "'";
1997 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001998 }
Ian Rogers0d604842012-04-16 14:50:24 -07001999 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002000 /* Get the type of the "this" arg, which should either be a sub-interface of called
2001 * interface or Object (see comments in RegType::JoinClass).
2002 */
2003 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2004 if (this_type.IsZero()) {
2005 /* null pointer always passes (and always fails at runtime) */
2006 } else {
2007 if (this_type.IsUninitializedTypes()) {
2008 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2009 << this_type;
2010 break;
2011 }
2012 // In the past we have tried to assert that "called_interface" is assignable
2013 // from "this_type.GetClass()", however, as we do an imprecise Join
2014 // (RegType::JoinClass) we don't have full information on what interfaces are
2015 // implemented by "this_type". For example, two classes may implement the same
2016 // interfaces and have a common parent that doesn't implement the interface. The
2017 // join will set "this_type" to the parent class and a test that this implements
2018 // the interface will incorrectly fail.
2019 }
2020 /*
2021 * We don't have an object instance, so we can't find the concrete method. However, all of
2022 * the type information is in the abstract method, so we're good.
2023 */
2024 const char* descriptor;
2025 if (abs_method == NULL) {
2026 uint32_t method_idx = dec_insn.vB;
2027 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2028 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2029 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2030 } else {
2031 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2032 }
2033 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
2034 work_line_->SetResultRegisterType(return_type);
2035 work_line_->SetResultRegisterType(return_type);
2036 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002037 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002038 }
jeffhaobdb76512011-09-07 11:43:16 -07002039 case Instruction::NEG_INT:
2040 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002041 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002042 break;
2043 case Instruction::NEG_LONG:
2044 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002045 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002046 break;
2047 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002048 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002049 break;
2050 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002051 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002052 break;
2053 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002054 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002055 break;
2056 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002057 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002058 break;
2059 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002060 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002061 break;
2062 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002063 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002064 break;
2065 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002066 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002067 break;
2068 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002069 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002070 break;
2071 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002072 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002073 break;
2074 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002075 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002076 break;
2077 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002078 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002079 break;
2080 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002081 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002082 break;
2083 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002084 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002085 break;
2086 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002087 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002088 break;
2089 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002090 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002091 break;
2092 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002093 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002094 break;
2095 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002096 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002097 break;
2098
2099 case Instruction::ADD_INT:
2100 case Instruction::SUB_INT:
2101 case Instruction::MUL_INT:
2102 case Instruction::REM_INT:
2103 case Instruction::DIV_INT:
2104 case Instruction::SHL_INT:
2105 case Instruction::SHR_INT:
2106 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002107 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002108 break;
2109 case Instruction::AND_INT:
2110 case Instruction::OR_INT:
2111 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002112 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002113 break;
2114 case Instruction::ADD_LONG:
2115 case Instruction::SUB_LONG:
2116 case Instruction::MUL_LONG:
2117 case Instruction::DIV_LONG:
2118 case Instruction::REM_LONG:
2119 case Instruction::AND_LONG:
2120 case Instruction::OR_LONG:
2121 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002122 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002123 break;
2124 case Instruction::SHL_LONG:
2125 case Instruction::SHR_LONG:
2126 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002127 /* shift distance is Int, making these different from other binary operations */
2128 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002129 break;
2130 case Instruction::ADD_FLOAT:
2131 case Instruction::SUB_FLOAT:
2132 case Instruction::MUL_FLOAT:
2133 case Instruction::DIV_FLOAT:
2134 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002135 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002136 break;
2137 case Instruction::ADD_DOUBLE:
2138 case Instruction::SUB_DOUBLE:
2139 case Instruction::MUL_DOUBLE:
2140 case Instruction::DIV_DOUBLE:
2141 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002142 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002143 break;
2144 case Instruction::ADD_INT_2ADDR:
2145 case Instruction::SUB_INT_2ADDR:
2146 case Instruction::MUL_INT_2ADDR:
2147 case Instruction::REM_INT_2ADDR:
2148 case Instruction::SHL_INT_2ADDR:
2149 case Instruction::SHR_INT_2ADDR:
2150 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002151 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002152 break;
2153 case Instruction::AND_INT_2ADDR:
2154 case Instruction::OR_INT_2ADDR:
2155 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002156 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002157 break;
2158 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002159 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002160 break;
2161 case Instruction::ADD_LONG_2ADDR:
2162 case Instruction::SUB_LONG_2ADDR:
2163 case Instruction::MUL_LONG_2ADDR:
2164 case Instruction::DIV_LONG_2ADDR:
2165 case Instruction::REM_LONG_2ADDR:
2166 case Instruction::AND_LONG_2ADDR:
2167 case Instruction::OR_LONG_2ADDR:
2168 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002169 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002170 break;
2171 case Instruction::SHL_LONG_2ADDR:
2172 case Instruction::SHR_LONG_2ADDR:
2173 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002174 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002175 break;
2176 case Instruction::ADD_FLOAT_2ADDR:
2177 case Instruction::SUB_FLOAT_2ADDR:
2178 case Instruction::MUL_FLOAT_2ADDR:
2179 case Instruction::DIV_FLOAT_2ADDR:
2180 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002181 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002182 break;
2183 case Instruction::ADD_DOUBLE_2ADDR:
2184 case Instruction::SUB_DOUBLE_2ADDR:
2185 case Instruction::MUL_DOUBLE_2ADDR:
2186 case Instruction::DIV_DOUBLE_2ADDR:
2187 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002188 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002189 break;
2190 case Instruction::ADD_INT_LIT16:
2191 case Instruction::RSUB_INT:
2192 case Instruction::MUL_INT_LIT16:
2193 case Instruction::DIV_INT_LIT16:
2194 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002195 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002196 break;
2197 case Instruction::AND_INT_LIT16:
2198 case Instruction::OR_INT_LIT16:
2199 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002200 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002201 break;
2202 case Instruction::ADD_INT_LIT8:
2203 case Instruction::RSUB_INT_LIT8:
2204 case Instruction::MUL_INT_LIT8:
2205 case Instruction::DIV_INT_LIT8:
2206 case Instruction::REM_INT_LIT8:
2207 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002208 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002209 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002210 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002211 break;
2212 case Instruction::AND_INT_LIT8:
2213 case Instruction::OR_INT_LIT8:
2214 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002215 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002216 break;
2217
Ian Rogersd81871c2011-10-03 13:57:23 -07002218 /* These should never appear during verification. */
jeffhao9a4f0032012-08-30 16:17:40 -07002219 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002220 case Instruction::UNUSED_EE:
2221 case Instruction::UNUSED_EF:
2222 case Instruction::UNUSED_F2:
2223 case Instruction::UNUSED_F3:
2224 case Instruction::UNUSED_F4:
2225 case Instruction::UNUSED_F5:
2226 case Instruction::UNUSED_F6:
2227 case Instruction::UNUSED_F7:
2228 case Instruction::UNUSED_F8:
2229 case Instruction::UNUSED_F9:
2230 case Instruction::UNUSED_FA:
2231 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002232 case Instruction::UNUSED_F0:
2233 case Instruction::UNUSED_F1:
2234 case Instruction::UNUSED_E3:
2235 case Instruction::UNUSED_E8:
2236 case Instruction::UNUSED_E7:
2237 case Instruction::UNUSED_E4:
2238 case Instruction::UNUSED_E9:
2239 case Instruction::UNUSED_FC:
2240 case Instruction::UNUSED_E5:
2241 case Instruction::UNUSED_EA:
2242 case Instruction::UNUSED_FD:
2243 case Instruction::UNUSED_E6:
2244 case Instruction::UNUSED_EB:
2245 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002246 case Instruction::UNUSED_3E:
2247 case Instruction::UNUSED_3F:
2248 case Instruction::UNUSED_40:
2249 case Instruction::UNUSED_41:
2250 case Instruction::UNUSED_42:
2251 case Instruction::UNUSED_43:
2252 case Instruction::UNUSED_73:
2253 case Instruction::UNUSED_79:
2254 case Instruction::UNUSED_7A:
2255 case Instruction::UNUSED_EC:
2256 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002257 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002258 break;
2259
2260 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002261 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002262 * complain if an instruction is missing (which is desirable).
2263 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002264 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002265
Ian Rogersad0b3a32012-04-16 14:50:24 -07002266 if (have_pending_hard_failure_) {
2267 if (!Runtime::Current()->IsStarted()) {
jeffhaob57e9522012-04-26 18:08:21 -07002268 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002269 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002270 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002271 /* immediate failure, reject class */
2272 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2273 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002274 } else if (have_pending_runtime_throw_failure_) {
2275 /* slow path will throw, mark following code as unreachable */
2276 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002277 }
jeffhaobdb76512011-09-07 11:43:16 -07002278 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002279 * If we didn't just set the result register, clear it out. This ensures that you can only use
2280 * "move-result" immediately after the result is set. (We could check this statically, but it's
2281 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002282 */
2283 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002284 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002285 }
2286
jeffhaoa0a764a2011-09-16 10:43:38 -07002287 /* Handle "continue". Tag the next consecutive instruction. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002288 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers776ac1f2012-04-13 23:36:36 -07002289 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
Ian Rogersd81871c2011-10-03 13:57:23 -07002290 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
jeffhaod5347e02012-03-22 17:25:05 -07002291 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002292 return false;
2293 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002294 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2295 // next instruction isn't one.
jeffhaod5347e02012-03-22 17:25:05 -07002296 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002297 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002298 }
2299 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2300 if (next_line != NULL) {
2301 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2302 // needed.
2303 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002304 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002305 }
jeffhaobdb76512011-09-07 11:43:16 -07002306 } else {
2307 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002308 * We're not recording register data for the next instruction, so we don't know what the prior
2309 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002310 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002311 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002312 }
2313 }
2314
2315 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002316 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002317 *
2318 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002319 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002320 * somebody could get a reference field, check it for zero, and if the
2321 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002322 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002323 * that, and will reject the code.
2324 *
2325 * TODO: avoid re-fetching the branch target
2326 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002327 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002328 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002329 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002330 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002331 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002332 return false;
2333 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002334 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002335 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002336 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002337 }
jeffhaobdb76512011-09-07 11:43:16 -07002338 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002339 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002340 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002341 }
jeffhaobdb76512011-09-07 11:43:16 -07002342 }
2343
2344 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002345 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002346 *
2347 * We've already verified that the table is structurally sound, so we
2348 * just need to walk through and tag the targets.
2349 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002350 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002351 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2352 const uint16_t* switch_insns = insns + offset_to_switch;
2353 int switch_count = switch_insns[1];
2354 int offset_to_targets, targ;
2355
2356 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2357 /* 0 = sig, 1 = count, 2/3 = first key */
2358 offset_to_targets = 4;
2359 } else {
2360 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002361 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002362 offset_to_targets = 2 + 2 * switch_count;
2363 }
2364
2365 /* verify each switch target */
2366 for (targ = 0; targ < switch_count; targ++) {
2367 int offset;
2368 uint32_t abs_offset;
2369
2370 /* offsets are 32-bit, and only partly endian-swapped */
2371 offset = switch_insns[offset_to_targets + targ * 2] |
2372 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002373 abs_offset = work_insn_idx_ + offset;
2374 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002375 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002376 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002377 }
2378 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002379 return false;
2380 }
2381 }
2382
2383 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002384 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2385 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002386 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002387 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002388 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002389 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002390
Ian Rogers0571d352011-11-03 19:51:38 -07002391 for (; iterator.HasNext(); iterator.Next()) {
2392 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002393 within_catch_all = true;
2394 }
jeffhaobdb76512011-09-07 11:43:16 -07002395 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002396 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2397 * "work_regs", because at runtime the exception will be thrown before the instruction
2398 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002399 */
Ian Rogers0571d352011-11-03 19:51:38 -07002400 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002401 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002402 }
jeffhaobdb76512011-09-07 11:43:16 -07002403 }
2404
2405 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002406 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2407 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002408 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002409 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002410 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002411 * The state in work_line reflects the post-execution state. If the current instruction is a
2412 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002413 * it will do so before grabbing the lock).
2414 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002415 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002416 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002417 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002418 return false;
2419 }
2420 }
2421 }
2422
jeffhaod1f0fde2011-09-08 17:25:33 -07002423 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002424 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002425 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002426 return false;
2427 }
jeffhaobdb76512011-09-07 11:43:16 -07002428 }
2429
2430 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002431 * Update start_guess. Advance to the next instruction of that's
2432 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002433 * neither of those exists we're in a return or throw; leave start_guess
2434 * alone and let the caller sort it out.
2435 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002436 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002437 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002438 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002439 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002440 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002441 }
2442
Ian Rogersd81871c2011-10-03 13:57:23 -07002443 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2444 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002445
2446 return true;
2447}
2448
Ian Rogers776ac1f2012-04-13 23:36:36 -07002449const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002450 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002451 const RegType& referrer = GetDeclaringClass();
2452 Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002453 const RegType& result =
2454 klass != NULL ? reg_types_.FromClass(klass)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002455 : reg_types_.FromDescriptor(class_loader_, descriptor);
2456 if (result.IsConflict()) {
2457 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2458 << "' in " << referrer;
2459 return result;
2460 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002461 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002462 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002463 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002464 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002465 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002466 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002467 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002468 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002469 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002470 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002471}
2472
Ian Rogers776ac1f2012-04-13 23:36:36 -07002473const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002474 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002475 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002476 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002477 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2478 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002479 CatchHandlerIterator iterator(handlers_ptr);
2480 for (; iterator.HasNext(); iterator.Next()) {
2481 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2482 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002483 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07002484 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002485 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002486 if (common_super == NULL) {
2487 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2488 // as that is caught at runtime
2489 common_super = &exception;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002490 } else if (!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002491 // We don't know enough about the type and the common path merge will result in
2492 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002493 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002494 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002495 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002496 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002497 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002498 common_super = &common_super->Merge(exception, &reg_types_);
2499 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002500 }
2501 }
2502 }
2503 }
Ian Rogers0571d352011-11-03 19:51:38 -07002504 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002505 }
2506 }
2507 if (common_super == NULL) {
2508 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002509 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002510 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002511 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002512 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002513}
2514
Ian Rogersad0b3a32012-04-16 14:50:24 -07002515Method* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx, MethodType method_type) {
2516 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002517 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002518 if (klass_type.IsConflict()) {
2519 std::string append(" in attempt to access method ");
2520 append += dex_file_->GetMethodName(method_id);
2521 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002522 return NULL;
2523 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002524 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002525 return NULL; // Can't resolve Class so no more to do here
2526 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002527 Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002528 const RegType& referrer = GetDeclaringClass();
2529 Method* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002530 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002531 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002532 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002533
2534 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002535 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002536 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002537 res_method = klass->FindInterfaceMethod(name, signature);
2538 } else {
2539 res_method = klass->FindVirtualMethod(name, signature);
2540 }
2541 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002542 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002543 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002544 // If a virtual or interface method wasn't found with the expected type, look in
2545 // the direct methods. This can happen when the wrong invoke type is used or when
2546 // a class has changed, and will be flagged as an error in later checks.
2547 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2548 res_method = klass->FindDirectMethod(name, signature);
2549 }
2550 if (res_method == NULL) {
2551 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2552 << PrettyDescriptor(klass) << "." << name
2553 << " " << signature;
2554 return NULL;
2555 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002556 }
2557 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002558 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2559 // enforce them here.
2560 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002561 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2562 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002563 return NULL;
2564 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002565 // Disallow any calls to class initializers.
2566 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002567 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2568 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002569 return NULL;
2570 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002571 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002572 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002573 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002574 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002575 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002576 }
jeffhaode0d9c92012-02-27 13:58:13 -08002577 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2578 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002579 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2580 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002581 return NULL;
2582 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002583 // Check that interface methods match interface classes.
2584 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2585 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2586 << " is in an interface class " << PrettyClass(klass);
2587 return NULL;
2588 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2589 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2590 << " is in a non-interface class " << PrettyClass(klass);
2591 return NULL;
2592 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002593 // See if the method type implied by the invoke instruction matches the access flags for the
2594 // target method.
2595 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2596 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2597 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2598 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002599 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2600 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002601 return NULL;
2602 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002603 return res_method;
2604}
2605
Ian Rogers776ac1f2012-04-13 23:36:36 -07002606Method* MethodVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
Ian Rogers46685432012-06-03 22:26:43 -07002607 MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002608 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2609 // we're making.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002610 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002611 if (res_method == NULL) { // error or class is unresolved
2612 return NULL;
2613 }
2614
Ian Rogersd81871c2011-10-03 13:57:23 -07002615 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2616 // has a vtable entry for the target method.
2617 if (is_super) {
2618 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002619 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002620 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002621 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
2622 << PrettyMethod(method_idx_, *dex_file_)
2623 << " to super " << PrettyMethod(res_method);
2624 return NULL;
2625 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002626 Class* super_klass = super.GetClass();
2627 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002628 MethodHelper mh(res_method);
2629 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
2630 << PrettyMethod(method_idx_, *dex_file_)
2631 << " to super " << super
2632 << "." << mh.GetName()
2633 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002634 return NULL;
2635 }
2636 }
2637 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2638 // match the call to the signature. Also, we might might be calling through an abstract method
2639 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08002640 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002641 /* caught by static verifier */
2642 DCHECK(is_range || expected_args <= 5);
2643 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002644 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002645 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2646 return NULL;
2647 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002648
jeffhaobdb76512011-09-07 11:43:16 -07002649 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002650 * Check the "this" argument, which must be an instance of the class that declared the method.
2651 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2652 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002653 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002654 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002655 if (!res_method->IsStatic()) {
2656 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002657 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002658 return NULL;
2659 }
2660 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002661 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002662 return NULL;
2663 }
2664 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07002665 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
2666 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002667 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002668 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002669 return NULL;
2670 }
2671 }
2672 actual_args++;
2673 }
2674 /*
2675 * Process the target method's signature. This signature may or may not
2676 * have been verified, so we can't assume it's properly formed.
2677 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002678 MethodHelper mh(res_method);
2679 const DexFile::TypeList* params = mh.GetParameterTypeList();
2680 size_t params_size = params == NULL ? 0 : params->Size();
2681 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002682 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002683 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002684 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2685 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002686 return NULL;
2687 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002688 const char* descriptor =
2689 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2690 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002691 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002692 << " missing signature component";
2693 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002694 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002695 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002696 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002697 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002698 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002699 }
2700 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2701 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002702 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002703 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002704 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002705 return NULL;
2706 } else {
2707 return res_method;
2708 }
2709}
2710
Ian Rogers776ac1f2012-04-13 23:36:36 -07002711void MethodVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08002712 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002713 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002714 if (res_type.IsConflict()) { // bad class
2715 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002716 } else {
2717 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2718 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002719 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002720 } else if (!is_filled) {
2721 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002722 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002723 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002724 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002725 } else {
2726 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2727 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002728 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002729 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002730 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002731 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002732 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002733 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002734 return;
2735 }
2736 }
2737 // filled-array result goes into "result" register
2738 work_line_->SetResultRegisterType(res_type);
2739 }
2740 }
2741}
2742
Ian Rogers776ac1f2012-04-13 23:36:36 -07002743void MethodVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002744 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002745 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002746 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002748 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002749 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002750 if (array_type.IsZero()) {
2751 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2752 // instruction type. TODO: have a proper notion of bottom here.
2753 if (!is_primitive || insn_type.IsCategory1Types()) {
2754 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08002755 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002756 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002757 // Category 2
Elliott Hughesadb8c672012-03-06 16:49:32 -08002758 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
Ian Rogers89310de2012-02-01 13:47:30 -08002759 }
jeffhaofc3144e2012-02-01 17:21:15 -08002760 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002761 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002762 } else {
2763 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002764 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002765 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002766 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002767 << " source for aget-object";
2768 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002769 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002770 << " source for category 1 aget";
2771 } else if (is_primitive && !insn_type.Equals(component_type) &&
2772 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2773 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002774 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogersd81871c2011-10-03 13:57:23 -07002775 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002776 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002777 // Use knowledge of the field type which is stronger than the type inferred from the
2778 // instruction, which can't differentiate object types and ints from floats, longs from
2779 // doubles.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002780 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002781 }
2782 }
2783 }
2784}
2785
Ian Rogers776ac1f2012-04-13 23:36:36 -07002786void MethodVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002787 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002788 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002789 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002790 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002791 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002792 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002793 if (array_type.IsZero()) {
2794 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2795 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002796 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002797 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002798 } else {
2799 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002800 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002801 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002802 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002803 << " source for aput-object";
2804 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002805 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002806 << " source for category 1 aput";
2807 } else if (is_primitive && !insn_type.Equals(component_type) &&
2808 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2809 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002810 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002811 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002812 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002813 // The instruction agrees with the type of array, confirm the value to be stored does too
2814 // Note: we use the instruction type (rather than the component type) for aput-object as
2815 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08002816 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 }
2818 }
2819 }
2820}
2821
Ian Rogers776ac1f2012-04-13 23:36:36 -07002822Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002823 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2824 // Check access to class
2825 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002826 if (klass_type.IsConflict()) { // bad class
2827 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
2828 field_idx, dex_file_->GetFieldName(field_id),
2829 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002830 return NULL;
2831 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002832 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002833 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08002834 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002835 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2836 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002837 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002838 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
2839 << dex_file_->GetFieldName(field_id) << ") in "
2840 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 DCHECK(Thread::Current()->IsExceptionPending());
2842 Thread::Current()->ClearException();
2843 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002844 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2845 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002846 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002847 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002848 return NULL;
2849 } else if (!field->IsStatic()) {
2850 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
2851 return NULL;
2852 } else {
2853 return field;
2854 }
2855}
2856
Ian Rogers776ac1f2012-04-13 23:36:36 -07002857Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002858 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2859 // Check access to class
2860 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002861 if (klass_type.IsConflict()) {
2862 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
2863 field_idx, dex_file_->GetFieldName(field_id),
2864 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002865 return NULL;
2866 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002867 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002868 return NULL; // Can't resolve Class so no more to do here
2869 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002870 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2871 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002872 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002873 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
2874 << dex_file_->GetFieldName(field_id) << ") in "
2875 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002876 DCHECK(Thread::Current()->IsExceptionPending());
2877 Thread::Current()->ClearException();
2878 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002879 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2880 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002881 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002882 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002883 return NULL;
2884 } else if (field->IsStatic()) {
2885 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
2886 << " to not be static";
2887 return NULL;
2888 } else if (obj_type.IsZero()) {
2889 // Cannot infer and check type, however, access will cause null pointer exception
2890 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07002891 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002892 const RegType& field_klass = reg_types_.FromClass(field->GetDeclaringClass());
2893 if (obj_type.IsUninitializedTypes() &&
2894 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
2895 !field_klass.Equals(GetDeclaringClass()))) {
2896 // Field accesses through uninitialized references are only allowable for constructors where
2897 // the field is declared in this class
2898 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
2899 << " of a not fully initialized object within the context of "
2900 << PrettyMethod(method_idx_, *dex_file_);
2901 return NULL;
2902 } else if (!field_klass.IsAssignableFrom(obj_type)) {
2903 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
2904 // of C1. For resolution to occur the declared class of the field must be compatible with
2905 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
2906 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
2907 << " from object of type " << obj_type;
2908 return NULL;
2909 } else {
2910 return field;
2911 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002912 }
2913}
2914
Ian Rogers776ac1f2012-04-13 23:36:36 -07002915void MethodVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002916 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002917 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002918 Field* field;
2919 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002920 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002921 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002922 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07002923 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002924 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002925 const char* descriptor;
Ian Rogers365c1022012-06-22 15:05:28 -07002926 ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002927 if (field != NULL) {
2928 descriptor = FieldHelper(field).GetTypeDescriptor();
2929 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07002930 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002931 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2932 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2933 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07002934 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002935 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
2936 if (is_primitive) {
2937 if (field_type.Equals(insn_type) ||
2938 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
2939 (field_type.IsDouble() && insn_type.IsLongTypes())) {
2940 // expected that read is of the correct primitive type or that int reads are reading
2941 // floats or long reads are reading doubles
2942 } else {
2943 // This is a global failure rather than a class change failure as the instructions and
2944 // the descriptors for the type should have been consistent within the same file at
2945 // compile time
2946 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
2947 << " to be of type '" << insn_type
2948 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002949 return;
2950 }
2951 } else {
2952 if (!insn_type.IsAssignableFrom(field_type)) {
2953 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
2954 << " to be compatible with type '" << insn_type
2955 << "' but found type '" << field_type
2956 << "' in get-object";
2957 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Conflict());
2958 return;
2959 }
2960 }
2961 work_line_->SetRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002962}
2963
Ian Rogers776ac1f2012-04-13 23:36:36 -07002964void MethodVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002965 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002966 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002967 Field* field;
2968 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07002969 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002970 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002971 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07002972 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002973 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002974 const char* descriptor;
Ian Rogers365c1022012-06-22 15:05:28 -07002975 ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002976 if (field != NULL) {
2977 descriptor = FieldHelper(field).GetTypeDescriptor();
2978 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07002979 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002980 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2981 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2982 loader = class_loader_;
2983 }
2984 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
2985 if (field != NULL) {
2986 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
2987 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
2988 << " from other class " << GetDeclaringClass();
2989 return;
2990 }
2991 }
2992 if (is_primitive) {
2993 // Primitive field assignability rules are weaker than regular assignability rules
2994 bool instruction_compatible;
2995 bool value_compatible;
2996 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
2997 if (field_type.IsIntegralTypes()) {
2998 instruction_compatible = insn_type.IsIntegralTypes();
2999 value_compatible = value_type.IsIntegralTypes();
3000 } else if (field_type.IsFloat()) {
3001 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3002 value_compatible = value_type.IsFloatTypes();
3003 } else if (field_type.IsLong()) {
3004 instruction_compatible = insn_type.IsLong();
3005 value_compatible = value_type.IsLongTypes();
3006 } else if (field_type.IsDouble()) {
3007 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3008 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003009 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003010 instruction_compatible = false; // reference field with primitive store
3011 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003012 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003013 if (!instruction_compatible) {
3014 // This is a global failure rather than a class change failure as the instructions and
3015 // the descriptors for the type should have been consistent within the same file at
3016 // compile time
3017 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3018 << " to be of type '" << insn_type
3019 << "' but found type '" << field_type
3020 << "' in put";
3021 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003022 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003023 if (!value_compatible) {
3024 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
3025 << " of type " << value_type
3026 << " but expected " << field_type
3027 << " for store to " << PrettyField(field) << " in put";
3028 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003029 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003030 } else {
3031 if (!insn_type.IsAssignableFrom(field_type)) {
3032 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3033 << " to be compatible with type '" << insn_type
3034 << "' but found type '" << field_type
3035 << "' in put-object";
3036 return;
3037 }
3038 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003039 }
3040}
3041
Ian Rogers776ac1f2012-04-13 23:36:36 -07003042bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003043 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003044 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003045 return false;
3046 }
3047 return true;
3048}
3049
Ian Rogers776ac1f2012-04-13 23:36:36 -07003050bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003051 bool changed = true;
3052 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3053 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003054 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003055 * We haven't processed this instruction before, and we haven't touched the registers here, so
3056 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3057 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003058 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003059 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003060 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003061 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3062 if (gDebugVerify) {
3063 copy->CopyFromLine(target_line);
3064 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003065 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003066 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003067 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003068 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003069 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003070 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003071 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3072 << *copy.get() << " MERGE\n"
3073 << *merge_line << " ==\n"
3074 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003075 }
3076 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003077 if (changed) {
3078 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003079 }
3080 return true;
3081}
3082
Ian Rogers776ac1f2012-04-13 23:36:36 -07003083InsnFlags* MethodVerifier::CurrentInsnFlags() {
3084 return &insn_flags_[work_insn_idx_];
3085}
3086
Ian Rogersad0b3a32012-04-16 14:50:24 -07003087const RegType& MethodVerifier::GetMethodReturnType() {
3088 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx_);
3089 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3090 uint16_t return_type_idx = proto_id.return_type_idx_;
3091 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
3092 return reg_types_.FromDescriptor(class_loader_, descriptor);
3093}
3094
3095const RegType& MethodVerifier::GetDeclaringClass() {
3096 if (foo_method_ != NULL) {
3097 return reg_types_.FromClass(foo_method_->GetDeclaringClass());
3098 } else {
3099 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx_);
3100 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
3101 return reg_types_.FromDescriptor(class_loader_, descriptor);
3102 }
3103}
3104
Ian Rogers776ac1f2012-04-13 23:36:36 -07003105void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003106 size_t* log2_max_gc_pc) {
3107 size_t local_gc_points = 0;
3108 size_t max_insn = 0;
3109 size_t max_ref_reg = -1;
3110 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3111 if (insn_flags_[i].IsGcPoint()) {
3112 local_gc_points++;
3113 max_insn = i;
3114 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003115 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003116 }
3117 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003118 *gc_points = local_gc_points;
3119 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3120 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003121 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003122 i++;
3123 }
3124 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003125}
3126
Ian Rogers776ac1f2012-04-13 23:36:36 -07003127const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003128 size_t num_entries, ref_bitmap_bits, pc_bits;
3129 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3130 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003131 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003132 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003133 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003134 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003135 return NULL;
3136 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003137 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3138 // There are 2 bytes to encode the number of entries
3139 if (num_entries >= 65536) {
3140 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003141 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003142 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003143 return NULL;
3144 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003145 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003146 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003147 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003148 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003149 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003150 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003151 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003152 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003153 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003154 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003155 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003156 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3157 return NULL;
3158 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003159 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003160 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003161 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003162 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003163 return NULL;
3164 }
3165 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003166 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3167 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003168 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003169 table->push_back(num_entries & 0xFF);
3170 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003171 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003172 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3173 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003174 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003175 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003176 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003177 }
3178 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003179 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003180 }
3181 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003182 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003183 return table;
3184}
jeffhaoa0a764a2011-09-16 10:43:38 -07003185
Ian Rogers776ac1f2012-04-13 23:36:36 -07003186void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003187 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3188 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003189 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003190 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003191 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003192 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3193 if (insn_flags_[i].IsGcPoint()) {
3194 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003195 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003196 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3197 map_index++;
3198 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003199 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003200 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003201 CHECK_LT(j / 8, map.RegWidth());
3202 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3203 } else if ((j / 8) < map.RegWidth()) {
3204 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3205 } else {
3206 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3207 }
3208 }
3209 } else {
3210 CHECK(reg_bitmap == NULL);
3211 }
3212 }
3213}
jeffhaoa0a764a2011-09-16 10:43:38 -07003214
Ian Rogers776ac1f2012-04-13 23:36:36 -07003215void MethodVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003216 {
3217 MutexLock mu(*gc_maps_lock_);
3218 GcMapTable::iterator it = gc_maps_->find(ref);
3219 if (it != gc_maps_->end()) {
3220 delete it->second;
3221 gc_maps_->erase(it);
3222 }
3223 gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003224 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003225 CHECK(GetGcMap(ref) != NULL);
3226}
3227
Ian Rogers776ac1f2012-04-13 23:36:36 -07003228const std::vector<uint8_t>* MethodVerifier::GetGcMap(Compiler::MethodReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003229 MutexLock mu(*gc_maps_lock_);
3230 GcMapTable::const_iterator it = gc_maps_->find(ref);
3231 if (it == gc_maps_->end()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003232 return NULL;
3233 }
3234 CHECK(it->second != NULL);
3235 return it->second;
3236}
3237
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003238Mutex* MethodVerifier::gc_maps_lock_ = NULL;
3239MethodVerifier::GcMapTable* MethodVerifier::gc_maps_ = NULL;
3240
3241Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
3242MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
3243
3244#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
3245Mutex* MethodVerifier::inferred_reg_category_maps_lock_ = NULL;
3246MethodVerifier::InferredRegCategoryMapTable* MethodVerifier::inferred_reg_category_maps_ = NULL;
3247#endif
3248
3249void MethodVerifier::Init() {
3250 gc_maps_lock_ = new Mutex("verifier GC maps lock");
3251 {
3252 MutexLock mu(*gc_maps_lock_);
3253 gc_maps_ = new MethodVerifier::GcMapTable;
3254 }
3255
3256 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
3257 {
3258 MutexLock mu(*rejected_classes_lock_);
3259 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
3260 }
3261
3262#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
3263 inferred_reg_category_maps_lock_ = new Mutex("verifier GC maps lock");
3264 {
3265 MutexLock mu(*inferred_reg_category_maps_lock_);
3266 inferred_reg_category_maps_ = new MethodVerifier::InferredRegCategoryMapTable;
3267 }
3268#endif
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003269}
3270
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003271void MethodVerifier::Shutdown() {
3272 {
3273 MutexLock mu(*gc_maps_lock_);
3274 STLDeleteValues(gc_maps_);
3275 delete gc_maps_;
3276 gc_maps_ = NULL;
3277 }
3278 delete gc_maps_lock_;
3279 gc_maps_lock_ = NULL;
3280
3281 {
3282 MutexLock mu(*rejected_classes_lock_);
3283 delete rejected_classes_;
3284 rejected_classes_ = NULL;
3285 }
3286 delete rejected_classes_lock_;
3287 rejected_classes_lock_ = NULL;
3288
3289#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
3290 {
3291 MutexLock mu(*inferred_reg_category_maps_lock_);
3292 STLDeleteValues(inferred_reg_category_maps_);
3293 delete inferred_reg_category_maps_;
3294 inferred_reg_category_maps_ = NULL;
3295 }
3296 delete inferred_reg_category_maps_lock_;
3297 inferred_reg_category_maps_lock_ = NULL;
3298#endif
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003299}
jeffhaod1224c72012-02-29 13:43:08 -08003300
Ian Rogers776ac1f2012-04-13 23:36:36 -07003301void MethodVerifier::AddRejectedClass(Compiler::ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003302 {
3303 MutexLock mu(*rejected_classes_lock_);
3304 rejected_classes_->insert(ref);
3305 }
jeffhaod1224c72012-02-29 13:43:08 -08003306 CHECK(IsClassRejected(ref));
3307}
3308
Ian Rogers776ac1f2012-04-13 23:36:36 -07003309bool MethodVerifier::IsClassRejected(Compiler::ClassReference ref) {
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003310 MutexLock mu(*rejected_classes_lock_);
3311 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08003312}
3313
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -07003314#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
TDYa12789f96052012-07-12 20:49:53 -07003315const greenland::InferredRegCategoryMap* MethodVerifier::GenerateInferredRegCategoryMap() {
Logan Chienfca7e872011-12-20 20:08:22 +08003316 uint32_t insns_size = code_item_->insns_size_in_code_units_;
3317 uint16_t regs_size = code_item_->registers_size_;
3318
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003319 UniquePtr<InferredRegCategoryMap> table(new InferredRegCategoryMap(insns_size, regs_size));
Logan Chienfca7e872011-12-20 20:08:22 +08003320
3321 for (size_t i = 0; i < insns_size; ++i) {
3322 if (RegisterLine* line = reg_table_.GetLine(i)) {
TDYa127526643e2012-05-26 01:01:48 -07003323 const Instruction* inst = Instruction::At(code_item_->insns_ + i);
3324
3325 // GC points
3326 if (inst->IsBranch() || inst->IsInvoke()) {
3327 for (size_t r = 0; r < regs_size; ++r) {
3328 const RegType &rt = line->GetRegisterType(r);
3329 if (rt.IsNonZeroReferenceTypes()) {
3330 table->SetRegCanBeObject(r);
3331 }
TDYa127b2eb5c12012-05-24 15:52:10 -07003332 }
3333 }
3334
TDYa127526643e2012-05-26 01:01:48 -07003335 /* We only use InferredRegCategoryMap in one case */
3336 if (inst->IsBranch()) {
TDYa127b2eb5c12012-05-24 15:52:10 -07003337 for (size_t r = 0; r < regs_size; ++r) {
3338 const RegType &rt = line->GetRegisterType(r);
3339
3340 if (rt.IsZero()) {
TDYa12789f96052012-07-12 20:49:53 -07003341 table->SetRegCategory(i, r, greenland::kRegZero);
TDYa127b2eb5c12012-05-24 15:52:10 -07003342 } else if (rt.IsCategory1Types()) {
TDYa12789f96052012-07-12 20:49:53 -07003343 table->SetRegCategory(i, r, greenland::kRegCat1nr);
TDYa127b2eb5c12012-05-24 15:52:10 -07003344 } else if (rt.IsCategory2Types()) {
TDYa12789f96052012-07-12 20:49:53 -07003345 table->SetRegCategory(i, r, greenland::kRegCat2);
TDYa127b2eb5c12012-05-24 15:52:10 -07003346 } else if (rt.IsReferenceTypes()) {
TDYa12789f96052012-07-12 20:49:53 -07003347 table->SetRegCategory(i, r, greenland::kRegObject);
TDYa127b2eb5c12012-05-24 15:52:10 -07003348 } else {
TDYa12789f96052012-07-12 20:49:53 -07003349 table->SetRegCategory(i, r, greenland::kRegUnknown);
TDYa127b2eb5c12012-05-24 15:52:10 -07003350 }
Logan Chienfca7e872011-12-20 20:08:22 +08003351 }
3352 }
3353 }
3354 }
3355
3356 return table.release();
3357}
Logan Chiendd361c92012-04-10 23:40:37 +08003358
Ian Rogers776ac1f2012-04-13 23:36:36 -07003359void MethodVerifier::SetInferredRegCategoryMap(Compiler::MethodReference ref,
3360 const InferredRegCategoryMap& inferred_reg_category_map) {
Shih-wei Liaocd05a622012-08-15 00:02:05 -07003361 {
3362 MutexLock mu(*inferred_reg_category_maps_lock_);
3363 InferredRegCategoryMapTable::iterator it = inferred_reg_category_maps_->find(ref);
3364 if (it == inferred_reg_category_maps_->end()) {
3365 inferred_reg_category_maps_->Put(ref, &inferred_reg_category_map);
3366 } else {
3367 CHECK(*(it->second) == inferred_reg_category_map);
3368 delete &inferred_reg_category_map;
3369 }
Logan Chiendd361c92012-04-10 23:40:37 +08003370 }
Logan Chiendd361c92012-04-10 23:40:37 +08003371 CHECK(GetInferredRegCategoryMap(ref) != NULL);
3372}
3373
TDYa12789f96052012-07-12 20:49:53 -07003374const greenland::InferredRegCategoryMap*
Ian Rogers776ac1f2012-04-13 23:36:36 -07003375MethodVerifier::GetInferredRegCategoryMap(Compiler::MethodReference ref) {
Logan Chiendd361c92012-04-10 23:40:37 +08003376 MutexLock mu(*inferred_reg_category_maps_lock_);
3377
3378 InferredRegCategoryMapTable::const_iterator it =
3379 inferred_reg_category_maps_->find(ref);
3380
3381 if (it == inferred_reg_category_maps_->end()) {
3382 return NULL;
3383 }
3384 CHECK(it->second != NULL);
3385 return it->second;
3386}
Logan Chienfca7e872011-12-20 20:08:22 +08003387#endif
3388
Ian Rogersd81871c2011-10-03 13:57:23 -07003389} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003390} // namespace art