blob: 188209bfb84fcc1ce048ad5da87b4789bc256088 [file] [log] [blame]
Vladimir Markoc7f83202014-01-24 17:55:18 +00001/*
2 * Copyright (C) 2014 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 */
16
17#include "verified_method.h"
18
19#include <algorithm>
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Vladimir Markoc7f83202014-01-24 17:55:18 +000021#include <vector>
22
Mathieu Chartiere401d142015-04-22 13:56:20 -070023#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070024#include "base/enums.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000025#include "base/logging.h"
26#include "base/stl_util.h"
27#include "dex_file.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000028#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080029#include "dex_instruction_utils.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000030#include "mirror/class-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000031#include "mirror/dex_cache-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000032#include "mirror/object-inl.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080033#include "utils.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000034#include "verifier/method_verifier-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070035#include "verifier/reg_type-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000036#include "verifier/register_line-inl.h"
37
38namespace art {
39
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +000040VerifiedMethod::VerifiedMethod(uint32_t encountered_error_types, bool has_runtime_throw)
Andreas Gampe0760a812015-08-26 17:12:51 -070041 : encountered_error_types_(encountered_error_types),
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +000042 has_runtime_throw_(has_runtime_throw) {
Andreas Gampe0760a812015-08-26 17:12:51 -070043}
44
Nicolas Geoffrayc51c7ca2016-11-25 15:46:48 +000045const VerifiedMethod* VerifiedMethod::Create(verifier::MethodVerifier* method_verifier) {
46 DCHECK(Runtime::Current()->IsAotCompiler());
Andreas Gampe0760a812015-08-26 17:12:51 -070047 std::unique_ptr<VerifiedMethod> verified_method(
48 new VerifiedMethod(method_verifier->GetEncounteredFailureTypes(),
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +000049 method_verifier->HasInstructionThatWillThrow()));
Andreas Gampe0760a812015-08-26 17:12:51 -070050
Vladimir Markoc7f83202014-01-24 17:55:18 +000051 if (method_verifier->HasCheckCasts()) {
52 verified_method->GenerateSafeCastSet(method_verifier);
53 }
Jeff Hao848f70a2014-01-15 13:49:50 -080054
Vladimir Markoc7f83202014-01-24 17:55:18 +000055 return verified_method.release();
56}
57
Vladimir Markoc7f83202014-01-24 17:55:18 +000058bool VerifiedMethod::IsSafeCast(uint32_t pc) const {
59 return std::binary_search(safe_cast_set_.begin(), safe_cast_set_.end(), pc);
60}
61
Mathieu Chartier091d2382015-03-06 10:59:06 -080062bool VerifiedMethod::GenerateDequickenMap(verifier::MethodVerifier* method_verifier) {
Mathieu Chartier36b58f52014-12-10 12:06:45 -080063 if (method_verifier->HasFailures()) {
Mathieu Chartier091d2382015-03-06 10:59:06 -080064 return false;
Mathieu Chartier36b58f52014-12-10 12:06:45 -080065 }
66 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
67 const uint16_t* insns = code_item->insns_;
68 const Instruction* inst = Instruction::At(insns);
69 const Instruction* end = Instruction::At(insns + code_item->insns_size_in_code_units_);
70 for (; inst < end; inst = inst->Next()) {
71 const bool is_virtual_quick = inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK;
72 const bool is_range_quick = inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK;
73 if (is_virtual_quick || is_range_quick) {
74 uint32_t dex_pc = inst->GetDexPc(insns);
75 verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
Mathieu Chartiere401d142015-04-22 13:56:20 -070076 ArtMethod* method =
Mathieu Chartier091d2382015-03-06 10:59:06 -080077 method_verifier->GetQuickInvokedMethod(inst, line, is_range_quick, true);
78 if (method == nullptr) {
79 // It can be null if the line wasn't verified since it was unreachable.
80 return false;
81 }
Mathieu Chartier36b58f52014-12-10 12:06:45 -080082 // The verifier must know what the type of the object was or else we would have gotten a
83 // failure. Put the dex method index in the dequicken map since we need this to get number of
84 // arguments in the compiler.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080085 dequicken_map_.Put(dex_pc, DexFileReference(method->GetDexFile(),
86 method->GetDexMethodIndex()));
87 } else if (IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) {
88 uint32_t dex_pc = inst->GetDexPc(insns);
89 verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
Mathieu Chartierc7853442015-03-27 14:35:38 -070090 ArtField* field = method_verifier->GetQuickFieldAccess(inst, line);
Mathieu Chartier091d2382015-03-06 10:59:06 -080091 if (field == nullptr) {
92 // It can be null if the line wasn't verified since it was unreachable.
93 return false;
94 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080095 // The verifier must know what the type of the field was or else we would have gotten a
96 // failure. Put the dex field index in the dequicken map since we need this for lowering
97 // in the compiler.
98 // TODO: Putting a field index in a method reference is gross.
99 dequicken_map_.Put(dex_pc, DexFileReference(field->GetDexFile(), field->GetDexFieldIndex()));
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800100 }
101 }
Mathieu Chartier091d2382015-03-06 10:59:06 -0800102 return true;
Mathieu Chartier36b58f52014-12-10 12:06:45 -0800103}
104
Vladimir Markoc7f83202014-01-24 17:55:18 +0000105void VerifiedMethod::GenerateSafeCastSet(verifier::MethodVerifier* method_verifier) {
106 /*
107 * Walks over the method code and adds any cast instructions in which
108 * the type cast is implicit to a set, which is used in the code generation
109 * to elide these casts.
110 */
111 if (method_verifier->HasFailures()) {
112 return;
113 }
114 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
115 const Instruction* inst = Instruction::At(code_item->insns_);
116 const Instruction* end = Instruction::At(code_item->insns_ +
117 code_item->insns_size_in_code_units_);
118
119 for (; inst < end; inst = inst->Next()) {
120 Instruction::Code code = inst->Opcode();
Nicolas Geoffrayd1665a02016-12-12 13:07:07 +0000121 if (code == Instruction::CHECK_CAST) {
Vladimir Markoc7f83202014-01-24 17:55:18 +0000122 uint32_t dex_pc = inst->GetDexPc(code_item->insns_);
Stephen Kyle40d35182014-10-03 13:47:56 +0100123 if (!method_verifier->GetInstructionFlags(dex_pc).IsVisited()) {
124 // Do not attempt to quicken this instruction, it's unreachable anyway.
125 continue;
126 }
Vladimir Markoc7f83202014-01-24 17:55:18 +0000127 const verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
Nicolas Geoffrayd1665a02016-12-12 13:07:07 +0000128 const verifier::RegType& reg_type(line->GetRegisterType(method_verifier,
129 inst->VRegA_21c()));
130 const verifier::RegType& cast_type =
131 method_verifier->ResolveCheckedClass(dex::TypeIndex(inst->VRegB_21c()));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000132 // Pass null for the method verifier to not record the VerifierDeps dependency
133 // if the types are not assignable.
134 if (cast_type.IsStrictlyAssignableFrom(reg_type, /* method_verifier */ nullptr)) {
135 // The types are assignable, we record that dependency in the VerifierDeps so
136 // that if this changes after OTA, we will re-verify again.
137 // We check if reg_type has a class, as the verifier may have inferred it's
138 // 'null'.
139 if (reg_type.HasClass()) {
140 DCHECK(cast_type.HasClass());
141 verifier::VerifierDeps::MaybeRecordAssignability(method_verifier->GetDexFile(),
142 cast_type.GetClass(),
143 reg_type.GetClass(),
144 /* strict */ true,
145 /* assignable */ true);
146 }
Vladimir Markoc7f83202014-01-24 17:55:18 +0000147 // Verify ordering for push_back() to the sorted vector.
148 DCHECK(safe_cast_set_.empty() || safe_cast_set_.back() < dex_pc);
149 safe_cast_set_.push_back(dex_pc);
150 }
151 }
152 }
153}
154
155} // namespace art