blob: bcac374d4660dfbb2778b496974679e5f844dfdd [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Ian Rogers776ac1f2012-04-13 23:36:36 -070017#include "method_verifier.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080022#include "base/stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070023#include "class_linker.h"
Brian Carlstrome7d856b2012-01-11 18:10:55 -080024#include "compiler.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070025#include "dex_file.h"
26#include "dex_instruction.h"
27#include "dex_instruction_visitor.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "gc/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080029#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070030#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070031#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "mirror/abstract_method-inl.h"
33#include "mirror/class.h"
34#include "mirror/class-inl.h"
35#include "mirror/dex_cache.h"
36#include "mirror/field-inl.h"
37#include "mirror/object-inl.h"
38#include "mirror/object_array-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080039#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070040#include "runtime.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080041#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070042
buzbeec531cef2012-10-18 07:09:20 -070043#if defined(ART_USE_LLVM_COMPILER)
TDYa12789f96052012-07-12 20:49:53 -070044#include "greenland/backend_types.h"
45#include "greenland/inferred_reg_category_map.h"
Logan Chienfca7e872011-12-20 20:08:22 +080046#endif
47
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070048namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070049namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070050
Ian Rogers2c8a8572011-10-24 17:11:36 -070051static const bool gDebugVerify = false;
52
Ian Rogers776ac1f2012-04-13 23:36:36 -070053class InsnFlags {
54 public:
55 InsnFlags() : length_(0), flags_(0) {}
56
57 void SetLengthInCodeUnits(size_t length) {
58 CHECK_LT(length, 65536u);
59 length_ = length;
60 }
61 size_t GetLengthInCodeUnits() {
62 return length_;
63 }
64 bool IsOpcode() const {
65 return length_ != 0;
66 }
67
68 void SetInTry() {
69 flags_ |= 1 << kInTry;
70 }
71 void ClearInTry() {
72 flags_ &= ~(1 << kInTry);
73 }
74 bool IsInTry() const {
75 return (flags_ & (1 << kInTry)) != 0;
76 }
77
78 void SetBranchTarget() {
79 flags_ |= 1 << kBranchTarget;
80 }
81 void ClearBranchTarget() {
82 flags_ &= ~(1 << kBranchTarget);
83 }
84 bool IsBranchTarget() const {
85 return (flags_ & (1 << kBranchTarget)) != 0;
86 }
87
88 void SetGcPoint() {
89 flags_ |= 1 << kGcPoint;
90 }
91 void ClearGcPoint() {
92 flags_ &= ~(1 << kGcPoint);
93 }
94 bool IsGcPoint() const {
95 return (flags_ & (1 << kGcPoint)) != 0;
96 }
97
98 void SetVisited() {
99 flags_ |= 1 << kVisited;
100 }
101 void ClearVisited() {
102 flags_ &= ~(1 << kVisited);
103 }
104 bool IsVisited() const {
105 return (flags_ & (1 << kVisited)) != 0;
106 }
107
108 void SetChanged() {
109 flags_ |= 1 << kChanged;
110 }
111 void ClearChanged() {
112 flags_ &= ~(1 << kChanged);
113 }
114 bool IsChanged() const {
115 return (flags_ & (1 << kChanged)) != 0;
116 }
117
118 bool IsVisitedOrChanged() const {
119 return IsVisited() || IsChanged();
120 }
121
122 std::string Dump() {
123 char encoding[6];
124 if (!IsOpcode()) {
125 strncpy(encoding, "XXXXX", sizeof(encoding));
126 } else {
127 strncpy(encoding, "-----", sizeof(encoding));
128 if (IsInTry()) encoding[kInTry] = 'T';
129 if (IsBranchTarget()) encoding[kBranchTarget] = 'B';
130 if (IsGcPoint()) encoding[kGcPoint] = 'G';
131 if (IsVisited()) encoding[kVisited] = 'V';
132 if (IsChanged()) encoding[kChanged] = 'C';
133 }
134 return std::string(encoding);
135 }
Elliott Hughesa21039c2012-06-21 12:09:25 -0700136
Ian Rogers776ac1f2012-04-13 23:36:36 -0700137 private:
138 enum {
139 kInTry,
140 kBranchTarget,
141 kGcPoint,
142 kVisited,
143 kChanged,
144 };
145
146 // Size of instruction in code units
147 uint16_t length_;
148 uint8_t flags_;
Ian Rogers84fa0742011-10-25 18:13:30 -0700149};
Ian Rogersd81871c2011-10-03 13:57:23 -0700150
Ian Rogersd81871c2011-10-03 13:57:23 -0700151void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
152 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -0700153 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700154 DCHECK_GT(insns_size, 0U);
155
156 for (uint32_t i = 0; i < insns_size; i++) {
157 bool interesting = false;
158 switch (mode) {
159 case kTrackRegsAll:
160 interesting = flags[i].IsOpcode();
161 break;
162 case kTrackRegsGcPoints:
163 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
164 break;
165 case kTrackRegsBranches:
166 interesting = flags[i].IsBranchTarget();
167 break;
168 default:
169 break;
170 }
171 if (interesting) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700172 pc_to_register_line_.Put(i, new RegisterLine(registers_size, verifier));
Ian Rogersd81871c2011-10-03 13:57:23 -0700173 }
174 }
175}
176
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800177MethodVerifier::FailureKind MethodVerifier::VerifyClass(const mirror::Class* klass,
178 std::string& error) {
jeffhaobdb76512011-09-07 11:43:16 -0700179 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700180 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700181 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800182 mirror::Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -0800183 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800184 error = "Verifier rejected class ";
185 error += PrettyDescriptor(klass);
186 error += " that has no super class";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700187 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -0700188 }
Ian Rogers1c5eb702012-02-01 09:18:34 -0800189 if (super != NULL && super->IsFinal()) {
190 error = "Verifier rejected class ";
191 error += PrettyDescriptor(klass);
192 error += " that attempts to sub-class final class ";
193 error += PrettyDescriptor(super);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700194 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -0700195 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700196 ClassHelper kh(klass);
197 const DexFile& dex_file = kh.GetDexFile();
198 uint32_t class_def_idx;
199 if (!dex_file.FindClassDefIndex(kh.GetDescriptor(), class_def_idx)) {
200 error = "Verifier rejected class ";
201 error += PrettyDescriptor(klass);
202 error += " that isn't present in dex file ";
203 error += dex_file.GetLocation();
jeffhaof1e6b7c2012-06-05 18:33:30 -0700204 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700205 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700206 return VerifyClass(&dex_file, kh.GetDexCache(), klass->GetClassLoader(), class_def_idx, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700207}
208
Ian Rogers365c1022012-06-22 15:05:28 -0700209MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800210 mirror::DexCache* dex_cache,
211 mirror::ClassLoader* class_loader,
212 uint32_t class_def_idx,
213 std::string& error) {
jeffhaof56197c2012-03-05 18:01:54 -0800214 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
215 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700216 if (class_data == NULL) {
217 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700218 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700219 }
jeffhaof56197c2012-03-05 18:01:54 -0800220 ClassDataItemIterator it(*dex_file, class_data);
221 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
222 it.Next();
223 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700224 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700225 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700226 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700227 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800228 while (it.HasNextDirectMethod()) {
229 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700230 if (method_idx == previous_direct_method_idx) {
231 // smali can create dex files with two encoded_methods sharing the same method_idx
232 // http://code.google.com/p/smali/issues/detail?id=119
233 it.Next();
234 continue;
235 }
236 previous_direct_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700237 InvokeType type = it.GetMethodInvokeType(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238 mirror::AbstractMethod* method =
239 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700240 if (method == NULL) {
241 DCHECK(Thread::Current()->IsExceptionPending());
242 // We couldn't resolve the method, but continue regardless.
243 Thread::Current()->ClearException();
244 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700245 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
246 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags());
247 if (result != kNoFailure) {
248 if (result == kHardFailure) {
249 hard_fail = true;
250 if (error_count > 0) {
251 error += "\n";
252 }
253 error = "Verifier rejected class ";
254 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
255 error += " due to bad method ";
256 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700257 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700258 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800259 }
260 it.Next();
261 }
jeffhao9b0b1882012-10-01 16:51:22 -0700262 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800263 while (it.HasNextVirtualMethod()) {
264 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700265 if (method_idx == previous_virtual_method_idx) {
266 // smali can create dex files with two encoded_methods sharing the same method_idx
267 // http://code.google.com/p/smali/issues/detail?id=119
268 it.Next();
269 continue;
270 }
271 previous_virtual_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700272 InvokeType type = it.GetMethodInvokeType(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800273 mirror::AbstractMethod* method =
274 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700275 if (method == NULL) {
276 DCHECK(Thread::Current()->IsExceptionPending());
277 // We couldn't resolve the method, but continue regardless.
278 Thread::Current()->ClearException();
279 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700280 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
281 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags());
282 if (result != kNoFailure) {
283 if (result == kHardFailure) {
284 hard_fail = true;
285 if (error_count > 0) {
286 error += "\n";
287 }
288 error = "Verifier rejected class ";
289 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
290 error += " due to bad method ";
291 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700292 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700293 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800294 }
295 it.Next();
296 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700297 if (error_count == 0) {
298 return kNoFailure;
299 } else {
300 return hard_fail ? kHardFailure : kSoftFailure;
301 }
jeffhaof56197c2012-03-05 18:01:54 -0800302}
303
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800304MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
305 const DexFile* dex_file,
306 mirror::DexCache* dex_cache,
307 mirror::ClassLoader* class_loader,
308 uint32_t class_def_idx,
309 const DexFile::CodeItem* code_item,
310 mirror::AbstractMethod* method,
311 uint32_t method_access_flags) {
Ian Rogersc8982582012-09-07 16:53:25 -0700312 MethodVerifier::FailureKind result = kNoFailure;
313 uint64_t start_ns = NanoTime();
314
Ian Rogersad0b3a32012-04-16 14:50:24 -0700315 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item, method_idx,
Elliott Hughes80537bb2013-01-04 16:37:26 -0800316 method, method_access_flags, true);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700317 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700318 // Verification completed, however failures may be pending that didn't cause the verification
319 // to hard fail.
Ian Rogerse551e952012-06-03 22:59:14 -0700320 CHECK(!verifier.have_pending_hard_failure_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700321 if (verifier.failures_.size() != 0) {
322 verifier.DumpFailures(LOG(INFO) << "Soft verification failures in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700323 << PrettyMethod(method_idx, *dex_file) << "\n");
Ian Rogersc8982582012-09-07 16:53:25 -0700324 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800325 }
326 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700327 // Bad method data.
328 CHECK_NE(verifier.failures_.size(), 0U);
329 CHECK(verifier.have_pending_hard_failure_);
330 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700331 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800332 if (gDebugVerify) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700333 std::cout << "\n" << verifier.info_messages_.str();
jeffhaof56197c2012-03-05 18:01:54 -0800334 verifier.Dump(std::cout);
335 }
Ian Rogersc8982582012-09-07 16:53:25 -0700336 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800337 }
Ian Rogersc8982582012-09-07 16:53:25 -0700338 uint64_t duration_ns = NanoTime() - start_ns;
339 if (duration_ns > MsToNs(100)) {
340 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
341 << " took " << PrettyDuration(duration_ns);
342 }
343 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800344}
345
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800346void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800347 const DexFile* dex_file, mirror::DexCache* dex_cache,
348 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
349 const DexFile::CodeItem* code_item,
350 mirror::AbstractMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800351 uint32_t method_access_flags) {
352 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item,
Elliott Hughes80537bb2013-01-04 16:37:26 -0800353 dex_method_idx, method, method_access_flags, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700354 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800355 verifier.DumpFailures(os);
356 os << verifier.info_messages_.str();
357 verifier.Dump(os);
358}
359
360std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800361 const DexFile* dex_file,
362 mirror::DexCache* dex_cache,
363 mirror::ClassLoader* class_loader,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800364 uint32_t class_def_idx,
365 const DexFile::CodeItem* code_item,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800366 mirror::AbstractMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800367 uint32_t method_access_flags, uint32_t dex_pc) {
368 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item,
Elliott Hughes80537bb2013-01-04 16:37:26 -0800369 dex_method_idx, method, method_access_flags, true);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800370 verifier.Verify();
371 return verifier.DescribeVRegs(dex_pc);
jeffhaoba5ebb92011-08-25 17:24:37 -0700372}
373
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800374MethodVerifier::MethodVerifier(const DexFile* dex_file, mirror::DexCache* dex_cache,
375 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
376 const DexFile::CodeItem* code_item,
377 uint32_t dex_method_idx, mirror::AbstractMethod* method,
378 uint32_t method_access_flags,
Elliott Hughes80537bb2013-01-04 16:37:26 -0800379 bool can_load_classes)
380 : reg_types_(can_load_classes),
381 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800382 dex_method_idx_(dex_method_idx),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700383 foo_method_(method),
384 method_access_flags_(method_access_flags),
jeffhaof56197c2012-03-05 18:01:54 -0800385 dex_file_(dex_file),
386 dex_cache_(dex_cache),
387 class_loader_(class_loader),
388 class_def_idx_(class_def_idx),
389 code_item_(code_item),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700390 interesting_dex_pc_(-1),
391 monitor_enter_dex_pcs_(NULL),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700392 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700393 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800394 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800395 monitor_enter_count_(0),
396 can_load_classes_(can_load_classes) {
jeffhaof56197c2012-03-05 18:01:54 -0800397}
398
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800399void MethodVerifier::FindLocksAtDexPc(mirror::AbstractMethod* m, uint32_t dex_pc,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800400 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700401 MethodHelper mh(m);
402 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
403 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800404 m, m->GetAccessFlags(), false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700405 verifier.interesting_dex_pc_ = dex_pc;
406 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
407 verifier.FindLocksAtDexPc();
408}
409
410void MethodVerifier::FindLocksAtDexPc() {
411 CHECK(monitor_enter_dex_pcs_ != NULL);
412 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
413
414 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
415 // verification. In practice, the phase we want relies on data structures set up by all the
416 // earlier passes, so we just run the full method verification and bail out early when we've
417 // got what we wanted.
418 Verify();
419}
420
Ian Rogersad0b3a32012-04-16 14:50:24 -0700421bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700422 // If there aren't any instructions, make sure that's expected, then exit successfully.
423 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700424 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700425 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700426 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700427 } else {
428 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700429 }
jeffhaobdb76512011-09-07 11:43:16 -0700430 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700431 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
432 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700433 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
434 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700435 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700436 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700437 // Allocate and initialize an array to hold instruction data.
438 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
439 // Run through the instructions and see if the width checks out.
440 bool result = ComputeWidthsAndCountOps();
441 // Flag instructions guarded by a "try" block and check exception handlers.
442 result = result && ScanTryCatchBlocks();
443 // Perform static instruction verification.
444 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700445 // Perform code-flow analysis and return.
446 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700447}
448
Ian Rogers776ac1f2012-04-13 23:36:36 -0700449std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700450 switch (error) {
451 case VERIFY_ERROR_NO_CLASS:
452 case VERIFY_ERROR_NO_FIELD:
453 case VERIFY_ERROR_NO_METHOD:
454 case VERIFY_ERROR_ACCESS_CLASS:
455 case VERIFY_ERROR_ACCESS_FIELD:
456 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700457 case VERIFY_ERROR_INSTANTIATION:
458 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800459 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700460 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
461 // class change and instantiation errors into soft verification errors so that we re-verify
462 // at runtime. We may fail to find or to agree on access because of not yet available class
463 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
464 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
465 // paths" that dynamically perform the verification and cause the behavior to be that akin
466 // to an interpreter.
467 error = VERIFY_ERROR_BAD_CLASS_SOFT;
468 } else {
469 have_pending_runtime_throw_failure_ = true;
470 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700471 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700472 // Indication that verification should be retried at runtime.
473 case VERIFY_ERROR_BAD_CLASS_SOFT:
474 if (!Runtime::Current()->IsCompiler()) {
475 // It is runtime so hard fail.
476 have_pending_hard_failure_ = true;
477 }
478 break;
jeffhaod5347e02012-03-22 17:25:05 -0700479 // Hard verification failures at compile time will still fail at runtime, so the class is
480 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700481 case VERIFY_ERROR_BAD_CLASS_HARD: {
482 if (Runtime::Current()->IsCompiler()) {
jeffhaof56197c2012-03-05 18:01:54 -0800483 Compiler::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800484 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800485 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700486 have_pending_hard_failure_ = true;
487 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800488 }
489 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700490 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800491 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700492 work_insn_idx_));
493 std::ostringstream* failure_message = new std::ostringstream(location);
494 failure_messages_.push_back(failure_message);
495 return *failure_message;
496}
497
498void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
499 size_t failure_num = failure_messages_.size();
500 DCHECK_NE(failure_num, 0U);
501 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
502 prepend += last_fail_message->str();
503 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
504 delete last_fail_message;
505}
506
507void MethodVerifier::AppendToLastFailMessage(std::string append) {
508 size_t failure_num = failure_messages_.size();
509 DCHECK_NE(failure_num, 0U);
510 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
511 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800512}
513
Ian Rogers776ac1f2012-04-13 23:36:36 -0700514bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700515 const uint16_t* insns = code_item_->insns_;
516 size_t insns_size = code_item_->insns_size_in_code_units_;
517 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700518 size_t new_instance_count = 0;
519 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700520 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700521
Ian Rogersd81871c2011-10-03 13:57:23 -0700522 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700523 Instruction::Code opcode = inst->Opcode();
524 if (opcode == Instruction::NEW_INSTANCE) {
525 new_instance_count++;
526 } else if (opcode == Instruction::MONITOR_ENTER) {
527 monitor_enter_count++;
528 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700529 size_t inst_size = inst->SizeInCodeUnits();
530 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
531 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700532 inst = inst->Next();
533 }
534
Ian Rogersd81871c2011-10-03 13:57:23 -0700535 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700536 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
537 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700538 return false;
539 }
540
Ian Rogersd81871c2011-10-03 13:57:23 -0700541 new_instance_count_ = new_instance_count;
542 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700543 return true;
544}
545
Ian Rogers776ac1f2012-04-13 23:36:36 -0700546bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700547 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700548 if (tries_size == 0) {
549 return true;
550 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700551 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700552 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700553
554 for (uint32_t idx = 0; idx < tries_size; idx++) {
555 const DexFile::TryItem* try_item = &tries[idx];
556 uint32_t start = try_item->start_addr_;
557 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700558 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700559 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
560 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700561 return false;
562 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700563 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700564 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700565 return false;
566 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700567 for (uint32_t dex_pc = start; dex_pc < end;
568 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
569 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700570 }
571 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800572 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700573 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700574 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700575 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700576 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700577 CatchHandlerIterator iterator(handlers_ptr);
578 for (; iterator.HasNext(); iterator.Next()) {
579 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700580 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700581 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700582 return false;
583 }
jeffhao60f83e32012-02-13 17:16:30 -0800584 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
585 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700586 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700587 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800588 return false;
589 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700590 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700591 // Ensure exception types are resolved so that they don't need resolution to be delivered,
592 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700593 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800594 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
595 iterator.GetHandlerTypeIndex(),
596 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700597 if (exception_type == NULL) {
598 DCHECK(Thread::Current()->IsExceptionPending());
599 Thread::Current()->ClearException();
600 }
601 }
jeffhaobdb76512011-09-07 11:43:16 -0700602 }
Ian Rogers0571d352011-11-03 19:51:38 -0700603 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700604 }
jeffhaobdb76512011-09-07 11:43:16 -0700605 return true;
606}
607
Ian Rogers776ac1f2012-04-13 23:36:36 -0700608bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700609 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700610
Ian Rogers0c7abda2012-09-19 13:33:42 -0700611 /* 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 -0700612 insn_flags_[0].SetBranchTarget();
Ian Rogers0c7abda2012-09-19 13:33:42 -0700613 insn_flags_[0].SetGcPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700614
615 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700616 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700617 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700618 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700619 return false;
620 }
621 /* Flag instructions that are garbage collection points */
622 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
623 insn_flags_[dex_pc].SetGcPoint();
624 }
625 dex_pc += inst->SizeInCodeUnits();
626 inst = inst->Next();
627 }
628 return true;
629}
630
Ian Rogers776ac1f2012-04-13 23:36:36 -0700631bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800632 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700633 bool result = true;
634 switch (inst->GetVerifyTypeArgumentA()) {
635 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800636 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700637 break;
638 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800639 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700640 break;
641 }
642 switch (inst->GetVerifyTypeArgumentB()) {
643 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800644 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700645 break;
646 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800647 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700648 break;
649 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800650 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700651 break;
652 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800653 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700654 break;
655 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800656 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700657 break;
658 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800659 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700660 break;
661 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800662 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700663 break;
664 }
665 switch (inst->GetVerifyTypeArgumentC()) {
666 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800667 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700668 break;
669 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800670 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700671 break;
672 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800673 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 break;
675 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800676 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700677 break;
678 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800679 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700680 break;
681 }
682 switch (inst->GetVerifyExtraFlags()) {
683 case Instruction::kVerifyArrayData:
684 result = result && CheckArrayData(code_offset);
685 break;
686 case Instruction::kVerifyBranchTarget:
687 result = result && CheckBranchTarget(code_offset);
688 break;
689 case Instruction::kVerifySwitchTargets:
690 result = result && CheckSwitchTargets(code_offset);
691 break;
692 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800693 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700694 break;
695 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800696 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700697 break;
698 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700699 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700700 result = false;
701 break;
702 }
703 return result;
704}
705
Ian Rogers776ac1f2012-04-13 23:36:36 -0700706bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700707 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700708 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
709 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700710 return false;
711 }
712 return true;
713}
714
Ian Rogers776ac1f2012-04-13 23:36:36 -0700715bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700716 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700717 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
718 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700719 return false;
720 }
721 return true;
722}
723
Ian Rogers776ac1f2012-04-13 23:36:36 -0700724bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700725 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700726 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
727 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700728 return false;
729 }
730 return true;
731}
732
Ian Rogers776ac1f2012-04-13 23:36:36 -0700733bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700734 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700735 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
736 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700737 return false;
738 }
739 return true;
740}
741
Ian Rogers776ac1f2012-04-13 23:36:36 -0700742bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700743 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700744 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
745 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700746 return false;
747 }
748 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700749 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700750 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700751 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700752 return false;
753 }
754 return true;
755}
756
Ian Rogers776ac1f2012-04-13 23:36:36 -0700757bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700758 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700759 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
760 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700761 return false;
762 }
763 return true;
764}
765
Ian Rogers776ac1f2012-04-13 23:36:36 -0700766bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700767 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700768 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
769 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700770 return false;
771 }
772 return true;
773}
774
Ian Rogers776ac1f2012-04-13 23:36:36 -0700775bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700776 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700777 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
778 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700779 return false;
780 }
781 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700782 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700783 const char* cp = descriptor;
784 while (*cp++ == '[') {
785 bracket_count++;
786 }
787 if (bracket_count == 0) {
788 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700789 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700790 return false;
791 } else if (bracket_count > 255) {
792 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700793 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700794 return false;
795 }
796 return true;
797}
798
Ian Rogers776ac1f2012-04-13 23:36:36 -0700799bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700800 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
801 const uint16_t* insns = code_item_->insns_ + cur_offset;
802 const uint16_t* array_data;
803 int32_t array_data_offset;
804
805 DCHECK_LT(cur_offset, insn_count);
806 /* make sure the start of the array data table is in range */
807 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
808 if ((int32_t) cur_offset + array_data_offset < 0 ||
809 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700810 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
811 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700812 return false;
813 }
814 /* offset to array data table is a relative branch-style offset */
815 array_data = insns + array_data_offset;
816 /* make sure the table is 32-bit aligned */
817 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700818 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
819 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700820 return false;
821 }
822 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700823 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700824 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
825 /* make sure the end of the switch is in range */
826 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700827 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
828 << ", data offset " << array_data_offset << ", end "
829 << cur_offset + array_data_offset + table_size
830 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700831 return false;
832 }
833 return true;
834}
835
Ian Rogers776ac1f2012-04-13 23:36:36 -0700836bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700837 int32_t offset;
838 bool isConditional, selfOkay;
839 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
840 return false;
841 }
842 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700843 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 -0700844 return false;
845 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700846 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
847 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700848 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700849 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700850 return false;
851 }
852 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
853 int32_t abs_offset = cur_offset + offset;
854 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700855 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700856 << reinterpret_cast<void*>(abs_offset) << ") at "
857 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700858 return false;
859 }
860 insn_flags_[abs_offset].SetBranchTarget();
861 return true;
862}
863
Ian Rogers776ac1f2012-04-13 23:36:36 -0700864bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 bool* selfOkay) {
866 const uint16_t* insns = code_item_->insns_ + cur_offset;
867 *pConditional = false;
868 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700869 switch (*insns & 0xff) {
870 case Instruction::GOTO:
871 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700872 break;
873 case Instruction::GOTO_32:
874 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700875 *selfOkay = true;
876 break;
877 case Instruction::GOTO_16:
878 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700879 break;
880 case Instruction::IF_EQ:
881 case Instruction::IF_NE:
882 case Instruction::IF_LT:
883 case Instruction::IF_GE:
884 case Instruction::IF_GT:
885 case Instruction::IF_LE:
886 case Instruction::IF_EQZ:
887 case Instruction::IF_NEZ:
888 case Instruction::IF_LTZ:
889 case Instruction::IF_GEZ:
890 case Instruction::IF_GTZ:
891 case Instruction::IF_LEZ:
892 *pOffset = (int16_t) insns[1];
893 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700894 break;
895 default:
896 return false;
897 break;
898 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700899 return true;
900}
901
Ian Rogers776ac1f2012-04-13 23:36:36 -0700902bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700903 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700904 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700905 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700906 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700907 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
908 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700909 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
910 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700911 return false;
912 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700913 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700914 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700915 /* make sure the table is 32-bit aligned */
916 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700917 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
918 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700919 return false;
920 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700921 uint32_t switch_count = switch_insns[1];
922 int32_t keys_offset, targets_offset;
923 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700924 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
925 /* 0=sig, 1=count, 2/3=firstKey */
926 targets_offset = 4;
927 keys_offset = -1;
928 expected_signature = Instruction::kPackedSwitchSignature;
929 } else {
930 /* 0=sig, 1=count, 2..count*2 = keys */
931 keys_offset = 2;
932 targets_offset = 2 + 2 * switch_count;
933 expected_signature = Instruction::kSparseSwitchSignature;
934 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700935 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700936 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700937 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
938 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700939 return false;
940 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700941 /* make sure the end of the switch is in range */
942 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700943 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
944 << switch_offset << ", end "
945 << (cur_offset + switch_offset + table_size)
946 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700947 return false;
948 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700949 /* for a sparse switch, verify the keys are in ascending order */
950 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700951 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
952 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700953 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
954 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
955 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700956 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
957 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700958 return false;
959 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700960 last_key = key;
961 }
962 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700963 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700964 for (uint32_t targ = 0; targ < switch_count; targ++) {
965 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
966 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
967 int32_t abs_offset = cur_offset + offset;
968 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700969 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700970 << reinterpret_cast<void*>(abs_offset) << ") at "
971 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700972 return false;
973 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 insn_flags_[abs_offset].SetBranchTarget();
975 }
976 return true;
977}
978
Ian Rogers776ac1f2012-04-13 23:36:36 -0700979bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700981 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700982 return false;
983 }
984 uint16_t registers_size = code_item_->registers_size_;
985 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800986 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700987 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
988 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700989 return false;
990 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700991 }
992
993 return true;
994}
995
Ian Rogers776ac1f2012-04-13 23:36:36 -0700996bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700997 uint16_t registers_size = code_item_->registers_size_;
998 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
999 // integer overflow when adding them here.
1000 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001001 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
1002 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001003 return false;
1004 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001005 return true;
1006}
1007
Ian Rogers0c7abda2012-09-19 13:33:42 -07001008static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -08001009 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
1010 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
1011 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
1012 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
1013 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
1014 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
1015 gc_map.begin(),
1016 gc_map.end());
1017 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
1018 DCHECK_EQ(gc_map.size(),
1019 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
1020 (length_prefixed_gc_map->at(1) << 16) |
1021 (length_prefixed_gc_map->at(2) << 8) |
1022 (length_prefixed_gc_map->at(3) << 0)));
1023 return length_prefixed_gc_map;
1024}
1025
Ian Rogers776ac1f2012-04-13 23:36:36 -07001026bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001027 uint16_t registers_size = code_item_->registers_size_;
1028 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001029
Ian Rogersd81871c2011-10-03 13:57:23 -07001030 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001031 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1032 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001033 }
1034 /* Create and initialize table holding register status */
Elliott Hughes460384f2012-04-04 16:53:10 -07001035 reg_table_.Init(kTrackRegsGcPoints, insn_flags_.get(), insns_size, registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001036
Ian Rogersd81871c2011-10-03 13:57:23 -07001037 work_line_.reset(new RegisterLine(registers_size, this));
1038 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001039
Ian Rogersd81871c2011-10-03 13:57:23 -07001040 /* Initialize register types of method arguments. */
1041 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001042 DCHECK_NE(failures_.size(), 0U);
1043 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001044 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001045 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001046 return false;
1047 }
1048 /* Perform code flow verification. */
1049 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001050 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001051 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001052 }
1053
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001054 Compiler::MethodReference ref(dex_file_, dex_method_idx_);
TDYa127b2eb5c12012-05-24 15:52:10 -07001055
TDYa127b2eb5c12012-05-24 15:52:10 -07001056
Ian Rogersd81871c2011-10-03 13:57:23 -07001057 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -08001058 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1059 if (map.get() == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001060 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001061 return false; // Not a real failure, but a failure to encode
1062 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001063#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001064 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -07001065#endif
Ian Rogers0c7abda2012-09-19 13:33:42 -07001066 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
1067 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
Logan Chiendd361c92012-04-10 23:40:37 +08001068
TDYa127ce4cc0d2012-11-18 16:59:53 -08001069#if defined(ART_USE_LLVM_COMPILER)
Logan Chienfca7e872011-12-20 20:08:22 +08001070 /* Generate Inferred Register Category for LLVM-based Code Generator */
1071 const InferredRegCategoryMap* table = GenerateInferredRegCategoryMap();
Ian Rogers776ac1f2012-04-13 23:36:36 -07001072 verifier::MethodVerifier::SetInferredRegCategoryMap(ref, *table);
Logan Chienfca7e872011-12-20 20:08:22 +08001073#endif
1074
jeffhaobdb76512011-09-07 11:43:16 -07001075 return true;
1076}
1077
Ian Rogersad0b3a32012-04-16 14:50:24 -07001078std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1079 DCHECK_EQ(failures_.size(), failure_messages_.size());
1080 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001081 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001082 }
1083 return os;
1084}
1085
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001086extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001087 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001088 v->Dump(std::cerr);
1089}
1090
Ian Rogers776ac1f2012-04-13 23:36:36 -07001091void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001092 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001093 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001094 return;
jeffhaobdb76512011-09-07 11:43:16 -07001095 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001096 {
1097 os << "Register Types:\n";
1098 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1099 std::ostream indent_os(&indent_filter);
1100 reg_types_.Dump(indent_os);
1101 }
Ian Rogersb4903572012-10-11 11:52:56 -07001102 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001103 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1104 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001105 const Instruction* inst = Instruction::At(code_item_->insns_);
1106 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1107 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001108 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1109 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001110 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001111 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001112 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump() << " ";
1113 const bool kDumpHexOfInstruction = false;
1114 if (kDumpHexOfInstruction) {
1115 indent_os << inst->DumpHex(5) << " ";
1116 }
1117 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001118 inst = inst->Next();
1119 }
jeffhaobdb76512011-09-07 11:43:16 -07001120}
1121
Ian Rogersd81871c2011-10-03 13:57:23 -07001122static bool IsPrimitiveDescriptor(char descriptor) {
1123 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001124 case 'I':
1125 case 'C':
1126 case 'S':
1127 case 'B':
1128 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001129 case 'F':
1130 case 'D':
1131 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001132 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001133 default:
1134 return false;
1135 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001136}
1137
Ian Rogers776ac1f2012-04-13 23:36:36 -07001138bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001139 RegisterLine* reg_line = reg_table_.GetLine(0);
1140 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1141 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001142
Ian Rogersd81871c2011-10-03 13:57:23 -07001143 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1144 //Include the "this" pointer.
1145 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001146 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001147 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1148 // argument as uninitialized. This restricts field access until the superclass constructor is
1149 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001150 const RegType& declaring_class = GetDeclaringClass();
1151 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001152 reg_line->SetRegisterType(arg_start + cur_arg,
1153 reg_types_.UninitializedThisArgument(declaring_class));
1154 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001155 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001156 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001157 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001158 }
1159
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001160 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001161 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001162 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001163
1164 for (; iterator.HasNext(); iterator.Next()) {
1165 const char* descriptor = iterator.GetDescriptor();
1166 if (descriptor == NULL) {
1167 LOG(FATAL) << "Null descriptor";
1168 }
1169 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001170 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1171 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001172 return false;
1173 }
1174 switch (descriptor[0]) {
1175 case 'L':
1176 case '[':
1177 // We assume that reference arguments are initialized. The only way it could be otherwise
1178 // (assuming the caller was verified) is if the current method is <init>, but in that case
1179 // it's effectively considered initialized the instant we reach here (in the sense that we
1180 // can return without doing anything or call virtual methods).
1181 {
Ian Rogersb4903572012-10-11 11:52:56 -07001182 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001183 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001184 }
1185 break;
1186 case 'Z':
1187 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1188 break;
1189 case 'C':
1190 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1191 break;
1192 case 'B':
1193 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1194 break;
1195 case 'I':
1196 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1197 break;
1198 case 'S':
1199 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1200 break;
1201 case 'F':
1202 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1203 break;
1204 case 'J':
1205 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001206 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1207 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1208 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001209 cur_arg++;
1210 break;
1211 }
1212 default:
jeffhaod5347e02012-03-22 17:25:05 -07001213 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001214 return false;
1215 }
1216 cur_arg++;
1217 }
1218 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001219 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001220 return false;
1221 }
1222 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1223 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1224 // format. Only major difference from the method argument format is that 'V' is supported.
1225 bool result;
1226 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1227 result = descriptor[1] == '\0';
1228 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1229 size_t i = 0;
1230 do {
1231 i++;
1232 } while (descriptor[i] == '['); // process leading [
1233 if (descriptor[i] == 'L') { // object array
1234 do {
1235 i++; // find closing ;
1236 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1237 result = descriptor[i] == ';';
1238 } else { // primitive array
1239 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1240 }
1241 } else if (descriptor[0] == 'L') {
1242 // could be more thorough here, but shouldn't be required
1243 size_t i = 0;
1244 do {
1245 i++;
1246 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1247 result = descriptor[i] == ';';
1248 } else {
1249 result = false;
1250 }
1251 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001252 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1253 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001254 }
1255 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001256}
1257
Ian Rogers776ac1f2012-04-13 23:36:36 -07001258bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001259 const uint16_t* insns = code_item_->insns_;
1260 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001261
jeffhaobdb76512011-09-07 11:43:16 -07001262 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001263 insn_flags_[0].SetChanged();
1264 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001265
jeffhaobdb76512011-09-07 11:43:16 -07001266 /* Continue until no instructions are marked "changed". */
1267 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001268 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1269 uint32_t insn_idx = start_guess;
1270 for (; insn_idx < insns_size; insn_idx++) {
1271 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001272 break;
1273 }
jeffhaobdb76512011-09-07 11:43:16 -07001274 if (insn_idx == insns_size) {
1275 if (start_guess != 0) {
1276 /* try again, starting from the top */
1277 start_guess = 0;
1278 continue;
1279 } else {
1280 /* all flags are clear */
1281 break;
1282 }
1283 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001284 // We carry the working set of registers from instruction to instruction. If this address can
1285 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1286 // "changed" flags, we need to load the set of registers from the table.
1287 // Because we always prefer to continue on to the next instruction, we should never have a
1288 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1289 // target.
1290 work_insn_idx_ = insn_idx;
1291 if (insn_flags_[insn_idx].IsBranchTarget()) {
1292 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001293 } else {
1294#ifndef NDEBUG
1295 /*
1296 * Sanity check: retrieve the stored register line (assuming
1297 * a full table) and make sure it actually matches.
1298 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001299 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1300 if (register_line != NULL) {
1301 if (work_line_->CompareLine(register_line) != 0) {
1302 Dump(std::cout);
1303 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001304 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001305 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1306 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001307 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 }
jeffhaobdb76512011-09-07 11:43:16 -07001309 }
1310#endif
1311 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001312 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001313 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001314 prepend += " failed to verify: ";
1315 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001316 return false;
1317 }
jeffhaobdb76512011-09-07 11:43:16 -07001318 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001319 insn_flags_[insn_idx].SetVisited();
1320 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001321 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001322
Ian Rogers1c849e52012-06-28 14:00:33 -07001323 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001324 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001325 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001326 * (besides the wasted space), but it indicates a flaw somewhere
1327 * down the line, possibly in the verifier.
1328 *
1329 * If we've substituted "always throw" instructions into the stream,
1330 * we are almost certainly going to have some dead code.
1331 */
1332 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001333 uint32_t insn_idx = 0;
1334 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001335 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001336 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001337 * may or may not be preceded by a padding NOP (for alignment).
1338 */
1339 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1340 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1341 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001342 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001343 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1344 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1345 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001346 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001347 }
1348
Ian Rogersd81871c2011-10-03 13:57:23 -07001349 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001350 if (dead_start < 0)
1351 dead_start = insn_idx;
1352 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001353 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001354 dead_start = -1;
1355 }
1356 }
1357 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001358 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001359 }
1360 }
jeffhaobdb76512011-09-07 11:43:16 -07001361 return true;
1362}
1363
Ian Rogers776ac1f2012-04-13 23:36:36 -07001364bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001365 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1366 // We want the state _before_ the instruction, for the case where the dex pc we're
1367 // interested in is itself a monitor-enter instruction (which is a likely place
1368 // for a thread to be suspended).
1369 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001370 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001371 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1372 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1373 }
1374 }
1375
jeffhaobdb76512011-09-07 11:43:16 -07001376 /*
1377 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001378 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001379 * control to another statement:
1380 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001381 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001382 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001383 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001384 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001385 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001386 * throw an exception that is handled by an encompassing "try"
1387 * block.
1388 *
1389 * We can also return, in which case there is no successor instruction
1390 * from this point.
1391 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001392 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001393 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001394 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1395 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001396 DecodedInstruction dec_insn(inst);
Ian Rogersa75a0132012-09-28 11:41:42 -07001397 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001398
jeffhaobdb76512011-09-07 11:43:16 -07001399 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001400 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001401 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001402 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001403 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1404 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001405 }
jeffhaobdb76512011-09-07 11:43:16 -07001406
1407 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001408 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001409 * can throw an exception, we will copy/merge this into the "catch"
1410 * address rather than work_line, because we don't want the result
1411 * from the "successful" code path (e.g. a check-cast that "improves"
1412 * a type) to be visible to the exception handler.
1413 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001414 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001415 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001416 } else {
1417#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001418 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001419#endif
1420 }
1421
Elliott Hughesadb8c672012-03-06 16:49:32 -08001422 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07001423 case Instruction::NOP:
1424 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001425 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001426 * a signature that looks like a NOP; if we see one of these in
1427 * the course of executing code then we have a problem.
1428 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001429 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001430 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001431 }
1432 break;
1433
1434 case Instruction::MOVE:
1435 case Instruction::MOVE_FROM16:
1436 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001437 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001438 break;
1439 case Instruction::MOVE_WIDE:
1440 case Instruction::MOVE_WIDE_FROM16:
1441 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001442 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07001443 break;
1444 case Instruction::MOVE_OBJECT:
1445 case Instruction::MOVE_OBJECT_FROM16:
1446 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001447 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001448 break;
1449
1450 /*
1451 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001452 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001453 * might want to hold the result in an actual CPU register, so the
1454 * Dalvik spec requires that these only appear immediately after an
1455 * invoke or filled-new-array.
1456 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001457 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001458 * redundant with the reset done below, but it can make the debug info
1459 * easier to read in some cases.)
1460 */
1461 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001462 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07001463 break;
1464 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001465 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001466 break;
1467 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001468 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07001469 break;
1470
Ian Rogersd81871c2011-10-03 13:57:23 -07001471 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001472 /*
jeffhao60f83e32012-02-13 17:16:30 -08001473 * This statement can only appear as the first instruction in an exception handler. We verify
1474 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001475 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001476 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08001477 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001478 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001479 }
jeffhaobdb76512011-09-07 11:43:16 -07001480 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001481 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1482 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001483 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001484 }
jeffhaobdb76512011-09-07 11:43:16 -07001485 }
1486 break;
1487 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001488 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001489 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001490 const RegType& return_type = GetMethodReturnType();
1491 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001492 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001493 } else {
1494 // Compilers may generate synthetic functions that write byte values into boolean fields.
1495 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001496 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001497 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1498 ((return_type.IsBoolean() || return_type.IsByte() ||
1499 return_type.IsShort() || return_type.IsChar()) &&
1500 src_type.IsInteger()));
1501 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001502 bool success =
1503 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
1504 if (!success) {
1505 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001506 }
jeffhaobdb76512011-09-07 11:43:16 -07001507 }
1508 }
1509 break;
1510 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001511 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001512 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001513 const RegType& return_type = GetMethodReturnType();
1514 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001515 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001516 } else {
1517 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001518 bool success = work_line_->VerifyRegisterType(dec_insn.vA, return_type);
1519 if (!success) {
1520 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001521 }
jeffhaobdb76512011-09-07 11:43:16 -07001522 }
1523 }
1524 break;
1525 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001526 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001527 const RegType& return_type = GetMethodReturnType();
1528 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001529 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001530 } else {
1531 /* return_type is the *expected* return type, not register value */
1532 DCHECK(!return_type.IsZero());
1533 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08001534 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07001535 // Disallow returning uninitialized values and verify that the reference in vAA is an
1536 // instance of the "return_type"
1537 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001538 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001539 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001540 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1541 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001542 }
1543 }
1544 }
1545 break;
1546
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001547 /* could be boolean, int, float, or a null reference */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001548 case Instruction::CONST_4:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001549 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001550 reg_types_.FromCat1Const(static_cast<int32_t>(dec_insn.vB << 28) >> 28, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001551 break;
jeffhaobdb76512011-09-07 11:43:16 -07001552 case Instruction::CONST_16:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001553 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001554 reg_types_.FromCat1Const(static_cast<int16_t>(dec_insn.vB), true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001555 break;
jeffhaobdb76512011-09-07 11:43:16 -07001556 case Instruction::CONST:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001557 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const(dec_insn.vB, true));
jeffhaobdb76512011-09-07 11:43:16 -07001558 break;
1559 case Instruction::CONST_HIGH16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001560 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001561 reg_types_.FromCat1Const(dec_insn.vB << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001562 break;
jeffhaobdb76512011-09-07 11:43:16 -07001563 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001564 case Instruction::CONST_WIDE_16: {
1565 int64_t val = static_cast<int16_t>(dec_insn.vB);
1566 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1567 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1568 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001569 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001570 }
1571 case Instruction::CONST_WIDE_32: {
1572 int64_t val = static_cast<int32_t>(dec_insn.vB);
1573 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1574 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1575 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1576 break;
1577 }
1578 case Instruction::CONST_WIDE: {
1579 int64_t val = dec_insn.vB_wide;
1580 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1581 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1582 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1583 break;
1584 }
1585 case Instruction::CONST_WIDE_HIGH16: {
1586 int64_t val = static_cast<uint64_t>(dec_insn.vB) << 48;
1587 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1588 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1589 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1590 break;
1591 }
jeffhaobdb76512011-09-07 11:43:16 -07001592 case Instruction::CONST_STRING:
1593 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001594 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001595 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001596 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001597 // Get type from instruction if unresolved then we need an access check
1598 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08001599 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001600 // Register holds class, ie its type is class, on error it will hold Conflict.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001601 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogersb4903572012-10-11 11:52:56 -07001602 res_type.IsConflict() ? res_type
1603 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001604 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001605 }
jeffhaobdb76512011-09-07 11:43:16 -07001606 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001607 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001608 break;
1609 case Instruction::MONITOR_EXIT:
1610 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001611 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001612 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001613 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001614 * to the need to handle asynchronous exceptions, a now-deprecated
1615 * feature that Dalvik doesn't support.)
1616 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001617 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001618 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001619 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001620 * structured locking checks are working, the former would have
1621 * failed on the -enter instruction, and the latter is impossible.
1622 *
1623 * This is fortunate, because issue 3221411 prevents us from
1624 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001625 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001626 * some catch blocks (which will show up as "dead" code when
1627 * we skip them here); if we can't, then the code path could be
1628 * "live" so we still need to check it.
1629 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001630 opcode_flags &= ~Instruction::kThrow;
1631 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001632 break;
1633
Ian Rogers28ad40d2011-10-27 15:19:26 -07001634 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001635 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001636 /*
1637 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1638 * could be a "upcast" -- not expected, so we don't try to address it.)
1639 *
1640 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001641 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001642 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001643 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001644 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001645 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001646 if (res_type.IsConflict()) {
1647 DCHECK_NE(failures_.size(), 0U);
1648 if (!is_checkcast) {
1649 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
1650 }
1651 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001652 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001653 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1654 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001655 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001656 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001657 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001658 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001659 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07001660 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001661 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001662 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001663 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001664 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001665 }
jeffhaobdb76512011-09-07 11:43:16 -07001666 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001667 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001668 }
1669 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001670 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001671 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001672 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001673 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001674 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001675 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001676 }
1677 }
1678 break;
1679 }
1680 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001681 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001682 if (res_type.IsConflict()) {
1683 DCHECK_NE(failures_.size(), 0U);
1684 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001685 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001686 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1687 // can't create an instance of an interface or abstract class */
1688 if (!res_type.IsInstantiableTypes()) {
1689 Fail(VERIFY_ERROR_INSTANTIATION)
1690 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001691 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001692 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001693 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1694 // Any registers holding previous allocations from this address that have not yet been
1695 // initialized must be marked invalid.
1696 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1697 // add the new uninitialized reference to the register state
1698 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001699 break;
1700 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001701 case Instruction::NEW_ARRAY:
1702 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001703 break;
1704 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08001705 VerifyNewArray(dec_insn, true, false);
1706 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001707 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001708 case Instruction::FILLED_NEW_ARRAY_RANGE:
1709 VerifyNewArray(dec_insn, true, true);
1710 just_set_result = true; // Filled new array range sets result register
1711 break;
jeffhaobdb76512011-09-07 11:43:16 -07001712 case Instruction::CMPL_FLOAT:
1713 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001714 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001715 break;
1716 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001717 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001718 break;
1719 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001720 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001721 break;
1722 case Instruction::CMPL_DOUBLE:
1723 case Instruction::CMPG_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001724 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vB, reg_types_.DoubleLo(),
1725 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001726 break;
1727 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001728 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vC, reg_types_.DoubleLo(),
1729 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001730 break;
1731 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001732 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001733 break;
1734 case Instruction::CMP_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001735 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vB, reg_types_.LongLo(),
1736 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001737 break;
1738 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001739 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vC, reg_types_.LongLo(),
1740 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001741 break;
1742 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001743 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001744 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001745 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001746 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersb4903572012-10-11 11:52:56 -07001747 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001748 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001749 }
1750 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001751 }
jeffhaobdb76512011-09-07 11:43:16 -07001752 case Instruction::GOTO:
1753 case Instruction::GOTO_16:
1754 case Instruction::GOTO_32:
1755 /* no effect on or use of registers */
1756 break;
1757
1758 case Instruction::PACKED_SWITCH:
1759 case Instruction::SPARSE_SWITCH:
1760 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001761 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001762 break;
1763
Ian Rogersd81871c2011-10-03 13:57:23 -07001764 case Instruction::FILL_ARRAY_DATA: {
1765 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001766 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08001767 /* array_type can be null if the reg type is Zero */
1768 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001769 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001770 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001771 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001772 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1773 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001774 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001775 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1776 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001777 } else {
jeffhao457cc512012-02-02 16:55:13 -08001778 // Now verify if the element width in the table matches the element width declared in
1779 // the array
1780 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1781 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001782 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001783 } else {
1784 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1785 // Since we don't compress the data in Dex, expect to see equal width of data stored
1786 // in the table and expected from the array class.
1787 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001788 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1789 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001790 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001791 }
1792 }
jeffhaobdb76512011-09-07 11:43:16 -07001793 }
1794 }
1795 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001796 }
jeffhaobdb76512011-09-07 11:43:16 -07001797 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001798 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001799 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1800 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001801 bool mismatch = false;
1802 if (reg_type1.IsZero()) { // zero then integral or reference expected
1803 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1804 } else if (reg_type1.IsReferenceTypes()) { // both references?
1805 mismatch = !reg_type2.IsReferenceTypes();
1806 } else { // both integral?
1807 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1808 }
1809 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001810 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1811 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001812 }
1813 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001814 }
jeffhaobdb76512011-09-07 11:43:16 -07001815 case Instruction::IF_LT:
1816 case Instruction::IF_GE:
1817 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001818 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001819 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1820 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001821 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001822 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1823 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001824 }
1825 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001826 }
jeffhaobdb76512011-09-07 11:43:16 -07001827 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001828 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001829 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001830 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001831 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001832 }
jeffhaobdb76512011-09-07 11:43:16 -07001833 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001834 }
jeffhaobdb76512011-09-07 11:43:16 -07001835 case Instruction::IF_LTZ:
1836 case Instruction::IF_GEZ:
1837 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001838 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001839 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001840 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001841 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1842 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001843 }
jeffhaobdb76512011-09-07 11:43:16 -07001844 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001845 }
jeffhaobdb76512011-09-07 11:43:16 -07001846 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001847 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
1848 break;
jeffhaobdb76512011-09-07 11:43:16 -07001849 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001850 VerifyAGet(dec_insn, reg_types_.Byte(), true);
1851 break;
jeffhaobdb76512011-09-07 11:43:16 -07001852 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07001853 VerifyAGet(dec_insn, reg_types_.Char(), true);
1854 break;
jeffhaobdb76512011-09-07 11:43:16 -07001855 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001856 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001857 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001858 case Instruction::AGET:
1859 VerifyAGet(dec_insn, reg_types_.Integer(), true);
1860 break;
jeffhaobdb76512011-09-07 11:43:16 -07001861 case Instruction::AGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001862 VerifyAGet(dec_insn, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001863 break;
1864 case Instruction::AGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001865 VerifyAGet(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001866 break;
1867
Ian Rogersd81871c2011-10-03 13:57:23 -07001868 case Instruction::APUT_BOOLEAN:
1869 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
1870 break;
1871 case Instruction::APUT_BYTE:
1872 VerifyAPut(dec_insn, reg_types_.Byte(), true);
1873 break;
1874 case Instruction::APUT_CHAR:
1875 VerifyAPut(dec_insn, reg_types_.Char(), true);
1876 break;
1877 case Instruction::APUT_SHORT:
1878 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001879 break;
1880 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001881 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001882 break;
1883 case Instruction::APUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001884 VerifyAPut(dec_insn, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001885 break;
1886 case Instruction::APUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001887 VerifyAPut(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001888 break;
1889
jeffhaobdb76512011-09-07 11:43:16 -07001890 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001891 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001892 break;
jeffhaobdb76512011-09-07 11:43:16 -07001893 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001894 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001895 break;
jeffhaobdb76512011-09-07 11:43:16 -07001896 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001897 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001898 break;
jeffhaobdb76512011-09-07 11:43:16 -07001899 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001900 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001901 break;
1902 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001903 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001904 break;
1905 case Instruction::IGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001906 VerifyISGet(dec_insn, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001907 break;
1908 case Instruction::IGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001909 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001910 break;
jeffhaobdb76512011-09-07 11:43:16 -07001911
Ian Rogersd81871c2011-10-03 13:57:23 -07001912 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001913 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001914 break;
1915 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001916 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001917 break;
1918 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001919 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001920 break;
1921 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001922 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001923 break;
1924 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001925 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001926 break;
1927 case Instruction::IPUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001928 VerifyISPut(dec_insn, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001929 break;
jeffhaobdb76512011-09-07 11:43:16 -07001930 case Instruction::IPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001931 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001932 break;
1933
jeffhaobdb76512011-09-07 11:43:16 -07001934 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001935 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001936 break;
jeffhaobdb76512011-09-07 11:43:16 -07001937 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001938 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001939 break;
jeffhaobdb76512011-09-07 11:43:16 -07001940 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001941 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001942 break;
jeffhaobdb76512011-09-07 11:43:16 -07001943 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001944 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001945 break;
1946 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001947 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001948 break;
1949 case Instruction::SGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001950 VerifyISGet(dec_insn, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001951 break;
1952 case Instruction::SGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001953 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001954 break;
1955
1956 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001957 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001958 break;
1959 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001960 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001961 break;
1962 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001963 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001964 break;
1965 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001966 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001967 break;
1968 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001969 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001970 break;
1971 case Instruction::SPUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001972 VerifyISPut(dec_insn, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001973 break;
1974 case Instruction::SPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001975 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001976 break;
1977
1978 case Instruction::INVOKE_VIRTUAL:
1979 case Instruction::INVOKE_VIRTUAL_RANGE:
1980 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001981 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001982 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
1983 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
1984 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
1985 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001986 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL,
1987 is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001988 const char* descriptor;
1989 if (called_method == NULL) {
1990 uint32_t method_idx = dec_insn.vB;
1991 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1992 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1993 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1994 } else {
1995 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07001996 }
Ian Rogersb4903572012-10-11 11:52:56 -07001997 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001998 if (!return_type.IsLowHalf()) {
1999 work_line_->SetResultRegisterType(return_type);
2000 } else {
2001 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2002 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002003 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002004 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002005 }
jeffhaobdb76512011-09-07 11:43:16 -07002006 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002007 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002008 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002009 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT,
2010 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002011 const char* return_type_descriptor;
2012 bool is_constructor;
2013 if (called_method == NULL) {
2014 uint32_t method_idx = dec_insn.vB;
2015 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2016 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
2017 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2018 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2019 } else {
2020 is_constructor = called_method->IsConstructor();
2021 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2022 }
2023 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002024 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002025 * Some additional checks when calling a constructor. We know from the invocation arg check
2026 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2027 * that to require that called_method->klass is the same as this->klass or this->super,
2028 * allowing the latter only if the "this" argument is the same as the "this" argument to
2029 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002030 */
jeffhaob57e9522012-04-26 18:08:21 -07002031 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2032 if (this_type.IsConflict()) // failure.
2033 break;
jeffhaobdb76512011-09-07 11:43:16 -07002034
jeffhaob57e9522012-04-26 18:08:21 -07002035 /* no null refs allowed (?) */
2036 if (this_type.IsZero()) {
2037 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2038 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002039 }
jeffhaob57e9522012-04-26 18:08:21 -07002040
2041 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002042 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2043 // TODO: re-enable constructor type verification
2044 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002045 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002046 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2047 // break;
2048 // }
jeffhaob57e9522012-04-26 18:08:21 -07002049
2050 /* arg must be an uninitialized reference */
2051 if (!this_type.IsUninitializedTypes()) {
2052 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2053 << this_type;
2054 break;
2055 }
2056
2057 /*
2058 * Replace the uninitialized reference with an initialized one. We need to do this for all
2059 * registers that have the same object instance in them, not just the "this" register.
2060 */
2061 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002062 }
Ian Rogersb4903572012-10-11 11:52:56 -07002063 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2064 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002065 if (!return_type.IsLowHalf()) {
2066 work_line_->SetResultRegisterType(return_type);
2067 } else {
2068 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2069 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002070 just_set_result = true;
2071 break;
2072 }
2073 case Instruction::INVOKE_STATIC:
2074 case Instruction::INVOKE_STATIC_RANGE: {
2075 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002076 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002077 const char* descriptor;
2078 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002079 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002080 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2081 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002082 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002083 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002084 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002085 }
Ian Rogersb4903572012-10-11 11:52:56 -07002086 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002087 if (!return_type.IsLowHalf()) {
2088 work_line_->SetResultRegisterType(return_type);
2089 } else {
2090 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2091 }
jeffhaobdb76512011-09-07 11:43:16 -07002092 just_set_result = true;
2093 }
2094 break;
jeffhaobdb76512011-09-07 11:43:16 -07002095 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002096 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002097 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002098 mirror::AbstractMethod* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002099 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002100 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002101 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2102 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2103 << PrettyMethod(abs_method) << "'";
2104 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002105 }
Ian Rogers0d604842012-04-16 14:50:24 -07002106 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002107 /* Get the type of the "this" arg, which should either be a sub-interface of called
2108 * interface or Object (see comments in RegType::JoinClass).
2109 */
2110 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2111 if (this_type.IsZero()) {
2112 /* null pointer always passes (and always fails at runtime) */
2113 } else {
2114 if (this_type.IsUninitializedTypes()) {
2115 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2116 << this_type;
2117 break;
2118 }
2119 // In the past we have tried to assert that "called_interface" is assignable
2120 // from "this_type.GetClass()", however, as we do an imprecise Join
2121 // (RegType::JoinClass) we don't have full information on what interfaces are
2122 // implemented by "this_type". For example, two classes may implement the same
2123 // interfaces and have a common parent that doesn't implement the interface. The
2124 // join will set "this_type" to the parent class and a test that this implements
2125 // the interface will incorrectly fail.
2126 }
2127 /*
2128 * We don't have an object instance, so we can't find the concrete method. However, all of
2129 * the type information is in the abstract method, so we're good.
2130 */
2131 const char* descriptor;
2132 if (abs_method == NULL) {
2133 uint32_t method_idx = dec_insn.vB;
2134 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2135 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2136 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2137 } else {
2138 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2139 }
Ian Rogersb4903572012-10-11 11:52:56 -07002140 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002141 if (!return_type.IsLowHalf()) {
2142 work_line_->SetResultRegisterType(return_type);
2143 } else {
2144 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2145 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002146 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002147 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002148 }
jeffhaobdb76512011-09-07 11:43:16 -07002149 case Instruction::NEG_INT:
2150 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002151 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002152 break;
2153 case Instruction::NEG_LONG:
2154 case Instruction::NOT_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002155 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2156 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002157 break;
2158 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002159 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002160 break;
2161 case Instruction::NEG_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002162 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2163 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002164 break;
2165 case Instruction::INT_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002166 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2167 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002168 break;
2169 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002170 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002171 break;
2172 case Instruction::INT_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002173 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2174 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002175 break;
2176 case Instruction::LONG_TO_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002177 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Integer(),
2178 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002179 break;
2180 case Instruction::LONG_TO_FLOAT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002181 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Float(),
2182 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002183 break;
2184 case Instruction::LONG_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002185 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2186 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002187 break;
2188 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002189 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002190 break;
2191 case Instruction::FLOAT_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002192 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2193 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002194 break;
2195 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002196 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2197 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002198 break;
2199 case Instruction::DOUBLE_TO_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002200 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Integer(),
2201 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002202 break;
2203 case Instruction::DOUBLE_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002204 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2205 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002206 break;
2207 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002208 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Float(),
2209 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002210 break;
2211 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002212 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002213 break;
2214 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002215 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002216 break;
2217 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002218 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002219 break;
2220
2221 case Instruction::ADD_INT:
2222 case Instruction::SUB_INT:
2223 case Instruction::MUL_INT:
2224 case Instruction::REM_INT:
2225 case Instruction::DIV_INT:
2226 case Instruction::SHL_INT:
2227 case Instruction::SHR_INT:
2228 case Instruction::USHR_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002229 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(),
2230 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002231 break;
2232 case Instruction::AND_INT:
2233 case Instruction::OR_INT:
2234 case Instruction::XOR_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002235 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(),
2236 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002237 break;
2238 case Instruction::ADD_LONG:
2239 case Instruction::SUB_LONG:
2240 case Instruction::MUL_LONG:
2241 case Instruction::DIV_LONG:
2242 case Instruction::REM_LONG:
2243 case Instruction::AND_LONG:
2244 case Instruction::OR_LONG:
2245 case Instruction::XOR_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002246 work_line_->CheckBinaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2247 reg_types_.LongLo(), reg_types_.LongHi(),
2248 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002249 break;
2250 case Instruction::SHL_LONG:
2251 case Instruction::SHR_LONG:
2252 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002253 /* shift distance is Int, making these different from other binary operations */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002254 work_line_->CheckBinaryOpWideShift(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2255 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002256 break;
2257 case Instruction::ADD_FLOAT:
2258 case Instruction::SUB_FLOAT:
2259 case Instruction::MUL_FLOAT:
2260 case Instruction::DIV_FLOAT:
2261 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002262 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002263 break;
2264 case Instruction::ADD_DOUBLE:
2265 case Instruction::SUB_DOUBLE:
2266 case Instruction::MUL_DOUBLE:
2267 case Instruction::DIV_DOUBLE:
2268 case Instruction::REM_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002269 work_line_->CheckBinaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2270 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2271 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002272 break;
2273 case Instruction::ADD_INT_2ADDR:
2274 case Instruction::SUB_INT_2ADDR:
2275 case Instruction::MUL_INT_2ADDR:
2276 case Instruction::REM_INT_2ADDR:
2277 case Instruction::SHL_INT_2ADDR:
2278 case Instruction::SHR_INT_2ADDR:
2279 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002280 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002281 break;
2282 case Instruction::AND_INT_2ADDR:
2283 case Instruction::OR_INT_2ADDR:
2284 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002285 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002286 break;
2287 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002288 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002289 break;
2290 case Instruction::ADD_LONG_2ADDR:
2291 case Instruction::SUB_LONG_2ADDR:
2292 case Instruction::MUL_LONG_2ADDR:
2293 case Instruction::DIV_LONG_2ADDR:
2294 case Instruction::REM_LONG_2ADDR:
2295 case Instruction::AND_LONG_2ADDR:
2296 case Instruction::OR_LONG_2ADDR:
2297 case Instruction::XOR_LONG_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002298 work_line_->CheckBinaryOp2addrWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2299 reg_types_.LongLo(), reg_types_.LongHi(),
2300 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002301 break;
2302 case Instruction::SHL_LONG_2ADDR:
2303 case Instruction::SHR_LONG_2ADDR:
2304 case Instruction::USHR_LONG_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002305 work_line_->CheckBinaryOp2addrWideShift(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2306 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002307 break;
2308 case Instruction::ADD_FLOAT_2ADDR:
2309 case Instruction::SUB_FLOAT_2ADDR:
2310 case Instruction::MUL_FLOAT_2ADDR:
2311 case Instruction::DIV_FLOAT_2ADDR:
2312 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002313 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002314 break;
2315 case Instruction::ADD_DOUBLE_2ADDR:
2316 case Instruction::SUB_DOUBLE_2ADDR:
2317 case Instruction::MUL_DOUBLE_2ADDR:
2318 case Instruction::DIV_DOUBLE_2ADDR:
2319 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002320 work_line_->CheckBinaryOp2addrWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2321 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2322 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002323 break;
2324 case Instruction::ADD_INT_LIT16:
2325 case Instruction::RSUB_INT:
2326 case Instruction::MUL_INT_LIT16:
2327 case Instruction::DIV_INT_LIT16:
2328 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002329 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002330 break;
2331 case Instruction::AND_INT_LIT16:
2332 case Instruction::OR_INT_LIT16:
2333 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002334 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002335 break;
2336 case Instruction::ADD_INT_LIT8:
2337 case Instruction::RSUB_INT_LIT8:
2338 case Instruction::MUL_INT_LIT8:
2339 case Instruction::DIV_INT_LIT8:
2340 case Instruction::REM_INT_LIT8:
2341 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002342 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002343 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002344 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002345 break;
2346 case Instruction::AND_INT_LIT8:
2347 case Instruction::OR_INT_LIT8:
2348 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002349 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002350 break;
2351
Ian Rogersd81871c2011-10-03 13:57:23 -07002352 /* These should never appear during verification. */
jeffhao9a4f0032012-08-30 16:17:40 -07002353 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002354 case Instruction::UNUSED_EE:
2355 case Instruction::UNUSED_EF:
2356 case Instruction::UNUSED_F2:
2357 case Instruction::UNUSED_F3:
2358 case Instruction::UNUSED_F4:
2359 case Instruction::UNUSED_F5:
2360 case Instruction::UNUSED_F6:
2361 case Instruction::UNUSED_F7:
2362 case Instruction::UNUSED_F8:
2363 case Instruction::UNUSED_F9:
2364 case Instruction::UNUSED_FA:
2365 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002366 case Instruction::UNUSED_F0:
2367 case Instruction::UNUSED_F1:
2368 case Instruction::UNUSED_E3:
2369 case Instruction::UNUSED_E8:
2370 case Instruction::UNUSED_E7:
2371 case Instruction::UNUSED_E4:
2372 case Instruction::UNUSED_E9:
2373 case Instruction::UNUSED_FC:
2374 case Instruction::UNUSED_E5:
2375 case Instruction::UNUSED_EA:
2376 case Instruction::UNUSED_FD:
2377 case Instruction::UNUSED_E6:
2378 case Instruction::UNUSED_EB:
2379 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002380 case Instruction::UNUSED_3E:
2381 case Instruction::UNUSED_3F:
2382 case Instruction::UNUSED_40:
2383 case Instruction::UNUSED_41:
2384 case Instruction::UNUSED_42:
2385 case Instruction::UNUSED_43:
2386 case Instruction::UNUSED_73:
2387 case Instruction::UNUSED_79:
2388 case Instruction::UNUSED_7A:
2389 case Instruction::UNUSED_EC:
2390 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002391 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002392 break;
2393
2394 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002395 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002396 * complain if an instruction is missing (which is desirable).
2397 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002398 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002399
Ian Rogersad0b3a32012-04-16 14:50:24 -07002400 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002401 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002402 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002403 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002404 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002405 /* immediate failure, reject class */
2406 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2407 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002408 } else if (have_pending_runtime_throw_failure_) {
2409 /* slow path will throw, mark following code as unreachable */
2410 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002411 }
jeffhaobdb76512011-09-07 11:43:16 -07002412 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002413 * If we didn't just set the result register, clear it out. This ensures that you can only use
2414 * "move-result" immediately after the result is set. (We could check this statically, but it's
2415 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002416 */
2417 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002418 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002419 }
2420
jeffhaoa0a764a2011-09-16 10:43:38 -07002421 /* Handle "continue". Tag the next consecutive instruction. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002422 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers776ac1f2012-04-13 23:36:36 -07002423 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
Ian Rogersd81871c2011-10-03 13:57:23 -07002424 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
jeffhaod5347e02012-03-22 17:25:05 -07002425 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002426 return false;
2427 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002428 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2429 // next instruction isn't one.
jeffhaod5347e02012-03-22 17:25:05 -07002430 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002431 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002432 }
2433 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2434 if (next_line != NULL) {
2435 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2436 // needed.
2437 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002438 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002439 }
jeffhaobdb76512011-09-07 11:43:16 -07002440 } else {
2441 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002442 * We're not recording register data for the next instruction, so we don't know what the prior
2443 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002444 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002445 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002446 }
2447 }
2448
2449 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002450 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002451 *
2452 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002453 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002454 * somebody could get a reference field, check it for zero, and if the
2455 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002456 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002457 * that, and will reject the code.
2458 *
2459 * TODO: avoid re-fetching the branch target
2460 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002461 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002462 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002463 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002464 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002465 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002466 return false;
2467 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002468 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002469 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002470 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002471 }
jeffhaobdb76512011-09-07 11:43:16 -07002472 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002473 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002474 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002475 }
jeffhaobdb76512011-09-07 11:43:16 -07002476 }
2477
2478 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002479 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002480 *
2481 * We've already verified that the table is structurally sound, so we
2482 * just need to walk through and tag the targets.
2483 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002484 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002485 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2486 const uint16_t* switch_insns = insns + offset_to_switch;
2487 int switch_count = switch_insns[1];
2488 int offset_to_targets, targ;
2489
2490 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2491 /* 0 = sig, 1 = count, 2/3 = first key */
2492 offset_to_targets = 4;
2493 } else {
2494 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002495 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002496 offset_to_targets = 2 + 2 * switch_count;
2497 }
2498
2499 /* verify each switch target */
2500 for (targ = 0; targ < switch_count; targ++) {
2501 int offset;
2502 uint32_t abs_offset;
2503
2504 /* offsets are 32-bit, and only partly endian-swapped */
2505 offset = switch_insns[offset_to_targets + targ * 2] |
2506 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002507 abs_offset = work_insn_idx_ + offset;
2508 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002509 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002510 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002511 }
2512 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002513 return false;
2514 }
2515 }
2516
2517 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002518 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2519 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002520 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002521 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002522 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002523 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002524
Ian Rogers0571d352011-11-03 19:51:38 -07002525 for (; iterator.HasNext(); iterator.Next()) {
2526 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002527 within_catch_all = true;
2528 }
jeffhaobdb76512011-09-07 11:43:16 -07002529 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002530 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2531 * "work_regs", because at runtime the exception will be thrown before the instruction
2532 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002533 */
Ian Rogers0571d352011-11-03 19:51:38 -07002534 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002535 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002536 }
jeffhaobdb76512011-09-07 11:43:16 -07002537 }
2538
2539 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002540 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2541 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002542 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002543 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002544 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002545 * The state in work_line reflects the post-execution state. If the current instruction is a
2546 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002547 * it will do so before grabbing the lock).
2548 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002549 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002550 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002551 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002552 return false;
2553 }
2554 }
2555 }
2556
jeffhaod1f0fde2011-09-08 17:25:33 -07002557 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002558 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002559 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002560 return false;
2561 }
jeffhaobdb76512011-09-07 11:43:16 -07002562 }
2563
2564 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002565 * Update start_guess. Advance to the next instruction of that's
2566 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002567 * neither of those exists we're in a return or throw; leave start_guess
2568 * alone and let the caller sort it out.
2569 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002570 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002571 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002572 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002573 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002574 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002575 }
2576
Ian Rogersd81871c2011-10-03 13:57:23 -07002577 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2578 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002579
2580 return true;
2581}
2582
Ian Rogers776ac1f2012-04-13 23:36:36 -07002583const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002584 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002585 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002586 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002587 const RegType& result =
Ian Rogersb4903572012-10-11 11:52:56 -07002588 klass != NULL ? reg_types_.FromClass(klass, klass->IsFinal())
2589 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002590 if (result.IsConflict()) {
2591 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2592 << "' in " << referrer;
2593 return result;
2594 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002595 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002596 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002597 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002598 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002599 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002600 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002601 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002602 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002603 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002604 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002605}
2606
Ian Rogers776ac1f2012-04-13 23:36:36 -07002607const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002608 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002609 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002610 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002611 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2612 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002613 CatchHandlerIterator iterator(handlers_ptr);
2614 for (; iterator.HasNext(); iterator.Next()) {
2615 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2616 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002617 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002618 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002619 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002620 if (common_super == NULL) {
2621 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2622 // as that is caught at runtime
2623 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002624 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002625 // We don't know enough about the type and the common path merge will result in
2626 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002627 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002628 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002629 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002630 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002631 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002632 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002633 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002634 }
2635 }
2636 }
2637 }
Ian Rogers0571d352011-11-03 19:51:38 -07002638 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002639 }
2640 }
2641 if (common_super == NULL) {
2642 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002643 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002644 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002645 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002646 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002647}
2648
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002649mirror::AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
2650 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002651 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002652 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002653 if (klass_type.IsConflict()) {
2654 std::string append(" in attempt to access method ");
2655 append += dex_file_->GetMethodName(method_id);
2656 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002657 return NULL;
2658 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002659 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002660 return NULL; // Can't resolve Class so no more to do here
2661 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002662 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002663 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002664 mirror::AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002665 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002666 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002667 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002668
2669 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002670 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002671 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002672 res_method = klass->FindInterfaceMethod(name, signature);
2673 } else {
2674 res_method = klass->FindVirtualMethod(name, signature);
2675 }
2676 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002677 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002678 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002679 // If a virtual or interface method wasn't found with the expected type, look in
2680 // the direct methods. This can happen when the wrong invoke type is used or when
2681 // a class has changed, and will be flagged as an error in later checks.
2682 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2683 res_method = klass->FindDirectMethod(name, signature);
2684 }
2685 if (res_method == NULL) {
2686 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2687 << PrettyDescriptor(klass) << "." << name
2688 << " " << signature;
2689 return NULL;
2690 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002691 }
2692 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002693 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2694 // enforce them here.
2695 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002696 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2697 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002698 return NULL;
2699 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002700 // Disallow any calls to class initializers.
2701 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002702 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2703 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002704 return NULL;
2705 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002706 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002707 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002708 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002709 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002710 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002711 }
jeffhaode0d9c92012-02-27 13:58:13 -08002712 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2713 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002714 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2715 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002716 return NULL;
2717 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002718 // Check that interface methods match interface classes.
2719 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2720 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2721 << " is in an interface class " << PrettyClass(klass);
2722 return NULL;
2723 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2724 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2725 << " is in a non-interface class " << PrettyClass(klass);
2726 return NULL;
2727 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002728 // See if the method type implied by the invoke instruction matches the access flags for the
2729 // target method.
2730 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2731 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2732 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2733 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002734 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2735 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002736 return NULL;
2737 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002738 return res_method;
2739}
2740
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002741mirror::AbstractMethod* MethodVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
2742 MethodType method_type, bool is_range,
2743 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002744 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2745 // we're making.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002746 mirror::AbstractMethod* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002747 if (res_method == NULL) { // error or class is unresolved
2748 return NULL;
2749 }
2750
Ian Rogersd81871c2011-10-03 13:57:23 -07002751 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2752 // has a vtable entry for the target method.
2753 if (is_super) {
2754 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002755 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002756 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002757 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002758 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002759 << " to super " << PrettyMethod(res_method);
2760 return NULL;
2761 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002762 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002763 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002764 MethodHelper mh(res_method);
2765 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002766 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002767 << " to super " << super
2768 << "." << mh.GetName()
2769 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002770 return NULL;
2771 }
2772 }
2773 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2774 // match the call to the signature. Also, we might might be calling through an abstract method
2775 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08002776 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002777 /* caught by static verifier */
2778 DCHECK(is_range || expected_args <= 5);
2779 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002780 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002781 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2782 return NULL;
2783 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002784
jeffhaobdb76512011-09-07 11:43:16 -07002785 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002786 * Check the "this" argument, which must be an instance of the class that declared the method.
2787 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2788 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002789 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002790 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002791 if (!res_method->IsStatic()) {
2792 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002793 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002794 return NULL;
2795 }
2796 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002797 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002798 return NULL;
2799 }
2800 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002801 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogersb4903572012-10-11 11:52:56 -07002802 const RegType& res_method_class = reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogers9074b992011-10-26 17:41:55 -07002803 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002804 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002805 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002806 return NULL;
2807 }
2808 }
2809 actual_args++;
2810 }
2811 /*
2812 * Process the target method's signature. This signature may or may not
2813 * have been verified, so we can't assume it's properly formed.
2814 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002815 MethodHelper mh(res_method);
2816 const DexFile::TypeList* params = mh.GetParameterTypeList();
2817 size_t params_size = params == NULL ? 0 : params->Size();
2818 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002819 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002820 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002821 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2822 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002823 return NULL;
2824 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002825 const char* descriptor =
2826 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2827 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002828 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002829 << " missing signature component";
2830 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002831 }
Ian Rogersb4903572012-10-11 11:52:56 -07002832 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002833 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002834 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002835 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002836 }
2837 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2838 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002839 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002840 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002841 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002842 return NULL;
2843 } else {
2844 return res_method;
2845 }
2846}
2847
Ian Rogers776ac1f2012-04-13 23:36:36 -07002848void MethodVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08002849 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002850 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002851 if (res_type.IsConflict()) { // bad class
2852 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002853 } else {
2854 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2855 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002856 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002857 } else if (!is_filled) {
2858 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002859 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002860 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002861 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002862 } else {
2863 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2864 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002865 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002866 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002867 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002868 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002869 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002870 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002871 return;
2872 }
2873 }
2874 // filled-array result goes into "result" register
2875 work_line_->SetResultRegisterType(res_type);
2876 }
2877 }
2878}
2879
Ian Rogers776ac1f2012-04-13 23:36:36 -07002880void MethodVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002881 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002882 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002883 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002884 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002885 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002886 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002887 if (array_type.IsZero()) {
2888 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2889 // instruction type. TODO: have a proper notion of bottom here.
2890 if (!is_primitive || insn_type.IsCategory1Types()) {
2891 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08002892 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002893 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002894 // Category 2
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002895 work_line_->SetRegisterTypeWide(dec_insn.vA, reg_types_.FromCat2ConstLo(0, false),
2896 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08002897 }
jeffhaofc3144e2012-02-01 17:21:15 -08002898 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002899 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002900 } else {
2901 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002902 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002903 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002904 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002905 << " source for aget-object";
2906 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002907 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002908 << " source for category 1 aget";
2909 } else if (is_primitive && !insn_type.Equals(component_type) &&
2910 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002911 (insn_type.IsLong() && component_type.IsDouble()))) {
2912 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
2913 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002914 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002915 // Use knowledge of the field type which is stronger than the type inferred from the
2916 // instruction, which can't differentiate object types and ints from floats, longs from
2917 // doubles.
2918 if (!component_type.IsLowHalf()) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002919 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002920 } else {
2921 work_line_->SetRegisterTypeWide(dec_insn.vA, component_type,
2922 component_type.HighHalf(&reg_types_));
2923 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002924 }
2925 }
2926 }
2927}
2928
Ian Rogers776ac1f2012-04-13 23:36:36 -07002929void MethodVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002930 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002931 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002932 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002933 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002934 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002935 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002936 if (array_type.IsZero()) {
2937 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2938 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002939 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002940 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002941 } else {
2942 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002943 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002944 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002945 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002946 << " source for aput-object";
2947 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002948 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002949 << " source for category 1 aput";
2950 } else if (is_primitive && !insn_type.Equals(component_type) &&
2951 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2952 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002953 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002954 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002955 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002956 // The instruction agrees with the type of array, confirm the value to be stored does too
2957 // Note: we use the instruction type (rather than the component type) for aput-object as
2958 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08002959 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002960 }
2961 }
2962 }
2963}
2964
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002965mirror::Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002966 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2967 // Check access to class
2968 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002969 if (klass_type.IsConflict()) { // bad class
2970 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
2971 field_idx, dex_file_->GetFieldName(field_id),
2972 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002973 return NULL;
2974 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002975 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002976 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08002977 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002978 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07002979 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002980 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002981 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
2982 << dex_file_->GetFieldName(field_id) << ") in "
2983 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002984 DCHECK(Thread::Current()->IsExceptionPending());
2985 Thread::Current()->ClearException();
2986 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002987 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2988 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002989 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002990 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002991 return NULL;
2992 } else if (!field->IsStatic()) {
2993 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
2994 return NULL;
2995 } else {
2996 return field;
2997 }
2998}
2999
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003000mirror::Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003001 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3002 // Check access to class
3003 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003004 if (klass_type.IsConflict()) {
3005 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3006 field_idx, dex_file_->GetFieldName(field_id),
3007 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003008 return NULL;
3009 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003010 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003011 return NULL; // Can't resolve Class so no more to do here
3012 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003013 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07003014 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003015 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003016 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
3017 << dex_file_->GetFieldName(field_id) << ") in "
3018 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003019 DCHECK(Thread::Current()->IsExceptionPending());
3020 Thread::Current()->ClearException();
3021 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003022 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3023 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003024 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003025 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003026 return NULL;
3027 } else if (field->IsStatic()) {
3028 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3029 << " to not be static";
3030 return NULL;
3031 } else if (obj_type.IsZero()) {
3032 // Cannot infer and check type, however, access will cause null pointer exception
3033 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003034 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003035 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersb4903572012-10-11 11:52:56 -07003036 const RegType& field_klass = reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003037 if (obj_type.IsUninitializedTypes() &&
3038 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3039 !field_klass.Equals(GetDeclaringClass()))) {
3040 // Field accesses through uninitialized references are only allowable for constructors where
3041 // the field is declared in this class
3042 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
3043 << " of a not fully initialized object within the context of "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003044 << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003045 return NULL;
3046 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3047 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3048 // of C1. For resolution to occur the declared class of the field must be compatible with
3049 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3050 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3051 << " from object of type " << obj_type;
3052 return NULL;
3053 } else {
3054 return field;
3055 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003056 }
3057}
3058
Ian Rogers776ac1f2012-04-13 23:36:36 -07003059void MethodVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003060 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003061 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003062 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003063 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003064 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003065 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003066 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003067 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003068 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003069 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003070 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003071 if (field != NULL) {
3072 descriptor = FieldHelper(field).GetTypeDescriptor();
3073 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003074 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003075 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3076 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3077 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07003078 }
Ian Rogersb4903572012-10-11 11:52:56 -07003079 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003080 if (is_primitive) {
3081 if (field_type.Equals(insn_type) ||
3082 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3083 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3084 // expected that read is of the correct primitive type or that int reads are reading
3085 // floats or long reads are reading doubles
3086 } else {
3087 // This is a global failure rather than a class change failure as the instructions and
3088 // the descriptors for the type should have been consistent within the same file at
3089 // compile time
3090 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3091 << " to be of type '" << insn_type
3092 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003093 return;
3094 }
3095 } else {
3096 if (!insn_type.IsAssignableFrom(field_type)) {
3097 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3098 << " to be compatible with type '" << insn_type
3099 << "' but found type '" << field_type
3100 << "' in get-object";
3101 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Conflict());
3102 return;
3103 }
3104 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003105 if (!field_type.IsLowHalf()) {
3106 work_line_->SetRegisterType(dec_insn.vA, field_type);
3107 } else {
3108 work_line_->SetRegisterTypeWide(dec_insn.vA, field_type, field_type.HighHalf(&reg_types_));
3109 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003110}
3111
Ian Rogers776ac1f2012-04-13 23:36:36 -07003112void MethodVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003113 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003114 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003115 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003116 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003117 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003118 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003119 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07003120 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003121 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003122 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003123 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003124 if (field != NULL) {
3125 descriptor = FieldHelper(field).GetTypeDescriptor();
3126 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003127 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003128 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3129 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3130 loader = class_loader_;
3131 }
Ian Rogersb4903572012-10-11 11:52:56 -07003132 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003133 if (field != NULL) {
3134 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3135 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3136 << " from other class " << GetDeclaringClass();
3137 return;
3138 }
3139 }
3140 if (is_primitive) {
3141 // Primitive field assignability rules are weaker than regular assignability rules
3142 bool instruction_compatible;
3143 bool value_compatible;
3144 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
3145 if (field_type.IsIntegralTypes()) {
3146 instruction_compatible = insn_type.IsIntegralTypes();
3147 value_compatible = value_type.IsIntegralTypes();
3148 } else if (field_type.IsFloat()) {
3149 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3150 value_compatible = value_type.IsFloatTypes();
3151 } else if (field_type.IsLong()) {
3152 instruction_compatible = insn_type.IsLong();
3153 value_compatible = value_type.IsLongTypes();
3154 } else if (field_type.IsDouble()) {
3155 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3156 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003157 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003158 instruction_compatible = false; // reference field with primitive store
3159 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003160 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003161 if (!instruction_compatible) {
3162 // This is a global failure rather than a class change failure as the instructions and
3163 // the descriptors for the type should have been consistent within the same file at
3164 // compile time
3165 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3166 << " to be of type '" << insn_type
3167 << "' but found type '" << field_type
3168 << "' in put";
3169 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003170 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003171 if (!value_compatible) {
3172 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
3173 << " of type " << value_type
3174 << " but expected " << field_type
3175 << " for store to " << PrettyField(field) << " in put";
3176 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003177 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003178 } else {
3179 if (!insn_type.IsAssignableFrom(field_type)) {
3180 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3181 << " to be compatible with type '" << insn_type
3182 << "' but found type '" << field_type
3183 << "' in put-object";
3184 return;
3185 }
3186 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003187 }
3188}
3189
Ian Rogers776ac1f2012-04-13 23:36:36 -07003190bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003191 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003192 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003193 return false;
3194 }
3195 return true;
3196}
3197
Ian Rogers776ac1f2012-04-13 23:36:36 -07003198bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003199 bool changed = true;
3200 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3201 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003202 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003203 * We haven't processed this instruction before, and we haven't touched the registers here, so
3204 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3205 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003206 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003207 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003208 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003209 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3210 if (gDebugVerify) {
3211 copy->CopyFromLine(target_line);
3212 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003213 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003214 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003215 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003216 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003217 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003218 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003219 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3220 << *copy.get() << " MERGE\n"
3221 << *merge_line << " ==\n"
3222 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003223 }
3224 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003225 if (changed) {
3226 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003227 }
3228 return true;
3229}
3230
Ian Rogers776ac1f2012-04-13 23:36:36 -07003231InsnFlags* MethodVerifier::CurrentInsnFlags() {
3232 return &insn_flags_[work_insn_idx_];
3233}
3234
Ian Rogersad0b3a32012-04-16 14:50:24 -07003235const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003236 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003237 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3238 uint16_t return_type_idx = proto_id.return_type_idx_;
3239 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003240 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003241}
3242
3243const RegType& MethodVerifier::GetDeclaringClass() {
3244 if (foo_method_ != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003245 mirror::Class* klass = foo_method_->GetDeclaringClass();
Ian Rogersb4903572012-10-11 11:52:56 -07003246 return reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003247 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003248 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003249 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogersb4903572012-10-11 11:52:56 -07003250 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003251 }
3252}
3253
Ian Rogers776ac1f2012-04-13 23:36:36 -07003254void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003255 size_t* log2_max_gc_pc) {
3256 size_t local_gc_points = 0;
3257 size_t max_insn = 0;
3258 size_t max_ref_reg = -1;
3259 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3260 if (insn_flags_[i].IsGcPoint()) {
3261 local_gc_points++;
3262 max_insn = i;
3263 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003264 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003265 }
3266 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003267 *gc_points = local_gc_points;
3268 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3269 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003270 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003271 i++;
3272 }
3273 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003274}
3275
Ian Rogers776ac1f2012-04-13 23:36:36 -07003276const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003277 size_t num_entries, ref_bitmap_bits, pc_bits;
3278 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3279 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003280 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
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 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003284 return NULL;
3285 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003286 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3287 // There are 2 bytes to encode the number of entries
3288 if (num_entries >= 65536) {
3289 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003290 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003291 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003292 return NULL;
3293 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003294 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003295 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003296 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003297 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003298 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003299 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003300 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003301 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003302 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003303 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003304 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003305 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3306 return NULL;
3307 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003308 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003309 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003310 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003311 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003312 return NULL;
3313 }
3314 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003315 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3316 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003317 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003318 table->push_back(num_entries & 0xFF);
3319 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003320 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003321 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3322 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003323 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003324 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003325 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003326 }
3327 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003328 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003329 }
3330 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003331 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003332 return table;
3333}
jeffhaoa0a764a2011-09-16 10:43:38 -07003334
Ian Rogers776ac1f2012-04-13 23:36:36 -07003335void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003336 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3337 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003338 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003339 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003340 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003341 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3342 if (insn_flags_[i].IsGcPoint()) {
3343 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003344 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003345 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3346 map_index++;
3347 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003348 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003349 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003350 CHECK_LT(j / 8, map.RegWidth());
3351 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3352 } else if ((j / 8) < map.RegWidth()) {
3353 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3354 } else {
3355 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3356 }
3357 }
3358 } else {
3359 CHECK(reg_bitmap == NULL);
3360 }
3361 }
3362}
jeffhaoa0a764a2011-09-16 10:43:38 -07003363
Ian Rogers0c7abda2012-09-19 13:33:42 -07003364void MethodVerifier::SetDexGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003365 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003366 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003367 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
3368 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003369 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003370 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003371 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003372 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003373 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003374 CHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003375}
3376
Ian Rogers0c7abda2012-09-19 13:33:42 -07003377const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(Compiler::MethodReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003378 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003379 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
3380 if (it == dex_gc_maps_->end()) {
Ian Rogers64b6d142012-10-29 16:34:15 -07003381 LOG(WARNING) << "Didn't find GC map for: " << PrettyMethod(ref.second, *ref.first);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003382 return NULL;
3383 }
3384 CHECK(it->second != NULL);
3385 return it->second;
3386}
3387
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003388std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3389 RegisterLine* line = reg_table_.GetLine(dex_pc);
3390 std::vector<int32_t> result;
3391 for (size_t i = 0; i < line->NumRegs(); ++i) {
3392 const RegType& type = line->GetRegisterType(i);
3393 if (type.IsConstant()) {
3394 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3395 result.push_back(type.ConstantValue());
3396 } else if (type.IsConstantLo()) {
3397 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3398 result.push_back(type.ConstantValueLo());
3399 } else if (type.IsConstantHi()) {
3400 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3401 result.push_back(type.ConstantValueHi());
3402 } else if (type.IsIntegralTypes()) {
3403 result.push_back(kIntVReg);
3404 result.push_back(0);
3405 } else if (type.IsFloat()) {
3406 result.push_back(kFloatVReg);
3407 result.push_back(0);
3408 } else if (type.IsLong()) {
3409 result.push_back(kLongLoVReg);
3410 result.push_back(0);
3411 result.push_back(kLongHiVReg);
3412 result.push_back(0);
3413 ++i;
3414 } else if (type.IsDouble()) {
3415 result.push_back(kDoubleLoVReg);
3416 result.push_back(0);
3417 result.push_back(kDoubleHiVReg);
3418 result.push_back(0);
3419 ++i;
3420 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
3421 result.push_back(kUndefined);
3422 result.push_back(0);
3423 } else {
3424 CHECK(type.IsNonZeroReferenceTypes()) << type;
3425 result.push_back(kReferenceVReg);
3426 result.push_back(0);
3427 }
3428 }
3429 return result;
3430}
3431
Ian Rogers0c7abda2012-09-19 13:33:42 -07003432Mutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
3433MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003434
3435Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
3436MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
3437
buzbeec531cef2012-10-18 07:09:20 -07003438#if defined(ART_USE_LLVM_COMPILER)
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003439Mutex* MethodVerifier::inferred_reg_category_maps_lock_ = NULL;
3440MethodVerifier::InferredRegCategoryMapTable* MethodVerifier::inferred_reg_category_maps_ = NULL;
3441#endif
3442
3443void MethodVerifier::Init() {
Ian Rogers0c7abda2012-09-19 13:33:42 -07003444 dex_gc_maps_lock_ = new Mutex("verifier GC maps lock");
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 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003449 }
3450
3451 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
3452 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003453 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003454 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
3455 }
3456
buzbeec531cef2012-10-18 07:09:20 -07003457#if defined(ART_USE_LLVM_COMPILER)
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003458 inferred_reg_category_maps_lock_ = new Mutex("verifier GC maps lock");
3459 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003460 MutexLock mu(self, *inferred_reg_category_maps_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003461 inferred_reg_category_maps_ = new MethodVerifier::InferredRegCategoryMapTable;
3462 }
3463#endif
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003464}
3465
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003466void MethodVerifier::Shutdown() {
Ian Rogers50b35e22012-10-04 10:09:15 -07003467 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003468 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003469 MutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003470 STLDeleteValues(dex_gc_maps_);
3471 delete dex_gc_maps_;
3472 dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003473 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003474 delete dex_gc_maps_lock_;
3475 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003476
3477 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003478 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003479 delete rejected_classes_;
3480 rejected_classes_ = NULL;
3481 }
3482 delete rejected_classes_lock_;
3483 rejected_classes_lock_ = NULL;
3484
buzbeec531cef2012-10-18 07:09:20 -07003485#if defined(ART_USE_LLVM_COMPILER)
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003486 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003487 MutexLock mu(self, *inferred_reg_category_maps_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003488 STLDeleteValues(inferred_reg_category_maps_);
3489 delete inferred_reg_category_maps_;
3490 inferred_reg_category_maps_ = NULL;
3491 }
3492 delete inferred_reg_category_maps_lock_;
3493 inferred_reg_category_maps_lock_ = NULL;
3494#endif
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003495}
jeffhaod1224c72012-02-29 13:43:08 -08003496
Ian Rogers776ac1f2012-04-13 23:36:36 -07003497void MethodVerifier::AddRejectedClass(Compiler::ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003498 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003499 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003500 rejected_classes_->insert(ref);
3501 }
jeffhaod1224c72012-02-29 13:43:08 -08003502 CHECK(IsClassRejected(ref));
3503}
3504
Ian Rogers776ac1f2012-04-13 23:36:36 -07003505bool MethodVerifier::IsClassRejected(Compiler::ClassReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003506 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003507 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08003508}
3509
buzbeec531cef2012-10-18 07:09:20 -07003510#if defined(ART_USE_LLVM_COMPILER)
TDYa12789f96052012-07-12 20:49:53 -07003511const greenland::InferredRegCategoryMap* MethodVerifier::GenerateInferredRegCategoryMap() {
Logan Chienfca7e872011-12-20 20:08:22 +08003512 uint32_t insns_size = code_item_->insns_size_in_code_units_;
3513 uint16_t regs_size = code_item_->registers_size_;
3514
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003515 UniquePtr<InferredRegCategoryMap> table(new InferredRegCategoryMap(insns_size, regs_size));
Logan Chienfca7e872011-12-20 20:08:22 +08003516
3517 for (size_t i = 0; i < insns_size; ++i) {
3518 if (RegisterLine* line = reg_table_.GetLine(i)) {
TDYa127526643e2012-05-26 01:01:48 -07003519 const Instruction* inst = Instruction::At(code_item_->insns_ + i);
TDYa127526643e2012-05-26 01:01:48 -07003520 /* We only use InferredRegCategoryMap in one case */
3521 if (inst->IsBranch()) {
TDYa127b2eb5c12012-05-24 15:52:10 -07003522 for (size_t r = 0; r < regs_size; ++r) {
3523 const RegType &rt = line->GetRegisterType(r);
3524
3525 if (rt.IsZero()) {
TDYa12789f96052012-07-12 20:49:53 -07003526 table->SetRegCategory(i, r, greenland::kRegZero);
TDYa127b2eb5c12012-05-24 15:52:10 -07003527 } else if (rt.IsCategory1Types()) {
TDYa12789f96052012-07-12 20:49:53 -07003528 table->SetRegCategory(i, r, greenland::kRegCat1nr);
TDYa127b2eb5c12012-05-24 15:52:10 -07003529 } else if (rt.IsCategory2Types()) {
TDYa12789f96052012-07-12 20:49:53 -07003530 table->SetRegCategory(i, r, greenland::kRegCat2);
TDYa127b2eb5c12012-05-24 15:52:10 -07003531 } else if (rt.IsReferenceTypes()) {
TDYa12789f96052012-07-12 20:49:53 -07003532 table->SetRegCategory(i, r, greenland::kRegObject);
TDYa127b2eb5c12012-05-24 15:52:10 -07003533 } else {
TDYa12789f96052012-07-12 20:49:53 -07003534 table->SetRegCategory(i, r, greenland::kRegUnknown);
TDYa127b2eb5c12012-05-24 15:52:10 -07003535 }
Logan Chienfca7e872011-12-20 20:08:22 +08003536 }
3537 }
3538 }
3539 }
3540
3541 return table.release();
3542}
Logan Chiendd361c92012-04-10 23:40:37 +08003543
Ian Rogers776ac1f2012-04-13 23:36:36 -07003544void MethodVerifier::SetInferredRegCategoryMap(Compiler::MethodReference ref,
3545 const InferredRegCategoryMap& inferred_reg_category_map) {
Shih-wei Liaocd05a622012-08-15 00:02:05 -07003546 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003547 MutexLock mu(Thread::Current(), *inferred_reg_category_maps_lock_);
Shih-wei Liaocd05a622012-08-15 00:02:05 -07003548 InferredRegCategoryMapTable::iterator it = inferred_reg_category_maps_->find(ref);
3549 if (it == inferred_reg_category_maps_->end()) {
3550 inferred_reg_category_maps_->Put(ref, &inferred_reg_category_map);
3551 } else {
3552 CHECK(*(it->second) == inferred_reg_category_map);
3553 delete &inferred_reg_category_map;
3554 }
Logan Chiendd361c92012-04-10 23:40:37 +08003555 }
Logan Chiendd361c92012-04-10 23:40:37 +08003556 CHECK(GetInferredRegCategoryMap(ref) != NULL);
3557}
3558
TDYa12789f96052012-07-12 20:49:53 -07003559const greenland::InferredRegCategoryMap*
Ian Rogers776ac1f2012-04-13 23:36:36 -07003560MethodVerifier::GetInferredRegCategoryMap(Compiler::MethodReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003561 MutexLock mu(Thread::Current(), *inferred_reg_category_maps_lock_);
Logan Chiendd361c92012-04-10 23:40:37 +08003562
3563 InferredRegCategoryMapTable::const_iterator it =
3564 inferred_reg_category_maps_->find(ref);
3565
3566 if (it == inferred_reg_category_maps_->end()) {
3567 return NULL;
3568 }
3569 CHECK(it->second != NULL);
3570 return it->second;
3571}
Logan Chienfca7e872011-12-20 20:08:22 +08003572#endif
3573
Ian Rogersd81871c2011-10-03 13:57:23 -07003574} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003575} // namespace art