blob: 7e8a4a4fcda83041c86d82a85f8a3135c07d51ee [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
30namespace art {
31
Ian Rogers98379392014-02-24 16:53:16 -080032inline mirror::Class* ClassLinker::FindSystemClass(Thread* self, const char* descriptor) {
Mathieu Chartier9865bde2015-12-21 09:58:16 -080033 return FindClass(self, descriptor, ScopedNullHandle<mirror::ClassLoader>());
Ian Rogers98379392014-02-24 16:53:16 -080034}
35
Mathieu Chartierb74cd292014-05-29 14:31:33 -070036inline mirror::Class* ClassLinker::FindArrayClass(Thread* self, mirror::Class** element_class) {
Ian Rogers98379392014-02-24 16:53:16 -080037 for (size_t i = 0; i < kFindArrayCacheSize; ++i) {
Ian Rogersa55cf412014-02-27 00:31:26 -080038 // Read the cached array class once to avoid races with other threads setting it.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070039 mirror::Class* array_class = find_array_class_cache_[i].Read();
Mathieu Chartierb74cd292014-05-29 14:31:33 -070040 if (array_class != nullptr && array_class->GetComponentType() == *element_class) {
Ian Rogers98379392014-02-24 16:53:16 -080041 return array_class;
42 }
43 }
Ian Rogers1ff3c982014-08-12 02:30:58 -070044 std::string descriptor = "[";
45 std::string temp;
46 descriptor += (*element_class)->GetDescriptor(&temp);
Mathieu Chartierb74cd292014-05-29 14:31:33 -070047 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 Rogers98379392014-02-24 16:53:16 -080050 mirror::Class* array_class = FindClass(self, descriptor.c_str(), class_loader);
Nicolas Geoffray9638b642015-06-23 18:16:46 +010051 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 Rogers98379392014-02-24 16:53:16 -080060 return array_class;
61}
62
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -070063inline mirror::String* ClassLinker::ResolveString(uint32_t string_idx, ArtMethod* referrer) {
Mathieu Chartiereace4582014-11-24 18:29:54 -080064 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Vladimir Marko05792b92015-08-03 11:56:49 +010065 // 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 Chartier2cebb242015-04-21 16:50:40 -070068 if (UNLIKELY(resolved_string == nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070069 StackHandleScope<1> hs(Thread::Current());
70 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071 const DexFile& dex_file = *dex_cache->GetDexFile();
72 resolved_string = ResolveString(dex_file, string_idx, dex_cache);
Ian Rogerseebe03a2014-05-02 11:09:57 -070073 if (resolved_string != nullptr) {
74 DCHECK_EQ(dex_cache->GetResolvedString(string_idx), resolved_string);
75 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076 }
77 return resolved_string;
78}
79
Vladimir Marko05792b92015-08-03 11:56:49 +010080inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx, ArtMethod* referrer) {
81 mirror::Class* resolved_type = referrer->GetDexCacheResolvedType(type_idx, image_pointer_size_);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070082 if (UNLIKELY(resolved_type == nullptr)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080083 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070084 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 Rogers2dd0e2c2013-01-24 12:42:14 -080087 const DexFile& dex_file = *dex_cache->GetDexFile();
88 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
Andreas Gampe58a5af82014-07-31 16:23:49 -070089 // 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 Rogers2dd0e2c2013-01-24 12:42:14 -080091 }
92 return resolved_type;
93}
94
Mathieu Chartierc7853442015-03-27 14:35:38 -070095inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx, ArtField* referrer) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080096 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -070097 mirror::DexCache* dex_cache_ptr = declaring_class->GetDexCache();
98 mirror::Class* resolved_type = dex_cache_ptr->GetResolvedType(type_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070099 if (UNLIKELY(resolved_type == nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700100 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 Rogers2dd0e2c2013-01-24 12:42:14 -0800103 const DexFile& dex_file = *dex_cache->GetDexFile();
104 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700105 // 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 Rogers2dd0e2c2013-01-24 12:42:14 -0800107 }
108 return resolved_type;
109}
110
Mathieu Chartiere401d142015-04-22 13:56:20 -0700111inline ArtMethod* ClassLinker::GetResolvedMethod(uint32_t method_idx, ArtMethod* referrer) {
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700112 ArtMethod* resolved_method = referrer->GetDexCacheResolvedMethod(method_idx, image_pointer_size_);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700113 if (resolved_method == nullptr || resolved_method->IsRuntimeMethod()) {
114 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115 }
116 return resolved_method;
117}
118
Alex Lightfedd91d2016-01-07 14:49:16 -0800119inline mirror::Class* ClassLinker::ResolveReferencedClassOfMethod(Thread* self,
120 uint32_t method_idx,
121 ArtMethod* referrer) {
122 // 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.
130 mirror::Class* declaring_class = referrer->GetDeclaringClass();
131 StackHandleScope<2> hs(self);
132 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
133 const DexFile* dex_file = h_dex_cache->GetDexFile();
134 const DexFile::MethodId& method = dex_file->GetMethodId(method_idx);
135 mirror::Class* resolved_type = h_dex_cache->GetResolvedType(method.class_idx_);
136 if (UNLIKELY(resolved_type == nullptr)) {
137 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
138 resolved_type = ResolveType(*dex_file, method.class_idx_, h_dex_cache, class_loader);
139 }
140 return resolved_type;
141}
142
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800143template <ClassLinker::ResolveMode kResolveMode>
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700144inline ArtMethod* ClassLinker::ResolveMethod(Thread* self,
145 uint32_t method_idx,
146 ArtMethod* referrer,
147 InvokeType type) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700148 ArtMethod* resolved_method = GetResolvedMethod(method_idx, referrer);
149 if (UNLIKELY(resolved_method == nullptr)) {
150 mirror::Class* declaring_class = referrer->GetDeclaringClass();
151 StackHandleScope<2> hs(self);
152 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
153 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
154 const DexFile* dex_file = h_dex_cache->GetDexFile();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800155 resolved_method = ResolveMethod<kResolveMode>(*dex_file,
156 method_idx,
157 h_dex_cache,
158 h_class_loader,
159 referrer,
160 type);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700161 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700162 // Note: We cannot check here to see whether we added the method to the cache. It
163 // might be an erroneous class, which results in it being hidden from us.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700164 return resolved_method;
165}
166
Mathieu Chartierc7853442015-03-27 14:35:38 -0700167inline ArtField* ClassLinker::GetResolvedField(uint32_t field_idx, mirror::DexCache* dex_cache) {
168 return dex_cache->GetResolvedField(field_idx, image_pointer_size_);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700169}
170
Mathieu Chartierc7853442015-03-27 14:35:38 -0700171inline ArtField* ClassLinker::GetResolvedField(
172 uint32_t field_idx, mirror::Class* field_declaring_class) {
173 return GetResolvedField(field_idx, field_declaring_class->GetDexCache());
174}
175
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700176inline ArtField* ClassLinker::ResolveField(uint32_t field_idx, ArtMethod* referrer,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700177 bool is_static) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700178 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700179 ArtField* resolved_field = GetResolvedField(field_idx, declaring_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700180 if (UNLIKELY(resolved_field == nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700181 StackHandleScope<2> hs(Thread::Current());
182 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
183 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800184 const DexFile& dex_file = *dex_cache->GetDexFile();
185 resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700186 // Note: We cannot check here to see whether we added the field to the cache. The type
187 // might be an erroneous class, which results in it being hidden from us.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800188 }
189 return resolved_field;
190}
191
Ian Rogersc0542af2014-09-03 16:16:56 -0700192inline mirror::Object* ClassLinker::AllocObject(Thread* self) {
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700193 return GetClassRoot(kJavaLangObject)->Alloc<true, false>(
194 self,
Ian Rogersc0542af2014-09-03 16:16:56 -0700195 Runtime::Current()->GetHeap()->GetCurrentAllocator());
196}
197
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800198template <class T>
199inline mirror::ObjectArray<T>* ClassLinker::AllocObjectArray(Thread* self, size_t length) {
200 return mirror::ObjectArray<T>::Alloc(self, GetClassRoot(kObjectArrayClass), length);
201}
202
203inline mirror::ObjectArray<mirror::Class>* ClassLinker::AllocClassArray(Thread* self,
204 size_t length) {
205 return mirror::ObjectArray<mirror::Class>::Alloc(self, GetClassRoot(kClassArrayClass), length);
206}
207
208inline mirror::ObjectArray<mirror::String>* ClassLinker::AllocStringArray(Thread* self,
209 size_t length) {
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700210 return mirror::ObjectArray<mirror::String>::Alloc(self,
211 GetClassRoot(kJavaLangStringArrayClass),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800212 length);
213}
214
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800215inline mirror::IfTable* ClassLinker::AllocIfTable(Thread* self, size_t ifcount) {
216 return down_cast<mirror::IfTable*>(
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700217 mirror::IfTable::Alloc(self,
218 GetClassRoot(kObjectArrayClass),
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700219 ifcount * mirror::IfTable::kMax));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220}
221
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800222inline mirror::Class* ClassLinker::GetClassRoot(ClassRoot class_root)
Mathieu Chartier90443472015-07-16 20:32:27 -0700223 SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700224 DCHECK(!class_roots_.IsNull());
225 mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read();
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700226 mirror::Class* klass = class_roots->Get(class_root);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700227 DCHECK(klass != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800228 return klass;
229}
230
231} // namespace art
232
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700233#endif // ART_RUNTIME_CLASS_LINKER_INL_H_