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