blob: d6cd665dd6f3a37071a123dfb6c5c5b75a56ef32 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Ian Rogers776ac1f2012-04-13 23:36:36 -070017#include "method_verifier.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Brian Carlstrom1f870082011-08-23 16:02:11 -070021#include "class_linker.h"
Brian Carlstrome7d856b2012-01-11 18:10:55 -080022#include "compiler.h"
jeffhaob4df5142011-09-19 20:25:32 -070023#include "dex_cache.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070024#include "dex_file.h"
25#include "dex_instruction.h"
26#include "dex_instruction_visitor.h"
Ian Rogers0c7abda2012-09-19 13:33:42 -070027#include "verifier/dex_gc_map.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080028#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070029#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070030#include "leb128.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070031#include "logging.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080032#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070033#include "runtime.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070034#include "stringpiece.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070035
buzbeec531cef2012-10-18 07:09:20 -070036#if defined(ART_USE_LLVM_COMPILER)
TDYa12789f96052012-07-12 20:49:53 -070037#include "greenland/backend_types.h"
38#include "greenland/inferred_reg_category_map.h"
Logan Chienfca7e872011-12-20 20:08:22 +080039#endif
40
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070041namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070042namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070043
Ian Rogers2c8a8572011-10-24 17:11:36 -070044static const bool gDebugVerify = false;
45
Ian Rogers776ac1f2012-04-13 23:36:36 -070046class InsnFlags {
47 public:
48 InsnFlags() : length_(0), flags_(0) {}
49
50 void SetLengthInCodeUnits(size_t length) {
51 CHECK_LT(length, 65536u);
52 length_ = length;
53 }
54 size_t GetLengthInCodeUnits() {
55 return length_;
56 }
57 bool IsOpcode() const {
58 return length_ != 0;
59 }
60
61 void SetInTry() {
62 flags_ |= 1 << kInTry;
63 }
64 void ClearInTry() {
65 flags_ &= ~(1 << kInTry);
66 }
67 bool IsInTry() const {
68 return (flags_ & (1 << kInTry)) != 0;
69 }
70
71 void SetBranchTarget() {
72 flags_ |= 1 << kBranchTarget;
73 }
74 void ClearBranchTarget() {
75 flags_ &= ~(1 << kBranchTarget);
76 }
77 bool IsBranchTarget() const {
78 return (flags_ & (1 << kBranchTarget)) != 0;
79 }
80
81 void SetGcPoint() {
82 flags_ |= 1 << kGcPoint;
83 }
84 void ClearGcPoint() {
85 flags_ &= ~(1 << kGcPoint);
86 }
87 bool IsGcPoint() const {
88 return (flags_ & (1 << kGcPoint)) != 0;
89 }
90
91 void SetVisited() {
92 flags_ |= 1 << kVisited;
93 }
94 void ClearVisited() {
95 flags_ &= ~(1 << kVisited);
96 }
97 bool IsVisited() const {
98 return (flags_ & (1 << kVisited)) != 0;
99 }
100
101 void SetChanged() {
102 flags_ |= 1 << kChanged;
103 }
104 void ClearChanged() {
105 flags_ &= ~(1 << kChanged);
106 }
107 bool IsChanged() const {
108 return (flags_ & (1 << kChanged)) != 0;
109 }
110
111 bool IsVisitedOrChanged() const {
112 return IsVisited() || IsChanged();
113 }
114
115 std::string Dump() {
116 char encoding[6];
117 if (!IsOpcode()) {
118 strncpy(encoding, "XXXXX", sizeof(encoding));
119 } else {
120 strncpy(encoding, "-----", sizeof(encoding));
121 if (IsInTry()) encoding[kInTry] = 'T';
122 if (IsBranchTarget()) encoding[kBranchTarget] = 'B';
123 if (IsGcPoint()) encoding[kGcPoint] = 'G';
124 if (IsVisited()) encoding[kVisited] = 'V';
125 if (IsChanged()) encoding[kChanged] = 'C';
126 }
127 return std::string(encoding);
128 }
Elliott Hughesa21039c2012-06-21 12:09:25 -0700129
Ian Rogers776ac1f2012-04-13 23:36:36 -0700130 private:
131 enum {
132 kInTry,
133 kBranchTarget,
134 kGcPoint,
135 kVisited,
136 kChanged,
137 };
138
139 // Size of instruction in code units
140 uint16_t length_;
141 uint8_t flags_;
Ian Rogers84fa0742011-10-25 18:13:30 -0700142};
Ian Rogersd81871c2011-10-03 13:57:23 -0700143
Ian Rogersd81871c2011-10-03 13:57:23 -0700144void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
145 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -0700146 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700147 DCHECK_GT(insns_size, 0U);
148
149 for (uint32_t i = 0; i < insns_size; i++) {
150 bool interesting = false;
151 switch (mode) {
152 case kTrackRegsAll:
153 interesting = flags[i].IsOpcode();
154 break;
155 case kTrackRegsGcPoints:
156 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
157 break;
158 case kTrackRegsBranches:
159 interesting = flags[i].IsBranchTarget();
160 break;
161 default:
162 break;
163 }
164 if (interesting) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700165 pc_to_register_line_.Put(i, new RegisterLine(registers_size, verifier));
Ian Rogersd81871c2011-10-03 13:57:23 -0700166 }
167 }
168}
169
jeffhaof1e6b7c2012-06-05 18:33:30 -0700170MethodVerifier::FailureKind MethodVerifier::VerifyClass(const Class* klass, std::string& error) {
jeffhaobdb76512011-09-07 11:43:16 -0700171 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700172 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700173 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700174 Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -0800175 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800176 error = "Verifier rejected class ";
177 error += PrettyDescriptor(klass);
178 error += " that has no super class";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700179 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -0700180 }
Ian Rogers1c5eb702012-02-01 09:18:34 -0800181 if (super != NULL && super->IsFinal()) {
182 error = "Verifier rejected class ";
183 error += PrettyDescriptor(klass);
184 error += " that attempts to sub-class final class ";
185 error += PrettyDescriptor(super);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700186 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -0700187 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700188 ClassHelper kh(klass);
189 const DexFile& dex_file = kh.GetDexFile();
190 uint32_t class_def_idx;
191 if (!dex_file.FindClassDefIndex(kh.GetDescriptor(), class_def_idx)) {
192 error = "Verifier rejected class ";
193 error += PrettyDescriptor(klass);
194 error += " that isn't present in dex file ";
195 error += dex_file.GetLocation();
jeffhaof1e6b7c2012-06-05 18:33:30 -0700196 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700197 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700198 return VerifyClass(&dex_file, kh.GetDexCache(), klass->GetClassLoader(), class_def_idx, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700199}
200
Ian Rogers365c1022012-06-22 15:05:28 -0700201MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
202 DexCache* dex_cache, ClassLoader* class_loader, uint32_t class_def_idx, std::string& error) {
jeffhaof56197c2012-03-05 18:01:54 -0800203 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
204 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700205 if (class_data == NULL) {
206 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700207 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700208 }
jeffhaof56197c2012-03-05 18:01:54 -0800209 ClassDataItemIterator it(*dex_file, class_data);
210 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
211 it.Next();
212 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700213 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700214 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700215 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700216 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800217 while (it.HasNextDirectMethod()) {
218 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700219 if (method_idx == previous_direct_method_idx) {
220 // smali can create dex files with two encoded_methods sharing the same method_idx
221 // http://code.google.com/p/smali/issues/detail?id=119
222 it.Next();
223 continue;
224 }
225 previous_direct_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700226 InvokeType type = it.GetMethodInvokeType(class_def);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700227 AbstractMethod* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700228 if (method == NULL) {
229 DCHECK(Thread::Current()->IsExceptionPending());
230 // We couldn't resolve the method, but continue regardless.
231 Thread::Current()->ClearException();
232 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700233 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
234 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags());
235 if (result != kNoFailure) {
236 if (result == kHardFailure) {
237 hard_fail = true;
238 if (error_count > 0) {
239 error += "\n";
240 }
241 error = "Verifier rejected class ";
242 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
243 error += " due to bad method ";
244 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700245 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700246 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800247 }
248 it.Next();
249 }
jeffhao9b0b1882012-10-01 16:51:22 -0700250 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800251 while (it.HasNextVirtualMethod()) {
252 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700253 if (method_idx == previous_virtual_method_idx) {
254 // smali can create dex files with two encoded_methods sharing the same method_idx
255 // http://code.google.com/p/smali/issues/detail?id=119
256 it.Next();
257 continue;
258 }
259 previous_virtual_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700260 InvokeType type = it.GetMethodInvokeType(class_def);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700261 AbstractMethod* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700262 if (method == NULL) {
263 DCHECK(Thread::Current()->IsExceptionPending());
264 // We couldn't resolve the method, but continue regardless.
265 Thread::Current()->ClearException();
266 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700267 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
268 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags());
269 if (result != kNoFailure) {
270 if (result == kHardFailure) {
271 hard_fail = true;
272 if (error_count > 0) {
273 error += "\n";
274 }
275 error = "Verifier rejected class ";
276 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
277 error += " due to bad method ";
278 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700279 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700280 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800281 }
282 it.Next();
283 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700284 if (error_count == 0) {
285 return kNoFailure;
286 } else {
287 return hard_fail ? kHardFailure : kSoftFailure;
288 }
jeffhaof56197c2012-03-05 18:01:54 -0800289}
290
jeffhaof1e6b7c2012-06-05 18:33:30 -0700291MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx, const DexFile* dex_file,
Ian Rogers365c1022012-06-22 15:05:28 -0700292 DexCache* dex_cache, ClassLoader* class_loader, uint32_t class_def_idx,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700293 const DexFile::CodeItem* code_item, AbstractMethod* method, uint32_t method_access_flags) {
Ian Rogersc8982582012-09-07 16:53:25 -0700294 MethodVerifier::FailureKind result = kNoFailure;
295 uint64_t start_ns = NanoTime();
296
Ian Rogersad0b3a32012-04-16 14:50:24 -0700297 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item, method_idx,
298 method, method_access_flags);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700299 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700300 // Verification completed, however failures may be pending that didn't cause the verification
301 // to hard fail.
Ian Rogerse551e952012-06-03 22:59:14 -0700302 CHECK(!verifier.have_pending_hard_failure_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700303 if (verifier.failures_.size() != 0) {
304 verifier.DumpFailures(LOG(INFO) << "Soft verification failures in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700305 << PrettyMethod(method_idx, *dex_file) << "\n");
Ian Rogersc8982582012-09-07 16:53:25 -0700306 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800307 }
308 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700309 // Bad method data.
310 CHECK_NE(verifier.failures_.size(), 0U);
311 CHECK(verifier.have_pending_hard_failure_);
312 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700313 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800314 if (gDebugVerify) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700315 std::cout << "\n" << verifier.info_messages_.str();
jeffhaof56197c2012-03-05 18:01:54 -0800316 verifier.Dump(std::cout);
317 }
Ian Rogersc8982582012-09-07 16:53:25 -0700318 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800319 }
Ian Rogersc8982582012-09-07 16:53:25 -0700320 uint64_t duration_ns = NanoTime() - start_ns;
321 if (duration_ns > MsToNs(100)) {
322 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
323 << " took " << PrettyDuration(duration_ns);
324 }
325 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800326}
327
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800328void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
329 const DexFile* dex_file, DexCache* dex_cache,
330 ClassLoader* class_loader, uint32_t class_def_idx,
331 const DexFile::CodeItem* code_item, AbstractMethod* method,
332 uint32_t method_access_flags) {
333 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item,
334 dex_method_idx, method, method_access_flags);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700335 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800336 verifier.DumpFailures(os);
337 os << verifier.info_messages_.str();
338 verifier.Dump(os);
339}
340
341std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_method_idx,
342 const DexFile* dex_file, DexCache* dex_cache,
343 ClassLoader* class_loader,
344 uint32_t class_def_idx,
345 const DexFile::CodeItem* code_item,
346 AbstractMethod* method,
347 uint32_t method_access_flags, uint32_t dex_pc) {
348 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item,
349 dex_method_idx, method, method_access_flags);
350 verifier.Verify();
351 return verifier.DescribeVRegs(dex_pc);
jeffhaoba5ebb92011-08-25 17:24:37 -0700352}
353
Ian Rogers776ac1f2012-04-13 23:36:36 -0700354MethodVerifier::MethodVerifier(const DexFile* dex_file, DexCache* dex_cache,
Ian Rogers365c1022012-06-22 15:05:28 -0700355 ClassLoader* class_loader, uint32_t class_def_idx, const DexFile::CodeItem* code_item,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800356 uint32_t dex_method_idx, AbstractMethod* method, uint32_t method_access_flags)
jeffhaof56197c2012-03-05 18:01:54 -0800357 : work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800358 dex_method_idx_(dex_method_idx),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700359 foo_method_(method),
360 method_access_flags_(method_access_flags),
jeffhaof56197c2012-03-05 18:01:54 -0800361 dex_file_(dex_file),
362 dex_cache_(dex_cache),
363 class_loader_(class_loader),
364 class_def_idx_(class_def_idx),
365 code_item_(code_item),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700366 interesting_dex_pc_(-1),
367 monitor_enter_dex_pcs_(NULL),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700368 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700369 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800370 new_instance_count_(0),
371 monitor_enter_count_(0) {
372}
373
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800374void MethodVerifier::FindLocksAtDexPc(AbstractMethod* m, uint32_t dex_pc,
375 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700376 MethodHelper mh(m);
377 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
378 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
379 m, m->GetAccessFlags());
380 verifier.interesting_dex_pc_ = dex_pc;
381 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
382 verifier.FindLocksAtDexPc();
383}
384
385void MethodVerifier::FindLocksAtDexPc() {
386 CHECK(monitor_enter_dex_pcs_ != NULL);
387 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
388
389 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
390 // verification. In practice, the phase we want relies on data structures set up by all the
391 // earlier passes, so we just run the full method verification and bail out early when we've
392 // got what we wanted.
393 Verify();
394}
395
Ian Rogersad0b3a32012-04-16 14:50:24 -0700396bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700397 // If there aren't any instructions, make sure that's expected, then exit successfully.
398 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700399 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700400 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700401 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700402 } else {
403 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700404 }
jeffhaobdb76512011-09-07 11:43:16 -0700405 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700406 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
407 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700408 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
409 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700410 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700411 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700412 // Allocate and initialize an array to hold instruction data.
413 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
414 // Run through the instructions and see if the width checks out.
415 bool result = ComputeWidthsAndCountOps();
416 // Flag instructions guarded by a "try" block and check exception handlers.
417 result = result && ScanTryCatchBlocks();
418 // Perform static instruction verification.
419 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700420 // Perform code-flow analysis and return.
421 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700422}
423
Ian Rogers776ac1f2012-04-13 23:36:36 -0700424std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700425 switch (error) {
426 case VERIFY_ERROR_NO_CLASS:
427 case VERIFY_ERROR_NO_FIELD:
428 case VERIFY_ERROR_NO_METHOD:
429 case VERIFY_ERROR_ACCESS_CLASS:
430 case VERIFY_ERROR_ACCESS_FIELD:
431 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700432 case VERIFY_ERROR_INSTANTIATION:
433 case VERIFY_ERROR_CLASS_CHANGE:
jeffhaofaf459e2012-08-31 15:32:47 -0700434 if (Runtime::Current()->IsCompiler()) {
435 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
436 // class change and instantiation errors into soft verification errors so that we re-verify
437 // at runtime. We may fail to find or to agree on access because of not yet available class
438 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
439 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
440 // paths" that dynamically perform the verification and cause the behavior to be that akin
441 // to an interpreter.
442 error = VERIFY_ERROR_BAD_CLASS_SOFT;
443 } else {
444 have_pending_runtime_throw_failure_ = true;
445 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700446 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700447 // Indication that verification should be retried at runtime.
448 case VERIFY_ERROR_BAD_CLASS_SOFT:
449 if (!Runtime::Current()->IsCompiler()) {
450 // It is runtime so hard fail.
451 have_pending_hard_failure_ = true;
452 }
453 break;
jeffhaod5347e02012-03-22 17:25:05 -0700454 // Hard verification failures at compile time will still fail at runtime, so the class is
455 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700456 case VERIFY_ERROR_BAD_CLASS_HARD: {
457 if (Runtime::Current()->IsCompiler()) {
jeffhaof56197c2012-03-05 18:01:54 -0800458 Compiler::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800459 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800460 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700461 have_pending_hard_failure_ = true;
462 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800463 }
464 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700465 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800466 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700467 work_insn_idx_));
468 std::ostringstream* failure_message = new std::ostringstream(location);
469 failure_messages_.push_back(failure_message);
470 return *failure_message;
471}
472
473void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
474 size_t failure_num = failure_messages_.size();
475 DCHECK_NE(failure_num, 0U);
476 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
477 prepend += last_fail_message->str();
478 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
479 delete last_fail_message;
480}
481
482void MethodVerifier::AppendToLastFailMessage(std::string append) {
483 size_t failure_num = failure_messages_.size();
484 DCHECK_NE(failure_num, 0U);
485 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
486 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800487}
488
Ian Rogers776ac1f2012-04-13 23:36:36 -0700489bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700490 const uint16_t* insns = code_item_->insns_;
491 size_t insns_size = code_item_->insns_size_in_code_units_;
492 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700493 size_t new_instance_count = 0;
494 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700495 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700496
Ian Rogersd81871c2011-10-03 13:57:23 -0700497 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700498 Instruction::Code opcode = inst->Opcode();
499 if (opcode == Instruction::NEW_INSTANCE) {
500 new_instance_count++;
501 } else if (opcode == Instruction::MONITOR_ENTER) {
502 monitor_enter_count++;
503 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700504 size_t inst_size = inst->SizeInCodeUnits();
505 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
506 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700507 inst = inst->Next();
508 }
509
Ian Rogersd81871c2011-10-03 13:57:23 -0700510 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700511 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
512 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700513 return false;
514 }
515
Ian Rogersd81871c2011-10-03 13:57:23 -0700516 new_instance_count_ = new_instance_count;
517 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700518 return true;
519}
520
Ian Rogers776ac1f2012-04-13 23:36:36 -0700521bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700522 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700523 if (tries_size == 0) {
524 return true;
525 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700526 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700527 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700528
529 for (uint32_t idx = 0; idx < tries_size; idx++) {
530 const DexFile::TryItem* try_item = &tries[idx];
531 uint32_t start = try_item->start_addr_;
532 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700533 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700534 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
535 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700536 return false;
537 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700538 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700539 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700540 return false;
541 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700542 for (uint32_t dex_pc = start; dex_pc < end;
543 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
544 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700545 }
546 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800547 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700548 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700549 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700550 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700551 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700552 CatchHandlerIterator iterator(handlers_ptr);
553 for (; iterator.HasNext(); iterator.Next()) {
554 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700555 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700556 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700557 return false;
558 }
jeffhao60f83e32012-02-13 17:16:30 -0800559 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
560 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700561 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700562 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800563 return false;
564 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700565 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700566 // Ensure exception types are resolved so that they don't need resolution to be delivered,
567 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700568 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
jeffhaof56197c2012-03-05 18:01:54 -0800569 Class* exception_type = linker->ResolveType(*dex_file_, iterator.GetHandlerTypeIndex(),
570 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700571 if (exception_type == NULL) {
572 DCHECK(Thread::Current()->IsExceptionPending());
573 Thread::Current()->ClearException();
574 }
575 }
jeffhaobdb76512011-09-07 11:43:16 -0700576 }
Ian Rogers0571d352011-11-03 19:51:38 -0700577 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700578 }
jeffhaobdb76512011-09-07 11:43:16 -0700579 return true;
580}
581
Ian Rogers776ac1f2012-04-13 23:36:36 -0700582bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700583 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700584
Ian Rogers0c7abda2012-09-19 13:33:42 -0700585 /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */
Ian Rogersd81871c2011-10-03 13:57:23 -0700586 insn_flags_[0].SetBranchTarget();
Ian Rogers0c7abda2012-09-19 13:33:42 -0700587 insn_flags_[0].SetGcPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700588
589 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700590 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700591 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700592 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700593 return false;
594 }
595 /* Flag instructions that are garbage collection points */
596 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
597 insn_flags_[dex_pc].SetGcPoint();
598 }
599 dex_pc += inst->SizeInCodeUnits();
600 inst = inst->Next();
601 }
602 return true;
603}
604
Ian Rogers776ac1f2012-04-13 23:36:36 -0700605bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800606 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700607 bool result = true;
608 switch (inst->GetVerifyTypeArgumentA()) {
609 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800610 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700611 break;
612 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800613 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700614 break;
615 }
616 switch (inst->GetVerifyTypeArgumentB()) {
617 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800618 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700619 break;
620 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800621 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700622 break;
623 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800624 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700625 break;
626 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800627 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700628 break;
629 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800630 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700631 break;
632 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800633 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700634 break;
635 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800636 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700637 break;
638 }
639 switch (inst->GetVerifyTypeArgumentC()) {
640 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800641 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700642 break;
643 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800644 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700645 break;
646 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800647 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700648 break;
649 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800650 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700651 break;
652 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800653 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700654 break;
655 }
656 switch (inst->GetVerifyExtraFlags()) {
657 case Instruction::kVerifyArrayData:
658 result = result && CheckArrayData(code_offset);
659 break;
660 case Instruction::kVerifyBranchTarget:
661 result = result && CheckBranchTarget(code_offset);
662 break;
663 case Instruction::kVerifySwitchTargets:
664 result = result && CheckSwitchTargets(code_offset);
665 break;
666 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800667 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700668 break;
669 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800670 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700671 break;
672 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700673 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 result = false;
675 break;
676 }
677 return result;
678}
679
Ian Rogers776ac1f2012-04-13 23:36:36 -0700680bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700681 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700682 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
683 << code_item_->registers_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::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700690 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700691 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
692 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700693 return false;
694 }
695 return true;
696}
697
Ian Rogers776ac1f2012-04-13 23:36:36 -0700698bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700699 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700700 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
701 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700702 return false;
703 }
704 return true;
705}
706
Ian Rogers776ac1f2012-04-13 23:36:36 -0700707bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700708 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700709 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
710 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700711 return false;
712 }
713 return true;
714}
715
Ian Rogers776ac1f2012-04-13 23:36:36 -0700716bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700717 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
719 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700720 return false;
721 }
722 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700723 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700724 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700725 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700726 return false;
727 }
728 return true;
729}
730
Ian Rogers776ac1f2012-04-13 23:36:36 -0700731bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700732 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
734 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700735 return false;
736 }
737 return true;
738}
739
Ian Rogers776ac1f2012-04-13 23:36:36 -0700740bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700741 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700742 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
743 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700744 return false;
745 }
746 return true;
747}
748
Ian Rogers776ac1f2012-04-13 23:36:36 -0700749bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700750 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700751 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
752 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700753 return false;
754 }
755 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700756 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700757 const char* cp = descriptor;
758 while (*cp++ == '[') {
759 bracket_count++;
760 }
761 if (bracket_count == 0) {
762 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700763 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700764 return false;
765 } else if (bracket_count > 255) {
766 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700767 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700768 return false;
769 }
770 return true;
771}
772
Ian Rogers776ac1f2012-04-13 23:36:36 -0700773bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700774 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
775 const uint16_t* insns = code_item_->insns_ + cur_offset;
776 const uint16_t* array_data;
777 int32_t array_data_offset;
778
779 DCHECK_LT(cur_offset, insn_count);
780 /* make sure the start of the array data table is in range */
781 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
782 if ((int32_t) cur_offset + array_data_offset < 0 ||
783 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700784 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
785 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700786 return false;
787 }
788 /* offset to array data table is a relative branch-style offset */
789 array_data = insns + array_data_offset;
790 /* make sure the table is 32-bit aligned */
791 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700792 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
793 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700794 return false;
795 }
796 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700797 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700798 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
799 /* make sure the end of the switch is in range */
800 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700801 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
802 << ", data offset " << array_data_offset << ", end "
803 << cur_offset + array_data_offset + table_size
804 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700805 return false;
806 }
807 return true;
808}
809
Ian Rogers776ac1f2012-04-13 23:36:36 -0700810bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700811 int32_t offset;
812 bool isConditional, selfOkay;
813 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
814 return false;
815 }
816 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700817 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 -0700818 return false;
819 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700820 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
821 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700822 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700823 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700824 return false;
825 }
826 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
827 int32_t abs_offset = cur_offset + offset;
828 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700829 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700830 << reinterpret_cast<void*>(abs_offset) << ") at "
831 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700832 return false;
833 }
834 insn_flags_[abs_offset].SetBranchTarget();
835 return true;
836}
837
Ian Rogers776ac1f2012-04-13 23:36:36 -0700838bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700839 bool* selfOkay) {
840 const uint16_t* insns = code_item_->insns_ + cur_offset;
841 *pConditional = false;
842 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700843 switch (*insns & 0xff) {
844 case Instruction::GOTO:
845 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700846 break;
847 case Instruction::GOTO_32:
848 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700849 *selfOkay = true;
850 break;
851 case Instruction::GOTO_16:
852 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700853 break;
854 case Instruction::IF_EQ:
855 case Instruction::IF_NE:
856 case Instruction::IF_LT:
857 case Instruction::IF_GE:
858 case Instruction::IF_GT:
859 case Instruction::IF_LE:
860 case Instruction::IF_EQZ:
861 case Instruction::IF_NEZ:
862 case Instruction::IF_LTZ:
863 case Instruction::IF_GEZ:
864 case Instruction::IF_GTZ:
865 case Instruction::IF_LEZ:
866 *pOffset = (int16_t) insns[1];
867 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700868 break;
869 default:
870 return false;
871 break;
872 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700873 return true;
874}
875
Ian Rogers776ac1f2012-04-13 23:36:36 -0700876bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700877 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700878 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700879 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700880 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700881 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
882 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700883 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
884 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700885 return false;
886 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700887 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700888 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700889 /* make sure the table is 32-bit aligned */
890 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700891 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
892 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700893 return false;
894 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700895 uint32_t switch_count = switch_insns[1];
896 int32_t keys_offset, targets_offset;
897 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700898 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
899 /* 0=sig, 1=count, 2/3=firstKey */
900 targets_offset = 4;
901 keys_offset = -1;
902 expected_signature = Instruction::kPackedSwitchSignature;
903 } else {
904 /* 0=sig, 1=count, 2..count*2 = keys */
905 keys_offset = 2;
906 targets_offset = 2 + 2 * switch_count;
907 expected_signature = Instruction::kSparseSwitchSignature;
908 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700909 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700910 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700911 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
912 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700913 return false;
914 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700915 /* make sure the end of the switch is in range */
916 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700917 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
918 << switch_offset << ", end "
919 << (cur_offset + switch_offset + table_size)
920 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700921 return false;
922 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700923 /* for a sparse switch, verify the keys are in ascending order */
924 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700925 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
926 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700927 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
928 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
929 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700930 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
931 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700932 return false;
933 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700934 last_key = key;
935 }
936 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700937 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700938 for (uint32_t targ = 0; targ < switch_count; targ++) {
939 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
940 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
941 int32_t abs_offset = cur_offset + offset;
942 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700943 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700944 << reinterpret_cast<void*>(abs_offset) << ") at "
945 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700946 return false;
947 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700948 insn_flags_[abs_offset].SetBranchTarget();
949 }
950 return true;
951}
952
Ian Rogers776ac1f2012-04-13 23:36:36 -0700953bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700954 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700955 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700956 return false;
957 }
958 uint16_t registers_size = code_item_->registers_size_;
959 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800960 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
962 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700963 return false;
964 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700965 }
966
967 return true;
968}
969
Ian Rogers776ac1f2012-04-13 23:36:36 -0700970bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 uint16_t registers_size = code_item_->registers_size_;
972 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
973 // integer overflow when adding them here.
974 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700975 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
976 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700977 return false;
978 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700979 return true;
980}
981
buzbeec531cef2012-10-18 07:09:20 -0700982#if !defined(ART_USE_LLVM_COMPILER)
Ian Rogers0c7abda2012-09-19 13:33:42 -0700983static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -0800984 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
985 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
986 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
987 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
988 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
989 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
990 gc_map.begin(),
991 gc_map.end());
992 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
993 DCHECK_EQ(gc_map.size(),
994 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
995 (length_prefixed_gc_map->at(1) << 16) |
996 (length_prefixed_gc_map->at(2) << 8) |
997 (length_prefixed_gc_map->at(3) << 0)));
998 return length_prefixed_gc_map;
999}
Ian Rogers30bce5a2012-09-20 16:30:53 -07001000#endif
Brian Carlstrom75412882012-01-18 01:26:54 -08001001
Ian Rogers776ac1f2012-04-13 23:36:36 -07001002bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001003 uint16_t registers_size = code_item_->registers_size_;
1004 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001005
Ian Rogersd81871c2011-10-03 13:57:23 -07001006 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001007 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1008 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001009 }
1010 /* Create and initialize table holding register status */
Elliott Hughes460384f2012-04-04 16:53:10 -07001011 reg_table_.Init(kTrackRegsGcPoints, insn_flags_.get(), insns_size, registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001012
Ian Rogersd81871c2011-10-03 13:57:23 -07001013 work_line_.reset(new RegisterLine(registers_size, this));
1014 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001015
Ian Rogersd81871c2011-10-03 13:57:23 -07001016 /* Initialize register types of method arguments. */
1017 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001018 DCHECK_NE(failures_.size(), 0U);
1019 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001020 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001021 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001022 return false;
1023 }
1024 /* Perform code flow verification. */
1025 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001026 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001027 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001028 }
1029
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001030 Compiler::MethodReference ref(dex_file_, dex_method_idx_);
TDYa127b2eb5c12012-05-24 15:52:10 -07001031
buzbeec531cef2012-10-18 07:09:20 -07001032#if !defined(ART_USE_LLVM_COMPILER)
TDYa127b2eb5c12012-05-24 15:52:10 -07001033
Ian Rogersd81871c2011-10-03 13:57:23 -07001034 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -08001035 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1036 if (map.get() == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001037 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001038 return false; // Not a real failure, but a failure to encode
1039 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001040#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001041 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -07001042#endif
Ian Rogers0c7abda2012-09-19 13:33:42 -07001043 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
1044 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
Logan Chiendd361c92012-04-10 23:40:37 +08001045
buzbeec531cef2012-10-18 07:09:20 -07001046#else // defined(ART_USE_LLVM_COMPILER)
Logan Chienfca7e872011-12-20 20:08:22 +08001047 /* Generate Inferred Register Category for LLVM-based Code Generator */
1048 const InferredRegCategoryMap* table = GenerateInferredRegCategoryMap();
Ian Rogers776ac1f2012-04-13 23:36:36 -07001049 verifier::MethodVerifier::SetInferredRegCategoryMap(ref, *table);
TDYa127b2eb5c12012-05-24 15:52:10 -07001050
Logan Chienfca7e872011-12-20 20:08:22 +08001051#endif
1052
jeffhaobdb76512011-09-07 11:43:16 -07001053 return true;
1054}
1055
Ian Rogersad0b3a32012-04-16 14:50:24 -07001056std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1057 DCHECK_EQ(failures_.size(), failure_messages_.size());
1058 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001059 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001060 }
1061 return os;
1062}
1063
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001064extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001065 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001066 v->Dump(std::cerr);
1067}
1068
Ian Rogers776ac1f2012-04-13 23:36:36 -07001069void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001070 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001071 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001072 return;
jeffhaobdb76512011-09-07 11:43:16 -07001073 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001074 {
1075 os << "Register Types:\n";
1076 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1077 std::ostream indent_os(&indent_filter);
1078 reg_types_.Dump(indent_os);
1079 }
Ian Rogersb4903572012-10-11 11:52:56 -07001080 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001081 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1082 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001083 const Instruction* inst = Instruction::At(code_item_->insns_);
1084 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1085 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001086 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1087 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001088 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001089 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001090 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump() << " ";
1091 const bool kDumpHexOfInstruction = false;
1092 if (kDumpHexOfInstruction) {
1093 indent_os << inst->DumpHex(5) << " ";
1094 }
1095 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001096 inst = inst->Next();
1097 }
jeffhaobdb76512011-09-07 11:43:16 -07001098}
1099
Ian Rogersd81871c2011-10-03 13:57:23 -07001100static bool IsPrimitiveDescriptor(char descriptor) {
1101 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001102 case 'I':
1103 case 'C':
1104 case 'S':
1105 case 'B':
1106 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001107 case 'F':
1108 case 'D':
1109 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001110 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001111 default:
1112 return false;
1113 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001114}
1115
Ian Rogers776ac1f2012-04-13 23:36:36 -07001116bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001117 RegisterLine* reg_line = reg_table_.GetLine(0);
1118 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1119 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001120
Ian Rogersd81871c2011-10-03 13:57:23 -07001121 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1122 //Include the "this" pointer.
1123 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001124 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001125 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1126 // argument as uninitialized. This restricts field access until the superclass constructor is
1127 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001128 const RegType& declaring_class = GetDeclaringClass();
1129 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001130 reg_line->SetRegisterType(arg_start + cur_arg,
1131 reg_types_.UninitializedThisArgument(declaring_class));
1132 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001133 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001134 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001135 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001136 }
1137
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001138 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001139 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001140 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001141
1142 for (; iterator.HasNext(); iterator.Next()) {
1143 const char* descriptor = iterator.GetDescriptor();
1144 if (descriptor == NULL) {
1145 LOG(FATAL) << "Null descriptor";
1146 }
1147 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001148 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1149 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001150 return false;
1151 }
1152 switch (descriptor[0]) {
1153 case 'L':
1154 case '[':
1155 // We assume that reference arguments are initialized. The only way it could be otherwise
1156 // (assuming the caller was verified) is if the current method is <init>, but in that case
1157 // it's effectively considered initialized the instant we reach here (in the sense that we
1158 // can return without doing anything or call virtual methods).
1159 {
Ian Rogersb4903572012-10-11 11:52:56 -07001160 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001161 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001162 }
1163 break;
1164 case 'Z':
1165 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1166 break;
1167 case 'C':
1168 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1169 break;
1170 case 'B':
1171 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1172 break;
1173 case 'I':
1174 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1175 break;
1176 case 'S':
1177 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1178 break;
1179 case 'F':
1180 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1181 break;
1182 case 'J':
1183 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001184 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1185 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1186 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001187 cur_arg++;
1188 break;
1189 }
1190 default:
jeffhaod5347e02012-03-22 17:25:05 -07001191 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001192 return false;
1193 }
1194 cur_arg++;
1195 }
1196 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001197 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001198 return false;
1199 }
1200 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1201 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1202 // format. Only major difference from the method argument format is that 'V' is supported.
1203 bool result;
1204 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1205 result = descriptor[1] == '\0';
1206 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1207 size_t i = 0;
1208 do {
1209 i++;
1210 } while (descriptor[i] == '['); // process leading [
1211 if (descriptor[i] == 'L') { // object array
1212 do {
1213 i++; // find closing ;
1214 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1215 result = descriptor[i] == ';';
1216 } else { // primitive array
1217 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1218 }
1219 } else if (descriptor[0] == 'L') {
1220 // could be more thorough here, but shouldn't be required
1221 size_t i = 0;
1222 do {
1223 i++;
1224 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1225 result = descriptor[i] == ';';
1226 } else {
1227 result = false;
1228 }
1229 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001230 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1231 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001232 }
1233 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001234}
1235
Ian Rogers776ac1f2012-04-13 23:36:36 -07001236bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001237 const uint16_t* insns = code_item_->insns_;
1238 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001239
jeffhaobdb76512011-09-07 11:43:16 -07001240 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001241 insn_flags_[0].SetChanged();
1242 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001243
jeffhaobdb76512011-09-07 11:43:16 -07001244 /* Continue until no instructions are marked "changed". */
1245 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001246 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1247 uint32_t insn_idx = start_guess;
1248 for (; insn_idx < insns_size; insn_idx++) {
1249 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001250 break;
1251 }
jeffhaobdb76512011-09-07 11:43:16 -07001252 if (insn_idx == insns_size) {
1253 if (start_guess != 0) {
1254 /* try again, starting from the top */
1255 start_guess = 0;
1256 continue;
1257 } else {
1258 /* all flags are clear */
1259 break;
1260 }
1261 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001262 // We carry the working set of registers from instruction to instruction. If this address can
1263 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1264 // "changed" flags, we need to load the set of registers from the table.
1265 // Because we always prefer to continue on to the next instruction, we should never have a
1266 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1267 // target.
1268 work_insn_idx_ = insn_idx;
1269 if (insn_flags_[insn_idx].IsBranchTarget()) {
1270 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001271 } else {
1272#ifndef NDEBUG
1273 /*
1274 * Sanity check: retrieve the stored register line (assuming
1275 * a full table) and make sure it actually matches.
1276 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001277 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1278 if (register_line != NULL) {
1279 if (work_line_->CompareLine(register_line) != 0) {
1280 Dump(std::cout);
1281 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001282 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001283 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1284 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001285 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001286 }
jeffhaobdb76512011-09-07 11:43:16 -07001287 }
1288#endif
1289 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001290 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001291 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001292 prepend += " failed to verify: ";
1293 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001294 return false;
1295 }
jeffhaobdb76512011-09-07 11:43:16 -07001296 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001297 insn_flags_[insn_idx].SetVisited();
1298 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001299 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001300
Ian Rogers1c849e52012-06-28 14:00:33 -07001301 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001302 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001303 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001304 * (besides the wasted space), but it indicates a flaw somewhere
1305 * down the line, possibly in the verifier.
1306 *
1307 * If we've substituted "always throw" instructions into the stream,
1308 * we are almost certainly going to have some dead code.
1309 */
1310 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001311 uint32_t insn_idx = 0;
1312 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001313 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001314 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001315 * may or may not be preceded by a padding NOP (for alignment).
1316 */
1317 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1318 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1319 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001320 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001321 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1322 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1323 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001324 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001325 }
1326
Ian Rogersd81871c2011-10-03 13:57:23 -07001327 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001328 if (dead_start < 0)
1329 dead_start = insn_idx;
1330 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001331 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001332 dead_start = -1;
1333 }
1334 }
1335 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001336 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001337 }
1338 }
jeffhaobdb76512011-09-07 11:43:16 -07001339 return true;
1340}
1341
Ian Rogers776ac1f2012-04-13 23:36:36 -07001342bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001343#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001344 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001345 gDvm.verifierStats.instrsReexamined++;
1346 } else {
1347 gDvm.verifierStats.instrsExamined++;
1348 }
1349#endif
1350
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001351 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1352 // We want the state _before_ the instruction, for the case where the dex pc we're
1353 // interested in is itself a monitor-enter instruction (which is a likely place
1354 // for a thread to be suspended).
1355 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
1356 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1357 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1358 }
1359 }
1360
jeffhaobdb76512011-09-07 11:43:16 -07001361 /*
1362 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001363 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001364 * control to another statement:
1365 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001366 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001367 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001368 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001369 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001370 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001371 * throw an exception that is handled by an encompassing "try"
1372 * block.
1373 *
1374 * We can also return, in which case there is no successor instruction
1375 * from this point.
1376 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001377 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001378 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001379 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1380 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001381 DecodedInstruction dec_insn(inst);
Ian Rogersa75a0132012-09-28 11:41:42 -07001382 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001383
jeffhaobdb76512011-09-07 11:43:16 -07001384 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001385 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001386 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001387 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001388 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1389 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001390 }
jeffhaobdb76512011-09-07 11:43:16 -07001391
1392 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001393 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001394 * can throw an exception, we will copy/merge this into the "catch"
1395 * address rather than work_line, because we don't want the result
1396 * from the "successful" code path (e.g. a check-cast that "improves"
1397 * a type) to be visible to the exception handler.
1398 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001399 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001400 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001401 } else {
1402#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001403 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001404#endif
1405 }
1406
Elliott Hughesadb8c672012-03-06 16:49:32 -08001407 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07001408 case Instruction::NOP:
1409 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001410 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001411 * a signature that looks like a NOP; if we see one of these in
1412 * the course of executing code then we have a problem.
1413 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001414 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001415 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001416 }
1417 break;
1418
1419 case Instruction::MOVE:
1420 case Instruction::MOVE_FROM16:
1421 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001422 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001423 break;
1424 case Instruction::MOVE_WIDE:
1425 case Instruction::MOVE_WIDE_FROM16:
1426 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001427 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07001428 break;
1429 case Instruction::MOVE_OBJECT:
1430 case Instruction::MOVE_OBJECT_FROM16:
1431 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001432 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001433 break;
1434
1435 /*
1436 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001437 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001438 * might want to hold the result in an actual CPU register, so the
1439 * Dalvik spec requires that these only appear immediately after an
1440 * invoke or filled-new-array.
1441 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001442 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001443 * redundant with the reset done below, but it can make the debug info
1444 * easier to read in some cases.)
1445 */
1446 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001447 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07001448 break;
1449 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001450 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001451 break;
1452 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001453 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07001454 break;
1455
Ian Rogersd81871c2011-10-03 13:57:23 -07001456 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001457 /*
jeffhao60f83e32012-02-13 17:16:30 -08001458 * This statement can only appear as the first instruction in an exception handler. We verify
1459 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001460 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001461 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08001462 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001463 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001464 }
jeffhaobdb76512011-09-07 11:43:16 -07001465 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001466 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1467 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001468 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001469 }
jeffhaobdb76512011-09-07 11:43:16 -07001470 }
1471 break;
1472 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001473 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001474 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001475 const RegType& return_type = GetMethodReturnType();
1476 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001477 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001478 } else {
1479 // Compilers may generate synthetic functions that write byte values into boolean fields.
1480 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001481 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001482 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1483 ((return_type.IsBoolean() || return_type.IsByte() ||
1484 return_type.IsShort() || return_type.IsChar()) &&
1485 src_type.IsInteger()));
1486 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001487 bool success =
1488 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
1489 if (!success) {
1490 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001491 }
jeffhaobdb76512011-09-07 11:43:16 -07001492 }
1493 }
1494 break;
1495 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001496 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001497 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001498 const RegType& return_type = GetMethodReturnType();
1499 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001500 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001501 } else {
1502 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001503 bool success = work_line_->VerifyRegisterType(dec_insn.vA, return_type);
1504 if (!success) {
1505 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001506 }
jeffhaobdb76512011-09-07 11:43:16 -07001507 }
1508 }
1509 break;
1510 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001511 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001512 const RegType& return_type = GetMethodReturnType();
1513 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001514 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001515 } else {
1516 /* return_type is the *expected* return type, not register value */
1517 DCHECK(!return_type.IsZero());
1518 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08001519 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07001520 // Disallow returning uninitialized values and verify that the reference in vAA is an
1521 // instance of the "return_type"
1522 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001523 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001524 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001525 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1526 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001527 }
1528 }
1529 }
1530 break;
1531
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001532 /* could be boolean, int, float, or a null reference */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001533 case Instruction::CONST_4:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001534 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001535 reg_types_.FromCat1Const(static_cast<int32_t>(dec_insn.vB << 28) >> 28, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001536 break;
jeffhaobdb76512011-09-07 11:43:16 -07001537 case Instruction::CONST_16:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001538 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001539 reg_types_.FromCat1Const(static_cast<int16_t>(dec_insn.vB), true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001540 break;
jeffhaobdb76512011-09-07 11:43:16 -07001541 case Instruction::CONST:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001542 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const(dec_insn.vB, true));
jeffhaobdb76512011-09-07 11:43:16 -07001543 break;
1544 case Instruction::CONST_HIGH16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001545 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001546 reg_types_.FromCat1Const(dec_insn.vB << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001547 break;
jeffhaobdb76512011-09-07 11:43:16 -07001548 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001549 case Instruction::CONST_WIDE_16: {
1550 int64_t val = static_cast<int16_t>(dec_insn.vB);
1551 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1552 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1553 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001554 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001555 }
1556 case Instruction::CONST_WIDE_32: {
1557 int64_t val = static_cast<int32_t>(dec_insn.vB);
1558 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1559 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1560 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1561 break;
1562 }
1563 case Instruction::CONST_WIDE: {
1564 int64_t val = dec_insn.vB_wide;
1565 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1566 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1567 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1568 break;
1569 }
1570 case Instruction::CONST_WIDE_HIGH16: {
1571 int64_t val = static_cast<uint64_t>(dec_insn.vB) << 48;
1572 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1573 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1574 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1575 break;
1576 }
jeffhaobdb76512011-09-07 11:43:16 -07001577 case Instruction::CONST_STRING:
1578 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001579 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001580 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001581 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001582 // Get type from instruction if unresolved then we need an access check
1583 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08001584 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001585 // Register holds class, ie its type is class, on error it will hold Conflict.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001586 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogersb4903572012-10-11 11:52:56 -07001587 res_type.IsConflict() ? res_type
1588 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001589 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001590 }
jeffhaobdb76512011-09-07 11:43:16 -07001591 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001592 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001593 break;
1594 case Instruction::MONITOR_EXIT:
1595 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001596 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001597 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001598 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001599 * to the need to handle asynchronous exceptions, a now-deprecated
1600 * feature that Dalvik doesn't support.)
1601 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001602 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001603 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001604 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001605 * structured locking checks are working, the former would have
1606 * failed on the -enter instruction, and the latter is impossible.
1607 *
1608 * This is fortunate, because issue 3221411 prevents us from
1609 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001610 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001611 * some catch blocks (which will show up as "dead" code when
1612 * we skip them here); if we can't, then the code path could be
1613 * "live" so we still need to check it.
1614 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001615 opcode_flags &= ~Instruction::kThrow;
1616 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001617 break;
1618
Ian Rogers28ad40d2011-10-27 15:19:26 -07001619 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001620 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001621 /*
1622 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1623 * could be a "upcast" -- not expected, so we don't try to address it.)
1624 *
1625 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001626 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001627 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001628 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001629 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001630 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001631 if (res_type.IsConflict()) {
1632 DCHECK_NE(failures_.size(), 0U);
1633 if (!is_checkcast) {
1634 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
1635 }
1636 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001637 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001638 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1639 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001640 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001641 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001642 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001643 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001644 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07001645 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001646 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001647 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001648 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001649 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001650 }
jeffhaobdb76512011-09-07 11:43:16 -07001651 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001652 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001653 }
1654 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001655 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001656 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001657 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001658 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001659 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001660 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001661 }
1662 }
1663 break;
1664 }
1665 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001666 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001667 if (res_type.IsConflict()) {
1668 DCHECK_NE(failures_.size(), 0U);
1669 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001670 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001671 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1672 // can't create an instance of an interface or abstract class */
1673 if (!res_type.IsInstantiableTypes()) {
1674 Fail(VERIFY_ERROR_INSTANTIATION)
1675 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001676 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001677 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001678 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1679 // Any registers holding previous allocations from this address that have not yet been
1680 // initialized must be marked invalid.
1681 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1682 // add the new uninitialized reference to the register state
1683 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001684 break;
1685 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001686 case Instruction::NEW_ARRAY:
1687 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001688 break;
1689 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08001690 VerifyNewArray(dec_insn, true, false);
1691 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001692 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001693 case Instruction::FILLED_NEW_ARRAY_RANGE:
1694 VerifyNewArray(dec_insn, true, true);
1695 just_set_result = true; // Filled new array range sets result register
1696 break;
jeffhaobdb76512011-09-07 11:43:16 -07001697 case Instruction::CMPL_FLOAT:
1698 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001699 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001700 break;
1701 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001702 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001703 break;
1704 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001705 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001706 break;
1707 case Instruction::CMPL_DOUBLE:
1708 case Instruction::CMPG_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001709 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vB, reg_types_.DoubleLo(),
1710 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001711 break;
1712 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001713 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vC, reg_types_.DoubleLo(),
1714 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001715 break;
1716 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001717 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001718 break;
1719 case Instruction::CMP_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001720 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vB, reg_types_.LongLo(),
1721 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001722 break;
1723 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001724 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vC, reg_types_.LongLo(),
1725 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001726 break;
1727 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001728 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001729 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001730 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001731 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersb4903572012-10-11 11:52:56 -07001732 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001733 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001734 }
1735 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001736 }
jeffhaobdb76512011-09-07 11:43:16 -07001737 case Instruction::GOTO:
1738 case Instruction::GOTO_16:
1739 case Instruction::GOTO_32:
1740 /* no effect on or use of registers */
1741 break;
1742
1743 case Instruction::PACKED_SWITCH:
1744 case Instruction::SPARSE_SWITCH:
1745 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001746 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001747 break;
1748
Ian Rogersd81871c2011-10-03 13:57:23 -07001749 case Instruction::FILL_ARRAY_DATA: {
1750 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001751 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08001752 /* array_type can be null if the reg type is Zero */
1753 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001754 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001755 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001756 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001757 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1758 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001759 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001760 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1761 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001762 } else {
jeffhao457cc512012-02-02 16:55:13 -08001763 // Now verify if the element width in the table matches the element width declared in
1764 // the array
1765 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1766 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001767 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001768 } else {
1769 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1770 // Since we don't compress the data in Dex, expect to see equal width of data stored
1771 // in the table and expected from the array class.
1772 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001773 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1774 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001775 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001776 }
1777 }
jeffhaobdb76512011-09-07 11:43:16 -07001778 }
1779 }
1780 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001781 }
jeffhaobdb76512011-09-07 11:43:16 -07001782 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001783 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001784 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1785 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001786 bool mismatch = false;
1787 if (reg_type1.IsZero()) { // zero then integral or reference expected
1788 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1789 } else if (reg_type1.IsReferenceTypes()) { // both references?
1790 mismatch = !reg_type2.IsReferenceTypes();
1791 } else { // both integral?
1792 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1793 }
1794 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001795 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1796 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001797 }
1798 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001799 }
jeffhaobdb76512011-09-07 11:43:16 -07001800 case Instruction::IF_LT:
1801 case Instruction::IF_GE:
1802 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001803 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001804 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1805 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001806 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001807 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1808 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001809 }
1810 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001811 }
jeffhaobdb76512011-09-07 11:43:16 -07001812 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001813 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001814 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001815 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001816 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001817 }
jeffhaobdb76512011-09-07 11:43:16 -07001818 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001819 }
jeffhaobdb76512011-09-07 11:43:16 -07001820 case Instruction::IF_LTZ:
1821 case Instruction::IF_GEZ:
1822 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001823 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001824 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001825 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001826 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1827 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001828 }
jeffhaobdb76512011-09-07 11:43:16 -07001829 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001830 }
jeffhaobdb76512011-09-07 11:43:16 -07001831 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001832 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
1833 break;
jeffhaobdb76512011-09-07 11:43:16 -07001834 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001835 VerifyAGet(dec_insn, reg_types_.Byte(), true);
1836 break;
jeffhaobdb76512011-09-07 11:43:16 -07001837 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07001838 VerifyAGet(dec_insn, reg_types_.Char(), true);
1839 break;
jeffhaobdb76512011-09-07 11:43:16 -07001840 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001841 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001842 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001843 case Instruction::AGET:
1844 VerifyAGet(dec_insn, reg_types_.Integer(), true);
1845 break;
jeffhaobdb76512011-09-07 11:43:16 -07001846 case Instruction::AGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001847 VerifyAGet(dec_insn, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001848 break;
1849 case Instruction::AGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001850 VerifyAGet(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001851 break;
1852
Ian Rogersd81871c2011-10-03 13:57:23 -07001853 case Instruction::APUT_BOOLEAN:
1854 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
1855 break;
1856 case Instruction::APUT_BYTE:
1857 VerifyAPut(dec_insn, reg_types_.Byte(), true);
1858 break;
1859 case Instruction::APUT_CHAR:
1860 VerifyAPut(dec_insn, reg_types_.Char(), true);
1861 break;
1862 case Instruction::APUT_SHORT:
1863 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001864 break;
1865 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001866 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001867 break;
1868 case Instruction::APUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001869 VerifyAPut(dec_insn, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001870 break;
1871 case Instruction::APUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001872 VerifyAPut(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001873 break;
1874
jeffhaobdb76512011-09-07 11:43:16 -07001875 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001876 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001877 break;
jeffhaobdb76512011-09-07 11:43:16 -07001878 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001879 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001880 break;
jeffhaobdb76512011-09-07 11:43:16 -07001881 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001882 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001883 break;
jeffhaobdb76512011-09-07 11:43:16 -07001884 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001885 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001886 break;
1887 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001888 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001889 break;
1890 case Instruction::IGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001891 VerifyISGet(dec_insn, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001892 break;
1893 case Instruction::IGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001894 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001895 break;
jeffhaobdb76512011-09-07 11:43:16 -07001896
Ian Rogersd81871c2011-10-03 13:57:23 -07001897 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001898 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001899 break;
1900 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001901 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001902 break;
1903 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001904 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001905 break;
1906 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001907 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001908 break;
1909 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001910 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001911 break;
1912 case Instruction::IPUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001913 VerifyISPut(dec_insn, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001914 break;
jeffhaobdb76512011-09-07 11:43:16 -07001915 case Instruction::IPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001916 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001917 break;
1918
jeffhaobdb76512011-09-07 11:43:16 -07001919 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001920 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001921 break;
jeffhaobdb76512011-09-07 11:43:16 -07001922 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001923 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001924 break;
jeffhaobdb76512011-09-07 11:43:16 -07001925 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001926 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001927 break;
jeffhaobdb76512011-09-07 11:43:16 -07001928 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001929 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001930 break;
1931 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001932 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001933 break;
1934 case Instruction::SGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001935 VerifyISGet(dec_insn, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001936 break;
1937 case Instruction::SGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001938 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001939 break;
1940
1941 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001942 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001943 break;
1944 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001945 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001946 break;
1947 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001948 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001949 break;
1950 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001951 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001952 break;
1953 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001954 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001955 break;
1956 case Instruction::SPUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001957 VerifyISPut(dec_insn, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001958 break;
1959 case Instruction::SPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001960 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001961 break;
1962
1963 case Instruction::INVOKE_VIRTUAL:
1964 case Instruction::INVOKE_VIRTUAL_RANGE:
1965 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001966 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001967 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
1968 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
1969 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
1970 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartier66f19252012-09-18 08:57:04 -07001971 AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001972 const char* descriptor;
1973 if (called_method == NULL) {
1974 uint32_t method_idx = dec_insn.vB;
1975 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1976 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1977 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1978 } else {
1979 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07001980 }
Ian Rogersb4903572012-10-11 11:52:56 -07001981 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001982 if (!return_type.IsLowHalf()) {
1983 work_line_->SetResultRegisterType(return_type);
1984 } else {
1985 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
1986 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001987 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001988 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001989 }
jeffhaobdb76512011-09-07 11:43:16 -07001990 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001991 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001992 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartier66f19252012-09-18 08:57:04 -07001993 AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07001994 const char* return_type_descriptor;
1995 bool is_constructor;
1996 if (called_method == NULL) {
1997 uint32_t method_idx = dec_insn.vB;
1998 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1999 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
2000 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2001 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2002 } else {
2003 is_constructor = called_method->IsConstructor();
2004 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2005 }
2006 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002007 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002008 * Some additional checks when calling a constructor. We know from the invocation arg check
2009 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2010 * that to require that called_method->klass is the same as this->klass or this->super,
2011 * allowing the latter only if the "this" argument is the same as the "this" argument to
2012 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002013 */
jeffhaob57e9522012-04-26 18:08:21 -07002014 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2015 if (this_type.IsConflict()) // failure.
2016 break;
jeffhaobdb76512011-09-07 11:43:16 -07002017
jeffhaob57e9522012-04-26 18:08:21 -07002018 /* no null refs allowed (?) */
2019 if (this_type.IsZero()) {
2020 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2021 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002022 }
jeffhaob57e9522012-04-26 18:08:21 -07002023
2024 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002025 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2026 // TODO: re-enable constructor type verification
2027 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002028 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002029 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2030 // break;
2031 // }
jeffhaob57e9522012-04-26 18:08:21 -07002032
2033 /* arg must be an uninitialized reference */
2034 if (!this_type.IsUninitializedTypes()) {
2035 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2036 << this_type;
2037 break;
2038 }
2039
2040 /*
2041 * Replace the uninitialized reference with an initialized one. We need to do this for all
2042 * registers that have the same object instance in them, not just the "this" register.
2043 */
2044 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002045 }
Ian Rogersb4903572012-10-11 11:52:56 -07002046 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2047 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002048 if (!return_type.IsLowHalf()) {
2049 work_line_->SetResultRegisterType(return_type);
2050 } else {
2051 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2052 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002053 just_set_result = true;
2054 break;
2055 }
2056 case Instruction::INVOKE_STATIC:
2057 case Instruction::INVOKE_STATIC_RANGE: {
2058 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartier66f19252012-09-18 08:57:04 -07002059 AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002060 const char* descriptor;
2061 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002062 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002063 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2064 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002065 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002066 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002067 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002068 }
Ian Rogersb4903572012-10-11 11:52:56 -07002069 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002070 if (!return_type.IsLowHalf()) {
2071 work_line_->SetResultRegisterType(return_type);
2072 } else {
2073 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2074 }
jeffhaobdb76512011-09-07 11:43:16 -07002075 just_set_result = true;
2076 }
2077 break;
jeffhaobdb76512011-09-07 11:43:16 -07002078 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002079 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002080 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartier66f19252012-09-18 08:57:04 -07002081 AbstractMethod* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002082 if (abs_method != NULL) {
2083 Class* called_interface = abs_method->GetDeclaringClass();
2084 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2085 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2086 << PrettyMethod(abs_method) << "'";
2087 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002088 }
Ian Rogers0d604842012-04-16 14:50:24 -07002089 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002090 /* Get the type of the "this" arg, which should either be a sub-interface of called
2091 * interface or Object (see comments in RegType::JoinClass).
2092 */
2093 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2094 if (this_type.IsZero()) {
2095 /* null pointer always passes (and always fails at runtime) */
2096 } else {
2097 if (this_type.IsUninitializedTypes()) {
2098 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2099 << this_type;
2100 break;
2101 }
2102 // In the past we have tried to assert that "called_interface" is assignable
2103 // from "this_type.GetClass()", however, as we do an imprecise Join
2104 // (RegType::JoinClass) we don't have full information on what interfaces are
2105 // implemented by "this_type". For example, two classes may implement the same
2106 // interfaces and have a common parent that doesn't implement the interface. The
2107 // join will set "this_type" to the parent class and a test that this implements
2108 // the interface will incorrectly fail.
2109 }
2110 /*
2111 * We don't have an object instance, so we can't find the concrete method. However, all of
2112 * the type information is in the abstract method, so we're good.
2113 */
2114 const char* descriptor;
2115 if (abs_method == NULL) {
2116 uint32_t method_idx = dec_insn.vB;
2117 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2118 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2119 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2120 } else {
2121 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2122 }
Ian Rogersb4903572012-10-11 11:52:56 -07002123 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002124 if (!return_type.IsLowHalf()) {
2125 work_line_->SetResultRegisterType(return_type);
2126 } else {
2127 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2128 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002129 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002130 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002131 }
jeffhaobdb76512011-09-07 11:43:16 -07002132 case Instruction::NEG_INT:
2133 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002134 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002135 break;
2136 case Instruction::NEG_LONG:
2137 case Instruction::NOT_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002138 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2139 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002140 break;
2141 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002142 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002143 break;
2144 case Instruction::NEG_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002145 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2146 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002147 break;
2148 case Instruction::INT_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002149 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2150 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002151 break;
2152 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002153 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002154 break;
2155 case Instruction::INT_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002156 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2157 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002158 break;
2159 case Instruction::LONG_TO_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002160 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Integer(),
2161 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002162 break;
2163 case Instruction::LONG_TO_FLOAT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002164 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Float(),
2165 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002166 break;
2167 case Instruction::LONG_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002168 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2169 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002170 break;
2171 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002172 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002173 break;
2174 case Instruction::FLOAT_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002175 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2176 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002177 break;
2178 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002179 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2180 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002181 break;
2182 case Instruction::DOUBLE_TO_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002183 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Integer(),
2184 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002185 break;
2186 case Instruction::DOUBLE_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002187 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2188 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002189 break;
2190 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002191 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Float(),
2192 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002193 break;
2194 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002195 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002196 break;
2197 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002198 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002199 break;
2200 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002201 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002202 break;
2203
2204 case Instruction::ADD_INT:
2205 case Instruction::SUB_INT:
2206 case Instruction::MUL_INT:
2207 case Instruction::REM_INT:
2208 case Instruction::DIV_INT:
2209 case Instruction::SHL_INT:
2210 case Instruction::SHR_INT:
2211 case Instruction::USHR_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002212 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(),
2213 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002214 break;
2215 case Instruction::AND_INT:
2216 case Instruction::OR_INT:
2217 case Instruction::XOR_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002218 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(),
2219 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002220 break;
2221 case Instruction::ADD_LONG:
2222 case Instruction::SUB_LONG:
2223 case Instruction::MUL_LONG:
2224 case Instruction::DIV_LONG:
2225 case Instruction::REM_LONG:
2226 case Instruction::AND_LONG:
2227 case Instruction::OR_LONG:
2228 case Instruction::XOR_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002229 work_line_->CheckBinaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2230 reg_types_.LongLo(), reg_types_.LongHi(),
2231 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002232 break;
2233 case Instruction::SHL_LONG:
2234 case Instruction::SHR_LONG:
2235 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002236 /* shift distance is Int, making these different from other binary operations */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002237 work_line_->CheckBinaryOpWideShift(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2238 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002239 break;
2240 case Instruction::ADD_FLOAT:
2241 case Instruction::SUB_FLOAT:
2242 case Instruction::MUL_FLOAT:
2243 case Instruction::DIV_FLOAT:
2244 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002245 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002246 break;
2247 case Instruction::ADD_DOUBLE:
2248 case Instruction::SUB_DOUBLE:
2249 case Instruction::MUL_DOUBLE:
2250 case Instruction::DIV_DOUBLE:
2251 case Instruction::REM_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002252 work_line_->CheckBinaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2253 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2254 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002255 break;
2256 case Instruction::ADD_INT_2ADDR:
2257 case Instruction::SUB_INT_2ADDR:
2258 case Instruction::MUL_INT_2ADDR:
2259 case Instruction::REM_INT_2ADDR:
2260 case Instruction::SHL_INT_2ADDR:
2261 case Instruction::SHR_INT_2ADDR:
2262 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002263 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002264 break;
2265 case Instruction::AND_INT_2ADDR:
2266 case Instruction::OR_INT_2ADDR:
2267 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002268 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002269 break;
2270 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002271 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002272 break;
2273 case Instruction::ADD_LONG_2ADDR:
2274 case Instruction::SUB_LONG_2ADDR:
2275 case Instruction::MUL_LONG_2ADDR:
2276 case Instruction::DIV_LONG_2ADDR:
2277 case Instruction::REM_LONG_2ADDR:
2278 case Instruction::AND_LONG_2ADDR:
2279 case Instruction::OR_LONG_2ADDR:
2280 case Instruction::XOR_LONG_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002281 work_line_->CheckBinaryOp2addrWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2282 reg_types_.LongLo(), reg_types_.LongHi(),
2283 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002284 break;
2285 case Instruction::SHL_LONG_2ADDR:
2286 case Instruction::SHR_LONG_2ADDR:
2287 case Instruction::USHR_LONG_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002288 work_line_->CheckBinaryOp2addrWideShift(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2289 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002290 break;
2291 case Instruction::ADD_FLOAT_2ADDR:
2292 case Instruction::SUB_FLOAT_2ADDR:
2293 case Instruction::MUL_FLOAT_2ADDR:
2294 case Instruction::DIV_FLOAT_2ADDR:
2295 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002296 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002297 break;
2298 case Instruction::ADD_DOUBLE_2ADDR:
2299 case Instruction::SUB_DOUBLE_2ADDR:
2300 case Instruction::MUL_DOUBLE_2ADDR:
2301 case Instruction::DIV_DOUBLE_2ADDR:
2302 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002303 work_line_->CheckBinaryOp2addrWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2304 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2305 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002306 break;
2307 case Instruction::ADD_INT_LIT16:
2308 case Instruction::RSUB_INT:
2309 case Instruction::MUL_INT_LIT16:
2310 case Instruction::DIV_INT_LIT16:
2311 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002312 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002313 break;
2314 case Instruction::AND_INT_LIT16:
2315 case Instruction::OR_INT_LIT16:
2316 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002317 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002318 break;
2319 case Instruction::ADD_INT_LIT8:
2320 case Instruction::RSUB_INT_LIT8:
2321 case Instruction::MUL_INT_LIT8:
2322 case Instruction::DIV_INT_LIT8:
2323 case Instruction::REM_INT_LIT8:
2324 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002325 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002326 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002327 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002328 break;
2329 case Instruction::AND_INT_LIT8:
2330 case Instruction::OR_INT_LIT8:
2331 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002332 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002333 break;
2334
Ian Rogersd81871c2011-10-03 13:57:23 -07002335 /* These should never appear during verification. */
jeffhao9a4f0032012-08-30 16:17:40 -07002336 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002337 case Instruction::UNUSED_EE:
2338 case Instruction::UNUSED_EF:
2339 case Instruction::UNUSED_F2:
2340 case Instruction::UNUSED_F3:
2341 case Instruction::UNUSED_F4:
2342 case Instruction::UNUSED_F5:
2343 case Instruction::UNUSED_F6:
2344 case Instruction::UNUSED_F7:
2345 case Instruction::UNUSED_F8:
2346 case Instruction::UNUSED_F9:
2347 case Instruction::UNUSED_FA:
2348 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002349 case Instruction::UNUSED_F0:
2350 case Instruction::UNUSED_F1:
2351 case Instruction::UNUSED_E3:
2352 case Instruction::UNUSED_E8:
2353 case Instruction::UNUSED_E7:
2354 case Instruction::UNUSED_E4:
2355 case Instruction::UNUSED_E9:
2356 case Instruction::UNUSED_FC:
2357 case Instruction::UNUSED_E5:
2358 case Instruction::UNUSED_EA:
2359 case Instruction::UNUSED_FD:
2360 case Instruction::UNUSED_E6:
2361 case Instruction::UNUSED_EB:
2362 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002363 case Instruction::UNUSED_3E:
2364 case Instruction::UNUSED_3F:
2365 case Instruction::UNUSED_40:
2366 case Instruction::UNUSED_41:
2367 case Instruction::UNUSED_42:
2368 case Instruction::UNUSED_43:
2369 case Instruction::UNUSED_73:
2370 case Instruction::UNUSED_79:
2371 case Instruction::UNUSED_7A:
2372 case Instruction::UNUSED_EC:
2373 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002374 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002375 break;
2376
2377 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002378 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002379 * complain if an instruction is missing (which is desirable).
2380 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002381 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002382
Ian Rogersad0b3a32012-04-16 14:50:24 -07002383 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002384 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002385 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002386 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002387 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002388 /* immediate failure, reject class */
2389 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2390 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002391 } else if (have_pending_runtime_throw_failure_) {
2392 /* slow path will throw, mark following code as unreachable */
2393 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002394 }
jeffhaobdb76512011-09-07 11:43:16 -07002395 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002396 * If we didn't just set the result register, clear it out. This ensures that you can only use
2397 * "move-result" immediately after the result is set. (We could check this statically, but it's
2398 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002399 */
2400 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002401 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002402 }
2403
jeffhaoa0a764a2011-09-16 10:43:38 -07002404 /* Handle "continue". Tag the next consecutive instruction. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002405 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers776ac1f2012-04-13 23:36:36 -07002406 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
Ian Rogersd81871c2011-10-03 13:57:23 -07002407 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
jeffhaod5347e02012-03-22 17:25:05 -07002408 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002409 return false;
2410 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002411 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2412 // next instruction isn't one.
jeffhaod5347e02012-03-22 17:25:05 -07002413 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002414 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002415 }
2416 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2417 if (next_line != NULL) {
2418 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2419 // needed.
2420 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002421 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002422 }
jeffhaobdb76512011-09-07 11:43:16 -07002423 } else {
2424 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002425 * We're not recording register data for the next instruction, so we don't know what the prior
2426 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002427 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002428 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002429 }
2430 }
2431
2432 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002433 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002434 *
2435 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002436 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002437 * somebody could get a reference field, check it for zero, and if the
2438 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002439 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002440 * that, and will reject the code.
2441 *
2442 * TODO: avoid re-fetching the branch target
2443 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002444 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002445 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002446 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002447 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002448 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002449 return false;
2450 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002451 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002452 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002453 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002454 }
jeffhaobdb76512011-09-07 11:43:16 -07002455 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002456 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002457 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002458 }
jeffhaobdb76512011-09-07 11:43:16 -07002459 }
2460
2461 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002462 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002463 *
2464 * We've already verified that the table is structurally sound, so we
2465 * just need to walk through and tag the targets.
2466 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002467 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002468 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2469 const uint16_t* switch_insns = insns + offset_to_switch;
2470 int switch_count = switch_insns[1];
2471 int offset_to_targets, targ;
2472
2473 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2474 /* 0 = sig, 1 = count, 2/3 = first key */
2475 offset_to_targets = 4;
2476 } else {
2477 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002478 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002479 offset_to_targets = 2 + 2 * switch_count;
2480 }
2481
2482 /* verify each switch target */
2483 for (targ = 0; targ < switch_count; targ++) {
2484 int offset;
2485 uint32_t abs_offset;
2486
2487 /* offsets are 32-bit, and only partly endian-swapped */
2488 offset = switch_insns[offset_to_targets + targ * 2] |
2489 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002490 abs_offset = work_insn_idx_ + offset;
2491 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002492 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002493 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002494 }
2495 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002496 return false;
2497 }
2498 }
2499
2500 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002501 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2502 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002503 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002504 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002505 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002506 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002507
Ian Rogers0571d352011-11-03 19:51:38 -07002508 for (; iterator.HasNext(); iterator.Next()) {
2509 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002510 within_catch_all = true;
2511 }
jeffhaobdb76512011-09-07 11:43:16 -07002512 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002513 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2514 * "work_regs", because at runtime the exception will be thrown before the instruction
2515 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002516 */
Ian Rogers0571d352011-11-03 19:51:38 -07002517 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002518 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002519 }
jeffhaobdb76512011-09-07 11:43:16 -07002520 }
2521
2522 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002523 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2524 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002525 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002526 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002527 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002528 * The state in work_line reflects the post-execution state. If the current instruction is a
2529 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002530 * it will do so before grabbing the lock).
2531 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002532 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002533 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002534 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002535 return false;
2536 }
2537 }
2538 }
2539
jeffhaod1f0fde2011-09-08 17:25:33 -07002540 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002541 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002542 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002543 return false;
2544 }
jeffhaobdb76512011-09-07 11:43:16 -07002545 }
2546
2547 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002548 * Update start_guess. Advance to the next instruction of that's
2549 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002550 * neither of those exists we're in a return or throw; leave start_guess
2551 * alone and let the caller sort it out.
2552 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002553 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002554 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002555 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002556 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002557 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002558 }
2559
Ian Rogersd81871c2011-10-03 13:57:23 -07002560 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2561 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002562
2563 return true;
2564}
2565
Ian Rogers776ac1f2012-04-13 23:36:36 -07002566const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002567 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002568 const RegType& referrer = GetDeclaringClass();
2569 Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002570 const RegType& result =
Ian Rogersb4903572012-10-11 11:52:56 -07002571 klass != NULL ? reg_types_.FromClass(klass, klass->IsFinal())
2572 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002573 if (result.IsConflict()) {
2574 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2575 << "' in " << referrer;
2576 return result;
2577 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002578 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002579 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002580 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002581 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002582 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002583 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002584 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002585 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002586 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002587 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002588}
2589
Ian Rogers776ac1f2012-04-13 23:36:36 -07002590const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002591 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002592 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002593 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002594 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2595 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002596 CatchHandlerIterator iterator(handlers_ptr);
2597 for (; iterator.HasNext(); iterator.Next()) {
2598 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2599 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002600 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002601 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002602 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002603 if (common_super == NULL) {
2604 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2605 // as that is caught at runtime
2606 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002607 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002608 // We don't know enough about the type and the common path merge will result in
2609 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002610 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002611 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002612 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002613 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002614 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002615 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002616 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002617 }
2618 }
2619 }
2620 }
Ian Rogers0571d352011-11-03 19:51:38 -07002621 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002622 }
2623 }
2624 if (common_super == NULL) {
2625 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002626 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002627 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002628 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002629 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002630}
2631
Mathieu Chartier66f19252012-09-18 08:57:04 -07002632AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002633 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002634 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002635 if (klass_type.IsConflict()) {
2636 std::string append(" in attempt to access method ");
2637 append += dex_file_->GetMethodName(method_id);
2638 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002639 return NULL;
2640 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002641 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002642 return NULL; // Can't resolve Class so no more to do here
2643 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002644 Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002645 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier66f19252012-09-18 08:57:04 -07002646 AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002647 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002648 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002649 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002650
2651 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002652 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002653 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002654 res_method = klass->FindInterfaceMethod(name, signature);
2655 } else {
2656 res_method = klass->FindVirtualMethod(name, signature);
2657 }
2658 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002659 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002660 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002661 // If a virtual or interface method wasn't found with the expected type, look in
2662 // the direct methods. This can happen when the wrong invoke type is used or when
2663 // a class has changed, and will be flagged as an error in later checks.
2664 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2665 res_method = klass->FindDirectMethod(name, signature);
2666 }
2667 if (res_method == NULL) {
2668 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2669 << PrettyDescriptor(klass) << "." << name
2670 << " " << signature;
2671 return NULL;
2672 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002673 }
2674 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002675 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2676 // enforce them here.
2677 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002678 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2679 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002680 return NULL;
2681 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002682 // Disallow any calls to class initializers.
2683 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002684 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2685 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002686 return NULL;
2687 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002688 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002689 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002690 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002691 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002692 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002693 }
jeffhaode0d9c92012-02-27 13:58:13 -08002694 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2695 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002696 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2697 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002698 return NULL;
2699 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002700 // Check that interface methods match interface classes.
2701 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2702 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2703 << " is in an interface class " << PrettyClass(klass);
2704 return NULL;
2705 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2706 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2707 << " is in a non-interface class " << PrettyClass(klass);
2708 return NULL;
2709 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002710 // See if the method type implied by the invoke instruction matches the access flags for the
2711 // target method.
2712 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2713 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2714 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2715 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002716 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2717 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002718 return NULL;
2719 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002720 return res_method;
2721}
2722
Mathieu Chartier66f19252012-09-18 08:57:04 -07002723AbstractMethod* MethodVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
Ian Rogers46685432012-06-03 22:26:43 -07002724 MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002725 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2726 // we're making.
Mathieu Chartier66f19252012-09-18 08:57:04 -07002727 AbstractMethod* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002728 if (res_method == NULL) { // error or class is unresolved
2729 return NULL;
2730 }
2731
Ian Rogersd81871c2011-10-03 13:57:23 -07002732 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2733 // has a vtable entry for the target method.
2734 if (is_super) {
2735 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002736 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002737 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002738 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002739 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002740 << " to super " << PrettyMethod(res_method);
2741 return NULL;
2742 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002743 Class* super_klass = super.GetClass();
2744 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002745 MethodHelper mh(res_method);
2746 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002747 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002748 << " to super " << super
2749 << "." << mh.GetName()
2750 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002751 return NULL;
2752 }
2753 }
2754 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2755 // match the call to the signature. Also, we might might be calling through an abstract method
2756 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08002757 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002758 /* caught by static verifier */
2759 DCHECK(is_range || expected_args <= 5);
2760 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002761 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002762 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2763 return NULL;
2764 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002765
jeffhaobdb76512011-09-07 11:43:16 -07002766 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002767 * Check the "this" argument, which must be an instance of the class that declared the method.
2768 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2769 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002770 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002771 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002772 if (!res_method->IsStatic()) {
2773 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002774 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002775 return NULL;
2776 }
2777 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002778 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002779 return NULL;
2780 }
2781 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersb4903572012-10-11 11:52:56 -07002782 Class* klass = res_method->GetDeclaringClass();
2783 const RegType& res_method_class = reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogers9074b992011-10-26 17:41:55 -07002784 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002785 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002786 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002787 return NULL;
2788 }
2789 }
2790 actual_args++;
2791 }
2792 /*
2793 * Process the target method's signature. This signature may or may not
2794 * have been verified, so we can't assume it's properly formed.
2795 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002796 MethodHelper mh(res_method);
2797 const DexFile::TypeList* params = mh.GetParameterTypeList();
2798 size_t params_size = params == NULL ? 0 : params->Size();
2799 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002800 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002801 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002802 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2803 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002804 return NULL;
2805 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002806 const char* descriptor =
2807 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2808 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002809 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002810 << " missing signature component";
2811 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002812 }
Ian Rogersb4903572012-10-11 11:52:56 -07002813 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002814 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002815 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002816 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 }
2818 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2819 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002820 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002821 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002822 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002823 return NULL;
2824 } else {
2825 return res_method;
2826 }
2827}
2828
Ian Rogers776ac1f2012-04-13 23:36:36 -07002829void MethodVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08002830 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002831 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002832 if (res_type.IsConflict()) { // bad class
2833 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002834 } else {
2835 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2836 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002837 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002838 } else if (!is_filled) {
2839 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002840 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002841 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002842 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002843 } else {
2844 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2845 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002846 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002847 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002848 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002849 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002850 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002851 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002852 return;
2853 }
2854 }
2855 // filled-array result goes into "result" register
2856 work_line_->SetResultRegisterType(res_type);
2857 }
2858 }
2859}
2860
Ian Rogers776ac1f2012-04-13 23:36:36 -07002861void MethodVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002862 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002863 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002864 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002865 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002866 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002867 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002868 if (array_type.IsZero()) {
2869 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2870 // instruction type. TODO: have a proper notion of bottom here.
2871 if (!is_primitive || insn_type.IsCategory1Types()) {
2872 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08002873 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002874 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002875 // Category 2
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002876 work_line_->SetRegisterTypeWide(dec_insn.vA, reg_types_.FromCat2ConstLo(0, false),
2877 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08002878 }
jeffhaofc3144e2012-02-01 17:21:15 -08002879 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002880 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002881 } else {
2882 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002883 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002884 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002885 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002886 << " source for aget-object";
2887 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002888 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002889 << " source for category 1 aget";
2890 } else if (is_primitive && !insn_type.Equals(component_type) &&
2891 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002892 (insn_type.IsLong() && component_type.IsDouble()))) {
2893 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
2894 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002895 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002896 // Use knowledge of the field type which is stronger than the type inferred from the
2897 // instruction, which can't differentiate object types and ints from floats, longs from
2898 // doubles.
2899 if (!component_type.IsLowHalf()) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002900 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002901 } else {
2902 work_line_->SetRegisterTypeWide(dec_insn.vA, component_type,
2903 component_type.HighHalf(&reg_types_));
2904 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002905 }
2906 }
2907 }
2908}
2909
Ian Rogers776ac1f2012-04-13 23:36:36 -07002910void MethodVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002911 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002912 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002913 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002914 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002915 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002916 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002917 if (array_type.IsZero()) {
2918 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2919 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002920 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002921 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002922 } else {
2923 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002924 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002925 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002926 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002927 << " source for aput-object";
2928 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002929 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002930 << " source for category 1 aput";
2931 } else if (is_primitive && !insn_type.Equals(component_type) &&
2932 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2933 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002934 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002935 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002936 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002937 // The instruction agrees with the type of array, confirm the value to be stored does too
2938 // Note: we use the instruction type (rather than the component type) for aput-object as
2939 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08002940 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002941 }
2942 }
2943 }
2944}
2945
Ian Rogers776ac1f2012-04-13 23:36:36 -07002946Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002947 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2948 // Check access to class
2949 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002950 if (klass_type.IsConflict()) { // bad class
2951 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
2952 field_idx, dex_file_->GetFieldName(field_id),
2953 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002954 return NULL;
2955 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002956 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002957 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08002958 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002959 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2960 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002961 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002962 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
2963 << dex_file_->GetFieldName(field_id) << ") in "
2964 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002965 DCHECK(Thread::Current()->IsExceptionPending());
2966 Thread::Current()->ClearException();
2967 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002968 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2969 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002970 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002971 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002972 return NULL;
2973 } else if (!field->IsStatic()) {
2974 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
2975 return NULL;
2976 } else {
2977 return field;
2978 }
2979}
2980
Ian Rogers776ac1f2012-04-13 23:36:36 -07002981Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002982 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2983 // Check access to class
2984 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002985 if (klass_type.IsConflict()) {
2986 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
2987 field_idx, dex_file_->GetFieldName(field_id),
2988 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002989 return NULL;
2990 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002991 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002992 return NULL; // Can't resolve Class so no more to do here
2993 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002994 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2995 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002996 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002997 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
2998 << dex_file_->GetFieldName(field_id) << ") in "
2999 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003000 DCHECK(Thread::Current()->IsExceptionPending());
3001 Thread::Current()->ClearException();
3002 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003003 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3004 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003005 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003006 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003007 return NULL;
3008 } else if (field->IsStatic()) {
3009 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3010 << " to not be static";
3011 return NULL;
3012 } else if (obj_type.IsZero()) {
3013 // Cannot infer and check type, however, access will cause null pointer exception
3014 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003015 } else {
Ian Rogersb4903572012-10-11 11:52:56 -07003016 Class* klass = field->GetDeclaringClass();
3017 const RegType& field_klass = reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003018 if (obj_type.IsUninitializedTypes() &&
3019 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3020 !field_klass.Equals(GetDeclaringClass()))) {
3021 // Field accesses through uninitialized references are only allowable for constructors where
3022 // the field is declared in this class
3023 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
3024 << " of a not fully initialized object within the context of "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003025 << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003026 return NULL;
3027 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3028 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3029 // of C1. For resolution to occur the declared class of the field must be compatible with
3030 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3031 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3032 << " from object of type " << obj_type;
3033 return NULL;
3034 } else {
3035 return field;
3036 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003037 }
3038}
3039
Ian Rogers776ac1f2012-04-13 23:36:36 -07003040void MethodVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003041 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003042 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003043 Field* field;
3044 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003045 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003046 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003047 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003048 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003049 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003050 const char* descriptor;
Ian Rogers365c1022012-06-22 15:05:28 -07003051 ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003052 if (field != NULL) {
3053 descriptor = FieldHelper(field).GetTypeDescriptor();
3054 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003055 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003056 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3057 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3058 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07003059 }
Ian Rogersb4903572012-10-11 11:52:56 -07003060 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003061 if (is_primitive) {
3062 if (field_type.Equals(insn_type) ||
3063 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3064 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3065 // expected that read is of the correct primitive type or that int reads are reading
3066 // floats or long reads are reading doubles
3067 } else {
3068 // This is a global failure rather than a class change failure as the instructions and
3069 // the descriptors for the type should have been consistent within the same file at
3070 // compile time
3071 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3072 << " to be of type '" << insn_type
3073 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003074 return;
3075 }
3076 } else {
3077 if (!insn_type.IsAssignableFrom(field_type)) {
3078 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3079 << " to be compatible with type '" << insn_type
3080 << "' but found type '" << field_type
3081 << "' in get-object";
3082 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Conflict());
3083 return;
3084 }
3085 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003086 if (!field_type.IsLowHalf()) {
3087 work_line_->SetRegisterType(dec_insn.vA, field_type);
3088 } else {
3089 work_line_->SetRegisterTypeWide(dec_insn.vA, field_type, field_type.HighHalf(&reg_types_));
3090 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003091}
3092
Ian Rogers776ac1f2012-04-13 23:36:36 -07003093void MethodVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003094 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003095 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003096 Field* field;
3097 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003098 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003099 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003100 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07003101 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003102 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003103 const char* descriptor;
Ian Rogers365c1022012-06-22 15:05:28 -07003104 ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003105 if (field != NULL) {
3106 descriptor = FieldHelper(field).GetTypeDescriptor();
3107 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003108 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003109 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3110 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3111 loader = class_loader_;
3112 }
Ian Rogersb4903572012-10-11 11:52:56 -07003113 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003114 if (field != NULL) {
3115 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3116 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3117 << " from other class " << GetDeclaringClass();
3118 return;
3119 }
3120 }
3121 if (is_primitive) {
3122 // Primitive field assignability rules are weaker than regular assignability rules
3123 bool instruction_compatible;
3124 bool value_compatible;
3125 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
3126 if (field_type.IsIntegralTypes()) {
3127 instruction_compatible = insn_type.IsIntegralTypes();
3128 value_compatible = value_type.IsIntegralTypes();
3129 } else if (field_type.IsFloat()) {
3130 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3131 value_compatible = value_type.IsFloatTypes();
3132 } else if (field_type.IsLong()) {
3133 instruction_compatible = insn_type.IsLong();
3134 value_compatible = value_type.IsLongTypes();
3135 } else if (field_type.IsDouble()) {
3136 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3137 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003138 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003139 instruction_compatible = false; // reference field with primitive store
3140 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003141 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003142 if (!instruction_compatible) {
3143 // This is a global failure rather than a class change failure as the instructions and
3144 // the descriptors for the type should have been consistent within the same file at
3145 // compile time
3146 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3147 << " to be of type '" << insn_type
3148 << "' but found type '" << field_type
3149 << "' in put";
3150 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003151 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003152 if (!value_compatible) {
3153 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
3154 << " of type " << value_type
3155 << " but expected " << field_type
3156 << " for store to " << PrettyField(field) << " in put";
3157 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003158 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003159 } else {
3160 if (!insn_type.IsAssignableFrom(field_type)) {
3161 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3162 << " to be compatible with type '" << insn_type
3163 << "' but found type '" << field_type
3164 << "' in put-object";
3165 return;
3166 }
3167 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003168 }
3169}
3170
Ian Rogers776ac1f2012-04-13 23:36:36 -07003171bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003172 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003173 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003174 return false;
3175 }
3176 return true;
3177}
3178
Ian Rogers776ac1f2012-04-13 23:36:36 -07003179bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003180 bool changed = true;
3181 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3182 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003183 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003184 * We haven't processed this instruction before, and we haven't touched the registers here, so
3185 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3186 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003187 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003188 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003189 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003190 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3191 if (gDebugVerify) {
3192 copy->CopyFromLine(target_line);
3193 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003194 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003195 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003196 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003197 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003198 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003199 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003200 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3201 << *copy.get() << " MERGE\n"
3202 << *merge_line << " ==\n"
3203 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003204 }
3205 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003206 if (changed) {
3207 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003208 }
3209 return true;
3210}
3211
Ian Rogers776ac1f2012-04-13 23:36:36 -07003212InsnFlags* MethodVerifier::CurrentInsnFlags() {
3213 return &insn_flags_[work_insn_idx_];
3214}
3215
Ian Rogersad0b3a32012-04-16 14:50:24 -07003216const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003217 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003218 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3219 uint16_t return_type_idx = proto_id.return_type_idx_;
3220 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003221 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003222}
3223
3224const RegType& MethodVerifier::GetDeclaringClass() {
3225 if (foo_method_ != NULL) {
Ian Rogersb4903572012-10-11 11:52:56 -07003226 Class* klass = foo_method_->GetDeclaringClass();
3227 return reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003228 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003229 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003230 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogersb4903572012-10-11 11:52:56 -07003231 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003232 }
3233}
3234
Ian Rogers776ac1f2012-04-13 23:36:36 -07003235void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003236 size_t* log2_max_gc_pc) {
3237 size_t local_gc_points = 0;
3238 size_t max_insn = 0;
3239 size_t max_ref_reg = -1;
3240 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3241 if (insn_flags_[i].IsGcPoint()) {
3242 local_gc_points++;
3243 max_insn = i;
3244 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003245 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003246 }
3247 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003248 *gc_points = local_gc_points;
3249 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3250 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003251 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003252 i++;
3253 }
3254 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003255}
3256
Ian Rogers776ac1f2012-04-13 23:36:36 -07003257const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003258 size_t num_entries, ref_bitmap_bits, pc_bits;
3259 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3260 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003261 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003262 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003263 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003264 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003265 return NULL;
3266 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003267 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3268 // There are 2 bytes to encode the number of entries
3269 if (num_entries >= 65536) {
3270 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003271 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003272 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003273 return NULL;
3274 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003275 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003276 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003277 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003278 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003279 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003280 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003281 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003282 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003283 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003284 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003285 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003286 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3287 return NULL;
3288 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003289 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003290 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003291 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003292 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003293 return NULL;
3294 }
3295 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003296 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3297 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003298 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003299 table->push_back(num_entries & 0xFF);
3300 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003301 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003302 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3303 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003304 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003305 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003306 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003307 }
3308 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003309 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003310 }
3311 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003312 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003313 return table;
3314}
jeffhaoa0a764a2011-09-16 10:43:38 -07003315
Ian Rogers776ac1f2012-04-13 23:36:36 -07003316void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003317 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3318 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003319 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003320 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003321 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003322 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3323 if (insn_flags_[i].IsGcPoint()) {
3324 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003325 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003326 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3327 map_index++;
3328 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003329 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003330 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003331 CHECK_LT(j / 8, map.RegWidth());
3332 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3333 } else if ((j / 8) < map.RegWidth()) {
3334 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3335 } else {
3336 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3337 }
3338 }
3339 } else {
3340 CHECK(reg_bitmap == NULL);
3341 }
3342 }
3343}
jeffhaoa0a764a2011-09-16 10:43:38 -07003344
Ian Rogers0c7abda2012-09-19 13:33:42 -07003345void MethodVerifier::SetDexGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003346 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003347 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003348 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
3349 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003350 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003351 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003352 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003353 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003354 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003355 CHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003356}
3357
Ian Rogers0c7abda2012-09-19 13:33:42 -07003358const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(Compiler::MethodReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003359 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003360 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
3361 if (it == dex_gc_maps_->end()) {
Ian Rogers64b6d142012-10-29 16:34:15 -07003362 LOG(WARNING) << "Didn't find GC map for: " << PrettyMethod(ref.second, *ref.first);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003363 return NULL;
3364 }
3365 CHECK(it->second != NULL);
3366 return it->second;
3367}
3368
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003369std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3370 RegisterLine* line = reg_table_.GetLine(dex_pc);
3371 std::vector<int32_t> result;
3372 for (size_t i = 0; i < line->NumRegs(); ++i) {
3373 const RegType& type = line->GetRegisterType(i);
3374 if (type.IsConstant()) {
3375 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3376 result.push_back(type.ConstantValue());
3377 } else if (type.IsConstantLo()) {
3378 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3379 result.push_back(type.ConstantValueLo());
3380 } else if (type.IsConstantHi()) {
3381 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3382 result.push_back(type.ConstantValueHi());
3383 } else if (type.IsIntegralTypes()) {
3384 result.push_back(kIntVReg);
3385 result.push_back(0);
3386 } else if (type.IsFloat()) {
3387 result.push_back(kFloatVReg);
3388 result.push_back(0);
3389 } else if (type.IsLong()) {
3390 result.push_back(kLongLoVReg);
3391 result.push_back(0);
3392 result.push_back(kLongHiVReg);
3393 result.push_back(0);
3394 ++i;
3395 } else if (type.IsDouble()) {
3396 result.push_back(kDoubleLoVReg);
3397 result.push_back(0);
3398 result.push_back(kDoubleHiVReg);
3399 result.push_back(0);
3400 ++i;
3401 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
3402 result.push_back(kUndefined);
3403 result.push_back(0);
3404 } else {
3405 CHECK(type.IsNonZeroReferenceTypes()) << type;
3406 result.push_back(kReferenceVReg);
3407 result.push_back(0);
3408 }
3409 }
3410 return result;
3411}
3412
Ian Rogers0c7abda2012-09-19 13:33:42 -07003413Mutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
3414MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003415
3416Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
3417MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
3418
buzbeec531cef2012-10-18 07:09:20 -07003419#if defined(ART_USE_LLVM_COMPILER)
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003420Mutex* MethodVerifier::inferred_reg_category_maps_lock_ = NULL;
3421MethodVerifier::InferredRegCategoryMapTable* MethodVerifier::inferred_reg_category_maps_ = NULL;
3422#endif
3423
3424void MethodVerifier::Init() {
Ian Rogers0c7abda2012-09-19 13:33:42 -07003425 dex_gc_maps_lock_ = new Mutex("verifier GC maps lock");
Ian Rogers50b35e22012-10-04 10:09:15 -07003426 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003427 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003428 MutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003429 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003430 }
3431
3432 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
3433 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003434 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003435 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
3436 }
3437
buzbeec531cef2012-10-18 07:09:20 -07003438#if defined(ART_USE_LLVM_COMPILER)
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003439 inferred_reg_category_maps_lock_ = new Mutex("verifier GC maps lock");
3440 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003441 MutexLock mu(self, *inferred_reg_category_maps_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003442 inferred_reg_category_maps_ = new MethodVerifier::InferredRegCategoryMapTable;
3443 }
3444#endif
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003445}
3446
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003447void MethodVerifier::Shutdown() {
Ian Rogers50b35e22012-10-04 10:09:15 -07003448 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003449 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003450 MutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003451 STLDeleteValues(dex_gc_maps_);
3452 delete dex_gc_maps_;
3453 dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003454 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003455 delete dex_gc_maps_lock_;
3456 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003457
3458 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003459 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003460 delete rejected_classes_;
3461 rejected_classes_ = NULL;
3462 }
3463 delete rejected_classes_lock_;
3464 rejected_classes_lock_ = NULL;
3465
buzbeec531cef2012-10-18 07:09:20 -07003466#if defined(ART_USE_LLVM_COMPILER)
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003467 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003468 MutexLock mu(self, *inferred_reg_category_maps_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003469 STLDeleteValues(inferred_reg_category_maps_);
3470 delete inferred_reg_category_maps_;
3471 inferred_reg_category_maps_ = NULL;
3472 }
3473 delete inferred_reg_category_maps_lock_;
3474 inferred_reg_category_maps_lock_ = NULL;
3475#endif
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003476}
jeffhaod1224c72012-02-29 13:43:08 -08003477
Ian Rogers776ac1f2012-04-13 23:36:36 -07003478void MethodVerifier::AddRejectedClass(Compiler::ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003479 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003480 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003481 rejected_classes_->insert(ref);
3482 }
jeffhaod1224c72012-02-29 13:43:08 -08003483 CHECK(IsClassRejected(ref));
3484}
3485
Ian Rogers776ac1f2012-04-13 23:36:36 -07003486bool MethodVerifier::IsClassRejected(Compiler::ClassReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003487 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003488 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08003489}
3490
buzbeec531cef2012-10-18 07:09:20 -07003491#if defined(ART_USE_LLVM_COMPILER)
TDYa12789f96052012-07-12 20:49:53 -07003492const greenland::InferredRegCategoryMap* MethodVerifier::GenerateInferredRegCategoryMap() {
Logan Chienfca7e872011-12-20 20:08:22 +08003493 uint32_t insns_size = code_item_->insns_size_in_code_units_;
3494 uint16_t regs_size = code_item_->registers_size_;
3495
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003496 UniquePtr<InferredRegCategoryMap> table(new InferredRegCategoryMap(insns_size, regs_size));
Logan Chienfca7e872011-12-20 20:08:22 +08003497
3498 for (size_t i = 0; i < insns_size; ++i) {
3499 if (RegisterLine* line = reg_table_.GetLine(i)) {
TDYa127526643e2012-05-26 01:01:48 -07003500 const Instruction* inst = Instruction::At(code_item_->insns_ + i);
3501
3502 // GC points
3503 if (inst->IsBranch() || inst->IsInvoke()) {
3504 for (size_t r = 0; r < regs_size; ++r) {
3505 const RegType &rt = line->GetRegisterType(r);
3506 if (rt.IsNonZeroReferenceTypes()) {
3507 table->SetRegCanBeObject(r);
3508 }
TDYa127b2eb5c12012-05-24 15:52:10 -07003509 }
3510 }
3511
TDYa127526643e2012-05-26 01:01:48 -07003512 /* We only use InferredRegCategoryMap in one case */
3513 if (inst->IsBranch()) {
TDYa127b2eb5c12012-05-24 15:52:10 -07003514 for (size_t r = 0; r < regs_size; ++r) {
3515 const RegType &rt = line->GetRegisterType(r);
3516
3517 if (rt.IsZero()) {
TDYa12789f96052012-07-12 20:49:53 -07003518 table->SetRegCategory(i, r, greenland::kRegZero);
TDYa127b2eb5c12012-05-24 15:52:10 -07003519 } else if (rt.IsCategory1Types()) {
TDYa12789f96052012-07-12 20:49:53 -07003520 table->SetRegCategory(i, r, greenland::kRegCat1nr);
TDYa127b2eb5c12012-05-24 15:52:10 -07003521 } else if (rt.IsCategory2Types()) {
TDYa12789f96052012-07-12 20:49:53 -07003522 table->SetRegCategory(i, r, greenland::kRegCat2);
TDYa127b2eb5c12012-05-24 15:52:10 -07003523 } else if (rt.IsReferenceTypes()) {
TDYa12789f96052012-07-12 20:49:53 -07003524 table->SetRegCategory(i, r, greenland::kRegObject);
TDYa127b2eb5c12012-05-24 15:52:10 -07003525 } else {
TDYa12789f96052012-07-12 20:49:53 -07003526 table->SetRegCategory(i, r, greenland::kRegUnknown);
TDYa127b2eb5c12012-05-24 15:52:10 -07003527 }
Logan Chienfca7e872011-12-20 20:08:22 +08003528 }
3529 }
3530 }
3531 }
3532
3533 return table.release();
3534}
Logan Chiendd361c92012-04-10 23:40:37 +08003535
Ian Rogers776ac1f2012-04-13 23:36:36 -07003536void MethodVerifier::SetInferredRegCategoryMap(Compiler::MethodReference ref,
3537 const InferredRegCategoryMap& inferred_reg_category_map) {
Shih-wei Liaocd05a622012-08-15 00:02:05 -07003538 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003539 MutexLock mu(Thread::Current(), *inferred_reg_category_maps_lock_);
Shih-wei Liaocd05a622012-08-15 00:02:05 -07003540 InferredRegCategoryMapTable::iterator it = inferred_reg_category_maps_->find(ref);
3541 if (it == inferred_reg_category_maps_->end()) {
3542 inferred_reg_category_maps_->Put(ref, &inferred_reg_category_map);
3543 } else {
3544 CHECK(*(it->second) == inferred_reg_category_map);
3545 delete &inferred_reg_category_map;
3546 }
Logan Chiendd361c92012-04-10 23:40:37 +08003547 }
Logan Chiendd361c92012-04-10 23:40:37 +08003548 CHECK(GetInferredRegCategoryMap(ref) != NULL);
3549}
3550
TDYa12789f96052012-07-12 20:49:53 -07003551const greenland::InferredRegCategoryMap*
Ian Rogers776ac1f2012-04-13 23:36:36 -07003552MethodVerifier::GetInferredRegCategoryMap(Compiler::MethodReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003553 MutexLock mu(Thread::Current(), *inferred_reg_category_maps_lock_);
Logan Chiendd361c92012-04-10 23:40:37 +08003554
3555 InferredRegCategoryMapTable::const_iterator it =
3556 inferred_reg_category_maps_->find(ref);
3557
3558 if (it == inferred_reg_category_maps_->end()) {
3559 return NULL;
3560 }
3561 CHECK(it->second != NULL);
3562 return it->second;
3563}
Logan Chienfca7e872011-12-20 20:08:22 +08003564#endif
3565
Ian Rogersd81871c2011-10-03 13:57:23 -07003566} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003567} // namespace art