blob: 16e0ec30805ce8c66186b4188de4a70921671756 [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"
Mathieu Chartier52e4b432014-06-10 11:22:31 -070021#include "gc/heap-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070022#include "mirror/art_field.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070023#include "mirror/class_loader.h"
Mathieu Chartierbc56fc32014-06-03 15:37:03 -070024#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/iftable.h"
26#include "mirror/object_array.h"
Ian Rogers98379392014-02-24 16:53:16 -080027#include "object_utils.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.
Ian Rogers98379392014-02-24 16:53:16 -080044 mirror::Class* array_class = find_array_class_cache_[i];
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());
50 std::string descriptor = "[" + (*element_class)->GetDescriptor();
51 StackHandleScope<2> hs(Thread::Current());
52 Handle<mirror::ClassLoader> class_loader(hs.NewHandle((*element_class)->GetClassLoader()));
53 HandleWrapper<mirror::Class> h_element_class(hs.NewHandleWrapper(element_class));
Ian Rogers98379392014-02-24 16:53:16 -080054 mirror::Class* array_class = FindClass(self, descriptor.c_str(), class_loader);
55 // Benign races in storing array class and incrementing index.
56 size_t victim_index = find_array_class_cache_next_victim_;
57 find_array_class_cache_[victim_index] = array_class;
58 find_array_class_cache_next_victim_ = (victim_index + 1) % kFindArrayCacheSize;
59 return array_class;
60}
61
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080062inline mirror::String* ClassLinker::ResolveString(uint32_t string_idx,
Ian Rogersef7d42f2014-01-06 12:55:46 -080063 mirror::ArtMethod* referrer) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064 mirror::String* resolved_string = referrer->GetDexCacheStrings()->Get(string_idx);
65 if (UNLIKELY(resolved_string == NULL)) {
66 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070067 StackHandleScope<1> hs(Thread::Current());
68 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 const DexFile& dex_file = *dex_cache->GetDexFile();
70 resolved_string = ResolveString(dex_file, string_idx, dex_cache);
Ian Rogerseebe03a2014-05-02 11:09:57 -070071 if (resolved_string != nullptr) {
72 DCHECK_EQ(dex_cache->GetResolvedString(string_idx), resolved_string);
73 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074 }
75 return resolved_string;
76}
77
78inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx,
Ian Rogersef7d42f2014-01-06 12:55:46 -080079 mirror::ArtMethod* referrer) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080 mirror::Class* resolved_type = referrer->GetDexCacheResolvedTypes()->Get(type_idx);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070081 if (UNLIKELY(resolved_type == nullptr)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080082 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070083 StackHandleScope<2> hs(Thread::Current());
84 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
85 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086 const DexFile& dex_file = *dex_cache->GetDexFile();
87 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
Ian Rogerseebe03a2014-05-02 11:09:57 -070088 if (resolved_type != nullptr) {
89 DCHECK_EQ(dex_cache->GetResolvedType(type_idx), resolved_type);
90 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091 }
92 return resolved_type;
93}
94
Ian Rogersef7d42f2014-01-06 12:55:46 -080095inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx, mirror::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);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099 if (UNLIKELY(resolved_type == NULL)) {
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);
Ian Rogerseebe03a2014-05-02 11:09:57 -0700105 if (resolved_type != nullptr) {
106 DCHECK_EQ(dex_cache->GetResolvedType(type_idx), resolved_type);
107 }
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) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700115 mirror::ArtMethod* resolved_method =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116 referrer->GetDexCacheResolvedMethods()->Get(method_idx);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700117 if (resolved_method == nullptr || resolved_method->IsRuntimeMethod()) {
118 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800119 }
120 return resolved_method;
121}
122
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700123inline mirror::ArtMethod* ClassLinker::ResolveMethod(Thread* self, uint32_t method_idx,
124 mirror::ArtMethod** referrer,
125 InvokeType type) {
126 mirror::ArtMethod* resolved_method = GetResolvedMethod(method_idx, *referrer, type);
127 if (LIKELY(resolved_method != nullptr)) {
128 return resolved_method;
129 }
130 mirror::Class* declaring_class = (*referrer)->GetDeclaringClass();
131 StackHandleScope<3> hs(self);
132 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
133 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
134 HandleWrapper<mirror::ArtMethod> h_referrer(hs.NewHandleWrapper(referrer));
135 const DexFile* dex_file = h_dex_cache->GetDexFile();
136 resolved_method = ResolveMethod(*dex_file, method_idx, h_dex_cache, h_class_loader, h_referrer,
137 type);
138 if (resolved_method != nullptr) {
139 DCHECK_EQ(h_dex_cache->GetResolvedMethod(method_idx), resolved_method);
140 }
141 return resolved_method;
142}
143
144inline mirror::ArtField* ClassLinker::GetResolvedField(uint32_t field_idx,
145 mirror::Class* field_declaring_class) {
146 return field_declaring_class->GetDexCache()->GetResolvedField(field_idx);
147}
148
149inline mirror::ArtField* ClassLinker::ResolveField(uint32_t field_idx, mirror::ArtMethod* referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700150 bool is_static) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700151 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700152 mirror::ArtField* resolved_field = GetResolvedField(field_idx, declaring_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153 if (UNLIKELY(resolved_field == NULL)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700154 StackHandleScope<2> hs(Thread::Current());
155 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
156 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800157 const DexFile& dex_file = *dex_cache->GetDexFile();
158 resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
Ian Rogerseebe03a2014-05-02 11:09:57 -0700159 if (resolved_field != nullptr) {
160 DCHECK_EQ(dex_cache->GetResolvedField(field_idx), resolved_field);
161 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800162 }
163 return resolved_field;
164}
165
166template <class T>
167inline mirror::ObjectArray<T>* ClassLinker::AllocObjectArray(Thread* self, size_t length) {
168 return mirror::ObjectArray<T>::Alloc(self, GetClassRoot(kObjectArrayClass), length);
169}
170
171inline mirror::ObjectArray<mirror::Class>* ClassLinker::AllocClassArray(Thread* self,
172 size_t length) {
173 return mirror::ObjectArray<mirror::Class>::Alloc(self, GetClassRoot(kClassArrayClass), length);
174}
175
176inline mirror::ObjectArray<mirror::String>* ClassLinker::AllocStringArray(Thread* self,
177 size_t length) {
178 return mirror::ObjectArray<mirror::String>::Alloc(self, GetClassRoot(kJavaLangStringArrayClass),
179 length);
180}
181
Brian Carlstromea46f952013-07-30 01:26:50 -0700182inline mirror::ObjectArray<mirror::ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self,
183 size_t length) {
184 return mirror::ObjectArray<mirror::ArtMethod>::Alloc(self,
185 GetClassRoot(kJavaLangReflectArtMethodArrayClass), length);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186}
187
188inline mirror::IfTable* ClassLinker::AllocIfTable(Thread* self, size_t ifcount) {
189 return down_cast<mirror::IfTable*>(
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700190 mirror::IfTable::Alloc(self, GetClassRoot(kObjectArrayClass),
191 ifcount * mirror::IfTable::kMax));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800192}
193
Brian Carlstromea46f952013-07-30 01:26:50 -0700194inline mirror::ObjectArray<mirror::ArtField>* ClassLinker::AllocArtFieldArray(Thread* self,
195 size_t length) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700196 gc::Heap* const heap = Runtime::Current()->GetHeap();
197 // Can't have movable field arrays for mark compact since we need these arrays to always be valid
198 // so that we can do Object::VisitReferences in the case where the fields don't fit in the
199 // reference offsets word.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700200 return mirror::ObjectArray<mirror::ArtField>::Alloc(
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700201 self, GetClassRoot(kJavaLangReflectArtFieldArrayClass), length,
202 kMoveFieldArrays ? heap->GetCurrentAllocator() : heap->GetCurrentNonMovingAllocator());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800203}
204
205inline mirror::Class* ClassLinker::GetClassRoot(ClassRoot class_root)
206 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
207 DCHECK(class_roots_ != NULL);
208 mirror::Class* klass = class_roots_->Get(class_root);
209 DCHECK(klass != NULL);
210 return klass;
211}
212
213} // namespace art
214
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700215#endif // ART_RUNTIME_CLASS_LINKER_INL_H_