blob: f745088dd6e90975a2ca2bb6a8e84711883414d6 [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"
Brian Carlstromea46f952013-07-30 01:26:50 -070021#include "mirror/art_field.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070022#include "mirror/class_loader.h"
Mathieu Chartierbc56fc32014-06-03 15:37:03 -070023#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "mirror/iftable.h"
25#include "mirror/object_array.h"
Ian Rogers98379392014-02-24 16:53:16 -080026#include "object_utils.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070027#include "handle_scope-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028
29namespace art {
30
Ian Rogers98379392014-02-24 16:53:16 -080031inline bool ClassLinker::IsInBootClassPath(const char* descriptor) {
32 DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, boot_class_path_);
33 return pair.second != nullptr;
34}
35
36inline mirror::Class* ClassLinker::FindSystemClass(Thread* self, const char* descriptor) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -070037 return FindClass(self, descriptor, NullHandle<mirror::ClassLoader>());
Ian Rogers98379392014-02-24 16:53:16 -080038}
39
Mathieu Chartierb74cd292014-05-29 14:31:33 -070040inline mirror::Class* ClassLinker::FindArrayClass(Thread* self, mirror::Class** element_class) {
Ian Rogers98379392014-02-24 16:53:16 -080041 for (size_t i = 0; i < kFindArrayCacheSize; ++i) {
Ian Rogersa55cf412014-02-27 00:31:26 -080042 // Read the cached array class once to avoid races with other threads setting it.
Ian Rogers98379392014-02-24 16:53:16 -080043 mirror::Class* array_class = find_array_class_cache_[i];
Mathieu Chartierb74cd292014-05-29 14:31:33 -070044 if (array_class != nullptr && array_class->GetComponentType() == *element_class) {
Ian Rogers98379392014-02-24 16:53:16 -080045 return array_class;
46 }
47 }
Mathieu Chartierb74cd292014-05-29 14:31:33 -070048 DCHECK(!(*element_class)->IsPrimitiveVoid());
49 std::string descriptor = "[" + (*element_class)->GetDescriptor();
50 StackHandleScope<2> hs(Thread::Current());
51 Handle<mirror::ClassLoader> class_loader(hs.NewHandle((*element_class)->GetClassLoader()));
52 HandleWrapper<mirror::Class> h_element_class(hs.NewHandleWrapper(element_class));
Ian Rogers98379392014-02-24 16:53:16 -080053 mirror::Class* array_class = FindClass(self, descriptor.c_str(), class_loader);
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] = array_class;
57 find_array_class_cache_next_victim_ = (victim_index + 1) % kFindArrayCacheSize;
58 return array_class;
59}
60
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061inline mirror::String* ClassLinker::ResolveString(uint32_t string_idx,
Ian Rogersef7d42f2014-01-06 12:55:46 -080062 mirror::ArtMethod* referrer) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063 mirror::String* resolved_string = referrer->GetDexCacheStrings()->Get(string_idx);
64 if (UNLIKELY(resolved_string == NULL)) {
65 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070066 StackHandleScope<1> hs(Thread::Current());
67 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068 const DexFile& dex_file = *dex_cache->GetDexFile();
69 resolved_string = ResolveString(dex_file, string_idx, dex_cache);
Ian Rogerseebe03a2014-05-02 11:09:57 -070070 if (resolved_string != nullptr) {
71 DCHECK_EQ(dex_cache->GetResolvedString(string_idx), resolved_string);
72 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073 }
74 return resolved_string;
75}
76
77inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx,
Ian Rogersef7d42f2014-01-06 12:55:46 -080078 mirror::ArtMethod* referrer) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079 mirror::Class* resolved_type = referrer->GetDexCacheResolvedTypes()->Get(type_idx);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070080 if (UNLIKELY(resolved_type == nullptr)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070082 StackHandleScope<2> hs(Thread::Current());
83 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
84 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085 const DexFile& dex_file = *dex_cache->GetDexFile();
86 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
Ian Rogerseebe03a2014-05-02 11:09:57 -070087 if (resolved_type != nullptr) {
88 DCHECK_EQ(dex_cache->GetResolvedType(type_idx), resolved_type);
89 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080090 }
91 return resolved_type;
92}
93
Ian Rogersef7d42f2014-01-06 12:55:46 -080094inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx, mirror::ArtField* referrer) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080095 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -070096 mirror::DexCache* dex_cache_ptr = declaring_class->GetDexCache();
97 mirror::Class* resolved_type = dex_cache_ptr->GetResolvedType(type_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098 if (UNLIKELY(resolved_type == NULL)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070099 StackHandleScope<2> hs(Thread::Current());
100 Handle<mirror::DexCache> dex_cache(hs.NewHandle(dex_cache_ptr));
101 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800102 const DexFile& dex_file = *dex_cache->GetDexFile();
103 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
Ian Rogerseebe03a2014-05-02 11:09:57 -0700104 if (resolved_type != nullptr) {
105 DCHECK_EQ(dex_cache->GetResolvedType(type_idx), resolved_type);
106 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107 }
108 return resolved_type;
109}
110
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700111inline mirror::ArtMethod* ClassLinker::GetResolvedMethod(uint32_t method_idx,
112 mirror::ArtMethod* referrer,
113 InvokeType type) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700114 mirror::ArtMethod* resolved_method =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115 referrer->GetDexCacheResolvedMethods()->Get(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);
137 if (resolved_method != nullptr) {
138 DCHECK_EQ(h_dex_cache->GetResolvedMethod(method_idx), resolved_method);
139 }
140 return resolved_method;
141}
142
143inline mirror::ArtField* ClassLinker::GetResolvedField(uint32_t field_idx,
144 mirror::Class* field_declaring_class) {
145 return field_declaring_class->GetDexCache()->GetResolvedField(field_idx);
146}
147
148inline mirror::ArtField* ClassLinker::ResolveField(uint32_t field_idx, mirror::ArtMethod* referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700149 bool is_static) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700150 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700151 mirror::ArtField* resolved_field = GetResolvedField(field_idx, declaring_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800152 if (UNLIKELY(resolved_field == NULL)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700153 StackHandleScope<2> hs(Thread::Current());
154 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
155 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800156 const DexFile& dex_file = *dex_cache->GetDexFile();
157 resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
Ian Rogerseebe03a2014-05-02 11:09:57 -0700158 if (resolved_field != nullptr) {
159 DCHECK_EQ(dex_cache->GetResolvedField(field_idx), resolved_field);
160 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161 }
162 return resolved_field;
163}
164
165template <class T>
166inline mirror::ObjectArray<T>* ClassLinker::AllocObjectArray(Thread* self, size_t length) {
167 return mirror::ObjectArray<T>::Alloc(self, GetClassRoot(kObjectArrayClass), length);
168}
169
170inline mirror::ObjectArray<mirror::Class>* ClassLinker::AllocClassArray(Thread* self,
171 size_t length) {
172 return mirror::ObjectArray<mirror::Class>::Alloc(self, GetClassRoot(kClassArrayClass), length);
173}
174
175inline mirror::ObjectArray<mirror::String>* ClassLinker::AllocStringArray(Thread* self,
176 size_t length) {
177 return mirror::ObjectArray<mirror::String>::Alloc(self, GetClassRoot(kJavaLangStringArrayClass),
178 length);
179}
180
Brian Carlstromea46f952013-07-30 01:26:50 -0700181inline mirror::ObjectArray<mirror::ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self,
182 size_t length) {
183 return mirror::ObjectArray<mirror::ArtMethod>::Alloc(self,
184 GetClassRoot(kJavaLangReflectArtMethodArrayClass), length);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800185}
186
187inline mirror::IfTable* ClassLinker::AllocIfTable(Thread* self, size_t ifcount) {
188 return down_cast<mirror::IfTable*>(
189 mirror::IfTable::Alloc(self, GetClassRoot(kObjectArrayClass), ifcount * mirror::IfTable::kMax));
190}
191
Brian Carlstromea46f952013-07-30 01:26:50 -0700192inline mirror::ObjectArray<mirror::ArtField>* ClassLinker::AllocArtFieldArray(Thread* self,
193 size_t length) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700194 return mirror::ObjectArray<mirror::ArtField>::Alloc(
195 self, GetClassRoot(kJavaLangReflectArtFieldArrayClass), length);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800196}
197
198inline mirror::Class* ClassLinker::GetClassRoot(ClassRoot class_root)
199 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
200 DCHECK(class_roots_ != NULL);
201 mirror::Class* klass = class_roots_->Get(class_root);
202 DCHECK(klass != NULL);
203 return klass;
204}
205
206} // namespace art
207
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700208#endif // ART_RUNTIME_CLASS_LINKER_INL_H_