Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_MIRROR_CLASS_INL_H_ |
| 18 | #define ART_RUNTIME_MIRROR_CLASS_INL_H_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | |
| 20 | #include "class.h" |
| 21 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 22 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 23 | #include "art_method.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 24 | #include "art_method-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 25 | #include "class_loader.h" |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 26 | #include "common_throws.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 27 | #include "dex_cache.h" |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 28 | #include "dex_file.h" |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 29 | #include "gc/heap-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 30 | #include "iftable.h" |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 31 | #include "length_prefixed_array.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 32 | #include "object_array-inl.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 33 | #include "read_barrier-inl.h" |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 34 | #include "reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | #include "runtime.h" |
| 36 | #include "string.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 37 | #include "utils.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 38 | |
| 39 | namespace art { |
| 40 | namespace mirror { |
| 41 | |
Hiroshi Yamauchi | 25023c7 | 2014-05-09 11:45:53 -0700 | [diff] [blame] | 42 | template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 43 | inline uint32_t Class::GetObjectSize() { |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 44 | // Note: Extra parentheses to avoid the comma being interpreted as macro parameter separator. |
| 45 | DCHECK((!IsVariableSize<kVerifyFlags, kReadBarrierOption>())) << " class=" << PrettyTypeOf(this); |
Hiroshi Yamauchi | e01a520 | 2015-03-19 12:35:04 -0700 | [diff] [blame] | 46 | return GetField32(ObjectSizeOffset()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 49 | inline Class* Class::GetSuperClass() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 50 | // Can only get super class for loaded classes (hack for when runtime is |
| 51 | // initializing) |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 52 | DCHECK(IsLoaded() || IsErroneous() || !Runtime::Current()->IsStarted()) << IsLoaded(); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 53 | return GetFieldObject<Class>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 56 | inline ClassLoader* Class::GetClassLoader() { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 57 | return GetFieldObject<ClassLoader>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_)); |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Mathieu Chartier | c2f4d02 | 2014-03-03 16:11:42 -0800 | [diff] [blame] | 60 | template<VerifyObjectFlags kVerifyFlags> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 61 | inline DexCache* Class::GetDexCache() { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 62 | return GetFieldObject<DexCache, kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_)); |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 65 | inline LengthPrefixedArray<ArtMethod>* Class::GetDirectMethodsPtr() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 66 | DCHECK(IsLoaded() || IsErroneous()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 67 | return GetDirectMethodsPtrUnchecked(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 70 | inline LengthPrefixedArray<ArtMethod>* Class::GetDirectMethodsPtrUnchecked() { |
| 71 | return reinterpret_cast<LengthPrefixedArray<ArtMethod>*>( |
| 72 | GetField64(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_))); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 75 | inline LengthPrefixedArray<ArtMethod>* Class::GetVirtualMethodsPtrUnchecked() { |
| 76 | return reinterpret_cast<LengthPrefixedArray<ArtMethod>*>( |
| 77 | GetField64(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_))); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 80 | inline void Class::SetDirectMethodsPtr(LengthPrefixedArray<ArtMethod>* new_direct_methods) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 81 | DCHECK(GetDirectMethodsPtrUnchecked() == nullptr); |
| 82 | SetDirectMethodsPtrUnchecked(new_direct_methods); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 85 | inline void Class::SetDirectMethodsPtrUnchecked( |
| 86 | LengthPrefixedArray<ArtMethod>* new_direct_methods) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 87 | SetField64<false>(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), |
| 88 | reinterpret_cast<uint64_t>(new_direct_methods)); |
| 89 | } |
| 90 | |
| 91 | inline ArtMethod* Class::GetDirectMethodUnchecked(size_t i, size_t pointer_size) { |
| 92 | CheckPointerSize(pointer_size); |
| 93 | auto* methods = GetDirectMethodsPtrUnchecked(); |
| 94 | DCHECK(methods != nullptr); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 95 | return &methods->At(i, |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 96 | ArtMethod::Size(pointer_size), |
| 97 | ArtMethod::Alignment(pointer_size)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | inline ArtMethod* Class::GetDirectMethod(size_t i, size_t pointer_size) { |
| 101 | CheckPointerSize(pointer_size); |
| 102 | auto* methods = GetDirectMethodsPtr(); |
| 103 | DCHECK(methods != nullptr); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 104 | return &methods->At(i, |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 105 | ArtMethod::Size(pointer_size), |
| 106 | ArtMethod::Alignment(pointer_size)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 109 | template<VerifyObjectFlags kVerifyFlags> |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 110 | inline LengthPrefixedArray<ArtMethod>* Class::GetVirtualMethodsPtr() { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 111 | DCHECK(IsLoaded<kVerifyFlags>() || IsErroneous<kVerifyFlags>()); |
| 112 | return GetVirtualMethodsPtrUnchecked(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 115 | inline void Class::SetVirtualMethodsPtr(LengthPrefixedArray<ArtMethod>* new_virtual_methods) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 116 | // TODO: we reassign virtual methods to grow the table for miranda |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 117 | // methods.. they should really just be assigned once. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 118 | SetField64<false>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), |
| 119 | reinterpret_cast<uint64_t>(new_virtual_methods)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 122 | template<VerifyObjectFlags kVerifyFlags> |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 123 | inline ArtMethod* Class::GetVirtualMethod(size_t i, size_t pointer_size) { |
| 124 | CheckPointerSize(pointer_size); |
Hiroshi Yamauchi | f4c15a1 | 2014-10-20 16:56:58 -0700 | [diff] [blame] | 125 | DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>()) |
| 126 | << PrettyClass(this) << " status=" << GetStatus(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 127 | return GetVirtualMethodUnchecked(i, pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 130 | inline ArtMethod* Class::GetVirtualMethodDuringLinking(size_t i, size_t pointer_size) { |
| 131 | CheckPointerSize(pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 132 | DCHECK(IsLoaded() || IsErroneous()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 133 | return GetVirtualMethodUnchecked(i, pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 136 | inline ArtMethod* Class::GetVirtualMethodUnchecked(size_t i, size_t pointer_size) { |
| 137 | CheckPointerSize(pointer_size); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 138 | LengthPrefixedArray<ArtMethod>* methods = GetVirtualMethodsPtrUnchecked(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 139 | DCHECK(methods != nullptr); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 140 | return &methods->At(i, |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 141 | ArtMethod::Size(pointer_size), |
| 142 | ArtMethod::Alignment(pointer_size)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 145 | inline PointerArray* Class::GetVTable() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 146 | DCHECK(IsResolved() || IsErroneous()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 147 | return GetFieldObject<PointerArray>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 150 | inline PointerArray* Class::GetVTableDuringLinking() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 151 | DCHECK(IsLoaded() || IsErroneous()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 152 | return GetFieldObject<PointerArray>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 155 | inline void Class::SetVTable(PointerArray* new_vtable) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 156 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 159 | inline MemberOffset Class::EmbeddedImTableEntryOffset(uint32_t i, size_t pointer_size) { |
| 160 | DCHECK_LT(i, kImtSize); |
| 161 | return MemberOffset( |
| 162 | EmbeddedImTableOffset(pointer_size).Uint32Value() + i * ImTableEntrySize(pointer_size)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 165 | inline ArtMethod* Class::GetEmbeddedImTableEntry(uint32_t i, size_t pointer_size) { |
| 166 | DCHECK(ShouldHaveEmbeddedImtAndVTable()); |
| 167 | return GetFieldPtrWithSize<ArtMethod*>( |
| 168 | EmbeddedImTableEntryOffset(i, pointer_size), pointer_size); |
| 169 | } |
| 170 | |
| 171 | inline void Class::SetEmbeddedImTableEntry(uint32_t i, ArtMethod* method, size_t pointer_size) { |
| 172 | DCHECK(ShouldHaveEmbeddedImtAndVTable()); |
| 173 | SetFieldPtrWithSize<false>(EmbeddedImTableEntryOffset(i, pointer_size), method, pointer_size); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 176 | inline bool Class::HasVTable() { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 177 | return GetVTable() != nullptr || ShouldHaveEmbeddedImtAndVTable(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | inline int32_t Class::GetVTableLength() { |
| 181 | if (ShouldHaveEmbeddedImtAndVTable()) { |
| 182 | return GetEmbeddedVTableLength(); |
| 183 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 184 | return GetVTable() != nullptr ? GetVTable()->GetLength() : 0; |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 187 | inline ArtMethod* Class::GetVTableEntry(uint32_t i, size_t pointer_size) { |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 188 | if (ShouldHaveEmbeddedImtAndVTable()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 189 | return GetEmbeddedVTableEntry(i, pointer_size); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 190 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 191 | auto* vtable = GetVTable(); |
| 192 | DCHECK(vtable != nullptr); |
| 193 | return vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | inline int32_t Class::GetEmbeddedVTableLength() { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 197 | return GetField32(MemberOffset(EmbeddedVTableLengthOffset())); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | inline void Class::SetEmbeddedVTableLength(int32_t len) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 201 | SetField32<false>(MemberOffset(EmbeddedVTableLengthOffset()), len); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 204 | inline MemberOffset Class::EmbeddedVTableEntryOffset(uint32_t i, size_t pointer_size) { |
| 205 | return MemberOffset( |
| 206 | EmbeddedVTableOffset(pointer_size).Uint32Value() + i * VTableEntrySize(pointer_size)); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 209 | inline ArtMethod* Class::GetEmbeddedVTableEntry(uint32_t i, size_t pointer_size) { |
| 210 | return GetFieldPtrWithSize<ArtMethod*>(EmbeddedVTableEntryOffset(i, pointer_size), pointer_size); |
| 211 | } |
| 212 | |
| 213 | inline void Class::SetEmbeddedVTableEntryUnchecked( |
| 214 | uint32_t i, ArtMethod* method, size_t pointer_size) { |
| 215 | SetFieldPtrWithSize<false>(EmbeddedVTableEntryOffset(i, pointer_size), method, pointer_size); |
| 216 | } |
| 217 | |
| 218 | inline void Class::SetEmbeddedVTableEntry(uint32_t i, ArtMethod* method, size_t pointer_size) { |
| 219 | auto* vtable = GetVTableDuringLinking(); |
| 220 | CHECK_EQ(method, vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size)); |
| 221 | SetEmbeddedVTableEntryUnchecked(i, method, pointer_size); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 224 | inline bool Class::Implements(Class* klass) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 225 | DCHECK(klass != nullptr); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 226 | DCHECK(klass->IsInterface()) << PrettyClass(this); |
| 227 | // All interfaces implemented directly and by our superclass, and |
| 228 | // recursively all super-interfaces of those interfaces, are listed |
| 229 | // in iftable_, so we can just do a linear scan through that. |
| 230 | int32_t iftable_count = GetIfTableCount(); |
| 231 | IfTable* iftable = GetIfTable(); |
| 232 | for (int32_t i = 0; i < iftable_count; i++) { |
| 233 | if (iftable->GetInterface(i) == klass) { |
| 234 | return true; |
| 235 | } |
| 236 | } |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | // Determine whether "this" is assignable from "src", where both of these |
| 241 | // are array classes. |
| 242 | // |
| 243 | // Consider an array class, e.g. Y[][], where Y is a subclass of X. |
| 244 | // Y[][] = Y[][] --> true (identity) |
| 245 | // X[][] = Y[][] --> true (element superclass) |
| 246 | // Y = Y[][] --> false |
| 247 | // Y[] = Y[][] --> false |
| 248 | // Object = Y[][] --> true (everything is an object) |
| 249 | // Object[] = Y[][] --> true |
| 250 | // Object[][] = Y[][] --> true |
| 251 | // Object[][][] = Y[][] --> false (too many []s) |
| 252 | // Serializable = Y[][] --> true (all arrays are Serializable) |
| 253 | // Serializable[] = Y[][] --> true |
| 254 | // Serializable[][] = Y[][] --> false (unless Y is Serializable) |
| 255 | // |
| 256 | // Don't forget about primitive types. |
| 257 | // Object[] = int[] --> false |
| 258 | // |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 259 | inline bool Class::IsArrayAssignableFromArray(Class* src) { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 260 | DCHECK(IsArrayClass()) << PrettyClass(this); |
| 261 | DCHECK(src->IsArrayClass()) << PrettyClass(src); |
| 262 | return GetComponentType()->IsAssignableFrom(src->GetComponentType()); |
| 263 | } |
| 264 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 265 | inline bool Class::IsAssignableFromArray(Class* src) { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 266 | DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom |
| 267 | DCHECK(src->IsArrayClass()) << PrettyClass(src); |
| 268 | if (!IsArrayClass()) { |
| 269 | // If "this" is not also an array, it must be Object. |
| 270 | // src's super should be java_lang_Object, since it is an array. |
| 271 | Class* java_lang_Object = src->GetSuperClass(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 272 | DCHECK(java_lang_Object != nullptr) << PrettyClass(src); |
| 273 | DCHECK(java_lang_Object->GetSuperClass() == nullptr) << PrettyClass(src); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 274 | return this == java_lang_Object; |
| 275 | } |
| 276 | return IsArrayAssignableFromArray(src); |
| 277 | } |
| 278 | |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame] | 279 | template <bool throw_on_failure, bool use_referrers_cache> |
| 280 | inline bool Class::ResolvedFieldAccessTest(Class* access_to, ArtField* field, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 281 | uint32_t field_idx, DexCache* dex_cache) { |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame] | 282 | DCHECK_EQ(use_referrers_cache, dex_cache == nullptr); |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 283 | if (UNLIKELY(!this->CanAccess(access_to))) { |
| 284 | // The referrer class can't access the field's declaring class but may still be able |
| 285 | // to access the field if the FieldId specifies an accessible subclass of the declaring |
| 286 | // class rather than the declaring class itself. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 287 | DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache; |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 288 | uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetFieldId(field_idx).class_idx_; |
| 289 | // The referenced class has already been resolved with the field, get it from the dex cache. |
| 290 | Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx); |
| 291 | DCHECK(dex_access_to != nullptr); |
| 292 | if (UNLIKELY(!this->CanAccess(dex_access_to))) { |
| 293 | if (throw_on_failure) { |
| 294 | ThrowIllegalAccessErrorClass(this, dex_access_to); |
| 295 | } |
| 296 | return false; |
| 297 | } |
| 298 | DCHECK_EQ(this->CanAccessMember(access_to, field->GetAccessFlags()), |
| 299 | this->CanAccessMember(dex_access_to, field->GetAccessFlags())); |
| 300 | } |
| 301 | if (LIKELY(this->CanAccessMember(access_to, field->GetAccessFlags()))) { |
| 302 | return true; |
| 303 | } |
| 304 | if (throw_on_failure) { |
| 305 | ThrowIllegalAccessErrorField(this, field); |
| 306 | } |
| 307 | return false; |
| 308 | } |
| 309 | |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame] | 310 | template <bool throw_on_failure, bool use_referrers_cache, InvokeType throw_invoke_type> |
| 311 | inline bool Class::ResolvedMethodAccessTest(Class* access_to, ArtMethod* method, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 312 | uint32_t method_idx, DexCache* dex_cache) { |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 313 | static_assert(throw_on_failure || throw_invoke_type == kStatic, "Non-default throw invoke type"); |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame] | 314 | DCHECK_EQ(use_referrers_cache, dex_cache == nullptr); |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 315 | if (UNLIKELY(!this->CanAccess(access_to))) { |
| 316 | // The referrer class can't access the method's declaring class but may still be able |
| 317 | // to access the method if the MethodId specifies an accessible subclass of the declaring |
| 318 | // class rather than the declaring class itself. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 319 | DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache; |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 320 | uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_; |
| 321 | // The referenced class has already been resolved with the method, get it from the dex cache. |
| 322 | Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx); |
| 323 | DCHECK(dex_access_to != nullptr); |
| 324 | if (UNLIKELY(!this->CanAccess(dex_access_to))) { |
| 325 | if (throw_on_failure) { |
| 326 | ThrowIllegalAccessErrorClassForMethodDispatch(this, dex_access_to, |
| 327 | method, throw_invoke_type); |
| 328 | } |
| 329 | return false; |
| 330 | } |
| 331 | DCHECK_EQ(this->CanAccessMember(access_to, method->GetAccessFlags()), |
| 332 | this->CanAccessMember(dex_access_to, method->GetAccessFlags())); |
| 333 | } |
| 334 | if (LIKELY(this->CanAccessMember(access_to, method->GetAccessFlags()))) { |
| 335 | return true; |
| 336 | } |
| 337 | if (throw_on_failure) { |
| 338 | ThrowIllegalAccessErrorMethod(this, method); |
| 339 | } |
| 340 | return false; |
| 341 | } |
| 342 | |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame] | 343 | inline bool Class::CanAccessResolvedField(Class* access_to, ArtField* field, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 344 | DexCache* dex_cache, uint32_t field_idx) { |
| 345 | return ResolvedFieldAccessTest<false, false>(access_to, field, field_idx, dex_cache); |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | inline bool Class::CheckResolvedFieldAccess(Class* access_to, ArtField* field, |
| 349 | uint32_t field_idx) { |
| 350 | return ResolvedFieldAccessTest<true, true>(access_to, field, field_idx, nullptr); |
| 351 | } |
| 352 | |
| 353 | inline bool Class::CanAccessResolvedMethod(Class* access_to, ArtMethod* method, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 354 | DexCache* dex_cache, uint32_t method_idx) { |
| 355 | return ResolvedMethodAccessTest<false, false, kStatic>(access_to, method, method_idx, dex_cache); |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | template <InvokeType throw_invoke_type> |
| 359 | inline bool Class::CheckResolvedMethodAccess(Class* access_to, ArtMethod* method, |
| 360 | uint32_t method_idx) { |
| 361 | return ResolvedMethodAccessTest<true, true, throw_invoke_type>(access_to, method, method_idx, |
| 362 | nullptr); |
| 363 | } |
| 364 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 365 | inline bool Class::IsSubClass(Class* klass) { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 366 | DCHECK(!IsInterface()) << PrettyClass(this); |
| 367 | DCHECK(!IsArrayClass()) << PrettyClass(this); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 368 | Class* current = this; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 369 | do { |
| 370 | if (current == klass) { |
| 371 | return true; |
| 372 | } |
| 373 | current = current->GetSuperClass(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 374 | } while (current != nullptr); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 375 | return false; |
| 376 | } |
| 377 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 378 | inline ArtMethod* Class::FindVirtualMethodForInterface(ArtMethod* method, size_t pointer_size) { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 379 | Class* declaring_class = method->GetDeclaringClass(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 380 | DCHECK(declaring_class != nullptr) << PrettyClass(this); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 381 | DCHECK(declaring_class->IsInterface()) << PrettyMethod(method); |
| 382 | // TODO cache to improve lookup speed |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 383 | const int32_t iftable_count = GetIfTableCount(); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 384 | IfTable* iftable = GetIfTable(); |
| 385 | for (int32_t i = 0; i < iftable_count; i++) { |
| 386 | if (iftable->GetInterface(i) == declaring_class) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 387 | return iftable->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>( |
| 388 | method->GetMethodIndex(), pointer_size); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 389 | } |
| 390 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 391 | return nullptr; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 394 | inline ArtMethod* Class::FindVirtualMethodForVirtual(ArtMethod* method, size_t pointer_size) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 395 | // Only miranda or default methods may come from interfaces and be used as a virtual. |
| 396 | DCHECK(!method->GetDeclaringClass()->IsInterface() || method->IsDefault() || method->IsMiranda()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 397 | // The argument method may from a super class. |
| 398 | // Use the index to a potentially overridden one for this instance's class. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 399 | return GetVTableEntry(method->GetMethodIndex(), pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 400 | } |
| 401 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 402 | inline ArtMethod* Class::FindVirtualMethodForSuper(ArtMethod* method, size_t pointer_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 403 | DCHECK(!method->GetDeclaringClass()->IsInterface()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 404 | return GetSuperClass()->GetVTableEntry(method->GetMethodIndex(), pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 405 | } |
| 406 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 407 | inline ArtMethod* Class::FindVirtualMethodForVirtualOrInterface(ArtMethod* method, |
| 408 | size_t pointer_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 409 | if (method->IsDirect()) { |
| 410 | return method; |
| 411 | } |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 412 | if (method->GetDeclaringClass()->IsInterface() && !method->IsMiranda()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 413 | return FindVirtualMethodForInterface(method, pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 414 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 415 | return FindVirtualMethodForVirtual(method, pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 418 | inline IfTable* Class::GetIfTable() { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 419 | return GetFieldObject<IfTable>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 422 | inline int32_t Class::GetIfTableCount() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 423 | IfTable* iftable = GetIfTable(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 424 | if (iftable == nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 425 | return 0; |
| 426 | } |
| 427 | return iftable->Count(); |
| 428 | } |
| 429 | |
| 430 | inline void Class::SetIfTable(IfTable* new_iftable) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 431 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 434 | inline LengthPrefixedArray<ArtField>* Class::GetIFieldsPtr() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 435 | DCHECK(IsLoaded() || IsErroneous()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 436 | return GetFieldPtr<LengthPrefixedArray<ArtField>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 439 | inline MemberOffset Class::GetFirstReferenceInstanceFieldOffset() { |
| 440 | Class* super_class = GetSuperClass(); |
| 441 | return (super_class != nullptr) |
| 442 | ? MemberOffset(RoundUp(super_class->GetObjectSize(), |
| 443 | sizeof(mirror::HeapReference<mirror::Object>))) |
| 444 | : ClassOffset(); |
| 445 | } |
| 446 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 447 | inline MemberOffset Class::GetFirstReferenceStaticFieldOffset(size_t pointer_size) { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 448 | DCHECK(IsResolved()); |
| 449 | uint32_t base = sizeof(mirror::Class); // Static fields come after the class. |
| 450 | if (ShouldHaveEmbeddedImtAndVTable()) { |
| 451 | // Static fields come after the embedded tables. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 452 | base = mirror::Class::ComputeClassSize( |
| 453 | true, GetEmbeddedVTableLength(), 0, 0, 0, 0, 0, pointer_size); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 454 | } |
| 455 | return MemberOffset(base); |
| 456 | } |
| 457 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 458 | inline MemberOffset Class::GetFirstReferenceStaticFieldOffsetDuringLinking(size_t pointer_size) { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 459 | DCHECK(IsLoaded()); |
| 460 | uint32_t base = sizeof(mirror::Class); // Static fields come after the class. |
| 461 | if (ShouldHaveEmbeddedImtAndVTable()) { |
| 462 | // Static fields come after the embedded tables. |
| 463 | base = mirror::Class::ComputeClassSize(true, GetVTableDuringLinking()->GetLength(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 464 | 0, 0, 0, 0, 0, pointer_size); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 465 | } |
| 466 | return MemberOffset(base); |
| 467 | } |
| 468 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 469 | inline void Class::SetIFieldsPtr(LengthPrefixedArray<ArtField>* new_ifields) { |
| 470 | DCHECK(GetIFieldsPtrUnchecked() == nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 471 | return SetFieldPtr<false>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 472 | } |
| 473 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 474 | inline void Class::SetIFieldsPtrUnchecked(LengthPrefixedArray<ArtField>* new_ifields) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 475 | SetFieldPtr<false, true, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields); |
| 476 | } |
| 477 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 478 | inline LengthPrefixedArray<ArtField>* Class::GetSFieldsPtrUnchecked() { |
| 479 | return GetFieldPtr<LengthPrefixedArray<ArtField>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 482 | inline LengthPrefixedArray<ArtField>* Class::GetIFieldsPtrUnchecked() { |
| 483 | return GetFieldPtr<LengthPrefixedArray<ArtField>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 486 | inline LengthPrefixedArray<ArtField>* Class::GetSFieldsPtr() { |
Mathieu Chartier | 987ca8b | 2015-03-15 14:19:14 -0700 | [diff] [blame] | 487 | DCHECK(IsLoaded() || IsErroneous()) << GetStatus(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 488 | return GetSFieldsPtrUnchecked(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 489 | } |
| 490 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 491 | inline void Class::SetSFieldsPtr(LengthPrefixedArray<ArtField>* new_sfields) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 492 | DCHECK((IsRetired() && new_sfields == nullptr) || |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 493 | GetFieldPtr<ArtField*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_)) == nullptr); |
| 494 | SetFieldPtr<false>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 495 | } |
| 496 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 497 | inline void Class::SetSFieldsPtrUnchecked(LengthPrefixedArray<ArtField>* new_sfields) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 498 | SetFieldPtr<false, true, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 499 | } |
| 500 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 501 | inline ArtField* Class::GetStaticField(uint32_t i) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 502 | return &GetSFieldsPtr()->At(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 503 | } |
| 504 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 505 | inline ArtField* Class::GetInstanceField(uint32_t i) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 506 | return &GetIFieldsPtr()->At(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 509 | template<VerifyObjectFlags kVerifyFlags> |
| 510 | inline uint32_t Class::GetReferenceInstanceOffsets() { |
| 511 | DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>()); |
| 512 | return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_)); |
| 513 | } |
| 514 | |
| 515 | inline void Class::SetClinitThreadId(pid_t new_clinit_thread_id) { |
| 516 | if (Runtime::Current()->IsActiveTransaction()) { |
| 517 | SetField32<true>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id); |
| 518 | } else { |
| 519 | SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id); |
| 520 | } |
| 521 | } |
| 522 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 523 | template<VerifyObjectFlags kVerifyFlags> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 524 | inline uint32_t Class::GetAccessFlags() { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 525 | // Check class is loaded/retired or this is java.lang.String that has a |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 526 | // circularity issue during loading the names of its members |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 527 | DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() || |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 528 | IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() || |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 529 | this == String::GetJavaLangString()) |
Andreas Gampe | a692866 | 2014-12-12 11:06:00 -0800 | [diff] [blame] | 530 | << "IsIdxLoaded=" << IsIdxLoaded<kVerifyFlags>() |
| 531 | << " IsRetired=" << IsRetired<kVerifyFlags>() |
| 532 | << " IsErroneous=" << |
| 533 | IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() |
| 534 | << " IsString=" << (this == String::GetJavaLangString()) |
Andreas Gampe | a692866 | 2014-12-12 11:06:00 -0800 | [diff] [blame] | 535 | << " descriptor=" << PrettyDescriptor(this); |
Hiroshi Yamauchi | e01a520 | 2015-03-19 12:35:04 -0700 | [diff] [blame] | 536 | return GetField32<kVerifyFlags>(AccessFlagsOffset()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 537 | } |
| 538 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 539 | inline String* Class::GetName() { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 540 | return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(Class, name_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 541 | } |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 542 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 543 | inline void Class::SetName(String* name) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 544 | if (Runtime::Current()->IsActiveTransaction()) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 545 | SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 546 | } else { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 547 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 548 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 549 | } |
| 550 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 551 | template<VerifyObjectFlags kVerifyFlags> |
| 552 | inline Primitive::Type Class::GetPrimitiveType() { |
Roland Levillain | 33d6903 | 2015-06-18 18:20:59 +0100 | [diff] [blame] | 553 | static_assert(sizeof(Primitive::Type) == sizeof(int32_t), |
| 554 | "art::Primitive::Type and int32_t have different sizes."); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 555 | int32_t v32 = GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_)); |
| 556 | Primitive::Type type = static_cast<Primitive::Type>(v32 & 0xFFFF); |
| 557 | DCHECK_EQ(static_cast<size_t>(v32 >> 16), Primitive::ComponentSizeShift(type)); |
| 558 | return type; |
| 559 | } |
| 560 | |
| 561 | template<VerifyObjectFlags kVerifyFlags> |
| 562 | inline size_t Class::GetPrimitiveTypeSizeShift() { |
Roland Levillain | 33d6903 | 2015-06-18 18:20:59 +0100 | [diff] [blame] | 563 | static_assert(sizeof(Primitive::Type) == sizeof(int32_t), |
| 564 | "art::Primitive::Type and int32_t have different sizes."); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 565 | int32_t v32 = GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_)); |
| 566 | size_t size_shift = static_cast<Primitive::Type>(v32 >> 16); |
| 567 | DCHECK_EQ(size_shift, Primitive::ComponentSizeShift(static_cast<Primitive::Type>(v32 & 0xFFFF))); |
| 568 | return size_shift; |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 571 | inline void Class::CheckObjectAlloc() { |
Hiroshi Yamauchi | 4cd662e | 2014-04-03 16:28:10 -0700 | [diff] [blame] | 572 | DCHECK(!IsArrayClass()) |
| 573 | << PrettyClass(this) |
| 574 | << "A array shouldn't be allocated through this " |
| 575 | << "as it requires a pre-fence visitor that sets the class size."; |
| 576 | DCHECK(!IsClassClass()) |
| 577 | << PrettyClass(this) |
| 578 | << "A class object shouldn't be allocated through this " |
| 579 | << "as it requires a pre-fence visitor that sets the class size."; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 580 | DCHECK(!IsStringClass()) |
| 581 | << PrettyClass(this) |
| 582 | << "A string shouldn't be allocated through this " |
| 583 | << "as it requires a pre-fence visitor that sets the class size."; |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 584 | DCHECK(IsInstantiable()) << PrettyClass(this); |
| 585 | // TODO: decide whether we want this check. It currently fails during bootstrap. |
| 586 | // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this); |
| 587 | DCHECK_GE(this->object_size_, sizeof(Object)); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Mathieu Chartier | 8668c3c | 2014-04-24 16:48:11 -0700 | [diff] [blame] | 590 | template<bool kIsInstrumented, bool kCheckAddFinalizer> |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 591 | inline Object* Class::Alloc(Thread* self, gc::AllocatorType allocator_type) { |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 592 | CheckObjectAlloc(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 593 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 8668c3c | 2014-04-24 16:48:11 -0700 | [diff] [blame] | 594 | const bool add_finalizer = kCheckAddFinalizer && IsFinalizable(); |
| 595 | if (!kCheckAddFinalizer) { |
| 596 | DCHECK(!IsFinalizable()); |
| 597 | } |
| 598 | mirror::Object* obj = |
| 599 | heap->AllocObjectWithAllocator<kIsInstrumented, false>(self, this, this->object_size_, |
| 600 | allocator_type, VoidFunctor()); |
| 601 | if (add_finalizer && LIKELY(obj != nullptr)) { |
| 602 | heap->AddFinalizerReference(self, &obj); |
Pavel Vyssotski | 3ac90da | 2014-12-02 19:54:50 +0600 | [diff] [blame] | 603 | if (UNLIKELY(self->IsExceptionPending())) { |
| 604 | // Failed to allocate finalizer reference, it means that the whole allocation failed. |
| 605 | obj = nullptr; |
| 606 | } |
Mathieu Chartier | 8668c3c | 2014-04-24 16:48:11 -0700 | [diff] [blame] | 607 | } |
| 608 | return obj; |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | inline Object* Class::AllocObject(Thread* self) { |
| 612 | return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
| 613 | } |
| 614 | |
| 615 | inline Object* Class::AllocNonMovableObject(Thread* self) { |
| 616 | return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentNonMovingAllocator()); |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 619 | inline uint32_t Class::ComputeClassSize(bool has_embedded_tables, |
| 620 | uint32_t num_vtable_entries, |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 621 | uint32_t num_8bit_static_fields, |
| 622 | uint32_t num_16bit_static_fields, |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 623 | uint32_t num_32bit_static_fields, |
| 624 | uint32_t num_64bit_static_fields, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 625 | uint32_t num_ref_static_fields, |
| 626 | size_t pointer_size) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 627 | // Space used by java.lang.Class and its instance fields. |
| 628 | uint32_t size = sizeof(Class); |
| 629 | // Space used by embedded tables. |
| 630 | if (has_embedded_tables) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 631 | const uint32_t embedded_imt_size = kImtSize * ImTableEntrySize(pointer_size); |
| 632 | const uint32_t embedded_vtable_size = num_vtable_entries * VTableEntrySize(pointer_size); |
| 633 | size = RoundUp(size + sizeof(uint32_t) /* embedded vtable len */, pointer_size) + |
| 634 | embedded_imt_size + embedded_vtable_size; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 635 | } |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 636 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 637 | // Space used by reference statics. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 638 | size += num_ref_static_fields * sizeof(HeapReference<Object>); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 639 | if (!IsAligned<8>(size) && num_64bit_static_fields > 0) { |
| 640 | uint32_t gap = 8 - (size & 0x7); |
| 641 | size += gap; // will be padded |
| 642 | // Shuffle 4-byte fields forward. |
| 643 | while (gap >= sizeof(uint32_t) && num_32bit_static_fields != 0) { |
| 644 | --num_32bit_static_fields; |
| 645 | gap -= sizeof(uint32_t); |
| 646 | } |
| 647 | // Shuffle 2-byte fields forward. |
| 648 | while (gap >= sizeof(uint16_t) && num_16bit_static_fields != 0) { |
| 649 | --num_16bit_static_fields; |
| 650 | gap -= sizeof(uint16_t); |
| 651 | } |
| 652 | // Shuffle byte fields forward. |
| 653 | while (gap >= sizeof(uint8_t) && num_8bit_static_fields != 0) { |
| 654 | --num_8bit_static_fields; |
| 655 | gap -= sizeof(uint8_t); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 656 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 657 | } |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 658 | // Guaranteed to be at least 4 byte aligned. No need for further alignments. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 659 | // Space used for primitive static fields. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 660 | size += num_8bit_static_fields * sizeof(uint8_t) + num_16bit_static_fields * sizeof(uint16_t) + |
| 661 | num_32bit_static_fields * sizeof(uint32_t) + num_64bit_static_fields * sizeof(uint64_t); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 662 | return size; |
| 663 | } |
| 664 | |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 665 | template <typename Visitor> |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 666 | inline void Class::VisitReferences(mirror::Class* klass, const Visitor& visitor) { |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 667 | VisitInstanceFieldsReferences(klass, visitor); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 668 | // Right after a class is allocated, but not yet loaded |
Igor Murashkin | c449e8b | 2015-06-10 15:56:42 -0700 | [diff] [blame] | 669 | // (kStatusNotReady, see ClassLinker::LoadClass()), GC may find it |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 670 | // and scan it. IsTemp() may call Class::GetAccessFlags() but may |
| 671 | // fail in the DCHECK in Class::GetAccessFlags() because the class |
| 672 | // status is kStatusNotReady. To avoid it, rely on IsResolved() |
| 673 | // only. This is fine because a temp class never goes into the |
| 674 | // kStatusResolved state. |
| 675 | if (IsResolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 676 | // Temp classes don't ever populate imt/vtable or static fields and they are not even |
Hiroshi Yamauchi | f4c15a1 | 2014-10-20 16:56:58 -0700 | [diff] [blame] | 677 | // allocated with the right size for those. Also, unresolved classes don't have fields |
| 678 | // linked yet. |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 679 | VisitStaticFieldsReferences(this, visitor); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 680 | } |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 681 | // Since this class is reachable, we must also visit the associated roots when we scan it. |
| 682 | VisitNativeRoots(visitor, Runtime::Current()->GetClassLinker()->GetImagePointerSize()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 685 | template<ReadBarrierOption kReadBarrierOption> |
| 686 | inline bool Class::IsReferenceClass() const { |
| 687 | return this == Reference::GetJavaLangRefReference<kReadBarrierOption>(); |
| 688 | } |
| 689 | |
Hiroshi Yamauchi | 25023c7 | 2014-05-09 11:45:53 -0700 | [diff] [blame] | 690 | template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> |
| 691 | inline bool Class::IsClassClass() { |
| 692 | Class* java_lang_Class = GetClass<kVerifyFlags, kReadBarrierOption>()-> |
| 693 | template GetClass<kVerifyFlags, kReadBarrierOption>(); |
| 694 | return this == java_lang_Class; |
| 695 | } |
| 696 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 697 | inline const DexFile& Class::GetDexFile() { |
| 698 | return *GetDexCache()->GetDexFile(); |
| 699 | } |
| 700 | |
| 701 | inline bool Class::DescriptorEquals(const char* match) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 702 | if (IsArrayClass()) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 703 | return match[0] == '[' && GetComponentType()->DescriptorEquals(match + 1); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 704 | } else if (IsPrimitive()) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 705 | return strcmp(Primitive::Descriptor(GetPrimitiveType()), match) == 0; |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 706 | } else if (IsProxyClass()) { |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 707 | return ProxyDescriptorEquals(match); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 708 | } else { |
| 709 | const DexFile& dex_file = GetDexFile(); |
| 710 | const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_); |
| 711 | return strcmp(dex_file.GetTypeDescriptor(type_id), match) == 0; |
| 712 | } |
| 713 | } |
| 714 | |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 715 | inline void Class::AssertInitializedOrInitializingInThread(Thread* self) { |
| 716 | if (kIsDebugBuild && !IsInitialized()) { |
| 717 | CHECK(IsInitializing()) << PrettyClass(this) << " is not initializing: " << GetStatus(); |
| 718 | CHECK_EQ(GetClinitThreadId(), self->GetTid()) << PrettyClass(this) |
| 719 | << " is initializing in a different thread"; |
| 720 | } |
| 721 | } |
| 722 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 723 | inline ObjectArray<Class>* Class::GetInterfaces() { |
| 724 | CHECK(IsProxyClass()); |
| 725 | // First static field. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 726 | auto* field = GetStaticField(0); |
| 727 | DCHECK_STREQ(field->GetName(), "interfaces"); |
| 728 | MemberOffset field_offset = field->GetOffset(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 729 | return GetFieldObject<ObjectArray<Class>>(field_offset); |
| 730 | } |
| 731 | |
| 732 | inline ObjectArray<ObjectArray<Class>>* Class::GetThrows() { |
| 733 | CHECK(IsProxyClass()); |
| 734 | // Second static field. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 735 | auto* field = GetStaticField(1); |
| 736 | DCHECK_STREQ(field->GetName(), "throws"); |
| 737 | MemberOffset field_offset = field->GetOffset(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 738 | return GetFieldObject<ObjectArray<ObjectArray<Class>>>(field_offset); |
| 739 | } |
| 740 | |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 741 | inline MemberOffset Class::GetDisableIntrinsicFlagOffset() { |
| 742 | CHECK(IsReferenceClass()); |
| 743 | // First static field |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 744 | auto* field = GetStaticField(0); |
| 745 | DCHECK_STREQ(field->GetName(), "disableIntrinsic"); |
| 746 | return field->GetOffset(); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | inline MemberOffset Class::GetSlowPathFlagOffset() { |
| 750 | CHECK(IsReferenceClass()); |
| 751 | // Second static field |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 752 | auto* field = GetStaticField(1); |
| 753 | DCHECK_STREQ(field->GetName(), "slowPathEnabled"); |
| 754 | return field->GetOffset(); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | inline bool Class::GetSlowPathEnabled() { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 758 | return GetFieldBoolean(GetSlowPathFlagOffset()); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | inline void Class::SetSlowPath(bool enabled) { |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 762 | SetFieldBoolean<false, false>(GetSlowPathFlagOffset(), enabled); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 765 | inline void Class::InitializeClassVisitor::operator()( |
| 766 | mirror::Object* obj, size_t usable_size) const { |
| 767 | DCHECK_LE(class_size_, usable_size); |
| 768 | // Avoid AsClass as object is not yet in live bitmap or allocation stack. |
| 769 | mirror::Class* klass = down_cast<mirror::Class*>(obj); |
| 770 | // DCHECK(klass->IsClass()); |
| 771 | klass->SetClassSize(class_size_); |
| 772 | klass->SetPrimitiveType(Primitive::kPrimNot); // Default to not being primitive. |
| 773 | klass->SetDexClassDefIndex(DexFile::kDexNoIndex16); // Default to no valid class def index. |
| 774 | klass->SetDexTypeIndex(DexFile::kDexNoIndex16); // Default to no valid type index. |
| 775 | } |
| 776 | |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 777 | inline void Class::SetAccessFlags(uint32_t new_access_flags) { |
| 778 | // Called inside a transaction when setting pre-verified flag during boot image compilation. |
| 779 | if (Runtime::Current()->IsActiveTransaction()) { |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 780 | SetField32<true>(AccessFlagsOffset(), new_access_flags); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 781 | } else { |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 782 | SetField32<false>(AccessFlagsOffset(), new_access_flags); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | inline void Class::SetClassFlags(uint32_t new_flags) { |
| 787 | if (Runtime::Current()->IsActiveTransaction()) { |
| 788 | SetField32<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_flags_), new_flags); |
| 789 | } else { |
| 790 | SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_flags_), new_flags); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 794 | inline uint32_t Class::NumDirectInterfaces() { |
| 795 | if (IsPrimitive()) { |
| 796 | return 0; |
| 797 | } else if (IsArrayClass()) { |
| 798 | return 2; |
| 799 | } else if (IsProxyClass()) { |
| 800 | mirror::ObjectArray<mirror::Class>* interfaces = GetInterfaces(); |
| 801 | return interfaces != nullptr ? interfaces->GetLength() : 0; |
| 802 | } else { |
| 803 | const DexFile::TypeList* interfaces = GetInterfaceTypeList(); |
| 804 | if (interfaces == nullptr) { |
| 805 | return 0; |
| 806 | } else { |
| 807 | return interfaces->Size(); |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 812 | inline void Class::SetDexCacheStrings(GcRoot<String>* new_dex_cache_strings) { |
| 813 | SetFieldPtr<false>(DexCacheStringsOffset(), new_dex_cache_strings); |
Mathieu Chartier | eace458 | 2014-11-24 18:29:54 -0800 | [diff] [blame] | 814 | } |
| 815 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 816 | inline GcRoot<String>* Class::GetDexCacheStrings() { |
| 817 | return GetFieldPtr<GcRoot<String>*>(DexCacheStringsOffset()); |
Mathieu Chartier | eace458 | 2014-11-24 18:29:54 -0800 | [diff] [blame] | 818 | } |
| 819 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 820 | template<class Visitor> |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 821 | void mirror::Class::VisitNativeRoots(Visitor& visitor, size_t pointer_size) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 822 | for (ArtField& field : GetSFieldsUnchecked()) { |
| 823 | // Visit roots first in case the declaring class gets moved. |
| 824 | field.VisitRoots(visitor); |
| 825 | if (kIsDebugBuild && IsResolved()) { |
| 826 | CHECK_EQ(field.GetDeclaringClass(), this) << GetStatus(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 827 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 828 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 829 | for (ArtField& field : GetIFieldsUnchecked()) { |
| 830 | // Visit roots first in case the declaring class gets moved. |
| 831 | field.VisitRoots(visitor); |
| 832 | if (kIsDebugBuild && IsResolved()) { |
| 833 | CHECK_EQ(field.GetDeclaringClass(), this) << GetStatus(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 834 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 835 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 836 | for (ArtMethod& method : GetDirectMethods(pointer_size)) { |
Mathieu Chartier | 1147b9b | 2015-09-14 18:50:08 -0700 | [diff] [blame] | 837 | method.VisitRoots(visitor, pointer_size); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 838 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 839 | for (ArtMethod& method : GetVirtualMethods(pointer_size)) { |
Mathieu Chartier | 1147b9b | 2015-09-14 18:50:08 -0700 | [diff] [blame] | 840 | method.VisitRoots(visitor, pointer_size); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 841 | } |
| 842 | } |
| 843 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 844 | inline IterationRange<StrideIterator<ArtMethod>> Class::GetDirectMethods(size_t pointer_size) { |
| 845 | CheckPointerSize(pointer_size); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 846 | return MakeIterationRangeFromLengthPrefixedArray(GetDirectMethodsPtrUnchecked(), |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 847 | ArtMethod::Size(pointer_size), |
| 848 | ArtMethod::Alignment(pointer_size)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | inline IterationRange<StrideIterator<ArtMethod>> Class::GetVirtualMethods(size_t pointer_size) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 852 | CheckPointerSize(pointer_size); |
| 853 | return MakeIterationRangeFromLengthPrefixedArray(GetVirtualMethodsPtrUnchecked(), |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 854 | ArtMethod::Size(pointer_size), |
| 855 | ArtMethod::Alignment(pointer_size)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | inline IterationRange<StrideIterator<ArtField>> Class::GetIFields() { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 859 | return MakeIterationRangeFromLengthPrefixedArray(GetIFieldsPtr()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | inline IterationRange<StrideIterator<ArtField>> Class::GetSFields() { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 863 | return MakeIterationRangeFromLengthPrefixedArray(GetSFieldsPtr()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | inline IterationRange<StrideIterator<ArtField>> Class::GetIFieldsUnchecked() { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 867 | return MakeIterationRangeFromLengthPrefixedArray(GetIFieldsPtrUnchecked()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | inline IterationRange<StrideIterator<ArtField>> Class::GetSFieldsUnchecked() { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 871 | return MakeIterationRangeFromLengthPrefixedArray(GetSFieldsPtrUnchecked()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | inline MemberOffset Class::EmbeddedImTableOffset(size_t pointer_size) { |
| 875 | CheckPointerSize(pointer_size); |
| 876 | // Round up since we want the embedded imt and vtable to be pointer size aligned in case 64 bits. |
| 877 | // Add 32 bits for embedded vtable length. |
| 878 | return MemberOffset( |
| 879 | RoundUp(EmbeddedVTableLengthOffset().Uint32Value() + sizeof(uint32_t), pointer_size)); |
| 880 | } |
| 881 | |
| 882 | inline MemberOffset Class::EmbeddedVTableOffset(size_t pointer_size) { |
| 883 | CheckPointerSize(pointer_size); |
| 884 | return MemberOffset(EmbeddedImTableOffset(pointer_size).Uint32Value() + |
| 885 | kImtSize * ImTableEntrySize(pointer_size)); |
| 886 | } |
| 887 | |
| 888 | inline void Class::CheckPointerSize(size_t pointer_size) { |
| 889 | DCHECK(ValidPointerSize(pointer_size)) << pointer_size; |
| 890 | DCHECK_EQ(pointer_size, Runtime::Current()->GetClassLinker()->GetImagePointerSize()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 891 | } |
| 892 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 893 | template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> |
| 894 | inline Class* Class::GetComponentType() { |
| 895 | return GetFieldObject<Class, kVerifyFlags, kReadBarrierOption>(ComponentTypeOffset()); |
| 896 | } |
| 897 | |
| 898 | template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> |
| 899 | inline bool Class::IsArrayClass() { |
| 900 | return GetComponentType<kVerifyFlags, kReadBarrierOption>() != nullptr; |
| 901 | } |
| 902 | |
| 903 | inline bool Class::IsAssignableFrom(Class* src) { |
| 904 | DCHECK(src != nullptr); |
| 905 | if (this == src) { |
| 906 | // Can always assign to things of the same type. |
| 907 | return true; |
| 908 | } else if (IsObjectClass()) { |
| 909 | // Can assign any reference to java.lang.Object. |
| 910 | return !src->IsPrimitive(); |
| 911 | } else if (IsInterface()) { |
| 912 | return src->Implements(this); |
| 913 | } else if (src->IsArrayClass()) { |
| 914 | return IsAssignableFromArray(src); |
| 915 | } else { |
| 916 | return !src->IsInterface() && src->IsSubClass(this); |
| 917 | } |
| 918 | } |
| 919 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 920 | inline uint32_t Class::NumDirectMethods() { |
| 921 | LengthPrefixedArray<ArtMethod>* arr = GetDirectMethodsPtrUnchecked(); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 922 | return arr != nullptr ? arr->size() : 0u; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | inline uint32_t Class::NumVirtualMethods() { |
| 926 | LengthPrefixedArray<ArtMethod>* arr = GetVirtualMethodsPtrUnchecked(); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 927 | return arr != nullptr ? arr->size() : 0u; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | inline uint32_t Class::NumInstanceFields() { |
| 931 | LengthPrefixedArray<ArtField>* arr = GetIFieldsPtrUnchecked(); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 932 | return arr != nullptr ? arr->size() : 0u; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | inline uint32_t Class::NumStaticFields() { |
| 936 | LengthPrefixedArray<ArtField>* arr = GetSFieldsPtrUnchecked(); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 937 | return arr != nullptr ? arr->size() : 0u; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 938 | } |
| 939 | |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame^] | 940 | template <typename Visitor> |
| 941 | inline void Class::FixupNativePointers(mirror::Class* dest, |
| 942 | size_t pointer_size, |
| 943 | const Visitor& visitor) { |
| 944 | // Update the field arrays. |
| 945 | LengthPrefixedArray<ArtField>* const sfields = GetSFieldsPtr(); |
| 946 | LengthPrefixedArray<ArtField>* const new_sfields = visitor(sfields); |
| 947 | if (sfields != new_sfields) { |
| 948 | dest->SetSFieldsPtrUnchecked(new_sfields); |
| 949 | } |
| 950 | LengthPrefixedArray<ArtField>* const ifields = GetIFieldsPtr(); |
| 951 | LengthPrefixedArray<ArtField>* const new_ifields = visitor(ifields); |
| 952 | if (ifields != new_ifields) { |
| 953 | dest->SetIFieldsPtrUnchecked(new_ifields); |
| 954 | } |
| 955 | // Update direct and virtual method arrays. |
| 956 | LengthPrefixedArray<ArtMethod>* direct_methods = GetDirectMethodsPtr(); |
| 957 | LengthPrefixedArray<ArtMethod>* new_direct_methods = visitor(direct_methods); |
| 958 | if (direct_methods != new_direct_methods) { |
| 959 | dest->SetDirectMethodsPtrUnchecked(new_direct_methods); |
| 960 | } |
| 961 | LengthPrefixedArray<ArtMethod>* virtual_methods = GetVirtualMethodsPtr(); |
| 962 | LengthPrefixedArray<ArtMethod>* new_virtual_methods = visitor(virtual_methods); |
| 963 | if (virtual_methods != new_virtual_methods) { |
| 964 | dest->SetVirtualMethodsPtr(new_virtual_methods); |
| 965 | } |
| 966 | // Update dex cache strings. |
| 967 | GcRoot<mirror::String>* strings = GetDexCacheStrings(); |
| 968 | GcRoot<mirror::String>* new_strings = visitor(strings); |
| 969 | if (strings != new_strings) { |
| 970 | dest->SetDexCacheStrings(new_strings); |
| 971 | } |
| 972 | // Fix up embedded tables. |
| 973 | if (!IsTemp() && ShouldHaveEmbeddedImtAndVTable()) { |
| 974 | for (int32_t i = 0, count = GetEmbeddedVTableLength(); i < count; ++i) { |
| 975 | ArtMethod* method = GetEmbeddedVTableEntry(i, pointer_size); |
| 976 | ArtMethod* new_method = visitor(method); |
| 977 | if (method != new_method) { |
| 978 | dest->SetEmbeddedVTableEntryUnchecked(i, new_method, pointer_size); |
| 979 | } |
| 980 | } |
| 981 | for (size_t i = 0; i < mirror::Class::kImtSize; ++i) { |
| 982 | ArtMethod* method = GetEmbeddedImTableEntry(i, pointer_size); |
| 983 | ArtMethod* new_method = visitor(method); |
| 984 | if (method != new_method) { |
| 985 | dest->SetEmbeddedImTableEntry(i, new_method, pointer_size); |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | } |
| 990 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 991 | } // namespace mirror |
| 992 | } // namespace art |
| 993 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 994 | #endif // ART_RUNTIME_MIRROR_CLASS_INL_H_ |