blob: fa971c4c2b2146713a516e603a143b10fc1c4906 [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
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 Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_CLASS_LINKER_INL_H_
18#define ART_RUNTIME_CLASS_LINKER_INL_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
Mathieu Chartierc7853442015-03-27 14:35:38 -070020#include "art_field.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "class_linker.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070022#include "gc_root-inl.h"
Mathieu Chartier52e4b432014-06-10 11:22:31 -070023#include "gc/heap-inl.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070024#include "mirror/class_loader.h"
Mathieu Chartierbc56fc32014-06-03 15:37:03 -070025#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "mirror/iftable.h"
27#include "mirror/object_array.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070028#include "handle_scope-inl.h"
Mathieu Chartierc4f39252016-10-05 18:32:08 -070029#include "scoped_thread_state_change-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070031#include <atomic>
32
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033namespace art {
34
Ian Rogers98379392014-02-24 16:53:16 -080035inline mirror::Class* ClassLinker::FindSystemClass(Thread* self, const char* descriptor) {
Mathieu Chartier9865bde2015-12-21 09:58:16 -080036 return FindClass(self, descriptor, ScopedNullHandle<mirror::ClassLoader>());
Ian Rogers98379392014-02-24 16:53:16 -080037}
38
Mathieu Chartierb74cd292014-05-29 14:31:33 -070039inline mirror::Class* ClassLinker::FindArrayClass(Thread* self, mirror::Class** element_class) {
Ian Rogers98379392014-02-24 16:53:16 -080040 for (size_t i = 0; i < kFindArrayCacheSize; ++i) {
Ian Rogersa55cf412014-02-27 00:31:26 -080041 // Read the cached array class once to avoid races with other threads setting it.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070042 mirror::Class* array_class = find_array_class_cache_[i].Read();
Mathieu Chartierb74cd292014-05-29 14:31:33 -070043 if (array_class != nullptr && array_class->GetComponentType() == *element_class) {
Ian Rogers98379392014-02-24 16:53:16 -080044 return array_class;
45 }
46 }
Ian Rogers1ff3c982014-08-12 02:30:58 -070047 std::string descriptor = "[";
48 std::string temp;
49 descriptor += (*element_class)->GetDescriptor(&temp);
Mathieu Chartierb74cd292014-05-29 14:31:33 -070050 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 Rogers98379392014-02-24 16:53:16 -080053 mirror::Class* array_class = FindClass(self, descriptor.c_str(), class_loader);
Nicolas Geoffray9638b642015-06-23 18:16:46 +010054 if (array_class != nullptr) {
55 // Benign races in storing array class and incrementing index.
56 size_t victim_index = find_array_class_cache_next_victim_;
57 find_array_class_cache_[victim_index] = GcRoot<mirror::Class>(array_class);
58 find_array_class_cache_next_victim_ = (victim_index + 1) % kFindArrayCacheSize;
59 } else {
60 // We should have a NoClassDefFoundError.
61 self->AssertPendingException();
62 }
Ian Rogers98379392014-02-24 16:53:16 -080063 return array_class;
64}
65
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -070066inline mirror::String* ClassLinker::ResolveString(uint32_t string_idx, ArtMethod* referrer) {
Mathieu Chartier3398c782016-09-30 10:27:43 -070067 Thread::PoisonObjectPointersIfDebug();
Mathieu Chartiereace4582014-11-24 18:29:54 -080068 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Vladimir Marko05792b92015-08-03 11:56:49 +010069 // MethodVerifier refuses methods with string_idx out of bounds.
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070070 DCHECK_LT(string_idx, declaring_class->GetDexFile().NumStringIds());;
71 mirror::String* string =
Narayan Kamathc38a6f82016-09-29 17:07:20 +010072 mirror::StringDexCachePair::Lookup(declaring_class->GetDexCacheStrings(),
73 string_idx,
74 mirror::DexCache::kDexCacheStringCacheSize).Read();
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070075 if (UNLIKELY(string == nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070076 StackHandleScope<1> hs(Thread::Current());
77 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078 const DexFile& dex_file = *dex_cache->GetDexFile();
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070079 string = ResolveString(dex_file, string_idx, dex_cache);
80 if (string != nullptr) {
81 DCHECK_EQ(dex_cache->GetResolvedString(string_idx), string);
Ian Rogerseebe03a2014-05-02 11:09:57 -070082 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080083 }
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070084 return string;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085}
86
Vladimir Marko05792b92015-08-03 11:56:49 +010087inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx, ArtMethod* referrer) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070088 Thread::PoisonObjectPointersIfDebug();
Mathieu Chartier3398c782016-09-30 10:27:43 -070089 mirror::Class* resolved_type = referrer->GetDexCacheResolvedType(type_idx, image_pointer_size_);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070090 if (UNLIKELY(resolved_type == nullptr)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070092 StackHandleScope<2> hs(Thread::Current());
93 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
94 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080095 const DexFile& dex_file = *dex_cache->GetDexFile();
96 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
Andreas Gampe58a5af82014-07-31 16:23:49 -070097 // Note: We cannot check here to see whether we added the type to the cache. The type
98 // might be an erroneous class, which results in it being hidden from us.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099 }
100 return resolved_type;
101}
102
Mathieu Chartierc7853442015-03-27 14:35:38 -0700103inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx, ArtField* referrer) {
Mathieu Chartier3398c782016-09-30 10:27:43 -0700104 Thread::PoisonObjectPointersIfDebug();
105 ObjPtr<mirror::Class> declaring_class = referrer->GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700106 mirror::DexCache* dex_cache_ptr = declaring_class->GetDexCache();
107 mirror::Class* resolved_type = dex_cache_ptr->GetResolvedType(type_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700108 if (UNLIKELY(resolved_type == nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700109 StackHandleScope<2> hs(Thread::Current());
110 Handle<mirror::DexCache> dex_cache(hs.NewHandle(dex_cache_ptr));
111 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800112 const DexFile& dex_file = *dex_cache->GetDexFile();
113 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700114 // Note: We cannot check here to see whether we added the type to the cache. The type
115 // might be an erroneous class, which results in it being hidden from us.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116 }
117 return resolved_type;
118}
119
Mathieu Chartiere401d142015-04-22 13:56:20 -0700120inline ArtMethod* ClassLinker::GetResolvedMethod(uint32_t method_idx, ArtMethod* referrer) {
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700121 ArtMethod* resolved_method = referrer->GetDexCacheResolvedMethod(method_idx, image_pointer_size_);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700122 if (resolved_method == nullptr || resolved_method->IsRuntimeMethod()) {
123 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800124 }
125 return resolved_method;
126}
127
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100128inline mirror::Class* ClassLinker::ResolveReferencedClassOfMethod(
129 uint32_t method_idx,
130 Handle<mirror::DexCache> dex_cache,
131 Handle<mirror::ClassLoader> class_loader) {
Alex Lightfedd91d2016-01-07 14:49:16 -0800132 // NB: We cannot simply use `GetResolvedMethod(method_idx, ...)->GetDeclaringClass()`. This is
133 // because if we did so than an invoke-super could be incorrectly dispatched in cases where
134 // GetMethodId(method_idx).class_idx_ refers to a non-interface, non-direct-superclass
135 // (super*-class?) of the referrer and the direct superclass of the referrer contains a concrete
136 // implementation of the method. If this class's implementation of the method is copied from an
137 // interface (either miranda, default or conflict) we would incorrectly assume that is what we
138 // want to invoke on, instead of the 'concrete' implementation that the direct superclass
139 // contains.
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100140 const DexFile* dex_file = dex_cache->GetDexFile();
Alex Lightfedd91d2016-01-07 14:49:16 -0800141 const DexFile::MethodId& method = dex_file->GetMethodId(method_idx);
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100142 mirror::Class* resolved_type = dex_cache->GetResolvedType(method.class_idx_);
Alex Lightfedd91d2016-01-07 14:49:16 -0800143 if (UNLIKELY(resolved_type == nullptr)) {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100144 resolved_type = ResolveType(*dex_file, method.class_idx_, dex_cache, class_loader);
Alex Lightfedd91d2016-01-07 14:49:16 -0800145 }
146 return resolved_type;
147}
148
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800149template <ClassLinker::ResolveMode kResolveMode>
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700150inline ArtMethod* ClassLinker::ResolveMethod(Thread* self,
151 uint32_t method_idx,
152 ArtMethod* referrer,
153 InvokeType type) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700154 ArtMethod* resolved_method = GetResolvedMethod(method_idx, referrer);
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700155 Thread::PoisonObjectPointersIfDebug();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700156 if (UNLIKELY(resolved_method == nullptr)) {
157 mirror::Class* declaring_class = referrer->GetDeclaringClass();
158 StackHandleScope<2> hs(self);
159 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
160 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
161 const DexFile* dex_file = h_dex_cache->GetDexFile();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800162 resolved_method = ResolveMethod<kResolveMode>(*dex_file,
163 method_idx,
164 h_dex_cache,
165 h_class_loader,
166 referrer,
167 type);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700168 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700169 // Note: We cannot check here to see whether we added the method to the cache. It
170 // might be an erroneous class, which results in it being hidden from us.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700171 return resolved_method;
172}
173
Mathieu Chartierc7853442015-03-27 14:35:38 -0700174inline ArtField* ClassLinker::GetResolvedField(uint32_t field_idx, mirror::DexCache* dex_cache) {
175 return dex_cache->GetResolvedField(field_idx, image_pointer_size_);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700176}
177
Mathieu Chartierc7853442015-03-27 14:35:38 -0700178inline ArtField* ClassLinker::GetResolvedField(
179 uint32_t field_idx, mirror::Class* field_declaring_class) {
180 return GetResolvedField(field_idx, field_declaring_class->GetDexCache());
181}
182
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700183inline ArtField* ClassLinker::ResolveField(uint32_t field_idx, ArtMethod* referrer,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700184 bool is_static) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700185 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700186 ArtField* resolved_field = GetResolvedField(field_idx, declaring_class);
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700187 Thread::PoisonObjectPointersIfDebug();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700188 if (UNLIKELY(resolved_field == nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700189 StackHandleScope<2> hs(Thread::Current());
190 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
191 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800192 const DexFile& dex_file = *dex_cache->GetDexFile();
193 resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700194 // Note: We cannot check here to see whether we added the field to the cache. The type
195 // might be an erroneous class, which results in it being hidden from us.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800196 }
197 return resolved_field;
198}
199
Ian Rogersc0542af2014-09-03 16:16:56 -0700200inline mirror::Object* ClassLinker::AllocObject(Thread* self) {
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700201 return GetClassRoot(kJavaLangObject)->Alloc<true, false>(
202 self,
Ian Rogersc0542af2014-09-03 16:16:56 -0700203 Runtime::Current()->GetHeap()->GetCurrentAllocator());
204}
205
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800206template <class T>
207inline mirror::ObjectArray<T>* ClassLinker::AllocObjectArray(Thread* self, size_t length) {
208 return mirror::ObjectArray<T>::Alloc(self, GetClassRoot(kObjectArrayClass), length);
209}
210
211inline mirror::ObjectArray<mirror::Class>* ClassLinker::AllocClassArray(Thread* self,
212 size_t length) {
213 return mirror::ObjectArray<mirror::Class>::Alloc(self, GetClassRoot(kClassArrayClass), length);
214}
215
216inline mirror::ObjectArray<mirror::String>* ClassLinker::AllocStringArray(Thread* self,
217 size_t length) {
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700218 return mirror::ObjectArray<mirror::String>::Alloc(self,
219 GetClassRoot(kJavaLangStringArrayClass),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 length);
221}
222
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800223inline mirror::IfTable* ClassLinker::AllocIfTable(Thread* self, size_t ifcount) {
224 return down_cast<mirror::IfTable*>(
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700225 mirror::IfTable::Alloc(self,
226 GetClassRoot(kObjectArrayClass),
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700227 ifcount * mirror::IfTable::kMax));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800228}
229
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800230inline mirror::Class* ClassLinker::GetClassRoot(ClassRoot class_root)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700231 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700232 DCHECK(!class_roots_.IsNull());
233 mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read();
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700234 mirror::Class* klass = class_roots->Get(class_root);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700235 DCHECK(klass != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236 return klass;
237}
238
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700239template<ReadBarrierOption kReadBarrierOption>
240ArtMethod* ClassLinker::FindMethodForProxy(mirror::Class* proxy_class, ArtMethod* proxy_method) {
241 DCHECK(proxy_class->IsProxyClass());
242 DCHECK(proxy_method->IsProxyMethod<kReadBarrierOption>());
243 {
244 Thread* const self = Thread::Current();
245 ReaderMutexLock mu(self, dex_lock_);
246 // Locate the dex cache of the original interface/Object
247 for (const DexCacheData& data : dex_caches_) {
248 if (!self->IsJWeakCleared(data.weak_root) &&
249 proxy_method->HasSameDexCacheResolvedTypes(data.resolved_types,
250 image_pointer_size_)) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700251 ObjPtr<mirror::DexCache> dex_cache =
252 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700253 if (dex_cache != nullptr) {
254 ArtMethod* resolved_method = dex_cache->GetResolvedMethod(
255 proxy_method->GetDexMethodIndex(), image_pointer_size_);
256 CHECK(resolved_method != nullptr);
257 return resolved_method;
258 }
259 }
260 }
261 }
262 LOG(FATAL) << "Didn't find dex cache for " << PrettyClass(proxy_class) << " "
263 << PrettyMethod(proxy_method);
264 UNREACHABLE();
265}
266
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800267} // namespace art
268
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700269#endif // ART_RUNTIME_CLASS_LINKER_INL_H_