blob: 3c51f526165f6da8f56c7540c07f3d4ad6f48c44 [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"
Andreas Gampec15a2f42017-04-21 12:09:39 -070028#include "mirror/object_array-inl.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
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070036inline mirror::Class* ClassLinker::FindArrayClass(Thread* self,
37 ObjPtr<mirror::Class>* element_class) {
Ian Rogers98379392014-02-24 16:53:16 -080038 for (size_t i = 0; i < kFindArrayCacheSize; ++i) {
Ian Rogersa55cf412014-02-27 00:31:26 -080039 // Read the cached array class once to avoid races with other threads setting it.
Mathieu Chartier28357fa2016-10-18 16:27:40 -070040 ObjPtr<mirror::Class> array_class = find_array_class_cache_[i].Read();
Mathieu Chartierb74cd292014-05-29 14:31:33 -070041 if (array_class != nullptr && array_class->GetComponentType() == *element_class) {
Mathieu Chartier28357fa2016-10-18 16:27:40 -070042 return array_class.Ptr();
Ian Rogers98379392014-02-24 16:53:16 -080043 }
44 }
Ian Rogers1ff3c982014-08-12 02:30:58 -070045 std::string descriptor = "[";
46 std::string temp;
47 descriptor += (*element_class)->GetDescriptor(&temp);
Mathieu Chartierb74cd292014-05-29 14:31:33 -070048 StackHandleScope<2> hs(Thread::Current());
49 Handle<mirror::ClassLoader> class_loader(hs.NewHandle((*element_class)->GetClassLoader()));
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070050 HandleWrapperObjPtr<mirror::Class> h_element_class(hs.NewHandleWrapper(element_class));
Mathieu Chartier28357fa2016-10-18 16:27:40 -070051 ObjPtr<mirror::Class> array_class = FindClass(self, descriptor.c_str(), class_loader);
Nicolas Geoffray9638b642015-06-23 18:16:46 +010052 if (array_class != nullptr) {
53 // Benign races in storing array class and incrementing index.
54 size_t victim_index = find_array_class_cache_next_victim_;
55 find_array_class_cache_[victim_index] = GcRoot<mirror::Class>(array_class);
56 find_array_class_cache_next_victim_ = (victim_index + 1) % kFindArrayCacheSize;
57 } else {
58 // We should have a NoClassDefFoundError.
59 self->AssertPendingException();
60 }
Mathieu Chartier28357fa2016-10-18 16:27:40 -070061 return array_class.Ptr();
Ian Rogers98379392014-02-24 16:53:16 -080062}
63
Vladimir Marko8d6768d2017-03-14 10:13:21 +000064inline ObjPtr<mirror::Class> ClassLinker::LookupResolvedType(
65 dex::TypeIndex type_idx,
66 ObjPtr<mirror::DexCache> dex_cache,
67 ObjPtr<mirror::ClassLoader> class_loader) {
68 ObjPtr<mirror::Class> type = dex_cache->GetResolvedType(type_idx);
69 if (type == nullptr) {
70 type = Runtime::Current()->GetClassLinker()->LookupResolvedType(
71 *dex_cache->GetDexFile(), type_idx, dex_cache, class_loader);
72 }
73 return type;
74}
75
Andreas Gampea5b09a62016-11-17 15:21:22 -080076inline mirror::Class* ClassLinker::ResolveType(dex::TypeIndex type_idx, ArtMethod* referrer) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070077 Thread::PoisonObjectPointersIfDebug();
Mathieu Chartierfb568d32016-12-06 13:21:38 -080078 if (kIsDebugBuild) {
79 Thread::Current()->AssertNoPendingException();
80 }
Vladimir Marko942fd312017-01-16 20:52:19 +000081 ObjPtr<mirror::Class> resolved_type = referrer->GetDexCache()->GetResolvedType(type_idx);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070082 if (UNLIKELY(resolved_type == nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070083 StackHandleScope<2> hs(Thread::Current());
Vladimir Marko942fd312017-01-16 20:52:19 +000084 ObjPtr<mirror::Class> declaring_class = referrer->GetDeclaringClass();
Alex Light4ba388a2017-01-27 10:26:49 -080085 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
Vladimir Marko942fd312017-01-16 20:52:19 +000086 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
87 const DexFile& dex_file = *dex_cache->GetDexFile();
88 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080089 }
Mathieu Chartier28357fa2016-10-18 16:27:40 -070090 return resolved_type.Ptr();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091}
92
Mathieu Chartiere401d142015-04-22 13:56:20 -070093inline ArtMethod* ClassLinker::GetResolvedMethod(uint32_t method_idx, ArtMethod* referrer) {
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -070094 ArtMethod* resolved_method = referrer->GetDexCacheResolvedMethod(method_idx, image_pointer_size_);
Mathieu Chartier0cd81352014-05-22 16:48:55 -070095 if (resolved_method == nullptr || resolved_method->IsRuntimeMethod()) {
96 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097 }
98 return resolved_method;
99}
100
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100101inline mirror::Class* ClassLinker::ResolveReferencedClassOfMethod(
102 uint32_t method_idx,
103 Handle<mirror::DexCache> dex_cache,
104 Handle<mirror::ClassLoader> class_loader) {
Alex Lightfedd91d2016-01-07 14:49:16 -0800105 // NB: We cannot simply use `GetResolvedMethod(method_idx, ...)->GetDeclaringClass()`. This is
106 // because if we did so than an invoke-super could be incorrectly dispatched in cases where
107 // GetMethodId(method_idx).class_idx_ refers to a non-interface, non-direct-superclass
108 // (super*-class?) of the referrer and the direct superclass of the referrer contains a concrete
109 // implementation of the method. If this class's implementation of the method is copied from an
110 // interface (either miranda, default or conflict) we would incorrectly assume that is what we
111 // want to invoke on, instead of the 'concrete' implementation that the direct superclass
112 // contains.
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100113 const DexFile* dex_file = dex_cache->GetDexFile();
Alex Lightfedd91d2016-01-07 14:49:16 -0800114 const DexFile::MethodId& method = dex_file->GetMethodId(method_idx);
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700115 ObjPtr<mirror::Class> resolved_type = dex_cache->GetResolvedType(method.class_idx_);
Alex Lightfedd91d2016-01-07 14:49:16 -0800116 if (UNLIKELY(resolved_type == nullptr)) {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100117 resolved_type = ResolveType(*dex_file, method.class_idx_, dex_cache, class_loader);
Alex Lightfedd91d2016-01-07 14:49:16 -0800118 }
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700119 return resolved_type.Ptr();
Alex Lightfedd91d2016-01-07 14:49:16 -0800120}
121
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800122template <ClassLinker::ResolveMode kResolveMode>
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700123inline ArtMethod* ClassLinker::ResolveMethod(Thread* self,
124 uint32_t method_idx,
125 ArtMethod* referrer,
126 InvokeType type) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700127 ArtMethod* resolved_method = GetResolvedMethod(method_idx, referrer);
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700128 Thread::PoisonObjectPointersIfDebug();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700129 if (UNLIKELY(resolved_method == nullptr)) {
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700130 ObjPtr<mirror::Class> declaring_class = referrer->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700131 StackHandleScope<2> hs(self);
Alex Light4ba388a2017-01-27 10:26:49 -0800132 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(referrer->GetDexCache()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700133 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
134 const DexFile* dex_file = h_dex_cache->GetDexFile();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800135 resolved_method = ResolveMethod<kResolveMode>(*dex_file,
136 method_idx,
137 h_dex_cache,
138 h_class_loader,
139 referrer,
140 type);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700141 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700142 // Note: We cannot check here to see whether we added the method to the cache. It
143 // might be an erroneous class, which results in it being hidden from us.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700144 return resolved_method;
145}
146
Vladimir Markof44d36c2017-03-14 14:18:46 +0000147inline ArtField* ClassLinker::LookupResolvedField(uint32_t field_idx,
148 ArtMethod* referrer,
149 bool is_static) {
150 ObjPtr<mirror::DexCache> dex_cache = referrer->GetDexCache();
151 ArtField* field = dex_cache->GetResolvedField(field_idx, image_pointer_size_);
152 if (field == nullptr) {
153 field = LookupResolvedField(field_idx, dex_cache, referrer->GetClassLoader(), is_static);
154 }
155 return field;
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700156}
157
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700158inline ArtField* ClassLinker::ResolveField(uint32_t field_idx,
159 ArtMethod* referrer,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700160 bool is_static) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700161 Thread::PoisonObjectPointersIfDebug();
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700162 ObjPtr<mirror::Class> declaring_class = referrer->GetDeclaringClass();
Vladimir Markof44d36c2017-03-14 14:18:46 +0000163 ArtField* resolved_field =
164 referrer->GetDexCache()->GetResolvedField(field_idx, image_pointer_size_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700165 if (UNLIKELY(resolved_field == nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700166 StackHandleScope<2> hs(Thread::Current());
Alex Lightdba61482016-12-21 08:20:29 -0800167 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700168 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 const DexFile& dex_file = *dex_cache->GetDexFile();
170 resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700171 // Note: We cannot check here to see whether we added the field to the cache. The type
172 // might be an erroneous class, which results in it being hidden from us.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173 }
174 return resolved_field;
175}
176
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700177inline mirror::Class* ClassLinker::GetClassRoot(ClassRoot class_root) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700178 DCHECK(!class_roots_.IsNull());
179 mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read();
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700180 ObjPtr<mirror::Class> klass = class_roots->Get(class_root);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700181 DCHECK(klass != nullptr);
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700182 return klass.Ptr();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800183}
184
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700185template<ReadBarrierOption kReadBarrierOption>
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700186ArtMethod* ClassLinker::FindMethodForProxy(ObjPtr<mirror::Class> proxy_class,
187 ArtMethod* proxy_method) {
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700188 DCHECK(proxy_class->IsProxyClass());
Mathieu Chartier90c5a9b2017-02-01 13:10:06 -0800189 DCHECK(proxy_method->IsProxyMethod());
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700190 {
191 Thread* const self = Thread::Current();
Andreas Gampecc1b5352016-12-01 16:58:38 -0800192 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700193 // Locate the dex cache of the original interface/Object
194 for (const DexCacheData& data : dex_caches_) {
195 if (!self->IsJWeakCleared(data.weak_root) &&
Vladimir Marko942fd312017-01-16 20:52:19 +0000196 proxy_method->HasSameDexCacheResolvedMethods(data.resolved_methods,
197 image_pointer_size_)) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700198 ObjPtr<mirror::DexCache> dex_cache =
199 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700200 if (dex_cache != nullptr) {
201 ArtMethod* resolved_method = dex_cache->GetResolvedMethod(
202 proxy_method->GetDexMethodIndex(), image_pointer_size_);
203 CHECK(resolved_method != nullptr);
204 return resolved_method;
205 }
206 }
207 }
208 }
David Sehr709b0702016-10-13 09:12:37 -0700209 LOG(FATAL) << "Didn't find dex cache for " << proxy_class->PrettyClass() << " "
210 << proxy_method->PrettyMethod();
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700211 UNREACHABLE();
212}
213
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800214} // namespace art
215
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700216#endif // ART_RUNTIME_CLASS_LINKER_INL_H_