blob: 3af90b2b8672999c351492caafa72fb5cd4b19f6 [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
20#include "class_linker.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070021#include "gc_root-inl.h"
Mathieu Chartier52e4b432014-06-10 11:22:31 -070022#include "gc/heap-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070023#include "mirror/art_field.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 bool ClassLinker::IsInBootClassPath(const char* descriptor) {
33 DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, boot_class_path_);
34 return pair.second != nullptr;
35}
36
37inline mirror::Class* ClassLinker::FindSystemClass(Thread* self, const char* descriptor) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -070038 return FindClass(self, descriptor, NullHandle<mirror::ClassLoader>());
Ian Rogers98379392014-02-24 16:53:16 -080039}
40
Mathieu Chartierb74cd292014-05-29 14:31:33 -070041inline mirror::Class* ClassLinker::FindArrayClass(Thread* self, 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.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070044 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) {
Ian Rogers98379392014-02-24 16:53:16 -080046 return array_class;
47 }
48 }
Mathieu Chartierb74cd292014-05-29 14:31:33 -070049 DCHECK(!(*element_class)->IsPrimitiveVoid());
Ian Rogers1ff3c982014-08-12 02:30:58 -070050 std::string descriptor = "[";
51 std::string temp;
52 descriptor += (*element_class)->GetDescriptor(&temp);
Mathieu Chartierb74cd292014-05-29 14:31:33 -070053 StackHandleScope<2> hs(Thread::Current());
54 Handle<mirror::ClassLoader> class_loader(hs.NewHandle((*element_class)->GetClassLoader()));
55 HandleWrapper<mirror::Class> h_element_class(hs.NewHandleWrapper(element_class));
Ian Rogers98379392014-02-24 16:53:16 -080056 mirror::Class* array_class = FindClass(self, descriptor.c_str(), class_loader);
57 // Benign races in storing array class and incrementing index.
58 size_t victim_index = find_array_class_cache_next_victim_;
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070059 find_array_class_cache_[victim_index] = GcRoot<mirror::Class>(array_class);
Ian Rogers98379392014-02-24 16:53:16 -080060 find_array_class_cache_next_victim_ = (victim_index + 1) % kFindArrayCacheSize;
61 return array_class;
62}
63
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064inline mirror::String* ClassLinker::ResolveString(uint32_t string_idx,
Ian Rogersef7d42f2014-01-06 12:55:46 -080065 mirror::ArtMethod* referrer) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066 mirror::String* resolved_string = referrer->GetDexCacheStrings()->Get(string_idx);
67 if (UNLIKELY(resolved_string == NULL)) {
68 mirror::Class* declaring_class = referrer->GetDeclaringClass();
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
80inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx,
Ian Rogersef7d42f2014-01-06 12:55:46 -080081 mirror::ArtMethod* referrer) {
Andreas Gampe58a5af82014-07-31 16:23:49 -070082 mirror::Class* resolved_type = referrer->GetDexCacheResolvedType(type_idx);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070083 if (UNLIKELY(resolved_type == nullptr)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070085 StackHandleScope<2> hs(Thread::Current());
86 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
87 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080088 const DexFile& dex_file = *dex_cache->GetDexFile();
89 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
Andreas Gampe58a5af82014-07-31 16:23:49 -070090 // Note: We cannot check here to see whether we added the type to the cache. The type
91 // might be an erroneous class, which results in it being hidden from us.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092 }
93 return resolved_type;
94}
95
Ian Rogersef7d42f2014-01-06 12:55:46 -080096inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx, mirror::ArtField* referrer) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -070098 mirror::DexCache* dex_cache_ptr = declaring_class->GetDexCache();
99 mirror::Class* resolved_type = dex_cache_ptr->GetResolvedType(type_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100 if (UNLIKELY(resolved_type == NULL)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700101 StackHandleScope<2> hs(Thread::Current());
102 Handle<mirror::DexCache> dex_cache(hs.NewHandle(dex_cache_ptr));
103 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104 const DexFile& dex_file = *dex_cache->GetDexFile();
105 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700106 // Note: We cannot check here to see whether we added the type to the cache. The type
107 // might be an erroneous class, which results in it being hidden from us.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800108 }
109 return resolved_type;
110}
111
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700112inline mirror::ArtMethod* ClassLinker::GetResolvedMethod(uint32_t method_idx,
113 mirror::ArtMethod* referrer,
114 InvokeType type) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700115 mirror::ArtMethod* resolved_method = referrer->GetDexCacheResolvedMethod(method_idx);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700116 if (resolved_method == nullptr || resolved_method->IsRuntimeMethod()) {
117 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800118 }
119 return resolved_method;
120}
121
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700122inline mirror::ArtMethod* ClassLinker::ResolveMethod(Thread* self, uint32_t method_idx,
123 mirror::ArtMethod** referrer,
124 InvokeType type) {
125 mirror::ArtMethod* resolved_method = GetResolvedMethod(method_idx, *referrer, type);
126 if (LIKELY(resolved_method != nullptr)) {
127 return resolved_method;
128 }
129 mirror::Class* declaring_class = (*referrer)->GetDeclaringClass();
130 StackHandleScope<3> hs(self);
131 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
132 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
133 HandleWrapper<mirror::ArtMethod> h_referrer(hs.NewHandleWrapper(referrer));
134 const DexFile* dex_file = h_dex_cache->GetDexFile();
135 resolved_method = ResolveMethod(*dex_file, method_idx, h_dex_cache, h_class_loader, h_referrer,
136 type);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700137 // Note: We cannot check here to see whether we added the method to the cache. It
138 // might be an erroneous class, which results in it being hidden from us.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700139 return resolved_method;
140}
141
142inline mirror::ArtField* ClassLinker::GetResolvedField(uint32_t field_idx,
143 mirror::Class* field_declaring_class) {
144 return field_declaring_class->GetDexCache()->GetResolvedField(field_idx);
145}
146
147inline mirror::ArtField* ClassLinker::ResolveField(uint32_t field_idx, mirror::ArtMethod* referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700148 bool is_static) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700149 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700150 mirror::ArtField* resolved_field = GetResolvedField(field_idx, declaring_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800151 if (UNLIKELY(resolved_field == NULL)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700152 StackHandleScope<2> hs(Thread::Current());
153 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
154 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155 const DexFile& dex_file = *dex_cache->GetDexFile();
156 resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700157 // Note: We cannot check here to see whether we added the field to the cache. The type
158 // might be an erroneous class, which results in it being hidden from us.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800159 }
160 return resolved_field;
161}
162
163template <class T>
164inline mirror::ObjectArray<T>* ClassLinker::AllocObjectArray(Thread* self, size_t length) {
165 return mirror::ObjectArray<T>::Alloc(self, GetClassRoot(kObjectArrayClass), length);
166}
167
168inline mirror::ObjectArray<mirror::Class>* ClassLinker::AllocClassArray(Thread* self,
169 size_t length) {
170 return mirror::ObjectArray<mirror::Class>::Alloc(self, GetClassRoot(kClassArrayClass), length);
171}
172
173inline mirror::ObjectArray<mirror::String>* ClassLinker::AllocStringArray(Thread* self,
174 size_t length) {
175 return mirror::ObjectArray<mirror::String>::Alloc(self, GetClassRoot(kJavaLangStringArrayClass),
176 length);
177}
178
Brian Carlstromea46f952013-07-30 01:26:50 -0700179inline mirror::ObjectArray<mirror::ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self,
180 size_t length) {
181 return mirror::ObjectArray<mirror::ArtMethod>::Alloc(self,
182 GetClassRoot(kJavaLangReflectArtMethodArrayClass), length);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800183}
184
185inline mirror::IfTable* ClassLinker::AllocIfTable(Thread* self, size_t ifcount) {
186 return down_cast<mirror::IfTable*>(
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700187 mirror::IfTable::Alloc(self, GetClassRoot(kObjectArrayClass),
188 ifcount * mirror::IfTable::kMax));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800189}
190
Brian Carlstromea46f952013-07-30 01:26:50 -0700191inline mirror::ObjectArray<mirror::ArtField>* ClassLinker::AllocArtFieldArray(Thread* self,
192 size_t length) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700193 gc::Heap* const heap = Runtime::Current()->GetHeap();
194 // Can't have movable field arrays for mark compact since we need these arrays to always be valid
195 // so that we can do Object::VisitReferences in the case where the fields don't fit in the
196 // reference offsets word.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700197 return mirror::ObjectArray<mirror::ArtField>::Alloc(
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700198 self, GetClassRoot(kJavaLangReflectArtFieldArrayClass), length,
199 kMoveFieldArrays ? heap->GetCurrentAllocator() : heap->GetCurrentNonMovingAllocator());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800200}
201
202inline mirror::Class* ClassLinker::GetClassRoot(ClassRoot class_root)
203 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700204 DCHECK(!class_roots_.IsNull());
205 mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read();
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700206 mirror::Class* klass = class_roots->Get(class_root);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800207 DCHECK(klass != NULL);
208 return klass;
209}
210
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700211inline mirror::DexCache* ClassLinker::GetDexCache(size_t idx) {
212 dex_lock_.AssertSharedHeld(Thread::Current());
213 DCHECK(idx < dex_caches_.size());
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700214 return dex_caches_[idx].Read();
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700215}
216
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217} // namespace art
218
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700219#endif // ART_RUNTIME_CLASS_LINKER_INL_H_