blob: 648b455e50703f409c15fba08396db8282907d60 [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
Mathieu Chartierc7853442015-03-27 14:35:38 -070020#include "art_field.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "class_linker.h"
Mathieu Chartier52e4b432014-06-10 11:22:31 -070022#include "gc/heap-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070023#include "gc_root-inl.h"
24#include "handle_scope-inl.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070025#include "mirror/class_loader.h"
Mathieu Chartierbc56fc32014-06-03 15:37:03 -070026#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/iftable.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070028#include "mirror/object_array-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070029#include "obj_ptr-inl.h"
Mathieu Chartierc4f39252016-10-05 18:32:08 -070030#include "scoped_thread_state_change-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070032#include <atomic>
33
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034namespace art {
35
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070036inline mirror::Class* ClassLinker::FindArrayClass(Thread* self,
37 ObjPtr<mirror::Class>* element_class) {
Ian Rogers98379392014-02-24 16:53:16 -080038 for (size_t i = 0; i < kFindArrayCacheSize; ++i) {
Ian Rogersa55cf412014-02-27 00:31:26 -080039 // Read the cached array class once to avoid races with other threads setting it.
Mathieu Chartier28357fa2016-10-18 16:27:40 -070040 ObjPtr<mirror::Class> array_class = find_array_class_cache_[i].Read();
Mathieu Chartierb74cd292014-05-29 14:31:33 -070041 if (array_class != nullptr && array_class->GetComponentType() == *element_class) {
Mathieu Chartier28357fa2016-10-18 16:27:40 -070042 return array_class.Ptr();
Ian Rogers98379392014-02-24 16:53:16 -080043 }
44 }
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()));
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070050 HandleWrapperObjPtr<mirror::Class> h_element_class(hs.NewHandleWrapper(element_class));
Mathieu Chartier28357fa2016-10-18 16:27:40 -070051 ObjPtr<mirror::Class> array_class = FindClass(self, descriptor.c_str(), class_loader);
Nicolas Geoffray9638b642015-06-23 18:16:46 +010052 if (array_class != nullptr) {
53 // Benign races in storing array class and incrementing index.
54 size_t victim_index = find_array_class_cache_next_victim_;
55 find_array_class_cache_[victim_index] = GcRoot<mirror::Class>(array_class);
56 find_array_class_cache_next_victim_ = (victim_index + 1) % kFindArrayCacheSize;
57 } else {
58 // We should have a NoClassDefFoundError.
59 self->AssertPendingException();
60 }
Mathieu Chartier28357fa2016-10-18 16:27:40 -070061 return array_class.Ptr();
Ian Rogers98379392014-02-24 16:53:16 -080062}
63
Vladimir Marko8d6768d2017-03-14 10:13:21 +000064inline ObjPtr<mirror::Class> ClassLinker::LookupResolvedType(
65 dex::TypeIndex type_idx,
66 ObjPtr<mirror::DexCache> dex_cache,
67 ObjPtr<mirror::ClassLoader> class_loader) {
68 ObjPtr<mirror::Class> type = dex_cache->GetResolvedType(type_idx);
69 if (type == nullptr) {
70 type = Runtime::Current()->GetClassLinker()->LookupResolvedType(
71 *dex_cache->GetDexFile(), type_idx, dex_cache, class_loader);
72 }
73 return type;
74}
75
Vladimir Marko28e012a2017-12-07 11:22:59 +000076inline ObjPtr<mirror::Class> ClassLinker::ResolveType(dex::TypeIndex type_idx,
77 ArtMethod* referrer) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070078 Thread::PoisonObjectPointersIfDebug();
Mathieu Chartierfb568d32016-12-06 13:21:38 -080079 if (kIsDebugBuild) {
80 Thread::Current()->AssertNoPendingException();
81 }
Vladimir Marko942fd312017-01-16 20:52:19 +000082 ObjPtr<mirror::Class> resolved_type = referrer->GetDexCache()->GetResolvedType(type_idx);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070083 if (UNLIKELY(resolved_type == nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070084 StackHandleScope<2> hs(Thread::Current());
Vladimir Marko942fd312017-01-16 20:52:19 +000085 ObjPtr<mirror::Class> declaring_class = referrer->GetDeclaringClass();
Alex Light4ba388a2017-01-27 10:26:49 -080086 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
Vladimir Marko942fd312017-01-16 20:52:19 +000087 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
88 const DexFile& dex_file = *dex_cache->GetDexFile();
89 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080090 }
Vladimir Marko28e012a2017-12-07 11:22:59 +000091 return resolved_type;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092}
93
Vladimir Markoba118822017-06-12 15:41:56 +010094template <bool kThrowOnError, typename ClassGetter>
95inline bool ClassLinker::CheckInvokeClassMismatch(ObjPtr<mirror::DexCache> dex_cache,
96 InvokeType type,
97 ClassGetter class_getter) {
98 switch (type) {
99 case kStatic:
100 case kSuper:
101 break;
102 case kInterface: {
103 // We have to check whether the method id really belongs to an interface (dex static bytecode
104 // constraints A15, A16). Otherwise you must not invoke-interface on it.
105 ObjPtr<mirror::Class> klass = class_getter();
106 if (UNLIKELY(!klass->IsInterface())) {
107 if (kThrowOnError) {
108 ThrowIncompatibleClassChangeError(klass,
109 "Found class %s, but interface was expected",
110 klass->PrettyDescriptor().c_str());
111 }
112 return true;
113 }
114 break;
115 }
116 case kDirect:
117 if (dex_cache->GetDexFile()->GetVersion() >= DexFile::kDefaultMethodsVersion) {
118 break;
119 }
120 FALLTHROUGH_INTENDED;
121 case kVirtual: {
122 // Similarly, invoke-virtual (and invoke-direct without default methods) must reference
123 // a non-interface class (dex static bytecode constraint A24, A25).
124 ObjPtr<mirror::Class> klass = class_getter();
125 if (UNLIKELY(klass->IsInterface())) {
126 if (kThrowOnError) {
127 ThrowIncompatibleClassChangeError(klass,
128 "Found interface %s, but class was expected",
129 klass->PrettyDescriptor().c_str());
130 }
131 return true;
132 }
133 break;
134 }
135 default:
136 LOG(FATAL) << "Unreachable - invocation type: " << type;
137 UNREACHABLE();
138 }
139 return false;
140}
141
142template <bool kThrow>
143inline bool ClassLinker::CheckInvokeClassMismatch(ObjPtr<mirror::DexCache> dex_cache,
144 InvokeType type,
145 uint32_t method_idx,
146 ObjPtr<mirror::ClassLoader> class_loader) {
147 return CheckInvokeClassMismatch<kThrow>(
148 dex_cache,
149 type,
150 [this, dex_cache, method_idx, class_loader]() REQUIRES_SHARED(Locks::mutator_lock_) {
151 const DexFile& dex_file = *dex_cache->GetDexFile();
152 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
153 ObjPtr<mirror::Class> klass =
154 LookupResolvedType(dex_file, method_id.class_idx_, dex_cache, class_loader);
155 DCHECK(klass != nullptr);
156 return klass;
157 });
158}
159
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100160inline ArtMethod* ClassLinker::LookupResolvedMethod(uint32_t method_idx,
161 ObjPtr<mirror::DexCache> dex_cache,
162 ObjPtr<mirror::ClassLoader> class_loader) {
163 PointerSize pointer_size = image_pointer_size_;
164 ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, pointer_size);
165 if (resolved == nullptr) {
166 const DexFile& dex_file = *dex_cache->GetDexFile();
167 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
168 ObjPtr<mirror::Class> klass = LookupResolvedType(method_id.class_idx_, dex_cache, class_loader);
169 if (klass != nullptr) {
170 if (klass->IsInterface()) {
171 resolved = klass->FindInterfaceMethod(dex_cache, method_idx, pointer_size);
172 } else {
173 resolved = klass->FindClassMethod(dex_cache, method_idx, pointer_size);
174 }
175 if (resolved != nullptr) {
176 dex_cache->SetResolvedMethod(method_idx, resolved, pointer_size);
177 }
178 }
179 }
180 return resolved;
181}
182
Vladimir Markoba118822017-06-12 15:41:56 +0100183template <InvokeType type, ClassLinker::ResolveMode kResolveMode>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700184inline ArtMethod* ClassLinker::GetResolvedMethod(uint32_t method_idx, ArtMethod* referrer) {
Vladimir Markoba118822017-06-12 15:41:56 +0100185 DCHECK(referrer != nullptr);
186 // Note: The referrer can be a Proxy constructor. In that case, we need to do the
187 // lookup in the context of the original method from where it steals the code.
188 // However, we delay the GetInterfaceMethodIfProxy() until needed.
189 DCHECK(!referrer->IsProxyMethod() || referrer->IsConstructor());
Vladimir Marko5122e6b2017-08-17 16:10:09 +0100190 ArtMethod* resolved_method = referrer->GetDexCache<kWithoutReadBarrier>()->GetResolvedMethod(
191 method_idx, image_pointer_size_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100192 if (resolved_method == nullptr) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700193 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800194 }
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100195 DCHECK(!resolved_method->IsRuntimeMethod());
Vladimir Markoba118822017-06-12 15:41:56 +0100196 if (kResolveMode == ResolveMode::kCheckICCEAndIAE) {
197 referrer = referrer->GetInterfaceMethodIfProxy(image_pointer_size_);
198 // Check if the invoke type matches the class type.
199 ObjPtr<mirror::DexCache> dex_cache = referrer->GetDexCache();
200 ObjPtr<mirror::ClassLoader> class_loader = referrer->GetClassLoader();
201 if (CheckInvokeClassMismatch</* kThrow */ false>(dex_cache, type, method_idx, class_loader)) {
202 return nullptr;
203 }
204 // Check access.
205 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
206 if (!referring_class->CanAccessResolvedMethod(resolved_method->GetDeclaringClass(),
207 resolved_method,
208 dex_cache,
209 method_idx)) {
210 return nullptr;
211 }
212 // Check if the invoke type matches the method type.
213 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
214 return nullptr;
215 }
Alex Lightfedd91d2016-01-07 14:49:16 -0800216 }
Vladimir Markoba118822017-06-12 15:41:56 +0100217 return resolved_method;
Alex Lightfedd91d2016-01-07 14:49:16 -0800218}
219
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800220template <ClassLinker::ResolveMode kResolveMode>
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700221inline ArtMethod* ClassLinker::ResolveMethod(Thread* self,
222 uint32_t method_idx,
223 ArtMethod* referrer,
224 InvokeType type) {
Vladimir Markoba118822017-06-12 15:41:56 +0100225 DCHECK(referrer != nullptr);
226 // Note: The referrer can be a Proxy constructor. In that case, we need to do the
227 // lookup in the context of the original method from where it steals the code.
228 // However, we delay the GetInterfaceMethodIfProxy() until needed.
229 DCHECK(!referrer->IsProxyMethod() || referrer->IsConstructor());
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700230 Thread::PoisonObjectPointersIfDebug();
Vladimir Marko5122e6b2017-08-17 16:10:09 +0100231 ArtMethod* resolved_method = referrer->GetDexCache<kWithoutReadBarrier>()->GetResolvedMethod(
232 method_idx, image_pointer_size_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100233 DCHECK(resolved_method == nullptr || !resolved_method->IsRuntimeMethod());
234 if (UNLIKELY(resolved_method == nullptr)) {
Vladimir Markoba118822017-06-12 15:41:56 +0100235 referrer = referrer->GetInterfaceMethodIfProxy(image_pointer_size_);
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700236 ObjPtr<mirror::Class> declaring_class = referrer->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700237 StackHandleScope<2> hs(self);
Alex Light4ba388a2017-01-27 10:26:49 -0800238 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(referrer->GetDexCache()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700239 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
240 const DexFile* dex_file = h_dex_cache->GetDexFile();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800241 resolved_method = ResolveMethod<kResolveMode>(*dex_file,
242 method_idx,
243 h_dex_cache,
244 h_class_loader,
245 referrer,
246 type);
Vladimir Markoba118822017-06-12 15:41:56 +0100247 } else if (kResolveMode == ResolveMode::kCheckICCEAndIAE) {
248 referrer = referrer->GetInterfaceMethodIfProxy(image_pointer_size_);
249 // Check if the invoke type matches the class type.
250 ObjPtr<mirror::DexCache> dex_cache = referrer->GetDexCache();
251 ObjPtr<mirror::ClassLoader> class_loader = referrer->GetClassLoader();
252 if (CheckInvokeClassMismatch</* kThrow */ true>(dex_cache, type, method_idx, class_loader)) {
253 DCHECK(Thread::Current()->IsExceptionPending());
254 return nullptr;
255 }
256 // Check access.
257 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
258 if (!referring_class->CheckResolvedMethodAccess(resolved_method->GetDeclaringClass(),
259 resolved_method,
260 dex_cache,
261 method_idx,
262 type)) {
263 DCHECK(Thread::Current()->IsExceptionPending());
264 return nullptr;
265 }
266 // Check if the invoke type matches the method type.
267 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
268 ThrowIncompatibleClassChangeError(type,
269 resolved_method->GetInvokeType(),
270 resolved_method,
271 referrer);
272 return nullptr;
273 }
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700274 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700275 // Note: We cannot check here to see whether we added the method to the cache. It
276 // might be an erroneous class, which results in it being hidden from us.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700277 return resolved_method;
278}
279
Vladimir Markof44d36c2017-03-14 14:18:46 +0000280inline ArtField* ClassLinker::LookupResolvedField(uint32_t field_idx,
281 ArtMethod* referrer,
282 bool is_static) {
283 ObjPtr<mirror::DexCache> dex_cache = referrer->GetDexCache();
284 ArtField* field = dex_cache->GetResolvedField(field_idx, image_pointer_size_);
285 if (field == nullptr) {
286 field = LookupResolvedField(field_idx, dex_cache, referrer->GetClassLoader(), is_static);
287 }
288 return field;
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700289}
290
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700291inline ArtField* ClassLinker::ResolveField(uint32_t field_idx,
292 ArtMethod* referrer,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700293 bool is_static) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700294 Thread::PoisonObjectPointersIfDebug();
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700295 ObjPtr<mirror::Class> declaring_class = referrer->GetDeclaringClass();
Vladimir Markof44d36c2017-03-14 14:18:46 +0000296 ArtField* resolved_field =
297 referrer->GetDexCache()->GetResolvedField(field_idx, image_pointer_size_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700298 if (UNLIKELY(resolved_field == nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700299 StackHandleScope<2> hs(Thread::Current());
Alex Lightdba61482016-12-21 08:20:29 -0800300 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700301 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800302 const DexFile& dex_file = *dex_cache->GetDexFile();
303 resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700304 // Note: We cannot check here to see whether we added the field to the cache. The type
305 // might be an erroneous class, which results in it being hidden from us.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800306 }
307 return resolved_field;
308}
309
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700310inline mirror::Class* ClassLinker::GetClassRoot(ClassRoot class_root) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700311 DCHECK(!class_roots_.IsNull());
312 mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read();
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700313 ObjPtr<mirror::Class> klass = class_roots->Get(class_root);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700314 DCHECK(klass != nullptr);
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700315 return klass.Ptr();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800316}
317
Mathieu Chartier72041a02017-07-14 18:23:25 -0700318template <class Visitor>
319inline void ClassLinker::VisitClassTables(const Visitor& visitor) {
320 Thread* const self = Thread::Current();
321 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
322 for (const ClassLoaderData& data : class_loaders_) {
323 if (data.class_table != nullptr) {
324 visitor(data.class_table);
325 }
326 }
327}
328
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800329} // namespace art
330
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700331#endif // ART_RUNTIME_CLASS_LINKER_INL_H_