blob: ad238b8310df0c0bfadb2bbf9e26b1265b481d0c [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();
jeffhao9b0b1882012-10-01 16:51:22 -0700215 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800216 while (it.HasNextDirectMethod()) {
217 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700218 if (method_idx == previous_direct_method_idx) {
219 // smali can create dex files with two encoded_methods sharing the same method_idx
220 // http://code.google.com/p/smali/issues/detail?id=119
221 it.Next();
222 continue;
223 }
224 previous_direct_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700225 InvokeType type = it.GetMethodInvokeType(class_def);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700226 AbstractMethod* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700227 if (method == NULL) {
228 DCHECK(Thread::Current()->IsExceptionPending());
229 // We couldn't resolve the method, but continue regardless.
230 Thread::Current()->ClearException();
231 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700232 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
233 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags());
234 if (result != kNoFailure) {
235 if (result == kHardFailure) {
236 hard_fail = true;
237 if (error_count > 0) {
238 error += "\n";
239 }
240 error = "Verifier rejected class ";
241 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
242 error += " due to bad method ";
243 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700244 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700245 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800246 }
247 it.Next();
248 }
jeffhao9b0b1882012-10-01 16:51:22 -0700249 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800250 while (it.HasNextVirtualMethod()) {
251 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700252 if (method_idx == previous_virtual_method_idx) {
253 // smali can create dex files with two encoded_methods sharing the same method_idx
254 // http://code.google.com/p/smali/issues/detail?id=119
255 it.Next();
256 continue;
257 }
258 previous_virtual_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700259 InvokeType type = it.GetMethodInvokeType(class_def);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700260 AbstractMethod* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700261 if (method == NULL) {
262 DCHECK(Thread::Current()->IsExceptionPending());
263 // We couldn't resolve the method, but continue regardless.
264 Thread::Current()->ClearException();
265 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700266 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
267 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags());
268 if (result != kNoFailure) {
269 if (result == kHardFailure) {
270 hard_fail = true;
271 if (error_count > 0) {
272 error += "\n";
273 }
274 error = "Verifier rejected class ";
275 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
276 error += " due to bad method ";
277 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700278 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700279 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800280 }
281 it.Next();
282 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700283 if (error_count == 0) {
284 return kNoFailure;
285 } else {
286 return hard_fail ? kHardFailure : kSoftFailure;
287 }
jeffhaof56197c2012-03-05 18:01:54 -0800288}
289
jeffhaof1e6b7c2012-06-05 18:33:30 -0700290MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx, const DexFile* dex_file,
Ian Rogers365c1022012-06-22 15:05:28 -0700291 DexCache* dex_cache, ClassLoader* class_loader, uint32_t class_def_idx,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700292 const DexFile::CodeItem* code_item, AbstractMethod* method, uint32_t method_access_flags) {
Ian Rogersc8982582012-09-07 16:53:25 -0700293 MethodVerifier::FailureKind result = kNoFailure;
294 uint64_t start_ns = NanoTime();
295
Ian Rogersad0b3a32012-04-16 14:50:24 -0700296 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item, method_idx,
297 method, method_access_flags);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700298 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700299 // Verification completed, however failures may be pending that didn't cause the verification
300 // to hard fail.
Ian Rogerse551e952012-06-03 22:59:14 -0700301 CHECK(!verifier.have_pending_hard_failure_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700302 if (verifier.failures_.size() != 0) {
303 verifier.DumpFailures(LOG(INFO) << "Soft verification failures in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700304 << PrettyMethod(method_idx, *dex_file) << "\n");
Ian Rogersc8982582012-09-07 16:53:25 -0700305 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800306 }
307 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700308 // Bad method data.
309 CHECK_NE(verifier.failures_.size(), 0U);
310 CHECK(verifier.have_pending_hard_failure_);
311 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700312 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800313 if (gDebugVerify) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700314 std::cout << "\n" << verifier.info_messages_.str();
jeffhaof56197c2012-03-05 18:01:54 -0800315 verifier.Dump(std::cout);
316 }
Ian Rogersc8982582012-09-07 16:53:25 -0700317 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800318 }
Ian Rogersc8982582012-09-07 16:53:25 -0700319 uint64_t duration_ns = NanoTime() - start_ns;
320 if (duration_ns > MsToNs(100)) {
321 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
322 << " took " << PrettyDuration(duration_ns);
323 }
324 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800325}
326
Mathieu Chartier66f19252012-09-18 08:57:04 -0700327void MethodVerifier::VerifyMethodAndDump(AbstractMethod* method) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800328 CHECK(method != NULL);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700329 MethodHelper mh(method);
330 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
331 mh.GetClassDefIndex(), mh.GetCodeItem(), method->GetDexMethodIndex(),
332 method, method->GetAccessFlags());
333 verifier.Verify();
Elliott Hughesc073b072012-05-24 19:29:17 -0700334 verifier.DumpFailures(LOG(INFO) << "Dump of method " << PrettyMethod(method) << "\n")
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700335 << verifier.info_messages_.str() << MutatorLockedDumpable<MethodVerifier>(verifier);
jeffhaoba5ebb92011-08-25 17:24:37 -0700336}
337
Ian Rogers776ac1f2012-04-13 23:36:36 -0700338MethodVerifier::MethodVerifier(const DexFile* dex_file, DexCache* dex_cache,
Ian Rogers365c1022012-06-22 15:05:28 -0700339 ClassLoader* class_loader, uint32_t class_def_idx, const DexFile::CodeItem* code_item,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700340 uint32_t method_idx, AbstractMethod* method, uint32_t method_access_flags)
jeffhaof56197c2012-03-05 18:01:54 -0800341 : work_insn_idx_(-1),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700342 method_idx_(method_idx),
343 foo_method_(method),
344 method_access_flags_(method_access_flags),
jeffhaof56197c2012-03-05 18:01:54 -0800345 dex_file_(dex_file),
346 dex_cache_(dex_cache),
347 class_loader_(class_loader),
348 class_def_idx_(class_def_idx),
349 code_item_(code_item),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700350 interesting_dex_pc_(-1),
351 monitor_enter_dex_pcs_(NULL),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700352 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700353 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800354 new_instance_count_(0),
355 monitor_enter_count_(0) {
356}
357
Mathieu Chartier66f19252012-09-18 08:57:04 -0700358void MethodVerifier::FindLocksAtDexPc(AbstractMethod* m, uint32_t dex_pc, std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700359 MethodHelper mh(m);
360 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
361 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
362 m, m->GetAccessFlags());
363 verifier.interesting_dex_pc_ = dex_pc;
364 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
365 verifier.FindLocksAtDexPc();
366}
367
368void MethodVerifier::FindLocksAtDexPc() {
369 CHECK(monitor_enter_dex_pcs_ != NULL);
370 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
371
372 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
373 // verification. In practice, the phase we want relies on data structures set up by all the
374 // earlier passes, so we just run the full method verification and bail out early when we've
375 // got what we wanted.
376 Verify();
377}
378
Ian Rogersad0b3a32012-04-16 14:50:24 -0700379bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700380 // If there aren't any instructions, make sure that's expected, then exit successfully.
381 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700382 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700383 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700384 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700385 } else {
386 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700387 }
jeffhaobdb76512011-09-07 11:43:16 -0700388 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700389 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
390 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700391 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
392 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700393 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700394 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700395 // Allocate and initialize an array to hold instruction data.
396 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
397 // Run through the instructions and see if the width checks out.
398 bool result = ComputeWidthsAndCountOps();
399 // Flag instructions guarded by a "try" block and check exception handlers.
400 result = result && ScanTryCatchBlocks();
401 // Perform static instruction verification.
402 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700403 // Perform code-flow analysis and return.
404 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700405}
406
Ian Rogers776ac1f2012-04-13 23:36:36 -0700407std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700408 switch (error) {
409 case VERIFY_ERROR_NO_CLASS:
410 case VERIFY_ERROR_NO_FIELD:
411 case VERIFY_ERROR_NO_METHOD:
412 case VERIFY_ERROR_ACCESS_CLASS:
413 case VERIFY_ERROR_ACCESS_FIELD:
414 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700415 case VERIFY_ERROR_INSTANTIATION:
416 case VERIFY_ERROR_CLASS_CHANGE:
jeffhaofaf459e2012-08-31 15:32:47 -0700417 if (Runtime::Current()->IsCompiler()) {
418 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
419 // class change and instantiation errors into soft verification errors so that we re-verify
420 // at runtime. We may fail to find or to agree on access because of not yet available class
421 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
422 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
423 // paths" that dynamically perform the verification and cause the behavior to be that akin
424 // to an interpreter.
425 error = VERIFY_ERROR_BAD_CLASS_SOFT;
426 } else {
427 have_pending_runtime_throw_failure_ = true;
428 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700429 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700430 // Indication that verification should be retried at runtime.
431 case VERIFY_ERROR_BAD_CLASS_SOFT:
432 if (!Runtime::Current()->IsCompiler()) {
433 // It is runtime so hard fail.
434 have_pending_hard_failure_ = true;
435 }
436 break;
jeffhaod5347e02012-03-22 17:25:05 -0700437 // Hard verification failures at compile time will still fail at runtime, so the class is
438 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700439 case VERIFY_ERROR_BAD_CLASS_HARD: {
440 if (Runtime::Current()->IsCompiler()) {
jeffhaof56197c2012-03-05 18:01:54 -0800441 Compiler::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800442 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800443 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700444 have_pending_hard_failure_ = true;
445 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800446 }
447 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700448 failures_.push_back(error);
449 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(method_idx_, *dex_file_).c_str(),
450 work_insn_idx_));
451 std::ostringstream* failure_message = new std::ostringstream(location);
452 failure_messages_.push_back(failure_message);
453 return *failure_message;
454}
455
456void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
457 size_t failure_num = failure_messages_.size();
458 DCHECK_NE(failure_num, 0U);
459 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
460 prepend += last_fail_message->str();
461 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
462 delete last_fail_message;
463}
464
465void MethodVerifier::AppendToLastFailMessage(std::string append) {
466 size_t failure_num = failure_messages_.size();
467 DCHECK_NE(failure_num, 0U);
468 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
469 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800470}
471
Ian Rogers776ac1f2012-04-13 23:36:36 -0700472bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700473 const uint16_t* insns = code_item_->insns_;
474 size_t insns_size = code_item_->insns_size_in_code_units_;
475 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700476 size_t new_instance_count = 0;
477 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700478 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700479
Ian Rogersd81871c2011-10-03 13:57:23 -0700480 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700481 Instruction::Code opcode = inst->Opcode();
482 if (opcode == Instruction::NEW_INSTANCE) {
483 new_instance_count++;
484 } else if (opcode == Instruction::MONITOR_ENTER) {
485 monitor_enter_count++;
486 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700487 size_t inst_size = inst->SizeInCodeUnits();
488 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
489 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700490 inst = inst->Next();
491 }
492
Ian Rogersd81871c2011-10-03 13:57:23 -0700493 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700494 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
495 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700496 return false;
497 }
498
Ian Rogersd81871c2011-10-03 13:57:23 -0700499 new_instance_count_ = new_instance_count;
500 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700501 return true;
502}
503
Ian Rogers776ac1f2012-04-13 23:36:36 -0700504bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700505 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700506 if (tries_size == 0) {
507 return true;
508 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700509 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700510 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700511
512 for (uint32_t idx = 0; idx < tries_size; idx++) {
513 const DexFile::TryItem* try_item = &tries[idx];
514 uint32_t start = try_item->start_addr_;
515 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700516 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700517 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
518 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700519 return false;
520 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700521 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700522 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700523 return false;
524 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700525 for (uint32_t dex_pc = start; dex_pc < end;
526 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
527 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700528 }
529 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800530 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700531 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700532 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700533 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700534 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700535 CatchHandlerIterator iterator(handlers_ptr);
536 for (; iterator.HasNext(); iterator.Next()) {
537 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700538 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700539 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700540 return false;
541 }
jeffhao60f83e32012-02-13 17:16:30 -0800542 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
543 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700544 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700545 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800546 return false;
547 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700548 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700549 // Ensure exception types are resolved so that they don't need resolution to be delivered,
550 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700551 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
jeffhaof56197c2012-03-05 18:01:54 -0800552 Class* exception_type = linker->ResolveType(*dex_file_, iterator.GetHandlerTypeIndex(),
553 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700554 if (exception_type == NULL) {
555 DCHECK(Thread::Current()->IsExceptionPending());
556 Thread::Current()->ClearException();
557 }
558 }
jeffhaobdb76512011-09-07 11:43:16 -0700559 }
Ian Rogers0571d352011-11-03 19:51:38 -0700560 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700561 }
jeffhaobdb76512011-09-07 11:43:16 -0700562 return true;
563}
564
Ian Rogers776ac1f2012-04-13 23:36:36 -0700565bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700566 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700567
Ian Rogers0c7abda2012-09-19 13:33:42 -0700568 /* 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 -0700569 insn_flags_[0].SetBranchTarget();
Ian Rogers0c7abda2012-09-19 13:33:42 -0700570 insn_flags_[0].SetGcPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700571
572 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700573 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700574 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700575 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700576 return false;
577 }
578 /* Flag instructions that are garbage collection points */
579 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
580 insn_flags_[dex_pc].SetGcPoint();
581 }
582 dex_pc += inst->SizeInCodeUnits();
583 inst = inst->Next();
584 }
585 return true;
586}
587
Ian Rogers776ac1f2012-04-13 23:36:36 -0700588bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800589 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700590 bool result = true;
591 switch (inst->GetVerifyTypeArgumentA()) {
592 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800593 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700594 break;
595 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800596 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700597 break;
598 }
599 switch (inst->GetVerifyTypeArgumentB()) {
600 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800601 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700602 break;
603 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800604 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700605 break;
606 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800607 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700608 break;
609 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800610 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700611 break;
612 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800613 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700614 break;
615 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800616 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700617 break;
618 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800619 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700620 break;
621 }
622 switch (inst->GetVerifyTypeArgumentC()) {
623 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800624 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700625 break;
626 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800627 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700628 break;
629 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800630 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700631 break;
632 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800633 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700634 break;
635 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800636 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700637 break;
638 }
639 switch (inst->GetVerifyExtraFlags()) {
640 case Instruction::kVerifyArrayData:
641 result = result && CheckArrayData(code_offset);
642 break;
643 case Instruction::kVerifyBranchTarget:
644 result = result && CheckBranchTarget(code_offset);
645 break;
646 case Instruction::kVerifySwitchTargets:
647 result = result && CheckSwitchTargets(code_offset);
648 break;
649 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800650 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700651 break;
652 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800653 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700654 break;
655 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700656 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700657 result = false;
658 break;
659 }
660 return result;
661}
662
Ian Rogers776ac1f2012-04-13 23:36:36 -0700663bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700664 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700665 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
666 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700667 return false;
668 }
669 return true;
670}
671
Ian Rogers776ac1f2012-04-13 23:36:36 -0700672bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700673 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700674 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
675 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700676 return false;
677 }
678 return true;
679}
680
Ian Rogers776ac1f2012-04-13 23:36:36 -0700681bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700682 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700683 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
684 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700685 return false;
686 }
687 return true;
688}
689
Ian Rogers776ac1f2012-04-13 23:36:36 -0700690bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700691 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700692 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
693 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700694 return false;
695 }
696 return true;
697}
698
Ian Rogers776ac1f2012-04-13 23:36:36 -0700699bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700700 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700701 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
702 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700703 return false;
704 }
705 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700706 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700707 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700708 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700709 return false;
710 }
711 return true;
712}
713
Ian Rogers776ac1f2012-04-13 23:36:36 -0700714bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700715 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700716 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
717 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700718 return false;
719 }
720 return true;
721}
722
Ian Rogers776ac1f2012-04-13 23:36:36 -0700723bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700724 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700725 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
726 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700727 return false;
728 }
729 return true;
730}
731
Ian Rogers776ac1f2012-04-13 23:36:36 -0700732bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700733 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
735 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700736 return false;
737 }
738 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700739 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700740 const char* cp = descriptor;
741 while (*cp++ == '[') {
742 bracket_count++;
743 }
744 if (bracket_count == 0) {
745 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700746 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700747 return false;
748 } else if (bracket_count > 255) {
749 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700750 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700751 return false;
752 }
753 return true;
754}
755
Ian Rogers776ac1f2012-04-13 23:36:36 -0700756bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700757 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
758 const uint16_t* insns = code_item_->insns_ + cur_offset;
759 const uint16_t* array_data;
760 int32_t array_data_offset;
761
762 DCHECK_LT(cur_offset, insn_count);
763 /* make sure the start of the array data table is in range */
764 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
765 if ((int32_t) cur_offset + array_data_offset < 0 ||
766 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700767 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
768 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700769 return false;
770 }
771 /* offset to array data table is a relative branch-style offset */
772 array_data = insns + array_data_offset;
773 /* make sure the table is 32-bit aligned */
774 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700775 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
776 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700777 return false;
778 }
779 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700780 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700781 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
782 /* make sure the end of the switch is in range */
783 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700784 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
785 << ", data offset " << array_data_offset << ", end "
786 << cur_offset + array_data_offset + table_size
787 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700788 return false;
789 }
790 return true;
791}
792
Ian Rogers776ac1f2012-04-13 23:36:36 -0700793bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700794 int32_t offset;
795 bool isConditional, selfOkay;
796 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
797 return false;
798 }
799 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700800 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 -0700801 return false;
802 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700803 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
804 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700805 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700806 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700807 return false;
808 }
809 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
810 int32_t abs_offset = cur_offset + offset;
811 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700812 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700813 << reinterpret_cast<void*>(abs_offset) << ") at "
814 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700815 return false;
816 }
817 insn_flags_[abs_offset].SetBranchTarget();
818 return true;
819}
820
Ian Rogers776ac1f2012-04-13 23:36:36 -0700821bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700822 bool* selfOkay) {
823 const uint16_t* insns = code_item_->insns_ + cur_offset;
824 *pConditional = false;
825 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700826 switch (*insns & 0xff) {
827 case Instruction::GOTO:
828 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700829 break;
830 case Instruction::GOTO_32:
831 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700832 *selfOkay = true;
833 break;
834 case Instruction::GOTO_16:
835 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700836 break;
837 case Instruction::IF_EQ:
838 case Instruction::IF_NE:
839 case Instruction::IF_LT:
840 case Instruction::IF_GE:
841 case Instruction::IF_GT:
842 case Instruction::IF_LE:
843 case Instruction::IF_EQZ:
844 case Instruction::IF_NEZ:
845 case Instruction::IF_LTZ:
846 case Instruction::IF_GEZ:
847 case Instruction::IF_GTZ:
848 case Instruction::IF_LEZ:
849 *pOffset = (int16_t) insns[1];
850 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700851 break;
852 default:
853 return false;
854 break;
855 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700856 return true;
857}
858
Ian Rogers776ac1f2012-04-13 23:36:36 -0700859bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700860 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700861 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700862 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700863 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700864 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
865 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700866 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
867 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700868 return false;
869 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700870 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700871 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700872 /* make sure the table is 32-bit aligned */
873 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700874 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
875 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700876 return false;
877 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700878 uint32_t switch_count = switch_insns[1];
879 int32_t keys_offset, targets_offset;
880 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700881 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
882 /* 0=sig, 1=count, 2/3=firstKey */
883 targets_offset = 4;
884 keys_offset = -1;
885 expected_signature = Instruction::kPackedSwitchSignature;
886 } else {
887 /* 0=sig, 1=count, 2..count*2 = keys */
888 keys_offset = 2;
889 targets_offset = 2 + 2 * switch_count;
890 expected_signature = Instruction::kSparseSwitchSignature;
891 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700893 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700894 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
895 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700896 return false;
897 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700898 /* make sure the end of the switch is in range */
899 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700900 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
901 << switch_offset << ", end "
902 << (cur_offset + switch_offset + table_size)
903 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700904 return false;
905 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700906 /* for a sparse switch, verify the keys are in ascending order */
907 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700908 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
909 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700910 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
911 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
912 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700913 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
914 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700915 return false;
916 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700917 last_key = key;
918 }
919 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700920 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700921 for (uint32_t targ = 0; targ < switch_count; targ++) {
922 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
923 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
924 int32_t abs_offset = cur_offset + offset;
925 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700926 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700927 << reinterpret_cast<void*>(abs_offset) << ") at "
928 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700929 return false;
930 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700931 insn_flags_[abs_offset].SetBranchTarget();
932 }
933 return true;
934}
935
Ian Rogers776ac1f2012-04-13 23:36:36 -0700936bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700937 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700938 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700939 return false;
940 }
941 uint16_t registers_size = code_item_->registers_size_;
942 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800943 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700944 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
945 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700946 return false;
947 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700948 }
949
950 return true;
951}
952
Ian Rogers776ac1f2012-04-13 23:36:36 -0700953bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700954 uint16_t registers_size = code_item_->registers_size_;
955 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
956 // integer overflow when adding them here.
957 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700958 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
959 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700960 return false;
961 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700962 return true;
963}
964
Ian Rogers30bce5a2012-09-20 16:30:53 -0700965#if !defined(ART_USE_LLVM_COMPILER) && !defined(ART_USE_GREENLAND_COMPILER)
Ian Rogers0c7abda2012-09-19 13:33:42 -0700966static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -0800967 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
968 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
969 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
970 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
971 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
972 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
973 gc_map.begin(),
974 gc_map.end());
975 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
976 DCHECK_EQ(gc_map.size(),
977 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
978 (length_prefixed_gc_map->at(1) << 16) |
979 (length_prefixed_gc_map->at(2) << 8) |
980 (length_prefixed_gc_map->at(3) << 0)));
981 return length_prefixed_gc_map;
982}
Ian Rogers30bce5a2012-09-20 16:30:53 -0700983#endif
Brian Carlstrom75412882012-01-18 01:26:54 -0800984
Ian Rogers776ac1f2012-04-13 23:36:36 -0700985bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700986 uint16_t registers_size = code_item_->registers_size_;
987 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -0700988
Ian Rogersd81871c2011-10-03 13:57:23 -0700989 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -0800990 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
991 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700992 }
993 /* Create and initialize table holding register status */
Elliott Hughes460384f2012-04-04 16:53:10 -0700994 reg_table_.Init(kTrackRegsGcPoints, insn_flags_.get(), insns_size, registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -0700995
Ian Rogersd81871c2011-10-03 13:57:23 -0700996 work_line_.reset(new RegisterLine(registers_size, this));
997 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -0700998
Ian Rogersd81871c2011-10-03 13:57:23 -0700999 /* Initialize register types of method arguments. */
1000 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001001 DCHECK_NE(failures_.size(), 0U);
1002 std::string prepend("Bad signature in ");
1003 prepend += PrettyMethod(method_idx_, *dex_file_);
1004 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001005 return false;
1006 }
1007 /* Perform code flow verification. */
1008 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001009 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001010 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001011 }
1012
TDYa127b2eb5c12012-05-24 15:52:10 -07001013 Compiler::MethodReference ref(dex_file_, method_idx_);
1014
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -07001015#if !defined(ART_USE_LLVM_COMPILER) && !defined(ART_USE_GREENLAND_COMPILER)
TDYa127b2eb5c12012-05-24 15:52:10 -07001016
Ian Rogersd81871c2011-10-03 13:57:23 -07001017 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -08001018 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1019 if (map.get() == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001020 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001021 return false; // Not a real failure, but a failure to encode
1022 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001023#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001024 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -07001025#endif
Ian Rogers0c7abda2012-09-19 13:33:42 -07001026 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
1027 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
Logan Chiendd361c92012-04-10 23:40:37 +08001028
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -07001029#else // defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Logan Chienfca7e872011-12-20 20:08:22 +08001030 /* Generate Inferred Register Category for LLVM-based Code Generator */
1031 const InferredRegCategoryMap* table = GenerateInferredRegCategoryMap();
Ian Rogers776ac1f2012-04-13 23:36:36 -07001032 verifier::MethodVerifier::SetInferredRegCategoryMap(ref, *table);
TDYa127b2eb5c12012-05-24 15:52:10 -07001033
Logan Chienfca7e872011-12-20 20:08:22 +08001034#endif
1035
jeffhaobdb76512011-09-07 11:43:16 -07001036 return true;
1037}
1038
Ian Rogersad0b3a32012-04-16 14:50:24 -07001039std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1040 DCHECK_EQ(failures_.size(), failure_messages_.size());
1041 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001042 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001043 }
1044 return os;
1045}
1046
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001047extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001048 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001049 v->Dump(std::cerr);
1050}
1051
Ian Rogers776ac1f2012-04-13 23:36:36 -07001052void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001053 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001054 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001055 return;
jeffhaobdb76512011-09-07 11:43:16 -07001056 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001057 DCHECK(code_item_ != NULL);
1058 const Instruction* inst = Instruction::At(code_item_->insns_);
1059 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1060 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Elliott Hughesaa6e1cd2012-01-18 19:26:06 -08001061 os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
Elliott Hughesc073b072012-05-24 19:29:17 -07001062 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001063 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1064 if (reg_line != NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001065 os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001066 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001067 inst = inst->Next();
1068 }
jeffhaobdb76512011-09-07 11:43:16 -07001069}
1070
Ian Rogersd81871c2011-10-03 13:57:23 -07001071static bool IsPrimitiveDescriptor(char descriptor) {
1072 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001073 case 'I':
1074 case 'C':
1075 case 'S':
1076 case 'B':
1077 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001078 case 'F':
1079 case 'D':
1080 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001081 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001082 default:
1083 return false;
1084 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001085}
1086
Ian Rogers776ac1f2012-04-13 23:36:36 -07001087bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001088 RegisterLine* reg_line = reg_table_.GetLine(0);
1089 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1090 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001091
Ian Rogersd81871c2011-10-03 13:57:23 -07001092 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1093 //Include the "this" pointer.
1094 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001095 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001096 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1097 // argument as uninitialized. This restricts field access until the superclass constructor is
1098 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001099 const RegType& declaring_class = GetDeclaringClass();
1100 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001101 reg_line->SetRegisterType(arg_start + cur_arg,
1102 reg_types_.UninitializedThisArgument(declaring_class));
1103 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001104 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001105 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001106 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001107 }
1108
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001109 const DexFile::ProtoId& proto_id =
Ian Rogersad0b3a32012-04-16 14:50:24 -07001110 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001111 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001112
1113 for (; iterator.HasNext(); iterator.Next()) {
1114 const char* descriptor = iterator.GetDescriptor();
1115 if (descriptor == NULL) {
1116 LOG(FATAL) << "Null descriptor";
1117 }
1118 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001119 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1120 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001121 return false;
1122 }
1123 switch (descriptor[0]) {
1124 case 'L':
1125 case '[':
1126 // We assume that reference arguments are initialized. The only way it could be otherwise
1127 // (assuming the caller was verified) is if the current method is <init>, but in that case
1128 // it's effectively considered initialized the instant we reach here (in the sense that we
1129 // can return without doing anything or call virtual methods).
1130 {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001131 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001132 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001133 }
1134 break;
1135 case 'Z':
1136 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1137 break;
1138 case 'C':
1139 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1140 break;
1141 case 'B':
1142 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1143 break;
1144 case 'I':
1145 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1146 break;
1147 case 'S':
1148 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1149 break;
1150 case 'F':
1151 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1152 break;
1153 case 'J':
1154 case 'D': {
1155 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1156 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1157 cur_arg++;
1158 break;
1159 }
1160 default:
jeffhaod5347e02012-03-22 17:25:05 -07001161 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001162 return false;
1163 }
1164 cur_arg++;
1165 }
1166 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001167 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001168 return false;
1169 }
1170 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1171 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1172 // format. Only major difference from the method argument format is that 'V' is supported.
1173 bool result;
1174 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1175 result = descriptor[1] == '\0';
1176 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1177 size_t i = 0;
1178 do {
1179 i++;
1180 } while (descriptor[i] == '['); // process leading [
1181 if (descriptor[i] == 'L') { // object array
1182 do {
1183 i++; // find closing ;
1184 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1185 result = descriptor[i] == ';';
1186 } else { // primitive array
1187 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1188 }
1189 } else if (descriptor[0] == 'L') {
1190 // could be more thorough here, but shouldn't be required
1191 size_t i = 0;
1192 do {
1193 i++;
1194 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1195 result = descriptor[i] == ';';
1196 } else {
1197 result = false;
1198 }
1199 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001200 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1201 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001202 }
1203 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001204}
1205
Ian Rogers776ac1f2012-04-13 23:36:36 -07001206bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001207 const uint16_t* insns = code_item_->insns_;
1208 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001209
jeffhaobdb76512011-09-07 11:43:16 -07001210 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001211 insn_flags_[0].SetChanged();
1212 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001213
jeffhaobdb76512011-09-07 11:43:16 -07001214 /* Continue until no instructions are marked "changed". */
1215 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001216 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1217 uint32_t insn_idx = start_guess;
1218 for (; insn_idx < insns_size; insn_idx++) {
1219 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001220 break;
1221 }
jeffhaobdb76512011-09-07 11:43:16 -07001222 if (insn_idx == insns_size) {
1223 if (start_guess != 0) {
1224 /* try again, starting from the top */
1225 start_guess = 0;
1226 continue;
1227 } else {
1228 /* all flags are clear */
1229 break;
1230 }
1231 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001232 // We carry the working set of registers from instruction to instruction. If this address can
1233 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1234 // "changed" flags, we need to load the set of registers from the table.
1235 // Because we always prefer to continue on to the next instruction, we should never have a
1236 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1237 // target.
1238 work_insn_idx_ = insn_idx;
1239 if (insn_flags_[insn_idx].IsBranchTarget()) {
1240 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001241 } else {
1242#ifndef NDEBUG
1243 /*
1244 * Sanity check: retrieve the stored register line (assuming
1245 * a full table) and make sure it actually matches.
1246 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001247 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1248 if (register_line != NULL) {
1249 if (work_line_->CompareLine(register_line) != 0) {
1250 Dump(std::cout);
1251 std::cout << info_messages_.str();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001252 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001253 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1254 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001255 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001256 }
jeffhaobdb76512011-09-07 11:43:16 -07001257 }
1258#endif
1259 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001260 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001261 std::string prepend(PrettyMethod(method_idx_, *dex_file_));
1262 prepend += " failed to verify: ";
1263 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001264 return false;
1265 }
jeffhaobdb76512011-09-07 11:43:16 -07001266 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001267 insn_flags_[insn_idx].SetVisited();
1268 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001269 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001270
Ian Rogers1c849e52012-06-28 14:00:33 -07001271 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001272 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001273 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001274 * (besides the wasted space), but it indicates a flaw somewhere
1275 * down the line, possibly in the verifier.
1276 *
1277 * If we've substituted "always throw" instructions into the stream,
1278 * we are almost certainly going to have some dead code.
1279 */
1280 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001281 uint32_t insn_idx = 0;
1282 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001283 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001284 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001285 * may or may not be preceded by a padding NOP (for alignment).
1286 */
1287 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1288 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1289 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001290 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001291 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1292 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1293 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001294 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001295 }
1296
Ian Rogersd81871c2011-10-03 13:57:23 -07001297 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001298 if (dead_start < 0)
1299 dead_start = insn_idx;
1300 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001301 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001302 dead_start = -1;
1303 }
1304 }
1305 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001306 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001307 }
1308 }
jeffhaobdb76512011-09-07 11:43:16 -07001309 return true;
1310}
1311
Ian Rogers776ac1f2012-04-13 23:36:36 -07001312bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001313#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001314 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001315 gDvm.verifierStats.instrsReexamined++;
1316 } else {
1317 gDvm.verifierStats.instrsExamined++;
1318 }
1319#endif
1320
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001321 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1322 // We want the state _before_ the instruction, for the case where the dex pc we're
1323 // interested in is itself a monitor-enter instruction (which is a likely place
1324 // for a thread to be suspended).
1325 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
1326 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1327 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1328 }
1329 }
1330
jeffhaobdb76512011-09-07 11:43:16 -07001331 /*
1332 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001333 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001334 * control to another statement:
1335 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001336 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001337 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001338 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001339 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001340 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001341 * throw an exception that is handled by an encompassing "try"
1342 * block.
1343 *
1344 * We can also return, in which case there is no successor instruction
1345 * from this point.
1346 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001347 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001348 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001349 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1350 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001351 DecodedInstruction dec_insn(inst);
Ian Rogersa75a0132012-09-28 11:41:42 -07001352 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001353
jeffhaobdb76512011-09-07 11:43:16 -07001354 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001355 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001356 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001357 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001358 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1359 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001360 }
jeffhaobdb76512011-09-07 11:43:16 -07001361
1362 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001363 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001364 * can throw an exception, we will copy/merge this into the "catch"
1365 * address rather than work_line, because we don't want the result
1366 * from the "successful" code path (e.g. a check-cast that "improves"
1367 * a type) to be visible to the exception handler.
1368 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001369 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001370 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001371 } else {
1372#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001373 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001374#endif
1375 }
1376
Elliott Hughesadb8c672012-03-06 16:49:32 -08001377 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07001378 case Instruction::NOP:
1379 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001380 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001381 * a signature that looks like a NOP; if we see one of these in
1382 * the course of executing code then we have a problem.
1383 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001384 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001385 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001386 }
1387 break;
1388
1389 case Instruction::MOVE:
1390 case Instruction::MOVE_FROM16:
1391 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001392 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001393 break;
1394 case Instruction::MOVE_WIDE:
1395 case Instruction::MOVE_WIDE_FROM16:
1396 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001397 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07001398 break;
1399 case Instruction::MOVE_OBJECT:
1400 case Instruction::MOVE_OBJECT_FROM16:
1401 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001402 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001403 break;
1404
1405 /*
1406 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001407 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001408 * might want to hold the result in an actual CPU register, so the
1409 * Dalvik spec requires that these only appear immediately after an
1410 * invoke or filled-new-array.
1411 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001412 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001413 * redundant with the reset done below, but it can make the debug info
1414 * easier to read in some cases.)
1415 */
1416 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001417 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07001418 break;
1419 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001420 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001421 break;
1422 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001423 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07001424 break;
1425
Ian Rogersd81871c2011-10-03 13:57:23 -07001426 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001427 /*
jeffhao60f83e32012-02-13 17:16:30 -08001428 * This statement can only appear as the first instruction in an exception handler. We verify
1429 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001430 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001431 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08001432 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001433 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001434 }
jeffhaobdb76512011-09-07 11:43:16 -07001435 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001436 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1437 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001438 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001439 }
jeffhaobdb76512011-09-07 11:43:16 -07001440 }
1441 break;
1442 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001443 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001444 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001445 const RegType& return_type = GetMethodReturnType();
1446 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001447 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001448 } else {
1449 // Compilers may generate synthetic functions that write byte values into boolean fields.
1450 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001451 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001452 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1453 ((return_type.IsBoolean() || return_type.IsByte() ||
1454 return_type.IsShort() || return_type.IsChar()) &&
1455 src_type.IsInteger()));
1456 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001457 bool success =
1458 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
1459 if (!success) {
1460 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001461 }
jeffhaobdb76512011-09-07 11:43:16 -07001462 }
1463 }
1464 break;
1465 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001466 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001467 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001468 const RegType& return_type = GetMethodReturnType();
1469 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001470 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001471 } else {
1472 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001473 bool success = work_line_->VerifyRegisterType(dec_insn.vA, return_type);
1474 if (!success) {
1475 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001476 }
jeffhaobdb76512011-09-07 11:43:16 -07001477 }
1478 }
1479 break;
1480 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001481 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001482 const RegType& return_type = GetMethodReturnType();
1483 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001484 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001485 } else {
1486 /* return_type is the *expected* return type, not register value */
1487 DCHECK(!return_type.IsZero());
1488 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08001489 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07001490 // Disallow returning uninitialized values and verify that the reference in vAA is an
1491 // instance of the "return_type"
1492 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001493 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001494 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001495 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1496 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001497 }
1498 }
1499 }
1500 break;
1501
1502 case Instruction::CONST_4:
1503 case Instruction::CONST_16:
1504 case Instruction::CONST:
1505 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001506 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const((int32_t) dec_insn.vB));
jeffhaobdb76512011-09-07 11:43:16 -07001507 break;
1508 case Instruction::CONST_HIGH16:
1509 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001510 work_line_->SetRegisterType(dec_insn.vA,
1511 reg_types_.FromCat1Const((int32_t) dec_insn.vB << 16));
jeffhaobdb76512011-09-07 11:43:16 -07001512 break;
1513 case Instruction::CONST_WIDE_16:
1514 case Instruction::CONST_WIDE_32:
1515 case Instruction::CONST_WIDE:
1516 case Instruction::CONST_WIDE_HIGH16:
1517 /* could be long or double; resolved upon use */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001518 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07001519 break;
1520 case Instruction::CONST_STRING:
1521 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001522 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001523 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001524 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001525 // Get type from instruction if unresolved then we need an access check
1526 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08001527 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001528 // Register holds class, ie its type is class, on error it will hold Conflict.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001529 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogersad0b3a32012-04-16 14:50:24 -07001530 res_type.IsConflict() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001531 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001532 }
jeffhaobdb76512011-09-07 11:43:16 -07001533 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001534 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001535 break;
1536 case Instruction::MONITOR_EXIT:
1537 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001538 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001539 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001540 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001541 * to the need to handle asynchronous exceptions, a now-deprecated
1542 * feature that Dalvik doesn't support.)
1543 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001544 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001545 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001546 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001547 * structured locking checks are working, the former would have
1548 * failed on the -enter instruction, and the latter is impossible.
1549 *
1550 * This is fortunate, because issue 3221411 prevents us from
1551 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001552 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001553 * some catch blocks (which will show up as "dead" code when
1554 * we skip them here); if we can't, then the code path could be
1555 * "live" so we still need to check it.
1556 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001557 opcode_flags &= ~Instruction::kThrow;
1558 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001559 break;
1560
Ian Rogers28ad40d2011-10-27 15:19:26 -07001561 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001562 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001563 /*
1564 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1565 * could be a "upcast" -- not expected, so we don't try to address it.)
1566 *
1567 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001568 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001569 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001570 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001571 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001572 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001573 if (res_type.IsConflict()) {
1574 DCHECK_NE(failures_.size(), 0U);
1575 if (!is_checkcast) {
1576 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
1577 }
1578 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001579 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001580 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1581 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001582 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001583 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001584 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001585 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001586 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07001587 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001588 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001589 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001590 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001591 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001592 }
jeffhaobdb76512011-09-07 11:43:16 -07001593 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001594 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001595 }
1596 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001597 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001598 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001599 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001600 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001601 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001602 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001603 }
1604 }
1605 break;
1606 }
1607 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001608 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001609 if (res_type.IsConflict()) {
1610 DCHECK_NE(failures_.size(), 0U);
1611 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001612 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001613 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1614 // can't create an instance of an interface or abstract class */
1615 if (!res_type.IsInstantiableTypes()) {
1616 Fail(VERIFY_ERROR_INSTANTIATION)
1617 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001618 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001619 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001620 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1621 // Any registers holding previous allocations from this address that have not yet been
1622 // initialized must be marked invalid.
1623 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1624 // add the new uninitialized reference to the register state
1625 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001626 break;
1627 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001628 case Instruction::NEW_ARRAY:
1629 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001630 break;
1631 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08001632 VerifyNewArray(dec_insn, true, false);
1633 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001634 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001635 case Instruction::FILLED_NEW_ARRAY_RANGE:
1636 VerifyNewArray(dec_insn, true, true);
1637 just_set_result = true; // Filled new array range sets result register
1638 break;
jeffhaobdb76512011-09-07 11:43:16 -07001639 case Instruction::CMPL_FLOAT:
1640 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001641 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001642 break;
1643 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001644 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001645 break;
1646 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001647 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001648 break;
1649 case Instruction::CMPL_DOUBLE:
1650 case Instruction::CMPG_DOUBLE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001651 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08001652 break;
1653 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001654 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08001655 break;
1656 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001657 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001658 break;
1659 case Instruction::CMP_LONG:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001660 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08001661 break;
1662 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001663 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08001664 break;
1665 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001666 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001667 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001668 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001669 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001670 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001671 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001672 }
1673 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001674 }
jeffhaobdb76512011-09-07 11:43:16 -07001675 case Instruction::GOTO:
1676 case Instruction::GOTO_16:
1677 case Instruction::GOTO_32:
1678 /* no effect on or use of registers */
1679 break;
1680
1681 case Instruction::PACKED_SWITCH:
1682 case Instruction::SPARSE_SWITCH:
1683 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001684 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001685 break;
1686
Ian Rogersd81871c2011-10-03 13:57:23 -07001687 case Instruction::FILL_ARRAY_DATA: {
1688 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001689 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08001690 /* array_type can be null if the reg type is Zero */
1691 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001692 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001693 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001694 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001695 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1696 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001697 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001698 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1699 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001700 } else {
jeffhao457cc512012-02-02 16:55:13 -08001701 // Now verify if the element width in the table matches the element width declared in
1702 // the array
1703 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1704 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001705 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001706 } else {
1707 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1708 // Since we don't compress the data in Dex, expect to see equal width of data stored
1709 // in the table and expected from the array class.
1710 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001711 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1712 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001713 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001714 }
1715 }
jeffhaobdb76512011-09-07 11:43:16 -07001716 }
1717 }
1718 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001719 }
jeffhaobdb76512011-09-07 11:43:16 -07001720 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001721 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001722 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1723 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001724 bool mismatch = false;
1725 if (reg_type1.IsZero()) { // zero then integral or reference expected
1726 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1727 } else if (reg_type1.IsReferenceTypes()) { // both references?
1728 mismatch = !reg_type2.IsReferenceTypes();
1729 } else { // both integral?
1730 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1731 }
1732 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1734 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001735 }
1736 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001737 }
jeffhaobdb76512011-09-07 11:43:16 -07001738 case Instruction::IF_LT:
1739 case Instruction::IF_GE:
1740 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001741 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001742 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1743 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001744 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001745 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1746 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001747 }
1748 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001749 }
jeffhaobdb76512011-09-07 11:43:16 -07001750 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001751 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001752 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001753 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001754 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001755 }
jeffhaobdb76512011-09-07 11:43:16 -07001756 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001757 }
jeffhaobdb76512011-09-07 11:43:16 -07001758 case Instruction::IF_LTZ:
1759 case Instruction::IF_GEZ:
1760 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001761 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001762 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001763 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001764 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1765 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001766 }
jeffhaobdb76512011-09-07 11:43:16 -07001767 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001768 }
jeffhaobdb76512011-09-07 11:43:16 -07001769 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001770 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
1771 break;
jeffhaobdb76512011-09-07 11:43:16 -07001772 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001773 VerifyAGet(dec_insn, reg_types_.Byte(), true);
1774 break;
jeffhaobdb76512011-09-07 11:43:16 -07001775 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07001776 VerifyAGet(dec_insn, reg_types_.Char(), true);
1777 break;
jeffhaobdb76512011-09-07 11:43:16 -07001778 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001779 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001780 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001781 case Instruction::AGET:
1782 VerifyAGet(dec_insn, reg_types_.Integer(), true);
1783 break;
jeffhaobdb76512011-09-07 11:43:16 -07001784 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001785 VerifyAGet(dec_insn, reg_types_.Long(), true);
1786 break;
1787 case Instruction::AGET_OBJECT:
1788 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001789 break;
1790
Ian Rogersd81871c2011-10-03 13:57:23 -07001791 case Instruction::APUT_BOOLEAN:
1792 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
1793 break;
1794 case Instruction::APUT_BYTE:
1795 VerifyAPut(dec_insn, reg_types_.Byte(), true);
1796 break;
1797 case Instruction::APUT_CHAR:
1798 VerifyAPut(dec_insn, reg_types_.Char(), true);
1799 break;
1800 case Instruction::APUT_SHORT:
1801 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001802 break;
1803 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001804 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001805 break;
1806 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001807 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001808 break;
1809 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001810 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001811 break;
1812
jeffhaobdb76512011-09-07 11:43:16 -07001813 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001814 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001815 break;
jeffhaobdb76512011-09-07 11:43:16 -07001816 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001817 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001818 break;
jeffhaobdb76512011-09-07 11:43:16 -07001819 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001820 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001821 break;
jeffhaobdb76512011-09-07 11:43:16 -07001822 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001823 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001824 break;
1825 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001826 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001827 break;
1828 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001829 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001830 break;
1831 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001832 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001833 break;
jeffhaobdb76512011-09-07 11:43:16 -07001834
Ian Rogersd81871c2011-10-03 13:57:23 -07001835 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001836 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001837 break;
1838 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001839 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001840 break;
1841 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001842 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001843 break;
1844 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001845 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001846 break;
1847 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001848 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001849 break;
1850 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001851 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001852 break;
jeffhaobdb76512011-09-07 11:43:16 -07001853 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001854 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001855 break;
1856
jeffhaobdb76512011-09-07 11:43:16 -07001857 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001858 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001859 break;
jeffhaobdb76512011-09-07 11:43:16 -07001860 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001861 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001862 break;
jeffhaobdb76512011-09-07 11:43:16 -07001863 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001864 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001865 break;
jeffhaobdb76512011-09-07 11:43:16 -07001866 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001867 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001868 break;
1869 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001870 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001871 break;
1872 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001873 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001874 break;
1875 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001876 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001877 break;
1878
1879 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001880 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001881 break;
1882 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001883 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001884 break;
1885 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001886 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001887 break;
1888 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001889 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001890 break;
1891 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001892 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001893 break;
1894 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001895 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001896 break;
1897 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001898 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001899 break;
1900
1901 case Instruction::INVOKE_VIRTUAL:
1902 case Instruction::INVOKE_VIRTUAL_RANGE:
1903 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001904 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001905 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
1906 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
1907 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
1908 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartier66f19252012-09-18 08:57:04 -07001909 AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001910 const char* descriptor;
1911 if (called_method == NULL) {
1912 uint32_t method_idx = dec_insn.vB;
1913 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1914 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1915 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1916 } else {
1917 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07001918 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001919 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
1920 work_line_->SetResultRegisterType(return_type);
1921 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001922 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001923 }
jeffhaobdb76512011-09-07 11:43:16 -07001924 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001925 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001926 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartier66f19252012-09-18 08:57:04 -07001927 AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07001928 const char* return_type_descriptor;
1929 bool is_constructor;
1930 if (called_method == NULL) {
1931 uint32_t method_idx = dec_insn.vB;
1932 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1933 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
1934 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1935 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1936 } else {
1937 is_constructor = called_method->IsConstructor();
1938 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
1939 }
1940 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07001941 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001942 * Some additional checks when calling a constructor. We know from the invocation arg check
1943 * that the "this" argument is an instance of called_method->klass. Now we further restrict
1944 * that to require that called_method->klass is the same as this->klass or this->super,
1945 * allowing the latter only if the "this" argument is the same as the "this" argument to
1946 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07001947 */
jeffhaob57e9522012-04-26 18:08:21 -07001948 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1949 if (this_type.IsConflict()) // failure.
1950 break;
jeffhaobdb76512011-09-07 11:43:16 -07001951
jeffhaob57e9522012-04-26 18:08:21 -07001952 /* no null refs allowed (?) */
1953 if (this_type.IsZero()) {
1954 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
1955 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07001956 }
jeffhaob57e9522012-04-26 18:08:21 -07001957
1958 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07001959 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
1960 // TODO: re-enable constructor type verification
1961 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07001962 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07001963 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
1964 // break;
1965 // }
jeffhaob57e9522012-04-26 18:08:21 -07001966
1967 /* arg must be an uninitialized reference */
1968 if (!this_type.IsUninitializedTypes()) {
1969 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
1970 << this_type;
1971 break;
1972 }
1973
1974 /*
1975 * Replace the uninitialized reference with an initialized one. We need to do this for all
1976 * registers that have the same object instance in them, not just the "this" register.
1977 */
1978 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001979 }
Ian Rogers46685432012-06-03 22:26:43 -07001980 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001981 work_line_->SetResultRegisterType(return_type);
1982 just_set_result = true;
1983 break;
1984 }
1985 case Instruction::INVOKE_STATIC:
1986 case Instruction::INVOKE_STATIC_RANGE: {
1987 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartier66f19252012-09-18 08:57:04 -07001988 AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001989 const char* descriptor;
1990 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001991 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001992 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1993 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07001994 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001995 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001996 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001997 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001998 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07001999 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002000 just_set_result = true;
2001 }
2002 break;
jeffhaobdb76512011-09-07 11:43:16 -07002003 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002004 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002005 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartier66f19252012-09-18 08:57:04 -07002006 AbstractMethod* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002007 if (abs_method != NULL) {
2008 Class* called_interface = abs_method->GetDeclaringClass();
2009 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2010 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2011 << PrettyMethod(abs_method) << "'";
2012 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002013 }
Ian Rogers0d604842012-04-16 14:50:24 -07002014 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002015 /* Get the type of the "this" arg, which should either be a sub-interface of called
2016 * interface or Object (see comments in RegType::JoinClass).
2017 */
2018 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2019 if (this_type.IsZero()) {
2020 /* null pointer always passes (and always fails at runtime) */
2021 } else {
2022 if (this_type.IsUninitializedTypes()) {
2023 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2024 << this_type;
2025 break;
2026 }
2027 // In the past we have tried to assert that "called_interface" is assignable
2028 // from "this_type.GetClass()", however, as we do an imprecise Join
2029 // (RegType::JoinClass) we don't have full information on what interfaces are
2030 // implemented by "this_type". For example, two classes may implement the same
2031 // interfaces and have a common parent that doesn't implement the interface. The
2032 // join will set "this_type" to the parent class and a test that this implements
2033 // the interface will incorrectly fail.
2034 }
2035 /*
2036 * We don't have an object instance, so we can't find the concrete method. However, all of
2037 * the type information is in the abstract method, so we're good.
2038 */
2039 const char* descriptor;
2040 if (abs_method == NULL) {
2041 uint32_t method_idx = dec_insn.vB;
2042 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2043 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2044 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2045 } else {
2046 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2047 }
2048 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
2049 work_line_->SetResultRegisterType(return_type);
2050 work_line_->SetResultRegisterType(return_type);
2051 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002052 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002053 }
jeffhaobdb76512011-09-07 11:43:16 -07002054 case Instruction::NEG_INT:
2055 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002056 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002057 break;
2058 case Instruction::NEG_LONG:
2059 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002060 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002061 break;
2062 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002063 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002064 break;
2065 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002066 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002067 break;
2068 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002069 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002070 break;
2071 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002072 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002073 break;
2074 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002075 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002076 break;
2077 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002078 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002079 break;
2080 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002081 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002082 break;
2083 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002084 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002085 break;
2086 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002087 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002088 break;
2089 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002090 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002091 break;
2092 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002093 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002094 break;
2095 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002096 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002097 break;
2098 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002099 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002100 break;
2101 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002102 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002103 break;
2104 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002105 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002106 break;
2107 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002108 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002109 break;
2110 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002112 break;
2113
2114 case Instruction::ADD_INT:
2115 case Instruction::SUB_INT:
2116 case Instruction::MUL_INT:
2117 case Instruction::REM_INT:
2118 case Instruction::DIV_INT:
2119 case Instruction::SHL_INT:
2120 case Instruction::SHR_INT:
2121 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002122 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002123 break;
2124 case Instruction::AND_INT:
2125 case Instruction::OR_INT:
2126 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002127 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002128 break;
2129 case Instruction::ADD_LONG:
2130 case Instruction::SUB_LONG:
2131 case Instruction::MUL_LONG:
2132 case Instruction::DIV_LONG:
2133 case Instruction::REM_LONG:
2134 case Instruction::AND_LONG:
2135 case Instruction::OR_LONG:
2136 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002137 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002138 break;
2139 case Instruction::SHL_LONG:
2140 case Instruction::SHR_LONG:
2141 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002142 /* shift distance is Int, making these different from other binary operations */
2143 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002144 break;
2145 case Instruction::ADD_FLOAT:
2146 case Instruction::SUB_FLOAT:
2147 case Instruction::MUL_FLOAT:
2148 case Instruction::DIV_FLOAT:
2149 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002150 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002151 break;
2152 case Instruction::ADD_DOUBLE:
2153 case Instruction::SUB_DOUBLE:
2154 case Instruction::MUL_DOUBLE:
2155 case Instruction::DIV_DOUBLE:
2156 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002157 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002158 break;
2159 case Instruction::ADD_INT_2ADDR:
2160 case Instruction::SUB_INT_2ADDR:
2161 case Instruction::MUL_INT_2ADDR:
2162 case Instruction::REM_INT_2ADDR:
2163 case Instruction::SHL_INT_2ADDR:
2164 case Instruction::SHR_INT_2ADDR:
2165 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002166 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002167 break;
2168 case Instruction::AND_INT_2ADDR:
2169 case Instruction::OR_INT_2ADDR:
2170 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002171 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002172 break;
2173 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002174 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002175 break;
2176 case Instruction::ADD_LONG_2ADDR:
2177 case Instruction::SUB_LONG_2ADDR:
2178 case Instruction::MUL_LONG_2ADDR:
2179 case Instruction::DIV_LONG_2ADDR:
2180 case Instruction::REM_LONG_2ADDR:
2181 case Instruction::AND_LONG_2ADDR:
2182 case Instruction::OR_LONG_2ADDR:
2183 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002184 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002185 break;
2186 case Instruction::SHL_LONG_2ADDR:
2187 case Instruction::SHR_LONG_2ADDR:
2188 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002189 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002190 break;
2191 case Instruction::ADD_FLOAT_2ADDR:
2192 case Instruction::SUB_FLOAT_2ADDR:
2193 case Instruction::MUL_FLOAT_2ADDR:
2194 case Instruction::DIV_FLOAT_2ADDR:
2195 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002196 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002197 break;
2198 case Instruction::ADD_DOUBLE_2ADDR:
2199 case Instruction::SUB_DOUBLE_2ADDR:
2200 case Instruction::MUL_DOUBLE_2ADDR:
2201 case Instruction::DIV_DOUBLE_2ADDR:
2202 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002203 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002204 break;
2205 case Instruction::ADD_INT_LIT16:
2206 case Instruction::RSUB_INT:
2207 case Instruction::MUL_INT_LIT16:
2208 case Instruction::DIV_INT_LIT16:
2209 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002210 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002211 break;
2212 case Instruction::AND_INT_LIT16:
2213 case Instruction::OR_INT_LIT16:
2214 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002215 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002216 break;
2217 case Instruction::ADD_INT_LIT8:
2218 case Instruction::RSUB_INT_LIT8:
2219 case Instruction::MUL_INT_LIT8:
2220 case Instruction::DIV_INT_LIT8:
2221 case Instruction::REM_INT_LIT8:
2222 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002223 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002224 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002225 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002226 break;
2227 case Instruction::AND_INT_LIT8:
2228 case Instruction::OR_INT_LIT8:
2229 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002230 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002231 break;
2232
Ian Rogersd81871c2011-10-03 13:57:23 -07002233 /* These should never appear during verification. */
jeffhao9a4f0032012-08-30 16:17:40 -07002234 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002235 case Instruction::UNUSED_EE:
2236 case Instruction::UNUSED_EF:
2237 case Instruction::UNUSED_F2:
2238 case Instruction::UNUSED_F3:
2239 case Instruction::UNUSED_F4:
2240 case Instruction::UNUSED_F5:
2241 case Instruction::UNUSED_F6:
2242 case Instruction::UNUSED_F7:
2243 case Instruction::UNUSED_F8:
2244 case Instruction::UNUSED_F9:
2245 case Instruction::UNUSED_FA:
2246 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002247 case Instruction::UNUSED_F0:
2248 case Instruction::UNUSED_F1:
2249 case Instruction::UNUSED_E3:
2250 case Instruction::UNUSED_E8:
2251 case Instruction::UNUSED_E7:
2252 case Instruction::UNUSED_E4:
2253 case Instruction::UNUSED_E9:
2254 case Instruction::UNUSED_FC:
2255 case Instruction::UNUSED_E5:
2256 case Instruction::UNUSED_EA:
2257 case Instruction::UNUSED_FD:
2258 case Instruction::UNUSED_E6:
2259 case Instruction::UNUSED_EB:
2260 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002261 case Instruction::UNUSED_3E:
2262 case Instruction::UNUSED_3F:
2263 case Instruction::UNUSED_40:
2264 case Instruction::UNUSED_41:
2265 case Instruction::UNUSED_42:
2266 case Instruction::UNUSED_43:
2267 case Instruction::UNUSED_73:
2268 case Instruction::UNUSED_79:
2269 case Instruction::UNUSED_7A:
2270 case Instruction::UNUSED_EC:
2271 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002272 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002273 break;
2274
2275 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002276 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002277 * complain if an instruction is missing (which is desirable).
2278 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002279 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002280
Ian Rogersad0b3a32012-04-16 14:50:24 -07002281 if (have_pending_hard_failure_) {
2282 if (!Runtime::Current()->IsStarted()) {
jeffhaob57e9522012-04-26 18:08:21 -07002283 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002284 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002285 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002286 /* immediate failure, reject class */
2287 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2288 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002289 } else if (have_pending_runtime_throw_failure_) {
2290 /* slow path will throw, mark following code as unreachable */
2291 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002292 }
jeffhaobdb76512011-09-07 11:43:16 -07002293 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002294 * If we didn't just set the result register, clear it out. This ensures that you can only use
2295 * "move-result" immediately after the result is set. (We could check this statically, but it's
2296 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002297 */
2298 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002299 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002300 }
2301
jeffhaoa0a764a2011-09-16 10:43:38 -07002302 /* Handle "continue". Tag the next consecutive instruction. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002303 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers776ac1f2012-04-13 23:36:36 -07002304 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
Ian Rogersd81871c2011-10-03 13:57:23 -07002305 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
jeffhaod5347e02012-03-22 17:25:05 -07002306 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002307 return false;
2308 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002309 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2310 // next instruction isn't one.
jeffhaod5347e02012-03-22 17:25:05 -07002311 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002312 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002313 }
2314 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2315 if (next_line != NULL) {
2316 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2317 // needed.
2318 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002319 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002320 }
jeffhaobdb76512011-09-07 11:43:16 -07002321 } else {
2322 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002323 * We're not recording register data for the next instruction, so we don't know what the prior
2324 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002325 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002326 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002327 }
2328 }
2329
2330 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002331 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002332 *
2333 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002334 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002335 * somebody could get a reference field, check it for zero, and if the
2336 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002337 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002338 * that, and will reject the code.
2339 *
2340 * TODO: avoid re-fetching the branch target
2341 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002342 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002343 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002344 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002345 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002346 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002347 return false;
2348 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002349 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002350 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002351 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002352 }
jeffhaobdb76512011-09-07 11:43:16 -07002353 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002354 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002355 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002356 }
jeffhaobdb76512011-09-07 11:43:16 -07002357 }
2358
2359 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002360 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002361 *
2362 * We've already verified that the table is structurally sound, so we
2363 * just need to walk through and tag the targets.
2364 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002365 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002366 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2367 const uint16_t* switch_insns = insns + offset_to_switch;
2368 int switch_count = switch_insns[1];
2369 int offset_to_targets, targ;
2370
2371 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2372 /* 0 = sig, 1 = count, 2/3 = first key */
2373 offset_to_targets = 4;
2374 } else {
2375 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002376 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002377 offset_to_targets = 2 + 2 * switch_count;
2378 }
2379
2380 /* verify each switch target */
2381 for (targ = 0; targ < switch_count; targ++) {
2382 int offset;
2383 uint32_t abs_offset;
2384
2385 /* offsets are 32-bit, and only partly endian-swapped */
2386 offset = switch_insns[offset_to_targets + targ * 2] |
2387 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002388 abs_offset = work_insn_idx_ + offset;
2389 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002390 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002391 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002392 }
2393 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002394 return false;
2395 }
2396 }
2397
2398 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002399 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2400 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002401 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002402 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002403 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002404 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002405
Ian Rogers0571d352011-11-03 19:51:38 -07002406 for (; iterator.HasNext(); iterator.Next()) {
2407 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002408 within_catch_all = true;
2409 }
jeffhaobdb76512011-09-07 11:43:16 -07002410 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002411 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2412 * "work_regs", because at runtime the exception will be thrown before the instruction
2413 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002414 */
Ian Rogers0571d352011-11-03 19:51:38 -07002415 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002416 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002417 }
jeffhaobdb76512011-09-07 11:43:16 -07002418 }
2419
2420 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002421 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2422 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002423 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002424 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002425 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002426 * The state in work_line reflects the post-execution state. If the current instruction is a
2427 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002428 * it will do so before grabbing the lock).
2429 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002430 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002431 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002432 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002433 return false;
2434 }
2435 }
2436 }
2437
jeffhaod1f0fde2011-09-08 17:25:33 -07002438 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002439 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002440 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002441 return false;
2442 }
jeffhaobdb76512011-09-07 11:43:16 -07002443 }
2444
2445 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002446 * Update start_guess. Advance to the next instruction of that's
2447 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002448 * neither of those exists we're in a return or throw; leave start_guess
2449 * alone and let the caller sort it out.
2450 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002451 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002452 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002453 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002454 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002455 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002456 }
2457
Ian Rogersd81871c2011-10-03 13:57:23 -07002458 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2459 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002460
2461 return true;
2462}
2463
Ian Rogers776ac1f2012-04-13 23:36:36 -07002464const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002465 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002466 const RegType& referrer = GetDeclaringClass();
2467 Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002468 const RegType& result =
2469 klass != NULL ? reg_types_.FromClass(klass)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002470 : reg_types_.FromDescriptor(class_loader_, descriptor);
2471 if (result.IsConflict()) {
2472 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2473 << "' in " << referrer;
2474 return result;
2475 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002476 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002477 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002478 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002479 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002480 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002481 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002482 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002483 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002484 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002485 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002486}
2487
Ian Rogers776ac1f2012-04-13 23:36:36 -07002488const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002489 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002490 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002491 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002492 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2493 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002494 CatchHandlerIterator iterator(handlers_ptr);
2495 for (; iterator.HasNext(); iterator.Next()) {
2496 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2497 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002498 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07002499 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002500 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002501 if (common_super == NULL) {
2502 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2503 // as that is caught at runtime
2504 common_super = &exception;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002505 } else if (!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002506 // We don't know enough about the type and the common path merge will result in
2507 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002508 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002509 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002510 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002511 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002512 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002513 common_super = &common_super->Merge(exception, &reg_types_);
2514 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002515 }
2516 }
2517 }
2518 }
Ian Rogers0571d352011-11-03 19:51:38 -07002519 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002520 }
2521 }
2522 if (common_super == NULL) {
2523 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002524 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002525 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002526 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002527 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002528}
2529
Mathieu Chartier66f19252012-09-18 08:57:04 -07002530AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002531 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002532 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002533 if (klass_type.IsConflict()) {
2534 std::string append(" in attempt to access method ");
2535 append += dex_file_->GetMethodName(method_id);
2536 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002537 return NULL;
2538 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002539 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002540 return NULL; // Can't resolve Class so no more to do here
2541 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002542 Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002543 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier66f19252012-09-18 08:57:04 -07002544 AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002545 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002546 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002547 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002548
2549 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002550 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002551 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002552 res_method = klass->FindInterfaceMethod(name, signature);
2553 } else {
2554 res_method = klass->FindVirtualMethod(name, signature);
2555 }
2556 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002557 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002558 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002559 // If a virtual or interface method wasn't found with the expected type, look in
2560 // the direct methods. This can happen when the wrong invoke type is used or when
2561 // a class has changed, and will be flagged as an error in later checks.
2562 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2563 res_method = klass->FindDirectMethod(name, signature);
2564 }
2565 if (res_method == NULL) {
2566 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2567 << PrettyDescriptor(klass) << "." << name
2568 << " " << signature;
2569 return NULL;
2570 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002571 }
2572 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002573 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2574 // enforce them here.
2575 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002576 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2577 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002578 return NULL;
2579 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002580 // Disallow any calls to class initializers.
2581 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002582 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2583 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002584 return NULL;
2585 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002586 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002587 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002588 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002589 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002590 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002591 }
jeffhaode0d9c92012-02-27 13:58:13 -08002592 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2593 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002594 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2595 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002596 return NULL;
2597 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002598 // Check that interface methods match interface classes.
2599 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2600 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2601 << " is in an interface class " << PrettyClass(klass);
2602 return NULL;
2603 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2604 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2605 << " is in a non-interface class " << PrettyClass(klass);
2606 return NULL;
2607 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002608 // See if the method type implied by the invoke instruction matches the access flags for the
2609 // target method.
2610 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2611 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2612 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2613 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002614 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2615 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002616 return NULL;
2617 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002618 return res_method;
2619}
2620
Mathieu Chartier66f19252012-09-18 08:57:04 -07002621AbstractMethod* MethodVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
Ian Rogers46685432012-06-03 22:26:43 -07002622 MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002623 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2624 // we're making.
Mathieu Chartier66f19252012-09-18 08:57:04 -07002625 AbstractMethod* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002626 if (res_method == NULL) { // error or class is unresolved
2627 return NULL;
2628 }
2629
Ian Rogersd81871c2011-10-03 13:57:23 -07002630 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2631 // has a vtable entry for the target method.
2632 if (is_super) {
2633 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002634 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002635 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002636 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
2637 << PrettyMethod(method_idx_, *dex_file_)
2638 << " to super " << PrettyMethod(res_method);
2639 return NULL;
2640 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002641 Class* super_klass = super.GetClass();
2642 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002643 MethodHelper mh(res_method);
2644 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
2645 << PrettyMethod(method_idx_, *dex_file_)
2646 << " to super " << super
2647 << "." << mh.GetName()
2648 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002649 return NULL;
2650 }
2651 }
2652 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2653 // match the call to the signature. Also, we might might be calling through an abstract method
2654 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08002655 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002656 /* caught by static verifier */
2657 DCHECK(is_range || expected_args <= 5);
2658 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002659 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002660 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2661 return NULL;
2662 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002663
jeffhaobdb76512011-09-07 11:43:16 -07002664 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002665 * Check the "this" argument, which must be an instance of the class that declared the method.
2666 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2667 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002668 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002669 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002670 if (!res_method->IsStatic()) {
2671 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002672 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002673 return NULL;
2674 }
2675 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002676 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002677 return NULL;
2678 }
2679 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07002680 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
2681 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002682 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002683 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002684 return NULL;
2685 }
2686 }
2687 actual_args++;
2688 }
2689 /*
2690 * Process the target method's signature. This signature may or may not
2691 * have been verified, so we can't assume it's properly formed.
2692 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002693 MethodHelper mh(res_method);
2694 const DexFile::TypeList* params = mh.GetParameterTypeList();
2695 size_t params_size = params == NULL ? 0 : params->Size();
2696 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002697 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002698 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002699 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2700 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002701 return NULL;
2702 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002703 const char* descriptor =
2704 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2705 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002706 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002707 << " missing signature component";
2708 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002709 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002710 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002711 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002712 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002713 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002714 }
2715 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2716 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002717 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002719 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 return NULL;
2721 } else {
2722 return res_method;
2723 }
2724}
2725
Ian Rogers776ac1f2012-04-13 23:36:36 -07002726void MethodVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08002727 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002728 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002729 if (res_type.IsConflict()) { // bad class
2730 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002731 } else {
2732 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2733 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002735 } else if (!is_filled) {
2736 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002737 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002738 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002739 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002740 } else {
2741 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2742 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002743 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002744 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002745 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002746 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002747 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002748 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002749 return;
2750 }
2751 }
2752 // filled-array result goes into "result" register
2753 work_line_->SetResultRegisterType(res_type);
2754 }
2755 }
2756}
2757
Ian Rogers776ac1f2012-04-13 23:36:36 -07002758void MethodVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002759 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002760 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002761 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002762 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002763 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002764 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002765 if (array_type.IsZero()) {
2766 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2767 // instruction type. TODO: have a proper notion of bottom here.
2768 if (!is_primitive || insn_type.IsCategory1Types()) {
2769 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08002770 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002771 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002772 // Category 2
Elliott Hughesadb8c672012-03-06 16:49:32 -08002773 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
Ian Rogers89310de2012-02-01 13:47:30 -08002774 }
jeffhaofc3144e2012-02-01 17:21:15 -08002775 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002776 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002777 } else {
2778 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002779 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002780 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002781 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002782 << " source for aget-object";
2783 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002784 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002785 << " source for category 1 aget";
2786 } else if (is_primitive && !insn_type.Equals(component_type) &&
2787 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2788 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002789 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogersd81871c2011-10-03 13:57:23 -07002790 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002791 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002792 // Use knowledge of the field type which is stronger than the type inferred from the
2793 // instruction, which can't differentiate object types and ints from floats, longs from
2794 // doubles.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002795 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002796 }
2797 }
2798 }
2799}
2800
Ian Rogers776ac1f2012-04-13 23:36:36 -07002801void MethodVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002802 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002803 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002804 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002805 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002806 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002807 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002808 if (array_type.IsZero()) {
2809 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2810 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002811 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002812 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002813 } else {
2814 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002815 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002816 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002817 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002818 << " source for aput-object";
2819 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002820 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002821 << " source for category 1 aput";
2822 } else if (is_primitive && !insn_type.Equals(component_type) &&
2823 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2824 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002825 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002826 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002827 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002828 // The instruction agrees with the type of array, confirm the value to be stored does too
2829 // Note: we use the instruction type (rather than the component type) for aput-object as
2830 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08002831 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002832 }
2833 }
2834 }
2835}
2836
Ian Rogers776ac1f2012-04-13 23:36:36 -07002837Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002838 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2839 // Check access to class
2840 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002841 if (klass_type.IsConflict()) { // bad class
2842 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
2843 field_idx, dex_file_->GetFieldName(field_id),
2844 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002845 return NULL;
2846 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002847 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002848 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08002849 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002850 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2851 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002852 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002853 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
2854 << dex_file_->GetFieldName(field_id) << ") in "
2855 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002856 DCHECK(Thread::Current()->IsExceptionPending());
2857 Thread::Current()->ClearException();
2858 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002859 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2860 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002861 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002862 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002863 return NULL;
2864 } else if (!field->IsStatic()) {
2865 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
2866 return NULL;
2867 } else {
2868 return field;
2869 }
2870}
2871
Ian Rogers776ac1f2012-04-13 23:36:36 -07002872Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002873 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2874 // Check access to class
2875 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002876 if (klass_type.IsConflict()) {
2877 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
2878 field_idx, dex_file_->GetFieldName(field_id),
2879 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002880 return NULL;
2881 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002882 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002883 return NULL; // Can't resolve Class so no more to do here
2884 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002885 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2886 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002887 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002888 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
2889 << dex_file_->GetFieldName(field_id) << ") in "
2890 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002891 DCHECK(Thread::Current()->IsExceptionPending());
2892 Thread::Current()->ClearException();
2893 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002894 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2895 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002896 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002897 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002898 return NULL;
2899 } else if (field->IsStatic()) {
2900 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
2901 << " to not be static";
2902 return NULL;
2903 } else if (obj_type.IsZero()) {
2904 // Cannot infer and check type, however, access will cause null pointer exception
2905 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07002906 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002907 const RegType& field_klass = reg_types_.FromClass(field->GetDeclaringClass());
2908 if (obj_type.IsUninitializedTypes() &&
2909 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
2910 !field_klass.Equals(GetDeclaringClass()))) {
2911 // Field accesses through uninitialized references are only allowable for constructors where
2912 // the field is declared in this class
2913 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
2914 << " of a not fully initialized object within the context of "
2915 << PrettyMethod(method_idx_, *dex_file_);
2916 return NULL;
2917 } else if (!field_klass.IsAssignableFrom(obj_type)) {
2918 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
2919 // of C1. For resolution to occur the declared class of the field must be compatible with
2920 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
2921 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
2922 << " from object of type " << obj_type;
2923 return NULL;
2924 } else {
2925 return field;
2926 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002927 }
2928}
2929
Ian Rogers776ac1f2012-04-13 23:36:36 -07002930void MethodVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002931 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002932 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002933 Field* field;
2934 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002935 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002936 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002937 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07002938 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002939 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002940 const char* descriptor;
Ian Rogers365c1022012-06-22 15:05:28 -07002941 ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002942 if (field != NULL) {
2943 descriptor = FieldHelper(field).GetTypeDescriptor();
2944 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07002945 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002946 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2947 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2948 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07002949 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002950 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
2951 if (is_primitive) {
2952 if (field_type.Equals(insn_type) ||
2953 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
2954 (field_type.IsDouble() && insn_type.IsLongTypes())) {
2955 // expected that read is of the correct primitive type or that int reads are reading
2956 // floats or long reads are reading doubles
2957 } else {
2958 // This is a global failure rather than a class change failure as the instructions and
2959 // the descriptors for the type should have been consistent within the same file at
2960 // compile time
2961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
2962 << " to be of type '" << insn_type
2963 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002964 return;
2965 }
2966 } else {
2967 if (!insn_type.IsAssignableFrom(field_type)) {
2968 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
2969 << " to be compatible with type '" << insn_type
2970 << "' but found type '" << field_type
2971 << "' in get-object";
2972 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Conflict());
2973 return;
2974 }
2975 }
2976 work_line_->SetRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002977}
2978
Ian Rogers776ac1f2012-04-13 23:36:36 -07002979void MethodVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002980 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002981 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002982 Field* field;
2983 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07002984 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002985 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002986 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07002987 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002988 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002989 const char* descriptor;
Ian Rogers365c1022012-06-22 15:05:28 -07002990 ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002991 if (field != NULL) {
2992 descriptor = FieldHelper(field).GetTypeDescriptor();
2993 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07002994 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002995 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2996 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2997 loader = class_loader_;
2998 }
2999 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
3000 if (field != NULL) {
3001 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3002 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3003 << " from other class " << GetDeclaringClass();
3004 return;
3005 }
3006 }
3007 if (is_primitive) {
3008 // Primitive field assignability rules are weaker than regular assignability rules
3009 bool instruction_compatible;
3010 bool value_compatible;
3011 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
3012 if (field_type.IsIntegralTypes()) {
3013 instruction_compatible = insn_type.IsIntegralTypes();
3014 value_compatible = value_type.IsIntegralTypes();
3015 } else if (field_type.IsFloat()) {
3016 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3017 value_compatible = value_type.IsFloatTypes();
3018 } else if (field_type.IsLong()) {
3019 instruction_compatible = insn_type.IsLong();
3020 value_compatible = value_type.IsLongTypes();
3021 } else if (field_type.IsDouble()) {
3022 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3023 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003024 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003025 instruction_compatible = false; // reference field with primitive store
3026 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003027 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003028 if (!instruction_compatible) {
3029 // This is a global failure rather than a class change failure as the instructions and
3030 // the descriptors for the type should have been consistent within the same file at
3031 // compile time
3032 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3033 << " to be of type '" << insn_type
3034 << "' but found type '" << field_type
3035 << "' in put";
3036 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003037 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003038 if (!value_compatible) {
3039 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
3040 << " of type " << value_type
3041 << " but expected " << field_type
3042 << " for store to " << PrettyField(field) << " in put";
3043 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003044 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003045 } else {
3046 if (!insn_type.IsAssignableFrom(field_type)) {
3047 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3048 << " to be compatible with type '" << insn_type
3049 << "' but found type '" << field_type
3050 << "' in put-object";
3051 return;
3052 }
3053 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003054 }
3055}
3056
Ian Rogers776ac1f2012-04-13 23:36:36 -07003057bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003058 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003059 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003060 return false;
3061 }
3062 return true;
3063}
3064
Ian Rogers776ac1f2012-04-13 23:36:36 -07003065bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003066 bool changed = true;
3067 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3068 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003069 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003070 * We haven't processed this instruction before, and we haven't touched the registers here, so
3071 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3072 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003073 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003074 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003075 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003076 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3077 if (gDebugVerify) {
3078 copy->CopyFromLine(target_line);
3079 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003080 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003081 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003082 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003083 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003084 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003085 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003086 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3087 << *copy.get() << " MERGE\n"
3088 << *merge_line << " ==\n"
3089 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003090 }
3091 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003092 if (changed) {
3093 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003094 }
3095 return true;
3096}
3097
Ian Rogers776ac1f2012-04-13 23:36:36 -07003098InsnFlags* MethodVerifier::CurrentInsnFlags() {
3099 return &insn_flags_[work_insn_idx_];
3100}
3101
Ian Rogersad0b3a32012-04-16 14:50:24 -07003102const RegType& MethodVerifier::GetMethodReturnType() {
3103 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx_);
3104 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3105 uint16_t return_type_idx = proto_id.return_type_idx_;
3106 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
3107 return reg_types_.FromDescriptor(class_loader_, descriptor);
3108}
3109
3110const RegType& MethodVerifier::GetDeclaringClass() {
3111 if (foo_method_ != NULL) {
3112 return reg_types_.FromClass(foo_method_->GetDeclaringClass());
3113 } else {
3114 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx_);
3115 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
3116 return reg_types_.FromDescriptor(class_loader_, descriptor);
3117 }
3118}
3119
Ian Rogers776ac1f2012-04-13 23:36:36 -07003120void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003121 size_t* log2_max_gc_pc) {
3122 size_t local_gc_points = 0;
3123 size_t max_insn = 0;
3124 size_t max_ref_reg = -1;
3125 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3126 if (insn_flags_[i].IsGcPoint()) {
3127 local_gc_points++;
3128 max_insn = i;
3129 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003130 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003131 }
3132 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003133 *gc_points = local_gc_points;
3134 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3135 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003136 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003137 i++;
3138 }
3139 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003140}
3141
Ian Rogers776ac1f2012-04-13 23:36:36 -07003142const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003143 size_t num_entries, ref_bitmap_bits, pc_bits;
3144 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3145 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003146 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003147 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003148 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003149 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003150 return NULL;
3151 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003152 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3153 // There are 2 bytes to encode the number of entries
3154 if (num_entries >= 65536) {
3155 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003156 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003157 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003158 return NULL;
3159 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003160 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003161 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003162 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003163 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003164 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003165 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003166 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003167 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003168 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003169 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003170 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003171 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3172 return NULL;
3173 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003174 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003175 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003176 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003177 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003178 return NULL;
3179 }
3180 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003181 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3182 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003183 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003184 table->push_back(num_entries & 0xFF);
3185 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003186 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003187 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3188 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003189 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003190 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003191 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003192 }
3193 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003194 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003195 }
3196 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003197 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003198 return table;
3199}
jeffhaoa0a764a2011-09-16 10:43:38 -07003200
Ian Rogers776ac1f2012-04-13 23:36:36 -07003201void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003202 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3203 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003204 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003205 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003206 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003207 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3208 if (insn_flags_[i].IsGcPoint()) {
3209 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003210 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003211 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3212 map_index++;
3213 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003214 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003215 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003216 CHECK_LT(j / 8, map.RegWidth());
3217 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3218 } else if ((j / 8) < map.RegWidth()) {
3219 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3220 } else {
3221 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3222 }
3223 }
3224 } else {
3225 CHECK(reg_bitmap == NULL);
3226 }
3227 }
3228}
jeffhaoa0a764a2011-09-16 10:43:38 -07003229
Ian Rogers0c7abda2012-09-19 13:33:42 -07003230void MethodVerifier::SetDexGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003231 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003232 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003233 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
3234 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003235 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003236 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003237 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003238 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003239 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003240 CHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003241}
3242
Ian Rogers0c7abda2012-09-19 13:33:42 -07003243const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(Compiler::MethodReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003244 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003245 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
3246 if (it == dex_gc_maps_->end()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003247 return NULL;
3248 }
3249 CHECK(it->second != NULL);
3250 return it->second;
3251}
3252
Ian Rogers0c7abda2012-09-19 13:33:42 -07003253Mutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
3254MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003255
3256Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
3257MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
3258
3259#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
3260Mutex* MethodVerifier::inferred_reg_category_maps_lock_ = NULL;
3261MethodVerifier::InferredRegCategoryMapTable* MethodVerifier::inferred_reg_category_maps_ = NULL;
3262#endif
3263
3264void MethodVerifier::Init() {
Ian Rogers0c7abda2012-09-19 13:33:42 -07003265 dex_gc_maps_lock_ = new Mutex("verifier GC maps lock");
Ian Rogers50b35e22012-10-04 10:09:15 -07003266 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003267 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003268 MutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003269 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003270 }
3271
3272 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
3273 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003274 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003275 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
3276 }
3277
3278#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
3279 inferred_reg_category_maps_lock_ = new Mutex("verifier GC maps lock");
3280 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003281 MutexLock mu(self, *inferred_reg_category_maps_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003282 inferred_reg_category_maps_ = new MethodVerifier::InferredRegCategoryMapTable;
3283 }
3284#endif
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003285}
3286
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003287void MethodVerifier::Shutdown() {
Ian Rogers50b35e22012-10-04 10:09:15 -07003288 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003289 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003290 MutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003291 STLDeleteValues(dex_gc_maps_);
3292 delete dex_gc_maps_;
3293 dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003294 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003295 delete dex_gc_maps_lock_;
3296 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003297
3298 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003299 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003300 delete rejected_classes_;
3301 rejected_classes_ = NULL;
3302 }
3303 delete rejected_classes_lock_;
3304 rejected_classes_lock_ = NULL;
3305
3306#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
3307 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003308 MutexLock mu(self, *inferred_reg_category_maps_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003309 STLDeleteValues(inferred_reg_category_maps_);
3310 delete inferred_reg_category_maps_;
3311 inferred_reg_category_maps_ = NULL;
3312 }
3313 delete inferred_reg_category_maps_lock_;
3314 inferred_reg_category_maps_lock_ = NULL;
3315#endif
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003316}
jeffhaod1224c72012-02-29 13:43:08 -08003317
Ian Rogers776ac1f2012-04-13 23:36:36 -07003318void MethodVerifier::AddRejectedClass(Compiler::ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003319 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003320 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003321 rejected_classes_->insert(ref);
3322 }
jeffhaod1224c72012-02-29 13:43:08 -08003323 CHECK(IsClassRejected(ref));
3324}
3325
Ian Rogers776ac1f2012-04-13 23:36:36 -07003326bool MethodVerifier::IsClassRejected(Compiler::ClassReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003327 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003328 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08003329}
3330
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -07003331#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
TDYa12789f96052012-07-12 20:49:53 -07003332const greenland::InferredRegCategoryMap* MethodVerifier::GenerateInferredRegCategoryMap() {
Logan Chienfca7e872011-12-20 20:08:22 +08003333 uint32_t insns_size = code_item_->insns_size_in_code_units_;
3334 uint16_t regs_size = code_item_->registers_size_;
3335
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003336 UniquePtr<InferredRegCategoryMap> table(new InferredRegCategoryMap(insns_size, regs_size));
Logan Chienfca7e872011-12-20 20:08:22 +08003337
3338 for (size_t i = 0; i < insns_size; ++i) {
3339 if (RegisterLine* line = reg_table_.GetLine(i)) {
TDYa127526643e2012-05-26 01:01:48 -07003340 const Instruction* inst = Instruction::At(code_item_->insns_ + i);
3341
3342 // GC points
3343 if (inst->IsBranch() || inst->IsInvoke()) {
3344 for (size_t r = 0; r < regs_size; ++r) {
3345 const RegType &rt = line->GetRegisterType(r);
3346 if (rt.IsNonZeroReferenceTypes()) {
3347 table->SetRegCanBeObject(r);
3348 }
TDYa127b2eb5c12012-05-24 15:52:10 -07003349 }
3350 }
3351
TDYa127526643e2012-05-26 01:01:48 -07003352 /* We only use InferredRegCategoryMap in one case */
3353 if (inst->IsBranch()) {
TDYa127b2eb5c12012-05-24 15:52:10 -07003354 for (size_t r = 0; r < regs_size; ++r) {
3355 const RegType &rt = line->GetRegisterType(r);
3356
3357 if (rt.IsZero()) {
TDYa12789f96052012-07-12 20:49:53 -07003358 table->SetRegCategory(i, r, greenland::kRegZero);
TDYa127b2eb5c12012-05-24 15:52:10 -07003359 } else if (rt.IsCategory1Types()) {
TDYa12789f96052012-07-12 20:49:53 -07003360 table->SetRegCategory(i, r, greenland::kRegCat1nr);
TDYa127b2eb5c12012-05-24 15:52:10 -07003361 } else if (rt.IsCategory2Types()) {
TDYa12789f96052012-07-12 20:49:53 -07003362 table->SetRegCategory(i, r, greenland::kRegCat2);
TDYa127b2eb5c12012-05-24 15:52:10 -07003363 } else if (rt.IsReferenceTypes()) {
TDYa12789f96052012-07-12 20:49:53 -07003364 table->SetRegCategory(i, r, greenland::kRegObject);
TDYa127b2eb5c12012-05-24 15:52:10 -07003365 } else {
TDYa12789f96052012-07-12 20:49:53 -07003366 table->SetRegCategory(i, r, greenland::kRegUnknown);
TDYa127b2eb5c12012-05-24 15:52:10 -07003367 }
Logan Chienfca7e872011-12-20 20:08:22 +08003368 }
3369 }
3370 }
3371 }
3372
3373 return table.release();
3374}
Logan Chiendd361c92012-04-10 23:40:37 +08003375
Ian Rogers776ac1f2012-04-13 23:36:36 -07003376void MethodVerifier::SetInferredRegCategoryMap(Compiler::MethodReference ref,
3377 const InferredRegCategoryMap& inferred_reg_category_map) {
Shih-wei Liaocd05a622012-08-15 00:02:05 -07003378 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003379 MutexLock mu(Thread::Current(), *inferred_reg_category_maps_lock_);
Shih-wei Liaocd05a622012-08-15 00:02:05 -07003380 InferredRegCategoryMapTable::iterator it = inferred_reg_category_maps_->find(ref);
3381 if (it == inferred_reg_category_maps_->end()) {
3382 inferred_reg_category_maps_->Put(ref, &inferred_reg_category_map);
3383 } else {
3384 CHECK(*(it->second) == inferred_reg_category_map);
3385 delete &inferred_reg_category_map;
3386 }
Logan Chiendd361c92012-04-10 23:40:37 +08003387 }
Logan Chiendd361c92012-04-10 23:40:37 +08003388 CHECK(GetInferredRegCategoryMap(ref) != NULL);
3389}
3390
TDYa12789f96052012-07-12 20:49:53 -07003391const greenland::InferredRegCategoryMap*
Ian Rogers776ac1f2012-04-13 23:36:36 -07003392MethodVerifier::GetInferredRegCategoryMap(Compiler::MethodReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003393 MutexLock mu(Thread::Current(), *inferred_reg_category_maps_lock_);
Logan Chiendd361c92012-04-10 23:40:37 +08003394
3395 InferredRegCategoryMapTable::const_iterator it =
3396 inferred_reg_category_maps_->find(ref);
3397
3398 if (it == inferred_reg_category_maps_->end()) {
3399 return NULL;
3400 }
3401 CHECK(it->second != NULL);
3402 return it->second;
3403}
Logan Chienfca7e872011-12-20 20:08:22 +08003404#endif
3405
Ian Rogersd81871c2011-10-03 13:57:23 -07003406} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003407} // namespace art