blob: 9c5b63232e39f3b676caf092b9ff099b6352d569 [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"
Vladimir Markoc7f83202014-01-24 17:55:18 +000023#include "dex_file.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000024#include "dex_instruction-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080025#include "runtime.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000026#include "verifier/method_verifier-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070027#include "verifier/reg_type-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000028#include "verifier/register_line-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080029#include "verifier/verifier_deps.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000030
31namespace art {
32
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +000033VerifiedMethod::VerifiedMethod(uint32_t encountered_error_types, bool has_runtime_throw)
Andreas Gampe0760a812015-08-26 17:12:51 -070034 : encountered_error_types_(encountered_error_types),
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +000035 has_runtime_throw_(has_runtime_throw) {
Andreas Gampe0760a812015-08-26 17:12:51 -070036}
37
Nicolas Geoffrayc51c7ca2016-11-25 15:46:48 +000038const VerifiedMethod* VerifiedMethod::Create(verifier::MethodVerifier* method_verifier) {
39 DCHECK(Runtime::Current()->IsAotCompiler());
Andreas Gampe0760a812015-08-26 17:12:51 -070040 std::unique_ptr<VerifiedMethod> verified_method(
41 new VerifiedMethod(method_verifier->GetEncounteredFailureTypes(),
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +000042 method_verifier->HasInstructionThatWillThrow()));
Andreas Gampe0760a812015-08-26 17:12:51 -070043
Vladimir Markoc7f83202014-01-24 17:55:18 +000044 if (method_verifier->HasCheckCasts()) {
45 verified_method->GenerateSafeCastSet(method_verifier);
46 }
Jeff Hao848f70a2014-01-15 13:49:50 -080047
Vladimir Markoc7f83202014-01-24 17:55:18 +000048 return verified_method.release();
49}
50
Vladimir Markoc7f83202014-01-24 17:55:18 +000051bool VerifiedMethod::IsSafeCast(uint32_t pc) const {
Andreas Gampe26699c62017-05-12 08:19:28 -070052 if (safe_cast_set_ == nullptr) {
53 return false;
54 }
55 return std::binary_search(safe_cast_set_->begin(), safe_cast_set_->end(), pc);
Vladimir Markoc7f83202014-01-24 17:55:18 +000056}
57
Vladimir Markoc7f83202014-01-24 17:55:18 +000058void VerifiedMethod::GenerateSafeCastSet(verifier::MethodVerifier* method_verifier) {
59 /*
60 * Walks over the method code and adds any cast instructions in which
61 * the type cast is implicit to a set, which is used in the code generation
62 * to elide these casts.
63 */
64 if (method_verifier->HasFailures()) {
65 return;
66 }
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -070067 IterationRange<DexInstructionIterator> instructions = method_verifier->CodeItem()->Instructions();
68 for (auto it = instructions.begin(); it != instructions.end(); ++it) {
69 const Instruction& inst = *it;
70 const Instruction::Code code = inst.Opcode();
Nicolas Geoffrayd1665a02016-12-12 13:07:07 +000071 if (code == Instruction::CHECK_CAST) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -070072 const uint32_t dex_pc = it.GetDexPC(instructions.begin());
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