blob: 73aee9cf0c2ca415a58b705f098949205f7b519f [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 Rogers0c7abda2012-09-19 13:33:42 -070027#include "verifier/dex_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 Rogers0c7abda2012-09-19 13:33:42 -0700552 /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */
Ian Rogersd81871c2011-10-03 13:57:23 -0700553 insn_flags_[0].SetBranchTarget();
Ian Rogers0c7abda2012-09-19 13:33:42 -0700554 insn_flags_[0].SetGcPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700555
556 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700557 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700558 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700559 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700560 return false;
561 }
562 /* Flag instructions that are garbage collection points */
563 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
564 insn_flags_[dex_pc].SetGcPoint();
565 }
566 dex_pc += inst->SizeInCodeUnits();
567 inst = inst->Next();
568 }
569 return true;
570}
571
Ian Rogers776ac1f2012-04-13 23:36:36 -0700572bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800573 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700574 bool result = true;
575 switch (inst->GetVerifyTypeArgumentA()) {
576 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800577 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700578 break;
579 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800580 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700581 break;
582 }
583 switch (inst->GetVerifyTypeArgumentB()) {
584 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800585 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700586 break;
587 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800588 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700589 break;
590 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800591 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700592 break;
593 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800594 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700595 break;
596 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800597 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700598 break;
599 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800600 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700601 break;
602 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800603 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700604 break;
605 }
606 switch (inst->GetVerifyTypeArgumentC()) {
607 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800608 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700609 break;
610 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800611 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700612 break;
613 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800614 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700615 break;
616 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800617 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700618 break;
619 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800620 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700621 break;
622 }
623 switch (inst->GetVerifyExtraFlags()) {
624 case Instruction::kVerifyArrayData:
625 result = result && CheckArrayData(code_offset);
626 break;
627 case Instruction::kVerifyBranchTarget:
628 result = result && CheckBranchTarget(code_offset);
629 break;
630 case Instruction::kVerifySwitchTargets:
631 result = result && CheckSwitchTargets(code_offset);
632 break;
633 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800634 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700635 break;
636 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800637 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700638 break;
639 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700640 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700641 result = false;
642 break;
643 }
644 return result;
645}
646
Ian Rogers776ac1f2012-04-13 23:36:36 -0700647bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700648 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700649 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
650 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700651 return false;
652 }
653 return true;
654}
655
Ian Rogers776ac1f2012-04-13 23:36:36 -0700656bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700657 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700658 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
659 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700660 return false;
661 }
662 return true;
663}
664
Ian Rogers776ac1f2012-04-13 23:36:36 -0700665bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700666 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700667 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
668 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700669 return false;
670 }
671 return true;
672}
673
Ian Rogers776ac1f2012-04-13 23:36:36 -0700674bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700675 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700676 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
677 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700678 return false;
679 }
680 return true;
681}
682
Ian Rogers776ac1f2012-04-13 23:36:36 -0700683bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700684 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700685 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
686 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700687 return false;
688 }
689 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700690 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700691 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700692 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700693 return false;
694 }
695 return true;
696}
697
Ian Rogers776ac1f2012-04-13 23:36:36 -0700698bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700699 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700700 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
701 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700702 return false;
703 }
704 return true;
705}
706
Ian Rogers776ac1f2012-04-13 23:36:36 -0700707bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700708 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700709 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
710 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700711 return false;
712 }
713 return true;
714}
715
Ian Rogers776ac1f2012-04-13 23:36:36 -0700716bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700717 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
719 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700720 return false;
721 }
722 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700723 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700724 const char* cp = descriptor;
725 while (*cp++ == '[') {
726 bracket_count++;
727 }
728 if (bracket_count == 0) {
729 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700730 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700731 return false;
732 } else if (bracket_count > 255) {
733 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700735 return false;
736 }
737 return true;
738}
739
Ian Rogers776ac1f2012-04-13 23:36:36 -0700740bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700741 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
742 const uint16_t* insns = code_item_->insns_ + cur_offset;
743 const uint16_t* array_data;
744 int32_t array_data_offset;
745
746 DCHECK_LT(cur_offset, insn_count);
747 /* make sure the start of the array data table is in range */
748 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
749 if ((int32_t) cur_offset + array_data_offset < 0 ||
750 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700751 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
752 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700753 return false;
754 }
755 /* offset to array data table is a relative branch-style offset */
756 array_data = insns + array_data_offset;
757 /* make sure the table is 32-bit aligned */
758 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700759 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
760 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700761 return false;
762 }
763 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700764 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700765 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
766 /* make sure the end of the switch is in range */
767 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700768 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
769 << ", data offset " << array_data_offset << ", end "
770 << cur_offset + array_data_offset + table_size
771 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700772 return false;
773 }
774 return true;
775}
776
Ian Rogers776ac1f2012-04-13 23:36:36 -0700777bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700778 int32_t offset;
779 bool isConditional, selfOkay;
780 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
781 return false;
782 }
783 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700784 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 -0700785 return false;
786 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700787 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
788 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700789 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700790 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700791 return false;
792 }
793 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
794 int32_t abs_offset = cur_offset + offset;
795 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700796 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700797 << reinterpret_cast<void*>(abs_offset) << ") at "
798 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700799 return false;
800 }
801 insn_flags_[abs_offset].SetBranchTarget();
802 return true;
803}
804
Ian Rogers776ac1f2012-04-13 23:36:36 -0700805bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700806 bool* selfOkay) {
807 const uint16_t* insns = code_item_->insns_ + cur_offset;
808 *pConditional = false;
809 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700810 switch (*insns & 0xff) {
811 case Instruction::GOTO:
812 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700813 break;
814 case Instruction::GOTO_32:
815 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700816 *selfOkay = true;
817 break;
818 case Instruction::GOTO_16:
819 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700820 break;
821 case Instruction::IF_EQ:
822 case Instruction::IF_NE:
823 case Instruction::IF_LT:
824 case Instruction::IF_GE:
825 case Instruction::IF_GT:
826 case Instruction::IF_LE:
827 case Instruction::IF_EQZ:
828 case Instruction::IF_NEZ:
829 case Instruction::IF_LTZ:
830 case Instruction::IF_GEZ:
831 case Instruction::IF_GTZ:
832 case Instruction::IF_LEZ:
833 *pOffset = (int16_t) insns[1];
834 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700835 break;
836 default:
837 return false;
838 break;
839 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700840 return true;
841}
842
Ian Rogers776ac1f2012-04-13 23:36:36 -0700843bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700844 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700845 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700846 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700847 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700848 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
849 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700850 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
851 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700852 return false;
853 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700854 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700855 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700856 /* make sure the table is 32-bit aligned */
857 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700858 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
859 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700860 return false;
861 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700862 uint32_t switch_count = switch_insns[1];
863 int32_t keys_offset, targets_offset;
864 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700865 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
866 /* 0=sig, 1=count, 2/3=firstKey */
867 targets_offset = 4;
868 keys_offset = -1;
869 expected_signature = Instruction::kPackedSwitchSignature;
870 } else {
871 /* 0=sig, 1=count, 2..count*2 = keys */
872 keys_offset = 2;
873 targets_offset = 2 + 2 * switch_count;
874 expected_signature = Instruction::kSparseSwitchSignature;
875 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700876 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700877 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700878 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
879 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700880 return false;
881 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700882 /* make sure the end of the switch is in range */
883 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700884 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
885 << switch_offset << ", end "
886 << (cur_offset + switch_offset + table_size)
887 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700888 return false;
889 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700890 /* for a sparse switch, verify the keys are in ascending order */
891 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
893 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700894 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
895 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
896 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700897 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
898 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700899 return false;
900 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700901 last_key = key;
902 }
903 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700904 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700905 for (uint32_t targ = 0; targ < switch_count; targ++) {
906 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
907 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
908 int32_t abs_offset = cur_offset + offset;
909 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700910 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700911 << reinterpret_cast<void*>(abs_offset) << ") at "
912 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700913 return false;
914 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700915 insn_flags_[abs_offset].SetBranchTarget();
916 }
917 return true;
918}
919
Ian Rogers776ac1f2012-04-13 23:36:36 -0700920bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700921 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700922 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700923 return false;
924 }
925 uint16_t registers_size = code_item_->registers_size_;
926 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800927 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700928 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
929 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700930 return false;
931 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700932 }
933
934 return true;
935}
936
Ian Rogers776ac1f2012-04-13 23:36:36 -0700937bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700938 uint16_t registers_size = code_item_->registers_size_;
939 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
940 // integer overflow when adding them here.
941 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700942 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
943 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700944 return false;
945 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700946 return true;
947}
948
Ian Rogers0c7abda2012-09-19 13:33:42 -0700949static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -0800950 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
951 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
952 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
953 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
954 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
955 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
956 gc_map.begin(),
957 gc_map.end());
958 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
959 DCHECK_EQ(gc_map.size(),
960 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
961 (length_prefixed_gc_map->at(1) << 16) |
962 (length_prefixed_gc_map->at(2) << 8) |
963 (length_prefixed_gc_map->at(3) << 0)));
964 return length_prefixed_gc_map;
965}
966
Ian Rogers776ac1f2012-04-13 23:36:36 -0700967bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700968 uint16_t registers_size = code_item_->registers_size_;
969 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -0700970
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -0800972 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
973 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 }
975 /* Create and initialize table holding register status */
Elliott Hughes460384f2012-04-04 16:53:10 -0700976 reg_table_.Init(kTrackRegsGcPoints, insn_flags_.get(), insns_size, registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -0700977
Ian Rogersd81871c2011-10-03 13:57:23 -0700978 work_line_.reset(new RegisterLine(registers_size, this));
979 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -0700980
Ian Rogersd81871c2011-10-03 13:57:23 -0700981 /* Initialize register types of method arguments. */
982 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700983 DCHECK_NE(failures_.size(), 0U);
984 std::string prepend("Bad signature in ");
985 prepend += PrettyMethod(method_idx_, *dex_file_);
986 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -0700987 return false;
988 }
989 /* Perform code flow verification. */
990 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700991 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700992 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700993 }
994
TDYa127b2eb5c12012-05-24 15:52:10 -0700995 Compiler::MethodReference ref(dex_file_, method_idx_);
996
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700997#if !defined(ART_USE_LLVM_COMPILER) && !defined(ART_USE_GREENLAND_COMPILER)
TDYa127b2eb5c12012-05-24 15:52:10 -0700998
Ian Rogersd81871c2011-10-03 13:57:23 -0700999 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -08001000 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1001 if (map.get() == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001002 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001003 return false; // Not a real failure, but a failure to encode
1004 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001005#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001006 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -07001007#endif
Ian Rogers0c7abda2012-09-19 13:33:42 -07001008 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
1009 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
Logan Chiendd361c92012-04-10 23:40:37 +08001010
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -07001011#else // defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Logan Chienfca7e872011-12-20 20:08:22 +08001012 /* Generate Inferred Register Category for LLVM-based Code Generator */
1013 const InferredRegCategoryMap* table = GenerateInferredRegCategoryMap();
Ian Rogers776ac1f2012-04-13 23:36:36 -07001014 verifier::MethodVerifier::SetInferredRegCategoryMap(ref, *table);
TDYa127b2eb5c12012-05-24 15:52:10 -07001015
Logan Chienfca7e872011-12-20 20:08:22 +08001016#endif
1017
jeffhaobdb76512011-09-07 11:43:16 -07001018 return true;
1019}
1020
Ian Rogersad0b3a32012-04-16 14:50:24 -07001021std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1022 DCHECK_EQ(failures_.size(), failure_messages_.size());
1023 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001024 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001025 }
1026 return os;
1027}
1028
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001029extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001030 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001031 v->Dump(std::cerr);
1032}
1033
Ian Rogers776ac1f2012-04-13 23:36:36 -07001034void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001035 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001036 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001037 return;
jeffhaobdb76512011-09-07 11:43:16 -07001038 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 DCHECK(code_item_ != NULL);
1040 const Instruction* inst = Instruction::At(code_item_->insns_);
1041 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1042 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Elliott Hughesaa6e1cd2012-01-18 19:26:06 -08001043 os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
Elliott Hughesc073b072012-05-24 19:29:17 -07001044 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001045 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1046 if (reg_line != NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001047 os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001048 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001049 inst = inst->Next();
1050 }
jeffhaobdb76512011-09-07 11:43:16 -07001051}
1052
Ian Rogersd81871c2011-10-03 13:57:23 -07001053static bool IsPrimitiveDescriptor(char descriptor) {
1054 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001055 case 'I':
1056 case 'C':
1057 case 'S':
1058 case 'B':
1059 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001060 case 'F':
1061 case 'D':
1062 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001063 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001064 default:
1065 return false;
1066 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001067}
1068
Ian Rogers776ac1f2012-04-13 23:36:36 -07001069bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001070 RegisterLine* reg_line = reg_table_.GetLine(0);
1071 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1072 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001073
Ian Rogersd81871c2011-10-03 13:57:23 -07001074 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1075 //Include the "this" pointer.
1076 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001077 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001078 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1079 // argument as uninitialized. This restricts field access until the superclass constructor is
1080 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001081 const RegType& declaring_class = GetDeclaringClass();
1082 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001083 reg_line->SetRegisterType(arg_start + cur_arg,
1084 reg_types_.UninitializedThisArgument(declaring_class));
1085 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001086 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001087 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001088 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001089 }
1090
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001091 const DexFile::ProtoId& proto_id =
Ian Rogersad0b3a32012-04-16 14:50:24 -07001092 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001093 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001094
1095 for (; iterator.HasNext(); iterator.Next()) {
1096 const char* descriptor = iterator.GetDescriptor();
1097 if (descriptor == NULL) {
1098 LOG(FATAL) << "Null descriptor";
1099 }
1100 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001101 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1102 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001103 return false;
1104 }
1105 switch (descriptor[0]) {
1106 case 'L':
1107 case '[':
1108 // We assume that reference arguments are initialized. The only way it could be otherwise
1109 // (assuming the caller was verified) is if the current method is <init>, but in that case
1110 // it's effectively considered initialized the instant we reach here (in the sense that we
1111 // can return without doing anything or call virtual methods).
1112 {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001113 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001114 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001115 }
1116 break;
1117 case 'Z':
1118 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1119 break;
1120 case 'C':
1121 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1122 break;
1123 case 'B':
1124 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1125 break;
1126 case 'I':
1127 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1128 break;
1129 case 'S':
1130 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1131 break;
1132 case 'F':
1133 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1134 break;
1135 case 'J':
1136 case 'D': {
1137 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1138 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1139 cur_arg++;
1140 break;
1141 }
1142 default:
jeffhaod5347e02012-03-22 17:25:05 -07001143 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001144 return false;
1145 }
1146 cur_arg++;
1147 }
1148 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001149 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001150 return false;
1151 }
1152 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1153 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1154 // format. Only major difference from the method argument format is that 'V' is supported.
1155 bool result;
1156 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1157 result = descriptor[1] == '\0';
1158 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1159 size_t i = 0;
1160 do {
1161 i++;
1162 } while (descriptor[i] == '['); // process leading [
1163 if (descriptor[i] == 'L') { // object array
1164 do {
1165 i++; // find closing ;
1166 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1167 result = descriptor[i] == ';';
1168 } else { // primitive array
1169 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1170 }
1171 } else if (descriptor[0] == 'L') {
1172 // could be more thorough here, but shouldn't be required
1173 size_t i = 0;
1174 do {
1175 i++;
1176 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1177 result = descriptor[i] == ';';
1178 } else {
1179 result = false;
1180 }
1181 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001182 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1183 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001184 }
1185 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001186}
1187
Ian Rogers776ac1f2012-04-13 23:36:36 -07001188bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001189 const uint16_t* insns = code_item_->insns_;
1190 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001191
jeffhaobdb76512011-09-07 11:43:16 -07001192 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001193 insn_flags_[0].SetChanged();
1194 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001195
jeffhaobdb76512011-09-07 11:43:16 -07001196 /* Continue until no instructions are marked "changed". */
1197 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001198 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1199 uint32_t insn_idx = start_guess;
1200 for (; insn_idx < insns_size; insn_idx++) {
1201 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001202 break;
1203 }
jeffhaobdb76512011-09-07 11:43:16 -07001204 if (insn_idx == insns_size) {
1205 if (start_guess != 0) {
1206 /* try again, starting from the top */
1207 start_guess = 0;
1208 continue;
1209 } else {
1210 /* all flags are clear */
1211 break;
1212 }
1213 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001214 // We carry the working set of registers from instruction to instruction. If this address can
1215 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1216 // "changed" flags, we need to load the set of registers from the table.
1217 // Because we always prefer to continue on to the next instruction, we should never have a
1218 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1219 // target.
1220 work_insn_idx_ = insn_idx;
1221 if (insn_flags_[insn_idx].IsBranchTarget()) {
1222 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001223 } else {
1224#ifndef NDEBUG
1225 /*
1226 * Sanity check: retrieve the stored register line (assuming
1227 * a full table) and make sure it actually matches.
1228 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001229 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1230 if (register_line != NULL) {
1231 if (work_line_->CompareLine(register_line) != 0) {
1232 Dump(std::cout);
1233 std::cout << info_messages_.str();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001234 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001235 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1236 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001237 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001238 }
jeffhaobdb76512011-09-07 11:43:16 -07001239 }
1240#endif
1241 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001242 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001243 std::string prepend(PrettyMethod(method_idx_, *dex_file_));
1244 prepend += " failed to verify: ";
1245 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001246 return false;
1247 }
jeffhaobdb76512011-09-07 11:43:16 -07001248 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001249 insn_flags_[insn_idx].SetVisited();
1250 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001251 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001252
Ian Rogers1c849e52012-06-28 14:00:33 -07001253 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001254 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001255 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001256 * (besides the wasted space), but it indicates a flaw somewhere
1257 * down the line, possibly in the verifier.
1258 *
1259 * If we've substituted "always throw" instructions into the stream,
1260 * we are almost certainly going to have some dead code.
1261 */
1262 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001263 uint32_t insn_idx = 0;
1264 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001265 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001266 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001267 * may or may not be preceded by a padding NOP (for alignment).
1268 */
1269 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1270 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1271 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001272 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001273 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1274 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1275 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001276 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001277 }
1278
Ian Rogersd81871c2011-10-03 13:57:23 -07001279 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001280 if (dead_start < 0)
1281 dead_start = insn_idx;
1282 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001283 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001284 dead_start = -1;
1285 }
1286 }
1287 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001288 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001289 }
1290 }
jeffhaobdb76512011-09-07 11:43:16 -07001291 return true;
1292}
1293
Ian Rogers776ac1f2012-04-13 23:36:36 -07001294bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001295#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001296 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001297 gDvm.verifierStats.instrsReexamined++;
1298 } else {
1299 gDvm.verifierStats.instrsExamined++;
1300 }
1301#endif
1302
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001303 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1304 // We want the state _before_ the instruction, for the case where the dex pc we're
1305 // interested in is itself a monitor-enter instruction (which is a likely place
1306 // for a thread to be suspended).
1307 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
1308 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1309 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1310 }
1311 }
1312
jeffhaobdb76512011-09-07 11:43:16 -07001313 /*
1314 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001315 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001316 * control to another statement:
1317 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001318 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001319 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001320 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001321 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001322 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001323 * throw an exception that is handled by an encompassing "try"
1324 * block.
1325 *
1326 * We can also return, in which case there is no successor instruction
1327 * from this point.
1328 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001329 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001330 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001331 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1332 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001333 DecodedInstruction dec_insn(inst);
1334 int opcode_flags = Instruction::Flags(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001335
jeffhaobdb76512011-09-07 11:43:16 -07001336 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001337 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001338 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001339 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001340 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1341 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001342 }
jeffhaobdb76512011-09-07 11:43:16 -07001343
1344 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001345 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001346 * can throw an exception, we will copy/merge this into the "catch"
1347 * address rather than work_line, because we don't want the result
1348 * from the "successful" code path (e.g. a check-cast that "improves"
1349 * a type) to be visible to the exception handler.
1350 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001351 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001352 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001353 } else {
1354#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001355 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001356#endif
1357 }
1358
Elliott Hughesadb8c672012-03-06 16:49:32 -08001359 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07001360 case Instruction::NOP:
1361 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001362 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001363 * a signature that looks like a NOP; if we see one of these in
1364 * the course of executing code then we have a problem.
1365 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001366 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001367 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001368 }
1369 break;
1370
1371 case Instruction::MOVE:
1372 case Instruction::MOVE_FROM16:
1373 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001374 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001375 break;
1376 case Instruction::MOVE_WIDE:
1377 case Instruction::MOVE_WIDE_FROM16:
1378 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001379 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07001380 break;
1381 case Instruction::MOVE_OBJECT:
1382 case Instruction::MOVE_OBJECT_FROM16:
1383 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001384 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001385 break;
1386
1387 /*
1388 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001389 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001390 * might want to hold the result in an actual CPU register, so the
1391 * Dalvik spec requires that these only appear immediately after an
1392 * invoke or filled-new-array.
1393 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001394 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001395 * redundant with the reset done below, but it can make the debug info
1396 * easier to read in some cases.)
1397 */
1398 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001399 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07001400 break;
1401 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001402 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001403 break;
1404 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001405 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07001406 break;
1407
Ian Rogersd81871c2011-10-03 13:57:23 -07001408 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001409 /*
jeffhao60f83e32012-02-13 17:16:30 -08001410 * This statement can only appear as the first instruction in an exception handler. We verify
1411 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001412 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001413 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08001414 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001415 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001416 }
jeffhaobdb76512011-09-07 11:43:16 -07001417 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001418 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1419 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001420 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001421 }
jeffhaobdb76512011-09-07 11:43:16 -07001422 }
1423 break;
1424 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001425 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001426 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001427 const RegType& return_type = GetMethodReturnType();
1428 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001429 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001430 } else {
1431 // Compilers may generate synthetic functions that write byte values into boolean fields.
1432 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001433 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001434 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1435 ((return_type.IsBoolean() || return_type.IsByte() ||
1436 return_type.IsShort() || return_type.IsChar()) &&
1437 src_type.IsInteger()));
1438 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001439 bool success =
1440 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
1441 if (!success) {
1442 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001443 }
jeffhaobdb76512011-09-07 11:43:16 -07001444 }
1445 }
1446 break;
1447 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001448 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001449 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001450 const RegType& return_type = GetMethodReturnType();
1451 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001452 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001453 } else {
1454 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001455 bool success = work_line_->VerifyRegisterType(dec_insn.vA, return_type);
1456 if (!success) {
1457 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001458 }
jeffhaobdb76512011-09-07 11:43:16 -07001459 }
1460 }
1461 break;
1462 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001463 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001464 const RegType& return_type = GetMethodReturnType();
1465 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001466 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001467 } else {
1468 /* return_type is the *expected* return type, not register value */
1469 DCHECK(!return_type.IsZero());
1470 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08001471 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07001472 // Disallow returning uninitialized values and verify that the reference in vAA is an
1473 // instance of the "return_type"
1474 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001475 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001476 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001477 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1478 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001479 }
1480 }
1481 }
1482 break;
1483
1484 case Instruction::CONST_4:
1485 case Instruction::CONST_16:
1486 case Instruction::CONST:
1487 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001488 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const((int32_t) dec_insn.vB));
jeffhaobdb76512011-09-07 11:43:16 -07001489 break;
1490 case Instruction::CONST_HIGH16:
1491 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001492 work_line_->SetRegisterType(dec_insn.vA,
1493 reg_types_.FromCat1Const((int32_t) dec_insn.vB << 16));
jeffhaobdb76512011-09-07 11:43:16 -07001494 break;
1495 case Instruction::CONST_WIDE_16:
1496 case Instruction::CONST_WIDE_32:
1497 case Instruction::CONST_WIDE:
1498 case Instruction::CONST_WIDE_HIGH16:
1499 /* could be long or double; resolved upon use */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001500 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07001501 break;
1502 case Instruction::CONST_STRING:
1503 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001504 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001505 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001506 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001507 // Get type from instruction if unresolved then we need an access check
1508 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08001509 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001510 // Register holds class, ie its type is class, on error it will hold Conflict.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001511 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogersad0b3a32012-04-16 14:50:24 -07001512 res_type.IsConflict() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001513 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001514 }
jeffhaobdb76512011-09-07 11:43:16 -07001515 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001516 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001517 break;
1518 case Instruction::MONITOR_EXIT:
1519 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001520 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001521 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001522 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001523 * to the need to handle asynchronous exceptions, a now-deprecated
1524 * feature that Dalvik doesn't support.)
1525 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001526 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001527 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001528 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001529 * structured locking checks are working, the former would have
1530 * failed on the -enter instruction, and the latter is impossible.
1531 *
1532 * This is fortunate, because issue 3221411 prevents us from
1533 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001534 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001535 * some catch blocks (which will show up as "dead" code when
1536 * we skip them here); if we can't, then the code path could be
1537 * "live" so we still need to check it.
1538 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001539 opcode_flags &= ~Instruction::kThrow;
1540 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001541 break;
1542
Ian Rogers28ad40d2011-10-27 15:19:26 -07001543 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001544 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001545 /*
1546 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1547 * could be a "upcast" -- not expected, so we don't try to address it.)
1548 *
1549 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001550 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001551 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001552 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001553 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001554 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001555 if (res_type.IsConflict()) {
1556 DCHECK_NE(failures_.size(), 0U);
1557 if (!is_checkcast) {
1558 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
1559 }
1560 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001561 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001562 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1563 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001564 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001565 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001566 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001567 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001568 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07001569 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001570 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001571 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001572 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001573 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001574 }
jeffhaobdb76512011-09-07 11:43:16 -07001575 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001576 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001577 }
1578 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001579 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001580 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001581 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001582 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001583 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001584 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001585 }
1586 }
1587 break;
1588 }
1589 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001590 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001591 if (res_type.IsConflict()) {
1592 DCHECK_NE(failures_.size(), 0U);
1593 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001594 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001595 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1596 // can't create an instance of an interface or abstract class */
1597 if (!res_type.IsInstantiableTypes()) {
1598 Fail(VERIFY_ERROR_INSTANTIATION)
1599 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001600 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001601 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001602 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1603 // Any registers holding previous allocations from this address that have not yet been
1604 // initialized must be marked invalid.
1605 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1606 // add the new uninitialized reference to the register state
1607 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001608 break;
1609 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001610 case Instruction::NEW_ARRAY:
1611 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001612 break;
1613 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08001614 VerifyNewArray(dec_insn, true, false);
1615 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001616 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001617 case Instruction::FILLED_NEW_ARRAY_RANGE:
1618 VerifyNewArray(dec_insn, true, true);
1619 just_set_result = true; // Filled new array range sets result register
1620 break;
jeffhaobdb76512011-09-07 11:43:16 -07001621 case Instruction::CMPL_FLOAT:
1622 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001623 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001624 break;
1625 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001626 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001627 break;
1628 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001629 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001630 break;
1631 case Instruction::CMPL_DOUBLE:
1632 case Instruction::CMPG_DOUBLE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001633 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08001634 break;
1635 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001636 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08001637 break;
1638 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001639 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001640 break;
1641 case Instruction::CMP_LONG:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001642 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08001643 break;
1644 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001645 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08001646 break;
1647 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001648 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001649 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001650 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001651 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001652 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001653 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001654 }
1655 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001656 }
jeffhaobdb76512011-09-07 11:43:16 -07001657 case Instruction::GOTO:
1658 case Instruction::GOTO_16:
1659 case Instruction::GOTO_32:
1660 /* no effect on or use of registers */
1661 break;
1662
1663 case Instruction::PACKED_SWITCH:
1664 case Instruction::SPARSE_SWITCH:
1665 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001666 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001667 break;
1668
Ian Rogersd81871c2011-10-03 13:57:23 -07001669 case Instruction::FILL_ARRAY_DATA: {
1670 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001671 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08001672 /* array_type can be null if the reg type is Zero */
1673 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001674 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001675 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001676 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001677 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1678 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001679 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001680 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1681 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001682 } else {
jeffhao457cc512012-02-02 16:55:13 -08001683 // Now verify if the element width in the table matches the element width declared in
1684 // the array
1685 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1686 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001687 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001688 } else {
1689 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1690 // Since we don't compress the data in Dex, expect to see equal width of data stored
1691 // in the table and expected from the array class.
1692 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001693 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1694 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001695 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001696 }
1697 }
jeffhaobdb76512011-09-07 11:43:16 -07001698 }
1699 }
1700 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001701 }
jeffhaobdb76512011-09-07 11:43:16 -07001702 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001703 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001704 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1705 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001706 bool mismatch = false;
1707 if (reg_type1.IsZero()) { // zero then integral or reference expected
1708 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1709 } else if (reg_type1.IsReferenceTypes()) { // both references?
1710 mismatch = !reg_type2.IsReferenceTypes();
1711 } else { // both integral?
1712 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1713 }
1714 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001715 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1716 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001717 }
1718 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001719 }
jeffhaobdb76512011-09-07 11:43:16 -07001720 case Instruction::IF_LT:
1721 case Instruction::IF_GE:
1722 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001723 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001724 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1725 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001726 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001727 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1728 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001729 }
1730 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001731 }
jeffhaobdb76512011-09-07 11:43:16 -07001732 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001733 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001734 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001735 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001736 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001737 }
jeffhaobdb76512011-09-07 11:43:16 -07001738 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001739 }
jeffhaobdb76512011-09-07 11:43:16 -07001740 case Instruction::IF_LTZ:
1741 case Instruction::IF_GEZ:
1742 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001743 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001744 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001745 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001746 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1747 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001748 }
jeffhaobdb76512011-09-07 11:43:16 -07001749 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001750 }
jeffhaobdb76512011-09-07 11:43:16 -07001751 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001752 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
1753 break;
jeffhaobdb76512011-09-07 11:43:16 -07001754 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001755 VerifyAGet(dec_insn, reg_types_.Byte(), true);
1756 break;
jeffhaobdb76512011-09-07 11:43:16 -07001757 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07001758 VerifyAGet(dec_insn, reg_types_.Char(), true);
1759 break;
jeffhaobdb76512011-09-07 11:43:16 -07001760 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001761 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001762 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001763 case Instruction::AGET:
1764 VerifyAGet(dec_insn, reg_types_.Integer(), true);
1765 break;
jeffhaobdb76512011-09-07 11:43:16 -07001766 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001767 VerifyAGet(dec_insn, reg_types_.Long(), true);
1768 break;
1769 case Instruction::AGET_OBJECT:
1770 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001771 break;
1772
Ian Rogersd81871c2011-10-03 13:57:23 -07001773 case Instruction::APUT_BOOLEAN:
1774 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
1775 break;
1776 case Instruction::APUT_BYTE:
1777 VerifyAPut(dec_insn, reg_types_.Byte(), true);
1778 break;
1779 case Instruction::APUT_CHAR:
1780 VerifyAPut(dec_insn, reg_types_.Char(), true);
1781 break;
1782 case Instruction::APUT_SHORT:
1783 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001784 break;
1785 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001786 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001787 break;
1788 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001789 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001790 break;
1791 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001792 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001793 break;
1794
jeffhaobdb76512011-09-07 11:43:16 -07001795 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001796 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001797 break;
jeffhaobdb76512011-09-07 11:43:16 -07001798 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001799 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001800 break;
jeffhaobdb76512011-09-07 11:43:16 -07001801 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001802 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001803 break;
jeffhaobdb76512011-09-07 11:43:16 -07001804 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001805 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001806 break;
1807 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001808 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001809 break;
1810 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001811 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001812 break;
1813 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001814 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001815 break;
jeffhaobdb76512011-09-07 11:43:16 -07001816
Ian Rogersd81871c2011-10-03 13:57:23 -07001817 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001818 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001819 break;
1820 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001821 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001822 break;
1823 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001824 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001825 break;
1826 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001827 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001828 break;
1829 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001830 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001831 break;
1832 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001833 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001834 break;
jeffhaobdb76512011-09-07 11:43:16 -07001835 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001836 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001837 break;
1838
jeffhaobdb76512011-09-07 11:43:16 -07001839 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001840 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001841 break;
jeffhaobdb76512011-09-07 11:43:16 -07001842 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001843 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001844 break;
jeffhaobdb76512011-09-07 11:43:16 -07001845 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001846 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001847 break;
jeffhaobdb76512011-09-07 11:43:16 -07001848 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001849 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001850 break;
1851 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001852 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001853 break;
1854 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001855 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001856 break;
1857 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001858 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001859 break;
1860
1861 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001862 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001863 break;
1864 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001865 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001866 break;
1867 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001868 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001869 break;
1870 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001871 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001872 break;
1873 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001874 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001875 break;
1876 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001877 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001878 break;
1879 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001880 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001881 break;
1882
1883 case Instruction::INVOKE_VIRTUAL:
1884 case Instruction::INVOKE_VIRTUAL_RANGE:
1885 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001886 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001887 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
1888 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
1889 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
1890 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001891 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001892 const char* descriptor;
1893 if (called_method == NULL) {
1894 uint32_t method_idx = dec_insn.vB;
1895 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1896 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1897 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1898 } else {
1899 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07001900 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001901 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
1902 work_line_->SetResultRegisterType(return_type);
1903 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001904 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001905 }
jeffhaobdb76512011-09-07 11:43:16 -07001906 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001907 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001908 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001909 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07001910 const char* return_type_descriptor;
1911 bool is_constructor;
1912 if (called_method == NULL) {
1913 uint32_t method_idx = dec_insn.vB;
1914 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1915 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
1916 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1917 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1918 } else {
1919 is_constructor = called_method->IsConstructor();
1920 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
1921 }
1922 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07001923 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001924 * Some additional checks when calling a constructor. We know from the invocation arg check
1925 * that the "this" argument is an instance of called_method->klass. Now we further restrict
1926 * that to require that called_method->klass is the same as this->klass or this->super,
1927 * allowing the latter only if the "this" argument is the same as the "this" argument to
1928 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07001929 */
jeffhaob57e9522012-04-26 18:08:21 -07001930 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1931 if (this_type.IsConflict()) // failure.
1932 break;
jeffhaobdb76512011-09-07 11:43:16 -07001933
jeffhaob57e9522012-04-26 18:08:21 -07001934 /* no null refs allowed (?) */
1935 if (this_type.IsZero()) {
1936 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
1937 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07001938 }
jeffhaob57e9522012-04-26 18:08:21 -07001939
1940 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07001941 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
1942 // TODO: re-enable constructor type verification
1943 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07001944 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07001945 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
1946 // break;
1947 // }
jeffhaob57e9522012-04-26 18:08:21 -07001948
1949 /* arg must be an uninitialized reference */
1950 if (!this_type.IsUninitializedTypes()) {
1951 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
1952 << this_type;
1953 break;
1954 }
1955
1956 /*
1957 * Replace the uninitialized reference with an initialized one. We need to do this for all
1958 * registers that have the same object instance in them, not just the "this" register.
1959 */
1960 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001961 }
Ian Rogers46685432012-06-03 22:26:43 -07001962 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001963 work_line_->SetResultRegisterType(return_type);
1964 just_set_result = true;
1965 break;
1966 }
1967 case Instruction::INVOKE_STATIC:
1968 case Instruction::INVOKE_STATIC_RANGE: {
1969 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
1970 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001971 const char* descriptor;
1972 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001973 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001974 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1975 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07001976 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001977 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001978 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001979 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001980 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07001981 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07001982 just_set_result = true;
1983 }
1984 break;
jeffhaobdb76512011-09-07 11:43:16 -07001985 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001986 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001987 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001988 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001989 if (abs_method != NULL) {
1990 Class* called_interface = abs_method->GetDeclaringClass();
1991 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
1992 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
1993 << PrettyMethod(abs_method) << "'";
1994 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001995 }
Ian Rogers0d604842012-04-16 14:50:24 -07001996 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001997 /* Get the type of the "this" arg, which should either be a sub-interface of called
1998 * interface or Object (see comments in RegType::JoinClass).
1999 */
2000 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2001 if (this_type.IsZero()) {
2002 /* null pointer always passes (and always fails at runtime) */
2003 } else {
2004 if (this_type.IsUninitializedTypes()) {
2005 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2006 << this_type;
2007 break;
2008 }
2009 // In the past we have tried to assert that "called_interface" is assignable
2010 // from "this_type.GetClass()", however, as we do an imprecise Join
2011 // (RegType::JoinClass) we don't have full information on what interfaces are
2012 // implemented by "this_type". For example, two classes may implement the same
2013 // interfaces and have a common parent that doesn't implement the interface. The
2014 // join will set "this_type" to the parent class and a test that this implements
2015 // the interface will incorrectly fail.
2016 }
2017 /*
2018 * We don't have an object instance, so we can't find the concrete method. However, all of
2019 * the type information is in the abstract method, so we're good.
2020 */
2021 const char* descriptor;
2022 if (abs_method == NULL) {
2023 uint32_t method_idx = dec_insn.vB;
2024 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2025 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2026 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2027 } else {
2028 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2029 }
2030 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
2031 work_line_->SetResultRegisterType(return_type);
2032 work_line_->SetResultRegisterType(return_type);
2033 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002034 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002035 }
jeffhaobdb76512011-09-07 11:43:16 -07002036 case Instruction::NEG_INT:
2037 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002038 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002039 break;
2040 case Instruction::NEG_LONG:
2041 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002042 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002043 break;
2044 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002045 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002046 break;
2047 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002048 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002049 break;
2050 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002051 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002052 break;
2053 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002054 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002055 break;
2056 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002057 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002058 break;
2059 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002060 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002061 break;
2062 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002063 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002064 break;
2065 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002066 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002067 break;
2068 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002069 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002070 break;
2071 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002072 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002073 break;
2074 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002075 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002076 break;
2077 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002078 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002079 break;
2080 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002081 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002082 break;
2083 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002084 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002085 break;
2086 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002087 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002088 break;
2089 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002090 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002091 break;
2092 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002093 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002094 break;
2095
2096 case Instruction::ADD_INT:
2097 case Instruction::SUB_INT:
2098 case Instruction::MUL_INT:
2099 case Instruction::REM_INT:
2100 case Instruction::DIV_INT:
2101 case Instruction::SHL_INT:
2102 case Instruction::SHR_INT:
2103 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002104 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002105 break;
2106 case Instruction::AND_INT:
2107 case Instruction::OR_INT:
2108 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002109 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002110 break;
2111 case Instruction::ADD_LONG:
2112 case Instruction::SUB_LONG:
2113 case Instruction::MUL_LONG:
2114 case Instruction::DIV_LONG:
2115 case Instruction::REM_LONG:
2116 case Instruction::AND_LONG:
2117 case Instruction::OR_LONG:
2118 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002119 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002120 break;
2121 case Instruction::SHL_LONG:
2122 case Instruction::SHR_LONG:
2123 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002124 /* shift distance is Int, making these different from other binary operations */
2125 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002126 break;
2127 case Instruction::ADD_FLOAT:
2128 case Instruction::SUB_FLOAT:
2129 case Instruction::MUL_FLOAT:
2130 case Instruction::DIV_FLOAT:
2131 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002132 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002133 break;
2134 case Instruction::ADD_DOUBLE:
2135 case Instruction::SUB_DOUBLE:
2136 case Instruction::MUL_DOUBLE:
2137 case Instruction::DIV_DOUBLE:
2138 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002139 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002140 break;
2141 case Instruction::ADD_INT_2ADDR:
2142 case Instruction::SUB_INT_2ADDR:
2143 case Instruction::MUL_INT_2ADDR:
2144 case Instruction::REM_INT_2ADDR:
2145 case Instruction::SHL_INT_2ADDR:
2146 case Instruction::SHR_INT_2ADDR:
2147 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002148 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002149 break;
2150 case Instruction::AND_INT_2ADDR:
2151 case Instruction::OR_INT_2ADDR:
2152 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002153 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002154 break;
2155 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002156 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002157 break;
2158 case Instruction::ADD_LONG_2ADDR:
2159 case Instruction::SUB_LONG_2ADDR:
2160 case Instruction::MUL_LONG_2ADDR:
2161 case Instruction::DIV_LONG_2ADDR:
2162 case Instruction::REM_LONG_2ADDR:
2163 case Instruction::AND_LONG_2ADDR:
2164 case Instruction::OR_LONG_2ADDR:
2165 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002166 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002167 break;
2168 case Instruction::SHL_LONG_2ADDR:
2169 case Instruction::SHR_LONG_2ADDR:
2170 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002171 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002172 break;
2173 case Instruction::ADD_FLOAT_2ADDR:
2174 case Instruction::SUB_FLOAT_2ADDR:
2175 case Instruction::MUL_FLOAT_2ADDR:
2176 case Instruction::DIV_FLOAT_2ADDR:
2177 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002178 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002179 break;
2180 case Instruction::ADD_DOUBLE_2ADDR:
2181 case Instruction::SUB_DOUBLE_2ADDR:
2182 case Instruction::MUL_DOUBLE_2ADDR:
2183 case Instruction::DIV_DOUBLE_2ADDR:
2184 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002185 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002186 break;
2187 case Instruction::ADD_INT_LIT16:
2188 case Instruction::RSUB_INT:
2189 case Instruction::MUL_INT_LIT16:
2190 case Instruction::DIV_INT_LIT16:
2191 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002192 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002193 break;
2194 case Instruction::AND_INT_LIT16:
2195 case Instruction::OR_INT_LIT16:
2196 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002197 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002198 break;
2199 case Instruction::ADD_INT_LIT8:
2200 case Instruction::RSUB_INT_LIT8:
2201 case Instruction::MUL_INT_LIT8:
2202 case Instruction::DIV_INT_LIT8:
2203 case Instruction::REM_INT_LIT8:
2204 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002205 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002206 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002207 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002208 break;
2209 case Instruction::AND_INT_LIT8:
2210 case Instruction::OR_INT_LIT8:
2211 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002212 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002213 break;
2214
Ian Rogersd81871c2011-10-03 13:57:23 -07002215 /* These should never appear during verification. */
jeffhao9a4f0032012-08-30 16:17:40 -07002216 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002217 case Instruction::UNUSED_EE:
2218 case Instruction::UNUSED_EF:
2219 case Instruction::UNUSED_F2:
2220 case Instruction::UNUSED_F3:
2221 case Instruction::UNUSED_F4:
2222 case Instruction::UNUSED_F5:
2223 case Instruction::UNUSED_F6:
2224 case Instruction::UNUSED_F7:
2225 case Instruction::UNUSED_F8:
2226 case Instruction::UNUSED_F9:
2227 case Instruction::UNUSED_FA:
2228 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002229 case Instruction::UNUSED_F0:
2230 case Instruction::UNUSED_F1:
2231 case Instruction::UNUSED_E3:
2232 case Instruction::UNUSED_E8:
2233 case Instruction::UNUSED_E7:
2234 case Instruction::UNUSED_E4:
2235 case Instruction::UNUSED_E9:
2236 case Instruction::UNUSED_FC:
2237 case Instruction::UNUSED_E5:
2238 case Instruction::UNUSED_EA:
2239 case Instruction::UNUSED_FD:
2240 case Instruction::UNUSED_E6:
2241 case Instruction::UNUSED_EB:
2242 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002243 case Instruction::UNUSED_3E:
2244 case Instruction::UNUSED_3F:
2245 case Instruction::UNUSED_40:
2246 case Instruction::UNUSED_41:
2247 case Instruction::UNUSED_42:
2248 case Instruction::UNUSED_43:
2249 case Instruction::UNUSED_73:
2250 case Instruction::UNUSED_79:
2251 case Instruction::UNUSED_7A:
2252 case Instruction::UNUSED_EC:
2253 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002255 break;
2256
2257 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002258 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002259 * complain if an instruction is missing (which is desirable).
2260 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002261 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002262
Ian Rogersad0b3a32012-04-16 14:50:24 -07002263 if (have_pending_hard_failure_) {
2264 if (!Runtime::Current()->IsStarted()) {
jeffhaob57e9522012-04-26 18:08:21 -07002265 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002266 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002267 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002268 /* immediate failure, reject class */
2269 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2270 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002271 } else if (have_pending_runtime_throw_failure_) {
2272 /* slow path will throw, mark following code as unreachable */
2273 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002274 }
jeffhaobdb76512011-09-07 11:43:16 -07002275 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002276 * If we didn't just set the result register, clear it out. This ensures that you can only use
2277 * "move-result" immediately after the result is set. (We could check this statically, but it's
2278 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002279 */
2280 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002281 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002282 }
2283
jeffhaoa0a764a2011-09-16 10:43:38 -07002284 /* Handle "continue". Tag the next consecutive instruction. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002285 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers776ac1f2012-04-13 23:36:36 -07002286 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
Ian Rogersd81871c2011-10-03 13:57:23 -07002287 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
jeffhaod5347e02012-03-22 17:25:05 -07002288 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002289 return false;
2290 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002291 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2292 // next instruction isn't one.
jeffhaod5347e02012-03-22 17:25:05 -07002293 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002294 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002295 }
2296 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2297 if (next_line != NULL) {
2298 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2299 // needed.
2300 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002301 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002302 }
jeffhaobdb76512011-09-07 11:43:16 -07002303 } else {
2304 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002305 * We're not recording register data for the next instruction, so we don't know what the prior
2306 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002307 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002308 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002309 }
2310 }
2311
2312 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002313 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002314 *
2315 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002316 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002317 * somebody could get a reference field, check it for zero, and if the
2318 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002319 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002320 * that, and will reject the code.
2321 *
2322 * TODO: avoid re-fetching the branch target
2323 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002324 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002325 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002326 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002327 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002328 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002329 return false;
2330 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002331 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002332 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002333 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002334 }
jeffhaobdb76512011-09-07 11:43:16 -07002335 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002336 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002337 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002338 }
jeffhaobdb76512011-09-07 11:43:16 -07002339 }
2340
2341 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002342 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002343 *
2344 * We've already verified that the table is structurally sound, so we
2345 * just need to walk through and tag the targets.
2346 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002347 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002348 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2349 const uint16_t* switch_insns = insns + offset_to_switch;
2350 int switch_count = switch_insns[1];
2351 int offset_to_targets, targ;
2352
2353 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2354 /* 0 = sig, 1 = count, 2/3 = first key */
2355 offset_to_targets = 4;
2356 } else {
2357 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002358 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002359 offset_to_targets = 2 + 2 * switch_count;
2360 }
2361
2362 /* verify each switch target */
2363 for (targ = 0; targ < switch_count; targ++) {
2364 int offset;
2365 uint32_t abs_offset;
2366
2367 /* offsets are 32-bit, and only partly endian-swapped */
2368 offset = switch_insns[offset_to_targets + targ * 2] |
2369 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002370 abs_offset = work_insn_idx_ + offset;
2371 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002372 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002373 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002374 }
2375 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002376 return false;
2377 }
2378 }
2379
2380 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002381 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2382 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002383 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002384 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002385 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002386 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002387
Ian Rogers0571d352011-11-03 19:51:38 -07002388 for (; iterator.HasNext(); iterator.Next()) {
2389 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002390 within_catch_all = true;
2391 }
jeffhaobdb76512011-09-07 11:43:16 -07002392 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002393 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2394 * "work_regs", because at runtime the exception will be thrown before the instruction
2395 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002396 */
Ian Rogers0571d352011-11-03 19:51:38 -07002397 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002398 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002399 }
jeffhaobdb76512011-09-07 11:43:16 -07002400 }
2401
2402 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002403 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2404 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002405 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002406 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002407 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002408 * The state in work_line reflects the post-execution state. If the current instruction is a
2409 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002410 * it will do so before grabbing the lock).
2411 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002412 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002413 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002414 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002415 return false;
2416 }
2417 }
2418 }
2419
jeffhaod1f0fde2011-09-08 17:25:33 -07002420 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002421 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002422 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002423 return false;
2424 }
jeffhaobdb76512011-09-07 11:43:16 -07002425 }
2426
2427 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002428 * Update start_guess. Advance to the next instruction of that's
2429 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002430 * neither of those exists we're in a return or throw; leave start_guess
2431 * alone and let the caller sort it out.
2432 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002433 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002434 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002435 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002436 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002437 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002438 }
2439
Ian Rogersd81871c2011-10-03 13:57:23 -07002440 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2441 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002442
2443 return true;
2444}
2445
Ian Rogers776ac1f2012-04-13 23:36:36 -07002446const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002447 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002448 const RegType& referrer = GetDeclaringClass();
2449 Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002450 const RegType& result =
2451 klass != NULL ? reg_types_.FromClass(klass)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002452 : reg_types_.FromDescriptor(class_loader_, descriptor);
2453 if (result.IsConflict()) {
2454 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2455 << "' in " << referrer;
2456 return result;
2457 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002458 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002459 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002460 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002461 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002462 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002463 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002464 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002465 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002466 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002467 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002468}
2469
Ian Rogers776ac1f2012-04-13 23:36:36 -07002470const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002471 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002472 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002473 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002474 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2475 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002476 CatchHandlerIterator iterator(handlers_ptr);
2477 for (; iterator.HasNext(); iterator.Next()) {
2478 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2479 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002480 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07002481 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002482 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002483 if (common_super == NULL) {
2484 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2485 // as that is caught at runtime
2486 common_super = &exception;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002487 } else if (!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002488 // We don't know enough about the type and the common path merge will result in
2489 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002490 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002491 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002492 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002493 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002494 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002495 common_super = &common_super->Merge(exception, &reg_types_);
2496 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002497 }
2498 }
2499 }
2500 }
Ian Rogers0571d352011-11-03 19:51:38 -07002501 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002502 }
2503 }
2504 if (common_super == NULL) {
2505 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002506 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002507 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002508 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002509 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002510}
2511
Ian Rogersad0b3a32012-04-16 14:50:24 -07002512Method* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx, MethodType method_type) {
2513 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002514 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002515 if (klass_type.IsConflict()) {
2516 std::string append(" in attempt to access method ");
2517 append += dex_file_->GetMethodName(method_id);
2518 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002519 return NULL;
2520 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002521 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002522 return NULL; // Can't resolve Class so no more to do here
2523 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002524 Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002525 const RegType& referrer = GetDeclaringClass();
2526 Method* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002527 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002528 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002529 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002530
2531 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002532 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002533 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002534 res_method = klass->FindInterfaceMethod(name, signature);
2535 } else {
2536 res_method = klass->FindVirtualMethod(name, signature);
2537 }
2538 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002539 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002540 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002541 // If a virtual or interface method wasn't found with the expected type, look in
2542 // the direct methods. This can happen when the wrong invoke type is used or when
2543 // a class has changed, and will be flagged as an error in later checks.
2544 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2545 res_method = klass->FindDirectMethod(name, signature);
2546 }
2547 if (res_method == NULL) {
2548 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2549 << PrettyDescriptor(klass) << "." << name
2550 << " " << signature;
2551 return NULL;
2552 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002553 }
2554 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002555 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2556 // enforce them here.
2557 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002558 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2559 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002560 return NULL;
2561 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002562 // Disallow any calls to class initializers.
2563 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002564 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2565 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002566 return NULL;
2567 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002568 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002569 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002570 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002571 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002572 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002573 }
jeffhaode0d9c92012-02-27 13:58:13 -08002574 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2575 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002576 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2577 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002578 return NULL;
2579 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002580 // Check that interface methods match interface classes.
2581 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2582 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2583 << " is in an interface class " << PrettyClass(klass);
2584 return NULL;
2585 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2586 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2587 << " is in a non-interface class " << PrettyClass(klass);
2588 return NULL;
2589 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002590 // See if the method type implied by the invoke instruction matches the access flags for the
2591 // target method.
2592 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2593 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2594 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2595 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002596 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2597 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002598 return NULL;
2599 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002600 return res_method;
2601}
2602
Ian Rogers776ac1f2012-04-13 23:36:36 -07002603Method* MethodVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
Ian Rogers46685432012-06-03 22:26:43 -07002604 MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002605 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2606 // we're making.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002607 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002608 if (res_method == NULL) { // error or class is unresolved
2609 return NULL;
2610 }
2611
Ian Rogersd81871c2011-10-03 13:57:23 -07002612 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2613 // has a vtable entry for the target method.
2614 if (is_super) {
2615 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002616 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002617 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002618 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
2619 << PrettyMethod(method_idx_, *dex_file_)
2620 << " to super " << PrettyMethod(res_method);
2621 return NULL;
2622 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002623 Class* super_klass = super.GetClass();
2624 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002625 MethodHelper mh(res_method);
2626 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
2627 << PrettyMethod(method_idx_, *dex_file_)
2628 << " to super " << super
2629 << "." << mh.GetName()
2630 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002631 return NULL;
2632 }
2633 }
2634 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2635 // match the call to the signature. Also, we might might be calling through an abstract method
2636 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08002637 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002638 /* caught by static verifier */
2639 DCHECK(is_range || expected_args <= 5);
2640 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002641 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002642 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2643 return NULL;
2644 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002645
jeffhaobdb76512011-09-07 11:43:16 -07002646 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002647 * Check the "this" argument, which must be an instance of the class that declared the method.
2648 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2649 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002650 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002651 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002652 if (!res_method->IsStatic()) {
2653 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002654 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002655 return NULL;
2656 }
2657 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002658 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002659 return NULL;
2660 }
2661 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07002662 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
2663 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002664 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002665 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002666 return NULL;
2667 }
2668 }
2669 actual_args++;
2670 }
2671 /*
2672 * Process the target method's signature. This signature may or may not
2673 * have been verified, so we can't assume it's properly formed.
2674 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002675 MethodHelper mh(res_method);
2676 const DexFile::TypeList* params = mh.GetParameterTypeList();
2677 size_t params_size = params == NULL ? 0 : params->Size();
2678 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002679 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002680 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002681 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2682 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002683 return NULL;
2684 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002685 const char* descriptor =
2686 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2687 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002688 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002689 << " missing signature component";
2690 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002691 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002692 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002693 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002694 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002695 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002696 }
2697 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2698 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002699 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002700 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002701 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002702 return NULL;
2703 } else {
2704 return res_method;
2705 }
2706}
2707
Ian Rogers776ac1f2012-04-13 23:36:36 -07002708void MethodVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08002709 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002710 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002711 if (res_type.IsConflict()) { // bad class
2712 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002713 } else {
2714 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2715 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002716 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002717 } else if (!is_filled) {
2718 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002719 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002720 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002721 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002722 } else {
2723 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2724 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002725 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002726 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002727 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002728 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002729 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002730 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002731 return;
2732 }
2733 }
2734 // filled-array result goes into "result" register
2735 work_line_->SetResultRegisterType(res_type);
2736 }
2737 }
2738}
2739
Ian Rogers776ac1f2012-04-13 23:36:36 -07002740void MethodVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002741 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002742 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002743 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002744 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002745 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002746 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002747 if (array_type.IsZero()) {
2748 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2749 // instruction type. TODO: have a proper notion of bottom here.
2750 if (!is_primitive || insn_type.IsCategory1Types()) {
2751 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08002752 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002753 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002754 // Category 2
Elliott Hughesadb8c672012-03-06 16:49:32 -08002755 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
Ian Rogers89310de2012-02-01 13:47:30 -08002756 }
jeffhaofc3144e2012-02-01 17:21:15 -08002757 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002758 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002759 } else {
2760 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002761 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002762 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002763 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002764 << " source for aget-object";
2765 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002766 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002767 << " source for category 1 aget";
2768 } else if (is_primitive && !insn_type.Equals(component_type) &&
2769 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2770 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002771 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogersd81871c2011-10-03 13:57:23 -07002772 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002773 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002774 // Use knowledge of the field type which is stronger than the type inferred from the
2775 // instruction, which can't differentiate object types and ints from floats, longs from
2776 // doubles.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002777 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002778 }
2779 }
2780 }
2781}
2782
Ian Rogers776ac1f2012-04-13 23:36:36 -07002783void MethodVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002784 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002785 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002786 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002787 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002788 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002789 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002790 if (array_type.IsZero()) {
2791 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2792 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002793 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002794 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002795 } else {
2796 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002797 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002798 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002800 << " source for aput-object";
2801 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002802 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002803 << " source for category 1 aput";
2804 } else if (is_primitive && !insn_type.Equals(component_type) &&
2805 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2806 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002807 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002808 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002809 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002810 // The instruction agrees with the type of array, confirm the value to be stored does too
2811 // Note: we use the instruction type (rather than the component type) for aput-object as
2812 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08002813 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002814 }
2815 }
2816 }
2817}
2818
Ian Rogers776ac1f2012-04-13 23:36:36 -07002819Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002820 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2821 // Check access to class
2822 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002823 if (klass_type.IsConflict()) { // bad class
2824 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
2825 field_idx, dex_file_->GetFieldName(field_id),
2826 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002827 return NULL;
2828 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002829 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002830 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08002831 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002832 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2833 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002834 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002835 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
2836 << dex_file_->GetFieldName(field_id) << ") in "
2837 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002838 DCHECK(Thread::Current()->IsExceptionPending());
2839 Thread::Current()->ClearException();
2840 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002841 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2842 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002843 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002844 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002845 return NULL;
2846 } else if (!field->IsStatic()) {
2847 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
2848 return NULL;
2849 } else {
2850 return field;
2851 }
2852}
2853
Ian Rogers776ac1f2012-04-13 23:36:36 -07002854Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002855 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2856 // Check access to class
2857 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002858 if (klass_type.IsConflict()) {
2859 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
2860 field_idx, dex_file_->GetFieldName(field_id),
2861 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002862 return NULL;
2863 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002864 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002865 return NULL; // Can't resolve Class so no more to do here
2866 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002867 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2868 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002869 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002870 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
2871 << dex_file_->GetFieldName(field_id) << ") in "
2872 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002873 DCHECK(Thread::Current()->IsExceptionPending());
2874 Thread::Current()->ClearException();
2875 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002876 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2877 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002878 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002879 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002880 return NULL;
2881 } else if (field->IsStatic()) {
2882 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
2883 << " to not be static";
2884 return NULL;
2885 } else if (obj_type.IsZero()) {
2886 // Cannot infer and check type, however, access will cause null pointer exception
2887 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07002888 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002889 const RegType& field_klass = reg_types_.FromClass(field->GetDeclaringClass());
2890 if (obj_type.IsUninitializedTypes() &&
2891 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
2892 !field_klass.Equals(GetDeclaringClass()))) {
2893 // Field accesses through uninitialized references are only allowable for constructors where
2894 // the field is declared in this class
2895 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
2896 << " of a not fully initialized object within the context of "
2897 << PrettyMethod(method_idx_, *dex_file_);
2898 return NULL;
2899 } else if (!field_klass.IsAssignableFrom(obj_type)) {
2900 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
2901 // of C1. For resolution to occur the declared class of the field must be compatible with
2902 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
2903 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
2904 << " from object of type " << obj_type;
2905 return NULL;
2906 } else {
2907 return field;
2908 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002909 }
2910}
2911
Ian Rogers776ac1f2012-04-13 23:36:36 -07002912void MethodVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002913 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002914 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002915 Field* field;
2916 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002917 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002918 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002919 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07002920 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002921 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002922 const char* descriptor;
Ian Rogers365c1022012-06-22 15:05:28 -07002923 ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002924 if (field != NULL) {
2925 descriptor = FieldHelper(field).GetTypeDescriptor();
2926 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07002927 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002928 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2929 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2930 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07002931 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002932 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
2933 if (is_primitive) {
2934 if (field_type.Equals(insn_type) ||
2935 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
2936 (field_type.IsDouble() && insn_type.IsLongTypes())) {
2937 // expected that read is of the correct primitive type or that int reads are reading
2938 // floats or long reads are reading doubles
2939 } else {
2940 // This is a global failure rather than a class change failure as the instructions and
2941 // the descriptors for the type should have been consistent within the same file at
2942 // compile time
2943 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
2944 << " to be of type '" << insn_type
2945 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002946 return;
2947 }
2948 } else {
2949 if (!insn_type.IsAssignableFrom(field_type)) {
2950 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
2951 << " to be compatible with type '" << insn_type
2952 << "' but found type '" << field_type
2953 << "' in get-object";
2954 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Conflict());
2955 return;
2956 }
2957 }
2958 work_line_->SetRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002959}
2960
Ian Rogers776ac1f2012-04-13 23:36:36 -07002961void MethodVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002962 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002963 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002964 Field* field;
2965 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07002966 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002967 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002968 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07002969 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002970 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002971 const char* descriptor;
Ian Rogers365c1022012-06-22 15:05:28 -07002972 ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002973 if (field != NULL) {
2974 descriptor = FieldHelper(field).GetTypeDescriptor();
2975 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07002976 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002977 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2978 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2979 loader = class_loader_;
2980 }
2981 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
2982 if (field != NULL) {
2983 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
2984 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
2985 << " from other class " << GetDeclaringClass();
2986 return;
2987 }
2988 }
2989 if (is_primitive) {
2990 // Primitive field assignability rules are weaker than regular assignability rules
2991 bool instruction_compatible;
2992 bool value_compatible;
2993 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
2994 if (field_type.IsIntegralTypes()) {
2995 instruction_compatible = insn_type.IsIntegralTypes();
2996 value_compatible = value_type.IsIntegralTypes();
2997 } else if (field_type.IsFloat()) {
2998 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
2999 value_compatible = value_type.IsFloatTypes();
3000 } else if (field_type.IsLong()) {
3001 instruction_compatible = insn_type.IsLong();
3002 value_compatible = value_type.IsLongTypes();
3003 } else if (field_type.IsDouble()) {
3004 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3005 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003006 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003007 instruction_compatible = false; // reference field with primitive store
3008 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003009 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003010 if (!instruction_compatible) {
3011 // This is a global failure rather than a class change failure as the instructions and
3012 // the descriptors for the type should have been consistent within the same file at
3013 // compile time
3014 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3015 << " to be of type '" << insn_type
3016 << "' but found type '" << field_type
3017 << "' in put";
3018 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003019 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003020 if (!value_compatible) {
3021 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
3022 << " of type " << value_type
3023 << " but expected " << field_type
3024 << " for store to " << PrettyField(field) << " in put";
3025 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003026 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003027 } else {
3028 if (!insn_type.IsAssignableFrom(field_type)) {
3029 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3030 << " to be compatible with type '" << insn_type
3031 << "' but found type '" << field_type
3032 << "' in put-object";
3033 return;
3034 }
3035 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003036 }
3037}
3038
Ian Rogers776ac1f2012-04-13 23:36:36 -07003039bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003040 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003041 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003042 return false;
3043 }
3044 return true;
3045}
3046
Ian Rogers776ac1f2012-04-13 23:36:36 -07003047bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003048 bool changed = true;
3049 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3050 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003051 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003052 * We haven't processed this instruction before, and we haven't touched the registers here, so
3053 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3054 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003055 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003056 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003057 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003058 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3059 if (gDebugVerify) {
3060 copy->CopyFromLine(target_line);
3061 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003062 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003063 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003064 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003065 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003066 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003067 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003068 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3069 << *copy.get() << " MERGE\n"
3070 << *merge_line << " ==\n"
3071 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003072 }
3073 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003074 if (changed) {
3075 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003076 }
3077 return true;
3078}
3079
Ian Rogers776ac1f2012-04-13 23:36:36 -07003080InsnFlags* MethodVerifier::CurrentInsnFlags() {
3081 return &insn_flags_[work_insn_idx_];
3082}
3083
Ian Rogersad0b3a32012-04-16 14:50:24 -07003084const RegType& MethodVerifier::GetMethodReturnType() {
3085 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx_);
3086 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3087 uint16_t return_type_idx = proto_id.return_type_idx_;
3088 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
3089 return reg_types_.FromDescriptor(class_loader_, descriptor);
3090}
3091
3092const RegType& MethodVerifier::GetDeclaringClass() {
3093 if (foo_method_ != NULL) {
3094 return reg_types_.FromClass(foo_method_->GetDeclaringClass());
3095 } else {
3096 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx_);
3097 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
3098 return reg_types_.FromDescriptor(class_loader_, descriptor);
3099 }
3100}
3101
Ian Rogers776ac1f2012-04-13 23:36:36 -07003102void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003103 size_t* log2_max_gc_pc) {
3104 size_t local_gc_points = 0;
3105 size_t max_insn = 0;
3106 size_t max_ref_reg = -1;
3107 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3108 if (insn_flags_[i].IsGcPoint()) {
3109 local_gc_points++;
3110 max_insn = i;
3111 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003112 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003113 }
3114 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003115 *gc_points = local_gc_points;
3116 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3117 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003118 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003119 i++;
3120 }
3121 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003122}
3123
Ian Rogers776ac1f2012-04-13 23:36:36 -07003124const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003125 size_t num_entries, ref_bitmap_bits, pc_bits;
3126 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3127 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003128 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003129 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003130 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003131 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003132 return NULL;
3133 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003134 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3135 // There are 2 bytes to encode the number of entries
3136 if (num_entries >= 65536) {
3137 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003138 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003139 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003140 return NULL;
3141 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003142 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003143 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003144 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003145 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003146 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003147 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003148 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003149 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003150 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003151 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003152 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003153 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3154 return NULL;
3155 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003156 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003157 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003158 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003159 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003160 return NULL;
3161 }
3162 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003163 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3164 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003165 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003166 table->push_back(num_entries & 0xFF);
3167 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003168 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003169 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3170 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003171 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003172 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003173 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003174 }
3175 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003176 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003177 }
3178 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003179 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003180 return table;
3181}
jeffhaoa0a764a2011-09-16 10:43:38 -07003182
Ian Rogers776ac1f2012-04-13 23:36:36 -07003183void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003184 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3185 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003186 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003187 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003188 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003189 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3190 if (insn_flags_[i].IsGcPoint()) {
3191 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003192 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003193 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3194 map_index++;
3195 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003196 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003197 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003198 CHECK_LT(j / 8, map.RegWidth());
3199 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3200 } else if ((j / 8) < map.RegWidth()) {
3201 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3202 } else {
3203 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3204 }
3205 }
3206 } else {
3207 CHECK(reg_bitmap == NULL);
3208 }
3209 }
3210}
jeffhaoa0a764a2011-09-16 10:43:38 -07003211
Ian Rogers0c7abda2012-09-19 13:33:42 -07003212void MethodVerifier::SetDexGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003213 {
Ian Rogers0c7abda2012-09-19 13:33:42 -07003214 MutexLock mu(*dex_gc_maps_lock_);
3215 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
3216 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003217 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003218 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003219 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003220 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003221 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003222 CHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003223}
3224
Ian Rogers0c7abda2012-09-19 13:33:42 -07003225const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(Compiler::MethodReference ref) {
3226 MutexLock mu(*dex_gc_maps_lock_);
3227 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
3228 if (it == dex_gc_maps_->end()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003229 return NULL;
3230 }
3231 CHECK(it->second != NULL);
3232 return it->second;
3233}
3234
Ian Rogers0c7abda2012-09-19 13:33:42 -07003235Mutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
3236MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003237
3238Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
3239MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
3240
3241#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
3242Mutex* MethodVerifier::inferred_reg_category_maps_lock_ = NULL;
3243MethodVerifier::InferredRegCategoryMapTable* MethodVerifier::inferred_reg_category_maps_ = NULL;
3244#endif
3245
3246void MethodVerifier::Init() {
Ian Rogers0c7abda2012-09-19 13:33:42 -07003247 dex_gc_maps_lock_ = new Mutex("verifier GC maps lock");
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003248 {
Ian Rogers0c7abda2012-09-19 13:33:42 -07003249 MutexLock mu(*dex_gc_maps_lock_);
3250 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003251 }
3252
3253 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
3254 {
3255 MutexLock mu(*rejected_classes_lock_);
3256 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
3257 }
3258
3259#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
3260 inferred_reg_category_maps_lock_ = new Mutex("verifier GC maps lock");
3261 {
3262 MutexLock mu(*inferred_reg_category_maps_lock_);
3263 inferred_reg_category_maps_ = new MethodVerifier::InferredRegCategoryMapTable;
3264 }
3265#endif
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003266}
3267
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003268void MethodVerifier::Shutdown() {
3269 {
Ian Rogers0c7abda2012-09-19 13:33:42 -07003270 MutexLock mu(*dex_gc_maps_lock_);
3271 STLDeleteValues(dex_gc_maps_);
3272 delete dex_gc_maps_;
3273 dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003274 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003275 delete dex_gc_maps_lock_;
3276 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003277
3278 {
3279 MutexLock mu(*rejected_classes_lock_);
3280 delete rejected_classes_;
3281 rejected_classes_ = NULL;
3282 }
3283 delete rejected_classes_lock_;
3284 rejected_classes_lock_ = NULL;
3285
3286#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
3287 {
3288 MutexLock mu(*inferred_reg_category_maps_lock_);
3289 STLDeleteValues(inferred_reg_category_maps_);
3290 delete inferred_reg_category_maps_;
3291 inferred_reg_category_maps_ = NULL;
3292 }
3293 delete inferred_reg_category_maps_lock_;
3294 inferred_reg_category_maps_lock_ = NULL;
3295#endif
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003296}
jeffhaod1224c72012-02-29 13:43:08 -08003297
Ian Rogers776ac1f2012-04-13 23:36:36 -07003298void MethodVerifier::AddRejectedClass(Compiler::ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003299 {
3300 MutexLock mu(*rejected_classes_lock_);
3301 rejected_classes_->insert(ref);
3302 }
jeffhaod1224c72012-02-29 13:43:08 -08003303 CHECK(IsClassRejected(ref));
3304}
3305
Ian Rogers776ac1f2012-04-13 23:36:36 -07003306bool MethodVerifier::IsClassRejected(Compiler::ClassReference ref) {
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003307 MutexLock mu(*rejected_classes_lock_);
3308 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08003309}
3310
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -07003311#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
TDYa12789f96052012-07-12 20:49:53 -07003312const greenland::InferredRegCategoryMap* MethodVerifier::GenerateInferredRegCategoryMap() {
Logan Chienfca7e872011-12-20 20:08:22 +08003313 uint32_t insns_size = code_item_->insns_size_in_code_units_;
3314 uint16_t regs_size = code_item_->registers_size_;
3315
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003316 UniquePtr<InferredRegCategoryMap> table(new InferredRegCategoryMap(insns_size, regs_size));
Logan Chienfca7e872011-12-20 20:08:22 +08003317
3318 for (size_t i = 0; i < insns_size; ++i) {
3319 if (RegisterLine* line = reg_table_.GetLine(i)) {
TDYa127526643e2012-05-26 01:01:48 -07003320 const Instruction* inst = Instruction::At(code_item_->insns_ + i);
3321
3322 // GC points
3323 if (inst->IsBranch() || inst->IsInvoke()) {
3324 for (size_t r = 0; r < regs_size; ++r) {
3325 const RegType &rt = line->GetRegisterType(r);
3326 if (rt.IsNonZeroReferenceTypes()) {
3327 table->SetRegCanBeObject(r);
3328 }
TDYa127b2eb5c12012-05-24 15:52:10 -07003329 }
3330 }
3331
TDYa127526643e2012-05-26 01:01:48 -07003332 /* We only use InferredRegCategoryMap in one case */
3333 if (inst->IsBranch()) {
TDYa127b2eb5c12012-05-24 15:52:10 -07003334 for (size_t r = 0; r < regs_size; ++r) {
3335 const RegType &rt = line->GetRegisterType(r);
3336
3337 if (rt.IsZero()) {
TDYa12789f96052012-07-12 20:49:53 -07003338 table->SetRegCategory(i, r, greenland::kRegZero);
TDYa127b2eb5c12012-05-24 15:52:10 -07003339 } else if (rt.IsCategory1Types()) {
TDYa12789f96052012-07-12 20:49:53 -07003340 table->SetRegCategory(i, r, greenland::kRegCat1nr);
TDYa127b2eb5c12012-05-24 15:52:10 -07003341 } else if (rt.IsCategory2Types()) {
TDYa12789f96052012-07-12 20:49:53 -07003342 table->SetRegCategory(i, r, greenland::kRegCat2);
TDYa127b2eb5c12012-05-24 15:52:10 -07003343 } else if (rt.IsReferenceTypes()) {
TDYa12789f96052012-07-12 20:49:53 -07003344 table->SetRegCategory(i, r, greenland::kRegObject);
TDYa127b2eb5c12012-05-24 15:52:10 -07003345 } else {
TDYa12789f96052012-07-12 20:49:53 -07003346 table->SetRegCategory(i, r, greenland::kRegUnknown);
TDYa127b2eb5c12012-05-24 15:52:10 -07003347 }
Logan Chienfca7e872011-12-20 20:08:22 +08003348 }
3349 }
3350 }
3351 }
3352
3353 return table.release();
3354}
Logan Chiendd361c92012-04-10 23:40:37 +08003355
Ian Rogers776ac1f2012-04-13 23:36:36 -07003356void MethodVerifier::SetInferredRegCategoryMap(Compiler::MethodReference ref,
3357 const InferredRegCategoryMap& inferred_reg_category_map) {
Shih-wei Liaocd05a622012-08-15 00:02:05 -07003358 {
3359 MutexLock mu(*inferred_reg_category_maps_lock_);
3360 InferredRegCategoryMapTable::iterator it = inferred_reg_category_maps_->find(ref);
3361 if (it == inferred_reg_category_maps_->end()) {
3362 inferred_reg_category_maps_->Put(ref, &inferred_reg_category_map);
3363 } else {
3364 CHECK(*(it->second) == inferred_reg_category_map);
3365 delete &inferred_reg_category_map;
3366 }
Logan Chiendd361c92012-04-10 23:40:37 +08003367 }
Logan Chiendd361c92012-04-10 23:40:37 +08003368 CHECK(GetInferredRegCategoryMap(ref) != NULL);
3369}
3370
TDYa12789f96052012-07-12 20:49:53 -07003371const greenland::InferredRegCategoryMap*
Ian Rogers776ac1f2012-04-13 23:36:36 -07003372MethodVerifier::GetInferredRegCategoryMap(Compiler::MethodReference ref) {
Logan Chiendd361c92012-04-10 23:40:37 +08003373 MutexLock mu(*inferred_reg_category_maps_lock_);
3374
3375 InferredRegCategoryMapTable::const_iterator it =
3376 inferred_reg_category_maps_->find(ref);
3377
3378 if (it == inferred_reg_category_maps_->end()) {
3379 return NULL;
3380 }
3381 CHECK(it->second != NULL);
3382 return it->second;
3383}
Logan Chienfca7e872011-12-20 20:08:22 +08003384#endif
3385
Ian Rogersd81871c2011-10-03 13:57:23 -07003386} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003387} // namespace art