blob: f7197827277a9e7b236a233ccf2afe474517090c [file] [log] [blame]
Ian Rogers7b078e82014-09-10 14:44:24 -07001/*
2 * Copyright (C) 2012 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#ifndef ART_RUNTIME_VERIFIER_REG_TYPE_INL_H_
18#define ART_RUNTIME_VERIFIER_REG_TYPE_INL_H_
19
20#include "reg_type.h"
21
22#include "base/casts.h"
Mathieu Chartierde40d472015-10-15 17:47:48 -070023#include "base/scoped_arena_allocator.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010024#include "method_verifier.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include "mirror/class.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010026#include "verifier_deps.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070027
28namespace art {
29namespace verifier {
30
31inline bool RegType::CanAccess(const RegType& other) const {
Andreas Gampeeed3a5d2017-11-29 14:58:34 -080032 DCHECK(IsReferenceTypes());
33 DCHECK(!IsNull());
Ian Rogers7b078e82014-09-10 14:44:24 -070034 if (Equals(other)) {
35 return true; // Trivial accessibility.
36 } else {
37 bool this_unresolved = IsUnresolvedTypes();
38 bool other_unresolved = other.IsUnresolvedTypes();
39 if (!this_unresolved && !other_unresolved) {
40 return GetClass()->CanAccess(other.GetClass());
41 } else if (!other_unresolved) {
42 return other.GetClass()->IsPublic(); // Be conservative, only allow if other is public.
43 } else {
44 return false; // More complicated test not possible on unresolved types, be conservative.
45 }
46 }
47}
48
Mathieu Chartier3398c782016-09-30 10:27:43 -070049inline bool RegType::CanAccessMember(ObjPtr<mirror::Class> klass, uint32_t access_flags) const {
Andreas Gampeeed3a5d2017-11-29 14:58:34 -080050 DCHECK(IsReferenceTypes());
Ian Rogers7b078e82014-09-10 14:44:24 -070051 if ((access_flags & kAccPublic) != 0) {
52 return true;
53 }
Andreas Gampeeed3a5d2017-11-29 14:58:34 -080054 if (IsNull()) {
55 return true;
56 }
Ian Rogers7b078e82014-09-10 14:44:24 -070057 if (!IsUnresolvedTypes()) {
58 return GetClass()->CanAccessMember(klass, access_flags);
59 } else {
60 return false; // More complicated test not possible on unresolved types, be conservative.
61 }
62}
63
64inline bool RegType::IsConstantBoolean() const {
65 if (!IsConstant()) {
66 return false;
67 } else {
68 const ConstantType* const_val = down_cast<const ConstantType*>(this);
69 return const_val->ConstantValue() >= 0 && const_val->ConstantValue() <= 1;
70 }
71}
72
David Brazdilca3c8c32016-09-06 14:04:48 +010073inline bool RegType::AssignableFrom(const RegType& lhs,
74 const RegType& rhs,
75 bool strict,
76 MethodVerifier* verifier) {
Ian Rogers7b078e82014-09-10 14:44:24 -070077 if (lhs.Equals(rhs)) {
78 return true;
79 } else {
Andreas Gampe42ae05a2017-05-16 09:31:37 -070080 switch (lhs.GetAssignmentType()) {
81 case AssignmentType::kBoolean:
82 return rhs.IsBooleanTypes();
83 case AssignmentType::kByte:
84 return rhs.IsByteTypes();
85 case AssignmentType::kShort:
86 return rhs.IsShortTypes();
87 case AssignmentType::kChar:
88 return rhs.IsCharTypes();
89 case AssignmentType::kInteger:
90 return rhs.IsIntegralTypes();
91 case AssignmentType::kFloat:
92 return rhs.IsFloatTypes();
93 case AssignmentType::kLongLo:
94 return rhs.IsLongTypes();
95 case AssignmentType::kDoubleLo:
96 return rhs.IsDoubleTypes();
97 case AssignmentType::kConflict:
98 LOG(WARNING) << "RegType::AssignableFrom lhs is Conflict!";
David Brazdil68b5c0b2016-01-19 14:25:29 +000099 return false;
Andreas Gampe42ae05a2017-05-16 09:31:37 -0700100 case AssignmentType::kReference:
Andreas Gampeeed3a5d2017-11-29 14:58:34 -0800101 if (rhs.IsZeroOrNull()) {
Andreas Gampe42ae05a2017-05-16 09:31:37 -0700102 return true; // All reference types can be assigned null.
103 } else if (!rhs.IsReferenceTypes()) {
104 return false; // Expect rhs to be a reference type.
105 } else if (lhs.IsUninitializedTypes() || rhs.IsUninitializedTypes()) {
106 // Uninitialized types are only allowed to be assigned to themselves.
107 // TODO: Once we have a proper "reference" super type, this needs to be extended.
108 return false;
109 } else if (lhs.IsJavaLangObject()) {
110 return true; // All reference types can be assigned to Object.
111 } else if (!strict && !lhs.IsUnresolvedTypes() && lhs.GetClass()->IsInterface()) {
112 // If we're not strict allow assignment to any interface, see comment in ClassJoin.
113 return true;
114 } else if (lhs.IsJavaLangObjectArray()) {
115 return rhs.IsObjectArrayTypes(); // All reference arrays may be assigned to Object[]
116 } else if (lhs.HasClass() && rhs.HasClass()) {
117 // Test assignability from the Class point-of-view.
118 bool result = lhs.GetClass()->IsAssignableFrom(rhs.GetClass());
119 // Record assignability dependency. The `verifier` is null during unit tests and
120 // VerifiedMethod::GenerateSafeCastSet.
121 if (verifier != nullptr) {
122 VerifierDeps::MaybeRecordAssignability(
123 verifier->GetDexFile(), lhs.GetClass(), rhs.GetClass(), strict, result);
124 }
125 return result;
126 } else {
127 // Unresolved types are only assignable for null and equality.
Andreas Gampeeed3a5d2017-11-29 14:58:34 -0800128 // Null cannot be the left-hand side.
Andreas Gampe42ae05a2017-05-16 09:31:37 -0700129 return false;
David Brazdilca3c8c32016-09-06 14:04:48 +0100130 }
Andreas Gampe42ae05a2017-05-16 09:31:37 -0700131 case AssignmentType::kNotAssignable:
132 break;
Ian Rogers7b078e82014-09-10 14:44:24 -0700133 }
Andreas Gampe42ae05a2017-05-16 09:31:37 -0700134 LOG(FATAL) << "Unexpected register type in IsAssignableFrom: '"
135 << lhs << "' := '" << rhs << "'";
136 UNREACHABLE();
Ian Rogers7b078e82014-09-10 14:44:24 -0700137 }
138}
139
David Brazdilca3c8c32016-09-06 14:04:48 +0100140inline bool RegType::IsAssignableFrom(const RegType& src, MethodVerifier* verifier) const {
141 return AssignableFrom(*this, src, false, verifier);
Ian Rogers7b078e82014-09-10 14:44:24 -0700142}
143
David Brazdilca3c8c32016-09-06 14:04:48 +0100144inline bool RegType::IsStrictlyAssignableFrom(const RegType& src, MethodVerifier* verifier) const {
145 return AssignableFrom(*this, src, true, verifier);
Ian Rogers7b078e82014-09-10 14:44:24 -0700146}
147
148inline const DoubleHiType* DoubleHiType::GetInstance() {
149 DCHECK(instance_ != nullptr);
150 return instance_;
151}
152
153inline const DoubleLoType* DoubleLoType::GetInstance() {
154 DCHECK(instance_ != nullptr);
155 return instance_;
156}
157
158inline const LongHiType* LongHiType::GetInstance() {
159 DCHECK(instance_ != nullptr);
160 return instance_;
161}
162
163inline const LongLoType* LongLoType::GetInstance() {
164 DCHECK(instance_ != nullptr);
165 return instance_;
166}
167
168inline const FloatType* FloatType::GetInstance() {
169 DCHECK(instance_ != nullptr);
170 return instance_;
171}
172
173inline const CharType* CharType::GetInstance() {
174 DCHECK(instance_ != nullptr);
175 return instance_;
176}
177
178inline const ShortType* ShortType::GetInstance() {
179 DCHECK(instance_ != nullptr);
180 return instance_;
181}
182
183inline const ByteType* ByteType::GetInstance() {
184 DCHECK(instance_ != nullptr);
185 return instance_;
186}
187
188
189inline const IntegerType* IntegerType::GetInstance() {
190 DCHECK(instance_ != nullptr);
191 return instance_;
192}
193
194inline const BooleanType* BooleanType::GetInstance() {
195 DCHECK(BooleanType::instance_ != nullptr);
196 return BooleanType::instance_;
197}
198
199inline const ConflictType* ConflictType::GetInstance() {
200 DCHECK(instance_ != nullptr);
201 return instance_;
202}
203
204inline const UndefinedType* UndefinedType::GetInstance() {
205 DCHECK(instance_ != nullptr);
206 return instance_;
207}
208
Andreas Gampeeed3a5d2017-11-29 14:58:34 -0800209inline const NullType* NullType::GetInstance() {
210 DCHECK(instance_ != nullptr);
211 return instance_;
212}
213
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100214inline void* RegType::operator new(size_t size, ScopedArenaAllocator* allocator) {
215 return allocator->Alloc(size, kArenaAllocMisc);
Mathieu Chartierde40d472015-10-15 17:47:48 -0700216}
217
Ian Rogers7b078e82014-09-10 14:44:24 -0700218} // namespace verifier
219} // namespace art
220
221#endif // ART_RUNTIME_VERIFIER_REG_TYPE_INL_H_