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