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 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 20 | #include "art_field.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 21 | #include "class_linker.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 22 | #include "gc_root-inl.h" |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 23 | #include "gc/heap-inl.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 24 | #include "mirror/class_loader.h" |
Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 25 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 26 | #include "mirror/iftable.h" |
| 27 | #include "mirror/object_array.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 28 | #include "handle_scope-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 32 | inline mirror::Class* ClassLinker::FindSystemClass(Thread* self, const char* descriptor) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 33 | return FindClass(self, descriptor, NullHandle<mirror::ClassLoader>()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 34 | } |
| 35 | |
Mathieu Chartier | b74cd29 | 2014-05-29 14:31:33 -0700 | [diff] [blame] | 36 | inline mirror::Class* ClassLinker::FindArrayClass(Thread* self, mirror::Class** element_class) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 37 | for (size_t i = 0; i < kFindArrayCacheSize; ++i) { |
Ian Rogers | a55cf41 | 2014-02-27 00:31:26 -0800 | [diff] [blame] | 38 | // Read the cached array class once to avoid races with other threads setting it. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 39 | mirror::Class* array_class = find_array_class_cache_[i].Read(); |
Mathieu Chartier | b74cd29 | 2014-05-29 14:31:33 -0700 | [diff] [blame] | 40 | if (array_class != nullptr && array_class->GetComponentType() == *element_class) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 41 | return array_class; |
| 42 | } |
| 43 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 44 | std::string descriptor = "["; |
| 45 | std::string temp; |
| 46 | descriptor += (*element_class)->GetDescriptor(&temp); |
Mathieu Chartier | b74cd29 | 2014-05-29 14:31:33 -0700 | [diff] [blame] | 47 | StackHandleScope<2> hs(Thread::Current()); |
| 48 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle((*element_class)->GetClassLoader())); |
| 49 | HandleWrapper<mirror::Class> h_element_class(hs.NewHandleWrapper(element_class)); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 50 | mirror::Class* array_class = FindClass(self, descriptor.c_str(), class_loader); |
Nicolas Geoffray | 9638b64 | 2015-06-23 18:16:46 +0100 | [diff] [blame] | 51 | if (array_class != nullptr) { |
| 52 | // Benign races in storing array class and incrementing index. |
| 53 | size_t victim_index = find_array_class_cache_next_victim_; |
| 54 | find_array_class_cache_[victim_index] = GcRoot<mirror::Class>(array_class); |
| 55 | find_array_class_cache_next_victim_ = (victim_index + 1) % kFindArrayCacheSize; |
| 56 | } else { |
| 57 | // We should have a NoClassDefFoundError. |
| 58 | self->AssertPendingException(); |
| 59 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 60 | return array_class; |
| 61 | } |
| 62 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame^] | 63 | inline mirror::String* ClassLinker::ResolveString(uint32_t string_idx, ArtMethod* referrer) { |
Mathieu Chartier | eace458 | 2014-11-24 18:29:54 -0800 | [diff] [blame] | 64 | mirror::Class* declaring_class = referrer->GetDeclaringClass(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 65 | // MethodVerifier refuses methods with string_idx out of bounds. |
| 66 | DCHECK_LT(string_idx, declaring_class->GetDexCache()->NumStrings()); |
| 67 | mirror::String* resolved_string = declaring_class->GetDexCacheStrings()[string_idx].Read(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 68 | if (UNLIKELY(resolved_string == nullptr)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 69 | StackHandleScope<1> hs(Thread::Current()); |
| 70 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache())); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 71 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 72 | resolved_string = ResolveString(dex_file, string_idx, dex_cache); |
Ian Rogers | eebe03a | 2014-05-02 11:09:57 -0700 | [diff] [blame] | 73 | if (resolved_string != nullptr) { |
| 74 | DCHECK_EQ(dex_cache->GetResolvedString(string_idx), resolved_string); |
| 75 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 76 | } |
| 77 | return resolved_string; |
| 78 | } |
| 79 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 80 | inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx, ArtMethod* referrer) { |
| 81 | mirror::Class* resolved_type = referrer->GetDexCacheResolvedType(type_idx, image_pointer_size_); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 82 | if (UNLIKELY(resolved_type == nullptr)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 83 | mirror::Class* declaring_class = referrer->GetDeclaringClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 84 | StackHandleScope<2> hs(Thread::Current()); |
| 85 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache())); |
| 86 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader())); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 87 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 88 | resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 89 | // Note: We cannot check here to see whether we added the type to the cache. The type |
| 90 | // might be an erroneous class, which results in it being hidden from us. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 91 | } |
| 92 | return resolved_type; |
| 93 | } |
| 94 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 95 | inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx, ArtField* referrer) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 96 | mirror::Class* declaring_class = referrer->GetDeclaringClass(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 97 | mirror::DexCache* dex_cache_ptr = declaring_class->GetDexCache(); |
| 98 | mirror::Class* resolved_type = dex_cache_ptr->GetResolvedType(type_idx); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 99 | if (UNLIKELY(resolved_type == nullptr)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 100 | StackHandleScope<2> hs(Thread::Current()); |
| 101 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(dex_cache_ptr)); |
| 102 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader())); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 103 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 104 | resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 105 | // Note: We cannot check here to see whether we added the type to the cache. The type |
| 106 | // might be an erroneous class, which results in it being hidden from us. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 107 | } |
| 108 | return resolved_type; |
| 109 | } |
| 110 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 111 | inline ArtMethod* ClassLinker::GetResolvedMethod(uint32_t method_idx, ArtMethod* referrer) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame^] | 112 | ArtMethod* resolved_method = referrer->GetDexCacheResolvedMethod(method_idx, image_pointer_size_); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 113 | if (resolved_method == nullptr || resolved_method->IsRuntimeMethod()) { |
| 114 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 115 | } |
| 116 | return resolved_method; |
| 117 | } |
| 118 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame^] | 119 | inline ArtMethod* ClassLinker::ResolveMethod(Thread* self, |
| 120 | uint32_t method_idx, |
| 121 | ArtMethod* referrer, |
| 122 | InvokeType type) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 123 | ArtMethod* resolved_method = GetResolvedMethod(method_idx, referrer); |
| 124 | if (UNLIKELY(resolved_method == nullptr)) { |
| 125 | mirror::Class* declaring_class = referrer->GetDeclaringClass(); |
| 126 | StackHandleScope<2> hs(self); |
| 127 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache())); |
| 128 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader())); |
| 129 | const DexFile* dex_file = h_dex_cache->GetDexFile(); |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame^] | 130 | resolved_method = ResolveMethod(*dex_file, |
| 131 | method_idx, |
| 132 | h_dex_cache, |
| 133 | h_class_loader, |
| 134 | referrer, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 135 | type); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 136 | } |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 137 | // Note: We cannot check here to see whether we added the method to the cache. It |
| 138 | // might be an erroneous class, which results in it being hidden from us. |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 139 | return resolved_method; |
| 140 | } |
| 141 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 142 | inline ArtField* ClassLinker::GetResolvedField(uint32_t field_idx, mirror::DexCache* dex_cache) { |
| 143 | return dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 146 | inline ArtField* ClassLinker::GetResolvedField( |
| 147 | uint32_t field_idx, mirror::Class* field_declaring_class) { |
| 148 | return GetResolvedField(field_idx, field_declaring_class->GetDexCache()); |
| 149 | } |
| 150 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 151 | inline ArtField* ClassLinker::ResolveField(uint32_t field_idx, ArtMethod* referrer, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 152 | bool is_static) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 153 | mirror::Class* declaring_class = referrer->GetDeclaringClass(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 154 | ArtField* resolved_field = GetResolvedField(field_idx, declaring_class); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 155 | if (UNLIKELY(resolved_field == nullptr)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 156 | StackHandleScope<2> hs(Thread::Current()); |
| 157 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache())); |
| 158 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader())); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 159 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 160 | resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 161 | // Note: We cannot check here to see whether we added the field to the cache. The type |
| 162 | // might be an erroneous class, which results in it being hidden from us. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 163 | } |
| 164 | return resolved_field; |
| 165 | } |
| 166 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 167 | inline mirror::Object* ClassLinker::AllocObject(Thread* self) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame^] | 168 | return GetClassRoot(kJavaLangObject)->Alloc<true, false>( |
| 169 | self, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 170 | Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
| 171 | } |
| 172 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 173 | template <class T> |
| 174 | inline mirror::ObjectArray<T>* ClassLinker::AllocObjectArray(Thread* self, size_t length) { |
| 175 | return mirror::ObjectArray<T>::Alloc(self, GetClassRoot(kObjectArrayClass), length); |
| 176 | } |
| 177 | |
| 178 | inline mirror::ObjectArray<mirror::Class>* ClassLinker::AllocClassArray(Thread* self, |
| 179 | size_t length) { |
| 180 | return mirror::ObjectArray<mirror::Class>::Alloc(self, GetClassRoot(kClassArrayClass), length); |
| 181 | } |
| 182 | |
| 183 | inline mirror::ObjectArray<mirror::String>* ClassLinker::AllocStringArray(Thread* self, |
| 184 | size_t length) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame^] | 185 | return mirror::ObjectArray<mirror::String>::Alloc(self, |
| 186 | GetClassRoot(kJavaLangStringArrayClass), |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 187 | length); |
| 188 | } |
| 189 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 190 | inline mirror::IfTable* ClassLinker::AllocIfTable(Thread* self, size_t ifcount) { |
| 191 | return down_cast<mirror::IfTable*>( |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame^] | 192 | mirror::IfTable::Alloc(self, |
| 193 | GetClassRoot(kObjectArrayClass), |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 194 | ifcount * mirror::IfTable::kMax)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 197 | inline mirror::Class* ClassLinker::GetClassRoot(ClassRoot class_root) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 198 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 199 | DCHECK(!class_roots_.IsNull()); |
| 200 | mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 201 | mirror::Class* klass = class_roots->Get(class_root); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 202 | DCHECK(klass != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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_ |