blob: ce5129dc6138c353c84c16f76560f3a180fedbaa [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Ian Rogers776ac1f2012-04-13 23:36:36 -070017#include "method_verifier.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Brian Carlstrom1f870082011-08-23 16:02:11 -070021#include "class_linker.h"
Brian Carlstrome7d856b2012-01-11 18:10:55 -080022#include "compiler.h"
jeffhaob4df5142011-09-19 20:25:32 -070023#include "dex_cache.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070024#include "dex_file.h"
25#include "dex_instruction.h"
26#include "dex_instruction_visitor.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070027#include "gc_map.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070028#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070029#include "leb128.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070030#include "logging.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080031#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070032#include "runtime.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070033#include "stringpiece.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070034
Logan Chienfca7e872011-12-20 20:08:22 +080035#if defined(ART_USE_LLVM_COMPILER)
36#include "compiler_llvm/backend_types.h"
37#include "compiler_llvm/inferred_reg_category_map.h"
38using namespace art::compiler_llvm;
39#endif
40
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -070041#if defined(ART_USE_GREENLAND_COMPILER)
42#include "greenland/backend_types.h"
43#include "greenland/inferred_reg_category_map.h"
44using namespace art::greenland;
45#endif
46
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070047namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070048namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070049
Ian Rogers2c8a8572011-10-24 17:11:36 -070050static const bool gDebugVerify = false;
51
Ian Rogers776ac1f2012-04-13 23:36:36 -070052class InsnFlags {
53 public:
54 InsnFlags() : length_(0), flags_(0) {}
55
56 void SetLengthInCodeUnits(size_t length) {
57 CHECK_LT(length, 65536u);
58 length_ = length;
59 }
60 size_t GetLengthInCodeUnits() {
61 return length_;
62 }
63 bool IsOpcode() const {
64 return length_ != 0;
65 }
66
67 void SetInTry() {
68 flags_ |= 1 << kInTry;
69 }
70 void ClearInTry() {
71 flags_ &= ~(1 << kInTry);
72 }
73 bool IsInTry() const {
74 return (flags_ & (1 << kInTry)) != 0;
75 }
76
77 void SetBranchTarget() {
78 flags_ |= 1 << kBranchTarget;
79 }
80 void ClearBranchTarget() {
81 flags_ &= ~(1 << kBranchTarget);
82 }
83 bool IsBranchTarget() const {
84 return (flags_ & (1 << kBranchTarget)) != 0;
85 }
86
87 void SetGcPoint() {
88 flags_ |= 1 << kGcPoint;
89 }
90 void ClearGcPoint() {
91 flags_ &= ~(1 << kGcPoint);
92 }
93 bool IsGcPoint() const {
94 return (flags_ & (1 << kGcPoint)) != 0;
95 }
96
97 void SetVisited() {
98 flags_ |= 1 << kVisited;
99 }
100 void ClearVisited() {
101 flags_ &= ~(1 << kVisited);
102 }
103 bool IsVisited() const {
104 return (flags_ & (1 << kVisited)) != 0;
105 }
106
107 void SetChanged() {
108 flags_ |= 1 << kChanged;
109 }
110 void ClearChanged() {
111 flags_ &= ~(1 << kChanged);
112 }
113 bool IsChanged() const {
114 return (flags_ & (1 << kChanged)) != 0;
115 }
116
117 bool IsVisitedOrChanged() const {
118 return IsVisited() || IsChanged();
119 }
120
121 std::string Dump() {
122 char encoding[6];
123 if (!IsOpcode()) {
124 strncpy(encoding, "XXXXX", sizeof(encoding));
125 } else {
126 strncpy(encoding, "-----", sizeof(encoding));
127 if (IsInTry()) encoding[kInTry] = 'T';
128 if (IsBranchTarget()) encoding[kBranchTarget] = 'B';
129 if (IsGcPoint()) encoding[kGcPoint] = 'G';
130 if (IsVisited()) encoding[kVisited] = 'V';
131 if (IsChanged()) encoding[kChanged] = 'C';
132 }
133 return std::string(encoding);
134 }
Elliott Hughesa21039c2012-06-21 12:09:25 -0700135
Ian Rogers776ac1f2012-04-13 23:36:36 -0700136 private:
137 enum {
138 kInTry,
139 kBranchTarget,
140 kGcPoint,
141 kVisited,
142 kChanged,
143 };
144
145 // Size of instruction in code units
146 uint16_t length_;
147 uint8_t flags_;
Ian Rogers84fa0742011-10-25 18:13:30 -0700148};
Ian Rogersd81871c2011-10-03 13:57:23 -0700149
Ian Rogersd81871c2011-10-03 13:57:23 -0700150void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
151 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -0700152 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700153 DCHECK_GT(insns_size, 0U);
154
155 for (uint32_t i = 0; i < insns_size; i++) {
156 bool interesting = false;
157 switch (mode) {
158 case kTrackRegsAll:
159 interesting = flags[i].IsOpcode();
160 break;
161 case kTrackRegsGcPoints:
162 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
163 break;
164 case kTrackRegsBranches:
165 interesting = flags[i].IsBranchTarget();
166 break;
167 default:
168 break;
169 }
170 if (interesting) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700171 pc_to_register_line_.Put(i, new RegisterLine(registers_size, verifier));
Ian Rogersd81871c2011-10-03 13:57:23 -0700172 }
173 }
174}
175
jeffhaof1e6b7c2012-06-05 18:33:30 -0700176MethodVerifier::FailureKind MethodVerifier::VerifyClass(const Class* klass, std::string& error) {
jeffhaobdb76512011-09-07 11:43:16 -0700177 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700178 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700179 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700180 Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -0800181 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800182 error = "Verifier rejected class ";
183 error += PrettyDescriptor(klass);
184 error += " that has no super class";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700185 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -0700186 }
Ian Rogers1c5eb702012-02-01 09:18:34 -0800187 if (super != NULL && super->IsFinal()) {
188 error = "Verifier rejected class ";
189 error += PrettyDescriptor(klass);
190 error += " that attempts to sub-class final class ";
191 error += PrettyDescriptor(super);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700192 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -0700193 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700194 ClassHelper kh(klass);
195 const DexFile& dex_file = kh.GetDexFile();
196 uint32_t class_def_idx;
197 if (!dex_file.FindClassDefIndex(kh.GetDescriptor(), class_def_idx)) {
198 error = "Verifier rejected class ";
199 error += PrettyDescriptor(klass);
200 error += " that isn't present in dex file ";
201 error += dex_file.GetLocation();
jeffhaof1e6b7c2012-06-05 18:33:30 -0700202 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700203 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700204 return VerifyClass(&dex_file, kh.GetDexCache(), klass->GetClassLoader(), class_def_idx, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700205}
206
Ian Rogers365c1022012-06-22 15:05:28 -0700207MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
208 DexCache* dex_cache, ClassLoader* class_loader, uint32_t class_def_idx, std::string& error) {
jeffhaof56197c2012-03-05 18:01:54 -0800209 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
210 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700211 if (class_data == NULL) {
212 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700213 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700214 }
jeffhaof56197c2012-03-05 18:01:54 -0800215 ClassDataItemIterator it(*dex_file, class_data);
216 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
217 it.Next();
218 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700219 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700220 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700221 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaof56197c2012-03-05 18:01:54 -0800222 while (it.HasNextDirectMethod()) {
223 uint32_t method_idx = it.GetMemberIndex();
Ian Rogers08f753d2012-08-24 14:35:25 -0700224 InvokeType type = it.GetMethodInvokeType(class_def);
jeffhaoc0228b82012-08-29 18:15:05 -0700225 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700226 if (method == NULL) {
227 DCHECK(Thread::Current()->IsExceptionPending());
228 // We couldn't resolve the method, but continue regardless.
229 Thread::Current()->ClearException();
230 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700231 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
232 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags());
233 if (result != kNoFailure) {
234 if (result == kHardFailure) {
235 hard_fail = true;
236 if (error_count > 0) {
237 error += "\n";
238 }
239 error = "Verifier rejected class ";
240 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
241 error += " due to bad method ";
242 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700243 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700244 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800245 }
246 it.Next();
247 }
248 while (it.HasNextVirtualMethod()) {
249 uint32_t method_idx = it.GetMemberIndex();
Ian Rogers08f753d2012-08-24 14:35:25 -0700250 InvokeType type = it.GetMethodInvokeType(class_def);
jeffhaoc0228b82012-08-29 18:15:05 -0700251 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700252 if (method == NULL) {
253 DCHECK(Thread::Current()->IsExceptionPending());
254 // We couldn't resolve the method, but continue regardless.
255 Thread::Current()->ClearException();
256 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700257 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
258 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags());
259 if (result != kNoFailure) {
260 if (result == kHardFailure) {
261 hard_fail = true;
262 if (error_count > 0) {
263 error += "\n";
264 }
265 error = "Verifier rejected class ";
266 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
267 error += " due to bad method ";
268 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700269 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700270 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800271 }
272 it.Next();
273 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700274 if (error_count == 0) {
275 return kNoFailure;
276 } else {
277 return hard_fail ? kHardFailure : kSoftFailure;
278 }
jeffhaof56197c2012-03-05 18:01:54 -0800279}
280
jeffhaof1e6b7c2012-06-05 18:33:30 -0700281MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx, const DexFile* dex_file,
Ian Rogers365c1022012-06-22 15:05:28 -0700282 DexCache* dex_cache, ClassLoader* class_loader, uint32_t class_def_idx,
jeffhaof1e6b7c2012-06-05 18:33:30 -0700283 const DexFile::CodeItem* code_item, Method* method, uint32_t method_access_flags) {
Ian Rogersc8982582012-09-07 16:53:25 -0700284 MethodVerifier::FailureKind result = kNoFailure;
285 uint64_t start_ns = NanoTime();
286
Ian Rogersad0b3a32012-04-16 14:50:24 -0700287 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item, method_idx,
288 method, method_access_flags);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700289 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700290 // Verification completed, however failures may be pending that didn't cause the verification
291 // to hard fail.
Ian Rogerse551e952012-06-03 22:59:14 -0700292 CHECK(!verifier.have_pending_hard_failure_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700293 if (verifier.failures_.size() != 0) {
294 verifier.DumpFailures(LOG(INFO) << "Soft verification failures in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700295 << PrettyMethod(method_idx, *dex_file) << "\n");
Ian Rogersc8982582012-09-07 16:53:25 -0700296 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800297 }
298 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700299 // Bad method data.
300 CHECK_NE(verifier.failures_.size(), 0U);
301 CHECK(verifier.have_pending_hard_failure_);
302 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700303 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800304 if (gDebugVerify) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700305 std::cout << "\n" << verifier.info_messages_.str();
jeffhaof56197c2012-03-05 18:01:54 -0800306 verifier.Dump(std::cout);
307 }
Ian Rogersc8982582012-09-07 16:53:25 -0700308 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800309 }
Ian Rogersc8982582012-09-07 16:53:25 -0700310 uint64_t duration_ns = NanoTime() - start_ns;
311 if (duration_ns > MsToNs(100)) {
312 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
313 << " took " << PrettyDuration(duration_ns);
314 }
315 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800316}
317
Ian Rogersad0b3a32012-04-16 14:50:24 -0700318void MethodVerifier::VerifyMethodAndDump(Method* method) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800319 CHECK(method != NULL);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700320 MethodHelper mh(method);
321 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
322 mh.GetClassDefIndex(), mh.GetCodeItem(), method->GetDexMethodIndex(),
323 method, method->GetAccessFlags());
324 verifier.Verify();
Elliott Hughesc073b072012-05-24 19:29:17 -0700325 verifier.DumpFailures(LOG(INFO) << "Dump of method " << PrettyMethod(method) << "\n")
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700326 << verifier.info_messages_.str() << MutatorLockedDumpable<MethodVerifier>(verifier);
jeffhaoba5ebb92011-08-25 17:24:37 -0700327}
328
Ian Rogers776ac1f2012-04-13 23:36:36 -0700329MethodVerifier::MethodVerifier(const DexFile* dex_file, DexCache* dex_cache,
Ian Rogers365c1022012-06-22 15:05:28 -0700330 ClassLoader* class_loader, uint32_t class_def_idx, const DexFile::CodeItem* code_item,
Ian Rogersad0b3a32012-04-16 14:50:24 -0700331 uint32_t method_idx, Method* method, uint32_t method_access_flags)
jeffhaof56197c2012-03-05 18:01:54 -0800332 : work_insn_idx_(-1),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700333 method_idx_(method_idx),
334 foo_method_(method),
335 method_access_flags_(method_access_flags),
jeffhaof56197c2012-03-05 18:01:54 -0800336 dex_file_(dex_file),
337 dex_cache_(dex_cache),
338 class_loader_(class_loader),
339 class_def_idx_(class_def_idx),
340 code_item_(code_item),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700341 interesting_dex_pc_(-1),
342 monitor_enter_dex_pcs_(NULL),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700343 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700344 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800345 new_instance_count_(0),
346 monitor_enter_count_(0) {
347}
348
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700349void MethodVerifier::FindLocksAtDexPc(Method* m, uint32_t dex_pc, std::vector<uint32_t>& monitor_enter_dex_pcs) {
350 MethodHelper mh(m);
351 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
352 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
353 m, m->GetAccessFlags());
354 verifier.interesting_dex_pc_ = dex_pc;
355 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
356 verifier.FindLocksAtDexPc();
357}
358
359void MethodVerifier::FindLocksAtDexPc() {
360 CHECK(monitor_enter_dex_pcs_ != NULL);
361 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
362
363 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
364 // verification. In practice, the phase we want relies on data structures set up by all the
365 // earlier passes, so we just run the full method verification and bail out early when we've
366 // got what we wanted.
367 Verify();
368}
369
Ian Rogersad0b3a32012-04-16 14:50:24 -0700370bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700371 // If there aren't any instructions, make sure that's expected, then exit successfully.
372 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700373 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700374 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700375 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700376 } else {
377 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700378 }
jeffhaobdb76512011-09-07 11:43:16 -0700379 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700380 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
381 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700382 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
383 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700384 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700385 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700386 // Allocate and initialize an array to hold instruction data.
387 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
388 // Run through the instructions and see if the width checks out.
389 bool result = ComputeWidthsAndCountOps();
390 // Flag instructions guarded by a "try" block and check exception handlers.
391 result = result && ScanTryCatchBlocks();
392 // Perform static instruction verification.
393 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700394 // Perform code-flow analysis and return.
395 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700396}
397
Ian Rogers776ac1f2012-04-13 23:36:36 -0700398std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700399 switch (error) {
400 case VERIFY_ERROR_NO_CLASS:
401 case VERIFY_ERROR_NO_FIELD:
402 case VERIFY_ERROR_NO_METHOD:
403 case VERIFY_ERROR_ACCESS_CLASS:
404 case VERIFY_ERROR_ACCESS_FIELD:
405 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700406 case VERIFY_ERROR_INSTANTIATION:
407 case VERIFY_ERROR_CLASS_CHANGE:
jeffhaofaf459e2012-08-31 15:32:47 -0700408 if (Runtime::Current()->IsCompiler()) {
409 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
410 // class change and instantiation errors into soft verification errors so that we re-verify
411 // at runtime. We may fail to find or to agree on access because of not yet available class
412 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
413 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
414 // paths" that dynamically perform the verification and cause the behavior to be that akin
415 // to an interpreter.
416 error = VERIFY_ERROR_BAD_CLASS_SOFT;
417 } else {
418 have_pending_runtime_throw_failure_ = true;
419 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700420 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700421 // Indication that verification should be retried at runtime.
422 case VERIFY_ERROR_BAD_CLASS_SOFT:
423 if (!Runtime::Current()->IsCompiler()) {
424 // It is runtime so hard fail.
425 have_pending_hard_failure_ = true;
426 }
427 break;
jeffhaod5347e02012-03-22 17:25:05 -0700428 // Hard verification failures at compile time will still fail at runtime, so the class is
429 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700430 case VERIFY_ERROR_BAD_CLASS_HARD: {
431 if (Runtime::Current()->IsCompiler()) {
jeffhaof56197c2012-03-05 18:01:54 -0800432 Compiler::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800433 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800434 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700435 have_pending_hard_failure_ = true;
436 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800437 }
438 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700439 failures_.push_back(error);
440 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(method_idx_, *dex_file_).c_str(),
441 work_insn_idx_));
442 std::ostringstream* failure_message = new std::ostringstream(location);
443 failure_messages_.push_back(failure_message);
444 return *failure_message;
445}
446
447void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
448 size_t failure_num = failure_messages_.size();
449 DCHECK_NE(failure_num, 0U);
450 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
451 prepend += last_fail_message->str();
452 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
453 delete last_fail_message;
454}
455
456void MethodVerifier::AppendToLastFailMessage(std::string append) {
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 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800461}
462
Ian Rogers776ac1f2012-04-13 23:36:36 -0700463bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700464 const uint16_t* insns = code_item_->insns_;
465 size_t insns_size = code_item_->insns_size_in_code_units_;
466 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700467 size_t new_instance_count = 0;
468 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700469 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700470
Ian Rogersd81871c2011-10-03 13:57:23 -0700471 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700472 Instruction::Code opcode = inst->Opcode();
473 if (opcode == Instruction::NEW_INSTANCE) {
474 new_instance_count++;
475 } else if (opcode == Instruction::MONITOR_ENTER) {
476 monitor_enter_count++;
477 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700478 size_t inst_size = inst->SizeInCodeUnits();
479 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
480 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700481 inst = inst->Next();
482 }
483
Ian Rogersd81871c2011-10-03 13:57:23 -0700484 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700485 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
486 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700487 return false;
488 }
489
Ian Rogersd81871c2011-10-03 13:57:23 -0700490 new_instance_count_ = new_instance_count;
491 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700492 return true;
493}
494
Ian Rogers776ac1f2012-04-13 23:36:36 -0700495bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700496 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700497 if (tries_size == 0) {
498 return true;
499 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700500 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700501 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700502
503 for (uint32_t idx = 0; idx < tries_size; idx++) {
504 const DexFile::TryItem* try_item = &tries[idx];
505 uint32_t start = try_item->start_addr_;
506 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700507 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700508 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
509 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700510 return false;
511 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700512 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700513 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700514 return false;
515 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700516 for (uint32_t dex_pc = start; dex_pc < end;
517 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
518 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700519 }
520 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800521 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700522 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700523 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700524 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700525 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700526 CatchHandlerIterator iterator(handlers_ptr);
527 for (; iterator.HasNext(); iterator.Next()) {
528 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700529 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700530 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700531 return false;
532 }
jeffhao60f83e32012-02-13 17:16:30 -0800533 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
534 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700535 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700536 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800537 return false;
538 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700539 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700540 // Ensure exception types are resolved so that they don't need resolution to be delivered,
541 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700542 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
jeffhaof56197c2012-03-05 18:01:54 -0800543 Class* exception_type = linker->ResolveType(*dex_file_, iterator.GetHandlerTypeIndex(),
544 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700545 if (exception_type == NULL) {
546 DCHECK(Thread::Current()->IsExceptionPending());
547 Thread::Current()->ClearException();
548 }
549 }
jeffhaobdb76512011-09-07 11:43:16 -0700550 }
Ian Rogers0571d352011-11-03 19:51:38 -0700551 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700552 }
jeffhaobdb76512011-09-07 11:43:16 -0700553 return true;
554}
555
Ian Rogers776ac1f2012-04-13 23:36:36 -0700556bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700557 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700558
Ian Rogersd81871c2011-10-03 13:57:23 -0700559 /* Flag the start of the method as a branch target. */
560 insn_flags_[0].SetBranchTarget();
561
562 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700563 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700564 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700565 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700566 return false;
567 }
568 /* Flag instructions that are garbage collection points */
569 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
570 insn_flags_[dex_pc].SetGcPoint();
571 }
572 dex_pc += inst->SizeInCodeUnits();
573 inst = inst->Next();
574 }
575 return true;
576}
577
Ian Rogers776ac1f2012-04-13 23:36:36 -0700578bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800579 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700580 bool result = true;
581 switch (inst->GetVerifyTypeArgumentA()) {
582 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800583 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700584 break;
585 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800586 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700587 break;
588 }
589 switch (inst->GetVerifyTypeArgumentB()) {
590 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800591 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700592 break;
593 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800594 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700595 break;
596 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800597 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700598 break;
599 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800600 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700601 break;
602 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800603 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700604 break;
605 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800606 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700607 break;
608 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800609 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700610 break;
611 }
612 switch (inst->GetVerifyTypeArgumentC()) {
613 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800614 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700615 break;
616 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800617 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700618 break;
619 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800620 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700621 break;
622 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800623 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700624 break;
625 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800626 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700627 break;
628 }
629 switch (inst->GetVerifyExtraFlags()) {
630 case Instruction::kVerifyArrayData:
631 result = result && CheckArrayData(code_offset);
632 break;
633 case Instruction::kVerifyBranchTarget:
634 result = result && CheckBranchTarget(code_offset);
635 break;
636 case Instruction::kVerifySwitchTargets:
637 result = result && CheckSwitchTargets(code_offset);
638 break;
639 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800640 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700641 break;
642 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800643 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700644 break;
645 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700646 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700647 result = false;
648 break;
649 }
650 return result;
651}
652
Ian Rogers776ac1f2012-04-13 23:36:36 -0700653bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700654 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700655 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
656 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700657 return false;
658 }
659 return true;
660}
661
Ian Rogers776ac1f2012-04-13 23:36:36 -0700662bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700663 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700664 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
665 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700666 return false;
667 }
668 return true;
669}
670
Ian Rogers776ac1f2012-04-13 23:36:36 -0700671bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700672 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700673 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
674 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700675 return false;
676 }
677 return true;
678}
679
Ian Rogers776ac1f2012-04-13 23:36:36 -0700680bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700681 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700682 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
683 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700684 return false;
685 }
686 return true;
687}
688
Ian Rogers776ac1f2012-04-13 23:36:36 -0700689bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700690 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700691 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
692 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700693 return false;
694 }
695 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700696 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700697 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700698 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700699 return false;
700 }
701 return true;
702}
703
Ian Rogers776ac1f2012-04-13 23:36:36 -0700704bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700705 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700706 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
707 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700708 return false;
709 }
710 return true;
711}
712
Ian Rogers776ac1f2012-04-13 23:36:36 -0700713bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700714 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700715 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
716 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700717 return false;
718 }
719 return true;
720}
721
Ian Rogers776ac1f2012-04-13 23:36:36 -0700722bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700723 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
725 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700726 return false;
727 }
728 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700729 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700730 const char* cp = descriptor;
731 while (*cp++ == '[') {
732 bracket_count++;
733 }
734 if (bracket_count == 0) {
735 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700736 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700737 return false;
738 } else if (bracket_count > 255) {
739 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700740 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700741 return false;
742 }
743 return true;
744}
745
Ian Rogers776ac1f2012-04-13 23:36:36 -0700746bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700747 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
748 const uint16_t* insns = code_item_->insns_ + cur_offset;
749 const uint16_t* array_data;
750 int32_t array_data_offset;
751
752 DCHECK_LT(cur_offset, insn_count);
753 /* make sure the start of the array data table is in range */
754 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
755 if ((int32_t) cur_offset + array_data_offset < 0 ||
756 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700757 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
758 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700759 return false;
760 }
761 /* offset to array data table is a relative branch-style offset */
762 array_data = insns + array_data_offset;
763 /* make sure the table is 32-bit aligned */
764 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700765 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
766 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700767 return false;
768 }
769 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700770 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700771 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
772 /* make sure the end of the switch is in range */
773 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700774 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
775 << ", data offset " << array_data_offset << ", end "
776 << cur_offset + array_data_offset + table_size
777 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700778 return false;
779 }
780 return true;
781}
782
Ian Rogers776ac1f2012-04-13 23:36:36 -0700783bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700784 int32_t offset;
785 bool isConditional, selfOkay;
786 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
787 return false;
788 }
789 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700790 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 -0700791 return false;
792 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700793 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
794 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700795 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700796 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700797 return false;
798 }
799 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
800 int32_t abs_offset = cur_offset + offset;
801 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700802 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700803 << reinterpret_cast<void*>(abs_offset) << ") at "
804 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700805 return false;
806 }
807 insn_flags_[abs_offset].SetBranchTarget();
808 return true;
809}
810
Ian Rogers776ac1f2012-04-13 23:36:36 -0700811bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700812 bool* selfOkay) {
813 const uint16_t* insns = code_item_->insns_ + cur_offset;
814 *pConditional = false;
815 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700816 switch (*insns & 0xff) {
817 case Instruction::GOTO:
818 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700819 break;
820 case Instruction::GOTO_32:
821 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700822 *selfOkay = true;
823 break;
824 case Instruction::GOTO_16:
825 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700826 break;
827 case Instruction::IF_EQ:
828 case Instruction::IF_NE:
829 case Instruction::IF_LT:
830 case Instruction::IF_GE:
831 case Instruction::IF_GT:
832 case Instruction::IF_LE:
833 case Instruction::IF_EQZ:
834 case Instruction::IF_NEZ:
835 case Instruction::IF_LTZ:
836 case Instruction::IF_GEZ:
837 case Instruction::IF_GTZ:
838 case Instruction::IF_LEZ:
839 *pOffset = (int16_t) insns[1];
840 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700841 break;
842 default:
843 return false;
844 break;
845 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700846 return true;
847}
848
Ian Rogers776ac1f2012-04-13 23:36:36 -0700849bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700850 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700851 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700852 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700853 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700854 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
855 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700856 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
857 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700858 return false;
859 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700860 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700861 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700862 /* make sure the table is 32-bit aligned */
863 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700864 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
865 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700866 return false;
867 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700868 uint32_t switch_count = switch_insns[1];
869 int32_t keys_offset, targets_offset;
870 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700871 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
872 /* 0=sig, 1=count, 2/3=firstKey */
873 targets_offset = 4;
874 keys_offset = -1;
875 expected_signature = Instruction::kPackedSwitchSignature;
876 } else {
877 /* 0=sig, 1=count, 2..count*2 = keys */
878 keys_offset = 2;
879 targets_offset = 2 + 2 * switch_count;
880 expected_signature = Instruction::kSparseSwitchSignature;
881 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700882 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700883 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700884 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
885 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700886 return false;
887 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700888 /* make sure the end of the switch is in range */
889 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700890 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
891 << switch_offset << ", end "
892 << (cur_offset + switch_offset + table_size)
893 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700894 return false;
895 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700896 /* for a sparse switch, verify the keys are in ascending order */
897 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700898 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
899 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700900 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
901 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
902 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700903 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
904 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700905 return false;
906 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700907 last_key = key;
908 }
909 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700910 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700911 for (uint32_t targ = 0; targ < switch_count; targ++) {
912 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
913 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
914 int32_t abs_offset = cur_offset + offset;
915 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700916 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700917 << reinterpret_cast<void*>(abs_offset) << ") at "
918 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700919 return false;
920 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700921 insn_flags_[abs_offset].SetBranchTarget();
922 }
923 return true;
924}
925
Ian Rogers776ac1f2012-04-13 23:36:36 -0700926bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700927 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700928 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700929 return false;
930 }
931 uint16_t registers_size = code_item_->registers_size_;
932 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800933 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700934 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
935 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700936 return false;
937 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700938 }
939
940 return true;
941}
942
Ian Rogers776ac1f2012-04-13 23:36:36 -0700943bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 uint16_t registers_size = code_item_->registers_size_;
945 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
946 // integer overflow when adding them here.
947 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700948 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
949 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700950 return false;
951 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700952 return true;
953}
954
Brian Carlstrom75412882012-01-18 01:26:54 -0800955const std::vector<uint8_t>* CreateLengthPrefixedGcMap(const std::vector<uint8_t>& gc_map) {
956 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
957 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
958 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
959 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
960 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
961 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
962 gc_map.begin(),
963 gc_map.end());
964 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
965 DCHECK_EQ(gc_map.size(),
966 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
967 (length_prefixed_gc_map->at(1) << 16) |
968 (length_prefixed_gc_map->at(2) << 8) |
969 (length_prefixed_gc_map->at(3) << 0)));
970 return length_prefixed_gc_map;
971}
972
Ian Rogers776ac1f2012-04-13 23:36:36 -0700973bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 uint16_t registers_size = code_item_->registers_size_;
975 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -0700976
Ian Rogersd81871c2011-10-03 13:57:23 -0700977 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -0800978 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
979 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 }
981 /* Create and initialize table holding register status */
Elliott Hughes460384f2012-04-04 16:53:10 -0700982 reg_table_.Init(kTrackRegsGcPoints, insn_flags_.get(), insns_size, registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -0700983
Ian Rogersd81871c2011-10-03 13:57:23 -0700984 work_line_.reset(new RegisterLine(registers_size, this));
985 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -0700986
Ian Rogersd81871c2011-10-03 13:57:23 -0700987 /* Initialize register types of method arguments. */
988 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700989 DCHECK_NE(failures_.size(), 0U);
990 std::string prepend("Bad signature in ");
991 prepend += PrettyMethod(method_idx_, *dex_file_);
992 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -0700993 return false;
994 }
995 /* Perform code flow verification. */
996 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700997 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700999 }
1000
TDYa127b2eb5c12012-05-24 15:52:10 -07001001 Compiler::MethodReference ref(dex_file_, method_idx_);
1002
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -07001003#if !defined(ART_USE_LLVM_COMPILER) && !defined(ART_USE_GREENLAND_COMPILER)
TDYa127b2eb5c12012-05-24 15:52:10 -07001004
Ian Rogersd81871c2011-10-03 13:57:23 -07001005 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -08001006 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1007 if (map.get() == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001008 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001009 return false; // Not a real failure, but a failure to encode
1010 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001011#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001012 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -07001013#endif
Brian Carlstrom75412882012-01-18 01:26:54 -08001014 const std::vector<uint8_t>* gc_map = CreateLengthPrefixedGcMap(*(map.get()));
Ian Rogers776ac1f2012-04-13 23:36:36 -07001015 verifier::MethodVerifier::SetGcMap(ref, *gc_map);
Logan Chienfca7e872011-12-20 20:08:22 +08001016
Ian Rogersad0b3a32012-04-16 14:50:24 -07001017 if (foo_method_ != NULL) {
1018 foo_method_->SetGcMap(&gc_map->at(0));
1019 }
Logan Chiendd361c92012-04-10 23:40:37 +08001020
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -07001021#else // defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Logan Chienfca7e872011-12-20 20:08:22 +08001022 /* Generate Inferred Register Category for LLVM-based Code Generator */
1023 const InferredRegCategoryMap* table = GenerateInferredRegCategoryMap();
Ian Rogers776ac1f2012-04-13 23:36:36 -07001024 verifier::MethodVerifier::SetInferredRegCategoryMap(ref, *table);
TDYa127b2eb5c12012-05-24 15:52:10 -07001025
Logan Chienfca7e872011-12-20 20:08:22 +08001026#endif
1027
jeffhaobdb76512011-09-07 11:43:16 -07001028 return true;
1029}
1030
Ian Rogersad0b3a32012-04-16 14:50:24 -07001031std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1032 DCHECK_EQ(failures_.size(), failure_messages_.size());
1033 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001034 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001035 }
1036 return os;
1037}
1038
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001039extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001040 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001041 v->Dump(std::cerr);
1042}
1043
Ian Rogers776ac1f2012-04-13 23:36:36 -07001044void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001045 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001046 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001047 return;
jeffhaobdb76512011-09-07 11:43:16 -07001048 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001049 DCHECK(code_item_ != NULL);
1050 const Instruction* inst = Instruction::At(code_item_->insns_);
1051 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1052 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Elliott Hughesaa6e1cd2012-01-18 19:26:06 -08001053 os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
Elliott Hughesc073b072012-05-24 19:29:17 -07001054 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001055 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1056 if (reg_line != NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001057 os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001058 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001059 inst = inst->Next();
1060 }
jeffhaobdb76512011-09-07 11:43:16 -07001061}
1062
Ian Rogersd81871c2011-10-03 13:57:23 -07001063static bool IsPrimitiveDescriptor(char descriptor) {
1064 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001065 case 'I':
1066 case 'C':
1067 case 'S':
1068 case 'B':
1069 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001070 case 'F':
1071 case 'D':
1072 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001073 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001074 default:
1075 return false;
1076 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001077}
1078
Ian Rogers776ac1f2012-04-13 23:36:36 -07001079bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001080 RegisterLine* reg_line = reg_table_.GetLine(0);
1081 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1082 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001083
Ian Rogersd81871c2011-10-03 13:57:23 -07001084 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1085 //Include the "this" pointer.
1086 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001087 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001088 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1089 // argument as uninitialized. This restricts field access until the superclass constructor is
1090 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001091 const RegType& declaring_class = GetDeclaringClass();
1092 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001093 reg_line->SetRegisterType(arg_start + cur_arg,
1094 reg_types_.UninitializedThisArgument(declaring_class));
1095 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001096 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001097 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001098 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001099 }
1100
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001101 const DexFile::ProtoId& proto_id =
Ian Rogersad0b3a32012-04-16 14:50:24 -07001102 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001103 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001104
1105 for (; iterator.HasNext(); iterator.Next()) {
1106 const char* descriptor = iterator.GetDescriptor();
1107 if (descriptor == NULL) {
1108 LOG(FATAL) << "Null descriptor";
1109 }
1110 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001111 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1112 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001113 return false;
1114 }
1115 switch (descriptor[0]) {
1116 case 'L':
1117 case '[':
1118 // We assume that reference arguments are initialized. The only way it could be otherwise
1119 // (assuming the caller was verified) is if the current method is <init>, but in that case
1120 // it's effectively considered initialized the instant we reach here (in the sense that we
1121 // can return without doing anything or call virtual methods).
1122 {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001123 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001124 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001125 }
1126 break;
1127 case 'Z':
1128 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1129 break;
1130 case 'C':
1131 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1132 break;
1133 case 'B':
1134 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1135 break;
1136 case 'I':
1137 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1138 break;
1139 case 'S':
1140 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1141 break;
1142 case 'F':
1143 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1144 break;
1145 case 'J':
1146 case 'D': {
1147 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1148 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1149 cur_arg++;
1150 break;
1151 }
1152 default:
jeffhaod5347e02012-03-22 17:25:05 -07001153 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001154 return false;
1155 }
1156 cur_arg++;
1157 }
1158 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001159 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001160 return false;
1161 }
1162 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1163 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1164 // format. Only major difference from the method argument format is that 'V' is supported.
1165 bool result;
1166 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1167 result = descriptor[1] == '\0';
1168 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1169 size_t i = 0;
1170 do {
1171 i++;
1172 } while (descriptor[i] == '['); // process leading [
1173 if (descriptor[i] == 'L') { // object array
1174 do {
1175 i++; // find closing ;
1176 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1177 result = descriptor[i] == ';';
1178 } else { // primitive array
1179 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1180 }
1181 } else if (descriptor[0] == 'L') {
1182 // could be more thorough here, but shouldn't be required
1183 size_t i = 0;
1184 do {
1185 i++;
1186 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1187 result = descriptor[i] == ';';
1188 } else {
1189 result = false;
1190 }
1191 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001192 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1193 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001194 }
1195 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001196}
1197
Ian Rogers776ac1f2012-04-13 23:36:36 -07001198bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001199 const uint16_t* insns = code_item_->insns_;
1200 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001201
jeffhaobdb76512011-09-07 11:43:16 -07001202 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001203 insn_flags_[0].SetChanged();
1204 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001205
jeffhaobdb76512011-09-07 11:43:16 -07001206 /* Continue until no instructions are marked "changed". */
1207 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001208 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1209 uint32_t insn_idx = start_guess;
1210 for (; insn_idx < insns_size; insn_idx++) {
1211 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001212 break;
1213 }
jeffhaobdb76512011-09-07 11:43:16 -07001214 if (insn_idx == insns_size) {
1215 if (start_guess != 0) {
1216 /* try again, starting from the top */
1217 start_guess = 0;
1218 continue;
1219 } else {
1220 /* all flags are clear */
1221 break;
1222 }
1223 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001224 // We carry the working set of registers from instruction to instruction. If this address can
1225 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1226 // "changed" flags, we need to load the set of registers from the table.
1227 // Because we always prefer to continue on to the next instruction, we should never have a
1228 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1229 // target.
1230 work_insn_idx_ = insn_idx;
1231 if (insn_flags_[insn_idx].IsBranchTarget()) {
1232 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001233 } else {
1234#ifndef NDEBUG
1235 /*
1236 * Sanity check: retrieve the stored register line (assuming
1237 * a full table) and make sure it actually matches.
1238 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001239 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1240 if (register_line != NULL) {
1241 if (work_line_->CompareLine(register_line) != 0) {
1242 Dump(std::cout);
1243 std::cout << info_messages_.str();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001244 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001245 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1246 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001247 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001248 }
jeffhaobdb76512011-09-07 11:43:16 -07001249 }
1250#endif
1251 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001252 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001253 std::string prepend(PrettyMethod(method_idx_, *dex_file_));
1254 prepend += " failed to verify: ";
1255 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001256 return false;
1257 }
jeffhaobdb76512011-09-07 11:43:16 -07001258 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001259 insn_flags_[insn_idx].SetVisited();
1260 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001261 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001262
Ian Rogers1c849e52012-06-28 14:00:33 -07001263 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001264 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001265 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001266 * (besides the wasted space), but it indicates a flaw somewhere
1267 * down the line, possibly in the verifier.
1268 *
1269 * If we've substituted "always throw" instructions into the stream,
1270 * we are almost certainly going to have some dead code.
1271 */
1272 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001273 uint32_t insn_idx = 0;
1274 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001275 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001276 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001277 * may or may not be preceded by a padding NOP (for alignment).
1278 */
1279 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1280 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1281 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001282 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001283 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1284 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1285 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001286 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001287 }
1288
Ian Rogersd81871c2011-10-03 13:57:23 -07001289 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001290 if (dead_start < 0)
1291 dead_start = insn_idx;
1292 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001293 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001294 dead_start = -1;
1295 }
1296 }
1297 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001298 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001299 }
1300 }
jeffhaobdb76512011-09-07 11:43:16 -07001301 return true;
1302}
1303
Ian Rogers776ac1f2012-04-13 23:36:36 -07001304bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001305#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001306 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001307 gDvm.verifierStats.instrsReexamined++;
1308 } else {
1309 gDvm.verifierStats.instrsExamined++;
1310 }
1311#endif
1312
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001313 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1314 // We want the state _before_ the instruction, for the case where the dex pc we're
1315 // interested in is itself a monitor-enter instruction (which is a likely place
1316 // for a thread to be suspended).
1317 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
1318 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1319 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1320 }
1321 }
1322
jeffhaobdb76512011-09-07 11:43:16 -07001323 /*
1324 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001325 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001326 * control to another statement:
1327 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001328 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001329 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001330 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001331 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001332 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001333 * throw an exception that is handled by an encompassing "try"
1334 * block.
1335 *
1336 * We can also return, in which case there is no successor instruction
1337 * from this point.
1338 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001339 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001340 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001341 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1342 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001343 DecodedInstruction dec_insn(inst);
1344 int opcode_flags = Instruction::Flags(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001345
jeffhaobdb76512011-09-07 11:43:16 -07001346 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001347 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001348 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001349 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001350 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1351 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001352 }
jeffhaobdb76512011-09-07 11:43:16 -07001353
1354 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001355 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001356 * can throw an exception, we will copy/merge this into the "catch"
1357 * address rather than work_line, because we don't want the result
1358 * from the "successful" code path (e.g. a check-cast that "improves"
1359 * a type) to be visible to the exception handler.
1360 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001361 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001362 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001363 } else {
1364#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001365 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001366#endif
1367 }
1368
Elliott Hughesadb8c672012-03-06 16:49:32 -08001369 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07001370 case Instruction::NOP:
1371 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001372 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001373 * a signature that looks like a NOP; if we see one of these in
1374 * the course of executing code then we have a problem.
1375 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001376 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001377 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001378 }
1379 break;
1380
1381 case Instruction::MOVE:
1382 case Instruction::MOVE_FROM16:
1383 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001384 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001385 break;
1386 case Instruction::MOVE_WIDE:
1387 case Instruction::MOVE_WIDE_FROM16:
1388 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001389 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07001390 break;
1391 case Instruction::MOVE_OBJECT:
1392 case Instruction::MOVE_OBJECT_FROM16:
1393 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001394 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001395 break;
1396
1397 /*
1398 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001399 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001400 * might want to hold the result in an actual CPU register, so the
1401 * Dalvik spec requires that these only appear immediately after an
1402 * invoke or filled-new-array.
1403 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001404 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001405 * redundant with the reset done below, but it can make the debug info
1406 * easier to read in some cases.)
1407 */
1408 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001409 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07001410 break;
1411 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001412 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001413 break;
1414 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001415 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07001416 break;
1417
Ian Rogersd81871c2011-10-03 13:57:23 -07001418 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001419 /*
jeffhao60f83e32012-02-13 17:16:30 -08001420 * This statement can only appear as the first instruction in an exception handler. We verify
1421 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001422 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001423 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08001424 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001425 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001426 }
jeffhaobdb76512011-09-07 11:43:16 -07001427 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001428 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1429 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001430 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001431 }
jeffhaobdb76512011-09-07 11:43:16 -07001432 }
1433 break;
1434 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001435 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001436 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001437 const RegType& return_type = GetMethodReturnType();
1438 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001439 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001440 } else {
1441 // Compilers may generate synthetic functions that write byte values into boolean fields.
1442 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001443 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001444 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1445 ((return_type.IsBoolean() || return_type.IsByte() ||
1446 return_type.IsShort() || return_type.IsChar()) &&
1447 src_type.IsInteger()));
1448 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001449 bool success =
1450 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
1451 if (!success) {
1452 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001453 }
jeffhaobdb76512011-09-07 11:43:16 -07001454 }
1455 }
1456 break;
1457 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001458 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001459 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001460 const RegType& return_type = GetMethodReturnType();
1461 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001462 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001463 } else {
1464 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001465 bool success = work_line_->VerifyRegisterType(dec_insn.vA, return_type);
1466 if (!success) {
1467 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001468 }
jeffhaobdb76512011-09-07 11:43:16 -07001469 }
1470 }
1471 break;
1472 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001473 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001474 const RegType& return_type = GetMethodReturnType();
1475 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001476 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001477 } else {
1478 /* return_type is the *expected* return type, not register value */
1479 DCHECK(!return_type.IsZero());
1480 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08001481 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07001482 // Disallow returning uninitialized values and verify that the reference in vAA is an
1483 // instance of the "return_type"
1484 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001485 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001486 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001487 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1488 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001489 }
1490 }
1491 }
1492 break;
1493
1494 case Instruction::CONST_4:
1495 case Instruction::CONST_16:
1496 case Instruction::CONST:
1497 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001498 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const((int32_t) dec_insn.vB));
jeffhaobdb76512011-09-07 11:43:16 -07001499 break;
1500 case Instruction::CONST_HIGH16:
1501 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001502 work_line_->SetRegisterType(dec_insn.vA,
1503 reg_types_.FromCat1Const((int32_t) dec_insn.vB << 16));
jeffhaobdb76512011-09-07 11:43:16 -07001504 break;
1505 case Instruction::CONST_WIDE_16:
1506 case Instruction::CONST_WIDE_32:
1507 case Instruction::CONST_WIDE:
1508 case Instruction::CONST_WIDE_HIGH16:
1509 /* could be long or double; resolved upon use */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001510 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07001511 break;
1512 case Instruction::CONST_STRING:
1513 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001514 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001515 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001516 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001517 // Get type from instruction if unresolved then we need an access check
1518 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08001519 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001520 // Register holds class, ie its type is class, on error it will hold Conflict.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001521 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogersad0b3a32012-04-16 14:50:24 -07001522 res_type.IsConflict() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001523 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001524 }
jeffhaobdb76512011-09-07 11:43:16 -07001525 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001526 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001527 break;
1528 case Instruction::MONITOR_EXIT:
1529 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001530 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001531 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001532 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001533 * to the need to handle asynchronous exceptions, a now-deprecated
1534 * feature that Dalvik doesn't support.)
1535 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001536 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001537 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001538 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001539 * structured locking checks are working, the former would have
1540 * failed on the -enter instruction, and the latter is impossible.
1541 *
1542 * This is fortunate, because issue 3221411 prevents us from
1543 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001544 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001545 * some catch blocks (which will show up as "dead" code when
1546 * we skip them here); if we can't, then the code path could be
1547 * "live" so we still need to check it.
1548 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001549 opcode_flags &= ~Instruction::kThrow;
1550 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001551 break;
1552
Ian Rogers28ad40d2011-10-27 15:19:26 -07001553 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001554 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001555 /*
1556 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1557 * could be a "upcast" -- not expected, so we don't try to address it.)
1558 *
1559 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001560 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001561 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001562 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001563 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001564 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001565 if (res_type.IsConflict()) {
1566 DCHECK_NE(failures_.size(), 0U);
1567 if (!is_checkcast) {
1568 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
1569 }
1570 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001571 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001572 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1573 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001574 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001575 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001576 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001577 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001578 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07001579 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001580 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001581 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001582 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001583 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001584 }
jeffhaobdb76512011-09-07 11:43:16 -07001585 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001586 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001587 }
1588 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001589 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001590 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001591 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001592 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001593 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001594 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001595 }
1596 }
1597 break;
1598 }
1599 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001600 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001601 if (res_type.IsConflict()) {
1602 DCHECK_NE(failures_.size(), 0U);
1603 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001604 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001605 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1606 // can't create an instance of an interface or abstract class */
1607 if (!res_type.IsInstantiableTypes()) {
1608 Fail(VERIFY_ERROR_INSTANTIATION)
1609 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001610 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001611 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001612 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1613 // Any registers holding previous allocations from this address that have not yet been
1614 // initialized must be marked invalid.
1615 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1616 // add the new uninitialized reference to the register state
1617 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001618 break;
1619 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001620 case Instruction::NEW_ARRAY:
1621 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001622 break;
1623 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08001624 VerifyNewArray(dec_insn, true, false);
1625 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001626 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001627 case Instruction::FILLED_NEW_ARRAY_RANGE:
1628 VerifyNewArray(dec_insn, true, true);
1629 just_set_result = true; // Filled new array range sets result register
1630 break;
jeffhaobdb76512011-09-07 11:43:16 -07001631 case Instruction::CMPL_FLOAT:
1632 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001633 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001634 break;
1635 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001636 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001637 break;
1638 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001639 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001640 break;
1641 case Instruction::CMPL_DOUBLE:
1642 case Instruction::CMPG_DOUBLE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001643 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Double())) {
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_.Double())) {
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::CMP_LONG:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001652 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08001653 break;
1654 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001655 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08001656 break;
1657 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001658 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001659 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001660 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001661 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001662 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001663 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001664 }
1665 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001666 }
jeffhaobdb76512011-09-07 11:43:16 -07001667 case Instruction::GOTO:
1668 case Instruction::GOTO_16:
1669 case Instruction::GOTO_32:
1670 /* no effect on or use of registers */
1671 break;
1672
1673 case Instruction::PACKED_SWITCH:
1674 case Instruction::SPARSE_SWITCH:
1675 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001676 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001677 break;
1678
Ian Rogersd81871c2011-10-03 13:57:23 -07001679 case Instruction::FILL_ARRAY_DATA: {
1680 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001681 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08001682 /* array_type can be null if the reg type is Zero */
1683 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001684 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001685 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001686 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001687 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1688 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001689 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001690 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1691 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001692 } else {
jeffhao457cc512012-02-02 16:55:13 -08001693 // Now verify if the element width in the table matches the element width declared in
1694 // the array
1695 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1696 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001697 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001698 } else {
1699 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1700 // Since we don't compress the data in Dex, expect to see equal width of data stored
1701 // in the table and expected from the array class.
1702 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001703 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1704 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001705 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001706 }
1707 }
jeffhaobdb76512011-09-07 11:43:16 -07001708 }
1709 }
1710 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001711 }
jeffhaobdb76512011-09-07 11:43:16 -07001712 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001713 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001714 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1715 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001716 bool mismatch = false;
1717 if (reg_type1.IsZero()) { // zero then integral or reference expected
1718 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1719 } else if (reg_type1.IsReferenceTypes()) { // both references?
1720 mismatch = !reg_type2.IsReferenceTypes();
1721 } else { // both integral?
1722 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1723 }
1724 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001725 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1726 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001727 }
1728 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001729 }
jeffhaobdb76512011-09-07 11:43:16 -07001730 case Instruction::IF_LT:
1731 case Instruction::IF_GE:
1732 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001733 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001734 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1735 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001736 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001737 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1738 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001739 }
1740 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001741 }
jeffhaobdb76512011-09-07 11:43:16 -07001742 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001743 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001744 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001745 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001746 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001747 }
jeffhaobdb76512011-09-07 11:43:16 -07001748 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001749 }
jeffhaobdb76512011-09-07 11:43:16 -07001750 case Instruction::IF_LTZ:
1751 case Instruction::IF_GEZ:
1752 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001753 case Instruction::IF_LEZ: {
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.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001756 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1757 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001758 }
jeffhaobdb76512011-09-07 11:43:16 -07001759 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001760 }
jeffhaobdb76512011-09-07 11:43:16 -07001761 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001762 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
1763 break;
jeffhaobdb76512011-09-07 11:43:16 -07001764 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001765 VerifyAGet(dec_insn, reg_types_.Byte(), true);
1766 break;
jeffhaobdb76512011-09-07 11:43:16 -07001767 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07001768 VerifyAGet(dec_insn, reg_types_.Char(), true);
1769 break;
jeffhaobdb76512011-09-07 11:43:16 -07001770 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001771 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001772 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001773 case Instruction::AGET:
1774 VerifyAGet(dec_insn, reg_types_.Integer(), true);
1775 break;
jeffhaobdb76512011-09-07 11:43:16 -07001776 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001777 VerifyAGet(dec_insn, reg_types_.Long(), true);
1778 break;
1779 case Instruction::AGET_OBJECT:
1780 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001781 break;
1782
Ian Rogersd81871c2011-10-03 13:57:23 -07001783 case Instruction::APUT_BOOLEAN:
1784 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
1785 break;
1786 case Instruction::APUT_BYTE:
1787 VerifyAPut(dec_insn, reg_types_.Byte(), true);
1788 break;
1789 case Instruction::APUT_CHAR:
1790 VerifyAPut(dec_insn, reg_types_.Char(), true);
1791 break;
1792 case Instruction::APUT_SHORT:
1793 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001794 break;
1795 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001796 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001797 break;
1798 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001799 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001800 break;
1801 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001802 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001803 break;
1804
jeffhaobdb76512011-09-07 11:43:16 -07001805 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001806 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001807 break;
jeffhaobdb76512011-09-07 11:43:16 -07001808 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001809 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001810 break;
jeffhaobdb76512011-09-07 11:43:16 -07001811 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001812 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001813 break;
jeffhaobdb76512011-09-07 11:43:16 -07001814 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001815 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001816 break;
1817 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001818 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001819 break;
1820 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001821 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001822 break;
1823 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001824 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001825 break;
jeffhaobdb76512011-09-07 11:43:16 -07001826
Ian Rogersd81871c2011-10-03 13:57:23 -07001827 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001828 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001829 break;
1830 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001831 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001832 break;
1833 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001834 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001835 break;
1836 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001837 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001838 break;
1839 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001840 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001841 break;
1842 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001843 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001844 break;
jeffhaobdb76512011-09-07 11:43:16 -07001845 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001846 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001847 break;
1848
jeffhaobdb76512011-09-07 11:43:16 -07001849 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001850 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001851 break;
jeffhaobdb76512011-09-07 11:43:16 -07001852 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001853 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001854 break;
jeffhaobdb76512011-09-07 11:43:16 -07001855 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001856 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001857 break;
jeffhaobdb76512011-09-07 11:43:16 -07001858 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001859 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001860 break;
1861 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001862 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001863 break;
1864 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001865 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001866 break;
1867 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001868 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001869 break;
1870
1871 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001872 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001873 break;
1874 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001875 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001876 break;
1877 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001878 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001879 break;
1880 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001881 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001882 break;
1883 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001884 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001885 break;
1886 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001887 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001888 break;
1889 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001890 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001891 break;
1892
1893 case Instruction::INVOKE_VIRTUAL:
1894 case Instruction::INVOKE_VIRTUAL_RANGE:
1895 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001896 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001897 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
1898 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
1899 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
1900 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001901 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001902 const char* descriptor;
1903 if (called_method == NULL) {
1904 uint32_t method_idx = dec_insn.vB;
1905 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1906 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1907 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1908 } else {
1909 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07001910 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001911 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
1912 work_line_->SetResultRegisterType(return_type);
1913 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001914 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001915 }
jeffhaobdb76512011-09-07 11:43:16 -07001916 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001917 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001918 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001919 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07001920 const char* return_type_descriptor;
1921 bool is_constructor;
1922 if (called_method == NULL) {
1923 uint32_t method_idx = dec_insn.vB;
1924 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1925 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
1926 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1927 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1928 } else {
1929 is_constructor = called_method->IsConstructor();
1930 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
1931 }
1932 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07001933 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001934 * Some additional checks when calling a constructor. We know from the invocation arg check
1935 * that the "this" argument is an instance of called_method->klass. Now we further restrict
1936 * that to require that called_method->klass is the same as this->klass or this->super,
1937 * allowing the latter only if the "this" argument is the same as the "this" argument to
1938 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07001939 */
jeffhaob57e9522012-04-26 18:08:21 -07001940 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1941 if (this_type.IsConflict()) // failure.
1942 break;
jeffhaobdb76512011-09-07 11:43:16 -07001943
jeffhaob57e9522012-04-26 18:08:21 -07001944 /* no null refs allowed (?) */
1945 if (this_type.IsZero()) {
1946 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
1947 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07001948 }
jeffhaob57e9522012-04-26 18:08:21 -07001949
1950 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07001951 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
1952 // TODO: re-enable constructor type verification
1953 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07001954 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07001955 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
1956 // break;
1957 // }
jeffhaob57e9522012-04-26 18:08:21 -07001958
1959 /* arg must be an uninitialized reference */
1960 if (!this_type.IsUninitializedTypes()) {
1961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
1962 << this_type;
1963 break;
1964 }
1965
1966 /*
1967 * Replace the uninitialized reference with an initialized one. We need to do this for all
1968 * registers that have the same object instance in them, not just the "this" register.
1969 */
1970 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001971 }
Ian Rogers46685432012-06-03 22:26:43 -07001972 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001973 work_line_->SetResultRegisterType(return_type);
1974 just_set_result = true;
1975 break;
1976 }
1977 case Instruction::INVOKE_STATIC:
1978 case Instruction::INVOKE_STATIC_RANGE: {
1979 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
1980 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001981 const char* descriptor;
1982 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001983 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001984 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1985 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07001986 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001987 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001988 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001989 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001990 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07001991 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07001992 just_set_result = true;
1993 }
1994 break;
jeffhaobdb76512011-09-07 11:43:16 -07001995 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001996 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001997 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001998 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001999 if (abs_method != NULL) {
2000 Class* called_interface = abs_method->GetDeclaringClass();
2001 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2002 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2003 << PrettyMethod(abs_method) << "'";
2004 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002005 }
Ian Rogers0d604842012-04-16 14:50:24 -07002006 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002007 /* Get the type of the "this" arg, which should either be a sub-interface of called
2008 * interface or Object (see comments in RegType::JoinClass).
2009 */
2010 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2011 if (this_type.IsZero()) {
2012 /* null pointer always passes (and always fails at runtime) */
2013 } else {
2014 if (this_type.IsUninitializedTypes()) {
2015 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2016 << this_type;
2017 break;
2018 }
2019 // In the past we have tried to assert that "called_interface" is assignable
2020 // from "this_type.GetClass()", however, as we do an imprecise Join
2021 // (RegType::JoinClass) we don't have full information on what interfaces are
2022 // implemented by "this_type". For example, two classes may implement the same
2023 // interfaces and have a common parent that doesn't implement the interface. The
2024 // join will set "this_type" to the parent class and a test that this implements
2025 // the interface will incorrectly fail.
2026 }
2027 /*
2028 * We don't have an object instance, so we can't find the concrete method. However, all of
2029 * the type information is in the abstract method, so we're good.
2030 */
2031 const char* descriptor;
2032 if (abs_method == NULL) {
2033 uint32_t method_idx = dec_insn.vB;
2034 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2035 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2036 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2037 } else {
2038 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2039 }
2040 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor);
2041 work_line_->SetResultRegisterType(return_type);
2042 work_line_->SetResultRegisterType(return_type);
2043 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002044 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002045 }
jeffhaobdb76512011-09-07 11:43:16 -07002046 case Instruction::NEG_INT:
2047 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002048 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002049 break;
2050 case Instruction::NEG_LONG:
2051 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002052 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002053 break;
2054 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002055 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002056 break;
2057 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002058 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002059 break;
2060 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002061 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002062 break;
2063 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002064 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002065 break;
2066 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002067 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002068 break;
2069 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002070 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002071 break;
2072 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002073 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002074 break;
2075 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002076 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002077 break;
2078 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002079 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002080 break;
2081 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002082 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002083 break;
2084 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002085 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002086 break;
2087 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002088 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002089 break;
2090 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002091 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002092 break;
2093 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002094 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002095 break;
2096 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002097 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002098 break;
2099 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002100 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002101 break;
2102 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002103 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002104 break;
2105
2106 case Instruction::ADD_INT:
2107 case Instruction::SUB_INT:
2108 case Instruction::MUL_INT:
2109 case Instruction::REM_INT:
2110 case Instruction::DIV_INT:
2111 case Instruction::SHL_INT:
2112 case Instruction::SHR_INT:
2113 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002114 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002115 break;
2116 case Instruction::AND_INT:
2117 case Instruction::OR_INT:
2118 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002119 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002120 break;
2121 case Instruction::ADD_LONG:
2122 case Instruction::SUB_LONG:
2123 case Instruction::MUL_LONG:
2124 case Instruction::DIV_LONG:
2125 case Instruction::REM_LONG:
2126 case Instruction::AND_LONG:
2127 case Instruction::OR_LONG:
2128 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002129 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002130 break;
2131 case Instruction::SHL_LONG:
2132 case Instruction::SHR_LONG:
2133 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002134 /* shift distance is Int, making these different from other binary operations */
2135 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002136 break;
2137 case Instruction::ADD_FLOAT:
2138 case Instruction::SUB_FLOAT:
2139 case Instruction::MUL_FLOAT:
2140 case Instruction::DIV_FLOAT:
2141 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002142 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002143 break;
2144 case Instruction::ADD_DOUBLE:
2145 case Instruction::SUB_DOUBLE:
2146 case Instruction::MUL_DOUBLE:
2147 case Instruction::DIV_DOUBLE:
2148 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002149 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002150 break;
2151 case Instruction::ADD_INT_2ADDR:
2152 case Instruction::SUB_INT_2ADDR:
2153 case Instruction::MUL_INT_2ADDR:
2154 case Instruction::REM_INT_2ADDR:
2155 case Instruction::SHL_INT_2ADDR:
2156 case Instruction::SHR_INT_2ADDR:
2157 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002158 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002159 break;
2160 case Instruction::AND_INT_2ADDR:
2161 case Instruction::OR_INT_2ADDR:
2162 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002163 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002164 break;
2165 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002166 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002167 break;
2168 case Instruction::ADD_LONG_2ADDR:
2169 case Instruction::SUB_LONG_2ADDR:
2170 case Instruction::MUL_LONG_2ADDR:
2171 case Instruction::DIV_LONG_2ADDR:
2172 case Instruction::REM_LONG_2ADDR:
2173 case Instruction::AND_LONG_2ADDR:
2174 case Instruction::OR_LONG_2ADDR:
2175 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002176 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002177 break;
2178 case Instruction::SHL_LONG_2ADDR:
2179 case Instruction::SHR_LONG_2ADDR:
2180 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002181 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002182 break;
2183 case Instruction::ADD_FLOAT_2ADDR:
2184 case Instruction::SUB_FLOAT_2ADDR:
2185 case Instruction::MUL_FLOAT_2ADDR:
2186 case Instruction::DIV_FLOAT_2ADDR:
2187 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002188 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002189 break;
2190 case Instruction::ADD_DOUBLE_2ADDR:
2191 case Instruction::SUB_DOUBLE_2ADDR:
2192 case Instruction::MUL_DOUBLE_2ADDR:
2193 case Instruction::DIV_DOUBLE_2ADDR:
2194 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002195 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002196 break;
2197 case Instruction::ADD_INT_LIT16:
2198 case Instruction::RSUB_INT:
2199 case Instruction::MUL_INT_LIT16:
2200 case Instruction::DIV_INT_LIT16:
2201 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002202 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002203 break;
2204 case Instruction::AND_INT_LIT16:
2205 case Instruction::OR_INT_LIT16:
2206 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002207 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002208 break;
2209 case Instruction::ADD_INT_LIT8:
2210 case Instruction::RSUB_INT_LIT8:
2211 case Instruction::MUL_INT_LIT8:
2212 case Instruction::DIV_INT_LIT8:
2213 case Instruction::REM_INT_LIT8:
2214 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002215 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002216 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002217 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002218 break;
2219 case Instruction::AND_INT_LIT8:
2220 case Instruction::OR_INT_LIT8:
2221 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002222 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002223 break;
2224
Ian Rogersd81871c2011-10-03 13:57:23 -07002225 /* These should never appear during verification. */
jeffhao9a4f0032012-08-30 16:17:40 -07002226 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002227 case Instruction::UNUSED_EE:
2228 case Instruction::UNUSED_EF:
2229 case Instruction::UNUSED_F2:
2230 case Instruction::UNUSED_F3:
2231 case Instruction::UNUSED_F4:
2232 case Instruction::UNUSED_F5:
2233 case Instruction::UNUSED_F6:
2234 case Instruction::UNUSED_F7:
2235 case Instruction::UNUSED_F8:
2236 case Instruction::UNUSED_F9:
2237 case Instruction::UNUSED_FA:
2238 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002239 case Instruction::UNUSED_F0:
2240 case Instruction::UNUSED_F1:
2241 case Instruction::UNUSED_E3:
2242 case Instruction::UNUSED_E8:
2243 case Instruction::UNUSED_E7:
2244 case Instruction::UNUSED_E4:
2245 case Instruction::UNUSED_E9:
2246 case Instruction::UNUSED_FC:
2247 case Instruction::UNUSED_E5:
2248 case Instruction::UNUSED_EA:
2249 case Instruction::UNUSED_FD:
2250 case Instruction::UNUSED_E6:
2251 case Instruction::UNUSED_EB:
2252 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002253 case Instruction::UNUSED_3E:
2254 case Instruction::UNUSED_3F:
2255 case Instruction::UNUSED_40:
2256 case Instruction::UNUSED_41:
2257 case Instruction::UNUSED_42:
2258 case Instruction::UNUSED_43:
2259 case Instruction::UNUSED_73:
2260 case Instruction::UNUSED_79:
2261 case Instruction::UNUSED_7A:
2262 case Instruction::UNUSED_EC:
2263 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002264 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002265 break;
2266
2267 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002268 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002269 * complain if an instruction is missing (which is desirable).
2270 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002271 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002272
Ian Rogersad0b3a32012-04-16 14:50:24 -07002273 if (have_pending_hard_failure_) {
2274 if (!Runtime::Current()->IsStarted()) {
jeffhaob57e9522012-04-26 18:08:21 -07002275 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002276 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002277 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002278 /* immediate failure, reject class */
2279 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2280 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002281 } else if (have_pending_runtime_throw_failure_) {
2282 /* slow path will throw, mark following code as unreachable */
2283 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002284 }
jeffhaobdb76512011-09-07 11:43:16 -07002285 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002286 * If we didn't just set the result register, clear it out. This ensures that you can only use
2287 * "move-result" immediately after the result is set. (We could check this statically, but it's
2288 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002289 */
2290 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002291 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002292 }
2293
jeffhaoa0a764a2011-09-16 10:43:38 -07002294 /* Handle "continue". Tag the next consecutive instruction. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002295 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers776ac1f2012-04-13 23:36:36 -07002296 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
Ian Rogersd81871c2011-10-03 13:57:23 -07002297 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
jeffhaod5347e02012-03-22 17:25:05 -07002298 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002299 return false;
2300 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002301 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2302 // next instruction isn't one.
jeffhaod5347e02012-03-22 17:25:05 -07002303 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002304 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002305 }
2306 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2307 if (next_line != NULL) {
2308 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2309 // needed.
2310 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002311 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002312 }
jeffhaobdb76512011-09-07 11:43:16 -07002313 } else {
2314 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002315 * We're not recording register data for the next instruction, so we don't know what the prior
2316 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002317 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002318 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002319 }
2320 }
2321
2322 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002323 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002324 *
2325 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002326 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002327 * somebody could get a reference field, check it for zero, and if the
2328 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002329 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002330 * that, and will reject the code.
2331 *
2332 * TODO: avoid re-fetching the branch target
2333 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002334 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002335 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002336 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002337 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002338 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002339 return false;
2340 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002341 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002342 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002343 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002344 }
jeffhaobdb76512011-09-07 11:43:16 -07002345 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002346 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002347 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002348 }
jeffhaobdb76512011-09-07 11:43:16 -07002349 }
2350
2351 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002352 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002353 *
2354 * We've already verified that the table is structurally sound, so we
2355 * just need to walk through and tag the targets.
2356 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002357 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002358 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2359 const uint16_t* switch_insns = insns + offset_to_switch;
2360 int switch_count = switch_insns[1];
2361 int offset_to_targets, targ;
2362
2363 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2364 /* 0 = sig, 1 = count, 2/3 = first key */
2365 offset_to_targets = 4;
2366 } else {
2367 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002368 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002369 offset_to_targets = 2 + 2 * switch_count;
2370 }
2371
2372 /* verify each switch target */
2373 for (targ = 0; targ < switch_count; targ++) {
2374 int offset;
2375 uint32_t abs_offset;
2376
2377 /* offsets are 32-bit, and only partly endian-swapped */
2378 offset = switch_insns[offset_to_targets + targ * 2] |
2379 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002380 abs_offset = work_insn_idx_ + offset;
2381 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002382 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002383 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002384 }
2385 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002386 return false;
2387 }
2388 }
2389
2390 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002391 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2392 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002393 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002394 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002395 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002396 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002397
Ian Rogers0571d352011-11-03 19:51:38 -07002398 for (; iterator.HasNext(); iterator.Next()) {
2399 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002400 within_catch_all = true;
2401 }
jeffhaobdb76512011-09-07 11:43:16 -07002402 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002403 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2404 * "work_regs", because at runtime the exception will be thrown before the instruction
2405 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002406 */
Ian Rogers0571d352011-11-03 19:51:38 -07002407 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002408 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002409 }
jeffhaobdb76512011-09-07 11:43:16 -07002410 }
2411
2412 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002413 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2414 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002415 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002416 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002417 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002418 * The state in work_line reflects the post-execution state. If the current instruction is a
2419 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002420 * it will do so before grabbing the lock).
2421 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002422 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002423 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002424 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002425 return false;
2426 }
2427 }
2428 }
2429
jeffhaod1f0fde2011-09-08 17:25:33 -07002430 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002431 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002432 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002433 return false;
2434 }
jeffhaobdb76512011-09-07 11:43:16 -07002435 }
2436
2437 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002438 * Update start_guess. Advance to the next instruction of that's
2439 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002440 * neither of those exists we're in a return or throw; leave start_guess
2441 * alone and let the caller sort it out.
2442 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002443 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002444 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002445 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002446 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002447 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002448 }
2449
Ian Rogersd81871c2011-10-03 13:57:23 -07002450 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2451 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002452
2453 return true;
2454}
2455
Ian Rogers776ac1f2012-04-13 23:36:36 -07002456const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002457 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002458 const RegType& referrer = GetDeclaringClass();
2459 Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002460 const RegType& result =
2461 klass != NULL ? reg_types_.FromClass(klass)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002462 : reg_types_.FromDescriptor(class_loader_, descriptor);
2463 if (result.IsConflict()) {
2464 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2465 << "' in " << referrer;
2466 return result;
2467 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002468 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002469 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002470 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002471 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002472 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002473 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002474 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002475 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002476 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002477 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002478}
2479
Ian Rogers776ac1f2012-04-13 23:36:36 -07002480const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002481 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002482 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002483 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002484 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2485 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002486 CatchHandlerIterator iterator(handlers_ptr);
2487 for (; iterator.HasNext(); iterator.Next()) {
2488 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2489 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002490 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07002491 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002492 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002493 if (common_super == NULL) {
2494 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2495 // as that is caught at runtime
2496 common_super = &exception;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002497 } else if (!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002498 // We don't know enough about the type and the common path merge will result in
2499 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002500 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002501 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002502 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002503 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002504 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002505 common_super = &common_super->Merge(exception, &reg_types_);
2506 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002507 }
2508 }
2509 }
2510 }
Ian Rogers0571d352011-11-03 19:51:38 -07002511 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002512 }
2513 }
2514 if (common_super == NULL) {
2515 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002516 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002517 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002518 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002519 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002520}
2521
Ian Rogersad0b3a32012-04-16 14:50:24 -07002522Method* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx, MethodType method_type) {
2523 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002524 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002525 if (klass_type.IsConflict()) {
2526 std::string append(" in attempt to access method ");
2527 append += dex_file_->GetMethodName(method_id);
2528 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002529 return NULL;
2530 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002531 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002532 return NULL; // Can't resolve Class so no more to do here
2533 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002534 Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002535 const RegType& referrer = GetDeclaringClass();
2536 Method* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002537 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002538 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002539 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002540
2541 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002542 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002543 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002544 res_method = klass->FindInterfaceMethod(name, signature);
2545 } else {
2546 res_method = klass->FindVirtualMethod(name, signature);
2547 }
2548 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002549 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002550 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002551 // If a virtual or interface method wasn't found with the expected type, look in
2552 // the direct methods. This can happen when the wrong invoke type is used or when
2553 // a class has changed, and will be flagged as an error in later checks.
2554 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2555 res_method = klass->FindDirectMethod(name, signature);
2556 }
2557 if (res_method == NULL) {
2558 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2559 << PrettyDescriptor(klass) << "." << name
2560 << " " << signature;
2561 return NULL;
2562 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002563 }
2564 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002565 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2566 // enforce them here.
2567 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002568 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2569 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002570 return NULL;
2571 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002572 // Disallow any calls to class initializers.
2573 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002574 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2575 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002576 return NULL;
2577 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002578 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002579 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002580 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002581 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002582 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002583 }
jeffhaode0d9c92012-02-27 13:58:13 -08002584 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2585 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002586 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2587 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002588 return NULL;
2589 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002590 // Check that interface methods match interface classes.
2591 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2592 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2593 << " is in an interface class " << PrettyClass(klass);
2594 return NULL;
2595 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2596 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2597 << " is in a non-interface class " << PrettyClass(klass);
2598 return NULL;
2599 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002600 // See if the method type implied by the invoke instruction matches the access flags for the
2601 // target method.
2602 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2603 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2604 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2605 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002606 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2607 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002608 return NULL;
2609 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002610 return res_method;
2611}
2612
Ian Rogers776ac1f2012-04-13 23:36:36 -07002613Method* MethodVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
Ian Rogers46685432012-06-03 22:26:43 -07002614 MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002615 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2616 // we're making.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002617 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002618 if (res_method == NULL) { // error or class is unresolved
2619 return NULL;
2620 }
2621
Ian Rogersd81871c2011-10-03 13:57:23 -07002622 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2623 // has a vtable entry for the target method.
2624 if (is_super) {
2625 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002626 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002627 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002628 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
2629 << PrettyMethod(method_idx_, *dex_file_)
2630 << " to super " << PrettyMethod(res_method);
2631 return NULL;
2632 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002633 Class* super_klass = super.GetClass();
2634 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002635 MethodHelper mh(res_method);
2636 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
2637 << PrettyMethod(method_idx_, *dex_file_)
2638 << " to super " << super
2639 << "." << mh.GetName()
2640 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002641 return NULL;
2642 }
2643 }
2644 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2645 // match the call to the signature. Also, we might might be calling through an abstract method
2646 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08002647 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002648 /* caught by static verifier */
2649 DCHECK(is_range || expected_args <= 5);
2650 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002651 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002652 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2653 return NULL;
2654 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002655
jeffhaobdb76512011-09-07 11:43:16 -07002656 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002657 * Check the "this" argument, which must be an instance of the class that declared the method.
2658 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2659 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002660 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002661 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002662 if (!res_method->IsStatic()) {
2663 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002664 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002665 return NULL;
2666 }
2667 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002668 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002669 return NULL;
2670 }
2671 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07002672 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
2673 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002674 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002675 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002676 return NULL;
2677 }
2678 }
2679 actual_args++;
2680 }
2681 /*
2682 * Process the target method's signature. This signature may or may not
2683 * have been verified, so we can't assume it's properly formed.
2684 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002685 MethodHelper mh(res_method);
2686 const DexFile::TypeList* params = mh.GetParameterTypeList();
2687 size_t params_size = params == NULL ? 0 : params->Size();
2688 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002689 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002690 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002691 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2692 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002693 return NULL;
2694 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002695 const char* descriptor =
2696 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2697 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002698 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002699 << " missing signature component";
2700 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002701 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002702 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002703 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002704 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002705 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002706 }
2707 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2708 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002709 if (actual_args != expected_args) {
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 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002712 return NULL;
2713 } else {
2714 return res_method;
2715 }
2716}
2717
Ian Rogers776ac1f2012-04-13 23:36:36 -07002718void MethodVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08002719 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002720 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002721 if (res_type.IsConflict()) { // bad class
2722 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002723 } else {
2724 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2725 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002726 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002727 } else if (!is_filled) {
2728 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002729 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002730 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002731 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002732 } else {
2733 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2734 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002735 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002736 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002737 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002738 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002739 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002740 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002741 return;
2742 }
2743 }
2744 // filled-array result goes into "result" register
2745 work_line_->SetResultRegisterType(res_type);
2746 }
2747 }
2748}
2749
Ian Rogers776ac1f2012-04-13 23:36:36 -07002750void MethodVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002751 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002752 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002753 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002754 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002755 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002756 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002757 if (array_type.IsZero()) {
2758 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2759 // instruction type. TODO: have a proper notion of bottom here.
2760 if (!is_primitive || insn_type.IsCategory1Types()) {
2761 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08002762 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002763 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002764 // Category 2
Elliott Hughesadb8c672012-03-06 16:49:32 -08002765 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
Ian Rogers89310de2012-02-01 13:47:30 -08002766 }
jeffhaofc3144e2012-02-01 17:21:15 -08002767 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002768 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002769 } else {
2770 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002771 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002772 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002773 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002774 << " source for aget-object";
2775 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002776 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002777 << " source for category 1 aget";
2778 } else if (is_primitive && !insn_type.Equals(component_type) &&
2779 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2780 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002781 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogersd81871c2011-10-03 13:57:23 -07002782 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002783 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002784 // Use knowledge of the field type which is stronger than the type inferred from the
2785 // instruction, which can't differentiate object types and ints from floats, longs from
2786 // doubles.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002787 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002788 }
2789 }
2790 }
2791}
2792
Ian Rogers776ac1f2012-04-13 23:36:36 -07002793void MethodVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002794 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002795 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002796 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002797 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002798 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002799 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002800 if (array_type.IsZero()) {
2801 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2802 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002803 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002804 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002805 } else {
2806 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002807 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002808 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002809 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002810 << " source for aput-object";
2811 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002812 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002813 << " source for category 1 aput";
2814 } else if (is_primitive && !insn_type.Equals(component_type) &&
2815 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2816 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002817 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002818 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002819 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002820 // The instruction agrees with the type of array, confirm the value to be stored does too
2821 // Note: we use the instruction type (rather than the component type) for aput-object as
2822 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08002823 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002824 }
2825 }
2826 }
2827}
2828
Ian Rogers776ac1f2012-04-13 23:36:36 -07002829Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002830 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2831 // Check access to class
2832 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002833 if (klass_type.IsConflict()) { // bad class
2834 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
2835 field_idx, dex_file_->GetFieldName(field_id),
2836 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002837 return NULL;
2838 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002839 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002840 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08002841 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002842 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2843 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002844 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002845 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
2846 << dex_file_->GetFieldName(field_id) << ") in "
2847 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002848 DCHECK(Thread::Current()->IsExceptionPending());
2849 Thread::Current()->ClearException();
2850 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002851 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2852 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002853 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002854 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002855 return NULL;
2856 } else if (!field->IsStatic()) {
2857 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
2858 return NULL;
2859 } else {
2860 return field;
2861 }
2862}
2863
Ian Rogers776ac1f2012-04-13 23:36:36 -07002864Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002865 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2866 // Check access to class
2867 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002868 if (klass_type.IsConflict()) {
2869 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
2870 field_idx, dex_file_->GetFieldName(field_id),
2871 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002872 return NULL;
2873 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002874 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002875 return NULL; // Can't resolve Class so no more to do here
2876 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002877 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2878 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002879 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002880 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
2881 << dex_file_->GetFieldName(field_id) << ") in "
2882 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002883 DCHECK(Thread::Current()->IsExceptionPending());
2884 Thread::Current()->ClearException();
2885 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002886 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2887 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002888 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002889 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002890 return NULL;
2891 } else if (field->IsStatic()) {
2892 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
2893 << " to not be static";
2894 return NULL;
2895 } else if (obj_type.IsZero()) {
2896 // Cannot infer and check type, however, access will cause null pointer exception
2897 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07002898 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002899 const RegType& field_klass = reg_types_.FromClass(field->GetDeclaringClass());
2900 if (obj_type.IsUninitializedTypes() &&
2901 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
2902 !field_klass.Equals(GetDeclaringClass()))) {
2903 // Field accesses through uninitialized references are only allowable for constructors where
2904 // the field is declared in this class
2905 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
2906 << " of a not fully initialized object within the context of "
2907 << PrettyMethod(method_idx_, *dex_file_);
2908 return NULL;
2909 } else if (!field_klass.IsAssignableFrom(obj_type)) {
2910 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
2911 // of C1. For resolution to occur the declared class of the field must be compatible with
2912 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
2913 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
2914 << " from object of type " << obj_type;
2915 return NULL;
2916 } else {
2917 return field;
2918 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002919 }
2920}
2921
Ian Rogers776ac1f2012-04-13 23:36:36 -07002922void MethodVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002923 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002924 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002925 Field* field;
2926 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002927 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002928 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002929 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07002930 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002931 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002932 const char* descriptor;
Ian Rogers365c1022012-06-22 15:05:28 -07002933 ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002934 if (field != NULL) {
2935 descriptor = FieldHelper(field).GetTypeDescriptor();
2936 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07002937 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002938 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2939 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2940 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07002941 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002942 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
2943 if (is_primitive) {
2944 if (field_type.Equals(insn_type) ||
2945 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
2946 (field_type.IsDouble() && insn_type.IsLongTypes())) {
2947 // expected that read is of the correct primitive type or that int reads are reading
2948 // floats or long reads are reading doubles
2949 } else {
2950 // This is a global failure rather than a class change failure as the instructions and
2951 // the descriptors for the type should have been consistent within the same file at
2952 // compile time
2953 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
2954 << " to be of type '" << insn_type
2955 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002956 return;
2957 }
2958 } else {
2959 if (!insn_type.IsAssignableFrom(field_type)) {
2960 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
2961 << " to be compatible with type '" << insn_type
2962 << "' but found type '" << field_type
2963 << "' in get-object";
2964 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Conflict());
2965 return;
2966 }
2967 }
2968 work_line_->SetRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002969}
2970
Ian Rogers776ac1f2012-04-13 23:36:36 -07002971void MethodVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002972 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002973 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002974 Field* field;
2975 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07002976 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002977 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002978 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07002979 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002980 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002981 const char* descriptor;
Ian Rogers365c1022012-06-22 15:05:28 -07002982 ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002983 if (field != NULL) {
2984 descriptor = FieldHelper(field).GetTypeDescriptor();
2985 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07002986 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002987 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2988 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2989 loader = class_loader_;
2990 }
2991 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
2992 if (field != NULL) {
2993 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
2994 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
2995 << " from other class " << GetDeclaringClass();
2996 return;
2997 }
2998 }
2999 if (is_primitive) {
3000 // Primitive field assignability rules are weaker than regular assignability rules
3001 bool instruction_compatible;
3002 bool value_compatible;
3003 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
3004 if (field_type.IsIntegralTypes()) {
3005 instruction_compatible = insn_type.IsIntegralTypes();
3006 value_compatible = value_type.IsIntegralTypes();
3007 } else if (field_type.IsFloat()) {
3008 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3009 value_compatible = value_type.IsFloatTypes();
3010 } else if (field_type.IsLong()) {
3011 instruction_compatible = insn_type.IsLong();
3012 value_compatible = value_type.IsLongTypes();
3013 } else if (field_type.IsDouble()) {
3014 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3015 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003016 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003017 instruction_compatible = false; // reference field with primitive store
3018 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003019 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003020 if (!instruction_compatible) {
3021 // This is a global failure rather than a class change failure as the instructions and
3022 // the descriptors for the type should have been consistent within the same file at
3023 // compile time
3024 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3025 << " to be of type '" << insn_type
3026 << "' but found type '" << field_type
3027 << "' in put";
3028 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003029 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003030 if (!value_compatible) {
3031 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
3032 << " of type " << value_type
3033 << " but expected " << field_type
3034 << " for store to " << PrettyField(field) << " in put";
3035 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003036 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003037 } else {
3038 if (!insn_type.IsAssignableFrom(field_type)) {
3039 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3040 << " to be compatible with type '" << insn_type
3041 << "' but found type '" << field_type
3042 << "' in put-object";
3043 return;
3044 }
3045 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003046 }
3047}
3048
Ian Rogers776ac1f2012-04-13 23:36:36 -07003049bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003050 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003051 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003052 return false;
3053 }
3054 return true;
3055}
3056
Ian Rogers776ac1f2012-04-13 23:36:36 -07003057bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003058 bool changed = true;
3059 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3060 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003061 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003062 * We haven't processed this instruction before, and we haven't touched the registers here, so
3063 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3064 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003065 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003066 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003067 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003068 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3069 if (gDebugVerify) {
3070 copy->CopyFromLine(target_line);
3071 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003072 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003073 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003074 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003075 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003076 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003077 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003078 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3079 << *copy.get() << " MERGE\n"
3080 << *merge_line << " ==\n"
3081 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003082 }
3083 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003084 if (changed) {
3085 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003086 }
3087 return true;
3088}
3089
Ian Rogers776ac1f2012-04-13 23:36:36 -07003090InsnFlags* MethodVerifier::CurrentInsnFlags() {
3091 return &insn_flags_[work_insn_idx_];
3092}
3093
Ian Rogersad0b3a32012-04-16 14:50:24 -07003094const RegType& MethodVerifier::GetMethodReturnType() {
3095 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx_);
3096 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3097 uint16_t return_type_idx = proto_id.return_type_idx_;
3098 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
3099 return reg_types_.FromDescriptor(class_loader_, descriptor);
3100}
3101
3102const RegType& MethodVerifier::GetDeclaringClass() {
3103 if (foo_method_ != NULL) {
3104 return reg_types_.FromClass(foo_method_->GetDeclaringClass());
3105 } else {
3106 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx_);
3107 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
3108 return reg_types_.FromDescriptor(class_loader_, descriptor);
3109 }
3110}
3111
Ian Rogers776ac1f2012-04-13 23:36:36 -07003112void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003113 size_t* log2_max_gc_pc) {
3114 size_t local_gc_points = 0;
3115 size_t max_insn = 0;
3116 size_t max_ref_reg = -1;
3117 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3118 if (insn_flags_[i].IsGcPoint()) {
3119 local_gc_points++;
3120 max_insn = i;
3121 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003122 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003123 }
3124 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003125 *gc_points = local_gc_points;
3126 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3127 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003128 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003129 i++;
3130 }
3131 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003132}
3133
Ian Rogers776ac1f2012-04-13 23:36:36 -07003134const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003135 size_t num_entries, ref_bitmap_bits, pc_bits;
3136 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3137 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003138 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003139 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003140 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003141 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003142 return NULL;
3143 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003144 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3145 // There are 2 bytes to encode the number of entries
3146 if (num_entries >= 65536) {
3147 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003148 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003149 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003150 return NULL;
3151 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003152 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003153 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003154 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003155 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003156 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003157 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003158 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003159 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003160 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003161 // 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 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3164 return NULL;
3165 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003166 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003167 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003168 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003169 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003170 return NULL;
3171 }
3172 // Write table header
Ian Rogers776ac1f2012-04-13 23:36:36 -07003173 table->push_back(format | ((ref_bitmap_bytes >> PcToReferenceMap::kRegMapFormatShift) &
3174 ~PcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003175 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003176 table->push_back(num_entries & 0xFF);
3177 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003178 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003179 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3180 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003181 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003182 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003183 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003184 }
3185 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003186 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003187 }
3188 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003189 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003190 return table;
3191}
jeffhaoa0a764a2011-09-16 10:43:38 -07003192
Ian Rogers776ac1f2012-04-13 23:36:36 -07003193void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003194 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3195 // that the table data is well formed and all references are marked (or not) in the bitmap
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003196 PcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003197 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003198 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003199 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3200 if (insn_flags_[i].IsGcPoint()) {
3201 CHECK_LT(map_index, map.NumEntries());
3202 CHECK_EQ(map.GetPC(map_index), i);
3203 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3204 map_index++;
3205 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003206 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003207 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003208 CHECK_LT(j / 8, map.RegWidth());
3209 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3210 } else if ((j / 8) < map.RegWidth()) {
3211 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3212 } else {
3213 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3214 }
3215 }
3216 } else {
3217 CHECK(reg_bitmap == NULL);
3218 }
3219 }
3220}
jeffhaoa0a764a2011-09-16 10:43:38 -07003221
Ian Rogers776ac1f2012-04-13 23:36:36 -07003222void MethodVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003223 {
3224 MutexLock mu(*gc_maps_lock_);
3225 GcMapTable::iterator it = gc_maps_->find(ref);
3226 if (it != gc_maps_->end()) {
3227 delete it->second;
3228 gc_maps_->erase(it);
3229 }
3230 gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003231 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003232 CHECK(GetGcMap(ref) != NULL);
3233}
3234
Ian Rogers776ac1f2012-04-13 23:36:36 -07003235const std::vector<uint8_t>* MethodVerifier::GetGcMap(Compiler::MethodReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003236 MutexLock mu(*gc_maps_lock_);
3237 GcMapTable::const_iterator it = gc_maps_->find(ref);
3238 if (it == gc_maps_->end()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003239 return NULL;
3240 }
3241 CHECK(it->second != NULL);
3242 return it->second;
3243}
3244
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003245Mutex* MethodVerifier::gc_maps_lock_ = NULL;
3246MethodVerifier::GcMapTable* MethodVerifier::gc_maps_ = NULL;
3247
3248Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
3249MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
3250
3251#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
3252Mutex* MethodVerifier::inferred_reg_category_maps_lock_ = NULL;
3253MethodVerifier::InferredRegCategoryMapTable* MethodVerifier::inferred_reg_category_maps_ = NULL;
3254#endif
3255
3256void MethodVerifier::Init() {
3257 gc_maps_lock_ = new Mutex("verifier GC maps lock");
3258 {
3259 MutexLock mu(*gc_maps_lock_);
3260 gc_maps_ = new MethodVerifier::GcMapTable;
3261 }
3262
3263 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
3264 {
3265 MutexLock mu(*rejected_classes_lock_);
3266 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
3267 }
3268
3269#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
3270 inferred_reg_category_maps_lock_ = new Mutex("verifier GC maps lock");
3271 {
3272 MutexLock mu(*inferred_reg_category_maps_lock_);
3273 inferred_reg_category_maps_ = new MethodVerifier::InferredRegCategoryMapTable;
3274 }
3275#endif
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003276}
3277
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003278void MethodVerifier::Shutdown() {
3279 {
3280 MutexLock mu(*gc_maps_lock_);
3281 STLDeleteValues(gc_maps_);
3282 delete gc_maps_;
3283 gc_maps_ = NULL;
3284 }
3285 delete gc_maps_lock_;
3286 gc_maps_lock_ = NULL;
3287
3288 {
3289 MutexLock mu(*rejected_classes_lock_);
3290 delete rejected_classes_;
3291 rejected_classes_ = NULL;
3292 }
3293 delete rejected_classes_lock_;
3294 rejected_classes_lock_ = NULL;
3295
3296#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
3297 {
3298 MutexLock mu(*inferred_reg_category_maps_lock_);
3299 STLDeleteValues(inferred_reg_category_maps_);
3300 delete inferred_reg_category_maps_;
3301 inferred_reg_category_maps_ = NULL;
3302 }
3303 delete inferred_reg_category_maps_lock_;
3304 inferred_reg_category_maps_lock_ = NULL;
3305#endif
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003306}
jeffhaod1224c72012-02-29 13:43:08 -08003307
Ian Rogers776ac1f2012-04-13 23:36:36 -07003308void MethodVerifier::AddRejectedClass(Compiler::ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003309 {
3310 MutexLock mu(*rejected_classes_lock_);
3311 rejected_classes_->insert(ref);
3312 }
jeffhaod1224c72012-02-29 13:43:08 -08003313 CHECK(IsClassRejected(ref));
3314}
3315
Ian Rogers776ac1f2012-04-13 23:36:36 -07003316bool MethodVerifier::IsClassRejected(Compiler::ClassReference ref) {
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003317 MutexLock mu(*rejected_classes_lock_);
3318 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08003319}
3320
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -07003321#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Ian Rogers776ac1f2012-04-13 23:36:36 -07003322const InferredRegCategoryMap* MethodVerifier::GenerateInferredRegCategoryMap() {
Logan Chienfca7e872011-12-20 20:08:22 +08003323 uint32_t insns_size = code_item_->insns_size_in_code_units_;
3324 uint16_t regs_size = code_item_->registers_size_;
3325
3326 UniquePtr<InferredRegCategoryMap> table(
3327 new InferredRegCategoryMap(insns_size, regs_size));
3328
3329 for (size_t i = 0; i < insns_size; ++i) {
3330 if (RegisterLine* line = reg_table_.GetLine(i)) {
TDYa127526643e2012-05-26 01:01:48 -07003331 const Instruction* inst = Instruction::At(code_item_->insns_ + i);
3332
3333 // GC points
3334 if (inst->IsBranch() || inst->IsInvoke()) {
3335 for (size_t r = 0; r < regs_size; ++r) {
3336 const RegType &rt = line->GetRegisterType(r);
3337 if (rt.IsNonZeroReferenceTypes()) {
3338 table->SetRegCanBeObject(r);
3339 }
TDYa127b2eb5c12012-05-24 15:52:10 -07003340 }
3341 }
3342
TDYa127526643e2012-05-26 01:01:48 -07003343 /* We only use InferredRegCategoryMap in one case */
3344 if (inst->IsBranch()) {
TDYa127b2eb5c12012-05-24 15:52:10 -07003345 for (size_t r = 0; r < regs_size; ++r) {
3346 const RegType &rt = line->GetRegisterType(r);
3347
3348 if (rt.IsZero()) {
3349 table->SetRegCategory(i, r, kRegZero);
3350 } else if (rt.IsCategory1Types()) {
3351 table->SetRegCategory(i, r, kRegCat1nr);
3352 } else if (rt.IsCategory2Types()) {
3353 table->SetRegCategory(i, r, kRegCat2);
3354 } else if (rt.IsReferenceTypes()) {
3355 table->SetRegCategory(i, r, kRegObject);
3356 } else {
3357 table->SetRegCategory(i, r, kRegUnknown);
3358 }
Logan Chienfca7e872011-12-20 20:08:22 +08003359 }
3360 }
3361 }
3362 }
3363
3364 return table.release();
3365}
Logan Chiendd361c92012-04-10 23:40:37 +08003366
Ian Rogers776ac1f2012-04-13 23:36:36 -07003367void MethodVerifier::SetInferredRegCategoryMap(Compiler::MethodReference ref,
3368 const InferredRegCategoryMap& inferred_reg_category_map) {
Shih-wei Liaocd05a622012-08-15 00:02:05 -07003369 {
3370 MutexLock mu(*inferred_reg_category_maps_lock_);
3371 InferredRegCategoryMapTable::iterator it = inferred_reg_category_maps_->find(ref);
3372 if (it == inferred_reg_category_maps_->end()) {
3373 inferred_reg_category_maps_->Put(ref, &inferred_reg_category_map);
3374 } else {
3375 CHECK(*(it->second) == inferred_reg_category_map);
3376 delete &inferred_reg_category_map;
3377 }
Logan Chiendd361c92012-04-10 23:40:37 +08003378 }
Logan Chiendd361c92012-04-10 23:40:37 +08003379 CHECK(GetInferredRegCategoryMap(ref) != NULL);
3380}
3381
3382const InferredRegCategoryMap*
Ian Rogers776ac1f2012-04-13 23:36:36 -07003383MethodVerifier::GetInferredRegCategoryMap(Compiler::MethodReference ref) {
Logan Chiendd361c92012-04-10 23:40:37 +08003384 MutexLock mu(*inferred_reg_category_maps_lock_);
3385
3386 InferredRegCategoryMapTable::const_iterator it =
3387 inferred_reg_category_maps_->find(ref);
3388
3389 if (it == inferred_reg_category_maps_->end()) {
3390 return NULL;
3391 }
3392 CHECK(it->second != NULL);
3393 return it->second;
3394}
Logan Chienfca7e872011-12-20 20:08:22 +08003395#endif
3396
Ian Rogersd81871c2011-10-03 13:57:23 -07003397} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003398} // namespace art