blob: 51987692d121bb5f768bc2038655d3ca762b38ee [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 mirror::Class* ClassLinker::FindSystemClass(Thread* self, const char* descriptor) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -070033 return FindClass(self, descriptor, NullHandle<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 }
Mathieu Chartierb74cd292014-05-29 14:31:33 -070044 DCHECK(!(*element_class)->IsPrimitiveVoid());
Ian Rogers1ff3c982014-08-12 02:30:58 -070045 std::string descriptor = "[";
46 std::string temp;
47 descriptor += (*element_class)->GetDescriptor(&temp);
Mathieu Chartierb74cd292014-05-29 14:31:33 -070048 StackHandleScope<2> hs(Thread::Current());
49 Handle<mirror::ClassLoader> class_loader(hs.NewHandle((*element_class)->GetClassLoader()));
50 HandleWrapper<mirror::Class> h_element_class(hs.NewHandleWrapper(element_class));
Ian Rogers98379392014-02-24 16:53:16 -080051 mirror::Class* array_class = FindClass(self, descriptor.c_str(), class_loader);
52 // Benign races in storing array class and incrementing index.
53 size_t victim_index = find_array_class_cache_next_victim_;
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070054 find_array_class_cache_[victim_index] = GcRoot<mirror::Class>(array_class);
Ian Rogers98379392014-02-24 16:53:16 -080055 find_array_class_cache_next_victim_ = (victim_index + 1) % kFindArrayCacheSize;
56 return array_class;
57}
58
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059inline mirror::String* ClassLinker::ResolveString(uint32_t string_idx,
Ian Rogersef7d42f2014-01-06 12:55:46 -080060 mirror::ArtMethod* referrer) {
Mathieu Chartiereace4582014-11-24 18:29:54 -080061 mirror::Class* declaring_class = referrer->GetDeclaringClass();
62 mirror::String* resolved_string = declaring_class->GetDexCacheStrings()->Get(string_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063 if (UNLIKELY(resolved_string == NULL)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070064 StackHandleScope<1> hs(Thread::Current());
65 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066 const DexFile& dex_file = *dex_cache->GetDexFile();
67 resolved_string = ResolveString(dex_file, string_idx, dex_cache);
Ian Rogerseebe03a2014-05-02 11:09:57 -070068 if (resolved_string != nullptr) {
69 DCHECK_EQ(dex_cache->GetResolvedString(string_idx), resolved_string);
70 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071 }
72 return resolved_string;
73}
74
75inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx,
Ian Rogersef7d42f2014-01-06 12:55:46 -080076 mirror::ArtMethod* referrer) {
Andreas Gampe58a5af82014-07-31 16:23:49 -070077 mirror::Class* resolved_type = referrer->GetDexCacheResolvedType(type_idx);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070078 if (UNLIKELY(resolved_type == nullptr)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070080 StackHandleScope<2> hs(Thread::Current());
81 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
82 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080083 const DexFile& dex_file = *dex_cache->GetDexFile();
84 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
Andreas Gampe58a5af82014-07-31 16:23:49 -070085 // Note: We cannot check here to see whether we added the type to the cache. The type
86 // might be an erroneous class, which results in it being hidden from us.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087 }
88 return resolved_type;
89}
90
Ian Rogersef7d42f2014-01-06 12:55:46 -080091inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx, mirror::ArtField* referrer) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -070093 mirror::DexCache* dex_cache_ptr = declaring_class->GetDexCache();
94 mirror::Class* resolved_type = dex_cache_ptr->GetResolvedType(type_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080095 if (UNLIKELY(resolved_type == NULL)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070096 StackHandleScope<2> hs(Thread::Current());
97 Handle<mirror::DexCache> dex_cache(hs.NewHandle(dex_cache_ptr));
98 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099 const DexFile& dex_file = *dex_cache->GetDexFile();
100 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700101 // Note: We cannot check here to see whether we added the type to the cache. The type
102 // might be an erroneous class, which results in it being hidden from us.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103 }
104 return resolved_type;
105}
106
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700107inline mirror::ArtMethod* ClassLinker::GetResolvedMethod(uint32_t method_idx,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700108 mirror::ArtMethod* referrer) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700109 mirror::ArtMethod* resolved_method = referrer->GetDexCacheResolvedMethod(method_idx);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700110 if (resolved_method == nullptr || resolved_method->IsRuntimeMethod()) {
111 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800112 }
113 return resolved_method;
114}
115
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700116inline mirror::ArtMethod* ClassLinker::ResolveMethod(Thread* self, uint32_t method_idx,
117 mirror::ArtMethod** referrer,
118 InvokeType type) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700119 mirror::ArtMethod* resolved_method = GetResolvedMethod(method_idx, *referrer);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700120 if (LIKELY(resolved_method != nullptr)) {
121 return resolved_method;
122 }
123 mirror::Class* declaring_class = (*referrer)->GetDeclaringClass();
124 StackHandleScope<3> hs(self);
125 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
126 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
127 HandleWrapper<mirror::ArtMethod> h_referrer(hs.NewHandleWrapper(referrer));
128 const DexFile* dex_file = h_dex_cache->GetDexFile();
129 resolved_method = ResolveMethod(*dex_file, method_idx, h_dex_cache, h_class_loader, h_referrer,
130 type);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700131 // Note: We cannot check here to see whether we added the method to the cache. It
132 // might be an erroneous class, which results in it being hidden from us.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700133 return resolved_method;
134}
135
136inline mirror::ArtField* ClassLinker::GetResolvedField(uint32_t field_idx,
137 mirror::Class* field_declaring_class) {
138 return field_declaring_class->GetDexCache()->GetResolvedField(field_idx);
139}
140
141inline mirror::ArtField* ClassLinker::ResolveField(uint32_t field_idx, mirror::ArtMethod* referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700142 bool is_static) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700143 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700144 mirror::ArtField* resolved_field = GetResolvedField(field_idx, declaring_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145 if (UNLIKELY(resolved_field == NULL)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700146 StackHandleScope<2> hs(Thread::Current());
147 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
148 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800149 const DexFile& dex_file = *dex_cache->GetDexFile();
150 resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700151 // Note: We cannot check here to see whether we added the field to the cache. The type
152 // might be an erroneous class, which results in it being hidden from us.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153 }
154 return resolved_field;
155}
156
Ian Rogersc0542af2014-09-03 16:16:56 -0700157inline mirror::Object* ClassLinker::AllocObject(Thread* self) {
Andreas Gampe928f72b2014-09-09 19:53:48 -0700158 return GetClassRoot(kJavaLangObject)->Alloc<true, false>(self,
Ian Rogersc0542af2014-09-03 16:16:56 -0700159 Runtime::Current()->GetHeap()->GetCurrentAllocator());
160}
161
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800162template <class T>
163inline mirror::ObjectArray<T>* ClassLinker::AllocObjectArray(Thread* self, size_t length) {
164 return mirror::ObjectArray<T>::Alloc(self, GetClassRoot(kObjectArrayClass), length);
165}
166
167inline mirror::ObjectArray<mirror::Class>* ClassLinker::AllocClassArray(Thread* self,
168 size_t length) {
169 return mirror::ObjectArray<mirror::Class>::Alloc(self, GetClassRoot(kClassArrayClass), length);
170}
171
172inline mirror::ObjectArray<mirror::String>* ClassLinker::AllocStringArray(Thread* self,
173 size_t length) {
174 return mirror::ObjectArray<mirror::String>::Alloc(self, GetClassRoot(kJavaLangStringArrayClass),
175 length);
176}
177
Brian Carlstromea46f952013-07-30 01:26:50 -0700178inline mirror::ObjectArray<mirror::ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self,
179 size_t length) {
180 return mirror::ObjectArray<mirror::ArtMethod>::Alloc(self,
181 GetClassRoot(kJavaLangReflectArtMethodArrayClass), length);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800182}
183
184inline mirror::IfTable* ClassLinker::AllocIfTable(Thread* self, size_t ifcount) {
185 return down_cast<mirror::IfTable*>(
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700186 mirror::IfTable::Alloc(self, GetClassRoot(kObjectArrayClass),
187 ifcount * mirror::IfTable::kMax));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800188}
189
Brian Carlstromea46f952013-07-30 01:26:50 -0700190inline mirror::ObjectArray<mirror::ArtField>* ClassLinker::AllocArtFieldArray(Thread* self,
191 size_t length) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700192 gc::Heap* const heap = Runtime::Current()->GetHeap();
193 // Can't have movable field arrays for mark compact since we need these arrays to always be valid
194 // so that we can do Object::VisitReferences in the case where the fields don't fit in the
195 // reference offsets word.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700196 return mirror::ObjectArray<mirror::ArtField>::Alloc(
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700197 self, GetClassRoot(kJavaLangReflectArtFieldArrayClass), length,
198 kMoveFieldArrays ? heap->GetCurrentAllocator() : heap->GetCurrentNonMovingAllocator());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800199}
200
201inline mirror::Class* ClassLinker::GetClassRoot(ClassRoot class_root)
202 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700203 DCHECK(!class_roots_.IsNull());
204 mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read();
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700205 mirror::Class* klass = class_roots->Get(class_root);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800206 DCHECK(klass != NULL);
207 return klass;
208}
209
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700210inline mirror::DexCache* ClassLinker::GetDexCache(size_t idx) {
211 dex_lock_.AssertSharedHeld(Thread::Current());
212 DCHECK(idx < dex_caches_.size());
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700213 return dex_caches_[idx].Read();
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700214}
215
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800216} // namespace art
217
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700218#endif // ART_RUNTIME_CLASS_LINKER_INL_H_