blob: bd77a3cbbcad4a23c5a7653bd58a8d51ee90f15a [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
buzbeec531cef2012-10-18 07:09:20 -070035#if defined(ART_USE_LLVM_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
buzbeec531cef2012-10-18 07:09:20 -0700965#if !defined(ART_USE_LLVM_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
buzbeec531cef2012-10-18 07:09:20 -07001015#if !defined(ART_USE_LLVM_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
buzbeec531cef2012-10-18 07:09:20 -07001029#else // defined(ART_USE_LLVM_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 Rogersb4903572012-10-11 11:52:56 -07001057 reg_types_.Dump(os);
1058 os << "Dumping instructions and register lines:\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001059 const Instruction* inst = Instruction::At(code_item_->insns_);
1060 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1061 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Elliott Hughesaa6e1cd2012-01-18 19:26:06 -08001062 os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
Elliott Hughesc073b072012-05-24 19:29:17 -07001063 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001064 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1065 if (reg_line != NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001066 os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001067 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001068 inst = inst->Next();
1069 }
jeffhaobdb76512011-09-07 11:43:16 -07001070}
1071
Ian Rogersd81871c2011-10-03 13:57:23 -07001072static bool IsPrimitiveDescriptor(char descriptor) {
1073 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001074 case 'I':
1075 case 'C':
1076 case 'S':
1077 case 'B':
1078 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001079 case 'F':
1080 case 'D':
1081 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001082 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001083 default:
1084 return false;
1085 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001086}
1087
Ian Rogers776ac1f2012-04-13 23:36:36 -07001088bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001089 RegisterLine* reg_line = reg_table_.GetLine(0);
1090 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1091 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001092
Ian Rogersd81871c2011-10-03 13:57:23 -07001093 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1094 //Include the "this" pointer.
1095 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001096 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001097 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1098 // argument as uninitialized. This restricts field access until the superclass constructor is
1099 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001100 const RegType& declaring_class = GetDeclaringClass();
1101 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 reg_line->SetRegisterType(arg_start + cur_arg,
1103 reg_types_.UninitializedThisArgument(declaring_class));
1104 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001105 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001106 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001107 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001108 }
1109
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001110 const DexFile::ProtoId& proto_id =
Ian Rogersad0b3a32012-04-16 14:50:24 -07001111 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001112 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001113
1114 for (; iterator.HasNext(); iterator.Next()) {
1115 const char* descriptor = iterator.GetDescriptor();
1116 if (descriptor == NULL) {
1117 LOG(FATAL) << "Null descriptor";
1118 }
1119 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001120 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1121 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001122 return false;
1123 }
1124 switch (descriptor[0]) {
1125 case 'L':
1126 case '[':
1127 // We assume that reference arguments are initialized. The only way it could be otherwise
1128 // (assuming the caller was verified) is if the current method is <init>, but in that case
1129 // it's effectively considered initialized the instant we reach here (in the sense that we
1130 // can return without doing anything or call virtual methods).
1131 {
Ian Rogersb4903572012-10-11 11:52:56 -07001132 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001133 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001134 }
1135 break;
1136 case 'Z':
1137 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1138 break;
1139 case 'C':
1140 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1141 break;
1142 case 'B':
1143 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1144 break;
1145 case 'I':
1146 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1147 break;
1148 case 'S':
1149 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1150 break;
1151 case 'F':
1152 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1153 break;
1154 case 'J':
1155 case 'D': {
1156 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1157 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1158 cur_arg++;
1159 break;
1160 }
1161 default:
jeffhaod5347e02012-03-22 17:25:05 -07001162 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001163 return false;
1164 }
1165 cur_arg++;
1166 }
1167 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001168 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001169 return false;
1170 }
1171 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1172 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1173 // format. Only major difference from the method argument format is that 'V' is supported.
1174 bool result;
1175 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1176 result = descriptor[1] == '\0';
1177 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1178 size_t i = 0;
1179 do {
1180 i++;
1181 } while (descriptor[i] == '['); // process leading [
1182 if (descriptor[i] == 'L') { // object array
1183 do {
1184 i++; // find closing ;
1185 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1186 result = descriptor[i] == ';';
1187 } else { // primitive array
1188 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1189 }
1190 } else if (descriptor[0] == 'L') {
1191 // could be more thorough here, but shouldn't be required
1192 size_t i = 0;
1193 do {
1194 i++;
1195 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1196 result = descriptor[i] == ';';
1197 } else {
1198 result = false;
1199 }
1200 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001201 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1202 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001203 }
1204 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001205}
1206
Ian Rogers776ac1f2012-04-13 23:36:36 -07001207bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001208 const uint16_t* insns = code_item_->insns_;
1209 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001210
jeffhaobdb76512011-09-07 11:43:16 -07001211 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001212 insn_flags_[0].SetChanged();
1213 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001214
jeffhaobdb76512011-09-07 11:43:16 -07001215 /* Continue until no instructions are marked "changed". */
1216 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001217 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1218 uint32_t insn_idx = start_guess;
1219 for (; insn_idx < insns_size; insn_idx++) {
1220 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001221 break;
1222 }
jeffhaobdb76512011-09-07 11:43:16 -07001223 if (insn_idx == insns_size) {
1224 if (start_guess != 0) {
1225 /* try again, starting from the top */
1226 start_guess = 0;
1227 continue;
1228 } else {
1229 /* all flags are clear */
1230 break;
1231 }
1232 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001233 // We carry the working set of registers from instruction to instruction. If this address can
1234 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1235 // "changed" flags, we need to load the set of registers from the table.
1236 // Because we always prefer to continue on to the next instruction, we should never have a
1237 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1238 // target.
1239 work_insn_idx_ = insn_idx;
1240 if (insn_flags_[insn_idx].IsBranchTarget()) {
1241 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001242 } else {
1243#ifndef NDEBUG
1244 /*
1245 * Sanity check: retrieve the stored register line (assuming
1246 * a full table) and make sure it actually matches.
1247 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001248 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1249 if (register_line != NULL) {
1250 if (work_line_->CompareLine(register_line) != 0) {
1251 Dump(std::cout);
1252 std::cout << info_messages_.str();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001253 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001254 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1255 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001256 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001257 }
jeffhaobdb76512011-09-07 11:43:16 -07001258 }
1259#endif
1260 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001261 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001262 std::string prepend(PrettyMethod(method_idx_, *dex_file_));
1263 prepend += " failed to verify: ";
1264 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001265 return false;
1266 }
jeffhaobdb76512011-09-07 11:43:16 -07001267 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001268 insn_flags_[insn_idx].SetVisited();
1269 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001270 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001271
Ian Rogers1c849e52012-06-28 14:00:33 -07001272 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001273 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001274 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001275 * (besides the wasted space), but it indicates a flaw somewhere
1276 * down the line, possibly in the verifier.
1277 *
1278 * If we've substituted "always throw" instructions into the stream,
1279 * we are almost certainly going to have some dead code.
1280 */
1281 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001282 uint32_t insn_idx = 0;
1283 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001284 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001285 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001286 * may or may not be preceded by a padding NOP (for alignment).
1287 */
1288 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1289 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1290 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001291 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001292 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1293 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1294 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001295 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001296 }
1297
Ian Rogersd81871c2011-10-03 13:57:23 -07001298 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001299 if (dead_start < 0)
1300 dead_start = insn_idx;
1301 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001302 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001303 dead_start = -1;
1304 }
1305 }
1306 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001307 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001308 }
1309 }
jeffhaobdb76512011-09-07 11:43:16 -07001310 return true;
1311}
1312
Ian Rogers776ac1f2012-04-13 23:36:36 -07001313bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001314#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001315 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001316 gDvm.verifierStats.instrsReexamined++;
1317 } else {
1318 gDvm.verifierStats.instrsExamined++;
1319 }
1320#endif
1321
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001322 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1323 // We want the state _before_ the instruction, for the case where the dex pc we're
1324 // interested in is itself a monitor-enter instruction (which is a likely place
1325 // for a thread to be suspended).
1326 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
1327 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1328 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1329 }
1330 }
1331
jeffhaobdb76512011-09-07 11:43:16 -07001332 /*
1333 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001334 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001335 * control to another statement:
1336 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001337 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001338 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001339 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001340 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001341 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001342 * throw an exception that is handled by an encompassing "try"
1343 * block.
1344 *
1345 * We can also return, in which case there is no successor instruction
1346 * from this point.
1347 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001348 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001349 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001350 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1351 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001352 DecodedInstruction dec_insn(inst);
Ian Rogersa75a0132012-09-28 11:41:42 -07001353 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001354
jeffhaobdb76512011-09-07 11:43:16 -07001355 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001356 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001357 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001358 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001359 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1360 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001361 }
jeffhaobdb76512011-09-07 11:43:16 -07001362
1363 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001364 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001365 * can throw an exception, we will copy/merge this into the "catch"
1366 * address rather than work_line, because we don't want the result
1367 * from the "successful" code path (e.g. a check-cast that "improves"
1368 * a type) to be visible to the exception handler.
1369 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001370 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001371 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001372 } else {
1373#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001374 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001375#endif
1376 }
1377
Elliott Hughesadb8c672012-03-06 16:49:32 -08001378 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07001379 case Instruction::NOP:
1380 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001381 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001382 * a signature that looks like a NOP; if we see one of these in
1383 * the course of executing code then we have a problem.
1384 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001385 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001386 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001387 }
1388 break;
1389
1390 case Instruction::MOVE:
1391 case Instruction::MOVE_FROM16:
1392 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001393 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001394 break;
1395 case Instruction::MOVE_WIDE:
1396 case Instruction::MOVE_WIDE_FROM16:
1397 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001398 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07001399 break;
1400 case Instruction::MOVE_OBJECT:
1401 case Instruction::MOVE_OBJECT_FROM16:
1402 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001403 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001404 break;
1405
1406 /*
1407 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001408 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001409 * might want to hold the result in an actual CPU register, so the
1410 * Dalvik spec requires that these only appear immediately after an
1411 * invoke or filled-new-array.
1412 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001413 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001414 * redundant with the reset done below, but it can make the debug info
1415 * easier to read in some cases.)
1416 */
1417 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001418 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07001419 break;
1420 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001421 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001422 break;
1423 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001424 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07001425 break;
1426
Ian Rogersd81871c2011-10-03 13:57:23 -07001427 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001428 /*
jeffhao60f83e32012-02-13 17:16:30 -08001429 * This statement can only appear as the first instruction in an exception handler. We verify
1430 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001431 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001432 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08001433 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001434 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001435 }
jeffhaobdb76512011-09-07 11:43:16 -07001436 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001437 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1438 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001439 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001440 }
jeffhaobdb76512011-09-07 11:43:16 -07001441 }
1442 break;
1443 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001444 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001445 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001446 const RegType& return_type = GetMethodReturnType();
1447 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001448 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001449 } else {
1450 // Compilers may generate synthetic functions that write byte values into boolean fields.
1451 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001452 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001453 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1454 ((return_type.IsBoolean() || return_type.IsByte() ||
1455 return_type.IsShort() || return_type.IsChar()) &&
1456 src_type.IsInteger()));
1457 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001458 bool success =
1459 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
1460 if (!success) {
1461 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001462 }
jeffhaobdb76512011-09-07 11:43:16 -07001463 }
1464 }
1465 break;
1466 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001467 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001468 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001469 const RegType& return_type = GetMethodReturnType();
1470 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001471 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001472 } else {
1473 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001474 bool success = work_line_->VerifyRegisterType(dec_insn.vA, return_type);
1475 if (!success) {
1476 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001477 }
jeffhaobdb76512011-09-07 11:43:16 -07001478 }
1479 }
1480 break;
1481 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001482 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001483 const RegType& return_type = GetMethodReturnType();
1484 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001485 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001486 } else {
1487 /* return_type is the *expected* return type, not register value */
1488 DCHECK(!return_type.IsZero());
1489 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08001490 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07001491 // Disallow returning uninitialized values and verify that the reference in vAA is an
1492 // instance of the "return_type"
1493 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001494 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001495 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001496 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1497 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001498 }
1499 }
1500 }
1501 break;
1502
1503 case Instruction::CONST_4:
1504 case Instruction::CONST_16:
1505 case Instruction::CONST:
1506 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001507 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const((int32_t) dec_insn.vB));
jeffhaobdb76512011-09-07 11:43:16 -07001508 break;
1509 case Instruction::CONST_HIGH16:
1510 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001511 work_line_->SetRegisterType(dec_insn.vA,
1512 reg_types_.FromCat1Const((int32_t) dec_insn.vB << 16));
jeffhaobdb76512011-09-07 11:43:16 -07001513 break;
1514 case Instruction::CONST_WIDE_16:
1515 case Instruction::CONST_WIDE_32:
1516 case Instruction::CONST_WIDE:
1517 case Instruction::CONST_WIDE_HIGH16:
1518 /* could be long or double; resolved upon use */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001519 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07001520 break;
1521 case Instruction::CONST_STRING:
1522 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001523 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001524 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001525 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001526 // Get type from instruction if unresolved then we need an access check
1527 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08001528 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001529 // Register holds class, ie its type is class, on error it will hold Conflict.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001530 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogersb4903572012-10-11 11:52:56 -07001531 res_type.IsConflict() ? res_type
1532 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001533 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001534 }
jeffhaobdb76512011-09-07 11:43:16 -07001535 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001536 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001537 break;
1538 case Instruction::MONITOR_EXIT:
1539 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001540 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001541 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001542 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001543 * to the need to handle asynchronous exceptions, a now-deprecated
1544 * feature that Dalvik doesn't support.)
1545 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001546 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001547 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001548 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001549 * structured locking checks are working, the former would have
1550 * failed on the -enter instruction, and the latter is impossible.
1551 *
1552 * This is fortunate, because issue 3221411 prevents us from
1553 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001554 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001555 * some catch blocks (which will show up as "dead" code when
1556 * we skip them here); if we can't, then the code path could be
1557 * "live" so we still need to check it.
1558 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001559 opcode_flags &= ~Instruction::kThrow;
1560 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001561 break;
1562
Ian Rogers28ad40d2011-10-27 15:19:26 -07001563 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001564 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001565 /*
1566 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1567 * could be a "upcast" -- not expected, so we don't try to address it.)
1568 *
1569 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001570 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001571 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001572 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001573 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001574 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001575 if (res_type.IsConflict()) {
1576 DCHECK_NE(failures_.size(), 0U);
1577 if (!is_checkcast) {
1578 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
1579 }
1580 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001581 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001582 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1583 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001584 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001585 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001586 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001587 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001588 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07001589 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001590 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001591 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001592 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001593 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001594 }
jeffhaobdb76512011-09-07 11:43:16 -07001595 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001596 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001597 }
1598 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001599 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001600 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001601 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001602 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001603 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001604 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001605 }
1606 }
1607 break;
1608 }
1609 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001610 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001611 if (res_type.IsConflict()) {
1612 DCHECK_NE(failures_.size(), 0U);
1613 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001614 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001615 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1616 // can't create an instance of an interface or abstract class */
1617 if (!res_type.IsInstantiableTypes()) {
1618 Fail(VERIFY_ERROR_INSTANTIATION)
1619 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001620 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001621 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001622 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1623 // Any registers holding previous allocations from this address that have not yet been
1624 // initialized must be marked invalid.
1625 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1626 // add the new uninitialized reference to the register state
1627 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001628 break;
1629 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001630 case Instruction::NEW_ARRAY:
1631 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001632 break;
1633 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08001634 VerifyNewArray(dec_insn, true, false);
1635 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001636 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001637 case Instruction::FILLED_NEW_ARRAY_RANGE:
1638 VerifyNewArray(dec_insn, true, true);
1639 just_set_result = true; // Filled new array range sets result register
1640 break;
jeffhaobdb76512011-09-07 11:43:16 -07001641 case Instruction::CMPL_FLOAT:
1642 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001643 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001644 break;
1645 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001646 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001647 break;
1648 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001649 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001650 break;
1651 case Instruction::CMPL_DOUBLE:
1652 case Instruction::CMPG_DOUBLE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001653 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08001654 break;
1655 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001656 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08001657 break;
1658 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001659 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001660 break;
1661 case Instruction::CMP_LONG:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001662 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08001663 break;
1664 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001665 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08001666 break;
1667 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001668 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001669 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001670 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001671 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersb4903572012-10-11 11:52:56 -07001672 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001673 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001674 }
1675 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001676 }
jeffhaobdb76512011-09-07 11:43:16 -07001677 case Instruction::GOTO:
1678 case Instruction::GOTO_16:
1679 case Instruction::GOTO_32:
1680 /* no effect on or use of registers */
1681 break;
1682
1683 case Instruction::PACKED_SWITCH:
1684 case Instruction::SPARSE_SWITCH:
1685 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001686 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001687 break;
1688
Ian Rogersd81871c2011-10-03 13:57:23 -07001689 case Instruction::FILL_ARRAY_DATA: {
1690 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001691 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08001692 /* array_type can be null if the reg type is Zero */
1693 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001694 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001695 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001696 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001697 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1698 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001699 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001700 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1701 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001702 } else {
jeffhao457cc512012-02-02 16:55:13 -08001703 // Now verify if the element width in the table matches the element width declared in
1704 // the array
1705 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1706 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001707 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001708 } else {
1709 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1710 // Since we don't compress the data in Dex, expect to see equal width of data stored
1711 // in the table and expected from the array class.
1712 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001713 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1714 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001715 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001716 }
1717 }
jeffhaobdb76512011-09-07 11:43:16 -07001718 }
1719 }
1720 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001721 }
jeffhaobdb76512011-09-07 11:43:16 -07001722 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001723 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001724 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1725 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001726 bool mismatch = false;
1727 if (reg_type1.IsZero()) { // zero then integral or reference expected
1728 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1729 } else if (reg_type1.IsReferenceTypes()) { // both references?
1730 mismatch = !reg_type2.IsReferenceTypes();
1731 } else { // both integral?
1732 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1733 }
1734 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001735 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1736 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001737 }
1738 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001739 }
jeffhaobdb76512011-09-07 11:43:16 -07001740 case Instruction::IF_LT:
1741 case Instruction::IF_GE:
1742 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001743 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001744 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1745 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001746 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1748 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001749 }
1750 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001751 }
jeffhaobdb76512011-09-07 11:43:16 -07001752 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001753 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001754 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001755 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001756 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001757 }
jeffhaobdb76512011-09-07 11:43:16 -07001758 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001759 }
jeffhaobdb76512011-09-07 11:43:16 -07001760 case Instruction::IF_LTZ:
1761 case Instruction::IF_GEZ:
1762 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001763 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001764 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001765 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001766 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1767 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001768 }
jeffhaobdb76512011-09-07 11:43:16 -07001769 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001770 }
jeffhaobdb76512011-09-07 11:43:16 -07001771 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001772 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
1773 break;
jeffhaobdb76512011-09-07 11:43:16 -07001774 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001775 VerifyAGet(dec_insn, reg_types_.Byte(), true);
1776 break;
jeffhaobdb76512011-09-07 11:43:16 -07001777 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07001778 VerifyAGet(dec_insn, reg_types_.Char(), true);
1779 break;
jeffhaobdb76512011-09-07 11:43:16 -07001780 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001781 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001782 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001783 case Instruction::AGET:
1784 VerifyAGet(dec_insn, reg_types_.Integer(), true);
1785 break;
jeffhaobdb76512011-09-07 11:43:16 -07001786 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 VerifyAGet(dec_insn, reg_types_.Long(), true);
1788 break;
1789 case Instruction::AGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001790 VerifyAGet(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001791 break;
1792
Ian Rogersd81871c2011-10-03 13:57:23 -07001793 case Instruction::APUT_BOOLEAN:
1794 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
1795 break;
1796 case Instruction::APUT_BYTE:
1797 VerifyAPut(dec_insn, reg_types_.Byte(), true);
1798 break;
1799 case Instruction::APUT_CHAR:
1800 VerifyAPut(dec_insn, reg_types_.Char(), true);
1801 break;
1802 case Instruction::APUT_SHORT:
1803 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001804 break;
1805 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001806 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001807 break;
1808 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001809 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001810 break;
1811 case Instruction::APUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001812 VerifyAPut(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001813 break;
1814
jeffhaobdb76512011-09-07 11:43:16 -07001815 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001816 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001817 break;
jeffhaobdb76512011-09-07 11:43:16 -07001818 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001819 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001820 break;
jeffhaobdb76512011-09-07 11:43:16 -07001821 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001822 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001823 break;
jeffhaobdb76512011-09-07 11:43:16 -07001824 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001825 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001826 break;
1827 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001828 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001829 break;
1830 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001831 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001832 break;
1833 case Instruction::IGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001834 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001835 break;
jeffhaobdb76512011-09-07 11:43:16 -07001836
Ian Rogersd81871c2011-10-03 13:57:23 -07001837 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001838 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001839 break;
1840 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001841 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001842 break;
1843 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001844 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001845 break;
1846 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001847 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001848 break;
1849 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001850 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001851 break;
1852 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001853 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001854 break;
jeffhaobdb76512011-09-07 11:43:16 -07001855 case Instruction::IPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001856 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001857 break;
1858
jeffhaobdb76512011-09-07 11:43:16 -07001859 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001860 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001861 break;
jeffhaobdb76512011-09-07 11:43:16 -07001862 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001863 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001864 break;
jeffhaobdb76512011-09-07 11:43:16 -07001865 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001866 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001867 break;
jeffhaobdb76512011-09-07 11:43:16 -07001868 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001869 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001870 break;
1871 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001872 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001873 break;
1874 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001875 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001876 break;
1877 case Instruction::SGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001878 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001879 break;
1880
1881 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001882 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001883 break;
1884 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001885 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001886 break;
1887 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001888 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001889 break;
1890 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001891 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001892 break;
1893 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001894 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001895 break;
1896 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001897 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001898 break;
1899 case Instruction::SPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001900 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001901 break;
1902
1903 case Instruction::INVOKE_VIRTUAL:
1904 case Instruction::INVOKE_VIRTUAL_RANGE:
1905 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001906 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001907 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
1908 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
1909 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
1910 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartier66f19252012-09-18 08:57:04 -07001911 AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001912 const char* descriptor;
1913 if (called_method == NULL) {
1914 uint32_t method_idx = dec_insn.vB;
1915 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1916 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1917 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1918 } else {
1919 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07001920 }
Ian Rogersb4903572012-10-11 11:52:56 -07001921 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001922 work_line_->SetResultRegisterType(return_type);
1923 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001924 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001925 }
jeffhaobdb76512011-09-07 11:43:16 -07001926 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001927 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001928 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartier66f19252012-09-18 08:57:04 -07001929 AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07001930 const char* return_type_descriptor;
1931 bool is_constructor;
1932 if (called_method == NULL) {
1933 uint32_t method_idx = dec_insn.vB;
1934 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1935 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
1936 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1937 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1938 } else {
1939 is_constructor = called_method->IsConstructor();
1940 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
1941 }
1942 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07001943 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001944 * Some additional checks when calling a constructor. We know from the invocation arg check
1945 * that the "this" argument is an instance of called_method->klass. Now we further restrict
1946 * that to require that called_method->klass is the same as this->klass or this->super,
1947 * allowing the latter only if the "this" argument is the same as the "this" argument to
1948 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07001949 */
jeffhaob57e9522012-04-26 18:08:21 -07001950 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1951 if (this_type.IsConflict()) // failure.
1952 break;
jeffhaobdb76512011-09-07 11:43:16 -07001953
jeffhaob57e9522012-04-26 18:08:21 -07001954 /* no null refs allowed (?) */
1955 if (this_type.IsZero()) {
1956 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
1957 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07001958 }
jeffhaob57e9522012-04-26 18:08:21 -07001959
1960 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07001961 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
1962 // TODO: re-enable constructor type verification
1963 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07001964 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07001965 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
1966 // break;
1967 // }
jeffhaob57e9522012-04-26 18:08:21 -07001968
1969 /* arg must be an uninitialized reference */
1970 if (!this_type.IsUninitializedTypes()) {
1971 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
1972 << this_type;
1973 break;
1974 }
1975
1976 /*
1977 * Replace the uninitialized reference with an initialized one. We need to do this for all
1978 * registers that have the same object instance in them, not just the "this" register.
1979 */
1980 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001981 }
Ian Rogersb4903572012-10-11 11:52:56 -07001982 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
1983 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001984 work_line_->SetResultRegisterType(return_type);
1985 just_set_result = true;
1986 break;
1987 }
1988 case Instruction::INVOKE_STATIC:
1989 case Instruction::INVOKE_STATIC_RANGE: {
1990 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartier66f19252012-09-18 08:57:04 -07001991 AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001992 const char* descriptor;
1993 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001994 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001995 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1996 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07001997 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001998 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001999 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002000 }
Ian Rogersb4903572012-10-11 11:52:56 -07002001 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002002 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002003 just_set_result = true;
2004 }
2005 break;
jeffhaobdb76512011-09-07 11:43:16 -07002006 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002007 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002008 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartier66f19252012-09-18 08:57:04 -07002009 AbstractMethod* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002010 if (abs_method != NULL) {
2011 Class* called_interface = abs_method->GetDeclaringClass();
2012 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2013 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2014 << PrettyMethod(abs_method) << "'";
2015 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002016 }
Ian Rogers0d604842012-04-16 14:50:24 -07002017 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002018 /* Get the type of the "this" arg, which should either be a sub-interface of called
2019 * interface or Object (see comments in RegType::JoinClass).
2020 */
2021 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2022 if (this_type.IsZero()) {
2023 /* null pointer always passes (and always fails at runtime) */
2024 } else {
2025 if (this_type.IsUninitializedTypes()) {
2026 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2027 << this_type;
2028 break;
2029 }
2030 // In the past we have tried to assert that "called_interface" is assignable
2031 // from "this_type.GetClass()", however, as we do an imprecise Join
2032 // (RegType::JoinClass) we don't have full information on what interfaces are
2033 // implemented by "this_type". For example, two classes may implement the same
2034 // interfaces and have a common parent that doesn't implement the interface. The
2035 // join will set "this_type" to the parent class and a test that this implements
2036 // the interface will incorrectly fail.
2037 }
2038 /*
2039 * We don't have an object instance, so we can't find the concrete method. However, all of
2040 * the type information is in the abstract method, so we're good.
2041 */
2042 const char* descriptor;
2043 if (abs_method == NULL) {
2044 uint32_t method_idx = dec_insn.vB;
2045 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2046 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2047 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2048 } else {
2049 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2050 }
Ian Rogersb4903572012-10-11 11:52:56 -07002051 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002052 work_line_->SetResultRegisterType(return_type);
2053 work_line_->SetResultRegisterType(return_type);
2054 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002055 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002056 }
jeffhaobdb76512011-09-07 11:43:16 -07002057 case Instruction::NEG_INT:
2058 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002059 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002060 break;
2061 case Instruction::NEG_LONG:
2062 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002063 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002064 break;
2065 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002066 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002067 break;
2068 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002069 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002070 break;
2071 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002072 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002073 break;
2074 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002075 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002076 break;
2077 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002078 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002079 break;
2080 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002081 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002082 break;
2083 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002084 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002085 break;
2086 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002087 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002088 break;
2089 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002090 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002091 break;
2092 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002093 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002094 break;
2095 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002096 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002097 break;
2098 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002099 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002100 break;
2101 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002102 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002103 break;
2104 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002105 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002106 break;
2107 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002108 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002109 break;
2110 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002112 break;
2113 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002114 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002115 break;
2116
2117 case Instruction::ADD_INT:
2118 case Instruction::SUB_INT:
2119 case Instruction::MUL_INT:
2120 case Instruction::REM_INT:
2121 case Instruction::DIV_INT:
2122 case Instruction::SHL_INT:
2123 case Instruction::SHR_INT:
2124 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002125 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002126 break;
2127 case Instruction::AND_INT:
2128 case Instruction::OR_INT:
2129 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002130 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002131 break;
2132 case Instruction::ADD_LONG:
2133 case Instruction::SUB_LONG:
2134 case Instruction::MUL_LONG:
2135 case Instruction::DIV_LONG:
2136 case Instruction::REM_LONG:
2137 case Instruction::AND_LONG:
2138 case Instruction::OR_LONG:
2139 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002140 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002141 break;
2142 case Instruction::SHL_LONG:
2143 case Instruction::SHR_LONG:
2144 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002145 /* shift distance is Int, making these different from other binary operations */
2146 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002147 break;
2148 case Instruction::ADD_FLOAT:
2149 case Instruction::SUB_FLOAT:
2150 case Instruction::MUL_FLOAT:
2151 case Instruction::DIV_FLOAT:
2152 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002153 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002154 break;
2155 case Instruction::ADD_DOUBLE:
2156 case Instruction::SUB_DOUBLE:
2157 case Instruction::MUL_DOUBLE:
2158 case Instruction::DIV_DOUBLE:
2159 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002160 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002161 break;
2162 case Instruction::ADD_INT_2ADDR:
2163 case Instruction::SUB_INT_2ADDR:
2164 case Instruction::MUL_INT_2ADDR:
2165 case Instruction::REM_INT_2ADDR:
2166 case Instruction::SHL_INT_2ADDR:
2167 case Instruction::SHR_INT_2ADDR:
2168 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002169 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002170 break;
2171 case Instruction::AND_INT_2ADDR:
2172 case Instruction::OR_INT_2ADDR:
2173 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002174 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002175 break;
2176 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002177 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002178 break;
2179 case Instruction::ADD_LONG_2ADDR:
2180 case Instruction::SUB_LONG_2ADDR:
2181 case Instruction::MUL_LONG_2ADDR:
2182 case Instruction::DIV_LONG_2ADDR:
2183 case Instruction::REM_LONG_2ADDR:
2184 case Instruction::AND_LONG_2ADDR:
2185 case Instruction::OR_LONG_2ADDR:
2186 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002187 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002188 break;
2189 case Instruction::SHL_LONG_2ADDR:
2190 case Instruction::SHR_LONG_2ADDR:
2191 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002192 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002193 break;
2194 case Instruction::ADD_FLOAT_2ADDR:
2195 case Instruction::SUB_FLOAT_2ADDR:
2196 case Instruction::MUL_FLOAT_2ADDR:
2197 case Instruction::DIV_FLOAT_2ADDR:
2198 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002199 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002200 break;
2201 case Instruction::ADD_DOUBLE_2ADDR:
2202 case Instruction::SUB_DOUBLE_2ADDR:
2203 case Instruction::MUL_DOUBLE_2ADDR:
2204 case Instruction::DIV_DOUBLE_2ADDR:
2205 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002206 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002207 break;
2208 case Instruction::ADD_INT_LIT16:
2209 case Instruction::RSUB_INT:
2210 case Instruction::MUL_INT_LIT16:
2211 case Instruction::DIV_INT_LIT16:
2212 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002213 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002214 break;
2215 case Instruction::AND_INT_LIT16:
2216 case Instruction::OR_INT_LIT16:
2217 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002218 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002219 break;
2220 case Instruction::ADD_INT_LIT8:
2221 case Instruction::RSUB_INT_LIT8:
2222 case Instruction::MUL_INT_LIT8:
2223 case Instruction::DIV_INT_LIT8:
2224 case Instruction::REM_INT_LIT8:
2225 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002226 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002227 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002228 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002229 break;
2230 case Instruction::AND_INT_LIT8:
2231 case Instruction::OR_INT_LIT8:
2232 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002233 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002234 break;
2235
Ian Rogersd81871c2011-10-03 13:57:23 -07002236 /* These should never appear during verification. */
jeffhao9a4f0032012-08-30 16:17:40 -07002237 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002238 case Instruction::UNUSED_EE:
2239 case Instruction::UNUSED_EF:
2240 case Instruction::UNUSED_F2:
2241 case Instruction::UNUSED_F3:
2242 case Instruction::UNUSED_F4:
2243 case Instruction::UNUSED_F5:
2244 case Instruction::UNUSED_F6:
2245 case Instruction::UNUSED_F7:
2246 case Instruction::UNUSED_F8:
2247 case Instruction::UNUSED_F9:
2248 case Instruction::UNUSED_FA:
2249 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002250 case Instruction::UNUSED_F0:
2251 case Instruction::UNUSED_F1:
2252 case Instruction::UNUSED_E3:
2253 case Instruction::UNUSED_E8:
2254 case Instruction::UNUSED_E7:
2255 case Instruction::UNUSED_E4:
2256 case Instruction::UNUSED_E9:
2257 case Instruction::UNUSED_FC:
2258 case Instruction::UNUSED_E5:
2259 case Instruction::UNUSED_EA:
2260 case Instruction::UNUSED_FD:
2261 case Instruction::UNUSED_E6:
2262 case Instruction::UNUSED_EB:
2263 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002264 case Instruction::UNUSED_3E:
2265 case Instruction::UNUSED_3F:
2266 case Instruction::UNUSED_40:
2267 case Instruction::UNUSED_41:
2268 case Instruction::UNUSED_42:
2269 case Instruction::UNUSED_43:
2270 case Instruction::UNUSED_73:
2271 case Instruction::UNUSED_79:
2272 case Instruction::UNUSED_7A:
2273 case Instruction::UNUSED_EC:
2274 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002275 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002276 break;
2277
2278 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002279 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002280 * complain if an instruction is missing (which is desirable).
2281 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002282 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002283
Ian Rogersad0b3a32012-04-16 14:50:24 -07002284 if (have_pending_hard_failure_) {
2285 if (!Runtime::Current()->IsStarted()) {
jeffhaob57e9522012-04-26 18:08:21 -07002286 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002287 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002288 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002289 /* immediate failure, reject class */
2290 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2291 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002292 } else if (have_pending_runtime_throw_failure_) {
2293 /* slow path will throw, mark following code as unreachable */
2294 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002295 }
jeffhaobdb76512011-09-07 11:43:16 -07002296 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002297 * If we didn't just set the result register, clear it out. This ensures that you can only use
2298 * "move-result" immediately after the result is set. (We could check this statically, but it's
2299 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002300 */
2301 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002302 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002303 }
2304
jeffhaoa0a764a2011-09-16 10:43:38 -07002305 /* Handle "continue". Tag the next consecutive instruction. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002306 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers776ac1f2012-04-13 23:36:36 -07002307 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
Ian Rogersd81871c2011-10-03 13:57:23 -07002308 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
jeffhaod5347e02012-03-22 17:25:05 -07002309 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002310 return false;
2311 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002312 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2313 // next instruction isn't one.
jeffhaod5347e02012-03-22 17:25:05 -07002314 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002315 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002316 }
2317 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2318 if (next_line != NULL) {
2319 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2320 // needed.
2321 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002322 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002323 }
jeffhaobdb76512011-09-07 11:43:16 -07002324 } else {
2325 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002326 * We're not recording register data for the next instruction, so we don't know what the prior
2327 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002328 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002329 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002330 }
2331 }
2332
2333 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002334 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002335 *
2336 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002337 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002338 * somebody could get a reference field, check it for zero, and if the
2339 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002340 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002341 * that, and will reject the code.
2342 *
2343 * TODO: avoid re-fetching the branch target
2344 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002345 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002346 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002347 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002348 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002349 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002350 return false;
2351 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002352 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002353 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002354 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002355 }
jeffhaobdb76512011-09-07 11:43:16 -07002356 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002357 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002358 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002359 }
jeffhaobdb76512011-09-07 11:43:16 -07002360 }
2361
2362 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002363 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002364 *
2365 * We've already verified that the table is structurally sound, so we
2366 * just need to walk through and tag the targets.
2367 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002368 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002369 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2370 const uint16_t* switch_insns = insns + offset_to_switch;
2371 int switch_count = switch_insns[1];
2372 int offset_to_targets, targ;
2373
2374 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2375 /* 0 = sig, 1 = count, 2/3 = first key */
2376 offset_to_targets = 4;
2377 } else {
2378 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002379 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002380 offset_to_targets = 2 + 2 * switch_count;
2381 }
2382
2383 /* verify each switch target */
2384 for (targ = 0; targ < switch_count; targ++) {
2385 int offset;
2386 uint32_t abs_offset;
2387
2388 /* offsets are 32-bit, and only partly endian-swapped */
2389 offset = switch_insns[offset_to_targets + targ * 2] |
2390 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002391 abs_offset = work_insn_idx_ + offset;
2392 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002393 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002394 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002395 }
2396 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002397 return false;
2398 }
2399 }
2400
2401 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002402 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2403 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002404 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002405 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002406 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002407 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002408
Ian Rogers0571d352011-11-03 19:51:38 -07002409 for (; iterator.HasNext(); iterator.Next()) {
2410 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002411 within_catch_all = true;
2412 }
jeffhaobdb76512011-09-07 11:43:16 -07002413 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002414 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2415 * "work_regs", because at runtime the exception will be thrown before the instruction
2416 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002417 */
Ian Rogers0571d352011-11-03 19:51:38 -07002418 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002419 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002420 }
jeffhaobdb76512011-09-07 11:43:16 -07002421 }
2422
2423 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002424 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2425 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002426 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002427 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002428 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002429 * The state in work_line reflects the post-execution state. If the current instruction is a
2430 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002431 * it will do so before grabbing the lock).
2432 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002433 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002434 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002435 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002436 return false;
2437 }
2438 }
2439 }
2440
jeffhaod1f0fde2011-09-08 17:25:33 -07002441 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002442 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002443 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002444 return false;
2445 }
jeffhaobdb76512011-09-07 11:43:16 -07002446 }
2447
2448 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002449 * Update start_guess. Advance to the next instruction of that's
2450 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002451 * neither of those exists we're in a return or throw; leave start_guess
2452 * alone and let the caller sort it out.
2453 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002454 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002455 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002456 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002457 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002458 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002459 }
2460
Ian Rogersd81871c2011-10-03 13:57:23 -07002461 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2462 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002463
2464 return true;
2465}
2466
Ian Rogers776ac1f2012-04-13 23:36:36 -07002467const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002468 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002469 const RegType& referrer = GetDeclaringClass();
2470 Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002471 const RegType& result =
Ian Rogersb4903572012-10-11 11:52:56 -07002472 klass != NULL ? reg_types_.FromClass(klass, klass->IsFinal())
2473 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002474 if (result.IsConflict()) {
2475 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2476 << "' in " << referrer;
2477 return result;
2478 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002479 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002480 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002481 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002482 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002483 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002484 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002485 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002486 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002487 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002488 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002489}
2490
Ian Rogers776ac1f2012-04-13 23:36:36 -07002491const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002492 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002493 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002494 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002495 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2496 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002497 CatchHandlerIterator iterator(handlers_ptr);
2498 for (; iterator.HasNext(); iterator.Next()) {
2499 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2500 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002501 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002502 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002503 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002504 if (common_super == NULL) {
2505 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2506 // as that is caught at runtime
2507 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002508 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002509 // We don't know enough about the type and the common path merge will result in
2510 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002511 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002512 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002513 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002514 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002515 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002516 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002517 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002518 }
2519 }
2520 }
2521 }
Ian Rogers0571d352011-11-03 19:51:38 -07002522 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002523 }
2524 }
2525 if (common_super == NULL) {
2526 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002527 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002528 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002529 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002530 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002531}
2532
Mathieu Chartier66f19252012-09-18 08:57:04 -07002533AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002534 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002535 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002536 if (klass_type.IsConflict()) {
2537 std::string append(" in attempt to access method ");
2538 append += dex_file_->GetMethodName(method_id);
2539 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002540 return NULL;
2541 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002542 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002543 return NULL; // Can't resolve Class so no more to do here
2544 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002545 Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002546 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier66f19252012-09-18 08:57:04 -07002547 AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002548 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002549 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002550 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002551
2552 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002553 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002554 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002555 res_method = klass->FindInterfaceMethod(name, signature);
2556 } else {
2557 res_method = klass->FindVirtualMethod(name, signature);
2558 }
2559 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002560 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002561 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002562 // If a virtual or interface method wasn't found with the expected type, look in
2563 // the direct methods. This can happen when the wrong invoke type is used or when
2564 // a class has changed, and will be flagged as an error in later checks.
2565 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2566 res_method = klass->FindDirectMethod(name, signature);
2567 }
2568 if (res_method == NULL) {
2569 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2570 << PrettyDescriptor(klass) << "." << name
2571 << " " << signature;
2572 return NULL;
2573 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002574 }
2575 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002576 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2577 // enforce them here.
2578 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002579 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2580 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002581 return NULL;
2582 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002583 // Disallow any calls to class initializers.
2584 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002585 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2586 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002587 return NULL;
2588 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002589 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002590 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002591 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002592 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002593 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002594 }
jeffhaode0d9c92012-02-27 13:58:13 -08002595 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2596 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002597 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2598 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002599 return NULL;
2600 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002601 // Check that interface methods match interface classes.
2602 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2603 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2604 << " is in an interface class " << PrettyClass(klass);
2605 return NULL;
2606 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2607 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2608 << " is in a non-interface class " << PrettyClass(klass);
2609 return NULL;
2610 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002611 // See if the method type implied by the invoke instruction matches the access flags for the
2612 // target method.
2613 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2614 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2615 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2616 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002617 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2618 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002619 return NULL;
2620 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002621 return res_method;
2622}
2623
Mathieu Chartier66f19252012-09-18 08:57:04 -07002624AbstractMethod* MethodVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
Ian Rogers46685432012-06-03 22:26:43 -07002625 MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002626 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2627 // we're making.
Mathieu Chartier66f19252012-09-18 08:57:04 -07002628 AbstractMethod* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002629 if (res_method == NULL) { // error or class is unresolved
2630 return NULL;
2631 }
2632
Ian Rogersd81871c2011-10-03 13:57:23 -07002633 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2634 // has a vtable entry for the target method.
2635 if (is_super) {
2636 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002637 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002638 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002639 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
2640 << PrettyMethod(method_idx_, *dex_file_)
2641 << " to super " << PrettyMethod(res_method);
2642 return NULL;
2643 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002644 Class* super_klass = super.GetClass();
2645 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002646 MethodHelper mh(res_method);
2647 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
2648 << PrettyMethod(method_idx_, *dex_file_)
2649 << " to super " << super
2650 << "." << mh.GetName()
2651 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002652 return NULL;
2653 }
2654 }
2655 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2656 // match the call to the signature. Also, we might might be calling through an abstract method
2657 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08002658 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002659 /* caught by static verifier */
2660 DCHECK(is_range || expected_args <= 5);
2661 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002662 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002663 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2664 return NULL;
2665 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002666
jeffhaobdb76512011-09-07 11:43:16 -07002667 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002668 * Check the "this" argument, which must be an instance of the class that declared the method.
2669 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2670 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002671 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002672 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002673 if (!res_method->IsStatic()) {
2674 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002675 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002676 return NULL;
2677 }
2678 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002679 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002680 return NULL;
2681 }
2682 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersb4903572012-10-11 11:52:56 -07002683 Class* klass = res_method->GetDeclaringClass();
2684 const RegType& res_method_class = reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogers9074b992011-10-26 17:41:55 -07002685 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002686 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002687 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002688 return NULL;
2689 }
2690 }
2691 actual_args++;
2692 }
2693 /*
2694 * Process the target method's signature. This signature may or may not
2695 * have been verified, so we can't assume it's properly formed.
2696 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002697 MethodHelper mh(res_method);
2698 const DexFile::TypeList* params = mh.GetParameterTypeList();
2699 size_t params_size = params == NULL ? 0 : params->Size();
2700 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002701 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002702 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002703 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2704 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002705 return NULL;
2706 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002707 const char* descriptor =
2708 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2709 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002710 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002711 << " missing signature component";
2712 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002713 }
Ian Rogersb4903572012-10-11 11:52:56 -07002714 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002715 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002716 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002717 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002718 }
2719 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2720 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002721 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002722 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002723 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002724 return NULL;
2725 } else {
2726 return res_method;
2727 }
2728}
2729
Ian Rogers776ac1f2012-04-13 23:36:36 -07002730void MethodVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08002731 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002732 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002733 if (res_type.IsConflict()) { // bad class
2734 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002735 } else {
2736 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2737 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002738 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002739 } else if (!is_filled) {
2740 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002741 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002742 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002743 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002744 } else {
2745 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2746 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002747 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002748 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002749 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002750 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002751 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002752 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002753 return;
2754 }
2755 }
2756 // filled-array result goes into "result" register
2757 work_line_->SetResultRegisterType(res_type);
2758 }
2759 }
2760}
2761
Ian Rogers776ac1f2012-04-13 23:36:36 -07002762void MethodVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002763 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002764 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002765 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002766 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002767 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002768 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002769 if (array_type.IsZero()) {
2770 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2771 // instruction type. TODO: have a proper notion of bottom here.
2772 if (!is_primitive || insn_type.IsCategory1Types()) {
2773 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08002774 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002775 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002776 // Category 2
Elliott Hughesadb8c672012-03-06 16:49:32 -08002777 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
Ian Rogers89310de2012-02-01 13:47:30 -08002778 }
jeffhaofc3144e2012-02-01 17:21:15 -08002779 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002780 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002781 } else {
2782 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002783 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002784 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002785 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002786 << " source for aget-object";
2787 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002788 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002789 << " source for category 1 aget";
2790 } else if (is_primitive && !insn_type.Equals(component_type) &&
2791 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2792 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002793 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogersd81871c2011-10-03 13:57:23 -07002794 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002795 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002796 // Use knowledge of the field type which is stronger than the type inferred from the
2797 // instruction, which can't differentiate object types and ints from floats, longs from
2798 // doubles.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002799 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002800 }
2801 }
2802 }
2803}
2804
Ian Rogers776ac1f2012-04-13 23:36:36 -07002805void MethodVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002806 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002807 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002808 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002809 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002810 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002811 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002812 if (array_type.IsZero()) {
2813 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2814 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002815 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002816 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002817 } else {
2818 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002819 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002820 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002821 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002822 << " source for aput-object";
2823 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002824 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002825 << " source for category 1 aput";
2826 } else if (is_primitive && !insn_type.Equals(component_type) &&
2827 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2828 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002829 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002830 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002831 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002832 // The instruction agrees with the type of array, confirm the value to be stored does too
2833 // Note: we use the instruction type (rather than the component type) for aput-object as
2834 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08002835 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002836 }
2837 }
2838 }
2839}
2840
Ian Rogers776ac1f2012-04-13 23:36:36 -07002841Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002842 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2843 // Check access to class
2844 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002845 if (klass_type.IsConflict()) { // bad class
2846 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
2847 field_idx, dex_file_->GetFieldName(field_id),
2848 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002849 return NULL;
2850 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002851 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002852 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08002853 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002854 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2855 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002856 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002857 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
2858 << dex_file_->GetFieldName(field_id) << ") in "
2859 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002860 DCHECK(Thread::Current()->IsExceptionPending());
2861 Thread::Current()->ClearException();
2862 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002863 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2864 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002865 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002866 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002867 return NULL;
2868 } else if (!field->IsStatic()) {
2869 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
2870 return NULL;
2871 } else {
2872 return field;
2873 }
2874}
2875
Ian Rogers776ac1f2012-04-13 23:36:36 -07002876Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002877 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2878 // Check access to class
2879 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002880 if (klass_type.IsConflict()) {
2881 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
2882 field_idx, dex_file_->GetFieldName(field_id),
2883 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002884 return NULL;
2885 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002886 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002887 return NULL; // Can't resolve Class so no more to do here
2888 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002889 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2890 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002891 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002892 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
2893 << dex_file_->GetFieldName(field_id) << ") in "
2894 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002895 DCHECK(Thread::Current()->IsExceptionPending());
2896 Thread::Current()->ClearException();
2897 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002898 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2899 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002900 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002901 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002902 return NULL;
2903 } else if (field->IsStatic()) {
2904 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
2905 << " to not be static";
2906 return NULL;
2907 } else if (obj_type.IsZero()) {
2908 // Cannot infer and check type, however, access will cause null pointer exception
2909 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07002910 } else {
Ian Rogersb4903572012-10-11 11:52:56 -07002911 Class* klass = field->GetDeclaringClass();
2912 const RegType& field_klass = reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002913 if (obj_type.IsUninitializedTypes() &&
2914 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
2915 !field_klass.Equals(GetDeclaringClass()))) {
2916 // Field accesses through uninitialized references are only allowable for constructors where
2917 // the field is declared in this class
2918 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
2919 << " of a not fully initialized object within the context of "
2920 << PrettyMethod(method_idx_, *dex_file_);
2921 return NULL;
2922 } else if (!field_klass.IsAssignableFrom(obj_type)) {
2923 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
2924 // of C1. For resolution to occur the declared class of the field must be compatible with
2925 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
2926 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
2927 << " from object of type " << obj_type;
2928 return NULL;
2929 } else {
2930 return field;
2931 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002932 }
2933}
2934
Ian Rogers776ac1f2012-04-13 23:36:36 -07002935void MethodVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002936 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002937 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002938 Field* field;
2939 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002940 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002941 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002942 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07002943 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002944 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002945 const char* descriptor;
Ian Rogers365c1022012-06-22 15:05:28 -07002946 ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002947 if (field != NULL) {
2948 descriptor = FieldHelper(field).GetTypeDescriptor();
2949 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07002950 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002951 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2952 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2953 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07002954 }
Ian Rogersb4903572012-10-11 11:52:56 -07002955 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002956 if (is_primitive) {
2957 if (field_type.Equals(insn_type) ||
2958 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
2959 (field_type.IsDouble() && insn_type.IsLongTypes())) {
2960 // expected that read is of the correct primitive type or that int reads are reading
2961 // floats or long reads are reading doubles
2962 } else {
2963 // This is a global failure rather than a class change failure as the instructions and
2964 // the descriptors for the type should have been consistent within the same file at
2965 // compile time
2966 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
2967 << " to be of type '" << insn_type
2968 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002969 return;
2970 }
2971 } else {
2972 if (!insn_type.IsAssignableFrom(field_type)) {
2973 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
2974 << " to be compatible with type '" << insn_type
2975 << "' but found type '" << field_type
2976 << "' in get-object";
2977 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Conflict());
2978 return;
2979 }
2980 }
2981 work_line_->SetRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002982}
2983
Ian Rogers776ac1f2012-04-13 23:36:36 -07002984void MethodVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002985 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002986 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002987 Field* field;
2988 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07002989 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002990 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002991 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07002992 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002993 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002994 const char* descriptor;
Ian Rogers365c1022012-06-22 15:05:28 -07002995 ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002996 if (field != NULL) {
2997 descriptor = FieldHelper(field).GetTypeDescriptor();
2998 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07002999 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003000 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3001 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3002 loader = class_loader_;
3003 }
Ian Rogersb4903572012-10-11 11:52:56 -07003004 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003005 if (field != NULL) {
3006 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3007 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3008 << " from other class " << GetDeclaringClass();
3009 return;
3010 }
3011 }
3012 if (is_primitive) {
3013 // Primitive field assignability rules are weaker than regular assignability rules
3014 bool instruction_compatible;
3015 bool value_compatible;
3016 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
3017 if (field_type.IsIntegralTypes()) {
3018 instruction_compatible = insn_type.IsIntegralTypes();
3019 value_compatible = value_type.IsIntegralTypes();
3020 } else if (field_type.IsFloat()) {
3021 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3022 value_compatible = value_type.IsFloatTypes();
3023 } else if (field_type.IsLong()) {
3024 instruction_compatible = insn_type.IsLong();
3025 value_compatible = value_type.IsLongTypes();
3026 } else if (field_type.IsDouble()) {
3027 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3028 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003029 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003030 instruction_compatible = false; // reference field with primitive store
3031 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003032 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003033 if (!instruction_compatible) {
3034 // This is a global failure rather than a class change failure as the instructions and
3035 // the descriptors for the type should have been consistent within the same file at
3036 // compile time
3037 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3038 << " to be of type '" << insn_type
3039 << "' but found type '" << field_type
3040 << "' in put";
3041 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003042 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003043 if (!value_compatible) {
3044 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
3045 << " of type " << value_type
3046 << " but expected " << field_type
3047 << " for store to " << PrettyField(field) << " in put";
3048 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003049 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003050 } else {
3051 if (!insn_type.IsAssignableFrom(field_type)) {
3052 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3053 << " to be compatible with type '" << insn_type
3054 << "' but found type '" << field_type
3055 << "' in put-object";
3056 return;
3057 }
3058 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003059 }
3060}
3061
Ian Rogers776ac1f2012-04-13 23:36:36 -07003062bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003063 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003064 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003065 return false;
3066 }
3067 return true;
3068}
3069
Ian Rogers776ac1f2012-04-13 23:36:36 -07003070bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003071 bool changed = true;
3072 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3073 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003074 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003075 * We haven't processed this instruction before, and we haven't touched the registers here, so
3076 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3077 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003078 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003079 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003080 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003081 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3082 if (gDebugVerify) {
3083 copy->CopyFromLine(target_line);
3084 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003085 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003086 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003087 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003088 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003089 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003090 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003091 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3092 << *copy.get() << " MERGE\n"
3093 << *merge_line << " ==\n"
3094 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003095 }
3096 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003097 if (changed) {
3098 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003099 }
3100 return true;
3101}
3102
Ian Rogers776ac1f2012-04-13 23:36:36 -07003103InsnFlags* MethodVerifier::CurrentInsnFlags() {
3104 return &insn_flags_[work_insn_idx_];
3105}
3106
Ian Rogersad0b3a32012-04-16 14:50:24 -07003107const RegType& MethodVerifier::GetMethodReturnType() {
3108 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx_);
3109 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3110 uint16_t return_type_idx = proto_id.return_type_idx_;
3111 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003112 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003113}
3114
3115const RegType& MethodVerifier::GetDeclaringClass() {
3116 if (foo_method_ != NULL) {
Ian Rogersb4903572012-10-11 11:52:56 -07003117 Class* klass = foo_method_->GetDeclaringClass();
3118 return reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003119 } else {
3120 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx_);
3121 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogersb4903572012-10-11 11:52:56 -07003122 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003123 }
3124}
3125
Ian Rogers776ac1f2012-04-13 23:36:36 -07003126void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003127 size_t* log2_max_gc_pc) {
3128 size_t local_gc_points = 0;
3129 size_t max_insn = 0;
3130 size_t max_ref_reg = -1;
3131 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3132 if (insn_flags_[i].IsGcPoint()) {
3133 local_gc_points++;
3134 max_insn = i;
3135 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003136 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003137 }
3138 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003139 *gc_points = local_gc_points;
3140 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3141 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003142 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003143 i++;
3144 }
3145 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003146}
3147
Ian Rogers776ac1f2012-04-13 23:36:36 -07003148const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003149 size_t num_entries, ref_bitmap_bits, pc_bits;
3150 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3151 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003152 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003153 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003154 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003155 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003156 return NULL;
3157 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003158 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3159 // There are 2 bytes to encode the number of entries
3160 if (num_entries >= 65536) {
3161 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003162 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003163 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003164 return NULL;
3165 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003166 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003167 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003168 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003169 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003170 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003171 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003172 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003173 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003174 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003175 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003176 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003177 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3178 return NULL;
3179 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003180 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003181 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003182 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003183 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003184 return NULL;
3185 }
3186 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003187 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3188 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003189 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003190 table->push_back(num_entries & 0xFF);
3191 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003192 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003193 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3194 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003195 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003196 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003197 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003198 }
3199 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003200 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003201 }
3202 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003203 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003204 return table;
3205}
jeffhaoa0a764a2011-09-16 10:43:38 -07003206
Ian Rogers776ac1f2012-04-13 23:36:36 -07003207void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003208 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3209 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003210 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003211 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003212 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003213 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3214 if (insn_flags_[i].IsGcPoint()) {
3215 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003216 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003217 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3218 map_index++;
3219 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003220 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003221 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003222 CHECK_LT(j / 8, map.RegWidth());
3223 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3224 } else if ((j / 8) < map.RegWidth()) {
3225 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3226 } else {
3227 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3228 }
3229 }
3230 } else {
3231 CHECK(reg_bitmap == NULL);
3232 }
3233 }
3234}
jeffhaoa0a764a2011-09-16 10:43:38 -07003235
Ian Rogers0c7abda2012-09-19 13:33:42 -07003236void MethodVerifier::SetDexGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003237 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003238 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003239 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
3240 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003241 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003242 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003243 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003244 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003245 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003246 CHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003247}
3248
Ian Rogers0c7abda2012-09-19 13:33:42 -07003249const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(Compiler::MethodReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003250 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003251 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
3252 if (it == dex_gc_maps_->end()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003253 return NULL;
3254 }
3255 CHECK(it->second != NULL);
3256 return it->second;
3257}
3258
Ian Rogers0c7abda2012-09-19 13:33:42 -07003259Mutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
3260MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003261
3262Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
3263MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
3264
buzbeec531cef2012-10-18 07:09:20 -07003265#if defined(ART_USE_LLVM_COMPILER)
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003266Mutex* MethodVerifier::inferred_reg_category_maps_lock_ = NULL;
3267MethodVerifier::InferredRegCategoryMapTable* MethodVerifier::inferred_reg_category_maps_ = NULL;
3268#endif
3269
3270void MethodVerifier::Init() {
Ian Rogers0c7abda2012-09-19 13:33:42 -07003271 dex_gc_maps_lock_ = new Mutex("verifier GC maps lock");
Ian Rogers50b35e22012-10-04 10:09:15 -07003272 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003273 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003274 MutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003275 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003276 }
3277
3278 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
3279 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003280 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003281 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
3282 }
3283
buzbeec531cef2012-10-18 07:09:20 -07003284#if defined(ART_USE_LLVM_COMPILER)
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003285 inferred_reg_category_maps_lock_ = new Mutex("verifier GC maps lock");
3286 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003287 MutexLock mu(self, *inferred_reg_category_maps_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003288 inferred_reg_category_maps_ = new MethodVerifier::InferredRegCategoryMapTable;
3289 }
3290#endif
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003291}
3292
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003293void MethodVerifier::Shutdown() {
Ian Rogers50b35e22012-10-04 10:09:15 -07003294 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003295 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003296 MutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003297 STLDeleteValues(dex_gc_maps_);
3298 delete dex_gc_maps_;
3299 dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003300 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003301 delete dex_gc_maps_lock_;
3302 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003303
3304 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003305 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003306 delete rejected_classes_;
3307 rejected_classes_ = NULL;
3308 }
3309 delete rejected_classes_lock_;
3310 rejected_classes_lock_ = NULL;
3311
buzbeec531cef2012-10-18 07:09:20 -07003312#if defined(ART_USE_LLVM_COMPILER)
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003313 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003314 MutexLock mu(self, *inferred_reg_category_maps_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003315 STLDeleteValues(inferred_reg_category_maps_);
3316 delete inferred_reg_category_maps_;
3317 inferred_reg_category_maps_ = NULL;
3318 }
3319 delete inferred_reg_category_maps_lock_;
3320 inferred_reg_category_maps_lock_ = NULL;
3321#endif
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003322}
jeffhaod1224c72012-02-29 13:43:08 -08003323
Ian Rogers776ac1f2012-04-13 23:36:36 -07003324void MethodVerifier::AddRejectedClass(Compiler::ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003325 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003326 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003327 rejected_classes_->insert(ref);
3328 }
jeffhaod1224c72012-02-29 13:43:08 -08003329 CHECK(IsClassRejected(ref));
3330}
3331
Ian Rogers776ac1f2012-04-13 23:36:36 -07003332bool MethodVerifier::IsClassRejected(Compiler::ClassReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003333 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003334 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08003335}
3336
buzbeec531cef2012-10-18 07:09:20 -07003337#if defined(ART_USE_LLVM_COMPILER)
TDYa12789f96052012-07-12 20:49:53 -07003338const greenland::InferredRegCategoryMap* MethodVerifier::GenerateInferredRegCategoryMap() {
Logan Chienfca7e872011-12-20 20:08:22 +08003339 uint32_t insns_size = code_item_->insns_size_in_code_units_;
3340 uint16_t regs_size = code_item_->registers_size_;
3341
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003342 UniquePtr<InferredRegCategoryMap> table(new InferredRegCategoryMap(insns_size, regs_size));
Logan Chienfca7e872011-12-20 20:08:22 +08003343
3344 for (size_t i = 0; i < insns_size; ++i) {
3345 if (RegisterLine* line = reg_table_.GetLine(i)) {
TDYa127526643e2012-05-26 01:01:48 -07003346 const Instruction* inst = Instruction::At(code_item_->insns_ + i);
3347
3348 // GC points
3349 if (inst->IsBranch() || inst->IsInvoke()) {
3350 for (size_t r = 0; r < regs_size; ++r) {
3351 const RegType &rt = line->GetRegisterType(r);
3352 if (rt.IsNonZeroReferenceTypes()) {
3353 table->SetRegCanBeObject(r);
3354 }
TDYa127b2eb5c12012-05-24 15:52:10 -07003355 }
3356 }
3357
TDYa127526643e2012-05-26 01:01:48 -07003358 /* We only use InferredRegCategoryMap in one case */
3359 if (inst->IsBranch()) {
TDYa127b2eb5c12012-05-24 15:52:10 -07003360 for (size_t r = 0; r < regs_size; ++r) {
3361 const RegType &rt = line->GetRegisterType(r);
3362
3363 if (rt.IsZero()) {
TDYa12789f96052012-07-12 20:49:53 -07003364 table->SetRegCategory(i, r, greenland::kRegZero);
TDYa127b2eb5c12012-05-24 15:52:10 -07003365 } else if (rt.IsCategory1Types()) {
TDYa12789f96052012-07-12 20:49:53 -07003366 table->SetRegCategory(i, r, greenland::kRegCat1nr);
TDYa127b2eb5c12012-05-24 15:52:10 -07003367 } else if (rt.IsCategory2Types()) {
TDYa12789f96052012-07-12 20:49:53 -07003368 table->SetRegCategory(i, r, greenland::kRegCat2);
TDYa127b2eb5c12012-05-24 15:52:10 -07003369 } else if (rt.IsReferenceTypes()) {
TDYa12789f96052012-07-12 20:49:53 -07003370 table->SetRegCategory(i, r, greenland::kRegObject);
TDYa127b2eb5c12012-05-24 15:52:10 -07003371 } else {
TDYa12789f96052012-07-12 20:49:53 -07003372 table->SetRegCategory(i, r, greenland::kRegUnknown);
TDYa127b2eb5c12012-05-24 15:52:10 -07003373 }
Logan Chienfca7e872011-12-20 20:08:22 +08003374 }
3375 }
3376 }
3377 }
3378
3379 return table.release();
3380}
Logan Chiendd361c92012-04-10 23:40:37 +08003381
Ian Rogers776ac1f2012-04-13 23:36:36 -07003382void MethodVerifier::SetInferredRegCategoryMap(Compiler::MethodReference ref,
3383 const InferredRegCategoryMap& inferred_reg_category_map) {
Shih-wei Liaocd05a622012-08-15 00:02:05 -07003384 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003385 MutexLock mu(Thread::Current(), *inferred_reg_category_maps_lock_);
Shih-wei Liaocd05a622012-08-15 00:02:05 -07003386 InferredRegCategoryMapTable::iterator it = inferred_reg_category_maps_->find(ref);
3387 if (it == inferred_reg_category_maps_->end()) {
3388 inferred_reg_category_maps_->Put(ref, &inferred_reg_category_map);
3389 } else {
3390 CHECK(*(it->second) == inferred_reg_category_map);
3391 delete &inferred_reg_category_map;
3392 }
Logan Chiendd361c92012-04-10 23:40:37 +08003393 }
Logan Chiendd361c92012-04-10 23:40:37 +08003394 CHECK(GetInferredRegCategoryMap(ref) != NULL);
3395}
3396
TDYa12789f96052012-07-12 20:49:53 -07003397const greenland::InferredRegCategoryMap*
Ian Rogers776ac1f2012-04-13 23:36:36 -07003398MethodVerifier::GetInferredRegCategoryMap(Compiler::MethodReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003399 MutexLock mu(Thread::Current(), *inferred_reg_category_maps_lock_);
Logan Chiendd361c92012-04-10 23:40:37 +08003400
3401 InferredRegCategoryMapTable::const_iterator it =
3402 inferred_reg_category_maps_->find(ref);
3403
3404 if (it == inferred_reg_category_maps_->end()) {
3405 return NULL;
3406 }
3407 CHECK(it->second != NULL);
3408 return it->second;
3409}
Logan Chienfca7e872011-12-20 20:08:22 +08003410#endif
3411
Ian Rogersd81871c2011-10-03 13:57:23 -07003412} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003413} // namespace art