blob: 9ddc6cf0aee8f8f9db35e39c6883f346f96aad82 [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 Chartier28357fa2016-10-18 16:27:40 -070024#include "obj_ptr-inl.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070025#include "mirror/class_loader.h"
Mathieu Chartierbc56fc32014-06-03 15:37:03 -070026#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/iftable.h"
28#include "mirror/object_array.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070029#include "handle_scope-inl.h"
Mathieu Chartierc4f39252016-10-05 18:32:08 -070030#include "scoped_thread_state_change-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070032#include <atomic>
33
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034namespace art {
35
Ian Rogers98379392014-02-24 16:53:16 -080036inline mirror::Class* ClassLinker::FindSystemClass(Thread* self, const char* descriptor) {
Mathieu Chartier9865bde2015-12-21 09:58:16 -080037 return FindClass(self, descriptor, ScopedNullHandle<mirror::ClassLoader>());
Ian Rogers98379392014-02-24 16:53:16 -080038}
39
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070040inline mirror::Class* ClassLinker::FindArrayClass(Thread* self,
41 ObjPtr<mirror::Class>* element_class) {
Ian Rogers98379392014-02-24 16:53:16 -080042 for (size_t i = 0; i < kFindArrayCacheSize; ++i) {
Ian Rogersa55cf412014-02-27 00:31:26 -080043 // Read the cached array class once to avoid races with other threads setting it.
Mathieu Chartier28357fa2016-10-18 16:27:40 -070044 ObjPtr<mirror::Class> array_class = find_array_class_cache_[i].Read();
Mathieu Chartierb74cd292014-05-29 14:31:33 -070045 if (array_class != nullptr && array_class->GetComponentType() == *element_class) {
Mathieu Chartier28357fa2016-10-18 16:27:40 -070046 return array_class.Ptr();
Ian Rogers98379392014-02-24 16:53:16 -080047 }
48 }
Ian Rogers1ff3c982014-08-12 02:30:58 -070049 std::string descriptor = "[";
50 std::string temp;
51 descriptor += (*element_class)->GetDescriptor(&temp);
Mathieu Chartierb74cd292014-05-29 14:31:33 -070052 StackHandleScope<2> hs(Thread::Current());
53 Handle<mirror::ClassLoader> class_loader(hs.NewHandle((*element_class)->GetClassLoader()));
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070054 HandleWrapperObjPtr<mirror::Class> h_element_class(hs.NewHandleWrapper(element_class));
Mathieu Chartier28357fa2016-10-18 16:27:40 -070055 ObjPtr<mirror::Class> array_class = FindClass(self, descriptor.c_str(), class_loader);
Nicolas Geoffray9638b642015-06-23 18:16:46 +010056 if (array_class != nullptr) {
57 // Benign races in storing array class and incrementing index.
58 size_t victim_index = find_array_class_cache_next_victim_;
59 find_array_class_cache_[victim_index] = GcRoot<mirror::Class>(array_class);
60 find_array_class_cache_next_victim_ = (victim_index + 1) % kFindArrayCacheSize;
61 } else {
62 // We should have a NoClassDefFoundError.
63 self->AssertPendingException();
64 }
Mathieu Chartier28357fa2016-10-18 16:27:40 -070065 return array_class.Ptr();
Ian Rogers98379392014-02-24 16:53:16 -080066}
67
Andreas Gampe8a0128a2016-11-28 07:38:35 -080068inline mirror::String* ClassLinker::ResolveString(dex::StringIndex string_idx,
69 ArtMethod* referrer) {
Mathieu Chartier3398c782016-09-30 10:27:43 -070070 Thread::PoisonObjectPointersIfDebug();
Vladimir Markoec786222016-12-20 16:24:13 +000071 ObjPtr<mirror::String> string = referrer->GetDexCache()->GetResolvedString(string_idx);
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070072 if (UNLIKELY(string == nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070073 StackHandleScope<1> hs(Thread::Current());
Vladimir Markoec786222016-12-20 16:24:13 +000074 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080075 const DexFile& dex_file = *dex_cache->GetDexFile();
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070076 string = ResolveString(dex_file, string_idx, dex_cache);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080077 }
Mathieu Chartier28357fa2016-10-18 16:27:40 -070078 return string.Ptr();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079}
80
Vladimir Marko8d6768d2017-03-14 10:13:21 +000081inline ObjPtr<mirror::Class> ClassLinker::LookupResolvedType(
82 dex::TypeIndex type_idx,
83 ObjPtr<mirror::DexCache> dex_cache,
84 ObjPtr<mirror::ClassLoader> class_loader) {
85 ObjPtr<mirror::Class> type = dex_cache->GetResolvedType(type_idx);
86 if (type == nullptr) {
87 type = Runtime::Current()->GetClassLinker()->LookupResolvedType(
88 *dex_cache->GetDexFile(), type_idx, dex_cache, class_loader);
89 }
90 return type;
91}
92
Andreas Gampea5b09a62016-11-17 15:21:22 -080093inline mirror::Class* ClassLinker::ResolveType(dex::TypeIndex type_idx, ArtMethod* referrer) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070094 Thread::PoisonObjectPointersIfDebug();
Mathieu Chartierfb568d32016-12-06 13:21:38 -080095 if (kIsDebugBuild) {
96 Thread::Current()->AssertNoPendingException();
97 }
Vladimir Marko942fd312017-01-16 20:52:19 +000098 ObjPtr<mirror::Class> resolved_type = referrer->GetDexCache()->GetResolvedType(type_idx);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070099 if (UNLIKELY(resolved_type == nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700100 StackHandleScope<2> hs(Thread::Current());
Vladimir Marko942fd312017-01-16 20:52:19 +0000101 ObjPtr<mirror::Class> declaring_class = referrer->GetDeclaringClass();
Alex Light4ba388a2017-01-27 10:26:49 -0800102 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
Vladimir Marko942fd312017-01-16 20:52:19 +0000103 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
104 const DexFile& dex_file = *dex_cache->GetDexFile();
105 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106 }
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700107 return resolved_type.Ptr();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800108}
109
Mathieu Chartiere401d142015-04-22 13:56:20 -0700110inline ArtMethod* ClassLinker::GetResolvedMethod(uint32_t method_idx, ArtMethod* referrer) {
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700111 ArtMethod* resolved_method = referrer->GetDexCacheResolvedMethod(method_idx, image_pointer_size_);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700112 if (resolved_method == nullptr || resolved_method->IsRuntimeMethod()) {
113 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800114 }
115 return resolved_method;
116}
117
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100118inline mirror::Class* ClassLinker::ResolveReferencedClassOfMethod(
119 uint32_t method_idx,
120 Handle<mirror::DexCache> dex_cache,
121 Handle<mirror::ClassLoader> class_loader) {
Alex Lightfedd91d2016-01-07 14:49:16 -0800122 // NB: We cannot simply use `GetResolvedMethod(method_idx, ...)->GetDeclaringClass()`. This is
123 // because if we did so than an invoke-super could be incorrectly dispatched in cases where
124 // GetMethodId(method_idx).class_idx_ refers to a non-interface, non-direct-superclass
125 // (super*-class?) of the referrer and the direct superclass of the referrer contains a concrete
126 // implementation of the method. If this class's implementation of the method is copied from an
127 // interface (either miranda, default or conflict) we would incorrectly assume that is what we
128 // want to invoke on, instead of the 'concrete' implementation that the direct superclass
129 // contains.
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100130 const DexFile* dex_file = dex_cache->GetDexFile();
Alex Lightfedd91d2016-01-07 14:49:16 -0800131 const DexFile::MethodId& method = dex_file->GetMethodId(method_idx);
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700132 ObjPtr<mirror::Class> resolved_type = dex_cache->GetResolvedType(method.class_idx_);
Alex Lightfedd91d2016-01-07 14:49:16 -0800133 if (UNLIKELY(resolved_type == nullptr)) {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100134 resolved_type = ResolveType(*dex_file, method.class_idx_, dex_cache, class_loader);
Alex Lightfedd91d2016-01-07 14:49:16 -0800135 }
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700136 return resolved_type.Ptr();
Alex Lightfedd91d2016-01-07 14:49:16 -0800137}
138
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800139template <ClassLinker::ResolveMode kResolveMode>
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700140inline ArtMethod* ClassLinker::ResolveMethod(Thread* self,
141 uint32_t method_idx,
142 ArtMethod* referrer,
143 InvokeType type) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700144 ArtMethod* resolved_method = GetResolvedMethod(method_idx, referrer);
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700145 Thread::PoisonObjectPointersIfDebug();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700146 if (UNLIKELY(resolved_method == nullptr)) {
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700147 ObjPtr<mirror::Class> declaring_class = referrer->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700148 StackHandleScope<2> hs(self);
Alex Light4ba388a2017-01-27 10:26:49 -0800149 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(referrer->GetDexCache()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700150 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
151 const DexFile* dex_file = h_dex_cache->GetDexFile();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800152 resolved_method = ResolveMethod<kResolveMode>(*dex_file,
153 method_idx,
154 h_dex_cache,
155 h_class_loader,
156 referrer,
157 type);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700158 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700159 // Note: We cannot check here to see whether we added the method to the cache. It
160 // might be an erroneous class, which results in it being hidden from us.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700161 return resolved_method;
162}
163
Vladimir Markof44d36c2017-03-14 14:18:46 +0000164inline ArtField* ClassLinker::LookupResolvedField(uint32_t field_idx,
165 ArtMethod* referrer,
166 bool is_static) {
167 ObjPtr<mirror::DexCache> dex_cache = referrer->GetDexCache();
168 ArtField* field = dex_cache->GetResolvedField(field_idx, image_pointer_size_);
169 if (field == nullptr) {
170 field = LookupResolvedField(field_idx, dex_cache, referrer->GetClassLoader(), is_static);
171 }
172 return field;
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700173}
174
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700175inline ArtField* ClassLinker::ResolveField(uint32_t field_idx,
176 ArtMethod* referrer,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700177 bool is_static) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700178 Thread::PoisonObjectPointersIfDebug();
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700179 ObjPtr<mirror::Class> declaring_class = referrer->GetDeclaringClass();
Vladimir Markof44d36c2017-03-14 14:18:46 +0000180 ArtField* resolved_field =
181 referrer->GetDexCache()->GetResolvedField(field_idx, image_pointer_size_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700182 if (UNLIKELY(resolved_field == nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700183 StackHandleScope<2> hs(Thread::Current());
Alex Lightdba61482016-12-21 08:20:29 -0800184 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700185 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186 const DexFile& dex_file = *dex_cache->GetDexFile();
187 resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700188 // Note: We cannot check here to see whether we added the field to the cache. The type
189 // might be an erroneous class, which results in it being hidden from us.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800190 }
191 return resolved_field;
192}
193
Ian Rogersc0542af2014-09-03 16:16:56 -0700194inline mirror::Object* ClassLinker::AllocObject(Thread* self) {
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700195 return GetClassRoot(kJavaLangObject)->Alloc<true, false>(
196 self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700197 Runtime::Current()->GetHeap()->GetCurrentAllocator()).Ptr();
Ian Rogersc0542af2014-09-03 16:16:56 -0700198}
199
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800200template <class T>
201inline mirror::ObjectArray<T>* ClassLinker::AllocObjectArray(Thread* self, size_t length) {
202 return mirror::ObjectArray<T>::Alloc(self, GetClassRoot(kObjectArrayClass), length);
203}
204
205inline mirror::ObjectArray<mirror::Class>* ClassLinker::AllocClassArray(Thread* self,
206 size_t length) {
207 return mirror::ObjectArray<mirror::Class>::Alloc(self, GetClassRoot(kClassArrayClass), length);
208}
209
210inline mirror::ObjectArray<mirror::String>* ClassLinker::AllocStringArray(Thread* self,
211 size_t length) {
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700212 return mirror::ObjectArray<mirror::String>::Alloc(self,
213 GetClassRoot(kJavaLangStringArrayClass),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800214 length);
215}
216
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217inline mirror::IfTable* ClassLinker::AllocIfTable(Thread* self, size_t ifcount) {
218 return down_cast<mirror::IfTable*>(
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700219 mirror::IfTable::Alloc(self,
220 GetClassRoot(kObjectArrayClass),
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700221 ifcount * mirror::IfTable::kMax));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800222}
223
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700224inline mirror::Class* ClassLinker::GetClassRoot(ClassRoot class_root) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700225 DCHECK(!class_roots_.IsNull());
226 mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read();
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700227 ObjPtr<mirror::Class> klass = class_roots->Get(class_root);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700228 DCHECK(klass != nullptr);
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700229 return klass.Ptr();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800230}
231
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700232template<ReadBarrierOption kReadBarrierOption>
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700233ArtMethod* ClassLinker::FindMethodForProxy(ObjPtr<mirror::Class> proxy_class,
234 ArtMethod* proxy_method) {
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700235 DCHECK(proxy_class->IsProxyClass());
Mathieu Chartier90c5a9b2017-02-01 13:10:06 -0800236 DCHECK(proxy_method->IsProxyMethod());
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700237 {
238 Thread* const self = Thread::Current();
Andreas Gampecc1b5352016-12-01 16:58:38 -0800239 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700240 // Locate the dex cache of the original interface/Object
241 for (const DexCacheData& data : dex_caches_) {
242 if (!self->IsJWeakCleared(data.weak_root) &&
Vladimir Marko942fd312017-01-16 20:52:19 +0000243 proxy_method->HasSameDexCacheResolvedMethods(data.resolved_methods,
244 image_pointer_size_)) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700245 ObjPtr<mirror::DexCache> dex_cache =
246 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700247 if (dex_cache != nullptr) {
248 ArtMethod* resolved_method = dex_cache->GetResolvedMethod(
249 proxy_method->GetDexMethodIndex(), image_pointer_size_);
250 CHECK(resolved_method != nullptr);
251 return resolved_method;
252 }
253 }
254 }
255 }
David Sehr709b0702016-10-13 09:12:37 -0700256 LOG(FATAL) << "Didn't find dex cache for " << proxy_class->PrettyClass() << " "
257 << proxy_method->PrettyMethod();
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700258 UNREACHABLE();
259}
260
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800261} // namespace art
262
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700263#endif // ART_RUNTIME_CLASS_LINKER_INL_H_