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 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 36 | inline uint32_t Class::GetObjectSize() { |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 37 | DCHECK(!IsVariableSize()) << " class=" << PrettyTypeOf(this); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 38 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 41 | inline Class* Class::GetSuperClass() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 42 | // Can only get super class for loaded classes (hack for when runtime is |
| 43 | // initializing) |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 44 | DCHECK(IsLoaded() || IsErroneous() || !Runtime::Current()->IsStarted()) << IsLoaded(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 45 | return GetFieldObject<Class>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 48 | inline ClassLoader* Class::GetClassLoader() { |
| 49 | return GetFieldObject<ClassLoader>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false); |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Mathieu Chartier | c2f4d02 | 2014-03-03 16:11:42 -0800 | [diff] [blame] | 52 | template<VerifyObjectFlags kVerifyFlags> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 53 | inline DexCache* Class::GetDexCache() { |
Mathieu Chartier | c2f4d02 | 2014-03-03 16:11:42 -0800 | [diff] [blame] | 54 | return GetFieldObject<DexCache, kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false); |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 57 | inline ObjectArray<ArtMethod>* Class::GetDirectMethods() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 58 | DCHECK(IsLoaded() || IsErroneous()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 59 | return GetFieldObject<ObjectArray<ArtMethod> >( |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 60 | OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false); |
| 61 | } |
| 62 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 63 | inline void Class::SetDirectMethods(ObjectArray<ArtMethod>* new_direct_methods) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 64 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 65 | DCHECK(NULL == GetFieldObject<ObjectArray<ArtMethod> >( |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 66 | OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false)); |
| 67 | DCHECK_NE(0, new_direct_methods->GetLength()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 68 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), |
| 69 | new_direct_methods, false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 72 | inline ArtMethod* Class::GetDirectMethod(int32_t i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 73 | return GetDirectMethods()->Get(i); |
| 74 | } |
| 75 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 76 | inline void Class::SetDirectMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 77 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 78 | ObjectArray<ArtMethod>* direct_methods = |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 79 | GetFieldObject<ObjectArray<ArtMethod> >( |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 80 | OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 81 | direct_methods->Set<false>(i, f); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // Returns the number of static, private, and constructor methods. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 85 | inline uint32_t Class::NumDirectMethods() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 86 | return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0; |
| 87 | } |
| 88 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 89 | template<VerifyObjectFlags kVerifyFlags> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 90 | inline ObjectArray<ArtMethod>* Class::GetVirtualMethods() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 91 | DCHECK(IsLoaded() || IsErroneous()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 92 | return GetFieldObject<ObjectArray<ArtMethod> >( |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 93 | OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false); |
| 94 | } |
| 95 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 96 | inline void Class::SetVirtualMethods(ObjectArray<ArtMethod>* new_virtual_methods) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 97 | // TODO: we reassign virtual methods to grow the table for miranda |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 98 | // methods.. they should really just be assigned once. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 99 | DCHECK_NE(0, new_virtual_methods->GetLength()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 100 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), |
| 101 | new_virtual_methods, false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 104 | inline uint32_t Class::NumVirtualMethods() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 105 | return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0; |
| 106 | } |
| 107 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 108 | template<VerifyObjectFlags kVerifyFlags> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 109 | inline ArtMethod* Class::GetVirtualMethod(uint32_t i) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 110 | DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 111 | return GetVirtualMethods()->Get(i); |
| 112 | } |
| 113 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 114 | inline ArtMethod* Class::GetVirtualMethodDuringLinking(uint32_t i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 115 | DCHECK(IsLoaded() || IsErroneous()); |
| 116 | return GetVirtualMethods()->Get(i); |
| 117 | } |
| 118 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 119 | inline void Class::SetVirtualMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 120 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 121 | ObjectArray<ArtMethod>* virtual_methods = |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 122 | GetFieldObject<ObjectArray<ArtMethod> >( |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 123 | OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false); |
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 | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 129 | return GetFieldObject<ObjectArray<ArtMethod> >(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false); |
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 | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 134 | return GetFieldObject<ObjectArray<ArtMethod> >(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false); |
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) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 138 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false); |
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() { |
| 142 | return GetFieldObject<ObjectArray<ArtMethod> >(OFFSET_OF_OBJECT_MEMBER(Class, imtable_), false); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | inline void Class::SetImTable(ObjectArray<ArtMethod>* new_imtable) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 146 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, imtable_), new_imtable, false); |
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() { |
| 341 | return GetFieldObject<IfTable>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false); |
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) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 353 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false); |
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 | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 358 | return GetFieldObject<ObjectArray<ArtField>>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false); |
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 | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 363 | DCHECK(NULL == GetFieldObject<ObjectArray<ArtField> >( |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 364 | OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 365 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields, false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 368 | inline ObjectArray<ArtField>* Class::GetSFields() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 369 | DCHECK(IsLoaded() || IsErroneous()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 370 | return GetFieldObject<ObjectArray<ArtField> >(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 371 | } |
| 372 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 373 | inline void Class::SetSFields(ObjectArray<ArtField>* new_sfields) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 374 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 375 | DCHECK(NULL == GetFieldObject<ObjectArray<ArtField> >( |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 376 | OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 377 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields, false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 380 | inline uint32_t Class::NumStaticFields() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 381 | return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0; |
| 382 | } |
| 383 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 384 | inline ArtField* Class::GetStaticField(uint32_t i) // TODO: uint16_t |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 385 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 386 | return GetSFields()->Get(i); |
| 387 | } |
| 388 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 389 | inline void Class::SetStaticField(uint32_t i, ArtField* f) // TODO: uint16_t |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 390 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 391 | ObjectArray<ArtField>* sfields= GetFieldObject<ObjectArray<ArtField> >( |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 392 | OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 393 | sfields->Set<false>(i, f); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 396 | inline uint32_t Class::NumInstanceFields() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 397 | return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0; |
| 398 | } |
| 399 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 400 | inline ArtField* Class::GetInstanceField(uint32_t i) { // TODO: uint16_t |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 401 | DCHECK_NE(NumInstanceFields(), 0U); |
| 402 | return GetIFields()->Get(i); |
| 403 | } |
| 404 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 405 | inline void Class::SetInstanceField(uint32_t i, ArtField* f) // TODO: uint16_t |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 406 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 407 | ObjectArray<ArtField>* ifields= GetFieldObject<ObjectArray<ArtField> >( |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 408 | OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 409 | ifields->Set<false>(i, f); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | inline void Class::SetVerifyErrorClass(Class* klass) { |
| 413 | CHECK(klass != NULL) << PrettyClass(this); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 414 | if (Runtime::Current()->IsActiveTransaction()) { |
| 415 | SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false); |
| 416 | } else { |
| 417 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false); |
| 418 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 419 | } |
| 420 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 421 | template<VerifyObjectFlags kVerifyFlags> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 422 | inline uint32_t Class::GetAccessFlags() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 423 | // Check class is loaded or this is java.lang.String that has a |
| 424 | // circularity issue during loading the names of its members |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 425 | DCHECK(IsLoaded<kVerifyFlags>() || |
| 426 | IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() || |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 427 | this == String::GetJavaLangString() || |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 428 | this == ArtField::GetJavaLangReflectArtField() || |
| 429 | this == ArtMethod::GetJavaLangReflectArtMethod()); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 430 | return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 431 | } |
| 432 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 433 | inline String* Class::GetName() { |
| 434 | return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(Class, name_), false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 435 | } |
| 436 | inline void Class::SetName(String* name) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 437 | if (Runtime::Current()->IsActiveTransaction()) { |
| 438 | SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name, false); |
| 439 | } else { |
| 440 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name, false); |
| 441 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 442 | } |
| 443 | |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 444 | inline void Class::CheckObjectAlloc() { |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 445 | DCHECK(!IsArrayClass()) << PrettyClass(this); |
| 446 | DCHECK(IsInstantiable()) << PrettyClass(this); |
| 447 | // TODO: decide whether we want this check. It currently fails during bootstrap. |
| 448 | // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this); |
| 449 | DCHECK_GE(this->object_size_, sizeof(Object)); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 452 | template <bool kIsInstrumented> |
| 453 | inline Object* Class::Alloc(Thread* self, gc::AllocatorType allocator_type) { |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 454 | CheckObjectAlloc(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 455 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 456 | return heap->AllocObjectWithAllocator<kIsInstrumented, false>(self, this, this->object_size_, |
| 457 | allocator_type); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | inline Object* Class::AllocObject(Thread* self) { |
| 461 | return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
| 462 | } |
| 463 | |
| 464 | inline Object* Class::AllocNonMovableObject(Thread* self) { |
| 465 | return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentNonMovingAllocator()); |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame^] | 468 | template <bool kVisitClass, typename Visitor> |
| 469 | inline void Class::VisitReferences(mirror::Class* klass, const Visitor& visitor) { |
| 470 | VisitInstanceFieldsReferences<kVisitClass>(klass, visitor); |
| 471 | VisitStaticFieldsReferences<kVisitClass>(this, visitor); |
| 472 | } |
| 473 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 474 | } // namespace mirror |
| 475 | } // namespace art |
| 476 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 477 | #endif // ART_RUNTIME_MIRROR_CLASS_INL_H_ |