blob: 7afa6d475e93495045429b725c70225520cce671 [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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080022#include "base/stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070023#include "class_linker.h"
Brian Carlstrome7d856b2012-01-11 18:10:55 -080024#include "compiler.h"
jeffhaob4df5142011-09-19 20:25:32 -070025#include "dex_cache.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#include "dex_file.h"
27#include "dex_instruction.h"
28#include "dex_instruction_visitor.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080029#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070030#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070031#include "leb128.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 Hughese222ee02012-12-13 14:41:43 -080034#include "verifier/dex_gc_map.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,
Elliott Hughes80537bb2013-01-04 16:37:26 -0800298 method, method_access_flags, true);
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,
Elliott Hughes80537bb2013-01-04 16:37:26 -0800334 dex_method_idx, method, method_access_flags, true);
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,
Elliott Hughes80537bb2013-01-04 16:37:26 -0800349 dex_method_idx, method, method_access_flags, true);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800350 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,
Elliott Hughes80537bb2013-01-04 16:37:26 -0800356 uint32_t dex_method_idx, AbstractMethod* method, uint32_t method_access_flags,
357 bool can_load_classes)
358 : reg_types_(can_load_classes),
359 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800360 dex_method_idx_(dex_method_idx),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700361 foo_method_(method),
362 method_access_flags_(method_access_flags),
jeffhaof56197c2012-03-05 18:01:54 -0800363 dex_file_(dex_file),
364 dex_cache_(dex_cache),
365 class_loader_(class_loader),
366 class_def_idx_(class_def_idx),
367 code_item_(code_item),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700368 interesting_dex_pc_(-1),
369 monitor_enter_dex_pcs_(NULL),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700370 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700371 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800372 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800373 monitor_enter_count_(0),
374 can_load_classes_(can_load_classes) {
jeffhaof56197c2012-03-05 18:01:54 -0800375}
376
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800377void MethodVerifier::FindLocksAtDexPc(AbstractMethod* m, uint32_t dex_pc,
378 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700379 MethodHelper mh(m);
380 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
381 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800382 m, m->GetAccessFlags(), false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700383 verifier.interesting_dex_pc_ = dex_pc;
384 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
385 verifier.FindLocksAtDexPc();
386}
387
388void MethodVerifier::FindLocksAtDexPc() {
389 CHECK(monitor_enter_dex_pcs_ != NULL);
390 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
391
392 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
393 // verification. In practice, the phase we want relies on data structures set up by all the
394 // earlier passes, so we just run the full method verification and bail out early when we've
395 // got what we wanted.
396 Verify();
397}
398
Ian Rogersad0b3a32012-04-16 14:50:24 -0700399bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700400 // If there aren't any instructions, make sure that's expected, then exit successfully.
401 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700402 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700403 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700404 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700405 } else {
406 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700407 }
jeffhaobdb76512011-09-07 11:43:16 -0700408 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700409 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
410 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700411 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
412 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700413 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700414 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700415 // Allocate and initialize an array to hold instruction data.
416 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
417 // Run through the instructions and see if the width checks out.
418 bool result = ComputeWidthsAndCountOps();
419 // Flag instructions guarded by a "try" block and check exception handlers.
420 result = result && ScanTryCatchBlocks();
421 // Perform static instruction verification.
422 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700423 // Perform code-flow analysis and return.
424 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700425}
426
Ian Rogers776ac1f2012-04-13 23:36:36 -0700427std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700428 switch (error) {
429 case VERIFY_ERROR_NO_CLASS:
430 case VERIFY_ERROR_NO_FIELD:
431 case VERIFY_ERROR_NO_METHOD:
432 case VERIFY_ERROR_ACCESS_CLASS:
433 case VERIFY_ERROR_ACCESS_FIELD:
434 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700435 case VERIFY_ERROR_INSTANTIATION:
436 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800437 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700438 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
439 // class change and instantiation errors into soft verification errors so that we re-verify
440 // at runtime. We may fail to find or to agree on access because of not yet available class
441 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
442 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
443 // paths" that dynamically perform the verification and cause the behavior to be that akin
444 // to an interpreter.
445 error = VERIFY_ERROR_BAD_CLASS_SOFT;
446 } else {
447 have_pending_runtime_throw_failure_ = true;
448 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700449 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700450 // Indication that verification should be retried at runtime.
451 case VERIFY_ERROR_BAD_CLASS_SOFT:
452 if (!Runtime::Current()->IsCompiler()) {
453 // It is runtime so hard fail.
454 have_pending_hard_failure_ = true;
455 }
456 break;
jeffhaod5347e02012-03-22 17:25:05 -0700457 // Hard verification failures at compile time will still fail at runtime, so the class is
458 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700459 case VERIFY_ERROR_BAD_CLASS_HARD: {
460 if (Runtime::Current()->IsCompiler()) {
jeffhaof56197c2012-03-05 18:01:54 -0800461 Compiler::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800462 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800463 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700464 have_pending_hard_failure_ = true;
465 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800466 }
467 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700468 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800469 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700470 work_insn_idx_));
471 std::ostringstream* failure_message = new std::ostringstream(location);
472 failure_messages_.push_back(failure_message);
473 return *failure_message;
474}
475
476void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
477 size_t failure_num = failure_messages_.size();
478 DCHECK_NE(failure_num, 0U);
479 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
480 prepend += last_fail_message->str();
481 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
482 delete last_fail_message;
483}
484
485void MethodVerifier::AppendToLastFailMessage(std::string append) {
486 size_t failure_num = failure_messages_.size();
487 DCHECK_NE(failure_num, 0U);
488 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
489 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800490}
491
Ian Rogers776ac1f2012-04-13 23:36:36 -0700492bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700493 const uint16_t* insns = code_item_->insns_;
494 size_t insns_size = code_item_->insns_size_in_code_units_;
495 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700496 size_t new_instance_count = 0;
497 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700498 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700499
Ian Rogersd81871c2011-10-03 13:57:23 -0700500 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700501 Instruction::Code opcode = inst->Opcode();
502 if (opcode == Instruction::NEW_INSTANCE) {
503 new_instance_count++;
504 } else if (opcode == Instruction::MONITOR_ENTER) {
505 monitor_enter_count++;
506 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700507 size_t inst_size = inst->SizeInCodeUnits();
508 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
509 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700510 inst = inst->Next();
511 }
512
Ian Rogersd81871c2011-10-03 13:57:23 -0700513 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700514 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
515 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700516 return false;
517 }
518
Ian Rogersd81871c2011-10-03 13:57:23 -0700519 new_instance_count_ = new_instance_count;
520 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700521 return true;
522}
523
Ian Rogers776ac1f2012-04-13 23:36:36 -0700524bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700525 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700526 if (tries_size == 0) {
527 return true;
528 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700529 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700530 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700531
532 for (uint32_t idx = 0; idx < tries_size; idx++) {
533 const DexFile::TryItem* try_item = &tries[idx];
534 uint32_t start = try_item->start_addr_;
535 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700536 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700537 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
538 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700539 return false;
540 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700541 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700542 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700543 return false;
544 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700545 for (uint32_t dex_pc = start; dex_pc < end;
546 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
547 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700548 }
549 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800550 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700551 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700552 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700553 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700554 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700555 CatchHandlerIterator iterator(handlers_ptr);
556 for (; iterator.HasNext(); iterator.Next()) {
557 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700558 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700559 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700560 return false;
561 }
jeffhao60f83e32012-02-13 17:16:30 -0800562 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
563 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700564 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700565 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800566 return false;
567 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700568 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700569 // Ensure exception types are resolved so that they don't need resolution to be delivered,
570 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700571 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
jeffhaof56197c2012-03-05 18:01:54 -0800572 Class* exception_type = linker->ResolveType(*dex_file_, iterator.GetHandlerTypeIndex(),
573 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700574 if (exception_type == NULL) {
575 DCHECK(Thread::Current()->IsExceptionPending());
576 Thread::Current()->ClearException();
577 }
578 }
jeffhaobdb76512011-09-07 11:43:16 -0700579 }
Ian Rogers0571d352011-11-03 19:51:38 -0700580 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700581 }
jeffhaobdb76512011-09-07 11:43:16 -0700582 return true;
583}
584
Ian Rogers776ac1f2012-04-13 23:36:36 -0700585bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700586 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700587
Ian Rogers0c7abda2012-09-19 13:33:42 -0700588 /* 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 -0700589 insn_flags_[0].SetBranchTarget();
Ian Rogers0c7abda2012-09-19 13:33:42 -0700590 insn_flags_[0].SetGcPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700591
592 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700593 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700594 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700595 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700596 return false;
597 }
598 /* Flag instructions that are garbage collection points */
599 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
600 insn_flags_[dex_pc].SetGcPoint();
601 }
602 dex_pc += inst->SizeInCodeUnits();
603 inst = inst->Next();
604 }
605 return true;
606}
607
Ian Rogers776ac1f2012-04-13 23:36:36 -0700608bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800609 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700610 bool result = true;
611 switch (inst->GetVerifyTypeArgumentA()) {
612 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800613 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700614 break;
615 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800616 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700617 break;
618 }
619 switch (inst->GetVerifyTypeArgumentB()) {
620 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800621 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700622 break;
623 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800624 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700625 break;
626 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800627 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700628 break;
629 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800630 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700631 break;
632 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800633 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700634 break;
635 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800636 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700637 break;
638 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800639 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700640 break;
641 }
642 switch (inst->GetVerifyTypeArgumentC()) {
643 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800644 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700645 break;
646 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800647 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700648 break;
649 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800650 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700651 break;
652 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800653 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700654 break;
655 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800656 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700657 break;
658 }
659 switch (inst->GetVerifyExtraFlags()) {
660 case Instruction::kVerifyArrayData:
661 result = result && CheckArrayData(code_offset);
662 break;
663 case Instruction::kVerifyBranchTarget:
664 result = result && CheckBranchTarget(code_offset);
665 break;
666 case Instruction::kVerifySwitchTargets:
667 result = result && CheckSwitchTargets(code_offset);
668 break;
669 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800670 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700671 break;
672 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800673 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 break;
675 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700676 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700677 result = false;
678 break;
679 }
680 return result;
681}
682
Ian Rogers776ac1f2012-04-13 23:36:36 -0700683bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700684 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700685 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
686 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700687 return false;
688 }
689 return true;
690}
691
Ian Rogers776ac1f2012-04-13 23:36:36 -0700692bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700693 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700694 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
695 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700696 return false;
697 }
698 return true;
699}
700
Ian Rogers776ac1f2012-04-13 23:36:36 -0700701bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700702 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700703 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
704 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700705 return false;
706 }
707 return true;
708}
709
Ian Rogers776ac1f2012-04-13 23:36:36 -0700710bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700711 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700712 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
713 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700714 return false;
715 }
716 return true;
717}
718
Ian Rogers776ac1f2012-04-13 23:36:36 -0700719bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700720 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700721 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
722 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700723 return false;
724 }
725 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700726 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700727 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700728 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700729 return false;
730 }
731 return true;
732}
733
Ian Rogers776ac1f2012-04-13 23:36:36 -0700734bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700735 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700736 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
737 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700738 return false;
739 }
740 return true;
741}
742
Ian Rogers776ac1f2012-04-13 23:36:36 -0700743bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700744 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700745 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
746 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700747 return false;
748 }
749 return true;
750}
751
Ian Rogers776ac1f2012-04-13 23:36:36 -0700752bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700753 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700754 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
755 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700756 return false;
757 }
758 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700759 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700760 const char* cp = descriptor;
761 while (*cp++ == '[') {
762 bracket_count++;
763 }
764 if (bracket_count == 0) {
765 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700766 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700767 return false;
768 } else if (bracket_count > 255) {
769 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700770 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700771 return false;
772 }
773 return true;
774}
775
Ian Rogers776ac1f2012-04-13 23:36:36 -0700776bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700777 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
778 const uint16_t* insns = code_item_->insns_ + cur_offset;
779 const uint16_t* array_data;
780 int32_t array_data_offset;
781
782 DCHECK_LT(cur_offset, insn_count);
783 /* make sure the start of the array data table is in range */
784 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
785 if ((int32_t) cur_offset + array_data_offset < 0 ||
786 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700787 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
788 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700789 return false;
790 }
791 /* offset to array data table is a relative branch-style offset */
792 array_data = insns + array_data_offset;
793 /* make sure the table is 32-bit aligned */
794 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700795 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
796 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700797 return false;
798 }
799 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700800 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700801 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
802 /* make sure the end of the switch is in range */
803 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700804 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
805 << ", data offset " << array_data_offset << ", end "
806 << cur_offset + array_data_offset + table_size
807 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700808 return false;
809 }
810 return true;
811}
812
Ian Rogers776ac1f2012-04-13 23:36:36 -0700813bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700814 int32_t offset;
815 bool isConditional, selfOkay;
816 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
817 return false;
818 }
819 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700820 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 -0700821 return false;
822 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700823 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
824 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700825 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700826 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700827 return false;
828 }
829 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
830 int32_t abs_offset = cur_offset + offset;
831 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700832 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700833 << reinterpret_cast<void*>(abs_offset) << ") at "
834 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700835 return false;
836 }
837 insn_flags_[abs_offset].SetBranchTarget();
838 return true;
839}
840
Ian Rogers776ac1f2012-04-13 23:36:36 -0700841bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700842 bool* selfOkay) {
843 const uint16_t* insns = code_item_->insns_ + cur_offset;
844 *pConditional = false;
845 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700846 switch (*insns & 0xff) {
847 case Instruction::GOTO:
848 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700849 break;
850 case Instruction::GOTO_32:
851 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700852 *selfOkay = true;
853 break;
854 case Instruction::GOTO_16:
855 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700856 break;
857 case Instruction::IF_EQ:
858 case Instruction::IF_NE:
859 case Instruction::IF_LT:
860 case Instruction::IF_GE:
861 case Instruction::IF_GT:
862 case Instruction::IF_LE:
863 case Instruction::IF_EQZ:
864 case Instruction::IF_NEZ:
865 case Instruction::IF_LTZ:
866 case Instruction::IF_GEZ:
867 case Instruction::IF_GTZ:
868 case Instruction::IF_LEZ:
869 *pOffset = (int16_t) insns[1];
870 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700871 break;
872 default:
873 return false;
874 break;
875 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700876 return true;
877}
878
Ian Rogers776ac1f2012-04-13 23:36:36 -0700879bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700880 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700881 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700882 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700883 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700884 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
885 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700886 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
887 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700888 return false;
889 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700890 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700891 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700892 /* make sure the table is 32-bit aligned */
893 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700894 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
895 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700896 return false;
897 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700898 uint32_t switch_count = switch_insns[1];
899 int32_t keys_offset, targets_offset;
900 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700901 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
902 /* 0=sig, 1=count, 2/3=firstKey */
903 targets_offset = 4;
904 keys_offset = -1;
905 expected_signature = Instruction::kPackedSwitchSignature;
906 } else {
907 /* 0=sig, 1=count, 2..count*2 = keys */
908 keys_offset = 2;
909 targets_offset = 2 + 2 * switch_count;
910 expected_signature = Instruction::kSparseSwitchSignature;
911 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700912 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700913 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700914 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
915 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700916 return false;
917 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700918 /* make sure the end of the switch is in range */
919 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700920 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
921 << switch_offset << ", end "
922 << (cur_offset + switch_offset + table_size)
923 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700924 return false;
925 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700926 /* for a sparse switch, verify the keys are in ascending order */
927 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700928 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
929 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700930 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
931 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
932 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700933 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
934 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700935 return false;
936 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700937 last_key = key;
938 }
939 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700940 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700941 for (uint32_t targ = 0; targ < switch_count; targ++) {
942 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
943 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
944 int32_t abs_offset = cur_offset + offset;
945 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700946 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700947 << reinterpret_cast<void*>(abs_offset) << ") at "
948 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700949 return false;
950 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700951 insn_flags_[abs_offset].SetBranchTarget();
952 }
953 return true;
954}
955
Ian Rogers776ac1f2012-04-13 23:36:36 -0700956bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700957 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700958 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700959 return false;
960 }
961 uint16_t registers_size = code_item_->registers_size_;
962 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800963 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700964 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
965 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700966 return false;
967 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700968 }
969
970 return true;
971}
972
Ian Rogers776ac1f2012-04-13 23:36:36 -0700973bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 uint16_t registers_size = code_item_->registers_size_;
975 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
976 // integer overflow when adding them here.
977 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700978 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
979 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700980 return false;
981 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700982 return true;
983}
984
Ian Rogers0c7abda2012-09-19 13:33:42 -0700985static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -0800986 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
987 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
988 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
989 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
990 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
991 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
992 gc_map.begin(),
993 gc_map.end());
994 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
995 DCHECK_EQ(gc_map.size(),
996 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
997 (length_prefixed_gc_map->at(1) << 16) |
998 (length_prefixed_gc_map->at(2) << 8) |
999 (length_prefixed_gc_map->at(3) << 0)));
1000 return length_prefixed_gc_map;
1001}
1002
Ian Rogers776ac1f2012-04-13 23:36:36 -07001003bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001004 uint16_t registers_size = code_item_->registers_size_;
1005 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001006
Ian Rogersd81871c2011-10-03 13:57:23 -07001007 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001008 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1009 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001010 }
1011 /* Create and initialize table holding register status */
Elliott Hughes460384f2012-04-04 16:53:10 -07001012 reg_table_.Init(kTrackRegsGcPoints, insn_flags_.get(), insns_size, registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001013
Ian Rogersd81871c2011-10-03 13:57:23 -07001014 work_line_.reset(new RegisterLine(registers_size, this));
1015 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001016
Ian Rogersd81871c2011-10-03 13:57:23 -07001017 /* Initialize register types of method arguments. */
1018 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001019 DCHECK_NE(failures_.size(), 0U);
1020 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001021 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001022 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001023 return false;
1024 }
1025 /* Perform code flow verification. */
1026 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001027 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001028 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001029 }
1030
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001031 Compiler::MethodReference ref(dex_file_, dex_method_idx_);
TDYa127b2eb5c12012-05-24 15:52:10 -07001032
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
TDYa127ce4cc0d2012-11-18 16:59:53 -08001046#if 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);
Logan Chienfca7e872011-12-20 20:08:22 +08001050#endif
1051
jeffhaobdb76512011-09-07 11:43:16 -07001052 return true;
1053}
1054
Ian Rogersad0b3a32012-04-16 14:50:24 -07001055std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1056 DCHECK_EQ(failures_.size(), failure_messages_.size());
1057 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001058 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001059 }
1060 return os;
1061}
1062
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001063extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001064 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001065 v->Dump(std::cerr);
1066}
1067
Ian Rogers776ac1f2012-04-13 23:36:36 -07001068void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001069 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001070 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001071 return;
jeffhaobdb76512011-09-07 11:43:16 -07001072 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001073 {
1074 os << "Register Types:\n";
1075 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1076 std::ostream indent_os(&indent_filter);
1077 reg_types_.Dump(indent_os);
1078 }
Ian Rogersb4903572012-10-11 11:52:56 -07001079 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001080 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1081 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001082 const Instruction* inst = Instruction::At(code_item_->insns_);
1083 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1084 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001085 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1086 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001087 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001088 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001089 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump() << " ";
1090 const bool kDumpHexOfInstruction = false;
1091 if (kDumpHexOfInstruction) {
1092 indent_os << inst->DumpHex(5) << " ";
1093 }
1094 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001095 inst = inst->Next();
1096 }
jeffhaobdb76512011-09-07 11:43:16 -07001097}
1098
Ian Rogersd81871c2011-10-03 13:57:23 -07001099static bool IsPrimitiveDescriptor(char descriptor) {
1100 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001101 case 'I':
1102 case 'C':
1103 case 'S':
1104 case 'B':
1105 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001106 case 'F':
1107 case 'D':
1108 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001109 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001110 default:
1111 return false;
1112 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001113}
1114
Ian Rogers776ac1f2012-04-13 23:36:36 -07001115bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001116 RegisterLine* reg_line = reg_table_.GetLine(0);
1117 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1118 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001119
Ian Rogersd81871c2011-10-03 13:57:23 -07001120 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1121 //Include the "this" pointer.
1122 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001123 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001124 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1125 // argument as uninitialized. This restricts field access until the superclass constructor is
1126 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001127 const RegType& declaring_class = GetDeclaringClass();
1128 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001129 reg_line->SetRegisterType(arg_start + cur_arg,
1130 reg_types_.UninitializedThisArgument(declaring_class));
1131 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001132 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001133 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001134 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001135 }
1136
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001137 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001138 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001139 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001140
1141 for (; iterator.HasNext(); iterator.Next()) {
1142 const char* descriptor = iterator.GetDescriptor();
1143 if (descriptor == NULL) {
1144 LOG(FATAL) << "Null descriptor";
1145 }
1146 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001147 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1148 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001149 return false;
1150 }
1151 switch (descriptor[0]) {
1152 case 'L':
1153 case '[':
1154 // We assume that reference arguments are initialized. The only way it could be otherwise
1155 // (assuming the caller was verified) is if the current method is <init>, but in that case
1156 // it's effectively considered initialized the instant we reach here (in the sense that we
1157 // can return without doing anything or call virtual methods).
1158 {
Ian Rogersb4903572012-10-11 11:52:56 -07001159 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001160 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001161 }
1162 break;
1163 case 'Z':
1164 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1165 break;
1166 case 'C':
1167 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1168 break;
1169 case 'B':
1170 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1171 break;
1172 case 'I':
1173 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1174 break;
1175 case 'S':
1176 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1177 break;
1178 case 'F':
1179 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1180 break;
1181 case 'J':
1182 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001183 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1184 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1185 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001186 cur_arg++;
1187 break;
1188 }
1189 default:
jeffhaod5347e02012-03-22 17:25:05 -07001190 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001191 return false;
1192 }
1193 cur_arg++;
1194 }
1195 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001196 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001197 return false;
1198 }
1199 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1200 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1201 // format. Only major difference from the method argument format is that 'V' is supported.
1202 bool result;
1203 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1204 result = descriptor[1] == '\0';
1205 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1206 size_t i = 0;
1207 do {
1208 i++;
1209 } while (descriptor[i] == '['); // process leading [
1210 if (descriptor[i] == 'L') { // object array
1211 do {
1212 i++; // find closing ;
1213 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1214 result = descriptor[i] == ';';
1215 } else { // primitive array
1216 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1217 }
1218 } else if (descriptor[0] == 'L') {
1219 // could be more thorough here, but shouldn't be required
1220 size_t i = 0;
1221 do {
1222 i++;
1223 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1224 result = descriptor[i] == ';';
1225 } else {
1226 result = false;
1227 }
1228 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001229 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1230 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001231 }
1232 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001233}
1234
Ian Rogers776ac1f2012-04-13 23:36:36 -07001235bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001236 const uint16_t* insns = code_item_->insns_;
1237 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001238
jeffhaobdb76512011-09-07 11:43:16 -07001239 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001240 insn_flags_[0].SetChanged();
1241 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001242
jeffhaobdb76512011-09-07 11:43:16 -07001243 /* Continue until no instructions are marked "changed". */
1244 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001245 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1246 uint32_t insn_idx = start_guess;
1247 for (; insn_idx < insns_size; insn_idx++) {
1248 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001249 break;
1250 }
jeffhaobdb76512011-09-07 11:43:16 -07001251 if (insn_idx == insns_size) {
1252 if (start_guess != 0) {
1253 /* try again, starting from the top */
1254 start_guess = 0;
1255 continue;
1256 } else {
1257 /* all flags are clear */
1258 break;
1259 }
1260 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001261 // We carry the working set of registers from instruction to instruction. If this address can
1262 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1263 // "changed" flags, we need to load the set of registers from the table.
1264 // Because we always prefer to continue on to the next instruction, we should never have a
1265 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1266 // target.
1267 work_insn_idx_ = insn_idx;
1268 if (insn_flags_[insn_idx].IsBranchTarget()) {
1269 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001270 } else {
1271#ifndef NDEBUG
1272 /*
1273 * Sanity check: retrieve the stored register line (assuming
1274 * a full table) and make sure it actually matches.
1275 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001276 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1277 if (register_line != NULL) {
1278 if (work_line_->CompareLine(register_line) != 0) {
1279 Dump(std::cout);
1280 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001281 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001282 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1283 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001284 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001285 }
jeffhaobdb76512011-09-07 11:43:16 -07001286 }
1287#endif
1288 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001289 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001290 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001291 prepend += " failed to verify: ";
1292 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001293 return false;
1294 }
jeffhaobdb76512011-09-07 11:43:16 -07001295 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001296 insn_flags_[insn_idx].SetVisited();
1297 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001298 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001299
Ian Rogers1c849e52012-06-28 14:00:33 -07001300 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001301 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001302 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001303 * (besides the wasted space), but it indicates a flaw somewhere
1304 * down the line, possibly in the verifier.
1305 *
1306 * If we've substituted "always throw" instructions into the stream,
1307 * we are almost certainly going to have some dead code.
1308 */
1309 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001310 uint32_t insn_idx = 0;
1311 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001312 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001313 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001314 * may or may not be preceded by a padding NOP (for alignment).
1315 */
1316 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1317 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1318 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001319 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001320 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1321 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1322 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001323 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001324 }
1325
Ian Rogersd81871c2011-10-03 13:57:23 -07001326 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001327 if (dead_start < 0)
1328 dead_start = insn_idx;
1329 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001330 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001331 dead_start = -1;
1332 }
1333 }
1334 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001335 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001336 }
1337 }
jeffhaobdb76512011-09-07 11:43:16 -07001338 return true;
1339}
1340
Ian Rogers776ac1f2012-04-13 23:36:36 -07001341bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001342 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1343 // We want the state _before_ the instruction, for the case where the dex pc we're
1344 // interested in is itself a monitor-enter instruction (which is a likely place
1345 // for a thread to be suspended).
1346 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001347 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001348 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1349 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1350 }
1351 }
1352
jeffhaobdb76512011-09-07 11:43:16 -07001353 /*
1354 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001355 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001356 * control to another statement:
1357 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001358 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001359 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001360 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001361 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001362 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001363 * throw an exception that is handled by an encompassing "try"
1364 * block.
1365 *
1366 * We can also return, in which case there is no successor instruction
1367 * from this point.
1368 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001369 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001370 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001371 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1372 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001373 DecodedInstruction dec_insn(inst);
Ian Rogersa75a0132012-09-28 11:41:42 -07001374 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001375
jeffhaobdb76512011-09-07 11:43:16 -07001376 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001377 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001378 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001379 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001380 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1381 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001382 }
jeffhaobdb76512011-09-07 11:43:16 -07001383
1384 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001385 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001386 * can throw an exception, we will copy/merge this into the "catch"
1387 * address rather than work_line, because we don't want the result
1388 * from the "successful" code path (e.g. a check-cast that "improves"
1389 * a type) to be visible to the exception handler.
1390 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001391 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001392 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001393 } else {
1394#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001395 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001396#endif
1397 }
1398
Elliott Hughesadb8c672012-03-06 16:49:32 -08001399 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07001400 case Instruction::NOP:
1401 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001402 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001403 * a signature that looks like a NOP; if we see one of these in
1404 * the course of executing code then we have a problem.
1405 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001406 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001407 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001408 }
1409 break;
1410
1411 case Instruction::MOVE:
1412 case Instruction::MOVE_FROM16:
1413 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001414 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001415 break;
1416 case Instruction::MOVE_WIDE:
1417 case Instruction::MOVE_WIDE_FROM16:
1418 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001419 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07001420 break;
1421 case Instruction::MOVE_OBJECT:
1422 case Instruction::MOVE_OBJECT_FROM16:
1423 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001424 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001425 break;
1426
1427 /*
1428 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001429 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001430 * might want to hold the result in an actual CPU register, so the
1431 * Dalvik spec requires that these only appear immediately after an
1432 * invoke or filled-new-array.
1433 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001434 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001435 * redundant with the reset done below, but it can make the debug info
1436 * easier to read in some cases.)
1437 */
1438 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001439 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07001440 break;
1441 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001442 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001443 break;
1444 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001445 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07001446 break;
1447
Ian Rogersd81871c2011-10-03 13:57:23 -07001448 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001449 /*
jeffhao60f83e32012-02-13 17:16:30 -08001450 * This statement can only appear as the first instruction in an exception handler. We verify
1451 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001452 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001453 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08001454 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001455 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001456 }
jeffhaobdb76512011-09-07 11:43:16 -07001457 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001458 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1459 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001460 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001461 }
jeffhaobdb76512011-09-07 11:43:16 -07001462 }
1463 break;
1464 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001465 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001466 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001467 const RegType& return_type = GetMethodReturnType();
1468 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001469 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001470 } else {
1471 // Compilers may generate synthetic functions that write byte values into boolean fields.
1472 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001473 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001474 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1475 ((return_type.IsBoolean() || return_type.IsByte() ||
1476 return_type.IsShort() || return_type.IsChar()) &&
1477 src_type.IsInteger()));
1478 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001479 bool success =
1480 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
1481 if (!success) {
1482 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001483 }
jeffhaobdb76512011-09-07 11:43:16 -07001484 }
1485 }
1486 break;
1487 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001488 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001489 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001490 const RegType& return_type = GetMethodReturnType();
1491 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001492 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001493 } else {
1494 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001495 bool success = work_line_->VerifyRegisterType(dec_insn.vA, return_type);
1496 if (!success) {
1497 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001498 }
jeffhaobdb76512011-09-07 11:43:16 -07001499 }
1500 }
1501 break;
1502 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001503 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001504 const RegType& return_type = GetMethodReturnType();
1505 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001506 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001507 } else {
1508 /* return_type is the *expected* return type, not register value */
1509 DCHECK(!return_type.IsZero());
1510 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08001511 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07001512 // Disallow returning uninitialized values and verify that the reference in vAA is an
1513 // instance of the "return_type"
1514 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001515 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001516 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001517 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1518 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001519 }
1520 }
1521 }
1522 break;
1523
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001524 /* could be boolean, int, float, or a null reference */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001525 case Instruction::CONST_4:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001526 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001527 reg_types_.FromCat1Const(static_cast<int32_t>(dec_insn.vB << 28) >> 28, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001528 break;
jeffhaobdb76512011-09-07 11:43:16 -07001529 case Instruction::CONST_16:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001530 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001531 reg_types_.FromCat1Const(static_cast<int16_t>(dec_insn.vB), true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001532 break;
jeffhaobdb76512011-09-07 11:43:16 -07001533 case Instruction::CONST:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001534 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const(dec_insn.vB, true));
jeffhaobdb76512011-09-07 11:43:16 -07001535 break;
1536 case Instruction::CONST_HIGH16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001537 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001538 reg_types_.FromCat1Const(dec_insn.vB << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001539 break;
jeffhaobdb76512011-09-07 11:43:16 -07001540 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001541 case Instruction::CONST_WIDE_16: {
1542 int64_t val = static_cast<int16_t>(dec_insn.vB);
1543 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1544 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1545 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001546 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001547 }
1548 case Instruction::CONST_WIDE_32: {
1549 int64_t val = static_cast<int32_t>(dec_insn.vB);
1550 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1551 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1552 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1553 break;
1554 }
1555 case Instruction::CONST_WIDE: {
1556 int64_t val = dec_insn.vB_wide;
1557 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1558 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1559 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1560 break;
1561 }
1562 case Instruction::CONST_WIDE_HIGH16: {
1563 int64_t val = static_cast<uint64_t>(dec_insn.vB) << 48;
1564 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1565 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1566 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1567 break;
1568 }
jeffhaobdb76512011-09-07 11:43:16 -07001569 case Instruction::CONST_STRING:
1570 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001571 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001572 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001573 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001574 // Get type from instruction if unresolved then we need an access check
1575 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08001576 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001577 // Register holds class, ie its type is class, on error it will hold Conflict.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001578 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogersb4903572012-10-11 11:52:56 -07001579 res_type.IsConflict() ? res_type
1580 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001581 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001582 }
jeffhaobdb76512011-09-07 11:43:16 -07001583 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001584 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001585 break;
1586 case Instruction::MONITOR_EXIT:
1587 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001588 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001589 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001590 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001591 * to the need to handle asynchronous exceptions, a now-deprecated
1592 * feature that Dalvik doesn't support.)
1593 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001594 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001595 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001596 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001597 * structured locking checks are working, the former would have
1598 * failed on the -enter instruction, and the latter is impossible.
1599 *
1600 * This is fortunate, because issue 3221411 prevents us from
1601 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001602 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001603 * some catch blocks (which will show up as "dead" code when
1604 * we skip them here); if we can't, then the code path could be
1605 * "live" so we still need to check it.
1606 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001607 opcode_flags &= ~Instruction::kThrow;
1608 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001609 break;
1610
Ian Rogers28ad40d2011-10-27 15:19:26 -07001611 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001612 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001613 /*
1614 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1615 * could be a "upcast" -- not expected, so we don't try to address it.)
1616 *
1617 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001618 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001619 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001620 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001621 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001622 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001623 if (res_type.IsConflict()) {
1624 DCHECK_NE(failures_.size(), 0U);
1625 if (!is_checkcast) {
1626 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
1627 }
1628 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001629 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001630 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1631 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001632 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001633 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001634 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001635 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001636 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07001637 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001638 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001639 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001640 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001641 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001642 }
jeffhaobdb76512011-09-07 11:43:16 -07001643 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001644 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001645 }
1646 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001647 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001648 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001649 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001650 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001651 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001652 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001653 }
1654 }
1655 break;
1656 }
1657 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001658 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001659 if (res_type.IsConflict()) {
1660 DCHECK_NE(failures_.size(), 0U);
1661 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001662 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001663 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1664 // can't create an instance of an interface or abstract class */
1665 if (!res_type.IsInstantiableTypes()) {
1666 Fail(VERIFY_ERROR_INSTANTIATION)
1667 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001668 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001669 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001670 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1671 // Any registers holding previous allocations from this address that have not yet been
1672 // initialized must be marked invalid.
1673 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1674 // add the new uninitialized reference to the register state
1675 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001676 break;
1677 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001678 case Instruction::NEW_ARRAY:
1679 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001680 break;
1681 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08001682 VerifyNewArray(dec_insn, true, false);
1683 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001684 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001685 case Instruction::FILLED_NEW_ARRAY_RANGE:
1686 VerifyNewArray(dec_insn, true, true);
1687 just_set_result = true; // Filled new array range sets result register
1688 break;
jeffhaobdb76512011-09-07 11:43:16 -07001689 case Instruction::CMPL_FLOAT:
1690 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001691 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001692 break;
1693 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001694 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001695 break;
1696 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001697 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001698 break;
1699 case Instruction::CMPL_DOUBLE:
1700 case Instruction::CMPG_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001701 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vB, reg_types_.DoubleLo(),
1702 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001703 break;
1704 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001705 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vC, reg_types_.DoubleLo(),
1706 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001707 break;
1708 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001709 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001710 break;
1711 case Instruction::CMP_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001712 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vB, reg_types_.LongLo(),
1713 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001714 break;
1715 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001716 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vC, reg_types_.LongLo(),
1717 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001718 break;
1719 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001720 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001721 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001722 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001723 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersb4903572012-10-11 11:52:56 -07001724 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001725 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001726 }
1727 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001728 }
jeffhaobdb76512011-09-07 11:43:16 -07001729 case Instruction::GOTO:
1730 case Instruction::GOTO_16:
1731 case Instruction::GOTO_32:
1732 /* no effect on or use of registers */
1733 break;
1734
1735 case Instruction::PACKED_SWITCH:
1736 case Instruction::SPARSE_SWITCH:
1737 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001738 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001739 break;
1740
Ian Rogersd81871c2011-10-03 13:57:23 -07001741 case Instruction::FILL_ARRAY_DATA: {
1742 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001743 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08001744 /* array_type can be null if the reg type is Zero */
1745 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001746 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001748 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001749 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1750 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001751 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001752 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1753 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001754 } else {
jeffhao457cc512012-02-02 16:55:13 -08001755 // Now verify if the element width in the table matches the element width declared in
1756 // the array
1757 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1758 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001759 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001760 } else {
1761 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1762 // Since we don't compress the data in Dex, expect to see equal width of data stored
1763 // in the table and expected from the array class.
1764 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001765 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1766 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001767 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001768 }
1769 }
jeffhaobdb76512011-09-07 11:43:16 -07001770 }
1771 }
1772 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001773 }
jeffhaobdb76512011-09-07 11:43:16 -07001774 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001775 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001776 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1777 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001778 bool mismatch = false;
1779 if (reg_type1.IsZero()) { // zero then integral or reference expected
1780 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1781 } else if (reg_type1.IsReferenceTypes()) { // both references?
1782 mismatch = !reg_type2.IsReferenceTypes();
1783 } else { // both integral?
1784 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1785 }
1786 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001787 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1788 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001789 }
1790 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001791 }
jeffhaobdb76512011-09-07 11:43:16 -07001792 case Instruction::IF_LT:
1793 case Instruction::IF_GE:
1794 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001795 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001796 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1797 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001798 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1800 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001801 }
1802 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001803 }
jeffhaobdb76512011-09-07 11:43:16 -07001804 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001805 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001806 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001807 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001808 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001809 }
jeffhaobdb76512011-09-07 11:43:16 -07001810 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001811 }
jeffhaobdb76512011-09-07 11:43:16 -07001812 case Instruction::IF_LTZ:
1813 case Instruction::IF_GEZ:
1814 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001815 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001816 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001817 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001818 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1819 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001820 }
jeffhaobdb76512011-09-07 11:43:16 -07001821 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001822 }
jeffhaobdb76512011-09-07 11:43:16 -07001823 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001824 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
1825 break;
jeffhaobdb76512011-09-07 11:43:16 -07001826 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001827 VerifyAGet(dec_insn, reg_types_.Byte(), true);
1828 break;
jeffhaobdb76512011-09-07 11:43:16 -07001829 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07001830 VerifyAGet(dec_insn, reg_types_.Char(), true);
1831 break;
jeffhaobdb76512011-09-07 11:43:16 -07001832 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001833 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001834 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001835 case Instruction::AGET:
1836 VerifyAGet(dec_insn, reg_types_.Integer(), true);
1837 break;
jeffhaobdb76512011-09-07 11:43:16 -07001838 case Instruction::AGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001839 VerifyAGet(dec_insn, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001840 break;
1841 case Instruction::AGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001842 VerifyAGet(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001843 break;
1844
Ian Rogersd81871c2011-10-03 13:57:23 -07001845 case Instruction::APUT_BOOLEAN:
1846 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
1847 break;
1848 case Instruction::APUT_BYTE:
1849 VerifyAPut(dec_insn, reg_types_.Byte(), true);
1850 break;
1851 case Instruction::APUT_CHAR:
1852 VerifyAPut(dec_insn, reg_types_.Char(), true);
1853 break;
1854 case Instruction::APUT_SHORT:
1855 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001856 break;
1857 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001858 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001859 break;
1860 case Instruction::APUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001861 VerifyAPut(dec_insn, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001862 break;
1863 case Instruction::APUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001864 VerifyAPut(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001865 break;
1866
jeffhaobdb76512011-09-07 11:43:16 -07001867 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001868 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001869 break;
jeffhaobdb76512011-09-07 11:43:16 -07001870 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001871 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001872 break;
jeffhaobdb76512011-09-07 11:43:16 -07001873 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001874 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001875 break;
jeffhaobdb76512011-09-07 11:43:16 -07001876 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001877 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001878 break;
1879 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001880 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001881 break;
1882 case Instruction::IGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001883 VerifyISGet(dec_insn, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001884 break;
1885 case Instruction::IGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001886 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001887 break;
jeffhaobdb76512011-09-07 11:43:16 -07001888
Ian Rogersd81871c2011-10-03 13:57:23 -07001889 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001890 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001891 break;
1892 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001893 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001894 break;
1895 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001896 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001897 break;
1898 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001899 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001900 break;
1901 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001902 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001903 break;
1904 case Instruction::IPUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001905 VerifyISPut(dec_insn, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001906 break;
jeffhaobdb76512011-09-07 11:43:16 -07001907 case Instruction::IPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001908 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001909 break;
1910
jeffhaobdb76512011-09-07 11:43:16 -07001911 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001912 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001913 break;
jeffhaobdb76512011-09-07 11:43:16 -07001914 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001915 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001916 break;
jeffhaobdb76512011-09-07 11:43:16 -07001917 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001918 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001919 break;
jeffhaobdb76512011-09-07 11:43:16 -07001920 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001921 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001922 break;
1923 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001924 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001925 break;
1926 case Instruction::SGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001927 VerifyISGet(dec_insn, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001928 break;
1929 case Instruction::SGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001930 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001931 break;
1932
1933 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001934 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001935 break;
1936 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001937 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001938 break;
1939 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001940 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001941 break;
1942 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001943 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001944 break;
1945 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001946 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001947 break;
1948 case Instruction::SPUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001949 VerifyISPut(dec_insn, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001950 break;
1951 case Instruction::SPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001952 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001953 break;
1954
1955 case Instruction::INVOKE_VIRTUAL:
1956 case Instruction::INVOKE_VIRTUAL_RANGE:
1957 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001958 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001959 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
1960 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
1961 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
1962 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartier66f19252012-09-18 08:57:04 -07001963 AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001964 const char* descriptor;
1965 if (called_method == NULL) {
1966 uint32_t method_idx = dec_insn.vB;
1967 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1968 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1969 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1970 } else {
1971 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07001972 }
Ian Rogersb4903572012-10-11 11:52:56 -07001973 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001974 if (!return_type.IsLowHalf()) {
1975 work_line_->SetResultRegisterType(return_type);
1976 } else {
1977 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
1978 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001979 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001980 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001981 }
jeffhaobdb76512011-09-07 11:43:16 -07001982 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001983 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001984 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartier66f19252012-09-18 08:57:04 -07001985 AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07001986 const char* return_type_descriptor;
1987 bool is_constructor;
1988 if (called_method == NULL) {
1989 uint32_t method_idx = dec_insn.vB;
1990 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1991 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
1992 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1993 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1994 } else {
1995 is_constructor = called_method->IsConstructor();
1996 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
1997 }
1998 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07001999 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002000 * Some additional checks when calling a constructor. We know from the invocation arg check
2001 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2002 * that to require that called_method->klass is the same as this->klass or this->super,
2003 * allowing the latter only if the "this" argument is the same as the "this" argument to
2004 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002005 */
jeffhaob57e9522012-04-26 18:08:21 -07002006 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2007 if (this_type.IsConflict()) // failure.
2008 break;
jeffhaobdb76512011-09-07 11:43:16 -07002009
jeffhaob57e9522012-04-26 18:08:21 -07002010 /* no null refs allowed (?) */
2011 if (this_type.IsZero()) {
2012 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2013 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002014 }
jeffhaob57e9522012-04-26 18:08:21 -07002015
2016 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002017 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2018 // TODO: re-enable constructor type verification
2019 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002020 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002021 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2022 // break;
2023 // }
jeffhaob57e9522012-04-26 18:08:21 -07002024
2025 /* arg must be an uninitialized reference */
2026 if (!this_type.IsUninitializedTypes()) {
2027 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2028 << this_type;
2029 break;
2030 }
2031
2032 /*
2033 * Replace the uninitialized reference with an initialized one. We need to do this for all
2034 * registers that have the same object instance in them, not just the "this" register.
2035 */
2036 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002037 }
Ian Rogersb4903572012-10-11 11:52:56 -07002038 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2039 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002040 if (!return_type.IsLowHalf()) {
2041 work_line_->SetResultRegisterType(return_type);
2042 } else {
2043 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2044 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002045 just_set_result = true;
2046 break;
2047 }
2048 case Instruction::INVOKE_STATIC:
2049 case Instruction::INVOKE_STATIC_RANGE: {
2050 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartier66f19252012-09-18 08:57:04 -07002051 AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002052 const char* descriptor;
2053 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002054 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002055 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2056 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002057 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002058 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002059 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002060 }
Ian Rogersb4903572012-10-11 11:52:56 -07002061 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002062 if (!return_type.IsLowHalf()) {
2063 work_line_->SetResultRegisterType(return_type);
2064 } else {
2065 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2066 }
jeffhaobdb76512011-09-07 11:43:16 -07002067 just_set_result = true;
2068 }
2069 break;
jeffhaobdb76512011-09-07 11:43:16 -07002070 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002071 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002072 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartier66f19252012-09-18 08:57:04 -07002073 AbstractMethod* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002074 if (abs_method != NULL) {
2075 Class* called_interface = abs_method->GetDeclaringClass();
2076 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2077 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2078 << PrettyMethod(abs_method) << "'";
2079 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002080 }
Ian Rogers0d604842012-04-16 14:50:24 -07002081 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002082 /* Get the type of the "this" arg, which should either be a sub-interface of called
2083 * interface or Object (see comments in RegType::JoinClass).
2084 */
2085 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2086 if (this_type.IsZero()) {
2087 /* null pointer always passes (and always fails at runtime) */
2088 } else {
2089 if (this_type.IsUninitializedTypes()) {
2090 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2091 << this_type;
2092 break;
2093 }
2094 // In the past we have tried to assert that "called_interface" is assignable
2095 // from "this_type.GetClass()", however, as we do an imprecise Join
2096 // (RegType::JoinClass) we don't have full information on what interfaces are
2097 // implemented by "this_type". For example, two classes may implement the same
2098 // interfaces and have a common parent that doesn't implement the interface. The
2099 // join will set "this_type" to the parent class and a test that this implements
2100 // the interface will incorrectly fail.
2101 }
2102 /*
2103 * We don't have an object instance, so we can't find the concrete method. However, all of
2104 * the type information is in the abstract method, so we're good.
2105 */
2106 const char* descriptor;
2107 if (abs_method == NULL) {
2108 uint32_t method_idx = dec_insn.vB;
2109 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2110 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2111 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2112 } else {
2113 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2114 }
Ian Rogersb4903572012-10-11 11:52:56 -07002115 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002116 if (!return_type.IsLowHalf()) {
2117 work_line_->SetResultRegisterType(return_type);
2118 } else {
2119 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2120 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002121 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002122 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002123 }
jeffhaobdb76512011-09-07 11:43:16 -07002124 case Instruction::NEG_INT:
2125 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002126 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002127 break;
2128 case Instruction::NEG_LONG:
2129 case Instruction::NOT_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002130 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2131 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002132 break;
2133 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002134 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002135 break;
2136 case Instruction::NEG_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002137 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2138 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002139 break;
2140 case Instruction::INT_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002141 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2142 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002143 break;
2144 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002145 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002146 break;
2147 case Instruction::INT_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002148 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2149 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002150 break;
2151 case Instruction::LONG_TO_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002152 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Integer(),
2153 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002154 break;
2155 case Instruction::LONG_TO_FLOAT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002156 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Float(),
2157 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002158 break;
2159 case Instruction::LONG_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002160 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2161 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002162 break;
2163 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002164 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002165 break;
2166 case Instruction::FLOAT_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002167 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2168 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002169 break;
2170 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002171 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2172 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002173 break;
2174 case Instruction::DOUBLE_TO_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002175 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Integer(),
2176 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002177 break;
2178 case Instruction::DOUBLE_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002179 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2180 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002181 break;
2182 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002183 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Float(),
2184 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002185 break;
2186 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002187 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002188 break;
2189 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002190 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002191 break;
2192 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002193 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002194 break;
2195
2196 case Instruction::ADD_INT:
2197 case Instruction::SUB_INT:
2198 case Instruction::MUL_INT:
2199 case Instruction::REM_INT:
2200 case Instruction::DIV_INT:
2201 case Instruction::SHL_INT:
2202 case Instruction::SHR_INT:
2203 case Instruction::USHR_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002204 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(),
2205 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002206 break;
2207 case Instruction::AND_INT:
2208 case Instruction::OR_INT:
2209 case Instruction::XOR_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002210 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(),
2211 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002212 break;
2213 case Instruction::ADD_LONG:
2214 case Instruction::SUB_LONG:
2215 case Instruction::MUL_LONG:
2216 case Instruction::DIV_LONG:
2217 case Instruction::REM_LONG:
2218 case Instruction::AND_LONG:
2219 case Instruction::OR_LONG:
2220 case Instruction::XOR_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002221 work_line_->CheckBinaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2222 reg_types_.LongLo(), reg_types_.LongHi(),
2223 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002224 break;
2225 case Instruction::SHL_LONG:
2226 case Instruction::SHR_LONG:
2227 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002228 /* shift distance is Int, making these different from other binary operations */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002229 work_line_->CheckBinaryOpWideShift(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2230 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002231 break;
2232 case Instruction::ADD_FLOAT:
2233 case Instruction::SUB_FLOAT:
2234 case Instruction::MUL_FLOAT:
2235 case Instruction::DIV_FLOAT:
2236 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002237 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002238 break;
2239 case Instruction::ADD_DOUBLE:
2240 case Instruction::SUB_DOUBLE:
2241 case Instruction::MUL_DOUBLE:
2242 case Instruction::DIV_DOUBLE:
2243 case Instruction::REM_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002244 work_line_->CheckBinaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2245 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2246 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002247 break;
2248 case Instruction::ADD_INT_2ADDR:
2249 case Instruction::SUB_INT_2ADDR:
2250 case Instruction::MUL_INT_2ADDR:
2251 case Instruction::REM_INT_2ADDR:
2252 case Instruction::SHL_INT_2ADDR:
2253 case Instruction::SHR_INT_2ADDR:
2254 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002255 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002256 break;
2257 case Instruction::AND_INT_2ADDR:
2258 case Instruction::OR_INT_2ADDR:
2259 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002260 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002261 break;
2262 case Instruction::DIV_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::ADD_LONG_2ADDR:
2266 case Instruction::SUB_LONG_2ADDR:
2267 case Instruction::MUL_LONG_2ADDR:
2268 case Instruction::DIV_LONG_2ADDR:
2269 case Instruction::REM_LONG_2ADDR:
2270 case Instruction::AND_LONG_2ADDR:
2271 case Instruction::OR_LONG_2ADDR:
2272 case Instruction::XOR_LONG_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002273 work_line_->CheckBinaryOp2addrWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2274 reg_types_.LongLo(), reg_types_.LongHi(),
2275 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002276 break;
2277 case Instruction::SHL_LONG_2ADDR:
2278 case Instruction::SHR_LONG_2ADDR:
2279 case Instruction::USHR_LONG_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002280 work_line_->CheckBinaryOp2addrWideShift(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2281 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002282 break;
2283 case Instruction::ADD_FLOAT_2ADDR:
2284 case Instruction::SUB_FLOAT_2ADDR:
2285 case Instruction::MUL_FLOAT_2ADDR:
2286 case Instruction::DIV_FLOAT_2ADDR:
2287 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002288 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002289 break;
2290 case Instruction::ADD_DOUBLE_2ADDR:
2291 case Instruction::SUB_DOUBLE_2ADDR:
2292 case Instruction::MUL_DOUBLE_2ADDR:
2293 case Instruction::DIV_DOUBLE_2ADDR:
2294 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002295 work_line_->CheckBinaryOp2addrWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2296 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2297 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002298 break;
2299 case Instruction::ADD_INT_LIT16:
2300 case Instruction::RSUB_INT:
2301 case Instruction::MUL_INT_LIT16:
2302 case Instruction::DIV_INT_LIT16:
2303 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002304 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002305 break;
2306 case Instruction::AND_INT_LIT16:
2307 case Instruction::OR_INT_LIT16:
2308 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002309 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002310 break;
2311 case Instruction::ADD_INT_LIT8:
2312 case Instruction::RSUB_INT_LIT8:
2313 case Instruction::MUL_INT_LIT8:
2314 case Instruction::DIV_INT_LIT8:
2315 case Instruction::REM_INT_LIT8:
2316 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002317 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002318 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002319 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002320 break;
2321 case Instruction::AND_INT_LIT8:
2322 case Instruction::OR_INT_LIT8:
2323 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002324 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002325 break;
2326
Ian Rogersd81871c2011-10-03 13:57:23 -07002327 /* These should never appear during verification. */
jeffhao9a4f0032012-08-30 16:17:40 -07002328 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002329 case Instruction::UNUSED_EE:
2330 case Instruction::UNUSED_EF:
2331 case Instruction::UNUSED_F2:
2332 case Instruction::UNUSED_F3:
2333 case Instruction::UNUSED_F4:
2334 case Instruction::UNUSED_F5:
2335 case Instruction::UNUSED_F6:
2336 case Instruction::UNUSED_F7:
2337 case Instruction::UNUSED_F8:
2338 case Instruction::UNUSED_F9:
2339 case Instruction::UNUSED_FA:
2340 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002341 case Instruction::UNUSED_F0:
2342 case Instruction::UNUSED_F1:
2343 case Instruction::UNUSED_E3:
2344 case Instruction::UNUSED_E8:
2345 case Instruction::UNUSED_E7:
2346 case Instruction::UNUSED_E4:
2347 case Instruction::UNUSED_E9:
2348 case Instruction::UNUSED_FC:
2349 case Instruction::UNUSED_E5:
2350 case Instruction::UNUSED_EA:
2351 case Instruction::UNUSED_FD:
2352 case Instruction::UNUSED_E6:
2353 case Instruction::UNUSED_EB:
2354 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002355 case Instruction::UNUSED_3E:
2356 case Instruction::UNUSED_3F:
2357 case Instruction::UNUSED_40:
2358 case Instruction::UNUSED_41:
2359 case Instruction::UNUSED_42:
2360 case Instruction::UNUSED_43:
2361 case Instruction::UNUSED_73:
2362 case Instruction::UNUSED_79:
2363 case Instruction::UNUSED_7A:
2364 case Instruction::UNUSED_EC:
2365 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002366 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002367 break;
2368
2369 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002370 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002371 * complain if an instruction is missing (which is desirable).
2372 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002373 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002374
Ian Rogersad0b3a32012-04-16 14:50:24 -07002375 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002376 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002377 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002378 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002379 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002380 /* immediate failure, reject class */
2381 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2382 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002383 } else if (have_pending_runtime_throw_failure_) {
2384 /* slow path will throw, mark following code as unreachable */
2385 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002386 }
jeffhaobdb76512011-09-07 11:43:16 -07002387 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002388 * If we didn't just set the result register, clear it out. This ensures that you can only use
2389 * "move-result" immediately after the result is set. (We could check this statically, but it's
2390 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002391 */
2392 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002393 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002394 }
2395
jeffhaoa0a764a2011-09-16 10:43:38 -07002396 /* Handle "continue". Tag the next consecutive instruction. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002397 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers776ac1f2012-04-13 23:36:36 -07002398 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
Ian Rogersd81871c2011-10-03 13:57:23 -07002399 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
jeffhaod5347e02012-03-22 17:25:05 -07002400 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002401 return false;
2402 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002403 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2404 // next instruction isn't one.
jeffhaod5347e02012-03-22 17:25:05 -07002405 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002406 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002407 }
2408 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2409 if (next_line != NULL) {
2410 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2411 // needed.
2412 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002413 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002414 }
jeffhaobdb76512011-09-07 11:43:16 -07002415 } else {
2416 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002417 * We're not recording register data for the next instruction, so we don't know what the prior
2418 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002419 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002420 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002421 }
2422 }
2423
2424 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002425 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002426 *
2427 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002428 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002429 * somebody could get a reference field, check it for zero, and if the
2430 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002431 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002432 * that, and will reject the code.
2433 *
2434 * TODO: avoid re-fetching the branch target
2435 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002436 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002437 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002438 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002439 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002440 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002441 return false;
2442 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002443 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002444 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002445 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002446 }
jeffhaobdb76512011-09-07 11:43:16 -07002447 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002448 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002449 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002450 }
jeffhaobdb76512011-09-07 11:43:16 -07002451 }
2452
2453 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002454 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002455 *
2456 * We've already verified that the table is structurally sound, so we
2457 * just need to walk through and tag the targets.
2458 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002459 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002460 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2461 const uint16_t* switch_insns = insns + offset_to_switch;
2462 int switch_count = switch_insns[1];
2463 int offset_to_targets, targ;
2464
2465 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2466 /* 0 = sig, 1 = count, 2/3 = first key */
2467 offset_to_targets = 4;
2468 } else {
2469 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002470 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002471 offset_to_targets = 2 + 2 * switch_count;
2472 }
2473
2474 /* verify each switch target */
2475 for (targ = 0; targ < switch_count; targ++) {
2476 int offset;
2477 uint32_t abs_offset;
2478
2479 /* offsets are 32-bit, and only partly endian-swapped */
2480 offset = switch_insns[offset_to_targets + targ * 2] |
2481 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002482 abs_offset = work_insn_idx_ + offset;
2483 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002484 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002485 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002486 }
2487 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002488 return false;
2489 }
2490 }
2491
2492 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002493 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2494 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002495 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002496 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002497 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002498 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002499
Ian Rogers0571d352011-11-03 19:51:38 -07002500 for (; iterator.HasNext(); iterator.Next()) {
2501 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002502 within_catch_all = true;
2503 }
jeffhaobdb76512011-09-07 11:43:16 -07002504 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002505 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2506 * "work_regs", because at runtime the exception will be thrown before the instruction
2507 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002508 */
Ian Rogers0571d352011-11-03 19:51:38 -07002509 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002510 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002511 }
jeffhaobdb76512011-09-07 11:43:16 -07002512 }
2513
2514 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002515 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2516 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002517 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002518 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002519 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002520 * The state in work_line reflects the post-execution state. If the current instruction is a
2521 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002522 * it will do so before grabbing the lock).
2523 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002524 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002525 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002526 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002527 return false;
2528 }
2529 }
2530 }
2531
jeffhaod1f0fde2011-09-08 17:25:33 -07002532 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002533 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002534 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002535 return false;
2536 }
jeffhaobdb76512011-09-07 11:43:16 -07002537 }
2538
2539 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002540 * Update start_guess. Advance to the next instruction of that's
2541 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002542 * neither of those exists we're in a return or throw; leave start_guess
2543 * alone and let the caller sort it out.
2544 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002545 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002546 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002547 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002548 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002549 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002550 }
2551
Ian Rogersd81871c2011-10-03 13:57:23 -07002552 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2553 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002554
2555 return true;
2556}
2557
Ian Rogers776ac1f2012-04-13 23:36:36 -07002558const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002559 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002560 const RegType& referrer = GetDeclaringClass();
2561 Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002562 const RegType& result =
Ian Rogersb4903572012-10-11 11:52:56 -07002563 klass != NULL ? reg_types_.FromClass(klass, klass->IsFinal())
2564 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002565 if (result.IsConflict()) {
2566 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2567 << "' in " << referrer;
2568 return result;
2569 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002570 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002571 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002572 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002573 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002574 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002575 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002576 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002577 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002578 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002579 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002580}
2581
Ian Rogers776ac1f2012-04-13 23:36:36 -07002582const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002583 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002584 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002585 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002586 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2587 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002588 CatchHandlerIterator iterator(handlers_ptr);
2589 for (; iterator.HasNext(); iterator.Next()) {
2590 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2591 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002592 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002593 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002594 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002595 if (common_super == NULL) {
2596 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2597 // as that is caught at runtime
2598 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002599 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002600 // We don't know enough about the type and the common path merge will result in
2601 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002602 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002603 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002604 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002605 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002606 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002607 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002608 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002609 }
2610 }
2611 }
2612 }
Ian Rogers0571d352011-11-03 19:51:38 -07002613 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002614 }
2615 }
2616 if (common_super == NULL) {
2617 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002618 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002619 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002620 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002621 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002622}
2623
Mathieu Chartier66f19252012-09-18 08:57:04 -07002624AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002625 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002626 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002627 if (klass_type.IsConflict()) {
2628 std::string append(" in attempt to access method ");
2629 append += dex_file_->GetMethodName(method_id);
2630 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002631 return NULL;
2632 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002633 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002634 return NULL; // Can't resolve Class so no more to do here
2635 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002636 Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002637 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier66f19252012-09-18 08:57:04 -07002638 AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002639 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002640 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002641 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002642
2643 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002644 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002645 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002646 res_method = klass->FindInterfaceMethod(name, signature);
2647 } else {
2648 res_method = klass->FindVirtualMethod(name, signature);
2649 }
2650 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002651 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002652 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002653 // If a virtual or interface method wasn't found with the expected type, look in
2654 // the direct methods. This can happen when the wrong invoke type is used or when
2655 // a class has changed, and will be flagged as an error in later checks.
2656 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2657 res_method = klass->FindDirectMethod(name, signature);
2658 }
2659 if (res_method == NULL) {
2660 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2661 << PrettyDescriptor(klass) << "." << name
2662 << " " << signature;
2663 return NULL;
2664 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002665 }
2666 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002667 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2668 // enforce them here.
2669 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002670 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2671 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002672 return NULL;
2673 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002674 // Disallow any calls to class initializers.
2675 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002676 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2677 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002678 return NULL;
2679 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002680 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002681 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002682 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002683 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002684 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002685 }
jeffhaode0d9c92012-02-27 13:58:13 -08002686 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2687 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002688 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2689 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002690 return NULL;
2691 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002692 // Check that interface methods match interface classes.
2693 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2694 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2695 << " is in an interface class " << PrettyClass(klass);
2696 return NULL;
2697 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2698 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2699 << " is in a non-interface class " << PrettyClass(klass);
2700 return NULL;
2701 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002702 // See if the method type implied by the invoke instruction matches the access flags for the
2703 // target method.
2704 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2705 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2706 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2707 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002708 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2709 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002710 return NULL;
2711 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002712 return res_method;
2713}
2714
Mathieu Chartier66f19252012-09-18 08:57:04 -07002715AbstractMethod* MethodVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
Ian Rogers46685432012-06-03 22:26:43 -07002716 MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002717 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2718 // we're making.
Mathieu Chartier66f19252012-09-18 08:57:04 -07002719 AbstractMethod* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002720 if (res_method == NULL) { // error or class is unresolved
2721 return NULL;
2722 }
2723
Ian Rogersd81871c2011-10-03 13:57:23 -07002724 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2725 // has a vtable entry for the target method.
2726 if (is_super) {
2727 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002728 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002729 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002730 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002731 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002732 << " to super " << PrettyMethod(res_method);
2733 return NULL;
2734 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002735 Class* super_klass = super.GetClass();
2736 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002737 MethodHelper mh(res_method);
2738 Fail(VERIFY_ERROR_NO_METHOD) << "invalid 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 " << super
2741 << "." << mh.GetName()
2742 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002743 return NULL;
2744 }
2745 }
2746 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2747 // match the call to the signature. Also, we might might be calling through an abstract method
2748 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08002749 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002750 /* caught by static verifier */
2751 DCHECK(is_range || expected_args <= 5);
2752 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002753 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002754 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2755 return NULL;
2756 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002757
jeffhaobdb76512011-09-07 11:43:16 -07002758 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002759 * Check the "this" argument, which must be an instance of the class that declared the method.
2760 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2761 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002762 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002763 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002764 if (!res_method->IsStatic()) {
2765 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002766 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002767 return NULL;
2768 }
2769 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002770 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002771 return NULL;
2772 }
2773 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersb4903572012-10-11 11:52:56 -07002774 Class* klass = res_method->GetDeclaringClass();
2775 const RegType& res_method_class = reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogers9074b992011-10-26 17:41:55 -07002776 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002777 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002778 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002779 return NULL;
2780 }
2781 }
2782 actual_args++;
2783 }
2784 /*
2785 * Process the target method's signature. This signature may or may not
2786 * have been verified, so we can't assume it's properly formed.
2787 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002788 MethodHelper mh(res_method);
2789 const DexFile::TypeList* params = mh.GetParameterTypeList();
2790 size_t params_size = params == NULL ? 0 : params->Size();
2791 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002792 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002793 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002794 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2795 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002796 return NULL;
2797 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002798 const char* descriptor =
2799 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2800 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002801 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002802 << " missing signature component";
2803 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002804 }
Ian Rogersb4903572012-10-11 11:52:56 -07002805 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002806 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002807 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002808 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002809 }
2810 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2811 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002812 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002813 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002814 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002815 return NULL;
2816 } else {
2817 return res_method;
2818 }
2819}
2820
Ian Rogers776ac1f2012-04-13 23:36:36 -07002821void MethodVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08002822 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002823 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002824 if (res_type.IsConflict()) { // bad class
2825 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002826 } else {
2827 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2828 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002829 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002830 } else if (!is_filled) {
2831 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002832 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002833 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002834 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002835 } else {
2836 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2837 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002838 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002839 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002840 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002841 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002842 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002843 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002844 return;
2845 }
2846 }
2847 // filled-array result goes into "result" register
2848 work_line_->SetResultRegisterType(res_type);
2849 }
2850 }
2851}
2852
Ian Rogers776ac1f2012-04-13 23:36:36 -07002853void MethodVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002854 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002855 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002856 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002857 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002858 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002859 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002860 if (array_type.IsZero()) {
2861 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2862 // instruction type. TODO: have a proper notion of bottom here.
2863 if (!is_primitive || insn_type.IsCategory1Types()) {
2864 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08002865 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002866 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002867 // Category 2
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002868 work_line_->SetRegisterTypeWide(dec_insn.vA, reg_types_.FromCat2ConstLo(0, false),
2869 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08002870 }
jeffhaofc3144e2012-02-01 17:21:15 -08002871 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002872 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002873 } else {
2874 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002875 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002876 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002877 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002878 << " source for aget-object";
2879 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002880 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002881 << " source for category 1 aget";
2882 } else if (is_primitive && !insn_type.Equals(component_type) &&
2883 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002884 (insn_type.IsLong() && component_type.IsDouble()))) {
2885 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
2886 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002887 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002888 // Use knowledge of the field type which is stronger than the type inferred from the
2889 // instruction, which can't differentiate object types and ints from floats, longs from
2890 // doubles.
2891 if (!component_type.IsLowHalf()) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002892 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002893 } else {
2894 work_line_->SetRegisterTypeWide(dec_insn.vA, component_type,
2895 component_type.HighHalf(&reg_types_));
2896 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002897 }
2898 }
2899 }
2900}
2901
Ian Rogers776ac1f2012-04-13 23:36:36 -07002902void MethodVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002903 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002904 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002905 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002906 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002907 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002908 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002909 if (array_type.IsZero()) {
2910 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2911 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002912 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002913 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002914 } else {
2915 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002916 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002917 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002918 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002919 << " source for aput-object";
2920 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002921 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002922 << " source for category 1 aput";
2923 } else if (is_primitive && !insn_type.Equals(component_type) &&
2924 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2925 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002926 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002927 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002928 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002929 // The instruction agrees with the type of array, confirm the value to be stored does too
2930 // Note: we use the instruction type (rather than the component type) for aput-object as
2931 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08002932 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002933 }
2934 }
2935 }
2936}
2937
Ian Rogers776ac1f2012-04-13 23:36:36 -07002938Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002939 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2940 // Check access to class
2941 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002942 if (klass_type.IsConflict()) { // bad class
2943 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
2944 field_idx, dex_file_->GetFieldName(field_id),
2945 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002946 return NULL;
2947 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002948 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002949 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08002950 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002951 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2952 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002953 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002954 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
2955 << dex_file_->GetFieldName(field_id) << ") in "
2956 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002957 DCHECK(Thread::Current()->IsExceptionPending());
2958 Thread::Current()->ClearException();
2959 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002960 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2961 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002962 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002963 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002964 return NULL;
2965 } else if (!field->IsStatic()) {
2966 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
2967 return NULL;
2968 } else {
2969 return field;
2970 }
2971}
2972
Ian Rogers776ac1f2012-04-13 23:36:36 -07002973Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002974 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2975 // Check access to class
2976 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002977 if (klass_type.IsConflict()) {
2978 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
2979 field_idx, dex_file_->GetFieldName(field_id),
2980 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002981 return NULL;
2982 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002983 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002984 return NULL; // Can't resolve Class so no more to do here
2985 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002986 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2987 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002988 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002989 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
2990 << dex_file_->GetFieldName(field_id) << ") in "
2991 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002992 DCHECK(Thread::Current()->IsExceptionPending());
2993 Thread::Current()->ClearException();
2994 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002995 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2996 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002997 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002998 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002999 return NULL;
3000 } else if (field->IsStatic()) {
3001 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3002 << " to not be static";
3003 return NULL;
3004 } else if (obj_type.IsZero()) {
3005 // Cannot infer and check type, however, access will cause null pointer exception
3006 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003007 } else {
Ian Rogersb4903572012-10-11 11:52:56 -07003008 Class* klass = field->GetDeclaringClass();
3009 const RegType& field_klass = reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003010 if (obj_type.IsUninitializedTypes() &&
3011 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3012 !field_klass.Equals(GetDeclaringClass()))) {
3013 // Field accesses through uninitialized references are only allowable for constructors where
3014 // the field is declared in this class
3015 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
3016 << " of a not fully initialized object within the context of "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003017 << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003018 return NULL;
3019 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3020 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3021 // of C1. For resolution to occur the declared class of the field must be compatible with
3022 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3023 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3024 << " from object of type " << obj_type;
3025 return NULL;
3026 } else {
3027 return field;
3028 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003029 }
3030}
3031
Ian Rogers776ac1f2012-04-13 23:36:36 -07003032void MethodVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003033 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003034 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003035 Field* field;
3036 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003037 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003038 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003039 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003040 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003041 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003042 const char* descriptor;
Ian Rogers365c1022012-06-22 15:05:28 -07003043 ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003044 if (field != NULL) {
3045 descriptor = FieldHelper(field).GetTypeDescriptor();
3046 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003047 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003048 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3049 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3050 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07003051 }
Ian Rogersb4903572012-10-11 11:52:56 -07003052 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003053 if (is_primitive) {
3054 if (field_type.Equals(insn_type) ||
3055 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3056 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3057 // expected that read is of the correct primitive type or that int reads are reading
3058 // floats or long reads are reading doubles
3059 } else {
3060 // This is a global failure rather than a class change failure as the instructions and
3061 // the descriptors for the type should have been consistent within the same file at
3062 // compile time
3063 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3064 << " to be of type '" << insn_type
3065 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003066 return;
3067 }
3068 } else {
3069 if (!insn_type.IsAssignableFrom(field_type)) {
3070 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3071 << " to be compatible with type '" << insn_type
3072 << "' but found type '" << field_type
3073 << "' in get-object";
3074 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Conflict());
3075 return;
3076 }
3077 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003078 if (!field_type.IsLowHalf()) {
3079 work_line_->SetRegisterType(dec_insn.vA, field_type);
3080 } else {
3081 work_line_->SetRegisterTypeWide(dec_insn.vA, field_type, field_type.HighHalf(&reg_types_));
3082 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003083}
3084
Ian Rogers776ac1f2012-04-13 23:36:36 -07003085void MethodVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003086 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003087 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003088 Field* field;
3089 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003090 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003091 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003092 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07003093 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003094 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003095 const char* descriptor;
Ian Rogers365c1022012-06-22 15:05:28 -07003096 ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003097 if (field != NULL) {
3098 descriptor = FieldHelper(field).GetTypeDescriptor();
3099 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003100 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003101 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3102 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3103 loader = class_loader_;
3104 }
Ian Rogersb4903572012-10-11 11:52:56 -07003105 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003106 if (field != NULL) {
3107 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3108 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3109 << " from other class " << GetDeclaringClass();
3110 return;
3111 }
3112 }
3113 if (is_primitive) {
3114 // Primitive field assignability rules are weaker than regular assignability rules
3115 bool instruction_compatible;
3116 bool value_compatible;
3117 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
3118 if (field_type.IsIntegralTypes()) {
3119 instruction_compatible = insn_type.IsIntegralTypes();
3120 value_compatible = value_type.IsIntegralTypes();
3121 } else if (field_type.IsFloat()) {
3122 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3123 value_compatible = value_type.IsFloatTypes();
3124 } else if (field_type.IsLong()) {
3125 instruction_compatible = insn_type.IsLong();
3126 value_compatible = value_type.IsLongTypes();
3127 } else if (field_type.IsDouble()) {
3128 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3129 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003130 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003131 instruction_compatible = false; // reference field with primitive store
3132 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003133 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003134 if (!instruction_compatible) {
3135 // This is a global failure rather than a class change failure as the instructions and
3136 // the descriptors for the type should have been consistent within the same file at
3137 // compile time
3138 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3139 << " to be of type '" << insn_type
3140 << "' but found type '" << field_type
3141 << "' in put";
3142 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003143 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003144 if (!value_compatible) {
3145 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
3146 << " of type " << value_type
3147 << " but expected " << field_type
3148 << " for store to " << PrettyField(field) << " in put";
3149 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003150 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003151 } else {
3152 if (!insn_type.IsAssignableFrom(field_type)) {
3153 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3154 << " to be compatible with type '" << insn_type
3155 << "' but found type '" << field_type
3156 << "' in put-object";
3157 return;
3158 }
3159 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003160 }
3161}
3162
Ian Rogers776ac1f2012-04-13 23:36:36 -07003163bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003164 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003165 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003166 return false;
3167 }
3168 return true;
3169}
3170
Ian Rogers776ac1f2012-04-13 23:36:36 -07003171bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003172 bool changed = true;
3173 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3174 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003175 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003176 * We haven't processed this instruction before, and we haven't touched the registers here, so
3177 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3178 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003179 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003180 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003181 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003182 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3183 if (gDebugVerify) {
3184 copy->CopyFromLine(target_line);
3185 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003186 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003187 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003188 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003189 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003190 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003191 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003192 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3193 << *copy.get() << " MERGE\n"
3194 << *merge_line << " ==\n"
3195 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003196 }
3197 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003198 if (changed) {
3199 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003200 }
3201 return true;
3202}
3203
Ian Rogers776ac1f2012-04-13 23:36:36 -07003204InsnFlags* MethodVerifier::CurrentInsnFlags() {
3205 return &insn_flags_[work_insn_idx_];
3206}
3207
Ian Rogersad0b3a32012-04-16 14:50:24 -07003208const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003209 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003210 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3211 uint16_t return_type_idx = proto_id.return_type_idx_;
3212 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003213 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003214}
3215
3216const RegType& MethodVerifier::GetDeclaringClass() {
3217 if (foo_method_ != NULL) {
Ian Rogersb4903572012-10-11 11:52:56 -07003218 Class* klass = foo_method_->GetDeclaringClass();
3219 return reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003220 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003221 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003222 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogersb4903572012-10-11 11:52:56 -07003223 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003224 }
3225}
3226
Ian Rogers776ac1f2012-04-13 23:36:36 -07003227void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003228 size_t* log2_max_gc_pc) {
3229 size_t local_gc_points = 0;
3230 size_t max_insn = 0;
3231 size_t max_ref_reg = -1;
3232 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3233 if (insn_flags_[i].IsGcPoint()) {
3234 local_gc_points++;
3235 max_insn = i;
3236 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003237 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003238 }
3239 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003240 *gc_points = local_gc_points;
3241 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3242 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003243 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003244 i++;
3245 }
3246 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003247}
3248
Ian Rogers776ac1f2012-04-13 23:36:36 -07003249const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003250 size_t num_entries, ref_bitmap_bits, pc_bits;
3251 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3252 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003253 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003254 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003255 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003256 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003257 return NULL;
3258 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003259 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3260 // There are 2 bytes to encode the number of entries
3261 if (num_entries >= 65536) {
3262 // 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 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003265 return NULL;
3266 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003267 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003268 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003269 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003270 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003271 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003272 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003273 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003274 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003275 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003276 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003277 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003278 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3279 return NULL;
3280 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003281 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003282 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003283 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003284 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003285 return NULL;
3286 }
3287 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003288 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3289 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003290 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003291 table->push_back(num_entries & 0xFF);
3292 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003293 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003294 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3295 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003296 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003297 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003298 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003299 }
3300 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003301 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003302 }
3303 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003304 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003305 return table;
3306}
jeffhaoa0a764a2011-09-16 10:43:38 -07003307
Ian Rogers776ac1f2012-04-13 23:36:36 -07003308void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003309 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3310 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003311 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003312 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003313 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003314 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3315 if (insn_flags_[i].IsGcPoint()) {
3316 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003317 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003318 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3319 map_index++;
3320 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003321 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003322 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003323 CHECK_LT(j / 8, map.RegWidth());
3324 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3325 } else if ((j / 8) < map.RegWidth()) {
3326 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3327 } else {
3328 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3329 }
3330 }
3331 } else {
3332 CHECK(reg_bitmap == NULL);
3333 }
3334 }
3335}
jeffhaoa0a764a2011-09-16 10:43:38 -07003336
Ian Rogers0c7abda2012-09-19 13:33:42 -07003337void MethodVerifier::SetDexGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003338 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003339 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003340 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
3341 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003342 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003343 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003344 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003345 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003346 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003347 CHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003348}
3349
Ian Rogers0c7abda2012-09-19 13:33:42 -07003350const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(Compiler::MethodReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003351 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003352 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
3353 if (it == dex_gc_maps_->end()) {
Ian Rogers64b6d142012-10-29 16:34:15 -07003354 LOG(WARNING) << "Didn't find GC map for: " << PrettyMethod(ref.second, *ref.first);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003355 return NULL;
3356 }
3357 CHECK(it->second != NULL);
3358 return it->second;
3359}
3360
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003361std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3362 RegisterLine* line = reg_table_.GetLine(dex_pc);
3363 std::vector<int32_t> result;
3364 for (size_t i = 0; i < line->NumRegs(); ++i) {
3365 const RegType& type = line->GetRegisterType(i);
3366 if (type.IsConstant()) {
3367 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3368 result.push_back(type.ConstantValue());
3369 } else if (type.IsConstantLo()) {
3370 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3371 result.push_back(type.ConstantValueLo());
3372 } else if (type.IsConstantHi()) {
3373 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3374 result.push_back(type.ConstantValueHi());
3375 } else if (type.IsIntegralTypes()) {
3376 result.push_back(kIntVReg);
3377 result.push_back(0);
3378 } else if (type.IsFloat()) {
3379 result.push_back(kFloatVReg);
3380 result.push_back(0);
3381 } else if (type.IsLong()) {
3382 result.push_back(kLongLoVReg);
3383 result.push_back(0);
3384 result.push_back(kLongHiVReg);
3385 result.push_back(0);
3386 ++i;
3387 } else if (type.IsDouble()) {
3388 result.push_back(kDoubleLoVReg);
3389 result.push_back(0);
3390 result.push_back(kDoubleHiVReg);
3391 result.push_back(0);
3392 ++i;
3393 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
3394 result.push_back(kUndefined);
3395 result.push_back(0);
3396 } else {
3397 CHECK(type.IsNonZeroReferenceTypes()) << type;
3398 result.push_back(kReferenceVReg);
3399 result.push_back(0);
3400 }
3401 }
3402 return result;
3403}
3404
Ian Rogers0c7abda2012-09-19 13:33:42 -07003405Mutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
3406MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003407
3408Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
3409MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
3410
buzbeec531cef2012-10-18 07:09:20 -07003411#if defined(ART_USE_LLVM_COMPILER)
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003412Mutex* MethodVerifier::inferred_reg_category_maps_lock_ = NULL;
3413MethodVerifier::InferredRegCategoryMapTable* MethodVerifier::inferred_reg_category_maps_ = NULL;
3414#endif
3415
3416void MethodVerifier::Init() {
Ian Rogers0c7abda2012-09-19 13:33:42 -07003417 dex_gc_maps_lock_ = new Mutex("verifier GC maps lock");
Ian Rogers50b35e22012-10-04 10:09:15 -07003418 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003419 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003420 MutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003421 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003422 }
3423
3424 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
3425 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003426 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003427 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
3428 }
3429
buzbeec531cef2012-10-18 07:09:20 -07003430#if defined(ART_USE_LLVM_COMPILER)
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003431 inferred_reg_category_maps_lock_ = new Mutex("verifier GC maps lock");
3432 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003433 MutexLock mu(self, *inferred_reg_category_maps_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003434 inferred_reg_category_maps_ = new MethodVerifier::InferredRegCategoryMapTable;
3435 }
3436#endif
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003437}
3438
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003439void MethodVerifier::Shutdown() {
Ian Rogers50b35e22012-10-04 10:09:15 -07003440 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003441 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003442 MutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003443 STLDeleteValues(dex_gc_maps_);
3444 delete dex_gc_maps_;
3445 dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003446 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003447 delete dex_gc_maps_lock_;
3448 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003449
3450 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003451 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003452 delete rejected_classes_;
3453 rejected_classes_ = NULL;
3454 }
3455 delete rejected_classes_lock_;
3456 rejected_classes_lock_ = NULL;
3457
buzbeec531cef2012-10-18 07:09:20 -07003458#if defined(ART_USE_LLVM_COMPILER)
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003459 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003460 MutexLock mu(self, *inferred_reg_category_maps_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003461 STLDeleteValues(inferred_reg_category_maps_);
3462 delete inferred_reg_category_maps_;
3463 inferred_reg_category_maps_ = NULL;
3464 }
3465 delete inferred_reg_category_maps_lock_;
3466 inferred_reg_category_maps_lock_ = NULL;
3467#endif
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003468}
jeffhaod1224c72012-02-29 13:43:08 -08003469
Ian Rogers776ac1f2012-04-13 23:36:36 -07003470void MethodVerifier::AddRejectedClass(Compiler::ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003471 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003472 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003473 rejected_classes_->insert(ref);
3474 }
jeffhaod1224c72012-02-29 13:43:08 -08003475 CHECK(IsClassRejected(ref));
3476}
3477
Ian Rogers776ac1f2012-04-13 23:36:36 -07003478bool MethodVerifier::IsClassRejected(Compiler::ClassReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003479 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003480 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08003481}
3482
buzbeec531cef2012-10-18 07:09:20 -07003483#if defined(ART_USE_LLVM_COMPILER)
TDYa12789f96052012-07-12 20:49:53 -07003484const greenland::InferredRegCategoryMap* MethodVerifier::GenerateInferredRegCategoryMap() {
Logan Chienfca7e872011-12-20 20:08:22 +08003485 uint32_t insns_size = code_item_->insns_size_in_code_units_;
3486 uint16_t regs_size = code_item_->registers_size_;
3487
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003488 UniquePtr<InferredRegCategoryMap> table(new InferredRegCategoryMap(insns_size, regs_size));
Logan Chienfca7e872011-12-20 20:08:22 +08003489
3490 for (size_t i = 0; i < insns_size; ++i) {
3491 if (RegisterLine* line = reg_table_.GetLine(i)) {
TDYa127526643e2012-05-26 01:01:48 -07003492 const Instruction* inst = Instruction::At(code_item_->insns_ + i);
TDYa127526643e2012-05-26 01:01:48 -07003493 /* We only use InferredRegCategoryMap in one case */
3494 if (inst->IsBranch()) {
TDYa127b2eb5c12012-05-24 15:52:10 -07003495 for (size_t r = 0; r < regs_size; ++r) {
3496 const RegType &rt = line->GetRegisterType(r);
3497
3498 if (rt.IsZero()) {
TDYa12789f96052012-07-12 20:49:53 -07003499 table->SetRegCategory(i, r, greenland::kRegZero);
TDYa127b2eb5c12012-05-24 15:52:10 -07003500 } else if (rt.IsCategory1Types()) {
TDYa12789f96052012-07-12 20:49:53 -07003501 table->SetRegCategory(i, r, greenland::kRegCat1nr);
TDYa127b2eb5c12012-05-24 15:52:10 -07003502 } else if (rt.IsCategory2Types()) {
TDYa12789f96052012-07-12 20:49:53 -07003503 table->SetRegCategory(i, r, greenland::kRegCat2);
TDYa127b2eb5c12012-05-24 15:52:10 -07003504 } else if (rt.IsReferenceTypes()) {
TDYa12789f96052012-07-12 20:49:53 -07003505 table->SetRegCategory(i, r, greenland::kRegObject);
TDYa127b2eb5c12012-05-24 15:52:10 -07003506 } else {
TDYa12789f96052012-07-12 20:49:53 -07003507 table->SetRegCategory(i, r, greenland::kRegUnknown);
TDYa127b2eb5c12012-05-24 15:52:10 -07003508 }
Logan Chienfca7e872011-12-20 20:08:22 +08003509 }
3510 }
3511 }
3512 }
3513
3514 return table.release();
3515}
Logan Chiendd361c92012-04-10 23:40:37 +08003516
Ian Rogers776ac1f2012-04-13 23:36:36 -07003517void MethodVerifier::SetInferredRegCategoryMap(Compiler::MethodReference ref,
3518 const InferredRegCategoryMap& inferred_reg_category_map) {
Shih-wei Liaocd05a622012-08-15 00:02:05 -07003519 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003520 MutexLock mu(Thread::Current(), *inferred_reg_category_maps_lock_);
Shih-wei Liaocd05a622012-08-15 00:02:05 -07003521 InferredRegCategoryMapTable::iterator it = inferred_reg_category_maps_->find(ref);
3522 if (it == inferred_reg_category_maps_->end()) {
3523 inferred_reg_category_maps_->Put(ref, &inferred_reg_category_map);
3524 } else {
3525 CHECK(*(it->second) == inferred_reg_category_map);
3526 delete &inferred_reg_category_map;
3527 }
Logan Chiendd361c92012-04-10 23:40:37 +08003528 }
Logan Chiendd361c92012-04-10 23:40:37 +08003529 CHECK(GetInferredRegCategoryMap(ref) != NULL);
3530}
3531
TDYa12789f96052012-07-12 20:49:53 -07003532const greenland::InferredRegCategoryMap*
Ian Rogers776ac1f2012-04-13 23:36:36 -07003533MethodVerifier::GetInferredRegCategoryMap(Compiler::MethodReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003534 MutexLock mu(Thread::Current(), *inferred_reg_category_maps_lock_);
Logan Chiendd361c92012-04-10 23:40:37 +08003535
3536 InferredRegCategoryMapTable::const_iterator it =
3537 inferred_reg_category_maps_->find(ref);
3538
3539 if (it == inferred_reg_category_maps_->end()) {
3540 return NULL;
3541 }
3542 CHECK(it->second != NULL);
3543 return it->second;
3544}
Logan Chienfca7e872011-12-20 20:08:22 +08003545#endif
3546
Ian Rogersd81871c2011-10-03 13:57:23 -07003547} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003548} // namespace art