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 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 22 | #include "art_field.h" |
| 23 | #include "art_method.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 24 | #include "class_loader.h" |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 25 | #include "common_throws.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 26 | #include "dex_cache.h" |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 27 | #include "gc/heap-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | #include "iftable.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 29 | #include "object_array-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 30 | #include "runtime.h" |
| 31 | #include "string.h" |
| 32 | |
| 33 | namespace art { |
| 34 | namespace mirror { |
| 35 | |
Hiroshi Yamauchi | 25023c7 | 2014-05-09 11:45:53 -0700 | [diff] [blame] | 36 | template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 37 | inline uint32_t Class::GetObjectSize() { |
Hiroshi Yamauchi | 25023c7 | 2014-05-09 11:45:53 -0700 | [diff] [blame] | 38 | if (kIsDebugBuild) { |
| 39 | // Use a local variable as (D)CHECK can't handle the space between |
| 40 | // the two template params. |
| 41 | bool is_variable_size = IsVariableSize<kVerifyFlags, kReadBarrierOption>(); |
| 42 | CHECK(!is_variable_size) << " class=" << PrettyTypeOf(this); |
| 43 | } |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 44 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 47 | inline Class* Class::GetSuperClass() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 48 | // Can only get super class for loaded classes (hack for when runtime is |
| 49 | // initializing) |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 50 | DCHECK(IsLoaded() || IsErroneous() || !Runtime::Current()->IsStarted()) << IsLoaded(); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 51 | return GetFieldObject<Class>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 54 | inline ClassLoader* Class::GetClassLoader() { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 55 | return GetFieldObject<ClassLoader>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_)); |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Mathieu Chartier | c2f4d02 | 2014-03-03 16:11:42 -0800 | [diff] [blame] | 58 | template<VerifyObjectFlags kVerifyFlags> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 59 | inline DexCache* Class::GetDexCache() { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 60 | return GetFieldObject<DexCache, kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_)); |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 63 | inline ObjectArray<ArtMethod>* Class::GetDirectMethods() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 64 | DCHECK(IsLoaded() || IsErroneous()); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 65 | return GetFieldObject<ObjectArray<ArtMethod> >(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 68 | inline void Class::SetDirectMethods(ObjectArray<ArtMethod>* new_direct_methods) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 69 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 70 | DCHECK(NULL == GetFieldObject<ObjectArray<ArtMethod> >( |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 71 | OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_))); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 72 | DCHECK_NE(0, new_direct_methods->GetLength()); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 73 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), new_direct_methods); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 76 | inline ArtMethod* Class::GetDirectMethod(int32_t i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 77 | return GetDirectMethods()->Get(i); |
| 78 | } |
| 79 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 80 | inline void Class::SetDirectMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 81 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 82 | ObjectArray<ArtMethod>* direct_methods = |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 83 | GetFieldObject<ObjectArray<ArtMethod> >(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 84 | direct_methods->Set<false>(i, f); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // Returns the number of static, private, and constructor methods. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 88 | inline uint32_t Class::NumDirectMethods() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 89 | return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0; |
| 90 | } |
| 91 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 92 | template<VerifyObjectFlags kVerifyFlags> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 93 | inline ObjectArray<ArtMethod>* Class::GetVirtualMethods() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 94 | DCHECK(IsLoaded() || IsErroneous()); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 95 | return GetFieldObject<ObjectArray<ArtMethod> >(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 98 | inline void Class::SetVirtualMethods(ObjectArray<ArtMethod>* new_virtual_methods) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 99 | // TODO: we reassign virtual methods to grow the table for miranda |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 100 | // methods.. they should really just be assigned once. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 101 | DCHECK_NE(0, new_virtual_methods->GetLength()); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 102 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), new_virtual_methods); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 105 | inline uint32_t Class::NumVirtualMethods() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 106 | return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0; |
| 107 | } |
| 108 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 109 | template<VerifyObjectFlags kVerifyFlags> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 110 | inline ArtMethod* Class::GetVirtualMethod(uint32_t i) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 111 | DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 112 | return GetVirtualMethods()->Get(i); |
| 113 | } |
| 114 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 115 | inline ArtMethod* Class::GetVirtualMethodDuringLinking(uint32_t i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 116 | DCHECK(IsLoaded() || IsErroneous()); |
| 117 | return GetVirtualMethods()->Get(i); |
| 118 | } |
| 119 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 120 | inline void Class::SetVirtualMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 121 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 122 | ObjectArray<ArtMethod>* virtual_methods = |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 123 | GetFieldObject<ObjectArray<ArtMethod> >(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 124 | virtual_methods->Set<false>(i, f); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 127 | inline ObjectArray<ArtMethod>* Class::GetVTable() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 128 | DCHECK(IsResolved() || IsErroneous()); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 129 | return GetFieldObject<ObjectArray<ArtMethod> >(OFFSET_OF_OBJECT_MEMBER(Class, vtable_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 132 | inline ObjectArray<ArtMethod>* Class::GetVTableDuringLinking() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 133 | DCHECK(IsLoaded() || IsErroneous()); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 134 | return GetFieldObject<ObjectArray<ArtMethod> >(OFFSET_OF_OBJECT_MEMBER(Class, vtable_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 137 | inline void Class::SetVTable(ObjectArray<ArtMethod>* new_vtable) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 138 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 141 | inline ObjectArray<ArtMethod>* Class::GetImTable() { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 142 | return GetFieldObject<ObjectArray<ArtMethod> >(OFFSET_OF_OBJECT_MEMBER(Class, imtable_)); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | inline void Class::SetImTable(ObjectArray<ArtMethod>* new_imtable) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 146 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, imtable_), new_imtable); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 149 | inline bool Class::Implements(Class* klass) { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 150 | DCHECK(klass != NULL); |
| 151 | DCHECK(klass->IsInterface()) << PrettyClass(this); |
| 152 | // All interfaces implemented directly and by our superclass, and |
| 153 | // recursively all super-interfaces of those interfaces, are listed |
| 154 | // in iftable_, so we can just do a linear scan through that. |
| 155 | int32_t iftable_count = GetIfTableCount(); |
| 156 | IfTable* iftable = GetIfTable(); |
| 157 | for (int32_t i = 0; i < iftable_count; i++) { |
| 158 | if (iftable->GetInterface(i) == klass) { |
| 159 | return true; |
| 160 | } |
| 161 | } |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | // Determine whether "this" is assignable from "src", where both of these |
| 166 | // are array classes. |
| 167 | // |
| 168 | // Consider an array class, e.g. Y[][], where Y is a subclass of X. |
| 169 | // Y[][] = Y[][] --> true (identity) |
| 170 | // X[][] = Y[][] --> true (element superclass) |
| 171 | // Y = Y[][] --> false |
| 172 | // Y[] = Y[][] --> false |
| 173 | // Object = Y[][] --> true (everything is an object) |
| 174 | // Object[] = Y[][] --> true |
| 175 | // Object[][] = Y[][] --> true |
| 176 | // Object[][][] = Y[][] --> false (too many []s) |
| 177 | // Serializable = Y[][] --> true (all arrays are Serializable) |
| 178 | // Serializable[] = Y[][] --> true |
| 179 | // Serializable[][] = Y[][] --> false (unless Y is Serializable) |
| 180 | // |
| 181 | // Don't forget about primitive types. |
| 182 | // Object[] = int[] --> false |
| 183 | // |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 184 | inline bool Class::IsArrayAssignableFromArray(Class* src) { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 185 | DCHECK(IsArrayClass()) << PrettyClass(this); |
| 186 | DCHECK(src->IsArrayClass()) << PrettyClass(src); |
| 187 | return GetComponentType()->IsAssignableFrom(src->GetComponentType()); |
| 188 | } |
| 189 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 190 | inline bool Class::IsAssignableFromArray(Class* src) { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 191 | DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom |
| 192 | DCHECK(src->IsArrayClass()) << PrettyClass(src); |
| 193 | if (!IsArrayClass()) { |
| 194 | // If "this" is not also an array, it must be Object. |
| 195 | // src's super should be java_lang_Object, since it is an array. |
| 196 | Class* java_lang_Object = src->GetSuperClass(); |
| 197 | DCHECK(java_lang_Object != NULL) << PrettyClass(src); |
Ian Rogers | fa46d3e | 2013-05-15 00:16:04 -0700 | [diff] [blame] | 198 | DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 199 | return this == java_lang_Object; |
| 200 | } |
| 201 | return IsArrayAssignableFromArray(src); |
| 202 | } |
| 203 | |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame] | 204 | template <bool throw_on_failure, bool use_referrers_cache> |
| 205 | inline bool Class::ResolvedFieldAccessTest(Class* access_to, ArtField* field, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 206 | uint32_t field_idx, DexCache* dex_cache) { |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame] | 207 | DCHECK_EQ(use_referrers_cache, dex_cache == nullptr); |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 208 | if (UNLIKELY(!this->CanAccess(access_to))) { |
| 209 | // The referrer class can't access the field's declaring class but may still be able |
| 210 | // to access the field if the FieldId specifies an accessible subclass of the declaring |
| 211 | // class rather than the declaring class itself. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 212 | DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache; |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 213 | uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetFieldId(field_idx).class_idx_; |
| 214 | // The referenced class has already been resolved with the field, get it from the dex cache. |
| 215 | Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx); |
| 216 | DCHECK(dex_access_to != nullptr); |
| 217 | if (UNLIKELY(!this->CanAccess(dex_access_to))) { |
| 218 | if (throw_on_failure) { |
| 219 | ThrowIllegalAccessErrorClass(this, dex_access_to); |
| 220 | } |
| 221 | return false; |
| 222 | } |
| 223 | DCHECK_EQ(this->CanAccessMember(access_to, field->GetAccessFlags()), |
| 224 | this->CanAccessMember(dex_access_to, field->GetAccessFlags())); |
| 225 | } |
| 226 | if (LIKELY(this->CanAccessMember(access_to, field->GetAccessFlags()))) { |
| 227 | return true; |
| 228 | } |
| 229 | if (throw_on_failure) { |
| 230 | ThrowIllegalAccessErrorField(this, field); |
| 231 | } |
| 232 | return false; |
| 233 | } |
| 234 | |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame] | 235 | template <bool throw_on_failure, bool use_referrers_cache, InvokeType throw_invoke_type> |
| 236 | inline bool Class::ResolvedMethodAccessTest(Class* access_to, ArtMethod* method, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 237 | uint32_t method_idx, DexCache* dex_cache) { |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 238 | COMPILE_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] | 239 | DCHECK_EQ(use_referrers_cache, dex_cache == nullptr); |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 240 | if (UNLIKELY(!this->CanAccess(access_to))) { |
| 241 | // The referrer class can't access the method's declaring class but may still be able |
| 242 | // to access the method if the MethodId specifies an accessible subclass of the declaring |
| 243 | // class rather than the declaring class itself. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 244 | DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache; |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 245 | uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_; |
| 246 | // The referenced class has already been resolved with the method, get it from the dex cache. |
| 247 | Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx); |
| 248 | DCHECK(dex_access_to != nullptr); |
| 249 | if (UNLIKELY(!this->CanAccess(dex_access_to))) { |
| 250 | if (throw_on_failure) { |
| 251 | ThrowIllegalAccessErrorClassForMethodDispatch(this, dex_access_to, |
| 252 | method, throw_invoke_type); |
| 253 | } |
| 254 | return false; |
| 255 | } |
| 256 | DCHECK_EQ(this->CanAccessMember(access_to, method->GetAccessFlags()), |
| 257 | this->CanAccessMember(dex_access_to, method->GetAccessFlags())); |
| 258 | } |
| 259 | if (LIKELY(this->CanAccessMember(access_to, method->GetAccessFlags()))) { |
| 260 | return true; |
| 261 | } |
| 262 | if (throw_on_failure) { |
| 263 | ThrowIllegalAccessErrorMethod(this, method); |
| 264 | } |
| 265 | return false; |
| 266 | } |
| 267 | |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame] | 268 | inline bool Class::CanAccessResolvedField(Class* access_to, ArtField* field, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 269 | DexCache* dex_cache, uint32_t field_idx) { |
| 270 | return ResolvedFieldAccessTest<false, false>(access_to, field, field_idx, dex_cache); |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | inline bool Class::CheckResolvedFieldAccess(Class* access_to, ArtField* field, |
| 274 | uint32_t field_idx) { |
| 275 | return ResolvedFieldAccessTest<true, true>(access_to, field, field_idx, nullptr); |
| 276 | } |
| 277 | |
| 278 | inline bool Class::CanAccessResolvedMethod(Class* access_to, ArtMethod* method, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 279 | DexCache* dex_cache, uint32_t method_idx) { |
| 280 | return ResolvedMethodAccessTest<false, false, kStatic>(access_to, method, method_idx, dex_cache); |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | template <InvokeType throw_invoke_type> |
| 284 | inline bool Class::CheckResolvedMethodAccess(Class* access_to, ArtMethod* method, |
| 285 | uint32_t method_idx) { |
| 286 | return ResolvedMethodAccessTest<true, true, throw_invoke_type>(access_to, method, method_idx, |
| 287 | nullptr); |
| 288 | } |
| 289 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 290 | inline bool Class::IsSubClass(Class* klass) { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 291 | DCHECK(!IsInterface()) << PrettyClass(this); |
| 292 | DCHECK(!IsArrayClass()) << PrettyClass(this); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 293 | Class* current = this; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 294 | do { |
| 295 | if (current == klass) { |
| 296 | return true; |
| 297 | } |
| 298 | current = current->GetSuperClass(); |
| 299 | } while (current != NULL); |
| 300 | return false; |
| 301 | } |
| 302 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 303 | inline ArtMethod* Class::FindVirtualMethodForInterface(ArtMethod* method) { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 304 | Class* declaring_class = method->GetDeclaringClass(); |
| 305 | DCHECK(declaring_class != NULL) << PrettyClass(this); |
| 306 | DCHECK(declaring_class->IsInterface()) << PrettyMethod(method); |
| 307 | // TODO cache to improve lookup speed |
| 308 | int32_t iftable_count = GetIfTableCount(); |
| 309 | IfTable* iftable = GetIfTable(); |
| 310 | for (int32_t i = 0; i < iftable_count; i++) { |
| 311 | if (iftable->GetInterface(i) == declaring_class) { |
| 312 | return iftable->GetMethodArray(i)->Get(method->GetMethodIndex()); |
| 313 | } |
| 314 | } |
| 315 | return NULL; |
| 316 | } |
| 317 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 318 | inline ArtMethod* Class::FindVirtualMethodForVirtual(ArtMethod* method) { |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 319 | DCHECK(!method->GetDeclaringClass()->IsInterface() || method->IsMiranda()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 320 | // The argument method may from a super class. |
| 321 | // Use the index to a potentially overridden one for this instance's class. |
| 322 | return GetVTable()->Get(method->GetMethodIndex()); |
| 323 | } |
| 324 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 325 | inline ArtMethod* Class::FindVirtualMethodForSuper(ArtMethod* method) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 326 | DCHECK(!method->GetDeclaringClass()->IsInterface()); |
| 327 | return GetSuperClass()->GetVTable()->Get(method->GetMethodIndex()); |
| 328 | } |
| 329 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 330 | inline ArtMethod* Class::FindVirtualMethodForVirtualOrInterface(ArtMethod* method) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 331 | if (method->IsDirect()) { |
| 332 | return method; |
| 333 | } |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 334 | if (method->GetDeclaringClass()->IsInterface() && !method->IsMiranda()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 335 | return FindVirtualMethodForInterface(method); |
| 336 | } |
| 337 | return FindVirtualMethodForVirtual(method); |
| 338 | } |
| 339 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 340 | inline IfTable* Class::GetIfTable() { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 341 | return GetFieldObject<IfTable>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 344 | inline int32_t Class::GetIfTableCount() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 345 | IfTable* iftable = GetIfTable(); |
| 346 | if (iftable == NULL) { |
| 347 | return 0; |
| 348 | } |
| 349 | return iftable->Count(); |
| 350 | } |
| 351 | |
| 352 | inline void Class::SetIfTable(IfTable* new_iftable) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 353 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 354 | } |
| 355 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 356 | inline ObjectArray<ArtField>* Class::GetIFields() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 357 | DCHECK(IsLoaded() || IsErroneous()); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 358 | return GetFieldObject<ObjectArray<ArtField>>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 361 | inline void Class::SetIFields(ObjectArray<ArtField>* new_ifields) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 362 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 363 | DCHECK(NULL == GetFieldObject<ObjectArray<ArtField> >(OFFSET_OF_OBJECT_MEMBER(Class, ifields_))); |
| 364 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 367 | inline ObjectArray<ArtField>* Class::GetSFields() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 368 | DCHECK(IsLoaded() || IsErroneous()); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 369 | return GetFieldObject<ObjectArray<ArtField> >(OFFSET_OF_OBJECT_MEMBER(Class, sfields_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 372 | inline void Class::SetSFields(ObjectArray<ArtField>* new_sfields) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 373 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 374 | DCHECK(NULL == GetFieldObject<ObjectArray<ArtField> >(OFFSET_OF_OBJECT_MEMBER(Class, sfields_))); |
| 375 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 376 | } |
| 377 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 378 | inline uint32_t Class::NumStaticFields() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 379 | return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0; |
| 380 | } |
| 381 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 382 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 383 | inline ArtField* Class::GetStaticField(uint32_t i) // TODO: uint16_t |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 384 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 385 | return GetSFields()->GetWithoutChecks(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 388 | inline void Class::SetStaticField(uint32_t i, ArtField* f) // TODO: uint16_t |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 389 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 390 | ObjectArray<ArtField>* sfields= GetFieldObject<ObjectArray<ArtField> >( |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 391 | OFFSET_OF_OBJECT_MEMBER(Class, sfields_)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 392 | sfields->Set<false>(i, f); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 395 | inline uint32_t Class::NumInstanceFields() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 396 | return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0; |
| 397 | } |
| 398 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 399 | inline ArtField* Class::GetInstanceField(uint32_t i) { // TODO: uint16_t |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 400 | DCHECK_NE(NumInstanceFields(), 0U); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 401 | return GetIFields()->GetWithoutChecks(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 404 | inline void Class::SetInstanceField(uint32_t i, ArtField* f) // TODO: uint16_t |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 405 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 406 | ObjectArray<ArtField>* ifields= GetFieldObject<ObjectArray<ArtField> >( |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 407 | OFFSET_OF_OBJECT_MEMBER(Class, ifields_)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 408 | ifields->Set<false>(i, f); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 409 | } |
| 410 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 411 | template<VerifyObjectFlags kVerifyFlags> |
| 412 | inline uint32_t Class::GetReferenceInstanceOffsets() { |
| 413 | DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>()); |
| 414 | return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_)); |
| 415 | } |
| 416 | |
| 417 | inline void Class::SetClinitThreadId(pid_t new_clinit_thread_id) { |
| 418 | if (Runtime::Current()->IsActiveTransaction()) { |
| 419 | SetField32<true>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id); |
| 420 | } else { |
| 421 | SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id); |
| 422 | } |
| 423 | } |
| 424 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 425 | inline void Class::SetVerifyErrorClass(Class* klass) { |
| 426 | CHECK(klass != NULL) << PrettyClass(this); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 427 | if (Runtime::Current()->IsActiveTransaction()) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 428 | SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 429 | } else { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 430 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 431 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 434 | template<VerifyObjectFlags kVerifyFlags> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 435 | inline uint32_t Class::GetAccessFlags() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 436 | // Check class is loaded or this is java.lang.String that has a |
| 437 | // circularity issue during loading the names of its members |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 438 | DCHECK(IsLoaded<kVerifyFlags>() || |
| 439 | IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() || |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 440 | this == String::GetJavaLangString() || |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 441 | this == ArtField::GetJavaLangReflectArtField() || |
| 442 | this == ArtMethod::GetJavaLangReflectArtMethod()); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 443 | return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 444 | } |
| 445 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 446 | inline String* Class::GetName() { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 447 | return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(Class, name_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 448 | } |
| 449 | inline void Class::SetName(String* name) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 450 | if (Runtime::Current()->IsActiveTransaction()) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 451 | SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 452 | } else { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 453 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 454 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 455 | } |
| 456 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 457 | template<VerifyObjectFlags kVerifyFlags> |
| 458 | inline Primitive::Type Class::GetPrimitiveType() { |
| 459 | DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t)); |
| 460 | return static_cast<Primitive::Type>( |
| 461 | GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_))); |
| 462 | } |
| 463 | |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 464 | inline void Class::CheckObjectAlloc() { |
Hiroshi Yamauchi | 4cd662e | 2014-04-03 16:28:10 -0700 | [diff] [blame] | 465 | DCHECK(!IsArrayClass()) |
| 466 | << PrettyClass(this) |
| 467 | << "A array shouldn't be allocated through this " |
| 468 | << "as it requires a pre-fence visitor that sets the class size."; |
| 469 | DCHECK(!IsClassClass()) |
| 470 | << PrettyClass(this) |
| 471 | << "A class object shouldn't be allocated through this " |
| 472 | << "as it requires a pre-fence visitor that sets the class size."; |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 473 | DCHECK(IsInstantiable()) << PrettyClass(this); |
| 474 | // TODO: decide whether we want this check. It currently fails during bootstrap. |
| 475 | // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this); |
| 476 | DCHECK_GE(this->object_size_, sizeof(Object)); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Mathieu Chartier | 8668c3c | 2014-04-24 16:48:11 -0700 | [diff] [blame] | 479 | template<bool kIsInstrumented, bool kCheckAddFinalizer> |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 480 | inline Object* Class::Alloc(Thread* self, gc::AllocatorType allocator_type) { |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 481 | CheckObjectAlloc(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 482 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 8668c3c | 2014-04-24 16:48:11 -0700 | [diff] [blame] | 483 | const bool add_finalizer = kCheckAddFinalizer && IsFinalizable(); |
| 484 | if (!kCheckAddFinalizer) { |
| 485 | DCHECK(!IsFinalizable()); |
| 486 | } |
| 487 | mirror::Object* obj = |
| 488 | heap->AllocObjectWithAllocator<kIsInstrumented, false>(self, this, this->object_size_, |
| 489 | allocator_type, VoidFunctor()); |
| 490 | if (add_finalizer && LIKELY(obj != nullptr)) { |
| 491 | heap->AddFinalizerReference(self, &obj); |
| 492 | } |
| 493 | return obj; |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | inline Object* Class::AllocObject(Thread* self) { |
| 497 | return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
| 498 | } |
| 499 | |
| 500 | inline Object* Class::AllocNonMovableObject(Thread* self) { |
| 501 | return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentNonMovingAllocator()); |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 504 | template <bool kVisitClass, typename Visitor> |
| 505 | inline void Class::VisitReferences(mirror::Class* klass, const Visitor& visitor) { |
| 506 | VisitInstanceFieldsReferences<kVisitClass>(klass, visitor); |
| 507 | VisitStaticFieldsReferences<kVisitClass>(this, visitor); |
| 508 | } |
| 509 | |
Hiroshi Yamauchi | 6e83c17 | 2014-05-01 21:25:41 -0700 | [diff] [blame] | 510 | template<ReadBarrierOption kReadBarrierOption> |
Hiroshi Yamauchi | 9103c86 | 2014-04-22 13:51:07 -0700 | [diff] [blame] | 511 | bool Class::IsArtFieldClass() { |
Hiroshi Yamauchi | 6e83c17 | 2014-05-01 21:25:41 -0700 | [diff] [blame] | 512 | Class* java_lang_Class = GetClass<kVerifyNone, kReadBarrierOption>(); |
Hiroshi Yamauchi | 9103c86 | 2014-04-22 13:51:07 -0700 | [diff] [blame] | 513 | Class* java_lang_reflect_ArtField = |
Hiroshi Yamauchi | 6e83c17 | 2014-05-01 21:25:41 -0700 | [diff] [blame] | 514 | java_lang_Class->GetInstanceField(0)->GetClass<kVerifyNone, kReadBarrierOption>(); |
Hiroshi Yamauchi | 9103c86 | 2014-04-22 13:51:07 -0700 | [diff] [blame] | 515 | return this == java_lang_reflect_ArtField; |
| 516 | } |
| 517 | |
Hiroshi Yamauchi | 6e83c17 | 2014-05-01 21:25:41 -0700 | [diff] [blame] | 518 | template<ReadBarrierOption kReadBarrierOption> |
Hiroshi Yamauchi | 9103c86 | 2014-04-22 13:51:07 -0700 | [diff] [blame] | 519 | bool Class::IsArtMethodClass() { |
Hiroshi Yamauchi | 6e83c17 | 2014-05-01 21:25:41 -0700 | [diff] [blame] | 520 | return this == ArtMethod::GetJavaLangReflectArtMethod<kReadBarrierOption>(); |
Hiroshi Yamauchi | 9103c86 | 2014-04-22 13:51:07 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Hiroshi Yamauchi | 25023c7 | 2014-05-09 11:45:53 -0700 | [diff] [blame] | 523 | template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> |
| 524 | inline bool Class::IsClassClass() { |
| 525 | Class* java_lang_Class = GetClass<kVerifyFlags, kReadBarrierOption>()-> |
| 526 | template GetClass<kVerifyFlags, kReadBarrierOption>(); |
| 527 | return this == java_lang_Class; |
| 528 | } |
| 529 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 530 | } // namespace mirror |
| 531 | } // namespace art |
| 532 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 533 | #endif // ART_RUNTIME_MIRROR_CLASS_INL_H_ |