blob: cbca33320dfd35ac82fb9e2564487147e99b258a [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
Vladimir Markoc7f83202014-01-24 17:55:18 +000062void VerifiedMethod::GenerateSafeCastSet(verifier::MethodVerifier* method_verifier) {
63 /*
64 * Walks over the method code and adds any cast instructions in which
65 * the type cast is implicit to a set, which is used in the code generation
66 * to elide these casts.
67 */
68 if (method_verifier->HasFailures()) {
69 return;
70 }
71 const DexFile::CodeItem* code_item = method_verifier->CodeItem();
72 const Instruction* inst = Instruction::At(code_item->insns_);
73 const Instruction* end = Instruction::At(code_item->insns_ +
74 code_item->insns_size_in_code_units_);
75
76 for (; inst < end; inst = inst->Next()) {
77 Instruction::Code code = inst->Opcode();
Nicolas Geoffrayd1665a02016-12-12 13:07:07 +000078 if (code == Instruction::CHECK_CAST) {
Vladimir Markoc7f83202014-01-24 17:55:18 +000079 uint32_t dex_pc = inst->GetDexPc(code_item->insns_);
Stephen Kyle40d35182014-10-03 13:47:56 +010080 if (!method_verifier->GetInstructionFlags(dex_pc).IsVisited()) {
81 // Do not attempt to quicken this instruction, it's unreachable anyway.
82 continue;
83 }
Vladimir Markoc7f83202014-01-24 17:55:18 +000084 const verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
Nicolas Geoffrayd1665a02016-12-12 13:07:07 +000085 const verifier::RegType& reg_type(line->GetRegisterType(method_verifier,
86 inst->VRegA_21c()));
87 const verifier::RegType& cast_type =
88 method_verifier->ResolveCheckedClass(dex::TypeIndex(inst->VRegB_21c()));
Nicolas Geoffray119e8462016-12-21 10:29:43 +000089 // Pass null for the method verifier to not record the VerifierDeps dependency
90 // if the types are not assignable.
91 if (cast_type.IsStrictlyAssignableFrom(reg_type, /* method_verifier */ nullptr)) {
92 // The types are assignable, we record that dependency in the VerifierDeps so
93 // that if this changes after OTA, we will re-verify again.
94 // We check if reg_type has a class, as the verifier may have inferred it's
95 // 'null'.
96 if (reg_type.HasClass()) {
97 DCHECK(cast_type.HasClass());
98 verifier::VerifierDeps::MaybeRecordAssignability(method_verifier->GetDexFile(),
99 cast_type.GetClass(),
100 reg_type.GetClass(),
101 /* strict */ true,
102 /* assignable */ true);
103 }
Vladimir Markoc7f83202014-01-24 17:55:18 +0000104 // Verify ordering for push_back() to the sorted vector.
105 DCHECK(safe_cast_set_.empty() || safe_cast_set_.back() < dex_pc);
106 safe_cast_set_.push_back(dex_pc);
107 }
108 }
109 }
110}
111
112} // namespace art