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 #include "class_linker.h"
18
19 #include <unistd.h>
20
21 #include <algorithm>
22 #include <deque>
23 #include <forward_list>
24 #include <iostream>
25 #include <map>
26 #include <memory>
27 #include <queue>
28 #include <string>
29 #include <string_view>
30 #include <tuple>
31 #include <utility>
32 #include <vector>
33
34 #include "android-base/stringprintf.h"
35
36 #include "art_field-inl.h"
37 #include "art_method-inl.h"
38 #include "barrier.h"
39 #include "base/arena_allocator.h"
40 #include "base/casts.h"
41 #include "base/file_utils.h"
42 #include "base/hash_map.h"
43 #include "base/hash_set.h"
44 #include "base/leb128.h"
45 #include "base/logging.h"
46 #include "base/metrics/metrics.h"
47 #include "base/mutex-inl.h"
48 #include "base/os.h"
49 #include "base/quasi_atomic.h"
50 #include "base/scoped_arena_containers.h"
51 #include "base/scoped_flock.h"
52 #include "base/stl_util.h"
53 #include "base/string_view_cpp20.h"
54 #include "base/systrace.h"
55 #include "base/time_utils.h"
56 #include "base/unix_file/fd_file.h"
57 #include "base/utils.h"
58 #include "base/value_object.h"
59 #include "cha.h"
60 #include "class_linker-inl.h"
61 #include "class_loader_utils.h"
62 #include "class_root-inl.h"
63 #include "class_table-inl.h"
64 #include "compiler_callbacks.h"
65 #include "debug_print.h"
66 #include "debugger.h"
67 #include "dex/class_accessor-inl.h"
68 #include "dex/descriptors_names.h"
69 #include "dex/dex_file-inl.h"
70 #include "dex/dex_file_exception_helpers.h"
71 #include "dex/dex_file_loader.h"
72 #include "dex/signature-inl.h"
73 #include "dex/utf.h"
74 #include "entrypoints/entrypoint_utils-inl.h"
75 #include "entrypoints/runtime_asm_entrypoints.h"
76 #include "experimental_flags.h"
77 #include "gc/accounting/card_table-inl.h"
78 #include "gc/accounting/heap_bitmap-inl.h"
79 #include "gc/accounting/space_bitmap-inl.h"
80 #include "gc/heap-visit-objects-inl.h"
81 #include "gc/heap.h"
82 #include "gc/scoped_gc_critical_section.h"
83 #include "gc/space/image_space.h"
84 #include "gc/space/space-inl.h"
85 #include "gc_root-inl.h"
86 #include "handle_scope-inl.h"
87 #include "hidden_api.h"
88 #include "image-inl.h"
89 #include "imt_conflict_table.h"
90 #include "imtable-inl.h"
91 #include "intern_table-inl.h"
92 #include "interpreter/interpreter.h"
93 #include "interpreter/mterp/nterp.h"
94 #include "jit/debugger_interface.h"
95 #include "jit/jit.h"
96 #include "jit/jit_code_cache.h"
97 #include "jni/java_vm_ext.h"
98 #include "jni/jni_internal.h"
99 #include "linear_alloc.h"
100 #include "mirror/array-alloc-inl.h"
101 #include "mirror/array-inl.h"
102 #include "mirror/call_site.h"
103 #include "mirror/class-alloc-inl.h"
104 #include "mirror/class-inl.h"
105 #include "mirror/class.h"
106 #include "mirror/class_ext.h"
107 #include "mirror/class_loader.h"
108 #include "mirror/dex_cache-inl.h"
109 #include "mirror/dex_cache.h"
110 #include "mirror/emulated_stack_frame.h"
111 #include "mirror/field.h"
112 #include "mirror/iftable-inl.h"
113 #include "mirror/method.h"
114 #include "mirror/method_handle_impl.h"
115 #include "mirror/method_handles_lookup.h"
116 #include "mirror/method_type.h"
117 #include "mirror/object-inl.h"
118 #include "mirror/object-refvisitor-inl.h"
119 #include "mirror/object.h"
120 #include "mirror/object_array-alloc-inl.h"
121 #include "mirror/object_array-inl.h"
122 #include "mirror/object_array.h"
123 #include "mirror/object_reference.h"
124 #include "mirror/object_reference-inl.h"
125 #include "mirror/proxy.h"
126 #include "mirror/reference-inl.h"
127 #include "mirror/stack_trace_element.h"
128 #include "mirror/string-inl.h"
129 #include "mirror/throwable.h"
130 #include "mirror/var_handle.h"
131 #include "native/dalvik_system_DexFile.h"
132 #include "nativehelper/scoped_local_ref.h"
133 #include "nterp_helpers.h"
134 #include "oat.h"
135 #include "oat_file-inl.h"
136 #include "oat_file.h"
137 #include "oat_file_assistant.h"
138 #include "oat_file_manager.h"
139 #include "object_lock.h"
140 #include "profile/profile_compilation_info.h"
141 #include "runtime.h"
142 #include "runtime_callbacks.h"
143 #include "scoped_thread_state_change-inl.h"
144 #include "thread-inl.h"
145 #include "thread.h"
146 #include "thread_list.h"
147 #include "trace.h"
148 #include "transaction.h"
149 #include "vdex_file.h"
150 #include "verifier/class_verifier.h"
151 #include "verifier/verifier_deps.h"
152 #include "well_known_classes.h"
153
154 #include "interpreter/interpreter_mterp_impl.h"
155
156 namespace art {
157
158 using android::base::StringPrintf;
159
160 static constexpr bool kCheckImageObjects = kIsDebugBuild;
161 static constexpr bool kVerifyArtMethodDeclaringClasses = kIsDebugBuild;
162
163 static void ThrowNoClassDefFoundError(const char* fmt, ...)
164 __attribute__((__format__(__printf__, 1, 2)))
165 REQUIRES_SHARED(Locks::mutator_lock_);
ThrowNoClassDefFoundError(const char * fmt,...)166 static void ThrowNoClassDefFoundError(const char* fmt, ...) {
167 va_list args;
168 va_start(args, fmt);
169 Thread* self = Thread::Current();
170 self->ThrowNewExceptionV("Ljava/lang/NoClassDefFoundError;", fmt, args);
171 va_end(args);
172 }
173
HasInitWithString(Thread * self,ClassLinker * class_linker,const char * descriptor)174 static bool HasInitWithString(Thread* self, ClassLinker* class_linker, const char* descriptor)
175 REQUIRES_SHARED(Locks::mutator_lock_) {
176 ArtMethod* method = self->GetCurrentMethod(nullptr);
177 StackHandleScope<1> hs(self);
178 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(method != nullptr ?
179 method->GetDeclaringClass()->GetClassLoader() : nullptr));
180 ObjPtr<mirror::Class> exception_class = class_linker->FindClass(self, descriptor, class_loader);
181
182 if (exception_class == nullptr) {
183 // No exc class ~ no <init>-with-string.
184 CHECK(self->IsExceptionPending());
185 self->ClearException();
186 return false;
187 }
188
189 ArtMethod* exception_init_method = exception_class->FindConstructor(
190 "(Ljava/lang/String;)V", class_linker->GetImagePointerSize());
191 return exception_init_method != nullptr;
192 }
193
GetVerifyError(ObjPtr<mirror::Class> c)194 static ObjPtr<mirror::Object> GetVerifyError(ObjPtr<mirror::Class> c)
195 REQUIRES_SHARED(Locks::mutator_lock_) {
196 ObjPtr<mirror::ClassExt> ext(c->GetExtData());
197 if (ext == nullptr) {
198 return nullptr;
199 } else {
200 return ext->GetVerifyError();
201 }
202 }
203
204 // Helper for ThrowEarlierClassFailure. Throws the stored error.
HandleEarlierVerifyError(Thread * self,ClassLinker * class_linker,ObjPtr<mirror::Class> c)205 static void HandleEarlierVerifyError(Thread* self,
206 ClassLinker* class_linker,
207 ObjPtr<mirror::Class> c)
208 REQUIRES_SHARED(Locks::mutator_lock_) {
209 ObjPtr<mirror::Object> obj = GetVerifyError(c);
210 DCHECK(obj != nullptr);
211 self->AssertNoPendingException();
212 if (obj->IsClass()) {
213 // Previous error has been stored as class. Create a new exception of that type.
214
215 // It's possible the exception doesn't have a <init>(String).
216 std::string temp;
217 const char* descriptor = obj->AsClass()->GetDescriptor(&temp);
218
219 if (HasInitWithString(self, class_linker, descriptor)) {
220 self->ThrowNewException(descriptor, c->PrettyDescriptor().c_str());
221 } else {
222 self->ThrowNewException(descriptor, nullptr);
223 }
224 } else {
225 // Previous error has been stored as an instance. Just rethrow.
226 ObjPtr<mirror::Class> throwable_class = GetClassRoot<mirror::Throwable>(class_linker);
227 ObjPtr<mirror::Class> error_class = obj->GetClass();
228 CHECK(throwable_class->IsAssignableFrom(error_class));
229 self->SetException(obj->AsThrowable());
230 }
231 self->AssertPendingException();
232 }
233
ChangeInterpreterBridgeToNterp(ArtMethod * method,ClassLinker * class_linker)234 static void ChangeInterpreterBridgeToNterp(ArtMethod* method, ClassLinker* class_linker)
235 REQUIRES_SHARED(Locks::mutator_lock_) {
236 Runtime* runtime = Runtime::Current();
237 if (class_linker->IsQuickToInterpreterBridge(method->GetEntryPointFromQuickCompiledCode()) &&
238 CanMethodUseNterp(method)) {
239 if (method->GetDeclaringClass()->IsVisiblyInitialized() ||
240 !NeedsClinitCheckBeforeCall(method)) {
241 runtime->GetInstrumentation()->UpdateMethodsCode(method, interpreter::GetNterpEntryPoint());
242 } else {
243 // Put the resolution stub, which will initialize the class and then
244 // call the method with nterp.
245 runtime->GetInstrumentation()->UpdateMethodsCode(method, GetQuickResolutionStub());
246 }
247 }
248 }
249
250 // Ensures that methods have the kAccSkipAccessChecks bit set. We use the
251 // kAccVerificationAttempted bit on the class access flags to determine whether this has been done
252 // before.
EnsureSkipAccessChecksMethods(Handle<mirror::Class> klass,PointerSize pointer_size)253 static void EnsureSkipAccessChecksMethods(Handle<mirror::Class> klass, PointerSize pointer_size)
254 REQUIRES_SHARED(Locks::mutator_lock_) {
255 Runtime* runtime = Runtime::Current();
256 ClassLinker* class_linker = runtime->GetClassLinker();
257 if (!klass->WasVerificationAttempted()) {
258 klass->SetSkipAccessChecksFlagOnAllMethods(pointer_size);
259 klass->SetVerificationAttempted();
260 // Now that the class has passed verification, try to set nterp entrypoints
261 // to methods that currently use the switch interpreter.
262 if (interpreter::CanRuntimeUseNterp()) {
263 for (ArtMethod& m : klass->GetMethods(pointer_size)) {
264 ChangeInterpreterBridgeToNterp(&m, class_linker);
265 }
266 }
267 }
268 }
269
270 // Callback responsible for making a batch of classes visibly initialized
271 // after all threads have called it from a checkpoint, ensuring visibility.
272 class ClassLinker::VisiblyInitializedCallback final
273 : public Closure, public IntrusiveForwardListNode<VisiblyInitializedCallback> {
274 public:
VisiblyInitializedCallback(ClassLinker * class_linker)275 explicit VisiblyInitializedCallback(ClassLinker* class_linker)
276 : class_linker_(class_linker),
277 num_classes_(0u),
278 thread_visibility_counter_(0),
279 barriers_() {
280 std::fill_n(classes_, kMaxClasses, nullptr);
281 }
282
IsEmpty() const283 bool IsEmpty() const {
284 DCHECK_LE(num_classes_, kMaxClasses);
285 return num_classes_ == 0u;
286 }
287
IsFull() const288 bool IsFull() const {
289 DCHECK_LE(num_classes_, kMaxClasses);
290 return num_classes_ == kMaxClasses;
291 }
292
AddClass(Thread * self,ObjPtr<mirror::Class> klass)293 void AddClass(Thread* self, ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) {
294 DCHECK_EQ(klass->GetStatus(), ClassStatus::kInitialized);
295 DCHECK(!IsFull());
296 classes_[num_classes_] = self->GetJniEnv()->GetVm()->AddWeakGlobalRef(self, klass);
297 ++num_classes_;
298 }
299
AddBarrier(Barrier * barrier)300 void AddBarrier(Barrier* barrier) {
301 barriers_.push_front(barrier);
302 }
303
GetAndClearBarriers()304 std::forward_list<Barrier*> GetAndClearBarriers() {
305 std::forward_list<Barrier*> result;
306 result.swap(barriers_);
307 result.reverse(); // Return barriers in insertion order.
308 return result;
309 }
310
MakeVisible(Thread * self)311 void MakeVisible(Thread* self) {
312 DCHECK_EQ(thread_visibility_counter_.load(std::memory_order_relaxed), 0);
313 size_t count = Runtime::Current()->GetThreadList()->RunCheckpoint(this);
314 AdjustThreadVisibilityCounter(self, count);
315 }
316
Run(Thread * self)317 void Run(Thread* self) override {
318 self->ClearMakeVisiblyInitializedCounter();
319 AdjustThreadVisibilityCounter(self, -1);
320 }
321
322 private:
AdjustThreadVisibilityCounter(Thread * self,ssize_t adjustment)323 void AdjustThreadVisibilityCounter(Thread* self, ssize_t adjustment) {
324 ssize_t old = thread_visibility_counter_.fetch_add(adjustment, std::memory_order_relaxed);
325 if (old + adjustment == 0) {
326 // All threads passed the checkpoint. Mark classes as visibly initialized.
327 {
328 ScopedObjectAccess soa(self);
329 StackHandleScope<1u> hs(self);
330 MutableHandle<mirror::Class> klass = hs.NewHandle<mirror::Class>(nullptr);
331 JavaVMExt* vm = self->GetJniEnv()->GetVm();
332 for (size_t i = 0, num = num_classes_; i != num; ++i) {
333 klass.Assign(ObjPtr<mirror::Class>::DownCast(self->DecodeJObject(classes_[i])));
334 vm->DeleteWeakGlobalRef(self, classes_[i]);
335 if (klass != nullptr) {
336 mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self);
337 class_linker_->FixupStaticTrampolines(self, klass.Get());
338 }
339 }
340 num_classes_ = 0u;
341 }
342 class_linker_->VisiblyInitializedCallbackDone(self, this);
343 }
344 }
345
346 static constexpr size_t kMaxClasses = 16;
347
348 ClassLinker* const class_linker_;
349 size_t num_classes_;
350 jweak classes_[kMaxClasses];
351
352 // The thread visibility counter starts at 0 and it is incremented by the number of
353 // threads that need to run this callback (by the thread that request the callback
354 // to be run) and decremented once for each `Run()` execution. When it reaches 0,
355 // whether after the increment or after a decrement, we know that `Run()` was executed
356 // for all threads and therefore we can mark the classes as visibly initialized.
357 std::atomic<ssize_t> thread_visibility_counter_;
358
359 // List of barries to `Pass()` for threads that wait for the callback to complete.
360 std::forward_list<Barrier*> barriers_;
361 };
362
MakeInitializedClassesVisiblyInitialized(Thread * self,bool wait)363 void ClassLinker::MakeInitializedClassesVisiblyInitialized(Thread* self, bool wait) {
364 if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) {
365 return; // Nothing to do. Thanks to the x86 memory model classes skip the initialized status.
366 }
367 std::optional<Barrier> maybe_barrier; // Avoid constructing the Barrier for `wait == false`.
368 if (wait) {
369 maybe_barrier.emplace(0);
370 }
371 int wait_count = 0;
372 VisiblyInitializedCallback* callback = nullptr;
373 {
374 MutexLock lock(self, visibly_initialized_callback_lock_);
375 if (visibly_initialized_callback_ != nullptr && !visibly_initialized_callback_->IsEmpty()) {
376 callback = visibly_initialized_callback_.release();
377 running_visibly_initialized_callbacks_.push_front(*callback);
378 }
379 if (wait) {
380 DCHECK(maybe_barrier.has_value());
381 Barrier* barrier = std::addressof(*maybe_barrier);
382 for (VisiblyInitializedCallback& cb : running_visibly_initialized_callbacks_) {
383 cb.AddBarrier(barrier);
384 ++wait_count;
385 }
386 }
387 }
388 if (callback != nullptr) {
389 callback->MakeVisible(self);
390 }
391 if (wait_count != 0) {
392 DCHECK(maybe_barrier.has_value());
393 maybe_barrier->Increment(self, wait_count);
394 }
395 }
396
VisiblyInitializedCallbackDone(Thread * self,VisiblyInitializedCallback * callback)397 void ClassLinker::VisiblyInitializedCallbackDone(Thread* self,
398 VisiblyInitializedCallback* callback) {
399 MutexLock lock(self, visibly_initialized_callback_lock_);
400 // Pass the barriers if requested.
401 for (Barrier* barrier : callback->GetAndClearBarriers()) {
402 barrier->Pass(self);
403 }
404 // Remove the callback from the list of running callbacks.
405 auto before = running_visibly_initialized_callbacks_.before_begin();
406 auto it = running_visibly_initialized_callbacks_.begin();
407 DCHECK(it != running_visibly_initialized_callbacks_.end());
408 while (std::addressof(*it) != callback) {
409 before = it;
410 ++it;
411 DCHECK(it != running_visibly_initialized_callbacks_.end());
412 }
413 running_visibly_initialized_callbacks_.erase_after(before);
414 // Reuse or destroy the callback object.
415 if (visibly_initialized_callback_ == nullptr) {
416 visibly_initialized_callback_.reset(callback);
417 } else {
418 delete callback;
419 }
420 }
421
ForceClassInitialized(Thread * self,Handle<mirror::Class> klass)422 void ClassLinker::ForceClassInitialized(Thread* self, Handle<mirror::Class> klass) {
423 ClassLinker::VisiblyInitializedCallback* cb = MarkClassInitialized(self, klass);
424 if (cb != nullptr) {
425 cb->MakeVisible(self);
426 }
427 ScopedThreadSuspension sts(self, ThreadState::kSuspended);
428 MakeInitializedClassesVisiblyInitialized(self, /*wait=*/true);
429 }
430
MarkClassInitialized(Thread * self,Handle<mirror::Class> klass)431 ClassLinker::VisiblyInitializedCallback* ClassLinker::MarkClassInitialized(
432 Thread* self, Handle<mirror::Class> klass) {
433 if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) {
434 // Thanks to the x86 memory model, we do not need any memory fences and
435 // we can immediately mark the class as visibly initialized.
436 mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self);
437 FixupStaticTrampolines(self, klass.Get());
438 return nullptr;
439 }
440 if (Runtime::Current()->IsActiveTransaction()) {
441 // Transactions are single-threaded, so we can mark the class as visibly intialized.
442 // (Otherwise we'd need to track the callback's entry in the transaction for rollback.)
443 mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self);
444 FixupStaticTrampolines(self, klass.Get());
445 return nullptr;
446 }
447 mirror::Class::SetStatus(klass, ClassStatus::kInitialized, self);
448 MutexLock lock(self, visibly_initialized_callback_lock_);
449 if (visibly_initialized_callback_ == nullptr) {
450 visibly_initialized_callback_.reset(new VisiblyInitializedCallback(this));
451 }
452 DCHECK(!visibly_initialized_callback_->IsFull());
453 visibly_initialized_callback_->AddClass(self, klass.Get());
454
455 if (visibly_initialized_callback_->IsFull()) {
456 VisiblyInitializedCallback* callback = visibly_initialized_callback_.release();
457 running_visibly_initialized_callbacks_.push_front(*callback);
458 return callback;
459 } else {
460 return nullptr;
461 }
462 }
463
RegisterNative(Thread * self,ArtMethod * method,const void * native_method)464 const void* ClassLinker::RegisterNative(
465 Thread* self, ArtMethod* method, const void* native_method) {
466 CHECK(method->IsNative()) << method->PrettyMethod();
467 CHECK(native_method != nullptr) << method->PrettyMethod();
468 void* new_native_method = nullptr;
469 Runtime* runtime = Runtime::Current();
470 runtime->GetRuntimeCallbacks()->RegisterNativeMethod(method,
471 native_method,
472 /*out*/&new_native_method);
473 if (method->IsCriticalNative()) {
474 MutexLock lock(self, critical_native_code_with_clinit_check_lock_);
475 // Remove old registered method if any.
476 auto it = critical_native_code_with_clinit_check_.find(method);
477 if (it != critical_native_code_with_clinit_check_.end()) {
478 critical_native_code_with_clinit_check_.erase(it);
479 }
480 // To ensure correct memory visibility, we need the class to be visibly
481 // initialized before we can set the JNI entrypoint.
482 if (method->GetDeclaringClass()->IsVisiblyInitialized()) {
483 method->SetEntryPointFromJni(new_native_method);
484 } else {
485 critical_native_code_with_clinit_check_.emplace(method, new_native_method);
486 }
487 } else {
488 method->SetEntryPointFromJni(new_native_method);
489 }
490 return new_native_method;
491 }
492
UnregisterNative(Thread * self,ArtMethod * method)493 void ClassLinker::UnregisterNative(Thread* self, ArtMethod* method) {
494 CHECK(method->IsNative()) << method->PrettyMethod();
495 // Restore stub to lookup native pointer via dlsym.
496 if (method->IsCriticalNative()) {
497 MutexLock lock(self, critical_native_code_with_clinit_check_lock_);
498 auto it = critical_native_code_with_clinit_check_.find(method);
499 if (it != critical_native_code_with_clinit_check_.end()) {
500 critical_native_code_with_clinit_check_.erase(it);
501 }
502 method->SetEntryPointFromJni(GetJniDlsymLookupCriticalStub());
503 } else {
504 method->SetEntryPointFromJni(GetJniDlsymLookupStub());
505 }
506 }
507
GetRegisteredNative(Thread * self,ArtMethod * method)508 const void* ClassLinker::GetRegisteredNative(Thread* self, ArtMethod* method) {
509 if (method->IsCriticalNative()) {
510 MutexLock lock(self, critical_native_code_with_clinit_check_lock_);
511 auto it = critical_native_code_with_clinit_check_.find(method);
512 if (it != critical_native_code_with_clinit_check_.end()) {
513 return it->second;
514 }
515 const void* native_code = method->GetEntryPointFromJni();
516 return IsJniDlsymLookupCriticalStub(native_code) ? nullptr : native_code;
517 } else {
518 const void* native_code = method->GetEntryPointFromJni();
519 return IsJniDlsymLookupStub(native_code) ? nullptr : native_code;
520 }
521 }
522
ThrowEarlierClassFailure(ObjPtr<mirror::Class> c,bool wrap_in_no_class_def,bool log)523 void ClassLinker::ThrowEarlierClassFailure(ObjPtr<mirror::Class> c,
524 bool wrap_in_no_class_def,
525 bool log) {
526 // The class failed to initialize on a previous attempt, so we want to throw
527 // a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we
528 // failed in verification, in which case v2 5.4.1 says we need to re-throw
529 // the previous error.
530 Runtime* const runtime = Runtime::Current();
531 if (!runtime->IsAotCompiler()) { // Give info if this occurs at runtime.
532 std::string extra;
533 ObjPtr<mirror::Object> verify_error = GetVerifyError(c);
534 if (verify_error != nullptr) {
535 if (verify_error->IsClass()) {
536 extra = mirror::Class::PrettyDescriptor(verify_error->AsClass());
537 } else {
538 extra = verify_error->AsThrowable()->Dump();
539 }
540 }
541 if (log) {
542 LOG(INFO) << "Rejecting re-init on previously-failed class " << c->PrettyClass()
543 << ": " << extra;
544 }
545 }
546
547 CHECK(c->IsErroneous()) << c->PrettyClass() << " " << c->GetStatus();
548 Thread* self = Thread::Current();
549 if (runtime->IsAotCompiler()) {
550 // At compile time, accurate errors and NCDFE are disabled to speed compilation.
551 ObjPtr<mirror::Throwable> pre_allocated = runtime->GetPreAllocatedNoClassDefFoundError();
552 self->SetException(pre_allocated);
553 } else {
554 ObjPtr<mirror::Object> verify_error = GetVerifyError(c);
555 if (verify_error != nullptr) {
556 // Rethrow stored error.
557 HandleEarlierVerifyError(self, this, c);
558 }
559 // TODO This might be wrong if we hit an OOME while allocating the ClassExt. In that case we
560 // might have meant to go down the earlier if statement with the original error but it got
561 // swallowed by the OOM so we end up here.
562 if (verify_error == nullptr || wrap_in_no_class_def) {
563 // If there isn't a recorded earlier error, or this is a repeat throw from initialization,
564 // the top-level exception must be a NoClassDefFoundError. The potentially already pending
565 // exception will be a cause.
566 self->ThrowNewWrappedException("Ljava/lang/NoClassDefFoundError;",
567 c->PrettyDescriptor().c_str());
568 }
569 }
570 }
571
VlogClassInitializationFailure(Handle<mirror::Class> klass)572 static void VlogClassInitializationFailure(Handle<mirror::Class> klass)
573 REQUIRES_SHARED(Locks::mutator_lock_) {
574 if (VLOG_IS_ON(class_linker)) {
575 std::string temp;
576 LOG(INFO) << "Failed to initialize class " << klass->GetDescriptor(&temp) << " from "
577 << klass->GetLocation() << "\n" << Thread::Current()->GetException()->Dump();
578 }
579 }
580
WrapExceptionInInitializer(Handle<mirror::Class> klass)581 static void WrapExceptionInInitializer(Handle<mirror::Class> klass)
582 REQUIRES_SHARED(Locks::mutator_lock_) {
583 Thread* self = Thread::Current();
584 JNIEnv* env = self->GetJniEnv();
585
586 ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred());
587 CHECK(cause.get() != nullptr);
588
589 // Boot classpath classes should not fail initialization. This is a consistency debug check.
590 // This cannot in general be guaranteed, but in all likelihood leads to breakage down the line.
591 if (klass->GetClassLoader() == nullptr && !Runtime::Current()->IsAotCompiler()) {
592 std::string tmp;
593 // We want to LOG(FATAL) on debug builds since this really shouldn't be happening but we need to
594 // make sure to only do it if we don't have AsyncExceptions being thrown around since those
595 // could have caused the error.
596 bool known_impossible = kIsDebugBuild && !Runtime::Current()->AreAsyncExceptionsThrown();
597 LOG(known_impossible ? FATAL : WARNING) << klass->GetDescriptor(&tmp)
598 << " failed initialization: "
599 << self->GetException()->Dump();
600 }
601
602 env->ExceptionClear();
603 bool is_error = env->IsInstanceOf(cause.get(), WellKnownClasses::java_lang_Error);
604 env->Throw(cause.get());
605
606 // We only wrap non-Error exceptions; an Error can just be used as-is.
607 if (!is_error) {
608 self->ThrowNewWrappedException("Ljava/lang/ExceptionInInitializerError;", nullptr);
609 }
610 VlogClassInitializationFailure(klass);
611 }
612
ClassLinker(InternTable * intern_table,bool fast_class_not_found_exceptions)613 ClassLinker::ClassLinker(InternTable* intern_table, bool fast_class_not_found_exceptions)
614 : boot_class_table_(new ClassTable()),
615 failed_dex_cache_class_lookups_(0),
616 class_roots_(nullptr),
617 find_array_class_cache_next_victim_(0),
618 init_done_(false),
619 log_new_roots_(false),
620 intern_table_(intern_table),
621 fast_class_not_found_exceptions_(fast_class_not_found_exceptions),
622 jni_dlsym_lookup_trampoline_(nullptr),
623 jni_dlsym_lookup_critical_trampoline_(nullptr),
624 quick_resolution_trampoline_(nullptr),
625 quick_imt_conflict_trampoline_(nullptr),
626 quick_generic_jni_trampoline_(nullptr),
627 quick_to_interpreter_bridge_trampoline_(nullptr),
628 nterp_trampoline_(nullptr),
629 image_pointer_size_(kRuntimePointerSize),
630 visibly_initialized_callback_lock_("visibly initialized callback lock"),
631 visibly_initialized_callback_(nullptr),
632 critical_native_code_with_clinit_check_lock_("critical native code with clinit check lock"),
633 critical_native_code_with_clinit_check_(),
634 cha_(Runtime::Current()->IsAotCompiler() ? nullptr : new ClassHierarchyAnalysis()) {
635 // For CHA disabled during Aot, see b/34193647.
636
637 CHECK(intern_table_ != nullptr);
638 static_assert(kFindArrayCacheSize == arraysize(find_array_class_cache_),
639 "Array cache size wrong.");
640 std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr));
641 }
642
CheckSystemClass(Thread * self,Handle<mirror::Class> c1,const char * descriptor)643 void ClassLinker::CheckSystemClass(Thread* self, Handle<mirror::Class> c1, const char* descriptor) {
644 ObjPtr<mirror::Class> c2 = FindSystemClass(self, descriptor);
645 if (c2 == nullptr) {
646 LOG(FATAL) << "Could not find class " << descriptor;
647 UNREACHABLE();
648 }
649 if (c1.Get() != c2) {
650 std::ostringstream os1, os2;
651 c1->DumpClass(os1, mirror::Class::kDumpClassFullDetail);
652 c2->DumpClass(os2, mirror::Class::kDumpClassFullDetail);
653 LOG(FATAL) << "InitWithoutImage: Class mismatch for " << descriptor
654 << ". This is most likely the result of a broken build. Make sure that "
655 << "libcore and art projects match.\n\n"
656 << os1.str() << "\n\n" << os2.str();
657 UNREACHABLE();
658 }
659 }
660
InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path,std::string * error_msg)661 bool ClassLinker::InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path,
662 std::string* error_msg) {
663 VLOG(startup) << "ClassLinker::Init";
664
665 Thread* const self = Thread::Current();
666 Runtime* const runtime = Runtime::Current();
667 gc::Heap* const heap = runtime->GetHeap();
668
669 CHECK(!heap->HasBootImageSpace()) << "Runtime has image. We should use it.";
670 CHECK(!init_done_);
671
672 // Use the pointer size from the runtime since we are probably creating the image.
673 image_pointer_size_ = InstructionSetPointerSize(runtime->GetInstructionSet());
674
675 // java_lang_Class comes first, it's needed for AllocClass
676 // The GC can't handle an object with a null class since we can't get the size of this object.
677 heap->IncrementDisableMovingGC(self);
678 StackHandleScope<64> hs(self); // 64 is picked arbitrarily.
679 auto class_class_size = mirror::Class::ClassClassSize(image_pointer_size_);
680 // Allocate the object as non-movable so that there are no cases where Object::IsClass returns
681 // the incorrect result when comparing to-space vs from-space.
682 Handle<mirror::Class> java_lang_Class(hs.NewHandle(ObjPtr<mirror::Class>::DownCast(
683 heap->AllocNonMovableObject(self, nullptr, class_class_size, VoidFunctor()))));
684 CHECK(java_lang_Class != nullptr);
685 java_lang_Class->SetClassFlags(mirror::kClassFlagClass);
686 java_lang_Class->SetClass(java_lang_Class.Get());
687 if (kUseBakerReadBarrier) {
688 java_lang_Class->AssertReadBarrierState();
689 }
690 java_lang_Class->SetClassSize(class_class_size);
691 java_lang_Class->SetPrimitiveType(Primitive::kPrimNot);
692 heap->DecrementDisableMovingGC(self);
693 // AllocClass(ObjPtr<mirror::Class>) can now be used
694
695 // Class[] is used for reflection support.
696 auto class_array_class_size = mirror::ObjectArray<mirror::Class>::ClassSize(image_pointer_size_);
697 Handle<mirror::Class> class_array_class(hs.NewHandle(
698 AllocClass(self, java_lang_Class.Get(), class_array_class_size)));
699 class_array_class->SetComponentType(java_lang_Class.Get());
700
701 // java_lang_Object comes next so that object_array_class can be created.
702 Handle<mirror::Class> java_lang_Object(hs.NewHandle(
703 AllocClass(self, java_lang_Class.Get(), mirror::Object::ClassSize(image_pointer_size_))));
704 CHECK(java_lang_Object != nullptr);
705 // backfill Object as the super class of Class.
706 java_lang_Class->SetSuperClass(java_lang_Object.Get());
707 mirror::Class::SetStatus(java_lang_Object, ClassStatus::kLoaded, self);
708
709 java_lang_Object->SetObjectSize(sizeof(mirror::Object));
710 // Allocate in non-movable so that it's possible to check if a JNI weak global ref has been
711 // cleared without triggering the read barrier and unintentionally mark the sentinel alive.
712 runtime->SetSentinel(heap->AllocNonMovableObject(self,
713 java_lang_Object.Get(),
714 java_lang_Object->GetObjectSize(),
715 VoidFunctor()));
716
717 // Initialize the SubtypeCheck bitstring for java.lang.Object and java.lang.Class.
718 if (kBitstringSubtypeCheckEnabled) {
719 // It might seem the lock here is unnecessary, however all the SubtypeCheck
720 // functions are annotated to require locks all the way down.
721 //
722 // We take the lock here to avoid using NO_THREAD_SAFETY_ANALYSIS.
723 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
724 SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(java_lang_Object.Get());
725 SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(java_lang_Class.Get());
726 }
727
728 // Object[] next to hold class roots.
729 Handle<mirror::Class> object_array_class(hs.NewHandle(
730 AllocClass(self, java_lang_Class.Get(),
731 mirror::ObjectArray<mirror::Object>::ClassSize(image_pointer_size_))));
732 object_array_class->SetComponentType(java_lang_Object.Get());
733
734 // Setup java.lang.String.
735 //
736 // We make this class non-movable for the unlikely case where it were to be
737 // moved by a sticky-bit (minor) collection when using the Generational
738 // Concurrent Copying (CC) collector, potentially creating a stale reference
739 // in the `klass_` field of one of its instances allocated in the Large-Object
740 // Space (LOS) -- see the comment about the dirty card scanning logic in
741 // art::gc::collector::ConcurrentCopying::MarkingPhase.
742 Handle<mirror::Class> java_lang_String(hs.NewHandle(
743 AllocClass</* kMovable= */ false>(
744 self, java_lang_Class.Get(), mirror::String::ClassSize(image_pointer_size_))));
745 java_lang_String->SetStringClass();
746 mirror::Class::SetStatus(java_lang_String, ClassStatus::kResolved, self);
747
748 // Setup java.lang.ref.Reference.
749 Handle<mirror::Class> java_lang_ref_Reference(hs.NewHandle(
750 AllocClass(self, java_lang_Class.Get(), mirror::Reference::ClassSize(image_pointer_size_))));
751 java_lang_ref_Reference->SetObjectSize(mirror::Reference::InstanceSize());
752 mirror::Class::SetStatus(java_lang_ref_Reference, ClassStatus::kResolved, self);
753
754 // Create storage for root classes, save away our work so far (requires descriptors).
755 class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>(
756 mirror::ObjectArray<mirror::Class>::Alloc(self,
757 object_array_class.Get(),
758 static_cast<int32_t>(ClassRoot::kMax)));
759 CHECK(!class_roots_.IsNull());
760 SetClassRoot(ClassRoot::kJavaLangClass, java_lang_Class.Get());
761 SetClassRoot(ClassRoot::kJavaLangObject, java_lang_Object.Get());
762 SetClassRoot(ClassRoot::kClassArrayClass, class_array_class.Get());
763 SetClassRoot(ClassRoot::kObjectArrayClass, object_array_class.Get());
764 SetClassRoot(ClassRoot::kJavaLangString, java_lang_String.Get());
765 SetClassRoot(ClassRoot::kJavaLangRefReference, java_lang_ref_Reference.Get());
766
767 // Fill in the empty iftable. Needs to be done after the kObjectArrayClass root is set.
768 java_lang_Object->SetIfTable(AllocIfTable(self, 0));
769
770 // Create array interface entries to populate once we can load system classes.
771 object_array_class->SetIfTable(AllocIfTable(self, 2));
772 DCHECK_EQ(GetArrayIfTable(), object_array_class->GetIfTable());
773
774 // Setup the primitive type classes.
775 CreatePrimitiveClass(self, Primitive::kPrimBoolean, ClassRoot::kPrimitiveBoolean);
776 CreatePrimitiveClass(self, Primitive::kPrimByte, ClassRoot::kPrimitiveByte);
777 CreatePrimitiveClass(self, Primitive::kPrimChar, ClassRoot::kPrimitiveChar);
778 CreatePrimitiveClass(self, Primitive::kPrimShort, ClassRoot::kPrimitiveShort);
779 CreatePrimitiveClass(self, Primitive::kPrimInt, ClassRoot::kPrimitiveInt);
780 CreatePrimitiveClass(self, Primitive::kPrimLong, ClassRoot::kPrimitiveLong);
781 CreatePrimitiveClass(self, Primitive::kPrimFloat, ClassRoot::kPrimitiveFloat);
782 CreatePrimitiveClass(self, Primitive::kPrimDouble, ClassRoot::kPrimitiveDouble);
783 CreatePrimitiveClass(self, Primitive::kPrimVoid, ClassRoot::kPrimitiveVoid);
784
785 // Allocate the primitive array classes. We need only the native pointer
786 // array at this point (int[] or long[], depending on architecture) but
787 // we shall perform the same setup steps for all primitive array classes.
788 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveBoolean, ClassRoot::kBooleanArrayClass);
789 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveByte, ClassRoot::kByteArrayClass);
790 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveChar, ClassRoot::kCharArrayClass);
791 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveShort, ClassRoot::kShortArrayClass);
792 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveInt, ClassRoot::kIntArrayClass);
793 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveLong, ClassRoot::kLongArrayClass);
794 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveFloat, ClassRoot::kFloatArrayClass);
795 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveDouble, ClassRoot::kDoubleArrayClass);
796
797 // now that these are registered, we can use AllocClass() and AllocObjectArray
798
799 // Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache.
800 Handle<mirror::Class> java_lang_DexCache(hs.NewHandle(
801 AllocClass(self, java_lang_Class.Get(), mirror::DexCache::ClassSize(image_pointer_size_))));
802 SetClassRoot(ClassRoot::kJavaLangDexCache, java_lang_DexCache.Get());
803 java_lang_DexCache->SetDexCacheClass();
804 java_lang_DexCache->SetObjectSize(mirror::DexCache::InstanceSize());
805 mirror::Class::SetStatus(java_lang_DexCache, ClassStatus::kResolved, self);
806
807
808 // Setup dalvik.system.ClassExt
809 Handle<mirror::Class> dalvik_system_ClassExt(hs.NewHandle(
810 AllocClass(self, java_lang_Class.Get(), mirror::ClassExt::ClassSize(image_pointer_size_))));
811 SetClassRoot(ClassRoot::kDalvikSystemClassExt, dalvik_system_ClassExt.Get());
812 mirror::Class::SetStatus(dalvik_system_ClassExt, ClassStatus::kResolved, self);
813
814 // Set up array classes for string, field, method
815 Handle<mirror::Class> object_array_string(hs.NewHandle(
816 AllocClass(self, java_lang_Class.Get(),
817 mirror::ObjectArray<mirror::String>::ClassSize(image_pointer_size_))));
818 object_array_string->SetComponentType(java_lang_String.Get());
819 SetClassRoot(ClassRoot::kJavaLangStringArrayClass, object_array_string.Get());
820
821 LinearAlloc* linear_alloc = runtime->GetLinearAlloc();
822 // Create runtime resolution and imt conflict methods.
823 runtime->SetResolutionMethod(runtime->CreateResolutionMethod());
824 runtime->SetImtConflictMethod(runtime->CreateImtConflictMethod(linear_alloc));
825 runtime->SetImtUnimplementedMethod(runtime->CreateImtConflictMethod(linear_alloc));
826
827 // Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create
828 // DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses
829 // these roots.
830 if (boot_class_path.empty()) {
831 *error_msg = "Boot classpath is empty.";
832 return false;
833 }
834 for (auto& dex_file : boot_class_path) {
835 if (dex_file == nullptr) {
836 *error_msg = "Null dex file.";
837 return false;
838 }
839 AppendToBootClassPath(self, dex_file.get());
840 boot_dex_files_.push_back(std::move(dex_file));
841 }
842
843 // now we can use FindSystemClass
844
845 // Set up GenericJNI entrypoint. That is mainly a hack for common_compiler_test.h so that
846 // we do not need friend classes or a publicly exposed setter.
847 quick_generic_jni_trampoline_ = GetQuickGenericJniStub();
848 if (!runtime->IsAotCompiler()) {
849 // We need to set up the generic trampolines since we don't have an image.
850 jni_dlsym_lookup_trampoline_ = GetJniDlsymLookupStub();
851 jni_dlsym_lookup_critical_trampoline_ = GetJniDlsymLookupCriticalStub();
852 quick_resolution_trampoline_ = GetQuickResolutionStub();
853 quick_imt_conflict_trampoline_ = GetQuickImtConflictStub();
854 quick_generic_jni_trampoline_ = GetQuickGenericJniStub();
855 quick_to_interpreter_bridge_trampoline_ = GetQuickToInterpreterBridge();
856 nterp_trampoline_ = interpreter::GetNterpEntryPoint();
857 }
858
859 // Object, String, ClassExt and DexCache need to be rerun through FindSystemClass to finish init
860 mirror::Class::SetStatus(java_lang_Object, ClassStatus::kNotReady, self);
861 CheckSystemClass(self, java_lang_Object, "Ljava/lang/Object;");
862 CHECK_EQ(java_lang_Object->GetObjectSize(), mirror::Object::InstanceSize());
863 mirror::Class::SetStatus(java_lang_String, ClassStatus::kNotReady, self);
864 CheckSystemClass(self, java_lang_String, "Ljava/lang/String;");
865 mirror::Class::SetStatus(java_lang_DexCache, ClassStatus::kNotReady, self);
866 CheckSystemClass(self, java_lang_DexCache, "Ljava/lang/DexCache;");
867 CHECK_EQ(java_lang_DexCache->GetObjectSize(), mirror::DexCache::InstanceSize());
868 mirror::Class::SetStatus(dalvik_system_ClassExt, ClassStatus::kNotReady, self);
869 CheckSystemClass(self, dalvik_system_ClassExt, "Ldalvik/system/ClassExt;");
870 CHECK_EQ(dalvik_system_ClassExt->GetObjectSize(), mirror::ClassExt::InstanceSize());
871
872 // Run Class through FindSystemClass. This initializes the dex_cache_ fields and register it
873 // in class_table_.
874 CheckSystemClass(self, java_lang_Class, "Ljava/lang/Class;");
875
876 // Setup core array classes, i.e. Object[], String[] and Class[] and primitive
877 // arrays - can't be done until Object has a vtable and component classes are loaded.
878 FinishCoreArrayClassSetup(ClassRoot::kObjectArrayClass);
879 FinishCoreArrayClassSetup(ClassRoot::kClassArrayClass);
880 FinishCoreArrayClassSetup(ClassRoot::kJavaLangStringArrayClass);
881 FinishCoreArrayClassSetup(ClassRoot::kBooleanArrayClass);
882 FinishCoreArrayClassSetup(ClassRoot::kByteArrayClass);
883 FinishCoreArrayClassSetup(ClassRoot::kCharArrayClass);
884 FinishCoreArrayClassSetup(ClassRoot::kShortArrayClass);
885 FinishCoreArrayClassSetup(ClassRoot::kIntArrayClass);
886 FinishCoreArrayClassSetup(ClassRoot::kLongArrayClass);
887 FinishCoreArrayClassSetup(ClassRoot::kFloatArrayClass);
888 FinishCoreArrayClassSetup(ClassRoot::kDoubleArrayClass);
889
890 // Setup the single, global copy of "iftable".
891 auto java_lang_Cloneable = hs.NewHandle(FindSystemClass(self, "Ljava/lang/Cloneable;"));
892 CHECK(java_lang_Cloneable != nullptr);
893 auto java_io_Serializable = hs.NewHandle(FindSystemClass(self, "Ljava/io/Serializable;"));
894 CHECK(java_io_Serializable != nullptr);
895 // We assume that Cloneable/Serializable don't have superinterfaces -- normally we'd have to
896 // crawl up and explicitly list all of the supers as well.
897 object_array_class->GetIfTable()->SetInterface(0, java_lang_Cloneable.Get());
898 object_array_class->GetIfTable()->SetInterface(1, java_io_Serializable.Get());
899
900 // Check Class[] and Object[]'s interfaces. GetDirectInterface may cause thread suspension.
901 CHECK_EQ(java_lang_Cloneable.Get(),
902 mirror::Class::GetDirectInterface(self, class_array_class.Get(), 0));
903 CHECK_EQ(java_io_Serializable.Get(),
904 mirror::Class::GetDirectInterface(self, class_array_class.Get(), 1));
905 CHECK_EQ(java_lang_Cloneable.Get(),
906 mirror::Class::GetDirectInterface(self, object_array_class.Get(), 0));
907 CHECK_EQ(java_io_Serializable.Get(),
908 mirror::Class::GetDirectInterface(self, object_array_class.Get(), 1));
909
910 CHECK_EQ(object_array_string.Get(),
911 FindSystemClass(self, GetClassRootDescriptor(ClassRoot::kJavaLangStringArrayClass)));
912
913 // End of special init trickery, all subsequent classes may be loaded via FindSystemClass.
914
915 // Create java.lang.reflect.Proxy root.
916 SetClassRoot(ClassRoot::kJavaLangReflectProxy,
917 FindSystemClass(self, "Ljava/lang/reflect/Proxy;"));
918
919 // Create java.lang.reflect.Field.class root.
920 ObjPtr<mirror::Class> class_root = FindSystemClass(self, "Ljava/lang/reflect/Field;");
921 CHECK(class_root != nullptr);
922 SetClassRoot(ClassRoot::kJavaLangReflectField, class_root);
923
924 // Create java.lang.reflect.Field array root.
925 class_root = FindSystemClass(self, "[Ljava/lang/reflect/Field;");
926 CHECK(class_root != nullptr);
927 SetClassRoot(ClassRoot::kJavaLangReflectFieldArrayClass, class_root);
928
929 // Create java.lang.reflect.Constructor.class root and array root.
930 class_root = FindSystemClass(self, "Ljava/lang/reflect/Constructor;");
931 CHECK(class_root != nullptr);
932 SetClassRoot(ClassRoot::kJavaLangReflectConstructor, class_root);
933 class_root = FindSystemClass(self, "[Ljava/lang/reflect/Constructor;");
934 CHECK(class_root != nullptr);
935 SetClassRoot(ClassRoot::kJavaLangReflectConstructorArrayClass, class_root);
936
937 // Create java.lang.reflect.Method.class root and array root.
938 class_root = FindSystemClass(self, "Ljava/lang/reflect/Method;");
939 CHECK(class_root != nullptr);
940 SetClassRoot(ClassRoot::kJavaLangReflectMethod, class_root);
941 class_root = FindSystemClass(self, "[Ljava/lang/reflect/Method;");
942 CHECK(class_root != nullptr);
943 SetClassRoot(ClassRoot::kJavaLangReflectMethodArrayClass, class_root);
944
945 // Create java.lang.invoke.CallSite.class root
946 class_root = FindSystemClass(self, "Ljava/lang/invoke/CallSite;");
947 CHECK(class_root != nullptr);
948 SetClassRoot(ClassRoot::kJavaLangInvokeCallSite, class_root);
949
950 // Create java.lang.invoke.MethodType.class root
951 class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodType;");
952 CHECK(class_root != nullptr);
953 SetClassRoot(ClassRoot::kJavaLangInvokeMethodType, class_root);
954
955 // Create java.lang.invoke.MethodHandleImpl.class root
956 class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandleImpl;");
957 CHECK(class_root != nullptr);
958 SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandleImpl, class_root);
959 SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandle, class_root->GetSuperClass());
960
961 // Create java.lang.invoke.MethodHandles.Lookup.class root
962 class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandles$Lookup;");
963 CHECK(class_root != nullptr);
964 SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandlesLookup, class_root);
965
966 // Create java.lang.invoke.VarHandle.class root
967 class_root = FindSystemClass(self, "Ljava/lang/invoke/VarHandle;");
968 CHECK(class_root != nullptr);
969 SetClassRoot(ClassRoot::kJavaLangInvokeVarHandle, class_root);
970
971 // Create java.lang.invoke.FieldVarHandle.class root
972 class_root = FindSystemClass(self, "Ljava/lang/invoke/FieldVarHandle;");
973 CHECK(class_root != nullptr);
974 SetClassRoot(ClassRoot::kJavaLangInvokeFieldVarHandle, class_root);
975
976 // Create java.lang.invoke.ArrayElementVarHandle.class root
977 class_root = FindSystemClass(self, "Ljava/lang/invoke/ArrayElementVarHandle;");
978 CHECK(class_root != nullptr);
979 SetClassRoot(ClassRoot::kJavaLangInvokeArrayElementVarHandle, class_root);
980
981 // Create java.lang.invoke.ByteArrayViewVarHandle.class root
982 class_root = FindSystemClass(self, "Ljava/lang/invoke/ByteArrayViewVarHandle;");
983 CHECK(class_root != nullptr);
984 SetClassRoot(ClassRoot::kJavaLangInvokeByteArrayViewVarHandle, class_root);
985
986 // Create java.lang.invoke.ByteBufferViewVarHandle.class root
987 class_root = FindSystemClass(self, "Ljava/lang/invoke/ByteBufferViewVarHandle;");
988 CHECK(class_root != nullptr);
989 SetClassRoot(ClassRoot::kJavaLangInvokeByteBufferViewVarHandle, class_root);
990
991 class_root = FindSystemClass(self, "Ldalvik/system/EmulatedStackFrame;");
992 CHECK(class_root != nullptr);
993 SetClassRoot(ClassRoot::kDalvikSystemEmulatedStackFrame, class_root);
994
995 // java.lang.ref classes need to be specially flagged, but otherwise are normal classes
996 // finish initializing Reference class
997 mirror::Class::SetStatus(java_lang_ref_Reference, ClassStatus::kNotReady, self);
998 CheckSystemClass(self, java_lang_ref_Reference, "Ljava/lang/ref/Reference;");
999 CHECK_EQ(java_lang_ref_Reference->GetObjectSize(), mirror::Reference::InstanceSize());
1000 CHECK_EQ(java_lang_ref_Reference->GetClassSize(),
1001 mirror::Reference::ClassSize(image_pointer_size_));
1002 class_root = FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;");
1003 CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal);
1004 class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagFinalizerReference);
1005 class_root = FindSystemClass(self, "Ljava/lang/ref/PhantomReference;");
1006 CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal);
1007 class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagPhantomReference);
1008 class_root = FindSystemClass(self, "Ljava/lang/ref/SoftReference;");
1009 CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal);
1010 class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagSoftReference);
1011 class_root = FindSystemClass(self, "Ljava/lang/ref/WeakReference;");
1012 CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal);
1013 class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagWeakReference);
1014
1015 // Setup the ClassLoader, verifying the object_size_.
1016 class_root = FindSystemClass(self, "Ljava/lang/ClassLoader;");
1017 class_root->SetClassLoaderClass();
1018 CHECK_EQ(class_root->GetObjectSize(), mirror::ClassLoader::InstanceSize());
1019 SetClassRoot(ClassRoot::kJavaLangClassLoader, class_root);
1020
1021 // Set up java.lang.Throwable, java.lang.ClassNotFoundException, and
1022 // java.lang.StackTraceElement as a convenience.
1023 SetClassRoot(ClassRoot::kJavaLangThrowable, FindSystemClass(self, "Ljava/lang/Throwable;"));
1024 SetClassRoot(ClassRoot::kJavaLangClassNotFoundException,
1025 FindSystemClass(self, "Ljava/lang/ClassNotFoundException;"));
1026 SetClassRoot(ClassRoot::kJavaLangStackTraceElement,
1027 FindSystemClass(self, "Ljava/lang/StackTraceElement;"));
1028 SetClassRoot(ClassRoot::kJavaLangStackTraceElementArrayClass,
1029 FindSystemClass(self, "[Ljava/lang/StackTraceElement;"));
1030 SetClassRoot(ClassRoot::kJavaLangClassLoaderArrayClass,
1031 FindSystemClass(self, "[Ljava/lang/ClassLoader;"));
1032
1033 // Create conflict tables that depend on the class linker.
1034 runtime->FixupConflictTables();
1035
1036 FinishInit(self);
1037
1038 VLOG(startup) << "ClassLinker::InitFromCompiler exiting";
1039
1040 return true;
1041 }
1042
CreateStringInitBindings(Thread * self,ClassLinker * class_linker)1043 static void CreateStringInitBindings(Thread* self, ClassLinker* class_linker)
1044 REQUIRES_SHARED(Locks::mutator_lock_) {
1045 // Find String.<init> -> StringFactory bindings.
1046 ObjPtr<mirror::Class> string_factory_class =
1047 class_linker->FindSystemClass(self, "Ljava/lang/StringFactory;");
1048 CHECK(string_factory_class != nullptr);
1049 ObjPtr<mirror::Class> string_class = GetClassRoot<mirror::String>(class_linker);
1050 WellKnownClasses::InitStringInit(string_class, string_factory_class);
1051 // Update the primordial thread.
1052 self->InitStringEntryPoints();
1053 }
1054
FinishInit(Thread * self)1055 void ClassLinker::FinishInit(Thread* self) {
1056 VLOG(startup) << "ClassLinker::FinishInit entering";
1057
1058 CreateStringInitBindings(self, this);
1059
1060 // Let the heap know some key offsets into java.lang.ref instances
1061 // Note: we hard code the field indexes here rather than using FindInstanceField
1062 // as the types of the field can't be resolved prior to the runtime being
1063 // fully initialized
1064 StackHandleScope<3> hs(self);
1065 Handle<mirror::Class> java_lang_ref_Reference =
1066 hs.NewHandle(GetClassRoot<mirror::Reference>(this));
1067 Handle<mirror::Class> java_lang_ref_FinalizerReference =
1068 hs.NewHandle(FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"));
1069
1070 ArtField* pendingNext = java_lang_ref_Reference->GetInstanceField(0);
1071 CHECK_STREQ(pendingNext->GetName(), "pendingNext");
1072 CHECK_STREQ(pendingNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;");
1073
1074 ArtField* queue = java_lang_ref_Reference->GetInstanceField(1);
1075 CHECK_STREQ(queue->GetName(), "queue");
1076 CHECK_STREQ(queue->GetTypeDescriptor(), "Ljava/lang/ref/ReferenceQueue;");
1077
1078 ArtField* queueNext = java_lang_ref_Reference->GetInstanceField(2);
1079 CHECK_STREQ(queueNext->GetName(), "queueNext");
1080 CHECK_STREQ(queueNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;");
1081
1082 ArtField* referent = java_lang_ref_Reference->GetInstanceField(3);
1083 CHECK_STREQ(referent->GetName(), "referent");
1084 CHECK_STREQ(referent->GetTypeDescriptor(), "Ljava/lang/Object;");
1085
1086 ArtField* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2);
1087 CHECK_STREQ(zombie->GetName(), "zombie");
1088 CHECK_STREQ(zombie->GetTypeDescriptor(), "Ljava/lang/Object;");
1089
1090 // ensure all class_roots_ are initialized
1091 for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); i++) {
1092 ClassRoot class_root = static_cast<ClassRoot>(i);
1093 ObjPtr<mirror::Class> klass = GetClassRoot(class_root);
1094 CHECK(klass != nullptr);
1095 DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != nullptr);
1096 // note SetClassRoot does additional validation.
1097 // if possible add new checks there to catch errors early
1098 }
1099
1100 CHECK(GetArrayIfTable() != nullptr);
1101
1102 // disable the slow paths in FindClass and CreatePrimitiveClass now
1103 // that Object, Class, and Object[] are setup
1104 init_done_ = true;
1105
1106 // Under sanitization, the small carve-out to handle stack overflow might not be enough to
1107 // initialize the StackOverflowError class (as it might require running the verifier). Instead,
1108 // ensure that the class will be initialized.
1109 if (kMemoryToolIsAvailable && !Runtime::Current()->IsAotCompiler()) {
1110 verifier::ClassVerifier::Init(this); // Need to prepare the verifier.
1111
1112 ObjPtr<mirror::Class> soe_klass = FindSystemClass(self, "Ljava/lang/StackOverflowError;");
1113 if (soe_klass == nullptr || !EnsureInitialized(self, hs.NewHandle(soe_klass), true, true)) {
1114 // Strange, but don't crash.
1115 LOG(WARNING) << "Could not prepare StackOverflowError.";
1116 self->ClearException();
1117 }
1118 }
1119
1120 VLOG(startup) << "ClassLinker::FinishInit exiting";
1121 }
1122
RunRootClinits(Thread * self)1123 void ClassLinker::RunRootClinits(Thread* self) {
1124 for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); ++i) {
1125 ObjPtr<mirror::Class> c = GetClassRoot(ClassRoot(i), this);
1126 if (!c->IsArrayClass() && !c->IsPrimitive()) {
1127 StackHandleScope<1> hs(self);
1128 Handle<mirror::Class> h_class(hs.NewHandle(c));
1129 if (!EnsureInitialized(self, h_class, true, true)) {
1130 LOG(FATAL) << "Exception when initializing " << h_class->PrettyClass()
1131 << ": " << self->GetException()->Dump();
1132 }
1133 } else {
1134 DCHECK(c->IsInitialized());
1135 }
1136 }
1137 }
1138
InitializeObjectVirtualMethodHashes(ObjPtr<mirror::Class> java_lang_Object,PointerSize pointer_size,ArrayRef<uint32_t> virtual_method_hashes)1139 static void InitializeObjectVirtualMethodHashes(ObjPtr<mirror::Class> java_lang_Object,
1140 PointerSize pointer_size,
1141 /*out*/ ArrayRef<uint32_t> virtual_method_hashes)
1142 REQUIRES_SHARED(Locks::mutator_lock_) {
1143 ArraySlice<ArtMethod> virtual_methods = java_lang_Object->GetVirtualMethods(pointer_size);
1144 DCHECK_EQ(virtual_method_hashes.size(), virtual_methods.size());
1145 for (size_t i = 0; i != virtual_method_hashes.size(); ++i) {
1146 const char* name = virtual_methods[i].GetName();
1147 virtual_method_hashes[i] = ComputeModifiedUtf8Hash(name);
1148 }
1149 }
1150
1151 struct TrampolineCheckData {
1152 const void* quick_resolution_trampoline;
1153 const void* quick_imt_conflict_trampoline;
1154 const void* quick_generic_jni_trampoline;
1155 const void* quick_to_interpreter_bridge_trampoline;
1156 const void* nterp_trampoline;
1157 PointerSize pointer_size;
1158 ArtMethod* m;
1159 bool error;
1160 };
1161
InitFromBootImage(std::string * error_msg)1162 bool ClassLinker::InitFromBootImage(std::string* error_msg) {
1163 VLOG(startup) << __FUNCTION__ << " entering";
1164 CHECK(!init_done_);
1165
1166 Runtime* const runtime = Runtime::Current();
1167 Thread* const self = Thread::Current();
1168 gc::Heap* const heap = runtime->GetHeap();
1169 std::vector<gc::space::ImageSpace*> spaces = heap->GetBootImageSpaces();
1170 CHECK(!spaces.empty());
1171 const ImageHeader& image_header = spaces[0]->GetImageHeader();
1172 uint32_t pointer_size_unchecked = image_header.GetPointerSizeUnchecked();
1173 if (!ValidPointerSize(pointer_size_unchecked)) {
1174 *error_msg = StringPrintf("Invalid image pointer size: %u", pointer_size_unchecked);
1175 return false;
1176 }
1177 image_pointer_size_ = image_header.GetPointerSize();
1178 if (!runtime->IsAotCompiler()) {
1179 // Only the Aot compiler supports having an image with a different pointer size than the
1180 // runtime. This happens on the host for compiling 32 bit tests since we use a 64 bit libart
1181 // compiler. We may also use 32 bit dex2oat on a system with 64 bit apps.
1182 if (image_pointer_size_ != kRuntimePointerSize) {
1183 *error_msg = StringPrintf("Runtime must use current image pointer size: %zu vs %zu",
1184 static_cast<size_t>(image_pointer_size_),
1185 sizeof(void*));
1186 return false;
1187 }
1188 }
1189 DCHECK(!runtime->HasResolutionMethod());
1190 runtime->SetResolutionMethod(image_header.GetImageMethod(ImageHeader::kResolutionMethod));
1191 runtime->SetImtConflictMethod(image_header.GetImageMethod(ImageHeader::kImtConflictMethod));
1192 runtime->SetImtUnimplementedMethod(
1193 image_header.GetImageMethod(ImageHeader::kImtUnimplementedMethod));
1194 runtime->SetCalleeSaveMethod(
1195 image_header.GetImageMethod(ImageHeader::kSaveAllCalleeSavesMethod),
1196 CalleeSaveType::kSaveAllCalleeSaves);
1197 runtime->SetCalleeSaveMethod(
1198 image_header.GetImageMethod(ImageHeader::kSaveRefsOnlyMethod),
1199 CalleeSaveType::kSaveRefsOnly);
1200 runtime->SetCalleeSaveMethod(
1201 image_header.GetImageMethod(ImageHeader::kSaveRefsAndArgsMethod),
1202 CalleeSaveType::kSaveRefsAndArgs);
1203 runtime->SetCalleeSaveMethod(
1204 image_header.GetImageMethod(ImageHeader::kSaveEverythingMethod),
1205 CalleeSaveType::kSaveEverything);
1206 runtime->SetCalleeSaveMethod(
1207 image_header.GetImageMethod(ImageHeader::kSaveEverythingMethodForClinit),
1208 CalleeSaveType::kSaveEverythingForClinit);
1209 runtime->SetCalleeSaveMethod(
1210 image_header.GetImageMethod(ImageHeader::kSaveEverythingMethodForSuspendCheck),
1211 CalleeSaveType::kSaveEverythingForSuspendCheck);
1212
1213 std::vector<const OatFile*> oat_files =
1214 runtime->GetOatFileManager().RegisterImageOatFiles(spaces);
1215 DCHECK(!oat_files.empty());
1216 const OatHeader& default_oat_header = oat_files[0]->GetOatHeader();
1217 jni_dlsym_lookup_trampoline_ = default_oat_header.GetJniDlsymLookupTrampoline();
1218 jni_dlsym_lookup_critical_trampoline_ = default_oat_header.GetJniDlsymLookupCriticalTrampoline();
1219 quick_resolution_trampoline_ = default_oat_header.GetQuickResolutionTrampoline();
1220 quick_imt_conflict_trampoline_ = default_oat_header.GetQuickImtConflictTrampoline();
1221 quick_generic_jni_trampoline_ = default_oat_header.GetQuickGenericJniTrampoline();
1222 quick_to_interpreter_bridge_trampoline_ = default_oat_header.GetQuickToInterpreterBridge();
1223 nterp_trampoline_ = default_oat_header.GetNterpTrampoline();
1224 if (kIsDebugBuild) {
1225 // Check that the other images use the same trampoline.
1226 for (size_t i = 1; i < oat_files.size(); ++i) {
1227 const OatHeader& ith_oat_header = oat_files[i]->GetOatHeader();
1228 const void* ith_jni_dlsym_lookup_trampoline_ =
1229 ith_oat_header.GetJniDlsymLookupTrampoline();
1230 const void* ith_jni_dlsym_lookup_critical_trampoline_ =
1231 ith_oat_header.GetJniDlsymLookupCriticalTrampoline();
1232 const void* ith_quick_resolution_trampoline =
1233 ith_oat_header.GetQuickResolutionTrampoline();
1234 const void* ith_quick_imt_conflict_trampoline =
1235 ith_oat_header.GetQuickImtConflictTrampoline();
1236 const void* ith_quick_generic_jni_trampoline =
1237 ith_oat_header.GetQuickGenericJniTrampoline();
1238 const void* ith_quick_to_interpreter_bridge_trampoline =
1239 ith_oat_header.GetQuickToInterpreterBridge();
1240 const void* ith_nterp_trampoline =
1241 ith_oat_header.GetNterpTrampoline();
1242 if (ith_jni_dlsym_lookup_trampoline_ != jni_dlsym_lookup_trampoline_ ||
1243 ith_jni_dlsym_lookup_critical_trampoline_ != jni_dlsym_lookup_critical_trampoline_ ||
1244 ith_quick_resolution_trampoline != quick_resolution_trampoline_ ||
1245 ith_quick_imt_conflict_trampoline != quick_imt_conflict_trampoline_ ||
1246 ith_quick_generic_jni_trampoline != quick_generic_jni_trampoline_ ||
1247 ith_quick_to_interpreter_bridge_trampoline != quick_to_interpreter_bridge_trampoline_ ||
1248 ith_nterp_trampoline != nterp_trampoline_) {
1249 // Make sure that all methods in this image do not contain those trampolines as
1250 // entrypoints. Otherwise the class-linker won't be able to work with a single set.
1251 TrampolineCheckData data;
1252 data.error = false;
1253 data.pointer_size = GetImagePointerSize();
1254 data.quick_resolution_trampoline = ith_quick_resolution_trampoline;
1255 data.quick_imt_conflict_trampoline = ith_quick_imt_conflict_trampoline;
1256 data.quick_generic_jni_trampoline = ith_quick_generic_jni_trampoline;
1257 data.quick_to_interpreter_bridge_trampoline = ith_quick_to_interpreter_bridge_trampoline;
1258 data.nterp_trampoline = ith_nterp_trampoline;
1259 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
1260 auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
1261 if (obj->IsClass()) {
1262 ObjPtr<mirror::Class> klass = obj->AsClass();
1263 for (ArtMethod& m : klass->GetMethods(data.pointer_size)) {
1264 const void* entrypoint =
1265 m.GetEntryPointFromQuickCompiledCodePtrSize(data.pointer_size);
1266 if (entrypoint == data.quick_resolution_trampoline ||
1267 entrypoint == data.quick_imt_conflict_trampoline ||
1268 entrypoint == data.quick_generic_jni_trampoline ||
1269 entrypoint == data.quick_to_interpreter_bridge_trampoline) {
1270 data.m = &m;
1271 data.error = true;
1272 return;
1273 }
1274 }
1275 }
1276 };
1277 spaces[i]->GetLiveBitmap()->Walk(visitor);
1278 if (data.error) {
1279 ArtMethod* m = data.m;
1280 LOG(ERROR) << "Found a broken ArtMethod: " << ArtMethod::PrettyMethod(m);
1281 *error_msg = "Found an ArtMethod with a bad entrypoint";
1282 return false;
1283 }
1284 }
1285 }
1286 }
1287
1288 class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>(
1289 ObjPtr<mirror::ObjectArray<mirror::Class>>::DownCast(
1290 image_header.GetImageRoot(ImageHeader::kClassRoots)));
1291 DCHECK_EQ(GetClassRoot<mirror::Class>(this)->GetClassFlags(), mirror::kClassFlagClass);
1292
1293 DCHECK_EQ(GetClassRoot<mirror::Object>(this)->GetObjectSize(), sizeof(mirror::Object));
1294 ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects =
1295 ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast(
1296 image_header.GetImageRoot(ImageHeader::kBootImageLiveObjects));
1297 runtime->SetSentinel(boot_image_live_objects->Get(ImageHeader::kClearedJniWeakSentinel));
1298 DCHECK(runtime->GetSentinel().Read()->GetClass() == GetClassRoot<mirror::Object>(this));
1299
1300 for (size_t i = 0u, size = spaces.size(); i != size; ++i) {
1301 // Boot class loader, use a null handle.
1302 std::vector<std::unique_ptr<const DexFile>> dex_files;
1303 if (!AddImageSpace(spaces[i],
1304 ScopedNullHandle<mirror::ClassLoader>(),
1305 /*out*/&dex_files,
1306 error_msg)) {
1307 return false;
1308 }
1309 // Append opened dex files at the end.
1310 boot_dex_files_.insert(boot_dex_files_.end(),
1311 std::make_move_iterator(dex_files.begin()),
1312 std::make_move_iterator(dex_files.end()));
1313 }
1314 for (const std::unique_ptr<const DexFile>& dex_file : boot_dex_files_) {
1315 OatDexFile::MadviseDexFile(*dex_file, MadviseState::kMadviseStateAtLoad);
1316 }
1317 InitializeObjectVirtualMethodHashes(GetClassRoot<mirror::Object>(this),
1318 image_pointer_size_,
1319 ArrayRef<uint32_t>(object_virtual_method_hashes_));
1320 FinishInit(self);
1321
1322 VLOG(startup) << __FUNCTION__ << " exiting";
1323 return true;
1324 }
1325
AddExtraBootDexFiles(Thread * self,std::vector<std::unique_ptr<const DexFile>> && additional_dex_files)1326 void ClassLinker::AddExtraBootDexFiles(
1327 Thread* self,
1328 std::vector<std::unique_ptr<const DexFile>>&& additional_dex_files) {
1329 for (std::unique_ptr<const DexFile>& dex_file : additional_dex_files) {
1330 AppendToBootClassPath(self, dex_file.get());
1331 if (kIsDebugBuild) {
1332 for (const auto& boot_dex_file : boot_dex_files_) {
1333 DCHECK_NE(boot_dex_file->GetLocation(), dex_file->GetLocation());
1334 }
1335 }
1336 boot_dex_files_.push_back(std::move(dex_file));
1337 }
1338 }
1339
IsBootClassLoader(ScopedObjectAccessAlreadyRunnable & soa,ObjPtr<mirror::ClassLoader> class_loader)1340 bool ClassLinker::IsBootClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
1341 ObjPtr<mirror::ClassLoader> class_loader) {
1342 return class_loader == nullptr ||
1343 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader) ==
1344 class_loader->GetClass();
1345 }
1346
1347 class CHAOnDeleteUpdateClassVisitor {
1348 public:
CHAOnDeleteUpdateClassVisitor(LinearAlloc * alloc)1349 explicit CHAOnDeleteUpdateClassVisitor(LinearAlloc* alloc)
1350 : allocator_(alloc), cha_(Runtime::Current()->GetClassLinker()->GetClassHierarchyAnalysis()),
1351 pointer_size_(Runtime::Current()->GetClassLinker()->GetImagePointerSize()),
1352 self_(Thread::Current()) {}
1353
operator ()(ObjPtr<mirror::Class> klass)1354 bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) {
1355 // This class is going to be unloaded. Tell CHA about it.
1356 cha_->ResetSingleImplementationInHierarchy(klass, allocator_, pointer_size_);
1357 return true;
1358 }
1359 private:
1360 const LinearAlloc* allocator_;
1361 const ClassHierarchyAnalysis* cha_;
1362 const PointerSize pointer_size_;
1363 const Thread* self_;
1364 };
1365
1366 /*
1367 * A class used to ensure that all references to strings interned in an AppImage have been
1368 * properly recorded in the interned references list, and is only ever run in debug mode.
1369 */
1370 class CountInternedStringReferencesVisitor {
1371 public:
CountInternedStringReferencesVisitor(const gc::space::ImageSpace & space,const InternTable::UnorderedSet & image_interns)1372 CountInternedStringReferencesVisitor(const gc::space::ImageSpace& space,
1373 const InternTable::UnorderedSet& image_interns)
1374 : space_(space),
1375 image_interns_(image_interns),
1376 count_(0u) {}
1377
TestObject(ObjPtr<mirror::Object> referred_obj) const1378 void TestObject(ObjPtr<mirror::Object> referred_obj) const
1379 REQUIRES_SHARED(Locks::mutator_lock_) {
1380 if (referred_obj != nullptr &&
1381 space_.HasAddress(referred_obj.Ptr()) &&
1382 referred_obj->IsString()) {
1383 ObjPtr<mirror::String> referred_str = referred_obj->AsString();
1384 auto it = image_interns_.find(GcRoot<mirror::String>(referred_str));
1385 if (it != image_interns_.end() && it->Read() == referred_str) {
1386 ++count_;
1387 }
1388 }
1389 }
1390
VisitRootIfNonNull(mirror::CompressedReference<mirror::Object> * root) const1391 void VisitRootIfNonNull(
1392 mirror::CompressedReference<mirror::Object>* root) const
1393 REQUIRES_SHARED(Locks::mutator_lock_) {
1394 if (!root->IsNull()) {
1395 VisitRoot(root);
1396 }
1397 }
1398
VisitRoot(mirror::CompressedReference<mirror::Object> * root) const1399 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
1400 REQUIRES_SHARED(Locks::mutator_lock_) {
1401 TestObject(root->AsMirrorPtr());
1402 }
1403
1404 // Visit Class Fields
operator ()(ObjPtr<mirror::Object> obj,MemberOffset offset,bool is_static ATTRIBUTE_UNUSED) const1405 void operator()(ObjPtr<mirror::Object> obj,
1406 MemberOffset offset,
1407 bool is_static ATTRIBUTE_UNUSED) const
1408 REQUIRES_SHARED(Locks::mutator_lock_) {
1409 // References within image or across images don't need a read barrier.
1410 ObjPtr<mirror::Object> referred_obj =
1411 obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(offset);
1412 TestObject(referred_obj);
1413 }
1414
operator ()(ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED,ObjPtr<mirror::Reference> ref) const1415 void operator()(ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED,
1416 ObjPtr<mirror::Reference> ref) const
1417 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) {
1418 operator()(ref, mirror::Reference::ReferentOffset(), /*is_static=*/ false);
1419 }
1420
GetCount() const1421 size_t GetCount() const {
1422 return count_;
1423 }
1424
1425 private:
1426 const gc::space::ImageSpace& space_;
1427 const InternTable::UnorderedSet& image_interns_;
1428 mutable size_t count_; // Modified from the `const` callbacks.
1429 };
1430
1431 /*
1432 * This function counts references to strings interned in the AppImage.
1433 * This is used in debug build to check against the number of the recorded references.
1434 */
CountInternedStringReferences(gc::space::ImageSpace & space,const InternTable::UnorderedSet & image_interns)1435 size_t CountInternedStringReferences(gc::space::ImageSpace& space,
1436 const InternTable::UnorderedSet& image_interns)
1437 REQUIRES_SHARED(Locks::mutator_lock_) {
1438 const gc::accounting::ContinuousSpaceBitmap* bitmap = space.GetMarkBitmap();
1439 const ImageHeader& image_header = space.GetImageHeader();
1440 const uint8_t* target_base = space.GetMemMap()->Begin();
1441 const ImageSection& objects_section = image_header.GetObjectsSection();
1442
1443 auto objects_begin = reinterpret_cast<uintptr_t>(target_base + objects_section.Offset());
1444 auto objects_end = reinterpret_cast<uintptr_t>(target_base + objects_section.End());
1445
1446 CountInternedStringReferencesVisitor visitor(space, image_interns);
1447 bitmap->VisitMarkedRange(objects_begin,
1448 objects_end,
1449 [&space, &visitor](mirror::Object* obj)
1450 REQUIRES_SHARED(Locks::mutator_lock_) {
1451 if (space.HasAddress(obj)) {
1452 if (obj->IsDexCache()) {
1453 obj->VisitReferences</* kVisitNativeRoots= */ true,
1454 kVerifyNone,
1455 kWithoutReadBarrier>(visitor, visitor);
1456 } else {
1457 // Don't visit native roots for non-dex-cache as they can't contain
1458 // native references to strings. This is verified during compilation
1459 // by ImageWriter::VerifyNativeGCRootInvariants.
1460 obj->VisitReferences</* kVisitNativeRoots= */ false,
1461 kVerifyNone,
1462 kWithoutReadBarrier>(visitor, visitor);
1463 }
1464 }
1465 });
1466 return visitor.GetCount();
1467 }
1468
1469 template <typename Visitor>
VisitInternedStringReferences(gc::space::ImageSpace * space,const Visitor & visitor)1470 static void VisitInternedStringReferences(
1471 gc::space::ImageSpace* space,
1472 const Visitor& visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
1473 const uint8_t* target_base = space->Begin();
1474 const ImageSection& sro_section =
1475 space->GetImageHeader().GetImageStringReferenceOffsetsSection();
1476 const size_t num_string_offsets = sro_section.Size() / sizeof(AppImageReferenceOffsetInfo);
1477
1478 VLOG(image)
1479 << "ClassLinker:AppImage:InternStrings:imageStringReferenceOffsetCount = "
1480 << num_string_offsets;
1481
1482 const auto* sro_base =
1483 reinterpret_cast<const AppImageReferenceOffsetInfo*>(target_base + sro_section.Offset());
1484
1485 for (size_t offset_index = 0; offset_index < num_string_offsets; ++offset_index) {
1486 uint32_t base_offset = sro_base[offset_index].first;
1487
1488 uint32_t raw_member_offset = sro_base[offset_index].second;
1489 DCHECK_ALIGNED(base_offset, 2);
1490 DCHECK_ALIGNED(raw_member_offset, 2);
1491
1492 ObjPtr<mirror::Object> obj_ptr =
1493 reinterpret_cast<mirror::Object*>(space->Begin() + base_offset);
1494 MemberOffset member_offset(raw_member_offset);
1495 ObjPtr<mirror::String> referred_string =
1496 obj_ptr->GetFieldObject<mirror::String,
1497 kVerifyNone,
1498 kWithoutReadBarrier,
1499 /* kIsVolatile= */ false>(member_offset);
1500 DCHECK(referred_string != nullptr);
1501
1502 ObjPtr<mirror::String> visited = visitor(referred_string);
1503 if (visited != referred_string) {
1504 obj_ptr->SetFieldObject</* kTransactionActive= */ false,
1505 /* kCheckTransaction= */ false,
1506 kVerifyNone,
1507 /* kIsVolatile= */ false>(member_offset, visited);
1508 }
1509 }
1510 }
1511
VerifyInternedStringReferences(gc::space::ImageSpace * space)1512 static void VerifyInternedStringReferences(gc::space::ImageSpace* space)
1513 REQUIRES_SHARED(Locks::mutator_lock_) {
1514 InternTable::UnorderedSet image_interns;
1515 const ImageSection& section = space->GetImageHeader().GetInternedStringsSection();
1516 if (section.Size() > 0) {
1517 size_t read_count;
1518 const uint8_t* data = space->Begin() + section.Offset();
1519 InternTable::UnorderedSet image_set(data, /*make_copy_of_data=*/ false, &read_count);
1520 image_set.swap(image_interns);
1521 }
1522 size_t num_recorded_refs = 0u;
1523 VisitInternedStringReferences(
1524 space,
1525 [&image_interns, &num_recorded_refs](ObjPtr<mirror::String> str)
1526 REQUIRES_SHARED(Locks::mutator_lock_) {
1527 auto it = image_interns.find(GcRoot<mirror::String>(str));
1528 CHECK(it != image_interns.end());
1529 CHECK(it->Read() == str);
1530 ++num_recorded_refs;
1531 return str;
1532 });
1533 size_t num_found_refs = CountInternedStringReferences(*space, image_interns);
1534 CHECK_EQ(num_recorded_refs, num_found_refs);
1535 }
1536
1537 // new_class_set is the set of classes that were read from the class table section in the image.
1538 // If there was no class table section, it is null.
1539 // Note: using a class here to avoid having to make ClassLinker internals public.
1540 class AppImageLoadingHelper {
1541 public:
1542 static void Update(
1543 ClassLinker* class_linker,
1544 gc::space::ImageSpace* space,
1545 Handle<mirror::ClassLoader> class_loader,
1546 Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches)
1547 REQUIRES(!Locks::dex_lock_)
1548 REQUIRES_SHARED(Locks::mutator_lock_);
1549
1550 static void HandleAppImageStrings(gc::space::ImageSpace* space)
1551 REQUIRES_SHARED(Locks::mutator_lock_);
1552 };
1553
Update(ClassLinker * class_linker,gc::space::ImageSpace * space,Handle<mirror::ClassLoader> class_loader,Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches)1554 void AppImageLoadingHelper::Update(
1555 ClassLinker* class_linker,
1556 gc::space::ImageSpace* space,
1557 Handle<mirror::ClassLoader> class_loader,
1558 Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches)
1559 REQUIRES(!Locks::dex_lock_)
1560 REQUIRES_SHARED(Locks::mutator_lock_) {
1561 ScopedTrace app_image_timing("AppImage:Updating");
1562
1563 if (kIsDebugBuild && ClassLinker::kAppImageMayContainStrings) {
1564 // In debug build, verify the string references before applying
1565 // the Runtime::LoadAppImageStartupCache() option.
1566 VerifyInternedStringReferences(space);
1567 }
1568
1569 Thread* const self = Thread::Current();
1570 Runtime* const runtime = Runtime::Current();
1571 gc::Heap* const heap = runtime->GetHeap();
1572 const ImageHeader& header = space->GetImageHeader();
1573 {
1574 // Register dex caches with the class loader.
1575 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
1576 for (auto dex_cache : dex_caches.Iterate<mirror::DexCache>()) {
1577 const DexFile* const dex_file = dex_cache->GetDexFile();
1578 {
1579 WriterMutexLock mu2(self, *Locks::dex_lock_);
1580 CHECK(class_linker->FindDexCacheDataLocked(*dex_file) == nullptr);
1581 class_linker->RegisterDexFileLocked(*dex_file, dex_cache, class_loader.Get());
1582 }
1583 }
1584 }
1585
1586 if (ClassLinker::kAppImageMayContainStrings) {
1587 HandleAppImageStrings(space);
1588 }
1589
1590 if (kVerifyArtMethodDeclaringClasses) {
1591 ScopedTrace timing("AppImage:VerifyDeclaringClasses");
1592 ReaderMutexLock rmu(self, *Locks::heap_bitmap_lock_);
1593 gc::accounting::HeapBitmap* live_bitmap = heap->GetLiveBitmap();
1594 header.VisitPackedArtMethods([&](ArtMethod& method)
1595 REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
1596 ObjPtr<mirror::Class> klass = method.GetDeclaringClassUnchecked();
1597 if (klass != nullptr) {
1598 CHECK(live_bitmap->Test(klass.Ptr())) << "Image method has unmarked declaring class";
1599 }
1600 }, space->Begin(), kRuntimePointerSize);
1601 }
1602 }
1603
HandleAppImageStrings(gc::space::ImageSpace * space)1604 void AppImageLoadingHelper::HandleAppImageStrings(gc::space::ImageSpace* space) {
1605 // Iterate over the string reference offsets stored in the image and intern
1606 // the strings they point to.
1607 ScopedTrace timing("AppImage:InternString");
1608
1609 Runtime* const runtime = Runtime::Current();
1610 InternTable* const intern_table = runtime->GetInternTable();
1611
1612 // Add the intern table, removing any conflicts. For conflicts, store the new address in a map
1613 // for faster lookup.
1614 // TODO: Optimize with a bitmap or bloom filter
1615 SafeMap<mirror::String*, mirror::String*> intern_remap;
1616 auto func = [&](InternTable::UnorderedSet& interns)
1617 REQUIRES_SHARED(Locks::mutator_lock_)
1618 REQUIRES(Locks::intern_table_lock_) {
1619 const size_t non_boot_image_strings = intern_table->CountInterns(
1620 /*visit_boot_images=*/false,
1621 /*visit_non_boot_images=*/true);
1622 VLOG(image) << "AppImage:stringsInInternTableSize = " << interns.size();
1623 VLOG(image) << "AppImage:nonBootImageInternStrings = " << non_boot_image_strings;
1624 // Visit the smaller of the two sets to compute the intersection.
1625 if (interns.size() < non_boot_image_strings) {
1626 for (auto it = interns.begin(); it != interns.end(); ) {
1627 ObjPtr<mirror::String> string = it->Read();
1628 ObjPtr<mirror::String> existing = intern_table->LookupWeakLocked(string);
1629 if (existing == nullptr) {
1630 existing = intern_table->LookupStrongLocked(string);
1631 }
1632 if (existing != nullptr) {
1633 intern_remap.Put(string.Ptr(), existing.Ptr());
1634 it = interns.erase(it);
1635 } else {
1636 ++it;
1637 }
1638 }
1639 } else {
1640 intern_table->VisitInterns([&](const GcRoot<mirror::String>& root)
1641 REQUIRES_SHARED(Locks::mutator_lock_)
1642 REQUIRES(Locks::intern_table_lock_) {
1643 auto it = interns.find(root);
1644 if (it != interns.end()) {
1645 ObjPtr<mirror::String> existing = root.Read();
1646 intern_remap.Put(it->Read(), existing.Ptr());
1647 it = interns.erase(it);
1648 }
1649 }, /*visit_boot_images=*/false, /*visit_non_boot_images=*/true);
1650 }
1651 // Consistency check to ensure correctness.
1652 if (kIsDebugBuild) {
1653 for (GcRoot<mirror::String>& root : interns) {
1654 ObjPtr<mirror::String> string = root.Read();
1655 CHECK(intern_table->LookupWeakLocked(string) == nullptr) << string->ToModifiedUtf8();
1656 CHECK(intern_table->LookupStrongLocked(string) == nullptr) << string->ToModifiedUtf8();
1657 }
1658 }
1659 };
1660 intern_table->AddImageStringsToTable(space, func);
1661 if (!intern_remap.empty()) {
1662 VLOG(image) << "AppImage:conflictingInternStrings = " << intern_remap.size();
1663 VisitInternedStringReferences(
1664 space,
1665 [&intern_remap](ObjPtr<mirror::String> str) REQUIRES_SHARED(Locks::mutator_lock_) {
1666 auto it = intern_remap.find(str.Ptr());
1667 if (it != intern_remap.end()) {
1668 return ObjPtr<mirror::String>(it->second);
1669 }
1670 return str;
1671 });
1672 }
1673 }
1674
OpenOatDexFile(const OatFile * oat_file,const char * location,std::string * error_msg)1675 static std::unique_ptr<const DexFile> OpenOatDexFile(const OatFile* oat_file,
1676 const char* location,
1677 std::string* error_msg)
1678 REQUIRES_SHARED(Locks::mutator_lock_) {
1679 DCHECK(error_msg != nullptr);
1680 std::unique_ptr<const DexFile> dex_file;
1681 const OatDexFile* oat_dex_file = oat_file->GetOatDexFile(location, nullptr, error_msg);
1682 if (oat_dex_file == nullptr) {
1683 return std::unique_ptr<const DexFile>();
1684 }
1685 std::string inner_error_msg;
1686 dex_file = oat_dex_file->OpenDexFile(&inner_error_msg);
1687 if (dex_file == nullptr) {
1688 *error_msg = StringPrintf("Failed to open dex file %s from within oat file %s error '%s'",
1689 location,
1690 oat_file->GetLocation().c_str(),
1691 inner_error_msg.c_str());
1692 return std::unique_ptr<const DexFile>();
1693 }
1694
1695 if (dex_file->GetLocationChecksum() != oat_dex_file->GetDexFileLocationChecksum()) {
1696 *error_msg = StringPrintf("Checksums do not match for %s: %x vs %x",
1697 location,
1698 dex_file->GetLocationChecksum(),
1699 oat_dex_file->GetDexFileLocationChecksum());
1700 return std::unique_ptr<const DexFile>();
1701 }
1702 return dex_file;
1703 }
1704
OpenImageDexFiles(gc::space::ImageSpace * space,std::vector<std::unique_ptr<const DexFile>> * out_dex_files,std::string * error_msg)1705 bool ClassLinker::OpenImageDexFiles(gc::space::ImageSpace* space,
1706 std::vector<std::unique_ptr<const DexFile>>* out_dex_files,
1707 std::string* error_msg) {
1708 ScopedAssertNoThreadSuspension nts(__FUNCTION__);
1709 const ImageHeader& header = space->GetImageHeader();
1710 ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches);
1711 DCHECK(dex_caches_object != nullptr);
1712 ObjPtr<mirror::ObjectArray<mirror::DexCache>> dex_caches =
1713 dex_caches_object->AsObjectArray<mirror::DexCache>();
1714 const OatFile* oat_file = space->GetOatFile();
1715 for (auto dex_cache : dex_caches->Iterate()) {
1716 std::string dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8());
1717 std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file,
1718 dex_file_location.c_str(),
1719 error_msg);
1720 if (dex_file == nullptr) {
1721 return false;
1722 }
1723 dex_cache->SetDexFile(dex_file.get());
1724 out_dex_files->push_back(std::move(dex_file));
1725 }
1726 return true;
1727 }
1728
1729 // Helper class for ArtMethod checks when adding an image. Keeps all required functionality
1730 // together and caches some intermediate results.
1731 class ImageChecker final {
1732 public:
CheckObjects(gc::Heap * heap,ClassLinker * class_linker)1733 static void CheckObjects(gc::Heap* heap, ClassLinker* class_linker)
1734 REQUIRES_SHARED(Locks::mutator_lock_) {
1735 ImageChecker ic(heap, class_linker);
1736 auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
1737 DCHECK(obj != nullptr);
1738 CHECK(obj->GetClass() != nullptr) << "Null class in object " << obj;
1739 CHECK(obj->GetClass()->GetClass() != nullptr) << "Null class class " << obj;
1740 if (obj->IsClass()) {
1741 auto klass = obj->AsClass();
1742 for (ArtField& field : klass->GetIFields()) {
1743 CHECK_EQ(field.GetDeclaringClass(), klass);
1744 }
1745 for (ArtField& field : klass->GetSFields()) {
1746 CHECK_EQ(field.GetDeclaringClass(), klass);
1747 }
1748 const PointerSize pointer_size = ic.pointer_size_;
1749 for (ArtMethod& m : klass->GetMethods(pointer_size)) {
1750 ic.CheckArtMethod(&m, klass);
1751 }
1752 ObjPtr<mirror::PointerArray> vtable = klass->GetVTable();
1753 if (vtable != nullptr) {
1754 ic.CheckArtMethodPointerArray(vtable, nullptr);
1755 }
1756 if (klass->ShouldHaveImt()) {
1757 ImTable* imt = klass->GetImt(pointer_size);
1758 for (size_t i = 0; i < ImTable::kSize; ++i) {
1759 ic.CheckArtMethod(imt->Get(i, pointer_size), nullptr);
1760 }
1761 }
1762 if (klass->ShouldHaveEmbeddedVTable()) {
1763 for (int32_t i = 0; i < klass->GetEmbeddedVTableLength(); ++i) {
1764 ic.CheckArtMethod(klass->GetEmbeddedVTableEntry(i, pointer_size), nullptr);
1765 }
1766 }
1767 ObjPtr<mirror::IfTable> iftable = klass->GetIfTable();
1768 for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) {
1769 if (iftable->GetMethodArrayCount(i) > 0) {
1770 ic.CheckArtMethodPointerArray(iftable->GetMethodArray(i), nullptr);
1771 }
1772 }
1773 }
1774 };
1775 heap->VisitObjects(visitor);
1776 }
1777
1778 private:
ImageChecker(gc::Heap * heap,ClassLinker * class_linker)1779 ImageChecker(gc::Heap* heap, ClassLinker* class_linker)
1780 : spaces_(heap->GetBootImageSpaces()),
1781 pointer_size_(class_linker->GetImagePointerSize()) {
1782 space_begin_.reserve(spaces_.size());
1783 method_sections_.reserve(spaces_.size());
1784 runtime_method_sections_.reserve(spaces_.size());
1785 for (gc::space::ImageSpace* space : spaces_) {
1786 space_begin_.push_back(space->Begin());
1787 auto& header = space->GetImageHeader();
1788 method_sections_.push_back(&header.GetMethodsSection());
1789 runtime_method_sections_.push_back(&header.GetRuntimeMethodsSection());
1790 }
1791 }
1792
CheckArtMethod(ArtMethod * m,ObjPtr<mirror::Class> expected_class)1793 void CheckArtMethod(ArtMethod* m, ObjPtr<mirror::Class> expected_class)
1794 REQUIRES_SHARED(Locks::mutator_lock_) {
1795 if (m->IsRuntimeMethod()) {
1796 ObjPtr<mirror::Class> declaring_class = m->GetDeclaringClassUnchecked();
1797 CHECK(declaring_class == nullptr) << declaring_class << " " << m->PrettyMethod();
1798 } else if (m->IsCopied()) {
1799 CHECK(m->GetDeclaringClass() != nullptr) << m->PrettyMethod();
1800 } else if (expected_class != nullptr) {
1801 CHECK_EQ(m->GetDeclaringClassUnchecked(), expected_class) << m->PrettyMethod();
1802 }
1803 if (!spaces_.empty()) {
1804 bool contains = false;
1805 for (size_t i = 0; !contains && i != space_begin_.size(); ++i) {
1806 const size_t offset = reinterpret_cast<uint8_t*>(m) - space_begin_[i];
1807 contains = method_sections_[i]->Contains(offset) ||
1808 runtime_method_sections_[i]->Contains(offset);
1809 }
1810 CHECK(contains) << m << " not found";
1811 }
1812 }
1813
CheckArtMethodPointerArray(ObjPtr<mirror::PointerArray> arr,ObjPtr<mirror::Class> expected_class)1814 void CheckArtMethodPointerArray(ObjPtr<mirror::PointerArray> arr,
1815 ObjPtr<mirror::Class> expected_class)
1816 REQUIRES_SHARED(Locks::mutator_lock_) {
1817 CHECK(arr != nullptr);
1818 for (int32_t j = 0; j < arr->GetLength(); ++j) {
1819 auto* method = arr->GetElementPtrSize<ArtMethod*>(j, pointer_size_);
1820 // expected_class == null means we are a dex cache.
1821 if (expected_class != nullptr) {
1822 CHECK(method != nullptr);
1823 }
1824 if (method != nullptr) {
1825 CheckArtMethod(method, expected_class);
1826 }
1827 }
1828 }
1829
1830 const std::vector<gc::space::ImageSpace*>& spaces_;
1831 const PointerSize pointer_size_;
1832
1833 // Cached sections from the spaces.
1834 std::vector<const uint8_t*> space_begin_;
1835 std::vector<const ImageSection*> method_sections_;
1836 std::vector<const ImageSection*> runtime_method_sections_;
1837 };
1838
VerifyAppImage(const ImageHeader & header,const Handle<mirror::ClassLoader> & class_loader,ClassTable * class_table,gc::space::ImageSpace * space)1839 static void VerifyAppImage(const ImageHeader& header,
1840 const Handle<mirror::ClassLoader>& class_loader,
1841 ClassTable* class_table,
1842 gc::space::ImageSpace* space)
1843 REQUIRES_SHARED(Locks::mutator_lock_) {
1844 header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) {
1845 ObjPtr<mirror::Class> klass = method.GetDeclaringClass();
1846 if (klass != nullptr && !Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) {
1847 CHECK_EQ(class_table->LookupByDescriptor(klass), klass)
1848 << mirror::Class::PrettyClass(klass);
1849 }
1850 }, space->Begin(), kRuntimePointerSize);
1851 {
1852 // Verify that all direct interfaces of classes in the class table are also resolved.
1853 std::vector<ObjPtr<mirror::Class>> classes;
1854 auto verify_direct_interfaces_in_table = [&](ObjPtr<mirror::Class> klass)
1855 REQUIRES_SHARED(Locks::mutator_lock_) {
1856 if (!klass->IsPrimitive() && klass->GetClassLoader() == class_loader.Get()) {
1857 classes.push_back(klass);
1858 }
1859 return true;
1860 };
1861 class_table->Visit(verify_direct_interfaces_in_table);
1862 Thread* self = Thread::Current();
1863 for (ObjPtr<mirror::Class> klass : classes) {
1864 for (uint32_t i = 0, num = klass->NumDirectInterfaces(); i != num; ++i) {
1865 CHECK(klass->GetDirectInterface(self, klass, i) != nullptr)
1866 << klass->PrettyDescriptor() << " iface #" << i;
1867 }
1868 }
1869 }
1870 }
1871
AddImageSpace(gc::space::ImageSpace * space,Handle<mirror::ClassLoader> class_loader,std::vector<std::unique_ptr<const DexFile>> * out_dex_files,std::string * error_msg)1872 bool ClassLinker::AddImageSpace(
1873 gc::space::ImageSpace* space,
1874 Handle<mirror::ClassLoader> class_loader,
1875 std::vector<std::unique_ptr<const DexFile>>* out_dex_files,
1876 std::string* error_msg) {
1877 DCHECK(out_dex_files != nullptr);
1878 DCHECK(error_msg != nullptr);
1879 const uint64_t start_time = NanoTime();
1880 const bool app_image = class_loader != nullptr;
1881 const ImageHeader& header = space->GetImageHeader();
1882 ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches);
1883 DCHECK(dex_caches_object != nullptr);
1884 Runtime* const runtime = Runtime::Current();
1885 gc::Heap* const heap = runtime->GetHeap();
1886 Thread* const self = Thread::Current();
1887 // Check that the image is what we are expecting.
1888 if (image_pointer_size_ != space->GetImageHeader().GetPointerSize()) {
1889 *error_msg = StringPrintf("Application image pointer size does not match runtime: %zu vs %zu",
1890 static_cast<size_t>(space->GetImageHeader().GetPointerSize()),
1891 image_pointer_size_);
1892 return false;
1893 }
1894 size_t expected_image_roots = ImageHeader::NumberOfImageRoots(app_image);
1895 if (static_cast<size_t>(header.GetImageRoots()->GetLength()) != expected_image_roots) {
1896 *error_msg = StringPrintf("Expected %zu image roots but got %d",
1897 expected_image_roots,
1898 header.GetImageRoots()->GetLength());
1899 return false;
1900 }
1901 StackHandleScope<3> hs(self);
1902 Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches(
1903 hs.NewHandle(dex_caches_object->AsObjectArray<mirror::DexCache>()));
1904 Handle<mirror::ObjectArray<mirror::Class>> class_roots(hs.NewHandle(
1905 header.GetImageRoot(ImageHeader::kClassRoots)->AsObjectArray<mirror::Class>()));
1906 MutableHandle<mirror::ClassLoader> image_class_loader(hs.NewHandle(
1907 app_image ? header.GetImageRoot(ImageHeader::kAppImageClassLoader)->AsClassLoader()
1908 : nullptr));
1909 DCHECK(class_roots != nullptr);
1910 if (class_roots->GetLength() != static_cast<int32_t>(ClassRoot::kMax)) {
1911 *error_msg = StringPrintf("Expected %d class roots but got %d",
1912 class_roots->GetLength(),
1913 static_cast<int32_t>(ClassRoot::kMax));
1914 return false;
1915 }
1916 // Check against existing class roots to make sure they match the ones in the boot image.
1917 ObjPtr<mirror::ObjectArray<mirror::Class>> existing_class_roots = GetClassRoots();
1918 for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); i++) {
1919 if (class_roots->Get(i) != GetClassRoot(static_cast<ClassRoot>(i), existing_class_roots)) {
1920 *error_msg = "App image class roots must have pointer equality with runtime ones.";
1921 return false;
1922 }
1923 }
1924 const OatFile* oat_file = space->GetOatFile();
1925 if (oat_file->GetOatHeader().GetDexFileCount() !=
1926 static_cast<uint32_t>(dex_caches->GetLength())) {
1927 *error_msg = "Dex cache count and dex file count mismatch while trying to initialize from "
1928 "image";
1929 return false;
1930 }
1931
1932 for (auto dex_cache : dex_caches.Iterate<mirror::DexCache>()) {
1933 std::string dex_file_location = dex_cache->GetLocation()->ToModifiedUtf8();
1934 std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file,
1935 dex_file_location.c_str(),
1936 error_msg);
1937 if (dex_file == nullptr) {
1938 return false;
1939 }
1940
1941 LinearAlloc* linear_alloc = GetOrCreateAllocatorForClassLoader(class_loader.Get());
1942 DCHECK(linear_alloc != nullptr);
1943 DCHECK_EQ(linear_alloc == Runtime::Current()->GetLinearAlloc(), !app_image);
1944 {
1945 // Native fields are all null. Initialize them and allocate native memory.
1946 WriterMutexLock mu(self, *Locks::dex_lock_);
1947 dex_cache->InitializeNativeFields(dex_file.get(), linear_alloc);
1948 }
1949 if (!app_image) {
1950 // Register dex files, keep track of existing ones that are conflicts.
1951 AppendToBootClassPath(dex_file.get(), dex_cache);
1952 }
1953 out_dex_files->push_back(std::move(dex_file));
1954 }
1955
1956 if (app_image) {
1957 ScopedObjectAccessUnchecked soa(Thread::Current());
1958 ScopedAssertNoThreadSuspension sants("Checking app image", soa.Self());
1959 if (IsBootClassLoader(soa, image_class_loader.Get())) {
1960 *error_msg = "Unexpected BootClassLoader in app image";
1961 return false;
1962 }
1963 }
1964
1965 if (kCheckImageObjects) {
1966 if (!app_image) {
1967 ImageChecker::CheckObjects(heap, this);
1968 }
1969 }
1970
1971 // Set entry point to interpreter if in InterpretOnly mode.
1972 if (!runtime->IsAotCompiler() && runtime->GetInstrumentation()->InterpretOnly()) {
1973 // Set image methods' entry point to interpreter.
1974 header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) {
1975 if (!method.IsRuntimeMethod()) {
1976 DCHECK(method.GetDeclaringClass() != nullptr);
1977 if (!method.IsNative() && !method.IsResolutionMethod()) {
1978 method.SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(),
1979 image_pointer_size_);
1980 }
1981 }
1982 }, space->Begin(), image_pointer_size_);
1983 }
1984
1985 if (!runtime->IsAotCompiler()) {
1986 ScopedTrace trace("AppImage:UpdateCodeItemAndNterp");
1987 bool can_use_nterp = interpreter::CanRuntimeUseNterp();
1988 header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) {
1989 // In the image, the `data` pointer field of the ArtMethod contains the code
1990 // item offset. Change this to the actual pointer to the code item.
1991 if (method.HasCodeItem()) {
1992 const dex::CodeItem* code_item = method.GetDexFile()->GetCodeItem(
1993 reinterpret_cast32<uint32_t>(method.GetDataPtrSize(image_pointer_size_)));
1994 method.SetCodeItem(code_item);
1995 }
1996 // Set image methods' entry point that point to the interpreter bridge to the
1997 // nterp entry point.
1998 if (method.GetEntryPointFromQuickCompiledCode() == nterp_trampoline_) {
1999 if (can_use_nterp) {
2000 DCHECK(!NeedsClinitCheckBeforeCall(&method) ||
2001 method.GetDeclaringClass()->IsVisiblyInitialized());
2002 method.SetEntryPointFromQuickCompiledCode(interpreter::GetNterpEntryPoint());
2003 } else {
2004 method.SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge());
2005 }
2006 }
2007 }, space->Begin(), image_pointer_size_);
2008 }
2009
2010 if (runtime->IsVerificationSoftFail()) {
2011 header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) {
2012 if (!method.IsNative() && method.IsInvokable()) {
2013 method.ClearSkipAccessChecks();
2014 }
2015 }, space->Begin(), image_pointer_size_);
2016 }
2017
2018 ClassTable* class_table = nullptr;
2019 {
2020 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
2021 class_table = InsertClassTableForClassLoader(class_loader.Get());
2022 }
2023 // If we have a class table section, read it and use it for verification in
2024 // UpdateAppImageClassLoadersAndDexCaches.
2025 ClassTable::ClassSet temp_set;
2026 const ImageSection& class_table_section = header.GetClassTableSection();
2027 const bool added_class_table = class_table_section.Size() > 0u;
2028 if (added_class_table) {
2029 const uint64_t start_time2 = NanoTime();
2030 size_t read_count = 0;
2031 temp_set = ClassTable::ClassSet(space->Begin() + class_table_section.Offset(),
2032 /*make copy*/false,
2033 &read_count);
2034 VLOG(image) << "Adding class table classes took " << PrettyDuration(NanoTime() - start_time2);
2035 }
2036 if (app_image) {
2037 AppImageLoadingHelper::Update(this, space, class_loader, dex_caches);
2038
2039 {
2040 ScopedTrace trace("AppImage:UpdateClassLoaders");
2041 // Update class loader and resolved strings. If added_class_table is false, the resolved
2042 // strings were forwarded UpdateAppImageClassLoadersAndDexCaches.
2043 ObjPtr<mirror::ClassLoader> loader(class_loader.Get());
2044 for (const ClassTable::TableSlot& root : temp_set) {
2045 // Note: We probably don't need the read barrier unless we copy the app image objects into
2046 // the region space.
2047 ObjPtr<mirror::Class> klass(root.Read());
2048 // Do not update class loader for boot image classes where the app image
2049 // class loader is only the initiating loader but not the defining loader.
2050 // Avoid read barrier since we are comparing against null.
2051 if (klass->GetClassLoader<kDefaultVerifyFlags, kWithoutReadBarrier>() != nullptr) {
2052 klass->SetClassLoader(loader);
2053 }
2054 }
2055 }
2056
2057 if (kBitstringSubtypeCheckEnabled) {
2058 // Every class in the app image has initially SubtypeCheckInfo in the
2059 // Uninitialized state.
2060 //
2061 // The SubtypeCheck invariants imply that a SubtypeCheckInfo is at least Initialized
2062 // after class initialization is complete. The app image ClassStatus as-is
2063 // are almost all ClassStatus::Initialized, and being in the
2064 // SubtypeCheckInfo::kUninitialized state is violating that invariant.
2065 //
2066 // Force every app image class's SubtypeCheck to be at least kIninitialized.
2067 //
2068 // See also ImageWriter::FixupClass.
2069 ScopedTrace trace("AppImage:RecacluateSubtypeCheckBitstrings");
2070 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
2071 for (const ClassTable::TableSlot& root : temp_set) {
2072 SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(root.Read());
2073 }
2074 }
2075 }
2076 if (!oat_file->GetBssGcRoots().empty()) {
2077 // Insert oat file to class table for visiting .bss GC roots.
2078 class_table->InsertOatFile(oat_file);
2079 }
2080
2081 if (added_class_table) {
2082 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
2083 class_table->AddClassSet(std::move(temp_set));
2084 }
2085
2086 if (kIsDebugBuild && app_image) {
2087 // This verification needs to happen after the classes have been added to the class loader.
2088 // Since it ensures classes are in the class table.
2089 ScopedTrace trace("AppImage:Verify");
2090 VerifyAppImage(header, class_loader, class_table, space);
2091 }
2092
2093 VLOG(class_linker) << "Adding image space took " << PrettyDuration(NanoTime() - start_time);
2094 return true;
2095 }
2096
VisitClassRoots(RootVisitor * visitor,VisitRootFlags flags)2097 void ClassLinker::VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags) {
2098 // Acquire tracing_enabled before locking class linker lock to prevent lock order violation. Since
2099 // enabling tracing requires the mutator lock, there are no race conditions here.
2100 const bool tracing_enabled = Trace::IsTracingEnabled();
2101 Thread* const self = Thread::Current();
2102 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
2103 if (kUseReadBarrier) {
2104 // We do not track new roots for CC.
2105 DCHECK_EQ(0, flags & (kVisitRootFlagNewRoots |
2106 kVisitRootFlagClearRootLog |
2107 kVisitRootFlagStartLoggingNewRoots |
2108 kVisitRootFlagStopLoggingNewRoots));
2109 }
2110 if ((flags & kVisitRootFlagAllRoots) != 0) {
2111 // Argument for how root visiting deals with ArtField and ArtMethod roots.
2112 // There is 3 GC cases to handle:
2113 // Non moving concurrent:
2114 // This case is easy to handle since the reference members of ArtMethod and ArtFields are held
2115 // live by the class and class roots.
2116 //
2117 // Moving non-concurrent:
2118 // This case needs to call visit VisitNativeRoots in case the classes or dex cache arrays move.
2119 // To prevent missing roots, this case needs to ensure that there is no
2120 // suspend points between the point which we allocate ArtMethod arrays and place them in a
2121 // class which is in the class table.
2122 //
2123 // Moving concurrent:
2124 // Need to make sure to not copy ArtMethods without doing read barriers since the roots are
2125 // marked concurrently and we don't hold the classlinker_classes_lock_ when we do the copy.
2126 //
2127 // Use an unbuffered visitor since the class table uses a temporary GcRoot for holding decoded
2128 // ClassTable::TableSlot. The buffered root visiting would access a stale stack location for
2129 // these objects.
2130 UnbufferedRootVisitor root_visitor(visitor, RootInfo(kRootStickyClass));
2131 boot_class_table_->VisitRoots(root_visitor);
2132 // If tracing is enabled, then mark all the class loaders to prevent unloading.
2133 if ((flags & kVisitRootFlagClassLoader) != 0 || tracing_enabled) {
2134 for (const ClassLoaderData& data : class_loaders_) {
2135 GcRoot<mirror::Object> root(GcRoot<mirror::Object>(self->DecodeJObject(data.weak_root)));
2136 root.VisitRoot(visitor, RootInfo(kRootVMInternal));
2137 }
2138 }
2139 } else if (!kUseReadBarrier && (flags & kVisitRootFlagNewRoots) != 0) {
2140 for (auto& root : new_class_roots_) {
2141 ObjPtr<mirror::Class> old_ref = root.Read<kWithoutReadBarrier>();
2142 root.VisitRoot(visitor, RootInfo(kRootStickyClass));
2143 ObjPtr<mirror::Class> new_ref = root.Read<kWithoutReadBarrier>();
2144 // Concurrent moving GC marked new roots through the to-space invariant.
2145 CHECK_EQ(new_ref, old_ref);
2146 }
2147 for (const OatFile* oat_file : new_bss_roots_boot_oat_files_) {
2148 for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) {
2149 ObjPtr<mirror::Object> old_ref = root.Read<kWithoutReadBarrier>();
2150 if (old_ref != nullptr) {
2151 DCHECK(old_ref->IsClass());
2152 root.VisitRoot(visitor, RootInfo(kRootStickyClass));
2153 ObjPtr<mirror::Object> new_ref = root.Read<kWithoutReadBarrier>();
2154 // Concurrent moving GC marked new roots through the to-space invariant.
2155 CHECK_EQ(new_ref, old_ref);
2156 }
2157 }
2158 }
2159 }
2160 if (!kUseReadBarrier && (flags & kVisitRootFlagClearRootLog) != 0) {
2161 new_class_roots_.clear();
2162 new_bss_roots_boot_oat_files_.clear();
2163 }
2164 if (!kUseReadBarrier && (flags & kVisitRootFlagStartLoggingNewRoots) != 0) {
2165 log_new_roots_ = true;
2166 } else if (!kUseReadBarrier && (flags & kVisitRootFlagStopLoggingNewRoots) != 0) {
2167 log_new_roots_ = false;
2168 }
2169 // We deliberately ignore the class roots in the image since we
2170 // handle image roots by using the MS/CMS rescanning of dirty cards.
2171 }
2172
2173 // Keep in sync with InitCallback. Anything we visit, we need to
2174 // reinit references to when reinitializing a ClassLinker from a
2175 // mapped image.
VisitRoots(RootVisitor * visitor,VisitRootFlags flags)2176 void ClassLinker::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) {
2177 class_roots_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal));
2178 VisitClassRoots(visitor, flags);
2179 // Instead of visiting the find_array_class_cache_ drop it so that it doesn't prevent class
2180 // unloading if we are marking roots.
2181 DropFindArrayClassCache();
2182 }
2183
2184 class VisitClassLoaderClassesVisitor : public ClassLoaderVisitor {
2185 public:
VisitClassLoaderClassesVisitor(ClassVisitor * visitor)2186 explicit VisitClassLoaderClassesVisitor(ClassVisitor* visitor)
2187 : visitor_(visitor),
2188 done_(false) {}
2189
Visit(ObjPtr<mirror::ClassLoader> class_loader)2190 void Visit(ObjPtr<mirror::ClassLoader> class_loader)
2191 REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override {
2192 ClassTable* const class_table = class_loader->GetClassTable();
2193 if (!done_ && class_table != nullptr) {
2194 DefiningClassLoaderFilterVisitor visitor(class_loader, visitor_);
2195 if (!class_table->Visit(visitor)) {
2196 // If the visitor ClassTable returns false it means that we don't need to continue.
2197 done_ = true;
2198 }
2199 }
2200 }
2201
2202 private:
2203 // Class visitor that limits the class visits from a ClassTable to the classes with
2204 // the provided defining class loader. This filter is used to avoid multiple visits
2205 // of the same class which can be recorded for multiple initiating class loaders.
2206 class DefiningClassLoaderFilterVisitor : public ClassVisitor {
2207 public:
DefiningClassLoaderFilterVisitor(ObjPtr<mirror::ClassLoader> defining_class_loader,ClassVisitor * visitor)2208 DefiningClassLoaderFilterVisitor(ObjPtr<mirror::ClassLoader> defining_class_loader,
2209 ClassVisitor* visitor)
2210 : defining_class_loader_(defining_class_loader), visitor_(visitor) { }
2211
operator ()(ObjPtr<mirror::Class> klass)2212 bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) {
2213 if (klass->GetClassLoader() != defining_class_loader_) {
2214 return true;
2215 }
2216 return (*visitor_)(klass);
2217 }
2218
2219 const ObjPtr<mirror::ClassLoader> defining_class_loader_;
2220 ClassVisitor* const visitor_;
2221 };
2222
2223 ClassVisitor* const visitor_;
2224 // If done is true then we don't need to do any more visiting.
2225 bool done_;
2226 };
2227
VisitClassesInternal(ClassVisitor * visitor)2228 void ClassLinker::VisitClassesInternal(ClassVisitor* visitor) {
2229 if (boot_class_table_->Visit(*visitor)) {
2230 VisitClassLoaderClassesVisitor loader_visitor(visitor);
2231 VisitClassLoaders(&loader_visitor);
2232 }
2233 }
2234
VisitClasses(ClassVisitor * visitor)2235 void ClassLinker::VisitClasses(ClassVisitor* visitor) {
2236 Thread* const self = Thread::Current();
2237 ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_);
2238 // Not safe to have thread suspension when we are holding a lock.
2239 if (self != nullptr) {
2240 ScopedAssertNoThreadSuspension nts(__FUNCTION__);
2241 VisitClassesInternal(visitor);
2242 } else {
2243 VisitClassesInternal(visitor);
2244 }
2245 }
2246
2247 class GetClassesInToVector : public ClassVisitor {
2248 public:
operator ()(ObjPtr<mirror::Class> klass)2249 bool operator()(ObjPtr<mirror::Class> klass) override {
2250 classes_.push_back(klass);
2251 return true;
2252 }
2253 std::vector<ObjPtr<mirror::Class>> classes_;
2254 };
2255
2256 class GetClassInToObjectArray : public ClassVisitor {
2257 public:
GetClassInToObjectArray(mirror::ObjectArray<mirror::Class> * arr)2258 explicit GetClassInToObjectArray(mirror::ObjectArray<mirror::Class>* arr)
2259 : arr_(arr), index_(0) {}
2260
operator ()(ObjPtr<mirror::Class> klass)2261 bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) {
2262 ++index_;
2263 if (index_ <= arr_->GetLength()) {
2264 arr_->Set(index_ - 1, klass);
2265 return true;
2266 }
2267 return false;
2268 }
2269
Succeeded() const2270 bool Succeeded() const REQUIRES_SHARED(Locks::mutator_lock_) {
2271 return index_ <= arr_->GetLength();
2272 }
2273
2274 private:
2275 mirror::ObjectArray<mirror::Class>* const arr_;
2276 int32_t index_;
2277 };
2278
VisitClassesWithoutClassesLock(ClassVisitor * visitor)2279 void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor) {
2280 // TODO: it may be possible to avoid secondary storage if we iterate over dex caches. The problem
2281 // is avoiding duplicates.
2282 if (!kMovingClasses) {
2283 ScopedAssertNoThreadSuspension nts(__FUNCTION__);
2284 GetClassesInToVector accumulator;
2285 VisitClasses(&accumulator);
2286 for (ObjPtr<mirror::Class> klass : accumulator.classes_) {
2287 if (!visitor->operator()(klass)) {
2288 return;
2289 }
2290 }
2291 } else {
2292 Thread* const self = Thread::Current();
2293 StackHandleScope<1> hs(self);
2294 auto classes = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr);
2295 // We size the array assuming classes won't be added to the class table during the visit.
2296 // If this assumption fails we iterate again.
2297 while (true) {
2298 size_t class_table_size;
2299 {
2300 ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_);
2301 // Add 100 in case new classes get loaded when we are filling in the object array.
2302 class_table_size = NumZygoteClasses() + NumNonZygoteClasses() + 100;
2303 }
2304 ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this);
2305 classes.Assign(
2306 mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, class_table_size));
2307 CHECK(classes != nullptr); // OOME.
2308 GetClassInToObjectArray accumulator(classes.Get());
2309 VisitClasses(&accumulator);
2310 if (accumulator.Succeeded()) {
2311 break;
2312 }
2313 }
2314 for (int32_t i = 0; i < classes->GetLength(); ++i) {
2315 // If the class table shrank during creation of the clases array we expect null elements. If
2316 // the class table grew then the loop repeats. If classes are created after the loop has
2317 // finished then we don't visit.
2318 ObjPtr<mirror::Class> klass = classes->Get(i);
2319 if (klass != nullptr && !visitor->operator()(klass)) {
2320 return;
2321 }
2322 }
2323 }
2324 }
2325
~ClassLinker()2326 ClassLinker::~ClassLinker() {
2327 Thread* const self = Thread::Current();
2328 for (const ClassLoaderData& data : class_loaders_) {
2329 // CHA unloading analysis is not needed. No negative consequences are expected because
2330 // all the classloaders are deleted at the same time.
2331 DeleteClassLoader(self, data, /*cleanup_cha=*/ false);
2332 }
2333 class_loaders_.clear();
2334 while (!running_visibly_initialized_callbacks_.empty()) {
2335 std::unique_ptr<VisiblyInitializedCallback> callback(
2336 std::addressof(running_visibly_initialized_callbacks_.front()));
2337 running_visibly_initialized_callbacks_.pop_front();
2338 }
2339 }
2340
DeleteClassLoader(Thread * self,const ClassLoaderData & data,bool cleanup_cha)2341 void ClassLinker::DeleteClassLoader(Thread* self, const ClassLoaderData& data, bool cleanup_cha) {
2342 Runtime* const runtime = Runtime::Current();
2343 JavaVMExt* const vm = runtime->GetJavaVM();
2344 vm->DeleteWeakGlobalRef(self, data.weak_root);
2345 // Notify the JIT that we need to remove the methods and/or profiling info.
2346 if (runtime->GetJit() != nullptr) {
2347 jit::JitCodeCache* code_cache = runtime->GetJit()->GetCodeCache();
2348 if (code_cache != nullptr) {
2349 // For the JIT case, RemoveMethodsIn removes the CHA dependencies.
2350 code_cache->RemoveMethodsIn(self, *data.allocator);
2351 }
2352 } else if (cha_ != nullptr) {
2353 // If we don't have a JIT, we need to manually remove the CHA dependencies manually.
2354 cha_->RemoveDependenciesForLinearAlloc(data.allocator);
2355 }
2356 // Cleanup references to single implementation ArtMethods that will be deleted.
2357 if (cleanup_cha) {
2358 CHAOnDeleteUpdateClassVisitor visitor(data.allocator);
2359 data.class_table->Visit<CHAOnDeleteUpdateClassVisitor, kWithoutReadBarrier>(visitor);
2360 }
2361 {
2362 MutexLock lock(self, critical_native_code_with_clinit_check_lock_);
2363 auto end = critical_native_code_with_clinit_check_.end();
2364 for (auto it = critical_native_code_with_clinit_check_.begin(); it != end; ) {
2365 if (data.allocator->ContainsUnsafe(it->first)) {
2366 it = critical_native_code_with_clinit_check_.erase(it);
2367 } else {
2368 ++it;
2369 }
2370 }
2371 }
2372
2373 delete data.allocator;
2374 delete data.class_table;
2375 }
2376
AllocPointerArray(Thread * self,size_t length)2377 ObjPtr<mirror::PointerArray> ClassLinker::AllocPointerArray(Thread* self, size_t length) {
2378 return ObjPtr<mirror::PointerArray>::DownCast(
2379 image_pointer_size_ == PointerSize::k64
2380 ? ObjPtr<mirror::Array>(mirror::LongArray::Alloc(self, length))
2381 : ObjPtr<mirror::Array>(mirror::IntArray::Alloc(self, length)));
2382 }
2383
AllocDexCache(Thread * self,const DexFile & dex_file)2384 ObjPtr<mirror::DexCache> ClassLinker::AllocDexCache(Thread* self, const DexFile& dex_file) {
2385 StackHandleScope<1> hs(self);
2386 auto dex_cache(hs.NewHandle(ObjPtr<mirror::DexCache>::DownCast(
2387 GetClassRoot<mirror::DexCache>(this)->AllocObject(self))));
2388 if (dex_cache == nullptr) {
2389 self->AssertPendingOOMException();
2390 return nullptr;
2391 }
2392 // Use InternWeak() so that the location String can be collected when the ClassLoader
2393 // with this DexCache is collected.
2394 ObjPtr<mirror::String> location = intern_table_->InternWeak(dex_file.GetLocation().c_str());
2395 if (location == nullptr) {
2396 self->AssertPendingOOMException();
2397 return nullptr;
2398 }
2399 dex_cache->SetLocation(location);
2400 return dex_cache.Get();
2401 }
2402
AllocAndInitializeDexCache(Thread * self,const DexFile & dex_file,LinearAlloc * linear_alloc)2403 ObjPtr<mirror::DexCache> ClassLinker::AllocAndInitializeDexCache(Thread* self,
2404 const DexFile& dex_file,
2405 LinearAlloc* linear_alloc) {
2406 ObjPtr<mirror::DexCache> dex_cache = AllocDexCache(self, dex_file);
2407 if (dex_cache != nullptr) {
2408 WriterMutexLock mu(self, *Locks::dex_lock_);
2409 dex_cache->InitializeNativeFields(&dex_file, linear_alloc);
2410 }
2411 return dex_cache;
2412 }
2413
2414 template <bool kMovable, typename PreFenceVisitor>
AllocClass(Thread * self,ObjPtr<mirror::Class> java_lang_Class,uint32_t class_size,const PreFenceVisitor & pre_fence_visitor)2415 ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self,
2416 ObjPtr<mirror::Class> java_lang_Class,
2417 uint32_t class_size,
2418 const PreFenceVisitor& pre_fence_visitor) {
2419 DCHECK_GE(class_size, sizeof(mirror::Class));
2420 gc::Heap* heap = Runtime::Current()->GetHeap();
2421 ObjPtr<mirror::Object> k = (kMovingClasses && kMovable) ?
2422 heap->AllocObject(self, java_lang_Class, class_size, pre_fence_visitor) :
2423 heap->AllocNonMovableObject(self, java_lang_Class, class_size, pre_fence_visitor);
2424 if (UNLIKELY(k == nullptr)) {
2425 self->AssertPendingOOMException();
2426 return nullptr;
2427 }
2428 return k->AsClass();
2429 }
2430
2431 template <bool kMovable>
AllocClass(Thread * self,ObjPtr<mirror::Class> java_lang_Class,uint32_t class_size)2432 ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self,
2433 ObjPtr<mirror::Class> java_lang_Class,
2434 uint32_t class_size) {
2435 mirror::Class::InitializeClassVisitor visitor(class_size);
2436 return AllocClass<kMovable>(self, java_lang_Class, class_size, visitor);
2437 }
2438
AllocClass(Thread * self,uint32_t class_size)2439 ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self, uint32_t class_size) {
2440 return AllocClass(self, GetClassRoot<mirror::Class>(this), class_size);
2441 }
2442
AllocPrimitiveArrayClass(Thread * self,ClassRoot primitive_root,ClassRoot array_root)2443 void ClassLinker::AllocPrimitiveArrayClass(Thread* self,
2444 ClassRoot primitive_root,
2445 ClassRoot array_root) {
2446 // We make this class non-movable for the unlikely case where it were to be
2447 // moved by a sticky-bit (minor) collection when using the Generational
2448 // Concurrent Copying (CC) collector, potentially creating a stale reference
2449 // in the `klass_` field of one of its instances allocated in the Large-Object
2450 // Space (LOS) -- see the comment about the dirty card scanning logic in
2451 // art::gc::collector::ConcurrentCopying::MarkingPhase.
2452 ObjPtr<mirror::Class> array_class = AllocClass</* kMovable= */ false>(
2453 self, GetClassRoot<mirror::Class>(this), mirror::Array::ClassSize(image_pointer_size_));
2454 ObjPtr<mirror::Class> component_type = GetClassRoot(primitive_root, this);
2455 DCHECK(component_type->IsPrimitive());
2456 array_class->SetComponentType(component_type);
2457 SetClassRoot(array_root, array_class);
2458 }
2459
FinishArrayClassSetup(ObjPtr<mirror::Class> array_class)2460 void ClassLinker::FinishArrayClassSetup(ObjPtr<mirror::Class> array_class) {
2461 ObjPtr<mirror::Class> java_lang_Object = GetClassRoot<mirror::Object>(this);
2462 array_class->SetSuperClass(java_lang_Object);
2463 array_class->SetVTable(java_lang_Object->GetVTable());
2464 array_class->SetPrimitiveType(Primitive::kPrimNot);
2465 ObjPtr<mirror::Class> component_type = array_class->GetComponentType();
2466 array_class->SetClassFlags(component_type->IsPrimitive()
2467 ? mirror::kClassFlagNoReferenceFields
2468 : mirror::kClassFlagObjectArray);
2469 array_class->SetClassLoader(component_type->GetClassLoader());
2470 array_class->SetStatusForPrimitiveOrArray(ClassStatus::kLoaded);
2471 array_class->PopulateEmbeddedVTable(image_pointer_size_);
2472 ImTable* object_imt = java_lang_Object->GetImt(image_pointer_size_);
2473 array_class->SetImt(object_imt, image_pointer_size_);
2474 // Skip EnsureSkipAccessChecksMethods(). We can skip the verified status,
2475 // the kAccVerificationAttempted flag is added below, and there are no
2476 // methods that need the kAccSkipAccessChecks flag.
2477 DCHECK_EQ(array_class->NumMethods(), 0u);
2478
2479 // don't need to set new_class->SetObjectSize(..)
2480 // because Object::SizeOf delegates to Array::SizeOf
2481
2482 // All arrays have java/lang/Cloneable and java/io/Serializable as
2483 // interfaces. We need to set that up here, so that stuff like
2484 // "instanceof" works right.
2485
2486 // Use the single, global copies of "interfaces" and "iftable"
2487 // (remember not to free them for arrays).
2488 {
2489 ObjPtr<mirror::IfTable> array_iftable = GetArrayIfTable();
2490 CHECK(array_iftable != nullptr);
2491 array_class->SetIfTable(array_iftable);
2492 }
2493
2494 // Inherit access flags from the component type.
2495 int access_flags = component_type->GetAccessFlags();
2496 // Lose any implementation detail flags; in particular, arrays aren't finalizable.
2497 access_flags &= kAccJavaFlagsMask;
2498 // Arrays can't be used as a superclass or interface, so we want to add "abstract final"
2499 // and remove "interface".
2500 access_flags |= kAccAbstract | kAccFinal;
2501 access_flags &= ~kAccInterface;
2502 // Arrays are access-checks-clean and preverified.
2503 access_flags |= kAccVerificationAttempted;
2504
2505 array_class->SetAccessFlagsDuringLinking(access_flags);
2506
2507 // Array classes are fully initialized either during single threaded startup,
2508 // or from a pre-fence visitor, so visibly initialized.
2509 array_class->SetStatusForPrimitiveOrArray(ClassStatus::kVisiblyInitialized);
2510 }
2511
FinishCoreArrayClassSetup(ClassRoot array_root)2512 void ClassLinker::FinishCoreArrayClassSetup(ClassRoot array_root) {
2513 // Do not hold lock on the array class object, the initialization of
2514 // core array classes is done while the process is still single threaded.
2515 ObjPtr<mirror::Class> array_class = GetClassRoot(array_root, this);
2516 FinishArrayClassSetup(array_class);
2517
2518 std::string temp;
2519 const char* descriptor = array_class->GetDescriptor(&temp);
2520 size_t hash = ComputeModifiedUtf8Hash(descriptor);
2521 ObjPtr<mirror::Class> existing = InsertClass(descriptor, array_class, hash);
2522 CHECK(existing == nullptr);
2523 }
2524
AllocStackTraceElementArray(Thread * self,size_t length)2525 ObjPtr<mirror::ObjectArray<mirror::StackTraceElement>> ClassLinker::AllocStackTraceElementArray(
2526 Thread* self,
2527 size_t length) {
2528 return mirror::ObjectArray<mirror::StackTraceElement>::Alloc(
2529 self, GetClassRoot<mirror::ObjectArray<mirror::StackTraceElement>>(this), length);
2530 }
2531
EnsureResolved(Thread * self,const char * descriptor,ObjPtr<mirror::Class> klass)2532 ObjPtr<mirror::Class> ClassLinker::EnsureResolved(Thread* self,
2533 const char* descriptor,
2534 ObjPtr<mirror::Class> klass) {
2535 DCHECK(klass != nullptr);
2536 if (kIsDebugBuild) {
2537 StackHandleScope<1> hs(self);
2538 HandleWrapperObjPtr<mirror::Class> h = hs.NewHandleWrapper(&klass);
2539 Thread::PoisonObjectPointersIfDebug();
2540 }
2541
2542 // For temporary classes we must wait for them to be retired.
2543 if (init_done_ && klass->IsTemp()) {
2544 CHECK(!klass->IsResolved());
2545 if (klass->IsErroneousUnresolved()) {
2546 ThrowEarlierClassFailure(klass);
2547 return nullptr;
2548 }
2549 StackHandleScope<1> hs(self);
2550 Handle<mirror::Class> h_class(hs.NewHandle(klass));
2551 ObjectLock<mirror::Class> lock(self, h_class);
2552 // Loop and wait for the resolving thread to retire this class.
2553 while (!h_class->IsRetired() && !h_class->IsErroneousUnresolved()) {
2554 lock.WaitIgnoringInterrupts();
2555 }
2556 if (h_class->IsErroneousUnresolved()) {
2557 ThrowEarlierClassFailure(h_class.Get());
2558 return nullptr;
2559 }
2560 CHECK(h_class->IsRetired());
2561 // Get the updated class from class table.
2562 klass = LookupClass(self, descriptor, h_class.Get()->GetClassLoader());
2563 }
2564
2565 // Wait for the class if it has not already been linked.
2566 size_t index = 0;
2567 // Maximum number of yield iterations until we start sleeping.
2568 static const size_t kNumYieldIterations = 1000;
2569 // How long each sleep is in us.
2570 static const size_t kSleepDurationUS = 1000; // 1 ms.
2571 while (!klass->IsResolved() && !klass->IsErroneousUnresolved()) {
2572 StackHandleScope<1> hs(self);
2573 HandleWrapperObjPtr<mirror::Class> h_class(hs.NewHandleWrapper(&klass));
2574 {
2575 ObjectTryLock<mirror::Class> lock(self, h_class);
2576 // Can not use a monitor wait here since it may block when returning and deadlock if another
2577 // thread has locked klass.
2578 if (lock.Acquired()) {
2579 // Check for circular dependencies between classes, the lock is required for SetStatus.
2580 if (!h_class->IsResolved() && h_class->GetClinitThreadId() == self->GetTid()) {
2581 ThrowClassCircularityError(h_class.Get());
2582 mirror::Class::SetStatus(h_class, ClassStatus::kErrorUnresolved, self);
2583 return nullptr;
2584 }
2585 }
2586 }
2587 {
2588 // Handle wrapper deals with klass moving.
2589 ScopedThreadSuspension sts(self, kSuspended);
2590 if (index < kNumYieldIterations) {
2591 sched_yield();
2592 } else {
2593 usleep(kSleepDurationUS);
2594 }
2595 }
2596 ++index;
2597 }
2598
2599 if (klass->IsErroneousUnresolved()) {
2600 ThrowEarlierClassFailure(klass);
2601 return nullptr;
2602 }
2603 // Return the loaded class. No exceptions should be pending.
2604 CHECK(klass->IsResolved()) << klass->PrettyClass();
2605 self->AssertNoPendingException();
2606 return klass;
2607 }
2608
2609 using ClassPathEntry = std::pair<const DexFile*, const dex::ClassDef*>;
2610
2611 // Search a collection of DexFiles for a descriptor
FindInClassPath(const char * descriptor,size_t hash,const std::vector<const DexFile * > & class_path)2612 ClassPathEntry FindInClassPath(const char* descriptor,
2613 size_t hash, const std::vector<const DexFile*>& class_path) {
2614 for (const DexFile* dex_file : class_path) {
2615 DCHECK(dex_file != nullptr);
2616 const dex::ClassDef* dex_class_def = OatDexFile::FindClassDef(*dex_file, descriptor, hash);
2617 if (dex_class_def != nullptr) {
2618 return ClassPathEntry(dex_file, dex_class_def);
2619 }
2620 }
2621 return ClassPathEntry(nullptr, nullptr);
2622 }
2623
2624 // Helper macro to make sure each class loader lookup call handles the case the
2625 // class loader is not recognized, or the lookup threw an exception.
2626 #define RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION(call_, result_, thread_) \
2627 do { \
2628 auto local_call = call_; \
2629 if (!local_call) { \
2630 return false; \
2631 } \
2632 auto local_result = result_; \
2633 if (local_result != nullptr) { \
2634 return true; \
2635 } \
2636 auto local_thread = thread_; \
2637 if (local_thread->IsExceptionPending()) { \
2638 /* Pending exception means there was an error other than */ \
2639 /* ClassNotFound that must be returned to the caller. */ \
2640 return false; \
2641 } \
2642 } while (0)
2643
FindClassInSharedLibraries(ScopedObjectAccessAlreadyRunnable & soa,Thread * self,const char * descriptor,size_t hash,Handle<mirror::ClassLoader> class_loader,ObjPtr<mirror::Class> * result)2644 bool ClassLinker::FindClassInSharedLibraries(ScopedObjectAccessAlreadyRunnable& soa,
2645 Thread* self,
2646 const char* descriptor,
2647 size_t hash,
2648 Handle<mirror::ClassLoader> class_loader,
2649 /*out*/ ObjPtr<mirror::Class>* result) {
2650 ArtField* field =
2651 jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders);
2652 ObjPtr<mirror::Object> raw_shared_libraries = field->GetObject(class_loader.Get());
2653 if (raw_shared_libraries == nullptr) {
2654 return true;
2655 }
2656
2657 StackHandleScope<2> hs(self);
2658 Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries(
2659 hs.NewHandle(raw_shared_libraries->AsObjectArray<mirror::ClassLoader>()));
2660 MutableHandle<mirror::ClassLoader> temp_loader = hs.NewHandle<mirror::ClassLoader>(nullptr);
2661 for (auto loader : shared_libraries.Iterate<mirror::ClassLoader>()) {
2662 temp_loader.Assign(loader);
2663 RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION(
2664 FindClassInBaseDexClassLoader(soa, self, descriptor, hash, temp_loader, result),
2665 *result,
2666 self);
2667 }
2668 return true;
2669 }
2670
FindClassInBaseDexClassLoader(ScopedObjectAccessAlreadyRunnable & soa,Thread * self,const char * descriptor,size_t hash,Handle<mirror::ClassLoader> class_loader,ObjPtr<mirror::Class> * result)2671 bool ClassLinker::FindClassInBaseDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
2672 Thread* self,
2673 const char* descriptor,
2674 size_t hash,
2675 Handle<mirror::ClassLoader> class_loader,
2676 /*out*/ ObjPtr<mirror::Class>* result) {
2677 // Termination case: boot class loader.
2678 if (IsBootClassLoader(soa, class_loader.Get())) {
2679 RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION(
2680 FindClassInBootClassLoaderClassPath(self, descriptor, hash, result), *result, self);
2681 return true;
2682 }
2683
2684 if (IsPathOrDexClassLoader(soa, class_loader) || IsInMemoryDexClassLoader(soa, class_loader)) {
2685 // For regular path or dex class loader the search order is:
2686 // - parent
2687 // - shared libraries
2688 // - class loader dex files
2689
2690 // Create a handle as RegisterDexFile may allocate dex caches (and cause thread suspension).
2691 StackHandleScope<1> hs(self);
2692 Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent()));
2693 RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION(
2694 FindClassInBaseDexClassLoader(soa, self, descriptor, hash, h_parent, result),
2695 *result,
2696 self);
2697 RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION(
2698 FindClassInSharedLibraries(soa, self, descriptor, hash, class_loader, result),
2699 *result,
2700 self);
2701 RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION(
2702 FindClassInBaseDexClassLoaderClassPath(soa, descriptor, hash, class_loader, result),
2703 *result,
2704 self);
2705 // We did not find a class, but the class loader chain was recognized, so we
2706 // return true.
2707 return true;
2708 }
2709
2710 if (IsDelegateLastClassLoader(soa, class_loader)) {
2711 // For delegate last, the search order is:
2712 // - boot class path
2713 // - shared libraries
2714 // - class loader dex files
2715 // - parent
2716 RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION(
2717 FindClassInBootClassLoaderClassPath(self, descriptor, hash, result), *result, self);
2718 RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION(
2719 FindClassInSharedLibraries(soa, self, descriptor, hash, class_loader, result),
2720 *result,
2721 self);
2722 RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION(
2723 FindClassInBaseDexClassLoaderClassPath(soa, descriptor, hash, class_loader, result),
2724 *result,
2725 self);
2726
2727 // Create a handle as RegisterDexFile may allocate dex caches (and cause thread suspension).
2728 StackHandleScope<1> hs(self);
2729 Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent()));
2730 RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION(
2731 FindClassInBaseDexClassLoader(soa, self, descriptor, hash, h_parent, result),
2732 *result,
2733 self);
2734 // We did not find a class, but the class loader chain was recognized, so we
2735 // return true.
2736 return true;
2737 }
2738
2739 // Unsupported class loader.
2740 *result = nullptr;
2741 return false;
2742 }
2743
2744 #undef RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION
2745
2746 namespace {
2747
2748 // Matches exceptions caught in DexFile.defineClass.
MatchesDexFileCaughtExceptions(ObjPtr<mirror::Throwable> throwable,ClassLinker * class_linker)2749 ALWAYS_INLINE bool MatchesDexFileCaughtExceptions(ObjPtr<mirror::Throwable> throwable,
2750 ClassLinker* class_linker)
2751 REQUIRES_SHARED(Locks::mutator_lock_) {
2752 return
2753 // ClassNotFoundException.
2754 throwable->InstanceOf(GetClassRoot(ClassRoot::kJavaLangClassNotFoundException,
2755 class_linker))
2756 ||
2757 // NoClassDefFoundError. TODO: Reconsider this. b/130746382.
2758 throwable->InstanceOf(Runtime::Current()->GetPreAllocatedNoClassDefFoundError()->GetClass());
2759 }
2760
2761 // Clear exceptions caught in DexFile.defineClass.
FilterDexFileCaughtExceptions(Thread * self,ClassLinker * class_linker)2762 ALWAYS_INLINE void FilterDexFileCaughtExceptions(Thread* self, ClassLinker* class_linker)
2763 REQUIRES_SHARED(Locks::mutator_lock_) {
2764 if (MatchesDexFileCaughtExceptions(self->GetException(), class_linker)) {
2765 self->ClearException();
2766 }
2767 }
2768
2769 } // namespace
2770
2771 // Finds the class in the boot class loader.
2772 // If the class is found the method returns the resolved class. Otherwise it returns null.
FindClassInBootClassLoaderClassPath(Thread * self,const char * descriptor,size_t hash,ObjPtr<mirror::Class> * result)2773 bool ClassLinker::FindClassInBootClassLoaderClassPath(Thread* self,
2774 const char* descriptor,
2775 size_t hash,
2776 /*out*/ ObjPtr<mirror::Class>* result) {
2777 ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_);
2778 if (pair.second != nullptr) {
2779 ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, nullptr);
2780 if (klass != nullptr) {
2781 *result = EnsureResolved(self, descriptor, klass);
2782 } else {
2783 *result = DefineClass(self,
2784 descriptor,
2785 hash,
2786 ScopedNullHandle<mirror::ClassLoader>(),
2787 *pair.first,
2788 *pair.second);
2789 }
2790 if (*result == nullptr) {
2791 CHECK(self->IsExceptionPending()) << descriptor;
2792 FilterDexFileCaughtExceptions(self, this);
2793 }
2794 }
2795 // The boot classloader is always a known lookup.
2796 return true;
2797 }
2798
FindClassInBaseDexClassLoaderClassPath(ScopedObjectAccessAlreadyRunnable & soa,const char * descriptor,size_t hash,Handle<mirror::ClassLoader> class_loader,ObjPtr<mirror::Class> * result)2799 bool ClassLinker::FindClassInBaseDexClassLoaderClassPath(
2800 ScopedObjectAccessAlreadyRunnable& soa,
2801 const char* descriptor,
2802 size_t hash,
2803 Handle<mirror::ClassLoader> class_loader,
2804 /*out*/ ObjPtr<mirror::Class>* result) {
2805 DCHECK(IsPathOrDexClassLoader(soa, class_loader) ||
2806 IsInMemoryDexClassLoader(soa, class_loader) ||
2807 IsDelegateLastClassLoader(soa, class_loader))
2808 << "Unexpected class loader for descriptor " << descriptor;
2809
2810 const DexFile* dex_file = nullptr;
2811 const dex::ClassDef* class_def = nullptr;
2812 ObjPtr<mirror::Class> ret;
2813 auto find_class_def = [&](const DexFile* cp_dex_file) REQUIRES_SHARED(Locks::mutator_lock_) {
2814 const dex::ClassDef* cp_class_def = OatDexFile::FindClassDef(*cp_dex_file, descriptor, hash);
2815 if (cp_class_def != nullptr) {
2816 dex_file = cp_dex_file;
2817 class_def = cp_class_def;
2818 return false; // Found a class definition, stop visit.
2819 }
2820 return true; // Continue with the next DexFile.
2821 };
2822 VisitClassLoaderDexFiles(soa, class_loader, find_class_def);
2823
2824 if (class_def != nullptr) {
2825 *result = DefineClass(soa.Self(), descriptor, hash, class_loader, *dex_file, *class_def);
2826 if (UNLIKELY(*result == nullptr)) {
2827 CHECK(soa.Self()->IsExceptionPending()) << descriptor;
2828 FilterDexFileCaughtExceptions(soa.Self(), this);
2829 } else {
2830 DCHECK(!soa.Self()->IsExceptionPending());
2831 }
2832 }
2833 // A BaseDexClassLoader is always a known lookup.
2834 return true;
2835 }
2836
FindClass(Thread * self,const char * descriptor,Handle<mirror::ClassLoader> class_loader)2837 ObjPtr<mirror::Class> ClassLinker::FindClass(Thread* self,
2838 const char* descriptor,
2839 Handle<mirror::ClassLoader> class_loader) {
2840 DCHECK_NE(*descriptor, '\0') << "descriptor is empty string";
2841 DCHECK(self != nullptr);
2842 self->AssertNoPendingException();
2843 self->PoisonObjectPointers(); // For DefineClass, CreateArrayClass, etc...
2844 if (descriptor[1] == '\0') {
2845 // only the descriptors of primitive types should be 1 character long, also avoid class lookup
2846 // for primitive classes that aren't backed by dex files.
2847 return FindPrimitiveClass(descriptor[0]);
2848 }
2849 const size_t hash = ComputeModifiedUtf8Hash(descriptor);
2850 // Find the class in the loaded classes table.
2851 ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, class_loader.Get());
2852 if (klass != nullptr) {
2853 return EnsureResolved(self, descriptor, klass);
2854 }
2855 // Class is not yet loaded.
2856 if (descriptor[0] != '[' && class_loader == nullptr) {
2857 // Non-array class and the boot class loader, search the boot class path.
2858 ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_);
2859 if (pair.second != nullptr) {
2860 return DefineClass(self,
2861 descriptor,
2862 hash,
2863 ScopedNullHandle<mirror::ClassLoader>(),
2864 *pair.first,
2865 *pair.second);
2866 } else {
2867 // The boot class loader is searched ahead of the application class loader, failures are
2868 // expected and will be wrapped in a ClassNotFoundException. Use the pre-allocated error to
2869 // trigger the chaining with a proper stack trace.
2870 ObjPtr<mirror::Throwable> pre_allocated =
2871 Runtime::Current()->GetPreAllocatedNoClassDefFoundError();
2872 self->SetException(pre_allocated);
2873 return nullptr;
2874 }
2875 }
2876 ObjPtr<mirror::Class> result_ptr;
2877 bool descriptor_equals;
2878 if (descriptor[0] == '[') {
2879 result_ptr = CreateArrayClass(self, descriptor, hash, class_loader);
2880 DCHECK_EQ(result_ptr == nullptr, self->IsExceptionPending());
2881 DCHECK(result_ptr == nullptr || result_ptr->DescriptorEquals(descriptor));
2882 descriptor_equals = true;
2883 } else {
2884 ScopedObjectAccessUnchecked soa(self);
2885 bool known_hierarchy =
2886 FindClassInBaseDexClassLoader(soa, self, descriptor, hash, class_loader, &result_ptr);
2887 if (result_ptr != nullptr) {
2888 // The chain was understood and we found the class. We still need to add the class to
2889 // the class table to protect from racy programs that can try and redefine the path list
2890 // which would change the Class<?> returned for subsequent evaluation of const-class.
2891 DCHECK(known_hierarchy);
2892 DCHECK(result_ptr->DescriptorEquals(descriptor));
2893 descriptor_equals = true;
2894 } else if (!self->IsExceptionPending()) {
2895 // Either the chain wasn't understood or the class wasn't found.
2896 // If there is a pending exception we didn't clear, it is a not a ClassNotFoundException and
2897 // we should return it instead of silently clearing and retrying.
2898 //
2899 // If the chain was understood but we did not find the class, let the Java-side
2900 // rediscover all this and throw the exception with the right stack trace. Note that
2901 // the Java-side could still succeed for racy programs if another thread is actively
2902 // modifying the class loader's path list.
2903
2904 // The runtime is not allowed to call into java from a runtime-thread so just abort.
2905 if (self->IsRuntimeThread()) {
2906 // Oops, we can't call into java so we can't run actual class-loader code.
2907 // This is true for e.g. for the compiler (jit or aot).
2908 ObjPtr<mirror::Throwable> pre_allocated =
2909 Runtime::Current()->GetPreAllocatedNoClassDefFoundError();
2910 self->SetException(pre_allocated);
2911 return nullptr;
2912 }
2913
2914 // Inlined DescriptorToDot(descriptor) with extra validation.
2915 //
2916 // Throw NoClassDefFoundError early rather than potentially load a class only to fail
2917 // the DescriptorEquals() check below and give a confusing error message. For example,
2918 // when native code erroneously calls JNI GetFieldId() with signature "java/lang/String"
2919 // instead of "Ljava/lang/String;", the message below using the "dot" names would be
2920 // "class loader [...] returned class java.lang.String instead of java.lang.String".
2921 size_t descriptor_length = strlen(descriptor);
2922 if (UNLIKELY(descriptor[0] != 'L') ||
2923 UNLIKELY(descriptor[descriptor_length - 1] != ';') ||
2924 UNLIKELY(memchr(descriptor + 1, '.', descriptor_length - 2) != nullptr)) {
2925 ThrowNoClassDefFoundError("Invalid descriptor: %s.", descriptor);
2926 return nullptr;
2927 }
2928
2929 std::string class_name_string(descriptor + 1, descriptor_length - 2);
2930 std::replace(class_name_string.begin(), class_name_string.end(), '/', '.');
2931 if (known_hierarchy &&
2932 fast_class_not_found_exceptions_ &&
2933 !Runtime::Current()->IsJavaDebuggable()) {
2934 // For known hierarchy, we know that the class is going to throw an exception. If we aren't
2935 // debuggable, optimize this path by throwing directly here without going back to Java
2936 // language. This reduces how many ClassNotFoundExceptions happen.
2937 self->ThrowNewExceptionF("Ljava/lang/ClassNotFoundException;",
2938 "%s",
2939 class_name_string.c_str());
2940 } else {
2941 ScopedLocalRef<jobject> class_loader_object(
2942 soa.Env(), soa.AddLocalReference<jobject>(class_loader.Get()));
2943 ScopedLocalRef<jobject> result(soa.Env(), nullptr);
2944 {
2945 ScopedThreadStateChange tsc(self, kNative);
2946 ScopedLocalRef<jobject> class_name_object(
2947 soa.Env(), soa.Env()->NewStringUTF(class_name_string.c_str()));
2948 if (class_name_object.get() == nullptr) {
2949 DCHECK(self->IsExceptionPending()); // OOME.
2950 return nullptr;
2951 }
2952 CHECK(class_loader_object.get() != nullptr);
2953 result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(),
2954 WellKnownClasses::java_lang_ClassLoader_loadClass,
2955 class_name_object.get()));
2956 }
2957 if (result.get() == nullptr && !self->IsExceptionPending()) {
2958 // broken loader - throw NPE to be compatible with Dalvik
2959 ThrowNullPointerException(StringPrintf("ClassLoader.loadClass returned null for %s",
2960 class_name_string.c_str()).c_str());
2961 return nullptr;
2962 }
2963 result_ptr = soa.Decode<mirror::Class>(result.get());
2964 // Check the name of the returned class.
2965 descriptor_equals = (result_ptr != nullptr) && result_ptr->DescriptorEquals(descriptor);
2966 }
2967 } else {
2968 DCHECK(!MatchesDexFileCaughtExceptions(self->GetException(), this));
2969 }
2970 }
2971
2972 if (self->IsExceptionPending()) {
2973 // If the ClassLoader threw or array class allocation failed, pass that exception up.
2974 // However, to comply with the RI behavior, first check if another thread succeeded.
2975 result_ptr = LookupClass(self, descriptor, hash, class_loader.Get());
2976 if (result_ptr != nullptr && !result_ptr->IsErroneous()) {
2977 self->ClearException();
2978 return EnsureResolved(self, descriptor, result_ptr);
2979 }
2980 return nullptr;
2981 }
2982
2983 // Try to insert the class to the class table, checking for mismatch.
2984 ObjPtr<mirror::Class> old;
2985 {
2986 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
2987 ClassTable* const class_table = InsertClassTableForClassLoader(class_loader.Get());
2988 old = class_table->Lookup(descriptor, hash);
2989 if (old == nullptr) {
2990 old = result_ptr; // For the comparison below, after releasing the lock.
2991 if (descriptor_equals) {
2992 class_table->InsertWithHash(result_ptr, hash);
2993 WriteBarrier::ForEveryFieldWrite(class_loader.Get());
2994 } // else throw below, after releasing the lock.
2995 }
2996 }
2997 if (UNLIKELY(old != result_ptr)) {
2998 // Return `old` (even if `!descriptor_equals`) to mimic the RI behavior for parallel
2999 // capable class loaders. (All class loaders are considered parallel capable on Android.)
3000 ObjPtr<mirror::Class> loader_class = class_loader->GetClass();
3001 const char* loader_class_name =
3002 loader_class->GetDexFile().StringByTypeIdx(loader_class->GetDexTypeIndex());
3003 LOG(WARNING) << "Initiating class loader of type " << DescriptorToDot(loader_class_name)
3004 << " is not well-behaved; it returned a different Class for racing loadClass(\""
3005 << DescriptorToDot(descriptor) << "\").";
3006 return EnsureResolved(self, descriptor, old);
3007 }
3008 if (UNLIKELY(!descriptor_equals)) {
3009 std::string result_storage;
3010 const char* result_name = result_ptr->GetDescriptor(&result_storage);
3011 std::string loader_storage;
3012 const char* loader_class_name = class_loader->GetClass()->GetDescriptor(&loader_storage);
3013 ThrowNoClassDefFoundError(
3014 "Initiating class loader of type %s returned class %s instead of %s.",
3015 DescriptorToDot(loader_class_name).c_str(),
3016 DescriptorToDot(result_name).c_str(),
3017 DescriptorToDot(descriptor).c_str());
3018 return nullptr;
3019 }
3020 // Success.
3021 return result_ptr;
3022 }
3023
3024 // Helper for maintaining DefineClass counting. We need to notify callbacks when we start/end a
3025 // define-class and how many recursive DefineClasses we are at in order to allow for doing things
3026 // like pausing class definition.
3027 struct ScopedDefiningClass {
3028 public:
REQUIRES_SHAREDart::ScopedDefiningClass3029 explicit ScopedDefiningClass(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_)
3030 : self_(self), returned_(false) {
3031 Locks::mutator_lock_->AssertSharedHeld(self_);
3032 Runtime::Current()->GetRuntimeCallbacks()->BeginDefineClass();
3033 self_->IncrDefineClassCount();
3034 }
REQUIRES_SHAREDart::ScopedDefiningClass3035 ~ScopedDefiningClass() REQUIRES_SHARED(Locks::mutator_lock_) {
3036 Locks::mutator_lock_->AssertSharedHeld(self_);
3037 CHECK(returned_);
3038 }
3039
Finishart::ScopedDefiningClass3040 ObjPtr<mirror::Class> Finish(Handle<mirror::Class> h_klass)
3041 REQUIRES_SHARED(Locks::mutator_lock_) {
3042 CHECK(!returned_);
3043 self_->DecrDefineClassCount();
3044 Runtime::Current()->GetRuntimeCallbacks()->EndDefineClass();
3045 Thread::PoisonObjectPointersIfDebug();
3046 returned_ = true;
3047 return h_klass.Get();
3048 }
3049
Finishart::ScopedDefiningClass3050 ObjPtr<mirror::Class> Finish(ObjPtr<mirror::Class> klass)
3051 REQUIRES_SHARED(Locks::mutator_lock_) {
3052 StackHandleScope<1> hs(self_);
3053 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
3054 return Finish(h_klass);
3055 }
3056
Finishart::ScopedDefiningClass3057 ObjPtr<mirror::Class> Finish(nullptr_t np ATTRIBUTE_UNUSED)
3058 REQUIRES_SHARED(Locks::mutator_lock_) {
3059 ScopedNullHandle<mirror::Class> snh;
3060 return Finish(snh);
3061 }
3062
3063 private:
3064 Thread* self_;
3065 bool returned_;
3066 };
3067
DefineClass(Thread * self,const char * descriptor,size_t hash,Handle<mirror::ClassLoader> class_loader,const DexFile & dex_file,const dex::ClassDef & dex_class_def)3068 ObjPtr<mirror::Class> ClassLinker::DefineClass(Thread* self,
3069 const char* descriptor,
3070 size_t hash,
3071 Handle<mirror::ClassLoader> class_loader,
3072 const DexFile& dex_file,
3073 const dex::ClassDef& dex_class_def) {
3074 ScopedDefiningClass sdc(self);
3075 StackHandleScope<3> hs(self);
3076 metrics::AutoTimer timer{GetMetrics()->ClassLoadingTotalTime()};
3077 auto klass = hs.NewHandle<mirror::Class>(nullptr);
3078
3079 // Load the class from the dex file.
3080 if (UNLIKELY(!init_done_)) {
3081 // finish up init of hand crafted class_roots_
3082 if (strcmp(descriptor, "Ljava/lang/Object;") == 0) {
3083 klass.Assign(GetClassRoot<mirror::Object>(this));
3084 } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) {
3085 klass.Assign(GetClassRoot<mirror::Class>(this));
3086 } else if (strcmp(descriptor, "Ljava/lang/String;") == 0) {
3087 klass.Assign(GetClassRoot<mirror::String>(this));
3088 } else if (strcmp(descriptor, "Ljava/lang/ref/Reference;") == 0) {
3089 klass.Assign(GetClassRoot<mirror::Reference>(this));
3090 } else if (strcmp(descriptor, "Ljava/lang/DexCache;") == 0) {
3091 klass.Assign(GetClassRoot<mirror::DexCache>(this));
3092 } else if (strcmp(descriptor, "Ldalvik/system/ClassExt;") == 0) {
3093 klass.Assign(GetClassRoot<mirror::ClassExt>(this));
3094 }
3095 }
3096
3097 // For AOT-compilation of an app, we may use a shortened boot class path that excludes
3098 // some runtime modules. Prevent definition of classes in app class loader that could clash
3099 // with these modules as these classes could be resolved differently during execution.
3100 if (class_loader != nullptr &&
3101 Runtime::Current()->IsAotCompiler() &&
3102 IsUpdatableBootClassPathDescriptor(descriptor)) {
3103 ObjPtr<mirror::Throwable> pre_allocated =
3104 Runtime::Current()->GetPreAllocatedNoClassDefFoundError();
3105 self->SetException(pre_allocated);
3106 return sdc.Finish(nullptr);
3107 }
3108
3109 // For AOT-compilation of an app, we may use only a public SDK to resolve symbols. If the SDK
3110 // checks are configured (a non null SdkChecker) and the descriptor is not in the provided
3111 // public class path then we prevent the definition of the class.
3112 //
3113 // NOTE that we only do the checks for the boot classpath APIs. Anything else, like the app
3114 // classpath is not checked.
3115 if (class_loader == nullptr &&
3116 Runtime::Current()->IsAotCompiler() &&
3117 DenyAccessBasedOnPublicSdk(descriptor)) {
3118 ObjPtr<mirror::Throwable> pre_allocated =
3119 Runtime::Current()->GetPreAllocatedNoClassDefFoundError();
3120 self->SetException(pre_allocated);
3121 return sdc.Finish(nullptr);
3122 }
3123
3124 // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied
3125 // code to be executed. We put it up here so we can avoid all the allocations associated with
3126 // creating the class. This can happen with (eg) jit threads.
3127 if (!self->CanLoadClasses()) {
3128 // Make sure we don't try to load anything, potentially causing an infinite loop.
3129 ObjPtr<mirror::Throwable> pre_allocated =
3130 Runtime::Current()->GetPreAllocatedNoClassDefFoundError();
3131 self->SetException(pre_allocated);
3132 return sdc.Finish(nullptr);
3133 }
3134
3135 if (klass == nullptr) {
3136 // Allocate a class with the status of not ready.
3137 // Interface object should get the right size here. Regular class will
3138 // figure out the right size later and be replaced with one of the right
3139 // size when the class becomes resolved.
3140 if (CanAllocClass()) {
3141 klass.Assign(AllocClass(self, SizeOfClassWithoutEmbeddedTables(dex_file, dex_class_def)));
3142 } else {
3143 return sdc.Finish(nullptr);
3144 }
3145 }
3146 if (UNLIKELY(klass == nullptr)) {
3147 self->AssertPendingOOMException();
3148 return sdc.Finish(nullptr);
3149 }
3150 // Get the real dex file. This will return the input if there aren't any callbacks or they do
3151 // nothing.
3152 DexFile const* new_dex_file = nullptr;
3153 dex::ClassDef const* new_class_def = nullptr;
3154 // TODO We should ideally figure out some way to move this after we get a lock on the klass so it
3155 // will only be called once.
3156 Runtime::Current()->GetRuntimeCallbacks()->ClassPreDefine(descriptor,
3157 klass,
3158 class_loader,
3159 dex_file,
3160 dex_class_def,
3161 &new_dex_file,
3162 &new_class_def);
3163 // Check to see if an exception happened during runtime callbacks. Return if so.
3164 if (self->IsExceptionPending()) {
3165 return sdc.Finish(nullptr);
3166 }
3167 ObjPtr<mirror::DexCache> dex_cache = RegisterDexFile(*new_dex_file, class_loader.Get());
3168 if (dex_cache == nullptr) {
3169 self->AssertPendingException();
3170 return sdc.Finish(nullptr);
3171 }
3172 klass->SetDexCache(dex_cache);
3173 SetupClass(*new_dex_file, *new_class_def, klass, class_loader.Get());
3174
3175 // Mark the string class by setting its access flag.
3176 if (UNLIKELY(!init_done_)) {
3177 if (strcmp(descriptor, "Ljava/lang/String;") == 0) {
3178 klass->SetStringClass();
3179 }
3180 }
3181
3182 ObjectLock<mirror::Class> lock(self, klass);
3183 klass->SetClinitThreadId(self->GetTid());
3184 // Make sure we have a valid empty iftable even if there are errors.
3185 klass->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable());
3186
3187 // Add the newly loaded class to the loaded classes table.
3188 ObjPtr<mirror::Class> existing = InsertClass(descriptor, klass.Get(), hash);
3189 if (existing != nullptr) {
3190 // We failed to insert because we raced with another thread. Calling EnsureResolved may cause
3191 // this thread to block.
3192 return sdc.Finish(EnsureResolved(self, descriptor, existing));
3193 }
3194
3195 // Load the fields and other things after we are inserted in the table. This is so that we don't
3196 // end up allocating unfree-able linear alloc resources and then lose the race condition. The
3197 // other reason is that the field roots are only visited from the class table. So we need to be
3198 // inserted before we allocate / fill in these fields.
3199 LoadClass(self, *new_dex_file, *new_class_def, klass);
3200 if (self->IsExceptionPending()) {
3201 VLOG(class_linker) << self->GetException()->Dump();
3202 // An exception occured during load, set status to erroneous while holding klass' lock in case
3203 // notification is necessary.
3204 if (!klass->IsErroneous()) {
3205 mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self);
3206 }
3207 return sdc.Finish(nullptr);
3208 }
3209
3210 // Finish loading (if necessary) by finding parents
3211 CHECK(!klass->IsLoaded());
3212 if (!LoadSuperAndInterfaces(klass, *new_dex_file)) {
3213 // Loading failed.
3214 if (!klass->IsErroneous()) {
3215 mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self);
3216 }
3217 return sdc.Finish(nullptr);
3218 }
3219 CHECK(klass->IsLoaded());
3220
3221 // At this point the class is loaded. Publish a ClassLoad event.
3222 // Note: this may be a temporary class. It is a listener's responsibility to handle this.
3223 Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(klass);
3224
3225 // Link the class (if necessary)
3226 CHECK(!klass->IsResolved());
3227 // TODO: Use fast jobjects?
3228 auto interfaces = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr);
3229
3230 MutableHandle<mirror::Class> h_new_class = hs.NewHandle<mirror::Class>(nullptr);
3231 if (!LinkClass(self, descriptor, klass, interfaces, &h_new_class)) {
3232 // Linking failed.
3233 if (!klass->IsErroneous()) {
3234 mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self);
3235 }
3236 return sdc.Finish(nullptr);
3237 }
3238 self->AssertNoPendingException();
3239 CHECK(h_new_class != nullptr) << descriptor;
3240 CHECK(h_new_class->IsResolved() && !h_new_class->IsErroneousResolved()) << descriptor;
3241
3242 // Instrumentation may have updated entrypoints for all methods of all
3243 // classes. However it could not update methods of this class while we
3244 // were loading it. Now the class is resolved, we can update entrypoints
3245 // as required by instrumentation.
3246 if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) {
3247 // We must be in the kRunnable state to prevent instrumentation from
3248 // suspending all threads to update entrypoints while we are doing it
3249 // for this class.
3250 DCHECK_EQ(self->GetState(), kRunnable);
3251 Runtime::Current()->GetInstrumentation()->InstallStubsForClass(h_new_class.Get());
3252 }
3253
3254 /*
3255 * We send CLASS_PREPARE events to the debugger from here. The
3256 * definition of "preparation" is creating the static fields for a
3257 * class and initializing them to the standard default values, but not
3258 * executing any code (that comes later, during "initialization").
3259 *
3260 * We did the static preparation in LinkClass.
3261 *
3262 * The class has been prepared and resolved but possibly not yet verified
3263 * at this point.
3264 */
3265 Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(klass, h_new_class);
3266
3267 // Notify native debugger of the new class and its layout.
3268 jit::Jit::NewTypeLoadedIfUsingJit(h_new_class.Get());
3269
3270 return sdc.Finish(h_new_class);
3271 }
3272
SizeOfClassWithoutEmbeddedTables(const DexFile & dex_file,const dex::ClassDef & dex_class_def)3273 uint32_t ClassLinker::SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file,
3274 const dex::ClassDef& dex_class_def) {
3275 size_t num_ref = 0;
3276 size_t num_8 = 0;
3277 size_t num_16 = 0;
3278 size_t num_32 = 0;
3279 size_t num_64 = 0;
3280 ClassAccessor accessor(dex_file, dex_class_def);
3281 // We allow duplicate definitions of the same field in a class_data_item
3282 // but ignore the repeated indexes here, b/21868015.
3283 uint32_t last_field_idx = dex::kDexNoIndex;
3284 for (const ClassAccessor::Field& field : accessor.GetStaticFields()) {
3285 uint32_t field_idx = field.GetIndex();
3286 // Ordering enforced by DexFileVerifier.
3287 DCHECK(last_field_idx == dex::kDexNoIndex || last_field_idx <= field_idx);
3288 if (UNLIKELY(field_idx == last_field_idx)) {
3289 continue;
3290 }
3291 last_field_idx = field_idx;
3292 const dex::FieldId& field_id = dex_file.GetFieldId(field_idx);
3293 const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id);
3294 char c = descriptor[0];
3295 switch (c) {
3296 case 'L':
3297 case '[':
3298 num_ref++;
3299 break;
3300 case 'J':
3301 case 'D':
3302 num_64++;
3303 break;
3304 case 'I':
3305 case 'F':
3306 num_32++;
3307 break;
3308 case 'S':
3309 case 'C':
3310 num_16++;
3311 break;
3312 case 'B':
3313 case 'Z':
3314 num_8++;
3315 break;
3316 default:
3317 LOG(FATAL) << "Unknown descriptor: " << c;
3318 UNREACHABLE();
3319 }
3320 }
3321 return mirror::Class::ComputeClassSize(false,
3322 0,
3323 num_8,
3324 num_16,
3325 num_32,
3326 num_64,
3327 num_ref,
3328 image_pointer_size_);
3329 }
3330
3331 // Special case to get oat code without overwriting a trampoline.
GetQuickOatCodeFor(ArtMethod * method)3332 const void* ClassLinker::GetQuickOatCodeFor(ArtMethod* method) {
3333 CHECK(method->IsInvokable()) << method->PrettyMethod();
3334 if (method->IsProxyMethod()) {
3335 return GetQuickProxyInvokeHandler();
3336 }
3337 const void* code = method->GetOatMethodQuickCode(GetImagePointerSize());
3338 if (code != nullptr) {
3339 return code;
3340 }
3341
3342 jit::Jit* jit = Runtime::Current()->GetJit();
3343 if (jit != nullptr) {
3344 code = jit->GetCodeCache()->GetSavedEntryPointOfPreCompiledMethod(method);
3345 if (code != nullptr) {
3346 return code;
3347 }
3348 }
3349
3350 if (method->IsNative()) {
3351 // No code and native? Use generic trampoline.
3352 return GetQuickGenericJniStub();
3353 }
3354
3355 if (interpreter::CanRuntimeUseNterp() && CanMethodUseNterp(method)) {
3356 return interpreter::GetNterpEntryPoint();
3357 }
3358
3359 return GetQuickToInterpreterBridge();
3360 }
3361
ShouldUseInterpreterEntrypoint(ArtMethod * method,const void * quick_code)3362 bool ClassLinker::ShouldUseInterpreterEntrypoint(ArtMethod* method, const void* quick_code) {
3363 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
3364 if (UNLIKELY(method->IsNative() || method->IsProxyMethod())) {
3365 return false;
3366 }
3367
3368 if (quick_code == nullptr) {
3369 return true;
3370 }
3371
3372 Runtime* runtime = Runtime::Current();
3373 instrumentation::Instrumentation* instr = runtime->GetInstrumentation();
3374 if (instr->InterpretOnly()) {
3375 return true;
3376 }
3377
3378 if (runtime->GetClassLinker()->IsQuickToInterpreterBridge(quick_code)) {
3379 // Doing this check avoids doing compiled/interpreter transitions.
3380 return true;
3381 }
3382
3383 if (Thread::Current()->IsForceInterpreter()) {
3384 // Force the use of interpreter when it is required by the debugger.
3385 return true;
3386 }
3387
3388 if (Thread::Current()->IsAsyncExceptionPending()) {
3389 // Force use of interpreter to handle async-exceptions
3390 return true;
3391 }
3392
3393 if (quick_code == GetQuickInstrumentationEntryPoint()) {
3394 const void* instr_target = instr->GetCodeForInvoke(method);
3395 DCHECK_NE(instr_target, GetQuickInstrumentationEntryPoint()) << method->PrettyMethod();
3396 return ShouldUseInterpreterEntrypoint(method, instr_target);
3397 }
3398
3399 if (runtime->IsJavaDebuggable()) {
3400 // For simplicity, we ignore precompiled code and go to the interpreter
3401 // assuming we don't already have jitted code.
3402 // We could look at the oat file where `quick_code` is being defined,
3403 // and check whether it's been compiled debuggable, but we decided to
3404 // only rely on the JIT for debuggable apps.
3405 jit::Jit* jit = Runtime::Current()->GetJit();
3406 return (jit == nullptr) || !jit->GetCodeCache()->ContainsPc(quick_code);
3407 }
3408
3409 if (runtime->IsNativeDebuggable()) {
3410 DCHECK(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse());
3411 // If we are doing native debugging, ignore application's AOT code,
3412 // since we want to JIT it (at first use) with extra stackmaps for native
3413 // debugging. We keep however all AOT code from the boot image,
3414 // since the JIT-at-first-use is blocking and would result in non-negligible
3415 // startup performance impact.
3416 return !runtime->GetHeap()->IsInBootImageOatFile(quick_code);
3417 }
3418
3419 return false;
3420 }
3421
FixupStaticTrampolines(Thread * self,ObjPtr<mirror::Class> klass)3422 void ClassLinker::FixupStaticTrampolines(Thread* self, ObjPtr<mirror::Class> klass) {
3423 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
3424 DCHECK(klass->IsVisiblyInitialized()) << klass->PrettyDescriptor();
3425 size_t num_direct_methods = klass->NumDirectMethods();
3426 if (num_direct_methods == 0) {
3427 return; // No direct methods => no static methods.
3428 }
3429 if (UNLIKELY(klass->IsProxyClass())) {
3430 return;
3431 }
3432 PointerSize pointer_size = image_pointer_size_;
3433 if (std::any_of(klass->GetDirectMethods(pointer_size).begin(),
3434 klass->GetDirectMethods(pointer_size).end(),
3435 [](const ArtMethod& m) { return m.IsCriticalNative(); })) {
3436 // Store registered @CriticalNative methods, if any, to JNI entrypoints.
3437 // Direct methods are a contiguous chunk of memory, so use the ordering of the map.
3438 ArtMethod* first_method = klass->GetDirectMethod(0u, pointer_size);
3439 ArtMethod* last_method = klass->GetDirectMethod(num_direct_methods - 1u, pointer_size);
3440 MutexLock lock(self, critical_native_code_with_clinit_check_lock_);
3441 auto lb = critical_native_code_with_clinit_check_.lower_bound(first_method);
3442 while (lb != critical_native_code_with_clinit_check_.end() && lb->first <= last_method) {
3443 lb->first->SetEntryPointFromJni(lb->second);
3444 lb = critical_native_code_with_clinit_check_.erase(lb);
3445 }
3446 }
3447 Runtime* runtime = Runtime::Current();
3448 if (!runtime->IsStarted()) {
3449 if (runtime->IsAotCompiler() || runtime->GetHeap()->HasBootImageSpace()) {
3450 return; // OAT file unavailable.
3451 }
3452 }
3453
3454 const DexFile& dex_file = klass->GetDexFile();
3455 bool has_oat_class;
3456 OatFile::OatClass oat_class = OatFile::FindOatClass(dex_file,
3457 klass->GetDexClassDefIndex(),
3458 &has_oat_class);
3459 // Link the code of methods skipped by LinkCode.
3460 for (size_t method_index = 0; method_index < num_direct_methods; ++method_index) {
3461 ArtMethod* method = klass->GetDirectMethod(method_index, pointer_size);
3462 if (!method->IsStatic()) {
3463 // Only update static methods.
3464 continue;
3465 }
3466 const void* quick_code = nullptr;
3467
3468 // In order:
3469 // 1) Check if we have AOT Code.
3470 // 2) Check if we have JIT Code.
3471 // 3) Check if we can use Nterp.
3472 if (has_oat_class) {
3473 OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index);
3474 quick_code = oat_method.GetQuickCode();
3475 }
3476
3477 jit::Jit* jit = runtime->GetJit();
3478 if (quick_code == nullptr && jit != nullptr) {
3479 quick_code = jit->GetCodeCache()->GetSavedEntryPointOfPreCompiledMethod(method);
3480 }
3481
3482 if (quick_code == nullptr &&
3483 interpreter::CanRuntimeUseNterp() &&
3484 CanMethodUseNterp(method)) {
3485 quick_code = interpreter::GetNterpEntryPoint();
3486 }
3487
3488 // Check whether the method is native, in which case it's generic JNI.
3489 if (quick_code == nullptr && method->IsNative()) {
3490 quick_code = GetQuickGenericJniStub();
3491 } else if (ShouldUseInterpreterEntrypoint(method, quick_code)) {
3492 // Use interpreter entry point.
3493 if (IsQuickToInterpreterBridge(method->GetEntryPointFromQuickCompiledCode())) {
3494 // If we have the trampoline or the bridge already, no need to update.
3495 // This saves in not dirtying boot image memory.
3496 continue;
3497 }
3498 quick_code = GetQuickToInterpreterBridge();
3499 }
3500 CHECK(quick_code != nullptr);
3501 runtime->GetInstrumentation()->UpdateMethodsCode(method, quick_code);
3502 }
3503 // Ignore virtual methods on the iterator.
3504 }
3505
3506 // Does anything needed to make sure that the compiler will not generate a direct invoke to this
3507 // method. Should only be called on non-invokable methods.
EnsureThrowsInvocationError(ClassLinker * class_linker,ArtMethod * method)3508 inline void EnsureThrowsInvocationError(ClassLinker* class_linker, ArtMethod* method)
3509 REQUIRES_SHARED(Locks::mutator_lock_) {
3510 DCHECK(method != nullptr);
3511 DCHECK(!method->IsInvokable());
3512 method->SetEntryPointFromQuickCompiledCodePtrSize(
3513 class_linker->GetQuickToInterpreterBridgeTrampoline(),
3514 class_linker->GetImagePointerSize());
3515 }
3516
LinkCode(ClassLinker * class_linker,ArtMethod * method,const OatFile::OatClass * oat_class,uint32_t class_def_method_index)3517 static void LinkCode(ClassLinker* class_linker,
3518 ArtMethod* method,
3519 const OatFile::OatClass* oat_class,
3520 uint32_t class_def_method_index) REQUIRES_SHARED(Locks::mutator_lock_) {
3521 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
3522 Runtime* const runtime = Runtime::Current();
3523 if (runtime->IsAotCompiler()) {
3524 // The following code only applies to a non-compiler runtime.
3525 return;
3526 }
3527
3528 // Method shouldn't have already been linked.
3529 DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr);
3530
3531 if (!method->IsInvokable()) {
3532 EnsureThrowsInvocationError(class_linker, method);
3533 return;
3534 }
3535
3536 const void* quick_code = nullptr;
3537 if (oat_class != nullptr) {
3538 // Every kind of method should at least get an invoke stub from the oat_method.
3539 // non-abstract methods also get their code pointers.
3540 const OatFile::OatMethod oat_method = oat_class->GetOatMethod(class_def_method_index);
3541 quick_code = oat_method.GetQuickCode();
3542 }
3543
3544 bool enter_interpreter = class_linker->ShouldUseInterpreterEntrypoint(method, quick_code);
3545
3546 // Note: this mimics the logic in image_writer.cc that installs the resolution
3547 // stub only if we have compiled code and the method needs a class initialization
3548 // check.
3549 if (quick_code == nullptr) {
3550 method->SetEntryPointFromQuickCompiledCode(
3551 method->IsNative() ? GetQuickGenericJniStub() : GetQuickToInterpreterBridge());
3552 } else if (enter_interpreter) {
3553 method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge());
3554 } else if (NeedsClinitCheckBeforeCall(method)) {
3555 DCHECK(!method->GetDeclaringClass()->IsVisiblyInitialized()); // Actually ClassStatus::Idx.
3556 // If we do have code but the method needs a class initialization check before calling
3557 // that code, install the resolution stub that will perform the check.
3558 // It will be replaced by the proper entry point by ClassLinker::FixupStaticTrampolines
3559 // after initializing class (see ClassLinker::InitializeClass method).
3560 method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionStub());
3561 } else {
3562 method->SetEntryPointFromQuickCompiledCode(quick_code);
3563 }
3564
3565 if (method->IsNative()) {
3566 // Set up the dlsym lookup stub. Do not go through `UnregisterNative()`
3567 // as the extra processing for @CriticalNative is not needed yet.
3568 method->SetEntryPointFromJni(
3569 method->IsCriticalNative() ? GetJniDlsymLookupCriticalStub() : GetJniDlsymLookupStub());
3570
3571 if (enter_interpreter || quick_code == nullptr) {
3572 // We have a native method here without code. Then it should have the generic JNI
3573 // trampoline as entrypoint.
3574 // TODO: this doesn't handle all the cases where trampolines may be installed.
3575 DCHECK(class_linker->IsQuickGenericJniStub(method->GetEntryPointFromQuickCompiledCode()));
3576 }
3577 }
3578 }
3579
SetupClass(const DexFile & dex_file,const dex::ClassDef & dex_class_def,Handle<mirror::Class> klass,ObjPtr<mirror::ClassLoader> class_loader)3580 void ClassLinker::SetupClass(const DexFile& dex_file,
3581 const dex::ClassDef& dex_class_def,
3582 Handle<mirror::Class> klass,
3583 ObjPtr<mirror::ClassLoader> class_loader) {
3584 CHECK(klass != nullptr);
3585 CHECK(klass->GetDexCache() != nullptr);
3586 CHECK_EQ(ClassStatus::kNotReady, klass->GetStatus());
3587 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
3588 CHECK(descriptor != nullptr);
3589
3590 klass->SetClass(GetClassRoot<mirror::Class>(this));
3591 uint32_t access_flags = dex_class_def.GetJavaAccessFlags();
3592 CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U);
3593 klass->SetAccessFlagsDuringLinking(access_flags);
3594 klass->SetClassLoader(class_loader);
3595 DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot);
3596 mirror::Class::SetStatus(klass, ClassStatus::kIdx, nullptr);
3597
3598 klass->SetDexClassDefIndex(dex_file.GetIndexForClassDef(dex_class_def));
3599 klass->SetDexTypeIndex(dex_class_def.class_idx_);
3600 }
3601
AllocArtFieldArray(Thread * self,LinearAlloc * allocator,size_t length)3602 LengthPrefixedArray<ArtField>* ClassLinker::AllocArtFieldArray(Thread* self,
3603 LinearAlloc* allocator,
3604 size_t length) {
3605 if (length == 0) {
3606 return nullptr;
3607 }
3608 // If the ArtField alignment changes, review all uses of LengthPrefixedArray<ArtField>.
3609 static_assert(alignof(ArtField) == 4, "ArtField alignment is expected to be 4.");
3610 size_t storage_size = LengthPrefixedArray<ArtField>::ComputeSize(length);
3611 void* array_storage = allocator->Alloc(self, storage_size);
3612 auto* ret = new(array_storage) LengthPrefixedArray<ArtField>(length);
3613 CHECK(ret != nullptr);
3614 std::uninitialized_fill_n(&ret->At(0), length, ArtField());
3615 return ret;
3616 }
3617
AllocArtMethodArray(Thread * self,LinearAlloc * allocator,size_t length)3618 LengthPrefixedArray<ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self,
3619 LinearAlloc* allocator,
3620 size_t length) {
3621 if (length == 0) {
3622 return nullptr;
3623 }
3624 const size_t method_alignment = ArtMethod::Alignment(image_pointer_size_);
3625 const size_t method_size = ArtMethod::Size(image_pointer_size_);
3626 const size_t storage_size =
3627 LengthPrefixedArray<ArtMethod>::ComputeSize(length, method_size, method_alignment);
3628 void* array_storage = allocator->Alloc(self, storage_size);
3629 auto* ret = new (array_storage) LengthPrefixedArray<ArtMethod>(length);
3630 CHECK(ret != nullptr);
3631 for (size_t i = 0; i < length; ++i) {
3632 new(reinterpret_cast<void*>(&ret->At(i, method_size, method_alignment))) ArtMethod;
3633 }
3634 return ret;
3635 }
3636
GetAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader)3637 LinearAlloc* ClassLinker::GetAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) {
3638 if (class_loader == nullptr) {
3639 return Runtime::Current()->GetLinearAlloc();
3640 }
3641 LinearAlloc* allocator = class_loader->GetAllocator();
3642 DCHECK(allocator != nullptr);
3643 return allocator;
3644 }
3645
GetOrCreateAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader)3646 LinearAlloc* ClassLinker::GetOrCreateAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) {
3647 if (class_loader == nullptr) {
3648 return Runtime::Current()->GetLinearAlloc();
3649 }
3650 WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
3651 LinearAlloc* allocator = class_loader->GetAllocator();
3652 if (allocator == nullptr) {
3653 RegisterClassLoader(class_loader);
3654 allocator = class_loader->GetAllocator();
3655 CHECK(allocator != nullptr);
3656 }
3657 return allocator;
3658 }
3659
LoadClass(Thread * self,const DexFile & dex_file,const dex::ClassDef & dex_class_def,Handle<mirror::Class> klass)3660 void ClassLinker::LoadClass(Thread* self,
3661 const DexFile& dex_file,
3662 const dex::ClassDef& dex_class_def,
3663 Handle<mirror::Class> klass) {
3664 ClassAccessor accessor(dex_file,
3665 dex_class_def,
3666 /* parse_hiddenapi_class_data= */ klass->IsBootStrapClassLoaded());
3667 if (!accessor.HasClassData()) {
3668 return;
3669 }
3670 Runtime* const runtime = Runtime::Current();
3671 {
3672 // Note: We cannot have thread suspension until the field and method arrays are setup or else
3673 // Class::VisitFieldRoots may miss some fields or methods.
3674 ScopedAssertNoThreadSuspension nts(__FUNCTION__);
3675 // Load static fields.
3676 // We allow duplicate definitions of the same field in a class_data_item
3677 // but ignore the repeated indexes here, b/21868015.
3678 LinearAlloc* const allocator = GetAllocatorForClassLoader(klass->GetClassLoader());
3679 LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self,
3680 allocator,
3681 accessor.NumStaticFields());
3682 LengthPrefixedArray<ArtField>* ifields = AllocArtFieldArray(self,
3683 allocator,
3684 accessor.NumInstanceFields());
3685 size_t num_sfields = 0u;
3686 size_t num_ifields = 0u;
3687 uint32_t last_static_field_idx = 0u;
3688 uint32_t last_instance_field_idx = 0u;
3689
3690 // Methods
3691 bool has_oat_class = false;
3692 const OatFile::OatClass oat_class = (runtime->IsStarted() && !runtime->IsAotCompiler())
3693 ? OatFile::FindOatClass(dex_file, klass->GetDexClassDefIndex(), &has_oat_class)
3694 : OatFile::OatClass::Invalid();
3695 const OatFile::OatClass* oat_class_ptr = has_oat_class ? &oat_class : nullptr;
3696 klass->SetMethodsPtr(
3697 AllocArtMethodArray(self, allocator, accessor.NumMethods()),
3698 accessor.NumDirectMethods(),
3699 accessor.NumVirtualMethods());
3700 size_t class_def_method_index = 0;
3701 uint32_t last_dex_method_index = dex::kDexNoIndex;
3702 size_t last_class_def_method_index = 0;
3703
3704 // Use the visitor since the ranged based loops are bit slower from seeking. Seeking to the
3705 // methods needs to decode all of the fields.
3706 accessor.VisitFieldsAndMethods([&](
3707 const ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) {
3708 uint32_t field_idx = field.GetIndex();
3709 DCHECK_GE(field_idx, last_static_field_idx); // Ordering enforced by DexFileVerifier.
3710 if (num_sfields == 0 || LIKELY(field_idx > last_static_field_idx)) {
3711 LoadField(field, klass, &sfields->At(num_sfields));
3712 ++num_sfields;
3713 last_static_field_idx = field_idx;
3714 }
3715 }, [&](const ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) {
3716 uint32_t field_idx = field.GetIndex();
3717 DCHECK_GE(field_idx, last_instance_field_idx); // Ordering enforced by DexFileVerifier.
3718 if (num_ifields == 0 || LIKELY(field_idx > last_instance_field_idx)) {
3719 LoadField(field, klass, &ifields->At(num_ifields));
3720 ++num_ifields;
3721 last_instance_field_idx = field_idx;
3722 }
3723 }, [&](const ClassAccessor::Method& method) REQUIRES_SHARED(Locks::mutator_lock_) {
3724 ArtMethod* art_method = klass->GetDirectMethodUnchecked(class_def_method_index,
3725 image_pointer_size_);
3726 LoadMethod(dex_file, method, klass, art_method);
3727 LinkCode(this, art_method, oat_class_ptr, class_def_method_index);
3728 uint32_t it_method_index = method.GetIndex();
3729 if (last_dex_method_index == it_method_index) {
3730 // duplicate case
3731 art_method->SetMethodIndex(last_class_def_method_index);
3732 } else {
3733 art_method->SetMethodIndex(class_def_method_index);
3734 last_dex_method_index = it_method_index;
3735 last_class_def_method_index = class_def_method_index;
3736 }
3737 ++class_def_method_index;
3738 }, [&](const ClassAccessor::Method& method) REQUIRES_SHARED(Locks::mutator_lock_) {
3739 ArtMethod* art_method = klass->GetVirtualMethodUnchecked(
3740 class_def_method_index - accessor.NumDirectMethods(),
3741 image_pointer_size_);
3742 LoadMethod(dex_file, method, klass, art_method);
3743 LinkCode(this, art_method, oat_class_ptr, class_def_method_index);
3744 ++class_def_method_index;
3745 });
3746
3747 if (UNLIKELY(num_ifields + num_sfields != accessor.NumFields())) {
3748 LOG(WARNING) << "Duplicate fields in class " << klass->PrettyDescriptor()
3749 << " (unique static fields: " << num_sfields << "/" << accessor.NumStaticFields()
3750 << ", unique instance fields: " << num_ifields << "/" << accessor.NumInstanceFields()
3751 << ")";
3752 // NOTE: Not shrinking the over-allocated sfields/ifields, just setting size.
3753 if (sfields != nullptr) {
3754 sfields->SetSize(num_sfields);
3755 }
3756 if (ifields != nullptr) {
3757 ifields->SetSize(num_ifields);
3758 }
3759 }
3760 // Set the field arrays.
3761 klass->SetSFieldsPtr(sfields);
3762 DCHECK_EQ(klass->NumStaticFields(), num_sfields);
3763 klass->SetIFieldsPtr(ifields);
3764 DCHECK_EQ(klass->NumInstanceFields(), num_ifields);
3765 }
3766 // Ensure that the card is marked so that remembered sets pick up native roots.
3767 WriteBarrier::ForEveryFieldWrite(klass.Get());
3768 self->AllowThreadSuspension();
3769 }
3770
LoadField(const ClassAccessor::Field & field,Handle<mirror::Class> klass,ArtField * dst)3771 void ClassLinker::LoadField(const ClassAccessor::Field& field,
3772 Handle<mirror::Class> klass,
3773 ArtField* dst) {
3774 const uint32_t field_idx = field.GetIndex();
3775 dst->SetDexFieldIndex(field_idx);
3776 dst->SetDeclaringClass(klass.Get());
3777
3778 // Get access flags from the DexFile and set hiddenapi runtime access flags.
3779 dst->SetAccessFlags(field.GetAccessFlags() | hiddenapi::CreateRuntimeFlags(field));
3780 }
3781
LoadMethod(const DexFile & dex_file,const ClassAccessor::Method & method,Handle<mirror::Class> klass,ArtMethod * dst)3782 void ClassLinker::LoadMethod(const DexFile& dex_file,
3783 const ClassAccessor::Method& method,
3784 Handle<mirror::Class> klass,
3785 ArtMethod* dst) {
3786 const uint32_t dex_method_idx = method.GetIndex();
3787 const dex::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
3788 const char* method_name = dex_file.StringDataByIdx(method_id.name_idx_);
3789
3790 ScopedAssertNoThreadSuspension ants("LoadMethod");
3791 dst->SetDexMethodIndex(dex_method_idx);
3792 dst->SetDeclaringClass(klass.Get());
3793
3794 // Get access flags from the DexFile and set hiddenapi runtime access flags.
3795 uint32_t access_flags = method.GetAccessFlags() | hiddenapi::CreateRuntimeFlags(method);
3796
3797 if (UNLIKELY(strcmp("finalize", method_name) == 0)) {
3798 // Set finalizable flag on declaring class.
3799 if (strcmp("V", dex_file.GetShorty(method_id.proto_idx_)) == 0) {
3800 // Void return type.
3801 if (klass->GetClassLoader() != nullptr) { // All non-boot finalizer methods are flagged.
3802 klass->SetFinalizable();
3803 } else {
3804 std::string temp;
3805 const char* klass_descriptor = klass->GetDescriptor(&temp);
3806 // The Enum class declares a "final" finalize() method to prevent subclasses from
3807 // introducing a finalizer. We don't want to set the finalizable flag for Enum or its
3808 // subclasses, so we exclude it here.
3809 // We also want to avoid setting the flag on Object, where we know that finalize() is
3810 // empty.
3811 if (strcmp(klass_descriptor, "Ljava/lang/Object;") != 0 &&
3812 strcmp(klass_descriptor, "Ljava/lang/Enum;") != 0) {
3813 klass->SetFinalizable();
3814 }
3815 }
3816 }
3817 } else if (method_name[0] == '<') {
3818 // Fix broken access flags for initializers. Bug 11157540.
3819 bool is_init = (strcmp("<init>", method_name) == 0);
3820 bool is_clinit = !is_init && (strcmp("<clinit>", method_name) == 0);
3821 if (UNLIKELY(!is_init && !is_clinit)) {
3822 LOG(WARNING) << "Unexpected '<' at start of method name " << method_name;
3823 } else {
3824 if (UNLIKELY((access_flags & kAccConstructor) == 0)) {
3825 LOG(WARNING) << method_name << " didn't have expected constructor access flag in class "
3826 << klass->PrettyDescriptor() << " in dex file " << dex_file.GetLocation();
3827 access_flags |= kAccConstructor;
3828 }
3829 }
3830 }
3831 if (UNLIKELY((access_flags & kAccNative) != 0u)) {
3832 // Check if the native method is annotated with @FastNative or @CriticalNative.
3833 access_flags |= annotations::GetNativeMethodAnnotationAccessFlags(
3834 dex_file, dst->GetClassDef(), dex_method_idx);
3835 }
3836 dst->SetAccessFlags(access_flags);
3837 // Must be done after SetAccessFlags since IsAbstract depends on it.
3838 if (klass->IsInterface() && dst->IsAbstract()) {
3839 dst->CalculateAndSetImtIndex();
3840 }
3841 if (dst->HasCodeItem()) {
3842 DCHECK_NE(method.GetCodeItemOffset(), 0u);
3843 if (Runtime::Current()->IsAotCompiler()) {
3844 dst->SetDataPtrSize(reinterpret_cast32<void*>(method.GetCodeItemOffset()), image_pointer_size_);
3845 } else {
3846 dst->SetCodeItem(dst->GetDexFile()->GetCodeItem(method.GetCodeItemOffset()));
3847 }
3848 } else {
3849 dst->SetDataPtrSize(nullptr, image_pointer_size_);
3850 DCHECK_EQ(method.GetCodeItemOffset(), 0u);
3851 }
3852
3853 // Set optimization flags related to the shorty.
3854 const char* shorty = dst->GetShorty();
3855 bool all_parameters_are_reference = true;
3856 bool all_parameters_are_reference_or_int = true;
3857 bool return_type_is_fp = (shorty[0] == 'F' || shorty[0] == 'D');
3858
3859 for (size_t i = 1, e = strlen(shorty); i < e; ++i) {
3860 if (shorty[i] != 'L') {
3861 all_parameters_are_reference = false;
3862 if (shorty[i] == 'F' || shorty[i] == 'D' || shorty[i] == 'J') {
3863 all_parameters_are_reference_or_int = false;
3864 break;
3865 }
3866 }
3867 }
3868
3869 if (!dst->IsNative() && all_parameters_are_reference) {
3870 dst->SetNterpEntryPointFastPathFlag();
3871 }
3872
3873 if (!return_type_is_fp && all_parameters_are_reference_or_int) {
3874 dst->SetNterpInvokeFastPathFlag();
3875 }
3876 }
3877
AppendToBootClassPath(Thread * self,const DexFile * dex_file)3878 void ClassLinker::AppendToBootClassPath(Thread* self, const DexFile* dex_file) {
3879 ObjPtr<mirror::DexCache> dex_cache = AllocAndInitializeDexCache(
3880 self,
3881 *dex_file,
3882 Runtime::Current()->GetLinearAlloc());
3883 CHECK(dex_cache != nullptr) << "Failed to allocate dex cache for " << dex_file->GetLocation();
3884 AppendToBootClassPath(dex_file, dex_cache);
3885 }
3886
AppendToBootClassPath(const DexFile * dex_file,ObjPtr<mirror::DexCache> dex_cache)3887 void ClassLinker::AppendToBootClassPath(const DexFile* dex_file,
3888 ObjPtr<mirror::DexCache> dex_cache) {
3889 CHECK(dex_file != nullptr);
3890 CHECK(dex_cache != nullptr) << dex_file->GetLocation();
3891 boot_class_path_.push_back(dex_file);
3892 WriterMutexLock mu(Thread::Current(), *Locks::dex_lock_);
3893 RegisterDexFileLocked(*dex_file, dex_cache, /* class_loader= */ nullptr);
3894 }
3895
RegisterDexFileLocked(const DexFile & dex_file,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader)3896 void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file,
3897 ObjPtr<mirror::DexCache> dex_cache,
3898 ObjPtr<mirror::ClassLoader> class_loader) {
3899 Thread* const self = Thread::Current();
3900 Locks::dex_lock_->AssertExclusiveHeld(self);
3901 CHECK(dex_cache != nullptr) << dex_file.GetLocation();
3902 CHECK_EQ(dex_cache->GetDexFile(), &dex_file) << dex_file.GetLocation();
3903 // For app images, the dex cache location may be a suffix of the dex file location since the
3904 // dex file location is an absolute path.
3905 const std::string dex_cache_location = dex_cache->GetLocation()->ToModifiedUtf8();
3906 const size_t dex_cache_length = dex_cache_location.length();
3907 CHECK_GT(dex_cache_length, 0u) << dex_file.GetLocation();
3908 std::string dex_file_location = dex_file.GetLocation();
3909 // The following paths checks don't work on preopt when using boot dex files, where the dex
3910 // cache location is the one on device, and the dex_file's location is the one on host.
3911 if (!(Runtime::Current()->IsAotCompiler() && class_loader == nullptr && !kIsTargetBuild)) {
3912 CHECK_GE(dex_file_location.length(), dex_cache_length)
3913 << dex_cache_location << " " << dex_file.GetLocation();
3914 const std::string dex_file_suffix = dex_file_location.substr(
3915 dex_file_location.length() - dex_cache_length,
3916 dex_cache_length);
3917 // Example dex_cache location is SettingsProvider.apk and
3918 // dex file location is /system/priv-app/SettingsProvider/SettingsProvider.apk
3919 CHECK_EQ(dex_cache_location, dex_file_suffix);
3920 }
3921 const OatFile* oat_file =
3922 (dex_file.GetOatDexFile() != nullptr) ? dex_file.GetOatDexFile()->GetOatFile() : nullptr;
3923 // Clean up pass to remove null dex caches; null dex caches can occur due to class unloading
3924 // and we are lazily removing null entries. Also check if we need to initialize OatFile data
3925 // (.data.bimg.rel.ro and .bss sections) needed for code execution.
3926 bool initialize_oat_file_data = (oat_file != nullptr) && oat_file->IsExecutable();
3927 JavaVMExt* const vm = self->GetJniEnv()->GetVm();
3928 for (auto it = dex_caches_.begin(); it != dex_caches_.end(); ) {
3929 DexCacheData data = *it;
3930 if (self->IsJWeakCleared(data.weak_root)) {
3931 vm->DeleteWeakGlobalRef(self, data.weak_root);
3932 it = dex_caches_.erase(it);
3933 } else {
3934 if (initialize_oat_file_data &&
3935 it->dex_file->GetOatDexFile() != nullptr &&
3936 it->dex_file->GetOatDexFile()->GetOatFile() == oat_file) {
3937 initialize_oat_file_data = false; // Already initialized.
3938 }
3939 ++it;
3940 }
3941 }
3942 if (initialize_oat_file_data) {
3943 oat_file->InitializeRelocations();
3944 }
3945 // Let hiddenapi assign a domain to the newly registered dex file.
3946 hiddenapi::InitializeDexFileDomain(dex_file, class_loader);
3947
3948 jweak dex_cache_jweak = vm->AddWeakGlobalRef(self, dex_cache);
3949 DexCacheData data;
3950 data.weak_root = dex_cache_jweak;
3951 data.dex_file = dex_cache->GetDexFile();
3952 data.class_table = ClassTableForClassLoader(class_loader);
3953 AddNativeDebugInfoForDex(self, data.dex_file);
3954 DCHECK(data.class_table != nullptr);
3955 // Make sure to hold the dex cache live in the class table. This case happens for the boot class
3956 // path dex caches without an image.
3957 data.class_table->InsertStrongRoot(dex_cache);
3958 // Make sure that the dex cache holds the classloader live.
3959 dex_cache->SetClassLoader(class_loader);
3960 if (class_loader != nullptr) {
3961 // Since we added a strong root to the class table, do the write barrier as required for
3962 // remembered sets and generational GCs.
3963 WriteBarrier::ForEveryFieldWrite(class_loader);
3964 }
3965 dex_caches_.push_back(data);
3966 }
3967
DecodeDexCacheLocked(Thread * self,const DexCacheData * data)3968 ObjPtr<mirror::DexCache> ClassLinker::DecodeDexCacheLocked(Thread* self, const DexCacheData* data) {
3969 return data != nullptr
3970 ? ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data->weak_root))
3971 : nullptr;
3972 }
3973
IsSameClassLoader(ObjPtr<mirror::DexCache> dex_cache,const DexCacheData * data,ObjPtr<mirror::ClassLoader> class_loader)3974 bool ClassLinker::IsSameClassLoader(
3975 ObjPtr<mirror::DexCache> dex_cache,
3976 const DexCacheData* data,
3977 ObjPtr<mirror::ClassLoader> class_loader) {
3978 CHECK(data != nullptr);
3979 DCHECK_EQ(dex_cache->GetDexFile(), data->dex_file);
3980 return data->class_table == ClassTableForClassLoader(class_loader);
3981 }
3982
RegisterExistingDexCache(ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader)3983 void ClassLinker::RegisterExistingDexCache(ObjPtr<mirror::DexCache> dex_cache,
3984 ObjPtr<mirror::ClassLoader> class_loader) {
3985 SCOPED_TRACE << __FUNCTION__ << " " << dex_cache->GetDexFile()->GetLocation();
3986 Thread* self = Thread::Current();
3987 StackHandleScope<2> hs(self);
3988 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(dex_cache));
3989 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
3990 const DexFile* dex_file = dex_cache->GetDexFile();
3991 DCHECK(dex_file != nullptr) << "Attempt to register uninitialized dex_cache object!";
3992 if (kIsDebugBuild) {
3993 ReaderMutexLock mu(self, *Locks::dex_lock_);
3994 const DexCacheData* old_data = FindDexCacheDataLocked(*dex_file);
3995 ObjPtr<mirror::DexCache> old_dex_cache = DecodeDexCacheLocked(self, old_data);
3996 DCHECK(old_dex_cache.IsNull()) << "Attempt to manually register a dex cache thats already "
3997 << "been registered on dex file " << dex_file->GetLocation();
3998 }
3999 ClassTable* table;
4000 {
4001 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
4002 table = InsertClassTableForClassLoader(h_class_loader.Get());
4003 }
4004 // Avoid a deadlock between a garbage collecting thread running a checkpoint,
4005 // a thread holding the dex lock and blocking on a condition variable regarding
4006 // weak references access, and a thread blocking on the dex lock.
4007 gc::ScopedGCCriticalSection gcs(self, gc::kGcCauseClassLinker, gc::kCollectorTypeClassLinker);
4008 WriterMutexLock mu(self, *Locks::dex_lock_);
4009 RegisterDexFileLocked(*dex_file, h_dex_cache.Get(), h_class_loader.Get());
4010 table->InsertStrongRoot(h_dex_cache.Get());
4011 if (h_class_loader.Get() != nullptr) {
4012 // Since we added a strong root to the class table, do the write barrier as required for
4013 // remembered sets and generational GCs.
4014 WriteBarrier::ForEveryFieldWrite(h_class_loader.Get());
4015 }
4016 }
4017
ThrowDexFileAlreadyRegisteredError(Thread * self,const DexFile & dex_file)4018 static void ThrowDexFileAlreadyRegisteredError(Thread* self, const DexFile& dex_file)
4019 REQUIRES_SHARED(Locks::mutator_lock_) {
4020 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
4021 "Attempt to register dex file %s with multiple class loaders",
4022 dex_file.GetLocation().c_str());
4023 }
4024
RegisterDexFile(const DexFile & dex_file,ObjPtr<mirror::ClassLoader> class_loader)4025 ObjPtr<mirror::DexCache> ClassLinker::RegisterDexFile(const DexFile& dex_file,
4026 ObjPtr<mirror::ClassLoader> class_loader) {
4027 Thread* self = Thread::Current();
4028 ObjPtr<mirror::DexCache> old_dex_cache;
4029 bool registered_with_another_class_loader = false;
4030 {
4031 ReaderMutexLock mu(self, *Locks::dex_lock_);
4032 const DexCacheData* old_data = FindDexCacheDataLocked(dex_file);
4033 old_dex_cache = DecodeDexCacheLocked(self, old_data);
4034 if (old_dex_cache != nullptr) {
4035 if (IsSameClassLoader(old_dex_cache, old_data, class_loader)) {
4036 return old_dex_cache;
4037 } else {
4038 // TODO This is not very clean looking. Should maybe try to make a way to request exceptions
4039 // be thrown when it's safe to do so to simplify this.
4040 registered_with_another_class_loader = true;
4041 }
4042 }
4043 }
4044 // We need to have released the dex_lock_ to allocate safely.
4045 if (registered_with_another_class_loader) {
4046 ThrowDexFileAlreadyRegisteredError(self, dex_file);
4047 return nullptr;
4048 }
4049 SCOPED_TRACE << __FUNCTION__ << " " << dex_file.GetLocation();
4050 LinearAlloc* const linear_alloc = GetOrCreateAllocatorForClassLoader(class_loader);
4051 DCHECK(linear_alloc != nullptr);
4052 ClassTable* table;
4053 {
4054 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
4055 table = InsertClassTableForClassLoader(class_loader);
4056 }
4057 // Don't alloc while holding the lock, since allocation may need to
4058 // suspend all threads and another thread may need the dex_lock_ to
4059 // get to a suspend point.
4060 StackHandleScope<3> hs(self);
4061 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
4062 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(AllocDexCache(self, dex_file)));
4063 {
4064 // Avoid a deadlock between a garbage collecting thread running a checkpoint,
4065 // a thread holding the dex lock and blocking on a condition variable regarding
4066 // weak references access, and a thread blocking on the dex lock.
4067 gc::ScopedGCCriticalSection gcs(self, gc::kGcCauseClassLinker, gc::kCollectorTypeClassLinker);
4068 WriterMutexLock mu(self, *Locks::dex_lock_);
4069 const DexCacheData* old_data = FindDexCacheDataLocked(dex_file);
4070 old_dex_cache = DecodeDexCacheLocked(self, old_data);
4071 if (old_dex_cache == nullptr && h_dex_cache != nullptr) {
4072 // Do InitializeNativeFields while holding dex lock to make sure two threads don't call it
4073 // at the same time with the same dex cache. Since the .bss is shared this can cause failing
4074 // DCHECK that the arrays are null.
4075 h_dex_cache->InitializeNativeFields(&dex_file, linear_alloc);
4076 RegisterDexFileLocked(dex_file, h_dex_cache.Get(), h_class_loader.Get());
4077 }
4078 if (old_dex_cache != nullptr) {
4079 // Another thread managed to initialize the dex cache faster, so use that DexCache.
4080 // If this thread encountered OOME, ignore it.
4081 DCHECK_EQ(h_dex_cache == nullptr, self->IsExceptionPending());
4082 self->ClearException();
4083 // We cannot call EnsureSameClassLoader() or allocate an exception while holding the
4084 // dex_lock_.
4085 if (IsSameClassLoader(old_dex_cache, old_data, h_class_loader.Get())) {
4086 return old_dex_cache;
4087 } else {
4088 registered_with_another_class_loader = true;
4089 }
4090 }
4091 }
4092 if (registered_with_another_class_loader) {
4093 ThrowDexFileAlreadyRegisteredError(self, dex_file);
4094 return nullptr;
4095 }
4096 if (h_dex_cache == nullptr) {
4097 self->AssertPendingOOMException();
4098 return nullptr;
4099 }
4100 table->InsertStrongRoot(h_dex_cache.Get());
4101 if (h_class_loader.Get() != nullptr) {
4102 // Since we added a strong root to the class table, do the write barrier as required for
4103 // remembered sets and generational GCs.
4104 WriteBarrier::ForEveryFieldWrite(h_class_loader.Get());
4105 }
4106 VLOG(class_linker) << "Registered dex file " << dex_file.GetLocation();
4107 PaletteNotifyDexFileLoaded(dex_file.GetLocation().c_str());
4108 return h_dex_cache.Get();
4109 }
4110
IsDexFileRegistered(Thread * self,const DexFile & dex_file)4111 bool ClassLinker::IsDexFileRegistered(Thread* self, const DexFile& dex_file) {
4112 ReaderMutexLock mu(self, *Locks::dex_lock_);
4113 return DecodeDexCacheLocked(self, FindDexCacheDataLocked(dex_file)) != nullptr;
4114 }
4115
FindDexCache(Thread * self,const DexFile & dex_file)4116 ObjPtr<mirror::DexCache> ClassLinker::FindDexCache(Thread* self, const DexFile& dex_file) {
4117 ReaderMutexLock mu(self, *Locks::dex_lock_);
4118 const DexCacheData* dex_cache_data = FindDexCacheDataLocked(dex_file);
4119 ObjPtr<mirror::DexCache> dex_cache = DecodeDexCacheLocked(self, dex_cache_data);
4120 if (dex_cache != nullptr) {
4121 return dex_cache;
4122 }
4123 // Failure, dump diagnostic and abort.
4124 for (const DexCacheData& data : dex_caches_) {
4125 if (DecodeDexCacheLocked(self, &data) != nullptr) {
4126 LOG(FATAL_WITHOUT_ABORT) << "Registered dex file " << data.dex_file->GetLocation();
4127 }
4128 }
4129 LOG(FATAL) << "Failed to find DexCache for DexFile " << dex_file.GetLocation()
4130 << " " << &dex_file << " " << dex_cache_data->dex_file;
4131 UNREACHABLE();
4132 }
4133
FindClassTable(Thread * self,ObjPtr<mirror::DexCache> dex_cache)4134 ClassTable* ClassLinker::FindClassTable(Thread* self, ObjPtr<mirror::DexCache> dex_cache) {
4135 const DexFile* dex_file = dex_cache->GetDexFile();
4136 DCHECK(dex_file != nullptr);
4137 ReaderMutexLock mu(self, *Locks::dex_lock_);
4138 // Search assuming unique-ness of dex file.
4139 for (const DexCacheData& data : dex_caches_) {
4140 // Avoid decoding (and read barriers) other unrelated dex caches.
4141 if (data.dex_file == dex_file) {
4142 ObjPtr<mirror::DexCache> registered_dex_cache = DecodeDexCacheLocked(self, &data);
4143 if (registered_dex_cache != nullptr) {
4144 CHECK_EQ(registered_dex_cache, dex_cache) << dex_file->GetLocation();
4145 return data.class_table;
4146 }
4147 }
4148 }
4149 return nullptr;
4150 }
4151
FindDexCacheDataLocked(const DexFile & dex_file)4152 const ClassLinker::DexCacheData* ClassLinker::FindDexCacheDataLocked(const DexFile& dex_file) {
4153 // Search assuming unique-ness of dex file.
4154 for (const DexCacheData& data : dex_caches_) {
4155 // Avoid decoding (and read barriers) other unrelated dex caches.
4156 if (data.dex_file == &dex_file) {
4157 return &data;
4158 }
4159 }
4160 return nullptr;
4161 }
4162
CreatePrimitiveClass(Thread * self,Primitive::Type type,ClassRoot primitive_root)4163 void ClassLinker::CreatePrimitiveClass(Thread* self,
4164 Primitive::Type type,
4165 ClassRoot primitive_root) {
4166 ObjPtr<mirror::Class> primitive_class =
4167 AllocClass(self, mirror::Class::PrimitiveClassSize(image_pointer_size_));
4168 CHECK(primitive_class != nullptr) << "OOM for primitive class " << type;
4169 // Do not hold lock on the primitive class object, the initialization of
4170 // primitive classes is done while the process is still single threaded.
4171 primitive_class->SetAccessFlagsDuringLinking(
4172 kAccPublic | kAccFinal | kAccAbstract | kAccVerificationAttempted);
4173 primitive_class->SetPrimitiveType(type);
4174 primitive_class->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable());
4175 // Skip EnsureSkipAccessChecksMethods(). We can skip the verified status,
4176 // the kAccVerificationAttempted flag was added above, and there are no
4177 // methods that need the kAccSkipAccessChecks flag.
4178 DCHECK_EQ(primitive_class->NumMethods(), 0u);
4179 // Primitive classes are initialized during single threaded startup, so visibly initialized.
4180 primitive_class->SetStatusForPrimitiveOrArray(ClassStatus::kVisiblyInitialized);
4181 const char* descriptor = Primitive::Descriptor(type);
4182 ObjPtr<mirror::Class> existing = InsertClass(descriptor,
4183 primitive_class,
4184 ComputeModifiedUtf8Hash(descriptor));
4185 CHECK(existing == nullptr) << "InitPrimitiveClass(" << type << ") failed";
4186 SetClassRoot(primitive_root, primitive_class);
4187 }
4188
GetArrayIfTable()4189 inline ObjPtr<mirror::IfTable> ClassLinker::GetArrayIfTable() {
4190 return GetClassRoot<mirror::ObjectArray<mirror::Object>>(this)->GetIfTable();
4191 }
4192
4193 // Create an array class (i.e. the class object for the array, not the
4194 // array itself). "descriptor" looks like "[C" or "[[[[B" or
4195 // "[Ljava/lang/String;".
4196 //
4197 // If "descriptor" refers to an array of primitives, look up the
4198 // primitive type's internally-generated class object.
4199 //
4200 // "class_loader" is the class loader of the class that's referring to
4201 // us. It's used to ensure that we're looking for the element type in
4202 // the right context. It does NOT become the class loader for the
4203 // array class; that always comes from the base element class.
4204 //
4205 // Returns null with an exception raised on failure.
CreateArrayClass(Thread * self,const char * descriptor,size_t hash,Handle<mirror::ClassLoader> class_loader)4206 ObjPtr<mirror::Class> ClassLinker::CreateArrayClass(Thread* self,
4207 const char* descriptor,
4208 size_t hash,
4209 Handle<mirror::ClassLoader> class_loader) {
4210 // Identify the underlying component type
4211 CHECK_EQ('[', descriptor[0]);
4212 StackHandleScope<2> hs(self);
4213
4214 // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied
4215 // code to be executed. We put it up here so we can avoid all the allocations associated with
4216 // creating the class. This can happen with (eg) jit threads.
4217 if (!self->CanLoadClasses()) {
4218 // Make sure we don't try to load anything, potentially causing an infinite loop.
4219 ObjPtr<mirror::Throwable> pre_allocated =
4220 Runtime::Current()->GetPreAllocatedNoClassDefFoundError();
4221 self->SetException(pre_allocated);
4222 return nullptr;
4223 }
4224
4225 MutableHandle<mirror::Class> component_type(hs.NewHandle(FindClass(self, descriptor + 1,
4226 class_loader)));
4227 if (component_type == nullptr) {
4228 DCHECK(self->IsExceptionPending());
4229 // We need to accept erroneous classes as component types.
4230 const size_t component_hash = ComputeModifiedUtf8Hash(descriptor + 1);
4231 component_type.Assign(LookupClass(self, descriptor + 1, component_hash, class_loader.Get()));
4232 if (component_type == nullptr) {
4233 DCHECK(self->IsExceptionPending());
4234 return nullptr;
4235 } else {
4236 self->ClearException();
4237 }
4238 }
4239 if (UNLIKELY(component_type->IsPrimitiveVoid())) {
4240 ThrowNoClassDefFoundError("Attempt to create array of void primitive type");
4241 return nullptr;
4242 }
4243 // See if the component type is already loaded. Array classes are
4244 // always associated with the class loader of their underlying
4245 // element type -- an array of Strings goes with the loader for
4246 // java/lang/String -- so we need to look for it there. (The
4247 // caller should have checked for the existence of the class
4248 // before calling here, but they did so with *their* class loader,
4249 // not the component type's loader.)
4250 //
4251 // If we find it, the caller adds "loader" to the class' initiating
4252 // loader list, which should prevent us from going through this again.
4253 //
4254 // This call is unnecessary if "loader" and "component_type->GetClassLoader()"
4255 // are the same, because our caller (FindClass) just did the
4256 // lookup. (Even if we get this wrong we still have correct behavior,
4257 // because we effectively do this lookup again when we add the new
4258 // class to the hash table --- necessary because of possible races with
4259 // other threads.)
4260 if (class_loader.Get() != component_type->GetClassLoader()) {
4261 ObjPtr<mirror::Class> new_class =
4262 LookupClass(self, descriptor, hash, component_type->GetClassLoader());
4263 if (new_class != nullptr) {
4264 return new_class;
4265 }
4266 }
4267 // Core array classes, i.e. Object[], Class[], String[] and primitive
4268 // arrays, have special initialization and they should be found above.
4269 DCHECK(!component_type->IsObjectClass() ||
4270 // Guard from false positives for errors before setting superclass.
4271 component_type->IsErroneousUnresolved());
4272 DCHECK(!component_type->IsStringClass());
4273 DCHECK(!component_type->IsClassClass());
4274 DCHECK(!component_type->IsPrimitive());
4275
4276 // Fill out the fields in the Class.
4277 //
4278 // It is possible to execute some methods against arrays, because
4279 // all arrays are subclasses of java_lang_Object_, so we need to set
4280 // up a vtable. We can just point at the one in java_lang_Object_.
4281 //
4282 // Array classes are simple enough that we don't need to do a full
4283 // link step.
4284 size_t array_class_size = mirror::Array::ClassSize(image_pointer_size_);
4285 auto visitor = [this, array_class_size, component_type](ObjPtr<mirror::Object> obj,
4286 size_t usable_size)
4287 REQUIRES_SHARED(Locks::mutator_lock_) {
4288 ScopedAssertNoNewTransactionRecords sanntr("CreateArrayClass");
4289 mirror::Class::InitializeClassVisitor init_class(array_class_size);
4290 init_class(obj, usable_size);
4291 ObjPtr<mirror::Class> klass = ObjPtr<mirror::Class>::DownCast(obj);
4292 klass->SetComponentType(component_type.Get());
4293 // Do not hold lock for initialization, the fence issued after the visitor
4294 // returns ensures memory visibility together with the implicit consume
4295 // semantics (for all supported architectures) for any thread that loads
4296 // the array class reference from any memory locations afterwards.
4297 FinishArrayClassSetup(klass);
4298 };
4299 auto new_class = hs.NewHandle<mirror::Class>(
4300 AllocClass(self, GetClassRoot<mirror::Class>(this), array_class_size, visitor));
4301 if (new_class == nullptr) {
4302 self->AssertPendingOOMException();
4303 return nullptr;
4304 }
4305
4306 ObjPtr<mirror::Class> existing = InsertClass(descriptor, new_class.Get(), hash);
4307 if (existing == nullptr) {
4308 // We postpone ClassLoad and ClassPrepare events to this point in time to avoid
4309 // duplicate events in case of races. Array classes don't really follow dedicated
4310 // load and prepare, anyways.
4311 Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(new_class);
4312 Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(new_class, new_class);
4313
4314 jit::Jit::NewTypeLoadedIfUsingJit(new_class.Get());
4315 return new_class.Get();
4316 }
4317 // Another thread must have loaded the class after we
4318 // started but before we finished. Abandon what we've
4319 // done.
4320 //
4321 // (Yes, this happens.)
4322
4323 return existing;
4324 }
4325
LookupPrimitiveClass(char type)4326 ObjPtr<mirror::Class> ClassLinker::LookupPrimitiveClass(char type) {
4327 ClassRoot class_root;
4328 switch (type) {
4329 case 'B': class_root = ClassRoot::kPrimitiveByte; break;
4330 case 'C': class_root = ClassRoot::kPrimitiveChar; break;
4331 case 'D': class_root = ClassRoot::kPrimitiveDouble; break;
4332 case 'F': class_root = ClassRoot::kPrimitiveFloat; break;
4333 case 'I': class_root = ClassRoot::kPrimitiveInt; break;
4334 case 'J': class_root = ClassRoot::kPrimitiveLong; break;
4335 case 'S': class_root = ClassRoot::kPrimitiveShort; break;
4336 case 'Z': class_root = ClassRoot::kPrimitiveBoolean; break;
4337 case 'V': class_root = ClassRoot::kPrimitiveVoid; break;
4338 default:
4339 return nullptr;
4340 }
4341 return GetClassRoot(class_root, this);
4342 }
4343
FindPrimitiveClass(char type)4344 ObjPtr<mirror::Class> ClassLinker::FindPrimitiveClass(char type) {
4345 ObjPtr<mirror::Class> result = LookupPrimitiveClass(type);
4346 if (UNLIKELY(result == nullptr)) {
4347 std::string printable_type(PrintableChar(type));
4348 ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str());
4349 }
4350 return result;
4351 }
4352
InsertClass(const char * descriptor,ObjPtr<mirror::Class> klass,size_t hash)4353 ObjPtr<mirror::Class> ClassLinker::InsertClass(const char* descriptor,
4354 ObjPtr<mirror::Class> klass,
4355 size_t hash) {
4356 DCHECK(Thread::Current()->CanLoadClasses());
4357 if (VLOG_IS_ON(class_linker)) {
4358 ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache();
4359 std::string source;
4360 if (dex_cache != nullptr) {
4361 source += " from ";
4362 source += dex_cache->GetLocation()->ToModifiedUtf8();
4363 }
4364 LOG(INFO) << "Loaded class " << descriptor << source;
4365 }
4366 {
4367 WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
4368 const ObjPtr<mirror::ClassLoader> class_loader = klass->GetClassLoader();
4369 ClassTable* const class_table = InsertClassTableForClassLoader(class_loader);
4370 ObjPtr<mirror::Class> existing = class_table->Lookup(descriptor, hash);
4371 if (existing != nullptr) {
4372 return existing;
4373 }
4374 VerifyObject(klass);
4375 class_table->InsertWithHash(klass, hash);
4376 if (class_loader != nullptr) {
4377 // This is necessary because we need to have the card dirtied for remembered sets.
4378 WriteBarrier::ForEveryFieldWrite(class_loader);
4379 }
4380 if (log_new_roots_) {
4381 new_class_roots_.push_back(GcRoot<mirror::Class>(klass));
4382 }
4383 }
4384 if (kIsDebugBuild) {
4385 // Test that copied methods correctly can find their holder.
4386 for (ArtMethod& method : klass->GetCopiedMethods(image_pointer_size_)) {
4387 CHECK_EQ(GetHoldingClassOfCopiedMethod(&method), klass);
4388 }
4389 }
4390 return nullptr;
4391 }
4392
WriteBarrierForBootOatFileBssRoots(const OatFile * oat_file)4393 void ClassLinker::WriteBarrierForBootOatFileBssRoots(const OatFile* oat_file) {
4394 WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
4395 DCHECK(!oat_file->GetBssGcRoots().empty()) << oat_file->GetLocation();
4396 if (log_new_roots_ && !ContainsElement(new_bss_roots_boot_oat_files_, oat_file)) {
4397 new_bss_roots_boot_oat_files_.push_back(oat_file);
4398 }
4399 }
4400
4401 // TODO This should really be in mirror::Class.
UpdateClassMethods(ObjPtr<mirror::Class> klass,LengthPrefixedArray<ArtMethod> * new_methods)4402 void ClassLinker::UpdateClassMethods(ObjPtr<mirror::Class> klass,
4403 LengthPrefixedArray<ArtMethod>* new_methods) {
4404 klass->SetMethodsPtrUnchecked(new_methods,
4405 klass->NumDirectMethods(),
4406 klass->NumDeclaredVirtualMethods());
4407 // Need to mark the card so that the remembered sets and mod union tables get updated.
4408 WriteBarrier::ForEveryFieldWrite(klass);
4409 }
4410
LookupClass(Thread * self,const char * descriptor,ObjPtr<mirror::ClassLoader> class_loader)4411 ObjPtr<mirror::Class> ClassLinker::LookupClass(Thread* self,
4412 const char* descriptor,
4413 ObjPtr<mirror::ClassLoader> class_loader) {
4414 return LookupClass(self, descriptor, ComputeModifiedUtf8Hash(descriptor), class_loader);
4415 }
4416
LookupClass(Thread * self,const char * descriptor,size_t hash,ObjPtr<mirror::ClassLoader> class_loader)4417 ObjPtr<mirror::Class> ClassLinker::LookupClass(Thread* self,
4418 const char* descriptor,
4419 size_t hash,
4420 ObjPtr<mirror::ClassLoader> class_loader) {
4421 ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_);
4422 ClassTable* const class_table = ClassTableForClassLoader(class_loader);
4423 if (class_table != nullptr) {
4424 ObjPtr<mirror::Class> result = class_table->Lookup(descriptor, hash);
4425 if (result != nullptr) {
4426 return result;
4427 }
4428 }
4429 return nullptr;
4430 }
4431
4432 class MoveClassTableToPreZygoteVisitor : public ClassLoaderVisitor {
4433 public:
MoveClassTableToPreZygoteVisitor()4434 MoveClassTableToPreZygoteVisitor() {}
4435
Visit(ObjPtr<mirror::ClassLoader> class_loader)4436 void Visit(ObjPtr<mirror::ClassLoader> class_loader)
4437 REQUIRES(Locks::classlinker_classes_lock_)
4438 REQUIRES_SHARED(Locks::mutator_lock_) override {
4439 ClassTable* const class_table = class_loader->GetClassTable();
4440 if (class_table != nullptr) {
4441 class_table->FreezeSnapshot();
4442 }
4443 }
4444 };
4445
MoveClassTableToPreZygote()4446 void ClassLinker::MoveClassTableToPreZygote() {
4447 WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
4448 boot_class_table_->FreezeSnapshot();
4449 MoveClassTableToPreZygoteVisitor visitor;
4450 VisitClassLoaders(&visitor);
4451 }
4452
4453 // Look up classes by hash and descriptor and put all matching ones in the result array.
4454 class LookupClassesVisitor : public ClassLoaderVisitor {
4455 public:
LookupClassesVisitor(const char * descriptor,size_t hash,std::vector<ObjPtr<mirror::Class>> * result)4456 LookupClassesVisitor(const char* descriptor,
4457 size_t hash,
4458 std::vector<ObjPtr<mirror::Class>>* result)
4459 : descriptor_(descriptor),
4460 hash_(hash),
4461 result_(result) {}
4462
Visit(ObjPtr<mirror::ClassLoader> class_loader)4463 void Visit(ObjPtr<mirror::ClassLoader> class_loader)
4464 REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override {
4465 ClassTable* const class_table = class_loader->GetClassTable();
4466 ObjPtr<mirror::Class> klass = class_table->Lookup(descriptor_, hash_);
4467 // Add `klass` only if `class_loader` is its defining (not just initiating) class loader.
4468 if (klass != nullptr && klass->GetClassLoader() == class_loader) {
4469 result_->push_back(klass);
4470 }
4471 }
4472
4473 private:
4474 const char* const descriptor_;
4475 const size_t hash_;
4476 std::vector<ObjPtr<mirror::Class>>* const result_;
4477 };
4478
LookupClasses(const char * descriptor,std::vector<ObjPtr<mirror::Class>> & result)4479 void ClassLinker::LookupClasses(const char* descriptor,
4480 std::vector<ObjPtr<mirror::Class>>& result) {
4481 result.clear();
4482 Thread* const self = Thread::Current();
4483 ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_);
4484 const size_t hash = ComputeModifiedUtf8Hash(descriptor);
4485 ObjPtr<mirror::Class> klass = boot_class_table_->Lookup(descriptor, hash);
4486 if (klass != nullptr) {
4487 DCHECK(klass->GetClassLoader() == nullptr);
4488 result.push_back(klass);
4489 }
4490 LookupClassesVisitor visitor(descriptor, hash, &result);
4491 VisitClassLoaders(&visitor);
4492 }
4493
AttemptSupertypeVerification(Thread * self,verifier::VerifierDeps * verifier_deps,Handle<mirror::Class> klass,Handle<mirror::Class> supertype)4494 bool ClassLinker::AttemptSupertypeVerification(Thread* self,
4495 verifier::VerifierDeps* verifier_deps,
4496 Handle<mirror::Class> klass,
4497 Handle<mirror::Class> supertype) {
4498 DCHECK(self != nullptr);
4499 DCHECK(klass != nullptr);
4500 DCHECK(supertype != nullptr);
4501
4502 if (!supertype->IsVerified() && !supertype->IsErroneous()) {
4503 VerifyClass(self, verifier_deps, supertype);
4504 }
4505
4506 if (supertype->IsVerified()
4507 || supertype->ShouldVerifyAtRuntime()
4508 || supertype->IsVerifiedNeedsAccessChecks()) {
4509 // The supertype is either verified, or we soft failed at AOT time.
4510 DCHECK(supertype->IsVerified() || Runtime::Current()->IsAotCompiler());
4511 return true;
4512 }
4513 // If we got this far then we have a hard failure.
4514 std::string error_msg =
4515 StringPrintf("Rejecting class %s that attempts to sub-type erroneous class %s",
4516 klass->PrettyDescriptor().c_str(),
4517 supertype->PrettyDescriptor().c_str());
4518 LOG(WARNING) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8();
4519 StackHandleScope<1> hs(self);
4520 Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException()));
4521 if (cause != nullptr) {
4522 // Set during VerifyClass call (if at all).
4523 self->ClearException();
4524 }
4525 // Change into a verify error.
4526 ThrowVerifyError(klass.Get(), "%s", error_msg.c_str());
4527 if (cause != nullptr) {
4528 self->GetException()->SetCause(cause.Get());
4529 }
4530 ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex());
4531 if (Runtime::Current()->IsAotCompiler()) {
4532 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
4533 }
4534 // Need to grab the lock to change status.
4535 ObjectLock<mirror::Class> super_lock(self, klass);
4536 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
4537 return false;
4538 }
4539
VerifyClass(Thread * self,verifier::VerifierDeps * verifier_deps,Handle<mirror::Class> klass,verifier::HardFailLogMode log_level)4540 verifier::FailureKind ClassLinker::VerifyClass(Thread* self,
4541 verifier::VerifierDeps* verifier_deps,
4542 Handle<mirror::Class> klass,
4543 verifier::HardFailLogMode log_level) {
4544 {
4545 // TODO: assert that the monitor on the Class is held
4546 ObjectLock<mirror::Class> lock(self, klass);
4547
4548 // Is somebody verifying this now?
4549 ClassStatus old_status = klass->GetStatus();
4550 while (old_status == ClassStatus::kVerifying) {
4551 lock.WaitIgnoringInterrupts();
4552 // WaitIgnoringInterrupts can still receive an interrupt and return early, in this
4553 // case we may see the same status again. b/62912904. This is why the check is
4554 // greater or equal.
4555 CHECK(klass->IsErroneous() || (klass->GetStatus() >= old_status))
4556 << "Class '" << klass->PrettyClass()
4557 << "' performed an illegal verification state transition from " << old_status
4558 << " to " << klass->GetStatus();
4559 old_status = klass->GetStatus();
4560 }
4561
4562 // The class might already be erroneous, for example at compile time if we attempted to verify
4563 // this class as a parent to another.
4564 if (klass->IsErroneous()) {
4565 ThrowEarlierClassFailure(klass.Get());
4566 return verifier::FailureKind::kHardFailure;
4567 }
4568
4569 // Don't attempt to re-verify if already verified.
4570 if (klass->IsVerified()) {
4571 EnsureSkipAccessChecksMethods(klass, image_pointer_size_);
4572 if (verifier_deps != nullptr &&
4573 verifier_deps->ContainsDexFile(klass->GetDexFile()) &&
4574 !verifier_deps->HasRecordedVerifiedStatus(klass->GetDexFile(), *klass->GetClassDef()) &&
4575 !Runtime::Current()->IsAotCompiler()) {
4576 // If the klass is verified, but `verifier_deps` did not record it, this
4577 // means we are running background verification of a secondary dex file.
4578 // Re-run the verifier to populate `verifier_deps`.
4579 // No need to run the verification when running on the AOT Compiler, as
4580 // the driver handles those multithreaded cases already.
4581 std::string error_msg;
4582 verifier::FailureKind failure =
4583 PerformClassVerification(self, verifier_deps, klass, log_level, &error_msg);
4584 // We could have soft failures, so just check that we don't have a hard
4585 // failure.
4586 DCHECK_NE(failure, verifier::FailureKind::kHardFailure) << error_msg;
4587 }
4588 return verifier::FailureKind::kNoFailure;
4589 }
4590
4591 if (klass->IsVerifiedNeedsAccessChecks()) {
4592 if (!Runtime::Current()->IsAotCompiler()) {
4593 // Mark the class as having a verification attempt to avoid re-running
4594 // the verifier and avoid calling EnsureSkipAccessChecksMethods.
4595 klass->SetVerificationAttempted();
4596 mirror::Class::SetStatus(klass, ClassStatus::kVerified, self);
4597 }
4598 return verifier::FailureKind::kAccessChecksFailure;
4599 }
4600
4601 // For AOT, don't attempt to re-verify if we have already found we should
4602 // verify at runtime.
4603 if (klass->ShouldVerifyAtRuntime()) {
4604 CHECK(Runtime::Current()->IsAotCompiler());
4605 return verifier::FailureKind::kSoftFailure;
4606 }
4607
4608 DCHECK_EQ(klass->GetStatus(), ClassStatus::kResolved);
4609 mirror::Class::SetStatus(klass, ClassStatus::kVerifying, self);
4610
4611 // Skip verification if disabled.
4612 if (!Runtime::Current()->IsVerificationEnabled()) {
4613 mirror::Class::SetStatus(klass, ClassStatus::kVerified, self);
4614 EnsureSkipAccessChecksMethods(klass, image_pointer_size_);
4615 return verifier::FailureKind::kNoFailure;
4616 }
4617 }
4618
4619 VLOG(class_linker) << "Beginning verification for class: "
4620 << klass->PrettyDescriptor()
4621 << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8();
4622
4623 // Verify super class.
4624 StackHandleScope<2> hs(self);
4625 MutableHandle<mirror::Class> supertype(hs.NewHandle(klass->GetSuperClass()));
4626 // If we have a superclass and we get a hard verification failure we can return immediately.
4627 if (supertype != nullptr &&
4628 !AttemptSupertypeVerification(self, verifier_deps, klass, supertype)) {
4629 CHECK(self->IsExceptionPending()) << "Verification error should be pending.";
4630 return verifier::FailureKind::kHardFailure;
4631 }
4632
4633 // Verify all default super-interfaces.
4634 //
4635 // (1) Don't bother if the superclass has already had a soft verification failure.
4636 //
4637 // (2) Interfaces shouldn't bother to do this recursive verification because they cannot cause
4638 // recursive initialization by themselves. This is because when an interface is initialized
4639 // directly it must not initialize its superinterfaces. We are allowed to verify regardless
4640 // but choose not to for an optimization. If the interfaces is being verified due to a class
4641 // initialization (which would need all the default interfaces to be verified) the class code
4642 // will trigger the recursive verification anyway.
4643 if ((supertype == nullptr || supertype->IsVerified()) // See (1)
4644 && !klass->IsInterface()) { // See (2)
4645 int32_t iftable_count = klass->GetIfTableCount();
4646 MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr));
4647 // Loop through all interfaces this class has defined. It doesn't matter the order.
4648 for (int32_t i = 0; i < iftable_count; i++) {
4649 iface.Assign(klass->GetIfTable()->GetInterface(i));
4650 DCHECK(iface != nullptr);
4651 // We only care if we have default interfaces and can skip if we are already verified...
4652 if (LIKELY(!iface->HasDefaultMethods() || iface->IsVerified())) {
4653 continue;
4654 } else if (UNLIKELY(!AttemptSupertypeVerification(self, verifier_deps, klass, iface))) {
4655 // We had a hard failure while verifying this interface. Just return immediately.
4656 CHECK(self->IsExceptionPending()) << "Verification error should be pending.";
4657 return verifier::FailureKind::kHardFailure;
4658 } else if (UNLIKELY(!iface->IsVerified())) {
4659 // We softly failed to verify the iface. Stop checking and clean up.
4660 // Put the iface into the supertype handle so we know what caused us to fail.
4661 supertype.Assign(iface.Get());
4662 break;
4663 }
4664 }
4665 }
4666
4667 // At this point if verification failed, then supertype is the "first" supertype that failed
4668 // verification (without a specific order). If verification succeeded, then supertype is either
4669 // null or the original superclass of klass and is verified.
4670 DCHECK(supertype == nullptr ||
4671 supertype.Get() == klass->GetSuperClass() ||
4672 !supertype->IsVerified());
4673
4674 // Try to use verification information from the oat file, otherwise do runtime verification.
4675 const DexFile& dex_file = *klass->GetDexCache()->GetDexFile();
4676 ClassStatus oat_file_class_status(ClassStatus::kNotReady);
4677 bool preverified = VerifyClassUsingOatFile(self, dex_file, klass, oat_file_class_status);
4678
4679 VLOG(class_linker) << "Class preverified status for class "
4680 << klass->PrettyDescriptor()
4681 << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8()
4682 << ": "
4683 << preverified
4684 << "( " << oat_file_class_status << ")";
4685
4686 // If the oat file says the class had an error, re-run the verifier. That way we will get a
4687 // precise error message. To ensure a rerun, test:
4688 // mirror::Class::IsErroneous(oat_file_class_status) => !preverified
4689 DCHECK(!mirror::Class::IsErroneous(oat_file_class_status) || !preverified);
4690
4691 std::string error_msg;
4692 verifier::FailureKind verifier_failure = verifier::FailureKind::kNoFailure;
4693 if (!preverified) {
4694 verifier_failure = PerformClassVerification(self, verifier_deps, klass, log_level, &error_msg);
4695 }
4696
4697 // Verification is done, grab the lock again.
4698 ObjectLock<mirror::Class> lock(self, klass);
4699
4700 if (preverified || verifier_failure != verifier::FailureKind::kHardFailure) {
4701 if (!preverified && verifier_failure != verifier::FailureKind::kNoFailure) {
4702 VLOG(class_linker) << "Soft verification failure in class "
4703 << klass->PrettyDescriptor()
4704 << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8()
4705 << " because: " << error_msg;
4706 }
4707 self->AssertNoPendingException();
4708 // Make sure all classes referenced by catch blocks are resolved.
4709 ResolveClassExceptionHandlerTypes(klass);
4710 if (verifier_failure == verifier::FailureKind::kNoFailure) {
4711 // Even though there were no verifier failures we need to respect whether the super-class and
4712 // super-default-interfaces were verified or requiring runtime reverification.
4713 if (supertype == nullptr
4714 || supertype->IsVerified()
4715 || supertype->IsVerifiedNeedsAccessChecks()) {
4716 mirror::Class::SetStatus(klass, ClassStatus::kVerified, self);
4717 } else {
4718 CHECK(Runtime::Current()->IsAotCompiler());
4719 CHECK_EQ(supertype->GetStatus(), ClassStatus::kRetryVerificationAtRuntime);
4720 mirror::Class::SetStatus(klass, ClassStatus::kRetryVerificationAtRuntime, self);
4721 // Pretend a soft failure occurred so that we don't consider the class verified below.
4722 verifier_failure = verifier::FailureKind::kSoftFailure;
4723 }
4724 } else {
4725 CHECK(verifier_failure == verifier::FailureKind::kSoftFailure ||
4726 verifier_failure == verifier::FailureKind::kTypeChecksFailure ||
4727 verifier_failure == verifier::FailureKind::kAccessChecksFailure);
4728 // Soft failures at compile time should be retried at runtime. Soft
4729 // failures at runtime will be handled by slow paths in the generated
4730 // code. Set status accordingly.
4731 if (Runtime::Current()->IsAotCompiler()) {
4732 if (verifier_failure == verifier::FailureKind::kSoftFailure ||
4733 verifier_failure == verifier::FailureKind::kTypeChecksFailure) {
4734 mirror::Class::SetStatus(klass, ClassStatus::kRetryVerificationAtRuntime, self);
4735 } else {
4736 mirror::Class::SetStatus(klass, ClassStatus::kVerifiedNeedsAccessChecks, self);
4737 }
4738 } else {
4739 mirror::Class::SetStatus(klass, ClassStatus::kVerified, self);
4740 // As this is a fake verified status, make sure the methods are _not_ marked
4741 // kAccSkipAccessChecks later.
4742 klass->SetVerificationAttempted();
4743 }
4744 }
4745 } else {
4746 VLOG(verifier) << "Verification failed on class " << klass->PrettyDescriptor()
4747 << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8()
4748 << " because: " << error_msg;
4749 self->AssertNoPendingException();
4750 ThrowVerifyError(klass.Get(), "%s", error_msg.c_str());
4751 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
4752 }
4753 if (preverified || verifier_failure == verifier::FailureKind::kNoFailure) {
4754 if (oat_file_class_status == ClassStatus::kVerifiedNeedsAccessChecks ||
4755 UNLIKELY(Runtime::Current()->IsVerificationSoftFail())) {
4756 // Never skip access checks if the verification soft fail is forced.
4757 // Mark the class as having a verification attempt to avoid re-running the verifier.
4758 klass->SetVerificationAttempted();
4759 } else {
4760 // Class is verified so we don't need to do any access check on its methods.
4761 // Let the interpreter know it by setting the kAccSkipAccessChecks flag onto each
4762 // method.
4763 // Note: we're going here during compilation and at runtime. When we set the
4764 // kAccSkipAccessChecks flag when compiling image classes, the flag is recorded
4765 // in the image and is set when loading the image.
4766 EnsureSkipAccessChecksMethods(klass, image_pointer_size_);
4767 }
4768 }
4769 // Done verifying. Notify the compiler about the verification status, in case the class
4770 // was verified implicitly (eg super class of a compiled class).
4771 if (Runtime::Current()->IsAotCompiler()) {
4772 Runtime::Current()->GetCompilerCallbacks()->UpdateClassState(
4773 ClassReference(&klass->GetDexFile(), klass->GetDexClassDefIndex()), klass->GetStatus());
4774 }
4775 return verifier_failure;
4776 }
4777
PerformClassVerification(Thread * self,verifier::VerifierDeps * verifier_deps,Handle<mirror::Class> klass,verifier::HardFailLogMode log_level,std::string * error_msg)4778 verifier::FailureKind ClassLinker::PerformClassVerification(Thread* self,
4779 verifier::VerifierDeps* verifier_deps,
4780 Handle<mirror::Class> klass,
4781 verifier::HardFailLogMode log_level,
4782 std::string* error_msg) {
4783 Runtime* const runtime = Runtime::Current();
4784 return verifier::ClassVerifier::VerifyClass(self,
4785 verifier_deps,
4786 klass.Get(),
4787 runtime->GetCompilerCallbacks(),
4788 runtime->IsAotCompiler(),
4789 log_level,
4790 Runtime::Current()->GetTargetSdkVersion(),
4791 error_msg);
4792 }
4793
VerifyClassUsingOatFile(Thread * self,const DexFile & dex_file,Handle<mirror::Class> klass,ClassStatus & oat_file_class_status)4794 bool ClassLinker::VerifyClassUsingOatFile(Thread* self,
4795 const DexFile& dex_file,
4796 Handle<mirror::Class> klass,
4797 ClassStatus& oat_file_class_status) {
4798 // If we're compiling, we can only verify the class using the oat file if
4799 // we are not compiling the image or if the class we're verifying is not part of
4800 // the compilation unit (app - dependencies). We will let the compiler callback
4801 // tell us about the latter.
4802 if (Runtime::Current()->IsAotCompiler()) {
4803 CompilerCallbacks* callbacks = Runtime::Current()->GetCompilerCallbacks();
4804 // We are compiling an app (not the image).
4805 if (!callbacks->CanUseOatStatusForVerification(klass.Get())) {
4806 return false;
4807 }
4808 }
4809
4810 const OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
4811 // In case we run without an image there won't be a backing oat file.
4812 if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) {
4813 return false;
4814 }
4815
4816 uint16_t class_def_index = klass->GetDexClassDefIndex();
4817 oat_file_class_status = oat_dex_file->GetOatClass(class_def_index).GetStatus();
4818 if (oat_file_class_status >= ClassStatus::kVerified) {
4819 return true;
4820 }
4821 if (oat_file_class_status >= ClassStatus::kVerifiedNeedsAccessChecks) {
4822 // We return that the clas has already been verified, and the caller should
4823 // check the class status to ensure we run with access checks.
4824 return true;
4825 }
4826
4827 // Check the class status with the vdex file.
4828 const OatFile* oat_file = oat_dex_file->GetOatFile();
4829 if (oat_file != nullptr) {
4830 oat_file_class_status = oat_file->GetVdexFile()->ComputeClassStatus(self, klass);
4831 if (oat_file_class_status >= ClassStatus::kVerifiedNeedsAccessChecks) {
4832 return true;
4833 }
4834 }
4835
4836 // If we only verified a subset of the classes at compile time, we can end up with classes that
4837 // were resolved by the verifier.
4838 if (oat_file_class_status == ClassStatus::kResolved) {
4839 return false;
4840 }
4841 // We never expect a .oat file to have kRetryVerificationAtRuntime statuses.
4842 CHECK_NE(oat_file_class_status, ClassStatus::kRetryVerificationAtRuntime)
4843 << klass->PrettyClass() << " " << dex_file.GetLocation();
4844
4845 if (mirror::Class::IsErroneous(oat_file_class_status)) {
4846 // Compile time verification failed with a hard error. This is caused by invalid instructions
4847 // in the class. These errors are unrecoverable.
4848 return false;
4849 }
4850 if (oat_file_class_status == ClassStatus::kNotReady) {
4851 // Status is uninitialized if we couldn't determine the status at compile time, for example,
4852 // not loading the class.
4853 // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy
4854 // isn't a problem and this case shouldn't occur
4855 return false;
4856 }
4857 std::string temp;
4858 LOG(FATAL) << "Unexpected class status: " << oat_file_class_status
4859 << " " << dex_file.GetLocation() << " " << klass->PrettyClass() << " "
4860 << klass->GetDescriptor(&temp);
4861 UNREACHABLE();
4862 }
4863
ResolveClassExceptionHandlerTypes(Handle<mirror::Class> klass)4864 void ClassLinker::ResolveClassExceptionHandlerTypes(Handle<mirror::Class> klass) {
4865 for (ArtMethod& method : klass->GetMethods(image_pointer_size_)) {
4866 ResolveMethodExceptionHandlerTypes(&method);
4867 }
4868 }
4869
ResolveMethodExceptionHandlerTypes(ArtMethod * method)4870 void ClassLinker::ResolveMethodExceptionHandlerTypes(ArtMethod* method) {
4871 // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod.
4872 CodeItemDataAccessor accessor(method->DexInstructionData());
4873 if (!accessor.HasCodeItem()) {
4874 return; // native or abstract method
4875 }
4876 if (accessor.TriesSize() == 0) {
4877 return; // nothing to process
4878 }
4879 const uint8_t* handlers_ptr = accessor.GetCatchHandlerData(0);
4880 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
4881 for (uint32_t idx = 0; idx < handlers_size; idx++) {
4882 CatchHandlerIterator iterator(handlers_ptr);
4883 for (; iterator.HasNext(); iterator.Next()) {
4884 // Ensure exception types are resolved so that they don't need resolution to be delivered,
4885 // unresolved exception types will be ignored by exception delivery
4886 if (iterator.GetHandlerTypeIndex().IsValid()) {
4887 ObjPtr<mirror::Class> exception_type = ResolveType(iterator.GetHandlerTypeIndex(), method);
4888 if (exception_type == nullptr) {
4889 DCHECK(Thread::Current()->IsExceptionPending());
4890 Thread::Current()->ClearException();
4891 }
4892 }
4893 }
4894 handlers_ptr = iterator.EndDataPointer();
4895 }
4896 }
4897
CreateProxyClass(ScopedObjectAccessAlreadyRunnable & soa,jstring name,jobjectArray interfaces,jobject loader,jobjectArray methods,jobjectArray throws)4898 ObjPtr<mirror::Class> ClassLinker::CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa,
4899 jstring name,
4900 jobjectArray interfaces,
4901 jobject loader,
4902 jobjectArray methods,
4903 jobjectArray throws) {
4904 Thread* self = soa.Self();
4905
4906 // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied
4907 // code to be executed. We put it up here so we can avoid all the allocations associated with
4908 // creating the class. This can happen with (eg) jit-threads.
4909 if (!self->CanLoadClasses()) {
4910 // Make sure we don't try to load anything, potentially causing an infinite loop.
4911 ObjPtr<mirror::Throwable> pre_allocated =
4912 Runtime::Current()->GetPreAllocatedNoClassDefFoundError();
4913 self->SetException(pre_allocated);
4914 return nullptr;
4915 }
4916
4917 StackHandleScope<12> hs(self);
4918 MutableHandle<mirror::Class> temp_klass(hs.NewHandle(
4919 AllocClass(self, GetClassRoot<mirror::Class>(this), sizeof(mirror::Class))));
4920 if (temp_klass == nullptr) {
4921 CHECK(self->IsExceptionPending()); // OOME.
4922 return nullptr;
4923 }
4924 DCHECK(temp_klass->GetClass() != nullptr);
4925 temp_klass->SetObjectSize(sizeof(mirror::Proxy));
4926 // Set the class access flags incl. VerificationAttempted, so we do not try to set the flag on
4927 // the methods.
4928 temp_klass->SetAccessFlagsDuringLinking(
4929 kAccClassIsProxy | kAccPublic | kAccFinal | kAccVerificationAttempted);
4930 temp_klass->SetClassLoader(soa.Decode<mirror::ClassLoader>(loader));
4931 DCHECK_EQ(temp_klass->GetPrimitiveType(), Primitive::kPrimNot);
4932 temp_klass->SetName(soa.Decode<mirror::String>(name));
4933 temp_klass->SetDexCache(GetClassRoot<mirror::Proxy>(this)->GetDexCache());
4934 // Object has an empty iftable, copy it for that reason.
4935 temp_klass->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable());
4936 mirror::Class::SetStatus(temp_klass, ClassStatus::kIdx, self);
4937 std::string storage;
4938 const char* descriptor = temp_klass->GetDescriptor(&storage);
4939 const size_t hash = ComputeModifiedUtf8Hash(descriptor);
4940
4941 // Needs to be before we insert the class so that the allocator field is set.
4942 LinearAlloc* const allocator = GetOrCreateAllocatorForClassLoader(temp_klass->GetClassLoader());
4943
4944 // Insert the class before loading the fields as the field roots
4945 // (ArtField::declaring_class_) are only visited from the class
4946 // table. There can't be any suspend points between inserting the
4947 // class and setting the field arrays below.
4948 ObjPtr<mirror::Class> existing = InsertClass(descriptor, temp_klass.Get(), hash);
4949 CHECK(existing == nullptr);
4950
4951 // Instance fields are inherited, but we add a couple of static fields...
4952 const size_t num_fields = 2;
4953 LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, allocator, num_fields);
4954 temp_klass->SetSFieldsPtr(sfields);
4955
4956 // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by
4957 // our proxy, so Class.getInterfaces doesn't return the flattened set.
4958 ArtField& interfaces_sfield = sfields->At(0);
4959 interfaces_sfield.SetDexFieldIndex(0);
4960 interfaces_sfield.SetDeclaringClass(temp_klass.Get());
4961 interfaces_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal);
4962
4963 // 2. Create a static field 'throws' that holds exceptions thrown by our methods.
4964 ArtField& throws_sfield = sfields->At(1);
4965 throws_sfield.SetDexFieldIndex(1);
4966 throws_sfield.SetDeclaringClass(temp_klass.Get());
4967 throws_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal);
4968
4969 // Proxies have 1 direct method, the constructor
4970 const size_t num_direct_methods = 1;
4971
4972 // The array we get passed contains all methods, including private and static
4973 // ones that aren't proxied. We need to filter those out since only interface
4974 // methods (non-private & virtual) are actually proxied.
4975 Handle<mirror::ObjectArray<mirror::Method>> h_methods =
4976 hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Method>>(methods));
4977 DCHECK_EQ(h_methods->GetClass(), GetClassRoot<mirror::ObjectArray<mirror::Method>>())
4978 << mirror::Class::PrettyClass(h_methods->GetClass());
4979 // List of the actual virtual methods this class will have.
4980 std::vector<ArtMethod*> proxied_methods;
4981 std::vector<size_t> proxied_throws_idx;
4982 proxied_methods.reserve(h_methods->GetLength());
4983 proxied_throws_idx.reserve(h_methods->GetLength());
4984 // Filter out to only the non-private virtual methods.
4985 for (auto [mirror, idx] : ZipCount(h_methods.Iterate<mirror::Method>())) {
4986 ArtMethod* m = mirror->GetArtMethod();
4987 if (!m->IsPrivate() && !m->IsStatic()) {
4988 proxied_methods.push_back(m);
4989 proxied_throws_idx.push_back(idx);
4990 }
4991 }
4992 const size_t num_virtual_methods = proxied_methods.size();
4993 // We also need to filter out the 'throws'. The 'throws' are a Class[][] that
4994 // contains an array of all the classes each function is declared to throw.
4995 // This is used to wrap unexpected exceptions in a
4996 // UndeclaredThrowableException exception. This array is in the same order as
4997 // the methods array and like the methods array must be filtered to remove any
4998 // non-proxied methods.
4999 const bool has_filtered_methods =
5000 static_cast<int32_t>(num_virtual_methods) != h_methods->GetLength();
5001 MutableHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>> original_proxied_throws(
5002 hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>(throws)));
5003 MutableHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>> proxied_throws(
5004 hs.NewHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>(
5005 (has_filtered_methods)
5006 ? mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>::Alloc(
5007 self, original_proxied_throws->GetClass(), num_virtual_methods)
5008 : original_proxied_throws.Get()));
5009 if (proxied_throws.IsNull() && !original_proxied_throws.IsNull()) {
5010 self->AssertPendingOOMException();
5011 return nullptr;
5012 }
5013 if (has_filtered_methods) {
5014 for (auto [orig_idx, new_idx] : ZipCount(MakeIterationRange(proxied_throws_idx))) {
5015 DCHECK_LE(new_idx, orig_idx);
5016 proxied_throws->Set(new_idx, original_proxied_throws->Get(orig_idx));
5017 }
5018 }
5019
5020 // Create the methods array.
5021 LengthPrefixedArray<ArtMethod>* proxy_class_methods = AllocArtMethodArray(
5022 self, allocator, num_direct_methods + num_virtual_methods);
5023 // Currently AllocArtMethodArray cannot return null, but the OOM logic is left there in case we
5024 // want to throw OOM in the future.
5025 if (UNLIKELY(proxy_class_methods == nullptr)) {
5026 self->AssertPendingOOMException();
5027 return nullptr;
5028 }
5029 temp_klass->SetMethodsPtr(proxy_class_methods, num_direct_methods, num_virtual_methods);
5030
5031 // Create the single direct method.
5032 CreateProxyConstructor(temp_klass, temp_klass->GetDirectMethodUnchecked(0, image_pointer_size_));
5033
5034 // Create virtual method using specified prototypes.
5035 // TODO These should really use the iterators.
5036 for (size_t i = 0; i < num_virtual_methods; ++i) {
5037 auto* virtual_method = temp_klass->GetVirtualMethodUnchecked(i, image_pointer_size_);
5038 auto* prototype = proxied_methods[i];
5039 CreateProxyMethod(temp_klass, prototype, virtual_method);
5040 DCHECK(virtual_method->GetDeclaringClass() != nullptr);
5041 DCHECK(prototype->GetDeclaringClass() != nullptr);
5042 }
5043
5044 // The super class is java.lang.reflect.Proxy
5045 temp_klass->SetSuperClass(GetClassRoot<mirror::Proxy>(this));
5046 // Now effectively in the loaded state.
5047 mirror::Class::SetStatus(temp_klass, ClassStatus::kLoaded, self);
5048 self->AssertNoPendingException();
5049
5050 // At this point the class is loaded. Publish a ClassLoad event.
5051 // Note: this may be a temporary class. It is a listener's responsibility to handle this.
5052 Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(temp_klass);
5053
5054 MutableHandle<mirror::Class> klass = hs.NewHandle<mirror::Class>(nullptr);
5055 {
5056 // Must hold lock on object when resolved.
5057 ObjectLock<mirror::Class> resolution_lock(self, temp_klass);
5058 // Link the fields and virtual methods, creating vtable and iftables.
5059 // The new class will replace the old one in the class table.
5060 Handle<mirror::ObjectArray<mirror::Class>> h_interfaces(
5061 hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)));
5062 if (!LinkClass(self, descriptor, temp_klass, h_interfaces, &klass)) {
5063 if (!temp_klass->IsErroneous()) {
5064 mirror::Class::SetStatus(temp_klass, ClassStatus::kErrorUnresolved, self);
5065 }
5066 return nullptr;
5067 }
5068 }
5069 CHECK(temp_klass->IsRetired());
5070 CHECK_NE(temp_klass.Get(), klass.Get());
5071
5072 CHECK_EQ(interfaces_sfield.GetDeclaringClass(), klass.Get());
5073 interfaces_sfield.SetObject<false>(
5074 klass.Get(),
5075 soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces));
5076 CHECK_EQ(throws_sfield.GetDeclaringClass(), klass.Get());
5077 throws_sfield.SetObject<false>(
5078 klass.Get(),
5079 proxied_throws.Get());
5080
5081 Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(temp_klass, klass);
5082
5083 // SubtypeCheckInfo::Initialized must happen-before any new-instance for that type.
5084 // See also ClassLinker::EnsureInitialized().
5085 if (kBitstringSubtypeCheckEnabled) {
5086 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
5087 SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(klass.Get());
5088 // TODO: Avoid taking subtype_check_lock_ if SubtypeCheck for j.l.r.Proxy is already assigned.
5089 }
5090
5091 VisiblyInitializedCallback* callback = nullptr;
5092 {
5093 // Lock on klass is released. Lock new class object.
5094 ObjectLock<mirror::Class> initialization_lock(self, klass);
5095 EnsureSkipAccessChecksMethods(klass, image_pointer_size_);
5096 // Conservatively go through the ClassStatus::kInitialized state.
5097 callback = MarkClassInitialized(self, klass);
5098 }
5099 if (callback != nullptr) {
5100 callback->MakeVisible(self);
5101 }
5102
5103 // Consistency checks.
5104 if (kIsDebugBuild) {
5105 CHECK(klass->GetIFieldsPtr() == nullptr);
5106 CheckProxyConstructor(klass->GetDirectMethod(0, image_pointer_size_));
5107
5108 for (size_t i = 0; i < num_virtual_methods; ++i) {
5109 auto* virtual_method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_);
5110 CheckProxyMethod(virtual_method, proxied_methods[i]);
5111 }
5112
5113 StackHandleScope<1> hs2(self);
5114 Handle<mirror::String> decoded_name = hs2.NewHandle(soa.Decode<mirror::String>(name));
5115 std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces",
5116 decoded_name->ToModifiedUtf8().c_str()));
5117 CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(0)), interfaces_field_name);
5118
5119 std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws",
5120 decoded_name->ToModifiedUtf8().c_str()));
5121 CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(1)), throws_field_name);
5122
5123 CHECK_EQ(klass.Get()->GetProxyInterfaces(),
5124 soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces));
5125 CHECK_EQ(klass.Get()->GetProxyThrows(),
5126 proxied_throws.Get());
5127 }
5128 return klass.Get();
5129 }
5130
CreateProxyConstructor(Handle<mirror::Class> klass,ArtMethod * out)5131 void ClassLinker::CreateProxyConstructor(Handle<mirror::Class> klass, ArtMethod* out) {
5132 // Create constructor for Proxy that must initialize the method.
5133 ObjPtr<mirror::Class> proxy_class = GetClassRoot<mirror::Proxy>(this);
5134 CHECK_EQ(proxy_class->NumDirectMethods(), 21u);
5135
5136 // Find the <init>(InvocationHandler)V method. The exact method offset varies depending
5137 // on which front-end compiler was used to build the libcore DEX files.
5138 ArtMethod* proxy_constructor =
5139 jni::DecodeArtMethod(WellKnownClasses::java_lang_reflect_Proxy_init);
5140 DCHECK(proxy_constructor != nullptr)
5141 << "Could not find <init> method in java.lang.reflect.Proxy";
5142
5143 // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its
5144 // code_ too)
5145 DCHECK(out != nullptr);
5146 out->CopyFrom(proxy_constructor, image_pointer_size_);
5147 // Make this constructor public and fix the class to be our Proxy version.
5148 // Mark kAccCompileDontBother so that we don't take JIT samples for the method. b/62349349
5149 // Note that the compiler calls a ResolveMethod() overload that does not handle a Proxy referrer.
5150 out->SetAccessFlags((out->GetAccessFlags() & ~kAccProtected) |
5151 kAccPublic |
5152 kAccCompileDontBother);
5153 out->SetDeclaringClass(klass.Get());
5154
5155 // Set the original constructor method.
5156 out->SetDataPtrSize(proxy_constructor, image_pointer_size_);
5157 }
5158
CheckProxyConstructor(ArtMethod * constructor) const5159 void ClassLinker::CheckProxyConstructor(ArtMethod* constructor) const {
5160 CHECK(constructor->IsConstructor());
5161 auto* np = constructor->GetInterfaceMethodIfProxy(image_pointer_size_);
5162 CHECK_STREQ(np->GetName(), "<init>");
5163 CHECK_STREQ(np->GetSignature().ToString().c_str(), "(Ljava/lang/reflect/InvocationHandler;)V");
5164 DCHECK(constructor->IsPublic());
5165 }
5166
CreateProxyMethod(Handle<mirror::Class> klass,ArtMethod * prototype,ArtMethod * out)5167 void ClassLinker::CreateProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype,
5168 ArtMethod* out) {
5169 // We steal everything from the prototype (such as DexCache, invoke stub, etc.) then specialize
5170 // as necessary
5171 DCHECK(out != nullptr);
5172 out->CopyFrom(prototype, image_pointer_size_);
5173
5174 // Set class to be the concrete proxy class.
5175 out->SetDeclaringClass(klass.Get());
5176 // Clear the abstract and default flags to ensure that defaults aren't picked in
5177 // preference to the invocation handler.
5178 const uint32_t kRemoveFlags = kAccAbstract | kAccDefault;
5179 // Make the method final.
5180 // Mark kAccCompileDontBother so that we don't take JIT samples for the method. b/62349349
5181 const uint32_t kAddFlags = kAccFinal | kAccCompileDontBother;
5182 out->SetAccessFlags((out->GetAccessFlags() & ~kRemoveFlags) | kAddFlags);
5183
5184 // Set the original interface method.
5185 out->SetDataPtrSize(prototype, image_pointer_size_);
5186
5187 // At runtime the method looks like a reference and argument saving method, clone the code
5188 // related parameters from this method.
5189 out->SetEntryPointFromQuickCompiledCode(GetQuickProxyInvokeHandler());
5190 }
5191
CheckProxyMethod(ArtMethod * method,ArtMethod * prototype) const5192 void ClassLinker::CheckProxyMethod(ArtMethod* method, ArtMethod* prototype) const {
5193 // Basic consistency checks.
5194 CHECK(!prototype->IsFinal());
5195 CHECK(method->IsFinal());
5196 CHECK(method->IsInvokable());
5197
5198 // The proxy method doesn't have its own dex cache or dex file and so it steals those of its
5199 // interface prototype. The exception to this are Constructors and the Class of the Proxy itself.
5200 CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex());
5201 CHECK_EQ(prototype, method->GetInterfaceMethodIfProxy(image_pointer_size_));
5202 }
5203
CanWeInitializeClass(ObjPtr<mirror::Class> klass,bool can_init_statics,bool can_init_parents)5204 bool ClassLinker::CanWeInitializeClass(ObjPtr<mirror::Class> klass, bool can_init_statics,
5205 bool can_init_parents) {
5206 if (can_init_statics && can_init_parents) {
5207 return true;
5208 }
5209 if (!can_init_statics) {
5210 // Check if there's a class initializer.
5211 ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_);
5212 if (clinit != nullptr) {
5213 return false;
5214 }
5215 // Check if there are encoded static values needing initialization.
5216 if (klass->NumStaticFields() != 0) {
5217 const dex::ClassDef* dex_class_def = klass->GetClassDef();
5218 DCHECK(dex_class_def != nullptr);
5219 if (dex_class_def->static_values_off_ != 0) {
5220 return false;
5221 }
5222 }
5223 }
5224 // If we are a class we need to initialize all interfaces with default methods when we are
5225 // initialized. Check all of them.
5226 if (!klass->IsInterface()) {
5227 size_t num_interfaces = klass->GetIfTableCount();
5228 for (size_t i = 0; i < num_interfaces; i++) {
5229 ObjPtr<mirror::Class> iface = klass->GetIfTable()->GetInterface(i);
5230 if (iface->HasDefaultMethods() && !iface->IsInitialized()) {
5231 if (!can_init_parents || !CanWeInitializeClass(iface, can_init_statics, can_init_parents)) {
5232 return false;
5233 }
5234 }
5235 }
5236 }
5237 if (klass->IsInterface() || !klass->HasSuperClass()) {
5238 return true;
5239 }
5240 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
5241 if (super_class->IsInitialized()) {
5242 return true;
5243 }
5244 return can_init_parents && CanWeInitializeClass(super_class, can_init_statics, can_init_parents);
5245 }
5246
InitializeClass(Thread * self,Handle<mirror::Class> klass,bool can_init_statics,bool can_init_parents)5247 bool ClassLinker::InitializeClass(Thread* self,
5248 Handle<mirror::Class> klass,
5249 bool can_init_statics,
5250 bool can_init_parents) {
5251 // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol
5252
5253 // Are we already initialized and therefore done?
5254 // Note: we differ from the JLS here as we don't do this under the lock, this is benign as
5255 // an initialized class will never change its state.
5256 if (klass->IsInitialized()) {
5257 return true;
5258 }
5259
5260 // Fast fail if initialization requires a full runtime. Not part of the JLS.
5261 if (!CanWeInitializeClass(klass.Get(), can_init_statics, can_init_parents)) {
5262 return false;
5263 }
5264
5265 self->AllowThreadSuspension();
5266 Runtime* const runtime = Runtime::Current();
5267 const bool stats_enabled = runtime->HasStatsEnabled();
5268 uint64_t t0;
5269 {
5270 ObjectLock<mirror::Class> lock(self, klass);
5271
5272 // Re-check under the lock in case another thread initialized ahead of us.
5273 if (klass->IsInitialized()) {
5274 return true;
5275 }
5276
5277 // Was the class already found to be erroneous? Done under the lock to match the JLS.
5278 if (klass->IsErroneous()) {
5279 ThrowEarlierClassFailure(klass.Get(), true, /* log= */ true);
5280 VlogClassInitializationFailure(klass);
5281 return false;
5282 }
5283
5284 CHECK(klass->IsResolved() && !klass->IsErroneousResolved())
5285 << klass->PrettyClass() << ": state=" << klass->GetStatus();
5286
5287 if (!klass->IsVerified()) {
5288 VerifyClass(self, /*verifier_deps= */ nullptr, klass);
5289 if (!klass->IsVerified()) {
5290 // We failed to verify, expect either the klass to be erroneous or verification failed at
5291 // compile time.
5292 if (klass->IsErroneous()) {
5293 // The class is erroneous. This may be a verifier error, or another thread attempted
5294 // verification and/or initialization and failed. We can distinguish those cases by
5295 // whether an exception is already pending.
5296 if (self->IsExceptionPending()) {
5297 // Check that it's a VerifyError.
5298 DCHECK_EQ("java.lang.Class<java.lang.VerifyError>",
5299 mirror::Class::PrettyClass(self->GetException()->GetClass()));
5300 } else {
5301 // Check that another thread attempted initialization.
5302 DCHECK_NE(0, klass->GetClinitThreadId());
5303 DCHECK_NE(self->GetTid(), klass->GetClinitThreadId());
5304 // Need to rethrow the previous failure now.
5305 ThrowEarlierClassFailure(klass.Get(), true);
5306 }
5307 VlogClassInitializationFailure(klass);
5308 } else {
5309 CHECK(Runtime::Current()->IsAotCompiler());
5310 CHECK(klass->ShouldVerifyAtRuntime() || klass->IsVerifiedNeedsAccessChecks());
5311 self->AssertNoPendingException();
5312 self->SetException(Runtime::Current()->GetPreAllocatedNoClassDefFoundError());
5313 }
5314 self->AssertPendingException();
5315 return false;
5316 } else {
5317 self->AssertNoPendingException();
5318 }
5319
5320 // A separate thread could have moved us all the way to initialized. A "simple" example
5321 // involves a subclass of the current class being initialized at the same time (which
5322 // will implicitly initialize the superclass, if scheduled that way). b/28254258
5323 DCHECK(!klass->IsErroneous()) << klass->GetStatus();
5324 if (klass->IsInitialized()) {
5325 return true;
5326 }
5327 }
5328
5329 // If the class is ClassStatus::kInitializing, either this thread is
5330 // initializing higher up the stack or another thread has beat us
5331 // to initializing and we need to wait. Either way, this
5332 // invocation of InitializeClass will not be responsible for
5333 // running <clinit> and will return.
5334 if (klass->GetStatus() == ClassStatus::kInitializing) {
5335 // Could have got an exception during verification.
5336 if (self->IsExceptionPending()) {
5337 VlogClassInitializationFailure(klass);
5338 return false;
5339 }
5340 // We caught somebody else in the act; was it us?
5341 if (klass->GetClinitThreadId() == self->GetTid()) {
5342 // Yes. That's fine. Return so we can continue initializing.
5343 return true;
5344 }
5345 // No. That's fine. Wait for another thread to finish initializing.
5346 return WaitForInitializeClass(klass, self, lock);
5347 }
5348
5349 // Try to get the oat class's status for this class if the oat file is present. The compiler
5350 // tries to validate superclass descriptors, and writes the result into the oat file.
5351 // Runtime correctness is guaranteed by classpath checks done on loading. If the classpath
5352 // is different at runtime than it was at compile time, the oat file is rejected. So if the
5353 // oat file is present, the classpaths must match, and the runtime time check can be skipped.
5354 bool has_oat_class = false;
5355 const OatFile::OatClass oat_class = (runtime->IsStarted() && !runtime->IsAotCompiler())
5356 ? OatFile::FindOatClass(klass->GetDexFile(), klass->GetDexClassDefIndex(), &has_oat_class)
5357 : OatFile::OatClass::Invalid();
5358 if (oat_class.GetStatus() < ClassStatus::kSuperclassValidated &&
5359 !ValidateSuperClassDescriptors(klass)) {
5360 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
5361 return false;
5362 }
5363 self->AllowThreadSuspension();
5364
5365 CHECK_EQ(klass->GetStatus(), ClassStatus::kVerified) << klass->PrettyClass()
5366 << " self.tid=" << self->GetTid() << " clinit.tid=" << klass->GetClinitThreadId();
5367
5368 // From here out other threads may observe that we're initializing and so changes of state
5369 // require the a notification.
5370 klass->SetClinitThreadId(self->GetTid());
5371 mirror::Class::SetStatus(klass, ClassStatus::kInitializing, self);
5372
5373 t0 = stats_enabled ? NanoTime() : 0u;
5374 }
5375
5376 uint64_t t_sub = 0;
5377
5378 // Initialize super classes, must be done while initializing for the JLS.
5379 if (!klass->IsInterface() && klass->HasSuperClass()) {
5380 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
5381 if (!super_class->IsInitialized()) {
5382 CHECK(!super_class->IsInterface());
5383 CHECK(can_init_parents);
5384 StackHandleScope<1> hs(self);
5385 Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class));
5386 uint64_t super_t0 = stats_enabled ? NanoTime() : 0u;
5387 bool super_initialized = InitializeClass(self, handle_scope_super, can_init_statics, true);
5388 uint64_t super_t1 = stats_enabled ? NanoTime() : 0u;
5389 if (!super_initialized) {
5390 // The super class was verified ahead of entering initializing, we should only be here if
5391 // the super class became erroneous due to initialization.
5392 // For the case of aot compiler, the super class might also be initializing but we don't
5393 // want to process circular dependencies in pre-compile.
5394 CHECK(self->IsExceptionPending())
5395 << "Super class initialization failed for "
5396 << handle_scope_super->PrettyDescriptor()
5397 << " that has unexpected status " << handle_scope_super->GetStatus()
5398 << "\nPending exception:\n"
5399 << (self->GetException() != nullptr ? self->GetException()->Dump() : "");
5400 ObjectLock<mirror::Class> lock(self, klass);
5401 // Initialization failed because the super-class is erroneous.
5402 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
5403 return false;
5404 }
5405 t_sub = super_t1 - super_t0;
5406 }
5407 }
5408
5409 if (!klass->IsInterface()) {
5410 // Initialize interfaces with default methods for the JLS.
5411 size_t num_direct_interfaces = klass->NumDirectInterfaces();
5412 // Only setup the (expensive) handle scope if we actually need to.
5413 if (UNLIKELY(num_direct_interfaces > 0)) {
5414 StackHandleScope<1> hs_iface(self);
5415 MutableHandle<mirror::Class> handle_scope_iface(hs_iface.NewHandle<mirror::Class>(nullptr));
5416 for (size_t i = 0; i < num_direct_interfaces; i++) {
5417 handle_scope_iface.Assign(mirror::Class::GetDirectInterface(self, klass.Get(), i));
5418 CHECK(handle_scope_iface != nullptr) << klass->PrettyDescriptor() << " iface #" << i;
5419 CHECK(handle_scope_iface->IsInterface());
5420 if (handle_scope_iface->HasBeenRecursivelyInitialized()) {
5421 // We have already done this for this interface. Skip it.
5422 continue;
5423 }
5424 // We cannot just call initialize class directly because we need to ensure that ALL
5425 // interfaces with default methods are initialized. Non-default interface initialization
5426 // will not affect other non-default super-interfaces.
5427 // This is not very precise, misses all walking.
5428 uint64_t inf_t0 = stats_enabled ? NanoTime() : 0u;
5429 bool iface_initialized = InitializeDefaultInterfaceRecursive(self,
5430 handle_scope_iface,
5431 can_init_statics,
5432 can_init_parents);
5433 uint64_t inf_t1 = stats_enabled ? NanoTime() : 0u;
5434 if (!iface_initialized) {
5435 ObjectLock<mirror::Class> lock(self, klass);
5436 // Initialization failed because one of our interfaces with default methods is erroneous.
5437 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
5438 return false;
5439 }
5440 t_sub += inf_t1 - inf_t0;
5441 }
5442 }
5443 }
5444
5445 const size_t num_static_fields = klass->NumStaticFields();
5446 if (num_static_fields > 0) {
5447 const dex::ClassDef* dex_class_def = klass->GetClassDef();
5448 CHECK(dex_class_def != nullptr);
5449 StackHandleScope<3> hs(self);
5450 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
5451 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
5452
5453 // Eagerly fill in static fields so that the we don't have to do as many expensive
5454 // Class::FindStaticField in ResolveField.
5455 for (size_t i = 0; i < num_static_fields; ++i) {
5456 ArtField* field = klass->GetStaticField(i);
5457 const uint32_t field_idx = field->GetDexFieldIndex();
5458 ArtField* resolved_field = dex_cache->GetResolvedField(field_idx);
5459 if (resolved_field == nullptr) {
5460 // Populating cache of a dex file which defines `klass` should always be allowed.
5461 DCHECK(!hiddenapi::ShouldDenyAccessToMember(
5462 field,
5463 hiddenapi::AccessContext(class_loader.Get(), dex_cache.Get()),
5464 hiddenapi::AccessMethod::kNone));
5465 dex_cache->SetResolvedField(field_idx, field);
5466 } else {
5467 DCHECK_EQ(field, resolved_field);
5468 }
5469 }
5470
5471 annotations::RuntimeEncodedStaticFieldValueIterator value_it(dex_cache,
5472 class_loader,
5473 this,
5474 *dex_class_def);
5475 const DexFile& dex_file = *dex_cache->GetDexFile();
5476
5477 if (value_it.HasNext()) {
5478 ClassAccessor accessor(dex_file, *dex_class_def);
5479 CHECK(can_init_statics);
5480 for (const ClassAccessor::Field& field : accessor.GetStaticFields()) {
5481 if (!value_it.HasNext()) {
5482 break;
5483 }
5484 ArtField* art_field = ResolveField(field.GetIndex(),
5485 dex_cache,
5486 class_loader,
5487 /* is_static= */ true);
5488 if (Runtime::Current()->IsActiveTransaction()) {
5489 value_it.ReadValueToField<true>(art_field);
5490 } else {
5491 value_it.ReadValueToField<false>(art_field);
5492 }
5493 if (self->IsExceptionPending()) {
5494 break;
5495 }
5496 value_it.Next();
5497 }
5498 DCHECK(self->IsExceptionPending() || !value_it.HasNext());
5499 }
5500 }
5501
5502
5503 if (!self->IsExceptionPending()) {
5504 ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_);
5505 if (clinit != nullptr) {
5506 CHECK(can_init_statics);
5507 JValue result;
5508 clinit->Invoke(self, nullptr, 0, &result, "V");
5509 }
5510 }
5511 self->AllowThreadSuspension();
5512 uint64_t t1 = stats_enabled ? NanoTime() : 0u;
5513
5514 VisiblyInitializedCallback* callback = nullptr;
5515 bool success = true;
5516 {
5517 ObjectLock<mirror::Class> lock(self, klass);
5518
5519 if (self->IsExceptionPending()) {
5520 WrapExceptionInInitializer(klass);
5521 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
5522 success = false;
5523 } else if (Runtime::Current()->IsTransactionAborted()) {
5524 // The exception thrown when the transaction aborted has been caught and cleared
5525 // so we need to throw it again now.
5526 VLOG(compiler) << "Return from class initializer of "
5527 << mirror::Class::PrettyDescriptor(klass.Get())
5528 << " without exception while transaction was aborted: re-throw it now.";
5529 runtime->ThrowTransactionAbortError(self);
5530 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
5531 success = false;
5532 } else {
5533 if (stats_enabled) {
5534 RuntimeStats* global_stats = runtime->GetStats();
5535 RuntimeStats* thread_stats = self->GetStats();
5536 ++global_stats->class_init_count;
5537 ++thread_stats->class_init_count;
5538 global_stats->class_init_time_ns += (t1 - t0 - t_sub);
5539 thread_stats->class_init_time_ns += (t1 - t0 - t_sub);
5540 }
5541 // Set the class as initialized except if failed to initialize static fields.
5542 callback = MarkClassInitialized(self, klass);
5543 if (VLOG_IS_ON(class_linker)) {
5544 std::string temp;
5545 LOG(INFO) << "Initialized class " << klass->GetDescriptor(&temp) << " from " <<
5546 klass->GetLocation();
5547 }
5548 }
5549 }
5550 if (callback != nullptr) {
5551 callback->MakeVisible(self);
5552 }
5553 return success;
5554 }
5555
5556 // We recursively run down the tree of interfaces. We need to do this in the order they are declared
5557 // and perform the initialization only on those interfaces that contain default methods.
InitializeDefaultInterfaceRecursive(Thread * self,Handle<mirror::Class> iface,bool can_init_statics,bool can_init_parents)5558 bool ClassLinker::InitializeDefaultInterfaceRecursive(Thread* self,
5559 Handle<mirror::Class> iface,
5560 bool can_init_statics,
5561 bool can_init_parents) {
5562 CHECK(iface->IsInterface());
5563 size_t num_direct_ifaces = iface->NumDirectInterfaces();
5564 // Only create the (expensive) handle scope if we need it.
5565 if (UNLIKELY(num_direct_ifaces > 0)) {
5566 StackHandleScope<1> hs(self);
5567 MutableHandle<mirror::Class> handle_super_iface(hs.NewHandle<mirror::Class>(nullptr));
5568 // First we initialize all of iface's super-interfaces recursively.
5569 for (size_t i = 0; i < num_direct_ifaces; i++) {
5570 ObjPtr<mirror::Class> super_iface = mirror::Class::GetDirectInterface(self, iface.Get(), i);
5571 CHECK(super_iface != nullptr) << iface->PrettyDescriptor() << " iface #" << i;
5572 if (!super_iface->HasBeenRecursivelyInitialized()) {
5573 // Recursive step
5574 handle_super_iface.Assign(super_iface);
5575 if (!InitializeDefaultInterfaceRecursive(self,
5576 handle_super_iface,
5577 can_init_statics,
5578 can_init_parents)) {
5579 return false;
5580 }
5581 }
5582 }
5583 }
5584
5585 bool result = true;
5586 // Then we initialize 'iface' if it has default methods. We do not need to (and in fact must not)
5587 // initialize if we don't have default methods.
5588 if (iface->HasDefaultMethods()) {
5589 result = EnsureInitialized(self, iface, can_init_statics, can_init_parents);
5590 }
5591
5592 // Mark that this interface has undergone recursive default interface initialization so we know we
5593 // can skip it on any later class initializations. We do this even if we are not a default
5594 // interface since we can still avoid the traversal. This is purely a performance optimization.
5595 if (result) {
5596 // TODO This should be done in a better way
5597 // Note: Use a try-lock to avoid blocking when someone else is holding the lock on this
5598 // interface. It is bad (Java) style, but not impossible. Marking the recursive
5599 // initialization is a performance optimization (to avoid another idempotent visit
5600 // for other implementing classes/interfaces), and can be revisited later.
5601 ObjectTryLock<mirror::Class> lock(self, iface);
5602 if (lock.Acquired()) {
5603 iface->SetRecursivelyInitialized();
5604 }
5605 }
5606 return result;
5607 }
5608
WaitForInitializeClass(Handle<mirror::Class> klass,Thread * self,ObjectLock<mirror::Class> & lock)5609 bool ClassLinker::WaitForInitializeClass(Handle<mirror::Class> klass,
5610 Thread* self,
5611 ObjectLock<mirror::Class>& lock)
5612 REQUIRES_SHARED(Locks::mutator_lock_) {
5613 while (true) {
5614 self->AssertNoPendingException();
5615 CHECK(!klass->IsInitialized());
5616 lock.WaitIgnoringInterrupts();
5617
5618 // When we wake up, repeat the test for init-in-progress. If
5619 // there's an exception pending (only possible if
5620 // we were not using WaitIgnoringInterrupts), bail out.
5621 if (self->IsExceptionPending()) {
5622 WrapExceptionInInitializer(klass);
5623 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
5624 return false;
5625 }
5626 // Spurious wakeup? Go back to waiting.
5627 if (klass->GetStatus() == ClassStatus::kInitializing) {
5628 continue;
5629 }
5630 if (klass->GetStatus() == ClassStatus::kVerified &&
5631 Runtime::Current()->IsAotCompiler()) {
5632 // Compile time initialization failed.
5633 return false;
5634 }
5635 if (klass->IsErroneous()) {
5636 // The caller wants an exception, but it was thrown in a
5637 // different thread. Synthesize one here.
5638 ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread",
5639 klass->PrettyDescriptor().c_str());
5640 VlogClassInitializationFailure(klass);
5641 return false;
5642 }
5643 if (klass->IsInitialized()) {
5644 return true;
5645 }
5646 LOG(FATAL) << "Unexpected class status. " << klass->PrettyClass() << " is "
5647 << klass->GetStatus();
5648 }
5649 UNREACHABLE();
5650 }
5651
ThrowSignatureCheckResolveReturnTypeException(Handle<mirror::Class> klass,Handle<mirror::Class> super_klass,ArtMethod * method,ArtMethod * m)5652 static void ThrowSignatureCheckResolveReturnTypeException(Handle<mirror::Class> klass,
5653 Handle<mirror::Class> super_klass,
5654 ArtMethod* method,
5655 ArtMethod* m)
5656 REQUIRES_SHARED(Locks::mutator_lock_) {
5657 DCHECK(Thread::Current()->IsExceptionPending());
5658 DCHECK(!m->IsProxyMethod());
5659 const DexFile* dex_file = m->GetDexFile();
5660 const dex::MethodId& method_id = dex_file->GetMethodId(m->GetDexMethodIndex());
5661 const dex::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
5662 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
5663 std::string return_type = dex_file->PrettyType(return_type_idx);
5664 std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader());
5665 ThrowWrappedLinkageError(klass.Get(),
5666 "While checking class %s method %s signature against %s %s: "
5667 "Failed to resolve return type %s with %s",
5668 mirror::Class::PrettyDescriptor(klass.Get()).c_str(),
5669 ArtMethod::PrettyMethod(method).c_str(),
5670 super_klass->IsInterface() ? "interface" : "superclass",
5671 mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(),
5672 return_type.c_str(), class_loader.c_str());
5673 }
5674
ThrowSignatureCheckResolveArgException(Handle<mirror::Class> klass,Handle<mirror::Class> super_klass,ArtMethod * method,ArtMethod * m,uint32_t index,dex::TypeIndex arg_type_idx)5675 static void ThrowSignatureCheckResolveArgException(Handle<mirror::Class> klass,
5676 Handle<mirror::Class> super_klass,
5677 ArtMethod* method,
5678 ArtMethod* m,
5679 uint32_t index,
5680 dex::TypeIndex arg_type_idx)
5681 REQUIRES_SHARED(Locks::mutator_lock_) {
5682 DCHECK(Thread::Current()->IsExceptionPending());
5683 DCHECK(!m->IsProxyMethod());
5684 const DexFile* dex_file = m->GetDexFile();
5685 std::string arg_type = dex_file->PrettyType(arg_type_idx);
5686 std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader());
5687 ThrowWrappedLinkageError(klass.Get(),
5688 "While checking class %s method %s signature against %s %s: "
5689 "Failed to resolve arg %u type %s with %s",
5690 mirror::Class::PrettyDescriptor(klass.Get()).c_str(),
5691 ArtMethod::PrettyMethod(method).c_str(),
5692 super_klass->IsInterface() ? "interface" : "superclass",
5693 mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(),
5694 index, arg_type.c_str(), class_loader.c_str());
5695 }
5696
ThrowSignatureMismatch(Handle<mirror::Class> klass,Handle<mirror::Class> super_klass,ArtMethod * method,const std::string & error_msg)5697 static void ThrowSignatureMismatch(Handle<mirror::Class> klass,
5698 Handle<mirror::Class> super_klass,
5699 ArtMethod* method,
5700 const std::string& error_msg)
5701 REQUIRES_SHARED(Locks::mutator_lock_) {
5702 ThrowLinkageError(klass.Get(),
5703 "Class %s method %s resolves differently in %s %s: %s",
5704 mirror::Class::PrettyDescriptor(klass.Get()).c_str(),
5705 ArtMethod::PrettyMethod(method).c_str(),
5706 super_klass->IsInterface() ? "interface" : "superclass",
5707 mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(),
5708 error_msg.c_str());
5709 }
5710
HasSameSignatureWithDifferentClassLoaders(Thread * self,Handle<mirror::Class> klass,Handle<mirror::Class> super_klass,ArtMethod * method1,ArtMethod * method2)5711 static bool HasSameSignatureWithDifferentClassLoaders(Thread* self,
5712 Handle<mirror::Class> klass,
5713 Handle<mirror::Class> super_klass,
5714 ArtMethod* method1,
5715 ArtMethod* method2)
5716 REQUIRES_SHARED(Locks::mutator_lock_) {
5717 {
5718 StackHandleScope<1> hs(self);
5719 Handle<mirror::Class> return_type(hs.NewHandle(method1->ResolveReturnType()));
5720 if (UNLIKELY(return_type == nullptr)) {
5721 ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method1);
5722 return false;
5723 }
5724 ObjPtr<mirror::Class> other_return_type = method2->ResolveReturnType();
5725 if (UNLIKELY(other_return_type == nullptr)) {
5726 ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method2);
5727 return false;
5728 }
5729 if (UNLIKELY(other_return_type != return_type.Get())) {
5730 ThrowSignatureMismatch(klass, super_klass, method1,
5731 StringPrintf("Return types mismatch: %s(%p) vs %s(%p)",
5732 return_type->PrettyClassAndClassLoader().c_str(),
5733 return_type.Get(),
5734 other_return_type->PrettyClassAndClassLoader().c_str(),
5735 other_return_type.Ptr()));
5736 return false;
5737 }
5738 }
5739 const dex::TypeList* types1 = method1->GetParameterTypeList();
5740 const dex::TypeList* types2 = method2->GetParameterTypeList();
5741 if (types1 == nullptr) {
5742 if (types2 != nullptr && types2->Size() != 0) {
5743 ThrowSignatureMismatch(klass, super_klass, method1,
5744 StringPrintf("Type list mismatch with %s",
5745 method2->PrettyMethod(true).c_str()));
5746 return false;
5747 }
5748 return true;
5749 } else if (UNLIKELY(types2 == nullptr)) {
5750 if (types1->Size() != 0) {
5751 ThrowSignatureMismatch(klass, super_klass, method1,
5752 StringPrintf("Type list mismatch with %s",
5753 method2->PrettyMethod(true).c_str()));
5754 return false;
5755 }
5756 return true;
5757 }
5758 uint32_t num_types = types1->Size();
5759 if (UNLIKELY(num_types != types2->Size())) {
5760 ThrowSignatureMismatch(klass, super_klass, method1,
5761 StringPrintf("Type list mismatch with %s",
5762 method2->PrettyMethod(true).c_str()));
5763 return false;
5764 }
5765 for (uint32_t i = 0; i < num_types; ++i) {
5766 StackHandleScope<1> hs(self);
5767 dex::TypeIndex param_type_idx = types1->GetTypeItem(i).type_idx_;
5768 Handle<mirror::Class> param_type(hs.NewHandle(
5769 method1->ResolveClassFromTypeIndex(param_type_idx)));
5770 if (UNLIKELY(param_type == nullptr)) {
5771 ThrowSignatureCheckResolveArgException(klass, super_klass, method1,
5772 method1, i, param_type_idx);
5773 return false;
5774 }
5775 dex::TypeIndex other_param_type_idx = types2->GetTypeItem(i).type_idx_;
5776 ObjPtr<mirror::Class> other_param_type =
5777 method2->ResolveClassFromTypeIndex(other_param_type_idx);
5778 if (UNLIKELY(other_param_type == nullptr)) {
5779 ThrowSignatureCheckResolveArgException(klass, super_klass, method1,
5780 method2, i, other_param_type_idx);
5781 return false;
5782 }
5783 if (UNLIKELY(param_type.Get() != other_param_type)) {
5784 ThrowSignatureMismatch(klass, super_klass, method1,
5785 StringPrintf("Parameter %u type mismatch: %s(%p) vs %s(%p)",
5786 i,
5787 param_type->PrettyClassAndClassLoader().c_str(),
5788 param_type.Get(),
5789 other_param_type->PrettyClassAndClassLoader().c_str(),
5790 other_param_type.Ptr()));
5791 return false;
5792 }
5793 }
5794 return true;
5795 }
5796
5797
ValidateSuperClassDescriptors(Handle<mirror::Class> klass)5798 bool ClassLinker::ValidateSuperClassDescriptors(Handle<mirror::Class> klass) {
5799 if (klass->IsInterface()) {
5800 return true;
5801 }
5802 // Begin with the methods local to the superclass.
5803 Thread* self = Thread::Current();
5804 StackHandleScope<1> hs(self);
5805 MutableHandle<mirror::Class> super_klass(hs.NewHandle<mirror::Class>(nullptr));
5806 if (klass->HasSuperClass() &&
5807 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
5808 super_klass.Assign(klass->GetSuperClass());
5809 for (int i = klass->GetSuperClass()->GetVTableLength() - 1; i >= 0; --i) {
5810 auto* m = klass->GetVTableEntry(i, image_pointer_size_);
5811 auto* super_m = klass->GetSuperClass()->GetVTableEntry(i, image_pointer_size_);
5812 if (m != super_m) {
5813 if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self,
5814 klass,
5815 super_klass,
5816 m,
5817 super_m))) {
5818 self->AssertPendingException();
5819 return false;
5820 }
5821 }
5822 }
5823 }
5824 for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) {
5825 super_klass.Assign(klass->GetIfTable()->GetInterface(i));
5826 if (klass->GetClassLoader() != super_klass->GetClassLoader()) {
5827 uint32_t num_methods = super_klass->NumVirtualMethods();
5828 for (uint32_t j = 0; j < num_methods; ++j) {
5829 auto* m = klass->GetIfTable()->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>(
5830 j, image_pointer_size_);
5831 auto* super_m = super_klass->GetVirtualMethod(j, image_pointer_size_);
5832 if (m != super_m) {
5833 if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self,
5834 klass,
5835 super_klass,
5836 m,
5837 super_m))) {
5838 self->AssertPendingException();
5839 return false;
5840 }
5841 }
5842 }
5843 }
5844 }
5845 return true;
5846 }
5847
EnsureInitialized(Thread * self,Handle<mirror::Class> c,bool can_init_fields,bool can_init_parents)5848 bool ClassLinker::EnsureInitialized(Thread* self,
5849 Handle<mirror::Class> c,
5850 bool can_init_fields,
5851 bool can_init_parents) {
5852 DCHECK(c != nullptr);
5853
5854 if (c->IsInitialized()) {
5855 // If we've seen an initialized but not visibly initialized class
5856 // many times, request visible initialization.
5857 if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) {
5858 // Thanks to the x86 memory model classes skip the initialized status.
5859 DCHECK(c->IsVisiblyInitialized());
5860 } else if (UNLIKELY(!c->IsVisiblyInitialized())) {
5861 if (self->IncrementMakeVisiblyInitializedCounter()) {
5862 MakeInitializedClassesVisiblyInitialized(self, /*wait=*/ false);
5863 }
5864 }
5865 DCHECK(c->WasVerificationAttempted()) << c->PrettyClassAndClassLoader();
5866 return true;
5867 }
5868 // SubtypeCheckInfo::Initialized must happen-before any new-instance for that type.
5869 //
5870 // Ensure the bitstring is initialized before any of the class initialization
5871 // logic occurs. Once a class initializer starts running, objects can
5872 // escape into the heap and use the subtype checking code.
5873 //
5874 // Note: A class whose SubtypeCheckInfo is at least Initialized means it
5875 // can be used as a source for the IsSubClass check, and that all ancestors
5876 // of the class are Assigned (can be used as a target for IsSubClass check)
5877 // or Overflowed (can be used as a source for IsSubClass check).
5878 if (kBitstringSubtypeCheckEnabled) {
5879 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
5880 SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(c.Get());
5881 // TODO: Avoid taking subtype_check_lock_ if SubtypeCheck is already initialized.
5882 }
5883 const bool success = InitializeClass(self, c, can_init_fields, can_init_parents);
5884 if (!success) {
5885 if (can_init_fields && can_init_parents) {
5886 CHECK(self->IsExceptionPending()) << c->PrettyClass();
5887 } else {
5888 // There may or may not be an exception pending. If there is, clear it.
5889 // We propagate the exception only if we can initialize fields and parents.
5890 self->ClearException();
5891 }
5892 } else {
5893 self->AssertNoPendingException();
5894 }
5895 return success;
5896 }
5897
FixupTemporaryDeclaringClass(ObjPtr<mirror::Class> temp_class,ObjPtr<mirror::Class> new_class)5898 void ClassLinker::FixupTemporaryDeclaringClass(ObjPtr<mirror::Class> temp_class,
5899 ObjPtr<mirror::Class> new_class) {
5900 DCHECK_EQ(temp_class->NumInstanceFields(), 0u);
5901 for (ArtField& field : new_class->GetIFields()) {
5902 if (field.GetDeclaringClass() == temp_class) {
5903 field.SetDeclaringClass(new_class);
5904 }
5905 }
5906
5907 DCHECK_EQ(temp_class->NumStaticFields(), 0u);
5908 for (ArtField& field : new_class->GetSFields()) {
5909 if (field.GetDeclaringClass() == temp_class) {
5910 field.SetDeclaringClass(new_class);
5911 }
5912 }
5913
5914 DCHECK_EQ(temp_class->NumDirectMethods(), 0u);
5915 DCHECK_EQ(temp_class->NumVirtualMethods(), 0u);
5916 for (auto& method : new_class->GetMethods(image_pointer_size_)) {
5917 if (method.GetDeclaringClass() == temp_class) {
5918 method.SetDeclaringClass(new_class);
5919 }
5920 }
5921
5922 // Make sure the remembered set and mod-union tables know that we updated some of the native
5923 // roots.
5924 WriteBarrier::ForEveryFieldWrite(new_class);
5925 }
5926
RegisterClassLoader(ObjPtr<mirror::ClassLoader> class_loader)5927 void ClassLinker::RegisterClassLoader(ObjPtr<mirror::ClassLoader> class_loader) {
5928 CHECK(class_loader->GetAllocator() == nullptr);
5929 CHECK(class_loader->GetClassTable() == nullptr);
5930 Thread* const self = Thread::Current();
5931 ClassLoaderData data;
5932 data.weak_root = self->GetJniEnv()->GetVm()->AddWeakGlobalRef(self, class_loader);
5933 // Create and set the class table.
5934 data.class_table = new ClassTable;
5935 class_loader->SetClassTable(data.class_table);
5936 // Create and set the linear allocator.
5937 data.allocator = Runtime::Current()->CreateLinearAlloc();
5938 class_loader->SetAllocator(data.allocator);
5939 // Add to the list so that we know to free the data later.
5940 class_loaders_.push_back(data);
5941 }
5942
InsertClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader)5943 ClassTable* ClassLinker::InsertClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) {
5944 if (class_loader == nullptr) {
5945 return boot_class_table_.get();
5946 }
5947 ClassTable* class_table = class_loader->GetClassTable();
5948 if (class_table == nullptr) {
5949 RegisterClassLoader(class_loader);
5950 class_table = class_loader->GetClassTable();
5951 DCHECK(class_table != nullptr);
5952 }
5953 return class_table;
5954 }
5955
ClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader)5956 ClassTable* ClassLinker::ClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) {
5957 return class_loader == nullptr ? boot_class_table_.get() : class_loader->GetClassTable();
5958 }
5959
FindSuperImt(ObjPtr<mirror::Class> klass,PointerSize pointer_size)5960 static ImTable* FindSuperImt(ObjPtr<mirror::Class> klass, PointerSize pointer_size)
5961 REQUIRES_SHARED(Locks::mutator_lock_) {
5962 while (klass->HasSuperClass()) {
5963 klass = klass->GetSuperClass();
5964 if (klass->ShouldHaveImt()) {
5965 return klass->GetImt(pointer_size);
5966 }
5967 }
5968 return nullptr;
5969 }
5970
LinkClass(Thread * self,const char * descriptor,Handle<mirror::Class> klass,Handle<mirror::ObjectArray<mirror::Class>> interfaces,MutableHandle<mirror::Class> * h_new_class_out)5971 bool ClassLinker::LinkClass(Thread* self,
5972 const char* descriptor,
5973 Handle<mirror::Class> klass,
5974 Handle<mirror::ObjectArray<mirror::Class>> interfaces,
5975 MutableHandle<mirror::Class>* h_new_class_out) {
5976 CHECK_EQ(ClassStatus::kLoaded, klass->GetStatus());
5977
5978 if (!LinkSuperClass(klass)) {
5979 return false;
5980 }
5981 ArtMethod* imt_data[ImTable::kSize];
5982 // If there are any new conflicts compared to super class.
5983 bool new_conflict = false;
5984 std::fill_n(imt_data, arraysize(imt_data), Runtime::Current()->GetImtUnimplementedMethod());
5985 if (!LinkMethods(self, klass, interfaces, &new_conflict, imt_data)) {
5986 return false;
5987 }
5988 if (!LinkInstanceFields(self, klass)) {
5989 return false;
5990 }
5991 size_t class_size;
5992 if (!LinkStaticFields(self, klass, &class_size)) {
5993 return false;
5994 }
5995 CreateReferenceInstanceOffsets(klass);
5996 CHECK_EQ(ClassStatus::kLoaded, klass->GetStatus());
5997
5998 ImTable* imt = nullptr;
5999 if (klass->ShouldHaveImt()) {
6000 // If there are any new conflicts compared to the super class we can not make a copy. There
6001 // can be cases where both will have a conflict method at the same slot without having the same
6002 // set of conflicts. In this case, we can not share the IMT since the conflict table slow path
6003 // will possibly create a table that is incorrect for either of the classes.
6004 // Same IMT with new_conflict does not happen very often.
6005 if (!new_conflict) {
6006 ImTable* super_imt = FindSuperImt(klass.Get(), image_pointer_size_);
6007 if (super_imt != nullptr) {
6008 bool imt_equals = true;
6009 for (size_t i = 0; i < ImTable::kSize && imt_equals; ++i) {
6010 imt_equals = imt_equals && (super_imt->Get(i, image_pointer_size_) == imt_data[i]);
6011 }
6012 if (imt_equals) {
6013 imt = super_imt;
6014 }
6015 }
6016 }
6017 if (imt == nullptr) {
6018 LinearAlloc* allocator = GetAllocatorForClassLoader(klass->GetClassLoader());
6019 imt = reinterpret_cast<ImTable*>(
6020 allocator->Alloc(self, ImTable::SizeInBytes(image_pointer_size_)));
6021 if (imt == nullptr) {
6022 return false;
6023 }
6024 imt->Populate(imt_data, image_pointer_size_);
6025 }
6026 }
6027
6028 if (!klass->IsTemp() || (!init_done_ && klass->GetClassSize() == class_size)) {
6029 // We don't need to retire this class as it has no embedded tables or it was created the
6030 // correct size during class linker initialization.
6031 CHECK_EQ(klass->GetClassSize(), class_size) << klass->PrettyDescriptor();
6032
6033 if (klass->ShouldHaveEmbeddedVTable()) {
6034 klass->PopulateEmbeddedVTable(image_pointer_size_);
6035 }
6036 if (klass->ShouldHaveImt()) {
6037 klass->SetImt(imt, image_pointer_size_);
6038 }
6039
6040 // Update CHA info based on whether we override methods.
6041 // Have to do this before setting the class as resolved which allows
6042 // instantiation of klass.
6043 if (LIKELY(descriptor != nullptr) && cha_ != nullptr) {
6044 cha_->UpdateAfterLoadingOf(klass);
6045 }
6046
6047 // This will notify waiters on klass that saw the not yet resolved
6048 // class in the class_table_ during EnsureResolved.
6049 mirror::Class::SetStatus(klass, ClassStatus::kResolved, self);
6050 h_new_class_out->Assign(klass.Get());
6051 } else {
6052 CHECK(!klass->IsResolved());
6053 // Retire the temporary class and create the correctly sized resolved class.
6054 StackHandleScope<1> hs(self);
6055 Handle<mirror::Class> h_new_class =
6056 hs.NewHandle(mirror::Class::CopyOf(klass, self, class_size, imt, image_pointer_size_));
6057 // Set arrays to null since we don't want to have multiple classes with the same ArtField or
6058 // ArtMethod array pointers. If this occurs, it causes bugs in remembered sets since the GC
6059 // may not see any references to the target space and clean the card for a class if another
6060 // class had the same array pointer.
6061 klass->SetMethodsPtrUnchecked(nullptr, 0, 0);
6062 klass->SetSFieldsPtrUnchecked(nullptr);
6063 klass->SetIFieldsPtrUnchecked(nullptr);
6064 if (UNLIKELY(h_new_class == nullptr)) {
6065 self->AssertPendingOOMException();
6066 mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self);
6067 return false;
6068 }
6069
6070 CHECK_EQ(h_new_class->GetClassSize(), class_size);
6071 ObjectLock<mirror::Class> lock(self, h_new_class);
6072 FixupTemporaryDeclaringClass(klass.Get(), h_new_class.Get());
6073
6074 if (LIKELY(descriptor != nullptr)) {
6075 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
6076 const ObjPtr<mirror::ClassLoader> class_loader = h_new_class.Get()->GetClassLoader();
6077 ClassTable* const table = InsertClassTableForClassLoader(class_loader);
6078 const ObjPtr<mirror::Class> existing =
6079 table->UpdateClass(descriptor, h_new_class.Get(), ComputeModifiedUtf8Hash(descriptor));
6080 if (class_loader != nullptr) {
6081 // We updated the class in the class table, perform the write barrier so that the GC knows
6082 // about the change.
6083 WriteBarrier::ForEveryFieldWrite(class_loader);
6084 }
6085 CHECK_EQ(existing, klass.Get());
6086 if (log_new_roots_) {
6087 new_class_roots_.push_back(GcRoot<mirror::Class>(h_new_class.Get()));
6088 }
6089 }
6090
6091 // Update CHA info based on whether we override methods.
6092 // Have to do this before setting the class as resolved which allows
6093 // instantiation of klass.
6094 if (LIKELY(descriptor != nullptr) && cha_ != nullptr) {
6095 cha_->UpdateAfterLoadingOf(h_new_class);
6096 }
6097
6098 // This will notify waiters on temp class that saw the not yet resolved class in the
6099 // class_table_ during EnsureResolved.
6100 mirror::Class::SetStatus(klass, ClassStatus::kRetired, self);
6101
6102 CHECK_EQ(h_new_class->GetStatus(), ClassStatus::kResolving);
6103 // This will notify waiters on new_class that saw the not yet resolved
6104 // class in the class_table_ during EnsureResolved.
6105 mirror::Class::SetStatus(h_new_class, ClassStatus::kResolved, self);
6106 // Return the new class.
6107 h_new_class_out->Assign(h_new_class.Get());
6108 }
6109 return true;
6110 }
6111
LoadSuperAndInterfaces(Handle<mirror::Class> klass,const DexFile & dex_file)6112 bool ClassLinker::LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) {
6113 CHECK_EQ(ClassStatus::kIdx, klass->GetStatus());
6114 const dex::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex());
6115 dex::TypeIndex super_class_idx = class_def.superclass_idx_;
6116 if (super_class_idx.IsValid()) {
6117 // Check that a class does not inherit from itself directly.
6118 //
6119 // TODO: This is a cheap check to detect the straightforward case
6120 // of a class extending itself (b/28685551), but we should do a
6121 // proper cycle detection on loaded classes, to detect all cases
6122 // of class circularity errors (b/28830038).
6123 if (super_class_idx == class_def.class_idx_) {
6124 ThrowClassCircularityError(klass.Get(),
6125 "Class %s extends itself",
6126 klass->PrettyDescriptor().c_str());
6127 return false;
6128 }
6129
6130 ObjPtr<mirror::Class> super_class = ResolveType(super_class_idx, klass.Get());
6131 if (super_class == nullptr) {
6132 DCHECK(Thread::Current()->IsExceptionPending());
6133 return false;
6134 }
6135 // Verify
6136 if (!klass->CanAccess(super_class)) {
6137 ThrowIllegalAccessError(klass.Get(), "Class %s extended by class %s is inaccessible",
6138 super_class->PrettyDescriptor().c_str(),
6139 klass->PrettyDescriptor().c_str());
6140 return false;
6141 }
6142 CHECK(super_class->IsResolved());
6143 klass->SetSuperClass(super_class);
6144 }
6145 const dex::TypeList* interfaces = dex_file.GetInterfacesList(class_def);
6146 if (interfaces != nullptr) {
6147 for (size_t i = 0; i < interfaces->Size(); i++) {
6148 dex::TypeIndex idx = interfaces->GetTypeItem(i).type_idx_;
6149 ObjPtr<mirror::Class> interface = ResolveType(idx, klass.Get());
6150 if (interface == nullptr) {
6151 DCHECK(Thread::Current()->IsExceptionPending());
6152 return false;
6153 }
6154 // Verify
6155 if (!klass->CanAccess(interface)) {
6156 // TODO: the RI seemed to ignore this in my testing.
6157 ThrowIllegalAccessError(klass.Get(),
6158 "Interface %s implemented by class %s is inaccessible",
6159 interface->PrettyDescriptor().c_str(),
6160 klass->PrettyDescriptor().c_str());
6161 return false;
6162 }
6163 }
6164 }
6165 // Mark the class as loaded.
6166 mirror::Class::SetStatus(klass, ClassStatus::kLoaded, nullptr);
6167 return true;
6168 }
6169
LinkSuperClass(Handle<mirror::Class> klass)6170 bool ClassLinker::LinkSuperClass(Handle<mirror::Class> klass) {
6171 CHECK(!klass->IsPrimitive());
6172 ObjPtr<mirror::Class> super = klass->GetSuperClass();
6173 ObjPtr<mirror::Class> object_class = GetClassRoot<mirror::Object>(this);
6174 if (klass.Get() == object_class) {
6175 if (super != nullptr) {
6176 ThrowClassFormatError(klass.Get(), "java.lang.Object must not have a superclass");
6177 return false;
6178 }
6179 return true;
6180 }
6181 if (super == nullptr) {
6182 ThrowLinkageError(klass.Get(), "No superclass defined for class %s",
6183 klass->PrettyDescriptor().c_str());
6184 return false;
6185 }
6186 // Verify
6187 if (klass->IsInterface() && super != object_class) {
6188 ThrowClassFormatError(klass.Get(), "Interfaces must have java.lang.Object as superclass");
6189 return false;
6190 }
6191 if (super->IsFinal()) {
6192 ThrowVerifyError(klass.Get(),
6193 "Superclass %s of %s is declared final",
6194 super->PrettyDescriptor().c_str(),
6195 klass->PrettyDescriptor().c_str());
6196 return false;
6197 }
6198 if (super->IsInterface()) {
6199 ThrowIncompatibleClassChangeError(klass.Get(),
6200 "Superclass %s of %s is an interface",
6201 super->PrettyDescriptor().c_str(),
6202 klass->PrettyDescriptor().c_str());
6203 return false;
6204 }
6205 if (!klass->CanAccess(super)) {
6206 ThrowIllegalAccessError(klass.Get(), "Superclass %s is inaccessible to class %s",
6207 super->PrettyDescriptor().c_str(),
6208 klass->PrettyDescriptor().c_str());
6209 return false;
6210 }
6211
6212 // Inherit kAccClassIsFinalizable from the superclass in case this
6213 // class doesn't override finalize.
6214 if (super->IsFinalizable()) {
6215 klass->SetFinalizable();
6216 }
6217
6218 // Inherit class loader flag form super class.
6219 if (super->IsClassLoaderClass()) {
6220 klass->SetClassLoaderClass();
6221 }
6222
6223 // Inherit reference flags (if any) from the superclass.
6224 uint32_t reference_flags = (super->GetClassFlags() & mirror::kClassFlagReference);
6225 if (reference_flags != 0) {
6226 CHECK_EQ(klass->GetClassFlags(), 0u);
6227 klass->SetClassFlags(klass->GetClassFlags() | reference_flags);
6228 }
6229 // Disallow custom direct subclasses of java.lang.ref.Reference.
6230 if (init_done_ && super == GetClassRoot<mirror::Reference>(this)) {
6231 ThrowLinkageError(klass.Get(),
6232 "Class %s attempts to subclass java.lang.ref.Reference, which is not allowed",
6233 klass->PrettyDescriptor().c_str());
6234 return false;
6235 }
6236
6237 if (kIsDebugBuild) {
6238 // Ensure super classes are fully resolved prior to resolving fields..
6239 while (super != nullptr) {
6240 CHECK(super->IsResolved());
6241 super = super->GetSuperClass();
6242 }
6243 }
6244 return true;
6245 }
6246
6247 // A wrapper class representing the result of a method translation used for linking methods and
6248 // updating superclass default methods. For each method in a classes vtable there are 4 states it
6249 // could be in:
6250 // 1) No translation is necessary. In this case there is no MethodTranslation object for it. This
6251 // is the standard case and is true when the method is not overridable by a default method,
6252 // the class defines a concrete implementation of the method, the default method implementation
6253 // remains the same, or an abstract method stayed abstract.
6254 // 2) The method must be translated to a different default method. We note this with
6255 // CreateTranslatedMethod.
6256 // 3) The method must be replaced with a conflict method. This happens when a superclass
6257 // implements an interface with a default method and this class implements an unrelated
6258 // interface that also defines that default method. We note this with CreateConflictingMethod.
6259 // 4) The method must be replaced with an abstract miranda method. This happens when a superclass
6260 // implements an interface with a default method and this class implements a subinterface of
6261 // the superclass's interface which declares the default method abstract. We note this with
6262 // CreateAbstractMethod.
6263 //
6264 // When a method translation is unnecessary (case #1), we don't put it into the
6265 // default_translation maps. So an instance of MethodTranslation must be in one of #2-#4.
6266 class ClassLinker::MethodTranslation {
6267 public:
MethodTranslation()6268 MethodTranslation() : translation_(nullptr), type_(Type::kInvalid) {}
6269
6270 // This slot must become a default conflict method.
CreateConflictingMethod()6271 static MethodTranslation CreateConflictingMethod() {
6272 return MethodTranslation(Type::kConflict, /*translation=*/nullptr);
6273 }
6274
6275 // This slot must become an abstract method.
CreateAbstractMethod()6276 static MethodTranslation CreateAbstractMethod() {
6277 return MethodTranslation(Type::kAbstract, /*translation=*/nullptr);
6278 }
6279
6280 // Use the given method as the current value for this vtable slot during translation.
CreateTranslatedMethod(ArtMethod * new_method)6281 static MethodTranslation CreateTranslatedMethod(ArtMethod* new_method) {
6282 return MethodTranslation(Type::kTranslation, new_method);
6283 }
6284
6285 // Returns true if this is a method that must become a conflict method.
IsInConflict() const6286 bool IsInConflict() const {
6287 return type_ == Type::kConflict;
6288 }
6289
6290 // Returns true if this is a method that must become an abstract method.
IsAbstract() const6291 bool IsAbstract() const {
6292 return type_ == Type::kAbstract;
6293 }
6294
6295 // Returns true if this is a method that must become a different method.
IsTranslation() const6296 bool IsTranslation() const {
6297 return type_ == Type::kTranslation;
6298 }
6299
6300 // Get the translated version of this method.
GetTranslation() const6301 ArtMethod* GetTranslation() const {
6302 DCHECK(IsTranslation());
6303 DCHECK(translation_ != nullptr);
6304 return translation_;
6305 }
6306
6307 private:
6308 enum class Type {
6309 kInvalid,
6310 kTranslation,
6311 kConflict,
6312 kAbstract,
6313 };
6314
MethodTranslation(Type type,ArtMethod * translation)6315 MethodTranslation(Type type, ArtMethod* translation)
6316 : translation_(translation), type_(type) {}
6317
6318 ArtMethod* translation_;
6319 Type type_;
6320 };
6321
6322 // Populate the class vtable and itable. Compute return type indices.
LinkMethods(Thread * self,Handle<mirror::Class> klass,Handle<mirror::ObjectArray<mirror::Class>> interfaces,bool * out_new_conflict,ArtMethod ** out_imt)6323 bool ClassLinker::LinkMethods(Thread* self,
6324 Handle<mirror::Class> klass,
6325 Handle<mirror::ObjectArray<mirror::Class>> interfaces,
6326 bool* out_new_conflict,
6327 ArtMethod** out_imt) {
6328 self->AllowThreadSuspension();
6329 // A map from vtable indexes to the method they need to be updated to point to. Used because we
6330 // need to have default methods be in the virtuals array of each class but we don't set that up
6331 // until LinkInterfaceMethods.
6332 constexpr size_t kBufferSize = 8; // Avoid malloc/free for a few translations.
6333 std::pair<size_t, ClassLinker::MethodTranslation> buffer[kBufferSize];
6334 HashMap<size_t, ClassLinker::MethodTranslation> default_translations(buffer, kBufferSize);
6335 // Link virtual methods then interface methods.
6336 // We set up the interface lookup table first because we need it to determine if we need to update
6337 // any vtable entries with new default method implementations.
6338 return SetupInterfaceLookupTable(self, klass, interfaces)
6339 && LinkVirtualMethods(self, klass, /*out*/ &default_translations)
6340 && LinkInterfaceMethods(self, klass, default_translations, out_new_conflict, out_imt);
6341 }
6342
6343 // Comparator for name and signature of a method, used in finding overriding methods. Implementation
6344 // avoids the use of handles, if it didn't then rather than compare dex files we could compare dex
6345 // caches in the implementation below.
6346 class MethodNameAndSignatureComparator final : public ValueObject {
6347 public:
6348 explicit MethodNameAndSignatureComparator(ArtMethod* method)
REQUIRES_SHARED(Locks::mutator_lock_)6349 REQUIRES_SHARED(Locks::mutator_lock_) :
6350 dex_file_(method->GetDexFile()), mid_(&dex_file_->GetMethodId(method->GetDexMethodIndex())),
6351 name_(nullptr), name_len_(0) {
6352 DCHECK(!method->IsProxyMethod()) << method->PrettyMethod();
6353 }
6354
GetName()6355 const char* GetName() {
6356 if (name_ == nullptr) {
6357 name_ = dex_file_->StringDataAndUtf16LengthByIdx(mid_->name_idx_, &name_len_);
6358 }
6359 return name_;
6360 }
6361
HasSameNameAndSignature(ArtMethod * other)6362 bool HasSameNameAndSignature(ArtMethod* other)
6363 REQUIRES_SHARED(Locks::mutator_lock_) {
6364 DCHECK(!other->IsProxyMethod()) << other->PrettyMethod();
6365 const DexFile* other_dex_file = other->GetDexFile();
6366 const dex::MethodId& other_mid = other_dex_file->GetMethodId(other->GetDexMethodIndex());
6367 if (dex_file_ == other_dex_file) {
6368 return mid_->name_idx_ == other_mid.name_idx_ && mid_->proto_idx_ == other_mid.proto_idx_;
6369 }
6370 GetName(); // Only used to make sure its calculated.
6371 uint32_t other_name_len;
6372 const char* other_name = other_dex_file->StringDataAndUtf16LengthByIdx(other_mid.name_idx_,
6373 &other_name_len);
6374 if (name_len_ != other_name_len || strcmp(name_, other_name) != 0) {
6375 return false;
6376 }
6377 return dex_file_->GetMethodSignature(*mid_) == other_dex_file->GetMethodSignature(other_mid);
6378 }
6379
6380 private:
6381 // Dex file for the method to compare against.
6382 const DexFile* const dex_file_;
6383 // MethodId for the method to compare against.
6384 const dex::MethodId* const mid_;
6385 // Lazily computed name from the dex file's strings.
6386 const char* name_;
6387 // Lazily computed name length.
6388 uint32_t name_len_;
6389 };
6390
6391 class LinkVirtualHashTable {
6392 public:
LinkVirtualHashTable(Handle<mirror::Class> klass,size_t hash_size,uint32_t * hash_table,PointerSize image_pointer_size)6393 LinkVirtualHashTable(Handle<mirror::Class> klass,
6394 size_t hash_size,
6395 uint32_t* hash_table,
6396 PointerSize image_pointer_size)
6397 : klass_(klass),
6398 hash_size_(hash_size),
6399 hash_table_(hash_table),
6400 image_pointer_size_(image_pointer_size) {
6401 std::fill(hash_table_, hash_table_ + hash_size_, invalid_index_);
6402 }
6403
Add(uint32_t virtual_method_index)6404 void Add(uint32_t virtual_method_index) REQUIRES_SHARED(Locks::mutator_lock_) {
6405 ArtMethod* local_method = klass_->GetVirtualMethodDuringLinking(
6406 virtual_method_index, image_pointer_size_);
6407 const char* name = local_method->GetInterfaceMethodIfProxy(image_pointer_size_)->GetName();
6408 uint32_t hash = ComputeModifiedUtf8Hash(name);
6409 uint32_t index = hash % hash_size_;
6410 // Linear probe until we have an empty slot.
6411 while (hash_table_[index] != invalid_index_) {
6412 if (++index == hash_size_) {
6413 index = 0;
6414 }
6415 }
6416 hash_table_[index] = virtual_method_index;
6417 }
6418
FindAndRemove(MethodNameAndSignatureComparator * comparator,uint32_t hash)6419 uint32_t FindAndRemove(MethodNameAndSignatureComparator* comparator, uint32_t hash)
6420 REQUIRES_SHARED(Locks::mutator_lock_) {
6421 DCHECK_EQ(hash, ComputeModifiedUtf8Hash(comparator->GetName()));
6422 size_t index = hash % hash_size_;
6423 while (true) {
6424 const uint32_t value = hash_table_[index];
6425 // Since linear probe makes continuous blocks, hitting an invalid index means we are done
6426 // the block and can safely assume not found.
6427 if (value == invalid_index_) {
6428 break;
6429 }
6430 if (value != removed_index_) { // This signifies not already overriden.
6431 ArtMethod* virtual_method =
6432 klass_->GetVirtualMethodDuringLinking(value, image_pointer_size_);
6433 if (comparator->HasSameNameAndSignature(
6434 virtual_method->GetInterfaceMethodIfProxy(image_pointer_size_))) {
6435 hash_table_[index] = removed_index_;
6436 return value;
6437 }
6438 }
6439 if (++index == hash_size_) {
6440 index = 0;
6441 }
6442 }
6443 return GetNotFoundIndex();
6444 }
6445
GetNotFoundIndex()6446 static uint32_t GetNotFoundIndex() {
6447 return invalid_index_;
6448 }
6449
6450 private:
6451 static const uint32_t invalid_index_;
6452 static const uint32_t removed_index_;
6453
6454 Handle<mirror::Class> klass_;
6455 const size_t hash_size_;
6456 uint32_t* const hash_table_;
6457 const PointerSize image_pointer_size_;
6458 };
6459
6460 const uint32_t LinkVirtualHashTable::invalid_index_ = std::numeric_limits<uint32_t>::max();
6461 const uint32_t LinkVirtualHashTable::removed_index_ = std::numeric_limits<uint32_t>::max() - 1;
6462
LinkVirtualMethods(Thread * self,Handle<mirror::Class> klass,HashMap<size_t,ClassLinker::MethodTranslation> * default_translations)6463 bool ClassLinker::LinkVirtualMethods(
6464 Thread* self,
6465 Handle<mirror::Class> klass,
6466 /*out*/HashMap<size_t, ClassLinker::MethodTranslation>* default_translations) {
6467 const size_t num_virtual_methods = klass->NumVirtualMethods();
6468 if (klass->IsInterface()) {
6469 // No vtable.
6470 if (!IsUint<16>(num_virtual_methods)) {
6471 ThrowClassFormatError(klass.Get(), "Too many methods on interface: %zu", num_virtual_methods);
6472 return false;
6473 }
6474 bool has_defaults = false;
6475 // Assign each method an IMT index and set the default flag.
6476 for (size_t i = 0; i < num_virtual_methods; ++i) {
6477 ArtMethod* m = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_);
6478 m->SetMethodIndex(i);
6479 if (!m->IsAbstract()) {
6480 // If the dex file does not support default methods, throw ClassFormatError.
6481 // This check is necessary to protect from odd cases, such as native default
6482 // methods, that the dex file verifier permits for old dex file versions. b/157170505
6483 // FIXME: This should be `if (!m->GetDexFile()->SupportsDefaultMethods())` but we're
6484 // currently running CTS tests for default methods with dex file version 035 which
6485 // does not support default methods. So, we limit this to native methods. b/157718952
6486 if (m->IsNative()) {
6487 DCHECK(!m->GetDexFile()->SupportsDefaultMethods());
6488 ThrowClassFormatError(klass.Get(),
6489 "Dex file does not support default method '%s'",
6490 m->PrettyMethod().c_str());
6491 return false;
6492 }
6493 m->SetAccessFlags(m->GetAccessFlags() | kAccDefault);
6494 has_defaults = true;
6495 }
6496 }
6497 // Mark that we have default methods so that we won't need to scan the virtual_methods_ array
6498 // during initialization. This is a performance optimization. We could simply traverse the
6499 // virtual_methods_ array again during initialization.
6500 if (has_defaults) {
6501 klass->SetHasDefaultMethods();
6502 }
6503 return true;
6504 } else if (klass->HasSuperClass()) {
6505 const size_t super_vtable_length = klass->GetSuperClass()->GetVTableLength();
6506 const size_t max_count = num_virtual_methods + super_vtable_length;
6507 StackHandleScope<3> hs(self);
6508 Handle<mirror::Class> super_class(hs.NewHandle(klass->GetSuperClass()));
6509 MutableHandle<mirror::PointerArray> vtable;
6510 if (super_class->ShouldHaveEmbeddedVTable()) {
6511 vtable = hs.NewHandle(AllocPointerArray(self, max_count));
6512 if (UNLIKELY(vtable == nullptr)) {
6513 self->AssertPendingOOMException();
6514 return false;
6515 }
6516 for (size_t i = 0; i < super_vtable_length; i++) {
6517 vtable->SetElementPtrSize(
6518 i, super_class->GetEmbeddedVTableEntry(i, image_pointer_size_), image_pointer_size_);
6519 }
6520 // We might need to change vtable if we have new virtual methods or new interfaces (since that
6521 // might give us new default methods). If no new interfaces then we can skip the rest since
6522 // the class cannot override any of the super-class's methods. This is required for
6523 // correctness since without it we might not update overridden default method vtable entries
6524 // correctly.
6525 if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) {
6526 klass->SetVTable(vtable.Get());
6527 return true;
6528 }
6529 } else {
6530 DCHECK(super_class->IsAbstract() && !super_class->IsArrayClass());
6531 Handle<mirror::PointerArray> super_vtable = hs.NewHandle(super_class->GetVTable());
6532 CHECK(super_vtable != nullptr) << super_class->PrettyClass();
6533 // We might need to change vtable if we have new virtual methods or new interfaces (since that
6534 // might give us new default methods). See comment above.
6535 if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) {
6536 klass->SetVTable(super_vtable.Get());
6537 return true;
6538 }
6539 vtable = hs.NewHandle(ObjPtr<mirror::PointerArray>::DownCast(
6540 mirror::Array::CopyOf(super_vtable, self, max_count)));
6541 if (UNLIKELY(vtable == nullptr)) {
6542 self->AssertPendingOOMException();
6543 return false;
6544 }
6545 }
6546 // How the algorithm works:
6547 // 1. Populate hash table by adding num_virtual_methods from klass. The values in the hash
6548 // table are: invalid_index for unused slots, index super_vtable_length + i for a virtual
6549 // method which has not been matched to a vtable method, and j if the virtual method at the
6550 // index overrode the super virtual method at index j.
6551 // 2. Loop through super virtual methods, if they overwrite, update hash table to j
6552 // (j < super_vtable_length) to avoid redundant checks. (TODO maybe use this info for reducing
6553 // the need for the initial vtable which we later shrink back down).
6554 // 3. Add non overridden methods to the end of the vtable.
6555 static constexpr size_t kMaxStackHash = 250;
6556 // + 1 so that even if we only have new default methods we will still be able to use this hash
6557 // table (i.e. it will never have 0 size).
6558 const size_t hash_table_size = num_virtual_methods * 3 + 1;
6559 uint32_t* hash_table_ptr;
6560 std::unique_ptr<uint32_t[]> hash_heap_storage;
6561 if (hash_table_size <= kMaxStackHash) {
6562 hash_table_ptr = reinterpret_cast<uint32_t*>(
6563 alloca(hash_table_size * sizeof(*hash_table_ptr)));
6564 } else {
6565 hash_heap_storage.reset(new uint32_t[hash_table_size]);
6566 hash_table_ptr = hash_heap_storage.get();
6567 }
6568 LinkVirtualHashTable hash_table(klass, hash_table_size, hash_table_ptr, image_pointer_size_);
6569 // Add virtual methods to the hash table.
6570 for (size_t i = 0; i < num_virtual_methods; ++i) {
6571 DCHECK(klass->GetVirtualMethodDuringLinking(
6572 i, image_pointer_size_)->GetDeclaringClass() != nullptr);
6573 hash_table.Add(i);
6574 }
6575 // Loop through each super vtable method and see if they are overridden by a method we added to
6576 // the hash table.
6577 for (size_t j = 0; j < super_vtable_length; ++j) {
6578 // Search the hash table to see if we are overridden by any method.
6579 ArtMethod* super_method = vtable->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_);
6580 if (!klass->CanAccessMember(super_method->GetDeclaringClass(),
6581 super_method->GetAccessFlags())) {
6582 // Continue on to the next method since this one is package private and canot be overridden.
6583 // Before Android 4.1, the package-private method super_method might have been incorrectly
6584 // overridden.
6585 continue;
6586 }
6587 MethodNameAndSignatureComparator super_method_name_comparator(
6588 super_method->GetInterfaceMethodIfProxy(image_pointer_size_));
6589 // We remove the method so that subsequent lookups will be faster by making the hash-map
6590 // smaller as we go on.
6591 uint32_t hash = (j < mirror::Object::kVTableLength)
6592 ? object_virtual_method_hashes_[j]
6593 : ComputeModifiedUtf8Hash(super_method_name_comparator.GetName());
6594 uint32_t hash_index = hash_table.FindAndRemove(&super_method_name_comparator, hash);
6595 if (hash_index != hash_table.GetNotFoundIndex()) {
6596 ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(
6597 hash_index, image_pointer_size_);
6598 if (super_method->IsFinal()) {
6599 ThrowLinkageError(klass.Get(), "Method %s overrides final method in class %s",
6600 virtual_method->PrettyMethod().c_str(),
6601 super_method->GetDeclaringClassDescriptor());
6602 return false;
6603 }
6604 vtable->SetElementPtrSize(j, virtual_method, image_pointer_size_);
6605 virtual_method->SetMethodIndex(j);
6606 } else if (super_method->IsOverridableByDefaultMethod()) {
6607 // We didn't directly override this method but we might through default methods...
6608 // Check for default method update.
6609 ArtMethod* default_method = nullptr;
6610 switch (FindDefaultMethodImplementation(self,
6611 super_method,
6612 klass,
6613 /*out*/&default_method)) {
6614 case DefaultMethodSearchResult::kDefaultConflict: {
6615 // A conflict was found looking for default methods. Note this (assuming it wasn't
6616 // pre-existing) in the translations map.
6617 if (UNLIKELY(!super_method->IsDefaultConflicting())) {
6618 // Don't generate another conflict method to reduce memory use as an optimization.
6619 default_translations->insert(
6620 {j, ClassLinker::MethodTranslation::CreateConflictingMethod()});
6621 }
6622 break;
6623 }
6624 case DefaultMethodSearchResult::kAbstractFound: {
6625 // No conflict but method is abstract.
6626 // We note that this vtable entry must be made abstract.
6627 if (UNLIKELY(!super_method->IsAbstract())) {
6628 default_translations->insert(
6629 {j, ClassLinker::MethodTranslation::CreateAbstractMethod()});
6630 }
6631 break;
6632 }
6633 case DefaultMethodSearchResult::kDefaultFound: {
6634 if (UNLIKELY(super_method->IsDefaultConflicting() ||
6635 default_method->GetDeclaringClass() != super_method->GetDeclaringClass())) {
6636 // Found a default method implementation that is new.
6637 // TODO Refactor this add default methods to virtuals here and not in
6638 // LinkInterfaceMethods maybe.
6639 // The problem is default methods might override previously present
6640 // default-method or miranda-method vtable entries from the superclass.
6641 // Unfortunately we need these to be entries in this class's virtuals. We do not
6642 // give these entries there until LinkInterfaceMethods so we pass this map around
6643 // to let it know which vtable entries need to be updated.
6644 // Make a note that vtable entry j must be updated, store what it needs to be updated
6645 // to. We will allocate a virtual method slot in LinkInterfaceMethods and fix it up
6646 // then.
6647 default_translations->insert(
6648 {j, ClassLinker::MethodTranslation::CreateTranslatedMethod(default_method)});
6649 VLOG(class_linker) << "Method " << super_method->PrettyMethod()
6650 << " overridden by default "
6651 << default_method->PrettyMethod()
6652 << " in " << mirror::Class::PrettyClass(klass.Get());
6653 }
6654 break;
6655 }
6656 }
6657 }
6658 }
6659 size_t actual_count = super_vtable_length;
6660 // Add the non-overridden methods at the end.
6661 for (size_t i = 0; i < num_virtual_methods; ++i) {
6662 ArtMethod* local_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_);
6663 size_t method_idx = local_method->GetMethodIndexDuringLinking();
6664 if (method_idx < super_vtable_length &&
6665 local_method == vtable->GetElementPtrSize<ArtMethod*>(method_idx, image_pointer_size_)) {
6666 continue;
6667 }
6668 vtable->SetElementPtrSize(actual_count, local_method, image_pointer_size_);
6669 local_method->SetMethodIndex(actual_count);
6670 ++actual_count;
6671 }
6672 if (!IsUint<16>(actual_count)) {
6673 ThrowClassFormatError(klass.Get(), "Too many methods defined on class: %zd", actual_count);
6674 return false;
6675 }
6676 // Shrink vtable if possible
6677 CHECK_LE(actual_count, max_count);
6678 if (actual_count < max_count) {
6679 vtable.Assign(ObjPtr<mirror::PointerArray>::DownCast(
6680 mirror::Array::CopyOf(vtable, self, actual_count)));
6681 if (UNLIKELY(vtable == nullptr)) {
6682 self->AssertPendingOOMException();
6683 return false;
6684 }
6685 }
6686 klass->SetVTable(vtable.Get());
6687 } else {
6688 CHECK_EQ(klass.Get(), GetClassRoot<mirror::Object>(this));
6689 if (!IsUint<16>(num_virtual_methods)) {
6690 ThrowClassFormatError(klass.Get(), "Too many methods: %d",
6691 static_cast<int>(num_virtual_methods));
6692 return false;
6693 }
6694 ObjPtr<mirror::PointerArray> vtable = AllocPointerArray(self, num_virtual_methods);
6695 if (UNLIKELY(vtable == nullptr)) {
6696 self->AssertPendingOOMException();
6697 return false;
6698 }
6699 for (size_t i = 0; i < num_virtual_methods; ++i) {
6700 ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_);
6701 vtable->SetElementPtrSize(i, virtual_method, image_pointer_size_);
6702 virtual_method->SetMethodIndex(i & 0xFFFF);
6703 }
6704 klass->SetVTable(vtable);
6705 InitializeObjectVirtualMethodHashes(klass.Get(),
6706 image_pointer_size_,
6707 ArrayRef<uint32_t>(object_virtual_method_hashes_));
6708 }
6709 return true;
6710 }
6711
6712 // Determine if the given iface has any subinterface in the given list that declares the method
6713 // specified by 'target'.
6714 //
6715 // Arguments
6716 // - self: The thread we are running on
6717 // - target: A comparator that will match any method that overrides the method we are checking for
6718 // - iftable: The iftable we are searching for an overriding method on.
6719 // - ifstart: The index of the interface we are checking to see if anything overrides
6720 // - iface: The interface we are checking to see if anything overrides.
6721 // - image_pointer_size:
6722 // The image pointer size.
6723 //
6724 // Returns
6725 // - True: There is some method that matches the target comparator defined in an interface that
6726 // is a subtype of iface.
6727 // - False: There is no method that matches the target comparator in any interface that is a subtype
6728 // of iface.
ContainsOverridingMethodOf(Thread * self,MethodNameAndSignatureComparator & target,Handle<mirror::IfTable> iftable,size_t ifstart,Handle<mirror::Class> iface,PointerSize image_pointer_size)6729 static bool ContainsOverridingMethodOf(Thread* self,
6730 MethodNameAndSignatureComparator& target,
6731 Handle<mirror::IfTable> iftable,
6732 size_t ifstart,
6733 Handle<mirror::Class> iface,
6734 PointerSize image_pointer_size)
6735 REQUIRES_SHARED(Locks::mutator_lock_) {
6736 DCHECK(self != nullptr);
6737 DCHECK(iface != nullptr);
6738 DCHECK(iftable != nullptr);
6739 DCHECK_GE(ifstart, 0u);
6740 DCHECK_LT(ifstart, iftable->Count());
6741 DCHECK_EQ(iface.Get(), iftable->GetInterface(ifstart));
6742 DCHECK(iface->IsInterface());
6743
6744 size_t iftable_count = iftable->Count();
6745 StackHandleScope<1> hs(self);
6746 MutableHandle<mirror::Class> current_iface(hs.NewHandle<mirror::Class>(nullptr));
6747 for (size_t k = ifstart + 1; k < iftable_count; k++) {
6748 // Skip ifstart since our current interface obviously cannot override itself.
6749 current_iface.Assign(iftable->GetInterface(k));
6750 // Iterate through every method on this interface. The order does not matter.
6751 for (ArtMethod& current_method : current_iface->GetDeclaredVirtualMethods(image_pointer_size)) {
6752 if (UNLIKELY(target.HasSameNameAndSignature(
6753 current_method.GetInterfaceMethodIfProxy(image_pointer_size)))) {
6754 // Check if the i'th interface is a subtype of this one.
6755 if (iface->IsAssignableFrom(current_iface.Get())) {
6756 return true;
6757 }
6758 break;
6759 }
6760 }
6761 }
6762 return false;
6763 }
6764
6765 // Find the default method implementation for 'interface_method' in 'klass'. Stores it into
6766 // out_default_method and returns kDefaultFound on success. If no default method was found return
6767 // kAbstractFound and store nullptr into out_default_method. If an error occurs (such as a
6768 // default_method conflict) it will return kDefaultConflict.
FindDefaultMethodImplementation(Thread * self,ArtMethod * target_method,Handle<mirror::Class> klass,ArtMethod ** out_default_method) const6769 ClassLinker::DefaultMethodSearchResult ClassLinker::FindDefaultMethodImplementation(
6770 Thread* self,
6771 ArtMethod* target_method,
6772 Handle<mirror::Class> klass,
6773 /*out*/ArtMethod** out_default_method) const {
6774 DCHECK(self != nullptr);
6775 DCHECK(target_method != nullptr);
6776 DCHECK(out_default_method != nullptr);
6777
6778 *out_default_method = nullptr;
6779
6780 // We organize the interface table so that, for interface I any subinterfaces J follow it in the
6781 // table. This lets us walk the table backwards when searching for default methods. The first one
6782 // we encounter is the best candidate since it is the most specific. Once we have found it we keep
6783 // track of it and then continue checking all other interfaces, since we need to throw an error if
6784 // we encounter conflicting default method implementations (one is not a subtype of the other).
6785 //
6786 // The order of unrelated interfaces does not matter and is not defined.
6787 size_t iftable_count = klass->GetIfTableCount();
6788 if (iftable_count == 0) {
6789 // No interfaces. We have already reset out to null so just return kAbstractFound.
6790 return DefaultMethodSearchResult::kAbstractFound;
6791 }
6792
6793 StackHandleScope<3> hs(self);
6794 MutableHandle<mirror::Class> chosen_iface(hs.NewHandle<mirror::Class>(nullptr));
6795 MutableHandle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable()));
6796 MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr));
6797 MethodNameAndSignatureComparator target_name_comparator(
6798 target_method->GetInterfaceMethodIfProxy(image_pointer_size_));
6799 // Iterates over the klass's iftable in reverse
6800 for (size_t k = iftable_count; k != 0; ) {
6801 --k;
6802
6803 DCHECK_LT(k, iftable->Count());
6804
6805 iface.Assign(iftable->GetInterface(k));
6806 // Iterate through every declared method on this interface. The order does not matter.
6807 for (auto& method_iter : iface->GetDeclaredVirtualMethods(image_pointer_size_)) {
6808 ArtMethod* current_method = &method_iter;
6809 // Skip abstract methods and methods with different names.
6810 if (current_method->IsAbstract() ||
6811 !target_name_comparator.HasSameNameAndSignature(
6812 current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) {
6813 continue;
6814 } else if (!current_method->IsPublic()) {
6815 // The verifier should have caught the non-public method for dex version 37. Just warn and
6816 // skip it since this is from before default-methods so we don't really need to care that it
6817 // has code.
6818 LOG(WARNING) << "Interface method " << current_method->PrettyMethod()
6819 << " is not public! "
6820 << "This will be a fatal error in subsequent versions of android. "
6821 << "Continuing anyway.";
6822 }
6823 if (UNLIKELY(chosen_iface != nullptr)) {
6824 // We have multiple default impls of the same method. This is a potential default conflict.
6825 // We need to check if this possibly conflicting method is either a superclass of the chosen
6826 // default implementation or is overridden by a non-default interface method. In either case
6827 // there is no conflict.
6828 if (!iface->IsAssignableFrom(chosen_iface.Get()) &&
6829 !ContainsOverridingMethodOf(self,
6830 target_name_comparator,
6831 iftable,
6832 k,
6833 iface,
6834 image_pointer_size_)) {
6835 VLOG(class_linker) << "Conflicting default method implementations found: "
6836 << current_method->PrettyMethod() << " and "
6837 << ArtMethod::PrettyMethod(*out_default_method) << " in class "
6838 << klass->PrettyClass() << " conflict.";
6839 *out_default_method = nullptr;
6840 return DefaultMethodSearchResult::kDefaultConflict;
6841 } else {
6842 break; // Continue checking at the next interface.
6843 }
6844 } else {
6845 // chosen_iface == null
6846 if (!ContainsOverridingMethodOf(self,
6847 target_name_comparator,
6848 iftable,
6849 k,
6850 iface,
6851 image_pointer_size_)) {
6852 // Don't set this as the chosen interface if something else is overriding it (because that
6853 // other interface would be potentially chosen instead if it was default). If the other
6854 // interface was abstract then we wouldn't select this interface as chosen anyway since
6855 // the abstract method masks it.
6856 *out_default_method = current_method;
6857 chosen_iface.Assign(iface.Get());
6858 // We should now finish traversing the graph to find if we have default methods that
6859 // conflict.
6860 } else {
6861 VLOG(class_linker) << "A default method '" << current_method->PrettyMethod()
6862 << "' was "
6863 << "skipped because it was overridden by an abstract method in a "
6864 << "subinterface on class '" << klass->PrettyClass() << "'";
6865 }
6866 }
6867 break;
6868 }
6869 }
6870 if (*out_default_method != nullptr) {
6871 VLOG(class_linker) << "Default method '" << (*out_default_method)->PrettyMethod()
6872 << "' selected "
6873 << "as the implementation for '" << target_method->PrettyMethod()
6874 << "' in '" << klass->PrettyClass() << "'";
6875 return DefaultMethodSearchResult::kDefaultFound;
6876 } else {
6877 return DefaultMethodSearchResult::kAbstractFound;
6878 }
6879 }
6880
AddMethodToConflictTable(ObjPtr<mirror::Class> klass,ArtMethod * conflict_method,ArtMethod * interface_method,ArtMethod * method)6881 ArtMethod* ClassLinker::AddMethodToConflictTable(ObjPtr<mirror::Class> klass,
6882 ArtMethod* conflict_method,
6883 ArtMethod* interface_method,
6884 ArtMethod* method) {
6885 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
6886 Runtime* const runtime = Runtime::Current();
6887 LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader());
6888
6889 // Create a new entry if the existing one is the shared conflict method.
6890 ArtMethod* new_conflict_method = (conflict_method == runtime->GetImtConflictMethod())
6891 ? runtime->CreateImtConflictMethod(linear_alloc)
6892 : conflict_method;
6893
6894 // Allocate a new table. Note that we will leak this table at the next conflict,
6895 // but that's a tradeoff compared to making the table fixed size.
6896 void* data = linear_alloc->Alloc(
6897 Thread::Current(), ImtConflictTable::ComputeSizeWithOneMoreEntry(current_table,
6898 image_pointer_size_));
6899 if (data == nullptr) {
6900 LOG(ERROR) << "Failed to allocate conflict table";
6901 return conflict_method;
6902 }
6903 ImtConflictTable* new_table = new (data) ImtConflictTable(current_table,
6904 interface_method,
6905 method,
6906 image_pointer_size_);
6907
6908 // Do a fence to ensure threads see the data in the table before it is assigned
6909 // to the conflict method.
6910 // Note that there is a race in the presence of multiple threads and we may leak
6911 // memory from the LinearAlloc, but that's a tradeoff compared to using
6912 // atomic operations.
6913 std::atomic_thread_fence(std::memory_order_release);
6914 new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_);
6915 return new_conflict_method;
6916 }
6917
AllocateIfTableMethodArrays(Thread * self,Handle<mirror::Class> klass,Handle<mirror::IfTable> iftable)6918 bool ClassLinker::AllocateIfTableMethodArrays(Thread* self,
6919 Handle<mirror::Class> klass,
6920 Handle<mirror::IfTable> iftable) {
6921 DCHECK(!klass->IsInterface());
6922 const bool has_superclass = klass->HasSuperClass();
6923 const bool extend_super_iftable = has_superclass;
6924 const size_t ifcount = klass->GetIfTableCount();
6925 const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U;
6926 for (size_t i = 0; i < ifcount; ++i) {
6927 size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods();
6928 if (num_methods > 0) {
6929 const bool is_super = i < super_ifcount;
6930 // This is an interface implemented by a super-class. Therefore we can just copy the method
6931 // array from the superclass.
6932 const bool super_interface = is_super && extend_super_iftable;
6933 ObjPtr<mirror::PointerArray> method_array;
6934 if (super_interface) {
6935 ObjPtr<mirror::IfTable> if_table = klass->GetSuperClass()->GetIfTable();
6936 DCHECK(if_table != nullptr);
6937 DCHECK(if_table->GetMethodArray(i) != nullptr);
6938 // If we are working on a super interface, try extending the existing method array.
6939 StackHandleScope<1u> hs(self);
6940 Handle<mirror::PointerArray> old_array = hs.NewHandle(if_table->GetMethodArray(i));
6941 method_array =
6942 ObjPtr<mirror::PointerArray>::DownCast(mirror::Object::Clone(old_array, self));
6943 } else {
6944 method_array = AllocPointerArray(self, num_methods);
6945 }
6946 if (UNLIKELY(method_array == nullptr)) {
6947 self->AssertPendingOOMException();
6948 return false;
6949 }
6950 iftable->SetMethodArray(i, method_array);
6951 }
6952 }
6953 return true;
6954 }
6955
SetIMTRef(ArtMethod * unimplemented_method,ArtMethod * imt_conflict_method,ArtMethod * current_method,bool * new_conflict,ArtMethod ** imt_ref)6956 void ClassLinker::SetIMTRef(ArtMethod* unimplemented_method,
6957 ArtMethod* imt_conflict_method,
6958 ArtMethod* current_method,
6959 /*out*/bool* new_conflict,
6960 /*out*/ArtMethod** imt_ref) {
6961 // Place method in imt if entry is empty, place conflict otherwise.
6962 if (*imt_ref == unimplemented_method) {
6963 *imt_ref = current_method;
6964 } else if (!(*imt_ref)->IsRuntimeMethod()) {
6965 // If we are not a conflict and we have the same signature and name as the imt
6966 // entry, it must be that we overwrote a superclass vtable entry.
6967 // Note that we have checked IsRuntimeMethod, as there may be multiple different
6968 // conflict methods.
6969 MethodNameAndSignatureComparator imt_comparator(
6970 (*imt_ref)->GetInterfaceMethodIfProxy(image_pointer_size_));
6971 if (imt_comparator.HasSameNameAndSignature(
6972 current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) {
6973 *imt_ref = current_method;
6974 } else {
6975 *imt_ref = imt_conflict_method;
6976 *new_conflict = true;
6977 }
6978 } else {
6979 // Place the default conflict method. Note that there may be an existing conflict
6980 // method in the IMT, but it could be one tailored to the super class, with a
6981 // specific ImtConflictTable.
6982 *imt_ref = imt_conflict_method;
6983 *new_conflict = true;
6984 }
6985 }
6986
FillIMTAndConflictTables(ObjPtr<mirror::Class> klass)6987 void ClassLinker::FillIMTAndConflictTables(ObjPtr<mirror::Class> klass) {
6988 DCHECK(klass->ShouldHaveImt()) << klass->PrettyClass();
6989 DCHECK(!klass->IsTemp()) << klass->PrettyClass();
6990 ArtMethod* imt_data[ImTable::kSize];
6991 Runtime* const runtime = Runtime::Current();
6992 ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod();
6993 ArtMethod* const conflict_method = runtime->GetImtConflictMethod();
6994 std::fill_n(imt_data, arraysize(imt_data), unimplemented_method);
6995 if (klass->GetIfTable() != nullptr) {
6996 bool new_conflict = false;
6997 FillIMTFromIfTable(klass->GetIfTable(),
6998 unimplemented_method,
6999 conflict_method,
7000 klass,
7001 /*create_conflict_tables=*/true,
7002 /*ignore_copied_methods=*/false,
7003 &new_conflict,
7004 &imt_data[0]);
7005 }
7006 // Compare the IMT with the super class including the conflict methods. If they are equivalent,
7007 // we can just use the same pointer.
7008 ImTable* imt = nullptr;
7009 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
7010 if (super_class != nullptr && super_class->ShouldHaveImt()) {
7011 ImTable* super_imt = super_class->GetImt(image_pointer_size_);
7012 bool same = true;
7013 for (size_t i = 0; same && i < ImTable::kSize; ++i) {
7014 ArtMethod* method = imt_data[i];
7015 ArtMethod* super_method = super_imt->Get(i, image_pointer_size_);
7016 if (method != super_method) {
7017 bool is_conflict_table = method->IsRuntimeMethod() &&
7018 method != unimplemented_method &&
7019 method != conflict_method;
7020 // Verify conflict contents.
7021 bool super_conflict_table = super_method->IsRuntimeMethod() &&
7022 super_method != unimplemented_method &&
7023 super_method != conflict_method;
7024 if (!is_conflict_table || !super_conflict_table) {
7025 same = false;
7026 } else {
7027 ImtConflictTable* table1 = method->GetImtConflictTable(image_pointer_size_);
7028 ImtConflictTable* table2 = super_method->GetImtConflictTable(image_pointer_size_);
7029 same = same && table1->Equals(table2, image_pointer_size_);
7030 }
7031 }
7032 }
7033 if (same) {
7034 imt = super_imt;
7035 }
7036 }
7037 if (imt == nullptr) {
7038 imt = klass->GetImt(image_pointer_size_);
7039 DCHECK(imt != nullptr);
7040 imt->Populate(imt_data, image_pointer_size_);
7041 } else {
7042 klass->SetImt(imt, image_pointer_size_);
7043 }
7044 }
7045
CreateImtConflictTable(size_t count,LinearAlloc * linear_alloc,PointerSize image_pointer_size)7046 ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count,
7047 LinearAlloc* linear_alloc,
7048 PointerSize image_pointer_size) {
7049 void* data = linear_alloc->Alloc(Thread::Current(),
7050 ImtConflictTable::ComputeSize(count,
7051 image_pointer_size));
7052 return (data != nullptr) ? new (data) ImtConflictTable(count, image_pointer_size) : nullptr;
7053 }
7054
CreateImtConflictTable(size_t count,LinearAlloc * linear_alloc)7055 ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, LinearAlloc* linear_alloc) {
7056 return CreateImtConflictTable(count, linear_alloc, image_pointer_size_);
7057 }
7058
FillIMTFromIfTable(ObjPtr<mirror::IfTable> if_table,ArtMethod * unimplemented_method,ArtMethod * imt_conflict_method,ObjPtr<mirror::Class> klass,bool create_conflict_tables,bool ignore_copied_methods,bool * new_conflict,ArtMethod ** imt)7059 void ClassLinker::FillIMTFromIfTable(ObjPtr<mirror::IfTable> if_table,
7060 ArtMethod* unimplemented_method,
7061 ArtMethod* imt_conflict_method,
7062 ObjPtr<mirror::Class> klass,
7063 bool create_conflict_tables,
7064 bool ignore_copied_methods,
7065 /*out*/bool* new_conflict,
7066 /*out*/ArtMethod** imt) {
7067 uint32_t conflict_counts[ImTable::kSize] = {};
7068 for (size_t i = 0, length = if_table->Count(); i < length; ++i) {
7069 ObjPtr<mirror::Class> interface = if_table->GetInterface(i);
7070 const size_t num_virtuals = interface->NumVirtualMethods();
7071 const size_t method_array_count = if_table->GetMethodArrayCount(i);
7072 // Virtual methods can be larger than the if table methods if there are default methods.
7073 DCHECK_GE(num_virtuals, method_array_count);
7074 if (kIsDebugBuild) {
7075 if (klass->IsInterface()) {
7076 DCHECK_EQ(method_array_count, 0u);
7077 } else {
7078 DCHECK_EQ(interface->NumDeclaredVirtualMethods(), method_array_count);
7079 }
7080 }
7081 if (method_array_count == 0) {
7082 continue;
7083 }
7084 ObjPtr<mirror::PointerArray> method_array = if_table->GetMethodArray(i);
7085 for (size_t j = 0; j < method_array_count; ++j) {
7086 ArtMethod* implementation_method =
7087 method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_);
7088 if (ignore_copied_methods && implementation_method->IsCopied()) {
7089 continue;
7090 }
7091 DCHECK(implementation_method != nullptr);
7092 // Miranda methods cannot be used to implement an interface method, but they are safe to put
7093 // in the IMT since their entrypoint is the interface trampoline. If we put any copied methods
7094 // or interface methods in the IMT here they will not create extra conflicts since we compare
7095 // names and signatures in SetIMTRef.
7096 ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_);
7097 const uint32_t imt_index = interface_method->GetImtIndex();
7098
7099 // There is only any conflicts if all of the interface methods for an IMT slot don't have
7100 // the same implementation method, keep track of this to avoid creating a conflict table in
7101 // this case.
7102
7103 // Conflict table size for each IMT slot.
7104 ++conflict_counts[imt_index];
7105
7106 SetIMTRef(unimplemented_method,
7107 imt_conflict_method,
7108 implementation_method,
7109 /*out*/new_conflict,
7110 /*out*/&imt[imt_index]);
7111 }
7112 }
7113
7114 if (create_conflict_tables) {
7115 // Create the conflict tables.
7116 LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader());
7117 for (size_t i = 0; i < ImTable::kSize; ++i) {
7118 size_t conflicts = conflict_counts[i];
7119 if (imt[i] == imt_conflict_method) {
7120 ImtConflictTable* new_table = CreateImtConflictTable(conflicts, linear_alloc);
7121 if (new_table != nullptr) {
7122 ArtMethod* new_conflict_method =
7123 Runtime::Current()->CreateImtConflictMethod(linear_alloc);
7124 new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_);
7125 imt[i] = new_conflict_method;
7126 } else {
7127 LOG(ERROR) << "Failed to allocate conflict table";
7128 imt[i] = imt_conflict_method;
7129 }
7130 } else {
7131 DCHECK_NE(imt[i], imt_conflict_method);
7132 }
7133 }
7134
7135 for (size_t i = 0, length = if_table->Count(); i < length; ++i) {
7136 ObjPtr<mirror::Class> interface = if_table->GetInterface(i);
7137 const size_t method_array_count = if_table->GetMethodArrayCount(i);
7138 // Virtual methods can be larger than the if table methods if there are default methods.
7139 if (method_array_count == 0) {
7140 continue;
7141 }
7142 ObjPtr<mirror::PointerArray> method_array = if_table->GetMethodArray(i);
7143 for (size_t j = 0; j < method_array_count; ++j) {
7144 ArtMethod* implementation_method =
7145 method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_);
7146 if (ignore_copied_methods && implementation_method->IsCopied()) {
7147 continue;
7148 }
7149 DCHECK(implementation_method != nullptr);
7150 ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_);
7151 const uint32_t imt_index = interface_method->GetImtIndex();
7152 if (!imt[imt_index]->IsRuntimeMethod() ||
7153 imt[imt_index] == unimplemented_method ||
7154 imt[imt_index] == imt_conflict_method) {
7155 continue;
7156 }
7157 ImtConflictTable* table = imt[imt_index]->GetImtConflictTable(image_pointer_size_);
7158 const size_t num_entries = table->NumEntries(image_pointer_size_);
7159 table->SetInterfaceMethod(num_entries, image_pointer_size_, interface_method);
7160 table->SetImplementationMethod(num_entries, image_pointer_size_, implementation_method);
7161 }
7162 }
7163 }
7164 }
7165
7166 // Simple helper function that checks that no subtypes of 'val' are contained within the 'classes'
7167 // set.
NotSubinterfaceOfAny(const HashSet<mirror::Class * > & classes,ObjPtr<mirror::Class> val)7168 static bool NotSubinterfaceOfAny(
7169 const HashSet<mirror::Class*>& classes,
7170 ObjPtr<mirror::Class> val)
7171 REQUIRES(Roles::uninterruptible_)
7172 REQUIRES_SHARED(Locks::mutator_lock_) {
7173 DCHECK(val != nullptr);
7174 for (ObjPtr<mirror::Class> c : classes) {
7175 if (val->IsAssignableFrom(c)) {
7176 return false;
7177 }
7178 }
7179 return true;
7180 }
7181
7182 // Fills in and flattens the interface inheritance hierarchy.
7183 //
7184 // By the end of this function all interfaces in the transitive closure of to_process are added to
7185 // the iftable and every interface precedes all of its sub-interfaces in this list.
7186 //
7187 // all I, J: Interface | I <: J implies J precedes I
7188 //
7189 // (note A <: B means that A is a subtype of B)
7190 //
7191 // This returns the total number of items in the iftable. The iftable might be resized down after
7192 // this call.
7193 //
7194 // We order this backwards so that we do not need to reorder superclass interfaces when new
7195 // interfaces are added in subclass's interface tables.
7196 //
7197 // Upon entry into this function iftable is a copy of the superclass's iftable with the first
7198 // super_ifcount entries filled in with the transitive closure of the interfaces of the superclass.
7199 // The other entries are uninitialized. We will fill in the remaining entries in this function. The
7200 // iftable must be large enough to hold all interfaces without changing its size.
FillIfTable(Thread * self,ObjPtr<mirror::Class> klass,ObjPtr<mirror::ObjectArray<mirror::Class>> interfaces,ObjPtr<mirror::IfTable> iftable,size_t super_ifcount,size_t num_interfaces)7201 static size_t FillIfTable(Thread* self,
7202 ObjPtr<mirror::Class> klass,
7203 ObjPtr<mirror::ObjectArray<mirror::Class>> interfaces,
7204 ObjPtr<mirror::IfTable> iftable,
7205 size_t super_ifcount,
7206 size_t num_interfaces)
7207 REQUIRES_SHARED(Locks::mutator_lock_) {
7208 ScopedAssertNoThreadSuspension nts(__FUNCTION__);
7209 // This is the set of all classes already in the iftable. Used to make checking
7210 // if a class has already been added quicker.
7211 constexpr size_t kBufferSize = 32; // 256 bytes on 64-bit architectures.
7212 mirror::Class* buffer[kBufferSize];
7213 HashSet<mirror::Class*> classes_in_iftable(buffer, kBufferSize);
7214 // The first super_ifcount elements are from the superclass. We note that they are already added.
7215 for (size_t i = 0; i < super_ifcount; i++) {
7216 ObjPtr<mirror::Class> iface = iftable->GetInterface(i);
7217 DCHECK(NotSubinterfaceOfAny(classes_in_iftable, iface)) << "Bad ordering.";
7218 classes_in_iftable.insert(iface.Ptr());
7219 }
7220 size_t filled_ifcount = super_ifcount;
7221 const bool have_interfaces = interfaces != nullptr;
7222 for (size_t i = 0; i != num_interfaces; ++i) {
7223 ObjPtr<mirror::Class> interface = have_interfaces
7224 ? interfaces->Get(i)
7225 : mirror::Class::GetDirectInterface(self, klass, i);
7226
7227 // Let us call the first filled_ifcount elements of iftable the current-iface-list.
7228 // At this point in the loop current-iface-list has the invariant that:
7229 // for every pair of interfaces I,J within it:
7230 // if index_of(I) < index_of(J) then I is not a subtype of J
7231
7232 // If we have already seen this element then all of its super-interfaces must already be in the
7233 // current-iface-list so we can skip adding it.
7234 if (classes_in_iftable.find(interface.Ptr()) == classes_in_iftable.end()) {
7235 // We haven't seen this interface so add all of its super-interfaces onto the
7236 // current-iface-list, skipping those already on it.
7237 int32_t ifcount = interface->GetIfTableCount();
7238 for (int32_t j = 0; j < ifcount; j++) {
7239 ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j);
7240 if (!ContainsElement(classes_in_iftable, super_interface)) {
7241 DCHECK(NotSubinterfaceOfAny(classes_in_iftable, super_interface)) << "Bad ordering.";
7242 classes_in_iftable.insert(super_interface.Ptr());
7243 iftable->SetInterface(filled_ifcount, super_interface);
7244 filled_ifcount++;
7245 }
7246 }
7247 DCHECK(NotSubinterfaceOfAny(classes_in_iftable, interface)) << "Bad ordering";
7248 // Place this interface onto the current-iface-list after all of its super-interfaces.
7249 classes_in_iftable.insert(interface.Ptr());
7250 iftable->SetInterface(filled_ifcount, interface);
7251 filled_ifcount++;
7252 } else if (kIsDebugBuild) {
7253 // Check all super-interfaces are already in the list.
7254 int32_t ifcount = interface->GetIfTableCount();
7255 for (int32_t j = 0; j < ifcount; j++) {
7256 ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j);
7257 DCHECK(ContainsElement(classes_in_iftable, super_interface))
7258 << "Iftable does not contain " << mirror::Class::PrettyClass(super_interface)
7259 << ", a superinterface of " << interface->PrettyClass();
7260 }
7261 }
7262 }
7263 if (kIsDebugBuild) {
7264 // Check that the iftable is ordered correctly.
7265 for (size_t i = 0; i < filled_ifcount; i++) {
7266 ObjPtr<mirror::Class> if_a = iftable->GetInterface(i);
7267 for (size_t j = i + 1; j < filled_ifcount; j++) {
7268 ObjPtr<mirror::Class> if_b = iftable->GetInterface(j);
7269 // !(if_a <: if_b)
7270 CHECK(!if_b->IsAssignableFrom(if_a))
7271 << "Bad interface order: " << mirror::Class::PrettyClass(if_a) << " (index " << i
7272 << ") extends "
7273 << if_b->PrettyClass() << " (index " << j << ") and so should be after it in the "
7274 << "interface list.";
7275 }
7276 }
7277 }
7278 return filled_ifcount;
7279 }
7280
SetupInterfaceLookupTable(Thread * self,Handle<mirror::Class> klass,Handle<mirror::ObjectArray<mirror::Class>> interfaces)7281 bool ClassLinker::SetupInterfaceLookupTable(Thread* self,
7282 Handle<mirror::Class> klass,
7283 Handle<mirror::ObjectArray<mirror::Class>> interfaces) {
7284 StackHandleScope<1> hs(self);
7285 const bool has_superclass = klass->HasSuperClass();
7286 const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U;
7287 const bool have_interfaces = interfaces != nullptr;
7288 const size_t num_interfaces =
7289 have_interfaces ? interfaces->GetLength() : klass->NumDirectInterfaces();
7290 if (num_interfaces == 0) {
7291 if (super_ifcount == 0) {
7292 if (LIKELY(has_superclass)) {
7293 klass->SetIfTable(klass->GetSuperClass()->GetIfTable());
7294 }
7295 // Class implements no interfaces.
7296 DCHECK_EQ(klass->GetIfTableCount(), 0);
7297 return true;
7298 }
7299 // Class implements same interfaces as parent, are any of these not marker interfaces?
7300 bool has_non_marker_interface = false;
7301 ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable();
7302 for (size_t i = 0; i < super_ifcount; ++i) {
7303 if (super_iftable->GetMethodArrayCount(i) > 0) {
7304 has_non_marker_interface = true;
7305 break;
7306 }
7307 }
7308 // Class just inherits marker interfaces from parent so recycle parent's iftable.
7309 if (!has_non_marker_interface) {
7310 klass->SetIfTable(super_iftable);
7311 return true;
7312 }
7313 }
7314 size_t ifcount = super_ifcount + num_interfaces;
7315 // Check that every class being implemented is an interface.
7316 for (size_t i = 0; i < num_interfaces; i++) {
7317 ObjPtr<mirror::Class> interface = have_interfaces
7318 ? interfaces->GetWithoutChecks(i)
7319 : mirror::Class::GetDirectInterface(self, klass.Get(), i);
7320 DCHECK(interface != nullptr);
7321 if (UNLIKELY(!interface->IsInterface())) {
7322 std::string temp;
7323 ThrowIncompatibleClassChangeError(klass.Get(),
7324 "Class %s implements non-interface class %s",
7325 klass->PrettyDescriptor().c_str(),
7326 PrettyDescriptor(interface->GetDescriptor(&temp)).c_str());
7327 return false;
7328 }
7329 ifcount += interface->GetIfTableCount();
7330 }
7331 // Create the interface function table.
7332 MutableHandle<mirror::IfTable> iftable(hs.NewHandle(AllocIfTable(self, ifcount)));
7333 if (UNLIKELY(iftable == nullptr)) {
7334 self->AssertPendingOOMException();
7335 return false;
7336 }
7337 // Fill in table with superclass's iftable.
7338 if (super_ifcount != 0) {
7339 ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable();
7340 for (size_t i = 0; i < super_ifcount; i++) {
7341 ObjPtr<mirror::Class> super_interface = super_iftable->GetInterface(i);
7342 iftable->SetInterface(i, super_interface);
7343 }
7344 }
7345
7346 // Note that AllowThreadSuspension is to thread suspension as pthread_testcancel is to pthread
7347 // cancellation. That is it will suspend if one has a pending suspend request but otherwise
7348 // doesn't really do anything.
7349 self->AllowThreadSuspension();
7350
7351 const size_t new_ifcount = FillIfTable(
7352 self, klass.Get(), interfaces.Get(), iftable.Get(), super_ifcount, num_interfaces);
7353
7354 self->AllowThreadSuspension();
7355
7356 // Shrink iftable in case duplicates were found
7357 if (new_ifcount < ifcount) {
7358 DCHECK_NE(num_interfaces, 0U);
7359 iftable.Assign(ObjPtr<mirror::IfTable>::DownCast(
7360 mirror::IfTable::CopyOf(iftable, self, new_ifcount * mirror::IfTable::kMax)));
7361 if (UNLIKELY(iftable == nullptr)) {
7362 self->AssertPendingOOMException();
7363 return false;
7364 }
7365 ifcount = new_ifcount;
7366 } else {
7367 DCHECK_EQ(new_ifcount, ifcount);
7368 }
7369 klass->SetIfTable(iftable.Get());
7370 return true;
7371 }
7372
7373 // Finds the method with a name/signature that matches cmp in the given lists of methods. The list
7374 // of methods must be unique.
FindSameNameAndSignature(MethodNameAndSignatureComparator & cmp ATTRIBUTE_UNUSED)7375 static ArtMethod* FindSameNameAndSignature(MethodNameAndSignatureComparator& cmp ATTRIBUTE_UNUSED) {
7376 return nullptr;
7377 }
7378
7379 template <typename ... Types>
FindSameNameAndSignature(MethodNameAndSignatureComparator & cmp,const ScopedArenaVector<ArtMethod * > & list,const Types &...rest)7380 static ArtMethod* FindSameNameAndSignature(MethodNameAndSignatureComparator& cmp,
7381 const ScopedArenaVector<ArtMethod*>& list,
7382 const Types& ... rest)
7383 REQUIRES_SHARED(Locks::mutator_lock_) {
7384 for (ArtMethod* method : list) {
7385 if (cmp.HasSameNameAndSignature(method)) {
7386 return method;
7387 }
7388 }
7389 return FindSameNameAndSignature(cmp, rest...);
7390 }
7391
7392 namespace {
7393
7394 // Check that all vtable entries are present in this class's virtuals or are the same as a
7395 // superclasses vtable entry.
CheckClassOwnsVTableEntries(Thread * self,Handle<mirror::Class> klass,PointerSize pointer_size)7396 void CheckClassOwnsVTableEntries(Thread* self,
7397 Handle<mirror::Class> klass,
7398 PointerSize pointer_size)
7399 REQUIRES_SHARED(Locks::mutator_lock_) {
7400 StackHandleScope<2> hs(self);
7401 Handle<mirror::PointerArray> check_vtable(hs.NewHandle(klass->GetVTableDuringLinking()));
7402 ObjPtr<mirror::Class> super_temp = (klass->HasSuperClass()) ? klass->GetSuperClass() : nullptr;
7403 Handle<mirror::Class> superclass(hs.NewHandle(super_temp));
7404 int32_t super_vtable_length = (superclass != nullptr) ? superclass->GetVTableLength() : 0;
7405 for (int32_t i = 0; i < check_vtable->GetLength(); ++i) {
7406 ArtMethod* m = check_vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size);
7407 CHECK(m != nullptr);
7408
7409 if (m->GetMethodIndexDuringLinking() != i) {
7410 LOG(WARNING) << m->PrettyMethod()
7411 << " has an unexpected method index for its spot in the vtable for class"
7412 << klass->PrettyClass();
7413 }
7414 ArraySlice<ArtMethod> virtuals = klass->GetVirtualMethodsSliceUnchecked(pointer_size);
7415 auto is_same_method = [m] (const ArtMethod& meth) {
7416 return &meth == m;
7417 };
7418 if (!((super_vtable_length > i && superclass->GetVTableEntry(i, pointer_size) == m) ||
7419 std::find_if(virtuals.begin(), virtuals.end(), is_same_method) != virtuals.end())) {
7420 LOG(WARNING) << m->PrettyMethod() << " does not seem to be owned by current class "
7421 << klass->PrettyClass() << " or any of its superclasses!";
7422 }
7423 }
7424 }
7425
7426 // Check to make sure the vtable does not have duplicates. Duplicates could cause problems when a
7427 // method is overridden in a subclass.
7428 template <PointerSize kPointerSize>
CheckVTableHasNoDuplicates(Thread * self,Handle<mirror::Class> klass)7429 void CheckVTableHasNoDuplicates(Thread* self, Handle<mirror::Class> klass)
7430 REQUIRES_SHARED(Locks::mutator_lock_) {
7431 StackHandleScope<1> hs(self);
7432 Handle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking()));
7433 int32_t num_entries = vtable->GetLength();
7434
7435 // Observations:
7436 // * The older implementation was O(n^2) and got too expensive for apps with larger classes.
7437 // * Many classes do not override Object functions (e.g., equals/hashCode/toString). Thus,
7438 // for many classes outside of libcore a cross-dexfile check has to be run anyways.
7439 // * In the cross-dexfile case, with the O(n^2), in the best case O(n) cross checks would have
7440 // to be done. It is thus OK in a single-pass algorithm to read all data, anyways.
7441 // * The single-pass algorithm will trade memory for speed, but that is OK.
7442
7443 CHECK_GT(num_entries, 0);
7444
7445 auto log_fn = [&vtable, &klass](int32_t i, int32_t j) REQUIRES_SHARED(Locks::mutator_lock_) {
7446 ArtMethod* m1 = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(i);
7447 ArtMethod* m2 = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(j);
7448 LOG(WARNING) << "vtable entries " << i << " and " << j << " are identical for "
7449 << klass->PrettyClass() << " in method " << m1->PrettyMethod()
7450 << " (0x" << std::hex << reinterpret_cast<uintptr_t>(m2) << ") and "
7451 << m2->PrettyMethod() << " (0x" << std::hex
7452 << reinterpret_cast<uintptr_t>(m2) << ")";
7453 };
7454 struct BaseHashType {
7455 static size_t HashCombine(size_t seed, size_t val) {
7456 return seed ^ (val + 0x9e3779b9 + (seed << 6) + (seed >> 2));
7457 }
7458 };
7459
7460 // Check assuming all entries come from the same dex file.
7461 {
7462 // Find the first interesting method and its dex file.
7463 int32_t start = 0;
7464 for (; start < num_entries; ++start) {
7465 ArtMethod* vtable_entry = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(start);
7466 // Don't bother if we cannot 'see' the vtable entry (i.e. it is a package-private member
7467 // maybe).
7468 if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(),
7469 vtable_entry->GetAccessFlags())) {
7470 continue;
7471 }
7472 break;
7473 }
7474 if (start == num_entries) {
7475 return;
7476 }
7477 const DexFile* dex_file =
7478 vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(start)->
7479 GetInterfaceMethodIfProxy(kPointerSize)->GetDexFile();
7480
7481 // Helper function to avoid logging if we have to run the cross-file checks.
7482 auto check_fn = [&](bool log_warn) REQUIRES_SHARED(Locks::mutator_lock_) {
7483 // Use a map to store seen entries, as the storage space is too large for a bitvector.
7484 using PairType = std::pair<uint32_t, uint16_t>;
7485 struct PairHash : BaseHashType {
7486 size_t operator()(const PairType& key) const {
7487 return BaseHashType::HashCombine(BaseHashType::HashCombine(0, key.first), key.second);
7488 }
7489 };
7490 HashMap<PairType, int32_t, DefaultMapEmptyFn<PairType, int32_t>, PairHash> seen;
7491 seen.reserve(2 * num_entries);
7492 bool need_slow_path = false;
7493 bool found_dup = false;
7494 for (int i = start; i < num_entries; ++i) {
7495 // Can use Unchecked here as the start loop already ensured that the arrays are correct
7496 // wrt/ kPointerSize.
7497 ArtMethod* vtable_entry = vtable->GetElementPtrSizeUnchecked<ArtMethod*, kPointerSize>(i);
7498 if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(),
7499 vtable_entry->GetAccessFlags())) {
7500 continue;
7501 }
7502 ArtMethod* m = vtable_entry->GetInterfaceMethodIfProxy(kPointerSize);
7503 if (dex_file != m->GetDexFile()) {
7504 need_slow_path = true;
7505 break;
7506 }
7507 const dex::MethodId* m_mid = &dex_file->GetMethodId(m->GetDexMethodIndex());
7508 PairType pair = std::make_pair(m_mid->name_idx_.index_, m_mid->proto_idx_.index_);
7509 auto it = seen.find(pair);
7510 if (it != seen.end()) {
7511 found_dup = true;
7512 if (log_warn) {
7513 log_fn(it->second, i);
7514 }
7515 } else {
7516 seen.insert(std::make_pair(pair, i));
7517 }
7518 }
7519 return std::make_pair(need_slow_path, found_dup);
7520 };
7521 std::pair<bool, bool> result = check_fn(/* log_warn= */ false);
7522 if (!result.first) {
7523 if (result.second) {
7524 check_fn(/* log_warn= */ true);
7525 }
7526 return;
7527 }
7528 }
7529
7530 // Need to check across dex files.
7531 struct Entry {
7532 size_t cached_hash = 0;
7533 uint32_t name_len = 0;
7534 const char* name = nullptr;
7535 Signature signature = Signature::NoSignature();
7536
7537 Entry() = default;
7538 Entry(const Entry& other) = default;
7539 Entry& operator=(const Entry& other) = default;
7540
7541 Entry(const DexFile* dex_file, const dex::MethodId& mid)
7542 : name_len(0), // Explicit to enforce ordering with -Werror,-Wreorder-ctor.
7543 // This call writes `name_len` and it is therefore necessary that the
7544 // initializer for `name_len` comes before it, otherwise the value
7545 // from the call would be overwritten by that initializer.
7546 name(dex_file->StringDataAndUtf16LengthByIdx(mid.name_idx_, &name_len)),
7547 signature(dex_file->GetMethodSignature(mid)) {
7548 // The `name_len` has been initialized to the UTF16 length. Calculate length in bytes.
7549 if (name[name_len] != 0) {
7550 name_len += strlen(name + name_len);
7551 }
7552 }
7553
7554 bool operator==(const Entry& other) const {
7555 return name_len == other.name_len &&
7556 memcmp(name, other.name, name_len) == 0 &&
7557 signature == other.signature;
7558 }
7559 };
7560 struct EntryHash {
7561 size_t operator()(const Entry& key) const {
7562 return key.cached_hash;
7563 }
7564 };
7565 HashMap<Entry, int32_t, DefaultMapEmptyFn<Entry, int32_t>, EntryHash> map;
7566 for (int32_t i = 0; i < num_entries; ++i) {
7567 // Can use Unchecked here as the first loop already ensured that the arrays are correct
7568 // wrt/ kPointerSize.
7569 ArtMethod* vtable_entry = vtable->GetElementPtrSizeUnchecked<ArtMethod*, kPointerSize>(i);
7570 // Don't bother if we cannot 'see' the vtable entry (i.e. it is a package-private member
7571 // maybe).
7572 if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(),
7573 vtable_entry->GetAccessFlags())) {
7574 continue;
7575 }
7576 ArtMethod* m = vtable_entry->GetInterfaceMethodIfProxy(kPointerSize);
7577 const DexFile* dex_file = m->GetDexFile();
7578 const dex::MethodId& mid = dex_file->GetMethodId(m->GetDexMethodIndex());
7579
7580 Entry e(dex_file, mid);
7581
7582 size_t string_hash = std::hash<std::string_view>()(std::string_view(e.name, e.name_len));
7583 size_t sig_hash = std::hash<std::string>()(e.signature.ToString());
7584 e.cached_hash = BaseHashType::HashCombine(BaseHashType::HashCombine(0u, string_hash),
7585 sig_hash);
7586
7587 auto it = map.find(e);
7588 if (it != map.end()) {
7589 log_fn(it->second, i);
7590 } else {
7591 map.insert(std::make_pair(e, i));
7592 }
7593 }
7594 }
7595
CheckVTableHasNoDuplicates(Thread * self,Handle<mirror::Class> klass,PointerSize pointer_size)7596 void CheckVTableHasNoDuplicates(Thread* self,
7597 Handle<mirror::Class> klass,
7598 PointerSize pointer_size)
7599 REQUIRES_SHARED(Locks::mutator_lock_) {
7600 switch (pointer_size) {
7601 case PointerSize::k64:
7602 CheckVTableHasNoDuplicates<PointerSize::k64>(self, klass);
7603 break;
7604 case PointerSize::k32:
7605 CheckVTableHasNoDuplicates<PointerSize::k32>(self, klass);
7606 break;
7607 }
7608 }
7609
CheckVTable(Thread * self,Handle<mirror::Class> klass,PointerSize pointer_size)7610 static void CheckVTable(Thread* self, Handle<mirror::Class> klass, PointerSize pointer_size)
7611 REQUIRES_SHARED(Locks::mutator_lock_) {
7612 CheckClassOwnsVTableEntries(self, klass, pointer_size);
7613 CheckVTableHasNoDuplicates(self, klass, pointer_size);
7614 }
7615
7616 } // namespace
7617
FillImtFromSuperClass(Handle<mirror::Class> klass,ArtMethod * unimplemented_method,ArtMethod * imt_conflict_method,bool * new_conflict,ArtMethod ** imt)7618 void ClassLinker::FillImtFromSuperClass(Handle<mirror::Class> klass,
7619 ArtMethod* unimplemented_method,
7620 ArtMethod* imt_conflict_method,
7621 bool* new_conflict,
7622 ArtMethod** imt) {
7623 DCHECK(klass->HasSuperClass());
7624 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
7625 if (super_class->ShouldHaveImt()) {
7626 ImTable* super_imt = super_class->GetImt(image_pointer_size_);
7627 for (size_t i = 0; i < ImTable::kSize; ++i) {
7628 imt[i] = super_imt->Get(i, image_pointer_size_);
7629 }
7630 } else {
7631 // No imt in the super class, need to reconstruct from the iftable.
7632 ObjPtr<mirror::IfTable> if_table = super_class->GetIfTable();
7633 if (if_table->Count() != 0) {
7634 // Ignore copied methods since we will handle these in LinkInterfaceMethods.
7635 FillIMTFromIfTable(if_table,
7636 unimplemented_method,
7637 imt_conflict_method,
7638 klass.Get(),
7639 /*create_conflict_tables=*/false,
7640 /*ignore_copied_methods=*/true,
7641 /*out*/new_conflict,
7642 /*out*/imt);
7643 }
7644 }
7645 }
7646
7647 class ClassLinker::LinkInterfaceMethodsHelper {
7648 public:
LinkInterfaceMethodsHelper(ClassLinker * class_linker,Handle<mirror::Class> klass,Thread * self,Runtime * runtime)7649 LinkInterfaceMethodsHelper(ClassLinker* class_linker,
7650 Handle<mirror::Class> klass,
7651 Thread* self,
7652 Runtime* runtime)
7653 : class_linker_(class_linker),
7654 klass_(klass),
7655 method_alignment_(ArtMethod::Alignment(class_linker->GetImagePointerSize())),
7656 method_size_(ArtMethod::Size(class_linker->GetImagePointerSize())),
7657 self_(self),
7658 stack_(runtime->GetLinearAlloc()->GetArenaPool()),
7659 allocator_(&stack_),
7660 default_conflict_methods_(allocator_.Adapter()),
7661 overriding_default_conflict_methods_(allocator_.Adapter()),
7662 miranda_methods_(allocator_.Adapter()),
7663 default_methods_(allocator_.Adapter()),
7664 overriding_default_methods_(allocator_.Adapter()),
7665 move_table_(allocator_.Adapter()) {
7666 }
7667
7668 ArtMethod* FindMethod(ArtMethod* interface_method,
7669 MethodNameAndSignatureComparator& interface_name_comparator,
7670 ArtMethod* vtable_impl)
7671 REQUIRES_SHARED(Locks::mutator_lock_);
7672
7673 ArtMethod* GetOrCreateMirandaMethod(ArtMethod* interface_method,
7674 MethodNameAndSignatureComparator& interface_name_comparator)
7675 REQUIRES_SHARED(Locks::mutator_lock_);
7676
HasNewVirtuals() const7677 bool HasNewVirtuals() const {
7678 return !(miranda_methods_.empty() &&
7679 default_methods_.empty() &&
7680 overriding_default_methods_.empty() &&
7681 overriding_default_conflict_methods_.empty() &&
7682 default_conflict_methods_.empty());
7683 }
7684
7685 void ReallocMethods() REQUIRES_SHARED(Locks::mutator_lock_);
7686
7687 ObjPtr<mirror::PointerArray> UpdateVtable(
7688 const HashMap<size_t, ClassLinker::MethodTranslation>& default_translations,
7689 Handle<mirror::PointerArray> old_vtable) REQUIRES_SHARED(Locks::mutator_lock_);
7690
7691 void UpdateIfTable(Handle<mirror::IfTable> iftable) REQUIRES_SHARED(Locks::mutator_lock_);
7692
7693 void UpdateIMT(ArtMethod** out_imt);
7694
CheckNoStaleMethodsInDexCache()7695 void CheckNoStaleMethodsInDexCache() REQUIRES_SHARED(Locks::mutator_lock_) {
7696 if (kIsDebugBuild) {
7697 PointerSize pointer_size = class_linker_->GetImagePointerSize();
7698 // Check that there are no stale methods are in the dex cache array.
7699 auto* resolved_methods = klass_->GetDexCache()->GetResolvedMethods();
7700 for (size_t i = 0, count = klass_->GetDexCache()->NumResolvedMethods(); i < count; ++i) {
7701 auto pair = mirror::DexCache::GetNativePair(resolved_methods, i);
7702 ArtMethod* m = pair.object;
7703 CHECK(move_table_.find(m) == move_table_.end() ||
7704 // The original versions of copied methods will still be present so allow those too.
7705 // Note that if the first check passes this might fail to GetDeclaringClass().
7706 std::find_if(m->GetDeclaringClass()->GetMethods(pointer_size).begin(),
7707 m->GetDeclaringClass()->GetMethods(pointer_size).end(),
7708 [m] (ArtMethod& meth) {
7709 return &meth == m;
7710 }) != m->GetDeclaringClass()->GetMethods(pointer_size).end())
7711 << "Obsolete method " << m->PrettyMethod() << " is in dex cache!";
7712 }
7713 }
7714 }
7715
ClobberOldMethods(LengthPrefixedArray<ArtMethod> * old_methods,LengthPrefixedArray<ArtMethod> * methods)7716 void ClobberOldMethods(LengthPrefixedArray<ArtMethod>* old_methods,
7717 LengthPrefixedArray<ArtMethod>* methods) {
7718 if (kIsDebugBuild) {
7719 CHECK(methods != nullptr);
7720 // Put some random garbage in old methods to help find stale pointers.
7721 if (methods != old_methods && old_methods != nullptr) {
7722 // Need to make sure the GC is not running since it could be scanning the methods we are
7723 // about to overwrite.
7724 ScopedThreadStateChange tsc(self_, kSuspended);
7725 gc::ScopedGCCriticalSection gcs(self_,
7726 gc::kGcCauseClassLinker,
7727 gc::kCollectorTypeClassLinker);
7728 const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_methods->size(),
7729 method_size_,
7730 method_alignment_);
7731 memset(old_methods, 0xFEu, old_size);
7732 }
7733 }
7734 }
7735
7736 private:
NumberOfNewVirtuals() const7737 size_t NumberOfNewVirtuals() const {
7738 return miranda_methods_.size() +
7739 default_methods_.size() +
7740 overriding_default_conflict_methods_.size() +
7741 overriding_default_methods_.size() +
7742 default_conflict_methods_.size();
7743 }
7744
FillTables()7745 bool FillTables() REQUIRES_SHARED(Locks::mutator_lock_) {
7746 return !klass_->IsInterface();
7747 }
7748
LogNewVirtuals() const7749 void LogNewVirtuals() const REQUIRES_SHARED(Locks::mutator_lock_) {
7750 DCHECK(!klass_->IsInterface() || (default_methods_.empty() && miranda_methods_.empty()))
7751 << "Interfaces should only have default-conflict methods appended to them.";
7752 VLOG(class_linker) << mirror::Class::PrettyClass(klass_.Get()) << ": miranda_methods="
7753 << miranda_methods_.size()
7754 << " default_methods=" << default_methods_.size()
7755 << " overriding_default_methods=" << overriding_default_methods_.size()
7756 << " default_conflict_methods=" << default_conflict_methods_.size()
7757 << " overriding_default_conflict_methods="
7758 << overriding_default_conflict_methods_.size();
7759 }
7760
7761 ClassLinker* class_linker_;
7762 Handle<mirror::Class> klass_;
7763 size_t method_alignment_;
7764 size_t method_size_;
7765 Thread* const self_;
7766
7767 // These are allocated on the heap to begin, we then transfer to linear alloc when we re-create
7768 // the virtual methods array.
7769 // Need to use low 4GB arenas for compiler or else the pointers wont fit in 32 bit method array
7770 // during cross compilation.
7771 // Use the linear alloc pool since this one is in the low 4gb for the compiler.
7772 ArenaStack stack_;
7773 ScopedArenaAllocator allocator_;
7774
7775 ScopedArenaVector<ArtMethod*> default_conflict_methods_;
7776 ScopedArenaVector<ArtMethod*> overriding_default_conflict_methods_;
7777 ScopedArenaVector<ArtMethod*> miranda_methods_;
7778 ScopedArenaVector<ArtMethod*> default_methods_;
7779 ScopedArenaVector<ArtMethod*> overriding_default_methods_;
7780
7781 ScopedArenaUnorderedMap<ArtMethod*, ArtMethod*> move_table_;
7782 };
7783
FindMethod(ArtMethod * interface_method,MethodNameAndSignatureComparator & interface_name_comparator,ArtMethod * vtable_impl)7784 ArtMethod* ClassLinker::LinkInterfaceMethodsHelper::FindMethod(
7785 ArtMethod* interface_method,
7786 MethodNameAndSignatureComparator& interface_name_comparator,
7787 ArtMethod* vtable_impl) {
7788 ArtMethod* current_method = nullptr;
7789 switch (class_linker_->FindDefaultMethodImplementation(self_,
7790 interface_method,
7791 klass_,
7792 /*out*/¤t_method)) {
7793 case DefaultMethodSearchResult::kDefaultConflict: {
7794 // Default method conflict.
7795 DCHECK(current_method == nullptr);
7796 ArtMethod* default_conflict_method = nullptr;
7797 if (vtable_impl != nullptr && vtable_impl->IsDefaultConflicting()) {
7798 // We can reuse the method from the superclass, don't bother adding it to virtuals.
7799 default_conflict_method = vtable_impl;
7800 } else {
7801 // See if we already have a conflict method for this method.
7802 ArtMethod* preexisting_conflict = FindSameNameAndSignature(
7803 interface_name_comparator,
7804 default_conflict_methods_,
7805 overriding_default_conflict_methods_);
7806 if (LIKELY(preexisting_conflict != nullptr)) {
7807 // We already have another conflict we can reuse.
7808 default_conflict_method = preexisting_conflict;
7809 } else {
7810 // Note that we do this even if we are an interface since we need to create this and
7811 // cannot reuse another classes.
7812 // Create a new conflict method for this to use.
7813 default_conflict_method = reinterpret_cast<ArtMethod*>(allocator_.Alloc(method_size_));
7814 new(default_conflict_method) ArtMethod(interface_method,
7815 class_linker_->GetImagePointerSize());
7816 if (vtable_impl == nullptr) {
7817 // Save the conflict method. We need to add it to the vtable.
7818 default_conflict_methods_.push_back(default_conflict_method);
7819 } else {
7820 // Save the conflict method but it is already in the vtable.
7821 overriding_default_conflict_methods_.push_back(default_conflict_method);
7822 }
7823 }
7824 }
7825 current_method = default_conflict_method;
7826 break;
7827 } // case kDefaultConflict
7828 case DefaultMethodSearchResult::kDefaultFound: {
7829 DCHECK(current_method != nullptr);
7830 // Found a default method.
7831 if (vtable_impl != nullptr &&
7832 current_method->GetDeclaringClass() == vtable_impl->GetDeclaringClass()) {
7833 // We found a default method but it was the same one we already have from our
7834 // superclass. Don't bother adding it to our vtable again.
7835 current_method = vtable_impl;
7836 } else if (LIKELY(FillTables())) {
7837 // Interfaces don't need to copy default methods since they don't have vtables.
7838 // Only record this default method if it is new to save space.
7839 // TODO It might be worthwhile to copy default methods on interfaces anyway since it
7840 // would make lookup for interface super much faster. (We would only need to scan
7841 // the iftable to find if there is a NSME or AME.)
7842 ArtMethod* old = FindSameNameAndSignature(interface_name_comparator,
7843 default_methods_,
7844 overriding_default_methods_);
7845 if (old == nullptr) {
7846 // We found a default method implementation and there were no conflicts.
7847 if (vtable_impl == nullptr) {
7848 // Save the default method. We need to add it to the vtable.
7849 default_methods_.push_back(current_method);
7850 } else {
7851 // Save the default method but it is already in the vtable.
7852 overriding_default_methods_.push_back(current_method);
7853 }
7854 } else {
7855 CHECK(old == current_method) << "Multiple default implementations selected!";
7856 }
7857 }
7858 break;
7859 } // case kDefaultFound
7860 case DefaultMethodSearchResult::kAbstractFound: {
7861 DCHECK(current_method == nullptr);
7862 // Abstract method masks all defaults.
7863 if (vtable_impl != nullptr &&
7864 vtable_impl->IsAbstract() &&
7865 !vtable_impl->IsDefaultConflicting()) {
7866 // We need to make this an abstract method but the version in the vtable already is so
7867 // don't do anything.
7868 current_method = vtable_impl;
7869 }
7870 break;
7871 } // case kAbstractFound
7872 }
7873 return current_method;
7874 }
7875
GetOrCreateMirandaMethod(ArtMethod * interface_method,MethodNameAndSignatureComparator & interface_name_comparator)7876 ArtMethod* ClassLinker::LinkInterfaceMethodsHelper::GetOrCreateMirandaMethod(
7877 ArtMethod* interface_method,
7878 MethodNameAndSignatureComparator& interface_name_comparator) {
7879 // Find out if there is already a miranda method we can use.
7880 ArtMethod* miranda_method = FindSameNameAndSignature(interface_name_comparator,
7881 miranda_methods_);
7882 if (miranda_method == nullptr) {
7883 DCHECK(interface_method->IsAbstract()) << interface_method->PrettyMethod();
7884 miranda_method = reinterpret_cast<ArtMethod*>(allocator_.Alloc(method_size_));
7885 CHECK(miranda_method != nullptr);
7886 // Point the interface table at a phantom slot.
7887 new(miranda_method) ArtMethod(interface_method, class_linker_->GetImagePointerSize());
7888 miranda_methods_.push_back(miranda_method);
7889 }
7890 return miranda_method;
7891 }
7892
ReallocMethods()7893 void ClassLinker::LinkInterfaceMethodsHelper::ReallocMethods() {
7894 LogNewVirtuals();
7895
7896 const size_t old_method_count = klass_->NumMethods();
7897 const size_t new_method_count = old_method_count + NumberOfNewVirtuals();
7898 DCHECK_NE(old_method_count, new_method_count);
7899
7900 // Attempt to realloc to save RAM if possible.
7901 LengthPrefixedArray<ArtMethod>* old_methods = klass_->GetMethodsPtr();
7902 // The Realloced virtual methods aren't visible from the class roots, so there is no issue
7903 // where GCs could attempt to mark stale pointers due to memcpy. And since we overwrite the
7904 // realloced memory with out->CopyFrom, we are guaranteed to have objects in the to space since
7905 // CopyFrom has internal read barriers.
7906 //
7907 // TODO We should maybe move some of this into mirror::Class or at least into another method.
7908 const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_method_count,
7909 method_size_,
7910 method_alignment_);
7911 const size_t new_size = LengthPrefixedArray<ArtMethod>::ComputeSize(new_method_count,
7912 method_size_,
7913 method_alignment_);
7914 const size_t old_methods_ptr_size = (old_methods != nullptr) ? old_size : 0;
7915 auto* methods = reinterpret_cast<LengthPrefixedArray<ArtMethod>*>(
7916 class_linker_->GetAllocatorForClassLoader(klass_->GetClassLoader())->Realloc(
7917 self_, old_methods, old_methods_ptr_size, new_size));
7918 CHECK(methods != nullptr); // Native allocation failure aborts.
7919
7920 PointerSize pointer_size = class_linker_->GetImagePointerSize();
7921 if (methods != old_methods) {
7922 // Maps from heap allocated miranda method to linear alloc miranda method.
7923 StrideIterator<ArtMethod> out = methods->begin(method_size_, method_alignment_);
7924 // Copy over the old methods.
7925 for (auto& m : klass_->GetMethods(pointer_size)) {
7926 move_table_.emplace(&m, &*out);
7927 // The CopyFrom is only necessary to not miss read barriers since Realloc won't do read
7928 // barriers when it copies.
7929 out->CopyFrom(&m, pointer_size);
7930 ++out;
7931 }
7932 }
7933 StrideIterator<ArtMethod> out(methods->begin(method_size_, method_alignment_) + old_method_count);
7934 // Copy over miranda methods before copying vtable since CopyOf may cause thread suspension and
7935 // we want the roots of the miranda methods to get visited.
7936 for (size_t i = 0; i < miranda_methods_.size(); ++i) {
7937 ArtMethod* mir_method = miranda_methods_[i];
7938 ArtMethod& new_method = *out;
7939 new_method.CopyFrom(mir_method, pointer_size);
7940 uint32_t access_flags = new_method.GetAccessFlags();
7941 DCHECK_EQ(access_flags & kAccIntrinsic, 0u) << "Miranda method should not be an intrinsic!";
7942 DCHECK_EQ(access_flags & kAccDefault, 0u) << "Miranda method should not be a default method!";
7943 DCHECK_NE(access_flags & kAccAbstract, 0u) << "Miranda method should be abstract!";
7944 new_method.SetAccessFlags(access_flags | kAccCopied);
7945 move_table_.emplace(mir_method, &new_method);
7946 // Update the entry in the method array, as the array will be used for future lookups,
7947 // where thread suspension is allowed.
7948 // As such, the array should not contain locally allocated ArtMethod, otherwise the GC
7949 // would not see them.
7950 miranda_methods_[i] = &new_method;
7951 ++out;
7952 }
7953 // We need to copy the default methods into our own method table since the runtime requires that
7954 // every method on a class's vtable be in that respective class's virtual method table.
7955 // NOTE This means that two classes might have the same implementation of a method from the same
7956 // interface but will have different ArtMethod*s for them. This also means we cannot compare a
7957 // default method found on a class with one found on the declaring interface directly and must
7958 // look at the declaring class to determine if they are the same.
7959 for (ScopedArenaVector<ArtMethod*>* methods_vec : {&default_methods_,
7960 &overriding_default_methods_}) {
7961 for (size_t i = 0; i < methods_vec->size(); ++i) {
7962 ArtMethod* def_method = (*methods_vec)[i];
7963 ArtMethod& new_method = *out;
7964 new_method.CopyFrom(def_method, pointer_size);
7965 // Clear the kAccSkipAccessChecks flag if it is present. Since this class hasn't been
7966 // verified yet it shouldn't have methods that are skipping access checks.
7967 // TODO This is rather arbitrary. We should maybe support classes where only some of its
7968 // methods are skip_access_checks.
7969 DCHECK_EQ(new_method.GetAccessFlags() & kAccNative, 0u);
7970 constexpr uint32_t kSetFlags = kAccDefault | kAccCopied;
7971 constexpr uint32_t kMaskFlags = ~kAccSkipAccessChecks;
7972 new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags);
7973 move_table_.emplace(def_method, &new_method);
7974 // Update the entry in the method array, as the array will be used for future lookups,
7975 // where thread suspension is allowed.
7976 // As such, the array should not contain locally allocated ArtMethod, otherwise the GC
7977 // would not see them.
7978 (*methods_vec)[i] = &new_method;
7979 ++out;
7980 }
7981 }
7982 for (ScopedArenaVector<ArtMethod*>* methods_vec : {&default_conflict_methods_,
7983 &overriding_default_conflict_methods_}) {
7984 for (size_t i = 0; i < methods_vec->size(); ++i) {
7985 ArtMethod* conf_method = (*methods_vec)[i];
7986 ArtMethod& new_method = *out;
7987 new_method.CopyFrom(conf_method, pointer_size);
7988 // This is a type of default method (there are default method impls, just a conflict) so
7989 // mark this as a default. We use the `kAccAbstract` flag to distinguish it from invokable
7990 // copied default method without using a separate access flag but the default conflicting
7991 // method is technically not abstract and ArtMethod::IsAbstract() shall return false.
7992 // Also clear the kAccSkipAccessChecks bit since this class hasn't been verified yet it
7993 // shouldn't have methods that are skipping access checks. Also clear potential
7994 // kAccSingleImplementation to avoid CHA trying to inline the default method.
7995 uint32_t access_flags = new_method.GetAccessFlags();
7996 DCHECK_EQ(access_flags & kAccNative, 0u);
7997 DCHECK_EQ(access_flags & kAccIntrinsic, 0u);
7998 constexpr uint32_t kSetFlags = kAccDefault | kAccAbstract | kAccCopied;
7999 constexpr uint32_t kMaskFlags = ~(kAccSkipAccessChecks | kAccSingleImplementation);
8000 new_method.SetAccessFlags((access_flags | kSetFlags) & kMaskFlags);
8001 DCHECK(new_method.IsDefaultConflicting());
8002 DCHECK(!new_method.IsAbstract());
8003 // The actual method might or might not be marked abstract since we just copied it from a
8004 // (possibly default) interface method. We need to set it entry point to be the bridge so
8005 // that the compiler will not invoke the implementation of whatever method we copied from.
8006 EnsureThrowsInvocationError(class_linker_, &new_method);
8007 move_table_.emplace(conf_method, &new_method);
8008 // Update the entry in the method array, as the array will be used for future lookups,
8009 // where thread suspension is allowed.
8010 // As such, the array should not contain locally allocated ArtMethod, otherwise the GC
8011 // would not see them.
8012 (*methods_vec)[i] = &new_method;
8013 ++out;
8014 }
8015 }
8016 methods->SetSize(new_method_count);
8017 class_linker_->UpdateClassMethods(klass_.Get(), methods);
8018 }
8019
UpdateVtable(const HashMap<size_t,ClassLinker::MethodTranslation> & default_translations,Handle<mirror::PointerArray> old_vtable)8020 ObjPtr<mirror::PointerArray> ClassLinker::LinkInterfaceMethodsHelper::UpdateVtable(
8021 const HashMap<size_t, ClassLinker::MethodTranslation>& default_translations,
8022 Handle<mirror::PointerArray> old_vtable) {
8023 // Update the vtable to the new method structures. We can skip this for interfaces since they
8024 // do not have vtables.
8025 const size_t old_vtable_count = old_vtable->GetLength();
8026 const size_t new_vtable_count = old_vtable_count +
8027 miranda_methods_.size() +
8028 default_methods_.size() +
8029 default_conflict_methods_.size();
8030
8031 ObjPtr<mirror::PointerArray> vtable = ObjPtr<mirror::PointerArray>::DownCast(
8032 mirror::Array::CopyOf(old_vtable, self_, new_vtable_count));
8033 if (UNLIKELY(vtable == nullptr)) {
8034 self_->AssertPendingOOMException();
8035 return nullptr;
8036 }
8037
8038 size_t vtable_pos = old_vtable_count;
8039 PointerSize pointer_size = class_linker_->GetImagePointerSize();
8040 // Update all the newly copied method's indexes so they denote their placement in the vtable.
8041 for (const ScopedArenaVector<ArtMethod*>& methods_vec : {default_methods_,
8042 default_conflict_methods_,
8043 miranda_methods_}) {
8044 // These are the functions that are not already in the vtable!
8045 for (ArtMethod* new_vtable_method : methods_vec) {
8046 // Leave the declaring class alone the method's dex_code_item_offset_ and dex_method_index_
8047 // fields are references into the dex file the method was defined in. Since the ArtMethod
8048 // does not store that information it uses declaring_class_->dex_cache_.
8049 new_vtable_method->SetMethodIndex(0xFFFF & vtable_pos);
8050 vtable->SetElementPtrSize(vtable_pos, new_vtable_method, pointer_size);
8051 ++vtable_pos;
8052 }
8053 }
8054 DCHECK_EQ(vtable_pos, new_vtable_count);
8055
8056 // Update old vtable methods. We use the default_translations map to figure out what each
8057 // vtable entry should be updated to, if they need to be at all.
8058 for (size_t i = 0; i < old_vtable_count; ++i) {
8059 ArtMethod* translated_method = vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size);
8060 // Try and find what we need to change this method to.
8061 auto translation_it = default_translations.find(i);
8062 if (translation_it != default_translations.end()) {
8063 if (translation_it->second.IsInConflict()) {
8064 // Find which conflict method we are to use for this method.
8065 MethodNameAndSignatureComparator old_method_comparator(
8066 translated_method->GetInterfaceMethodIfProxy(pointer_size));
8067 // We only need to look through overriding_default_conflict_methods since this is an
8068 // overridden method we are fixing up here.
8069 ArtMethod* new_conflict_method = FindSameNameAndSignature(
8070 old_method_comparator, overriding_default_conflict_methods_);
8071 CHECK(new_conflict_method != nullptr) << "Expected a conflict method!";
8072 translated_method = new_conflict_method;
8073 } else if (translation_it->second.IsAbstract()) {
8074 // Find which miranda method we are to use for this method.
8075 MethodNameAndSignatureComparator old_method_comparator(
8076 translated_method->GetInterfaceMethodIfProxy(pointer_size));
8077 ArtMethod* miranda_method = FindSameNameAndSignature(old_method_comparator,
8078 miranda_methods_);
8079 DCHECK(miranda_method != nullptr);
8080 translated_method = miranda_method;
8081 } else {
8082 // Normal default method (changed from an older default or abstract interface method).
8083 DCHECK(translation_it->second.IsTranslation());
8084 translated_method = translation_it->second.GetTranslation();
8085 auto it = move_table_.find(translated_method);
8086 DCHECK(it != move_table_.end());
8087 translated_method = it->second;
8088 }
8089 } else {
8090 auto it = move_table_.find(translated_method);
8091 translated_method = (it != move_table_.end()) ? it->second : nullptr;
8092 }
8093
8094 if (translated_method != nullptr) {
8095 // Make sure the new_methods index is set.
8096 if (translated_method->GetMethodIndexDuringLinking() != i) {
8097 if (kIsDebugBuild) {
8098 auto* methods = klass_->GetMethodsPtr();
8099 CHECK_LE(reinterpret_cast<uintptr_t>(&*methods->begin(method_size_, method_alignment_)),
8100 reinterpret_cast<uintptr_t>(translated_method));
8101 CHECK_LT(reinterpret_cast<uintptr_t>(translated_method),
8102 reinterpret_cast<uintptr_t>(&*methods->end(method_size_, method_alignment_)));
8103 }
8104 translated_method->SetMethodIndex(0xFFFF & i);
8105 }
8106 vtable->SetElementPtrSize(i, translated_method, pointer_size);
8107 }
8108 }
8109 klass_->SetVTable(vtable);
8110 return vtable;
8111 }
8112
UpdateIfTable(Handle<mirror::IfTable> iftable)8113 void ClassLinker::LinkInterfaceMethodsHelper::UpdateIfTable(Handle<mirror::IfTable> iftable) {
8114 PointerSize pointer_size = class_linker_->GetImagePointerSize();
8115 const size_t ifcount = klass_->GetIfTableCount();
8116 // Go fix up all the stale iftable pointers.
8117 for (size_t i = 0; i < ifcount; ++i) {
8118 for (size_t j = 0, count = iftable->GetMethodArrayCount(i); j < count; ++j) {
8119 ObjPtr<mirror::PointerArray> method_array = iftable->GetMethodArray(i);
8120 ArtMethod* m = method_array->GetElementPtrSize<ArtMethod*>(j, pointer_size);
8121 DCHECK(m != nullptr) << klass_->PrettyClass();
8122 auto it = move_table_.find(m);
8123 if (it != move_table_.end()) {
8124 auto* new_m = it->second;
8125 DCHECK(new_m != nullptr) << klass_->PrettyClass();
8126 method_array->SetElementPtrSize(j, new_m, pointer_size);
8127 }
8128 }
8129 }
8130 }
8131
UpdateIMT(ArtMethod ** out_imt)8132 void ClassLinker::LinkInterfaceMethodsHelper::UpdateIMT(ArtMethod** out_imt) {
8133 // Fix up IMT next.
8134 for (size_t i = 0; i < ImTable::kSize; ++i) {
8135 auto it = move_table_.find(out_imt[i]);
8136 if (it != move_table_.end()) {
8137 out_imt[i] = it->second;
8138 }
8139 }
8140 }
8141
8142 // TODO This method needs to be split up into several smaller methods.
LinkInterfaceMethods(Thread * self,Handle<mirror::Class> klass,const HashMap<size_t,ClassLinker::MethodTranslation> & default_translations,bool * out_new_conflict,ArtMethod ** out_imt)8143 bool ClassLinker::LinkInterfaceMethods(
8144 Thread* self,
8145 Handle<mirror::Class> klass,
8146 const HashMap<size_t, ClassLinker::MethodTranslation>& default_translations,
8147 bool* out_new_conflict,
8148 ArtMethod** out_imt) {
8149 StackHandleScope<3> hs(self);
8150 Runtime* const runtime = Runtime::Current();
8151
8152 const bool is_interface = klass->IsInterface();
8153 const bool has_superclass = klass->HasSuperClass();
8154 const bool fill_tables = !is_interface;
8155 const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U;
8156 const size_t ifcount = klass->GetIfTableCount();
8157
8158 Handle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable()));
8159
8160 MutableHandle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking()));
8161 ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod();
8162 ArtMethod* const imt_conflict_method = runtime->GetImtConflictMethod();
8163 // Copy the IMT from the super class if possible.
8164 const bool extend_super_iftable = has_superclass;
8165 if (has_superclass && fill_tables) {
8166 FillImtFromSuperClass(klass,
8167 unimplemented_method,
8168 imt_conflict_method,
8169 out_new_conflict,
8170 out_imt);
8171 }
8172 // Allocate method arrays before since we don't want miss visiting miranda method roots due to
8173 // thread suspension.
8174 if (fill_tables) {
8175 if (!AllocateIfTableMethodArrays(self, klass, iftable)) {
8176 return false;
8177 }
8178 }
8179
8180 LinkInterfaceMethodsHelper helper(this, klass, self, runtime);
8181
8182 auto* old_cause = self->StartAssertNoThreadSuspension(
8183 "Copying ArtMethods for LinkInterfaceMethods");
8184 // Going in reverse to ensure that we will hit abstract methods that override defaults before the
8185 // defaults. This means we don't need to do any trickery when creating the Miranda methods, since
8186 // they will already be null. This has the additional benefit that the declarer of a miranda
8187 // method will actually declare an abstract method.
8188 for (size_t i = ifcount; i != 0u; ) {
8189 --i;
8190 DCHECK_LT(i, ifcount);
8191
8192 size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods();
8193 if (num_methods > 0) {
8194 StackHandleScope<2> hs2(self);
8195 const bool is_super = i < super_ifcount;
8196 const bool super_interface = is_super && extend_super_iftable;
8197 // We don't actually create or fill these tables for interfaces, we just copy some methods for
8198 // conflict methods. Just set this as nullptr in those cases.
8199 Handle<mirror::PointerArray> method_array(fill_tables
8200 ? hs2.NewHandle(iftable->GetMethodArray(i))
8201 : hs2.NewHandle<mirror::PointerArray>(nullptr));
8202
8203 ArraySlice<ArtMethod> input_virtual_methods;
8204 ScopedNullHandle<mirror::PointerArray> null_handle;
8205 Handle<mirror::PointerArray> input_vtable_array(null_handle);
8206 int32_t input_array_length = 0;
8207
8208 // TODO Cleanup Needed: In the presence of default methods this optimization is rather dirty
8209 // and confusing. Default methods should always look through all the superclasses
8210 // because they are the last choice of an implementation. We get around this by looking
8211 // at the super-classes iftable methods (copied into method_array previously) when we are
8212 // looking for the implementation of a super-interface method but that is rather dirty.
8213 bool using_virtuals;
8214 if (super_interface || is_interface) {
8215 // If we are overwriting a super class interface, try to only virtual methods instead of the
8216 // whole vtable.
8217 using_virtuals = true;
8218 input_virtual_methods = klass->GetDeclaredVirtualMethodsSlice(image_pointer_size_);
8219 input_array_length = input_virtual_methods.size();
8220 } else {
8221 // For a new interface, however, we need the whole vtable in case a new
8222 // interface method is implemented in the whole superclass.
8223 using_virtuals = false;
8224 DCHECK(vtable != nullptr);
8225 input_vtable_array = vtable;
8226 input_array_length = input_vtable_array->GetLength();
8227 }
8228
8229 // For each method in interface
8230 for (size_t j = 0; j < num_methods; ++j) {
8231 auto* interface_method = iftable->GetInterface(i)->GetVirtualMethod(j, image_pointer_size_);
8232 MethodNameAndSignatureComparator interface_name_comparator(
8233 interface_method->GetInterfaceMethodIfProxy(image_pointer_size_));
8234 uint32_t imt_index = interface_method->GetImtIndex();
8235 ArtMethod** imt_ptr = &out_imt[imt_index];
8236 // For each method listed in the interface's method list, find the
8237 // matching method in our class's method list. We want to favor the
8238 // subclass over the superclass, which just requires walking
8239 // back from the end of the vtable. (This only matters if the
8240 // superclass defines a private method and this class redefines
8241 // it -- otherwise it would use the same vtable slot. In .dex files
8242 // those don't end up in the virtual method table, so it shouldn't
8243 // matter which direction we go. We walk it backward anyway.)
8244 //
8245 // To find defaults we need to do the same but also go over interfaces.
8246 bool found_impl = false;
8247 ArtMethod* vtable_impl = nullptr;
8248 for (int32_t k = input_array_length - 1; k >= 0; --k) {
8249 ArtMethod* vtable_method = using_virtuals ?
8250 &input_virtual_methods[k] :
8251 input_vtable_array->GetElementPtrSize<ArtMethod*>(k, image_pointer_size_);
8252 ArtMethod* vtable_method_for_name_comparison =
8253 vtable_method->GetInterfaceMethodIfProxy(image_pointer_size_);
8254 DCHECK(!vtable_method->IsStatic()) << vtable_method->PrettyMethod();
8255 if (interface_name_comparator.HasSameNameAndSignature(
8256 vtable_method_for_name_comparison)) {
8257 if (!vtable_method->IsAbstract() && !vtable_method->IsPublic()) {
8258 // Must do EndAssertNoThreadSuspension before throw since the throw can cause
8259 // allocations.
8260 self->EndAssertNoThreadSuspension(old_cause);
8261 ThrowIllegalAccessError(klass.Get(),
8262 "Method '%s' implementing interface method '%s' is not public",
8263 vtable_method->PrettyMethod().c_str(),
8264 interface_method->PrettyMethod().c_str());
8265 return false;
8266 } else if (UNLIKELY(vtable_method->IsOverridableByDefaultMethod())) {
8267 // We might have a newer, better, default method for this, so we just skip it. If we
8268 // are still using this we will select it again when scanning for default methods. To
8269 // obviate the need to copy the method again we will make a note that we already found
8270 // a default here.
8271 // TODO This should be much cleaner.
8272 vtable_impl = vtable_method;
8273 break;
8274 } else {
8275 found_impl = true;
8276 if (LIKELY(fill_tables)) {
8277 method_array->SetElementPtrSize(j, vtable_method, image_pointer_size_);
8278 // Place method in imt if entry is empty, place conflict otherwise.
8279 SetIMTRef(unimplemented_method,
8280 imt_conflict_method,
8281 vtable_method,
8282 /*out*/out_new_conflict,
8283 /*out*/imt_ptr);
8284 }
8285 break;
8286 }
8287 }
8288 }
8289 // Continue on to the next method if we are done.
8290 if (LIKELY(found_impl)) {
8291 continue;
8292 } else if (LIKELY(super_interface)) {
8293 // Don't look for a default implementation when the super-method is implemented directly
8294 // by the class.
8295 //
8296 // See if we can use the superclasses method and skip searching everything else.
8297 // Note: !found_impl && super_interface
8298 CHECK(extend_super_iftable);
8299 // If this is a super_interface method it is possible we shouldn't override it because a
8300 // superclass could have implemented it directly. We get the method the superclass used
8301 // to implement this to know if we can override it with a default method. Doing this is
8302 // safe since we know that the super_iftable is filled in so we can simply pull it from
8303 // there. We don't bother if this is not a super-classes interface since in that case we
8304 // have scanned the entire vtable anyway and would have found it.
8305 // TODO This is rather dirty but it is faster than searching through the entire vtable
8306 // every time.
8307 ArtMethod* supers_method =
8308 method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_);
8309 DCHECK(supers_method != nullptr);
8310 DCHECK(interface_name_comparator.HasSameNameAndSignature(supers_method));
8311 if (LIKELY(!supers_method->IsOverridableByDefaultMethod())) {
8312 // The method is not overridable by a default method (i.e. it is directly implemented
8313 // in some class). Therefore move onto the next interface method.
8314 continue;
8315 } else {
8316 // If the super-classes method is override-able by a default method we need to keep
8317 // track of it since though it is override-able it is not guaranteed to be 'overridden'.
8318 // If it turns out not to be overridden and we did not keep track of it we might add it
8319 // to the vtable twice, causing corruption (vtable entries having inconsistent and
8320 // illegal states, incorrect vtable size, and incorrect or inconsistent iftable entries)
8321 // in this class and any subclasses.
8322 DCHECK(vtable_impl == nullptr || vtable_impl == supers_method)
8323 << "vtable_impl was " << ArtMethod::PrettyMethod(vtable_impl)
8324 << " and not 'nullptr' or "
8325 << supers_method->PrettyMethod()
8326 << " as expected. IFTable appears to be corrupt!";
8327 vtable_impl = supers_method;
8328 }
8329 }
8330 // If we haven't found it yet we should search through the interfaces for default methods.
8331 ArtMethod* current_method = helper.FindMethod(interface_method,
8332 interface_name_comparator,
8333 vtable_impl);
8334 if (LIKELY(fill_tables)) {
8335 if (current_method == nullptr && !super_interface) {
8336 // We could not find an implementation for this method and since it is a brand new
8337 // interface we searched the entire vtable (and all default methods) for an
8338 // implementation but couldn't find one. We therefore need to make a miranda method.
8339 current_method = helper.GetOrCreateMirandaMethod(interface_method,
8340 interface_name_comparator);
8341 }
8342
8343 if (current_method != nullptr) {
8344 // We found a default method implementation. Record it in the iftable and IMT.
8345 method_array->SetElementPtrSize(j, current_method, image_pointer_size_);
8346 SetIMTRef(unimplemented_method,
8347 imt_conflict_method,
8348 current_method,
8349 /*out*/out_new_conflict,
8350 /*out*/imt_ptr);
8351 }
8352 }
8353 } // For each method in interface end.
8354 } // if (num_methods > 0)
8355 } // For each interface.
8356 // TODO don't extend virtuals of interface unless necessary (when is it?).
8357 if (helper.HasNewVirtuals()) {
8358 LengthPrefixedArray<ArtMethod>* old_methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr;
8359 helper.ReallocMethods(); // No return value to check. Native allocation failure aborts.
8360 LengthPrefixedArray<ArtMethod>* methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr;
8361
8362 // Done copying methods, they are all roots in the class now, so we can end the no thread
8363 // suspension assert.
8364 self->EndAssertNoThreadSuspension(old_cause);
8365
8366 if (fill_tables) {
8367 vtable.Assign(helper.UpdateVtable(default_translations, vtable));
8368 if (UNLIKELY(vtable == nullptr)) {
8369 // The helper has already called self->AssertPendingOOMException();
8370 return false;
8371 }
8372 helper.UpdateIfTable(iftable);
8373 helper.UpdateIMT(out_imt);
8374 }
8375
8376 helper.CheckNoStaleMethodsInDexCache();
8377 helper.ClobberOldMethods(old_methods, methods);
8378 } else {
8379 self->EndAssertNoThreadSuspension(old_cause);
8380 }
8381 if (kIsDebugBuild && !is_interface) {
8382 CheckVTable(self, klass, image_pointer_size_);
8383 }
8384 return true;
8385 }
8386
8387 class ClassLinker::LinkFieldsHelper {
8388 public:
8389 static bool LinkFields(ClassLinker* class_linker,
8390 Thread* self,
8391 Handle<mirror::Class> klass,
8392 bool is_static,
8393 size_t* class_size)
8394 REQUIRES_SHARED(Locks::mutator_lock_);
8395
8396 private:
8397 enum class FieldTypeOrder : uint16_t;
8398 class FieldGaps;
8399
8400 struct FieldTypeOrderAndIndex {
8401 FieldTypeOrder field_type_order;
8402 uint16_t field_index;
8403 };
8404
8405 static FieldTypeOrder FieldTypeOrderFromFirstDescriptorCharacter(char first_char);
8406
8407 template <size_t kSize>
8408 static MemberOffset AssignFieldOffset(ArtField* field, MemberOffset field_offset)
8409 REQUIRES_SHARED(Locks::mutator_lock_);
8410 };
8411
8412 // We use the following order of field types for assigning offsets.
8413 // Some fields can be shuffled forward to fill gaps, see `ClassLinker::LinkFields()`.
8414 enum class ClassLinker::LinkFieldsHelper::FieldTypeOrder : uint16_t {
8415 kReference = 0u,
8416 kLong,
8417 kDouble,
8418 kInt,
8419 kFloat,
8420 kChar,
8421 kShort,
8422 kBoolean,
8423 kByte,
8424
8425 kLast64BitType = kDouble,
8426 kLast32BitType = kFloat,
8427 kLast16BitType = kShort,
8428 };
8429
8430 ALWAYS_INLINE
8431 ClassLinker::LinkFieldsHelper::FieldTypeOrder
FieldTypeOrderFromFirstDescriptorCharacter(char first_char)8432 ClassLinker::LinkFieldsHelper::FieldTypeOrderFromFirstDescriptorCharacter(char first_char) {
8433 switch (first_char) {
8434 case 'J':
8435 return FieldTypeOrder::kLong;
8436 case 'D':
8437 return FieldTypeOrder::kDouble;
8438 case 'I':
8439 return FieldTypeOrder::kInt;
8440 case 'F':
8441 return FieldTypeOrder::kFloat;
8442 case 'C':
8443 return FieldTypeOrder::kChar;
8444 case 'S':
8445 return FieldTypeOrder::kShort;
8446 case 'Z':
8447 return FieldTypeOrder::kBoolean;
8448 case 'B':
8449 return FieldTypeOrder::kByte;
8450 default:
8451 DCHECK(first_char == 'L' || first_char == '[') << first_char;
8452 return FieldTypeOrder::kReference;
8453 }
8454 }
8455
8456 // Gaps where we can insert fields in object layout.
8457 class ClassLinker::LinkFieldsHelper::FieldGaps {
8458 public:
8459 template <uint32_t kSize>
AlignFieldOffset(MemberOffset field_offset)8460 ALWAYS_INLINE MemberOffset AlignFieldOffset(MemberOffset field_offset) {
8461 static_assert(kSize == 2u || kSize == 4u || kSize == 8u);
8462 if (!IsAligned<kSize>(field_offset.Uint32Value())) {
8463 uint32_t gap_start = field_offset.Uint32Value();
8464 field_offset = MemberOffset(RoundUp(gap_start, kSize));
8465 AddGaps<kSize - 1u>(gap_start, field_offset.Uint32Value());
8466 }
8467 return field_offset;
8468 }
8469
8470 template <uint32_t kSize>
HasGap() const8471 bool HasGap() const {
8472 static_assert(kSize == 1u || kSize == 2u || kSize == 4u);
8473 return (kSize == 1u && gap1_offset_ != kNoOffset) ||
8474 (kSize <= 2u && gap2_offset_ != kNoOffset) ||
8475 gap4_offset_ != kNoOffset;
8476 }
8477
8478 template <uint32_t kSize>
ReleaseGap()8479 MemberOffset ReleaseGap() {
8480 static_assert(kSize == 1u || kSize == 2u || kSize == 4u);
8481 uint32_t result;
8482 if (kSize == 1u && gap1_offset_ != kNoOffset) {
8483 DCHECK(gap2_offset_ == kNoOffset || gap2_offset_ > gap1_offset_);
8484 DCHECK(gap4_offset_ == kNoOffset || gap4_offset_ > gap1_offset_);
8485 result = gap1_offset_;
8486 gap1_offset_ = kNoOffset;
8487 } else if (kSize <= 2u && gap2_offset_ != kNoOffset) {
8488 DCHECK(gap4_offset_ == kNoOffset || gap4_offset_ > gap2_offset_);
8489 result = gap2_offset_;
8490 gap2_offset_ = kNoOffset;
8491 if (kSize < 2u) {
8492 AddGaps<1u>(result + kSize, result + 2u);
8493 }
8494 } else {
8495 DCHECK_NE(gap4_offset_, kNoOffset);
8496 result = gap4_offset_;
8497 gap4_offset_ = kNoOffset;
8498 if (kSize < 4u) {
8499 AddGaps<kSize | 2u>(result + kSize, result + 4u);
8500 }
8501 }
8502 return MemberOffset(result);
8503 }
8504
8505 private:
8506 template <uint32_t kGapsToCheck>
AddGaps(uint32_t gap_start,uint32_t gap_end)8507 void AddGaps(uint32_t gap_start, uint32_t gap_end) {
8508 if ((kGapsToCheck & 1u) != 0u) {
8509 DCHECK_LT(gap_start, gap_end);
8510 DCHECK_ALIGNED(gap_end, 2u);
8511 if ((gap_start & 1u) != 0u) {
8512 DCHECK_EQ(gap1_offset_, kNoOffset);
8513 gap1_offset_ = gap_start;
8514 gap_start += 1u;
8515 if (kGapsToCheck == 1u || gap_start == gap_end) {
8516 DCHECK_EQ(gap_start, gap_end);
8517 return;
8518 }
8519 }
8520 }
8521
8522 if ((kGapsToCheck & 2u) != 0u) {
8523 DCHECK_LT(gap_start, gap_end);
8524 DCHECK_ALIGNED(gap_start, 2u);
8525 DCHECK_ALIGNED(gap_end, 4u);
8526 if ((gap_start & 2u) != 0u) {
8527 DCHECK_EQ(gap2_offset_, kNoOffset);
8528 gap2_offset_ = gap_start;
8529 gap_start += 2u;
8530 if (kGapsToCheck <= 3u || gap_start == gap_end) {
8531 DCHECK_EQ(gap_start, gap_end);
8532 return;
8533 }
8534 }
8535 }
8536
8537 if ((kGapsToCheck & 4u) != 0u) {
8538 DCHECK_LT(gap_start, gap_end);
8539 DCHECK_ALIGNED(gap_start, 4u);
8540 DCHECK_ALIGNED(gap_end, 8u);
8541 DCHECK_EQ(gap_start + 4u, gap_end);
8542 DCHECK_EQ(gap4_offset_, kNoOffset);
8543 gap4_offset_ = gap_start;
8544 return;
8545 }
8546
8547 DCHECK(false) << "Remaining gap: " << gap_start << " to " << gap_end
8548 << " after checking " << kGapsToCheck;
8549 }
8550
8551 static constexpr uint32_t kNoOffset = static_cast<uint32_t>(-1);
8552
8553 uint32_t gap4_offset_ = kNoOffset;
8554 uint32_t gap2_offset_ = kNoOffset;
8555 uint32_t gap1_offset_ = kNoOffset;
8556 };
8557
8558 template <size_t kSize>
8559 ALWAYS_INLINE
AssignFieldOffset(ArtField * field,MemberOffset field_offset)8560 MemberOffset ClassLinker::LinkFieldsHelper::AssignFieldOffset(ArtField* field,
8561 MemberOffset field_offset) {
8562 DCHECK_ALIGNED(field_offset.Uint32Value(), kSize);
8563 DCHECK_EQ(Primitive::ComponentSize(field->GetTypeAsPrimitiveType()), kSize);
8564 field->SetOffset(field_offset);
8565 return MemberOffset(field_offset.Uint32Value() + kSize);
8566 }
8567
LinkFields(ClassLinker * class_linker,Thread * self,Handle<mirror::Class> klass,bool is_static,size_t * class_size)8568 bool ClassLinker::LinkFieldsHelper::LinkFields(ClassLinker* class_linker,
8569 Thread* self,
8570 Handle<mirror::Class> klass,
8571 bool is_static,
8572 size_t* class_size) {
8573 self->AllowThreadSuspension();
8574 const size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
8575 LengthPrefixedArray<ArtField>* const fields = is_static ? klass->GetSFieldsPtr() :
8576 klass->GetIFieldsPtr();
8577
8578 // Initialize field_offset
8579 MemberOffset field_offset(0);
8580 if (is_static) {
8581 field_offset = klass->GetFirstReferenceStaticFieldOffsetDuringLinking(
8582 class_linker->GetImagePointerSize());
8583 } else {
8584 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
8585 if (super_class != nullptr) {
8586 CHECK(super_class->IsResolved())
8587 << klass->PrettyClass() << " " << super_class->PrettyClass();
8588 field_offset = MemberOffset(super_class->GetObjectSize());
8589 }
8590 }
8591
8592 CHECK_EQ(num_fields == 0, fields == nullptr) << klass->PrettyClass();
8593
8594 // we want a relatively stable order so that adding new fields
8595 // minimizes disruption of C++ version such as Class and Method.
8596 //
8597 // The overall sort order order is:
8598 // 1) All object reference fields, sorted alphabetically.
8599 // 2) All java long (64-bit) integer fields, sorted alphabetically.
8600 // 3) All java double (64-bit) floating point fields, sorted alphabetically.
8601 // 4) All java int (32-bit) integer fields, sorted alphabetically.
8602 // 5) All java float (32-bit) floating point fields, sorted alphabetically.
8603 // 6) All java char (16-bit) integer fields, sorted alphabetically.
8604 // 7) All java short (16-bit) integer fields, sorted alphabetically.
8605 // 8) All java boolean (8-bit) integer fields, sorted alphabetically.
8606 // 9) All java byte (8-bit) integer fields, sorted alphabetically.
8607 //
8608 // (References are first to increase the chance of reference visiting
8609 // being able to take a fast path using a bitmap of references at the
8610 // start of the object, see `Class::reference_instance_offsets_`.)
8611 //
8612 // Once the fields are sorted in this order we will attempt to fill any gaps
8613 // that might be present in the memory layout of the structure.
8614 // Note that we shall not fill gaps between the superclass fields.
8615
8616 // Collect fields and their "type order index" (see numbered points above).
8617 const char* old_no_suspend_cause = self->StartAssertNoThreadSuspension(
8618 "Using plain ArtField references");
8619 constexpr size_t kStackBufferEntries = 64; // Avoid allocations for small number of fields.
8620 FieldTypeOrderAndIndex stack_buffer[kStackBufferEntries];
8621 std::vector<FieldTypeOrderAndIndex> heap_buffer;
8622 ArrayRef<FieldTypeOrderAndIndex> sorted_fields;
8623 if (num_fields <= kStackBufferEntries) {
8624 sorted_fields = ArrayRef<FieldTypeOrderAndIndex>(stack_buffer, num_fields);
8625 } else {
8626 heap_buffer.resize(num_fields);
8627 sorted_fields = ArrayRef<FieldTypeOrderAndIndex>(heap_buffer);
8628 }
8629 size_t num_reference_fields = 0;
8630 size_t primitive_fields_start = num_fields;
8631 DCHECK_LE(num_fields, 1u << 16);
8632 for (size_t i = 0; i != num_fields; ++i) {
8633 ArtField* field = &fields->At(i);
8634 const char* descriptor = field->GetTypeDescriptor();
8635 FieldTypeOrder field_type_order = FieldTypeOrderFromFirstDescriptorCharacter(descriptor[0]);
8636 uint16_t field_index = dchecked_integral_cast<uint16_t>(i);
8637 // Insert references to the start, other fields to the end.
8638 DCHECK_LT(num_reference_fields, primitive_fields_start);
8639 if (field_type_order == FieldTypeOrder::kReference) {
8640 sorted_fields[num_reference_fields] = { field_type_order, field_index };
8641 ++num_reference_fields;
8642 } else {
8643 --primitive_fields_start;
8644 sorted_fields[primitive_fields_start] = { field_type_order, field_index };
8645 }
8646 }
8647 DCHECK_EQ(num_reference_fields, primitive_fields_start);
8648
8649 // Reference fields are already sorted by field index (and dex field index).
8650 DCHECK(std::is_sorted(
8651 sorted_fields.begin(),
8652 sorted_fields.begin() + num_reference_fields,
8653 [fields](const auto& lhs, const auto& rhs) REQUIRES_SHARED(Locks::mutator_lock_) {
8654 ArtField* lhs_field = &fields->At(lhs.field_index);
8655 ArtField* rhs_field = &fields->At(rhs.field_index);
8656 CHECK_EQ(lhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
8657 CHECK_EQ(rhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
8658 CHECK_EQ(lhs_field->GetDexFieldIndex() < rhs_field->GetDexFieldIndex(),
8659 lhs.field_index < rhs.field_index);
8660 return lhs_field->GetDexFieldIndex() < rhs_field->GetDexFieldIndex();
8661 }));
8662 // Primitive fields were stored in reverse order of their field index (and dex field index).
8663 DCHECK(std::is_sorted(
8664 sorted_fields.begin() + primitive_fields_start,
8665 sorted_fields.end(),
8666 [fields](const auto& lhs, const auto& rhs) REQUIRES_SHARED(Locks::mutator_lock_) {
8667 ArtField* lhs_field = &fields->At(lhs.field_index);
8668 ArtField* rhs_field = &fields->At(rhs.field_index);
8669 CHECK_NE(lhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
8670 CHECK_NE(rhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
8671 CHECK_EQ(lhs_field->GetDexFieldIndex() > rhs_field->GetDexFieldIndex(),
8672 lhs.field_index > rhs.field_index);
8673 return lhs.field_index > rhs.field_index;
8674 }));
8675 // Sort the primitive fields by the field type order, then field index.
8676 std::sort(sorted_fields.begin() + primitive_fields_start,
8677 sorted_fields.end(),
8678 [](const auto& lhs, const auto& rhs) {
8679 if (lhs.field_type_order != rhs.field_type_order) {
8680 return lhs.field_type_order < rhs.field_type_order;
8681 } else {
8682 return lhs.field_index < rhs.field_index;
8683 }
8684 });
8685 // Primitive fields are now sorted by field size (descending), then type, then field index.
8686 DCHECK(std::is_sorted(
8687 sorted_fields.begin() + primitive_fields_start,
8688 sorted_fields.end(),
8689 [fields](const auto& lhs, const auto& rhs) REQUIRES_SHARED(Locks::mutator_lock_) {
8690 ArtField* lhs_field = &fields->At(lhs.field_index);
8691 ArtField* rhs_field = &fields->At(rhs.field_index);
8692 Primitive::Type lhs_type = lhs_field->GetTypeAsPrimitiveType();
8693 CHECK_NE(lhs_type, Primitive::kPrimNot);
8694 Primitive::Type rhs_type = rhs_field->GetTypeAsPrimitiveType();
8695 CHECK_NE(rhs_type, Primitive::kPrimNot);
8696 if (lhs_type != rhs_type) {
8697 size_t lhs_size = Primitive::ComponentSize(lhs_type);
8698 size_t rhs_size = Primitive::ComponentSize(rhs_type);
8699 return (lhs_size != rhs_size) ? (lhs_size > rhs_size) : (lhs_type < rhs_type);
8700 } else {
8701 return lhs_field->GetDexFieldIndex() < rhs_field->GetDexFieldIndex();
8702 }
8703 }));
8704
8705 // Process reference fields.
8706 FieldGaps field_gaps;
8707 size_t index = 0u;
8708 if (num_reference_fields != 0u) {
8709 constexpr size_t kReferenceSize = sizeof(mirror::HeapReference<mirror::Object>);
8710 field_offset = field_gaps.AlignFieldOffset<kReferenceSize>(field_offset);
8711 for (; index != num_reference_fields; ++index) {
8712 ArtField* field = &fields->At(sorted_fields[index].field_index);
8713 field_offset = AssignFieldOffset<kReferenceSize>(field, field_offset);
8714 }
8715 }
8716 // Process 64-bit fields.
8717 if (index != num_fields &&
8718 sorted_fields[index].field_type_order <= FieldTypeOrder::kLast64BitType) {
8719 field_offset = field_gaps.AlignFieldOffset<8u>(field_offset);
8720 while (index != num_fields &&
8721 sorted_fields[index].field_type_order <= FieldTypeOrder::kLast64BitType) {
8722 ArtField* field = &fields->At(sorted_fields[index].field_index);
8723 field_offset = AssignFieldOffset<8u>(field, field_offset);
8724 ++index;
8725 }
8726 }
8727 // Process 32-bit fields.
8728 if (index != num_fields &&
8729 sorted_fields[index].field_type_order <= FieldTypeOrder::kLast32BitType) {
8730 field_offset = field_gaps.AlignFieldOffset<4u>(field_offset);
8731 if (field_gaps.HasGap<4u>()) {
8732 ArtField* field = &fields->At(sorted_fields[index].field_index);
8733 AssignFieldOffset<4u>(field, field_gaps.ReleaseGap<4u>()); // Ignore return value.
8734 ++index;
8735 DCHECK(!field_gaps.HasGap<4u>()); // There can be only one gap for a 32-bit field.
8736 }
8737 while (index != num_fields &&
8738 sorted_fields[index].field_type_order <= FieldTypeOrder::kLast32BitType) {
8739 ArtField* field = &fields->At(sorted_fields[index].field_index);
8740 field_offset = AssignFieldOffset<4u>(field, field_offset);
8741 ++index;
8742 }
8743 }
8744 // Process 16-bit fields.
8745 if (index != num_fields &&
8746 sorted_fields[index].field_type_order <= FieldTypeOrder::kLast16BitType) {
8747 field_offset = field_gaps.AlignFieldOffset<2u>(field_offset);
8748 while (index != num_fields &&
8749 sorted_fields[index].field_type_order <= FieldTypeOrder::kLast16BitType &&
8750 field_gaps.HasGap<2u>()) {
8751 ArtField* field = &fields->At(sorted_fields[index].field_index);
8752 AssignFieldOffset<2u>(field, field_gaps.ReleaseGap<2u>()); // Ignore return value.
8753 ++index;
8754 }
8755 while (index != num_fields &&
8756 sorted_fields[index].field_type_order <= FieldTypeOrder::kLast16BitType) {
8757 ArtField* field = &fields->At(sorted_fields[index].field_index);
8758 field_offset = AssignFieldOffset<2u>(field, field_offset);
8759 ++index;
8760 }
8761 }
8762 // Process 8-bit fields.
8763 for (; index != num_fields && field_gaps.HasGap<1u>(); ++index) {
8764 ArtField* field = &fields->At(sorted_fields[index].field_index);
8765 AssignFieldOffset<1u>(field, field_gaps.ReleaseGap<1u>()); // Ignore return value.
8766 }
8767 for (; index != num_fields; ++index) {
8768 ArtField* field = &fields->At(sorted_fields[index].field_index);
8769 field_offset = AssignFieldOffset<1u>(field, field_offset);
8770 }
8771
8772 self->EndAssertNoThreadSuspension(old_no_suspend_cause);
8773
8774 // We lie to the GC about the java.lang.ref.Reference.referent field, so it doesn't scan it.
8775 DCHECK(!class_linker->init_done_ || !klass->DescriptorEquals("Ljava/lang/ref/Reference;"));
8776 if (!is_static &&
8777 UNLIKELY(!class_linker->init_done_) &&
8778 klass->DescriptorEquals("Ljava/lang/ref/Reference;")) {
8779 // We know there are no non-reference fields in the Reference classes, and we know
8780 // that 'referent' is alphabetically last, so this is easy...
8781 CHECK_EQ(num_reference_fields, num_fields) << klass->PrettyClass();
8782 CHECK_STREQ(fields->At(num_fields - 1).GetName(), "referent")
8783 << klass->PrettyClass();
8784 --num_reference_fields;
8785 }
8786
8787 size_t size = field_offset.Uint32Value();
8788 // Update klass
8789 if (is_static) {
8790 klass->SetNumReferenceStaticFields(num_reference_fields);
8791 *class_size = size;
8792 } else {
8793 klass->SetNumReferenceInstanceFields(num_reference_fields);
8794 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
8795 if (num_reference_fields == 0 || super_class == nullptr) {
8796 // object has one reference field, klass, but we ignore it since we always visit the class.
8797 // super_class is null iff the class is java.lang.Object.
8798 if (super_class == nullptr ||
8799 (super_class->GetClassFlags() & mirror::kClassFlagNoReferenceFields) != 0) {
8800 klass->SetClassFlags(klass->GetClassFlags() | mirror::kClassFlagNoReferenceFields);
8801 }
8802 }
8803 if (kIsDebugBuild) {
8804 DCHECK_EQ(super_class == nullptr, klass->DescriptorEquals("Ljava/lang/Object;"));
8805 size_t total_reference_instance_fields = 0;
8806 ObjPtr<mirror::Class> cur_super = klass.Get();
8807 while (cur_super != nullptr) {
8808 total_reference_instance_fields += cur_super->NumReferenceInstanceFieldsDuringLinking();
8809 cur_super = cur_super->GetSuperClass();
8810 }
8811 if (super_class == nullptr) {
8812 CHECK_EQ(total_reference_instance_fields, 1u) << klass->PrettyDescriptor();
8813 } else {
8814 // Check that there is at least num_reference_fields other than Object.class.
8815 CHECK_GE(total_reference_instance_fields, 1u + num_reference_fields)
8816 << klass->PrettyClass();
8817 }
8818 }
8819 if (!klass->IsVariableSize()) {
8820 std::string temp;
8821 DCHECK_GE(size, sizeof(mirror::Object)) << klass->GetDescriptor(&temp);
8822 size_t previous_size = klass->GetObjectSize();
8823 if (previous_size != 0) {
8824 // Make sure that we didn't originally have an incorrect size.
8825 CHECK_EQ(previous_size, size) << klass->GetDescriptor(&temp);
8826 }
8827 klass->SetObjectSize(size);
8828 }
8829 }
8830
8831 if (kIsDebugBuild) {
8832 // Make sure that the fields array is ordered by name but all reference
8833 // offsets are at the beginning as far as alignment allows.
8834 MemberOffset start_ref_offset = is_static
8835 ? klass->GetFirstReferenceStaticFieldOffsetDuringLinking(class_linker->image_pointer_size_)
8836 : klass->GetFirstReferenceInstanceFieldOffset();
8837 MemberOffset end_ref_offset(start_ref_offset.Uint32Value() +
8838 num_reference_fields *
8839 sizeof(mirror::HeapReference<mirror::Object>));
8840 MemberOffset current_ref_offset = start_ref_offset;
8841 for (size_t i = 0; i < num_fields; i++) {
8842 ArtField* field = &fields->At(i);
8843 VLOG(class_linker) << "LinkFields: " << (is_static ? "static" : "instance")
8844 << " class=" << klass->PrettyClass() << " field=" << field->PrettyField()
8845 << " offset=" << field->GetOffsetDuringLinking();
8846 if (i != 0) {
8847 ArtField* const prev_field = &fields->At(i - 1);
8848 // NOTE: The field names can be the same. This is not possible in the Java language
8849 // but it's valid Java/dex bytecode and for example proguard can generate such bytecode.
8850 DCHECK_LE(strcmp(prev_field->GetName(), field->GetName()), 0);
8851 }
8852 Primitive::Type type = field->GetTypeAsPrimitiveType();
8853 bool is_primitive = type != Primitive::kPrimNot;
8854 if (klass->DescriptorEquals("Ljava/lang/ref/Reference;") &&
8855 strcmp("referent", field->GetName()) == 0) {
8856 is_primitive = true; // We lied above, so we have to expect a lie here.
8857 }
8858 MemberOffset offset = field->GetOffsetDuringLinking();
8859 if (is_primitive) {
8860 if (offset.Uint32Value() < end_ref_offset.Uint32Value()) {
8861 // Shuffled before references.
8862 size_t type_size = Primitive::ComponentSize(type);
8863 CHECK_LT(type_size, sizeof(mirror::HeapReference<mirror::Object>));
8864 CHECK_LT(offset.Uint32Value(), start_ref_offset.Uint32Value());
8865 CHECK_LE(offset.Uint32Value() + type_size, start_ref_offset.Uint32Value());
8866 CHECK(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(offset.Uint32Value()));
8867 }
8868 } else {
8869 CHECK_EQ(current_ref_offset.Uint32Value(), offset.Uint32Value());
8870 current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() +
8871 sizeof(mirror::HeapReference<mirror::Object>));
8872 }
8873 }
8874 CHECK_EQ(current_ref_offset.Uint32Value(), end_ref_offset.Uint32Value());
8875 }
8876 return true;
8877 }
8878
LinkInstanceFields(Thread * self,Handle<mirror::Class> klass)8879 bool ClassLinker::LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) {
8880 CHECK(klass != nullptr);
8881 return LinkFieldsHelper::LinkFields(this, self, klass, false, nullptr);
8882 }
8883
LinkStaticFields(Thread * self,Handle<mirror::Class> klass,size_t * class_size)8884 bool ClassLinker::LinkStaticFields(Thread* self, Handle<mirror::Class> klass, size_t* class_size) {
8885 CHECK(klass != nullptr);
8886 return LinkFieldsHelper::LinkFields(this, self, klass, true, class_size);
8887 }
8888
8889 // Set the bitmap of reference instance field offsets.
CreateReferenceInstanceOffsets(Handle<mirror::Class> klass)8890 void ClassLinker::CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) {
8891 uint32_t reference_offsets = 0;
8892 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
8893 // Leave the reference offsets as 0 for mirror::Object (the class field is handled specially).
8894 if (super_class != nullptr) {
8895 reference_offsets = super_class->GetReferenceInstanceOffsets();
8896 // Compute reference offsets unless our superclass overflowed.
8897 if (reference_offsets != mirror::Class::kClassWalkSuper) {
8898 size_t num_reference_fields = klass->NumReferenceInstanceFieldsDuringLinking();
8899 if (num_reference_fields != 0u) {
8900 // All of the fields that contain object references are guaranteed be grouped in memory
8901 // starting at an appropriately aligned address after super class object data.
8902 uint32_t start_offset = RoundUp(super_class->GetObjectSize(),
8903 sizeof(mirror::HeapReference<mirror::Object>));
8904 uint32_t start_bit = (start_offset - mirror::kObjectHeaderSize) /
8905 sizeof(mirror::HeapReference<mirror::Object>);
8906 if (start_bit + num_reference_fields > 32) {
8907 reference_offsets = mirror::Class::kClassWalkSuper;
8908 } else {
8909 reference_offsets |= (0xffffffffu << start_bit) &
8910 (0xffffffffu >> (32 - (start_bit + num_reference_fields)));
8911 }
8912 }
8913 }
8914 }
8915 klass->SetReferenceInstanceOffsets(reference_offsets);
8916 }
8917
DoResolveString(dex::StringIndex string_idx,ObjPtr<mirror::DexCache> dex_cache)8918 ObjPtr<mirror::String> ClassLinker::DoResolveString(dex::StringIndex string_idx,
8919 ObjPtr<mirror::DexCache> dex_cache) {
8920 StackHandleScope<1> hs(Thread::Current());
8921 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(dex_cache));
8922 return DoResolveString(string_idx, h_dex_cache);
8923 }
8924
DoResolveString(dex::StringIndex string_idx,Handle<mirror::DexCache> dex_cache)8925 ObjPtr<mirror::String> ClassLinker::DoResolveString(dex::StringIndex string_idx,
8926 Handle<mirror::DexCache> dex_cache) {
8927 const DexFile& dex_file = *dex_cache->GetDexFile();
8928 uint32_t utf16_length;
8929 const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length);
8930 ObjPtr<mirror::String> string = intern_table_->InternStrong(utf16_length, utf8_data);
8931 if (string != nullptr) {
8932 dex_cache->SetResolvedString(string_idx, string);
8933 }
8934 return string;
8935 }
8936
DoLookupString(dex::StringIndex string_idx,ObjPtr<mirror::DexCache> dex_cache)8937 ObjPtr<mirror::String> ClassLinker::DoLookupString(dex::StringIndex string_idx,
8938 ObjPtr<mirror::DexCache> dex_cache) {
8939 DCHECK(dex_cache != nullptr);
8940 const DexFile& dex_file = *dex_cache->GetDexFile();
8941 uint32_t utf16_length;
8942 const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length);
8943 ObjPtr<mirror::String> string =
8944 intern_table_->LookupStrong(Thread::Current(), utf16_length, utf8_data);
8945 if (string != nullptr) {
8946 dex_cache->SetResolvedString(string_idx, string);
8947 }
8948 return string;
8949 }
8950
DoLookupResolvedType(dex::TypeIndex type_idx,ObjPtr<mirror::Class> referrer)8951 ObjPtr<mirror::Class> ClassLinker::DoLookupResolvedType(dex::TypeIndex type_idx,
8952 ObjPtr<mirror::Class> referrer) {
8953 return DoLookupResolvedType(type_idx, referrer->GetDexCache(), referrer->GetClassLoader());
8954 }
8955
DoLookupResolvedType(dex::TypeIndex type_idx,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader)8956 ObjPtr<mirror::Class> ClassLinker::DoLookupResolvedType(dex::TypeIndex type_idx,
8957 ObjPtr<mirror::DexCache> dex_cache,
8958 ObjPtr<mirror::ClassLoader> class_loader) {
8959 const DexFile& dex_file = *dex_cache->GetDexFile();
8960 const char* descriptor = dex_file.StringByTypeIdx(type_idx);
8961 ObjPtr<mirror::Class> type = LookupResolvedType(descriptor, class_loader);
8962 if (type != nullptr) {
8963 DCHECK(type->IsResolved());
8964 dex_cache->SetResolvedType(type_idx, type);
8965 }
8966 return type;
8967 }
8968
LookupResolvedType(const char * descriptor,ObjPtr<mirror::ClassLoader> class_loader)8969 ObjPtr<mirror::Class> ClassLinker::LookupResolvedType(const char* descriptor,
8970 ObjPtr<mirror::ClassLoader> class_loader) {
8971 DCHECK_NE(*descriptor, '\0') << "descriptor is empty string";
8972 ObjPtr<mirror::Class> type = nullptr;
8973 if (descriptor[1] == '\0') {
8974 // only the descriptors of primitive types should be 1 character long, also avoid class lookup
8975 // for primitive classes that aren't backed by dex files.
8976 type = LookupPrimitiveClass(descriptor[0]);
8977 } else {
8978 Thread* const self = Thread::Current();
8979 DCHECK(self != nullptr);
8980 const size_t hash = ComputeModifiedUtf8Hash(descriptor);
8981 // Find the class in the loaded classes table.
8982 type = LookupClass(self, descriptor, hash, class_loader);
8983 }
8984 return (type != nullptr && type->IsResolved()) ? type : nullptr;
8985 }
8986
8987 template <typename RefType>
DoResolveType(dex::TypeIndex type_idx,RefType referrer)8988 ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, RefType referrer) {
8989 StackHandleScope<2> hs(Thread::Current());
8990 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
8991 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader()));
8992 return DoResolveType(type_idx, dex_cache, class_loader);
8993 }
8994
8995 // Instantiate the above.
8996 template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx,
8997 ArtField* referrer);
8998 template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx,
8999 ArtMethod* referrer);
9000 template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx,
9001 ObjPtr<mirror::Class> referrer);
9002
DoResolveType(dex::TypeIndex type_idx,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader)9003 ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx,
9004 Handle<mirror::DexCache> dex_cache,
9005 Handle<mirror::ClassLoader> class_loader) {
9006 Thread* self = Thread::Current();
9007 const char* descriptor = dex_cache->GetDexFile()->StringByTypeIdx(type_idx);
9008 ObjPtr<mirror::Class> resolved = FindClass(self, descriptor, class_loader);
9009 if (resolved != nullptr) {
9010 // TODO: we used to throw here if resolved's class loader was not the
9011 // boot class loader. This was to permit different classes with the
9012 // same name to be loaded simultaneously by different loaders
9013 dex_cache->SetResolvedType(type_idx, resolved);
9014 } else {
9015 CHECK(self->IsExceptionPending())
9016 << "Expected pending exception for failed resolution of: " << descriptor;
9017 // Convert a ClassNotFoundException to a NoClassDefFoundError.
9018 StackHandleScope<1> hs(self);
9019 Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException()));
9020 if (cause->InstanceOf(GetClassRoot(ClassRoot::kJavaLangClassNotFoundException, this))) {
9021 DCHECK(resolved == nullptr); // No Handle needed to preserve resolved.
9022 self->ClearException();
9023 ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor);
9024 self->GetException()->SetCause(cause.Get());
9025 }
9026 }
9027 DCHECK((resolved == nullptr) || resolved->IsResolved())
9028 << resolved->PrettyDescriptor() << " " << resolved->GetStatus();
9029 return resolved;
9030 }
9031
FindResolvedMethod(ObjPtr<mirror::Class> klass,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader,uint32_t method_idx)9032 ArtMethod* ClassLinker::FindResolvedMethod(ObjPtr<mirror::Class> klass,
9033 ObjPtr<mirror::DexCache> dex_cache,
9034 ObjPtr<mirror::ClassLoader> class_loader,
9035 uint32_t method_idx) {
9036 // Search for the method using dex_cache and method_idx. The Class::Find*Method()
9037 // functions can optimize the search if the dex_cache is the same as the DexCache
9038 // of the class, with fall-back to name and signature search otherwise.
9039 ArtMethod* resolved = nullptr;
9040 if (klass->IsInterface()) {
9041 resolved = klass->FindInterfaceMethod(dex_cache, method_idx, image_pointer_size_);
9042 } else {
9043 resolved = klass->FindClassMethod(dex_cache, method_idx, image_pointer_size_);
9044 }
9045 DCHECK(resolved == nullptr || resolved->GetDeclaringClassUnchecked() != nullptr);
9046 if (resolved != nullptr &&
9047 // We pass AccessMethod::kNone instead of kLinking to not warn yet on the
9048 // access, as we'll be looking if the method can be accessed through an
9049 // interface.
9050 hiddenapi::ShouldDenyAccessToMember(resolved,
9051 hiddenapi::AccessContext(class_loader, dex_cache),
9052 hiddenapi::AccessMethod::kNone)) {
9053 // The resolved method that we have found cannot be accessed due to
9054 // hiddenapi (typically it is declared up the hierarchy and is not an SDK
9055 // method). Try to find an interface method from the implemented interfaces which is
9056 // part of the SDK.
9057 ArtMethod* itf_method = klass->FindAccessibleInterfaceMethod(resolved, image_pointer_size_);
9058 if (itf_method == nullptr) {
9059 // No interface method. Call ShouldDenyAccessToMember again but this time
9060 // with AccessMethod::kLinking to ensure that an appropriate warning is
9061 // logged.
9062 hiddenapi::ShouldDenyAccessToMember(resolved,
9063 hiddenapi::AccessContext(class_loader, dex_cache),
9064 hiddenapi::AccessMethod::kLinking);
9065 resolved = nullptr;
9066 } else {
9067 // We found an interface method that is accessible, continue with the resolved method.
9068 }
9069 }
9070 if (resolved != nullptr) {
9071 // In case of jmvti, the dex file gets verified before being registered, so first
9072 // check if it's registered before checking class tables.
9073 const DexFile& dex_file = *dex_cache->GetDexFile();
9074 DCHECK(!IsDexFileRegistered(Thread::Current(), dex_file) ||
9075 FindClassTable(Thread::Current(), dex_cache) == ClassTableForClassLoader(class_loader))
9076 << "DexFile referrer: " << dex_file.GetLocation()
9077 << " ClassLoader: " << DescribeLoaders(class_loader, "");
9078 // Be a good citizen and update the dex cache to speed subsequent calls.
9079 dex_cache->SetResolvedMethod(method_idx, resolved);
9080 // Disable the following invariant check as the verifier breaks it. b/73760543
9081 // const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
9082 // DCHECK(LookupResolvedType(method_id.class_idx_, dex_cache, class_loader) != nullptr)
9083 // << "Method: " << resolved->PrettyMethod() << ", "
9084 // << "Class: " << klass->PrettyClass() << " (" << klass->GetStatus() << "), "
9085 // << "DexFile referrer: " << dex_file.GetLocation();
9086 }
9087 return resolved;
9088 }
9089
9090 // Returns true if `method` is either null or hidden.
9091 // Does not print any warnings if it is hidden.
CheckNoSuchMethod(ArtMethod * method,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader)9092 static bool CheckNoSuchMethod(ArtMethod* method,
9093 ObjPtr<mirror::DexCache> dex_cache,
9094 ObjPtr<mirror::ClassLoader> class_loader)
9095 REQUIRES_SHARED(Locks::mutator_lock_) {
9096 return method == nullptr ||
9097 hiddenapi::ShouldDenyAccessToMember(method,
9098 hiddenapi::AccessContext(class_loader, dex_cache),
9099 hiddenapi::AccessMethod::kNone); // no warnings
9100 }
9101
FindIncompatibleMethod(ObjPtr<mirror::Class> klass,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader,uint32_t method_idx)9102 ArtMethod* ClassLinker::FindIncompatibleMethod(ObjPtr<mirror::Class> klass,
9103 ObjPtr<mirror::DexCache> dex_cache,
9104 ObjPtr<mirror::ClassLoader> class_loader,
9105 uint32_t method_idx) {
9106 if (klass->IsInterface()) {
9107 ArtMethod* method = klass->FindClassMethod(dex_cache, method_idx, image_pointer_size_);
9108 return CheckNoSuchMethod(method, dex_cache, class_loader) ? nullptr : method;
9109 } else {
9110 // If there was an interface method with the same signature, we would have
9111 // found it in the "copied" methods. Only DCHECK that the interface method
9112 // really does not exist.
9113 if (kIsDebugBuild) {
9114 ArtMethod* method =
9115 klass->FindInterfaceMethod(dex_cache, method_idx, image_pointer_size_);
9116 DCHECK(CheckNoSuchMethod(method, dex_cache, class_loader));
9117 }
9118 return nullptr;
9119 }
9120 }
9121
9122 template <ClassLinker::ResolveMode kResolveMode>
ResolveMethod(uint32_t method_idx,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader,ArtMethod * referrer,InvokeType type)9123 ArtMethod* ClassLinker::ResolveMethod(uint32_t method_idx,
9124 Handle<mirror::DexCache> dex_cache,
9125 Handle<mirror::ClassLoader> class_loader,
9126 ArtMethod* referrer,
9127 InvokeType type) {
9128 DCHECK(!Thread::Current()->IsExceptionPending()) << Thread::Current()->GetException()->Dump();
9129 DCHECK(dex_cache != nullptr);
9130 DCHECK(referrer == nullptr || !referrer->IsProxyMethod());
9131 // Check for hit in the dex cache.
9132 ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx);
9133 Thread::PoisonObjectPointersIfDebug();
9134 DCHECK(resolved == nullptr || !resolved->IsRuntimeMethod());
9135 bool valid_dex_cache_method = resolved != nullptr;
9136 if (kResolveMode == ResolveMode::kNoChecks && valid_dex_cache_method) {
9137 // We have a valid method from the DexCache and no checks to perform.
9138 DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex();
9139 return resolved;
9140 }
9141 const DexFile& dex_file = *dex_cache->GetDexFile();
9142 const dex::MethodId& method_id = dex_file.GetMethodId(method_idx);
9143 ObjPtr<mirror::Class> klass = nullptr;
9144 if (valid_dex_cache_method) {
9145 // We have a valid method from the DexCache but we need to perform ICCE and IAE checks.
9146 DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex();
9147 klass = LookupResolvedType(method_id.class_idx_, dex_cache.Get(), class_loader.Get());
9148 if (UNLIKELY(klass == nullptr)) {
9149 // We normaly should not end up here. However the verifier currently doesn't guarantee
9150 // the invariant of having the klass in the class table. b/73760543
9151 klass = ResolveType(method_id.class_idx_, dex_cache, class_loader);
9152 if (klass == nullptr) {
9153 // This can only happen if the current thread is not allowed to load
9154 // classes.
9155 DCHECK(!Thread::Current()->CanLoadClasses());
9156 DCHECK(Thread::Current()->IsExceptionPending());
9157 return nullptr;
9158 }
9159 }
9160 } else {
9161 // The method was not in the DexCache, resolve the declaring class.
9162 klass = ResolveType(method_id.class_idx_, dex_cache, class_loader);
9163 if (klass == nullptr) {
9164 DCHECK(Thread::Current()->IsExceptionPending());
9165 return nullptr;
9166 }
9167 }
9168
9169 // Check if the invoke type matches the class type.
9170 if (kResolveMode == ResolveMode::kCheckICCEAndIAE &&
9171 CheckInvokeClassMismatch</* kThrow= */ true>(
9172 dex_cache.Get(), type, [klass]() { return klass; })) {
9173 DCHECK(Thread::Current()->IsExceptionPending());
9174 return nullptr;
9175 }
9176
9177 if (!valid_dex_cache_method) {
9178 resolved = FindResolvedMethod(klass, dex_cache.Get(), class_loader.Get(), method_idx);
9179 }
9180
9181 // Note: We can check for IllegalAccessError only if we have a referrer.
9182 if (kResolveMode == ResolveMode::kCheckICCEAndIAE && resolved != nullptr && referrer != nullptr) {
9183 ObjPtr<mirror::Class> methods_class = resolved->GetDeclaringClass();
9184 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
9185 if (!referring_class->CheckResolvedMethodAccess(methods_class,
9186 resolved,
9187 dex_cache.Get(),
9188 method_idx,
9189 type)) {
9190 DCHECK(Thread::Current()->IsExceptionPending());
9191 return nullptr;
9192 }
9193 }
9194
9195 // If we found a method, check for incompatible class changes.
9196 if (LIKELY(resolved != nullptr) &&
9197 LIKELY(kResolveMode == ResolveMode::kNoChecks ||
9198 !resolved->CheckIncompatibleClassChange(type))) {
9199 return resolved;
9200 } else {
9201 // If we had a method, or if we can find one with another lookup type,
9202 // it's an incompatible-class-change error.
9203 if (resolved == nullptr) {
9204 resolved = FindIncompatibleMethod(klass, dex_cache.Get(), class_loader.Get(), method_idx);
9205 }
9206 if (resolved != nullptr) {
9207 ThrowIncompatibleClassChangeError(type, resolved->GetInvokeType(), resolved, referrer);
9208 } else {
9209 // We failed to find the method (using all lookup types), so throw a NoSuchMethodError.
9210 const char* name = dex_file.StringDataByIdx(method_id.name_idx_);
9211 const Signature signature = dex_file.GetMethodSignature(method_id);
9212 ThrowNoSuchMethodError(type, klass, name, signature);
9213 }
9214 Thread::Current()->AssertPendingException();
9215 return nullptr;
9216 }
9217 }
9218
ResolveMethodWithoutInvokeType(uint32_t method_idx,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader)9219 ArtMethod* ClassLinker::ResolveMethodWithoutInvokeType(uint32_t method_idx,
9220 Handle<mirror::DexCache> dex_cache,
9221 Handle<mirror::ClassLoader> class_loader) {
9222 ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx);
9223 Thread::PoisonObjectPointersIfDebug();
9224 if (resolved != nullptr) {
9225 DCHECK(!resolved->IsRuntimeMethod());
9226 DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex();
9227 return resolved;
9228 }
9229 // Fail, get the declaring class.
9230 const dex::MethodId& method_id = dex_cache->GetDexFile()->GetMethodId(method_idx);
9231 ObjPtr<mirror::Class> klass = ResolveType(method_id.class_idx_, dex_cache, class_loader);
9232 if (klass == nullptr) {
9233 Thread::Current()->AssertPendingException();
9234 return nullptr;
9235 }
9236 if (klass->IsInterface()) {
9237 resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx, image_pointer_size_);
9238 } else {
9239 resolved = klass->FindClassMethod(dex_cache.Get(), method_idx, image_pointer_size_);
9240 }
9241 if (resolved != nullptr &&
9242 hiddenapi::ShouldDenyAccessToMember(
9243 resolved,
9244 hiddenapi::AccessContext(class_loader.Get(), dex_cache.Get()),
9245 hiddenapi::AccessMethod::kLinking)) {
9246 resolved = nullptr;
9247 }
9248 return resolved;
9249 }
9250
LookupResolvedField(uint32_t field_idx,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader,bool is_static)9251 ArtField* ClassLinker::LookupResolvedField(uint32_t field_idx,
9252 ObjPtr<mirror::DexCache> dex_cache,
9253 ObjPtr<mirror::ClassLoader> class_loader,
9254 bool is_static) {
9255 const DexFile& dex_file = *dex_cache->GetDexFile();
9256 const dex::FieldId& field_id = dex_file.GetFieldId(field_idx);
9257 ObjPtr<mirror::Class> klass = dex_cache->GetResolvedType(field_id.class_idx_);
9258 if (klass == nullptr) {
9259 klass = LookupResolvedType(field_id.class_idx_, dex_cache, class_loader);
9260 }
9261 if (klass == nullptr) {
9262 // The class has not been resolved yet, so the field is also unresolved.
9263 return nullptr;
9264 }
9265 DCHECK(klass->IsResolved());
9266
9267 return FindResolvedField(klass, dex_cache, class_loader, field_idx, is_static);
9268 }
9269
ResolveField(uint32_t field_idx,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader,bool is_static)9270 ArtField* ClassLinker::ResolveField(uint32_t field_idx,
9271 Handle<mirror::DexCache> dex_cache,
9272 Handle<mirror::ClassLoader> class_loader,
9273 bool is_static) {
9274 DCHECK(dex_cache != nullptr);
9275 DCHECK(!Thread::Current()->IsExceptionPending()) << Thread::Current()->GetException()->Dump();
9276 ArtField* resolved = dex_cache->GetResolvedField(field_idx);
9277 Thread::PoisonObjectPointersIfDebug();
9278 if (resolved != nullptr) {
9279 return resolved;
9280 }
9281 const DexFile& dex_file = *dex_cache->GetDexFile();
9282 const dex::FieldId& field_id = dex_file.GetFieldId(field_idx);
9283 ObjPtr<mirror::Class> klass = ResolveType(field_id.class_idx_, dex_cache, class_loader);
9284 if (klass == nullptr) {
9285 DCHECK(Thread::Current()->IsExceptionPending());
9286 return nullptr;
9287 }
9288
9289 resolved = FindResolvedField(klass, dex_cache.Get(), class_loader.Get(), field_idx, is_static);
9290 if (resolved == nullptr) {
9291 const char* name = dex_file.GetFieldName(field_id);
9292 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
9293 ThrowNoSuchFieldError(is_static ? "static " : "instance ", klass, type, name);
9294 }
9295 return resolved;
9296 }
9297
ResolveFieldJLS(uint32_t field_idx,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader)9298 ArtField* ClassLinker::ResolveFieldJLS(uint32_t field_idx,
9299 Handle<mirror::DexCache> dex_cache,
9300 Handle<mirror::ClassLoader> class_loader) {
9301 DCHECK(dex_cache != nullptr);
9302 ArtField* resolved = dex_cache->GetResolvedField(field_idx);
9303 Thread::PoisonObjectPointersIfDebug();
9304 if (resolved != nullptr) {
9305 return resolved;
9306 }
9307 const DexFile& dex_file = *dex_cache->GetDexFile();
9308 const dex::FieldId& field_id = dex_file.GetFieldId(field_idx);
9309 ObjPtr<mirror::Class> klass = ResolveType(field_id.class_idx_, dex_cache, class_loader);
9310 if (klass == nullptr) {
9311 DCHECK(Thread::Current()->IsExceptionPending());
9312 return nullptr;
9313 }
9314
9315 resolved = FindResolvedFieldJLS(klass, dex_cache.Get(), class_loader.Get(), field_idx);
9316 if (resolved == nullptr) {
9317 const char* name = dex_file.GetFieldName(field_id);
9318 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
9319 ThrowNoSuchFieldError("", klass, type, name);
9320 }
9321 return resolved;
9322 }
9323
FindResolvedField(ObjPtr<mirror::Class> klass,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader,uint32_t field_idx,bool is_static)9324 ArtField* ClassLinker::FindResolvedField(ObjPtr<mirror::Class> klass,
9325 ObjPtr<mirror::DexCache> dex_cache,
9326 ObjPtr<mirror::ClassLoader> class_loader,
9327 uint32_t field_idx,
9328 bool is_static) {
9329 ArtField* resolved = nullptr;
9330 Thread* self = is_static ? Thread::Current() : nullptr;
9331 const DexFile& dex_file = *dex_cache->GetDexFile();
9332
9333 resolved = is_static ? mirror::Class::FindStaticField(self, klass, dex_cache, field_idx)
9334 : klass->FindInstanceField(dex_cache, field_idx);
9335
9336 if (resolved == nullptr) {
9337 const dex::FieldId& field_id = dex_file.GetFieldId(field_idx);
9338 const char* name = dex_file.GetFieldName(field_id);
9339 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
9340 resolved = is_static ? mirror::Class::FindStaticField(self, klass, name, type)
9341 : klass->FindInstanceField(name, type);
9342 }
9343
9344 if (resolved != nullptr &&
9345 hiddenapi::ShouldDenyAccessToMember(resolved,
9346 hiddenapi::AccessContext(class_loader, dex_cache),
9347 hiddenapi::AccessMethod::kLinking)) {
9348 resolved = nullptr;
9349 }
9350
9351 if (resolved != nullptr) {
9352 dex_cache->SetResolvedField(field_idx, resolved);
9353 }
9354
9355 return resolved;
9356 }
9357
FindResolvedFieldJLS(ObjPtr<mirror::Class> klass,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader,uint32_t field_idx)9358 ArtField* ClassLinker::FindResolvedFieldJLS(ObjPtr<mirror::Class> klass,
9359 ObjPtr<mirror::DexCache> dex_cache,
9360 ObjPtr<mirror::ClassLoader> class_loader,
9361 uint32_t field_idx) {
9362 ArtField* resolved = nullptr;
9363 Thread* self = Thread::Current();
9364 const DexFile& dex_file = *dex_cache->GetDexFile();
9365 const dex::FieldId& field_id = dex_file.GetFieldId(field_idx);
9366
9367 const char* name = dex_file.GetFieldName(field_id);
9368 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
9369 resolved = mirror::Class::FindField(self, klass, name, type);
9370
9371 if (resolved != nullptr &&
9372 hiddenapi::ShouldDenyAccessToMember(resolved,
9373 hiddenapi::AccessContext(class_loader, dex_cache),
9374 hiddenapi::AccessMethod::kLinking)) {
9375 resolved = nullptr;
9376 }
9377
9378 if (resolved != nullptr) {
9379 dex_cache->SetResolvedField(field_idx, resolved);
9380 }
9381
9382 return resolved;
9383 }
9384
ResolveMethodType(Thread * self,dex::ProtoIndex proto_idx,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader)9385 ObjPtr<mirror::MethodType> ClassLinker::ResolveMethodType(
9386 Thread* self,
9387 dex::ProtoIndex proto_idx,
9388 Handle<mirror::DexCache> dex_cache,
9389 Handle<mirror::ClassLoader> class_loader) {
9390 DCHECK(Runtime::Current()->IsMethodHandlesEnabled());
9391 DCHECK(dex_cache != nullptr);
9392
9393 ObjPtr<mirror::MethodType> resolved = dex_cache->GetResolvedMethodType(proto_idx);
9394 if (resolved != nullptr) {
9395 return resolved;
9396 }
9397
9398 StackHandleScope<4> hs(self);
9399
9400 // First resolve the return type.
9401 const DexFile& dex_file = *dex_cache->GetDexFile();
9402 const dex::ProtoId& proto_id = dex_file.GetProtoId(proto_idx);
9403 Handle<mirror::Class> return_type(hs.NewHandle(
9404 ResolveType(proto_id.return_type_idx_, dex_cache, class_loader)));
9405 if (return_type == nullptr) {
9406 DCHECK(self->IsExceptionPending());
9407 return nullptr;
9408 }
9409
9410 // Then resolve the argument types.
9411 //
9412 // TODO: Is there a better way to figure out the number of method arguments
9413 // other than by looking at the shorty ?
9414 const size_t num_method_args = strlen(dex_file.StringDataByIdx(proto_id.shorty_idx_)) - 1;
9415
9416 ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this);
9417 Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle(
9418 mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_method_args)));
9419 if (method_params == nullptr) {
9420 DCHECK(self->IsExceptionPending());
9421 return nullptr;
9422 }
9423
9424 DexFileParameterIterator it(dex_file, proto_id);
9425 int32_t i = 0;
9426 MutableHandle<mirror::Class> param_class = hs.NewHandle<mirror::Class>(nullptr);
9427 for (; it.HasNext(); it.Next()) {
9428 const dex::TypeIndex type_idx = it.GetTypeIdx();
9429 param_class.Assign(ResolveType(type_idx, dex_cache, class_loader));
9430 if (param_class == nullptr) {
9431 DCHECK(self->IsExceptionPending());
9432 return nullptr;
9433 }
9434
9435 method_params->Set(i++, param_class.Get());
9436 }
9437
9438 DCHECK(!it.HasNext());
9439
9440 Handle<mirror::MethodType> type = hs.NewHandle(
9441 mirror::MethodType::Create(self, return_type, method_params));
9442 dex_cache->SetResolvedMethodType(proto_idx, type.Get());
9443
9444 return type.Get();
9445 }
9446
ResolveMethodType(Thread * self,dex::ProtoIndex proto_idx,ArtMethod * referrer)9447 ObjPtr<mirror::MethodType> ClassLinker::ResolveMethodType(Thread* self,
9448 dex::ProtoIndex proto_idx,
9449 ArtMethod* referrer) {
9450 StackHandleScope<2> hs(self);
9451 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
9452 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader()));
9453 return ResolveMethodType(self, proto_idx, dex_cache, class_loader);
9454 }
9455
ResolveMethodHandleForField(Thread * self,const dex::MethodHandleItem & method_handle,ArtMethod * referrer)9456 ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandleForField(
9457 Thread* self,
9458 const dex::MethodHandleItem& method_handle,
9459 ArtMethod* referrer) {
9460 DexFile::MethodHandleType handle_type =
9461 static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_);
9462 mirror::MethodHandle::Kind kind;
9463 bool is_put;
9464 bool is_static;
9465 int32_t num_params;
9466 switch (handle_type) {
9467 case DexFile::MethodHandleType::kStaticPut: {
9468 kind = mirror::MethodHandle::Kind::kStaticPut;
9469 is_put = true;
9470 is_static = true;
9471 num_params = 1;
9472 break;
9473 }
9474 case DexFile::MethodHandleType::kStaticGet: {
9475 kind = mirror::MethodHandle::Kind::kStaticGet;
9476 is_put = false;
9477 is_static = true;
9478 num_params = 0;
9479 break;
9480 }
9481 case DexFile::MethodHandleType::kInstancePut: {
9482 kind = mirror::MethodHandle::Kind::kInstancePut;
9483 is_put = true;
9484 is_static = false;
9485 num_params = 2;
9486 break;
9487 }
9488 case DexFile::MethodHandleType::kInstanceGet: {
9489 kind = mirror::MethodHandle::Kind::kInstanceGet;
9490 is_put = false;
9491 is_static = false;
9492 num_params = 1;
9493 break;
9494 }
9495 case DexFile::MethodHandleType::kInvokeStatic:
9496 case DexFile::MethodHandleType::kInvokeInstance:
9497 case DexFile::MethodHandleType::kInvokeConstructor:
9498 case DexFile::MethodHandleType::kInvokeDirect:
9499 case DexFile::MethodHandleType::kInvokeInterface:
9500 UNREACHABLE();
9501 }
9502
9503 ArtField* target_field =
9504 ResolveField(method_handle.field_or_method_idx_, referrer, is_static);
9505 if (LIKELY(target_field != nullptr)) {
9506 ObjPtr<mirror::Class> target_class = target_field->GetDeclaringClass();
9507 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
9508 if (UNLIKELY(!referring_class->CanAccessMember(target_class, target_field->GetAccessFlags()))) {
9509 ThrowIllegalAccessErrorField(referring_class, target_field);
9510 return nullptr;
9511 }
9512 if (UNLIKELY(is_put && target_field->IsFinal())) {
9513 ThrowIllegalAccessErrorField(referring_class, target_field);
9514 return nullptr;
9515 }
9516 } else {
9517 DCHECK(Thread::Current()->IsExceptionPending());
9518 return nullptr;
9519 }
9520
9521 StackHandleScope<4> hs(self);
9522 ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this);
9523 Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle(
9524 mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params)));
9525 if (UNLIKELY(method_params == nullptr)) {
9526 DCHECK(self->IsExceptionPending());
9527 return nullptr;
9528 }
9529
9530 Handle<mirror::Class> constructor_class;
9531 Handle<mirror::Class> return_type;
9532 switch (handle_type) {
9533 case DexFile::MethodHandleType::kStaticPut: {
9534 method_params->Set(0, target_field->ResolveType());
9535 return_type = hs.NewHandle(GetClassRoot(ClassRoot::kPrimitiveVoid, this));
9536 break;
9537 }
9538 case DexFile::MethodHandleType::kStaticGet: {
9539 return_type = hs.NewHandle(target_field->ResolveType());
9540 break;
9541 }
9542 case DexFile::MethodHandleType::kInstancePut: {
9543 method_params->Set(0, target_field->GetDeclaringClass());
9544 method_params->Set(1, target_field->ResolveType());
9545 return_type = hs.NewHandle(GetClassRoot(ClassRoot::kPrimitiveVoid, this));
9546 break;
9547 }
9548 case DexFile::MethodHandleType::kInstanceGet: {
9549 method_params->Set(0, target_field->GetDeclaringClass());
9550 return_type = hs.NewHandle(target_field->ResolveType());
9551 break;
9552 }
9553 case DexFile::MethodHandleType::kInvokeStatic:
9554 case DexFile::MethodHandleType::kInvokeInstance:
9555 case DexFile::MethodHandleType::kInvokeConstructor:
9556 case DexFile::MethodHandleType::kInvokeDirect:
9557 case DexFile::MethodHandleType::kInvokeInterface:
9558 UNREACHABLE();
9559 }
9560
9561 for (int32_t i = 0; i < num_params; ++i) {
9562 if (UNLIKELY(method_params->Get(i) == nullptr)) {
9563 DCHECK(self->IsExceptionPending());
9564 return nullptr;
9565 }
9566 }
9567
9568 if (UNLIKELY(return_type.IsNull())) {
9569 DCHECK(self->IsExceptionPending());
9570 return nullptr;
9571 }
9572
9573 Handle<mirror::MethodType>
9574 method_type(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params)));
9575 if (UNLIKELY(method_type.IsNull())) {
9576 DCHECK(self->IsExceptionPending());
9577 return nullptr;
9578 }
9579
9580 uintptr_t target = reinterpret_cast<uintptr_t>(target_field);
9581 return mirror::MethodHandleImpl::Create(self, target, kind, method_type);
9582 }
9583
ResolveMethodHandleForMethod(Thread * self,const dex::MethodHandleItem & method_handle,ArtMethod * referrer)9584 ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandleForMethod(
9585 Thread* self,
9586 const dex::MethodHandleItem& method_handle,
9587 ArtMethod* referrer) {
9588 DexFile::MethodHandleType handle_type =
9589 static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_);
9590 mirror::MethodHandle::Kind kind;
9591 uint32_t receiver_count = 0;
9592 ArtMethod* target_method = nullptr;
9593 switch (handle_type) {
9594 case DexFile::MethodHandleType::kStaticPut:
9595 case DexFile::MethodHandleType::kStaticGet:
9596 case DexFile::MethodHandleType::kInstancePut:
9597 case DexFile::MethodHandleType::kInstanceGet:
9598 UNREACHABLE();
9599 case DexFile::MethodHandleType::kInvokeStatic: {
9600 kind = mirror::MethodHandle::Kind::kInvokeStatic;
9601 receiver_count = 0;
9602 target_method = ResolveMethod<ResolveMode::kNoChecks>(self,
9603 method_handle.field_or_method_idx_,
9604 referrer,
9605 InvokeType::kStatic);
9606 break;
9607 }
9608 case DexFile::MethodHandleType::kInvokeInstance: {
9609 kind = mirror::MethodHandle::Kind::kInvokeVirtual;
9610 receiver_count = 1;
9611 target_method = ResolveMethod<ResolveMode::kNoChecks>(self,
9612 method_handle.field_or_method_idx_,
9613 referrer,
9614 InvokeType::kVirtual);
9615 break;
9616 }
9617 case DexFile::MethodHandleType::kInvokeConstructor: {
9618 // Constructors are currently implemented as a transform. They
9619 // are special cased later in this method.
9620 kind = mirror::MethodHandle::Kind::kInvokeTransform;
9621 receiver_count = 0;
9622 target_method = ResolveMethod<ResolveMode::kNoChecks>(self,
9623 method_handle.field_or_method_idx_,
9624 referrer,
9625 InvokeType::kDirect);
9626 break;
9627 }
9628 case DexFile::MethodHandleType::kInvokeDirect: {
9629 kind = mirror::MethodHandle::Kind::kInvokeDirect;
9630 receiver_count = 1;
9631 StackHandleScope<2> hs(self);
9632 // A constant method handle with type kInvokeDirect can refer to
9633 // a method that is private or to a method in a super class. To
9634 // disambiguate the two options, we resolve the method ignoring
9635 // the invocation type to determine if the method is private. We
9636 // then resolve again specifying the intended invocation type to
9637 // force the appropriate checks.
9638 target_method = ResolveMethodWithoutInvokeType(method_handle.field_or_method_idx_,
9639 hs.NewHandle(referrer->GetDexCache()),
9640 hs.NewHandle(referrer->GetClassLoader()));
9641 if (UNLIKELY(target_method == nullptr)) {
9642 break;
9643 }
9644
9645 if (target_method->IsPrivate()) {
9646 kind = mirror::MethodHandle::Kind::kInvokeDirect;
9647 target_method = ResolveMethod<ResolveMode::kNoChecks>(self,
9648 method_handle.field_or_method_idx_,
9649 referrer,
9650 InvokeType::kDirect);
9651 } else {
9652 kind = mirror::MethodHandle::Kind::kInvokeSuper;
9653 target_method = ResolveMethod<ResolveMode::kNoChecks>(self,
9654 method_handle.field_or_method_idx_,
9655 referrer,
9656 InvokeType::kSuper);
9657 if (UNLIKELY(target_method == nullptr)) {
9658 break;
9659 }
9660 // Find the method specified in the parent in referring class
9661 // so invoke-super invokes the method in the parent of the
9662 // referrer.
9663 target_method =
9664 referrer->GetDeclaringClass()->FindVirtualMethodForVirtual(target_method,
9665 kRuntimePointerSize);
9666 }
9667 break;
9668 }
9669 case DexFile::MethodHandleType::kInvokeInterface: {
9670 kind = mirror::MethodHandle::Kind::kInvokeInterface;
9671 receiver_count = 1;
9672 target_method = ResolveMethod<ResolveMode::kNoChecks>(self,
9673 method_handle.field_or_method_idx_,
9674 referrer,
9675 InvokeType::kInterface);
9676 break;
9677 }
9678 }
9679
9680 if (UNLIKELY(target_method == nullptr)) {
9681 DCHECK(Thread::Current()->IsExceptionPending());
9682 return nullptr;
9683 }
9684
9685 ObjPtr<mirror::Class> target_class = target_method->GetDeclaringClass();
9686 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
9687 uint32_t access_flags = target_method->GetAccessFlags();
9688 if (UNLIKELY(!referring_class->CanAccessMember(target_class, access_flags))) {
9689 ThrowIllegalAccessErrorMethod(referring_class, target_method);
9690 return nullptr;
9691 }
9692
9693 // Calculate the number of parameters from the method shorty. We add the
9694 // receiver count (0 or 1) and deduct one for the return value.
9695 uint32_t shorty_length;
9696 target_method->GetShorty(&shorty_length);
9697 int32_t num_params = static_cast<int32_t>(shorty_length + receiver_count - 1);
9698
9699 StackHandleScope<5> hs(self);
9700 ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this);
9701 Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle(
9702 mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params)));
9703 if (method_params.Get() == nullptr) {
9704 DCHECK(self->IsExceptionPending());
9705 return nullptr;
9706 }
9707
9708 const DexFile* dex_file = referrer->GetDexFile();
9709 const dex::MethodId& method_id = dex_file->GetMethodId(method_handle.field_or_method_idx_);
9710 int32_t index = 0;
9711 if (receiver_count != 0) {
9712 // Insert receiver. Use the class identified in the method handle rather than the declaring
9713 // class of the resolved method which may be super class or default interface method
9714 // (b/115964401).
9715 ObjPtr<mirror::Class> receiver_class = LookupResolvedType(method_id.class_idx_, referrer);
9716 // receiver_class should have been resolved when resolving the target method.
9717 DCHECK(receiver_class != nullptr);
9718 method_params->Set(index++, receiver_class);
9719 }
9720
9721 const dex::ProtoId& proto_id = dex_file->GetProtoId(method_id.proto_idx_);
9722 DexFileParameterIterator it(*dex_file, proto_id);
9723 while (it.HasNext()) {
9724 DCHECK_LT(index, num_params);
9725 const dex::TypeIndex type_idx = it.GetTypeIdx();
9726 ObjPtr<mirror::Class> klass = ResolveType(type_idx, referrer);
9727 if (nullptr == klass) {
9728 DCHECK(self->IsExceptionPending());
9729 return nullptr;
9730 }
9731 method_params->Set(index++, klass);
9732 it.Next();
9733 }
9734
9735 Handle<mirror::Class> return_type =
9736 hs.NewHandle(ResolveType(proto_id.return_type_idx_, referrer));
9737 if (UNLIKELY(return_type.IsNull())) {
9738 DCHECK(self->IsExceptionPending());
9739 return nullptr;
9740 }
9741
9742 Handle<mirror::MethodType>
9743 method_type(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params)));
9744 if (UNLIKELY(method_type.IsNull())) {
9745 DCHECK(self->IsExceptionPending());
9746 return nullptr;
9747 }
9748
9749 if (UNLIKELY(handle_type == DexFile::MethodHandleType::kInvokeConstructor)) {
9750 Handle<mirror::Class> constructor_class = hs.NewHandle(target_method->GetDeclaringClass());
9751 Handle<mirror::MethodHandlesLookup> lookup =
9752 hs.NewHandle(mirror::MethodHandlesLookup::GetDefault(self));
9753 return lookup->FindConstructor(self, constructor_class, method_type);
9754 }
9755
9756 uintptr_t target = reinterpret_cast<uintptr_t>(target_method);
9757 return mirror::MethodHandleImpl::Create(self, target, kind, method_type);
9758 }
9759
ResolveMethodHandle(Thread * self,uint32_t method_handle_idx,ArtMethod * referrer)9760 ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandle(Thread* self,
9761 uint32_t method_handle_idx,
9762 ArtMethod* referrer)
9763 REQUIRES_SHARED(Locks::mutator_lock_) {
9764 const DexFile* const dex_file = referrer->GetDexFile();
9765 const dex::MethodHandleItem& method_handle = dex_file->GetMethodHandle(method_handle_idx);
9766 switch (static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_)) {
9767 case DexFile::MethodHandleType::kStaticPut:
9768 case DexFile::MethodHandleType::kStaticGet:
9769 case DexFile::MethodHandleType::kInstancePut:
9770 case DexFile::MethodHandleType::kInstanceGet:
9771 return ResolveMethodHandleForField(self, method_handle, referrer);
9772 case DexFile::MethodHandleType::kInvokeStatic:
9773 case DexFile::MethodHandleType::kInvokeInstance:
9774 case DexFile::MethodHandleType::kInvokeConstructor:
9775 case DexFile::MethodHandleType::kInvokeDirect:
9776 case DexFile::MethodHandleType::kInvokeInterface:
9777 return ResolveMethodHandleForMethod(self, method_handle, referrer);
9778 }
9779 }
9780
IsQuickResolutionStub(const void * entry_point) const9781 bool ClassLinker::IsQuickResolutionStub(const void* entry_point) const {
9782 return (entry_point == GetQuickResolutionStub()) ||
9783 (quick_resolution_trampoline_ == entry_point);
9784 }
9785
IsQuickToInterpreterBridge(const void * entry_point) const9786 bool ClassLinker::IsQuickToInterpreterBridge(const void* entry_point) const {
9787 return (entry_point == GetQuickToInterpreterBridge()) ||
9788 (quick_to_interpreter_bridge_trampoline_ == entry_point);
9789 }
9790
IsQuickGenericJniStub(const void * entry_point) const9791 bool ClassLinker::IsQuickGenericJniStub(const void* entry_point) const {
9792 return (entry_point == GetQuickGenericJniStub()) ||
9793 (quick_generic_jni_trampoline_ == entry_point);
9794 }
9795
IsJniDlsymLookupStub(const void * entry_point) const9796 bool ClassLinker::IsJniDlsymLookupStub(const void* entry_point) const {
9797 return entry_point == GetJniDlsymLookupStub() ||
9798 (jni_dlsym_lookup_trampoline_ == entry_point);
9799 }
9800
IsJniDlsymLookupCriticalStub(const void * entry_point) const9801 bool ClassLinker::IsJniDlsymLookupCriticalStub(const void* entry_point) const {
9802 return entry_point == GetJniDlsymLookupCriticalStub() ||
9803 (jni_dlsym_lookup_critical_trampoline_ == entry_point);
9804 }
9805
GetRuntimeQuickGenericJniStub() const9806 const void* ClassLinker::GetRuntimeQuickGenericJniStub() const {
9807 return GetQuickGenericJniStub();
9808 }
9809
SetEntryPointsToInterpreter(ArtMethod * method) const9810 void ClassLinker::SetEntryPointsToInterpreter(ArtMethod* method) const {
9811 if (!method->IsNative()) {
9812 method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge());
9813 } else {
9814 method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub());
9815 }
9816 }
9817
SetEntryPointsForObsoleteMethod(ArtMethod * method) const9818 void ClassLinker::SetEntryPointsForObsoleteMethod(ArtMethod* method) const {
9819 DCHECK(method->IsObsolete());
9820 // We cannot mess with the entrypoints of native methods because they are used to determine how
9821 // large the method's quick stack frame is. Without this information we cannot walk the stacks.
9822 if (!method->IsNative()) {
9823 method->SetEntryPointFromQuickCompiledCode(GetInvokeObsoleteMethodStub());
9824 }
9825 }
9826
DumpForSigQuit(std::ostream & os)9827 void ClassLinker::DumpForSigQuit(std::ostream& os) {
9828 ScopedObjectAccess soa(Thread::Current());
9829 ReaderMutexLock mu(soa.Self(), *Locks::classlinker_classes_lock_);
9830 os << "Zygote loaded classes=" << NumZygoteClasses() << " post zygote classes="
9831 << NumNonZygoteClasses() << "\n";
9832 ReaderMutexLock mu2(soa.Self(), *Locks::dex_lock_);
9833 os << "Dumping registered class loaders\n";
9834 size_t class_loader_index = 0;
9835 for (const ClassLoaderData& class_loader : class_loaders_) {
9836 ObjPtr<mirror::ClassLoader> loader =
9837 ObjPtr<mirror::ClassLoader>::DownCast(soa.Self()->DecodeJObject(class_loader.weak_root));
9838 if (loader != nullptr) {
9839 os << "#" << class_loader_index++ << " " << loader->GetClass()->PrettyDescriptor() << ": [";
9840 bool saw_one_dex_file = false;
9841 for (const DexCacheData& dex_cache : dex_caches_) {
9842 if (dex_cache.IsValid() && dex_cache.class_table == class_loader.class_table) {
9843 if (saw_one_dex_file) {
9844 os << ":";
9845 }
9846 saw_one_dex_file = true;
9847 os << dex_cache.dex_file->GetLocation();
9848 }
9849 }
9850 os << "]";
9851 bool found_parent = false;
9852 if (loader->GetParent() != nullptr) {
9853 size_t parent_index = 0;
9854 for (const ClassLoaderData& class_loader2 : class_loaders_) {
9855 ObjPtr<mirror::ClassLoader> loader2 = ObjPtr<mirror::ClassLoader>::DownCast(
9856 soa.Self()->DecodeJObject(class_loader2.weak_root));
9857 if (loader2 == loader->GetParent()) {
9858 os << ", parent #" << parent_index;
9859 found_parent = true;
9860 break;
9861 }
9862 parent_index++;
9863 }
9864 if (!found_parent) {
9865 os << ", unregistered parent of type "
9866 << loader->GetParent()->GetClass()->PrettyDescriptor();
9867 }
9868 } else {
9869 os << ", no parent";
9870 }
9871 os << "\n";
9872 }
9873 }
9874 os << "Done dumping class loaders\n";
9875 Runtime* runtime = Runtime::Current();
9876 os << "Classes initialized: " << runtime->GetStat(KIND_GLOBAL_CLASS_INIT_COUNT) << " in "
9877 << PrettyDuration(runtime->GetStat(KIND_GLOBAL_CLASS_INIT_TIME)) << "\n";
9878 }
9879
9880 class CountClassesVisitor : public ClassLoaderVisitor {
9881 public:
CountClassesVisitor()9882 CountClassesVisitor() : num_zygote_classes(0), num_non_zygote_classes(0) {}
9883
Visit(ObjPtr<mirror::ClassLoader> class_loader)9884 void Visit(ObjPtr<mirror::ClassLoader> class_loader)
9885 REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override {
9886 ClassTable* const class_table = class_loader->GetClassTable();
9887 if (class_table != nullptr) {
9888 num_zygote_classes += class_table->NumZygoteClasses(class_loader);
9889 num_non_zygote_classes += class_table->NumNonZygoteClasses(class_loader);
9890 }
9891 }
9892
9893 size_t num_zygote_classes;
9894 size_t num_non_zygote_classes;
9895 };
9896
NumZygoteClasses() const9897 size_t ClassLinker::NumZygoteClasses() const {
9898 CountClassesVisitor visitor;
9899 VisitClassLoaders(&visitor);
9900 return visitor.num_zygote_classes + boot_class_table_->NumZygoteClasses(nullptr);
9901 }
9902
NumNonZygoteClasses() const9903 size_t ClassLinker::NumNonZygoteClasses() const {
9904 CountClassesVisitor visitor;
9905 VisitClassLoaders(&visitor);
9906 return visitor.num_non_zygote_classes + boot_class_table_->NumNonZygoteClasses(nullptr);
9907 }
9908
NumLoadedClasses()9909 size_t ClassLinker::NumLoadedClasses() {
9910 ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
9911 // Only return non zygote classes since these are the ones which apps which care about.
9912 return NumNonZygoteClasses();
9913 }
9914
GetClassesLockOwner()9915 pid_t ClassLinker::GetClassesLockOwner() {
9916 return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid();
9917 }
9918
GetDexLockOwner()9919 pid_t ClassLinker::GetDexLockOwner() {
9920 return Locks::dex_lock_->GetExclusiveOwnerTid();
9921 }
9922
SetClassRoot(ClassRoot class_root,ObjPtr<mirror::Class> klass)9923 void ClassLinker::SetClassRoot(ClassRoot class_root, ObjPtr<mirror::Class> klass) {
9924 DCHECK(!init_done_);
9925
9926 DCHECK(klass != nullptr);
9927 DCHECK(klass->GetClassLoader() == nullptr);
9928
9929 mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read();
9930 DCHECK(class_roots != nullptr);
9931 DCHECK_LT(static_cast<uint32_t>(class_root), static_cast<uint32_t>(ClassRoot::kMax));
9932 int32_t index = static_cast<int32_t>(class_root);
9933 DCHECK(class_roots->Get(index) == nullptr);
9934 class_roots->Set<false>(index, klass);
9935 }
9936
CreateWellKnownClassLoader(Thread * self,const std::vector<const DexFile * > & dex_files,Handle<mirror::Class> loader_class,Handle<mirror::ClassLoader> parent_loader,Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries)9937 ObjPtr<mirror::ClassLoader> ClassLinker::CreateWellKnownClassLoader(
9938 Thread* self,
9939 const std::vector<const DexFile*>& dex_files,
9940 Handle<mirror::Class> loader_class,
9941 Handle<mirror::ClassLoader> parent_loader,
9942 Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries) {
9943
9944 StackHandleScope<5> hs(self);
9945
9946 ArtField* dex_elements_field =
9947 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements);
9948
9949 Handle<mirror::Class> dex_elements_class(hs.NewHandle(dex_elements_field->ResolveType()));
9950 DCHECK(dex_elements_class != nullptr);
9951 DCHECK(dex_elements_class->IsArrayClass());
9952 Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements(hs.NewHandle(
9953 mirror::ObjectArray<mirror::Object>::Alloc(self,
9954 dex_elements_class.Get(),
9955 dex_files.size())));
9956 Handle<mirror::Class> h_dex_element_class =
9957 hs.NewHandle(dex_elements_class->GetComponentType());
9958
9959 ArtField* element_file_field =
9960 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile);
9961 DCHECK_EQ(h_dex_element_class.Get(), element_file_field->GetDeclaringClass());
9962
9963 ArtField* cookie_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie);
9964 DCHECK_EQ(cookie_field->GetDeclaringClass(), element_file_field->LookupResolvedType());
9965
9966 ArtField* file_name_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_fileName);
9967 DCHECK_EQ(file_name_field->GetDeclaringClass(), element_file_field->LookupResolvedType());
9968
9969 // Fill the elements array.
9970 int32_t index = 0;
9971 for (const DexFile* dex_file : dex_files) {
9972 StackHandleScope<4> hs2(self);
9973
9974 // CreateWellKnownClassLoader is only used by gtests and compiler.
9975 // Index 0 of h_long_array is supposed to be the oat file but we can leave it null.
9976 Handle<mirror::LongArray> h_long_array = hs2.NewHandle(mirror::LongArray::Alloc(
9977 self,
9978 kDexFileIndexStart + 1));
9979 DCHECK(h_long_array != nullptr);
9980 h_long_array->Set(kDexFileIndexStart, reinterpret_cast64<int64_t>(dex_file));
9981
9982 // Note that this creates a finalizable dalvik.system.DexFile object and a corresponding
9983 // FinalizerReference which will never get cleaned up without a started runtime.
9984 Handle<mirror::Object> h_dex_file = hs2.NewHandle(
9985 cookie_field->GetDeclaringClass()->AllocObject(self));
9986 DCHECK(h_dex_file != nullptr);
9987 cookie_field->SetObject<false>(h_dex_file.Get(), h_long_array.Get());
9988
9989 Handle<mirror::String> h_file_name = hs2.NewHandle(
9990 mirror::String::AllocFromModifiedUtf8(self, dex_file->GetLocation().c_str()));
9991 DCHECK(h_file_name != nullptr);
9992 file_name_field->SetObject<false>(h_dex_file.Get(), h_file_name.Get());
9993
9994 Handle<mirror::Object> h_element = hs2.NewHandle(h_dex_element_class->AllocObject(self));
9995 DCHECK(h_element != nullptr);
9996 element_file_field->SetObject<false>(h_element.Get(), h_dex_file.Get());
9997
9998 h_dex_elements->Set(index, h_element.Get());
9999 index++;
10000 }
10001 DCHECK_EQ(index, h_dex_elements->GetLength());
10002
10003 // Create DexPathList.
10004 Handle<mirror::Object> h_dex_path_list = hs.NewHandle(
10005 dex_elements_field->GetDeclaringClass()->AllocObject(self));
10006 DCHECK(h_dex_path_list != nullptr);
10007 // Set elements.
10008 dex_elements_field->SetObject<false>(h_dex_path_list.Get(), h_dex_elements.Get());
10009 // Create an empty List for the "nativeLibraryDirectories," required for native tests.
10010 // Note: this code is uncommon(oatdump)/testing-only, so don't add further WellKnownClasses
10011 // elements.
10012 {
10013 ArtField* native_lib_dirs = dex_elements_field->GetDeclaringClass()->
10014 FindDeclaredInstanceField("nativeLibraryDirectories", "Ljava/util/List;");
10015 DCHECK(native_lib_dirs != nullptr);
10016 ObjPtr<mirror::Class> list_class = FindSystemClass(self, "Ljava/util/ArrayList;");
10017 DCHECK(list_class != nullptr);
10018 {
10019 StackHandleScope<1> h_list_scope(self);
10020 Handle<mirror::Class> h_list_class(h_list_scope.NewHandle<mirror::Class>(list_class));
10021 bool list_init = EnsureInitialized(self, h_list_class, true, true);
10022 DCHECK(list_init);
10023 list_class = h_list_class.Get();
10024 }
10025 ObjPtr<mirror::Object> list_object = list_class->AllocObject(self);
10026 // Note: we leave the object uninitialized. This must never leak into any non-testing code, but
10027 // is fine for testing. While it violates a Java-code invariant (the elementData field is
10028 // normally never null), as long as one does not try to add elements, this will still
10029 // work.
10030 native_lib_dirs->SetObject<false>(h_dex_path_list.Get(), list_object);
10031 }
10032
10033 // Create the class loader..
10034 Handle<mirror::ClassLoader> h_class_loader = hs.NewHandle<mirror::ClassLoader>(
10035 ObjPtr<mirror::ClassLoader>::DownCast(loader_class->AllocObject(self)));
10036 DCHECK(h_class_loader != nullptr);
10037 // Set DexPathList.
10038 ArtField* path_list_field =
10039 jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList);
10040 DCHECK(path_list_field != nullptr);
10041 path_list_field->SetObject<false>(h_class_loader.Get(), h_dex_path_list.Get());
10042
10043 // Make a pretend boot-classpath.
10044 // TODO: Should we scan the image?
10045 ArtField* const parent_field =
10046 mirror::Class::FindField(self,
10047 h_class_loader->GetClass(),
10048 "parent",
10049 "Ljava/lang/ClassLoader;");
10050 DCHECK(parent_field != nullptr);
10051 if (parent_loader.Get() == nullptr) {
10052 ScopedObjectAccessUnchecked soa(self);
10053 ObjPtr<mirror::Object> boot_loader(soa.Decode<mirror::Class>(
10054 WellKnownClasses::java_lang_BootClassLoader)->AllocObject(self));
10055 parent_field->SetObject<false>(h_class_loader.Get(), boot_loader);
10056 } else {
10057 parent_field->SetObject<false>(h_class_loader.Get(), parent_loader.Get());
10058 }
10059
10060 ArtField* shared_libraries_field =
10061 jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders);
10062 DCHECK(shared_libraries_field != nullptr);
10063 shared_libraries_field->SetObject<false>(h_class_loader.Get(), shared_libraries.Get());
10064
10065 return h_class_loader.Get();
10066 }
10067
CreateWellKnownClassLoader(Thread * self,const std::vector<const DexFile * > & dex_files,jclass loader_class,jobject parent_loader,jobject shared_libraries)10068 jobject ClassLinker::CreateWellKnownClassLoader(Thread* self,
10069 const std::vector<const DexFile*>& dex_files,
10070 jclass loader_class,
10071 jobject parent_loader,
10072 jobject shared_libraries) {
10073 CHECK(self->GetJniEnv()->IsSameObject(loader_class,
10074 WellKnownClasses::dalvik_system_PathClassLoader) ||
10075 self->GetJniEnv()->IsSameObject(loader_class,
10076 WellKnownClasses::dalvik_system_DelegateLastClassLoader) ||
10077 self->GetJniEnv()->IsSameObject(loader_class,
10078 WellKnownClasses::dalvik_system_InMemoryDexClassLoader));
10079
10080 // SOAAlreadyRunnable is protected, and we need something to add a global reference.
10081 // We could move the jobject to the callers, but all call-sites do this...
10082 ScopedObjectAccessUnchecked soa(self);
10083
10084 // For now, create a libcore-level DexFile for each ART DexFile. This "explodes" multidex.
10085 StackHandleScope<4> hs(self);
10086
10087 Handle<mirror::Class> h_loader_class =
10088 hs.NewHandle<mirror::Class>(soa.Decode<mirror::Class>(loader_class));
10089 Handle<mirror::ClassLoader> h_parent =
10090 hs.NewHandle<mirror::ClassLoader>(soa.Decode<mirror::ClassLoader>(parent_loader));
10091 Handle<mirror::ObjectArray<mirror::ClassLoader>> h_shared_libraries =
10092 hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::ClassLoader>>(shared_libraries));
10093
10094 ObjPtr<mirror::ClassLoader> loader = CreateWellKnownClassLoader(
10095 self,
10096 dex_files,
10097 h_loader_class,
10098 h_parent,
10099 h_shared_libraries);
10100
10101 // Make it a global ref and return.
10102 ScopedLocalRef<jobject> local_ref(
10103 soa.Env(), soa.Env()->AddLocalReference<jobject>(loader));
10104 return soa.Env()->NewGlobalRef(local_ref.get());
10105 }
10106
CreatePathClassLoader(Thread * self,const std::vector<const DexFile * > & dex_files)10107 jobject ClassLinker::CreatePathClassLoader(Thread* self,
10108 const std::vector<const DexFile*>& dex_files) {
10109 return CreateWellKnownClassLoader(self,
10110 dex_files,
10111 WellKnownClasses::dalvik_system_PathClassLoader,
10112 nullptr);
10113 }
10114
DropFindArrayClassCache()10115 void ClassLinker::DropFindArrayClassCache() {
10116 std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr));
10117 find_array_class_cache_next_victim_ = 0;
10118 }
10119
VisitClassLoaders(ClassLoaderVisitor * visitor) const10120 void ClassLinker::VisitClassLoaders(ClassLoaderVisitor* visitor) const {
10121 Thread* const self = Thread::Current();
10122 for (const ClassLoaderData& data : class_loaders_) {
10123 // Need to use DecodeJObject so that we get null for cleared JNI weak globals.
10124 ObjPtr<mirror::ClassLoader> class_loader = ObjPtr<mirror::ClassLoader>::DownCast(
10125 self->DecodeJObject(data.weak_root));
10126 if (class_loader != nullptr) {
10127 visitor->Visit(class_loader);
10128 }
10129 }
10130 }
10131
VisitAllocators(AllocatorVisitor * visitor) const10132 void ClassLinker::VisitAllocators(AllocatorVisitor* visitor) const {
10133 for (const ClassLoaderData& data : class_loaders_) {
10134 LinearAlloc* alloc = data.allocator;
10135 if (alloc != nullptr && !visitor->Visit(alloc)) {
10136 break;
10137 }
10138 }
10139 }
10140
InsertDexFileInToClassLoader(ObjPtr<mirror::Object> dex_file,ObjPtr<mirror::ClassLoader> class_loader)10141 void ClassLinker::InsertDexFileInToClassLoader(ObjPtr<mirror::Object> dex_file,
10142 ObjPtr<mirror::ClassLoader> class_loader) {
10143 DCHECK(dex_file != nullptr);
10144 Thread* const self = Thread::Current();
10145 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
10146 ClassTable* const table = ClassTableForClassLoader(class_loader);
10147 DCHECK(table != nullptr);
10148 if (table->InsertStrongRoot(dex_file) && class_loader != nullptr) {
10149 // It was not already inserted, perform the write barrier to let the GC know the class loader's
10150 // class table was modified.
10151 WriteBarrier::ForEveryFieldWrite(class_loader);
10152 }
10153 }
10154
CleanupClassLoaders()10155 void ClassLinker::CleanupClassLoaders() {
10156 Thread* const self = Thread::Current();
10157 std::vector<ClassLoaderData> to_delete;
10158 // Do the delete outside the lock to avoid lock violation in jit code cache.
10159 {
10160 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
10161 for (auto it = class_loaders_.begin(); it != class_loaders_.end(); ) {
10162 const ClassLoaderData& data = *it;
10163 // Need to use DecodeJObject so that we get null for cleared JNI weak globals.
10164 ObjPtr<mirror::ClassLoader> class_loader =
10165 ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(data.weak_root));
10166 if (class_loader != nullptr) {
10167 ++it;
10168 } else {
10169 VLOG(class_linker) << "Freeing class loader";
10170 to_delete.push_back(data);
10171 it = class_loaders_.erase(it);
10172 }
10173 }
10174 }
10175 for (ClassLoaderData& data : to_delete) {
10176 // CHA unloading analysis and SingleImplementaion cleanups are required.
10177 DeleteClassLoader(self, data, /*cleanup_cha=*/ true);
10178 }
10179 }
10180
10181 class ClassLinker::FindVirtualMethodHolderVisitor : public ClassVisitor {
10182 public:
FindVirtualMethodHolderVisitor(const ArtMethod * method,PointerSize pointer_size)10183 FindVirtualMethodHolderVisitor(const ArtMethod* method, PointerSize pointer_size)
10184 : method_(method),
10185 pointer_size_(pointer_size) {}
10186
operator ()(ObjPtr<mirror::Class> klass)10187 bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) override {
10188 if (klass->GetVirtualMethodsSliceUnchecked(pointer_size_).Contains(method_)) {
10189 holder_ = klass;
10190 }
10191 // Return false to stop searching if holder_ is not null.
10192 return holder_ == nullptr;
10193 }
10194
10195 ObjPtr<mirror::Class> holder_ = nullptr;
10196 const ArtMethod* const method_;
10197 const PointerSize pointer_size_;
10198 };
10199
GetHoldingClassOfCopiedMethod(ArtMethod * method)10200 ObjPtr<mirror::Class> ClassLinker::GetHoldingClassOfCopiedMethod(ArtMethod* method) {
10201 ScopedTrace trace(__FUNCTION__); // Since this function is slow, have a trace to notify people.
10202 CHECK(method->IsCopied());
10203 FindVirtualMethodHolderVisitor visitor(method, image_pointer_size_);
10204 VisitClasses(&visitor);
10205 return visitor.holder_;
10206 }
10207
AllocIfTable(Thread * self,size_t ifcount)10208 ObjPtr<mirror::IfTable> ClassLinker::AllocIfTable(Thread* self, size_t ifcount) {
10209 return ObjPtr<mirror::IfTable>::DownCast(ObjPtr<mirror::ObjectArray<mirror::Object>>(
10210 mirror::IfTable::Alloc(self,
10211 GetClassRoot<mirror::ObjectArray<mirror::Object>>(this),
10212 ifcount * mirror::IfTable::kMax)));
10213 }
10214
IsUpdatableBootClassPathDescriptor(const char * descriptor ATTRIBUTE_UNUSED)10215 bool ClassLinker::IsUpdatableBootClassPathDescriptor(const char* descriptor ATTRIBUTE_UNUSED) {
10216 // Should not be called on ClassLinker, only on AotClassLinker that overrides this.
10217 LOG(FATAL) << "UNREACHABLE";
10218 UNREACHABLE();
10219 }
10220
DenyAccessBasedOnPublicSdk(ArtMethod * art_method ATTRIBUTE_UNUSED) const10221 bool ClassLinker::DenyAccessBasedOnPublicSdk(ArtMethod* art_method ATTRIBUTE_UNUSED) const
10222 REQUIRES_SHARED(Locks::mutator_lock_) {
10223 // Should not be called on ClassLinker, only on AotClassLinker that overrides this.
10224 LOG(FATAL) << "UNREACHABLE";
10225 UNREACHABLE();
10226 }
10227
DenyAccessBasedOnPublicSdk(ArtField * art_field ATTRIBUTE_UNUSED) const10228 bool ClassLinker::DenyAccessBasedOnPublicSdk(ArtField* art_field ATTRIBUTE_UNUSED) const
10229 REQUIRES_SHARED(Locks::mutator_lock_) {
10230 // Should not be called on ClassLinker, only on AotClassLinker that overrides this.
10231 LOG(FATAL) << "UNREACHABLE";
10232 UNREACHABLE();
10233 }
10234
DenyAccessBasedOnPublicSdk(const char * type_descriptor ATTRIBUTE_UNUSED) const10235 bool ClassLinker::DenyAccessBasedOnPublicSdk(const char* type_descriptor ATTRIBUTE_UNUSED) const {
10236 // Should not be called on ClassLinker, only on AotClassLinker that overrides this.
10237 LOG(FATAL) << "UNREACHABLE";
10238 UNREACHABLE();
10239 }
10240
SetEnablePublicSdkChecks(bool enabled ATTRIBUTE_UNUSED)10241 void ClassLinker::SetEnablePublicSdkChecks(bool enabled ATTRIBUTE_UNUSED) {
10242 // Should not be called on ClassLinker, only on AotClassLinker that overrides this.
10243 LOG(FATAL) << "UNREACHABLE";
10244 UNREACHABLE();
10245 }
10246
10247 // Instantiate ClassLinker::ResolveMethod.
10248 template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
10249 uint32_t method_idx,
10250 Handle<mirror::DexCache> dex_cache,
10251 Handle<mirror::ClassLoader> class_loader,
10252 ArtMethod* referrer,
10253 InvokeType type);
10254 template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
10255 uint32_t method_idx,
10256 Handle<mirror::DexCache> dex_cache,
10257 Handle<mirror::ClassLoader> class_loader,
10258 ArtMethod* referrer,
10259 InvokeType type);
10260
10261 // Instantiate ClassLinker::AllocClass.
10262 template ObjPtr<mirror::Class> ClassLinker::AllocClass</* kMovable= */ true>(
10263 Thread* self,
10264 ObjPtr<mirror::Class> java_lang_Class,
10265 uint32_t class_size);
10266 template ObjPtr<mirror::Class> ClassLinker::AllocClass</* kMovable= */ false>(
10267 Thread* self,
10268 ObjPtr<mirror::Class> java_lang_Class,
10269 uint32_t class_size);
10270
10271 } // namespace art
10272