OSDN Git Service

Revert "Prune uses library classes even without profile DO NOT MERGE"
[android-x86/art.git] / runtime / class_linker-inl.h
1 /*
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
17 #ifndef ART_RUNTIME_CLASS_LINKER_INL_H_
18 #define ART_RUNTIME_CLASS_LINKER_INL_H_
19
20 #include "art_field.h"
21 #include "class_linker.h"
22 #include "gc_root-inl.h"
23 #include "gc/heap-inl.h"
24 #include "mirror/class_loader.h"
25 #include "mirror/dex_cache-inl.h"
26 #include "mirror/iftable.h"
27 #include "mirror/object_array.h"
28 #include "handle_scope-inl.h"
29
30 namespace art {
31
32 inline mirror::Class* ClassLinker::FindSystemClass(Thread* self, const char* descriptor) {
33   return FindClass(self, descriptor, ScopedNullHandle<mirror::ClassLoader>());
34 }
35
36 inline mirror::Class* ClassLinker::FindArrayClass(Thread* self, mirror::Class** element_class) {
37   for (size_t i = 0; i < kFindArrayCacheSize; ++i) {
38     // Read the cached array class once to avoid races with other threads setting it.
39     mirror::Class* array_class = find_array_class_cache_[i].Read();
40     if (array_class != nullptr && array_class->GetComponentType() == *element_class) {
41       return array_class;
42     }
43   }
44   std::string descriptor = "[";
45   std::string temp;
46   descriptor += (*element_class)->GetDescriptor(&temp);
47   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));
50   mirror::Class* array_class = FindClass(self, descriptor.c_str(), class_loader);
51   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   }
60   return array_class;
61 }
62
63 inline mirror::String* ClassLinker::ResolveString(uint32_t string_idx, ArtMethod* referrer) {
64   mirror::Class* declaring_class = referrer->GetDeclaringClass();
65   // 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();
68   if (UNLIKELY(resolved_string == nullptr)) {
69     StackHandleScope<1> hs(Thread::Current());
70     Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
71     const DexFile& dex_file = *dex_cache->GetDexFile();
72     resolved_string = ResolveString(dex_file, string_idx, dex_cache);
73     if (resolved_string != nullptr) {
74       DCHECK_EQ(dex_cache->GetResolvedString(string_idx), resolved_string);
75     }
76   }
77   return resolved_string;
78 }
79
80 inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx, ArtMethod* referrer) {
81   mirror::Class* resolved_type = referrer->GetDexCacheResolvedType(type_idx, image_pointer_size_);
82   if (UNLIKELY(resolved_type == nullptr)) {
83     mirror::Class* declaring_class = referrer->GetDeclaringClass();
84     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()));
87     const DexFile& dex_file = *dex_cache->GetDexFile();
88     resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
89     // 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.
91   }
92   return resolved_type;
93 }
94
95 inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx, ArtField* referrer) {
96   mirror::Class* declaring_class = referrer->GetDeclaringClass();
97   mirror::DexCache* dex_cache_ptr = declaring_class->GetDexCache();
98   mirror::Class* resolved_type = dex_cache_ptr->GetResolvedType(type_idx);
99   if (UNLIKELY(resolved_type == nullptr)) {
100     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()));
103     const DexFile& dex_file = *dex_cache->GetDexFile();
104     resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
105     // 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.
107   }
108   return resolved_type;
109 }
110
111 inline ArtMethod* ClassLinker::GetResolvedMethod(uint32_t method_idx, ArtMethod* referrer) {
112   ArtMethod* resolved_method = referrer->GetDexCacheResolvedMethod(method_idx, image_pointer_size_);
113   if (resolved_method == nullptr || resolved_method->IsRuntimeMethod()) {
114     return nullptr;
115   }
116   return resolved_method;
117 }
118
119 inline mirror::Class* ClassLinker::ResolveReferencedClassOfMethod(
120     uint32_t method_idx,
121     Handle<mirror::DexCache> dex_cache,
122     Handle<mirror::ClassLoader> class_loader) {
123   // NB: We cannot simply use `GetResolvedMethod(method_idx, ...)->GetDeclaringClass()`. This is
124   // because if we did so than an invoke-super could be incorrectly dispatched in cases where
125   // GetMethodId(method_idx).class_idx_ refers to a non-interface, non-direct-superclass
126   // (super*-class?) of the referrer and the direct superclass of the referrer contains a concrete
127   // implementation of the method. If this class's implementation of the method is copied from an
128   // interface (either miranda, default or conflict) we would incorrectly assume that is what we
129   // want to invoke on, instead of the 'concrete' implementation that the direct superclass
130   // contains.
131   const DexFile* dex_file = dex_cache->GetDexFile();
132   const DexFile::MethodId& method = dex_file->GetMethodId(method_idx);
133   mirror::Class* resolved_type = dex_cache->GetResolvedType(method.class_idx_);
134   if (UNLIKELY(resolved_type == nullptr)) {
135     resolved_type = ResolveType(*dex_file, method.class_idx_, dex_cache, class_loader);
136   }
137   return resolved_type;
138 }
139
140 template <ClassLinker::ResolveMode kResolveMode>
141 inline ArtMethod* ClassLinker::ResolveMethod(Thread* self,
142                                              uint32_t method_idx,
143                                              ArtMethod* referrer,
144                                              InvokeType type) {
145   ArtMethod* resolved_method = GetResolvedMethod(method_idx, referrer);
146   if (UNLIKELY(resolved_method == nullptr)) {
147     mirror::Class* declaring_class = referrer->GetDeclaringClass();
148     StackHandleScope<2> hs(self);
149     Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
150     Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
151     const DexFile* dex_file = h_dex_cache->GetDexFile();
152     resolved_method = ResolveMethod<kResolveMode>(*dex_file,
153                                                   method_idx,
154                                                   h_dex_cache,
155                                                   h_class_loader,
156                                                   referrer,
157                                                   type);
158   }
159   // Note: We cannot check here to see whether we added the method to the cache. It
160   //       might be an erroneous class, which results in it being hidden from us.
161   return resolved_method;
162 }
163
164 inline ArtField* ClassLinker::GetResolvedField(uint32_t field_idx, mirror::DexCache* dex_cache) {
165   return dex_cache->GetResolvedField(field_idx, image_pointer_size_);
166 }
167
168 inline ArtField* ClassLinker::GetResolvedField(
169     uint32_t field_idx, mirror::Class* field_declaring_class) {
170   return GetResolvedField(field_idx, field_declaring_class->GetDexCache());
171 }
172
173 inline ArtField* ClassLinker::ResolveField(uint32_t field_idx, ArtMethod* referrer,
174                                            bool is_static) {
175   mirror::Class* declaring_class = referrer->GetDeclaringClass();
176   ArtField* resolved_field = GetResolvedField(field_idx, declaring_class);
177   if (UNLIKELY(resolved_field == nullptr)) {
178     StackHandleScope<2> hs(Thread::Current());
179     Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
180     Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
181     const DexFile& dex_file = *dex_cache->GetDexFile();
182     resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
183     // Note: We cannot check here to see whether we added the field to the cache. The type
184     //       might be an erroneous class, which results in it being hidden from us.
185   }
186   return resolved_field;
187 }
188
189 inline mirror::Object* ClassLinker::AllocObject(Thread* self) {
190   return GetClassRoot(kJavaLangObject)->Alloc<true, false>(
191       self,
192       Runtime::Current()->GetHeap()->GetCurrentAllocator());
193 }
194
195 template <class T>
196 inline mirror::ObjectArray<T>* ClassLinker::AllocObjectArray(Thread* self, size_t length) {
197   return mirror::ObjectArray<T>::Alloc(self, GetClassRoot(kObjectArrayClass), length);
198 }
199
200 inline mirror::ObjectArray<mirror::Class>* ClassLinker::AllocClassArray(Thread* self,
201                                                                         size_t length) {
202   return mirror::ObjectArray<mirror::Class>::Alloc(self, GetClassRoot(kClassArrayClass), length);
203 }
204
205 inline mirror::ObjectArray<mirror::String>* ClassLinker::AllocStringArray(Thread* self,
206                                                                           size_t length) {
207   return mirror::ObjectArray<mirror::String>::Alloc(self,
208                                                     GetClassRoot(kJavaLangStringArrayClass),
209                                                     length);
210 }
211
212 inline mirror::IfTable* ClassLinker::AllocIfTable(Thread* self, size_t ifcount) {
213   return down_cast<mirror::IfTable*>(
214       mirror::IfTable::Alloc(self,
215                              GetClassRoot(kObjectArrayClass),
216                              ifcount * mirror::IfTable::kMax));
217 }
218
219 inline mirror::Class* ClassLinker::GetClassRoot(ClassRoot class_root)
220     SHARED_REQUIRES(Locks::mutator_lock_) {
221   DCHECK(!class_roots_.IsNull());
222   mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read();
223   mirror::Class* klass = class_roots->Get(class_root);
224   DCHECK(klass != nullptr);
225   return klass;
226 }
227
228 }  // namespace art
229
230 #endif  // ART_RUNTIME_CLASS_LINKER_INL_H_