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_CLASS_LINKER_INL_H_ |
| 18 | #define ART_RUNTIME_CLASS_LINKER_INL_H_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | |
| 20 | #include "class_linker.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 21 | #include "mirror/art_field.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 22 | #include "mirror/class_loader.h" |
Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 23 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | #include "mirror/iftable.h" |
| 25 | #include "mirror/object_array.h" |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 26 | #include "object_utils.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 27 | #include "handle_scope-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 31 | inline bool ClassLinker::IsInBootClassPath(const char* descriptor) { |
| 32 | DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, boot_class_path_); |
| 33 | return pair.second != nullptr; |
| 34 | } |
| 35 | |
| 36 | inline mirror::Class* ClassLinker::FindSystemClass(Thread* self, const char* descriptor) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 37 | return FindClass(self, descriptor, NullHandle<mirror::ClassLoader>()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Mathieu Chartier | b74cd29 | 2014-05-29 14:31:33 -0700 | [diff] [blame] | 40 | inline mirror::Class* ClassLinker::FindArrayClass(Thread* self, mirror::Class** element_class) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 41 | for (size_t i = 0; i < kFindArrayCacheSize; ++i) { |
Ian Rogers | a55cf41 | 2014-02-27 00:31:26 -0800 | [diff] [blame] | 42 | // Read the cached array class once to avoid races with other threads setting it. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 43 | mirror::Class* array_class = find_array_class_cache_[i]; |
Mathieu Chartier | b74cd29 | 2014-05-29 14:31:33 -0700 | [diff] [blame] | 44 | if (array_class != nullptr && array_class->GetComponentType() == *element_class) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 45 | return array_class; |
| 46 | } |
| 47 | } |
Mathieu Chartier | b74cd29 | 2014-05-29 14:31:33 -0700 | [diff] [blame] | 48 | DCHECK(!(*element_class)->IsPrimitiveVoid()); |
| 49 | std::string descriptor = "[" + (*element_class)->GetDescriptor(); |
| 50 | StackHandleScope<2> hs(Thread::Current()); |
| 51 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle((*element_class)->GetClassLoader())); |
| 52 | HandleWrapper<mirror::Class> h_element_class(hs.NewHandleWrapper(element_class)); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 53 | mirror::Class* array_class = FindClass(self, descriptor.c_str(), class_loader); |
| 54 | // Benign races in storing array class and incrementing index. |
| 55 | size_t victim_index = find_array_class_cache_next_victim_; |
| 56 | find_array_class_cache_[victim_index] = array_class; |
| 57 | find_array_class_cache_next_victim_ = (victim_index + 1) % kFindArrayCacheSize; |
| 58 | return array_class; |
| 59 | } |
| 60 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 61 | inline mirror::String* ClassLinker::ResolveString(uint32_t string_idx, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 62 | mirror::ArtMethod* referrer) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 63 | mirror::String* resolved_string = referrer->GetDexCacheStrings()->Get(string_idx); |
| 64 | if (UNLIKELY(resolved_string == NULL)) { |
| 65 | mirror::Class* declaring_class = referrer->GetDeclaringClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 66 | StackHandleScope<1> hs(Thread::Current()); |
| 67 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache())); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 68 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 69 | resolved_string = ResolveString(dex_file, string_idx, dex_cache); |
Ian Rogers | eebe03a | 2014-05-02 11:09:57 -0700 | [diff] [blame] | 70 | if (resolved_string != nullptr) { |
| 71 | DCHECK_EQ(dex_cache->GetResolvedString(string_idx), resolved_string); |
| 72 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 73 | } |
| 74 | return resolved_string; |
| 75 | } |
| 76 | |
| 77 | inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 78 | mirror::ArtMethod* referrer) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 79 | mirror::Class* resolved_type = referrer->GetDexCacheResolvedTypes()->Get(type_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 80 | if (UNLIKELY(resolved_type == nullptr)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 81 | mirror::Class* declaring_class = referrer->GetDeclaringClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 82 | StackHandleScope<2> hs(Thread::Current()); |
| 83 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache())); |
| 84 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader())); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 85 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 86 | resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader); |
Ian Rogers | eebe03a | 2014-05-02 11:09:57 -0700 | [diff] [blame] | 87 | if (resolved_type != nullptr) { |
| 88 | DCHECK_EQ(dex_cache->GetResolvedType(type_idx), resolved_type); |
| 89 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 90 | } |
| 91 | return resolved_type; |
| 92 | } |
| 93 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 94 | inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx, mirror::ArtField* referrer) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 95 | mirror::Class* declaring_class = referrer->GetDeclaringClass(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 96 | mirror::DexCache* dex_cache_ptr = declaring_class->GetDexCache(); |
| 97 | mirror::Class* resolved_type = dex_cache_ptr->GetResolvedType(type_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 98 | if (UNLIKELY(resolved_type == NULL)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 99 | StackHandleScope<2> hs(Thread::Current()); |
| 100 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(dex_cache_ptr)); |
| 101 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader())); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 102 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 103 | resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader); |
Ian Rogers | eebe03a | 2014-05-02 11:09:57 -0700 | [diff] [blame] | 104 | if (resolved_type != nullptr) { |
| 105 | DCHECK_EQ(dex_cache->GetResolvedType(type_idx), resolved_type); |
| 106 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 107 | } |
| 108 | return resolved_type; |
| 109 | } |
| 110 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 111 | inline mirror::ArtMethod* ClassLinker::GetResolvedMethod(uint32_t method_idx, |
| 112 | mirror::ArtMethod* referrer, |
| 113 | InvokeType type) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 114 | mirror::ArtMethod* resolved_method = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 115 | referrer->GetDexCacheResolvedMethods()->Get(method_idx); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 116 | if (resolved_method == nullptr || resolved_method->IsRuntimeMethod()) { |
| 117 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 118 | } |
| 119 | return resolved_method; |
| 120 | } |
| 121 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 122 | inline mirror::ArtMethod* ClassLinker::ResolveMethod(Thread* self, uint32_t method_idx, |
| 123 | mirror::ArtMethod** referrer, |
| 124 | InvokeType type) { |
| 125 | mirror::ArtMethod* resolved_method = GetResolvedMethod(method_idx, *referrer, type); |
| 126 | if (LIKELY(resolved_method != nullptr)) { |
| 127 | return resolved_method; |
| 128 | } |
| 129 | mirror::Class* declaring_class = (*referrer)->GetDeclaringClass(); |
| 130 | StackHandleScope<3> hs(self); |
| 131 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache())); |
| 132 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader())); |
| 133 | HandleWrapper<mirror::ArtMethod> h_referrer(hs.NewHandleWrapper(referrer)); |
| 134 | const DexFile* dex_file = h_dex_cache->GetDexFile(); |
| 135 | resolved_method = ResolveMethod(*dex_file, method_idx, h_dex_cache, h_class_loader, h_referrer, |
| 136 | type); |
| 137 | if (resolved_method != nullptr) { |
| 138 | DCHECK_EQ(h_dex_cache->GetResolvedMethod(method_idx), resolved_method); |
| 139 | } |
| 140 | return resolved_method; |
| 141 | } |
| 142 | |
| 143 | inline mirror::ArtField* ClassLinker::GetResolvedField(uint32_t field_idx, |
| 144 | mirror::Class* field_declaring_class) { |
| 145 | return field_declaring_class->GetDexCache()->GetResolvedField(field_idx); |
| 146 | } |
| 147 | |
| 148 | inline mirror::ArtField* ClassLinker::ResolveField(uint32_t field_idx, mirror::ArtMethod* referrer, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 149 | bool is_static) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 150 | mirror::Class* declaring_class = referrer->GetDeclaringClass(); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 151 | mirror::ArtField* resolved_field = GetResolvedField(field_idx, declaring_class); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 152 | if (UNLIKELY(resolved_field == NULL)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 153 | StackHandleScope<2> hs(Thread::Current()); |
| 154 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache())); |
| 155 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader())); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 156 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 157 | resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static); |
Ian Rogers | eebe03a | 2014-05-02 11:09:57 -0700 | [diff] [blame] | 158 | if (resolved_field != nullptr) { |
| 159 | DCHECK_EQ(dex_cache->GetResolvedField(field_idx), resolved_field); |
| 160 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 161 | } |
| 162 | return resolved_field; |
| 163 | } |
| 164 | |
| 165 | template <class T> |
| 166 | inline mirror::ObjectArray<T>* ClassLinker::AllocObjectArray(Thread* self, size_t length) { |
| 167 | return mirror::ObjectArray<T>::Alloc(self, GetClassRoot(kObjectArrayClass), length); |
| 168 | } |
| 169 | |
| 170 | inline mirror::ObjectArray<mirror::Class>* ClassLinker::AllocClassArray(Thread* self, |
| 171 | size_t length) { |
| 172 | return mirror::ObjectArray<mirror::Class>::Alloc(self, GetClassRoot(kClassArrayClass), length); |
| 173 | } |
| 174 | |
| 175 | inline mirror::ObjectArray<mirror::String>* ClassLinker::AllocStringArray(Thread* self, |
| 176 | size_t length) { |
| 177 | return mirror::ObjectArray<mirror::String>::Alloc(self, GetClassRoot(kJavaLangStringArrayClass), |
| 178 | length); |
| 179 | } |
| 180 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 181 | inline mirror::ObjectArray<mirror::ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self, |
| 182 | size_t length) { |
| 183 | return mirror::ObjectArray<mirror::ArtMethod>::Alloc(self, |
| 184 | GetClassRoot(kJavaLangReflectArtMethodArrayClass), length); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | inline mirror::IfTable* ClassLinker::AllocIfTable(Thread* self, size_t ifcount) { |
| 188 | return down_cast<mirror::IfTable*>( |
| 189 | mirror::IfTable::Alloc(self, GetClassRoot(kObjectArrayClass), ifcount * mirror::IfTable::kMax)); |
| 190 | } |
| 191 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 192 | inline mirror::ObjectArray<mirror::ArtField>* ClassLinker::AllocArtFieldArray(Thread* self, |
| 193 | size_t length) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 194 | return mirror::ObjectArray<mirror::ArtField>::Alloc( |
| 195 | self, GetClassRoot(kJavaLangReflectArtFieldArrayClass), length); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | inline mirror::Class* ClassLinker::GetClassRoot(ClassRoot class_root) |
| 199 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 200 | DCHECK(class_roots_ != NULL); |
| 201 | mirror::Class* klass = class_roots_->Get(class_root); |
| 202 | DCHECK(klass != NULL); |
| 203 | return klass; |
| 204 | } |
| 205 | |
| 206 | } // namespace art |
| 207 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 208 | #endif // ART_RUNTIME_CLASS_LINKER_INL_H_ |