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