blob: 524b0a691174a6d369d3107e1d3bae2fa382bb47 [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
22#include "base/logging.h"
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -080023#include "code_item_accessors-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000024#include "dex_file.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000025#include "dex_instruction-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080026#include "runtime.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000027#include "verifier/method_verifier-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070028#include "verifier/reg_type-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000029#include "verifier/register_line-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080030#include "verifier/verifier_deps.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000031
32namespace art {
33
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +000034VerifiedMethod::VerifiedMethod(uint32_t encountered_error_types, bool has_runtime_throw)
Andreas Gampe0760a812015-08-26 17:12:51 -070035 : encountered_error_types_(encountered_error_types),
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +000036 has_runtime_throw_(has_runtime_throw) {
Andreas Gampe0760a812015-08-26 17:12:51 -070037}
38
Nicolas Geoffrayc51c7ca2016-11-25 15:46:48 +000039const VerifiedMethod* VerifiedMethod::Create(verifier::MethodVerifier* method_verifier) {
40 DCHECK(Runtime::Current()->IsAotCompiler());
Andreas Gampe0760a812015-08-26 17:12:51 -070041 std::unique_ptr<VerifiedMethod> verified_method(
42 new VerifiedMethod(method_verifier->GetEncounteredFailureTypes(),
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +000043 method_verifier->HasInstructionThatWillThrow()));
Andreas Gampe0760a812015-08-26 17:12:51 -070044
Vladimir Markoc7f83202014-01-24 17:55:18 +000045 if (method_verifier->HasCheckCasts()) {
46 verified_method->GenerateSafeCastSet(method_verifier);
47 }
Jeff Hao848f70a2014-01-15 13:49:50 -080048
Vladimir Markoc7f83202014-01-24 17:55:18 +000049 return verified_method.release();
50}
51
Vladimir Markoc7f83202014-01-24 17:55:18 +000052bool VerifiedMethod::IsSafeCast(uint32_t pc) const {
Andreas Gampe26699c62017-05-12 08:19:28 -070053 if (safe_cast_set_ == nullptr) {
54 return false;
55 }
56 return std::binary_search(safe_cast_set_->begin(), safe_cast_set_->end(), pc);
Vladimir Markoc7f83202014-01-24 17:55:18 +000057}
58
Vladimir Markoc7f83202014-01-24 17:55:18 +000059void VerifiedMethod::GenerateSafeCastSet(verifier::MethodVerifier* method_verifier) {
60 /*
61 * Walks over the method code and adds any cast instructions in which
62 * the type cast is implicit to a set, which is used in the code generation
63 * to elide these casts.
64 */
65 if (method_verifier->HasFailures()) {
66 return;
67 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -080068 for (const DexInstructionPcPair& pair : method_verifier->CodeItem()) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -070069 const Instruction& inst = pair.Inst();
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -070070 const Instruction::Code code = inst.Opcode();
Nicolas Geoffrayd1665a02016-12-12 13:07:07 +000071 if (code == Instruction::CHECK_CAST) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -070072 const uint32_t dex_pc = pair.DexPc();
Stephen Kyle40d35182014-10-03 13:47:56 +010073 if (!method_verifier->GetInstructionFlags(dex_pc).IsVisited()) {
74 // Do not attempt to quicken this instruction, it's unreachable anyway.
75 continue;
76 }
Vladimir Markoc7f83202014-01-24 17:55:18 +000077 const verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
Nicolas Geoffrayd1665a02016-12-12 13:07:07 +000078 const verifier::RegType& reg_type(line->GetRegisterType(method_verifier,
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -070079 inst.VRegA_21c()));
Nicolas Geoffrayd1665a02016-12-12 13:07:07 +000080 const verifier::RegType& cast_type =
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -070081 method_verifier->ResolveCheckedClass(dex::TypeIndex(inst.VRegB_21c()));
Nicolas Geoffray119e8462016-12-21 10:29:43 +000082 // Pass null for the method verifier to not record the VerifierDeps dependency
83 // if the types are not assignable.
84 if (cast_type.IsStrictlyAssignableFrom(reg_type, /* method_verifier */ nullptr)) {
85 // The types are assignable, we record that dependency in the VerifierDeps so
86 // that if this changes after OTA, we will re-verify again.
87 // We check if reg_type has a class, as the verifier may have inferred it's
88 // 'null'.
89 if (reg_type.HasClass()) {
90 DCHECK(cast_type.HasClass());
91 verifier::VerifierDeps::MaybeRecordAssignability(method_verifier->GetDexFile(),
92 cast_type.GetClass(),
93 reg_type.GetClass(),
94 /* strict */ true,
95 /* assignable */ true);
96 }
Andreas Gampe26699c62017-05-12 08:19:28 -070097 if (safe_cast_set_ == nullptr) {
98 safe_cast_set_.reset(new SafeCastSet());
99 }
Vladimir Markoc7f83202014-01-24 17:55:18 +0000100 // Verify ordering for push_back() to the sorted vector.
Andreas Gampe26699c62017-05-12 08:19:28 -0700101 DCHECK(safe_cast_set_->empty() || safe_cast_set_->back() < dex_pc);
102 safe_cast_set_->push_back(dex_pc);
Vladimir Markoc7f83202014-01-24 17:55:18 +0000103 }
104 }
105 }
Andreas Gampe26699c62017-05-12 08:19:28 -0700106 DCHECK(safe_cast_set_ == nullptr || !safe_cast_set_->empty());
Vladimir Markoc7f83202014-01-24 17:55:18 +0000107}
108
109} // namespace art