1 /*
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"
23 #include "base/scoped_arena_allocator.h"
24 #include "mirror/class.h"
25 #include "method_verifier.h"
26 #include "verifier_deps.h"
27
28 namespace art {
29 namespace verifier {
30
CanAccess(const RegType & other)31 inline bool RegType::CanAccess(const RegType& other) const {
32 if (Equals(other)) {
33 return true; // Trivial accessibility.
34 } else {
35 bool this_unresolved = IsUnresolvedTypes();
36 bool other_unresolved = other.IsUnresolvedTypes();
37 if (!this_unresolved && !other_unresolved) {
38 return GetClass()->CanAccess(other.GetClass());
39 } else if (!other_unresolved) {
40 return other.GetClass()->IsPublic(); // Be conservative, only allow if other is public.
41 } else {
42 return false; // More complicated test not possible on unresolved types, be conservative.
43 }
44 }
45 }
46
CanAccessMember(ObjPtr<mirror::Class> klass,uint32_t access_flags)47 inline bool RegType::CanAccessMember(ObjPtr<mirror::Class> klass, uint32_t access_flags) const {
48 if ((access_flags & kAccPublic) != 0) {
49 return true;
50 }
51 if (!IsUnresolvedTypes()) {
52 return GetClass()->CanAccessMember(klass, access_flags);
53 } else {
54 return false; // More complicated test not possible on unresolved types, be conservative.
55 }
56 }
57
IsConstantBoolean()58 inline bool RegType::IsConstantBoolean() const {
59 if (!IsConstant()) {
60 return false;
61 } else {
62 const ConstantType* const_val = down_cast<const ConstantType*>(this);
63 return const_val->ConstantValue() >= 0 && const_val->ConstantValue() <= 1;
64 }
65 }
66
AssignableFrom(const RegType & lhs,const RegType & rhs,bool strict,MethodVerifier * verifier)67 inline bool RegType::AssignableFrom(const RegType& lhs,
68 const RegType& rhs,
69 bool strict,
70 MethodVerifier* verifier) {
71 if (lhs.Equals(rhs)) {
72 return true;
73 } else {
74 switch (lhs.GetAssignmentType()) {
75 case AssignmentType::kBoolean:
76 return rhs.IsBooleanTypes();
77 case AssignmentType::kByte:
78 return rhs.IsByteTypes();
79 case AssignmentType::kShort:
80 return rhs.IsShortTypes();
81 case AssignmentType::kChar:
82 return rhs.IsCharTypes();
83 case AssignmentType::kInteger:
84 return rhs.IsIntegralTypes();
85 case AssignmentType::kFloat:
86 return rhs.IsFloatTypes();
87 case AssignmentType::kLongLo:
88 return rhs.IsLongTypes();
89 case AssignmentType::kDoubleLo:
90 return rhs.IsDoubleTypes();
91 case AssignmentType::kConflict:
92 LOG(WARNING) << "RegType::AssignableFrom lhs is Conflict!";
93 return false;
94 case AssignmentType::kReference:
95 if (rhs.IsZero()) {
96 return true; // All reference types can be assigned null.
97 } else if (!rhs.IsReferenceTypes()) {
98 return false; // Expect rhs to be a reference type.
99 } else if (lhs.IsUninitializedTypes() || rhs.IsUninitializedTypes()) {
100 // Uninitialized types are only allowed to be assigned to themselves.
101 // TODO: Once we have a proper "reference" super type, this needs to be extended.
102 return false;
103 } else if (lhs.IsJavaLangObject()) {
104 return true; // All reference types can be assigned to Object.
105 } else if (!strict && !lhs.IsUnresolvedTypes() && lhs.GetClass()->IsInterface()) {
106 // If we're not strict allow assignment to any interface, see comment in ClassJoin.
107 return true;
108 } else if (lhs.IsJavaLangObjectArray()) {
109 return rhs.IsObjectArrayTypes(); // All reference arrays may be assigned to Object[]
110 } else if (lhs.HasClass() && rhs.HasClass()) {
111 // Test assignability from the Class point-of-view.
112 bool result = lhs.GetClass()->IsAssignableFrom(rhs.GetClass());
113 // Record assignability dependency. The `verifier` is null during unit tests and
114 // VerifiedMethod::GenerateSafeCastSet.
115 if (verifier != nullptr) {
116 VerifierDeps::MaybeRecordAssignability(
117 verifier->GetDexFile(), lhs.GetClass(), rhs.GetClass(), strict, result);
118 }
119 return result;
120 } else {
121 // Unresolved types are only assignable for null and equality.
122 return false;
123 }
124 case AssignmentType::kNotAssignable:
125 break;
126 }
127 LOG(FATAL) << "Unexpected register type in IsAssignableFrom: '"
128 << lhs << "' := '" << rhs << "'";
129 UNREACHABLE();
130 }
131 }
132
IsAssignableFrom(const RegType & src,MethodVerifier * verifier)133 inline bool RegType::IsAssignableFrom(const RegType& src, MethodVerifier* verifier) const {
134 return AssignableFrom(*this, src, false, verifier);
135 }
136
IsStrictlyAssignableFrom(const RegType & src,MethodVerifier * verifier)137 inline bool RegType::IsStrictlyAssignableFrom(const RegType& src, MethodVerifier* verifier) const {
138 return AssignableFrom(*this, src, true, verifier);
139 }
140
GetInstance()141 inline const DoubleHiType* DoubleHiType::GetInstance() {
142 DCHECK(instance_ != nullptr);
143 return instance_;
144 }
145
GetInstance()146 inline const DoubleLoType* DoubleLoType::GetInstance() {
147 DCHECK(instance_ != nullptr);
148 return instance_;
149 }
150
GetInstance()151 inline const LongHiType* LongHiType::GetInstance() {
152 DCHECK(instance_ != nullptr);
153 return instance_;
154 }
155
GetInstance()156 inline const LongLoType* LongLoType::GetInstance() {
157 DCHECK(instance_ != nullptr);
158 return instance_;
159 }
160
GetInstance()161 inline const FloatType* FloatType::GetInstance() {
162 DCHECK(instance_ != nullptr);
163 return instance_;
164 }
165
GetInstance()166 inline const CharType* CharType::GetInstance() {
167 DCHECK(instance_ != nullptr);
168 return instance_;
169 }
170
GetInstance()171 inline const ShortType* ShortType::GetInstance() {
172 DCHECK(instance_ != nullptr);
173 return instance_;
174 }
175
GetInstance()176 inline const ByteType* ByteType::GetInstance() {
177 DCHECK(instance_ != nullptr);
178 return instance_;
179 }
180
181
GetInstance()182 inline const IntegerType* IntegerType::GetInstance() {
183 DCHECK(instance_ != nullptr);
184 return instance_;
185 }
186
GetInstance()187 inline const BooleanType* BooleanType::GetInstance() {
188 DCHECK(BooleanType::instance_ != nullptr);
189 return BooleanType::instance_;
190 }
191
GetInstance()192 inline const ConflictType* ConflictType::GetInstance() {
193 DCHECK(instance_ != nullptr);
194 return instance_;
195 }
196
GetInstance()197 inline const UndefinedType* UndefinedType::GetInstance() {
198 DCHECK(instance_ != nullptr);
199 return instance_;
200 }
201
new(size_t size,ScopedArenaAllocator * arena)202 inline void* RegType::operator new(size_t size, ScopedArenaAllocator* arena) {
203 return arena->Alloc(size, kArenaAllocMisc);
204 }
205
206 } // namespace verifier
207 } // namespace art
208
209 #endif // ART_RUNTIME_VERIFIER_REG_TYPE_INL_H_
210