blob: d5b3090d32d8ed96b368f067bd1a4e93557b608a [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070030#include <atomic>
31
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032namespace art {
33
Ian Rogers98379392014-02-24 16:53:16 -080034inline mirror::Class* ClassLinker::FindSystemClass(Thread* self, const char* descriptor) {
Mathieu Chartier9865bde2015-12-21 09:58:16 -080035 return FindClass(self, descriptor, ScopedNullHandle<mirror::ClassLoader>());
Ian Rogers98379392014-02-24 16:53:16 -080036}
37
Mathieu Chartierb74cd292014-05-29 14:31:33 -070038inline mirror::Class* ClassLinker::FindArrayClass(Thread* self, mirror::Class** element_class) {
Ian Rogers98379392014-02-24 16:53:16 -080039 for (size_t i = 0; i < kFindArrayCacheSize; ++i) {
Ian Rogersa55cf412014-02-27 00:31:26 -080040 // Read the cached array class once to avoid races with other threads setting it.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070041 mirror::Class* array_class = find_array_class_cache_[i].Read();
Mathieu Chartierb74cd292014-05-29 14:31:33 -070042 if (array_class != nullptr && array_class->GetComponentType() == *element_class) {
Ian Rogers98379392014-02-24 16:53:16 -080043 return array_class;
44 }
45 }
Ian Rogers1ff3c982014-08-12 02:30:58 -070046 std::string descriptor = "[";
47 std::string temp;
48 descriptor += (*element_class)->GetDescriptor(&temp);
Mathieu Chartierb74cd292014-05-29 14:31:33 -070049 StackHandleScope<2> hs(Thread::Current());
50 Handle<mirror::ClassLoader> class_loader(hs.NewHandle((*element_class)->GetClassLoader()));
51 HandleWrapper<mirror::Class> h_element_class(hs.NewHandleWrapper(element_class));
Ian Rogers98379392014-02-24 16:53:16 -080052 mirror::Class* array_class = FindClass(self, descriptor.c_str(), class_loader);
Nicolas Geoffray9638b642015-06-23 18:16:46 +010053 if (array_class != nullptr) {
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] = GcRoot<mirror::Class>(array_class);
57 find_array_class_cache_next_victim_ = (victim_index + 1) % kFindArrayCacheSize;
58 } else {
59 // We should have a NoClassDefFoundError.
60 self->AssertPendingException();
61 }
Ian Rogers98379392014-02-24 16:53:16 -080062 return array_class;
63}
64
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -070065inline mirror::String* ClassLinker::ResolveString(uint32_t string_idx, ArtMethod* referrer) {
Mathieu Chartiereace4582014-11-24 18:29:54 -080066 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Vladimir Marko05792b92015-08-03 11:56:49 +010067 // MethodVerifier refuses methods with string_idx out of bounds.
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070068 DCHECK_LT(string_idx, declaring_class->GetDexFile().NumStringIds());;
69 mirror::String* string =
70 mirror::StringDexCachePair::LookupString(declaring_class->GetDexCacheStrings(),
71 string_idx,
72 mirror::DexCache::kDexCacheStringCacheSize).Read();
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070073 Thread::PoisonObjectPointersIfDebug();
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070074 if (UNLIKELY(string == nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070075 StackHandleScope<1> hs(Thread::Current());
76 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080077 const DexFile& dex_file = *dex_cache->GetDexFile();
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070078 string = ResolveString(dex_file, string_idx, dex_cache);
79 if (string != nullptr) {
80 DCHECK_EQ(dex_cache->GetResolvedString(string_idx), string);
Ian Rogerseebe03a2014-05-02 11:09:57 -070081 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080082 }
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070083 return string;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084}
85
Vladimir Marko05792b92015-08-03 11:56:49 +010086inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx, ArtMethod* referrer) {
87 mirror::Class* resolved_type = referrer->GetDexCacheResolvedType(type_idx, image_pointer_size_);
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070088 Thread::PoisonObjectPointersIfDebug();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070089 if (UNLIKELY(resolved_type == nullptr)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080090 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070091 StackHandleScope<2> hs(Thread::Current());
92 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
93 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094 const DexFile& dex_file = *dex_cache->GetDexFile();
95 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
Andreas Gampe58a5af82014-07-31 16:23:49 -070096 // Note: We cannot check here to see whether we added the type to the cache. The type
97 // might be an erroneous class, which results in it being hidden from us.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098 }
99 return resolved_type;
100}
101
Mathieu Chartierc7853442015-03-27 14:35:38 -0700102inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx, ArtField* referrer) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700104 mirror::DexCache* dex_cache_ptr = declaring_class->GetDexCache();
105 mirror::Class* resolved_type = dex_cache_ptr->GetResolvedType(type_idx);
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700106 Thread::PoisonObjectPointersIfDebug();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700107 if (UNLIKELY(resolved_type == nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700108 StackHandleScope<2> hs(Thread::Current());
109 Handle<mirror::DexCache> dex_cache(hs.NewHandle(dex_cache_ptr));
110 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111 const DexFile& dex_file = *dex_cache->GetDexFile();
112 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700113 // Note: We cannot check here to see whether we added the type to the cache. The type
114 // might be an erroneous class, which results in it being hidden from us.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115 }
116 return resolved_type;
117}
118
Mathieu Chartiere401d142015-04-22 13:56:20 -0700119inline ArtMethod* ClassLinker::GetResolvedMethod(uint32_t method_idx, ArtMethod* referrer) {
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700120 ArtMethod* resolved_method = referrer->GetDexCacheResolvedMethod(method_idx, image_pointer_size_);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700121 if (resolved_method == nullptr || resolved_method->IsRuntimeMethod()) {
122 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800123 }
124 return resolved_method;
125}
126
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100127inline mirror::Class* ClassLinker::ResolveReferencedClassOfMethod(
128 uint32_t method_idx,
129 Handle<mirror::DexCache> dex_cache,
130 Handle<mirror::ClassLoader> class_loader) {
Alex Lightfedd91d2016-01-07 14:49:16 -0800131 // NB: We cannot simply use `GetResolvedMethod(method_idx, ...)->GetDeclaringClass()`. This is
132 // because if we did so than an invoke-super could be incorrectly dispatched in cases where
133 // GetMethodId(method_idx).class_idx_ refers to a non-interface, non-direct-superclass
134 // (super*-class?) of the referrer and the direct superclass of the referrer contains a concrete
135 // implementation of the method. If this class's implementation of the method is copied from an
136 // interface (either miranda, default or conflict) we would incorrectly assume that is what we
137 // want to invoke on, instead of the 'concrete' implementation that the direct superclass
138 // contains.
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100139 const DexFile* dex_file = dex_cache->GetDexFile();
Alex Lightfedd91d2016-01-07 14:49:16 -0800140 const DexFile::MethodId& method = dex_file->GetMethodId(method_idx);
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100141 mirror::Class* resolved_type = dex_cache->GetResolvedType(method.class_idx_);
Alex Lightfedd91d2016-01-07 14:49:16 -0800142 if (UNLIKELY(resolved_type == nullptr)) {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100143 resolved_type = ResolveType(*dex_file, method.class_idx_, dex_cache, class_loader);
Alex Lightfedd91d2016-01-07 14:49:16 -0800144 }
145 return resolved_type;
146}
147
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800148template <ClassLinker::ResolveMode kResolveMode>
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700149inline ArtMethod* ClassLinker::ResolveMethod(Thread* self,
150 uint32_t method_idx,
151 ArtMethod* referrer,
152 InvokeType type) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700153 ArtMethod* resolved_method = GetResolvedMethod(method_idx, referrer);
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700154 Thread::PoisonObjectPointersIfDebug();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700155 if (UNLIKELY(resolved_method == nullptr)) {
156 mirror::Class* declaring_class = referrer->GetDeclaringClass();
157 StackHandleScope<2> hs(self);
158 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
159 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
160 const DexFile* dex_file = h_dex_cache->GetDexFile();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800161 resolved_method = ResolveMethod<kResolveMode>(*dex_file,
162 method_idx,
163 h_dex_cache,
164 h_class_loader,
165 referrer,
166 type);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700167 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700168 // Note: We cannot check here to see whether we added the method to the cache. It
169 // might be an erroneous class, which results in it being hidden from us.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700170 return resolved_method;
171}
172
Mathieu Chartierc7853442015-03-27 14:35:38 -0700173inline ArtField* ClassLinker::GetResolvedField(uint32_t field_idx, mirror::DexCache* dex_cache) {
174 return dex_cache->GetResolvedField(field_idx, image_pointer_size_);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700175}
176
Mathieu Chartierc7853442015-03-27 14:35:38 -0700177inline ArtField* ClassLinker::GetResolvedField(
178 uint32_t field_idx, mirror::Class* field_declaring_class) {
179 return GetResolvedField(field_idx, field_declaring_class->GetDexCache());
180}
181
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700182inline ArtField* ClassLinker::ResolveField(uint32_t field_idx, ArtMethod* referrer,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700183 bool is_static) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700184 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700185 ArtField* resolved_field = GetResolvedField(field_idx, declaring_class);
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700186 Thread::PoisonObjectPointersIfDebug();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700187 if (UNLIKELY(resolved_field == nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700188 StackHandleScope<2> hs(Thread::Current());
189 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
190 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800191 const DexFile& dex_file = *dex_cache->GetDexFile();
192 resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700193 // Note: We cannot check here to see whether we added the field to the cache. The type
194 // might be an erroneous class, which results in it being hidden from us.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800195 }
196 return resolved_field;
197}
198
Ian Rogersc0542af2014-09-03 16:16:56 -0700199inline mirror::Object* ClassLinker::AllocObject(Thread* self) {
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700200 return GetClassRoot(kJavaLangObject)->Alloc<true, false>(
201 self,
Ian Rogersc0542af2014-09-03 16:16:56 -0700202 Runtime::Current()->GetHeap()->GetCurrentAllocator());
203}
204
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800205template <class T>
206inline mirror::ObjectArray<T>* ClassLinker::AllocObjectArray(Thread* self, size_t length) {
207 return mirror::ObjectArray<T>::Alloc(self, GetClassRoot(kObjectArrayClass), length);
208}
209
210inline mirror::ObjectArray<mirror::Class>* ClassLinker::AllocClassArray(Thread* self,
211 size_t length) {
212 return mirror::ObjectArray<mirror::Class>::Alloc(self, GetClassRoot(kClassArrayClass), length);
213}
214
215inline mirror::ObjectArray<mirror::String>* ClassLinker::AllocStringArray(Thread* self,
216 size_t length) {
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700217 return mirror::ObjectArray<mirror::String>::Alloc(self,
218 GetClassRoot(kJavaLangStringArrayClass),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800219 length);
220}
221
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800222inline mirror::IfTable* ClassLinker::AllocIfTable(Thread* self, size_t ifcount) {
223 return down_cast<mirror::IfTable*>(
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700224 mirror::IfTable::Alloc(self,
225 GetClassRoot(kObjectArrayClass),
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700226 ifcount * mirror::IfTable::kMax));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800227}
228
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800229inline mirror::Class* ClassLinker::GetClassRoot(ClassRoot class_root)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700230 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700231 DCHECK(!class_roots_.IsNull());
232 mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read();
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700233 mirror::Class* klass = class_roots->Get(class_root);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700234 DCHECK(klass != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235 return klass;
236}
237
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700238template<ReadBarrierOption kReadBarrierOption>
239ArtMethod* ClassLinker::FindMethodForProxy(mirror::Class* proxy_class, ArtMethod* proxy_method) {
240 DCHECK(proxy_class->IsProxyClass());
241 DCHECK(proxy_method->IsProxyMethod<kReadBarrierOption>());
242 {
243 Thread* const self = Thread::Current();
244 ReaderMutexLock mu(self, dex_lock_);
245 // Locate the dex cache of the original interface/Object
246 for (const DexCacheData& data : dex_caches_) {
247 if (!self->IsJWeakCleared(data.weak_root) &&
248 proxy_method->HasSameDexCacheResolvedTypes(data.resolved_types,
249 image_pointer_size_)) {
250 mirror::DexCache* dex_cache = down_cast<mirror::DexCache*>(
251 self->DecodeJObject(data.weak_root));
252 if (dex_cache != nullptr) {
253 ArtMethod* resolved_method = dex_cache->GetResolvedMethod(
254 proxy_method->GetDexMethodIndex(), image_pointer_size_);
255 CHECK(resolved_method != nullptr);
256 return resolved_method;
257 }
258 }
259 }
260 }
261 LOG(FATAL) << "Didn't find dex cache for " << PrettyClass(proxy_class) << " "
262 << PrettyMethod(proxy_method);
263 UNREACHABLE();
264}
265
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800266} // namespace art
267
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700268#endif // ART_RUNTIME_CLASS_LINKER_INL_H_