OSDN Git Service

Differentiate between native alloc and normal background GC
[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 "obj_ptr-inl.h"
25 #include "mirror/class_loader.h"
26 #include "mirror/dex_cache-inl.h"
27 #include "mirror/iftable.h"
28 #include "mirror/object_array.h"
29 #include "handle_scope-inl.h"
30 #include "scoped_thread_state_change-inl.h"
31
32 #include <atomic>
33
34 namespace art {
35
36 inline mirror::Class* ClassLinker::FindSystemClass(Thread* self, const char* descriptor) {
37   return FindClass(self, descriptor, ScopedNullHandle<mirror::ClassLoader>());
38 }
39
40 inline mirror::Class* ClassLinker::FindArrayClass(Thread* self,
41                                                   ObjPtr<mirror::Class>* element_class) {
42   for (size_t i = 0; i < kFindArrayCacheSize; ++i) {
43     // Read the cached array class once to avoid races with other threads setting it.
44     ObjPtr<mirror::Class> array_class = find_array_class_cache_[i].Read();
45     if (array_class != nullptr && array_class->GetComponentType() == *element_class) {
46       return array_class.Ptr();
47     }
48   }
49   std::string descriptor = "[";
50   std::string temp;
51   descriptor += (*element_class)->GetDescriptor(&temp);
52   StackHandleScope<2> hs(Thread::Current());
53   Handle<mirror::ClassLoader> class_loader(hs.NewHandle((*element_class)->GetClassLoader()));
54   HandleWrapperObjPtr<mirror::Class> h_element_class(hs.NewHandleWrapper(element_class));
55   ObjPtr<mirror::Class> array_class = FindClass(self, descriptor.c_str(), class_loader);
56   if (array_class != nullptr) {
57     // Benign races in storing array class and incrementing index.
58     size_t victim_index = find_array_class_cache_next_victim_;
59     find_array_class_cache_[victim_index] = GcRoot<mirror::Class>(array_class);
60     find_array_class_cache_next_victim_ = (victim_index + 1) % kFindArrayCacheSize;
61   } else {
62     // We should have a NoClassDefFoundError.
63     self->AssertPendingException();
64   }
65   return array_class.Ptr();
66 }
67
68 inline mirror::String* ClassLinker::ResolveString(dex::StringIndex string_idx,
69                                                   ArtMethod* referrer) {
70   Thread::PoisonObjectPointersIfDebug();
71   ObjPtr<mirror::String> string = referrer->GetDexCache()->GetResolvedString(string_idx);
72   if (UNLIKELY(string == nullptr)) {
73     StackHandleScope<1> hs(Thread::Current());
74     Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
75     const DexFile& dex_file = *dex_cache->GetDexFile();
76     string = ResolveString(dex_file, string_idx, dex_cache);
77   }
78   return string.Ptr();
79 }
80
81 inline ObjPtr<mirror::Class> ClassLinker::LookupResolvedType(
82     dex::TypeIndex type_idx,
83     ObjPtr<mirror::DexCache> dex_cache,
84     ObjPtr<mirror::ClassLoader> class_loader) {
85   ObjPtr<mirror::Class> type = dex_cache->GetResolvedType(type_idx);
86   if (type == nullptr) {
87     type = Runtime::Current()->GetClassLinker()->LookupResolvedType(
88         *dex_cache->GetDexFile(), type_idx, dex_cache, class_loader);
89   }
90   return type;
91 }
92
93 inline mirror::Class* ClassLinker::ResolveType(dex::TypeIndex type_idx, ArtMethod* referrer) {
94   Thread::PoisonObjectPointersIfDebug();
95   if (kIsDebugBuild) {
96     Thread::Current()->AssertNoPendingException();
97   }
98   ObjPtr<mirror::Class> resolved_type = referrer->GetDexCache()->GetResolvedType(type_idx);
99   if (UNLIKELY(resolved_type == nullptr)) {
100     StackHandleScope<2> hs(Thread::Current());
101     ObjPtr<mirror::Class> declaring_class = referrer->GetDeclaringClass();
102     Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
103     Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
104     const DexFile& dex_file = *dex_cache->GetDexFile();
105     resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
106   }
107   return resolved_type.Ptr();
108 }
109
110 inline ArtMethod* ClassLinker::GetResolvedMethod(uint32_t method_idx, ArtMethod* referrer) {
111   ArtMethod* resolved_method = referrer->GetDexCacheResolvedMethod(method_idx, image_pointer_size_);
112   if (resolved_method == nullptr || resolved_method->IsRuntimeMethod()) {
113     return nullptr;
114   }
115   return resolved_method;
116 }
117
118 inline mirror::Class* ClassLinker::ResolveReferencedClassOfMethod(
119     uint32_t method_idx,
120     Handle<mirror::DexCache> dex_cache,
121     Handle<mirror::ClassLoader> class_loader) {
122   // NB: We cannot simply use `GetResolvedMethod(method_idx, ...)->GetDeclaringClass()`. This is
123   // because if we did so than an invoke-super could be incorrectly dispatched in cases where
124   // GetMethodId(method_idx).class_idx_ refers to a non-interface, non-direct-superclass
125   // (super*-class?) of the referrer and the direct superclass of the referrer contains a concrete
126   // implementation of the method. If this class's implementation of the method is copied from an
127   // interface (either miranda, default or conflict) we would incorrectly assume that is what we
128   // want to invoke on, instead of the 'concrete' implementation that the direct superclass
129   // contains.
130   const DexFile* dex_file = dex_cache->GetDexFile();
131   const DexFile::MethodId& method = dex_file->GetMethodId(method_idx);
132   ObjPtr<mirror::Class> resolved_type = dex_cache->GetResolvedType(method.class_idx_);
133   if (UNLIKELY(resolved_type == nullptr)) {
134     resolved_type = ResolveType(*dex_file, method.class_idx_, dex_cache, class_loader);
135   }
136   return resolved_type.Ptr();
137 }
138
139 template <ClassLinker::ResolveMode kResolveMode>
140 inline ArtMethod* ClassLinker::ResolveMethod(Thread* self,
141                                              uint32_t method_idx,
142                                              ArtMethod* referrer,
143                                              InvokeType type) {
144   ArtMethod* resolved_method = GetResolvedMethod(method_idx, referrer);
145   Thread::PoisonObjectPointersIfDebug();
146   if (UNLIKELY(resolved_method == nullptr)) {
147     ObjPtr<mirror::Class> declaring_class = referrer->GetDeclaringClass();
148     StackHandleScope<2> hs(self);
149     Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(referrer->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::LookupResolvedField(uint32_t field_idx,
165                                                   ArtMethod* referrer,
166                                                   bool is_static) {
167   ObjPtr<mirror::DexCache> dex_cache = referrer->GetDexCache();
168   ArtField* field = dex_cache->GetResolvedField(field_idx, image_pointer_size_);
169   if (field == nullptr) {
170     field = LookupResolvedField(field_idx, dex_cache, referrer->GetClassLoader(), is_static);
171   }
172   return field;
173 }
174
175 inline ArtField* ClassLinker::ResolveField(uint32_t field_idx,
176                                            ArtMethod* referrer,
177                                            bool is_static) {
178   Thread::PoisonObjectPointersIfDebug();
179   ObjPtr<mirror::Class> declaring_class = referrer->GetDeclaringClass();
180   ArtField* resolved_field =
181       referrer->GetDexCache()->GetResolvedField(field_idx, image_pointer_size_);
182   if (UNLIKELY(resolved_field == nullptr)) {
183     StackHandleScope<2> hs(Thread::Current());
184     Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
185     Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
186     const DexFile& dex_file = *dex_cache->GetDexFile();
187     resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
188     // Note: We cannot check here to see whether we added the field to the cache. The type
189     //       might be an erroneous class, which results in it being hidden from us.
190   }
191   return resolved_field;
192 }
193
194 inline mirror::Object* ClassLinker::AllocObject(Thread* self) {
195   return GetClassRoot(kJavaLangObject)->Alloc<true, false>(
196       self,
197       Runtime::Current()->GetHeap()->GetCurrentAllocator()).Ptr();
198 }
199
200 template <class T>
201 inline mirror::ObjectArray<T>* ClassLinker::AllocObjectArray(Thread* self, size_t length) {
202   return mirror::ObjectArray<T>::Alloc(self, GetClassRoot(kObjectArrayClass), length);
203 }
204
205 inline mirror::ObjectArray<mirror::Class>* ClassLinker::AllocClassArray(Thread* self,
206                                                                         size_t length) {
207   return mirror::ObjectArray<mirror::Class>::Alloc(self, GetClassRoot(kClassArrayClass), length);
208 }
209
210 inline mirror::ObjectArray<mirror::String>* ClassLinker::AllocStringArray(Thread* self,
211                                                                           size_t length) {
212   return mirror::ObjectArray<mirror::String>::Alloc(self,
213                                                     GetClassRoot(kJavaLangStringArrayClass),
214                                                     length);
215 }
216
217 inline mirror::IfTable* ClassLinker::AllocIfTable(Thread* self, size_t ifcount) {
218   return down_cast<mirror::IfTable*>(
219       mirror::IfTable::Alloc(self,
220                              GetClassRoot(kObjectArrayClass),
221                              ifcount * mirror::IfTable::kMax));
222 }
223
224 inline mirror::Class* ClassLinker::GetClassRoot(ClassRoot class_root) {
225   DCHECK(!class_roots_.IsNull());
226   mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read();
227   ObjPtr<mirror::Class> klass = class_roots->Get(class_root);
228   DCHECK(klass != nullptr);
229   return klass.Ptr();
230 }
231
232 template<ReadBarrierOption kReadBarrierOption>
233 ArtMethod* ClassLinker::FindMethodForProxy(ObjPtr<mirror::Class> proxy_class,
234                                            ArtMethod* proxy_method) {
235   DCHECK(proxy_class->IsProxyClass());
236   DCHECK(proxy_method->IsProxyMethod());
237   {
238     Thread* const self = Thread::Current();
239     ReaderMutexLock mu(self, *Locks::dex_lock_);
240     // Locate the dex cache of the original interface/Object
241     for (const DexCacheData& data : dex_caches_) {
242       if (!self->IsJWeakCleared(data.weak_root) &&
243           proxy_method->HasSameDexCacheResolvedMethods(data.resolved_methods,
244                                                        image_pointer_size_)) {
245         ObjPtr<mirror::DexCache> dex_cache =
246             ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
247         if (dex_cache != nullptr) {
248           ArtMethod* resolved_method = dex_cache->GetResolvedMethod(
249               proxy_method->GetDexMethodIndex(), image_pointer_size_);
250           CHECK(resolved_method != nullptr);
251           return resolved_method;
252         }
253       }
254     }
255   }
256   LOG(FATAL) << "Didn't find dex cache for " << proxy_class->PrettyClass() << " "
257       << proxy_method->PrettyMethod();
258   UNREACHABLE();
259 }
260
261 }  // namespace art
262
263 #endif  // ART_RUNTIME_CLASS_LINKER_INL_H_