blob: 9245828ae2643db40171d5e520925f8ec1a09534 [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"
Ian Rogers7b078e82014-09-10 14:44:24 -070024#include "mirror/class.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010025#include "method_verifier.h"
26#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 {
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
Mathieu Chartier3398c782016-09-30 10:27:43 -070047inline bool RegType::CanAccessMember(ObjPtr<mirror::Class> klass, uint32_t access_flags) const {
Ian Rogers7b078e82014-09-10 14:44:24 -070048 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
58inline 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
David Brazdilca3c8c32016-09-06 14:04:48 +010067inline bool RegType::AssignableFrom(const RegType& lhs,
68 const RegType& rhs,
69 bool strict,
70 MethodVerifier* verifier) {
Ian Rogers7b078e82014-09-10 14:44:24 -070071 if (lhs.Equals(rhs)) {
72 return true;
73 } else {
Andreas Gampe42ae05a2017-05-16 09:31:37 -070074 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!";
David Brazdil68b5c0b2016-01-19 14:25:29 +000093 return false;
Andreas Gampe42ae05a2017-05-16 09:31:37 -070094 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;
David Brazdilca3c8c32016-09-06 14:04:48 +0100123 }
Andreas Gampe42ae05a2017-05-16 09:31:37 -0700124 case AssignmentType::kNotAssignable:
125 break;
Ian Rogers7b078e82014-09-10 14:44:24 -0700126 }
Andreas Gampe42ae05a2017-05-16 09:31:37 -0700127 LOG(FATAL) << "Unexpected register type in IsAssignableFrom: '"
128 << lhs << "' := '" << rhs << "'";
129 UNREACHABLE();
Ian Rogers7b078e82014-09-10 14:44:24 -0700130 }
131}
132
David Brazdilca3c8c32016-09-06 14:04:48 +0100133inline bool RegType::IsAssignableFrom(const RegType& src, MethodVerifier* verifier) const {
134 return AssignableFrom(*this, src, false, verifier);
Ian Rogers7b078e82014-09-10 14:44:24 -0700135}
136
David Brazdilca3c8c32016-09-06 14:04:48 +0100137inline bool RegType::IsStrictlyAssignableFrom(const RegType& src, MethodVerifier* verifier) const {
138 return AssignableFrom(*this, src, true, verifier);
Ian Rogers7b078e82014-09-10 14:44:24 -0700139}
140
141inline const DoubleHiType* DoubleHiType::GetInstance() {
142 DCHECK(instance_ != nullptr);
143 return instance_;
144}
145
146inline const DoubleLoType* DoubleLoType::GetInstance() {
147 DCHECK(instance_ != nullptr);
148 return instance_;
149}
150
151inline const LongHiType* LongHiType::GetInstance() {
152 DCHECK(instance_ != nullptr);
153 return instance_;
154}
155
156inline const LongLoType* LongLoType::GetInstance() {
157 DCHECK(instance_ != nullptr);
158 return instance_;
159}
160
161inline const FloatType* FloatType::GetInstance() {
162 DCHECK(instance_ != nullptr);
163 return instance_;
164}
165
166inline const CharType* CharType::GetInstance() {
167 DCHECK(instance_ != nullptr);
168 return instance_;
169}
170
171inline const ShortType* ShortType::GetInstance() {
172 DCHECK(instance_ != nullptr);
173 return instance_;
174}
175
176inline const ByteType* ByteType::GetInstance() {
177 DCHECK(instance_ != nullptr);
178 return instance_;
179}
180
181
182inline const IntegerType* IntegerType::GetInstance() {
183 DCHECK(instance_ != nullptr);
184 return instance_;
185}
186
187inline const BooleanType* BooleanType::GetInstance() {
188 DCHECK(BooleanType::instance_ != nullptr);
189 return BooleanType::instance_;
190}
191
192inline const ConflictType* ConflictType::GetInstance() {
193 DCHECK(instance_ != nullptr);
194 return instance_;
195}
196
197inline const UndefinedType* UndefinedType::GetInstance() {
198 DCHECK(instance_ != nullptr);
199 return instance_;
200}
201
Mathieu Chartierde40d472015-10-15 17:47:48 -0700202inline void* RegType::operator new(size_t size, ScopedArenaAllocator* arena) {
203 return arena->Alloc(size, kArenaAllocMisc);
204}
205
Ian Rogers7b078e82014-09-10 14:44:24 -0700206} // namespace verifier
207} // namespace art
208
209#endif // ART_RUNTIME_VERIFIER_REG_TYPE_INL_H_