blob: 11a53e539de43d11bcd3b8a1d943064d342a813a [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"
25
26namespace art {
27namespace verifier {
28
29inline bool RegType::CanAccess(const RegType& other) const {
30 if (Equals(other)) {
31 return true; // Trivial accessibility.
32 } else {
33 bool this_unresolved = IsUnresolvedTypes();
34 bool other_unresolved = other.IsUnresolvedTypes();
35 if (!this_unresolved && !other_unresolved) {
36 return GetClass()->CanAccess(other.GetClass());
37 } else if (!other_unresolved) {
38 return other.GetClass()->IsPublic(); // Be conservative, only allow if other is public.
39 } else {
40 return false; // More complicated test not possible on unresolved types, be conservative.
41 }
42 }
43}
44
45inline bool RegType::CanAccessMember(mirror::Class* klass, uint32_t access_flags) const {
46 if ((access_flags & kAccPublic) != 0) {
47 return true;
48 }
49 if (!IsUnresolvedTypes()) {
50 return GetClass()->CanAccessMember(klass, access_flags);
51 } else {
52 return false; // More complicated test not possible on unresolved types, be conservative.
53 }
54}
55
56inline bool RegType::IsConstantBoolean() const {
57 if (!IsConstant()) {
58 return false;
59 } else {
60 const ConstantType* const_val = down_cast<const ConstantType*>(this);
61 return const_val->ConstantValue() >= 0 && const_val->ConstantValue() <= 1;
62 }
63}
64
65inline bool RegType::AssignableFrom(const RegType& lhs, const RegType& rhs, bool strict) {
66 if (lhs.Equals(rhs)) {
67 return true;
68 } else {
69 if (lhs.IsBoolean()) {
70 return rhs.IsBooleanTypes();
71 } else if (lhs.IsByte()) {
72 return rhs.IsByteTypes();
73 } else if (lhs.IsShort()) {
74 return rhs.IsShortTypes();
75 } else if (lhs.IsChar()) {
76 return rhs.IsCharTypes();
77 } else if (lhs.IsInteger()) {
78 return rhs.IsIntegralTypes();
79 } else if (lhs.IsFloat()) {
80 return rhs.IsFloatTypes();
81 } else if (lhs.IsLongLo()) {
82 return rhs.IsLongTypes();
83 } else if (lhs.IsDoubleLo()) {
84 return rhs.IsDoubleTypes();
Stephen Kyle40d35182014-10-03 13:47:56 +010085 } else if (lhs.IsConflict()) {
86 LOG(WARNING) << "RegType::AssignableFrom lhs is Conflict!";
87 return false;
Ian Rogers7b078e82014-09-10 14:44:24 -070088 } else {
89 CHECK(lhs.IsReferenceTypes())
90 << "Unexpected register type in IsAssignableFrom: '"
91 << lhs << "' := '" << rhs << "'";
92 if (rhs.IsZero()) {
93 return true; // All reference types can be assigned null.
94 } else if (!rhs.IsReferenceTypes()) {
95 return false; // Expect rhs to be a reference type.
96 } else if (lhs.IsJavaLangObject()) {
97 return true; // All reference types can be assigned to Object.
98 } else if (!strict && !lhs.IsUnresolvedTypes() && lhs.GetClass()->IsInterface()) {
99 // If we're not strict allow assignment to any interface, see comment in ClassJoin.
100 return true;
101 } else if (lhs.IsJavaLangObjectArray()) {
102 return rhs.IsObjectArrayTypes(); // All reference arrays may be assigned to Object[]
103 } else if (lhs.HasClass() && rhs.HasClass() &&
104 lhs.GetClass()->IsAssignableFrom(rhs.GetClass())) {
105 // We're assignable from the Class point-of-view.
106 return true;
107 } else {
108 // Unresolved types are only assignable for null and equality.
109 return false;
110 }
111 }
112 }
113}
114
115inline bool RegType::IsAssignableFrom(const RegType& src) const {
116 return AssignableFrom(*this, src, false);
117}
118
119inline bool RegType::IsStrictlyAssignableFrom(const RegType& src) const {
120 return AssignableFrom(*this, src, true);
121}
122
123inline const DoubleHiType* DoubleHiType::GetInstance() {
124 DCHECK(instance_ != nullptr);
125 return instance_;
126}
127
128inline const DoubleLoType* DoubleLoType::GetInstance() {
129 DCHECK(instance_ != nullptr);
130 return instance_;
131}
132
133inline const LongHiType* LongHiType::GetInstance() {
134 DCHECK(instance_ != nullptr);
135 return instance_;
136}
137
138inline const LongLoType* LongLoType::GetInstance() {
139 DCHECK(instance_ != nullptr);
140 return instance_;
141}
142
143inline const FloatType* FloatType::GetInstance() {
144 DCHECK(instance_ != nullptr);
145 return instance_;
146}
147
148inline const CharType* CharType::GetInstance() {
149 DCHECK(instance_ != nullptr);
150 return instance_;
151}
152
153inline const ShortType* ShortType::GetInstance() {
154 DCHECK(instance_ != nullptr);
155 return instance_;
156}
157
158inline const ByteType* ByteType::GetInstance() {
159 DCHECK(instance_ != nullptr);
160 return instance_;
161}
162
163
164inline const IntegerType* IntegerType::GetInstance() {
165 DCHECK(instance_ != nullptr);
166 return instance_;
167}
168
169inline const BooleanType* BooleanType::GetInstance() {
170 DCHECK(BooleanType::instance_ != nullptr);
171 return BooleanType::instance_;
172}
173
174inline const ConflictType* ConflictType::GetInstance() {
175 DCHECK(instance_ != nullptr);
176 return instance_;
177}
178
179inline const UndefinedType* UndefinedType::GetInstance() {
180 DCHECK(instance_ != nullptr);
181 return instance_;
182}
183
Mathieu Chartierde40d472015-10-15 17:47:48 -0700184inline void* RegType::operator new(size_t size, ScopedArenaAllocator* arena) {
185 return arena->Alloc(size, kArenaAllocMisc);
186}
187
Ian Rogers7b078e82014-09-10 14:44:24 -0700188} // namespace verifier
189} // namespace art
190
191#endif // ART_RUNTIME_VERIFIER_REG_TYPE_INL_H_