blob: b96989ddec184eaa5acf1db8e325c4799e3d38f4 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Ian Rogers776ac1f2012-04-13 23:36:36 -070017#include "method_verifier.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Brian Carlstrom1f870082011-08-23 16:02:11 -070021#include "class_linker.h"
Brian Carlstrome7d856b2012-01-11 18:10:55 -080022#include "compiler.h"
jeffhaob4df5142011-09-19 20:25:32 -070023#include "dex_cache.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070024#include "dex_file.h"
25#include "dex_instruction.h"
26#include "dex_instruction_visitor.h"
Ian Rogers0c7abda2012-09-19 13:33:42 -070027#include "verifier/dex_gc_map.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080028#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070029#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070030#include "leb128.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070031#include "logging.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080032#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070033#include "runtime.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070034#include "stringpiece.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070035
buzbeec531cef2012-10-18 07:09:20 -070036#if defined(ART_USE_LLVM_COMPILER)
TDYa12789f96052012-07-12 20:49:53 -070037#include "greenland/backend_types.h"
38#include "greenland/inferred_reg_category_map.h"
Logan Chienfca7e872011-12-20 20:08:22 +080039#endif
40
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070041namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070042namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070043
Ian Rogers2c8a8572011-10-24 17:11:36 -070044static const bool gDebugVerify = false;
45
Ian Rogers776ac1f2012-04-13 23:36:36 -070046class InsnFlags {
47 public:
48 InsnFlags() : length_(0), flags_(0) {}
49
50 void SetLengthInCodeUnits(size_t length) {
51 CHECK_LT(length, 65536u);
52 length_ = length;
53 }
54 size_t GetLengthInCodeUnits() {
55 return length_;
56 }
57 bool IsOpcode() const {
58 return length_ != 0;
59 }
60
61 void SetInTry() {
62 flags_ |= 1 << kInTry;
63 }
64 void ClearInTry() {
65 flags_ &= ~(1 << kInTry);
66 }
67 bool IsInTry() const {
68 return (flags_ & (1 << kInTry)) != 0;
69 }
70
71 void SetBranchTarget() {
72 flags_ |= 1 << kBranchTarget;
73 }
74 void ClearBranchTarget() {
75 flags_ &= ~(1 << kBranchTarget);
76 }
77 bool IsBranchTarget() const {
78 return (flags_ & (1 << kBranchTarget)) != 0;
79 }
80
81 void SetGcPoint() {
82 flags_ |= 1 << kGcPoint;
83 }
84 void ClearGcPoint() {
85 flags_ &= ~(1 << kGcPoint);
86 }
87 bool IsGcPoint() const {
88 return (flags_ & (1 << kGcPoint)) != 0;
89 }
90
91 void SetVisited() {
92 flags_ |= 1 << kVisited;
93 }
94 void ClearVisited() {
95 flags_ &= ~(1 << kVisited);
96 }
97 bool IsVisited() const {
98 return (flags_ & (1 << kVisited)) != 0;
99 }
100
101 void SetChanged() {
102 flags_ |= 1 << kChanged;
103 }
104 void ClearChanged() {
105 flags_ &= ~(1 << kChanged);
106 }
107 bool IsChanged() const {
108 return (flags_ & (1 << kChanged)) != 0;
109 }
110
111 bool IsVisitedOrChanged() const {
112 return IsVisited() || IsChanged();
113 }
114
115 std::string Dump() {
116 char encoding[6];
117 if (!IsOpcode()) {
118 strncpy(encoding, "XXXXX", sizeof(encoding));
119 } else {
120 strncpy(encoding, "-----", sizeof(encoding));
121 if (IsInTry()) encoding[kInTry] = 'T';
122 if (IsBranchTarget()) encoding[kBranchTarget] = 'B';
123 if (IsGcPoint()) encoding[kGcPoint] = 'G';
124 if (IsVisited()) encoding[kVisited] = 'V';
125 if (IsChanged()) encoding[kChanged] = 'C';
126 }
127 return std::string(encoding);
128 }
Elliott Hughesa21039c2012-06-21 12:09:25 -0700129
Ian Rogers776ac1f2012-04-13 23:36:36 -0700130 private:
131 enum {
132 kInTry,
133 kBranchTarget,
134 kGcPoint,
135 kVisited,
136 kChanged,
137 };
138
139 // Size of instruction in code units
140 uint16_t length_;
141 uint8_t flags_;
Ian Rogers84fa0742011-10-25 18:13:30 -0700142};
Ian Rogersd81871c2011-10-03 13:57:23 -0700143
Ian Rogersd81871c2011-10-03 13:57:23 -0700144void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
145 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -0700146 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700147 DCHECK_GT(insns_size, 0U);
148
149 for (uint32_t i = 0; i < insns_size; i++) {
150 bool interesting = false;
151 switch (mode) {
152 case kTrackRegsAll:
153 interesting = flags[i].IsOpcode();
154 break;
155 case kTrackRegsGcPoints:
156 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
157 break;
158 case kTrackRegsBranches:
159 interesting = flags[i].IsBranchTarget();
160 break;
161 default:
162 break;
163 }
164 if (interesting) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700165 pc_to_register_line_.Put(i, new RegisterLine(registers_size, verifier));
Ian Rogersd81871c2011-10-03 13:57:23 -0700166 }
167 }
168}
169
jeffhaof1e6b7c2012-06-05 18:33:30 -0700170MethodVerifier::FailureKind MethodVerifier::VerifyClass(const Class* klass, std::string& error) {
jeffhaobdb76512011-09-07 11:43:16 -0700171 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700172 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700173 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700174 Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -0800175 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800176 error = "Verifier rejected class ";
177 error += PrettyDescriptor(klass);
178 error += " that has no super class";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700179 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -0700180 }
Ian Rogers1c5eb702012-02-01 09:18:34 -0800181 if (super != NULL && super->IsFinal()) {
182 error = "Verifier rejected class ";
183 error += PrettyDescriptor(klass);
184 error += " that attempts to sub-class final class ";
185 error += PrettyDescriptor(super);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700186 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -0700187 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700188 ClassHelper kh(klass);
189 const DexFile& dex_file = kh.GetDexFile();
190 uint32_t class_def_idx;
191 if (!dex_file.FindClassDefIndex(kh.GetDescriptor(), class_def_idx)) {
192 error = "Verifier rejected class ";
193 error += PrettyDescriptor(klass);
194 error += " that isn't present in dex file ";
195 error += dex_file.GetLocation();
jeffhaof1e6b7c2012-06-05 18:33:30 -0700196 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700197 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700198 return VerifyClass(&dex_file, kh.GetDexCache(), klass->GetClassLoader(), class_def_idx, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700199}
200
Ian Rogers365c1022012-06-22 15:05:28 -0700201MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
202 DexCache* dex_cache, ClassLoader* class_loader, uint32_t class_def_idx, std::string& error) {
jeffhaof56197c2012-03-05 18:01:54 -0800203 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
204 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700205 if (class_data == NULL) {
206 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700207 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700208 }
jeffhaof56197c2012-03-05 18:01:54 -0800209 ClassDataItemIterator it(*dex_file, class_data);
210 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
211 it.Next();
212 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700213 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700214 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700215 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700216 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800217 while (it.HasNextDirectMethod()) {
218 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700219 if (method_idx == previous_direct_method_idx) {
220 // smali can create dex files with two encoded_methods sharing the same method_idx
221 // http://code.google.com/p/smali/issues/detail?id=119
222 it.Next();
223 continue;
224 }
225 previous_direct_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700226 InvokeType type = it.GetMethodInvokeType(class_def);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700227 AbstractMethod* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700228 if (method == NULL) {
229 DCHECK(Thread::Current()->IsExceptionPending());
230 // We couldn't resolve the method, but continue regardless.
231 Thread::Current()->ClearException();
232 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700233 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
234 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags());
235 if (result != kNoFailure) {
236 if (result == kHardFailure) {
237 hard_fail = true;
238 if (error_count > 0) {
239 error += "\n";
240 }
241 error = "Verifier rejected class ";
242 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
243 error += " due to bad method ";
244 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700245 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700246 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800247 }
248 it.Next();
249 }
jeffhao9b0b1882012-10-01 16:51:22 -0700250 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800251 while (it.HasNextVirtualMethod()) {
252 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700253 if (method_idx == previous_virtual_method_idx) {
254 // smali can create dex files with two encoded_methods sharing the same method_idx
255 // http://code.google.com/p/smali/issues/detail?id=119
256 it.Next();
257 continue;
258 }
259 previous_virtual_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700260 InvokeType type = it.GetMethodInvokeType(class_def);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700261 AbstractMethod* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700262 if (method == NULL) {
263 DCHECK(Thread::Current()->IsExceptionPending());
264 // We couldn't resolve the method, but continue regardless.
265 Thread::Current()->ClearException();
266 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700267 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
268 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags());
269 if (result != kNoFailure) {
270 if (result == kHardFailure) {
271 hard_fail = true;
272 if (error_count > 0) {
273 error += "\n";
274 }
275 error = "Verifier rejected class ";
276 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
277 error += " due to bad method ";
278 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700279 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700280 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800281 }
282 it.Next();
283 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700284 if (error_count == 0) {
285 return kNoFailure;
286 } else {
287 return hard_fail ? kHardFailure : kSoftFailure;
288 }
jeffhaof56197c2012-03-05 18:01:54 -0800289}
290
jeffhaof1e6b7c2012-06-05 18:33:30 -0700291MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx, const DexFile* dex_file,
Ian Rogers365c1022012-06-22 15:05:28 -0700292 DexCache* dex_cache, ClassLoader* class_loader, uint32_t class_def_idx,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700293 const DexFile::CodeItem* code_item, AbstractMethod* method, uint32_t method_access_flags) {
Ian Rogersc8982582012-09-07 16:53:25 -0700294 MethodVerifier::FailureKind result = kNoFailure;
295 uint64_t start_ns = NanoTime();
296
Ian Rogersad0b3a32012-04-16 14:50:24 -0700297 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item, method_idx,
298 method, method_access_flags);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700299 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700300 // Verification completed, however failures may be pending that didn't cause the verification
301 // to hard fail.
Ian Rogerse551e952012-06-03 22:59:14 -0700302 CHECK(!verifier.have_pending_hard_failure_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700303 if (verifier.failures_.size() != 0) {
304 verifier.DumpFailures(LOG(INFO) << "Soft verification failures in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700305 << PrettyMethod(method_idx, *dex_file) << "\n");
Ian Rogersc8982582012-09-07 16:53:25 -0700306 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800307 }
308 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700309 // Bad method data.
310 CHECK_NE(verifier.failures_.size(), 0U);
311 CHECK(verifier.have_pending_hard_failure_);
312 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700313 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800314 if (gDebugVerify) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700315 std::cout << "\n" << verifier.info_messages_.str();
jeffhaof56197c2012-03-05 18:01:54 -0800316 verifier.Dump(std::cout);
317 }
Ian Rogersc8982582012-09-07 16:53:25 -0700318 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800319 }
Ian Rogersc8982582012-09-07 16:53:25 -0700320 uint64_t duration_ns = NanoTime() - start_ns;
321 if (duration_ns > MsToNs(100)) {
322 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
323 << " took " << PrettyDuration(duration_ns);
324 }
325 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800326}
327
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800328void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
329 const DexFile* dex_file, DexCache* dex_cache,
330 ClassLoader* class_loader, uint32_t class_def_idx,
331 const DexFile::CodeItem* code_item, AbstractMethod* method,
332 uint32_t method_access_flags) {
333 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item,
334 dex_method_idx, method, method_access_flags);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700335 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800336 verifier.DumpFailures(os);
337 os << verifier.info_messages_.str();
338 verifier.Dump(os);
339}
340
341std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_method_idx,
342 const DexFile* dex_file, DexCache* dex_cache,
343 ClassLoader* class_loader,
344 uint32_t class_def_idx,
345 const DexFile::CodeItem* code_item,
346 AbstractMethod* method,
347 uint32_t method_access_flags, uint32_t dex_pc) {
348 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item,
349 dex_method_idx, method, method_access_flags);
350 verifier.Verify();
351 return verifier.DescribeVRegs(dex_pc);
jeffhaoba5ebb92011-08-25 17:24:37 -0700352}
353
Ian Rogers776ac1f2012-04-13 23:36:36 -0700354MethodVerifier::MethodVerifier(const DexFile* dex_file, DexCache* dex_cache,
Ian Rogers365c1022012-06-22 15:05:28 -0700355 ClassLoader* class_loader, uint32_t class_def_idx, const DexFile::CodeItem* code_item,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800356 uint32_t dex_method_idx, AbstractMethod* method, uint32_t method_access_flags)
jeffhaof56197c2012-03-05 18:01:54 -0800357 : work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800358 dex_method_idx_(dex_method_idx),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700359 foo_method_(method),
360 method_access_flags_(method_access_flags),
jeffhaof56197c2012-03-05 18:01:54 -0800361 dex_file_(dex_file),
362 dex_cache_(dex_cache),
363 class_loader_(class_loader),
364 class_def_idx_(class_def_idx),
365 code_item_(code_item),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700366 interesting_dex_pc_(-1),
367 monitor_enter_dex_pcs_(NULL),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700368 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700369 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800370 new_instance_count_(0),
371 monitor_enter_count_(0) {
372}
373
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800374void MethodVerifier::FindLocksAtDexPc(AbstractMethod* m, uint32_t dex_pc,
375 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700376 MethodHelper mh(m);
377 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
378 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
379 m, m->GetAccessFlags());
380 verifier.interesting_dex_pc_ = dex_pc;
381 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
382 verifier.FindLocksAtDexPc();
383}
384
385void MethodVerifier::FindLocksAtDexPc() {
386 CHECK(monitor_enter_dex_pcs_ != NULL);
387 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
388
389 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
390 // verification. In practice, the phase we want relies on data structures set up by all the
391 // earlier passes, so we just run the full method verification and bail out early when we've
392 // got what we wanted.
393 Verify();
394}
395
Ian Rogersad0b3a32012-04-16 14:50:24 -0700396bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700397 // If there aren't any instructions, make sure that's expected, then exit successfully.
398 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700399 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700400 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700401 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700402 } else {
403 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700404 }
jeffhaobdb76512011-09-07 11:43:16 -0700405 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700406 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
407 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700408 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
409 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700410 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700411 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700412 // Allocate and initialize an array to hold instruction data.
413 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
414 // Run through the instructions and see if the width checks out.
415 bool result = ComputeWidthsAndCountOps();
416 // Flag instructions guarded by a "try" block and check exception handlers.
417 result = result && ScanTryCatchBlocks();
418 // Perform static instruction verification.
419 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700420 // Perform code-flow analysis and return.
421 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700422}
423
Ian Rogers776ac1f2012-04-13 23:36:36 -0700424std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700425 switch (error) {
426 case VERIFY_ERROR_NO_CLASS:
427 case VERIFY_ERROR_NO_FIELD:
428 case VERIFY_ERROR_NO_METHOD:
429 case VERIFY_ERROR_ACCESS_CLASS:
430 case VERIFY_ERROR_ACCESS_FIELD:
431 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700432 case VERIFY_ERROR_INSTANTIATION:
433 case VERIFY_ERROR_CLASS_CHANGE:
jeffhaofaf459e2012-08-31 15:32:47 -0700434 if (Runtime::Current()->IsCompiler()) {
435 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
436 // class change and instantiation errors into soft verification errors so that we re-verify
437 // at runtime. We may fail to find or to agree on access because of not yet available class
438 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
439 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
440 // paths" that dynamically perform the verification and cause the behavior to be that akin
441 // to an interpreter.
442 error = VERIFY_ERROR_BAD_CLASS_SOFT;
443 } else {
444 have_pending_runtime_throw_failure_ = true;
445 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700446 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700447 // Indication that verification should be retried at runtime.
448 case VERIFY_ERROR_BAD_CLASS_SOFT:
449 if (!Runtime::Current()->IsCompiler()) {
450 // It is runtime so hard fail.
451 have_pending_hard_failure_ = true;
452 }
453 break;
jeffhaod5347e02012-03-22 17:25:05 -0700454 // Hard verification failures at compile time will still fail at runtime, so the class is
455 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700456 case VERIFY_ERROR_BAD_CLASS_HARD: {
457 if (Runtime::Current()->IsCompiler()) {
jeffhaof56197c2012-03-05 18:01:54 -0800458 Compiler::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800459 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800460 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700461 have_pending_hard_failure_ = true;
462 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800463 }
464 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700465 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800466 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700467 work_insn_idx_));
468 std::ostringstream* failure_message = new std::ostringstream(location);
469 failure_messages_.push_back(failure_message);
470 return *failure_message;
471}
472
473void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
474 size_t failure_num = failure_messages_.size();
475 DCHECK_NE(failure_num, 0U);
476 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
477 prepend += last_fail_message->str();
478 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
479 delete last_fail_message;
480}
481
482void MethodVerifier::AppendToLastFailMessage(std::string append) {
483 size_t failure_num = failure_messages_.size();
484 DCHECK_NE(failure_num, 0U);
485 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
486 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800487}
488
Ian Rogers776ac1f2012-04-13 23:36:36 -0700489bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700490 const uint16_t* insns = code_item_->insns_;
491 size_t insns_size = code_item_->insns_size_in_code_units_;
492 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700493 size_t new_instance_count = 0;
494 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700495 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700496
Ian Rogersd81871c2011-10-03 13:57:23 -0700497 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700498 Instruction::Code opcode = inst->Opcode();
499 if (opcode == Instruction::NEW_INSTANCE) {
500 new_instance_count++;
501 } else if (opcode == Instruction::MONITOR_ENTER) {
502 monitor_enter_count++;
503 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700504 size_t inst_size = inst->SizeInCodeUnits();
505 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
506 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700507 inst = inst->Next();
508 }
509
Ian Rogersd81871c2011-10-03 13:57:23 -0700510 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700511 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
512 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700513 return false;
514 }
515
Ian Rogersd81871c2011-10-03 13:57:23 -0700516 new_instance_count_ = new_instance_count;
517 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700518 return true;
519}
520
Ian Rogers776ac1f2012-04-13 23:36:36 -0700521bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700522 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700523 if (tries_size == 0) {
524 return true;
525 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700526 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700527 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700528
529 for (uint32_t idx = 0; idx < tries_size; idx++) {
530 const DexFile::TryItem* try_item = &tries[idx];
531 uint32_t start = try_item->start_addr_;
532 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700533 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700534 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
535 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700536 return false;
537 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700538 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700539 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700540 return false;
541 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700542 for (uint32_t dex_pc = start; dex_pc < end;
543 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
544 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700545 }
546 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800547 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700548 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700549 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700550 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700551 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700552 CatchHandlerIterator iterator(handlers_ptr);
553 for (; iterator.HasNext(); iterator.Next()) {
554 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700555 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700556 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700557 return false;
558 }
jeffhao60f83e32012-02-13 17:16:30 -0800559 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
560 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700561 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700562 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800563 return false;
564 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700565 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700566 // Ensure exception types are resolved so that they don't need resolution to be delivered,
567 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700568 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
jeffhaof56197c2012-03-05 18:01:54 -0800569 Class* exception_type = linker->ResolveType(*dex_file_, iterator.GetHandlerTypeIndex(),
570 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700571 if (exception_type == NULL) {
572 DCHECK(Thread::Current()->IsExceptionPending());
573 Thread::Current()->ClearException();
574 }
575 }
jeffhaobdb76512011-09-07 11:43:16 -0700576 }
Ian Rogers0571d352011-11-03 19:51:38 -0700577 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700578 }
jeffhaobdb76512011-09-07 11:43:16 -0700579 return true;
580}
581
Ian Rogers776ac1f2012-04-13 23:36:36 -0700582bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700583 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700584
Ian Rogers0c7abda2012-09-19 13:33:42 -0700585 /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */
Ian Rogersd81871c2011-10-03 13:57:23 -0700586 insn_flags_[0].SetBranchTarget();
Ian Rogers0c7abda2012-09-19 13:33:42 -0700587 insn_flags_[0].SetGcPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700588
589 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700590 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700591 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700592 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700593 return false;
594 }
595 /* Flag instructions that are garbage collection points */
596 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
597 insn_flags_[dex_pc].SetGcPoint();
598 }
599 dex_pc += inst->SizeInCodeUnits();
600 inst = inst->Next();
601 }
602 return true;
603}
604
Ian Rogers776ac1f2012-04-13 23:36:36 -0700605bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800606 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700607 bool result = true;
608 switch (inst->GetVerifyTypeArgumentA()) {
609 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800610 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700611 break;
612 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800613 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700614 break;
615 }
616 switch (inst->GetVerifyTypeArgumentB()) {
617 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800618 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700619 break;
620 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800621 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700622 break;
623 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800624 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700625 break;
626 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800627 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700628 break;
629 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800630 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700631 break;
632 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800633 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700634 break;
635 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800636 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700637 break;
638 }
639 switch (inst->GetVerifyTypeArgumentC()) {
640 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800641 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700642 break;
643 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800644 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700645 break;
646 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800647 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700648 break;
649 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800650 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700651 break;
652 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800653 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700654 break;
655 }
656 switch (inst->GetVerifyExtraFlags()) {
657 case Instruction::kVerifyArrayData:
658 result = result && CheckArrayData(code_offset);
659 break;
660 case Instruction::kVerifyBranchTarget:
661 result = result && CheckBranchTarget(code_offset);
662 break;
663 case Instruction::kVerifySwitchTargets:
664 result = result && CheckSwitchTargets(code_offset);
665 break;
666 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800667 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700668 break;
669 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800670 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700671 break;
672 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700673 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 result = false;
675 break;
676 }
677 return result;
678}
679
Ian Rogers776ac1f2012-04-13 23:36:36 -0700680bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700681 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700682 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
683 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700684 return false;
685 }
686 return true;
687}
688
Ian Rogers776ac1f2012-04-13 23:36:36 -0700689bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700690 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700691 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
692 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700693 return false;
694 }
695 return true;
696}
697
Ian Rogers776ac1f2012-04-13 23:36:36 -0700698bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700699 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700700 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
701 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700702 return false;
703 }
704 return true;
705}
706
Ian Rogers776ac1f2012-04-13 23:36:36 -0700707bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700708 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700709 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
710 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700711 return false;
712 }
713 return true;
714}
715
Ian Rogers776ac1f2012-04-13 23:36:36 -0700716bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700717 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
719 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700720 return false;
721 }
722 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700723 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700724 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700725 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700726 return false;
727 }
728 return true;
729}
730
Ian Rogers776ac1f2012-04-13 23:36:36 -0700731bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700732 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
734 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700735 return false;
736 }
737 return true;
738}
739
Ian Rogers776ac1f2012-04-13 23:36:36 -0700740bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700741 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700742 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
743 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700744 return false;
745 }
746 return true;
747}
748
Ian Rogers776ac1f2012-04-13 23:36:36 -0700749bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700750 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700751 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
752 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700753 return false;
754 }
755 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700756 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700757 const char* cp = descriptor;
758 while (*cp++ == '[') {
759 bracket_count++;
760 }
761 if (bracket_count == 0) {
762 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700763 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700764 return false;
765 } else if (bracket_count > 255) {
766 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700767 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700768 return false;
769 }
770 return true;
771}
772
Ian Rogers776ac1f2012-04-13 23:36:36 -0700773bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700774 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
775 const uint16_t* insns = code_item_->insns_ + cur_offset;
776 const uint16_t* array_data;
777 int32_t array_data_offset;
778
779 DCHECK_LT(cur_offset, insn_count);
780 /* make sure the start of the array data table is in range */
781 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
782 if ((int32_t) cur_offset + array_data_offset < 0 ||
783 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700784 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
785 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700786 return false;
787 }
788 /* offset to array data table is a relative branch-style offset */
789 array_data = insns + array_data_offset;
790 /* make sure the table is 32-bit aligned */
791 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700792 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
793 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700794 return false;
795 }
796 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700797 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700798 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
799 /* make sure the end of the switch is in range */
800 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700801 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
802 << ", data offset " << array_data_offset << ", end "
803 << cur_offset + array_data_offset + table_size
804 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700805 return false;
806 }
807 return true;
808}
809
Ian Rogers776ac1f2012-04-13 23:36:36 -0700810bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700811 int32_t offset;
812 bool isConditional, selfOkay;
813 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
814 return false;
815 }
816 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700817 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at" << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700818 return false;
819 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700820 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
821 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700822 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700823 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700824 return false;
825 }
826 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
827 int32_t abs_offset = cur_offset + offset;
828 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700829 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700830 << reinterpret_cast<void*>(abs_offset) << ") at "
831 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700832 return false;
833 }
834 insn_flags_[abs_offset].SetBranchTarget();
835 return true;
836}
837
Ian Rogers776ac1f2012-04-13 23:36:36 -0700838bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700839 bool* selfOkay) {
840 const uint16_t* insns = code_item_->insns_ + cur_offset;
841 *pConditional = false;
842 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700843 switch (*insns & 0xff) {
844 case Instruction::GOTO:
845 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700846 break;
847 case Instruction::GOTO_32:
848 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700849 *selfOkay = true;
850 break;
851 case Instruction::GOTO_16:
852 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700853 break;
854 case Instruction::IF_EQ:
855 case Instruction::IF_NE:
856 case Instruction::IF_LT:
857 case Instruction::IF_GE:
858 case Instruction::IF_GT:
859 case Instruction::IF_LE:
860 case Instruction::IF_EQZ:
861 case Instruction::IF_NEZ:
862 case Instruction::IF_LTZ:
863 case Instruction::IF_GEZ:
864 case Instruction::IF_GTZ:
865 case Instruction::IF_LEZ:
866 *pOffset = (int16_t) insns[1];
867 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700868 break;
869 default:
870 return false;
871 break;
872 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700873 return true;
874}
875
Ian Rogers776ac1f2012-04-13 23:36:36 -0700876bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700877 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700878 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700879 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700880 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700881 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
882 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700883 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
884 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700885 return false;
886 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700887 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700888 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700889 /* make sure the table is 32-bit aligned */
890 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700891 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
892 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700893 return false;
894 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700895 uint32_t switch_count = switch_insns[1];
896 int32_t keys_offset, targets_offset;
897 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700898 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
899 /* 0=sig, 1=count, 2/3=firstKey */
900 targets_offset = 4;
901 keys_offset = -1;
902 expected_signature = Instruction::kPackedSwitchSignature;
903 } else {
904 /* 0=sig, 1=count, 2..count*2 = keys */
905 keys_offset = 2;
906 targets_offset = 2 + 2 * switch_count;
907 expected_signature = Instruction::kSparseSwitchSignature;
908 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700909 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700910 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700911 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
912 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700913 return false;
914 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700915 /* make sure the end of the switch is in range */
916 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700917 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
918 << switch_offset << ", end "
919 << (cur_offset + switch_offset + table_size)
920 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700921 return false;
922 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700923 /* for a sparse switch, verify the keys are in ascending order */
924 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700925 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
926 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700927 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
928 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
929 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700930 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
931 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700932 return false;
933 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700934 last_key = key;
935 }
936 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700937 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700938 for (uint32_t targ = 0; targ < switch_count; targ++) {
939 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
940 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
941 int32_t abs_offset = cur_offset + offset;
942 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700943 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700944 << reinterpret_cast<void*>(abs_offset) << ") at "
945 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700946 return false;
947 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700948 insn_flags_[abs_offset].SetBranchTarget();
949 }
950 return true;
951}
952
Ian Rogers776ac1f2012-04-13 23:36:36 -0700953bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700954 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700955 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700956 return false;
957 }
958 uint16_t registers_size = code_item_->registers_size_;
959 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800960 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
962 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700963 return false;
964 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700965 }
966
967 return true;
968}
969
Ian Rogers776ac1f2012-04-13 23:36:36 -0700970bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 uint16_t registers_size = code_item_->registers_size_;
972 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
973 // integer overflow when adding them here.
974 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700975 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
976 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700977 return false;
978 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700979 return true;
980}
981
Ian Rogers0c7abda2012-09-19 13:33:42 -0700982static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -0800983 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
984 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
985 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
986 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
987 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
988 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
989 gc_map.begin(),
990 gc_map.end());
991 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
992 DCHECK_EQ(gc_map.size(),
993 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
994 (length_prefixed_gc_map->at(1) << 16) |
995 (length_prefixed_gc_map->at(2) << 8) |
996 (length_prefixed_gc_map->at(3) << 0)));
997 return length_prefixed_gc_map;
998}
999
Ian Rogers776ac1f2012-04-13 23:36:36 -07001000bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001001 uint16_t registers_size = code_item_->registers_size_;
1002 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001003
Ian Rogersd81871c2011-10-03 13:57:23 -07001004 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001005 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1006 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001007 }
1008 /* Create and initialize table holding register status */
Elliott Hughes460384f2012-04-04 16:53:10 -07001009 reg_table_.Init(kTrackRegsGcPoints, insn_flags_.get(), insns_size, registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001010
Ian Rogersd81871c2011-10-03 13:57:23 -07001011 work_line_.reset(new RegisterLine(registers_size, this));
1012 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001013
Ian Rogersd81871c2011-10-03 13:57:23 -07001014 /* Initialize register types of method arguments. */
1015 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001016 DCHECK_NE(failures_.size(), 0U);
1017 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001018 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001019 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001020 return false;
1021 }
1022 /* Perform code flow verification. */
1023 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001024 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001025 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001026 }
1027
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001028 Compiler::MethodReference ref(dex_file_, dex_method_idx_);
TDYa127b2eb5c12012-05-24 15:52:10 -07001029
TDYa127b2eb5c12012-05-24 15:52:10 -07001030
Ian Rogersd81871c2011-10-03 13:57:23 -07001031 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -08001032 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1033 if (map.get() == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001034 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001035 return false; // Not a real failure, but a failure to encode
1036 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001037#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001038 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -07001039#endif
Ian Rogers0c7abda2012-09-19 13:33:42 -07001040 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
1041 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
Logan Chiendd361c92012-04-10 23:40:37 +08001042
TDYa127ce4cc0d2012-11-18 16:59:53 -08001043#if defined(ART_USE_LLVM_COMPILER)
Logan Chienfca7e872011-12-20 20:08:22 +08001044 /* Generate Inferred Register Category for LLVM-based Code Generator */
1045 const InferredRegCategoryMap* table = GenerateInferredRegCategoryMap();
Ian Rogers776ac1f2012-04-13 23:36:36 -07001046 verifier::MethodVerifier::SetInferredRegCategoryMap(ref, *table);
Logan Chienfca7e872011-12-20 20:08:22 +08001047#endif
1048
TDYa127ce4cc0d2012-11-18 16:59:53 -08001049
jeffhaobdb76512011-09-07 11:43:16 -07001050 return true;
1051}
1052
Ian Rogersad0b3a32012-04-16 14:50:24 -07001053std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1054 DCHECK_EQ(failures_.size(), failure_messages_.size());
1055 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001056 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001057 }
1058 return os;
1059}
1060
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001061extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001062 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001063 v->Dump(std::cerr);
1064}
1065
Ian Rogers776ac1f2012-04-13 23:36:36 -07001066void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001067 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001068 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001069 return;
jeffhaobdb76512011-09-07 11:43:16 -07001070 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001071 {
1072 os << "Register Types:\n";
1073 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1074 std::ostream indent_os(&indent_filter);
1075 reg_types_.Dump(indent_os);
1076 }
Ian Rogersb4903572012-10-11 11:52:56 -07001077 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001078 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1079 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001080 const Instruction* inst = Instruction::At(code_item_->insns_);
1081 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1082 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001083 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1084 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001085 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001086 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001087 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump() << " ";
1088 const bool kDumpHexOfInstruction = false;
1089 if (kDumpHexOfInstruction) {
1090 indent_os << inst->DumpHex(5) << " ";
1091 }
1092 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001093 inst = inst->Next();
1094 }
jeffhaobdb76512011-09-07 11:43:16 -07001095}
1096
Ian Rogersd81871c2011-10-03 13:57:23 -07001097static bool IsPrimitiveDescriptor(char descriptor) {
1098 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001099 case 'I':
1100 case 'C':
1101 case 'S':
1102 case 'B':
1103 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001104 case 'F':
1105 case 'D':
1106 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001107 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001108 default:
1109 return false;
1110 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001111}
1112
Ian Rogers776ac1f2012-04-13 23:36:36 -07001113bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001114 RegisterLine* reg_line = reg_table_.GetLine(0);
1115 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1116 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001117
Ian Rogersd81871c2011-10-03 13:57:23 -07001118 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1119 //Include the "this" pointer.
1120 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001121 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001122 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1123 // argument as uninitialized. This restricts field access until the superclass constructor is
1124 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001125 const RegType& declaring_class = GetDeclaringClass();
1126 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001127 reg_line->SetRegisterType(arg_start + cur_arg,
1128 reg_types_.UninitializedThisArgument(declaring_class));
1129 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001130 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001131 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001132 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001133 }
1134
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001135 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001136 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001137 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001138
1139 for (; iterator.HasNext(); iterator.Next()) {
1140 const char* descriptor = iterator.GetDescriptor();
1141 if (descriptor == NULL) {
1142 LOG(FATAL) << "Null descriptor";
1143 }
1144 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001145 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1146 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001147 return false;
1148 }
1149 switch (descriptor[0]) {
1150 case 'L':
1151 case '[':
1152 // We assume that reference arguments are initialized. The only way it could be otherwise
1153 // (assuming the caller was verified) is if the current method is <init>, but in that case
1154 // it's effectively considered initialized the instant we reach here (in the sense that we
1155 // can return without doing anything or call virtual methods).
1156 {
Ian Rogersb4903572012-10-11 11:52:56 -07001157 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001158 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001159 }
1160 break;
1161 case 'Z':
1162 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1163 break;
1164 case 'C':
1165 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1166 break;
1167 case 'B':
1168 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1169 break;
1170 case 'I':
1171 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1172 break;
1173 case 'S':
1174 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1175 break;
1176 case 'F':
1177 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1178 break;
1179 case 'J':
1180 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001181 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1182 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1183 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001184 cur_arg++;
1185 break;
1186 }
1187 default:
jeffhaod5347e02012-03-22 17:25:05 -07001188 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001189 return false;
1190 }
1191 cur_arg++;
1192 }
1193 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001194 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001195 return false;
1196 }
1197 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1198 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1199 // format. Only major difference from the method argument format is that 'V' is supported.
1200 bool result;
1201 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1202 result = descriptor[1] == '\0';
1203 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1204 size_t i = 0;
1205 do {
1206 i++;
1207 } while (descriptor[i] == '['); // process leading [
1208 if (descriptor[i] == 'L') { // object array
1209 do {
1210 i++; // find closing ;
1211 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1212 result = descriptor[i] == ';';
1213 } else { // primitive array
1214 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1215 }
1216 } else if (descriptor[0] == 'L') {
1217 // could be more thorough here, but shouldn't be required
1218 size_t i = 0;
1219 do {
1220 i++;
1221 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1222 result = descriptor[i] == ';';
1223 } else {
1224 result = false;
1225 }
1226 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001227 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1228 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001229 }
1230 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001231}
1232
Ian Rogers776ac1f2012-04-13 23:36:36 -07001233bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001234 const uint16_t* insns = code_item_->insns_;
1235 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001236
jeffhaobdb76512011-09-07 11:43:16 -07001237 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001238 insn_flags_[0].SetChanged();
1239 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001240
jeffhaobdb76512011-09-07 11:43:16 -07001241 /* Continue until no instructions are marked "changed". */
1242 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001243 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1244 uint32_t insn_idx = start_guess;
1245 for (; insn_idx < insns_size; insn_idx++) {
1246 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001247 break;
1248 }
jeffhaobdb76512011-09-07 11:43:16 -07001249 if (insn_idx == insns_size) {
1250 if (start_guess != 0) {
1251 /* try again, starting from the top */
1252 start_guess = 0;
1253 continue;
1254 } else {
1255 /* all flags are clear */
1256 break;
1257 }
1258 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001259 // We carry the working set of registers from instruction to instruction. If this address can
1260 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1261 // "changed" flags, we need to load the set of registers from the table.
1262 // Because we always prefer to continue on to the next instruction, we should never have a
1263 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1264 // target.
1265 work_insn_idx_ = insn_idx;
1266 if (insn_flags_[insn_idx].IsBranchTarget()) {
1267 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001268 } else {
1269#ifndef NDEBUG
1270 /*
1271 * Sanity check: retrieve the stored register line (assuming
1272 * a full table) and make sure it actually matches.
1273 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001274 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1275 if (register_line != NULL) {
1276 if (work_line_->CompareLine(register_line) != 0) {
1277 Dump(std::cout);
1278 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001279 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001280 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1281 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001282 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001283 }
jeffhaobdb76512011-09-07 11:43:16 -07001284 }
1285#endif
1286 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001287 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001288 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001289 prepend += " failed to verify: ";
1290 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001291 return false;
1292 }
jeffhaobdb76512011-09-07 11:43:16 -07001293 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001294 insn_flags_[insn_idx].SetVisited();
1295 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001296 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001297
Ian Rogers1c849e52012-06-28 14:00:33 -07001298 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001299 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001300 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001301 * (besides the wasted space), but it indicates a flaw somewhere
1302 * down the line, possibly in the verifier.
1303 *
1304 * If we've substituted "always throw" instructions into the stream,
1305 * we are almost certainly going to have some dead code.
1306 */
1307 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 uint32_t insn_idx = 0;
1309 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001310 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001311 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001312 * may or may not be preceded by a padding NOP (for alignment).
1313 */
1314 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1315 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1316 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001317 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001318 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1319 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1320 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001321 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001322 }
1323
Ian Rogersd81871c2011-10-03 13:57:23 -07001324 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001325 if (dead_start < 0)
1326 dead_start = insn_idx;
1327 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001328 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001329 dead_start = -1;
1330 }
1331 }
1332 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001333 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001334 }
1335 }
jeffhaobdb76512011-09-07 11:43:16 -07001336 return true;
1337}
1338
Ian Rogers776ac1f2012-04-13 23:36:36 -07001339bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001340#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001341 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001342 gDvm.verifierStats.instrsReexamined++;
1343 } else {
1344 gDvm.verifierStats.instrsExamined++;
1345 }
1346#endif
1347
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001348 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1349 // We want the state _before_ the instruction, for the case where the dex pc we're
1350 // interested in is itself a monitor-enter instruction (which is a likely place
1351 // for a thread to be suspended).
1352 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
1353 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1354 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1355 }
1356 }
1357
jeffhaobdb76512011-09-07 11:43:16 -07001358 /*
1359 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001360 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001361 * control to another statement:
1362 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001363 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001364 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001365 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001366 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001367 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001368 * throw an exception that is handled by an encompassing "try"
1369 * block.
1370 *
1371 * We can also return, in which case there is no successor instruction
1372 * from this point.
1373 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001374 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001375 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001376 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1377 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001378 DecodedInstruction dec_insn(inst);
Ian Rogersa75a0132012-09-28 11:41:42 -07001379 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001380
jeffhaobdb76512011-09-07 11:43:16 -07001381 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001382 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001383 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001384 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001385 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1386 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001387 }
jeffhaobdb76512011-09-07 11:43:16 -07001388
1389 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001390 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001391 * can throw an exception, we will copy/merge this into the "catch"
1392 * address rather than work_line, because we don't want the result
1393 * from the "successful" code path (e.g. a check-cast that "improves"
1394 * a type) to be visible to the exception handler.
1395 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001396 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001397 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001398 } else {
1399#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001400 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001401#endif
1402 }
1403
Elliott Hughesadb8c672012-03-06 16:49:32 -08001404 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07001405 case Instruction::NOP:
1406 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001407 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001408 * a signature that looks like a NOP; if we see one of these in
1409 * the course of executing code then we have a problem.
1410 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001411 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001412 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001413 }
1414 break;
1415
1416 case Instruction::MOVE:
1417 case Instruction::MOVE_FROM16:
1418 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001419 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001420 break;
1421 case Instruction::MOVE_WIDE:
1422 case Instruction::MOVE_WIDE_FROM16:
1423 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001424 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07001425 break;
1426 case Instruction::MOVE_OBJECT:
1427 case Instruction::MOVE_OBJECT_FROM16:
1428 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001429 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001430 break;
1431
1432 /*
1433 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001434 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001435 * might want to hold the result in an actual CPU register, so the
1436 * Dalvik spec requires that these only appear immediately after an
1437 * invoke or filled-new-array.
1438 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001439 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001440 * redundant with the reset done below, but it can make the debug info
1441 * easier to read in some cases.)
1442 */
1443 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001444 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07001445 break;
1446 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001447 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001448 break;
1449 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001450 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07001451 break;
1452
Ian Rogersd81871c2011-10-03 13:57:23 -07001453 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001454 /*
jeffhao60f83e32012-02-13 17:16:30 -08001455 * This statement can only appear as the first instruction in an exception handler. We verify
1456 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001457 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001458 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08001459 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001460 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001461 }
jeffhaobdb76512011-09-07 11:43:16 -07001462 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001463 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1464 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001465 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001466 }
jeffhaobdb76512011-09-07 11:43:16 -07001467 }
1468 break;
1469 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001470 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001471 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001472 const RegType& return_type = GetMethodReturnType();
1473 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001474 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001475 } else {
1476 // Compilers may generate synthetic functions that write byte values into boolean fields.
1477 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001478 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001479 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1480 ((return_type.IsBoolean() || return_type.IsByte() ||
1481 return_type.IsShort() || return_type.IsChar()) &&
1482 src_type.IsInteger()));
1483 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001484 bool success =
1485 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
1486 if (!success) {
1487 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001488 }
jeffhaobdb76512011-09-07 11:43:16 -07001489 }
1490 }
1491 break;
1492 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001493 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001494 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001495 const RegType& return_type = GetMethodReturnType();
1496 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001497 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001498 } else {
1499 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001500 bool success = work_line_->VerifyRegisterType(dec_insn.vA, return_type);
1501 if (!success) {
1502 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001503 }
jeffhaobdb76512011-09-07 11:43:16 -07001504 }
1505 }
1506 break;
1507 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001508 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001509 const RegType& return_type = GetMethodReturnType();
1510 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001511 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001512 } else {
1513 /* return_type is the *expected* return type, not register value */
1514 DCHECK(!return_type.IsZero());
1515 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08001516 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07001517 // Disallow returning uninitialized values and verify that the reference in vAA is an
1518 // instance of the "return_type"
1519 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001520 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001521 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001522 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1523 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001524 }
1525 }
1526 }
1527 break;
1528
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001529 /* could be boolean, int, float, or a null reference */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001530 case Instruction::CONST_4:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001531 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001532 reg_types_.FromCat1Const(static_cast<int32_t>(dec_insn.vB << 28) >> 28, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001533 break;
jeffhaobdb76512011-09-07 11:43:16 -07001534 case Instruction::CONST_16:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001535 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001536 reg_types_.FromCat1Const(static_cast<int16_t>(dec_insn.vB), true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001537 break;
jeffhaobdb76512011-09-07 11:43:16 -07001538 case Instruction::CONST:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001539 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const(dec_insn.vB, true));
jeffhaobdb76512011-09-07 11:43:16 -07001540 break;
1541 case Instruction::CONST_HIGH16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001542 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001543 reg_types_.FromCat1Const(dec_insn.vB << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001544 break;
jeffhaobdb76512011-09-07 11:43:16 -07001545 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001546 case Instruction::CONST_WIDE_16: {
1547 int64_t val = static_cast<int16_t>(dec_insn.vB);
1548 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1549 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1550 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001551 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001552 }
1553 case Instruction::CONST_WIDE_32: {
1554 int64_t val = static_cast<int32_t>(dec_insn.vB);
1555 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1556 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1557 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1558 break;
1559 }
1560 case Instruction::CONST_WIDE: {
1561 int64_t val = dec_insn.vB_wide;
1562 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1563 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1564 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1565 break;
1566 }
1567 case Instruction::CONST_WIDE_HIGH16: {
1568 int64_t val = static_cast<uint64_t>(dec_insn.vB) << 48;
1569 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1570 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1571 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1572 break;
1573 }
jeffhaobdb76512011-09-07 11:43:16 -07001574 case Instruction::CONST_STRING:
1575 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001576 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001577 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001578 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001579 // Get type from instruction if unresolved then we need an access check
1580 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08001581 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001582 // Register holds class, ie its type is class, on error it will hold Conflict.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001583 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogersb4903572012-10-11 11:52:56 -07001584 res_type.IsConflict() ? res_type
1585 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001586 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001587 }
jeffhaobdb76512011-09-07 11:43:16 -07001588 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001589 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001590 break;
1591 case Instruction::MONITOR_EXIT:
1592 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001593 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001594 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001595 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001596 * to the need to handle asynchronous exceptions, a now-deprecated
1597 * feature that Dalvik doesn't support.)
1598 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001599 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001600 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001601 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001602 * structured locking checks are working, the former would have
1603 * failed on the -enter instruction, and the latter is impossible.
1604 *
1605 * This is fortunate, because issue 3221411 prevents us from
1606 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001607 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001608 * some catch blocks (which will show up as "dead" code when
1609 * we skip them here); if we can't, then the code path could be
1610 * "live" so we still need to check it.
1611 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001612 opcode_flags &= ~Instruction::kThrow;
1613 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001614 break;
1615
Ian Rogers28ad40d2011-10-27 15:19:26 -07001616 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001617 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001618 /*
1619 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1620 * could be a "upcast" -- not expected, so we don't try to address it.)
1621 *
1622 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001623 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001624 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001625 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001626 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001627 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001628 if (res_type.IsConflict()) {
1629 DCHECK_NE(failures_.size(), 0U);
1630 if (!is_checkcast) {
1631 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
1632 }
1633 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001634 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001635 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1636 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001637 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001638 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001639 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001640 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001641 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07001642 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001643 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001644 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001645 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001646 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001647 }
jeffhaobdb76512011-09-07 11:43:16 -07001648 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001649 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001650 }
1651 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001652 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001653 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001654 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001655 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001656 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001657 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001658 }
1659 }
1660 break;
1661 }
1662 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001663 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001664 if (res_type.IsConflict()) {
1665 DCHECK_NE(failures_.size(), 0U);
1666 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001667 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001668 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1669 // can't create an instance of an interface or abstract class */
1670 if (!res_type.IsInstantiableTypes()) {
1671 Fail(VERIFY_ERROR_INSTANTIATION)
1672 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001673 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001674 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001675 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1676 // Any registers holding previous allocations from this address that have not yet been
1677 // initialized must be marked invalid.
1678 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1679 // add the new uninitialized reference to the register state
1680 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001681 break;
1682 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001683 case Instruction::NEW_ARRAY:
1684 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001685 break;
1686 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08001687 VerifyNewArray(dec_insn, true, false);
1688 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001689 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001690 case Instruction::FILLED_NEW_ARRAY_RANGE:
1691 VerifyNewArray(dec_insn, true, true);
1692 just_set_result = true; // Filled new array range sets result register
1693 break;
jeffhaobdb76512011-09-07 11:43:16 -07001694 case Instruction::CMPL_FLOAT:
1695 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001696 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001697 break;
1698 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001699 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001700 break;
1701 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001702 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001703 break;
1704 case Instruction::CMPL_DOUBLE:
1705 case Instruction::CMPG_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001706 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vB, reg_types_.DoubleLo(),
1707 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001708 break;
1709 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001710 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vC, reg_types_.DoubleLo(),
1711 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001712 break;
1713 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001714 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001715 break;
1716 case Instruction::CMP_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001717 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vB, reg_types_.LongLo(),
1718 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001719 break;
1720 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001721 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vC, reg_types_.LongLo(),
1722 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001723 break;
1724 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001725 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001726 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001727 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001728 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersb4903572012-10-11 11:52:56 -07001729 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001730 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001731 }
1732 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001733 }
jeffhaobdb76512011-09-07 11:43:16 -07001734 case Instruction::GOTO:
1735 case Instruction::GOTO_16:
1736 case Instruction::GOTO_32:
1737 /* no effect on or use of registers */
1738 break;
1739
1740 case Instruction::PACKED_SWITCH:
1741 case Instruction::SPARSE_SWITCH:
1742 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001743 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001744 break;
1745
Ian Rogersd81871c2011-10-03 13:57:23 -07001746 case Instruction::FILL_ARRAY_DATA: {
1747 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001748 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08001749 /* array_type can be null if the reg type is Zero */
1750 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001751 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001752 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001753 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001754 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1755 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001756 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001757 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1758 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001759 } else {
jeffhao457cc512012-02-02 16:55:13 -08001760 // Now verify if the element width in the table matches the element width declared in
1761 // the array
1762 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1763 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001764 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001765 } else {
1766 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1767 // Since we don't compress the data in Dex, expect to see equal width of data stored
1768 // in the table and expected from the array class.
1769 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001770 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1771 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001772 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001773 }
1774 }
jeffhaobdb76512011-09-07 11:43:16 -07001775 }
1776 }
1777 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001778 }
jeffhaobdb76512011-09-07 11:43:16 -07001779 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001780 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001781 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1782 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001783 bool mismatch = false;
1784 if (reg_type1.IsZero()) { // zero then integral or reference expected
1785 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1786 } else if (reg_type1.IsReferenceTypes()) { // both references?
1787 mismatch = !reg_type2.IsReferenceTypes();
1788 } else { // both integral?
1789 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1790 }
1791 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001792 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1793 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001794 }
1795 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001796 }
jeffhaobdb76512011-09-07 11:43:16 -07001797 case Instruction::IF_LT:
1798 case Instruction::IF_GE:
1799 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001800 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001801 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1802 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001803 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001804 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1805 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001806 }
1807 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001808 }
jeffhaobdb76512011-09-07 11:43:16 -07001809 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001810 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001811 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001812 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001813 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001814 }
jeffhaobdb76512011-09-07 11:43:16 -07001815 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001816 }
jeffhaobdb76512011-09-07 11:43:16 -07001817 case Instruction::IF_LTZ:
1818 case Instruction::IF_GEZ:
1819 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001820 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001821 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001822 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001823 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1824 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001825 }
jeffhaobdb76512011-09-07 11:43:16 -07001826 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001827 }
jeffhaobdb76512011-09-07 11:43:16 -07001828 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001829 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
1830 break;
jeffhaobdb76512011-09-07 11:43:16 -07001831 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001832 VerifyAGet(dec_insn, reg_types_.Byte(), true);
1833 break;
jeffhaobdb76512011-09-07 11:43:16 -07001834 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07001835 VerifyAGet(dec_insn, reg_types_.Char(), true);
1836 break;
jeffhaobdb76512011-09-07 11:43:16 -07001837 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001838 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001839 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001840 case Instruction::AGET:
1841 VerifyAGet(dec_insn, reg_types_.Integer(), true);
1842 break;
jeffhaobdb76512011-09-07 11:43:16 -07001843 case Instruction::AGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001844 VerifyAGet(dec_insn, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001845 break;
1846 case Instruction::AGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001847 VerifyAGet(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001848 break;
1849
Ian Rogersd81871c2011-10-03 13:57:23 -07001850 case Instruction::APUT_BOOLEAN:
1851 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
1852 break;
1853 case Instruction::APUT_BYTE:
1854 VerifyAPut(dec_insn, reg_types_.Byte(), true);
1855 break;
1856 case Instruction::APUT_CHAR:
1857 VerifyAPut(dec_insn, reg_types_.Char(), true);
1858 break;
1859 case Instruction::APUT_SHORT:
1860 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001861 break;
1862 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001863 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001864 break;
1865 case Instruction::APUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001866 VerifyAPut(dec_insn, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001867 break;
1868 case Instruction::APUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001869 VerifyAPut(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001870 break;
1871
jeffhaobdb76512011-09-07 11:43:16 -07001872 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001873 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001874 break;
jeffhaobdb76512011-09-07 11:43:16 -07001875 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001876 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001877 break;
jeffhaobdb76512011-09-07 11:43:16 -07001878 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001879 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001880 break;
jeffhaobdb76512011-09-07 11:43:16 -07001881 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001882 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001883 break;
1884 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001885 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001886 break;
1887 case Instruction::IGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001888 VerifyISGet(dec_insn, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001889 break;
1890 case Instruction::IGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001891 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001892 break;
jeffhaobdb76512011-09-07 11:43:16 -07001893
Ian Rogersd81871c2011-10-03 13:57:23 -07001894 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001895 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001896 break;
1897 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001898 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001899 break;
1900 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001901 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001902 break;
1903 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001904 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001905 break;
1906 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001907 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001908 break;
1909 case Instruction::IPUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001910 VerifyISPut(dec_insn, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001911 break;
jeffhaobdb76512011-09-07 11:43:16 -07001912 case Instruction::IPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001913 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001914 break;
1915
jeffhaobdb76512011-09-07 11:43:16 -07001916 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001917 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001918 break;
jeffhaobdb76512011-09-07 11:43:16 -07001919 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001920 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001921 break;
jeffhaobdb76512011-09-07 11:43:16 -07001922 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001923 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001924 break;
jeffhaobdb76512011-09-07 11:43:16 -07001925 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001926 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001927 break;
1928 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001929 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001930 break;
1931 case Instruction::SGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001932 VerifyISGet(dec_insn, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001933 break;
1934 case Instruction::SGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001935 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001936 break;
1937
1938 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001939 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001940 break;
1941 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001942 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001943 break;
1944 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001945 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001946 break;
1947 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001948 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001949 break;
1950 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001951 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001952 break;
1953 case Instruction::SPUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001954 VerifyISPut(dec_insn, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001955 break;
1956 case Instruction::SPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001957 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001958 break;
1959
1960 case Instruction::INVOKE_VIRTUAL:
1961 case Instruction::INVOKE_VIRTUAL_RANGE:
1962 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001963 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001964 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
1965 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
1966 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
1967 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartier66f19252012-09-18 08:57:04 -07001968 AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001969 const char* descriptor;
1970 if (called_method == NULL) {
1971 uint32_t method_idx = dec_insn.vB;
1972 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1973 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1974 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1975 } else {
1976 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07001977 }
Ian Rogersb4903572012-10-11 11:52:56 -07001978 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001979 if (!return_type.IsLowHalf()) {
1980 work_line_->SetResultRegisterType(return_type);
1981 } else {
1982 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
1983 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001984 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001985 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001986 }
jeffhaobdb76512011-09-07 11:43:16 -07001987 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001988 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001989 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartier66f19252012-09-18 08:57:04 -07001990 AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07001991 const char* return_type_descriptor;
1992 bool is_constructor;
1993 if (called_method == NULL) {
1994 uint32_t method_idx = dec_insn.vB;
1995 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1996 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
1997 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1998 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1999 } else {
2000 is_constructor = called_method->IsConstructor();
2001 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2002 }
2003 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002004 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002005 * Some additional checks when calling a constructor. We know from the invocation arg check
2006 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2007 * that to require that called_method->klass is the same as this->klass or this->super,
2008 * allowing the latter only if the "this" argument is the same as the "this" argument to
2009 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002010 */
jeffhaob57e9522012-04-26 18:08:21 -07002011 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2012 if (this_type.IsConflict()) // failure.
2013 break;
jeffhaobdb76512011-09-07 11:43:16 -07002014
jeffhaob57e9522012-04-26 18:08:21 -07002015 /* no null refs allowed (?) */
2016 if (this_type.IsZero()) {
2017 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2018 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002019 }
jeffhaob57e9522012-04-26 18:08:21 -07002020
2021 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002022 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2023 // TODO: re-enable constructor type verification
2024 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002025 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002026 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2027 // break;
2028 // }
jeffhaob57e9522012-04-26 18:08:21 -07002029
2030 /* arg must be an uninitialized reference */
2031 if (!this_type.IsUninitializedTypes()) {
2032 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2033 << this_type;
2034 break;
2035 }
2036
2037 /*
2038 * Replace the uninitialized reference with an initialized one. We need to do this for all
2039 * registers that have the same object instance in them, not just the "this" register.
2040 */
2041 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002042 }
Ian Rogersb4903572012-10-11 11:52:56 -07002043 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2044 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002045 if (!return_type.IsLowHalf()) {
2046 work_line_->SetResultRegisterType(return_type);
2047 } else {
2048 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2049 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002050 just_set_result = true;
2051 break;
2052 }
2053 case Instruction::INVOKE_STATIC:
2054 case Instruction::INVOKE_STATIC_RANGE: {
2055 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartier66f19252012-09-18 08:57:04 -07002056 AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002057 const char* descriptor;
2058 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002059 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002060 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2061 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002062 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002063 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002064 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002065 }
Ian Rogersb4903572012-10-11 11:52:56 -07002066 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002067 if (!return_type.IsLowHalf()) {
2068 work_line_->SetResultRegisterType(return_type);
2069 } else {
2070 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2071 }
jeffhaobdb76512011-09-07 11:43:16 -07002072 just_set_result = true;
2073 }
2074 break;
jeffhaobdb76512011-09-07 11:43:16 -07002075 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002076 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002077 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartier66f19252012-09-18 08:57:04 -07002078 AbstractMethod* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002079 if (abs_method != NULL) {
2080 Class* called_interface = abs_method->GetDeclaringClass();
2081 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2082 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2083 << PrettyMethod(abs_method) << "'";
2084 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002085 }
Ian Rogers0d604842012-04-16 14:50:24 -07002086 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002087 /* Get the type of the "this" arg, which should either be a sub-interface of called
2088 * interface or Object (see comments in RegType::JoinClass).
2089 */
2090 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2091 if (this_type.IsZero()) {
2092 /* null pointer always passes (and always fails at runtime) */
2093 } else {
2094 if (this_type.IsUninitializedTypes()) {
2095 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2096 << this_type;
2097 break;
2098 }
2099 // In the past we have tried to assert that "called_interface" is assignable
2100 // from "this_type.GetClass()", however, as we do an imprecise Join
2101 // (RegType::JoinClass) we don't have full information on what interfaces are
2102 // implemented by "this_type". For example, two classes may implement the same
2103 // interfaces and have a common parent that doesn't implement the interface. The
2104 // join will set "this_type" to the parent class and a test that this implements
2105 // the interface will incorrectly fail.
2106 }
2107 /*
2108 * We don't have an object instance, so we can't find the concrete method. However, all of
2109 * the type information is in the abstract method, so we're good.
2110 */
2111 const char* descriptor;
2112 if (abs_method == NULL) {
2113 uint32_t method_idx = dec_insn.vB;
2114 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2115 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2116 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2117 } else {
2118 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2119 }
Ian Rogersb4903572012-10-11 11:52:56 -07002120 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002121 if (!return_type.IsLowHalf()) {
2122 work_line_->SetResultRegisterType(return_type);
2123 } else {
2124 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2125 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002126 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002127 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002128 }
jeffhaobdb76512011-09-07 11:43:16 -07002129 case Instruction::NEG_INT:
2130 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002131 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002132 break;
2133 case Instruction::NEG_LONG:
2134 case Instruction::NOT_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002135 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2136 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002137 break;
2138 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002139 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002140 break;
2141 case Instruction::NEG_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002142 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2143 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002144 break;
2145 case Instruction::INT_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002146 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2147 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002148 break;
2149 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002150 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002151 break;
2152 case Instruction::INT_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002153 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2154 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002155 break;
2156 case Instruction::LONG_TO_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002157 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Integer(),
2158 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002159 break;
2160 case Instruction::LONG_TO_FLOAT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002161 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Float(),
2162 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002163 break;
2164 case Instruction::LONG_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002165 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2166 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002167 break;
2168 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002169 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002170 break;
2171 case Instruction::FLOAT_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002172 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2173 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002174 break;
2175 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002176 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2177 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002178 break;
2179 case Instruction::DOUBLE_TO_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002180 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Integer(),
2181 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002182 break;
2183 case Instruction::DOUBLE_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002184 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2185 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002186 break;
2187 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002188 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Float(),
2189 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002190 break;
2191 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002192 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002193 break;
2194 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002195 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002196 break;
2197 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002198 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002199 break;
2200
2201 case Instruction::ADD_INT:
2202 case Instruction::SUB_INT:
2203 case Instruction::MUL_INT:
2204 case Instruction::REM_INT:
2205 case Instruction::DIV_INT:
2206 case Instruction::SHL_INT:
2207 case Instruction::SHR_INT:
2208 case Instruction::USHR_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002209 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(),
2210 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002211 break;
2212 case Instruction::AND_INT:
2213 case Instruction::OR_INT:
2214 case Instruction::XOR_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002215 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(),
2216 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002217 break;
2218 case Instruction::ADD_LONG:
2219 case Instruction::SUB_LONG:
2220 case Instruction::MUL_LONG:
2221 case Instruction::DIV_LONG:
2222 case Instruction::REM_LONG:
2223 case Instruction::AND_LONG:
2224 case Instruction::OR_LONG:
2225 case Instruction::XOR_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002226 work_line_->CheckBinaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2227 reg_types_.LongLo(), reg_types_.LongHi(),
2228 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002229 break;
2230 case Instruction::SHL_LONG:
2231 case Instruction::SHR_LONG:
2232 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002233 /* shift distance is Int, making these different from other binary operations */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002234 work_line_->CheckBinaryOpWideShift(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2235 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002236 break;
2237 case Instruction::ADD_FLOAT:
2238 case Instruction::SUB_FLOAT:
2239 case Instruction::MUL_FLOAT:
2240 case Instruction::DIV_FLOAT:
2241 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002242 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002243 break;
2244 case Instruction::ADD_DOUBLE:
2245 case Instruction::SUB_DOUBLE:
2246 case Instruction::MUL_DOUBLE:
2247 case Instruction::DIV_DOUBLE:
2248 case Instruction::REM_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002249 work_line_->CheckBinaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2250 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2251 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002252 break;
2253 case Instruction::ADD_INT_2ADDR:
2254 case Instruction::SUB_INT_2ADDR:
2255 case Instruction::MUL_INT_2ADDR:
2256 case Instruction::REM_INT_2ADDR:
2257 case Instruction::SHL_INT_2ADDR:
2258 case Instruction::SHR_INT_2ADDR:
2259 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002260 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002261 break;
2262 case Instruction::AND_INT_2ADDR:
2263 case Instruction::OR_INT_2ADDR:
2264 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002265 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002266 break;
2267 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002268 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002269 break;
2270 case Instruction::ADD_LONG_2ADDR:
2271 case Instruction::SUB_LONG_2ADDR:
2272 case Instruction::MUL_LONG_2ADDR:
2273 case Instruction::DIV_LONG_2ADDR:
2274 case Instruction::REM_LONG_2ADDR:
2275 case Instruction::AND_LONG_2ADDR:
2276 case Instruction::OR_LONG_2ADDR:
2277 case Instruction::XOR_LONG_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002278 work_line_->CheckBinaryOp2addrWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2279 reg_types_.LongLo(), reg_types_.LongHi(),
2280 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002281 break;
2282 case Instruction::SHL_LONG_2ADDR:
2283 case Instruction::SHR_LONG_2ADDR:
2284 case Instruction::USHR_LONG_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002285 work_line_->CheckBinaryOp2addrWideShift(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2286 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002287 break;
2288 case Instruction::ADD_FLOAT_2ADDR:
2289 case Instruction::SUB_FLOAT_2ADDR:
2290 case Instruction::MUL_FLOAT_2ADDR:
2291 case Instruction::DIV_FLOAT_2ADDR:
2292 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002293 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002294 break;
2295 case Instruction::ADD_DOUBLE_2ADDR:
2296 case Instruction::SUB_DOUBLE_2ADDR:
2297 case Instruction::MUL_DOUBLE_2ADDR:
2298 case Instruction::DIV_DOUBLE_2ADDR:
2299 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002300 work_line_->CheckBinaryOp2addrWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2301 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2302 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002303 break;
2304 case Instruction::ADD_INT_LIT16:
2305 case Instruction::RSUB_INT:
2306 case Instruction::MUL_INT_LIT16:
2307 case Instruction::DIV_INT_LIT16:
2308 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002309 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002310 break;
2311 case Instruction::AND_INT_LIT16:
2312 case Instruction::OR_INT_LIT16:
2313 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002314 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002315 break;
2316 case Instruction::ADD_INT_LIT8:
2317 case Instruction::RSUB_INT_LIT8:
2318 case Instruction::MUL_INT_LIT8:
2319 case Instruction::DIV_INT_LIT8:
2320 case Instruction::REM_INT_LIT8:
2321 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002322 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002323 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002324 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002325 break;
2326 case Instruction::AND_INT_LIT8:
2327 case Instruction::OR_INT_LIT8:
2328 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002329 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002330 break;
2331
Ian Rogersd81871c2011-10-03 13:57:23 -07002332 /* These should never appear during verification. */
jeffhao9a4f0032012-08-30 16:17:40 -07002333 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002334 case Instruction::UNUSED_EE:
2335 case Instruction::UNUSED_EF:
2336 case Instruction::UNUSED_F2:
2337 case Instruction::UNUSED_F3:
2338 case Instruction::UNUSED_F4:
2339 case Instruction::UNUSED_F5:
2340 case Instruction::UNUSED_F6:
2341 case Instruction::UNUSED_F7:
2342 case Instruction::UNUSED_F8:
2343 case Instruction::UNUSED_F9:
2344 case Instruction::UNUSED_FA:
2345 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002346 case Instruction::UNUSED_F0:
2347 case Instruction::UNUSED_F1:
2348 case Instruction::UNUSED_E3:
2349 case Instruction::UNUSED_E8:
2350 case Instruction::UNUSED_E7:
2351 case Instruction::UNUSED_E4:
2352 case Instruction::UNUSED_E9:
2353 case Instruction::UNUSED_FC:
2354 case Instruction::UNUSED_E5:
2355 case Instruction::UNUSED_EA:
2356 case Instruction::UNUSED_FD:
2357 case Instruction::UNUSED_E6:
2358 case Instruction::UNUSED_EB:
2359 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002360 case Instruction::UNUSED_3E:
2361 case Instruction::UNUSED_3F:
2362 case Instruction::UNUSED_40:
2363 case Instruction::UNUSED_41:
2364 case Instruction::UNUSED_42:
2365 case Instruction::UNUSED_43:
2366 case Instruction::UNUSED_73:
2367 case Instruction::UNUSED_79:
2368 case Instruction::UNUSED_7A:
2369 case Instruction::UNUSED_EC:
2370 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002371 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002372 break;
2373
2374 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002375 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002376 * complain if an instruction is missing (which is desirable).
2377 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002378 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002379
Ian Rogersad0b3a32012-04-16 14:50:24 -07002380 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002381 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002382 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002383 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002384 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002385 /* immediate failure, reject class */
2386 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2387 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002388 } else if (have_pending_runtime_throw_failure_) {
2389 /* slow path will throw, mark following code as unreachable */
2390 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002391 }
jeffhaobdb76512011-09-07 11:43:16 -07002392 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002393 * If we didn't just set the result register, clear it out. This ensures that you can only use
2394 * "move-result" immediately after the result is set. (We could check this statically, but it's
2395 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002396 */
2397 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002398 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002399 }
2400
jeffhaoa0a764a2011-09-16 10:43:38 -07002401 /* Handle "continue". Tag the next consecutive instruction. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002402 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers776ac1f2012-04-13 23:36:36 -07002403 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
Ian Rogersd81871c2011-10-03 13:57:23 -07002404 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
jeffhaod5347e02012-03-22 17:25:05 -07002405 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002406 return false;
2407 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002408 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2409 // next instruction isn't one.
jeffhaod5347e02012-03-22 17:25:05 -07002410 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002411 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002412 }
2413 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2414 if (next_line != NULL) {
2415 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2416 // needed.
2417 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002418 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002419 }
jeffhaobdb76512011-09-07 11:43:16 -07002420 } else {
2421 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002422 * We're not recording register data for the next instruction, so we don't know what the prior
2423 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002424 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002425 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002426 }
2427 }
2428
2429 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002430 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002431 *
2432 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002433 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002434 * somebody could get a reference field, check it for zero, and if the
2435 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002436 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002437 * that, and will reject the code.
2438 *
2439 * TODO: avoid re-fetching the branch target
2440 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002441 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002442 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002443 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002444 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002445 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002446 return false;
2447 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002448 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002449 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002450 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002451 }
jeffhaobdb76512011-09-07 11:43:16 -07002452 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002453 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002454 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002455 }
jeffhaobdb76512011-09-07 11:43:16 -07002456 }
2457
2458 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002459 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002460 *
2461 * We've already verified that the table is structurally sound, so we
2462 * just need to walk through and tag the targets.
2463 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002464 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002465 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2466 const uint16_t* switch_insns = insns + offset_to_switch;
2467 int switch_count = switch_insns[1];
2468 int offset_to_targets, targ;
2469
2470 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2471 /* 0 = sig, 1 = count, 2/3 = first key */
2472 offset_to_targets = 4;
2473 } else {
2474 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002475 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002476 offset_to_targets = 2 + 2 * switch_count;
2477 }
2478
2479 /* verify each switch target */
2480 for (targ = 0; targ < switch_count; targ++) {
2481 int offset;
2482 uint32_t abs_offset;
2483
2484 /* offsets are 32-bit, and only partly endian-swapped */
2485 offset = switch_insns[offset_to_targets + targ * 2] |
2486 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002487 abs_offset = work_insn_idx_ + offset;
2488 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002489 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002490 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002491 }
2492 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002493 return false;
2494 }
2495 }
2496
2497 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002498 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2499 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002500 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002501 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002502 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002503 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002504
Ian Rogers0571d352011-11-03 19:51:38 -07002505 for (; iterator.HasNext(); iterator.Next()) {
2506 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002507 within_catch_all = true;
2508 }
jeffhaobdb76512011-09-07 11:43:16 -07002509 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002510 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2511 * "work_regs", because at runtime the exception will be thrown before the instruction
2512 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002513 */
Ian Rogers0571d352011-11-03 19:51:38 -07002514 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002515 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002516 }
jeffhaobdb76512011-09-07 11:43:16 -07002517 }
2518
2519 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002520 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2521 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002522 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002523 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002524 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002525 * The state in work_line reflects the post-execution state. If the current instruction is a
2526 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002527 * it will do so before grabbing the lock).
2528 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002529 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002530 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002531 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002532 return false;
2533 }
2534 }
2535 }
2536
jeffhaod1f0fde2011-09-08 17:25:33 -07002537 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002538 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002539 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002540 return false;
2541 }
jeffhaobdb76512011-09-07 11:43:16 -07002542 }
2543
2544 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002545 * Update start_guess. Advance to the next instruction of that's
2546 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002547 * neither of those exists we're in a return or throw; leave start_guess
2548 * alone and let the caller sort it out.
2549 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002550 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002551 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002552 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002553 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002554 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002555 }
2556
Ian Rogersd81871c2011-10-03 13:57:23 -07002557 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2558 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002559
2560 return true;
2561}
2562
Ian Rogers776ac1f2012-04-13 23:36:36 -07002563const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002564 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002565 const RegType& referrer = GetDeclaringClass();
2566 Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002567 const RegType& result =
Ian Rogersb4903572012-10-11 11:52:56 -07002568 klass != NULL ? reg_types_.FromClass(klass, klass->IsFinal())
2569 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002570 if (result.IsConflict()) {
2571 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2572 << "' in " << referrer;
2573 return result;
2574 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002575 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002576 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002577 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002578 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002579 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002580 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002581 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002582 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002583 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002584 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002585}
2586
Ian Rogers776ac1f2012-04-13 23:36:36 -07002587const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002588 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002589 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002590 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002591 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2592 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002593 CatchHandlerIterator iterator(handlers_ptr);
2594 for (; iterator.HasNext(); iterator.Next()) {
2595 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2596 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002597 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002598 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002599 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002600 if (common_super == NULL) {
2601 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2602 // as that is caught at runtime
2603 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002604 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002605 // We don't know enough about the type and the common path merge will result in
2606 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002607 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002608 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002609 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002610 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002611 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002612 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002613 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002614 }
2615 }
2616 }
2617 }
Ian Rogers0571d352011-11-03 19:51:38 -07002618 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002619 }
2620 }
2621 if (common_super == NULL) {
2622 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002623 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002624 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002625 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002626 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002627}
2628
Mathieu Chartier66f19252012-09-18 08:57:04 -07002629AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002630 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002631 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002632 if (klass_type.IsConflict()) {
2633 std::string append(" in attempt to access method ");
2634 append += dex_file_->GetMethodName(method_id);
2635 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002636 return NULL;
2637 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002638 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002639 return NULL; // Can't resolve Class so no more to do here
2640 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002641 Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002642 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier66f19252012-09-18 08:57:04 -07002643 AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002644 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002645 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002646 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002647
2648 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002649 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002650 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002651 res_method = klass->FindInterfaceMethod(name, signature);
2652 } else {
2653 res_method = klass->FindVirtualMethod(name, signature);
2654 }
2655 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002656 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002657 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002658 // If a virtual or interface method wasn't found with the expected type, look in
2659 // the direct methods. This can happen when the wrong invoke type is used or when
2660 // a class has changed, and will be flagged as an error in later checks.
2661 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2662 res_method = klass->FindDirectMethod(name, signature);
2663 }
2664 if (res_method == NULL) {
2665 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2666 << PrettyDescriptor(klass) << "." << name
2667 << " " << signature;
2668 return NULL;
2669 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002670 }
2671 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002672 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2673 // enforce them here.
2674 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002675 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2676 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002677 return NULL;
2678 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002679 // Disallow any calls to class initializers.
2680 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002681 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2682 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002683 return NULL;
2684 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002685 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002686 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002687 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002688 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002689 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002690 }
jeffhaode0d9c92012-02-27 13:58:13 -08002691 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2692 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002693 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2694 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002695 return NULL;
2696 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002697 // Check that interface methods match interface classes.
2698 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2699 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2700 << " is in an interface class " << PrettyClass(klass);
2701 return NULL;
2702 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2703 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2704 << " is in a non-interface class " << PrettyClass(klass);
2705 return NULL;
2706 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002707 // See if the method type implied by the invoke instruction matches the access flags for the
2708 // target method.
2709 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2710 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2711 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2712 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002713 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2714 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002715 return NULL;
2716 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002717 return res_method;
2718}
2719
Mathieu Chartier66f19252012-09-18 08:57:04 -07002720AbstractMethod* MethodVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
Ian Rogers46685432012-06-03 22:26:43 -07002721 MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002722 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2723 // we're making.
Mathieu Chartier66f19252012-09-18 08:57:04 -07002724 AbstractMethod* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002725 if (res_method == NULL) { // error or class is unresolved
2726 return NULL;
2727 }
2728
Ian Rogersd81871c2011-10-03 13:57:23 -07002729 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2730 // has a vtable entry for the target method.
2731 if (is_super) {
2732 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002733 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002734 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002735 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002736 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002737 << " to super " << PrettyMethod(res_method);
2738 return NULL;
2739 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002740 Class* super_klass = super.GetClass();
2741 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002742 MethodHelper mh(res_method);
2743 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002744 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002745 << " to super " << super
2746 << "." << mh.GetName()
2747 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002748 return NULL;
2749 }
2750 }
2751 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2752 // match the call to the signature. Also, we might might be calling through an abstract method
2753 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08002754 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002755 /* caught by static verifier */
2756 DCHECK(is_range || expected_args <= 5);
2757 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002758 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002759 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2760 return NULL;
2761 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002762
jeffhaobdb76512011-09-07 11:43:16 -07002763 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002764 * Check the "this" argument, which must be an instance of the class that declared the method.
2765 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2766 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002767 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002768 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002769 if (!res_method->IsStatic()) {
2770 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002771 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002772 return NULL;
2773 }
2774 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002775 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002776 return NULL;
2777 }
2778 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersb4903572012-10-11 11:52:56 -07002779 Class* klass = res_method->GetDeclaringClass();
2780 const RegType& res_method_class = reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogers9074b992011-10-26 17:41:55 -07002781 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002782 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002783 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002784 return NULL;
2785 }
2786 }
2787 actual_args++;
2788 }
2789 /*
2790 * Process the target method's signature. This signature may or may not
2791 * have been verified, so we can't assume it's properly formed.
2792 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002793 MethodHelper mh(res_method);
2794 const DexFile::TypeList* params = mh.GetParameterTypeList();
2795 size_t params_size = params == NULL ? 0 : params->Size();
2796 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002797 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002799 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2800 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002801 return NULL;
2802 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002803 const char* descriptor =
2804 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2805 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002806 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002807 << " missing signature component";
2808 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002809 }
Ian Rogersb4903572012-10-11 11:52:56 -07002810 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002811 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002812 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002813 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002814 }
2815 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2816 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002818 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002819 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002820 return NULL;
2821 } else {
2822 return res_method;
2823 }
2824}
2825
Ian Rogers776ac1f2012-04-13 23:36:36 -07002826void MethodVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08002827 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002828 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002829 if (res_type.IsConflict()) { // bad class
2830 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002831 } else {
2832 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2833 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002834 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002835 } else if (!is_filled) {
2836 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002837 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002838 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002839 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002840 } else {
2841 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2842 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002843 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002844 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002845 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002846 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002847 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002848 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002849 return;
2850 }
2851 }
2852 // filled-array result goes into "result" register
2853 work_line_->SetResultRegisterType(res_type);
2854 }
2855 }
2856}
2857
Ian Rogers776ac1f2012-04-13 23:36:36 -07002858void MethodVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002859 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002860 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002861 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002862 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002863 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002864 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002865 if (array_type.IsZero()) {
2866 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2867 // instruction type. TODO: have a proper notion of bottom here.
2868 if (!is_primitive || insn_type.IsCategory1Types()) {
2869 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08002870 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002871 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002872 // Category 2
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002873 work_line_->SetRegisterTypeWide(dec_insn.vA, reg_types_.FromCat2ConstLo(0, false),
2874 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08002875 }
jeffhaofc3144e2012-02-01 17:21:15 -08002876 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002877 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002878 } else {
2879 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002880 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002881 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002882 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002883 << " source for aget-object";
2884 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002885 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002886 << " source for category 1 aget";
2887 } else if (is_primitive && !insn_type.Equals(component_type) &&
2888 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002889 (insn_type.IsLong() && component_type.IsDouble()))) {
2890 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
2891 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002892 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002893 // Use knowledge of the field type which is stronger than the type inferred from the
2894 // instruction, which can't differentiate object types and ints from floats, longs from
2895 // doubles.
2896 if (!component_type.IsLowHalf()) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002897 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002898 } else {
2899 work_line_->SetRegisterTypeWide(dec_insn.vA, component_type,
2900 component_type.HighHalf(&reg_types_));
2901 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002902 }
2903 }
2904 }
2905}
2906
Ian Rogers776ac1f2012-04-13 23:36:36 -07002907void MethodVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002908 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002909 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002910 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002911 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002912 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002913 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002914 if (array_type.IsZero()) {
2915 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2916 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002917 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002918 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002919 } else {
2920 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002921 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002922 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002923 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002924 << " source for aput-object";
2925 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002926 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002927 << " source for category 1 aput";
2928 } else if (is_primitive && !insn_type.Equals(component_type) &&
2929 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2930 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002931 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002932 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002933 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002934 // The instruction agrees with the type of array, confirm the value to be stored does too
2935 // Note: we use the instruction type (rather than the component type) for aput-object as
2936 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08002937 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002938 }
2939 }
2940 }
2941}
2942
Ian Rogers776ac1f2012-04-13 23:36:36 -07002943Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002944 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2945 // Check access to class
2946 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002947 if (klass_type.IsConflict()) { // bad class
2948 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
2949 field_idx, dex_file_->GetFieldName(field_id),
2950 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002951 return NULL;
2952 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002953 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002954 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08002955 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002956 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2957 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002958 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002959 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
2960 << dex_file_->GetFieldName(field_id) << ") in "
2961 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002962 DCHECK(Thread::Current()->IsExceptionPending());
2963 Thread::Current()->ClearException();
2964 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002965 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2966 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002967 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002968 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002969 return NULL;
2970 } else if (!field->IsStatic()) {
2971 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
2972 return NULL;
2973 } else {
2974 return field;
2975 }
2976}
2977
Ian Rogers776ac1f2012-04-13 23:36:36 -07002978Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002979 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2980 // Check access to class
2981 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002982 if (klass_type.IsConflict()) {
2983 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
2984 field_idx, dex_file_->GetFieldName(field_id),
2985 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002986 return NULL;
2987 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002988 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002989 return NULL; // Can't resolve Class so no more to do here
2990 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002991 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
2992 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002993 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002994 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
2995 << dex_file_->GetFieldName(field_id) << ") in "
2996 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002997 DCHECK(Thread::Current()->IsExceptionPending());
2998 Thread::Current()->ClearException();
2999 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003000 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3001 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003002 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003003 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003004 return NULL;
3005 } else if (field->IsStatic()) {
3006 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3007 << " to not be static";
3008 return NULL;
3009 } else if (obj_type.IsZero()) {
3010 // Cannot infer and check type, however, access will cause null pointer exception
3011 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003012 } else {
Ian Rogersb4903572012-10-11 11:52:56 -07003013 Class* klass = field->GetDeclaringClass();
3014 const RegType& field_klass = reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003015 if (obj_type.IsUninitializedTypes() &&
3016 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3017 !field_klass.Equals(GetDeclaringClass()))) {
3018 // Field accesses through uninitialized references are only allowable for constructors where
3019 // the field is declared in this class
3020 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
3021 << " of a not fully initialized object within the context of "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003022 << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003023 return NULL;
3024 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3025 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3026 // of C1. For resolution to occur the declared class of the field must be compatible with
3027 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3028 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3029 << " from object of type " << obj_type;
3030 return NULL;
3031 } else {
3032 return field;
3033 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003034 }
3035}
3036
Ian Rogers776ac1f2012-04-13 23:36:36 -07003037void MethodVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003038 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003039 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003040 Field* field;
3041 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003042 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003043 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003044 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003045 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003046 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003047 const char* descriptor;
Ian Rogers365c1022012-06-22 15:05:28 -07003048 ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003049 if (field != NULL) {
3050 descriptor = FieldHelper(field).GetTypeDescriptor();
3051 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003052 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003053 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3054 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3055 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07003056 }
Ian Rogersb4903572012-10-11 11:52:56 -07003057 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003058 if (is_primitive) {
3059 if (field_type.Equals(insn_type) ||
3060 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3061 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3062 // expected that read is of the correct primitive type or that int reads are reading
3063 // floats or long reads are reading doubles
3064 } else {
3065 // This is a global failure rather than a class change failure as the instructions and
3066 // the descriptors for the type should have been consistent within the same file at
3067 // compile time
3068 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3069 << " to be of type '" << insn_type
3070 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003071 return;
3072 }
3073 } else {
3074 if (!insn_type.IsAssignableFrom(field_type)) {
3075 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3076 << " to be compatible with type '" << insn_type
3077 << "' but found type '" << field_type
3078 << "' in get-object";
3079 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Conflict());
3080 return;
3081 }
3082 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003083 if (!field_type.IsLowHalf()) {
3084 work_line_->SetRegisterType(dec_insn.vA, field_type);
3085 } else {
3086 work_line_->SetRegisterTypeWide(dec_insn.vA, field_type, field_type.HighHalf(&reg_types_));
3087 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003088}
3089
Ian Rogers776ac1f2012-04-13 23:36:36 -07003090void MethodVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003091 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003092 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003093 Field* field;
3094 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003095 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003096 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003097 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07003098 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003099 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003100 const char* descriptor;
Ian Rogers365c1022012-06-22 15:05:28 -07003101 ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003102 if (field != NULL) {
3103 descriptor = FieldHelper(field).GetTypeDescriptor();
3104 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003105 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003106 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3107 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3108 loader = class_loader_;
3109 }
Ian Rogersb4903572012-10-11 11:52:56 -07003110 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003111 if (field != NULL) {
3112 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3113 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3114 << " from other class " << GetDeclaringClass();
3115 return;
3116 }
3117 }
3118 if (is_primitive) {
3119 // Primitive field assignability rules are weaker than regular assignability rules
3120 bool instruction_compatible;
3121 bool value_compatible;
3122 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
3123 if (field_type.IsIntegralTypes()) {
3124 instruction_compatible = insn_type.IsIntegralTypes();
3125 value_compatible = value_type.IsIntegralTypes();
3126 } else if (field_type.IsFloat()) {
3127 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3128 value_compatible = value_type.IsFloatTypes();
3129 } else if (field_type.IsLong()) {
3130 instruction_compatible = insn_type.IsLong();
3131 value_compatible = value_type.IsLongTypes();
3132 } else if (field_type.IsDouble()) {
3133 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3134 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003135 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003136 instruction_compatible = false; // reference field with primitive store
3137 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003138 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003139 if (!instruction_compatible) {
3140 // This is a global failure rather than a class change failure as the instructions and
3141 // the descriptors for the type should have been consistent within the same file at
3142 // compile time
3143 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3144 << " to be of type '" << insn_type
3145 << "' but found type '" << field_type
3146 << "' in put";
3147 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003148 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003149 if (!value_compatible) {
3150 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
3151 << " of type " << value_type
3152 << " but expected " << field_type
3153 << " for store to " << PrettyField(field) << " in put";
3154 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003155 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003156 } else {
3157 if (!insn_type.IsAssignableFrom(field_type)) {
3158 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3159 << " to be compatible with type '" << insn_type
3160 << "' but found type '" << field_type
3161 << "' in put-object";
3162 return;
3163 }
3164 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003165 }
3166}
3167
Ian Rogers776ac1f2012-04-13 23:36:36 -07003168bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003169 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003170 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003171 return false;
3172 }
3173 return true;
3174}
3175
Ian Rogers776ac1f2012-04-13 23:36:36 -07003176bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003177 bool changed = true;
3178 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3179 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003180 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003181 * We haven't processed this instruction before, and we haven't touched the registers here, so
3182 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3183 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003184 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003185 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003186 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003187 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3188 if (gDebugVerify) {
3189 copy->CopyFromLine(target_line);
3190 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003191 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003192 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003193 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003194 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003195 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003196 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003197 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3198 << *copy.get() << " MERGE\n"
3199 << *merge_line << " ==\n"
3200 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003201 }
3202 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003203 if (changed) {
3204 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003205 }
3206 return true;
3207}
3208
Ian Rogers776ac1f2012-04-13 23:36:36 -07003209InsnFlags* MethodVerifier::CurrentInsnFlags() {
3210 return &insn_flags_[work_insn_idx_];
3211}
3212
Ian Rogersad0b3a32012-04-16 14:50:24 -07003213const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003214 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003215 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3216 uint16_t return_type_idx = proto_id.return_type_idx_;
3217 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003218 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003219}
3220
3221const RegType& MethodVerifier::GetDeclaringClass() {
3222 if (foo_method_ != NULL) {
Ian Rogersb4903572012-10-11 11:52:56 -07003223 Class* klass = foo_method_->GetDeclaringClass();
3224 return reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003225 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003226 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003227 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogersb4903572012-10-11 11:52:56 -07003228 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003229 }
3230}
3231
Ian Rogers776ac1f2012-04-13 23:36:36 -07003232void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003233 size_t* log2_max_gc_pc) {
3234 size_t local_gc_points = 0;
3235 size_t max_insn = 0;
3236 size_t max_ref_reg = -1;
3237 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3238 if (insn_flags_[i].IsGcPoint()) {
3239 local_gc_points++;
3240 max_insn = i;
3241 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003242 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003243 }
3244 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003245 *gc_points = local_gc_points;
3246 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3247 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003248 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003249 i++;
3250 }
3251 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003252}
3253
Ian Rogers776ac1f2012-04-13 23:36:36 -07003254const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003255 size_t num_entries, ref_bitmap_bits, pc_bits;
3256 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3257 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003258 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003259 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003260 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003261 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003262 return NULL;
3263 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003264 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3265 // There are 2 bytes to encode the number of entries
3266 if (num_entries >= 65536) {
3267 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003268 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003269 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003270 return NULL;
3271 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003272 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003273 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003274 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003275 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003276 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003277 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003278 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003279 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003280 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003281 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003282 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003283 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3284 return NULL;
3285 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003286 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003287 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003288 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003289 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003290 return NULL;
3291 }
3292 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003293 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3294 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003295 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003296 table->push_back(num_entries & 0xFF);
3297 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003298 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003299 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3300 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003301 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003302 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003303 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003304 }
3305 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003306 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003307 }
3308 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003309 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003310 return table;
3311}
jeffhaoa0a764a2011-09-16 10:43:38 -07003312
Ian Rogers776ac1f2012-04-13 23:36:36 -07003313void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003314 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3315 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003316 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003317 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003318 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003319 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3320 if (insn_flags_[i].IsGcPoint()) {
3321 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003322 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003323 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3324 map_index++;
3325 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003326 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003327 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003328 CHECK_LT(j / 8, map.RegWidth());
3329 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3330 } else if ((j / 8) < map.RegWidth()) {
3331 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3332 } else {
3333 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3334 }
3335 }
3336 } else {
3337 CHECK(reg_bitmap == NULL);
3338 }
3339 }
3340}
jeffhaoa0a764a2011-09-16 10:43:38 -07003341
Ian Rogers0c7abda2012-09-19 13:33:42 -07003342void MethodVerifier::SetDexGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003343 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003344 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003345 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
3346 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003347 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003348 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003349 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003350 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003351 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003352 CHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003353}
3354
Ian Rogers0c7abda2012-09-19 13:33:42 -07003355const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(Compiler::MethodReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003356 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003357 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
3358 if (it == dex_gc_maps_->end()) {
Ian Rogers64b6d142012-10-29 16:34:15 -07003359 LOG(WARNING) << "Didn't find GC map for: " << PrettyMethod(ref.second, *ref.first);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003360 return NULL;
3361 }
3362 CHECK(it->second != NULL);
3363 return it->second;
3364}
3365
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003366std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3367 RegisterLine* line = reg_table_.GetLine(dex_pc);
3368 std::vector<int32_t> result;
3369 for (size_t i = 0; i < line->NumRegs(); ++i) {
3370 const RegType& type = line->GetRegisterType(i);
3371 if (type.IsConstant()) {
3372 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3373 result.push_back(type.ConstantValue());
3374 } else if (type.IsConstantLo()) {
3375 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3376 result.push_back(type.ConstantValueLo());
3377 } else if (type.IsConstantHi()) {
3378 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3379 result.push_back(type.ConstantValueHi());
3380 } else if (type.IsIntegralTypes()) {
3381 result.push_back(kIntVReg);
3382 result.push_back(0);
3383 } else if (type.IsFloat()) {
3384 result.push_back(kFloatVReg);
3385 result.push_back(0);
3386 } else if (type.IsLong()) {
3387 result.push_back(kLongLoVReg);
3388 result.push_back(0);
3389 result.push_back(kLongHiVReg);
3390 result.push_back(0);
3391 ++i;
3392 } else if (type.IsDouble()) {
3393 result.push_back(kDoubleLoVReg);
3394 result.push_back(0);
3395 result.push_back(kDoubleHiVReg);
3396 result.push_back(0);
3397 ++i;
3398 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
3399 result.push_back(kUndefined);
3400 result.push_back(0);
3401 } else {
3402 CHECK(type.IsNonZeroReferenceTypes()) << type;
3403 result.push_back(kReferenceVReg);
3404 result.push_back(0);
3405 }
3406 }
3407 return result;
3408}
3409
Ian Rogers0c7abda2012-09-19 13:33:42 -07003410Mutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
3411MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003412
3413Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
3414MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
3415
buzbeec531cef2012-10-18 07:09:20 -07003416#if defined(ART_USE_LLVM_COMPILER)
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003417Mutex* MethodVerifier::inferred_reg_category_maps_lock_ = NULL;
3418MethodVerifier::InferredRegCategoryMapTable* MethodVerifier::inferred_reg_category_maps_ = NULL;
3419#endif
3420
3421void MethodVerifier::Init() {
Ian Rogers0c7abda2012-09-19 13:33:42 -07003422 dex_gc_maps_lock_ = new Mutex("verifier GC maps lock");
Ian Rogers50b35e22012-10-04 10:09:15 -07003423 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003424 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003425 MutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003426 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003427 }
3428
3429 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
3430 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003431 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003432 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
3433 }
3434
buzbeec531cef2012-10-18 07:09:20 -07003435#if defined(ART_USE_LLVM_COMPILER)
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003436 inferred_reg_category_maps_lock_ = new Mutex("verifier GC maps lock");
3437 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003438 MutexLock mu(self, *inferred_reg_category_maps_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003439 inferred_reg_category_maps_ = new MethodVerifier::InferredRegCategoryMapTable;
3440 }
3441#endif
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003442}
3443
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003444void MethodVerifier::Shutdown() {
Ian Rogers50b35e22012-10-04 10:09:15 -07003445 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003446 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003447 MutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003448 STLDeleteValues(dex_gc_maps_);
3449 delete dex_gc_maps_;
3450 dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003451 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003452 delete dex_gc_maps_lock_;
3453 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003454
3455 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003456 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003457 delete rejected_classes_;
3458 rejected_classes_ = NULL;
3459 }
3460 delete rejected_classes_lock_;
3461 rejected_classes_lock_ = NULL;
3462
buzbeec531cef2012-10-18 07:09:20 -07003463#if defined(ART_USE_LLVM_COMPILER)
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003464 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003465 MutexLock mu(self, *inferred_reg_category_maps_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003466 STLDeleteValues(inferred_reg_category_maps_);
3467 delete inferred_reg_category_maps_;
3468 inferred_reg_category_maps_ = NULL;
3469 }
3470 delete inferred_reg_category_maps_lock_;
3471 inferred_reg_category_maps_lock_ = NULL;
3472#endif
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003473}
jeffhaod1224c72012-02-29 13:43:08 -08003474
Ian Rogers776ac1f2012-04-13 23:36:36 -07003475void MethodVerifier::AddRejectedClass(Compiler::ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003476 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003477 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003478 rejected_classes_->insert(ref);
3479 }
jeffhaod1224c72012-02-29 13:43:08 -08003480 CHECK(IsClassRejected(ref));
3481}
3482
Ian Rogers776ac1f2012-04-13 23:36:36 -07003483bool MethodVerifier::IsClassRejected(Compiler::ClassReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003484 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003485 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08003486}
3487
buzbeec531cef2012-10-18 07:09:20 -07003488#if defined(ART_USE_LLVM_COMPILER)
TDYa12789f96052012-07-12 20:49:53 -07003489const greenland::InferredRegCategoryMap* MethodVerifier::GenerateInferredRegCategoryMap() {
Logan Chienfca7e872011-12-20 20:08:22 +08003490 uint32_t insns_size = code_item_->insns_size_in_code_units_;
3491 uint16_t regs_size = code_item_->registers_size_;
3492
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003493 UniquePtr<InferredRegCategoryMap> table(new InferredRegCategoryMap(insns_size, regs_size));
Logan Chienfca7e872011-12-20 20:08:22 +08003494
3495 for (size_t i = 0; i < insns_size; ++i) {
3496 if (RegisterLine* line = reg_table_.GetLine(i)) {
TDYa127526643e2012-05-26 01:01:48 -07003497 const Instruction* inst = Instruction::At(code_item_->insns_ + i);
TDYa127526643e2012-05-26 01:01:48 -07003498 /* We only use InferredRegCategoryMap in one case */
3499 if (inst->IsBranch()) {
TDYa127b2eb5c12012-05-24 15:52:10 -07003500 for (size_t r = 0; r < regs_size; ++r) {
3501 const RegType &rt = line->GetRegisterType(r);
3502
3503 if (rt.IsZero()) {
TDYa12789f96052012-07-12 20:49:53 -07003504 table->SetRegCategory(i, r, greenland::kRegZero);
TDYa127b2eb5c12012-05-24 15:52:10 -07003505 } else if (rt.IsCategory1Types()) {
TDYa12789f96052012-07-12 20:49:53 -07003506 table->SetRegCategory(i, r, greenland::kRegCat1nr);
TDYa127b2eb5c12012-05-24 15:52:10 -07003507 } else if (rt.IsCategory2Types()) {
TDYa12789f96052012-07-12 20:49:53 -07003508 table->SetRegCategory(i, r, greenland::kRegCat2);
TDYa127b2eb5c12012-05-24 15:52:10 -07003509 } else if (rt.IsReferenceTypes()) {
TDYa12789f96052012-07-12 20:49:53 -07003510 table->SetRegCategory(i, r, greenland::kRegObject);
TDYa127b2eb5c12012-05-24 15:52:10 -07003511 } else {
TDYa12789f96052012-07-12 20:49:53 -07003512 table->SetRegCategory(i, r, greenland::kRegUnknown);
TDYa127b2eb5c12012-05-24 15:52:10 -07003513 }
Logan Chienfca7e872011-12-20 20:08:22 +08003514 }
3515 }
3516 }
3517 }
3518
3519 return table.release();
3520}
Logan Chiendd361c92012-04-10 23:40:37 +08003521
Ian Rogers776ac1f2012-04-13 23:36:36 -07003522void MethodVerifier::SetInferredRegCategoryMap(Compiler::MethodReference ref,
3523 const InferredRegCategoryMap& inferred_reg_category_map) {
Shih-wei Liaocd05a622012-08-15 00:02:05 -07003524 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003525 MutexLock mu(Thread::Current(), *inferred_reg_category_maps_lock_);
Shih-wei Liaocd05a622012-08-15 00:02:05 -07003526 InferredRegCategoryMapTable::iterator it = inferred_reg_category_maps_->find(ref);
3527 if (it == inferred_reg_category_maps_->end()) {
3528 inferred_reg_category_maps_->Put(ref, &inferred_reg_category_map);
3529 } else {
3530 CHECK(*(it->second) == inferred_reg_category_map);
3531 delete &inferred_reg_category_map;
3532 }
Logan Chiendd361c92012-04-10 23:40:37 +08003533 }
Logan Chiendd361c92012-04-10 23:40:37 +08003534 CHECK(GetInferredRegCategoryMap(ref) != NULL);
3535}
3536
TDYa12789f96052012-07-12 20:49:53 -07003537const greenland::InferredRegCategoryMap*
Ian Rogers776ac1f2012-04-13 23:36:36 -07003538MethodVerifier::GetInferredRegCategoryMap(Compiler::MethodReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003539 MutexLock mu(Thread::Current(), *inferred_reg_category_maps_lock_);
Logan Chiendd361c92012-04-10 23:40:37 +08003540
3541 InferredRegCategoryMapTable::const_iterator it =
3542 inferred_reg_category_maps_->find(ref);
3543
3544 if (it == inferred_reg_category_maps_->end()) {
3545 return NULL;
3546 }
3547 CHECK(it->second != NULL);
3548 return it->second;
3549}
Logan Chienfca7e872011-12-20 20:08:22 +08003550#endif
3551
Ian Rogersd81871c2011-10-03 13:57:23 -07003552} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003553} // namespace art