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 <unordered_map>
32 #include <utility>
33 #include <vector>
34
35 #include "android-base/stringprintf.h"
36
37 #include "art_field-inl.h"
38 #include "art_method-inl.h"
39 #include "barrier.h"
40 #include "base/arena_allocator.h"
41 #include "base/casts.h"
42 #include "base/file_utils.h"
43 #include "base/leb128.h"
44 #include "base/logging.h"
45 #include "base/mutex-inl.h"
46 #include "base/os.h"
47 #include "base/quasi_atomic.h"
48 #include "base/scoped_arena_containers.h"
49 #include "base/scoped_flock.h"
50 #include "base/stl_util.h"
51 #include "base/string_view_cpp20.h"
52 #include "base/systrace.h"
53 #include "base/time_utils.h"
54 #include "base/unix_file/fd_file.h"
55 #include "base/utils.h"
56 #include "base/value_object.h"
57 #include "cha.h"
58 #include "class_linker-inl.h"
59 #include "class_loader_utils.h"
60 #include "class_root-inl.h"
61 #include "class_table-inl.h"
62 #include "compiler_callbacks.h"
63 #include "debug_print.h"
64 #include "debugger.h"
65 #include "dex/class_accessor-inl.h"
66 #include "dex/descriptors_names.h"
67 #include "dex/dex_file-inl.h"
68 #include "dex/dex_file_exception_helpers.h"
69 #include "dex/dex_file_loader.h"
70 #include "dex/signature-inl.h"
71 #include "dex/utf.h"
72 #include "entrypoints/entrypoint_utils-inl.h"
73 #include "entrypoints/runtime_asm_entrypoints.h"
74 #include "experimental_flags.h"
75 #include "gc/accounting/card_table-inl.h"
76 #include "gc/accounting/heap_bitmap-inl.h"
77 #include "gc/accounting/space_bitmap-inl.h"
78 #include "gc/heap-visit-objects-inl.h"
79 #include "gc/heap.h"
80 #include "gc/scoped_gc_critical_section.h"
81 #include "gc/space/image_space.h"
82 #include "gc/space/space-inl.h"
83 #include "gc_root-inl.h"
84 #include "handle_scope-inl.h"
85 #include "hidden_api.h"
86 #include "image-inl.h"
87 #include "imt_conflict_table.h"
88 #include "imtable-inl.h"
89 #include "intern_table-inl.h"
90 #include "interpreter/interpreter.h"
91 #include "interpreter/mterp/nterp.h"
92 #include "jit/debugger_interface.h"
93 #include "jit/jit.h"
94 #include "jit/jit_code_cache.h"
95 #include "jni/java_vm_ext.h"
96 #include "jni/jni_internal.h"
97 #include "linear_alloc.h"
98 #include "mirror/array-alloc-inl.h"
99 #include "mirror/array-inl.h"
100 #include "mirror/call_site.h"
101 #include "mirror/class-alloc-inl.h"
102 #include "mirror/class-inl.h"
103 #include "mirror/class.h"
104 #include "mirror/class_ext.h"
105 #include "mirror/class_loader.h"
106 #include "mirror/dex_cache-inl.h"
107 #include "mirror/dex_cache.h"
108 #include "mirror/emulated_stack_frame.h"
109 #include "mirror/field.h"
110 #include "mirror/iftable-inl.h"
111 #include "mirror/method.h"
112 #include "mirror/method_handle_impl.h"
113 #include "mirror/method_handles_lookup.h"
114 #include "mirror/method_type.h"
115 #include "mirror/object-inl.h"
116 #include "mirror/object-refvisitor-inl.h"
117 #include "mirror/object.h"
118 #include "mirror/object_array-alloc-inl.h"
119 #include "mirror/object_array-inl.h"
120 #include "mirror/object_array.h"
121 #include "mirror/object_reference.h"
122 #include "mirror/object_reference-inl.h"
123 #include "mirror/proxy.h"
124 #include "mirror/reference-inl.h"
125 #include "mirror/stack_trace_element.h"
126 #include "mirror/string-inl.h"
127 #include "mirror/throwable.h"
128 #include "mirror/var_handle.h"
129 #include "native/dalvik_system_DexFile.h"
130 #include "nativehelper/scoped_local_ref.h"
131 #include "oat.h"
132 #include "oat_file-inl.h"
133 #include "oat_file.h"
134 #include "oat_file_assistant.h"
135 #include "oat_file_manager.h"
136 #include "object_lock.h"
137 #include "profile/profile_compilation_info.h"
138 #include "runtime.h"
139 #include "runtime_callbacks.h"
140 #include "scoped_thread_state_change-inl.h"
141 #include "thread-inl.h"
142 #include "thread.h"
143 #include "thread_list.h"
144 #include "trace.h"
145 #include "transaction.h"
146 #include "utils/dex_cache_arrays_layout-inl.h"
147 #include "verifier/class_verifier.h"
148 #include "well_known_classes.h"
149
150 #include "interpreter/interpreter_mterp_impl.h"
151
152 namespace art {
153
154 using android::base::StringPrintf;
155
156 static constexpr bool kCheckImageObjects = kIsDebugBuild;
157 static constexpr bool kVerifyArtMethodDeclaringClasses = kIsDebugBuild;
158
159 static void ThrowNoClassDefFoundError(const char* fmt, ...)
160 __attribute__((__format__(__printf__, 1, 2)))
161 REQUIRES_SHARED(Locks::mutator_lock_);
ThrowNoClassDefFoundError(const char * fmt,...)162 static void ThrowNoClassDefFoundError(const char* fmt, ...) {
163 va_list args;
164 va_start(args, fmt);
165 Thread* self = Thread::Current();
166 self->ThrowNewExceptionV("Ljava/lang/NoClassDefFoundError;", fmt, args);
167 va_end(args);
168 }
169
HasInitWithString(Thread * self,ClassLinker * class_linker,const char * descriptor)170 static bool HasInitWithString(Thread* self, ClassLinker* class_linker, const char* descriptor)
171 REQUIRES_SHARED(Locks::mutator_lock_) {
172 ArtMethod* method = self->GetCurrentMethod(nullptr);
173 StackHandleScope<1> hs(self);
174 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(method != nullptr ?
175 method->GetDeclaringClass()->GetClassLoader() : nullptr));
176 ObjPtr<mirror::Class> exception_class = class_linker->FindClass(self, descriptor, class_loader);
177
178 if (exception_class == nullptr) {
179 // No exc class ~ no <init>-with-string.
180 CHECK(self->IsExceptionPending());
181 self->ClearException();
182 return false;
183 }
184
185 ArtMethod* exception_init_method = exception_class->FindConstructor(
186 "(Ljava/lang/String;)V", class_linker->GetImagePointerSize());
187 return exception_init_method != nullptr;
188 }
189
GetVerifyError(ObjPtr<mirror::Class> c)190 static ObjPtr<mirror::Object> GetVerifyError(ObjPtr<mirror::Class> c)
191 REQUIRES_SHARED(Locks::mutator_lock_) {
192 ObjPtr<mirror::ClassExt> ext(c->GetExtData());
193 if (ext == nullptr) {
194 return nullptr;
195 } else {
196 return ext->GetVerifyError();
197 }
198 }
199
200 // Helper for ThrowEarlierClassFailure. Throws the stored error.
HandleEarlierVerifyError(Thread * self,ClassLinker * class_linker,ObjPtr<mirror::Class> c)201 static void HandleEarlierVerifyError(Thread* self,
202 ClassLinker* class_linker,
203 ObjPtr<mirror::Class> c)
204 REQUIRES_SHARED(Locks::mutator_lock_) {
205 ObjPtr<mirror::Object> obj = GetVerifyError(c);
206 DCHECK(obj != nullptr);
207 self->AssertNoPendingException();
208 if (obj->IsClass()) {
209 // Previous error has been stored as class. Create a new exception of that type.
210
211 // It's possible the exception doesn't have a <init>(String).
212 std::string temp;
213 const char* descriptor = obj->AsClass()->GetDescriptor(&temp);
214
215 if (HasInitWithString(self, class_linker, descriptor)) {
216 self->ThrowNewException(descriptor, c->PrettyDescriptor().c_str());
217 } else {
218 self->ThrowNewException(descriptor, nullptr);
219 }
220 } else {
221 // Previous error has been stored as an instance. Just rethrow.
222 ObjPtr<mirror::Class> throwable_class = GetClassRoot<mirror::Throwable>(class_linker);
223 ObjPtr<mirror::Class> error_class = obj->GetClass();
224 CHECK(throwable_class->IsAssignableFrom(error_class));
225 self->SetException(obj->AsThrowable());
226 }
227 self->AssertPendingException();
228 }
229
ChangeInterpreterBridgeToNterp(ArtMethod * method,ClassLinker * class_linker)230 static void ChangeInterpreterBridgeToNterp(ArtMethod* method, ClassLinker* class_linker)
231 REQUIRES_SHARED(Locks::mutator_lock_) {
232 Runtime* runtime = Runtime::Current();
233 if (class_linker->IsQuickToInterpreterBridge(method->GetEntryPointFromQuickCompiledCode()) &&
234 interpreter::CanMethodUseNterp(method)) {
235 if (method->GetDeclaringClass()->IsVisiblyInitialized() ||
236 !NeedsClinitCheckBeforeCall(method)) {
237 runtime->GetInstrumentation()->UpdateMethodsCode(method, interpreter::GetNterpEntryPoint());
238 } else {
239 // Put the resolution stub, which will initialize the class and then
240 // call the method with nterp.
241 runtime->GetInstrumentation()->UpdateMethodsCode(method, GetQuickResolutionStub());
242 }
243 }
244 }
245
246 // Ensures that methods have the kAccSkipAccessChecks bit set. We use the
247 // kAccVerificationAttempted bit on the class access flags to determine whether this has been done
248 // before.
EnsureSkipAccessChecksMethods(Handle<mirror::Class> klass,PointerSize pointer_size)249 static void EnsureSkipAccessChecksMethods(Handle<mirror::Class> klass, PointerSize pointer_size)
250 REQUIRES_SHARED(Locks::mutator_lock_) {
251 Runtime* runtime = Runtime::Current();
252 ClassLinker* class_linker = runtime->GetClassLinker();
253 if (!klass->WasVerificationAttempted()) {
254 klass->SetSkipAccessChecksFlagOnAllMethods(pointer_size);
255 klass->SetVerificationAttempted();
256 // Now that the class has passed verification, try to set nterp entrypoints
257 // to methods that currently use the switch interpreter.
258 if (interpreter::CanRuntimeUseNterp()) {
259 for (ArtMethod& m : klass->GetMethods(pointer_size)) {
260 ChangeInterpreterBridgeToNterp(&m, class_linker);
261 }
262 }
263 }
264 }
265
266 // Callback responsible for making a batch of classes visibly initialized
267 // after all threads have called it from a checkpoint, ensuring visibility.
268 class ClassLinker::VisiblyInitializedCallback final
269 : public Closure, public IntrusiveForwardListNode<VisiblyInitializedCallback> {
270 public:
VisiblyInitializedCallback(ClassLinker * class_linker)271 explicit VisiblyInitializedCallback(ClassLinker* class_linker)
272 : class_linker_(class_linker),
273 num_classes_(0u),
274 thread_visibility_counter_(0),
275 barriers_() {
276 std::fill_n(classes_, kMaxClasses, nullptr);
277 }
278
IsEmpty() const279 bool IsEmpty() const {
280 DCHECK_LE(num_classes_, kMaxClasses);
281 return num_classes_ == 0u;
282 }
283
IsFull() const284 bool IsFull() const {
285 DCHECK_LE(num_classes_, kMaxClasses);
286 return num_classes_ == kMaxClasses;
287 }
288
AddClass(Thread * self,ObjPtr<mirror::Class> klass)289 void AddClass(Thread* self, ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) {
290 DCHECK_EQ(klass->GetStatus(), ClassStatus::kInitialized);
291 DCHECK(!IsFull());
292 classes_[num_classes_] = self->GetJniEnv()->GetVm()->AddWeakGlobalRef(self, klass);
293 ++num_classes_;
294 }
295
AddBarrier(Barrier * barrier)296 void AddBarrier(Barrier* barrier) {
297 barriers_.push_front(barrier);
298 }
299
GetAndClearBarriers()300 std::forward_list<Barrier*> GetAndClearBarriers() {
301 std::forward_list<Barrier*> result;
302 result.swap(barriers_);
303 result.reverse(); // Return barriers in insertion order.
304 return result;
305 }
306
MakeVisible(Thread * self)307 void MakeVisible(Thread* self) {
308 DCHECK_EQ(thread_visibility_counter_.load(std::memory_order_relaxed), 0);
309 size_t count = Runtime::Current()->GetThreadList()->RunCheckpoint(this);
310 AdjustThreadVisibilityCounter(self, count);
311 }
312
Run(Thread * self)313 void Run(Thread* self) override {
314 self->ClearMakeVisiblyInitializedCounter();
315 AdjustThreadVisibilityCounter(self, -1);
316 }
317
318 private:
AdjustThreadVisibilityCounter(Thread * self,ssize_t adjustment)319 void AdjustThreadVisibilityCounter(Thread* self, ssize_t adjustment) {
320 ssize_t old = thread_visibility_counter_.fetch_add(adjustment, std::memory_order_relaxed);
321 if (old + adjustment == 0) {
322 // All threads passed the checkpoint. Mark classes as visibly initialized.
323 {
324 ScopedObjectAccess soa(self);
325 StackHandleScope<1u> hs(self);
326 MutableHandle<mirror::Class> klass = hs.NewHandle<mirror::Class>(nullptr);
327 JavaVMExt* vm = self->GetJniEnv()->GetVm();
328 for (size_t i = 0, num = num_classes_; i != num; ++i) {
329 klass.Assign(ObjPtr<mirror::Class>::DownCast(self->DecodeJObject(classes_[i])));
330 vm->DeleteWeakGlobalRef(self, classes_[i]);
331 if (klass != nullptr) {
332 mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self);
333 class_linker_->FixupStaticTrampolines(self, klass.Get());
334 }
335 }
336 num_classes_ = 0u;
337 }
338 class_linker_->VisiblyInitializedCallbackDone(self, this);
339 }
340 }
341
342 static constexpr size_t kMaxClasses = 16;
343
344 ClassLinker* const class_linker_;
345 size_t num_classes_;
346 jweak classes_[kMaxClasses];
347
348 // The thread visibility counter starts at 0 and it is incremented by the number of
349 // threads that need to run this callback (by the thread that request the callback
350 // to be run) and decremented once for each `Run()` execution. When it reaches 0,
351 // whether after the increment or after a decrement, we know that `Run()` was executed
352 // for all threads and therefore we can mark the classes as visibly initialized.
353 std::atomic<ssize_t> thread_visibility_counter_;
354
355 // List of barries to `Pass()` for threads that wait for the callback to complete.
356 std::forward_list<Barrier*> barriers_;
357 };
358
MakeInitializedClassesVisiblyInitialized(Thread * self,bool wait)359 void ClassLinker::MakeInitializedClassesVisiblyInitialized(Thread* self, bool wait) {
360 if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) {
361 return; // Nothing to do. Thanks to the x86 memory model classes skip the initialized status.
362 }
363 std::optional<Barrier> maybe_barrier; // Avoid constructing the Barrier for `wait == false`.
364 if (wait) {
365 maybe_barrier.emplace(0);
366 }
367 int wait_count = 0;
368 VisiblyInitializedCallback* callback = nullptr;
369 {
370 MutexLock lock(self, visibly_initialized_callback_lock_);
371 if (visibly_initialized_callback_ != nullptr && !visibly_initialized_callback_->IsEmpty()) {
372 callback = visibly_initialized_callback_.release();
373 running_visibly_initialized_callbacks_.push_front(*callback);
374 }
375 if (wait) {
376 DCHECK(maybe_barrier.has_value());
377 Barrier* barrier = std::addressof(*maybe_barrier);
378 for (VisiblyInitializedCallback& cb : running_visibly_initialized_callbacks_) {
379 cb.AddBarrier(barrier);
380 ++wait_count;
381 }
382 }
383 }
384 if (callback != nullptr) {
385 callback->MakeVisible(self);
386 }
387 if (wait_count != 0) {
388 DCHECK(maybe_barrier.has_value());
389 maybe_barrier->Increment(self, wait_count);
390 }
391 }
392
VisiblyInitializedCallbackDone(Thread * self,VisiblyInitializedCallback * callback)393 void ClassLinker::VisiblyInitializedCallbackDone(Thread* self,
394 VisiblyInitializedCallback* callback) {
395 MutexLock lock(self, visibly_initialized_callback_lock_);
396 // Pass the barriers if requested.
397 for (Barrier* barrier : callback->GetAndClearBarriers()) {
398 barrier->Pass(self);
399 }
400 // Remove the callback from the list of running callbacks.
401 auto before = running_visibly_initialized_callbacks_.before_begin();
402 auto it = running_visibly_initialized_callbacks_.begin();
403 DCHECK(it != running_visibly_initialized_callbacks_.end());
404 while (std::addressof(*it) != callback) {
405 before = it;
406 ++it;
407 DCHECK(it != running_visibly_initialized_callbacks_.end());
408 }
409 running_visibly_initialized_callbacks_.erase_after(before);
410 // Reuse or destroy the callback object.
411 if (visibly_initialized_callback_ == nullptr) {
412 visibly_initialized_callback_.reset(callback);
413 } else {
414 delete callback;
415 }
416 }
417
ForceClassInitialized(Thread * self,Handle<mirror::Class> klass)418 void ClassLinker::ForceClassInitialized(Thread* self, Handle<mirror::Class> klass) {
419 ClassLinker::VisiblyInitializedCallback* cb = MarkClassInitialized(self, klass);
420 if (cb != nullptr) {
421 cb->MakeVisible(self);
422 }
423 ScopedThreadSuspension sts(self, ThreadState::kSuspended);
424 MakeInitializedClassesVisiblyInitialized(self, /*wait=*/true);
425 }
426
MarkClassInitialized(Thread * self,Handle<mirror::Class> klass)427 ClassLinker::VisiblyInitializedCallback* ClassLinker::MarkClassInitialized(
428 Thread* self, Handle<mirror::Class> klass) {
429 if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) {
430 // Thanks to the x86 memory model, we do not need any memory fences and
431 // we can immediately mark the class as visibly initialized.
432 mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self);
433 FixupStaticTrampolines(self, klass.Get());
434 return nullptr;
435 }
436 if (Runtime::Current()->IsActiveTransaction()) {
437 // Transactions are single-threaded, so we can mark the class as visibly intialized.
438 // (Otherwise we'd need to track the callback's entry in the transaction for rollback.)
439 mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self);
440 FixupStaticTrampolines(self, klass.Get());
441 return nullptr;
442 }
443 mirror::Class::SetStatus(klass, ClassStatus::kInitialized, self);
444 MutexLock lock(self, visibly_initialized_callback_lock_);
445 if (visibly_initialized_callback_ == nullptr) {
446 visibly_initialized_callback_.reset(new VisiblyInitializedCallback(this));
447 }
448 DCHECK(!visibly_initialized_callback_->IsFull());
449 visibly_initialized_callback_->AddClass(self, klass.Get());
450
451 if (visibly_initialized_callback_->IsFull()) {
452 VisiblyInitializedCallback* callback = visibly_initialized_callback_.release();
453 running_visibly_initialized_callbacks_.push_front(*callback);
454 return callback;
455 } else {
456 return nullptr;
457 }
458 }
459
RegisterNative(Thread * self,ArtMethod * method,const void * native_method)460 const void* ClassLinker::RegisterNative(
461 Thread* self, ArtMethod* method, const void* native_method) {
462 CHECK(method->IsNative()) << method->PrettyMethod();
463 CHECK(native_method != nullptr) << method->PrettyMethod();
464 void* new_native_method = nullptr;
465 Runtime* runtime = Runtime::Current();
466 runtime->GetRuntimeCallbacks()->RegisterNativeMethod(method,
467 native_method,
468 /*out*/&new_native_method);
469 if (method->IsCriticalNative()) {
470 MutexLock lock(self, critical_native_code_with_clinit_check_lock_);
471 // Remove old registered method if any.
472 auto it = critical_native_code_with_clinit_check_.find(method);
473 if (it != critical_native_code_with_clinit_check_.end()) {
474 critical_native_code_with_clinit_check_.erase(it);
475 }
476 // To ensure correct memory visibility, we need the class to be visibly
477 // initialized before we can set the JNI entrypoint.
478 if (method->GetDeclaringClass()->IsVisiblyInitialized()) {
479 method->SetEntryPointFromJni(new_native_method);
480 } else {
481 critical_native_code_with_clinit_check_.emplace(method, new_native_method);
482 }
483 } else {
484 method->SetEntryPointFromJni(new_native_method);
485 }
486 return new_native_method;
487 }
488
UnregisterNative(Thread * self,ArtMethod * method)489 void ClassLinker::UnregisterNative(Thread* self, ArtMethod* method) {
490 CHECK(method->IsNative()) << method->PrettyMethod();
491 // Restore stub to lookup native pointer via dlsym.
492 if (method->IsCriticalNative()) {
493 MutexLock lock(self, critical_native_code_with_clinit_check_lock_);
494 auto it = critical_native_code_with_clinit_check_.find(method);
495 if (it != critical_native_code_with_clinit_check_.end()) {
496 critical_native_code_with_clinit_check_.erase(it);
497 }
498 method->SetEntryPointFromJni(GetJniDlsymLookupCriticalStub());
499 } else {
500 method->SetEntryPointFromJni(GetJniDlsymLookupStub());
501 }
502 }
503
GetRegisteredNative(Thread * self,ArtMethod * method)504 const void* ClassLinker::GetRegisteredNative(Thread* self, ArtMethod* method) {
505 if (method->IsCriticalNative()) {
506 MutexLock lock(self, critical_native_code_with_clinit_check_lock_);
507 auto it = critical_native_code_with_clinit_check_.find(method);
508 if (it != critical_native_code_with_clinit_check_.end()) {
509 return it->second;
510 }
511 const void* native_code = method->GetEntryPointFromJni();
512 return IsJniDlsymLookupCriticalStub(native_code) ? nullptr : native_code;
513 } else {
514 const void* native_code = method->GetEntryPointFromJni();
515 return IsJniDlsymLookupStub(native_code) ? nullptr : native_code;
516 }
517 }
518
ThrowEarlierClassFailure(ObjPtr<mirror::Class> c,bool wrap_in_no_class_def,bool log)519 void ClassLinker::ThrowEarlierClassFailure(ObjPtr<mirror::Class> c,
520 bool wrap_in_no_class_def,
521 bool log) {
522 // The class failed to initialize on a previous attempt, so we want to throw
523 // a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we
524 // failed in verification, in which case v2 5.4.1 says we need to re-throw
525 // the previous error.
526 Runtime* const runtime = Runtime::Current();
527 if (!runtime->IsAotCompiler()) { // Give info if this occurs at runtime.
528 std::string extra;
529 ObjPtr<mirror::Object> verify_error = GetVerifyError(c);
530 if (verify_error != nullptr) {
531 if (verify_error->IsClass()) {
532 extra = mirror::Class::PrettyDescriptor(verify_error->AsClass());
533 } else {
534 extra = verify_error->AsThrowable()->Dump();
535 }
536 }
537 if (log) {
538 LOG(INFO) << "Rejecting re-init on previously-failed class " << c->PrettyClass()
539 << ": " << extra;
540 }
541 }
542
543 CHECK(c->IsErroneous()) << c->PrettyClass() << " " << c->GetStatus();
544 Thread* self = Thread::Current();
545 if (runtime->IsAotCompiler()) {
546 // At compile time, accurate errors and NCDFE are disabled to speed compilation.
547 ObjPtr<mirror::Throwable> pre_allocated = runtime->GetPreAllocatedNoClassDefFoundError();
548 self->SetException(pre_allocated);
549 } else {
550 ObjPtr<mirror::Object> verify_error = GetVerifyError(c);
551 if (verify_error != nullptr) {
552 // Rethrow stored error.
553 HandleEarlierVerifyError(self, this, c);
554 }
555 // TODO This might be wrong if we hit an OOME while allocating the ClassExt. In that case we
556 // might have meant to go down the earlier if statement with the original error but it got
557 // swallowed by the OOM so we end up here.
558 if (verify_error == nullptr || wrap_in_no_class_def) {
559 // If there isn't a recorded earlier error, or this is a repeat throw from initialization,
560 // the top-level exception must be a NoClassDefFoundError. The potentially already pending
561 // exception will be a cause.
562 self->ThrowNewWrappedException("Ljava/lang/NoClassDefFoundError;",
563 c->PrettyDescriptor().c_str());
564 }
565 }
566 }
567
VlogClassInitializationFailure(Handle<mirror::Class> klass)568 static void VlogClassInitializationFailure(Handle<mirror::Class> klass)
569 REQUIRES_SHARED(Locks::mutator_lock_) {
570 if (VLOG_IS_ON(class_linker)) {
571 std::string temp;
572 LOG(INFO) << "Failed to initialize class " << klass->GetDescriptor(&temp) << " from "
573 << klass->GetLocation() << "\n" << Thread::Current()->GetException()->Dump();
574 }
575 }
576
WrapExceptionInInitializer(Handle<mirror::Class> klass)577 static void WrapExceptionInInitializer(Handle<mirror::Class> klass)
578 REQUIRES_SHARED(Locks::mutator_lock_) {
579 Thread* self = Thread::Current();
580 JNIEnv* env = self->GetJniEnv();
581
582 ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred());
583 CHECK(cause.get() != nullptr);
584
585 // Boot classpath classes should not fail initialization. This is a consistency debug check.
586 // This cannot in general be guaranteed, but in all likelihood leads to breakage down the line.
587 if (klass->GetClassLoader() == nullptr && !Runtime::Current()->IsAotCompiler()) {
588 std::string tmp;
589 // We want to LOG(FATAL) on debug builds since this really shouldn't be happening but we need to
590 // make sure to only do it if we don't have AsyncExceptions being thrown around since those
591 // could have caused the error.
592 bool known_impossible = kIsDebugBuild && !Runtime::Current()->AreAsyncExceptionsThrown();
593 LOG(known_impossible ? FATAL : WARNING) << klass->GetDescriptor(&tmp)
594 << " failed initialization: "
595 << self->GetException()->Dump();
596 }
597
598 env->ExceptionClear();
599 bool is_error = env->IsInstanceOf(cause.get(), WellKnownClasses::java_lang_Error);
600 env->Throw(cause.get());
601
602 // We only wrap non-Error exceptions; an Error can just be used as-is.
603 if (!is_error) {
604 self->ThrowNewWrappedException("Ljava/lang/ExceptionInInitializerError;", nullptr);
605 }
606 VlogClassInitializationFailure(klass);
607 }
608
609 // Gap between two fields in object layout.
610 struct FieldGap {
611 uint32_t start_offset; // The offset from the start of the object.
612 uint32_t size; // The gap size of 1, 2, or 4 bytes.
613 };
614 struct FieldGapsComparator {
FieldGapsComparatorart::FieldGapsComparator615 FieldGapsComparator() {
616 }
operator ()art::FieldGapsComparator617 bool operator() (const FieldGap& lhs, const FieldGap& rhs)
618 NO_THREAD_SAFETY_ANALYSIS {
619 // Sort by gap size, largest first. Secondary sort by starting offset.
620 // Note that the priority queue returns the largest element, so operator()
621 // should return true if lhs is less than rhs.
622 return lhs.size < rhs.size || (lhs.size == rhs.size && lhs.start_offset > rhs.start_offset);
623 }
624 };
625 using FieldGaps = std::priority_queue<FieldGap, std::vector<FieldGap>, FieldGapsComparator>;
626
627 // Adds largest aligned gaps to queue of gaps.
AddFieldGap(uint32_t gap_start,uint32_t gap_end,FieldGaps * gaps)628 static void AddFieldGap(uint32_t gap_start, uint32_t gap_end, FieldGaps* gaps) {
629 DCHECK(gaps != nullptr);
630
631 uint32_t current_offset = gap_start;
632 while (current_offset != gap_end) {
633 size_t remaining = gap_end - current_offset;
634 if (remaining >= sizeof(uint32_t) && IsAligned<4>(current_offset)) {
635 gaps->push(FieldGap {current_offset, sizeof(uint32_t)});
636 current_offset += sizeof(uint32_t);
637 } else if (remaining >= sizeof(uint16_t) && IsAligned<2>(current_offset)) {
638 gaps->push(FieldGap {current_offset, sizeof(uint16_t)});
639 current_offset += sizeof(uint16_t);
640 } else {
641 gaps->push(FieldGap {current_offset, sizeof(uint8_t)});
642 current_offset += sizeof(uint8_t);
643 }
644 DCHECK_LE(current_offset, gap_end) << "Overran gap";
645 }
646 }
647 // Shuffle fields forward, making use of gaps whenever possible.
648 template<int n>
ShuffleForward(size_t * current_field_idx,MemberOffset * field_offset,std::deque<ArtField * > * grouped_and_sorted_fields,FieldGaps * gaps)649 static void ShuffleForward(size_t* current_field_idx,
650 MemberOffset* field_offset,
651 std::deque<ArtField*>* grouped_and_sorted_fields,
652 FieldGaps* gaps)
653 REQUIRES_SHARED(Locks::mutator_lock_) {
654 DCHECK(current_field_idx != nullptr);
655 DCHECK(grouped_and_sorted_fields != nullptr);
656 DCHECK(gaps != nullptr);
657 DCHECK(field_offset != nullptr);
658
659 DCHECK(IsPowerOfTwo(n));
660 while (!grouped_and_sorted_fields->empty()) {
661 ArtField* field = grouped_and_sorted_fields->front();
662 Primitive::Type type = field->GetTypeAsPrimitiveType();
663 if (Primitive::ComponentSize(type) < n) {
664 break;
665 }
666 if (!IsAligned<n>(field_offset->Uint32Value())) {
667 MemberOffset old_offset = *field_offset;
668 *field_offset = MemberOffset(RoundUp(field_offset->Uint32Value(), n));
669 AddFieldGap(old_offset.Uint32Value(), field_offset->Uint32Value(), gaps);
670 }
671 CHECK(type != Primitive::kPrimNot) << field->PrettyField(); // should be primitive types
672 grouped_and_sorted_fields->pop_front();
673 if (!gaps->empty() && gaps->top().size >= n) {
674 FieldGap gap = gaps->top();
675 gaps->pop();
676 DCHECK_ALIGNED(gap.start_offset, n);
677 field->SetOffset(MemberOffset(gap.start_offset));
678 if (gap.size > n) {
679 AddFieldGap(gap.start_offset + n, gap.start_offset + gap.size, gaps);
680 }
681 } else {
682 DCHECK_ALIGNED(field_offset->Uint32Value(), n);
683 field->SetOffset(*field_offset);
684 *field_offset = MemberOffset(field_offset->Uint32Value() + n);
685 }
686 ++(*current_field_idx);
687 }
688 }
689
ClassLinker(InternTable * intern_table,bool fast_class_not_found_exceptions)690 ClassLinker::ClassLinker(InternTable* intern_table, bool fast_class_not_found_exceptions)
691 : boot_class_table_(new ClassTable()),
692 failed_dex_cache_class_lookups_(0),
693 class_roots_(nullptr),
694 find_array_class_cache_next_victim_(0),
695 init_done_(false),
696 log_new_roots_(false),
697 intern_table_(intern_table),
698 fast_class_not_found_exceptions_(fast_class_not_found_exceptions),
699 jni_dlsym_lookup_trampoline_(nullptr),
700 jni_dlsym_lookup_critical_trampoline_(nullptr),
701 quick_resolution_trampoline_(nullptr),
702 quick_imt_conflict_trampoline_(nullptr),
703 quick_generic_jni_trampoline_(nullptr),
704 quick_to_interpreter_bridge_trampoline_(nullptr),
705 image_pointer_size_(kRuntimePointerSize),
706 visibly_initialized_callback_lock_("visibly initialized callback lock"),
707 visibly_initialized_callback_(nullptr),
708 critical_native_code_with_clinit_check_lock_("critical native code with clinit check lock"),
709 critical_native_code_with_clinit_check_(),
710 cha_(Runtime::Current()->IsAotCompiler() ? nullptr : new ClassHierarchyAnalysis()) {
711 // For CHA disabled during Aot, see b/34193647.
712
713 CHECK(intern_table_ != nullptr);
714 static_assert(kFindArrayCacheSize == arraysize(find_array_class_cache_),
715 "Array cache size wrong.");
716 std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr));
717 }
718
CheckSystemClass(Thread * self,Handle<mirror::Class> c1,const char * descriptor)719 void ClassLinker::CheckSystemClass(Thread* self, Handle<mirror::Class> c1, const char* descriptor) {
720 ObjPtr<mirror::Class> c2 = FindSystemClass(self, descriptor);
721 if (c2 == nullptr) {
722 LOG(FATAL) << "Could not find class " << descriptor;
723 UNREACHABLE();
724 }
725 if (c1.Get() != c2) {
726 std::ostringstream os1, os2;
727 c1->DumpClass(os1, mirror::Class::kDumpClassFullDetail);
728 c2->DumpClass(os2, mirror::Class::kDumpClassFullDetail);
729 LOG(FATAL) << "InitWithoutImage: Class mismatch for " << descriptor
730 << ". This is most likely the result of a broken build. Make sure that "
731 << "libcore and art projects match.\n\n"
732 << os1.str() << "\n\n" << os2.str();
733 UNREACHABLE();
734 }
735 }
736
InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path,std::string * error_msg)737 bool ClassLinker::InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path,
738 std::string* error_msg) {
739 VLOG(startup) << "ClassLinker::Init";
740
741 Thread* const self = Thread::Current();
742 Runtime* const runtime = Runtime::Current();
743 gc::Heap* const heap = runtime->GetHeap();
744
745 CHECK(!heap->HasBootImageSpace()) << "Runtime has image. We should use it.";
746 CHECK(!init_done_);
747
748 // Use the pointer size from the runtime since we are probably creating the image.
749 image_pointer_size_ = InstructionSetPointerSize(runtime->GetInstructionSet());
750
751 // java_lang_Class comes first, it's needed for AllocClass
752 // The GC can't handle an object with a null class since we can't get the size of this object.
753 heap->IncrementDisableMovingGC(self);
754 StackHandleScope<64> hs(self); // 64 is picked arbitrarily.
755 auto class_class_size = mirror::Class::ClassClassSize(image_pointer_size_);
756 // Allocate the object as non-movable so that there are no cases where Object::IsClass returns
757 // the incorrect result when comparing to-space vs from-space.
758 Handle<mirror::Class> java_lang_Class(hs.NewHandle(ObjPtr<mirror::Class>::DownCast(
759 heap->AllocNonMovableObject(self, nullptr, class_class_size, VoidFunctor()))));
760 CHECK(java_lang_Class != nullptr);
761 java_lang_Class->SetClassFlags(mirror::kClassFlagClass);
762 java_lang_Class->SetClass(java_lang_Class.Get());
763 if (kUseBakerReadBarrier) {
764 java_lang_Class->AssertReadBarrierState();
765 }
766 java_lang_Class->SetClassSize(class_class_size);
767 java_lang_Class->SetPrimitiveType(Primitive::kPrimNot);
768 heap->DecrementDisableMovingGC(self);
769 // AllocClass(ObjPtr<mirror::Class>) can now be used
770
771 // Class[] is used for reflection support.
772 auto class_array_class_size = mirror::ObjectArray<mirror::Class>::ClassSize(image_pointer_size_);
773 Handle<mirror::Class> class_array_class(hs.NewHandle(
774 AllocClass(self, java_lang_Class.Get(), class_array_class_size)));
775 class_array_class->SetComponentType(java_lang_Class.Get());
776
777 // java_lang_Object comes next so that object_array_class can be created.
778 Handle<mirror::Class> java_lang_Object(hs.NewHandle(
779 AllocClass(self, java_lang_Class.Get(), mirror::Object::ClassSize(image_pointer_size_))));
780 CHECK(java_lang_Object != nullptr);
781 // backfill Object as the super class of Class.
782 java_lang_Class->SetSuperClass(java_lang_Object.Get());
783 mirror::Class::SetStatus(java_lang_Object, ClassStatus::kLoaded, self);
784
785 java_lang_Object->SetObjectSize(sizeof(mirror::Object));
786 // Allocate in non-movable so that it's possible to check if a JNI weak global ref has been
787 // cleared without triggering the read barrier and unintentionally mark the sentinel alive.
788 runtime->SetSentinel(heap->AllocNonMovableObject(self,
789 java_lang_Object.Get(),
790 java_lang_Object->GetObjectSize(),
791 VoidFunctor()));
792
793 // Initialize the SubtypeCheck bitstring for java.lang.Object and java.lang.Class.
794 if (kBitstringSubtypeCheckEnabled) {
795 // It might seem the lock here is unnecessary, however all the SubtypeCheck
796 // functions are annotated to require locks all the way down.
797 //
798 // We take the lock here to avoid using NO_THREAD_SAFETY_ANALYSIS.
799 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
800 SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(java_lang_Object.Get());
801 SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(java_lang_Class.Get());
802 }
803
804 // Object[] next to hold class roots.
805 Handle<mirror::Class> object_array_class(hs.NewHandle(
806 AllocClass(self, java_lang_Class.Get(),
807 mirror::ObjectArray<mirror::Object>::ClassSize(image_pointer_size_))));
808 object_array_class->SetComponentType(java_lang_Object.Get());
809
810 // Setup java.lang.String.
811 //
812 // We make this class non-movable for the unlikely case where it were to be
813 // moved by a sticky-bit (minor) collection when using the Generational
814 // Concurrent Copying (CC) collector, potentially creating a stale reference
815 // in the `klass_` field of one of its instances allocated in the Large-Object
816 // Space (LOS) -- see the comment about the dirty card scanning logic in
817 // art::gc::collector::ConcurrentCopying::MarkingPhase.
818 Handle<mirror::Class> java_lang_String(hs.NewHandle(
819 AllocClass</* kMovable= */ false>(
820 self, java_lang_Class.Get(), mirror::String::ClassSize(image_pointer_size_))));
821 java_lang_String->SetStringClass();
822 mirror::Class::SetStatus(java_lang_String, ClassStatus::kResolved, self);
823
824 // Setup java.lang.ref.Reference.
825 Handle<mirror::Class> java_lang_ref_Reference(hs.NewHandle(
826 AllocClass(self, java_lang_Class.Get(), mirror::Reference::ClassSize(image_pointer_size_))));
827 java_lang_ref_Reference->SetObjectSize(mirror::Reference::InstanceSize());
828 mirror::Class::SetStatus(java_lang_ref_Reference, ClassStatus::kResolved, self);
829
830 // Create storage for root classes, save away our work so far (requires descriptors).
831 class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>(
832 mirror::ObjectArray<mirror::Class>::Alloc(self,
833 object_array_class.Get(),
834 static_cast<int32_t>(ClassRoot::kMax)));
835 CHECK(!class_roots_.IsNull());
836 SetClassRoot(ClassRoot::kJavaLangClass, java_lang_Class.Get());
837 SetClassRoot(ClassRoot::kJavaLangObject, java_lang_Object.Get());
838 SetClassRoot(ClassRoot::kClassArrayClass, class_array_class.Get());
839 SetClassRoot(ClassRoot::kObjectArrayClass, object_array_class.Get());
840 SetClassRoot(ClassRoot::kJavaLangString, java_lang_String.Get());
841 SetClassRoot(ClassRoot::kJavaLangRefReference, java_lang_ref_Reference.Get());
842
843 // Fill in the empty iftable. Needs to be done after the kObjectArrayClass root is set.
844 java_lang_Object->SetIfTable(AllocIfTable(self, 0));
845
846 // Create array interface entries to populate once we can load system classes.
847 object_array_class->SetIfTable(AllocIfTable(self, 2));
848 DCHECK_EQ(GetArrayIfTable(), object_array_class->GetIfTable());
849
850 // Setup the primitive type classes.
851 CreatePrimitiveClass(self, Primitive::kPrimBoolean, ClassRoot::kPrimitiveBoolean);
852 CreatePrimitiveClass(self, Primitive::kPrimByte, ClassRoot::kPrimitiveByte);
853 CreatePrimitiveClass(self, Primitive::kPrimChar, ClassRoot::kPrimitiveChar);
854 CreatePrimitiveClass(self, Primitive::kPrimShort, ClassRoot::kPrimitiveShort);
855 CreatePrimitiveClass(self, Primitive::kPrimInt, ClassRoot::kPrimitiveInt);
856 CreatePrimitiveClass(self, Primitive::kPrimLong, ClassRoot::kPrimitiveLong);
857 CreatePrimitiveClass(self, Primitive::kPrimFloat, ClassRoot::kPrimitiveFloat);
858 CreatePrimitiveClass(self, Primitive::kPrimDouble, ClassRoot::kPrimitiveDouble);
859 CreatePrimitiveClass(self, Primitive::kPrimVoid, ClassRoot::kPrimitiveVoid);
860
861 // Allocate the primitive array classes. We need only the native pointer
862 // array at this point (int[] or long[], depending on architecture) but
863 // we shall perform the same setup steps for all primitive array classes.
864 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveBoolean, ClassRoot::kBooleanArrayClass);
865 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveByte, ClassRoot::kByteArrayClass);
866 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveChar, ClassRoot::kCharArrayClass);
867 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveShort, ClassRoot::kShortArrayClass);
868 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveInt, ClassRoot::kIntArrayClass);
869 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveLong, ClassRoot::kLongArrayClass);
870 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveFloat, ClassRoot::kFloatArrayClass);
871 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveDouble, ClassRoot::kDoubleArrayClass);
872
873 // now that these are registered, we can use AllocClass() and AllocObjectArray
874
875 // Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache.
876 Handle<mirror::Class> java_lang_DexCache(hs.NewHandle(
877 AllocClass(self, java_lang_Class.Get(), mirror::DexCache::ClassSize(image_pointer_size_))));
878 SetClassRoot(ClassRoot::kJavaLangDexCache, java_lang_DexCache.Get());
879 java_lang_DexCache->SetDexCacheClass();
880 java_lang_DexCache->SetObjectSize(mirror::DexCache::InstanceSize());
881 mirror::Class::SetStatus(java_lang_DexCache, ClassStatus::kResolved, self);
882
883
884 // Setup dalvik.system.ClassExt
885 Handle<mirror::Class> dalvik_system_ClassExt(hs.NewHandle(
886 AllocClass(self, java_lang_Class.Get(), mirror::ClassExt::ClassSize(image_pointer_size_))));
887 SetClassRoot(ClassRoot::kDalvikSystemClassExt, dalvik_system_ClassExt.Get());
888 mirror::Class::SetStatus(dalvik_system_ClassExt, ClassStatus::kResolved, self);
889
890 // Set up array classes for string, field, method
891 Handle<mirror::Class> object_array_string(hs.NewHandle(
892 AllocClass(self, java_lang_Class.Get(),
893 mirror::ObjectArray<mirror::String>::ClassSize(image_pointer_size_))));
894 object_array_string->SetComponentType(java_lang_String.Get());
895 SetClassRoot(ClassRoot::kJavaLangStringArrayClass, object_array_string.Get());
896
897 LinearAlloc* linear_alloc = runtime->GetLinearAlloc();
898 // Create runtime resolution and imt conflict methods.
899 runtime->SetResolutionMethod(runtime->CreateResolutionMethod());
900 runtime->SetImtConflictMethod(runtime->CreateImtConflictMethod(linear_alloc));
901 runtime->SetImtUnimplementedMethod(runtime->CreateImtConflictMethod(linear_alloc));
902
903 // Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create
904 // DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses
905 // these roots.
906 if (boot_class_path.empty()) {
907 *error_msg = "Boot classpath is empty.";
908 return false;
909 }
910 for (auto& dex_file : boot_class_path) {
911 if (dex_file == nullptr) {
912 *error_msg = "Null dex file.";
913 return false;
914 }
915 AppendToBootClassPath(self, dex_file.get());
916 boot_dex_files_.push_back(std::move(dex_file));
917 }
918
919 // now we can use FindSystemClass
920
921 // Set up GenericJNI entrypoint. That is mainly a hack for common_compiler_test.h so that
922 // we do not need friend classes or a publicly exposed setter.
923 quick_generic_jni_trampoline_ = GetQuickGenericJniStub();
924 if (!runtime->IsAotCompiler()) {
925 // We need to set up the generic trampolines since we don't have an image.
926 jni_dlsym_lookup_trampoline_ = GetJniDlsymLookupStub();
927 jni_dlsym_lookup_critical_trampoline_ = GetJniDlsymLookupCriticalStub();
928 quick_resolution_trampoline_ = GetQuickResolutionStub();
929 quick_imt_conflict_trampoline_ = GetQuickImtConflictStub();
930 quick_generic_jni_trampoline_ = GetQuickGenericJniStub();
931 quick_to_interpreter_bridge_trampoline_ = GetQuickToInterpreterBridge();
932 }
933
934 // Object, String, ClassExt and DexCache need to be rerun through FindSystemClass to finish init
935 mirror::Class::SetStatus(java_lang_Object, ClassStatus::kNotReady, self);
936 CheckSystemClass(self, java_lang_Object, "Ljava/lang/Object;");
937 CHECK_EQ(java_lang_Object->GetObjectSize(), mirror::Object::InstanceSize());
938 mirror::Class::SetStatus(java_lang_String, ClassStatus::kNotReady, self);
939 CheckSystemClass(self, java_lang_String, "Ljava/lang/String;");
940 mirror::Class::SetStatus(java_lang_DexCache, ClassStatus::kNotReady, self);
941 CheckSystemClass(self, java_lang_DexCache, "Ljava/lang/DexCache;");
942 CHECK_EQ(java_lang_DexCache->GetObjectSize(), mirror::DexCache::InstanceSize());
943 mirror::Class::SetStatus(dalvik_system_ClassExt, ClassStatus::kNotReady, self);
944 CheckSystemClass(self, dalvik_system_ClassExt, "Ldalvik/system/ClassExt;");
945 CHECK_EQ(dalvik_system_ClassExt->GetObjectSize(), mirror::ClassExt::InstanceSize());
946
947 // Run Class through FindSystemClass. This initializes the dex_cache_ fields and register it
948 // in class_table_.
949 CheckSystemClass(self, java_lang_Class, "Ljava/lang/Class;");
950
951 // Setup core array classes, i.e. Object[], String[] and Class[] and primitive
952 // arrays - can't be done until Object has a vtable and component classes are loaded.
953 FinishCoreArrayClassSetup(ClassRoot::kObjectArrayClass);
954 FinishCoreArrayClassSetup(ClassRoot::kClassArrayClass);
955 FinishCoreArrayClassSetup(ClassRoot::kJavaLangStringArrayClass);
956 FinishCoreArrayClassSetup(ClassRoot::kBooleanArrayClass);
957 FinishCoreArrayClassSetup(ClassRoot::kByteArrayClass);
958 FinishCoreArrayClassSetup(ClassRoot::kCharArrayClass);
959 FinishCoreArrayClassSetup(ClassRoot::kShortArrayClass);
960 FinishCoreArrayClassSetup(ClassRoot::kIntArrayClass);
961 FinishCoreArrayClassSetup(ClassRoot::kLongArrayClass);
962 FinishCoreArrayClassSetup(ClassRoot::kFloatArrayClass);
963 FinishCoreArrayClassSetup(ClassRoot::kDoubleArrayClass);
964
965 // Setup the single, global copy of "iftable".
966 auto java_lang_Cloneable = hs.NewHandle(FindSystemClass(self, "Ljava/lang/Cloneable;"));
967 CHECK(java_lang_Cloneable != nullptr);
968 auto java_io_Serializable = hs.NewHandle(FindSystemClass(self, "Ljava/io/Serializable;"));
969 CHECK(java_io_Serializable != nullptr);
970 // We assume that Cloneable/Serializable don't have superinterfaces -- normally we'd have to
971 // crawl up and explicitly list all of the supers as well.
972 object_array_class->GetIfTable()->SetInterface(0, java_lang_Cloneable.Get());
973 object_array_class->GetIfTable()->SetInterface(1, java_io_Serializable.Get());
974
975 // Check Class[] and Object[]'s interfaces. GetDirectInterface may cause thread suspension.
976 CHECK_EQ(java_lang_Cloneable.Get(),
977 mirror::Class::GetDirectInterface(self, class_array_class.Get(), 0));
978 CHECK_EQ(java_io_Serializable.Get(),
979 mirror::Class::GetDirectInterface(self, class_array_class.Get(), 1));
980 CHECK_EQ(java_lang_Cloneable.Get(),
981 mirror::Class::GetDirectInterface(self, object_array_class.Get(), 0));
982 CHECK_EQ(java_io_Serializable.Get(),
983 mirror::Class::GetDirectInterface(self, object_array_class.Get(), 1));
984
985 CHECK_EQ(object_array_string.Get(),
986 FindSystemClass(self, GetClassRootDescriptor(ClassRoot::kJavaLangStringArrayClass)));
987
988 // End of special init trickery, all subsequent classes may be loaded via FindSystemClass.
989
990 // Create java.lang.reflect.Proxy root.
991 SetClassRoot(ClassRoot::kJavaLangReflectProxy,
992 FindSystemClass(self, "Ljava/lang/reflect/Proxy;"));
993
994 // Create java.lang.reflect.Field.class root.
995 ObjPtr<mirror::Class> class_root = FindSystemClass(self, "Ljava/lang/reflect/Field;");
996 CHECK(class_root != nullptr);
997 SetClassRoot(ClassRoot::kJavaLangReflectField, class_root);
998
999 // Create java.lang.reflect.Field array root.
1000 class_root = FindSystemClass(self, "[Ljava/lang/reflect/Field;");
1001 CHECK(class_root != nullptr);
1002 SetClassRoot(ClassRoot::kJavaLangReflectFieldArrayClass, class_root);
1003
1004 // Create java.lang.reflect.Constructor.class root and array root.
1005 class_root = FindSystemClass(self, "Ljava/lang/reflect/Constructor;");
1006 CHECK(class_root != nullptr);
1007 SetClassRoot(ClassRoot::kJavaLangReflectConstructor, class_root);
1008 class_root = FindSystemClass(self, "[Ljava/lang/reflect/Constructor;");
1009 CHECK(class_root != nullptr);
1010 SetClassRoot(ClassRoot::kJavaLangReflectConstructorArrayClass, class_root);
1011
1012 // Create java.lang.reflect.Method.class root and array root.
1013 class_root = FindSystemClass(self, "Ljava/lang/reflect/Method;");
1014 CHECK(class_root != nullptr);
1015 SetClassRoot(ClassRoot::kJavaLangReflectMethod, class_root);
1016 class_root = FindSystemClass(self, "[Ljava/lang/reflect/Method;");
1017 CHECK(class_root != nullptr);
1018 SetClassRoot(ClassRoot::kJavaLangReflectMethodArrayClass, class_root);
1019
1020 // Create java.lang.invoke.CallSite.class root
1021 class_root = FindSystemClass(self, "Ljava/lang/invoke/CallSite;");
1022 CHECK(class_root != nullptr);
1023 SetClassRoot(ClassRoot::kJavaLangInvokeCallSite, class_root);
1024
1025 // Create java.lang.invoke.MethodType.class root
1026 class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodType;");
1027 CHECK(class_root != nullptr);
1028 SetClassRoot(ClassRoot::kJavaLangInvokeMethodType, class_root);
1029
1030 // Create java.lang.invoke.MethodHandleImpl.class root
1031 class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandleImpl;");
1032 CHECK(class_root != nullptr);
1033 SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandleImpl, class_root);
1034 SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandle, class_root->GetSuperClass());
1035
1036 // Create java.lang.invoke.MethodHandles.Lookup.class root
1037 class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandles$Lookup;");
1038 CHECK(class_root != nullptr);
1039 SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandlesLookup, class_root);
1040
1041 // Create java.lang.invoke.VarHandle.class root
1042 class_root = FindSystemClass(self, "Ljava/lang/invoke/VarHandle;");
1043 CHECK(class_root != nullptr);
1044 SetClassRoot(ClassRoot::kJavaLangInvokeVarHandle, class_root);
1045
1046 // Create java.lang.invoke.FieldVarHandle.class root
1047 class_root = FindSystemClass(self, "Ljava/lang/invoke/FieldVarHandle;");
1048 CHECK(class_root != nullptr);
1049 SetClassRoot(ClassRoot::kJavaLangInvokeFieldVarHandle, class_root);
1050
1051 // Create java.lang.invoke.ArrayElementVarHandle.class root
1052 class_root = FindSystemClass(self, "Ljava/lang/invoke/ArrayElementVarHandle;");
1053 CHECK(class_root != nullptr);
1054 SetClassRoot(ClassRoot::kJavaLangInvokeArrayElementVarHandle, class_root);
1055
1056 // Create java.lang.invoke.ByteArrayViewVarHandle.class root
1057 class_root = FindSystemClass(self, "Ljava/lang/invoke/ByteArrayViewVarHandle;");
1058 CHECK(class_root != nullptr);
1059 SetClassRoot(ClassRoot::kJavaLangInvokeByteArrayViewVarHandle, class_root);
1060
1061 // Create java.lang.invoke.ByteBufferViewVarHandle.class root
1062 class_root = FindSystemClass(self, "Ljava/lang/invoke/ByteBufferViewVarHandle;");
1063 CHECK(class_root != nullptr);
1064 SetClassRoot(ClassRoot::kJavaLangInvokeByteBufferViewVarHandle, class_root);
1065
1066 class_root = FindSystemClass(self, "Ldalvik/system/EmulatedStackFrame;");
1067 CHECK(class_root != nullptr);
1068 SetClassRoot(ClassRoot::kDalvikSystemEmulatedStackFrame, class_root);
1069
1070 // java.lang.ref classes need to be specially flagged, but otherwise are normal classes
1071 // finish initializing Reference class
1072 mirror::Class::SetStatus(java_lang_ref_Reference, ClassStatus::kNotReady, self);
1073 CheckSystemClass(self, java_lang_ref_Reference, "Ljava/lang/ref/Reference;");
1074 CHECK_EQ(java_lang_ref_Reference->GetObjectSize(), mirror::Reference::InstanceSize());
1075 CHECK_EQ(java_lang_ref_Reference->GetClassSize(),
1076 mirror::Reference::ClassSize(image_pointer_size_));
1077 class_root = FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;");
1078 CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal);
1079 class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagFinalizerReference);
1080 class_root = FindSystemClass(self, "Ljava/lang/ref/PhantomReference;");
1081 CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal);
1082 class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagPhantomReference);
1083 class_root = FindSystemClass(self, "Ljava/lang/ref/SoftReference;");
1084 CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal);
1085 class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagSoftReference);
1086 class_root = FindSystemClass(self, "Ljava/lang/ref/WeakReference;");
1087 CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal);
1088 class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagWeakReference);
1089
1090 // Setup the ClassLoader, verifying the object_size_.
1091 class_root = FindSystemClass(self, "Ljava/lang/ClassLoader;");
1092 class_root->SetClassLoaderClass();
1093 CHECK_EQ(class_root->GetObjectSize(), mirror::ClassLoader::InstanceSize());
1094 SetClassRoot(ClassRoot::kJavaLangClassLoader, class_root);
1095
1096 // Set up java.lang.Throwable, java.lang.ClassNotFoundException, and
1097 // java.lang.StackTraceElement as a convenience.
1098 SetClassRoot(ClassRoot::kJavaLangThrowable, FindSystemClass(self, "Ljava/lang/Throwable;"));
1099 SetClassRoot(ClassRoot::kJavaLangClassNotFoundException,
1100 FindSystemClass(self, "Ljava/lang/ClassNotFoundException;"));
1101 SetClassRoot(ClassRoot::kJavaLangStackTraceElement,
1102 FindSystemClass(self, "Ljava/lang/StackTraceElement;"));
1103 SetClassRoot(ClassRoot::kJavaLangStackTraceElementArrayClass,
1104 FindSystemClass(self, "[Ljava/lang/StackTraceElement;"));
1105 SetClassRoot(ClassRoot::kJavaLangClassLoaderArrayClass,
1106 FindSystemClass(self, "[Ljava/lang/ClassLoader;"));
1107
1108 // Create conflict tables that depend on the class linker.
1109 runtime->FixupConflictTables();
1110
1111 FinishInit(self);
1112
1113 VLOG(startup) << "ClassLinker::InitFromCompiler exiting";
1114
1115 return true;
1116 }
1117
CreateStringInitBindings(Thread * self,ClassLinker * class_linker)1118 static void CreateStringInitBindings(Thread* self, ClassLinker* class_linker)
1119 REQUIRES_SHARED(Locks::mutator_lock_) {
1120 // Find String.<init> -> StringFactory bindings.
1121 ObjPtr<mirror::Class> string_factory_class =
1122 class_linker->FindSystemClass(self, "Ljava/lang/StringFactory;");
1123 CHECK(string_factory_class != nullptr);
1124 ObjPtr<mirror::Class> string_class = GetClassRoot<mirror::String>(class_linker);
1125 WellKnownClasses::InitStringInit(string_class, string_factory_class);
1126 // Update the primordial thread.
1127 self->InitStringEntryPoints();
1128 }
1129
FinishInit(Thread * self)1130 void ClassLinker::FinishInit(Thread* self) {
1131 VLOG(startup) << "ClassLinker::FinishInit entering";
1132
1133 CreateStringInitBindings(self, this);
1134
1135 // Let the heap know some key offsets into java.lang.ref instances
1136 // Note: we hard code the field indexes here rather than using FindInstanceField
1137 // as the types of the field can't be resolved prior to the runtime being
1138 // fully initialized
1139 StackHandleScope<3> hs(self);
1140 Handle<mirror::Class> java_lang_ref_Reference =
1141 hs.NewHandle(GetClassRoot<mirror::Reference>(this));
1142 Handle<mirror::Class> java_lang_ref_FinalizerReference =
1143 hs.NewHandle(FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"));
1144
1145 ArtField* pendingNext = java_lang_ref_Reference->GetInstanceField(0);
1146 CHECK_STREQ(pendingNext->GetName(), "pendingNext");
1147 CHECK_STREQ(pendingNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;");
1148
1149 ArtField* queue = java_lang_ref_Reference->GetInstanceField(1);
1150 CHECK_STREQ(queue->GetName(), "queue");
1151 CHECK_STREQ(queue->GetTypeDescriptor(), "Ljava/lang/ref/ReferenceQueue;");
1152
1153 ArtField* queueNext = java_lang_ref_Reference->GetInstanceField(2);
1154 CHECK_STREQ(queueNext->GetName(), "queueNext");
1155 CHECK_STREQ(queueNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;");
1156
1157 ArtField* referent = java_lang_ref_Reference->GetInstanceField(3);
1158 CHECK_STREQ(referent->GetName(), "referent");
1159 CHECK_STREQ(referent->GetTypeDescriptor(), "Ljava/lang/Object;");
1160
1161 ArtField* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2);
1162 CHECK_STREQ(zombie->GetName(), "zombie");
1163 CHECK_STREQ(zombie->GetTypeDescriptor(), "Ljava/lang/Object;");
1164
1165 // ensure all class_roots_ are initialized
1166 for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); i++) {
1167 ClassRoot class_root = static_cast<ClassRoot>(i);
1168 ObjPtr<mirror::Class> klass = GetClassRoot(class_root);
1169 CHECK(klass != nullptr);
1170 DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != nullptr);
1171 // note SetClassRoot does additional validation.
1172 // if possible add new checks there to catch errors early
1173 }
1174
1175 CHECK(GetArrayIfTable() != nullptr);
1176
1177 // disable the slow paths in FindClass and CreatePrimitiveClass now
1178 // that Object, Class, and Object[] are setup
1179 init_done_ = true;
1180
1181 // Under sanitization, the small carve-out to handle stack overflow might not be enough to
1182 // initialize the StackOverflowError class (as it might require running the verifier). Instead,
1183 // ensure that the class will be initialized.
1184 if (kMemoryToolIsAvailable && !Runtime::Current()->IsAotCompiler()) {
1185 verifier::ClassVerifier::Init(this); // Need to prepare the verifier.
1186
1187 ObjPtr<mirror::Class> soe_klass = FindSystemClass(self, "Ljava/lang/StackOverflowError;");
1188 if (soe_klass == nullptr || !EnsureInitialized(self, hs.NewHandle(soe_klass), true, true)) {
1189 // Strange, but don't crash.
1190 LOG(WARNING) << "Could not prepare StackOverflowError.";
1191 self->ClearException();
1192 }
1193 }
1194
1195 VLOG(startup) << "ClassLinker::FinishInit exiting";
1196 }
1197
RunRootClinits(Thread * self)1198 void ClassLinker::RunRootClinits(Thread* self) {
1199 for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); ++i) {
1200 ObjPtr<mirror::Class> c = GetClassRoot(ClassRoot(i), this);
1201 if (!c->IsArrayClass() && !c->IsPrimitive()) {
1202 StackHandleScope<1> hs(self);
1203 Handle<mirror::Class> h_class(hs.NewHandle(c));
1204 if (!EnsureInitialized(self, h_class, true, true)) {
1205 LOG(FATAL) << "Exception when initializing " << h_class->PrettyClass()
1206 << ": " << self->GetException()->Dump();
1207 }
1208 } else {
1209 DCHECK(c->IsInitialized());
1210 }
1211 }
1212 }
1213
1214 struct TrampolineCheckData {
1215 const void* quick_resolution_trampoline;
1216 const void* quick_imt_conflict_trampoline;
1217 const void* quick_generic_jni_trampoline;
1218 const void* quick_to_interpreter_bridge_trampoline;
1219 PointerSize pointer_size;
1220 ArtMethod* m;
1221 bool error;
1222 };
1223
InitFromBootImage(std::string * error_msg)1224 bool ClassLinker::InitFromBootImage(std::string* error_msg) {
1225 VLOG(startup) << __FUNCTION__ << " entering";
1226 CHECK(!init_done_);
1227
1228 Runtime* const runtime = Runtime::Current();
1229 Thread* const self = Thread::Current();
1230 gc::Heap* const heap = runtime->GetHeap();
1231 std::vector<gc::space::ImageSpace*> spaces = heap->GetBootImageSpaces();
1232 CHECK(!spaces.empty());
1233 const ImageHeader& image_header = spaces[0]->GetImageHeader();
1234 uint32_t pointer_size_unchecked = image_header.GetPointerSizeUnchecked();
1235 if (!ValidPointerSize(pointer_size_unchecked)) {
1236 *error_msg = StringPrintf("Invalid image pointer size: %u", pointer_size_unchecked);
1237 return false;
1238 }
1239 image_pointer_size_ = image_header.GetPointerSize();
1240 if (!runtime->IsAotCompiler()) {
1241 // Only the Aot compiler supports having an image with a different pointer size than the
1242 // runtime. This happens on the host for compiling 32 bit tests since we use a 64 bit libart
1243 // compiler. We may also use 32 bit dex2oat on a system with 64 bit apps.
1244 if (image_pointer_size_ != kRuntimePointerSize) {
1245 *error_msg = StringPrintf("Runtime must use current image pointer size: %zu vs %zu",
1246 static_cast<size_t>(image_pointer_size_),
1247 sizeof(void*));
1248 return false;
1249 }
1250 }
1251 DCHECK(!runtime->HasResolutionMethod());
1252 runtime->SetResolutionMethod(image_header.GetImageMethod(ImageHeader::kResolutionMethod));
1253 runtime->SetImtConflictMethod(image_header.GetImageMethod(ImageHeader::kImtConflictMethod));
1254 runtime->SetImtUnimplementedMethod(
1255 image_header.GetImageMethod(ImageHeader::kImtUnimplementedMethod));
1256 runtime->SetCalleeSaveMethod(
1257 image_header.GetImageMethod(ImageHeader::kSaveAllCalleeSavesMethod),
1258 CalleeSaveType::kSaveAllCalleeSaves);
1259 runtime->SetCalleeSaveMethod(
1260 image_header.GetImageMethod(ImageHeader::kSaveRefsOnlyMethod),
1261 CalleeSaveType::kSaveRefsOnly);
1262 runtime->SetCalleeSaveMethod(
1263 image_header.GetImageMethod(ImageHeader::kSaveRefsAndArgsMethod),
1264 CalleeSaveType::kSaveRefsAndArgs);
1265 runtime->SetCalleeSaveMethod(
1266 image_header.GetImageMethod(ImageHeader::kSaveEverythingMethod),
1267 CalleeSaveType::kSaveEverything);
1268 runtime->SetCalleeSaveMethod(
1269 image_header.GetImageMethod(ImageHeader::kSaveEverythingMethodForClinit),
1270 CalleeSaveType::kSaveEverythingForClinit);
1271 runtime->SetCalleeSaveMethod(
1272 image_header.GetImageMethod(ImageHeader::kSaveEverythingMethodForSuspendCheck),
1273 CalleeSaveType::kSaveEverythingForSuspendCheck);
1274
1275 std::vector<const OatFile*> oat_files =
1276 runtime->GetOatFileManager().RegisterImageOatFiles(spaces);
1277 DCHECK(!oat_files.empty());
1278 const OatHeader& default_oat_header = oat_files[0]->GetOatHeader();
1279 jni_dlsym_lookup_trampoline_ = default_oat_header.GetJniDlsymLookupTrampoline();
1280 jni_dlsym_lookup_critical_trampoline_ = default_oat_header.GetJniDlsymLookupCriticalTrampoline();
1281 quick_resolution_trampoline_ = default_oat_header.GetQuickResolutionTrampoline();
1282 quick_imt_conflict_trampoline_ = default_oat_header.GetQuickImtConflictTrampoline();
1283 quick_generic_jni_trampoline_ = default_oat_header.GetQuickGenericJniTrampoline();
1284 quick_to_interpreter_bridge_trampoline_ = default_oat_header.GetQuickToInterpreterBridge();
1285 if (kIsDebugBuild) {
1286 // Check that the other images use the same trampoline.
1287 for (size_t i = 1; i < oat_files.size(); ++i) {
1288 const OatHeader& ith_oat_header = oat_files[i]->GetOatHeader();
1289 const void* ith_jni_dlsym_lookup_trampoline_ =
1290 ith_oat_header.GetJniDlsymLookupTrampoline();
1291 const void* ith_jni_dlsym_lookup_critical_trampoline_ =
1292 ith_oat_header.GetJniDlsymLookupCriticalTrampoline();
1293 const void* ith_quick_resolution_trampoline =
1294 ith_oat_header.GetQuickResolutionTrampoline();
1295 const void* ith_quick_imt_conflict_trampoline =
1296 ith_oat_header.GetQuickImtConflictTrampoline();
1297 const void* ith_quick_generic_jni_trampoline =
1298 ith_oat_header.GetQuickGenericJniTrampoline();
1299 const void* ith_quick_to_interpreter_bridge_trampoline =
1300 ith_oat_header.GetQuickToInterpreterBridge();
1301 if (ith_jni_dlsym_lookup_trampoline_ != jni_dlsym_lookup_trampoline_ ||
1302 ith_jni_dlsym_lookup_critical_trampoline_ != jni_dlsym_lookup_critical_trampoline_ ||
1303 ith_quick_resolution_trampoline != quick_resolution_trampoline_ ||
1304 ith_quick_imt_conflict_trampoline != quick_imt_conflict_trampoline_ ||
1305 ith_quick_generic_jni_trampoline != quick_generic_jni_trampoline_ ||
1306 ith_quick_to_interpreter_bridge_trampoline != quick_to_interpreter_bridge_trampoline_) {
1307 // Make sure that all methods in this image do not contain those trampolines as
1308 // entrypoints. Otherwise the class-linker won't be able to work with a single set.
1309 TrampolineCheckData data;
1310 data.error = false;
1311 data.pointer_size = GetImagePointerSize();
1312 data.quick_resolution_trampoline = ith_quick_resolution_trampoline;
1313 data.quick_imt_conflict_trampoline = ith_quick_imt_conflict_trampoline;
1314 data.quick_generic_jni_trampoline = ith_quick_generic_jni_trampoline;
1315 data.quick_to_interpreter_bridge_trampoline = ith_quick_to_interpreter_bridge_trampoline;
1316 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
1317 auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
1318 if (obj->IsClass()) {
1319 ObjPtr<mirror::Class> klass = obj->AsClass();
1320 for (ArtMethod& m : klass->GetMethods(data.pointer_size)) {
1321 const void* entrypoint =
1322 m.GetEntryPointFromQuickCompiledCodePtrSize(data.pointer_size);
1323 if (entrypoint == data.quick_resolution_trampoline ||
1324 entrypoint == data.quick_imt_conflict_trampoline ||
1325 entrypoint == data.quick_generic_jni_trampoline ||
1326 entrypoint == data.quick_to_interpreter_bridge_trampoline) {
1327 data.m = &m;
1328 data.error = true;
1329 return;
1330 }
1331 }
1332 }
1333 };
1334 spaces[i]->GetLiveBitmap()->Walk(visitor);
1335 if (data.error) {
1336 ArtMethod* m = data.m;
1337 LOG(ERROR) << "Found a broken ArtMethod: " << ArtMethod::PrettyMethod(m);
1338 *error_msg = "Found an ArtMethod with a bad entrypoint";
1339 return false;
1340 }
1341 }
1342 }
1343 }
1344
1345 class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>(
1346 ObjPtr<mirror::ObjectArray<mirror::Class>>::DownCast(
1347 image_header.GetImageRoot(ImageHeader::kClassRoots)));
1348 DCHECK_EQ(GetClassRoot<mirror::Class>(this)->GetClassFlags(), mirror::kClassFlagClass);
1349
1350 DCHECK_EQ(GetClassRoot<mirror::Object>(this)->GetObjectSize(), sizeof(mirror::Object));
1351 ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects =
1352 ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast(
1353 image_header.GetImageRoot(ImageHeader::kBootImageLiveObjects));
1354 runtime->SetSentinel(boot_image_live_objects->Get(ImageHeader::kClearedJniWeakSentinel));
1355 DCHECK(runtime->GetSentinel().Read()->GetClass() == GetClassRoot<mirror::Object>(this));
1356
1357 for (size_t i = 0u, size = spaces.size(); i != size; ++i) {
1358 // Boot class loader, use a null handle.
1359 std::vector<std::unique_ptr<const DexFile>> dex_files;
1360 if (!AddImageSpace(spaces[i],
1361 ScopedNullHandle<mirror::ClassLoader>(),
1362 /*out*/&dex_files,
1363 error_msg)) {
1364 return false;
1365 }
1366 // Append opened dex files at the end.
1367 boot_dex_files_.insert(boot_dex_files_.end(),
1368 std::make_move_iterator(dex_files.begin()),
1369 std::make_move_iterator(dex_files.end()));
1370 }
1371 for (const std::unique_ptr<const DexFile>& dex_file : boot_dex_files_) {
1372 OatDexFile::MadviseDexFile(*dex_file, MadviseState::kMadviseStateAtLoad);
1373 }
1374 FinishInit(self);
1375
1376 VLOG(startup) << __FUNCTION__ << " exiting";
1377 return true;
1378 }
1379
AddExtraBootDexFiles(Thread * self,std::vector<std::unique_ptr<const DexFile>> && additional_dex_files)1380 void ClassLinker::AddExtraBootDexFiles(
1381 Thread* self,
1382 std::vector<std::unique_ptr<const DexFile>>&& additional_dex_files) {
1383 for (std::unique_ptr<const DexFile>& dex_file : additional_dex_files) {
1384 AppendToBootClassPath(self, dex_file.get());
1385 boot_dex_files_.push_back(std::move(dex_file));
1386 }
1387 }
1388
IsBootClassLoader(ScopedObjectAccessAlreadyRunnable & soa,ObjPtr<mirror::ClassLoader> class_loader)1389 bool ClassLinker::IsBootClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
1390 ObjPtr<mirror::ClassLoader> class_loader) {
1391 return class_loader == nullptr ||
1392 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader) ==
1393 class_loader->GetClass();
1394 }
1395
1396 class CHAOnDeleteUpdateClassVisitor {
1397 public:
CHAOnDeleteUpdateClassVisitor(LinearAlloc * alloc)1398 explicit CHAOnDeleteUpdateClassVisitor(LinearAlloc* alloc)
1399 : allocator_(alloc), cha_(Runtime::Current()->GetClassLinker()->GetClassHierarchyAnalysis()),
1400 pointer_size_(Runtime::Current()->GetClassLinker()->GetImagePointerSize()),
1401 self_(Thread::Current()) {}
1402
operator ()(ObjPtr<mirror::Class> klass)1403 bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) {
1404 // This class is going to be unloaded. Tell CHA about it.
1405 cha_->ResetSingleImplementationInHierarchy(klass, allocator_, pointer_size_);
1406 return true;
1407 }
1408 private:
1409 const LinearAlloc* allocator_;
1410 const ClassHierarchyAnalysis* cha_;
1411 const PointerSize pointer_size_;
1412 const Thread* self_;
1413 };
1414
1415 /*
1416 * A class used to ensure that all references to strings interned in an AppImage have been
1417 * properly recorded in the interned references list, and is only ever run in debug mode.
1418 */
1419 class CountInternedStringReferencesVisitor {
1420 public:
CountInternedStringReferencesVisitor(const gc::space::ImageSpace & space,const InternTable::UnorderedSet & image_interns)1421 CountInternedStringReferencesVisitor(const gc::space::ImageSpace& space,
1422 const InternTable::UnorderedSet& image_interns)
1423 : space_(space),
1424 image_interns_(image_interns),
1425 count_(0u) {}
1426
TestObject(ObjPtr<mirror::Object> referred_obj) const1427 void TestObject(ObjPtr<mirror::Object> referred_obj) const
1428 REQUIRES_SHARED(Locks::mutator_lock_) {
1429 if (referred_obj != nullptr &&
1430 space_.HasAddress(referred_obj.Ptr()) &&
1431 referred_obj->IsString()) {
1432 ObjPtr<mirror::String> referred_str = referred_obj->AsString();
1433 auto it = image_interns_.find(GcRoot<mirror::String>(referred_str));
1434 if (it != image_interns_.end() && it->Read() == referred_str) {
1435 ++count_;
1436 }
1437 }
1438 }
1439
VisitRootIfNonNull(mirror::CompressedReference<mirror::Object> * root) const1440 void VisitRootIfNonNull(
1441 mirror::CompressedReference<mirror::Object>* root) const
1442 REQUIRES_SHARED(Locks::mutator_lock_) {
1443 if (!root->IsNull()) {
1444 VisitRoot(root);
1445 }
1446 }
1447
VisitRoot(mirror::CompressedReference<mirror::Object> * root) const1448 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
1449 REQUIRES_SHARED(Locks::mutator_lock_) {
1450 TestObject(root->AsMirrorPtr());
1451 }
1452
1453 // Visit Class Fields
operator ()(ObjPtr<mirror::Object> obj,MemberOffset offset,bool is_static ATTRIBUTE_UNUSED) const1454 void operator()(ObjPtr<mirror::Object> obj,
1455 MemberOffset offset,
1456 bool is_static ATTRIBUTE_UNUSED) const
1457 REQUIRES_SHARED(Locks::mutator_lock_) {
1458 // References within image or across images don't need a read barrier.
1459 ObjPtr<mirror::Object> referred_obj =
1460 obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(offset);
1461 TestObject(referred_obj);
1462 }
1463
operator ()(ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED,ObjPtr<mirror::Reference> ref) const1464 void operator()(ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED,
1465 ObjPtr<mirror::Reference> ref) const
1466 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) {
1467 operator()(ref, mirror::Reference::ReferentOffset(), /*is_static=*/ false);
1468 }
1469
GetCount() const1470 size_t GetCount() const {
1471 return count_;
1472 }
1473
1474 private:
1475 const gc::space::ImageSpace& space_;
1476 const InternTable::UnorderedSet& image_interns_;
1477 mutable size_t count_; // Modified from the `const` callbacks.
1478 };
1479
1480 /*
1481 * This function counts references to strings interned in the AppImage.
1482 * This is used in debug build to check against the number of the recorded references.
1483 */
CountInternedStringReferences(gc::space::ImageSpace & space,const InternTable::UnorderedSet & image_interns)1484 size_t CountInternedStringReferences(gc::space::ImageSpace& space,
1485 const InternTable::UnorderedSet& image_interns)
1486 REQUIRES_SHARED(Locks::mutator_lock_) {
1487 const gc::accounting::ContinuousSpaceBitmap* bitmap = space.GetMarkBitmap();
1488 const ImageHeader& image_header = space.GetImageHeader();
1489 const uint8_t* target_base = space.GetMemMap()->Begin();
1490 const ImageSection& objects_section = image_header.GetObjectsSection();
1491
1492 auto objects_begin = reinterpret_cast<uintptr_t>(target_base + objects_section.Offset());
1493 auto objects_end = reinterpret_cast<uintptr_t>(target_base + objects_section.End());
1494
1495 CountInternedStringReferencesVisitor visitor(space, image_interns);
1496 bitmap->VisitMarkedRange(objects_begin,
1497 objects_end,
1498 [&space, &visitor](mirror::Object* obj)
1499 REQUIRES_SHARED(Locks::mutator_lock_) {
1500 if (space.HasAddress(obj)) {
1501 if (obj->IsDexCache()) {
1502 obj->VisitReferences</* kVisitNativeRoots= */ true,
1503 kVerifyNone,
1504 kWithoutReadBarrier>(visitor, visitor);
1505 } else {
1506 // Don't visit native roots for non-dex-cache as they can't contain
1507 // native references to strings. This is verified during compilation
1508 // by ImageWriter::VerifyNativeGCRootInvariants.
1509 obj->VisitReferences</* kVisitNativeRoots= */ false,
1510 kVerifyNone,
1511 kWithoutReadBarrier>(visitor, visitor);
1512 }
1513 }
1514 });
1515 return visitor.GetCount();
1516 }
1517
1518 template <typename Visitor>
VisitInternedStringReferences(gc::space::ImageSpace * space,bool use_preresolved_strings,const Visitor & visitor)1519 static void VisitInternedStringReferences(
1520 gc::space::ImageSpace* space,
1521 bool use_preresolved_strings,
1522 const Visitor& visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
1523 const uint8_t* target_base = space->Begin();
1524 const ImageSection& sro_section =
1525 space->GetImageHeader().GetImageStringReferenceOffsetsSection();
1526 const size_t num_string_offsets = sro_section.Size() / sizeof(AppImageReferenceOffsetInfo);
1527
1528 VLOG(image)
1529 << "ClassLinker:AppImage:InternStrings:imageStringReferenceOffsetCount = "
1530 << num_string_offsets;
1531
1532 const auto* sro_base =
1533 reinterpret_cast<const AppImageReferenceOffsetInfo*>(target_base + sro_section.Offset());
1534
1535 for (size_t offset_index = 0; offset_index < num_string_offsets; ++offset_index) {
1536 uint32_t base_offset = sro_base[offset_index].first;
1537
1538 if (HasDexCacheStringNativeRefTag(base_offset)) {
1539 base_offset = ClearDexCacheNativeRefTags(base_offset);
1540 DCHECK_ALIGNED(base_offset, 2);
1541
1542 ObjPtr<mirror::DexCache> dex_cache =
1543 reinterpret_cast<mirror::DexCache*>(space->Begin() + base_offset);
1544 uint32_t string_slot_index = sro_base[offset_index].second;
1545
1546 mirror::StringDexCachePair source =
1547 dex_cache->GetStrings()[string_slot_index].load(std::memory_order_relaxed);
1548 ObjPtr<mirror::String> referred_string = source.object.Read();
1549 DCHECK(referred_string != nullptr);
1550
1551 ObjPtr<mirror::String> visited = visitor(referred_string);
1552 if (visited != referred_string) {
1553 // Because we are not using a helper function we need to mark the GC card manually.
1554 WriteBarrier::ForEveryFieldWrite(dex_cache);
1555 dex_cache->GetStrings()[string_slot_index].store(
1556 mirror::StringDexCachePair(visited, source.index), std::memory_order_relaxed);
1557 }
1558 } else if (HasDexCachePreResolvedStringNativeRefTag(base_offset)) {
1559 if (use_preresolved_strings) {
1560 base_offset = ClearDexCacheNativeRefTags(base_offset);
1561 DCHECK_ALIGNED(base_offset, 2);
1562
1563 ObjPtr<mirror::DexCache> dex_cache =
1564 reinterpret_cast<mirror::DexCache*>(space->Begin() + base_offset);
1565 uint32_t string_index = sro_base[offset_index].second;
1566
1567 GcRoot<mirror::String>* preresolved_strings =
1568 dex_cache->GetPreResolvedStrings();
1569 // Handle calls to ClearPreResolvedStrings that might occur concurrently by the profile
1570 // saver that runs shortly after startup. In case the strings are cleared, there is nothing
1571 // to fix up.
1572 if (preresolved_strings != nullptr) {
1573 ObjPtr<mirror::String> referred_string =
1574 preresolved_strings[string_index].Read();
1575 if (referred_string != nullptr) {
1576 ObjPtr<mirror::String> visited = visitor(referred_string);
1577 if (visited != referred_string) {
1578 // Because we are not using a helper function we need to mark the GC card manually.
1579 WriteBarrier::ForEveryFieldWrite(dex_cache);
1580 preresolved_strings[string_index] = GcRoot<mirror::String>(visited);
1581 }
1582 }
1583 }
1584 }
1585 } else {
1586 uint32_t raw_member_offset = sro_base[offset_index].second;
1587 DCHECK_ALIGNED(base_offset, 2);
1588 DCHECK_ALIGNED(raw_member_offset, 2);
1589
1590 ObjPtr<mirror::Object> obj_ptr =
1591 reinterpret_cast<mirror::Object*>(space->Begin() + base_offset);
1592 MemberOffset member_offset(raw_member_offset);
1593 ObjPtr<mirror::String> referred_string =
1594 obj_ptr->GetFieldObject<mirror::String,
1595 kVerifyNone,
1596 kWithoutReadBarrier,
1597 /* kIsVolatile= */ false>(member_offset);
1598 DCHECK(referred_string != nullptr);
1599
1600 ObjPtr<mirror::String> visited = visitor(referred_string);
1601 if (visited != referred_string) {
1602 obj_ptr->SetFieldObject</* kTransactionActive= */ false,
1603 /* kCheckTransaction= */ false,
1604 kVerifyNone,
1605 /* kIsVolatile= */ false>(member_offset, visited);
1606 }
1607 }
1608 }
1609 }
1610
VerifyInternedStringReferences(gc::space::ImageSpace * space)1611 static void VerifyInternedStringReferences(gc::space::ImageSpace* space)
1612 REQUIRES_SHARED(Locks::mutator_lock_) {
1613 InternTable::UnorderedSet image_interns;
1614 const ImageSection& section = space->GetImageHeader().GetInternedStringsSection();
1615 if (section.Size() > 0) {
1616 size_t read_count;
1617 const uint8_t* data = space->Begin() + section.Offset();
1618 InternTable::UnorderedSet image_set(data, /*make_copy_of_data=*/ false, &read_count);
1619 image_set.swap(image_interns);
1620 }
1621 size_t num_recorded_refs = 0u;
1622 VisitInternedStringReferences(
1623 space,
1624 /*use_preresolved_strings=*/ true,
1625 [&image_interns, &num_recorded_refs](ObjPtr<mirror::String> str)
1626 REQUIRES_SHARED(Locks::mutator_lock_) {
1627 auto it = image_interns.find(GcRoot<mirror::String>(str));
1628 CHECK(it != image_interns.end());
1629 CHECK(it->Read() == str);
1630 ++num_recorded_refs;
1631 return str;
1632 });
1633 size_t num_found_refs = CountInternedStringReferences(*space, image_interns);
1634 CHECK_EQ(num_recorded_refs, num_found_refs);
1635 }
1636
1637 // new_class_set is the set of classes that were read from the class table section in the image.
1638 // If there was no class table section, it is null.
1639 // Note: using a class here to avoid having to make ClassLinker internals public.
1640 class AppImageLoadingHelper {
1641 public:
1642 static void Update(
1643 ClassLinker* class_linker,
1644 gc::space::ImageSpace* space,
1645 Handle<mirror::ClassLoader> class_loader,
1646 Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches,
1647 ClassTable::ClassSet* new_class_set)
1648 REQUIRES(!Locks::dex_lock_)
1649 REQUIRES_SHARED(Locks::mutator_lock_);
1650
1651 static void HandleAppImageStrings(gc::space::ImageSpace* space)
1652 REQUIRES_SHARED(Locks::mutator_lock_);
1653 };
1654
Update(ClassLinker * class_linker,gc::space::ImageSpace * space,Handle<mirror::ClassLoader> class_loader,Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches,ClassTable::ClassSet * new_class_set)1655 void AppImageLoadingHelper::Update(
1656 ClassLinker* class_linker,
1657 gc::space::ImageSpace* space,
1658 Handle<mirror::ClassLoader> class_loader,
1659 Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches,
1660 ClassTable::ClassSet* new_class_set)
1661 REQUIRES(!Locks::dex_lock_)
1662 REQUIRES_SHARED(Locks::mutator_lock_) {
1663 ScopedTrace app_image_timing("AppImage:Updating");
1664
1665 if (kIsDebugBuild && ClassLinker::kAppImageMayContainStrings) {
1666 // In debug build, verify the string references before applying
1667 // the Runtime::LoadAppImageStartupCache() option.
1668 VerifyInternedStringReferences(space);
1669 }
1670
1671 Thread* const self = Thread::Current();
1672 Runtime* const runtime = Runtime::Current();
1673 gc::Heap* const heap = runtime->GetHeap();
1674 const ImageHeader& header = space->GetImageHeader();
1675 bool load_app_image_startup_cache = runtime->LoadAppImageStartupCache();
1676 {
1677 // Register dex caches with the class loader.
1678 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
1679 for (auto dex_cache : dex_caches.Iterate<mirror::DexCache>()) {
1680 const DexFile* const dex_file = dex_cache->GetDexFile();
1681 {
1682 WriterMutexLock mu2(self, *Locks::dex_lock_);
1683 CHECK(class_linker->FindDexCacheDataLocked(*dex_file) == nullptr);
1684 class_linker->RegisterDexFileLocked(*dex_file, dex_cache, class_loader.Get());
1685 }
1686
1687 if (!load_app_image_startup_cache) {
1688 dex_cache->ClearPreResolvedStrings();
1689 }
1690
1691 if (kIsDebugBuild) {
1692 CHECK(new_class_set != nullptr);
1693 mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes();
1694 const size_t num_types = dex_cache->NumResolvedTypes();
1695 for (size_t j = 0; j != num_types; ++j) {
1696 // The image space is not yet added to the heap, avoid read barriers.
1697 ObjPtr<mirror::Class> klass = types[j].load(std::memory_order_relaxed).object.Read();
1698
1699 if (space->HasAddress(klass.Ptr())) {
1700 DCHECK(!klass->IsErroneous()) << klass->GetStatus();
1701 auto it = new_class_set->find(ClassTable::TableSlot(klass));
1702 DCHECK(it != new_class_set->end());
1703 DCHECK_EQ(it->Read(), klass);
1704 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
1705
1706 if (super_class != nullptr && !heap->ObjectIsInBootImageSpace(super_class)) {
1707 auto it2 = new_class_set->find(ClassTable::TableSlot(super_class));
1708 DCHECK(it2 != new_class_set->end());
1709 DCHECK_EQ(it2->Read(), super_class);
1710 }
1711
1712 for (ArtMethod& m : klass->GetDirectMethods(kRuntimePointerSize)) {
1713 const void* code = m.GetEntryPointFromQuickCompiledCode();
1714 const void* oat_code = m.IsInvokable() ? class_linker->GetQuickOatCodeFor(&m) : code;
1715 if (!class_linker->IsQuickResolutionStub(code) &&
1716 !class_linker->IsQuickGenericJniStub(code) &&
1717 !class_linker->IsQuickToInterpreterBridge(code) &&
1718 !m.IsNative()) {
1719 DCHECK_EQ(code, oat_code) << m.PrettyMethod();
1720 }
1721 }
1722
1723 for (ArtMethod& m : klass->GetVirtualMethods(kRuntimePointerSize)) {
1724 const void* code = m.GetEntryPointFromQuickCompiledCode();
1725 const void* oat_code = m.IsInvokable() ? class_linker->GetQuickOatCodeFor(&m) : code;
1726 if (!class_linker->IsQuickResolutionStub(code) &&
1727 !class_linker->IsQuickGenericJniStub(code) &&
1728 !class_linker->IsQuickToInterpreterBridge(code) &&
1729 !m.IsNative()) {
1730 DCHECK_EQ(code, oat_code) << m.PrettyMethod();
1731 }
1732 }
1733 }
1734 }
1735 }
1736 }
1737 }
1738
1739 if (ClassLinker::kAppImageMayContainStrings) {
1740 HandleAppImageStrings(space);
1741 }
1742
1743 if (kVerifyArtMethodDeclaringClasses) {
1744 ScopedTrace timing("AppImage:VerifyDeclaringClasses");
1745 ReaderMutexLock rmu(self, *Locks::heap_bitmap_lock_);
1746 gc::accounting::HeapBitmap* live_bitmap = heap->GetLiveBitmap();
1747 header.VisitPackedArtMethods([&](ArtMethod& method)
1748 REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
1749 ObjPtr<mirror::Class> klass = method.GetDeclaringClassUnchecked();
1750 if (klass != nullptr) {
1751 CHECK(live_bitmap->Test(klass.Ptr())) << "Image method has unmarked declaring class";
1752 }
1753 }, space->Begin(), kRuntimePointerSize);
1754 }
1755 }
1756
HandleAppImageStrings(gc::space::ImageSpace * space)1757 void AppImageLoadingHelper::HandleAppImageStrings(gc::space::ImageSpace* space) {
1758 // Iterate over the string reference offsets stored in the image and intern
1759 // the strings they point to.
1760 ScopedTrace timing("AppImage:InternString");
1761
1762 Runtime* const runtime = Runtime::Current();
1763 InternTable* const intern_table = runtime->GetInternTable();
1764
1765 const bool load_startup_cache = runtime->LoadAppImageStartupCache();
1766
1767 // Add the intern table, removing any conflicts. For conflicts, store the new address in a map
1768 // for faster lookup.
1769 // TODO: Optimize with a bitmap or bloom filter
1770 SafeMap<mirror::String*, mirror::String*> intern_remap;
1771 auto func = [&](InternTable::UnorderedSet& interns)
1772 REQUIRES_SHARED(Locks::mutator_lock_)
1773 REQUIRES(Locks::intern_table_lock_) {
1774 const size_t non_boot_image_strings = intern_table->CountInterns(
1775 /*visit_boot_images=*/false,
1776 /*visit_non_boot_images=*/true);
1777 VLOG(image) << "AppImage:stringsInInternTableSize = " << interns.size();
1778 VLOG(image) << "AppImage:nonBootImageInternStrings = " << non_boot_image_strings;
1779 // Visit the smaller of the two sets to compute the intersection.
1780 if (interns.size() < non_boot_image_strings) {
1781 for (auto it = interns.begin(); it != interns.end(); ) {
1782 ObjPtr<mirror::String> string = it->Read();
1783 ObjPtr<mirror::String> existing = intern_table->LookupWeakLocked(string);
1784 if (existing == nullptr) {
1785 existing = intern_table->LookupStrongLocked(string);
1786 }
1787 if (existing != nullptr) {
1788 intern_remap.Put(string.Ptr(), existing.Ptr());
1789 it = interns.erase(it);
1790 } else {
1791 ++it;
1792 }
1793 }
1794 } else {
1795 intern_table->VisitInterns([&](const GcRoot<mirror::String>& root)
1796 REQUIRES_SHARED(Locks::mutator_lock_)
1797 REQUIRES(Locks::intern_table_lock_) {
1798 auto it = interns.find(root);
1799 if (it != interns.end()) {
1800 ObjPtr<mirror::String> existing = root.Read();
1801 intern_remap.Put(it->Read(), existing.Ptr());
1802 it = interns.erase(it);
1803 }
1804 }, /*visit_boot_images=*/false, /*visit_non_boot_images=*/true);
1805 }
1806 // Consistency check to ensure correctness.
1807 if (kIsDebugBuild) {
1808 for (GcRoot<mirror::String>& root : interns) {
1809 ObjPtr<mirror::String> string = root.Read();
1810 CHECK(intern_table->LookupWeakLocked(string) == nullptr) << string->ToModifiedUtf8();
1811 CHECK(intern_table->LookupStrongLocked(string) == nullptr) << string->ToModifiedUtf8();
1812 }
1813 }
1814 };
1815 intern_table->AddImageStringsToTable(space, func);
1816 if (!intern_remap.empty()) {
1817 VLOG(image) << "AppImage:conflictingInternStrings = " << intern_remap.size();
1818 VisitInternedStringReferences(
1819 space,
1820 load_startup_cache,
1821 [&intern_remap](ObjPtr<mirror::String> str) REQUIRES_SHARED(Locks::mutator_lock_) {
1822 auto it = intern_remap.find(str.Ptr());
1823 if (it != intern_remap.end()) {
1824 return ObjPtr<mirror::String>(it->second);
1825 }
1826 return str;
1827 });
1828 }
1829 }
1830
OpenOatDexFile(const OatFile * oat_file,const char * location,std::string * error_msg)1831 static std::unique_ptr<const DexFile> OpenOatDexFile(const OatFile* oat_file,
1832 const char* location,
1833 std::string* error_msg)
1834 REQUIRES_SHARED(Locks::mutator_lock_) {
1835 DCHECK(error_msg != nullptr);
1836 std::unique_ptr<const DexFile> dex_file;
1837 const OatDexFile* oat_dex_file = oat_file->GetOatDexFile(location, nullptr, error_msg);
1838 if (oat_dex_file == nullptr) {
1839 return std::unique_ptr<const DexFile>();
1840 }
1841 std::string inner_error_msg;
1842 dex_file = oat_dex_file->OpenDexFile(&inner_error_msg);
1843 if (dex_file == nullptr) {
1844 *error_msg = StringPrintf("Failed to open dex file %s from within oat file %s error '%s'",
1845 location,
1846 oat_file->GetLocation().c_str(),
1847 inner_error_msg.c_str());
1848 return std::unique_ptr<const DexFile>();
1849 }
1850
1851 if (dex_file->GetLocationChecksum() != oat_dex_file->GetDexFileLocationChecksum()) {
1852 *error_msg = StringPrintf("Checksums do not match for %s: %x vs %x",
1853 location,
1854 dex_file->GetLocationChecksum(),
1855 oat_dex_file->GetDexFileLocationChecksum());
1856 return std::unique_ptr<const DexFile>();
1857 }
1858 return dex_file;
1859 }
1860
OpenImageDexFiles(gc::space::ImageSpace * space,std::vector<std::unique_ptr<const DexFile>> * out_dex_files,std::string * error_msg)1861 bool ClassLinker::OpenImageDexFiles(gc::space::ImageSpace* space,
1862 std::vector<std::unique_ptr<const DexFile>>* out_dex_files,
1863 std::string* error_msg) {
1864 ScopedAssertNoThreadSuspension nts(__FUNCTION__);
1865 const ImageHeader& header = space->GetImageHeader();
1866 ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches);
1867 DCHECK(dex_caches_object != nullptr);
1868 ObjPtr<mirror::ObjectArray<mirror::DexCache>> dex_caches =
1869 dex_caches_object->AsObjectArray<mirror::DexCache>();
1870 const OatFile* oat_file = space->GetOatFile();
1871 for (auto dex_cache : dex_caches->Iterate()) {
1872 std::string dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8());
1873 std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file,
1874 dex_file_location.c_str(),
1875 error_msg);
1876 if (dex_file == nullptr) {
1877 return false;
1878 }
1879 dex_cache->SetDexFile(dex_file.get());
1880 out_dex_files->push_back(std::move(dex_file));
1881 }
1882 return true;
1883 }
1884
1885 // Helper class for ArtMethod checks when adding an image. Keeps all required functionality
1886 // together and caches some intermediate results.
1887 class ImageChecker final {
1888 public:
CheckObjects(gc::Heap * heap,ClassLinker * class_linker)1889 static void CheckObjects(gc::Heap* heap, ClassLinker* class_linker)
1890 REQUIRES_SHARED(Locks::mutator_lock_) {
1891 ImageChecker ic(heap, class_linker);
1892 auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
1893 DCHECK(obj != nullptr);
1894 CHECK(obj->GetClass() != nullptr) << "Null class in object " << obj;
1895 CHECK(obj->GetClass()->GetClass() != nullptr) << "Null class class " << obj;
1896 if (obj->IsClass()) {
1897 auto klass = obj->AsClass();
1898 for (ArtField& field : klass->GetIFields()) {
1899 CHECK_EQ(field.GetDeclaringClass(), klass);
1900 }
1901 for (ArtField& field : klass->GetSFields()) {
1902 CHECK_EQ(field.GetDeclaringClass(), klass);
1903 }
1904 const PointerSize pointer_size = ic.pointer_size_;
1905 for (ArtMethod& m : klass->GetMethods(pointer_size)) {
1906 ic.CheckArtMethod(&m, klass);
1907 }
1908 ObjPtr<mirror::PointerArray> vtable = klass->GetVTable();
1909 if (vtable != nullptr) {
1910 ic.CheckArtMethodPointerArray(vtable, nullptr);
1911 }
1912 if (klass->ShouldHaveImt()) {
1913 ImTable* imt = klass->GetImt(pointer_size);
1914 for (size_t i = 0; i < ImTable::kSize; ++i) {
1915 ic.CheckArtMethod(imt->Get(i, pointer_size), nullptr);
1916 }
1917 }
1918 if (klass->ShouldHaveEmbeddedVTable()) {
1919 for (int32_t i = 0; i < klass->GetEmbeddedVTableLength(); ++i) {
1920 ic.CheckArtMethod(klass->GetEmbeddedVTableEntry(i, pointer_size), nullptr);
1921 }
1922 }
1923 ObjPtr<mirror::IfTable> iftable = klass->GetIfTable();
1924 for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) {
1925 if (iftable->GetMethodArrayCount(i) > 0) {
1926 ic.CheckArtMethodPointerArray(iftable->GetMethodArray(i), nullptr);
1927 }
1928 }
1929 }
1930 };
1931 heap->VisitObjects(visitor);
1932 }
1933
CheckArtMethodDexCacheArray(gc::Heap * heap,ClassLinker * class_linker,mirror::MethodDexCacheType * arr,size_t size)1934 static void CheckArtMethodDexCacheArray(gc::Heap* heap,
1935 ClassLinker* class_linker,
1936 mirror::MethodDexCacheType* arr,
1937 size_t size)
1938 REQUIRES_SHARED(Locks::mutator_lock_) {
1939 ImageChecker ic(heap, class_linker);
1940 ic.CheckArtMethodDexCacheArray(arr, size);
1941 }
1942
1943 private:
ImageChecker(gc::Heap * heap,ClassLinker * class_linker)1944 ImageChecker(gc::Heap* heap, ClassLinker* class_linker)
1945 : spaces_(heap->GetBootImageSpaces()),
1946 pointer_size_(class_linker->GetImagePointerSize()) {
1947 space_begin_.reserve(spaces_.size());
1948 method_sections_.reserve(spaces_.size());
1949 runtime_method_sections_.reserve(spaces_.size());
1950 for (gc::space::ImageSpace* space : spaces_) {
1951 space_begin_.push_back(space->Begin());
1952 auto& header = space->GetImageHeader();
1953 method_sections_.push_back(&header.GetMethodsSection());
1954 runtime_method_sections_.push_back(&header.GetRuntimeMethodsSection());
1955 }
1956 }
1957
CheckArtMethod(ArtMethod * m,ObjPtr<mirror::Class> expected_class)1958 void CheckArtMethod(ArtMethod* m, ObjPtr<mirror::Class> expected_class)
1959 REQUIRES_SHARED(Locks::mutator_lock_) {
1960 if (m->IsRuntimeMethod()) {
1961 ObjPtr<mirror::Class> declaring_class = m->GetDeclaringClassUnchecked();
1962 CHECK(declaring_class == nullptr) << declaring_class << " " << m->PrettyMethod();
1963 } else if (m->IsCopied()) {
1964 CHECK(m->GetDeclaringClass() != nullptr) << m->PrettyMethod();
1965 } else if (expected_class != nullptr) {
1966 CHECK_EQ(m->GetDeclaringClassUnchecked(), expected_class) << m->PrettyMethod();
1967 }
1968 if (!spaces_.empty()) {
1969 bool contains = false;
1970 for (size_t i = 0; !contains && i != space_begin_.size(); ++i) {
1971 const size_t offset = reinterpret_cast<uint8_t*>(m) - space_begin_[i];
1972 contains = method_sections_[i]->Contains(offset) ||
1973 runtime_method_sections_[i]->Contains(offset);
1974 }
1975 CHECK(contains) << m << " not found";
1976 }
1977 }
1978
CheckArtMethodPointerArray(ObjPtr<mirror::PointerArray> arr,ObjPtr<mirror::Class> expected_class)1979 void CheckArtMethodPointerArray(ObjPtr<mirror::PointerArray> arr,
1980 ObjPtr<mirror::Class> expected_class)
1981 REQUIRES_SHARED(Locks::mutator_lock_) {
1982 CHECK(arr != nullptr);
1983 for (int32_t j = 0; j < arr->GetLength(); ++j) {
1984 auto* method = arr->GetElementPtrSize<ArtMethod*>(j, pointer_size_);
1985 // expected_class == null means we are a dex cache.
1986 if (expected_class != nullptr) {
1987 CHECK(method != nullptr);
1988 }
1989 if (method != nullptr) {
1990 CheckArtMethod(method, expected_class);
1991 }
1992 }
1993 }
1994
CheckArtMethodDexCacheArray(mirror::MethodDexCacheType * arr,size_t size)1995 void CheckArtMethodDexCacheArray(mirror::MethodDexCacheType* arr, size_t size)
1996 REQUIRES_SHARED(Locks::mutator_lock_) {
1997 CHECK_EQ(arr != nullptr, size != 0u);
1998 if (arr != nullptr) {
1999 bool contains = false;
2000 for (auto space : spaces_) {
2001 auto offset = reinterpret_cast<uint8_t*>(arr) - space->Begin();
2002 if (space->GetImageHeader().GetDexCacheArraysSection().Contains(offset)) {
2003 contains = true;
2004 break;
2005 }
2006 }
2007 CHECK(contains);
2008 }
2009 for (size_t j = 0; j < size; ++j) {
2010 auto pair = mirror::DexCache::GetNativePairPtrSize(arr, j, pointer_size_);
2011 ArtMethod* method = pair.object;
2012 // expected_class == null means we are a dex cache.
2013 if (method != nullptr) {
2014 CheckArtMethod(method, nullptr);
2015 }
2016 }
2017 }
2018
2019 const std::vector<gc::space::ImageSpace*>& spaces_;
2020 const PointerSize pointer_size_;
2021
2022 // Cached sections from the spaces.
2023 std::vector<const uint8_t*> space_begin_;
2024 std::vector<const ImageSection*> method_sections_;
2025 std::vector<const ImageSection*> runtime_method_sections_;
2026 };
2027
VerifyAppImage(const ImageHeader & header,const Handle<mirror::ClassLoader> & class_loader,const Handle<mirror::ObjectArray<mirror::DexCache>> & dex_caches,ClassTable * class_table,gc::space::ImageSpace * space)2028 static void VerifyAppImage(const ImageHeader& header,
2029 const Handle<mirror::ClassLoader>& class_loader,
2030 const Handle<mirror::ObjectArray<mirror::DexCache> >& dex_caches,
2031 ClassTable* class_table, gc::space::ImageSpace* space)
2032 REQUIRES_SHARED(Locks::mutator_lock_) {
2033 header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) {
2034 ObjPtr<mirror::Class> klass = method.GetDeclaringClass();
2035 if (klass != nullptr && !Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) {
2036 CHECK_EQ(class_table->LookupByDescriptor(klass), klass)
2037 << mirror::Class::PrettyClass(klass);
2038 }
2039 }, space->Begin(), kRuntimePointerSize);
2040 {
2041 // Verify that all direct interfaces of classes in the class table are also resolved.
2042 std::vector<ObjPtr<mirror::Class>> classes;
2043 auto verify_direct_interfaces_in_table = [&](ObjPtr<mirror::Class> klass)
2044 REQUIRES_SHARED(Locks::mutator_lock_) {
2045 if (!klass->IsPrimitive() && klass->GetClassLoader() == class_loader.Get()) {
2046 classes.push_back(klass);
2047 }
2048 return true;
2049 };
2050 class_table->Visit(verify_direct_interfaces_in_table);
2051 Thread* self = Thread::Current();
2052 for (ObjPtr<mirror::Class> klass : classes) {
2053 for (uint32_t i = 0, num = klass->NumDirectInterfaces(); i != num; ++i) {
2054 CHECK(klass->GetDirectInterface(self, klass, i) != nullptr)
2055 << klass->PrettyDescriptor() << " iface #" << i;
2056 }
2057 }
2058 }
2059 // Check that all non-primitive classes in dex caches are also in the class table.
2060 for (auto dex_cache : dex_caches.ConstIterate<mirror::DexCache>()) {
2061 mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes();
2062 for (int32_t j = 0, num_types = dex_cache->NumResolvedTypes(); j < num_types; j++) {
2063 ObjPtr<mirror::Class> klass = types[j].load(std::memory_order_relaxed).object.Read();
2064 if (klass != nullptr && !klass->IsPrimitive()) {
2065 CHECK(class_table->Contains(klass))
2066 << klass->PrettyDescriptor() << " " << dex_cache->GetDexFile()->GetLocation();
2067 }
2068 }
2069 }
2070 }
2071
AddImageSpace(gc::space::ImageSpace * space,Handle<mirror::ClassLoader> class_loader,std::vector<std::unique_ptr<const DexFile>> * out_dex_files,std::string * error_msg)2072 bool ClassLinker::AddImageSpace(
2073 gc::space::ImageSpace* space,
2074 Handle<mirror::ClassLoader> class_loader,
2075 std::vector<std::unique_ptr<const DexFile>>* out_dex_files,
2076 std::string* error_msg) {
2077 DCHECK(out_dex_files != nullptr);
2078 DCHECK(error_msg != nullptr);
2079 const uint64_t start_time = NanoTime();
2080 const bool app_image = class_loader != nullptr;
2081 const ImageHeader& header = space->GetImageHeader();
2082 ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches);
2083 DCHECK(dex_caches_object != nullptr);
2084 Runtime* const runtime = Runtime::Current();
2085 gc::Heap* const heap = runtime->GetHeap();
2086 Thread* const self = Thread::Current();
2087 // Check that the image is what we are expecting.
2088 if (image_pointer_size_ != space->GetImageHeader().GetPointerSize()) {
2089 *error_msg = StringPrintf("Application image pointer size does not match runtime: %zu vs %zu",
2090 static_cast<size_t>(space->GetImageHeader().GetPointerSize()),
2091 image_pointer_size_);
2092 return false;
2093 }
2094 size_t expected_image_roots = ImageHeader::NumberOfImageRoots(app_image);
2095 if (static_cast<size_t>(header.GetImageRoots()->GetLength()) != expected_image_roots) {
2096 *error_msg = StringPrintf("Expected %zu image roots but got %d",
2097 expected_image_roots,
2098 header.GetImageRoots()->GetLength());
2099 return false;
2100 }
2101 StackHandleScope<3> hs(self);
2102 Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches(
2103 hs.NewHandle(dex_caches_object->AsObjectArray<mirror::DexCache>()));
2104 Handle<mirror::ObjectArray<mirror::Class>> class_roots(hs.NewHandle(
2105 header.GetImageRoot(ImageHeader::kClassRoots)->AsObjectArray<mirror::Class>()));
2106 MutableHandle<mirror::ClassLoader> image_class_loader(hs.NewHandle(
2107 app_image ? header.GetImageRoot(ImageHeader::kAppImageClassLoader)->AsClassLoader()
2108 : nullptr));
2109 DCHECK(class_roots != nullptr);
2110 if (class_roots->GetLength() != static_cast<int32_t>(ClassRoot::kMax)) {
2111 *error_msg = StringPrintf("Expected %d class roots but got %d",
2112 class_roots->GetLength(),
2113 static_cast<int32_t>(ClassRoot::kMax));
2114 return false;
2115 }
2116 // Check against existing class roots to make sure they match the ones in the boot image.
2117 ObjPtr<mirror::ObjectArray<mirror::Class>> existing_class_roots = GetClassRoots();
2118 for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); i++) {
2119 if (class_roots->Get(i) != GetClassRoot(static_cast<ClassRoot>(i), existing_class_roots)) {
2120 *error_msg = "App image class roots must have pointer equality with runtime ones.";
2121 return false;
2122 }
2123 }
2124 const OatFile* oat_file = space->GetOatFile();
2125 if (oat_file->GetOatHeader().GetDexFileCount() !=
2126 static_cast<uint32_t>(dex_caches->GetLength())) {
2127 *error_msg = "Dex cache count and dex file count mismatch while trying to initialize from "
2128 "image";
2129 return false;
2130 }
2131
2132 for (auto dex_cache : dex_caches.Iterate<mirror::DexCache>()) {
2133 std::string dex_file_location = dex_cache->GetLocation()->ToModifiedUtf8();
2134 std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file,
2135 dex_file_location.c_str(),
2136 error_msg);
2137 if (dex_file == nullptr) {
2138 return false;
2139 }
2140
2141 if (app_image) {
2142 // The current dex file field is bogus, overwrite it so that we can get the dex file in the
2143 // loop below.
2144 dex_cache->SetDexFile(dex_file.get());
2145 mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes();
2146 for (int32_t j = 0, num_types = dex_cache->NumResolvedTypes(); j < num_types; j++) {
2147 ObjPtr<mirror::Class> klass = types[j].load(std::memory_order_relaxed).object.Read();
2148 if (klass != nullptr) {
2149 DCHECK(!klass->IsErroneous()) << klass->GetStatus();
2150 }
2151 }
2152 } else {
2153 if (kCheckImageObjects) {
2154 ImageChecker::CheckArtMethodDexCacheArray(heap,
2155 this,
2156 dex_cache->GetResolvedMethods(),
2157 dex_cache->NumResolvedMethods());
2158 }
2159 // Register dex files, keep track of existing ones that are conflicts.
2160 AppendToBootClassPath(dex_file.get(), dex_cache);
2161 }
2162 out_dex_files->push_back(std::move(dex_file));
2163 }
2164
2165 if (app_image) {
2166 ScopedObjectAccessUnchecked soa(Thread::Current());
2167 ScopedAssertNoThreadSuspension sants("Checking app image", soa.Self());
2168 if (IsBootClassLoader(soa, image_class_loader.Get())) {
2169 *error_msg = "Unexpected BootClassLoader in app image";
2170 return false;
2171 }
2172 }
2173
2174 if (kCheckImageObjects) {
2175 for (auto dex_cache : dex_caches.Iterate<mirror::DexCache>()) {
2176 for (size_t j = 0; j < dex_cache->NumResolvedFields(); ++j) {
2177 auto* field = dex_cache->GetResolvedField(j, image_pointer_size_);
2178 if (field != nullptr) {
2179 CHECK(field->GetDeclaringClass()->GetClass() != nullptr);
2180 }
2181 }
2182 }
2183 if (!app_image) {
2184 ImageChecker::CheckObjects(heap, this);
2185 }
2186 }
2187
2188 // Set entry point to interpreter if in InterpretOnly mode.
2189 if (!runtime->IsAotCompiler() && runtime->GetInstrumentation()->InterpretOnly()) {
2190 // Set image methods' entry point to interpreter.
2191 header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) {
2192 if (!method.IsRuntimeMethod()) {
2193 DCHECK(method.GetDeclaringClass() != nullptr);
2194 if (!method.IsNative() && !method.IsResolutionMethod()) {
2195 method.SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(),
2196 image_pointer_size_);
2197 }
2198 }
2199 }, space->Begin(), image_pointer_size_);
2200 }
2201
2202 if (interpreter::CanRuntimeUseNterp()) {
2203 // Set image methods' entry point that point to the interpreter bridge to the nterp entry point.
2204 header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) {
2205 ChangeInterpreterBridgeToNterp(&method, this);
2206 }, space->Begin(), image_pointer_size_);
2207 }
2208
2209 if (runtime->IsVerificationSoftFail()) {
2210 header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) {
2211 if (!method.IsNative() && method.IsInvokable()) {
2212 method.ClearSkipAccessChecks();
2213 }
2214 }, space->Begin(), image_pointer_size_);
2215 }
2216
2217 ClassTable* class_table = nullptr;
2218 {
2219 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
2220 class_table = InsertClassTableForClassLoader(class_loader.Get());
2221 }
2222 // If we have a class table section, read it and use it for verification in
2223 // UpdateAppImageClassLoadersAndDexCaches.
2224 ClassTable::ClassSet temp_set;
2225 const ImageSection& class_table_section = header.GetClassTableSection();
2226 const bool added_class_table = class_table_section.Size() > 0u;
2227 if (added_class_table) {
2228 const uint64_t start_time2 = NanoTime();
2229 size_t read_count = 0;
2230 temp_set = ClassTable::ClassSet(space->Begin() + class_table_section.Offset(),
2231 /*make copy*/false,
2232 &read_count);
2233 VLOG(image) << "Adding class table classes took " << PrettyDuration(NanoTime() - start_time2);
2234 }
2235 if (app_image) {
2236 AppImageLoadingHelper::Update(this, space, class_loader, dex_caches, &temp_set);
2237
2238 {
2239 ScopedTrace trace("AppImage:UpdateClassLoaders");
2240 // Update class loader and resolved strings. If added_class_table is false, the resolved
2241 // strings were forwarded UpdateAppImageClassLoadersAndDexCaches.
2242 ObjPtr<mirror::ClassLoader> loader(class_loader.Get());
2243 for (const ClassTable::TableSlot& root : temp_set) {
2244 // Note: We probably don't need the read barrier unless we copy the app image objects into
2245 // the region space.
2246 ObjPtr<mirror::Class> klass(root.Read());
2247 // Do not update class loader for boot image classes where the app image
2248 // class loader is only the initiating loader but not the defining loader.
2249 // Avoid read barrier since we are comparing against null.
2250 if (klass->GetClassLoader<kDefaultVerifyFlags, kWithoutReadBarrier>() != nullptr) {
2251 klass->SetClassLoader(loader);
2252 }
2253 }
2254 }
2255
2256 if (kBitstringSubtypeCheckEnabled) {
2257 // Every class in the app image has initially SubtypeCheckInfo in the
2258 // Uninitialized state.
2259 //
2260 // The SubtypeCheck invariants imply that a SubtypeCheckInfo is at least Initialized
2261 // after class initialization is complete. The app image ClassStatus as-is
2262 // are almost all ClassStatus::Initialized, and being in the
2263 // SubtypeCheckInfo::kUninitialized state is violating that invariant.
2264 //
2265 // Force every app image class's SubtypeCheck to be at least kIninitialized.
2266 //
2267 // See also ImageWriter::FixupClass.
2268 ScopedTrace trace("AppImage:RecacluateSubtypeCheckBitstrings");
2269 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
2270 for (const ClassTable::TableSlot& root : temp_set) {
2271 SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(root.Read());
2272 }
2273 }
2274 }
2275 if (!oat_file->GetBssGcRoots().empty()) {
2276 // Insert oat file to class table for visiting .bss GC roots.
2277 class_table->InsertOatFile(oat_file);
2278 }
2279
2280 if (added_class_table) {
2281 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
2282 class_table->AddClassSet(std::move(temp_set));
2283 }
2284
2285 if (kIsDebugBuild && app_image) {
2286 // This verification needs to happen after the classes have been added to the class loader.
2287 // Since it ensures classes are in the class table.
2288 ScopedTrace trace("AppImage:Verify");
2289 VerifyAppImage(header, class_loader, dex_caches, class_table, space);
2290 }
2291
2292 VLOG(class_linker) << "Adding image space took " << PrettyDuration(NanoTime() - start_time);
2293 return true;
2294 }
2295
ClassInClassTable(ObjPtr<mirror::Class> klass)2296 bool ClassLinker::ClassInClassTable(ObjPtr<mirror::Class> klass) {
2297 ClassTable* const class_table = ClassTableForClassLoader(klass->GetClassLoader());
2298 return class_table != nullptr && class_table->Contains(klass);
2299 }
2300
VisitClassRoots(RootVisitor * visitor,VisitRootFlags flags)2301 void ClassLinker::VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags) {
2302 // Acquire tracing_enabled before locking class linker lock to prevent lock order violation. Since
2303 // enabling tracing requires the mutator lock, there are no race conditions here.
2304 const bool tracing_enabled = Trace::IsTracingEnabled();
2305 Thread* const self = Thread::Current();
2306 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
2307 if (kUseReadBarrier) {
2308 // We do not track new roots for CC.
2309 DCHECK_EQ(0, flags & (kVisitRootFlagNewRoots |
2310 kVisitRootFlagClearRootLog |
2311 kVisitRootFlagStartLoggingNewRoots |
2312 kVisitRootFlagStopLoggingNewRoots));
2313 }
2314 if ((flags & kVisitRootFlagAllRoots) != 0) {
2315 // Argument for how root visiting deals with ArtField and ArtMethod roots.
2316 // There is 3 GC cases to handle:
2317 // Non moving concurrent:
2318 // This case is easy to handle since the reference members of ArtMethod and ArtFields are held
2319 // live by the class and class roots.
2320 //
2321 // Moving non-concurrent:
2322 // This case needs to call visit VisitNativeRoots in case the classes or dex cache arrays move.
2323 // To prevent missing roots, this case needs to ensure that there is no
2324 // suspend points between the point which we allocate ArtMethod arrays and place them in a
2325 // class which is in the class table.
2326 //
2327 // Moving concurrent:
2328 // Need to make sure to not copy ArtMethods without doing read barriers since the roots are
2329 // marked concurrently and we don't hold the classlinker_classes_lock_ when we do the copy.
2330 //
2331 // Use an unbuffered visitor since the class table uses a temporary GcRoot for holding decoded
2332 // ClassTable::TableSlot. The buffered root visiting would access a stale stack location for
2333 // these objects.
2334 UnbufferedRootVisitor root_visitor(visitor, RootInfo(kRootStickyClass));
2335 boot_class_table_->VisitRoots(root_visitor);
2336 // If tracing is enabled, then mark all the class loaders to prevent unloading.
2337 if ((flags & kVisitRootFlagClassLoader) != 0 || tracing_enabled) {
2338 for (const ClassLoaderData& data : class_loaders_) {
2339 GcRoot<mirror::Object> root(GcRoot<mirror::Object>(self->DecodeJObject(data.weak_root)));
2340 root.VisitRoot(visitor, RootInfo(kRootVMInternal));
2341 }
2342 }
2343 } else if (!kUseReadBarrier && (flags & kVisitRootFlagNewRoots) != 0) {
2344 for (auto& root : new_class_roots_) {
2345 ObjPtr<mirror::Class> old_ref = root.Read<kWithoutReadBarrier>();
2346 root.VisitRoot(visitor, RootInfo(kRootStickyClass));
2347 ObjPtr<mirror::Class> new_ref = root.Read<kWithoutReadBarrier>();
2348 // Concurrent moving GC marked new roots through the to-space invariant.
2349 CHECK_EQ(new_ref, old_ref);
2350 }
2351 for (const OatFile* oat_file : new_bss_roots_boot_oat_files_) {
2352 for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) {
2353 ObjPtr<mirror::Object> old_ref = root.Read<kWithoutReadBarrier>();
2354 if (old_ref != nullptr) {
2355 DCHECK(old_ref->IsClass());
2356 root.VisitRoot(visitor, RootInfo(kRootStickyClass));
2357 ObjPtr<mirror::Object> new_ref = root.Read<kWithoutReadBarrier>();
2358 // Concurrent moving GC marked new roots through the to-space invariant.
2359 CHECK_EQ(new_ref, old_ref);
2360 }
2361 }
2362 }
2363 }
2364 if (!kUseReadBarrier && (flags & kVisitRootFlagClearRootLog) != 0) {
2365 new_class_roots_.clear();
2366 new_bss_roots_boot_oat_files_.clear();
2367 }
2368 if (!kUseReadBarrier && (flags & kVisitRootFlagStartLoggingNewRoots) != 0) {
2369 log_new_roots_ = true;
2370 } else if (!kUseReadBarrier && (flags & kVisitRootFlagStopLoggingNewRoots) != 0) {
2371 log_new_roots_ = false;
2372 }
2373 // We deliberately ignore the class roots in the image since we
2374 // handle image roots by using the MS/CMS rescanning of dirty cards.
2375 }
2376
2377 // Keep in sync with InitCallback. Anything we visit, we need to
2378 // reinit references to when reinitializing a ClassLinker from a
2379 // mapped image.
VisitRoots(RootVisitor * visitor,VisitRootFlags flags)2380 void ClassLinker::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) {
2381 class_roots_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal));
2382 VisitClassRoots(visitor, flags);
2383 // Instead of visiting the find_array_class_cache_ drop it so that it doesn't prevent class
2384 // unloading if we are marking roots.
2385 DropFindArrayClassCache();
2386 }
2387
2388 class VisitClassLoaderClassesVisitor : public ClassLoaderVisitor {
2389 public:
VisitClassLoaderClassesVisitor(ClassVisitor * visitor)2390 explicit VisitClassLoaderClassesVisitor(ClassVisitor* visitor)
2391 : visitor_(visitor),
2392 done_(false) {}
2393
Visit(ObjPtr<mirror::ClassLoader> class_loader)2394 void Visit(ObjPtr<mirror::ClassLoader> class_loader)
2395 REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override {
2396 ClassTable* const class_table = class_loader->GetClassTable();
2397 if (!done_ && class_table != nullptr) {
2398 DefiningClassLoaderFilterVisitor visitor(class_loader, visitor_);
2399 if (!class_table->Visit(visitor)) {
2400 // If the visitor ClassTable returns false it means that we don't need to continue.
2401 done_ = true;
2402 }
2403 }
2404 }
2405
2406 private:
2407 // Class visitor that limits the class visits from a ClassTable to the classes with
2408 // the provided defining class loader. This filter is used to avoid multiple visits
2409 // of the same class which can be recorded for multiple initiating class loaders.
2410 class DefiningClassLoaderFilterVisitor : public ClassVisitor {
2411 public:
DefiningClassLoaderFilterVisitor(ObjPtr<mirror::ClassLoader> defining_class_loader,ClassVisitor * visitor)2412 DefiningClassLoaderFilterVisitor(ObjPtr<mirror::ClassLoader> defining_class_loader,
2413 ClassVisitor* visitor)
2414 : defining_class_loader_(defining_class_loader), visitor_(visitor) { }
2415
operator ()(ObjPtr<mirror::Class> klass)2416 bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) {
2417 if (klass->GetClassLoader() != defining_class_loader_) {
2418 return true;
2419 }
2420 return (*visitor_)(klass);
2421 }
2422
2423 const ObjPtr<mirror::ClassLoader> defining_class_loader_;
2424 ClassVisitor* const visitor_;
2425 };
2426
2427 ClassVisitor* const visitor_;
2428 // If done is true then we don't need to do any more visiting.
2429 bool done_;
2430 };
2431
VisitClassesInternal(ClassVisitor * visitor)2432 void ClassLinker::VisitClassesInternal(ClassVisitor* visitor) {
2433 if (boot_class_table_->Visit(*visitor)) {
2434 VisitClassLoaderClassesVisitor loader_visitor(visitor);
2435 VisitClassLoaders(&loader_visitor);
2436 }
2437 }
2438
VisitClasses(ClassVisitor * visitor)2439 void ClassLinker::VisitClasses(ClassVisitor* visitor) {
2440 Thread* const self = Thread::Current();
2441 ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_);
2442 // Not safe to have thread suspension when we are holding a lock.
2443 if (self != nullptr) {
2444 ScopedAssertNoThreadSuspension nts(__FUNCTION__);
2445 VisitClassesInternal(visitor);
2446 } else {
2447 VisitClassesInternal(visitor);
2448 }
2449 }
2450
2451 class GetClassesInToVector : public ClassVisitor {
2452 public:
operator ()(ObjPtr<mirror::Class> klass)2453 bool operator()(ObjPtr<mirror::Class> klass) override {
2454 classes_.push_back(klass);
2455 return true;
2456 }
2457 std::vector<ObjPtr<mirror::Class>> classes_;
2458 };
2459
2460 class GetClassInToObjectArray : public ClassVisitor {
2461 public:
GetClassInToObjectArray(mirror::ObjectArray<mirror::Class> * arr)2462 explicit GetClassInToObjectArray(mirror::ObjectArray<mirror::Class>* arr)
2463 : arr_(arr), index_(0) {}
2464
operator ()(ObjPtr<mirror::Class> klass)2465 bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) {
2466 ++index_;
2467 if (index_ <= arr_->GetLength()) {
2468 arr_->Set(index_ - 1, klass);
2469 return true;
2470 }
2471 return false;
2472 }
2473
Succeeded() const2474 bool Succeeded() const REQUIRES_SHARED(Locks::mutator_lock_) {
2475 return index_ <= arr_->GetLength();
2476 }
2477
2478 private:
2479 mirror::ObjectArray<mirror::Class>* const arr_;
2480 int32_t index_;
2481 };
2482
VisitClassesWithoutClassesLock(ClassVisitor * visitor)2483 void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor) {
2484 // TODO: it may be possible to avoid secondary storage if we iterate over dex caches. The problem
2485 // is avoiding duplicates.
2486 if (!kMovingClasses) {
2487 ScopedAssertNoThreadSuspension nts(__FUNCTION__);
2488 GetClassesInToVector accumulator;
2489 VisitClasses(&accumulator);
2490 for (ObjPtr<mirror::Class> klass : accumulator.classes_) {
2491 if (!visitor->operator()(klass)) {
2492 return;
2493 }
2494 }
2495 } else {
2496 Thread* const self = Thread::Current();
2497 StackHandleScope<1> hs(self);
2498 auto classes = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr);
2499 // We size the array assuming classes won't be added to the class table during the visit.
2500 // If this assumption fails we iterate again.
2501 while (true) {
2502 size_t class_table_size;
2503 {
2504 ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_);
2505 // Add 100 in case new classes get loaded when we are filling in the object array.
2506 class_table_size = NumZygoteClasses() + NumNonZygoteClasses() + 100;
2507 }
2508 ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this);
2509 classes.Assign(
2510 mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, class_table_size));
2511 CHECK(classes != nullptr); // OOME.
2512 GetClassInToObjectArray accumulator(classes.Get());
2513 VisitClasses(&accumulator);
2514 if (accumulator.Succeeded()) {
2515 break;
2516 }
2517 }
2518 for (int32_t i = 0; i < classes->GetLength(); ++i) {
2519 // If the class table shrank during creation of the clases array we expect null elements. If
2520 // the class table grew then the loop repeats. If classes are created after the loop has
2521 // finished then we don't visit.
2522 ObjPtr<mirror::Class> klass = classes->Get(i);
2523 if (klass != nullptr && !visitor->operator()(klass)) {
2524 return;
2525 }
2526 }
2527 }
2528 }
2529
~ClassLinker()2530 ClassLinker::~ClassLinker() {
2531 Thread* const self = Thread::Current();
2532 for (const ClassLoaderData& data : class_loaders_) {
2533 // CHA unloading analysis is not needed. No negative consequences are expected because
2534 // all the classloaders are deleted at the same time.
2535 DeleteClassLoader(self, data, /*cleanup_cha=*/ false);
2536 }
2537 class_loaders_.clear();
2538 while (!running_visibly_initialized_callbacks_.empty()) {
2539 std::unique_ptr<VisiblyInitializedCallback> callback(
2540 std::addressof(running_visibly_initialized_callbacks_.front()));
2541 running_visibly_initialized_callbacks_.pop_front();
2542 }
2543 }
2544
DeleteClassLoader(Thread * self,const ClassLoaderData & data,bool cleanup_cha)2545 void ClassLinker::DeleteClassLoader(Thread* self, const ClassLoaderData& data, bool cleanup_cha) {
2546 Runtime* const runtime = Runtime::Current();
2547 JavaVMExt* const vm = runtime->GetJavaVM();
2548 vm->DeleteWeakGlobalRef(self, data.weak_root);
2549 // Notify the JIT that we need to remove the methods and/or profiling info.
2550 if (runtime->GetJit() != nullptr) {
2551 jit::JitCodeCache* code_cache = runtime->GetJit()->GetCodeCache();
2552 if (code_cache != nullptr) {
2553 // For the JIT case, RemoveMethodsIn removes the CHA dependencies.
2554 code_cache->RemoveMethodsIn(self, *data.allocator);
2555 }
2556 } else if (cha_ != nullptr) {
2557 // If we don't have a JIT, we need to manually remove the CHA dependencies manually.
2558 cha_->RemoveDependenciesForLinearAlloc(data.allocator);
2559 }
2560 // Cleanup references to single implementation ArtMethods that will be deleted.
2561 if (cleanup_cha) {
2562 CHAOnDeleteUpdateClassVisitor visitor(data.allocator);
2563 data.class_table->Visit<CHAOnDeleteUpdateClassVisitor, kWithoutReadBarrier>(visitor);
2564 }
2565 {
2566 MutexLock lock(self, critical_native_code_with_clinit_check_lock_);
2567 auto end = critical_native_code_with_clinit_check_.end();
2568 for (auto it = critical_native_code_with_clinit_check_.begin(); it != end; ) {
2569 if (data.allocator->ContainsUnsafe(it->first)) {
2570 it = critical_native_code_with_clinit_check_.erase(it);
2571 } else {
2572 ++it;
2573 }
2574 }
2575 }
2576
2577 delete data.allocator;
2578 delete data.class_table;
2579 }
2580
AllocPointerArray(Thread * self,size_t length)2581 ObjPtr<mirror::PointerArray> ClassLinker::AllocPointerArray(Thread* self, size_t length) {
2582 return ObjPtr<mirror::PointerArray>::DownCast(
2583 image_pointer_size_ == PointerSize::k64
2584 ? ObjPtr<mirror::Array>(mirror::LongArray::Alloc(self, length))
2585 : ObjPtr<mirror::Array>(mirror::IntArray::Alloc(self, length)));
2586 }
2587
AllocDexCache(ObjPtr<mirror::String> * out_location,Thread * self,const DexFile & dex_file)2588 ObjPtr<mirror::DexCache> ClassLinker::AllocDexCache(/*out*/ ObjPtr<mirror::String>* out_location,
2589 Thread* self,
2590 const DexFile& dex_file) {
2591 StackHandleScope<1> hs(self);
2592 DCHECK(out_location != nullptr);
2593 auto dex_cache(hs.NewHandle(ObjPtr<mirror::DexCache>::DownCast(
2594 GetClassRoot<mirror::DexCache>(this)->AllocObject(self))));
2595 if (dex_cache == nullptr) {
2596 self->AssertPendingOOMException();
2597 return nullptr;
2598 }
2599 // Use InternWeak() so that the location String can be collected when the ClassLoader
2600 // with this DexCache is collected.
2601 ObjPtr<mirror::String> location = intern_table_->InternWeak(dex_file.GetLocation().c_str());
2602 if (location == nullptr) {
2603 self->AssertPendingOOMException();
2604 return nullptr;
2605 }
2606 *out_location = location;
2607 return dex_cache.Get();
2608 }
2609
AllocAndInitializeDexCache(Thread * self,const DexFile & dex_file,LinearAlloc * linear_alloc)2610 ObjPtr<mirror::DexCache> ClassLinker::AllocAndInitializeDexCache(Thread* self,
2611 const DexFile& dex_file,
2612 LinearAlloc* linear_alloc) {
2613 ObjPtr<mirror::String> location = nullptr;
2614 ObjPtr<mirror::DexCache> dex_cache = AllocDexCache(&location, self, dex_file);
2615 if (dex_cache != nullptr) {
2616 WriterMutexLock mu(self, *Locks::dex_lock_);
2617 DCHECK(location != nullptr);
2618 mirror::DexCache::InitializeDexCache(self,
2619 dex_cache,
2620 location,
2621 &dex_file,
2622 linear_alloc,
2623 image_pointer_size_);
2624 }
2625 return dex_cache;
2626 }
2627
2628 template <bool kMovable, typename PreFenceVisitor>
AllocClass(Thread * self,ObjPtr<mirror::Class> java_lang_Class,uint32_t class_size,const PreFenceVisitor & pre_fence_visitor)2629 ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self,
2630 ObjPtr<mirror::Class> java_lang_Class,
2631 uint32_t class_size,
2632 const PreFenceVisitor& pre_fence_visitor) {
2633 DCHECK_GE(class_size, sizeof(mirror::Class));
2634 gc::Heap* heap = Runtime::Current()->GetHeap();
2635 ObjPtr<mirror::Object> k = (kMovingClasses && kMovable) ?
2636 heap->AllocObject(self, java_lang_Class, class_size, pre_fence_visitor) :
2637 heap->AllocNonMovableObject(self, java_lang_Class, class_size, pre_fence_visitor);
2638 if (UNLIKELY(k == nullptr)) {
2639 self->AssertPendingOOMException();
2640 return nullptr;
2641 }
2642 return k->AsClass();
2643 }
2644
2645 template <bool kMovable>
AllocClass(Thread * self,ObjPtr<mirror::Class> java_lang_Class,uint32_t class_size)2646 ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self,
2647 ObjPtr<mirror::Class> java_lang_Class,
2648 uint32_t class_size) {
2649 mirror::Class::InitializeClassVisitor visitor(class_size);
2650 return AllocClass<kMovable>(self, java_lang_Class, class_size, visitor);
2651 }
2652
AllocClass(Thread * self,uint32_t class_size)2653 ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self, uint32_t class_size) {
2654 return AllocClass(self, GetClassRoot<mirror::Class>(this), class_size);
2655 }
2656
AllocPrimitiveArrayClass(Thread * self,ClassRoot primitive_root,ClassRoot array_root)2657 void ClassLinker::AllocPrimitiveArrayClass(Thread* self,
2658 ClassRoot primitive_root,
2659 ClassRoot array_root) {
2660 // We make this class non-movable for the unlikely case where it were to be
2661 // moved by a sticky-bit (minor) collection when using the Generational
2662 // Concurrent Copying (CC) collector, potentially creating a stale reference
2663 // in the `klass_` field of one of its instances allocated in the Large-Object
2664 // Space (LOS) -- see the comment about the dirty card scanning logic in
2665 // art::gc::collector::ConcurrentCopying::MarkingPhase.
2666 ObjPtr<mirror::Class> array_class = AllocClass</* kMovable= */ false>(
2667 self, GetClassRoot<mirror::Class>(this), mirror::Array::ClassSize(image_pointer_size_));
2668 ObjPtr<mirror::Class> component_type = GetClassRoot(primitive_root, this);
2669 DCHECK(component_type->IsPrimitive());
2670 array_class->SetComponentType(component_type);
2671 SetClassRoot(array_root, array_class);
2672 }
2673
FinishArrayClassSetup(ObjPtr<mirror::Class> array_class)2674 void ClassLinker::FinishArrayClassSetup(ObjPtr<mirror::Class> array_class) {
2675 ObjPtr<mirror::Class> java_lang_Object = GetClassRoot<mirror::Object>(this);
2676 array_class->SetSuperClass(java_lang_Object);
2677 array_class->SetVTable(java_lang_Object->GetVTable());
2678 array_class->SetPrimitiveType(Primitive::kPrimNot);
2679 ObjPtr<mirror::Class> component_type = array_class->GetComponentType();
2680 array_class->SetClassFlags(component_type->IsPrimitive()
2681 ? mirror::kClassFlagNoReferenceFields
2682 : mirror::kClassFlagObjectArray);
2683 array_class->SetClassLoader(component_type->GetClassLoader());
2684 array_class->SetStatusForPrimitiveOrArray(ClassStatus::kLoaded);
2685 array_class->PopulateEmbeddedVTable(image_pointer_size_);
2686 ImTable* object_imt = java_lang_Object->GetImt(image_pointer_size_);
2687 array_class->SetImt(object_imt, image_pointer_size_);
2688 // Skip EnsureSkipAccessChecksMethods(). We can skip the verified status,
2689 // the kAccVerificationAttempted flag is added below, and there are no
2690 // methods that need the kAccSkipAccessChecks flag.
2691 DCHECK_EQ(array_class->NumMethods(), 0u);
2692
2693 // don't need to set new_class->SetObjectSize(..)
2694 // because Object::SizeOf delegates to Array::SizeOf
2695
2696 // All arrays have java/lang/Cloneable and java/io/Serializable as
2697 // interfaces. We need to set that up here, so that stuff like
2698 // "instanceof" works right.
2699
2700 // Use the single, global copies of "interfaces" and "iftable"
2701 // (remember not to free them for arrays).
2702 {
2703 ObjPtr<mirror::IfTable> array_iftable = GetArrayIfTable();
2704 CHECK(array_iftable != nullptr);
2705 array_class->SetIfTable(array_iftable);
2706 }
2707
2708 // Inherit access flags from the component type.
2709 int access_flags = component_type->GetAccessFlags();
2710 // Lose any implementation detail flags; in particular, arrays aren't finalizable.
2711 access_flags &= kAccJavaFlagsMask;
2712 // Arrays can't be used as a superclass or interface, so we want to add "abstract final"
2713 // and remove "interface".
2714 access_flags |= kAccAbstract | kAccFinal;
2715 access_flags &= ~kAccInterface;
2716 // Arrays are access-checks-clean and preverified.
2717 access_flags |= kAccVerificationAttempted;
2718
2719 array_class->SetAccessFlagsDuringLinking(access_flags);
2720
2721 // Array classes are fully initialized either during single threaded startup,
2722 // or from a pre-fence visitor, so visibly initialized.
2723 array_class->SetStatusForPrimitiveOrArray(ClassStatus::kVisiblyInitialized);
2724 }
2725
FinishCoreArrayClassSetup(ClassRoot array_root)2726 void ClassLinker::FinishCoreArrayClassSetup(ClassRoot array_root) {
2727 // Do not hold lock on the array class object, the initialization of
2728 // core array classes is done while the process is still single threaded.
2729 ObjPtr<mirror::Class> array_class = GetClassRoot(array_root, this);
2730 FinishArrayClassSetup(array_class);
2731
2732 std::string temp;
2733 const char* descriptor = array_class->GetDescriptor(&temp);
2734 size_t hash = ComputeModifiedUtf8Hash(descriptor);
2735 ObjPtr<mirror::Class> existing = InsertClass(descriptor, array_class, hash);
2736 CHECK(existing == nullptr);
2737 }
2738
AllocStackTraceElementArray(Thread * self,size_t length)2739 ObjPtr<mirror::ObjectArray<mirror::StackTraceElement>> ClassLinker::AllocStackTraceElementArray(
2740 Thread* self,
2741 size_t length) {
2742 return mirror::ObjectArray<mirror::StackTraceElement>::Alloc(
2743 self, GetClassRoot<mirror::ObjectArray<mirror::StackTraceElement>>(this), length);
2744 }
2745
EnsureResolved(Thread * self,const char * descriptor,ObjPtr<mirror::Class> klass)2746 ObjPtr<mirror::Class> ClassLinker::EnsureResolved(Thread* self,
2747 const char* descriptor,
2748 ObjPtr<mirror::Class> klass) {
2749 DCHECK(klass != nullptr);
2750 if (kIsDebugBuild) {
2751 StackHandleScope<1> hs(self);
2752 HandleWrapperObjPtr<mirror::Class> h = hs.NewHandleWrapper(&klass);
2753 Thread::PoisonObjectPointersIfDebug();
2754 }
2755
2756 // For temporary classes we must wait for them to be retired.
2757 if (init_done_ && klass->IsTemp()) {
2758 CHECK(!klass->IsResolved());
2759 if (klass->IsErroneousUnresolved()) {
2760 ThrowEarlierClassFailure(klass);
2761 return nullptr;
2762 }
2763 StackHandleScope<1> hs(self);
2764 Handle<mirror::Class> h_class(hs.NewHandle(klass));
2765 ObjectLock<mirror::Class> lock(self, h_class);
2766 // Loop and wait for the resolving thread to retire this class.
2767 while (!h_class->IsRetired() && !h_class->IsErroneousUnresolved()) {
2768 lock.WaitIgnoringInterrupts();
2769 }
2770 if (h_class->IsErroneousUnresolved()) {
2771 ThrowEarlierClassFailure(h_class.Get());
2772 return nullptr;
2773 }
2774 CHECK(h_class->IsRetired());
2775 // Get the updated class from class table.
2776 klass = LookupClass(self, descriptor, h_class.Get()->GetClassLoader());
2777 }
2778
2779 // Wait for the class if it has not already been linked.
2780 size_t index = 0;
2781 // Maximum number of yield iterations until we start sleeping.
2782 static const size_t kNumYieldIterations = 1000;
2783 // How long each sleep is in us.
2784 static const size_t kSleepDurationUS = 1000; // 1 ms.
2785 while (!klass->IsResolved() && !klass->IsErroneousUnresolved()) {
2786 StackHandleScope<1> hs(self);
2787 HandleWrapperObjPtr<mirror::Class> h_class(hs.NewHandleWrapper(&klass));
2788 {
2789 ObjectTryLock<mirror::Class> lock(self, h_class);
2790 // Can not use a monitor wait here since it may block when returning and deadlock if another
2791 // thread has locked klass.
2792 if (lock.Acquired()) {
2793 // Check for circular dependencies between classes, the lock is required for SetStatus.
2794 if (!h_class->IsResolved() && h_class->GetClinitThreadId() == self->GetTid()) {
2795 ThrowClassCircularityError(h_class.Get());
2796 mirror::Class::SetStatus(h_class, ClassStatus::kErrorUnresolved, self);
2797 return nullptr;
2798 }
2799 }
2800 }
2801 {
2802 // Handle wrapper deals with klass moving.
2803 ScopedThreadSuspension sts(self, kSuspended);
2804 if (index < kNumYieldIterations) {
2805 sched_yield();
2806 } else {
2807 usleep(kSleepDurationUS);
2808 }
2809 }
2810 ++index;
2811 }
2812
2813 if (klass->IsErroneousUnresolved()) {
2814 ThrowEarlierClassFailure(klass);
2815 return nullptr;
2816 }
2817 // Return the loaded class. No exceptions should be pending.
2818 CHECK(klass->IsResolved()) << klass->PrettyClass();
2819 self->AssertNoPendingException();
2820 return klass;
2821 }
2822
2823 using ClassPathEntry = std::pair<const DexFile*, const dex::ClassDef*>;
2824
2825 // Search a collection of DexFiles for a descriptor
FindInClassPath(const char * descriptor,size_t hash,const std::vector<const DexFile * > & class_path)2826 ClassPathEntry FindInClassPath(const char* descriptor,
2827 size_t hash, const std::vector<const DexFile*>& class_path) {
2828 for (const DexFile* dex_file : class_path) {
2829 DCHECK(dex_file != nullptr);
2830 const dex::ClassDef* dex_class_def = OatDexFile::FindClassDef(*dex_file, descriptor, hash);
2831 if (dex_class_def != nullptr) {
2832 return ClassPathEntry(dex_file, dex_class_def);
2833 }
2834 }
2835 return ClassPathEntry(nullptr, nullptr);
2836 }
2837
FindClassInSharedLibraries(ScopedObjectAccessAlreadyRunnable & soa,Thread * self,const char * descriptor,size_t hash,Handle<mirror::ClassLoader> class_loader,ObjPtr<mirror::Class> * result)2838 bool ClassLinker::FindClassInSharedLibraries(ScopedObjectAccessAlreadyRunnable& soa,
2839 Thread* self,
2840 const char* descriptor,
2841 size_t hash,
2842 Handle<mirror::ClassLoader> class_loader,
2843 /*out*/ ObjPtr<mirror::Class>* result) {
2844 ArtField* field =
2845 jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders);
2846 ObjPtr<mirror::Object> raw_shared_libraries = field->GetObject(class_loader.Get());
2847 if (raw_shared_libraries == nullptr) {
2848 return true;
2849 }
2850
2851 StackHandleScope<2> hs(self);
2852 Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries(
2853 hs.NewHandle(raw_shared_libraries->AsObjectArray<mirror::ClassLoader>()));
2854 MutableHandle<mirror::ClassLoader> temp_loader = hs.NewHandle<mirror::ClassLoader>(nullptr);
2855 for (auto loader : shared_libraries.Iterate<mirror::ClassLoader>()) {
2856 temp_loader.Assign(loader);
2857 if (!FindClassInBaseDexClassLoader(soa, self, descriptor, hash, temp_loader, result)) {
2858 return false; // One of the shared libraries is not supported.
2859 }
2860 if (*result != nullptr) {
2861 return true; // Found the class up the chain.
2862 }
2863 }
2864 return true;
2865 }
2866
FindClassInBaseDexClassLoader(ScopedObjectAccessAlreadyRunnable & soa,Thread * self,const char * descriptor,size_t hash,Handle<mirror::ClassLoader> class_loader,ObjPtr<mirror::Class> * result)2867 bool ClassLinker::FindClassInBaseDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
2868 Thread* self,
2869 const char* descriptor,
2870 size_t hash,
2871 Handle<mirror::ClassLoader> class_loader,
2872 /*out*/ ObjPtr<mirror::Class>* result) {
2873 // Termination case: boot class loader.
2874 if (IsBootClassLoader(soa, class_loader.Get())) {
2875 *result = FindClassInBootClassLoaderClassPath(self, descriptor, hash);
2876 return true;
2877 }
2878
2879 if (IsPathOrDexClassLoader(soa, class_loader) || IsInMemoryDexClassLoader(soa, class_loader)) {
2880 // For regular path or dex class loader the search order is:
2881 // - parent
2882 // - shared libraries
2883 // - class loader dex files
2884
2885 // Handles as RegisterDexFile may allocate dex caches (and cause thread suspension).
2886 StackHandleScope<1> hs(self);
2887 Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent()));
2888 if (!FindClassInBaseDexClassLoader(soa, self, descriptor, hash, h_parent, result)) {
2889 return false; // One of the parents is not supported.
2890 }
2891 if (*result != nullptr) {
2892 return true; // Found the class up the chain.
2893 }
2894
2895 if (!FindClassInSharedLibraries(soa, self, descriptor, hash, class_loader, result)) {
2896 return false; // One of the shared library loader is not supported.
2897 }
2898 if (*result != nullptr) {
2899 return true; // Found the class in a shared library.
2900 }
2901
2902 // Search the current class loader classpath.
2903 *result = FindClassInBaseDexClassLoaderClassPath(soa, descriptor, hash, class_loader);
2904 return !soa.Self()->IsExceptionPending();
2905 }
2906
2907 if (IsDelegateLastClassLoader(soa, class_loader)) {
2908 // For delegate last, the search order is:
2909 // - boot class path
2910 // - shared libraries
2911 // - class loader dex files
2912 // - parent
2913 *result = FindClassInBootClassLoaderClassPath(self, descriptor, hash);
2914 if (*result != nullptr) {
2915 return true; // The class is part of the boot class path.
2916 }
2917 if (self->IsExceptionPending()) {
2918 // Pending exception means there was an error other than ClassNotFound that must be returned
2919 // to the caller.
2920 return false;
2921 }
2922
2923 if (!FindClassInSharedLibraries(soa, self, descriptor, hash, class_loader, result)) {
2924 return false; // One of the shared library loader is not supported.
2925 }
2926 if (*result != nullptr) {
2927 return true; // Found the class in a shared library.
2928 }
2929
2930 *result = FindClassInBaseDexClassLoaderClassPath(soa, descriptor, hash, class_loader);
2931 if (*result != nullptr) {
2932 return true; // Found the class in the current class loader
2933 }
2934 if (self->IsExceptionPending()) {
2935 // Pending exception means there was an error other than ClassNotFound that must be returned
2936 // to the caller.
2937 return false;
2938 }
2939
2940 // Handles as RegisterDexFile may allocate dex caches (and cause thread suspension).
2941 StackHandleScope<1> hs(self);
2942 Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent()));
2943 return FindClassInBaseDexClassLoader(soa, self, descriptor, hash, h_parent, result);
2944 }
2945
2946 // Unsupported class loader.
2947 *result = nullptr;
2948 return false;
2949 }
2950
2951 namespace {
2952
2953 // Matches exceptions caught in DexFile.defineClass.
MatchesDexFileCaughtExceptions(ObjPtr<mirror::Throwable> throwable,ClassLinker * class_linker)2954 ALWAYS_INLINE bool MatchesDexFileCaughtExceptions(ObjPtr<mirror::Throwable> throwable,
2955 ClassLinker* class_linker)
2956 REQUIRES_SHARED(Locks::mutator_lock_) {
2957 return
2958 // ClassNotFoundException.
2959 throwable->InstanceOf(GetClassRoot(ClassRoot::kJavaLangClassNotFoundException,
2960 class_linker))
2961 ||
2962 // NoClassDefFoundError. TODO: Reconsider this. b/130746382.
2963 throwable->InstanceOf(Runtime::Current()->GetPreAllocatedNoClassDefFoundError()->GetClass());
2964 }
2965
2966 // Clear exceptions caught in DexFile.defineClass.
FilterDexFileCaughtExceptions(Thread * self,ClassLinker * class_linker)2967 ALWAYS_INLINE void FilterDexFileCaughtExceptions(Thread* self, ClassLinker* class_linker)
2968 REQUIRES_SHARED(Locks::mutator_lock_) {
2969 if (MatchesDexFileCaughtExceptions(self->GetException(), class_linker)) {
2970 self->ClearException();
2971 }
2972 }
2973
2974 } // namespace
2975
2976 // Finds the class in the boot class loader.
2977 // If the class is found the method returns the resolved class. Otherwise it returns null.
FindClassInBootClassLoaderClassPath(Thread * self,const char * descriptor,size_t hash)2978 ObjPtr<mirror::Class> ClassLinker::FindClassInBootClassLoaderClassPath(Thread* self,
2979 const char* descriptor,
2980 size_t hash) {
2981 ObjPtr<mirror::Class> result = nullptr;
2982 ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_);
2983 if (pair.second != nullptr) {
2984 ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, nullptr);
2985 if (klass != nullptr) {
2986 result = EnsureResolved(self, descriptor, klass);
2987 } else {
2988 result = DefineClass(self,
2989 descriptor,
2990 hash,
2991 ScopedNullHandle<mirror::ClassLoader>(),
2992 *pair.first,
2993 *pair.second);
2994 }
2995 if (result == nullptr) {
2996 CHECK(self->IsExceptionPending()) << descriptor;
2997 FilterDexFileCaughtExceptions(self, this);
2998 }
2999 }
3000 return result;
3001 }
3002
FindClassInBaseDexClassLoaderClassPath(ScopedObjectAccessAlreadyRunnable & soa,const char * descriptor,size_t hash,Handle<mirror::ClassLoader> class_loader)3003 ObjPtr<mirror::Class> ClassLinker::FindClassInBaseDexClassLoaderClassPath(
3004 ScopedObjectAccessAlreadyRunnable& soa,
3005 const char* descriptor,
3006 size_t hash,
3007 Handle<mirror::ClassLoader> class_loader) {
3008 DCHECK(IsPathOrDexClassLoader(soa, class_loader) ||
3009 IsInMemoryDexClassLoader(soa, class_loader) ||
3010 IsDelegateLastClassLoader(soa, class_loader))
3011 << "Unexpected class loader for descriptor " << descriptor;
3012
3013 ObjPtr<mirror::Class> ret;
3014 auto define_class = [&](const DexFile* cp_dex_file) REQUIRES_SHARED(Locks::mutator_lock_) {
3015 const dex::ClassDef* dex_class_def = OatDexFile::FindClassDef(*cp_dex_file, descriptor, hash);
3016 if (dex_class_def != nullptr) {
3017 ObjPtr<mirror::Class> klass = DefineClass(soa.Self(),
3018 descriptor,
3019 hash,
3020 class_loader,
3021 *cp_dex_file,
3022 *dex_class_def);
3023 if (klass == nullptr) {
3024 CHECK(soa.Self()->IsExceptionPending()) << descriptor;
3025 FilterDexFileCaughtExceptions(soa.Self(), this);
3026 // TODO: Is it really right to break here, and not check the other dex files?
3027 } else {
3028 DCHECK(!soa.Self()->IsExceptionPending());
3029 }
3030 ret = klass;
3031 return false; // Found a Class (or error == nullptr), stop visit.
3032 }
3033 return true; // Continue with the next DexFile.
3034 };
3035
3036 VisitClassLoaderDexFiles(soa, class_loader, define_class);
3037 return ret;
3038 }
3039
FindClass(Thread * self,const char * descriptor,Handle<mirror::ClassLoader> class_loader)3040 ObjPtr<mirror::Class> ClassLinker::FindClass(Thread* self,
3041 const char* descriptor,
3042 Handle<mirror::ClassLoader> class_loader) {
3043 DCHECK_NE(*descriptor, '\0') << "descriptor is empty string";
3044 DCHECK(self != nullptr);
3045 self->AssertNoPendingException();
3046 self->PoisonObjectPointers(); // For DefineClass, CreateArrayClass, etc...
3047 if (descriptor[1] == '\0') {
3048 // only the descriptors of primitive types should be 1 character long, also avoid class lookup
3049 // for primitive classes that aren't backed by dex files.
3050 return FindPrimitiveClass(descriptor[0]);
3051 }
3052 const size_t hash = ComputeModifiedUtf8Hash(descriptor);
3053 // Find the class in the loaded classes table.
3054 ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, class_loader.Get());
3055 if (klass != nullptr) {
3056 return EnsureResolved(self, descriptor, klass);
3057 }
3058 // Class is not yet loaded.
3059 if (descriptor[0] != '[' && class_loader == nullptr) {
3060 // Non-array class and the boot class loader, search the boot class path.
3061 ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_);
3062 if (pair.second != nullptr) {
3063 return DefineClass(self,
3064 descriptor,
3065 hash,
3066 ScopedNullHandle<mirror::ClassLoader>(),
3067 *pair.first,
3068 *pair.second);
3069 } else {
3070 // The boot class loader is searched ahead of the application class loader, failures are
3071 // expected and will be wrapped in a ClassNotFoundException. Use the pre-allocated error to
3072 // trigger the chaining with a proper stack trace.
3073 ObjPtr<mirror::Throwable> pre_allocated =
3074 Runtime::Current()->GetPreAllocatedNoClassDefFoundError();
3075 self->SetException(pre_allocated);
3076 return nullptr;
3077 }
3078 }
3079 ObjPtr<mirror::Class> result_ptr;
3080 bool descriptor_equals;
3081 if (descriptor[0] == '[') {
3082 result_ptr = CreateArrayClass(self, descriptor, hash, class_loader);
3083 DCHECK_EQ(result_ptr == nullptr, self->IsExceptionPending());
3084 DCHECK(result_ptr == nullptr || result_ptr->DescriptorEquals(descriptor));
3085 descriptor_equals = true;
3086 } else {
3087 ScopedObjectAccessUnchecked soa(self);
3088 bool known_hierarchy =
3089 FindClassInBaseDexClassLoader(soa, self, descriptor, hash, class_loader, &result_ptr);
3090 if (result_ptr != nullptr) {
3091 // The chain was understood and we found the class. We still need to add the class to
3092 // the class table to protect from racy programs that can try and redefine the path list
3093 // which would change the Class<?> returned for subsequent evaluation of const-class.
3094 DCHECK(known_hierarchy);
3095 DCHECK(result_ptr->DescriptorEquals(descriptor));
3096 descriptor_equals = true;
3097 } else if (!self->IsExceptionPending()) {
3098 // Either the chain wasn't understood or the class wasn't found.
3099 // If there is a pending exception we didn't clear, it is a not a ClassNotFoundException and
3100 // we should return it instead of silently clearing and retrying.
3101 //
3102 // If the chain was understood but we did not find the class, let the Java-side
3103 // rediscover all this and throw the exception with the right stack trace. Note that
3104 // the Java-side could still succeed for racy programs if another thread is actively
3105 // modifying the class loader's path list.
3106
3107 // The runtime is not allowed to call into java from a runtime-thread so just abort.
3108 if (self->IsRuntimeThread()) {
3109 // Oops, we can't call into java so we can't run actual class-loader code.
3110 // This is true for e.g. for the compiler (jit or aot).
3111 ObjPtr<mirror::Throwable> pre_allocated =
3112 Runtime::Current()->GetPreAllocatedNoClassDefFoundError();
3113 self->SetException(pre_allocated);
3114 return nullptr;
3115 }
3116
3117 // Inlined DescriptorToDot(descriptor) with extra validation.
3118 //
3119 // Throw NoClassDefFoundError early rather than potentially load a class only to fail
3120 // the DescriptorEquals() check below and give a confusing error message. For example,
3121 // when native code erroneously calls JNI GetFieldId() with signature "java/lang/String"
3122 // instead of "Ljava/lang/String;", the message below using the "dot" names would be
3123 // "class loader [...] returned class java.lang.String instead of java.lang.String".
3124 size_t descriptor_length = strlen(descriptor);
3125 if (UNLIKELY(descriptor[0] != 'L') ||
3126 UNLIKELY(descriptor[descriptor_length - 1] != ';') ||
3127 UNLIKELY(memchr(descriptor + 1, '.', descriptor_length - 2) != nullptr)) {
3128 ThrowNoClassDefFoundError("Invalid descriptor: %s.", descriptor);
3129 return nullptr;
3130 }
3131
3132 std::string class_name_string(descriptor + 1, descriptor_length - 2);
3133 std::replace(class_name_string.begin(), class_name_string.end(), '/', '.');
3134 if (known_hierarchy &&
3135 fast_class_not_found_exceptions_ &&
3136 !Runtime::Current()->IsJavaDebuggable()) {
3137 // For known hierarchy, we know that the class is going to throw an exception. If we aren't
3138 // debuggable, optimize this path by throwing directly here without going back to Java
3139 // language. This reduces how many ClassNotFoundExceptions happen.
3140 self->ThrowNewExceptionF("Ljava/lang/ClassNotFoundException;",
3141 "%s",
3142 class_name_string.c_str());
3143 } else {
3144 ScopedLocalRef<jobject> class_loader_object(
3145 soa.Env(), soa.AddLocalReference<jobject>(class_loader.Get()));
3146 ScopedLocalRef<jobject> result(soa.Env(), nullptr);
3147 {
3148 ScopedThreadStateChange tsc(self, kNative);
3149 ScopedLocalRef<jobject> class_name_object(
3150 soa.Env(), soa.Env()->NewStringUTF(class_name_string.c_str()));
3151 if (class_name_object.get() == nullptr) {
3152 DCHECK(self->IsExceptionPending()); // OOME.
3153 return nullptr;
3154 }
3155 CHECK(class_loader_object.get() != nullptr);
3156 result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(),
3157 WellKnownClasses::java_lang_ClassLoader_loadClass,
3158 class_name_object.get()));
3159 }
3160 if (result.get() == nullptr && !self->IsExceptionPending()) {
3161 // broken loader - throw NPE to be compatible with Dalvik
3162 ThrowNullPointerException(StringPrintf("ClassLoader.loadClass returned null for %s",
3163 class_name_string.c_str()).c_str());
3164 return nullptr;
3165 }
3166 result_ptr = soa.Decode<mirror::Class>(result.get());
3167 // Check the name of the returned class.
3168 descriptor_equals = (result_ptr != nullptr) && result_ptr->DescriptorEquals(descriptor);
3169 }
3170 } else {
3171 DCHECK(!MatchesDexFileCaughtExceptions(self->GetException(), this));
3172 }
3173 }
3174
3175 if (self->IsExceptionPending()) {
3176 // If the ClassLoader threw or array class allocation failed, pass that exception up.
3177 // However, to comply with the RI behavior, first check if another thread succeeded.
3178 result_ptr = LookupClass(self, descriptor, hash, class_loader.Get());
3179 if (result_ptr != nullptr && !result_ptr->IsErroneous()) {
3180 self->ClearException();
3181 return EnsureResolved(self, descriptor, result_ptr);
3182 }
3183 return nullptr;
3184 }
3185
3186 // Try to insert the class to the class table, checking for mismatch.
3187 ObjPtr<mirror::Class> old;
3188 {
3189 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
3190 ClassTable* const class_table = InsertClassTableForClassLoader(class_loader.Get());
3191 old = class_table->Lookup(descriptor, hash);
3192 if (old == nullptr) {
3193 old = result_ptr; // For the comparison below, after releasing the lock.
3194 if (descriptor_equals) {
3195 class_table->InsertWithHash(result_ptr, hash);
3196 WriteBarrier::ForEveryFieldWrite(class_loader.Get());
3197 } // else throw below, after releasing the lock.
3198 }
3199 }
3200 if (UNLIKELY(old != result_ptr)) {
3201 // Return `old` (even if `!descriptor_equals`) to mimic the RI behavior for parallel
3202 // capable class loaders. (All class loaders are considered parallel capable on Android.)
3203 ObjPtr<mirror::Class> loader_class = class_loader->GetClass();
3204 const char* loader_class_name =
3205 loader_class->GetDexFile().StringByTypeIdx(loader_class->GetDexTypeIndex());
3206 LOG(WARNING) << "Initiating class loader of type " << DescriptorToDot(loader_class_name)
3207 << " is not well-behaved; it returned a different Class for racing loadClass(\""
3208 << DescriptorToDot(descriptor) << "\").";
3209 return EnsureResolved(self, descriptor, old);
3210 }
3211 if (UNLIKELY(!descriptor_equals)) {
3212 std::string result_storage;
3213 const char* result_name = result_ptr->GetDescriptor(&result_storage);
3214 std::string loader_storage;
3215 const char* loader_class_name = class_loader->GetClass()->GetDescriptor(&loader_storage);
3216 ThrowNoClassDefFoundError(
3217 "Initiating class loader of type %s returned class %s instead of %s.",
3218 DescriptorToDot(loader_class_name).c_str(),
3219 DescriptorToDot(result_name).c_str(),
3220 DescriptorToDot(descriptor).c_str());
3221 return nullptr;
3222 }
3223 // Success.
3224 return result_ptr;
3225 }
3226
3227 // Helper for maintaining DefineClass counting. We need to notify callbacks when we start/end a
3228 // define-class and how many recursive DefineClasses we are at in order to allow for doing things
3229 // like pausing class definition.
3230 struct ScopedDefiningClass {
3231 public:
REQUIRES_SHAREDart::ScopedDefiningClass3232 explicit ScopedDefiningClass(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_)
3233 : self_(self), returned_(false) {
3234 Locks::mutator_lock_->AssertSharedHeld(self_);
3235 Runtime::Current()->GetRuntimeCallbacks()->BeginDefineClass();
3236 self_->IncrDefineClassCount();
3237 }
REQUIRES_SHAREDart::ScopedDefiningClass3238 ~ScopedDefiningClass() REQUIRES_SHARED(Locks::mutator_lock_) {
3239 Locks::mutator_lock_->AssertSharedHeld(self_);
3240 CHECK(returned_);
3241 }
3242
Finishart::ScopedDefiningClass3243 ObjPtr<mirror::Class> Finish(Handle<mirror::Class> h_klass)
3244 REQUIRES_SHARED(Locks::mutator_lock_) {
3245 CHECK(!returned_);
3246 self_->DecrDefineClassCount();
3247 Runtime::Current()->GetRuntimeCallbacks()->EndDefineClass();
3248 Thread::PoisonObjectPointersIfDebug();
3249 returned_ = true;
3250 return h_klass.Get();
3251 }
3252
Finishart::ScopedDefiningClass3253 ObjPtr<mirror::Class> Finish(ObjPtr<mirror::Class> klass)
3254 REQUIRES_SHARED(Locks::mutator_lock_) {
3255 StackHandleScope<1> hs(self_);
3256 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
3257 return Finish(h_klass);
3258 }
3259
Finishart::ScopedDefiningClass3260 ObjPtr<mirror::Class> Finish(nullptr_t np ATTRIBUTE_UNUSED)
3261 REQUIRES_SHARED(Locks::mutator_lock_) {
3262 ScopedNullHandle<mirror::Class> snh;
3263 return Finish(snh);
3264 }
3265
3266 private:
3267 Thread* self_;
3268 bool returned_;
3269 };
3270
DefineClass(Thread * self,const char * descriptor,size_t hash,Handle<mirror::ClassLoader> class_loader,const DexFile & dex_file,const dex::ClassDef & dex_class_def)3271 ObjPtr<mirror::Class> ClassLinker::DefineClass(Thread* self,
3272 const char* descriptor,
3273 size_t hash,
3274 Handle<mirror::ClassLoader> class_loader,
3275 const DexFile& dex_file,
3276 const dex::ClassDef& dex_class_def) {
3277 ScopedDefiningClass sdc(self);
3278 StackHandleScope<3> hs(self);
3279 auto klass = hs.NewHandle<mirror::Class>(nullptr);
3280
3281 // Load the class from the dex file.
3282 if (UNLIKELY(!init_done_)) {
3283 // finish up init of hand crafted class_roots_
3284 if (strcmp(descriptor, "Ljava/lang/Object;") == 0) {
3285 klass.Assign(GetClassRoot<mirror::Object>(this));
3286 } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) {
3287 klass.Assign(GetClassRoot<mirror::Class>(this));
3288 } else if (strcmp(descriptor, "Ljava/lang/String;") == 0) {
3289 klass.Assign(GetClassRoot<mirror::String>(this));
3290 } else if (strcmp(descriptor, "Ljava/lang/ref/Reference;") == 0) {
3291 klass.Assign(GetClassRoot<mirror::Reference>(this));
3292 } else if (strcmp(descriptor, "Ljava/lang/DexCache;") == 0) {
3293 klass.Assign(GetClassRoot<mirror::DexCache>(this));
3294 } else if (strcmp(descriptor, "Ldalvik/system/ClassExt;") == 0) {
3295 klass.Assign(GetClassRoot<mirror::ClassExt>(this));
3296 }
3297 }
3298
3299 // For AOT-compilation of an app, we may use a shortened boot class path that excludes
3300 // some runtime modules. Prevent definition of classes in app class loader that could clash
3301 // with these modules as these classes could be resolved differently during execution.
3302 if (class_loader != nullptr &&
3303 Runtime::Current()->IsAotCompiler() &&
3304 IsUpdatableBootClassPathDescriptor(descriptor)) {
3305 ObjPtr<mirror::Throwable> pre_allocated =
3306 Runtime::Current()->GetPreAllocatedNoClassDefFoundError();
3307 self->SetException(pre_allocated);
3308 return sdc.Finish(nullptr);
3309 }
3310
3311 // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied
3312 // code to be executed. We put it up here so we can avoid all the allocations associated with
3313 // creating the class. This can happen with (eg) jit threads.
3314 if (!self->CanLoadClasses()) {
3315 // Make sure we don't try to load anything, potentially causing an infinite loop.
3316 ObjPtr<mirror::Throwable> pre_allocated =
3317 Runtime::Current()->GetPreAllocatedNoClassDefFoundError();
3318 self->SetException(pre_allocated);
3319 return sdc.Finish(nullptr);
3320 }
3321
3322 if (klass == nullptr) {
3323 // Allocate a class with the status of not ready.
3324 // Interface object should get the right size here. Regular class will
3325 // figure out the right size later and be replaced with one of the right
3326 // size when the class becomes resolved.
3327 if (CanAllocClass()) {
3328 klass.Assign(AllocClass(self, SizeOfClassWithoutEmbeddedTables(dex_file, dex_class_def)));
3329 } else {
3330 return sdc.Finish(nullptr);
3331 }
3332 }
3333 if (UNLIKELY(klass == nullptr)) {
3334 self->AssertPendingOOMException();
3335 return sdc.Finish(nullptr);
3336 }
3337 // Get the real dex file. This will return the input if there aren't any callbacks or they do
3338 // nothing.
3339 DexFile const* new_dex_file = nullptr;
3340 dex::ClassDef const* new_class_def = nullptr;
3341 // TODO We should ideally figure out some way to move this after we get a lock on the klass so it
3342 // will only be called once.
3343 Runtime::Current()->GetRuntimeCallbacks()->ClassPreDefine(descriptor,
3344 klass,
3345 class_loader,
3346 dex_file,
3347 dex_class_def,
3348 &new_dex_file,
3349 &new_class_def);
3350 // Check to see if an exception happened during runtime callbacks. Return if so.
3351 if (self->IsExceptionPending()) {
3352 return sdc.Finish(nullptr);
3353 }
3354 ObjPtr<mirror::DexCache> dex_cache = RegisterDexFile(*new_dex_file, class_loader.Get());
3355 if (dex_cache == nullptr) {
3356 self->AssertPendingException();
3357 return sdc.Finish(nullptr);
3358 }
3359 klass->SetDexCache(dex_cache);
3360 SetupClass(*new_dex_file, *new_class_def, klass, class_loader.Get());
3361
3362 // Mark the string class by setting its access flag.
3363 if (UNLIKELY(!init_done_)) {
3364 if (strcmp(descriptor, "Ljava/lang/String;") == 0) {
3365 klass->SetStringClass();
3366 }
3367 }
3368
3369 ObjectLock<mirror::Class> lock(self, klass);
3370 klass->SetClinitThreadId(self->GetTid());
3371 // Make sure we have a valid empty iftable even if there are errors.
3372 klass->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable());
3373
3374 // Add the newly loaded class to the loaded classes table.
3375 ObjPtr<mirror::Class> existing = InsertClass(descriptor, klass.Get(), hash);
3376 if (existing != nullptr) {
3377 // We failed to insert because we raced with another thread. Calling EnsureResolved may cause
3378 // this thread to block.
3379 return sdc.Finish(EnsureResolved(self, descriptor, existing));
3380 }
3381
3382 // Load the fields and other things after we are inserted in the table. This is so that we don't
3383 // end up allocating unfree-able linear alloc resources and then lose the race condition. The
3384 // other reason is that the field roots are only visited from the class table. So we need to be
3385 // inserted before we allocate / fill in these fields.
3386 LoadClass(self, *new_dex_file, *new_class_def, klass);
3387 if (self->IsExceptionPending()) {
3388 VLOG(class_linker) << self->GetException()->Dump();
3389 // An exception occured during load, set status to erroneous while holding klass' lock in case
3390 // notification is necessary.
3391 if (!klass->IsErroneous()) {
3392 mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self);
3393 }
3394 return sdc.Finish(nullptr);
3395 }
3396
3397 // Finish loading (if necessary) by finding parents
3398 CHECK(!klass->IsLoaded());
3399 if (!LoadSuperAndInterfaces(klass, *new_dex_file)) {
3400 // Loading failed.
3401 if (!klass->IsErroneous()) {
3402 mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self);
3403 }
3404 return sdc.Finish(nullptr);
3405 }
3406 CHECK(klass->IsLoaded());
3407
3408 // At this point the class is loaded. Publish a ClassLoad event.
3409 // Note: this may be a temporary class. It is a listener's responsibility to handle this.
3410 Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(klass);
3411
3412 // Link the class (if necessary)
3413 CHECK(!klass->IsResolved());
3414 // TODO: Use fast jobjects?
3415 auto interfaces = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr);
3416
3417 MutableHandle<mirror::Class> h_new_class = hs.NewHandle<mirror::Class>(nullptr);
3418 if (!LinkClass(self, descriptor, klass, interfaces, &h_new_class)) {
3419 // Linking failed.
3420 if (!klass->IsErroneous()) {
3421 mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self);
3422 }
3423 return sdc.Finish(nullptr);
3424 }
3425 self->AssertNoPendingException();
3426 CHECK(h_new_class != nullptr) << descriptor;
3427 CHECK(h_new_class->IsResolved() && !h_new_class->IsErroneousResolved()) << descriptor;
3428
3429 // Instrumentation may have updated entrypoints for all methods of all
3430 // classes. However it could not update methods of this class while we
3431 // were loading it. Now the class is resolved, we can update entrypoints
3432 // as required by instrumentation.
3433 if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) {
3434 // We must be in the kRunnable state to prevent instrumentation from
3435 // suspending all threads to update entrypoints while we are doing it
3436 // for this class.
3437 DCHECK_EQ(self->GetState(), kRunnable);
3438 Runtime::Current()->GetInstrumentation()->InstallStubsForClass(h_new_class.Get());
3439 }
3440
3441 /*
3442 * We send CLASS_PREPARE events to the debugger from here. The
3443 * definition of "preparation" is creating the static fields for a
3444 * class and initializing them to the standard default values, but not
3445 * executing any code (that comes later, during "initialization").
3446 *
3447 * We did the static preparation in LinkClass.
3448 *
3449 * The class has been prepared and resolved but possibly not yet verified
3450 * at this point.
3451 */
3452 Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(klass, h_new_class);
3453
3454 // Notify native debugger of the new class and its layout.
3455 jit::Jit::NewTypeLoadedIfUsingJit(h_new_class.Get());
3456
3457 return sdc.Finish(h_new_class);
3458 }
3459
SizeOfClassWithoutEmbeddedTables(const DexFile & dex_file,const dex::ClassDef & dex_class_def)3460 uint32_t ClassLinker::SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file,
3461 const dex::ClassDef& dex_class_def) {
3462 size_t num_ref = 0;
3463 size_t num_8 = 0;
3464 size_t num_16 = 0;
3465 size_t num_32 = 0;
3466 size_t num_64 = 0;
3467 ClassAccessor accessor(dex_file, dex_class_def);
3468 // We allow duplicate definitions of the same field in a class_data_item
3469 // but ignore the repeated indexes here, b/21868015.
3470 uint32_t last_field_idx = dex::kDexNoIndex;
3471 for (const ClassAccessor::Field& field : accessor.GetStaticFields()) {
3472 uint32_t field_idx = field.GetIndex();
3473 // Ordering enforced by DexFileVerifier.
3474 DCHECK(last_field_idx == dex::kDexNoIndex || last_field_idx <= field_idx);
3475 if (UNLIKELY(field_idx == last_field_idx)) {
3476 continue;
3477 }
3478 last_field_idx = field_idx;
3479 const dex::FieldId& field_id = dex_file.GetFieldId(field_idx);
3480 const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id);
3481 char c = descriptor[0];
3482 switch (c) {
3483 case 'L':
3484 case '[':
3485 num_ref++;
3486 break;
3487 case 'J':
3488 case 'D':
3489 num_64++;
3490 break;
3491 case 'I':
3492 case 'F':
3493 num_32++;
3494 break;
3495 case 'S':
3496 case 'C':
3497 num_16++;
3498 break;
3499 case 'B':
3500 case 'Z':
3501 num_8++;
3502 break;
3503 default:
3504 LOG(FATAL) << "Unknown descriptor: " << c;
3505 UNREACHABLE();
3506 }
3507 }
3508 return mirror::Class::ComputeClassSize(false,
3509 0,
3510 num_8,
3511 num_16,
3512 num_32,
3513 num_64,
3514 num_ref,
3515 image_pointer_size_);
3516 }
3517
3518 // Special case to get oat code without overwriting a trampoline.
GetQuickOatCodeFor(ArtMethod * method)3519 const void* ClassLinker::GetQuickOatCodeFor(ArtMethod* method) {
3520 CHECK(method->IsInvokable()) << method->PrettyMethod();
3521 if (method->IsProxyMethod()) {
3522 return GetQuickProxyInvokeHandler();
3523 }
3524 const void* code = method->GetOatMethodQuickCode(GetImagePointerSize());
3525 if (code != nullptr) {
3526 return code;
3527 }
3528
3529 jit::Jit* jit = Runtime::Current()->GetJit();
3530 if (jit != nullptr) {
3531 code = jit->GetCodeCache()->GetSavedEntryPointOfPreCompiledMethod(method);
3532 if (code != nullptr) {
3533 return code;
3534 }
3535 }
3536
3537 if (method->IsNative()) {
3538 // No code and native? Use generic trampoline.
3539 return GetQuickGenericJniStub();
3540 }
3541
3542 if (interpreter::CanRuntimeUseNterp() && interpreter::CanMethodUseNterp(method)) {
3543 return interpreter::GetNterpEntryPoint();
3544 }
3545
3546 return GetQuickToInterpreterBridge();
3547 }
3548
ShouldUseInterpreterEntrypoint(ArtMethod * method,const void * quick_code)3549 bool ClassLinker::ShouldUseInterpreterEntrypoint(ArtMethod* method, const void* quick_code) {
3550 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
3551 if (UNLIKELY(method->IsNative() || method->IsProxyMethod())) {
3552 return false;
3553 }
3554
3555 if (quick_code == nullptr) {
3556 return true;
3557 }
3558
3559 Runtime* runtime = Runtime::Current();
3560 instrumentation::Instrumentation* instr = runtime->GetInstrumentation();
3561 if (instr->InterpretOnly()) {
3562 return true;
3563 }
3564
3565 if (runtime->GetClassLinker()->IsQuickToInterpreterBridge(quick_code)) {
3566 // Doing this check avoids doing compiled/interpreter transitions.
3567 return true;
3568 }
3569
3570 if (Thread::Current()->IsForceInterpreter()) {
3571 // Force the use of interpreter when it is required by the debugger.
3572 return true;
3573 }
3574
3575 if (Thread::Current()->IsAsyncExceptionPending()) {
3576 // Force use of interpreter to handle async-exceptions
3577 return true;
3578 }
3579
3580 if (quick_code == GetQuickInstrumentationEntryPoint()) {
3581 const void* instr_target = instr->GetCodeForInvoke(method);
3582 DCHECK_NE(instr_target, GetQuickInstrumentationEntryPoint()) << method->PrettyMethod();
3583 return ShouldUseInterpreterEntrypoint(method, instr_target);
3584 }
3585
3586 if (runtime->IsJavaDebuggable()) {
3587 // For simplicity, we ignore precompiled code and go to the interpreter
3588 // assuming we don't already have jitted code.
3589 // We could look at the oat file where `quick_code` is being defined,
3590 // and check whether it's been compiled debuggable, but we decided to
3591 // only rely on the JIT for debuggable apps.
3592 jit::Jit* jit = Runtime::Current()->GetJit();
3593 return (jit == nullptr) || !jit->GetCodeCache()->ContainsPc(quick_code);
3594 }
3595
3596 if (runtime->IsNativeDebuggable()) {
3597 DCHECK(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse());
3598 // If we are doing native debugging, ignore application's AOT code,
3599 // since we want to JIT it (at first use) with extra stackmaps for native
3600 // debugging. We keep however all AOT code from the boot image,
3601 // since the JIT-at-first-use is blocking and would result in non-negligible
3602 // startup performance impact.
3603 return !runtime->GetHeap()->IsInBootImageOatFile(quick_code);
3604 }
3605
3606 return false;
3607 }
3608
FixupStaticTrampolines(Thread * self,ObjPtr<mirror::Class> klass)3609 void ClassLinker::FixupStaticTrampolines(Thread* self, ObjPtr<mirror::Class> klass) {
3610 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
3611 DCHECK(klass->IsVisiblyInitialized()) << klass->PrettyDescriptor();
3612 size_t num_direct_methods = klass->NumDirectMethods();
3613 if (num_direct_methods == 0) {
3614 return; // No direct methods => no static methods.
3615 }
3616 if (UNLIKELY(klass->IsProxyClass())) {
3617 return;
3618 }
3619 PointerSize pointer_size = image_pointer_size_;
3620 if (std::any_of(klass->GetDirectMethods(pointer_size).begin(),
3621 klass->GetDirectMethods(pointer_size).end(),
3622 [](const ArtMethod& m) { return m.IsCriticalNative(); })) {
3623 // Store registered @CriticalNative methods, if any, to JNI entrypoints.
3624 // Direct methods are a contiguous chunk of memory, so use the ordering of the map.
3625 ArtMethod* first_method = klass->GetDirectMethod(0u, pointer_size);
3626 ArtMethod* last_method = klass->GetDirectMethod(num_direct_methods - 1u, pointer_size);
3627 MutexLock lock(self, critical_native_code_with_clinit_check_lock_);
3628 auto lb = critical_native_code_with_clinit_check_.lower_bound(first_method);
3629 while (lb != critical_native_code_with_clinit_check_.end() && lb->first <= last_method) {
3630 lb->first->SetEntryPointFromJni(lb->second);
3631 lb = critical_native_code_with_clinit_check_.erase(lb);
3632 }
3633 }
3634 Runtime* runtime = Runtime::Current();
3635 if (!runtime->IsStarted()) {
3636 if (runtime->IsAotCompiler() || runtime->GetHeap()->HasBootImageSpace()) {
3637 return; // OAT file unavailable.
3638 }
3639 }
3640
3641 const DexFile& dex_file = klass->GetDexFile();
3642 bool has_oat_class;
3643 OatFile::OatClass oat_class = OatFile::FindOatClass(dex_file,
3644 klass->GetDexClassDefIndex(),
3645 &has_oat_class);
3646 // Link the code of methods skipped by LinkCode.
3647 for (size_t method_index = 0; method_index < num_direct_methods; ++method_index) {
3648 ArtMethod* method = klass->GetDirectMethod(method_index, pointer_size);
3649 if (!method->IsStatic()) {
3650 // Only update static methods.
3651 continue;
3652 }
3653 const void* quick_code = nullptr;
3654
3655 // In order:
3656 // 1) Check if we have AOT Code.
3657 // 2) Check if we have JIT Code.
3658 // 3) Check if we can use Nterp.
3659 if (has_oat_class) {
3660 OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index);
3661 quick_code = oat_method.GetQuickCode();
3662 }
3663
3664 jit::Jit* jit = runtime->GetJit();
3665 if (quick_code == nullptr && jit != nullptr) {
3666 quick_code = jit->GetCodeCache()->GetSavedEntryPointOfPreCompiledMethod(method);
3667 }
3668
3669 if (quick_code == nullptr &&
3670 interpreter::CanRuntimeUseNterp() &&
3671 interpreter::CanMethodUseNterp(method)) {
3672 quick_code = interpreter::GetNterpEntryPoint();
3673 }
3674
3675 // Check whether the method is native, in which case it's generic JNI.
3676 if (quick_code == nullptr && method->IsNative()) {
3677 quick_code = GetQuickGenericJniStub();
3678 } else if (ShouldUseInterpreterEntrypoint(method, quick_code)) {
3679 // Use interpreter entry point.
3680 if (IsQuickToInterpreterBridge(method->GetEntryPointFromQuickCompiledCode())) {
3681 // If we have the trampoline or the bridge already, no need to update.
3682 // This saves in not dirtying boot image memory.
3683 continue;
3684 }
3685 quick_code = GetQuickToInterpreterBridge();
3686 }
3687 CHECK(quick_code != nullptr);
3688 runtime->GetInstrumentation()->UpdateMethodsCode(method, quick_code);
3689 }
3690 // Ignore virtual methods on the iterator.
3691 }
3692
3693 // Does anything needed to make sure that the compiler will not generate a direct invoke to this
3694 // method. Should only be called on non-invokable methods.
EnsureThrowsInvocationError(ClassLinker * class_linker,ArtMethod * method)3695 inline void EnsureThrowsInvocationError(ClassLinker* class_linker, ArtMethod* method)
3696 REQUIRES_SHARED(Locks::mutator_lock_) {
3697 DCHECK(method != nullptr);
3698 DCHECK(!method->IsInvokable());
3699 method->SetEntryPointFromQuickCompiledCodePtrSize(
3700 class_linker->GetQuickToInterpreterBridgeTrampoline(),
3701 class_linker->GetImagePointerSize());
3702 }
3703
LinkCode(ClassLinker * class_linker,ArtMethod * method,const OatFile::OatClass * oat_class,uint32_t class_def_method_index)3704 static void LinkCode(ClassLinker* class_linker,
3705 ArtMethod* method,
3706 const OatFile::OatClass* oat_class,
3707 uint32_t class_def_method_index) REQUIRES_SHARED(Locks::mutator_lock_) {
3708 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
3709 Runtime* const runtime = Runtime::Current();
3710 if (runtime->IsAotCompiler()) {
3711 // The following code only applies to a non-compiler runtime.
3712 return;
3713 }
3714
3715 // Method shouldn't have already been linked.
3716 DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr);
3717
3718 if (!method->IsInvokable()) {
3719 EnsureThrowsInvocationError(class_linker, method);
3720 return;
3721 }
3722
3723 const void* quick_code = nullptr;
3724 if (oat_class != nullptr) {
3725 // Every kind of method should at least get an invoke stub from the oat_method.
3726 // non-abstract methods also get their code pointers.
3727 const OatFile::OatMethod oat_method = oat_class->GetOatMethod(class_def_method_index);
3728 quick_code = oat_method.GetQuickCode();
3729 }
3730
3731 bool enter_interpreter = class_linker->ShouldUseInterpreterEntrypoint(method, quick_code);
3732
3733 // Note: this mimics the logic in image_writer.cc that installs the resolution
3734 // stub only if we have compiled code and the method needs a class initialization
3735 // check.
3736 if (quick_code == nullptr) {
3737 method->SetEntryPointFromQuickCompiledCode(
3738 method->IsNative() ? GetQuickGenericJniStub() : GetQuickToInterpreterBridge());
3739 } else if (enter_interpreter) {
3740 method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge());
3741 } else if (NeedsClinitCheckBeforeCall(method)) {
3742 DCHECK(!method->GetDeclaringClass()->IsVisiblyInitialized()); // Actually ClassStatus::Idx.
3743 // If we do have code but the method needs a class initialization check before calling
3744 // that code, install the resolution stub that will perform the check.
3745 // It will be replaced by the proper entry point by ClassLinker::FixupStaticTrampolines
3746 // after initializing class (see ClassLinker::InitializeClass method).
3747 method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionStub());
3748 } else {
3749 method->SetEntryPointFromQuickCompiledCode(quick_code);
3750 }
3751
3752 if (method->IsNative()) {
3753 // Set up the dlsym lookup stub. Do not go through `UnregisterNative()`
3754 // as the extra processing for @CriticalNative is not needed yet.
3755 method->SetEntryPointFromJni(
3756 method->IsCriticalNative() ? GetJniDlsymLookupCriticalStub() : GetJniDlsymLookupStub());
3757
3758 if (enter_interpreter || quick_code == nullptr) {
3759 // We have a native method here without code. Then it should have the generic JNI
3760 // trampoline as entrypoint.
3761 // TODO: this doesn't handle all the cases where trampolines may be installed.
3762 DCHECK(class_linker->IsQuickGenericJniStub(method->GetEntryPointFromQuickCompiledCode()));
3763 }
3764 }
3765 }
3766
SetupClass(const DexFile & dex_file,const dex::ClassDef & dex_class_def,Handle<mirror::Class> klass,ObjPtr<mirror::ClassLoader> class_loader)3767 void ClassLinker::SetupClass(const DexFile& dex_file,
3768 const dex::ClassDef& dex_class_def,
3769 Handle<mirror::Class> klass,
3770 ObjPtr<mirror::ClassLoader> class_loader) {
3771 CHECK(klass != nullptr);
3772 CHECK(klass->GetDexCache() != nullptr);
3773 CHECK_EQ(ClassStatus::kNotReady, klass->GetStatus());
3774 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
3775 CHECK(descriptor != nullptr);
3776
3777 klass->SetClass(GetClassRoot<mirror::Class>(this));
3778 uint32_t access_flags = dex_class_def.GetJavaAccessFlags();
3779 CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U);
3780 klass->SetAccessFlagsDuringLinking(access_flags);
3781 klass->SetClassLoader(class_loader);
3782 DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot);
3783 mirror::Class::SetStatus(klass, ClassStatus::kIdx, nullptr);
3784
3785 klass->SetDexClassDefIndex(dex_file.GetIndexForClassDef(dex_class_def));
3786 klass->SetDexTypeIndex(dex_class_def.class_idx_);
3787 }
3788
AllocArtFieldArray(Thread * self,LinearAlloc * allocator,size_t length)3789 LengthPrefixedArray<ArtField>* ClassLinker::AllocArtFieldArray(Thread* self,
3790 LinearAlloc* allocator,
3791 size_t length) {
3792 if (length == 0) {
3793 return nullptr;
3794 }
3795 // If the ArtField alignment changes, review all uses of LengthPrefixedArray<ArtField>.
3796 static_assert(alignof(ArtField) == 4, "ArtField alignment is expected to be 4.");
3797 size_t storage_size = LengthPrefixedArray<ArtField>::ComputeSize(length);
3798 void* array_storage = allocator->Alloc(self, storage_size);
3799 auto* ret = new(array_storage) LengthPrefixedArray<ArtField>(length);
3800 CHECK(ret != nullptr);
3801 std::uninitialized_fill_n(&ret->At(0), length, ArtField());
3802 return ret;
3803 }
3804
AllocArtMethodArray(Thread * self,LinearAlloc * allocator,size_t length)3805 LengthPrefixedArray<ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self,
3806 LinearAlloc* allocator,
3807 size_t length) {
3808 if (length == 0) {
3809 return nullptr;
3810 }
3811 const size_t method_alignment = ArtMethod::Alignment(image_pointer_size_);
3812 const size_t method_size = ArtMethod::Size(image_pointer_size_);
3813 const size_t storage_size =
3814 LengthPrefixedArray<ArtMethod>::ComputeSize(length, method_size, method_alignment);
3815 void* array_storage = allocator->Alloc(self, storage_size);
3816 auto* ret = new (array_storage) LengthPrefixedArray<ArtMethod>(length);
3817 CHECK(ret != nullptr);
3818 for (size_t i = 0; i < length; ++i) {
3819 new(reinterpret_cast<void*>(&ret->At(i, method_size, method_alignment))) ArtMethod;
3820 }
3821 return ret;
3822 }
3823
GetAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader)3824 LinearAlloc* ClassLinker::GetAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) {
3825 if (class_loader == nullptr) {
3826 return Runtime::Current()->GetLinearAlloc();
3827 }
3828 LinearAlloc* allocator = class_loader->GetAllocator();
3829 DCHECK(allocator != nullptr);
3830 return allocator;
3831 }
3832
GetOrCreateAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader)3833 LinearAlloc* ClassLinker::GetOrCreateAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) {
3834 if (class_loader == nullptr) {
3835 return Runtime::Current()->GetLinearAlloc();
3836 }
3837 WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
3838 LinearAlloc* allocator = class_loader->GetAllocator();
3839 if (allocator == nullptr) {
3840 RegisterClassLoader(class_loader);
3841 allocator = class_loader->GetAllocator();
3842 CHECK(allocator != nullptr);
3843 }
3844 return allocator;
3845 }
3846
LoadClass(Thread * self,const DexFile & dex_file,const dex::ClassDef & dex_class_def,Handle<mirror::Class> klass)3847 void ClassLinker::LoadClass(Thread* self,
3848 const DexFile& dex_file,
3849 const dex::ClassDef& dex_class_def,
3850 Handle<mirror::Class> klass) {
3851 ClassAccessor accessor(dex_file,
3852 dex_class_def,
3853 /* parse_hiddenapi_class_data= */ klass->IsBootStrapClassLoaded());
3854 if (!accessor.HasClassData()) {
3855 return;
3856 }
3857 Runtime* const runtime = Runtime::Current();
3858 {
3859 // Note: We cannot have thread suspension until the field and method arrays are setup or else
3860 // Class::VisitFieldRoots may miss some fields or methods.
3861 ScopedAssertNoThreadSuspension nts(__FUNCTION__);
3862 // Load static fields.
3863 // We allow duplicate definitions of the same field in a class_data_item
3864 // but ignore the repeated indexes here, b/21868015.
3865 LinearAlloc* const allocator = GetAllocatorForClassLoader(klass->GetClassLoader());
3866 LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self,
3867 allocator,
3868 accessor.NumStaticFields());
3869 LengthPrefixedArray<ArtField>* ifields = AllocArtFieldArray(self,
3870 allocator,
3871 accessor.NumInstanceFields());
3872 size_t num_sfields = 0u;
3873 size_t num_ifields = 0u;
3874 uint32_t last_static_field_idx = 0u;
3875 uint32_t last_instance_field_idx = 0u;
3876
3877 // Methods
3878 bool has_oat_class = false;
3879 const OatFile::OatClass oat_class = (runtime->IsStarted() && !runtime->IsAotCompiler())
3880 ? OatFile::FindOatClass(dex_file, klass->GetDexClassDefIndex(), &has_oat_class)
3881 : OatFile::OatClass::Invalid();
3882 const OatFile::OatClass* oat_class_ptr = has_oat_class ? &oat_class : nullptr;
3883 klass->SetMethodsPtr(
3884 AllocArtMethodArray(self, allocator, accessor.NumMethods()),
3885 accessor.NumDirectMethods(),
3886 accessor.NumVirtualMethods());
3887 size_t class_def_method_index = 0;
3888 uint32_t last_dex_method_index = dex::kDexNoIndex;
3889 size_t last_class_def_method_index = 0;
3890
3891 // Use the visitor since the ranged based loops are bit slower from seeking. Seeking to the
3892 // methods needs to decode all of the fields.
3893 accessor.VisitFieldsAndMethods([&](
3894 const ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) {
3895 uint32_t field_idx = field.GetIndex();
3896 DCHECK_GE(field_idx, last_static_field_idx); // Ordering enforced by DexFileVerifier.
3897 if (num_sfields == 0 || LIKELY(field_idx > last_static_field_idx)) {
3898 LoadField(field, klass, &sfields->At(num_sfields));
3899 ++num_sfields;
3900 last_static_field_idx = field_idx;
3901 }
3902 }, [&](const ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) {
3903 uint32_t field_idx = field.GetIndex();
3904 DCHECK_GE(field_idx, last_instance_field_idx); // Ordering enforced by DexFileVerifier.
3905 if (num_ifields == 0 || LIKELY(field_idx > last_instance_field_idx)) {
3906 LoadField(field, klass, &ifields->At(num_ifields));
3907 ++num_ifields;
3908 last_instance_field_idx = field_idx;
3909 }
3910 }, [&](const ClassAccessor::Method& method) REQUIRES_SHARED(Locks::mutator_lock_) {
3911 ArtMethod* art_method = klass->GetDirectMethodUnchecked(class_def_method_index,
3912 image_pointer_size_);
3913 LoadMethod(dex_file, method, klass, art_method);
3914 LinkCode(this, art_method, oat_class_ptr, class_def_method_index);
3915 uint32_t it_method_index = method.GetIndex();
3916 if (last_dex_method_index == it_method_index) {
3917 // duplicate case
3918 art_method->SetMethodIndex(last_class_def_method_index);
3919 } else {
3920 art_method->SetMethodIndex(class_def_method_index);
3921 last_dex_method_index = it_method_index;
3922 last_class_def_method_index = class_def_method_index;
3923 }
3924 ++class_def_method_index;
3925 }, [&](const ClassAccessor::Method& method) REQUIRES_SHARED(Locks::mutator_lock_) {
3926 ArtMethod* art_method = klass->GetVirtualMethodUnchecked(
3927 class_def_method_index - accessor.NumDirectMethods(),
3928 image_pointer_size_);
3929 LoadMethod(dex_file, method, klass, art_method);
3930 LinkCode(this, art_method, oat_class_ptr, class_def_method_index);
3931 ++class_def_method_index;
3932 });
3933
3934 if (UNLIKELY(num_ifields + num_sfields != accessor.NumFields())) {
3935 LOG(WARNING) << "Duplicate fields in class " << klass->PrettyDescriptor()
3936 << " (unique static fields: " << num_sfields << "/" << accessor.NumStaticFields()
3937 << ", unique instance fields: " << num_ifields << "/" << accessor.NumInstanceFields()
3938 << ")";
3939 // NOTE: Not shrinking the over-allocated sfields/ifields, just setting size.
3940 if (sfields != nullptr) {
3941 sfields->SetSize(num_sfields);
3942 }
3943 if (ifields != nullptr) {
3944 ifields->SetSize(num_ifields);
3945 }
3946 }
3947 // Set the field arrays.
3948 klass->SetSFieldsPtr(sfields);
3949 DCHECK_EQ(klass->NumStaticFields(), num_sfields);
3950 klass->SetIFieldsPtr(ifields);
3951 DCHECK_EQ(klass->NumInstanceFields(), num_ifields);
3952 }
3953 // Ensure that the card is marked so that remembered sets pick up native roots.
3954 WriteBarrier::ForEveryFieldWrite(klass.Get());
3955 self->AllowThreadSuspension();
3956 }
3957
LoadField(const ClassAccessor::Field & field,Handle<mirror::Class> klass,ArtField * dst)3958 void ClassLinker::LoadField(const ClassAccessor::Field& field,
3959 Handle<mirror::Class> klass,
3960 ArtField* dst) {
3961 const uint32_t field_idx = field.GetIndex();
3962 dst->SetDexFieldIndex(field_idx);
3963 dst->SetDeclaringClass(klass.Get());
3964
3965 // Get access flags from the DexFile and set hiddenapi runtime access flags.
3966 dst->SetAccessFlags(field.GetAccessFlags() | hiddenapi::CreateRuntimeFlags(field));
3967 }
3968
LoadMethod(const DexFile & dex_file,const ClassAccessor::Method & method,Handle<mirror::Class> klass,ArtMethod * dst)3969 void ClassLinker::LoadMethod(const DexFile& dex_file,
3970 const ClassAccessor::Method& method,
3971 Handle<mirror::Class> klass,
3972 ArtMethod* dst) {
3973 const uint32_t dex_method_idx = method.GetIndex();
3974 const dex::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
3975 const char* method_name = dex_file.StringDataByIdx(method_id.name_idx_);
3976
3977 ScopedAssertNoThreadSuspension ants("LoadMethod");
3978 dst->SetDexMethodIndex(dex_method_idx);
3979 dst->SetDeclaringClass(klass.Get());
3980 dst->SetCodeItemOffset(method.GetCodeItemOffset());
3981
3982 // Get access flags from the DexFile and set hiddenapi runtime access flags.
3983 uint32_t access_flags = method.GetAccessFlags() | hiddenapi::CreateRuntimeFlags(method);
3984
3985 if (UNLIKELY(strcmp("finalize", method_name) == 0)) {
3986 // Set finalizable flag on declaring class.
3987 if (strcmp("V", dex_file.GetShorty(method_id.proto_idx_)) == 0) {
3988 // Void return type.
3989 if (klass->GetClassLoader() != nullptr) { // All non-boot finalizer methods are flagged.
3990 klass->SetFinalizable();
3991 } else {
3992 std::string temp;
3993 const char* klass_descriptor = klass->GetDescriptor(&temp);
3994 // The Enum class declares a "final" finalize() method to prevent subclasses from
3995 // introducing a finalizer. We don't want to set the finalizable flag for Enum or its
3996 // subclasses, so we exclude it here.
3997 // We also want to avoid setting the flag on Object, where we know that finalize() is
3998 // empty.
3999 if (strcmp(klass_descriptor, "Ljava/lang/Object;") != 0 &&
4000 strcmp(klass_descriptor, "Ljava/lang/Enum;") != 0) {
4001 klass->SetFinalizable();
4002 }
4003 }
4004 }
4005 } else if (method_name[0] == '<') {
4006 // Fix broken access flags for initializers. Bug 11157540.
4007 bool is_init = (strcmp("<init>", method_name) == 0);
4008 bool is_clinit = !is_init && (strcmp("<clinit>", method_name) == 0);
4009 if (UNLIKELY(!is_init && !is_clinit)) {
4010 LOG(WARNING) << "Unexpected '<' at start of method name " << method_name;
4011 } else {
4012 if (UNLIKELY((access_flags & kAccConstructor) == 0)) {
4013 LOG(WARNING) << method_name << " didn't have expected constructor access flag in class "
4014 << klass->PrettyDescriptor() << " in dex file " << dex_file.GetLocation();
4015 access_flags |= kAccConstructor;
4016 }
4017 }
4018 }
4019 if (UNLIKELY((access_flags & kAccNative) != 0u)) {
4020 // Check if the native method is annotated with @FastNative or @CriticalNative.
4021 access_flags |= annotations::GetNativeMethodAnnotationAccessFlags(
4022 dex_file, dst->GetClassDef(), dex_method_idx);
4023 }
4024 dst->SetAccessFlags(access_flags);
4025 // Must be done after SetAccessFlags since IsAbstract depends on it.
4026 if (klass->IsInterface() && dst->IsAbstract()) {
4027 dst->CalculateAndSetImtIndex();
4028 }
4029 }
4030
AppendToBootClassPath(Thread * self,const DexFile * dex_file)4031 void ClassLinker::AppendToBootClassPath(Thread* self, const DexFile* dex_file) {
4032 ObjPtr<mirror::DexCache> dex_cache = AllocAndInitializeDexCache(
4033 self,
4034 *dex_file,
4035 Runtime::Current()->GetLinearAlloc());
4036 CHECK(dex_cache != nullptr) << "Failed to allocate dex cache for " << dex_file->GetLocation();
4037 AppendToBootClassPath(dex_file, dex_cache);
4038 }
4039
AppendToBootClassPath(const DexFile * dex_file,ObjPtr<mirror::DexCache> dex_cache)4040 void ClassLinker::AppendToBootClassPath(const DexFile* dex_file,
4041 ObjPtr<mirror::DexCache> dex_cache) {
4042 CHECK(dex_file != nullptr);
4043 CHECK(dex_cache != nullptr) << dex_file->GetLocation();
4044 boot_class_path_.push_back(dex_file);
4045 WriterMutexLock mu(Thread::Current(), *Locks::dex_lock_);
4046 RegisterDexFileLocked(*dex_file, dex_cache, /* class_loader= */ nullptr);
4047 }
4048
RegisterDexFileLocked(const DexFile & dex_file,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader)4049 void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file,
4050 ObjPtr<mirror::DexCache> dex_cache,
4051 ObjPtr<mirror::ClassLoader> class_loader) {
4052 Thread* const self = Thread::Current();
4053 Locks::dex_lock_->AssertExclusiveHeld(self);
4054 CHECK(dex_cache != nullptr) << dex_file.GetLocation();
4055 // For app images, the dex cache location may be a suffix of the dex file location since the
4056 // dex file location is an absolute path.
4057 const std::string dex_cache_location = dex_cache->GetLocation()->ToModifiedUtf8();
4058 const size_t dex_cache_length = dex_cache_location.length();
4059 CHECK_GT(dex_cache_length, 0u) << dex_file.GetLocation();
4060 std::string dex_file_location = dex_file.GetLocation();
4061 // The following paths checks don't work on preopt when using boot dex files, where the dex
4062 // cache location is the one on device, and the dex_file's location is the one on host.
4063 if (!(Runtime::Current()->IsAotCompiler() && class_loader == nullptr && !kIsTargetBuild)) {
4064 CHECK_GE(dex_file_location.length(), dex_cache_length)
4065 << dex_cache_location << " " << dex_file.GetLocation();
4066 const std::string dex_file_suffix = dex_file_location.substr(
4067 dex_file_location.length() - dex_cache_length,
4068 dex_cache_length);
4069 // Example dex_cache location is SettingsProvider.apk and
4070 // dex file location is /system/priv-app/SettingsProvider/SettingsProvider.apk
4071 CHECK_EQ(dex_cache_location, dex_file_suffix);
4072 }
4073 const OatFile* oat_file =
4074 (dex_file.GetOatDexFile() != nullptr) ? dex_file.GetOatDexFile()->GetOatFile() : nullptr;
4075 // Clean up pass to remove null dex caches; null dex caches can occur due to class unloading
4076 // and we are lazily removing null entries. Also check if we need to initialize OatFile data
4077 // (.data.bimg.rel.ro and .bss sections) needed for code execution.
4078 bool initialize_oat_file_data = (oat_file != nullptr) && oat_file->IsExecutable();
4079 JavaVMExt* const vm = self->GetJniEnv()->GetVm();
4080 for (auto it = dex_caches_.begin(); it != dex_caches_.end(); ) {
4081 DexCacheData data = *it;
4082 if (self->IsJWeakCleared(data.weak_root)) {
4083 vm->DeleteWeakGlobalRef(self, data.weak_root);
4084 it = dex_caches_.erase(it);
4085 } else {
4086 if (initialize_oat_file_data &&
4087 it->dex_file->GetOatDexFile() != nullptr &&
4088 it->dex_file->GetOatDexFile()->GetOatFile() == oat_file) {
4089 initialize_oat_file_data = false; // Already initialized.
4090 }
4091 ++it;
4092 }
4093 }
4094 if (initialize_oat_file_data) {
4095 oat_file->InitializeRelocations();
4096 }
4097 // Let hiddenapi assign a domain to the newly registered dex file.
4098 hiddenapi::InitializeDexFileDomain(dex_file, class_loader);
4099
4100 jweak dex_cache_jweak = vm->AddWeakGlobalRef(self, dex_cache);
4101 dex_cache->SetDexFile(&dex_file);
4102 DexCacheData data;
4103 data.weak_root = dex_cache_jweak;
4104 data.dex_file = dex_cache->GetDexFile();
4105 data.class_table = ClassTableForClassLoader(class_loader);
4106 AddNativeDebugInfoForDex(self, data.dex_file);
4107 DCHECK(data.class_table != nullptr);
4108 // Make sure to hold the dex cache live in the class table. This case happens for the boot class
4109 // path dex caches without an image.
4110 data.class_table->InsertStrongRoot(dex_cache);
4111 // Make sure that the dex cache holds the classloader live.
4112 dex_cache->SetClassLoader(class_loader);
4113 if (class_loader != nullptr) {
4114 // Since we added a strong root to the class table, do the write barrier as required for
4115 // remembered sets and generational GCs.
4116 WriteBarrier::ForEveryFieldWrite(class_loader);
4117 }
4118 dex_caches_.push_back(data);
4119 }
4120
DecodeDexCacheLocked(Thread * self,const DexCacheData * data)4121 ObjPtr<mirror::DexCache> ClassLinker::DecodeDexCacheLocked(Thread* self, const DexCacheData* data) {
4122 return data != nullptr
4123 ? ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data->weak_root))
4124 : nullptr;
4125 }
4126
IsSameClassLoader(ObjPtr<mirror::DexCache> dex_cache,const DexCacheData * data,ObjPtr<mirror::ClassLoader> class_loader)4127 bool ClassLinker::IsSameClassLoader(
4128 ObjPtr<mirror::DexCache> dex_cache,
4129 const DexCacheData* data,
4130 ObjPtr<mirror::ClassLoader> class_loader) {
4131 CHECK(data != nullptr);
4132 DCHECK_EQ(dex_cache->GetDexFile(), data->dex_file);
4133 return data->class_table == ClassTableForClassLoader(class_loader);
4134 }
4135
RegisterExistingDexCache(ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader)4136 void ClassLinker::RegisterExistingDexCache(ObjPtr<mirror::DexCache> dex_cache,
4137 ObjPtr<mirror::ClassLoader> class_loader) {
4138 SCOPED_TRACE << __FUNCTION__ << " " << dex_cache->GetDexFile()->GetLocation();
4139 Thread* self = Thread::Current();
4140 StackHandleScope<2> hs(self);
4141 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(dex_cache));
4142 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
4143 const DexFile* dex_file = dex_cache->GetDexFile();
4144 DCHECK(dex_file != nullptr) << "Attempt to register uninitialized dex_cache object!";
4145 if (kIsDebugBuild) {
4146 ReaderMutexLock mu(self, *Locks::dex_lock_);
4147 const DexCacheData* old_data = FindDexCacheDataLocked(*dex_file);
4148 ObjPtr<mirror::DexCache> old_dex_cache = DecodeDexCacheLocked(self, old_data);
4149 DCHECK(old_dex_cache.IsNull()) << "Attempt to manually register a dex cache thats already "
4150 << "been registered on dex file " << dex_file->GetLocation();
4151 }
4152 ClassTable* table;
4153 {
4154 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
4155 table = InsertClassTableForClassLoader(h_class_loader.Get());
4156 }
4157 // Avoid a deadlock between a garbage collecting thread running a checkpoint,
4158 // a thread holding the dex lock and blocking on a condition variable regarding
4159 // weak references access, and a thread blocking on the dex lock.
4160 gc::ScopedGCCriticalSection gcs(self, gc::kGcCauseClassLinker, gc::kCollectorTypeClassLinker);
4161 WriterMutexLock mu(self, *Locks::dex_lock_);
4162 RegisterDexFileLocked(*dex_file, h_dex_cache.Get(), h_class_loader.Get());
4163 table->InsertStrongRoot(h_dex_cache.Get());
4164 if (h_class_loader.Get() != nullptr) {
4165 // Since we added a strong root to the class table, do the write barrier as required for
4166 // remembered sets and generational GCs.
4167 WriteBarrier::ForEveryFieldWrite(h_class_loader.Get());
4168 }
4169 }
4170
ThrowDexFileAlreadyRegisteredError(Thread * self,const DexFile & dex_file)4171 static void ThrowDexFileAlreadyRegisteredError(Thread* self, const DexFile& dex_file)
4172 REQUIRES_SHARED(Locks::mutator_lock_) {
4173 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
4174 "Attempt to register dex file %s with multiple class loaders",
4175 dex_file.GetLocation().c_str());
4176 }
4177
RegisterDexFile(const DexFile & dex_file,ObjPtr<mirror::ClassLoader> class_loader)4178 ObjPtr<mirror::DexCache> ClassLinker::RegisterDexFile(const DexFile& dex_file,
4179 ObjPtr<mirror::ClassLoader> class_loader) {
4180 Thread* self = Thread::Current();
4181 ObjPtr<mirror::DexCache> old_dex_cache;
4182 bool registered_with_another_class_loader = false;
4183 {
4184 ReaderMutexLock mu(self, *Locks::dex_lock_);
4185 const DexCacheData* old_data = FindDexCacheDataLocked(dex_file);
4186 old_dex_cache = DecodeDexCacheLocked(self, old_data);
4187 if (old_dex_cache != nullptr) {
4188 if (IsSameClassLoader(old_dex_cache, old_data, class_loader)) {
4189 return old_dex_cache;
4190 } else {
4191 // TODO This is not very clean looking. Should maybe try to make a way to request exceptions
4192 // be thrown when it's safe to do so to simplify this.
4193 registered_with_another_class_loader = true;
4194 }
4195 }
4196 }
4197 // We need to have released the dex_lock_ to allocate safely.
4198 if (registered_with_another_class_loader) {
4199 ThrowDexFileAlreadyRegisteredError(self, dex_file);
4200 return nullptr;
4201 }
4202 SCOPED_TRACE << __FUNCTION__ << " " << dex_file.GetLocation();
4203 LinearAlloc* const linear_alloc = GetOrCreateAllocatorForClassLoader(class_loader);
4204 DCHECK(linear_alloc != nullptr);
4205 ClassTable* table;
4206 {
4207 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
4208 table = InsertClassTableForClassLoader(class_loader);
4209 }
4210 // Don't alloc while holding the lock, since allocation may need to
4211 // suspend all threads and another thread may need the dex_lock_ to
4212 // get to a suspend point.
4213 StackHandleScope<3> hs(self);
4214 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
4215 ObjPtr<mirror::String> location;
4216 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(AllocDexCache(/*out*/&location,
4217 self,
4218 dex_file)));
4219 Handle<mirror::String> h_location(hs.NewHandle(location));
4220 {
4221 // Avoid a deadlock between a garbage collecting thread running a checkpoint,
4222 // a thread holding the dex lock and blocking on a condition variable regarding
4223 // weak references access, and a thread blocking on the dex lock.
4224 gc::ScopedGCCriticalSection gcs(self, gc::kGcCauseClassLinker, gc::kCollectorTypeClassLinker);
4225 WriterMutexLock mu(self, *Locks::dex_lock_);
4226 const DexCacheData* old_data = FindDexCacheDataLocked(dex_file);
4227 old_dex_cache = DecodeDexCacheLocked(self, old_data);
4228 if (old_dex_cache == nullptr && h_dex_cache != nullptr) {
4229 // Do InitializeDexCache while holding dex lock to make sure two threads don't call it at the
4230 // same time with the same dex cache. Since the .bss is shared this can cause failing DCHECK
4231 // that the arrays are null.
4232 mirror::DexCache::InitializeDexCache(self,
4233 h_dex_cache.Get(),
4234 h_location.Get(),
4235 &dex_file,
4236 linear_alloc,
4237 image_pointer_size_);
4238 RegisterDexFileLocked(dex_file, h_dex_cache.Get(), h_class_loader.Get());
4239 }
4240 if (old_dex_cache != nullptr) {
4241 // Another thread managed to initialize the dex cache faster, so use that DexCache.
4242 // If this thread encountered OOME, ignore it.
4243 DCHECK_EQ(h_dex_cache == nullptr, self->IsExceptionPending());
4244 self->ClearException();
4245 // We cannot call EnsureSameClassLoader() or allocate an exception while holding the
4246 // dex_lock_.
4247 if (IsSameClassLoader(old_dex_cache, old_data, h_class_loader.Get())) {
4248 return old_dex_cache;
4249 } else {
4250 registered_with_another_class_loader = true;
4251 }
4252 }
4253 }
4254 if (registered_with_another_class_loader) {
4255 ThrowDexFileAlreadyRegisteredError(self, dex_file);
4256 return nullptr;
4257 }
4258 if (h_dex_cache == nullptr) {
4259 self->AssertPendingOOMException();
4260 return nullptr;
4261 }
4262 table->InsertStrongRoot(h_dex_cache.Get());
4263 if (h_class_loader.Get() != nullptr) {
4264 // Since we added a strong root to the class table, do the write barrier as required for
4265 // remembered sets and generational GCs.
4266 WriteBarrier::ForEveryFieldWrite(h_class_loader.Get());
4267 }
4268 return h_dex_cache.Get();
4269 }
4270
IsDexFileRegistered(Thread * self,const DexFile & dex_file)4271 bool ClassLinker::IsDexFileRegistered(Thread* self, const DexFile& dex_file) {
4272 ReaderMutexLock mu(self, *Locks::dex_lock_);
4273 return DecodeDexCacheLocked(self, FindDexCacheDataLocked(dex_file)) != nullptr;
4274 }
4275
FindDexCache(Thread * self,const DexFile & dex_file)4276 ObjPtr<mirror::DexCache> ClassLinker::FindDexCache(Thread* self, const DexFile& dex_file) {
4277 ReaderMutexLock mu(self, *Locks::dex_lock_);
4278 const DexCacheData* dex_cache_data = FindDexCacheDataLocked(dex_file);
4279 ObjPtr<mirror::DexCache> dex_cache = DecodeDexCacheLocked(self, dex_cache_data);
4280 if (dex_cache != nullptr) {
4281 return dex_cache;
4282 }
4283 // Failure, dump diagnostic and abort.
4284 for (const DexCacheData& data : dex_caches_) {
4285 if (DecodeDexCacheLocked(self, &data) != nullptr) {
4286 LOG(FATAL_WITHOUT_ABORT) << "Registered dex file " << data.dex_file->GetLocation();
4287 }
4288 }
4289 LOG(FATAL) << "Failed to find DexCache for DexFile " << dex_file.GetLocation()
4290 << " " << &dex_file << " " << dex_cache_data->dex_file;
4291 UNREACHABLE();
4292 }
4293
FindClassTable(Thread * self,ObjPtr<mirror::DexCache> dex_cache)4294 ClassTable* ClassLinker::FindClassTable(Thread* self, ObjPtr<mirror::DexCache> dex_cache) {
4295 const DexFile* dex_file = dex_cache->GetDexFile();
4296 DCHECK(dex_file != nullptr);
4297 ReaderMutexLock mu(self, *Locks::dex_lock_);
4298 // Search assuming unique-ness of dex file.
4299 for (const DexCacheData& data : dex_caches_) {
4300 // Avoid decoding (and read barriers) other unrelated dex caches.
4301 if (data.dex_file == dex_file) {
4302 ObjPtr<mirror::DexCache> registered_dex_cache = DecodeDexCacheLocked(self, &data);
4303 if (registered_dex_cache != nullptr) {
4304 CHECK_EQ(registered_dex_cache, dex_cache) << dex_file->GetLocation();
4305 return data.class_table;
4306 }
4307 }
4308 }
4309 return nullptr;
4310 }
4311
FindDexCacheDataLocked(const DexFile & dex_file)4312 const ClassLinker::DexCacheData* ClassLinker::FindDexCacheDataLocked(const DexFile& dex_file) {
4313 // Search assuming unique-ness of dex file.
4314 for (const DexCacheData& data : dex_caches_) {
4315 // Avoid decoding (and read barriers) other unrelated dex caches.
4316 if (data.dex_file == &dex_file) {
4317 return &data;
4318 }
4319 }
4320 return nullptr;
4321 }
4322
CreatePrimitiveClass(Thread * self,Primitive::Type type,ClassRoot primitive_root)4323 void ClassLinker::CreatePrimitiveClass(Thread* self,
4324 Primitive::Type type,
4325 ClassRoot primitive_root) {
4326 ObjPtr<mirror::Class> primitive_class =
4327 AllocClass(self, mirror::Class::PrimitiveClassSize(image_pointer_size_));
4328 CHECK(primitive_class != nullptr) << "OOM for primitive class " << type;
4329 // Do not hold lock on the primitive class object, the initialization of
4330 // primitive classes is done while the process is still single threaded.
4331 primitive_class->SetAccessFlagsDuringLinking(
4332 kAccPublic | kAccFinal | kAccAbstract | kAccVerificationAttempted);
4333 primitive_class->SetPrimitiveType(type);
4334 primitive_class->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable());
4335 // Skip EnsureSkipAccessChecksMethods(). We can skip the verified status,
4336 // the kAccVerificationAttempted flag was added above, and there are no
4337 // methods that need the kAccSkipAccessChecks flag.
4338 DCHECK_EQ(primitive_class->NumMethods(), 0u);
4339 // Primitive classes are initialized during single threaded startup, so visibly initialized.
4340 primitive_class->SetStatusForPrimitiveOrArray(ClassStatus::kVisiblyInitialized);
4341 const char* descriptor = Primitive::Descriptor(type);
4342 ObjPtr<mirror::Class> existing = InsertClass(descriptor,
4343 primitive_class,
4344 ComputeModifiedUtf8Hash(descriptor));
4345 CHECK(existing == nullptr) << "InitPrimitiveClass(" << type << ") failed";
4346 SetClassRoot(primitive_root, primitive_class);
4347 }
4348
GetArrayIfTable()4349 inline ObjPtr<mirror::IfTable> ClassLinker::GetArrayIfTable() {
4350 return GetClassRoot<mirror::ObjectArray<mirror::Object>>(this)->GetIfTable();
4351 }
4352
4353 // Create an array class (i.e. the class object for the array, not the
4354 // array itself). "descriptor" looks like "[C" or "[[[[B" or
4355 // "[Ljava/lang/String;".
4356 //
4357 // If "descriptor" refers to an array of primitives, look up the
4358 // primitive type's internally-generated class object.
4359 //
4360 // "class_loader" is the class loader of the class that's referring to
4361 // us. It's used to ensure that we're looking for the element type in
4362 // the right context. It does NOT become the class loader for the
4363 // array class; that always comes from the base element class.
4364 //
4365 // Returns null with an exception raised on failure.
CreateArrayClass(Thread * self,const char * descriptor,size_t hash,Handle<mirror::ClassLoader> class_loader)4366 ObjPtr<mirror::Class> ClassLinker::CreateArrayClass(Thread* self,
4367 const char* descriptor,
4368 size_t hash,
4369 Handle<mirror::ClassLoader> class_loader) {
4370 // Identify the underlying component type
4371 CHECK_EQ('[', descriptor[0]);
4372 StackHandleScope<2> hs(self);
4373
4374 // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied
4375 // code to be executed. We put it up here so we can avoid all the allocations associated with
4376 // creating the class. This can happen with (eg) jit threads.
4377 if (!self->CanLoadClasses()) {
4378 // Make sure we don't try to load anything, potentially causing an infinite loop.
4379 ObjPtr<mirror::Throwable> pre_allocated =
4380 Runtime::Current()->GetPreAllocatedNoClassDefFoundError();
4381 self->SetException(pre_allocated);
4382 return nullptr;
4383 }
4384
4385 MutableHandle<mirror::Class> component_type(hs.NewHandle(FindClass(self, descriptor + 1,
4386 class_loader)));
4387 if (component_type == nullptr) {
4388 DCHECK(self->IsExceptionPending());
4389 // We need to accept erroneous classes as component types.
4390 const size_t component_hash = ComputeModifiedUtf8Hash(descriptor + 1);
4391 component_type.Assign(LookupClass(self, descriptor + 1, component_hash, class_loader.Get()));
4392 if (component_type == nullptr) {
4393 DCHECK(self->IsExceptionPending());
4394 return nullptr;
4395 } else {
4396 self->ClearException();
4397 }
4398 }
4399 if (UNLIKELY(component_type->IsPrimitiveVoid())) {
4400 ThrowNoClassDefFoundError("Attempt to create array of void primitive type");
4401 return nullptr;
4402 }
4403 // See if the component type is already loaded. Array classes are
4404 // always associated with the class loader of their underlying
4405 // element type -- an array of Strings goes with the loader for
4406 // java/lang/String -- so we need to look for it there. (The
4407 // caller should have checked for the existence of the class
4408 // before calling here, but they did so with *their* class loader,
4409 // not the component type's loader.)
4410 //
4411 // If we find it, the caller adds "loader" to the class' initiating
4412 // loader list, which should prevent us from going through this again.
4413 //
4414 // This call is unnecessary if "loader" and "component_type->GetClassLoader()"
4415 // are the same, because our caller (FindClass) just did the
4416 // lookup. (Even if we get this wrong we still have correct behavior,
4417 // because we effectively do this lookup again when we add the new
4418 // class to the hash table --- necessary because of possible races with
4419 // other threads.)
4420 if (class_loader.Get() != component_type->GetClassLoader()) {
4421 ObjPtr<mirror::Class> new_class =
4422 LookupClass(self, descriptor, hash, component_type->GetClassLoader());
4423 if (new_class != nullptr) {
4424 return new_class;
4425 }
4426 }
4427 // Core array classes, i.e. Object[], Class[], String[] and primitive
4428 // arrays, have special initialization and they should be found above.
4429 DCHECK(!component_type->IsObjectClass() ||
4430 // Guard from false positives for errors before setting superclass.
4431 component_type->IsErroneousUnresolved());
4432 DCHECK(!component_type->IsStringClass());
4433 DCHECK(!component_type->IsClassClass());
4434 DCHECK(!component_type->IsPrimitive());
4435
4436 // Fill out the fields in the Class.
4437 //
4438 // It is possible to execute some methods against arrays, because
4439 // all arrays are subclasses of java_lang_Object_, so we need to set
4440 // up a vtable. We can just point at the one in java_lang_Object_.
4441 //
4442 // Array classes are simple enough that we don't need to do a full
4443 // link step.
4444 size_t array_class_size = mirror::Array::ClassSize(image_pointer_size_);
4445 auto visitor = [this, array_class_size, component_type](ObjPtr<mirror::Object> obj,
4446 size_t usable_size)
4447 REQUIRES_SHARED(Locks::mutator_lock_) {
4448 ScopedAssertNoNewTransactionRecords sanntr("CreateArrayClass");
4449 mirror::Class::InitializeClassVisitor init_class(array_class_size);
4450 init_class(obj, usable_size);
4451 ObjPtr<mirror::Class> klass = ObjPtr<mirror::Class>::DownCast(obj);
4452 klass->SetComponentType(component_type.Get());
4453 // Do not hold lock for initialization, the fence issued after the visitor
4454 // returns ensures memory visibility together with the implicit consume
4455 // semantics (for all supported architectures) for any thread that loads
4456 // the array class reference from any memory locations afterwards.
4457 FinishArrayClassSetup(klass);
4458 };
4459 auto new_class = hs.NewHandle<mirror::Class>(
4460 AllocClass(self, GetClassRoot<mirror::Class>(this), array_class_size, visitor));
4461 if (new_class == nullptr) {
4462 self->AssertPendingOOMException();
4463 return nullptr;
4464 }
4465
4466 ObjPtr<mirror::Class> existing = InsertClass(descriptor, new_class.Get(), hash);
4467 if (existing == nullptr) {
4468 // We postpone ClassLoad and ClassPrepare events to this point in time to avoid
4469 // duplicate events in case of races. Array classes don't really follow dedicated
4470 // load and prepare, anyways.
4471 Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(new_class);
4472 Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(new_class, new_class);
4473
4474 jit::Jit::NewTypeLoadedIfUsingJit(new_class.Get());
4475 return new_class.Get();
4476 }
4477 // Another thread must have loaded the class after we
4478 // started but before we finished. Abandon what we've
4479 // done.
4480 //
4481 // (Yes, this happens.)
4482
4483 return existing;
4484 }
4485
LookupPrimitiveClass(char type)4486 ObjPtr<mirror::Class> ClassLinker::LookupPrimitiveClass(char type) {
4487 ClassRoot class_root;
4488 switch (type) {
4489 case 'B': class_root = ClassRoot::kPrimitiveByte; break;
4490 case 'C': class_root = ClassRoot::kPrimitiveChar; break;
4491 case 'D': class_root = ClassRoot::kPrimitiveDouble; break;
4492 case 'F': class_root = ClassRoot::kPrimitiveFloat; break;
4493 case 'I': class_root = ClassRoot::kPrimitiveInt; break;
4494 case 'J': class_root = ClassRoot::kPrimitiveLong; break;
4495 case 'S': class_root = ClassRoot::kPrimitiveShort; break;
4496 case 'Z': class_root = ClassRoot::kPrimitiveBoolean; break;
4497 case 'V': class_root = ClassRoot::kPrimitiveVoid; break;
4498 default:
4499 return nullptr;
4500 }
4501 return GetClassRoot(class_root, this);
4502 }
4503
FindPrimitiveClass(char type)4504 ObjPtr<mirror::Class> ClassLinker::FindPrimitiveClass(char type) {
4505 ObjPtr<mirror::Class> result = LookupPrimitiveClass(type);
4506 if (UNLIKELY(result == nullptr)) {
4507 std::string printable_type(PrintableChar(type));
4508 ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str());
4509 }
4510 return result;
4511 }
4512
InsertClass(const char * descriptor,ObjPtr<mirror::Class> klass,size_t hash)4513 ObjPtr<mirror::Class> ClassLinker::InsertClass(const char* descriptor,
4514 ObjPtr<mirror::Class> klass,
4515 size_t hash) {
4516 DCHECK(Thread::Current()->CanLoadClasses());
4517 if (VLOG_IS_ON(class_linker)) {
4518 ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache();
4519 std::string source;
4520 if (dex_cache != nullptr) {
4521 source += " from ";
4522 source += dex_cache->GetLocation()->ToModifiedUtf8();
4523 }
4524 LOG(INFO) << "Loaded class " << descriptor << source;
4525 }
4526 {
4527 WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
4528 const ObjPtr<mirror::ClassLoader> class_loader = klass->GetClassLoader();
4529 ClassTable* const class_table = InsertClassTableForClassLoader(class_loader);
4530 ObjPtr<mirror::Class> existing = class_table->Lookup(descriptor, hash);
4531 if (existing != nullptr) {
4532 return existing;
4533 }
4534 VerifyObject(klass);
4535 class_table->InsertWithHash(klass, hash);
4536 if (class_loader != nullptr) {
4537 // This is necessary because we need to have the card dirtied for remembered sets.
4538 WriteBarrier::ForEveryFieldWrite(class_loader);
4539 }
4540 if (log_new_roots_) {
4541 new_class_roots_.push_back(GcRoot<mirror::Class>(klass));
4542 }
4543 }
4544 if (kIsDebugBuild) {
4545 // Test that copied methods correctly can find their holder.
4546 for (ArtMethod& method : klass->GetCopiedMethods(image_pointer_size_)) {
4547 CHECK_EQ(GetHoldingClassOfCopiedMethod(&method), klass);
4548 }
4549 }
4550 return nullptr;
4551 }
4552
WriteBarrierForBootOatFileBssRoots(const OatFile * oat_file)4553 void ClassLinker::WriteBarrierForBootOatFileBssRoots(const OatFile* oat_file) {
4554 WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
4555 DCHECK(!oat_file->GetBssGcRoots().empty()) << oat_file->GetLocation();
4556 if (log_new_roots_ && !ContainsElement(new_bss_roots_boot_oat_files_, oat_file)) {
4557 new_bss_roots_boot_oat_files_.push_back(oat_file);
4558 }
4559 }
4560
4561 // TODO This should really be in mirror::Class.
UpdateClassMethods(ObjPtr<mirror::Class> klass,LengthPrefixedArray<ArtMethod> * new_methods)4562 void ClassLinker::UpdateClassMethods(ObjPtr<mirror::Class> klass,
4563 LengthPrefixedArray<ArtMethod>* new_methods) {
4564 klass->SetMethodsPtrUnchecked(new_methods,
4565 klass->NumDirectMethods(),
4566 klass->NumDeclaredVirtualMethods());
4567 // Need to mark the card so that the remembered sets and mod union tables get updated.
4568 WriteBarrier::ForEveryFieldWrite(klass);
4569 }
4570
LookupClass(Thread * self,const char * descriptor,ObjPtr<mirror::ClassLoader> class_loader)4571 ObjPtr<mirror::Class> ClassLinker::LookupClass(Thread* self,
4572 const char* descriptor,
4573 ObjPtr<mirror::ClassLoader> class_loader) {
4574 return LookupClass(self, descriptor, ComputeModifiedUtf8Hash(descriptor), class_loader);
4575 }
4576
LookupClass(Thread * self,const char * descriptor,size_t hash,ObjPtr<mirror::ClassLoader> class_loader)4577 ObjPtr<mirror::Class> ClassLinker::LookupClass(Thread* self,
4578 const char* descriptor,
4579 size_t hash,
4580 ObjPtr<mirror::ClassLoader> class_loader) {
4581 ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_);
4582 ClassTable* const class_table = ClassTableForClassLoader(class_loader);
4583 if (class_table != nullptr) {
4584 ObjPtr<mirror::Class> result = class_table->Lookup(descriptor, hash);
4585 if (result != nullptr) {
4586 return result;
4587 }
4588 }
4589 return nullptr;
4590 }
4591
4592 class MoveClassTableToPreZygoteVisitor : public ClassLoaderVisitor {
4593 public:
MoveClassTableToPreZygoteVisitor()4594 MoveClassTableToPreZygoteVisitor() {}
4595
Visit(ObjPtr<mirror::ClassLoader> class_loader)4596 void Visit(ObjPtr<mirror::ClassLoader> class_loader)
4597 REQUIRES(Locks::classlinker_classes_lock_)
4598 REQUIRES_SHARED(Locks::mutator_lock_) override {
4599 ClassTable* const class_table = class_loader->GetClassTable();
4600 if (class_table != nullptr) {
4601 class_table->FreezeSnapshot();
4602 }
4603 }
4604 };
4605
MoveClassTableToPreZygote()4606 void ClassLinker::MoveClassTableToPreZygote() {
4607 WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
4608 boot_class_table_->FreezeSnapshot();
4609 MoveClassTableToPreZygoteVisitor visitor;
4610 VisitClassLoaders(&visitor);
4611 }
4612
4613 // Look up classes by hash and descriptor and put all matching ones in the result array.
4614 class LookupClassesVisitor : public ClassLoaderVisitor {
4615 public:
LookupClassesVisitor(const char * descriptor,size_t hash,std::vector<ObjPtr<mirror::Class>> * result)4616 LookupClassesVisitor(const char* descriptor,
4617 size_t hash,
4618 std::vector<ObjPtr<mirror::Class>>* result)
4619 : descriptor_(descriptor),
4620 hash_(hash),
4621 result_(result) {}
4622
Visit(ObjPtr<mirror::ClassLoader> class_loader)4623 void Visit(ObjPtr<mirror::ClassLoader> class_loader)
4624 REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override {
4625 ClassTable* const class_table = class_loader->GetClassTable();
4626 ObjPtr<mirror::Class> klass = class_table->Lookup(descriptor_, hash_);
4627 // Add `klass` only if `class_loader` is its defining (not just initiating) class loader.
4628 if (klass != nullptr && klass->GetClassLoader() == class_loader) {
4629 result_->push_back(klass);
4630 }
4631 }
4632
4633 private:
4634 const char* const descriptor_;
4635 const size_t hash_;
4636 std::vector<ObjPtr<mirror::Class>>* const result_;
4637 };
4638
LookupClasses(const char * descriptor,std::vector<ObjPtr<mirror::Class>> & result)4639 void ClassLinker::LookupClasses(const char* descriptor,
4640 std::vector<ObjPtr<mirror::Class>>& result) {
4641 result.clear();
4642 Thread* const self = Thread::Current();
4643 ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_);
4644 const size_t hash = ComputeModifiedUtf8Hash(descriptor);
4645 ObjPtr<mirror::Class> klass = boot_class_table_->Lookup(descriptor, hash);
4646 if (klass != nullptr) {
4647 DCHECK(klass->GetClassLoader() == nullptr);
4648 result.push_back(klass);
4649 }
4650 LookupClassesVisitor visitor(descriptor, hash, &result);
4651 VisitClassLoaders(&visitor);
4652 }
4653
AttemptSupertypeVerification(Thread * self,Handle<mirror::Class> klass,Handle<mirror::Class> supertype)4654 bool ClassLinker::AttemptSupertypeVerification(Thread* self,
4655 Handle<mirror::Class> klass,
4656 Handle<mirror::Class> supertype) {
4657 DCHECK(self != nullptr);
4658 DCHECK(klass != nullptr);
4659 DCHECK(supertype != nullptr);
4660
4661 if (!supertype->IsVerified() && !supertype->IsErroneous()) {
4662 VerifyClass(self, supertype);
4663 }
4664
4665 if (supertype->IsVerified()
4666 || supertype->ShouldVerifyAtRuntime()
4667 || supertype->IsVerifiedNeedsAccessChecks()) {
4668 // The supertype is either verified, or we soft failed at AOT time.
4669 DCHECK(supertype->IsVerified() || Runtime::Current()->IsAotCompiler());
4670 return true;
4671 }
4672 // If we got this far then we have a hard failure.
4673 std::string error_msg =
4674 StringPrintf("Rejecting class %s that attempts to sub-type erroneous class %s",
4675 klass->PrettyDescriptor().c_str(),
4676 supertype->PrettyDescriptor().c_str());
4677 LOG(WARNING) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8();
4678 StackHandleScope<1> hs(self);
4679 Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException()));
4680 if (cause != nullptr) {
4681 // Set during VerifyClass call (if at all).
4682 self->ClearException();
4683 }
4684 // Change into a verify error.
4685 ThrowVerifyError(klass.Get(), "%s", error_msg.c_str());
4686 if (cause != nullptr) {
4687 self->GetException()->SetCause(cause.Get());
4688 }
4689 ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex());
4690 if (Runtime::Current()->IsAotCompiler()) {
4691 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
4692 }
4693 // Need to grab the lock to change status.
4694 ObjectLock<mirror::Class> super_lock(self, klass);
4695 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
4696 return false;
4697 }
4698
VerifyClass(Thread * self,Handle<mirror::Class> klass,verifier::HardFailLogMode log_level)4699 verifier::FailureKind ClassLinker::VerifyClass(
4700 Thread* self, Handle<mirror::Class> klass, verifier::HardFailLogMode log_level) {
4701 {
4702 // TODO: assert that the monitor on the Class is held
4703 ObjectLock<mirror::Class> lock(self, klass);
4704
4705 // Is somebody verifying this now?
4706 ClassStatus old_status = klass->GetStatus();
4707 while (old_status == ClassStatus::kVerifying) {
4708 lock.WaitIgnoringInterrupts();
4709 // WaitIgnoringInterrupts can still receive an interrupt and return early, in this
4710 // case we may see the same status again. b/62912904. This is why the check is
4711 // greater or equal.
4712 CHECK(klass->IsErroneous() || (klass->GetStatus() >= old_status))
4713 << "Class '" << klass->PrettyClass()
4714 << "' performed an illegal verification state transition from " << old_status
4715 << " to " << klass->GetStatus();
4716 old_status = klass->GetStatus();
4717 }
4718
4719 // The class might already be erroneous, for example at compile time if we attempted to verify
4720 // this class as a parent to another.
4721 if (klass->IsErroneous()) {
4722 ThrowEarlierClassFailure(klass.Get());
4723 return verifier::FailureKind::kHardFailure;
4724 }
4725
4726 // Don't attempt to re-verify if already verified.
4727 if (klass->IsVerified()) {
4728 EnsureSkipAccessChecksMethods(klass, image_pointer_size_);
4729 return verifier::FailureKind::kNoFailure;
4730 }
4731
4732 if (klass->IsVerifiedNeedsAccessChecks()) {
4733 if (!Runtime::Current()->IsAotCompiler()) {
4734 // Mark the class as having a verification attempt to avoid re-running
4735 // the verifier and avoid calling EnsureSkipAccessChecksMethods.
4736 klass->SetVerificationAttempted();
4737 mirror::Class::SetStatus(klass, ClassStatus::kVerified, self);
4738 }
4739 return verifier::FailureKind::kAccessChecksFailure;
4740 }
4741
4742 // For AOT, don't attempt to re-verify if we have already found we should
4743 // verify at runtime.
4744 if (klass->ShouldVerifyAtRuntime()) {
4745 CHECK(Runtime::Current()->IsAotCompiler());
4746 return verifier::FailureKind::kSoftFailure;
4747 }
4748
4749 DCHECK_EQ(klass->GetStatus(), ClassStatus::kResolved);
4750 mirror::Class::SetStatus(klass, ClassStatus::kVerifying, self);
4751
4752 // Skip verification if disabled.
4753 if (!Runtime::Current()->IsVerificationEnabled()) {
4754 mirror::Class::SetStatus(klass, ClassStatus::kVerified, self);
4755 EnsureSkipAccessChecksMethods(klass, image_pointer_size_);
4756 return verifier::FailureKind::kNoFailure;
4757 }
4758 }
4759
4760 VLOG(class_linker) << "Beginning verification for class: "
4761 << klass->PrettyDescriptor()
4762 << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8();
4763
4764 // Verify super class.
4765 StackHandleScope<2> hs(self);
4766 MutableHandle<mirror::Class> supertype(hs.NewHandle(klass->GetSuperClass()));
4767 // If we have a superclass and we get a hard verification failure we can return immediately.
4768 if (supertype != nullptr && !AttemptSupertypeVerification(self, klass, supertype)) {
4769 CHECK(self->IsExceptionPending()) << "Verification error should be pending.";
4770 return verifier::FailureKind::kHardFailure;
4771 }
4772
4773 // Verify all default super-interfaces.
4774 //
4775 // (1) Don't bother if the superclass has already had a soft verification failure.
4776 //
4777 // (2) Interfaces shouldn't bother to do this recursive verification because they cannot cause
4778 // recursive initialization by themselves. This is because when an interface is initialized
4779 // directly it must not initialize its superinterfaces. We are allowed to verify regardless
4780 // but choose not to for an optimization. If the interfaces is being verified due to a class
4781 // initialization (which would need all the default interfaces to be verified) the class code
4782 // will trigger the recursive verification anyway.
4783 if ((supertype == nullptr || supertype->IsVerified()) // See (1)
4784 && !klass->IsInterface()) { // See (2)
4785 int32_t iftable_count = klass->GetIfTableCount();
4786 MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr));
4787 // Loop through all interfaces this class has defined. It doesn't matter the order.
4788 for (int32_t i = 0; i < iftable_count; i++) {
4789 iface.Assign(klass->GetIfTable()->GetInterface(i));
4790 DCHECK(iface != nullptr);
4791 // We only care if we have default interfaces and can skip if we are already verified...
4792 if (LIKELY(!iface->HasDefaultMethods() || iface->IsVerified())) {
4793 continue;
4794 } else if (UNLIKELY(!AttemptSupertypeVerification(self, klass, iface))) {
4795 // We had a hard failure while verifying this interface. Just return immediately.
4796 CHECK(self->IsExceptionPending()) << "Verification error should be pending.";
4797 return verifier::FailureKind::kHardFailure;
4798 } else if (UNLIKELY(!iface->IsVerified())) {
4799 // We softly failed to verify the iface. Stop checking and clean up.
4800 // Put the iface into the supertype handle so we know what caused us to fail.
4801 supertype.Assign(iface.Get());
4802 break;
4803 }
4804 }
4805 }
4806
4807 // At this point if verification failed, then supertype is the "first" supertype that failed
4808 // verification (without a specific order). If verification succeeded, then supertype is either
4809 // null or the original superclass of klass and is verified.
4810 DCHECK(supertype == nullptr ||
4811 supertype.Get() == klass->GetSuperClass() ||
4812 !supertype->IsVerified());
4813
4814 // Try to use verification information from the oat file, otherwise do runtime verification.
4815 const DexFile& dex_file = *klass->GetDexCache()->GetDexFile();
4816 ClassStatus oat_file_class_status(ClassStatus::kNotReady);
4817 bool preverified = VerifyClassUsingOatFile(dex_file, klass.Get(), oat_file_class_status);
4818
4819 VLOG(class_linker) << "Class preverified status for class "
4820 << klass->PrettyDescriptor()
4821 << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8()
4822 << ": "
4823 << preverified
4824 << "( " << oat_file_class_status << ")";
4825
4826 // If the oat file says the class had an error, re-run the verifier. That way we will get a
4827 // precise error message. To ensure a rerun, test:
4828 // mirror::Class::IsErroneous(oat_file_class_status) => !preverified
4829 DCHECK(!mirror::Class::IsErroneous(oat_file_class_status) || !preverified);
4830
4831 std::string error_msg;
4832 verifier::FailureKind verifier_failure = verifier::FailureKind::kNoFailure;
4833 if (!preverified) {
4834 verifier_failure = PerformClassVerification(self, klass, log_level, &error_msg);
4835 }
4836
4837 // Verification is done, grab the lock again.
4838 ObjectLock<mirror::Class> lock(self, klass);
4839
4840 if (preverified || verifier_failure != verifier::FailureKind::kHardFailure) {
4841 if (!preverified && verifier_failure != verifier::FailureKind::kNoFailure) {
4842 VLOG(class_linker) << "Soft verification failure in class "
4843 << klass->PrettyDescriptor()
4844 << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8()
4845 << " because: " << error_msg;
4846 }
4847 self->AssertNoPendingException();
4848 // Make sure all classes referenced by catch blocks are resolved.
4849 ResolveClassExceptionHandlerTypes(klass);
4850 if (verifier_failure == verifier::FailureKind::kNoFailure) {
4851 // Even though there were no verifier failures we need to respect whether the super-class and
4852 // super-default-interfaces were verified or requiring runtime reverification.
4853 if (supertype == nullptr
4854 || supertype->IsVerified()
4855 || supertype->IsVerifiedNeedsAccessChecks()) {
4856 mirror::Class::SetStatus(klass, ClassStatus::kVerified, self);
4857 } else {
4858 CHECK(Runtime::Current()->IsAotCompiler());
4859 CHECK_EQ(supertype->GetStatus(), ClassStatus::kRetryVerificationAtRuntime);
4860 mirror::Class::SetStatus(klass, ClassStatus::kRetryVerificationAtRuntime, self);
4861 // Pretend a soft failure occurred so that we don't consider the class verified below.
4862 verifier_failure = verifier::FailureKind::kSoftFailure;
4863 }
4864 } else {
4865 CHECK(verifier_failure == verifier::FailureKind::kSoftFailure ||
4866 verifier_failure == verifier::FailureKind::kAccessChecksFailure);
4867 // Soft failures at compile time should be retried at runtime. Soft
4868 // failures at runtime will be handled by slow paths in the generated
4869 // code. Set status accordingly.
4870 if (Runtime::Current()->IsAotCompiler()) {
4871 if (verifier_failure == verifier::FailureKind::kSoftFailure) {
4872 mirror::Class::SetStatus(klass, ClassStatus::kRetryVerificationAtRuntime, self);
4873 } else {
4874 mirror::Class::SetStatus(klass, ClassStatus::kVerifiedNeedsAccessChecks, self);
4875 }
4876 } else {
4877 mirror::Class::SetStatus(klass, ClassStatus::kVerified, self);
4878 // As this is a fake verified status, make sure the methods are _not_ marked
4879 // kAccSkipAccessChecks later.
4880 klass->SetVerificationAttempted();
4881 }
4882 }
4883 } else {
4884 VLOG(verifier) << "Verification failed on class " << klass->PrettyDescriptor()
4885 << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8()
4886 << " because: " << error_msg;
4887 self->AssertNoPendingException();
4888 ThrowVerifyError(klass.Get(), "%s", error_msg.c_str());
4889 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
4890 }
4891 if (preverified || verifier_failure == verifier::FailureKind::kNoFailure) {
4892 if (oat_file_class_status == ClassStatus::kVerifiedNeedsAccessChecks ||
4893 UNLIKELY(Runtime::Current()->IsVerificationSoftFail())) {
4894 // Never skip access checks if the verification soft fail is forced.
4895 // Mark the class as having a verification attempt to avoid re-running the verifier.
4896 klass->SetVerificationAttempted();
4897 } else {
4898 // Class is verified so we don't need to do any access check on its methods.
4899 // Let the interpreter know it by setting the kAccSkipAccessChecks flag onto each
4900 // method.
4901 // Note: we're going here during compilation and at runtime. When we set the
4902 // kAccSkipAccessChecks flag when compiling image classes, the flag is recorded
4903 // in the image and is set when loading the image.
4904 EnsureSkipAccessChecksMethods(klass, image_pointer_size_);
4905 }
4906 }
4907 // Done verifying. Notify the compiler about the verification status, in case the class
4908 // was verified implicitly (eg super class of a compiled class).
4909 if (Runtime::Current()->IsAotCompiler()) {
4910 Runtime::Current()->GetCompilerCallbacks()->UpdateClassState(
4911 ClassReference(&klass->GetDexFile(), klass->GetDexClassDefIndex()), klass->GetStatus());
4912 }
4913 return verifier_failure;
4914 }
4915
PerformClassVerification(Thread * self,Handle<mirror::Class> klass,verifier::HardFailLogMode log_level,std::string * error_msg)4916 verifier::FailureKind ClassLinker::PerformClassVerification(Thread* self,
4917 Handle<mirror::Class> klass,
4918 verifier::HardFailLogMode log_level,
4919 std::string* error_msg) {
4920 Runtime* const runtime = Runtime::Current();
4921 return verifier::ClassVerifier::VerifyClass(self,
4922 klass.Get(),
4923 runtime->GetCompilerCallbacks(),
4924 runtime->IsAotCompiler(),
4925 log_level,
4926 Runtime::Current()->GetTargetSdkVersion(),
4927 error_msg);
4928 }
4929
VerifyClassUsingOatFile(const DexFile & dex_file,ObjPtr<mirror::Class> klass,ClassStatus & oat_file_class_status)4930 bool ClassLinker::VerifyClassUsingOatFile(const DexFile& dex_file,
4931 ObjPtr<mirror::Class> klass,
4932 ClassStatus& oat_file_class_status) {
4933 // If we're compiling, we can only verify the class using the oat file if
4934 // we are not compiling the image or if the class we're verifying is not part of
4935 // the compilation unit (app - dependencies). We will let the compiler callback
4936 // tell us about the latter.
4937 if (Runtime::Current()->IsAotCompiler()) {
4938 CompilerCallbacks* callbacks = Runtime::Current()->GetCompilerCallbacks();
4939 // We are compiling an app (not the image).
4940 if (!callbacks->CanUseOatStatusForVerification(klass.Ptr())) {
4941 return false;
4942 }
4943 }
4944
4945 const OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
4946 // In case we run without an image there won't be a backing oat file.
4947 if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) {
4948 return false;
4949 }
4950
4951 uint16_t class_def_index = klass->GetDexClassDefIndex();
4952 oat_file_class_status = oat_dex_file->GetOatClass(class_def_index).GetStatus();
4953 if (oat_file_class_status >= ClassStatus::kVerified) {
4954 return true;
4955 }
4956 if (oat_file_class_status >= ClassStatus::kVerifiedNeedsAccessChecks) {
4957 // We return that the clas has already been verified, and the caller should
4958 // check the class status to ensure we run with access checks.
4959 return true;
4960 }
4961 // If we only verified a subset of the classes at compile time, we can end up with classes that
4962 // were resolved by the verifier.
4963 if (oat_file_class_status == ClassStatus::kResolved) {
4964 return false;
4965 }
4966 // We never expect a .oat file to have kRetryVerificationAtRuntime statuses.
4967 CHECK_NE(oat_file_class_status, ClassStatus::kRetryVerificationAtRuntime)
4968 << klass->PrettyClass() << " " << dex_file.GetLocation();
4969
4970 if (mirror::Class::IsErroneous(oat_file_class_status)) {
4971 // Compile time verification failed with a hard error. This is caused by invalid instructions
4972 // in the class. These errors are unrecoverable.
4973 return false;
4974 }
4975 if (oat_file_class_status == ClassStatus::kNotReady) {
4976 // Status is uninitialized if we couldn't determine the status at compile time, for example,
4977 // not loading the class.
4978 // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy
4979 // isn't a problem and this case shouldn't occur
4980 return false;
4981 }
4982 std::string temp;
4983 LOG(FATAL) << "Unexpected class status: " << oat_file_class_status
4984 << " " << dex_file.GetLocation() << " " << klass->PrettyClass() << " "
4985 << klass->GetDescriptor(&temp);
4986 UNREACHABLE();
4987 }
4988
ResolveClassExceptionHandlerTypes(Handle<mirror::Class> klass)4989 void ClassLinker::ResolveClassExceptionHandlerTypes(Handle<mirror::Class> klass) {
4990 for (ArtMethod& method : klass->GetMethods(image_pointer_size_)) {
4991 ResolveMethodExceptionHandlerTypes(&method);
4992 }
4993 }
4994
ResolveMethodExceptionHandlerTypes(ArtMethod * method)4995 void ClassLinker::ResolveMethodExceptionHandlerTypes(ArtMethod* method) {
4996 // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod.
4997 CodeItemDataAccessor accessor(method->DexInstructionData());
4998 if (!accessor.HasCodeItem()) {
4999 return; // native or abstract method
5000 }
5001 if (accessor.TriesSize() == 0) {
5002 return; // nothing to process
5003 }
5004 const uint8_t* handlers_ptr = accessor.GetCatchHandlerData(0);
5005 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
5006 for (uint32_t idx = 0; idx < handlers_size; idx++) {
5007 CatchHandlerIterator iterator(handlers_ptr);
5008 for (; iterator.HasNext(); iterator.Next()) {
5009 // Ensure exception types are resolved so that they don't need resolution to be delivered,
5010 // unresolved exception types will be ignored by exception delivery
5011 if (iterator.GetHandlerTypeIndex().IsValid()) {
5012 ObjPtr<mirror::Class> exception_type = ResolveType(iterator.GetHandlerTypeIndex(), method);
5013 if (exception_type == nullptr) {
5014 DCHECK(Thread::Current()->IsExceptionPending());
5015 Thread::Current()->ClearException();
5016 }
5017 }
5018 }
5019 handlers_ptr = iterator.EndDataPointer();
5020 }
5021 }
5022
CreateProxyClass(ScopedObjectAccessAlreadyRunnable & soa,jstring name,jobjectArray interfaces,jobject loader,jobjectArray methods,jobjectArray throws)5023 ObjPtr<mirror::Class> ClassLinker::CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa,
5024 jstring name,
5025 jobjectArray interfaces,
5026 jobject loader,
5027 jobjectArray methods,
5028 jobjectArray throws) {
5029 Thread* self = soa.Self();
5030
5031 // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied
5032 // code to be executed. We put it up here so we can avoid all the allocations associated with
5033 // creating the class. This can happen with (eg) jit-threads.
5034 if (!self->CanLoadClasses()) {
5035 // Make sure we don't try to load anything, potentially causing an infinite loop.
5036 ObjPtr<mirror::Throwable> pre_allocated =
5037 Runtime::Current()->GetPreAllocatedNoClassDefFoundError();
5038 self->SetException(pre_allocated);
5039 return nullptr;
5040 }
5041
5042 StackHandleScope<12> hs(self);
5043 MutableHandle<mirror::Class> temp_klass(hs.NewHandle(
5044 AllocClass(self, GetClassRoot<mirror::Class>(this), sizeof(mirror::Class))));
5045 if (temp_klass == nullptr) {
5046 CHECK(self->IsExceptionPending()); // OOME.
5047 return nullptr;
5048 }
5049 DCHECK(temp_klass->GetClass() != nullptr);
5050 temp_klass->SetObjectSize(sizeof(mirror::Proxy));
5051 // Set the class access flags incl. VerificationAttempted, so we do not try to set the flag on
5052 // the methods.
5053 temp_klass->SetAccessFlagsDuringLinking(
5054 kAccClassIsProxy | kAccPublic | kAccFinal | kAccVerificationAttempted);
5055 temp_klass->SetClassLoader(soa.Decode<mirror::ClassLoader>(loader));
5056 DCHECK_EQ(temp_klass->GetPrimitiveType(), Primitive::kPrimNot);
5057 temp_klass->SetName(soa.Decode<mirror::String>(name));
5058 temp_klass->SetDexCache(GetClassRoot<mirror::Proxy>(this)->GetDexCache());
5059 // Object has an empty iftable, copy it for that reason.
5060 temp_klass->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable());
5061 mirror::Class::SetStatus(temp_klass, ClassStatus::kIdx, self);
5062 std::string storage;
5063 const char* descriptor = temp_klass->GetDescriptor(&storage);
5064 const size_t hash = ComputeModifiedUtf8Hash(descriptor);
5065
5066 // Needs to be before we insert the class so that the allocator field is set.
5067 LinearAlloc* const allocator = GetOrCreateAllocatorForClassLoader(temp_klass->GetClassLoader());
5068
5069 // Insert the class before loading the fields as the field roots
5070 // (ArtField::declaring_class_) are only visited from the class
5071 // table. There can't be any suspend points between inserting the
5072 // class and setting the field arrays below.
5073 ObjPtr<mirror::Class> existing = InsertClass(descriptor, temp_klass.Get(), hash);
5074 CHECK(existing == nullptr);
5075
5076 // Instance fields are inherited, but we add a couple of static fields...
5077 const size_t num_fields = 2;
5078 LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, allocator, num_fields);
5079 temp_klass->SetSFieldsPtr(sfields);
5080
5081 // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by
5082 // our proxy, so Class.getInterfaces doesn't return the flattened set.
5083 ArtField& interfaces_sfield = sfields->At(0);
5084 interfaces_sfield.SetDexFieldIndex(0);
5085 interfaces_sfield.SetDeclaringClass(temp_klass.Get());
5086 interfaces_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal);
5087
5088 // 2. Create a static field 'throws' that holds exceptions thrown by our methods.
5089 ArtField& throws_sfield = sfields->At(1);
5090 throws_sfield.SetDexFieldIndex(1);
5091 throws_sfield.SetDeclaringClass(temp_klass.Get());
5092 throws_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal);
5093
5094 // Proxies have 1 direct method, the constructor
5095 const size_t num_direct_methods = 1;
5096
5097 // The array we get passed contains all methods, including private and static
5098 // ones that aren't proxied. We need to filter those out since only interface
5099 // methods (non-private & virtual) are actually proxied.
5100 Handle<mirror::ObjectArray<mirror::Method>> h_methods =
5101 hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Method>>(methods));
5102 DCHECK_EQ(h_methods->GetClass(), GetClassRoot<mirror::ObjectArray<mirror::Method>>())
5103 << mirror::Class::PrettyClass(h_methods->GetClass());
5104 // List of the actual virtual methods this class will have.
5105 std::vector<ArtMethod*> proxied_methods;
5106 std::vector<size_t> proxied_throws_idx;
5107 proxied_methods.reserve(h_methods->GetLength());
5108 proxied_throws_idx.reserve(h_methods->GetLength());
5109 // Filter out to only the non-private virtual methods.
5110 for (auto [mirror, idx] : ZipCount(h_methods.Iterate<mirror::Method>())) {
5111 ArtMethod* m = mirror->GetArtMethod();
5112 if (!m->IsPrivate() && !m->IsStatic()) {
5113 proxied_methods.push_back(m);
5114 proxied_throws_idx.push_back(idx);
5115 }
5116 }
5117 const size_t num_virtual_methods = proxied_methods.size();
5118 // We also need to filter out the 'throws'. The 'throws' are a Class[][] that
5119 // contains an array of all the classes each function is declared to throw.
5120 // This is used to wrap unexpected exceptions in a
5121 // UndeclaredThrowableException exception. This array is in the same order as
5122 // the methods array and like the methods array must be filtered to remove any
5123 // non-proxied methods.
5124 const bool has_filtered_methods =
5125 static_cast<int32_t>(num_virtual_methods) != h_methods->GetLength();
5126 MutableHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>> original_proxied_throws(
5127 hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>(throws)));
5128 MutableHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>> proxied_throws(
5129 hs.NewHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>(
5130 (has_filtered_methods)
5131 ? mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>::Alloc(
5132 self, original_proxied_throws->GetClass(), num_virtual_methods)
5133 : original_proxied_throws.Get()));
5134 if (proxied_throws.IsNull() && !original_proxied_throws.IsNull()) {
5135 self->AssertPendingOOMException();
5136 return nullptr;
5137 }
5138 if (has_filtered_methods) {
5139 for (auto [orig_idx, new_idx] : ZipCount(MakeIterationRange(proxied_throws_idx))) {
5140 DCHECK_LE(new_idx, orig_idx);
5141 proxied_throws->Set(new_idx, original_proxied_throws->Get(orig_idx));
5142 }
5143 }
5144
5145 // Create the methods array.
5146 LengthPrefixedArray<ArtMethod>* proxy_class_methods = AllocArtMethodArray(
5147 self, allocator, num_direct_methods + num_virtual_methods);
5148 // Currently AllocArtMethodArray cannot return null, but the OOM logic is left there in case we
5149 // want to throw OOM in the future.
5150 if (UNLIKELY(proxy_class_methods == nullptr)) {
5151 self->AssertPendingOOMException();
5152 return nullptr;
5153 }
5154 temp_klass->SetMethodsPtr(proxy_class_methods, num_direct_methods, num_virtual_methods);
5155
5156 // Create the single direct method.
5157 CreateProxyConstructor(temp_klass, temp_klass->GetDirectMethodUnchecked(0, image_pointer_size_));
5158
5159 // Create virtual method using specified prototypes.
5160 // TODO These should really use the iterators.
5161 for (size_t i = 0; i < num_virtual_methods; ++i) {
5162 auto* virtual_method = temp_klass->GetVirtualMethodUnchecked(i, image_pointer_size_);
5163 auto* prototype = proxied_methods[i];
5164 CreateProxyMethod(temp_klass, prototype, virtual_method);
5165 DCHECK(virtual_method->GetDeclaringClass() != nullptr);
5166 DCHECK(prototype->GetDeclaringClass() != nullptr);
5167 }
5168
5169 // The super class is java.lang.reflect.Proxy
5170 temp_klass->SetSuperClass(GetClassRoot<mirror::Proxy>(this));
5171 // Now effectively in the loaded state.
5172 mirror::Class::SetStatus(temp_klass, ClassStatus::kLoaded, self);
5173 self->AssertNoPendingException();
5174
5175 // At this point the class is loaded. Publish a ClassLoad event.
5176 // Note: this may be a temporary class. It is a listener's responsibility to handle this.
5177 Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(temp_klass);
5178
5179 MutableHandle<mirror::Class> klass = hs.NewHandle<mirror::Class>(nullptr);
5180 {
5181 // Must hold lock on object when resolved.
5182 ObjectLock<mirror::Class> resolution_lock(self, temp_klass);
5183 // Link the fields and virtual methods, creating vtable and iftables.
5184 // The new class will replace the old one in the class table.
5185 Handle<mirror::ObjectArray<mirror::Class>> h_interfaces(
5186 hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)));
5187 if (!LinkClass(self, descriptor, temp_klass, h_interfaces, &klass)) {
5188 mirror::Class::SetStatus(temp_klass, ClassStatus::kErrorUnresolved, self);
5189 return nullptr;
5190 }
5191 }
5192 CHECK(temp_klass->IsRetired());
5193 CHECK_NE(temp_klass.Get(), klass.Get());
5194
5195 CHECK_EQ(interfaces_sfield.GetDeclaringClass(), klass.Get());
5196 interfaces_sfield.SetObject<false>(
5197 klass.Get(),
5198 soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces));
5199 CHECK_EQ(throws_sfield.GetDeclaringClass(), klass.Get());
5200 throws_sfield.SetObject<false>(
5201 klass.Get(),
5202 proxied_throws.Get());
5203
5204 Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(temp_klass, klass);
5205
5206 // SubtypeCheckInfo::Initialized must happen-before any new-instance for that type.
5207 // See also ClassLinker::EnsureInitialized().
5208 if (kBitstringSubtypeCheckEnabled) {
5209 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
5210 SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(klass.Get());
5211 // TODO: Avoid taking subtype_check_lock_ if SubtypeCheck for j.l.r.Proxy is already assigned.
5212 }
5213
5214 VisiblyInitializedCallback* callback = nullptr;
5215 {
5216 // Lock on klass is released. Lock new class object.
5217 ObjectLock<mirror::Class> initialization_lock(self, klass);
5218 EnsureSkipAccessChecksMethods(klass, image_pointer_size_);
5219 // Conservatively go through the ClassStatus::kInitialized state.
5220 callback = MarkClassInitialized(self, klass);
5221 }
5222 if (callback != nullptr) {
5223 callback->MakeVisible(self);
5224 }
5225
5226 // Consistency checks.
5227 if (kIsDebugBuild) {
5228 CHECK(klass->GetIFieldsPtr() == nullptr);
5229 CheckProxyConstructor(klass->GetDirectMethod(0, image_pointer_size_));
5230
5231 for (size_t i = 0; i < num_virtual_methods; ++i) {
5232 auto* virtual_method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_);
5233 CheckProxyMethod(virtual_method, proxied_methods[i]);
5234 }
5235
5236 StackHandleScope<1> hs2(self);
5237 Handle<mirror::String> decoded_name = hs2.NewHandle(soa.Decode<mirror::String>(name));
5238 std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces",
5239 decoded_name->ToModifiedUtf8().c_str()));
5240 CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(0)), interfaces_field_name);
5241
5242 std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws",
5243 decoded_name->ToModifiedUtf8().c_str()));
5244 CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(1)), throws_field_name);
5245
5246 CHECK_EQ(klass.Get()->GetProxyInterfaces(),
5247 soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces));
5248 CHECK_EQ(klass.Get()->GetProxyThrows(),
5249 proxied_throws.Get());
5250 }
5251 return klass.Get();
5252 }
5253
CreateProxyConstructor(Handle<mirror::Class> klass,ArtMethod * out)5254 void ClassLinker::CreateProxyConstructor(Handle<mirror::Class> klass, ArtMethod* out) {
5255 // Create constructor for Proxy that must initialize the method.
5256 ObjPtr<mirror::Class> proxy_class = GetClassRoot<mirror::Proxy>(this);
5257 CHECK_EQ(proxy_class->NumDirectMethods(), 21u);
5258
5259 // Find the <init>(InvocationHandler)V method. The exact method offset varies depending
5260 // on which front-end compiler was used to build the libcore DEX files.
5261 ArtMethod* proxy_constructor =
5262 jni::DecodeArtMethod(WellKnownClasses::java_lang_reflect_Proxy_init);
5263 DCHECK(proxy_constructor != nullptr)
5264 << "Could not find <init> method in java.lang.reflect.Proxy";
5265
5266 // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its
5267 // code_ too)
5268 DCHECK(out != nullptr);
5269 out->CopyFrom(proxy_constructor, image_pointer_size_);
5270 // Make this constructor public and fix the class to be our Proxy version.
5271 // Mark kAccCompileDontBother so that we don't take JIT samples for the method. b/62349349
5272 // Note that the compiler calls a ResolveMethod() overload that does not handle a Proxy referrer.
5273 out->SetAccessFlags((out->GetAccessFlags() & ~kAccProtected) |
5274 kAccPublic |
5275 kAccCompileDontBother);
5276 out->SetDeclaringClass(klass.Get());
5277
5278 // Set the original constructor method.
5279 out->SetDataPtrSize(proxy_constructor, image_pointer_size_);
5280 }
5281
CheckProxyConstructor(ArtMethod * constructor) const5282 void ClassLinker::CheckProxyConstructor(ArtMethod* constructor) const {
5283 CHECK(constructor->IsConstructor());
5284 auto* np = constructor->GetInterfaceMethodIfProxy(image_pointer_size_);
5285 CHECK_STREQ(np->GetName(), "<init>");
5286 CHECK_STREQ(np->GetSignature().ToString().c_str(), "(Ljava/lang/reflect/InvocationHandler;)V");
5287 DCHECK(constructor->IsPublic());
5288 }
5289
CreateProxyMethod(Handle<mirror::Class> klass,ArtMethod * prototype,ArtMethod * out)5290 void ClassLinker::CreateProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype,
5291 ArtMethod* out) {
5292 // We steal everything from the prototype (such as DexCache, invoke stub, etc.) then specialize
5293 // as necessary
5294 DCHECK(out != nullptr);
5295 out->CopyFrom(prototype, image_pointer_size_);
5296
5297 // Set class to be the concrete proxy class.
5298 out->SetDeclaringClass(klass.Get());
5299 // Clear the abstract, default and conflict flags to ensure that defaults aren't picked in
5300 // preference to the invocation handler.
5301 const uint32_t kRemoveFlags = kAccAbstract | kAccDefault | kAccDefaultConflict;
5302 // Make the method final.
5303 // Mark kAccCompileDontBother so that we don't take JIT samples for the method. b/62349349
5304 const uint32_t kAddFlags = kAccFinal | kAccCompileDontBother;
5305 out->SetAccessFlags((out->GetAccessFlags() & ~kRemoveFlags) | kAddFlags);
5306
5307 // Clear the dex_code_item_offset_. It needs to be 0 since proxy methods have no CodeItems but the
5308 // method they copy might (if it's a default method).
5309 out->SetCodeItemOffset(0);
5310
5311 // Set the original interface method.
5312 out->SetDataPtrSize(prototype, image_pointer_size_);
5313
5314 // At runtime the method looks like a reference and argument saving method, clone the code
5315 // related parameters from this method.
5316 out->SetEntryPointFromQuickCompiledCode(GetQuickProxyInvokeHandler());
5317 }
5318
CheckProxyMethod(ArtMethod * method,ArtMethod * prototype) const5319 void ClassLinker::CheckProxyMethod(ArtMethod* method, ArtMethod* prototype) const {
5320 // Basic consistency checks.
5321 CHECK(!prototype->IsFinal());
5322 CHECK(method->IsFinal());
5323 CHECK(method->IsInvokable());
5324
5325 // The proxy method doesn't have its own dex cache or dex file and so it steals those of its
5326 // interface prototype. The exception to this are Constructors and the Class of the Proxy itself.
5327 CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex());
5328 CHECK_EQ(prototype, method->GetInterfaceMethodIfProxy(image_pointer_size_));
5329 }
5330
CanWeInitializeClass(ObjPtr<mirror::Class> klass,bool can_init_statics,bool can_init_parents)5331 bool ClassLinker::CanWeInitializeClass(ObjPtr<mirror::Class> klass, bool can_init_statics,
5332 bool can_init_parents) {
5333 if (can_init_statics && can_init_parents) {
5334 return true;
5335 }
5336 if (!can_init_statics) {
5337 // Check if there's a class initializer.
5338 ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_);
5339 if (clinit != nullptr) {
5340 return false;
5341 }
5342 // Check if there are encoded static values needing initialization.
5343 if (klass->NumStaticFields() != 0) {
5344 const dex::ClassDef* dex_class_def = klass->GetClassDef();
5345 DCHECK(dex_class_def != nullptr);
5346 if (dex_class_def->static_values_off_ != 0) {
5347 return false;
5348 }
5349 }
5350 }
5351 // If we are a class we need to initialize all interfaces with default methods when we are
5352 // initialized. Check all of them.
5353 if (!klass->IsInterface()) {
5354 size_t num_interfaces = klass->GetIfTableCount();
5355 for (size_t i = 0; i < num_interfaces; i++) {
5356 ObjPtr<mirror::Class> iface = klass->GetIfTable()->GetInterface(i);
5357 if (iface->HasDefaultMethods() && !iface->IsInitialized()) {
5358 if (!can_init_parents || !CanWeInitializeClass(iface, can_init_statics, can_init_parents)) {
5359 return false;
5360 }
5361 }
5362 }
5363 }
5364 if (klass->IsInterface() || !klass->HasSuperClass()) {
5365 return true;
5366 }
5367 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
5368 if (super_class->IsInitialized()) {
5369 return true;
5370 }
5371 return can_init_parents && CanWeInitializeClass(super_class, can_init_statics, can_init_parents);
5372 }
5373
InitializeClass(Thread * self,Handle<mirror::Class> klass,bool can_init_statics,bool can_init_parents)5374 bool ClassLinker::InitializeClass(Thread* self,
5375 Handle<mirror::Class> klass,
5376 bool can_init_statics,
5377 bool can_init_parents) {
5378 // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol
5379
5380 // Are we already initialized and therefore done?
5381 // Note: we differ from the JLS here as we don't do this under the lock, this is benign as
5382 // an initialized class will never change its state.
5383 if (klass->IsInitialized()) {
5384 return true;
5385 }
5386
5387 // Fast fail if initialization requires a full runtime. Not part of the JLS.
5388 if (!CanWeInitializeClass(klass.Get(), can_init_statics, can_init_parents)) {
5389 return false;
5390 }
5391
5392 self->AllowThreadSuspension();
5393 Runtime* const runtime = Runtime::Current();
5394 const bool stats_enabled = runtime->HasStatsEnabled();
5395 uint64_t t0;
5396 {
5397 ObjectLock<mirror::Class> lock(self, klass);
5398
5399 // Re-check under the lock in case another thread initialized ahead of us.
5400 if (klass->IsInitialized()) {
5401 return true;
5402 }
5403
5404 // Was the class already found to be erroneous? Done under the lock to match the JLS.
5405 if (klass->IsErroneous()) {
5406 ThrowEarlierClassFailure(klass.Get(), true, /* log= */ true);
5407 VlogClassInitializationFailure(klass);
5408 return false;
5409 }
5410
5411 CHECK(klass->IsResolved() && !klass->IsErroneousResolved())
5412 << klass->PrettyClass() << ": state=" << klass->GetStatus();
5413
5414 if (!klass->IsVerified()) {
5415 VerifyClass(self, klass);
5416 if (!klass->IsVerified()) {
5417 // We failed to verify, expect either the klass to be erroneous or verification failed at
5418 // compile time.
5419 if (klass->IsErroneous()) {
5420 // The class is erroneous. This may be a verifier error, or another thread attempted
5421 // verification and/or initialization and failed. We can distinguish those cases by
5422 // whether an exception is already pending.
5423 if (self->IsExceptionPending()) {
5424 // Check that it's a VerifyError.
5425 DCHECK_EQ("java.lang.Class<java.lang.VerifyError>",
5426 mirror::Class::PrettyClass(self->GetException()->GetClass()));
5427 } else {
5428 // Check that another thread attempted initialization.
5429 DCHECK_NE(0, klass->GetClinitThreadId());
5430 DCHECK_NE(self->GetTid(), klass->GetClinitThreadId());
5431 // Need to rethrow the previous failure now.
5432 ThrowEarlierClassFailure(klass.Get(), true);
5433 }
5434 VlogClassInitializationFailure(klass);
5435 } else {
5436 CHECK(Runtime::Current()->IsAotCompiler());
5437 CHECK(klass->ShouldVerifyAtRuntime() || klass->IsVerifiedNeedsAccessChecks());
5438 self->AssertNoPendingException();
5439 self->SetException(Runtime::Current()->GetPreAllocatedNoClassDefFoundError());
5440 }
5441 self->AssertPendingException();
5442 return false;
5443 } else {
5444 self->AssertNoPendingException();
5445 }
5446
5447 // A separate thread could have moved us all the way to initialized. A "simple" example
5448 // involves a subclass of the current class being initialized at the same time (which
5449 // will implicitly initialize the superclass, if scheduled that way). b/28254258
5450 DCHECK(!klass->IsErroneous()) << klass->GetStatus();
5451 if (klass->IsInitialized()) {
5452 return true;
5453 }
5454 }
5455
5456 // If the class is ClassStatus::kInitializing, either this thread is
5457 // initializing higher up the stack or another thread has beat us
5458 // to initializing and we need to wait. Either way, this
5459 // invocation of InitializeClass will not be responsible for
5460 // running <clinit> and will return.
5461 if (klass->GetStatus() == ClassStatus::kInitializing) {
5462 // Could have got an exception during verification.
5463 if (self->IsExceptionPending()) {
5464 VlogClassInitializationFailure(klass);
5465 return false;
5466 }
5467 // We caught somebody else in the act; was it us?
5468 if (klass->GetClinitThreadId() == self->GetTid()) {
5469 // Yes. That's fine. Return so we can continue initializing.
5470 return true;
5471 }
5472 // No. That's fine. Wait for another thread to finish initializing.
5473 return WaitForInitializeClass(klass, self, lock);
5474 }
5475
5476 // Try to get the oat class's status for this class if the oat file is present. The compiler
5477 // tries to validate superclass descriptors, and writes the result into the oat file.
5478 // Runtime correctness is guaranteed by classpath checks done on loading. If the classpath
5479 // is different at runtime than it was at compile time, the oat file is rejected. So if the
5480 // oat file is present, the classpaths must match, and the runtime time check can be skipped.
5481 bool has_oat_class = false;
5482 const OatFile::OatClass oat_class = (runtime->IsStarted() && !runtime->IsAotCompiler())
5483 ? OatFile::FindOatClass(klass->GetDexFile(), klass->GetDexClassDefIndex(), &has_oat_class)
5484 : OatFile::OatClass::Invalid();
5485 if (oat_class.GetStatus() < ClassStatus::kSuperclassValidated &&
5486 !ValidateSuperClassDescriptors(klass)) {
5487 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
5488 return false;
5489 }
5490 self->AllowThreadSuspension();
5491
5492 CHECK_EQ(klass->GetStatus(), ClassStatus::kVerified) << klass->PrettyClass()
5493 << " self.tid=" << self->GetTid() << " clinit.tid=" << klass->GetClinitThreadId();
5494
5495 // From here out other threads may observe that we're initializing and so changes of state
5496 // require the a notification.
5497 klass->SetClinitThreadId(self->GetTid());
5498 mirror::Class::SetStatus(klass, ClassStatus::kInitializing, self);
5499
5500 t0 = stats_enabled ? NanoTime() : 0u;
5501 }
5502
5503 uint64_t t_sub = 0;
5504
5505 // Initialize super classes, must be done while initializing for the JLS.
5506 if (!klass->IsInterface() && klass->HasSuperClass()) {
5507 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
5508 if (!super_class->IsInitialized()) {
5509 CHECK(!super_class->IsInterface());
5510 CHECK(can_init_parents);
5511 StackHandleScope<1> hs(self);
5512 Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class));
5513 uint64_t super_t0 = stats_enabled ? NanoTime() : 0u;
5514 bool super_initialized = InitializeClass(self, handle_scope_super, can_init_statics, true);
5515 uint64_t super_t1 = stats_enabled ? NanoTime() : 0u;
5516 if (!super_initialized) {
5517 // The super class was verified ahead of entering initializing, we should only be here if
5518 // the super class became erroneous due to initialization.
5519 // For the case of aot compiler, the super class might also be initializing but we don't
5520 // want to process circular dependencies in pre-compile.
5521 CHECK(self->IsExceptionPending())
5522 << "Super class initialization failed for "
5523 << handle_scope_super->PrettyDescriptor()
5524 << " that has unexpected status " << handle_scope_super->GetStatus()
5525 << "\nPending exception:\n"
5526 << (self->GetException() != nullptr ? self->GetException()->Dump() : "");
5527 ObjectLock<mirror::Class> lock(self, klass);
5528 // Initialization failed because the super-class is erroneous.
5529 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
5530 return false;
5531 }
5532 t_sub = super_t1 - super_t0;
5533 }
5534 }
5535
5536 if (!klass->IsInterface()) {
5537 // Initialize interfaces with default methods for the JLS.
5538 size_t num_direct_interfaces = klass->NumDirectInterfaces();
5539 // Only setup the (expensive) handle scope if we actually need to.
5540 if (UNLIKELY(num_direct_interfaces > 0)) {
5541 StackHandleScope<1> hs_iface(self);
5542 MutableHandle<mirror::Class> handle_scope_iface(hs_iface.NewHandle<mirror::Class>(nullptr));
5543 for (size_t i = 0; i < num_direct_interfaces; i++) {
5544 handle_scope_iface.Assign(mirror::Class::GetDirectInterface(self, klass.Get(), i));
5545 CHECK(handle_scope_iface != nullptr) << klass->PrettyDescriptor() << " iface #" << i;
5546 CHECK(handle_scope_iface->IsInterface());
5547 if (handle_scope_iface->HasBeenRecursivelyInitialized()) {
5548 // We have already done this for this interface. Skip it.
5549 continue;
5550 }
5551 // We cannot just call initialize class directly because we need to ensure that ALL
5552 // interfaces with default methods are initialized. Non-default interface initialization
5553 // will not affect other non-default super-interfaces.
5554 // This is not very precise, misses all walking.
5555 uint64_t inf_t0 = stats_enabled ? NanoTime() : 0u;
5556 bool iface_initialized = InitializeDefaultInterfaceRecursive(self,
5557 handle_scope_iface,
5558 can_init_statics,
5559 can_init_parents);
5560 uint64_t inf_t1 = stats_enabled ? NanoTime() : 0u;
5561 if (!iface_initialized) {
5562 ObjectLock<mirror::Class> lock(self, klass);
5563 // Initialization failed because one of our interfaces with default methods is erroneous.
5564 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
5565 return false;
5566 }
5567 t_sub += inf_t1 - inf_t0;
5568 }
5569 }
5570 }
5571
5572 const size_t num_static_fields = klass->NumStaticFields();
5573 if (num_static_fields > 0) {
5574 const dex::ClassDef* dex_class_def = klass->GetClassDef();
5575 CHECK(dex_class_def != nullptr);
5576 StackHandleScope<3> hs(self);
5577 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
5578 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
5579
5580 // Eagerly fill in static fields so that the we don't have to do as many expensive
5581 // Class::FindStaticField in ResolveField.
5582 for (size_t i = 0; i < num_static_fields; ++i) {
5583 ArtField* field = klass->GetStaticField(i);
5584 const uint32_t field_idx = field->GetDexFieldIndex();
5585 ArtField* resolved_field = dex_cache->GetResolvedField(field_idx, image_pointer_size_);
5586 if (resolved_field == nullptr) {
5587 // Populating cache of a dex file which defines `klass` should always be allowed.
5588 DCHECK(!hiddenapi::ShouldDenyAccessToMember(
5589 field,
5590 hiddenapi::AccessContext(class_loader.Get(), dex_cache.Get()),
5591 hiddenapi::AccessMethod::kNone));
5592 dex_cache->SetResolvedField(field_idx, field, image_pointer_size_);
5593 } else {
5594 DCHECK_EQ(field, resolved_field);
5595 }
5596 }
5597
5598 annotations::RuntimeEncodedStaticFieldValueIterator value_it(dex_cache,
5599 class_loader,
5600 this,
5601 *dex_class_def);
5602 const DexFile& dex_file = *dex_cache->GetDexFile();
5603
5604 if (value_it.HasNext()) {
5605 ClassAccessor accessor(dex_file, *dex_class_def);
5606 CHECK(can_init_statics);
5607 for (const ClassAccessor::Field& field : accessor.GetStaticFields()) {
5608 if (!value_it.HasNext()) {
5609 break;
5610 }
5611 ArtField* art_field = ResolveField(field.GetIndex(),
5612 dex_cache,
5613 class_loader,
5614 /* is_static= */ true);
5615 if (Runtime::Current()->IsActiveTransaction()) {
5616 value_it.ReadValueToField<true>(art_field);
5617 } else {
5618 value_it.ReadValueToField<false>(art_field);
5619 }
5620 if (self->IsExceptionPending()) {
5621 break;
5622 }
5623 value_it.Next();
5624 }
5625 DCHECK(self->IsExceptionPending() || !value_it.HasNext());
5626 }
5627 }
5628
5629
5630 if (!self->IsExceptionPending()) {
5631 ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_);
5632 if (clinit != nullptr) {
5633 CHECK(can_init_statics);
5634 JValue result;
5635 clinit->Invoke(self, nullptr, 0, &result, "V");
5636 }
5637 }
5638 self->AllowThreadSuspension();
5639 uint64_t t1 = stats_enabled ? NanoTime() : 0u;
5640
5641 VisiblyInitializedCallback* callback = nullptr;
5642 bool success = true;
5643 {
5644 ObjectLock<mirror::Class> lock(self, klass);
5645
5646 if (self->IsExceptionPending()) {
5647 WrapExceptionInInitializer(klass);
5648 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
5649 success = false;
5650 } else if (Runtime::Current()->IsTransactionAborted()) {
5651 // The exception thrown when the transaction aborted has been caught and cleared
5652 // so we need to throw it again now.
5653 VLOG(compiler) << "Return from class initializer of "
5654 << mirror::Class::PrettyDescriptor(klass.Get())
5655 << " without exception while transaction was aborted: re-throw it now.";
5656 runtime->ThrowTransactionAbortError(self);
5657 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
5658 success = false;
5659 } else {
5660 if (stats_enabled) {
5661 RuntimeStats* global_stats = runtime->GetStats();
5662 RuntimeStats* thread_stats = self->GetStats();
5663 ++global_stats->class_init_count;
5664 ++thread_stats->class_init_count;
5665 global_stats->class_init_time_ns += (t1 - t0 - t_sub);
5666 thread_stats->class_init_time_ns += (t1 - t0 - t_sub);
5667 }
5668 // Set the class as initialized except if failed to initialize static fields.
5669 callback = MarkClassInitialized(self, klass);
5670 if (VLOG_IS_ON(class_linker)) {
5671 std::string temp;
5672 LOG(INFO) << "Initialized class " << klass->GetDescriptor(&temp) << " from " <<
5673 klass->GetLocation();
5674 }
5675 }
5676 }
5677 if (callback != nullptr) {
5678 callback->MakeVisible(self);
5679 }
5680 return success;
5681 }
5682
5683 // We recursively run down the tree of interfaces. We need to do this in the order they are declared
5684 // 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)5685 bool ClassLinker::InitializeDefaultInterfaceRecursive(Thread* self,
5686 Handle<mirror::Class> iface,
5687 bool can_init_statics,
5688 bool can_init_parents) {
5689 CHECK(iface->IsInterface());
5690 size_t num_direct_ifaces = iface->NumDirectInterfaces();
5691 // Only create the (expensive) handle scope if we need it.
5692 if (UNLIKELY(num_direct_ifaces > 0)) {
5693 StackHandleScope<1> hs(self);
5694 MutableHandle<mirror::Class> handle_super_iface(hs.NewHandle<mirror::Class>(nullptr));
5695 // First we initialize all of iface's super-interfaces recursively.
5696 for (size_t i = 0; i < num_direct_ifaces; i++) {
5697 ObjPtr<mirror::Class> super_iface = mirror::Class::GetDirectInterface(self, iface.Get(), i);
5698 CHECK(super_iface != nullptr) << iface->PrettyDescriptor() << " iface #" << i;
5699 if (!super_iface->HasBeenRecursivelyInitialized()) {
5700 // Recursive step
5701 handle_super_iface.Assign(super_iface);
5702 if (!InitializeDefaultInterfaceRecursive(self,
5703 handle_super_iface,
5704 can_init_statics,
5705 can_init_parents)) {
5706 return false;
5707 }
5708 }
5709 }
5710 }
5711
5712 bool result = true;
5713 // Then we initialize 'iface' if it has default methods. We do not need to (and in fact must not)
5714 // initialize if we don't have default methods.
5715 if (iface->HasDefaultMethods()) {
5716 result = EnsureInitialized(self, iface, can_init_statics, can_init_parents);
5717 }
5718
5719 // Mark that this interface has undergone recursive default interface initialization so we know we
5720 // can skip it on any later class initializations. We do this even if we are not a default
5721 // interface since we can still avoid the traversal. This is purely a performance optimization.
5722 if (result) {
5723 // TODO This should be done in a better way
5724 // Note: Use a try-lock to avoid blocking when someone else is holding the lock on this
5725 // interface. It is bad (Java) style, but not impossible. Marking the recursive
5726 // initialization is a performance optimization (to avoid another idempotent visit
5727 // for other implementing classes/interfaces), and can be revisited later.
5728 ObjectTryLock<mirror::Class> lock(self, iface);
5729 if (lock.Acquired()) {
5730 iface->SetRecursivelyInitialized();
5731 }
5732 }
5733 return result;
5734 }
5735
WaitForInitializeClass(Handle<mirror::Class> klass,Thread * self,ObjectLock<mirror::Class> & lock)5736 bool ClassLinker::WaitForInitializeClass(Handle<mirror::Class> klass,
5737 Thread* self,
5738 ObjectLock<mirror::Class>& lock)
5739 REQUIRES_SHARED(Locks::mutator_lock_) {
5740 while (true) {
5741 self->AssertNoPendingException();
5742 CHECK(!klass->IsInitialized());
5743 lock.WaitIgnoringInterrupts();
5744
5745 // When we wake up, repeat the test for init-in-progress. If
5746 // there's an exception pending (only possible if
5747 // we were not using WaitIgnoringInterrupts), bail out.
5748 if (self->IsExceptionPending()) {
5749 WrapExceptionInInitializer(klass);
5750 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
5751 return false;
5752 }
5753 // Spurious wakeup? Go back to waiting.
5754 if (klass->GetStatus() == ClassStatus::kInitializing) {
5755 continue;
5756 }
5757 if (klass->GetStatus() == ClassStatus::kVerified &&
5758 Runtime::Current()->IsAotCompiler()) {
5759 // Compile time initialization failed.
5760 return false;
5761 }
5762 if (klass->IsErroneous()) {
5763 // The caller wants an exception, but it was thrown in a
5764 // different thread. Synthesize one here.
5765 ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread",
5766 klass->PrettyDescriptor().c_str());
5767 VlogClassInitializationFailure(klass);
5768 return false;
5769 }
5770 if (klass->IsInitialized()) {
5771 return true;
5772 }
5773 LOG(FATAL) << "Unexpected class status. " << klass->PrettyClass() << " is "
5774 << klass->GetStatus();
5775 }
5776 UNREACHABLE();
5777 }
5778
ThrowSignatureCheckResolveReturnTypeException(Handle<mirror::Class> klass,Handle<mirror::Class> super_klass,ArtMethod * method,ArtMethod * m)5779 static void ThrowSignatureCheckResolveReturnTypeException(Handle<mirror::Class> klass,
5780 Handle<mirror::Class> super_klass,
5781 ArtMethod* method,
5782 ArtMethod* m)
5783 REQUIRES_SHARED(Locks::mutator_lock_) {
5784 DCHECK(Thread::Current()->IsExceptionPending());
5785 DCHECK(!m->IsProxyMethod());
5786 const DexFile* dex_file = m->GetDexFile();
5787 const dex::MethodId& method_id = dex_file->GetMethodId(m->GetDexMethodIndex());
5788 const dex::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
5789 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
5790 std::string return_type = dex_file->PrettyType(return_type_idx);
5791 std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader());
5792 ThrowWrappedLinkageError(klass.Get(),
5793 "While checking class %s method %s signature against %s %s: "
5794 "Failed to resolve return type %s with %s",
5795 mirror::Class::PrettyDescriptor(klass.Get()).c_str(),
5796 ArtMethod::PrettyMethod(method).c_str(),
5797 super_klass->IsInterface() ? "interface" : "superclass",
5798 mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(),
5799 return_type.c_str(), class_loader.c_str());
5800 }
5801
ThrowSignatureCheckResolveArgException(Handle<mirror::Class> klass,Handle<mirror::Class> super_klass,ArtMethod * method,ArtMethod * m,uint32_t index,dex::TypeIndex arg_type_idx)5802 static void ThrowSignatureCheckResolveArgException(Handle<mirror::Class> klass,
5803 Handle<mirror::Class> super_klass,
5804 ArtMethod* method,
5805 ArtMethod* m,
5806 uint32_t index,
5807 dex::TypeIndex arg_type_idx)
5808 REQUIRES_SHARED(Locks::mutator_lock_) {
5809 DCHECK(Thread::Current()->IsExceptionPending());
5810 DCHECK(!m->IsProxyMethod());
5811 const DexFile* dex_file = m->GetDexFile();
5812 std::string arg_type = dex_file->PrettyType(arg_type_idx);
5813 std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader());
5814 ThrowWrappedLinkageError(klass.Get(),
5815 "While checking class %s method %s signature against %s %s: "
5816 "Failed to resolve arg %u type %s with %s",
5817 mirror::Class::PrettyDescriptor(klass.Get()).c_str(),
5818 ArtMethod::PrettyMethod(method).c_str(),
5819 super_klass->IsInterface() ? "interface" : "superclass",
5820 mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(),
5821 index, arg_type.c_str(), class_loader.c_str());
5822 }
5823
ThrowSignatureMismatch(Handle<mirror::Class> klass,Handle<mirror::Class> super_klass,ArtMethod * method,const std::string & error_msg)5824 static void ThrowSignatureMismatch(Handle<mirror::Class> klass,
5825 Handle<mirror::Class> super_klass,
5826 ArtMethod* method,
5827 const std::string& error_msg)
5828 REQUIRES_SHARED(Locks::mutator_lock_) {
5829 ThrowLinkageError(klass.Get(),
5830 "Class %s method %s resolves differently in %s %s: %s",
5831 mirror::Class::PrettyDescriptor(klass.Get()).c_str(),
5832 ArtMethod::PrettyMethod(method).c_str(),
5833 super_klass->IsInterface() ? "interface" : "superclass",
5834 mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(),
5835 error_msg.c_str());
5836 }
5837
HasSameSignatureWithDifferentClassLoaders(Thread * self,Handle<mirror::Class> klass,Handle<mirror::Class> super_klass,ArtMethod * method1,ArtMethod * method2)5838 static bool HasSameSignatureWithDifferentClassLoaders(Thread* self,
5839 Handle<mirror::Class> klass,
5840 Handle<mirror::Class> super_klass,
5841 ArtMethod* method1,
5842 ArtMethod* method2)
5843 REQUIRES_SHARED(Locks::mutator_lock_) {
5844 {
5845 StackHandleScope<1> hs(self);
5846 Handle<mirror::Class> return_type(hs.NewHandle(method1->ResolveReturnType()));
5847 if (UNLIKELY(return_type == nullptr)) {
5848 ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method1);
5849 return false;
5850 }
5851 ObjPtr<mirror::Class> other_return_type = method2->ResolveReturnType();
5852 if (UNLIKELY(other_return_type == nullptr)) {
5853 ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method2);
5854 return false;
5855 }
5856 if (UNLIKELY(other_return_type != return_type.Get())) {
5857 ThrowSignatureMismatch(klass, super_klass, method1,
5858 StringPrintf("Return types mismatch: %s(%p) vs %s(%p)",
5859 return_type->PrettyClassAndClassLoader().c_str(),
5860 return_type.Get(),
5861 other_return_type->PrettyClassAndClassLoader().c_str(),
5862 other_return_type.Ptr()));
5863 return false;
5864 }
5865 }
5866 const dex::TypeList* types1 = method1->GetParameterTypeList();
5867 const dex::TypeList* types2 = method2->GetParameterTypeList();
5868 if (types1 == nullptr) {
5869 if (types2 != nullptr && types2->Size() != 0) {
5870 ThrowSignatureMismatch(klass, super_klass, method1,
5871 StringPrintf("Type list mismatch with %s",
5872 method2->PrettyMethod(true).c_str()));
5873 return false;
5874 }
5875 return true;
5876 } else if (UNLIKELY(types2 == nullptr)) {
5877 if (types1->Size() != 0) {
5878 ThrowSignatureMismatch(klass, super_klass, method1,
5879 StringPrintf("Type list mismatch with %s",
5880 method2->PrettyMethod(true).c_str()));
5881 return false;
5882 }
5883 return true;
5884 }
5885 uint32_t num_types = types1->Size();
5886 if (UNLIKELY(num_types != types2->Size())) {
5887 ThrowSignatureMismatch(klass, super_klass, method1,
5888 StringPrintf("Type list mismatch with %s",
5889 method2->PrettyMethod(true).c_str()));
5890 return false;
5891 }
5892 for (uint32_t i = 0; i < num_types; ++i) {
5893 StackHandleScope<1> hs(self);
5894 dex::TypeIndex param_type_idx = types1->GetTypeItem(i).type_idx_;
5895 Handle<mirror::Class> param_type(hs.NewHandle(
5896 method1->ResolveClassFromTypeIndex(param_type_idx)));
5897 if (UNLIKELY(param_type == nullptr)) {
5898 ThrowSignatureCheckResolveArgException(klass, super_klass, method1,
5899 method1, i, param_type_idx);
5900 return false;
5901 }
5902 dex::TypeIndex other_param_type_idx = types2->GetTypeItem(i).type_idx_;
5903 ObjPtr<mirror::Class> other_param_type =
5904 method2->ResolveClassFromTypeIndex(other_param_type_idx);
5905 if (UNLIKELY(other_param_type == nullptr)) {
5906 ThrowSignatureCheckResolveArgException(klass, super_klass, method1,
5907 method2, i, other_param_type_idx);
5908 return false;
5909 }
5910 if (UNLIKELY(param_type.Get() != other_param_type)) {
5911 ThrowSignatureMismatch(klass, super_klass, method1,
5912 StringPrintf("Parameter %u type mismatch: %s(%p) vs %s(%p)",
5913 i,
5914 param_type->PrettyClassAndClassLoader().c_str(),
5915 param_type.Get(),
5916 other_param_type->PrettyClassAndClassLoader().c_str(),
5917 other_param_type.Ptr()));
5918 return false;
5919 }
5920 }
5921 return true;
5922 }
5923
5924
ValidateSuperClassDescriptors(Handle<mirror::Class> klass)5925 bool ClassLinker::ValidateSuperClassDescriptors(Handle<mirror::Class> klass) {
5926 if (klass->IsInterface()) {
5927 return true;
5928 }
5929 // Begin with the methods local to the superclass.
5930 Thread* self = Thread::Current();
5931 StackHandleScope<1> hs(self);
5932 MutableHandle<mirror::Class> super_klass(hs.NewHandle<mirror::Class>(nullptr));
5933 if (klass->HasSuperClass() &&
5934 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
5935 super_klass.Assign(klass->GetSuperClass());
5936 for (int i = klass->GetSuperClass()->GetVTableLength() - 1; i >= 0; --i) {
5937 auto* m = klass->GetVTableEntry(i, image_pointer_size_);
5938 auto* super_m = klass->GetSuperClass()->GetVTableEntry(i, image_pointer_size_);
5939 if (m != super_m) {
5940 if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self,
5941 klass,
5942 super_klass,
5943 m,
5944 super_m))) {
5945 self->AssertPendingException();
5946 return false;
5947 }
5948 }
5949 }
5950 }
5951 for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) {
5952 super_klass.Assign(klass->GetIfTable()->GetInterface(i));
5953 if (klass->GetClassLoader() != super_klass->GetClassLoader()) {
5954 uint32_t num_methods = super_klass->NumVirtualMethods();
5955 for (uint32_t j = 0; j < num_methods; ++j) {
5956 auto* m = klass->GetIfTable()->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>(
5957 j, image_pointer_size_);
5958 auto* super_m = super_klass->GetVirtualMethod(j, image_pointer_size_);
5959 if (m != super_m) {
5960 if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self,
5961 klass,
5962 super_klass,
5963 m,
5964 super_m))) {
5965 self->AssertPendingException();
5966 return false;
5967 }
5968 }
5969 }
5970 }
5971 }
5972 return true;
5973 }
5974
EnsureInitialized(Thread * self,Handle<mirror::Class> c,bool can_init_fields,bool can_init_parents)5975 bool ClassLinker::EnsureInitialized(Thread* self,
5976 Handle<mirror::Class> c,
5977 bool can_init_fields,
5978 bool can_init_parents) {
5979 DCHECK(c != nullptr);
5980
5981 if (c->IsInitialized()) {
5982 // If we've seen an initialized but not visibly initialized class
5983 // many times, request visible initialization.
5984 if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) {
5985 // Thanks to the x86 memory model classes skip the initialized status.
5986 DCHECK(c->IsVisiblyInitialized());
5987 } else if (UNLIKELY(!c->IsVisiblyInitialized())) {
5988 if (self->IncrementMakeVisiblyInitializedCounter()) {
5989 MakeInitializedClassesVisiblyInitialized(self, /*wait=*/ false);
5990 }
5991 }
5992 DCHECK(c->WasVerificationAttempted()) << c->PrettyClassAndClassLoader();
5993 return true;
5994 }
5995 // SubtypeCheckInfo::Initialized must happen-before any new-instance for that type.
5996 //
5997 // Ensure the bitstring is initialized before any of the class initialization
5998 // logic occurs. Once a class initializer starts running, objects can
5999 // escape into the heap and use the subtype checking code.
6000 //
6001 // Note: A class whose SubtypeCheckInfo is at least Initialized means it
6002 // can be used as a source for the IsSubClass check, and that all ancestors
6003 // of the class are Assigned (can be used as a target for IsSubClass check)
6004 // or Overflowed (can be used as a source for IsSubClass check).
6005 if (kBitstringSubtypeCheckEnabled) {
6006 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
6007 SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(c.Get());
6008 // TODO: Avoid taking subtype_check_lock_ if SubtypeCheck is already initialized.
6009 }
6010 const bool success = InitializeClass(self, c, can_init_fields, can_init_parents);
6011 if (!success) {
6012 if (can_init_fields && can_init_parents) {
6013 CHECK(self->IsExceptionPending()) << c->PrettyClass();
6014 }
6015 } else {
6016 self->AssertNoPendingException();
6017 }
6018 return success;
6019 }
6020
FixupTemporaryDeclaringClass(ObjPtr<mirror::Class> temp_class,ObjPtr<mirror::Class> new_class)6021 void ClassLinker::FixupTemporaryDeclaringClass(ObjPtr<mirror::Class> temp_class,
6022 ObjPtr<mirror::Class> new_class) {
6023 DCHECK_EQ(temp_class->NumInstanceFields(), 0u);
6024 for (ArtField& field : new_class->GetIFields()) {
6025 if (field.GetDeclaringClass() == temp_class) {
6026 field.SetDeclaringClass(new_class);
6027 }
6028 }
6029
6030 DCHECK_EQ(temp_class->NumStaticFields(), 0u);
6031 for (ArtField& field : new_class->GetSFields()) {
6032 if (field.GetDeclaringClass() == temp_class) {
6033 field.SetDeclaringClass(new_class);
6034 }
6035 }
6036
6037 DCHECK_EQ(temp_class->NumDirectMethods(), 0u);
6038 DCHECK_EQ(temp_class->NumVirtualMethods(), 0u);
6039 for (auto& method : new_class->GetMethods(image_pointer_size_)) {
6040 if (method.GetDeclaringClass() == temp_class) {
6041 method.SetDeclaringClass(new_class);
6042 }
6043 }
6044
6045 // Make sure the remembered set and mod-union tables know that we updated some of the native
6046 // roots.
6047 WriteBarrier::ForEveryFieldWrite(new_class);
6048 }
6049
RegisterClassLoader(ObjPtr<mirror::ClassLoader> class_loader)6050 void ClassLinker::RegisterClassLoader(ObjPtr<mirror::ClassLoader> class_loader) {
6051 CHECK(class_loader->GetAllocator() == nullptr);
6052 CHECK(class_loader->GetClassTable() == nullptr);
6053 Thread* const self = Thread::Current();
6054 ClassLoaderData data;
6055 data.weak_root = self->GetJniEnv()->GetVm()->AddWeakGlobalRef(self, class_loader);
6056 // Create and set the class table.
6057 data.class_table = new ClassTable;
6058 class_loader->SetClassTable(data.class_table);
6059 // Create and set the linear allocator.
6060 data.allocator = Runtime::Current()->CreateLinearAlloc();
6061 class_loader->SetAllocator(data.allocator);
6062 // Add to the list so that we know to free the data later.
6063 class_loaders_.push_back(data);
6064 }
6065
InsertClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader)6066 ClassTable* ClassLinker::InsertClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) {
6067 if (class_loader == nullptr) {
6068 return boot_class_table_.get();
6069 }
6070 ClassTable* class_table = class_loader->GetClassTable();
6071 if (class_table == nullptr) {
6072 RegisterClassLoader(class_loader);
6073 class_table = class_loader->GetClassTable();
6074 DCHECK(class_table != nullptr);
6075 }
6076 return class_table;
6077 }
6078
ClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader)6079 ClassTable* ClassLinker::ClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) {
6080 return class_loader == nullptr ? boot_class_table_.get() : class_loader->GetClassTable();
6081 }
6082
FindSuperImt(ObjPtr<mirror::Class> klass,PointerSize pointer_size)6083 static ImTable* FindSuperImt(ObjPtr<mirror::Class> klass, PointerSize pointer_size)
6084 REQUIRES_SHARED(Locks::mutator_lock_) {
6085 while (klass->HasSuperClass()) {
6086 klass = klass->GetSuperClass();
6087 if (klass->ShouldHaveImt()) {
6088 return klass->GetImt(pointer_size);
6089 }
6090 }
6091 return nullptr;
6092 }
6093
LinkClass(Thread * self,const char * descriptor,Handle<mirror::Class> klass,Handle<mirror::ObjectArray<mirror::Class>> interfaces,MutableHandle<mirror::Class> * h_new_class_out)6094 bool ClassLinker::LinkClass(Thread* self,
6095 const char* descriptor,
6096 Handle<mirror::Class> klass,
6097 Handle<mirror::ObjectArray<mirror::Class>> interfaces,
6098 MutableHandle<mirror::Class>* h_new_class_out) {
6099 CHECK_EQ(ClassStatus::kLoaded, klass->GetStatus());
6100
6101 if (!LinkSuperClass(klass)) {
6102 return false;
6103 }
6104 ArtMethod* imt_data[ImTable::kSize];
6105 // If there are any new conflicts compared to super class.
6106 bool new_conflict = false;
6107 std::fill_n(imt_data, arraysize(imt_data), Runtime::Current()->GetImtUnimplementedMethod());
6108 if (!LinkMethods(self, klass, interfaces, &new_conflict, imt_data)) {
6109 return false;
6110 }
6111 if (!LinkInstanceFields(self, klass)) {
6112 return false;
6113 }
6114 size_t class_size;
6115 if (!LinkStaticFields(self, klass, &class_size)) {
6116 return false;
6117 }
6118 CreateReferenceInstanceOffsets(klass);
6119 CHECK_EQ(ClassStatus::kLoaded, klass->GetStatus());
6120
6121 ImTable* imt = nullptr;
6122 if (klass->ShouldHaveImt()) {
6123 // If there are any new conflicts compared to the super class we can not make a copy. There
6124 // can be cases where both will have a conflict method at the same slot without having the same
6125 // set of conflicts. In this case, we can not share the IMT since the conflict table slow path
6126 // will possibly create a table that is incorrect for either of the classes.
6127 // Same IMT with new_conflict does not happen very often.
6128 if (!new_conflict) {
6129 ImTable* super_imt = FindSuperImt(klass.Get(), image_pointer_size_);
6130 if (super_imt != nullptr) {
6131 bool imt_equals = true;
6132 for (size_t i = 0; i < ImTable::kSize && imt_equals; ++i) {
6133 imt_equals = imt_equals && (super_imt->Get(i, image_pointer_size_) == imt_data[i]);
6134 }
6135 if (imt_equals) {
6136 imt = super_imt;
6137 }
6138 }
6139 }
6140 if (imt == nullptr) {
6141 LinearAlloc* allocator = GetAllocatorForClassLoader(klass->GetClassLoader());
6142 imt = reinterpret_cast<ImTable*>(
6143 allocator->Alloc(self, ImTable::SizeInBytes(image_pointer_size_)));
6144 if (imt == nullptr) {
6145 return false;
6146 }
6147 imt->Populate(imt_data, image_pointer_size_);
6148 }
6149 }
6150
6151 if (!klass->IsTemp() || (!init_done_ && klass->GetClassSize() == class_size)) {
6152 // We don't need to retire this class as it has no embedded tables or it was created the
6153 // correct size during class linker initialization.
6154 CHECK_EQ(klass->GetClassSize(), class_size) << klass->PrettyDescriptor();
6155
6156 if (klass->ShouldHaveEmbeddedVTable()) {
6157 klass->PopulateEmbeddedVTable(image_pointer_size_);
6158 }
6159 if (klass->ShouldHaveImt()) {
6160 klass->SetImt(imt, image_pointer_size_);
6161 }
6162
6163 // Update CHA info based on whether we override methods.
6164 // Have to do this before setting the class as resolved which allows
6165 // instantiation of klass.
6166 if (LIKELY(descriptor != nullptr) && cha_ != nullptr) {
6167 cha_->UpdateAfterLoadingOf(klass);
6168 }
6169
6170 // This will notify waiters on klass that saw the not yet resolved
6171 // class in the class_table_ during EnsureResolved.
6172 mirror::Class::SetStatus(klass, ClassStatus::kResolved, self);
6173 h_new_class_out->Assign(klass.Get());
6174 } else {
6175 CHECK(!klass->IsResolved());
6176 // Retire the temporary class and create the correctly sized resolved class.
6177 StackHandleScope<1> hs(self);
6178 Handle<mirror::Class> h_new_class =
6179 hs.NewHandle(mirror::Class::CopyOf(klass, self, class_size, imt, image_pointer_size_));
6180 // Set arrays to null since we don't want to have multiple classes with the same ArtField or
6181 // ArtMethod array pointers. If this occurs, it causes bugs in remembered sets since the GC
6182 // may not see any references to the target space and clean the card for a class if another
6183 // class had the same array pointer.
6184 klass->SetMethodsPtrUnchecked(nullptr, 0, 0);
6185 klass->SetSFieldsPtrUnchecked(nullptr);
6186 klass->SetIFieldsPtrUnchecked(nullptr);
6187 if (UNLIKELY(h_new_class == nullptr)) {
6188 self->AssertPendingOOMException();
6189 mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self);
6190 return false;
6191 }
6192
6193 CHECK_EQ(h_new_class->GetClassSize(), class_size);
6194 ObjectLock<mirror::Class> lock(self, h_new_class);
6195 FixupTemporaryDeclaringClass(klass.Get(), h_new_class.Get());
6196
6197 if (LIKELY(descriptor != nullptr)) {
6198 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
6199 const ObjPtr<mirror::ClassLoader> class_loader = h_new_class.Get()->GetClassLoader();
6200 ClassTable* const table = InsertClassTableForClassLoader(class_loader);
6201 const ObjPtr<mirror::Class> existing =
6202 table->UpdateClass(descriptor, h_new_class.Get(), ComputeModifiedUtf8Hash(descriptor));
6203 if (class_loader != nullptr) {
6204 // We updated the class in the class table, perform the write barrier so that the GC knows
6205 // about the change.
6206 WriteBarrier::ForEveryFieldWrite(class_loader);
6207 }
6208 CHECK_EQ(existing, klass.Get());
6209 if (log_new_roots_) {
6210 new_class_roots_.push_back(GcRoot<mirror::Class>(h_new_class.Get()));
6211 }
6212 }
6213
6214 // Update CHA info based on whether we override methods.
6215 // Have to do this before setting the class as resolved which allows
6216 // instantiation of klass.
6217 if (LIKELY(descriptor != nullptr) && cha_ != nullptr) {
6218 cha_->UpdateAfterLoadingOf(h_new_class);
6219 }
6220
6221 // This will notify waiters on temp class that saw the not yet resolved class in the
6222 // class_table_ during EnsureResolved.
6223 mirror::Class::SetStatus(klass, ClassStatus::kRetired, self);
6224
6225 CHECK_EQ(h_new_class->GetStatus(), ClassStatus::kResolving);
6226 // This will notify waiters on new_class that saw the not yet resolved
6227 // class in the class_table_ during EnsureResolved.
6228 mirror::Class::SetStatus(h_new_class, ClassStatus::kResolved, self);
6229 // Return the new class.
6230 h_new_class_out->Assign(h_new_class.Get());
6231 }
6232 return true;
6233 }
6234
LoadSuperAndInterfaces(Handle<mirror::Class> klass,const DexFile & dex_file)6235 bool ClassLinker::LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) {
6236 CHECK_EQ(ClassStatus::kIdx, klass->GetStatus());
6237 const dex::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex());
6238 dex::TypeIndex super_class_idx = class_def.superclass_idx_;
6239 if (super_class_idx.IsValid()) {
6240 // Check that a class does not inherit from itself directly.
6241 //
6242 // TODO: This is a cheap check to detect the straightforward case
6243 // of a class extending itself (b/28685551), but we should do a
6244 // proper cycle detection on loaded classes, to detect all cases
6245 // of class circularity errors (b/28830038).
6246 if (super_class_idx == class_def.class_idx_) {
6247 ThrowClassCircularityError(klass.Get(),
6248 "Class %s extends itself",
6249 klass->PrettyDescriptor().c_str());
6250 return false;
6251 }
6252
6253 ObjPtr<mirror::Class> super_class = ResolveType(super_class_idx, klass.Get());
6254 if (super_class == nullptr) {
6255 DCHECK(Thread::Current()->IsExceptionPending());
6256 return false;
6257 }
6258 // Verify
6259 if (!klass->CanAccess(super_class)) {
6260 ThrowIllegalAccessError(klass.Get(), "Class %s extended by class %s is inaccessible",
6261 super_class->PrettyDescriptor().c_str(),
6262 klass->PrettyDescriptor().c_str());
6263 return false;
6264 }
6265 CHECK(super_class->IsResolved());
6266 klass->SetSuperClass(super_class);
6267 }
6268 const dex::TypeList* interfaces = dex_file.GetInterfacesList(class_def);
6269 if (interfaces != nullptr) {
6270 for (size_t i = 0; i < interfaces->Size(); i++) {
6271 dex::TypeIndex idx = interfaces->GetTypeItem(i).type_idx_;
6272 ObjPtr<mirror::Class> interface = ResolveType(idx, klass.Get());
6273 if (interface == nullptr) {
6274 DCHECK(Thread::Current()->IsExceptionPending());
6275 return false;
6276 }
6277 // Verify
6278 if (!klass->CanAccess(interface)) {
6279 // TODO: the RI seemed to ignore this in my testing.
6280 ThrowIllegalAccessError(klass.Get(),
6281 "Interface %s implemented by class %s is inaccessible",
6282 interface->PrettyDescriptor().c_str(),
6283 klass->PrettyDescriptor().c_str());
6284 return false;
6285 }
6286 }
6287 }
6288 // Mark the class as loaded.
6289 mirror::Class::SetStatus(klass, ClassStatus::kLoaded, nullptr);
6290 return true;
6291 }
6292
LinkSuperClass(Handle<mirror::Class> klass)6293 bool ClassLinker::LinkSuperClass(Handle<mirror::Class> klass) {
6294 CHECK(!klass->IsPrimitive());
6295 ObjPtr<mirror::Class> super = klass->GetSuperClass();
6296 ObjPtr<mirror::Class> object_class = GetClassRoot<mirror::Object>(this);
6297 if (klass.Get() == object_class) {
6298 if (super != nullptr) {
6299 ThrowClassFormatError(klass.Get(), "java.lang.Object must not have a superclass");
6300 return false;
6301 }
6302 return true;
6303 }
6304 if (super == nullptr) {
6305 ThrowLinkageError(klass.Get(), "No superclass defined for class %s",
6306 klass->PrettyDescriptor().c_str());
6307 return false;
6308 }
6309 // Verify
6310 if (klass->IsInterface() && super != object_class) {
6311 ThrowClassFormatError(klass.Get(), "Interfaces must have java.lang.Object as superclass");
6312 return false;
6313 }
6314 if (super->IsFinal()) {
6315 ThrowVerifyError(klass.Get(),
6316 "Superclass %s of %s is declared final",
6317 super->PrettyDescriptor().c_str(),
6318 klass->PrettyDescriptor().c_str());
6319 return false;
6320 }
6321 if (super->IsInterface()) {
6322 ThrowIncompatibleClassChangeError(klass.Get(),
6323 "Superclass %s of %s is an interface",
6324 super->PrettyDescriptor().c_str(),
6325 klass->PrettyDescriptor().c_str());
6326 return false;
6327 }
6328 if (!klass->CanAccess(super)) {
6329 ThrowIllegalAccessError(klass.Get(), "Superclass %s is inaccessible to class %s",
6330 super->PrettyDescriptor().c_str(),
6331 klass->PrettyDescriptor().c_str());
6332 return false;
6333 }
6334
6335 // Inherit kAccClassIsFinalizable from the superclass in case this
6336 // class doesn't override finalize.
6337 if (super->IsFinalizable()) {
6338 klass->SetFinalizable();
6339 }
6340
6341 // Inherit class loader flag form super class.
6342 if (super->IsClassLoaderClass()) {
6343 klass->SetClassLoaderClass();
6344 }
6345
6346 // Inherit reference flags (if any) from the superclass.
6347 uint32_t reference_flags = (super->GetClassFlags() & mirror::kClassFlagReference);
6348 if (reference_flags != 0) {
6349 CHECK_EQ(klass->GetClassFlags(), 0u);
6350 klass->SetClassFlags(klass->GetClassFlags() | reference_flags);
6351 }
6352 // Disallow custom direct subclasses of java.lang.ref.Reference.
6353 if (init_done_ && super == GetClassRoot<mirror::Reference>(this)) {
6354 ThrowLinkageError(klass.Get(),
6355 "Class %s attempts to subclass java.lang.ref.Reference, which is not allowed",
6356 klass->PrettyDescriptor().c_str());
6357 return false;
6358 }
6359
6360 if (kIsDebugBuild) {
6361 // Ensure super classes are fully resolved prior to resolving fields..
6362 while (super != nullptr) {
6363 CHECK(super->IsResolved());
6364 super = super->GetSuperClass();
6365 }
6366 }
6367 return true;
6368 }
6369
6370 // 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)6371 bool ClassLinker::LinkMethods(Thread* self,
6372 Handle<mirror::Class> klass,
6373 Handle<mirror::ObjectArray<mirror::Class>> interfaces,
6374 bool* out_new_conflict,
6375 ArtMethod** out_imt) {
6376 self->AllowThreadSuspension();
6377 // A map from vtable indexes to the method they need to be updated to point to. Used because we
6378 // need to have default methods be in the virtuals array of each class but we don't set that up
6379 // until LinkInterfaceMethods.
6380 std::unordered_map<size_t, ClassLinker::MethodTranslation> default_translations;
6381 // Link virtual methods then interface methods.
6382 // We set up the interface lookup table first because we need it to determine if we need to update
6383 // any vtable entries with new default method implementations.
6384 return SetupInterfaceLookupTable(self, klass, interfaces)
6385 && LinkVirtualMethods(self, klass, /*out*/ &default_translations)
6386 && LinkInterfaceMethods(self, klass, default_translations, out_new_conflict, out_imt);
6387 }
6388
6389 // Comparator for name and signature of a method, used in finding overriding methods. Implementation
6390 // avoids the use of handles, if it didn't then rather than compare dex files we could compare dex
6391 // caches in the implementation below.
6392 class MethodNameAndSignatureComparator final : public ValueObject {
6393 public:
6394 explicit MethodNameAndSignatureComparator(ArtMethod* method)
REQUIRES_SHARED(Locks::mutator_lock_)6395 REQUIRES_SHARED(Locks::mutator_lock_) :
6396 dex_file_(method->GetDexFile()), mid_(&dex_file_->GetMethodId(method->GetDexMethodIndex())),
6397 name_(nullptr), name_len_(0) {
6398 DCHECK(!method->IsProxyMethod()) << method->PrettyMethod();
6399 }
6400
GetName()6401 const char* GetName() {
6402 if (name_ == nullptr) {
6403 name_ = dex_file_->StringDataAndUtf16LengthByIdx(mid_->name_idx_, &name_len_);
6404 }
6405 return name_;
6406 }
6407
HasSameNameAndSignature(ArtMethod * other)6408 bool HasSameNameAndSignature(ArtMethod* other)
6409 REQUIRES_SHARED(Locks::mutator_lock_) {
6410 DCHECK(!other->IsProxyMethod()) << other->PrettyMethod();
6411 const DexFile* other_dex_file = other->GetDexFile();
6412 const dex::MethodId& other_mid = other_dex_file->GetMethodId(other->GetDexMethodIndex());
6413 if (dex_file_ == other_dex_file) {
6414 return mid_->name_idx_ == other_mid.name_idx_ && mid_->proto_idx_ == other_mid.proto_idx_;
6415 }
6416 GetName(); // Only used to make sure its calculated.
6417 uint32_t other_name_len;
6418 const char* other_name = other_dex_file->StringDataAndUtf16LengthByIdx(other_mid.name_idx_,
6419 &other_name_len);
6420 if (name_len_ != other_name_len || strcmp(name_, other_name) != 0) {
6421 return false;
6422 }
6423 return dex_file_->GetMethodSignature(*mid_) == other_dex_file->GetMethodSignature(other_mid);
6424 }
6425
6426 private:
6427 // Dex file for the method to compare against.
6428 const DexFile* const dex_file_;
6429 // MethodId for the method to compare against.
6430 const dex::MethodId* const mid_;
6431 // Lazily computed name from the dex file's strings.
6432 const char* name_;
6433 // Lazily computed name length.
6434 uint32_t name_len_;
6435 };
6436
6437 class LinkVirtualHashTable {
6438 public:
LinkVirtualHashTable(Handle<mirror::Class> klass,size_t hash_size,uint32_t * hash_table,PointerSize image_pointer_size)6439 LinkVirtualHashTable(Handle<mirror::Class> klass,
6440 size_t hash_size,
6441 uint32_t* hash_table,
6442 PointerSize image_pointer_size)
6443 : klass_(klass),
6444 hash_size_(hash_size),
6445 hash_table_(hash_table),
6446 image_pointer_size_(image_pointer_size) {
6447 std::fill(hash_table_, hash_table_ + hash_size_, invalid_index_);
6448 }
6449
Add(uint32_t virtual_method_index)6450 void Add(uint32_t virtual_method_index) REQUIRES_SHARED(Locks::mutator_lock_) {
6451 ArtMethod* local_method = klass_->GetVirtualMethodDuringLinking(
6452 virtual_method_index, image_pointer_size_);
6453 const char* name = local_method->GetInterfaceMethodIfProxy(image_pointer_size_)->GetName();
6454 uint32_t hash = ComputeModifiedUtf8Hash(name);
6455 uint32_t index = hash % hash_size_;
6456 // Linear probe until we have an empty slot.
6457 while (hash_table_[index] != invalid_index_) {
6458 if (++index == hash_size_) {
6459 index = 0;
6460 }
6461 }
6462 hash_table_[index] = virtual_method_index;
6463 }
6464
FindAndRemove(MethodNameAndSignatureComparator * comparator)6465 uint32_t FindAndRemove(MethodNameAndSignatureComparator* comparator)
6466 REQUIRES_SHARED(Locks::mutator_lock_) {
6467 const char* name = comparator->GetName();
6468 uint32_t hash = ComputeModifiedUtf8Hash(name);
6469 size_t index = hash % hash_size_;
6470 while (true) {
6471 const uint32_t value = hash_table_[index];
6472 // Since linear probe makes continuous blocks, hitting an invalid index means we are done
6473 // the block and can safely assume not found.
6474 if (value == invalid_index_) {
6475 break;
6476 }
6477 if (value != removed_index_) { // This signifies not already overriden.
6478 ArtMethod* virtual_method =
6479 klass_->GetVirtualMethodDuringLinking(value, image_pointer_size_);
6480 if (comparator->HasSameNameAndSignature(
6481 virtual_method->GetInterfaceMethodIfProxy(image_pointer_size_))) {
6482 hash_table_[index] = removed_index_;
6483 return value;
6484 }
6485 }
6486 if (++index == hash_size_) {
6487 index = 0;
6488 }
6489 }
6490 return GetNotFoundIndex();
6491 }
6492
GetNotFoundIndex()6493 static uint32_t GetNotFoundIndex() {
6494 return invalid_index_;
6495 }
6496
6497 private:
6498 static const uint32_t invalid_index_;
6499 static const uint32_t removed_index_;
6500
6501 Handle<mirror::Class> klass_;
6502 const size_t hash_size_;
6503 uint32_t* const hash_table_;
6504 const PointerSize image_pointer_size_;
6505 };
6506
6507 const uint32_t LinkVirtualHashTable::invalid_index_ = std::numeric_limits<uint32_t>::max();
6508 const uint32_t LinkVirtualHashTable::removed_index_ = std::numeric_limits<uint32_t>::max() - 1;
6509
LinkVirtualMethods(Thread * self,Handle<mirror::Class> klass,std::unordered_map<size_t,ClassLinker::MethodTranslation> * default_translations)6510 bool ClassLinker::LinkVirtualMethods(
6511 Thread* self,
6512 Handle<mirror::Class> klass,
6513 /*out*/std::unordered_map<size_t, ClassLinker::MethodTranslation>* default_translations) {
6514 const size_t num_virtual_methods = klass->NumVirtualMethods();
6515 if (klass->IsInterface()) {
6516 // No vtable.
6517 if (!IsUint<16>(num_virtual_methods)) {
6518 ThrowClassFormatError(klass.Get(), "Too many methods on interface: %zu", num_virtual_methods);
6519 return false;
6520 }
6521 bool has_defaults = false;
6522 // Assign each method an IMT index and set the default flag.
6523 for (size_t i = 0; i < num_virtual_methods; ++i) {
6524 ArtMethod* m = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_);
6525 m->SetMethodIndex(i);
6526 if (!m->IsAbstract()) {
6527 // If the dex file does not support default methods, throw ClassFormatError.
6528 // This check is necessary to protect from odd cases, such as native default
6529 // methods, that the dex file verifier permits for old dex file versions. b/157170505
6530 // FIXME: This should be `if (!m->GetDexFile()->SupportsDefaultMethods())` but we're
6531 // currently running CTS tests for default methods with dex file version 035 which
6532 // does not support default methods. So, we limit this to native methods. b/157718952
6533 if (m->IsNative()) {
6534 DCHECK(!m->GetDexFile()->SupportsDefaultMethods());
6535 ThrowClassFormatError(klass.Get(),
6536 "Dex file does not support default method '%s'",
6537 m->PrettyMethod().c_str());
6538 return false;
6539 }
6540 m->SetAccessFlags(m->GetAccessFlags() | kAccDefault);
6541 has_defaults = true;
6542 }
6543 }
6544 // Mark that we have default methods so that we won't need to scan the virtual_methods_ array
6545 // during initialization. This is a performance optimization. We could simply traverse the
6546 // virtual_methods_ array again during initialization.
6547 if (has_defaults) {
6548 klass->SetHasDefaultMethods();
6549 }
6550 return true;
6551 } else if (klass->HasSuperClass()) {
6552 const size_t super_vtable_length = klass->GetSuperClass()->GetVTableLength();
6553 const size_t max_count = num_virtual_methods + super_vtable_length;
6554 StackHandleScope<3> hs(self);
6555 Handle<mirror::Class> super_class(hs.NewHandle(klass->GetSuperClass()));
6556 MutableHandle<mirror::PointerArray> vtable;
6557 if (super_class->ShouldHaveEmbeddedVTable()) {
6558 vtable = hs.NewHandle(AllocPointerArray(self, max_count));
6559 if (UNLIKELY(vtable == nullptr)) {
6560 self->AssertPendingOOMException();
6561 return false;
6562 }
6563 for (size_t i = 0; i < super_vtable_length; i++) {
6564 vtable->SetElementPtrSize(
6565 i, super_class->GetEmbeddedVTableEntry(i, image_pointer_size_), image_pointer_size_);
6566 }
6567 // We might need to change vtable if we have new virtual methods or new interfaces (since that
6568 // might give us new default methods). If no new interfaces then we can skip the rest since
6569 // the class cannot override any of the super-class's methods. This is required for
6570 // correctness since without it we might not update overridden default method vtable entries
6571 // correctly.
6572 if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) {
6573 klass->SetVTable(vtable.Get());
6574 return true;
6575 }
6576 } else {
6577 DCHECK(super_class->IsAbstract() && !super_class->IsArrayClass());
6578 Handle<mirror::PointerArray> super_vtable = hs.NewHandle(super_class->GetVTable());
6579 CHECK(super_vtable != nullptr) << super_class->PrettyClass();
6580 // We might need to change vtable if we have new virtual methods or new interfaces (since that
6581 // might give us new default methods). See comment above.
6582 if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) {
6583 klass->SetVTable(super_vtable.Get());
6584 return true;
6585 }
6586 vtable = hs.NewHandle(ObjPtr<mirror::PointerArray>::DownCast(
6587 mirror::Array::CopyOf(super_vtable, self, max_count)));
6588 if (UNLIKELY(vtable == nullptr)) {
6589 self->AssertPendingOOMException();
6590 return false;
6591 }
6592 }
6593 // How the algorithm works:
6594 // 1. Populate hash table by adding num_virtual_methods from klass. The values in the hash
6595 // table are: invalid_index for unused slots, index super_vtable_length + i for a virtual
6596 // method which has not been matched to a vtable method, and j if the virtual method at the
6597 // index overrode the super virtual method at index j.
6598 // 2. Loop through super virtual methods, if they overwrite, update hash table to j
6599 // (j < super_vtable_length) to avoid redundant checks. (TODO maybe use this info for reducing
6600 // the need for the initial vtable which we later shrink back down).
6601 // 3. Add non overridden methods to the end of the vtable.
6602 static constexpr size_t kMaxStackHash = 250;
6603 // + 1 so that even if we only have new default methods we will still be able to use this hash
6604 // table (i.e. it will never have 0 size).
6605 const size_t hash_table_size = num_virtual_methods * 3 + 1;
6606 uint32_t* hash_table_ptr;
6607 std::unique_ptr<uint32_t[]> hash_heap_storage;
6608 if (hash_table_size <= kMaxStackHash) {
6609 hash_table_ptr = reinterpret_cast<uint32_t*>(
6610 alloca(hash_table_size * sizeof(*hash_table_ptr)));
6611 } else {
6612 hash_heap_storage.reset(new uint32_t[hash_table_size]);
6613 hash_table_ptr = hash_heap_storage.get();
6614 }
6615 LinkVirtualHashTable hash_table(klass, hash_table_size, hash_table_ptr, image_pointer_size_);
6616 // Add virtual methods to the hash table.
6617 for (size_t i = 0; i < num_virtual_methods; ++i) {
6618 DCHECK(klass->GetVirtualMethodDuringLinking(
6619 i, image_pointer_size_)->GetDeclaringClass() != nullptr);
6620 hash_table.Add(i);
6621 }
6622 // Loop through each super vtable method and see if they are overridden by a method we added to
6623 // the hash table.
6624 for (size_t j = 0; j < super_vtable_length; ++j) {
6625 // Search the hash table to see if we are overridden by any method.
6626 ArtMethod* super_method = vtable->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_);
6627 if (!klass->CanAccessMember(super_method->GetDeclaringClass(),
6628 super_method->GetAccessFlags())) {
6629 // Continue on to the next method since this one is package private and canot be overridden.
6630 // Before Android 4.1, the package-private method super_method might have been incorrectly
6631 // overridden.
6632 continue;
6633 }
6634 MethodNameAndSignatureComparator super_method_name_comparator(
6635 super_method->GetInterfaceMethodIfProxy(image_pointer_size_));
6636 // We remove the method so that subsequent lookups will be faster by making the hash-map
6637 // smaller as we go on.
6638 uint32_t hash_index = hash_table.FindAndRemove(&super_method_name_comparator);
6639 if (hash_index != hash_table.GetNotFoundIndex()) {
6640 ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(
6641 hash_index, image_pointer_size_);
6642 if (super_method->IsFinal()) {
6643 ThrowLinkageError(klass.Get(), "Method %s overrides final method in class %s",
6644 virtual_method->PrettyMethod().c_str(),
6645 super_method->GetDeclaringClassDescriptor());
6646 return false;
6647 }
6648 vtable->SetElementPtrSize(j, virtual_method, image_pointer_size_);
6649 virtual_method->SetMethodIndex(j);
6650 } else if (super_method->IsOverridableByDefaultMethod()) {
6651 // We didn't directly override this method but we might through default methods...
6652 // Check for default method update.
6653 ArtMethod* default_method = nullptr;
6654 switch (FindDefaultMethodImplementation(self,
6655 super_method,
6656 klass,
6657 /*out*/&default_method)) {
6658 case DefaultMethodSearchResult::kDefaultConflict: {
6659 // A conflict was found looking for default methods. Note this (assuming it wasn't
6660 // pre-existing) in the translations map.
6661 if (UNLIKELY(!super_method->IsDefaultConflicting())) {
6662 // Don't generate another conflict method to reduce memory use as an optimization.
6663 default_translations->insert(
6664 {j, ClassLinker::MethodTranslation::CreateConflictingMethod()});
6665 }
6666 break;
6667 }
6668 case DefaultMethodSearchResult::kAbstractFound: {
6669 // No conflict but method is abstract.
6670 // We note that this vtable entry must be made abstract.
6671 if (UNLIKELY(!super_method->IsAbstract())) {
6672 default_translations->insert(
6673 {j, ClassLinker::MethodTranslation::CreateAbstractMethod()});
6674 }
6675 break;
6676 }
6677 case DefaultMethodSearchResult::kDefaultFound: {
6678 if (UNLIKELY(super_method->IsDefaultConflicting() ||
6679 default_method->GetDeclaringClass() != super_method->GetDeclaringClass())) {
6680 // Found a default method implementation that is new.
6681 // TODO Refactor this add default methods to virtuals here and not in
6682 // LinkInterfaceMethods maybe.
6683 // The problem is default methods might override previously present
6684 // default-method or miranda-method vtable entries from the superclass.
6685 // Unfortunately we need these to be entries in this class's virtuals. We do not
6686 // give these entries there until LinkInterfaceMethods so we pass this map around
6687 // to let it know which vtable entries need to be updated.
6688 // Make a note that vtable entry j must be updated, store what it needs to be updated
6689 // to. We will allocate a virtual method slot in LinkInterfaceMethods and fix it up
6690 // then.
6691 default_translations->insert(
6692 {j, ClassLinker::MethodTranslation::CreateTranslatedMethod(default_method)});
6693 VLOG(class_linker) << "Method " << super_method->PrettyMethod()
6694 << " overridden by default "
6695 << default_method->PrettyMethod()
6696 << " in " << mirror::Class::PrettyClass(klass.Get());
6697 }
6698 break;
6699 }
6700 }
6701 }
6702 }
6703 size_t actual_count = super_vtable_length;
6704 // Add the non-overridden methods at the end.
6705 for (size_t i = 0; i < num_virtual_methods; ++i) {
6706 ArtMethod* local_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_);
6707 size_t method_idx = local_method->GetMethodIndexDuringLinking();
6708 if (method_idx < super_vtable_length &&
6709 local_method == vtable->GetElementPtrSize<ArtMethod*>(method_idx, image_pointer_size_)) {
6710 continue;
6711 }
6712 vtable->SetElementPtrSize(actual_count, local_method, image_pointer_size_);
6713 local_method->SetMethodIndex(actual_count);
6714 ++actual_count;
6715 }
6716 if (!IsUint<16>(actual_count)) {
6717 ThrowClassFormatError(klass.Get(), "Too many methods defined on class: %zd", actual_count);
6718 return false;
6719 }
6720 // Shrink vtable if possible
6721 CHECK_LE(actual_count, max_count);
6722 if (actual_count < max_count) {
6723 vtable.Assign(ObjPtr<mirror::PointerArray>::DownCast(
6724 mirror::Array::CopyOf(vtable, self, actual_count)));
6725 if (UNLIKELY(vtable == nullptr)) {
6726 self->AssertPendingOOMException();
6727 return false;
6728 }
6729 }
6730 klass->SetVTable(vtable.Get());
6731 } else {
6732 CHECK_EQ(klass.Get(), GetClassRoot<mirror::Object>(this));
6733 if (!IsUint<16>(num_virtual_methods)) {
6734 ThrowClassFormatError(klass.Get(), "Too many methods: %d",
6735 static_cast<int>(num_virtual_methods));
6736 return false;
6737 }
6738 ObjPtr<mirror::PointerArray> vtable = AllocPointerArray(self, num_virtual_methods);
6739 if (UNLIKELY(vtable == nullptr)) {
6740 self->AssertPendingOOMException();
6741 return false;
6742 }
6743 for (size_t i = 0; i < num_virtual_methods; ++i) {
6744 ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_);
6745 vtable->SetElementPtrSize(i, virtual_method, image_pointer_size_);
6746 virtual_method->SetMethodIndex(i & 0xFFFF);
6747 }
6748 klass->SetVTable(vtable);
6749 }
6750 return true;
6751 }
6752
6753 // Determine if the given iface has any subinterface in the given list that declares the method
6754 // specified by 'target'.
6755 //
6756 // Arguments
6757 // - self: The thread we are running on
6758 // - target: A comparator that will match any method that overrides the method we are checking for
6759 // - iftable: The iftable we are searching for an overriding method on.
6760 // - ifstart: The index of the interface we are checking to see if anything overrides
6761 // - iface: The interface we are checking to see if anything overrides.
6762 // - image_pointer_size:
6763 // The image pointer size.
6764 //
6765 // Returns
6766 // - True: There is some method that matches the target comparator defined in an interface that
6767 // is a subtype of iface.
6768 // - False: There is no method that matches the target comparator in any interface that is a subtype
6769 // of iface.
ContainsOverridingMethodOf(Thread * self,MethodNameAndSignatureComparator & target,Handle<mirror::IfTable> iftable,size_t ifstart,Handle<mirror::Class> iface,PointerSize image_pointer_size)6770 static bool ContainsOverridingMethodOf(Thread* self,
6771 MethodNameAndSignatureComparator& target,
6772 Handle<mirror::IfTable> iftable,
6773 size_t ifstart,
6774 Handle<mirror::Class> iface,
6775 PointerSize image_pointer_size)
6776 REQUIRES_SHARED(Locks::mutator_lock_) {
6777 DCHECK(self != nullptr);
6778 DCHECK(iface != nullptr);
6779 DCHECK(iftable != nullptr);
6780 DCHECK_GE(ifstart, 0u);
6781 DCHECK_LT(ifstart, iftable->Count());
6782 DCHECK_EQ(iface.Get(), iftable->GetInterface(ifstart));
6783 DCHECK(iface->IsInterface());
6784
6785 size_t iftable_count = iftable->Count();
6786 StackHandleScope<1> hs(self);
6787 MutableHandle<mirror::Class> current_iface(hs.NewHandle<mirror::Class>(nullptr));
6788 for (size_t k = ifstart + 1; k < iftable_count; k++) {
6789 // Skip ifstart since our current interface obviously cannot override itself.
6790 current_iface.Assign(iftable->GetInterface(k));
6791 // Iterate through every method on this interface. The order does not matter.
6792 for (ArtMethod& current_method : current_iface->GetDeclaredVirtualMethods(image_pointer_size)) {
6793 if (UNLIKELY(target.HasSameNameAndSignature(
6794 current_method.GetInterfaceMethodIfProxy(image_pointer_size)))) {
6795 // Check if the i'th interface is a subtype of this one.
6796 if (iface->IsAssignableFrom(current_iface.Get())) {
6797 return true;
6798 }
6799 break;
6800 }
6801 }
6802 }
6803 return false;
6804 }
6805
6806 // Find the default method implementation for 'interface_method' in 'klass'. Stores it into
6807 // out_default_method and returns kDefaultFound on success. If no default method was found return
6808 // kAbstractFound and store nullptr into out_default_method. If an error occurs (such as a
6809 // default_method conflict) it will return kDefaultConflict.
FindDefaultMethodImplementation(Thread * self,ArtMethod * target_method,Handle<mirror::Class> klass,ArtMethod ** out_default_method) const6810 ClassLinker::DefaultMethodSearchResult ClassLinker::FindDefaultMethodImplementation(
6811 Thread* self,
6812 ArtMethod* target_method,
6813 Handle<mirror::Class> klass,
6814 /*out*/ArtMethod** out_default_method) const {
6815 DCHECK(self != nullptr);
6816 DCHECK(target_method != nullptr);
6817 DCHECK(out_default_method != nullptr);
6818
6819 *out_default_method = nullptr;
6820
6821 // We organize the interface table so that, for interface I any subinterfaces J follow it in the
6822 // table. This lets us walk the table backwards when searching for default methods. The first one
6823 // we encounter is the best candidate since it is the most specific. Once we have found it we keep
6824 // track of it and then continue checking all other interfaces, since we need to throw an error if
6825 // we encounter conflicting default method implementations (one is not a subtype of the other).
6826 //
6827 // The order of unrelated interfaces does not matter and is not defined.
6828 size_t iftable_count = klass->GetIfTableCount();
6829 if (iftable_count == 0) {
6830 // No interfaces. We have already reset out to null so just return kAbstractFound.
6831 return DefaultMethodSearchResult::kAbstractFound;
6832 }
6833
6834 StackHandleScope<3> hs(self);
6835 MutableHandle<mirror::Class> chosen_iface(hs.NewHandle<mirror::Class>(nullptr));
6836 MutableHandle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable()));
6837 MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr));
6838 MethodNameAndSignatureComparator target_name_comparator(
6839 target_method->GetInterfaceMethodIfProxy(image_pointer_size_));
6840 // Iterates over the klass's iftable in reverse
6841 for (size_t k = iftable_count; k != 0; ) {
6842 --k;
6843
6844 DCHECK_LT(k, iftable->Count());
6845
6846 iface.Assign(iftable->GetInterface(k));
6847 // Iterate through every declared method on this interface. The order does not matter.
6848 for (auto& method_iter : iface->GetDeclaredVirtualMethods(image_pointer_size_)) {
6849 ArtMethod* current_method = &method_iter;
6850 // Skip abstract methods and methods with different names.
6851 if (current_method->IsAbstract() ||
6852 !target_name_comparator.HasSameNameAndSignature(
6853 current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) {
6854 continue;
6855 } else if (!current_method->IsPublic()) {
6856 // The verifier should have caught the non-public method for dex version 37. Just warn and
6857 // skip it since this is from before default-methods so we don't really need to care that it
6858 // has code.
6859 LOG(WARNING) << "Interface method " << current_method->PrettyMethod()
6860 << " is not public! "
6861 << "This will be a fatal error in subsequent versions of android. "
6862 << "Continuing anyway.";
6863 }
6864 if (UNLIKELY(chosen_iface != nullptr)) {
6865 // We have multiple default impls of the same method. This is a potential default conflict.
6866 // We need to check if this possibly conflicting method is either a superclass of the chosen
6867 // default implementation or is overridden by a non-default interface method. In either case
6868 // there is no conflict.
6869 if (!iface->IsAssignableFrom(chosen_iface.Get()) &&
6870 !ContainsOverridingMethodOf(self,
6871 target_name_comparator,
6872 iftable,
6873 k,
6874 iface,
6875 image_pointer_size_)) {
6876 VLOG(class_linker) << "Conflicting default method implementations found: "
6877 << current_method->PrettyMethod() << " and "
6878 << ArtMethod::PrettyMethod(*out_default_method) << " in class "
6879 << klass->PrettyClass() << " conflict.";
6880 *out_default_method = nullptr;
6881 return DefaultMethodSearchResult::kDefaultConflict;
6882 } else {
6883 break; // Continue checking at the next interface.
6884 }
6885 } else {
6886 // chosen_iface == null
6887 if (!ContainsOverridingMethodOf(self,
6888 target_name_comparator,
6889 iftable,
6890 k,
6891 iface,
6892 image_pointer_size_)) {
6893 // Don't set this as the chosen interface if something else is overriding it (because that
6894 // other interface would be potentially chosen instead if it was default). If the other
6895 // interface was abstract then we wouldn't select this interface as chosen anyway since
6896 // the abstract method masks it.
6897 *out_default_method = current_method;
6898 chosen_iface.Assign(iface.Get());
6899 // We should now finish traversing the graph to find if we have default methods that
6900 // conflict.
6901 } else {
6902 VLOG(class_linker) << "A default method '" << current_method->PrettyMethod()
6903 << "' was "
6904 << "skipped because it was overridden by an abstract method in a "
6905 << "subinterface on class '" << klass->PrettyClass() << "'";
6906 }
6907 }
6908 break;
6909 }
6910 }
6911 if (*out_default_method != nullptr) {
6912 VLOG(class_linker) << "Default method '" << (*out_default_method)->PrettyMethod()
6913 << "' selected "
6914 << "as the implementation for '" << target_method->PrettyMethod()
6915 << "' in '" << klass->PrettyClass() << "'";
6916 return DefaultMethodSearchResult::kDefaultFound;
6917 } else {
6918 return DefaultMethodSearchResult::kAbstractFound;
6919 }
6920 }
6921
AddMethodToConflictTable(ObjPtr<mirror::Class> klass,ArtMethod * conflict_method,ArtMethod * interface_method,ArtMethod * method,bool force_new_conflict_method)6922 ArtMethod* ClassLinker::AddMethodToConflictTable(ObjPtr<mirror::Class> klass,
6923 ArtMethod* conflict_method,
6924 ArtMethod* interface_method,
6925 ArtMethod* method,
6926 bool force_new_conflict_method) {
6927 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
6928 Runtime* const runtime = Runtime::Current();
6929 LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader());
6930 bool new_entry = conflict_method == runtime->GetImtConflictMethod() || force_new_conflict_method;
6931
6932 // Create a new entry if the existing one is the shared conflict method.
6933 ArtMethod* new_conflict_method = new_entry
6934 ? runtime->CreateImtConflictMethod(linear_alloc)
6935 : conflict_method;
6936
6937 // Allocate a new table. Note that we will leak this table at the next conflict,
6938 // but that's a tradeoff compared to making the table fixed size.
6939 void* data = linear_alloc->Alloc(
6940 Thread::Current(), ImtConflictTable::ComputeSizeWithOneMoreEntry(current_table,
6941 image_pointer_size_));
6942 if (data == nullptr) {
6943 LOG(ERROR) << "Failed to allocate conflict table";
6944 return conflict_method;
6945 }
6946 ImtConflictTable* new_table = new (data) ImtConflictTable(current_table,
6947 interface_method,
6948 method,
6949 image_pointer_size_);
6950
6951 // Do a fence to ensure threads see the data in the table before it is assigned
6952 // to the conflict method.
6953 // Note that there is a race in the presence of multiple threads and we may leak
6954 // memory from the LinearAlloc, but that's a tradeoff compared to using
6955 // atomic operations.
6956 std::atomic_thread_fence(std::memory_order_release);
6957 new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_);
6958 return new_conflict_method;
6959 }
6960
AllocateIfTableMethodArrays(Thread * self,Handle<mirror::Class> klass,Handle<mirror::IfTable> iftable)6961 bool ClassLinker::AllocateIfTableMethodArrays(Thread* self,
6962 Handle<mirror::Class> klass,
6963 Handle<mirror::IfTable> iftable) {
6964 DCHECK(!klass->IsInterface());
6965 const bool has_superclass = klass->HasSuperClass();
6966 const bool extend_super_iftable = has_superclass;
6967 const size_t ifcount = klass->GetIfTableCount();
6968 const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U;
6969 for (size_t i = 0; i < ifcount; ++i) {
6970 size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods();
6971 if (num_methods > 0) {
6972 const bool is_super = i < super_ifcount;
6973 // This is an interface implemented by a super-class. Therefore we can just copy the method
6974 // array from the superclass.
6975 const bool super_interface = is_super && extend_super_iftable;
6976 ObjPtr<mirror::PointerArray> method_array;
6977 if (super_interface) {
6978 ObjPtr<mirror::IfTable> if_table = klass->GetSuperClass()->GetIfTable();
6979 DCHECK(if_table != nullptr);
6980 DCHECK(if_table->GetMethodArray(i) != nullptr);
6981 // If we are working on a super interface, try extending the existing method array.
6982 StackHandleScope<1u> hs(self);
6983 Handle<mirror::PointerArray> old_array = hs.NewHandle(if_table->GetMethodArray(i));
6984 method_array =
6985 ObjPtr<mirror::PointerArray>::DownCast(mirror::Object::Clone(old_array, self));
6986 } else {
6987 method_array = AllocPointerArray(self, num_methods);
6988 }
6989 if (UNLIKELY(method_array == nullptr)) {
6990 self->AssertPendingOOMException();
6991 return false;
6992 }
6993 iftable->SetMethodArray(i, method_array);
6994 }
6995 }
6996 return true;
6997 }
6998
SetIMTRef(ArtMethod * unimplemented_method,ArtMethod * imt_conflict_method,ArtMethod * current_method,bool * new_conflict,ArtMethod ** imt_ref)6999 void ClassLinker::SetIMTRef(ArtMethod* unimplemented_method,
7000 ArtMethod* imt_conflict_method,
7001 ArtMethod* current_method,
7002 /*out*/bool* new_conflict,
7003 /*out*/ArtMethod** imt_ref) {
7004 // Place method in imt if entry is empty, place conflict otherwise.
7005 if (*imt_ref == unimplemented_method) {
7006 *imt_ref = current_method;
7007 } else if (!(*imt_ref)->IsRuntimeMethod()) {
7008 // If we are not a conflict and we have the same signature and name as the imt
7009 // entry, it must be that we overwrote a superclass vtable entry.
7010 // Note that we have checked IsRuntimeMethod, as there may be multiple different
7011 // conflict methods.
7012 MethodNameAndSignatureComparator imt_comparator(
7013 (*imt_ref)->GetInterfaceMethodIfProxy(image_pointer_size_));
7014 if (imt_comparator.HasSameNameAndSignature(
7015 current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) {
7016 *imt_ref = current_method;
7017 } else {
7018 *imt_ref = imt_conflict_method;
7019 *new_conflict = true;
7020 }
7021 } else {
7022 // Place the default conflict method. Note that there may be an existing conflict
7023 // method in the IMT, but it could be one tailored to the super class, with a
7024 // specific ImtConflictTable.
7025 *imt_ref = imt_conflict_method;
7026 *new_conflict = true;
7027 }
7028 }
7029
FillIMTAndConflictTables(ObjPtr<mirror::Class> klass)7030 void ClassLinker::FillIMTAndConflictTables(ObjPtr<mirror::Class> klass) {
7031 DCHECK(klass->ShouldHaveImt()) << klass->PrettyClass();
7032 DCHECK(!klass->IsTemp()) << klass->PrettyClass();
7033 ArtMethod* imt_data[ImTable::kSize];
7034 Runtime* const runtime = Runtime::Current();
7035 ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod();
7036 ArtMethod* const conflict_method = runtime->GetImtConflictMethod();
7037 std::fill_n(imt_data, arraysize(imt_data), unimplemented_method);
7038 if (klass->GetIfTable() != nullptr) {
7039 bool new_conflict = false;
7040 FillIMTFromIfTable(klass->GetIfTable(),
7041 unimplemented_method,
7042 conflict_method,
7043 klass,
7044 /*create_conflict_tables=*/true,
7045 /*ignore_copied_methods=*/false,
7046 &new_conflict,
7047 &imt_data[0]);
7048 }
7049 if (!klass->ShouldHaveImt()) {
7050 return;
7051 }
7052 // Compare the IMT with the super class including the conflict methods. If they are equivalent,
7053 // we can just use the same pointer.
7054 ImTable* imt = nullptr;
7055 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
7056 if (super_class != nullptr && super_class->ShouldHaveImt()) {
7057 ImTable* super_imt = super_class->GetImt(image_pointer_size_);
7058 bool same = true;
7059 for (size_t i = 0; same && i < ImTable::kSize; ++i) {
7060 ArtMethod* method = imt_data[i];
7061 ArtMethod* super_method = super_imt->Get(i, image_pointer_size_);
7062 if (method != super_method) {
7063 bool is_conflict_table = method->IsRuntimeMethod() &&
7064 method != unimplemented_method &&
7065 method != conflict_method;
7066 // Verify conflict contents.
7067 bool super_conflict_table = super_method->IsRuntimeMethod() &&
7068 super_method != unimplemented_method &&
7069 super_method != conflict_method;
7070 if (!is_conflict_table || !super_conflict_table) {
7071 same = false;
7072 } else {
7073 ImtConflictTable* table1 = method->GetImtConflictTable(image_pointer_size_);
7074 ImtConflictTable* table2 = super_method->GetImtConflictTable(image_pointer_size_);
7075 same = same && table1->Equals(table2, image_pointer_size_);
7076 }
7077 }
7078 }
7079 if (same) {
7080 imt = super_imt;
7081 }
7082 }
7083 if (imt == nullptr) {
7084 imt = klass->GetImt(image_pointer_size_);
7085 DCHECK(imt != nullptr);
7086 imt->Populate(imt_data, image_pointer_size_);
7087 } else {
7088 klass->SetImt(imt, image_pointer_size_);
7089 }
7090 }
7091
CreateImtConflictTable(size_t count,LinearAlloc * linear_alloc,PointerSize image_pointer_size)7092 ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count,
7093 LinearAlloc* linear_alloc,
7094 PointerSize image_pointer_size) {
7095 void* data = linear_alloc->Alloc(Thread::Current(),
7096 ImtConflictTable::ComputeSize(count,
7097 image_pointer_size));
7098 return (data != nullptr) ? new (data) ImtConflictTable(count, image_pointer_size) : nullptr;
7099 }
7100
CreateImtConflictTable(size_t count,LinearAlloc * linear_alloc)7101 ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, LinearAlloc* linear_alloc) {
7102 return CreateImtConflictTable(count, linear_alloc, image_pointer_size_);
7103 }
7104
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)7105 void ClassLinker::FillIMTFromIfTable(ObjPtr<mirror::IfTable> if_table,
7106 ArtMethod* unimplemented_method,
7107 ArtMethod* imt_conflict_method,
7108 ObjPtr<mirror::Class> klass,
7109 bool create_conflict_tables,
7110 bool ignore_copied_methods,
7111 /*out*/bool* new_conflict,
7112 /*out*/ArtMethod** imt) {
7113 uint32_t conflict_counts[ImTable::kSize] = {};
7114 for (size_t i = 0, length = if_table->Count(); i < length; ++i) {
7115 ObjPtr<mirror::Class> interface = if_table->GetInterface(i);
7116 const size_t num_virtuals = interface->NumVirtualMethods();
7117 const size_t method_array_count = if_table->GetMethodArrayCount(i);
7118 // Virtual methods can be larger than the if table methods if there are default methods.
7119 DCHECK_GE(num_virtuals, method_array_count);
7120 if (kIsDebugBuild) {
7121 if (klass->IsInterface()) {
7122 DCHECK_EQ(method_array_count, 0u);
7123 } else {
7124 DCHECK_EQ(interface->NumDeclaredVirtualMethods(), method_array_count);
7125 }
7126 }
7127 if (method_array_count == 0) {
7128 continue;
7129 }
7130 ObjPtr<mirror::PointerArray> method_array = if_table->GetMethodArray(i);
7131 for (size_t j = 0; j < method_array_count; ++j) {
7132 ArtMethod* implementation_method =
7133 method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_);
7134 if (ignore_copied_methods && implementation_method->IsCopied()) {
7135 continue;
7136 }
7137 DCHECK(implementation_method != nullptr);
7138 // Miranda methods cannot be used to implement an interface method, but they are safe to put
7139 // in the IMT since their entrypoint is the interface trampoline. If we put any copied methods
7140 // or interface methods in the IMT here they will not create extra conflicts since we compare
7141 // names and signatures in SetIMTRef.
7142 ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_);
7143 const uint32_t imt_index = interface_method->GetImtIndex();
7144
7145 // There is only any conflicts if all of the interface methods for an IMT slot don't have
7146 // the same implementation method, keep track of this to avoid creating a conflict table in
7147 // this case.
7148
7149 // Conflict table size for each IMT slot.
7150 ++conflict_counts[imt_index];
7151
7152 SetIMTRef(unimplemented_method,
7153 imt_conflict_method,
7154 implementation_method,
7155 /*out*/new_conflict,
7156 /*out*/&imt[imt_index]);
7157 }
7158 }
7159
7160 if (create_conflict_tables) {
7161 // Create the conflict tables.
7162 LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader());
7163 for (size_t i = 0; i < ImTable::kSize; ++i) {
7164 size_t conflicts = conflict_counts[i];
7165 if (imt[i] == imt_conflict_method) {
7166 ImtConflictTable* new_table = CreateImtConflictTable(conflicts, linear_alloc);
7167 if (new_table != nullptr) {
7168 ArtMethod* new_conflict_method =
7169 Runtime::Current()->CreateImtConflictMethod(linear_alloc);
7170 new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_);
7171 imt[i] = new_conflict_method;
7172 } else {
7173 LOG(ERROR) << "Failed to allocate conflict table";
7174 imt[i] = imt_conflict_method;
7175 }
7176 } else {
7177 DCHECK_NE(imt[i], imt_conflict_method);
7178 }
7179 }
7180
7181 for (size_t i = 0, length = if_table->Count(); i < length; ++i) {
7182 ObjPtr<mirror::Class> interface = if_table->GetInterface(i);
7183 const size_t method_array_count = if_table->GetMethodArrayCount(i);
7184 // Virtual methods can be larger than the if table methods if there are default methods.
7185 if (method_array_count == 0) {
7186 continue;
7187 }
7188 ObjPtr<mirror::PointerArray> method_array = if_table->GetMethodArray(i);
7189 for (size_t j = 0; j < method_array_count; ++j) {
7190 ArtMethod* implementation_method =
7191 method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_);
7192 if (ignore_copied_methods && implementation_method->IsCopied()) {
7193 continue;
7194 }
7195 DCHECK(implementation_method != nullptr);
7196 ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_);
7197 const uint32_t imt_index = interface_method->GetImtIndex();
7198 if (!imt[imt_index]->IsRuntimeMethod() ||
7199 imt[imt_index] == unimplemented_method ||
7200 imt[imt_index] == imt_conflict_method) {
7201 continue;
7202 }
7203 ImtConflictTable* table = imt[imt_index]->GetImtConflictTable(image_pointer_size_);
7204 const size_t num_entries = table->NumEntries(image_pointer_size_);
7205 table->SetInterfaceMethod(num_entries, image_pointer_size_, interface_method);
7206 table->SetImplementationMethod(num_entries, image_pointer_size_, implementation_method);
7207 }
7208 }
7209 }
7210 }
7211
7212 // Simple helper function that checks that no subtypes of 'val' are contained within the 'classes'
7213 // set.
NotSubinterfaceOfAny(const std::unordered_set<ObjPtr<mirror::Class>,HashObjPtr> & classes,ObjPtr<mirror::Class> val)7214 static bool NotSubinterfaceOfAny(
7215 const std::unordered_set<ObjPtr<mirror::Class>, HashObjPtr>& classes,
7216 ObjPtr<mirror::Class> val)
7217 REQUIRES(Roles::uninterruptible_)
7218 REQUIRES_SHARED(Locks::mutator_lock_) {
7219 DCHECK(val != nullptr);
7220 for (ObjPtr<mirror::Class> c : classes) {
7221 if (val->IsAssignableFrom(c)) {
7222 return false;
7223 }
7224 }
7225 return true;
7226 }
7227
7228 // Fills in and flattens the interface inheritance hierarchy.
7229 //
7230 // By the end of this function all interfaces in the transitive closure of to_process are added to
7231 // the iftable and every interface precedes all of its sub-interfaces in this list.
7232 //
7233 // all I, J: Interface | I <: J implies J precedes I
7234 //
7235 // (note A <: B means that A is a subtype of B)
7236 //
7237 // This returns the total number of items in the iftable. The iftable might be resized down after
7238 // this call.
7239 //
7240 // We order this backwards so that we do not need to reorder superclass interfaces when new
7241 // interfaces are added in subclass's interface tables.
7242 //
7243 // Upon entry into this function iftable is a copy of the superclass's iftable with the first
7244 // super_ifcount entries filled in with the transitive closure of the interfaces of the superclass.
7245 // The other entries are uninitialized. We will fill in the remaining entries in this function. The
7246 // iftable must be large enough to hold all interfaces without changing its size.
FillIfTable(ObjPtr<mirror::IfTable> iftable,size_t super_ifcount,const std::vector<ObjPtr<mirror::Class>> & to_process)7247 static size_t FillIfTable(ObjPtr<mirror::IfTable> iftable,
7248 size_t super_ifcount,
7249 const std::vector<ObjPtr<mirror::Class>>& to_process)
7250 REQUIRES(Roles::uninterruptible_)
7251 REQUIRES_SHARED(Locks::mutator_lock_) {
7252 // This is the set of all class's already in the iftable. Used to make checking if a class has
7253 // already been added quicker.
7254 std::unordered_set<ObjPtr<mirror::Class>, HashObjPtr> classes_in_iftable;
7255 // The first super_ifcount elements are from the superclass. We note that they are already added.
7256 for (size_t i = 0; i < super_ifcount; i++) {
7257 ObjPtr<mirror::Class> iface = iftable->GetInterface(i);
7258 DCHECK(NotSubinterfaceOfAny(classes_in_iftable, iface)) << "Bad ordering.";
7259 classes_in_iftable.insert(iface);
7260 }
7261 size_t filled_ifcount = super_ifcount;
7262 for (ObjPtr<mirror::Class> interface : to_process) {
7263 // Let us call the first filled_ifcount elements of iftable the current-iface-list.
7264 // At this point in the loop current-iface-list has the invariant that:
7265 // for every pair of interfaces I,J within it:
7266 // if index_of(I) < index_of(J) then I is not a subtype of J
7267
7268 // If we have already seen this element then all of its super-interfaces must already be in the
7269 // current-iface-list so we can skip adding it.
7270 if (!ContainsElement(classes_in_iftable, interface)) {
7271 // We haven't seen this interface so add all of its super-interfaces onto the
7272 // current-iface-list, skipping those already on it.
7273 int32_t ifcount = interface->GetIfTableCount();
7274 for (int32_t j = 0; j < ifcount; j++) {
7275 ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j);
7276 if (!ContainsElement(classes_in_iftable, super_interface)) {
7277 DCHECK(NotSubinterfaceOfAny(classes_in_iftable, super_interface)) << "Bad ordering.";
7278 classes_in_iftable.insert(super_interface);
7279 iftable->SetInterface(filled_ifcount, super_interface);
7280 filled_ifcount++;
7281 }
7282 }
7283 DCHECK(NotSubinterfaceOfAny(classes_in_iftable, interface)) << "Bad ordering";
7284 // Place this interface onto the current-iface-list after all of its super-interfaces.
7285 classes_in_iftable.insert(interface);
7286 iftable->SetInterface(filled_ifcount, interface);
7287 filled_ifcount++;
7288 } else if (kIsDebugBuild) {
7289 // Check all super-interfaces are already in the list.
7290 int32_t ifcount = interface->GetIfTableCount();
7291 for (int32_t j = 0; j < ifcount; j++) {
7292 ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j);
7293 DCHECK(ContainsElement(classes_in_iftable, super_interface))
7294 << "Iftable does not contain " << mirror::Class::PrettyClass(super_interface)
7295 << ", a superinterface of " << interface->PrettyClass();
7296 }
7297 }
7298 }
7299 if (kIsDebugBuild) {
7300 // Check that the iftable is ordered correctly.
7301 for (size_t i = 0; i < filled_ifcount; i++) {
7302 ObjPtr<mirror::Class> if_a = iftable->GetInterface(i);
7303 for (size_t j = i + 1; j < filled_ifcount; j++) {
7304 ObjPtr<mirror::Class> if_b = iftable->GetInterface(j);
7305 // !(if_a <: if_b)
7306 CHECK(!if_b->IsAssignableFrom(if_a))
7307 << "Bad interface order: " << mirror::Class::PrettyClass(if_a) << " (index " << i
7308 << ") extends "
7309 << if_b->PrettyClass() << " (index " << j << ") and so should be after it in the "
7310 << "interface list.";
7311 }
7312 }
7313 }
7314 return filled_ifcount;
7315 }
7316
SetupInterfaceLookupTable(Thread * self,Handle<mirror::Class> klass,Handle<mirror::ObjectArray<mirror::Class>> interfaces)7317 bool ClassLinker::SetupInterfaceLookupTable(Thread* self, Handle<mirror::Class> klass,
7318 Handle<mirror::ObjectArray<mirror::Class>> interfaces) {
7319 StackHandleScope<1> hs(self);
7320 const bool has_superclass = klass->HasSuperClass();
7321 const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U;
7322 const bool have_interfaces = interfaces != nullptr;
7323 const size_t num_interfaces =
7324 have_interfaces ? interfaces->GetLength() : klass->NumDirectInterfaces();
7325 if (num_interfaces == 0) {
7326 if (super_ifcount == 0) {
7327 if (LIKELY(has_superclass)) {
7328 klass->SetIfTable(klass->GetSuperClass()->GetIfTable());
7329 }
7330 // Class implements no interfaces.
7331 DCHECK_EQ(klass->GetIfTableCount(), 0);
7332 return true;
7333 }
7334 // Class implements same interfaces as parent, are any of these not marker interfaces?
7335 bool has_non_marker_interface = false;
7336 ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable();
7337 for (size_t i = 0; i < super_ifcount; ++i) {
7338 if (super_iftable->GetMethodArrayCount(i) > 0) {
7339 has_non_marker_interface = true;
7340 break;
7341 }
7342 }
7343 // Class just inherits marker interfaces from parent so recycle parent's iftable.
7344 if (!has_non_marker_interface) {
7345 klass->SetIfTable(super_iftable);
7346 return true;
7347 }
7348 }
7349 size_t ifcount = super_ifcount + num_interfaces;
7350 // Check that every class being implemented is an interface.
7351 for (size_t i = 0; i < num_interfaces; i++) {
7352 ObjPtr<mirror::Class> interface = have_interfaces
7353 ? interfaces->GetWithoutChecks(i)
7354 : mirror::Class::GetDirectInterface(self, klass.Get(), i);
7355 DCHECK(interface != nullptr);
7356 if (UNLIKELY(!interface->IsInterface())) {
7357 std::string temp;
7358 ThrowIncompatibleClassChangeError(klass.Get(),
7359 "Class %s implements non-interface class %s",
7360 klass->PrettyDescriptor().c_str(),
7361 PrettyDescriptor(interface->GetDescriptor(&temp)).c_str());
7362 return false;
7363 }
7364 ifcount += interface->GetIfTableCount();
7365 }
7366 // Create the interface function table.
7367 MutableHandle<mirror::IfTable> iftable(hs.NewHandle(AllocIfTable(self, ifcount)));
7368 if (UNLIKELY(iftable == nullptr)) {
7369 self->AssertPendingOOMException();
7370 return false;
7371 }
7372 // Fill in table with superclass's iftable.
7373 if (super_ifcount != 0) {
7374 ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable();
7375 for (size_t i = 0; i < super_ifcount; i++) {
7376 ObjPtr<mirror::Class> super_interface = super_iftable->GetInterface(i);
7377 iftable->SetInterface(i, super_interface);
7378 }
7379 }
7380
7381 // Note that AllowThreadSuspension is to thread suspension as pthread_testcancel is to pthread
7382 // cancellation. That is it will suspend if one has a pending suspend request but otherwise
7383 // doesn't really do anything.
7384 self->AllowThreadSuspension();
7385
7386 size_t new_ifcount;
7387 {
7388 ScopedAssertNoThreadSuspension nts("Copying mirror::Class*'s for FillIfTable");
7389 std::vector<ObjPtr<mirror::Class>> to_add;
7390 for (size_t i = 0; i < num_interfaces; i++) {
7391 ObjPtr<mirror::Class> interface = have_interfaces ? interfaces->Get(i) :
7392 mirror::Class::GetDirectInterface(self, klass.Get(), i);
7393 to_add.push_back(interface);
7394 }
7395
7396 new_ifcount = FillIfTable(iftable.Get(), super_ifcount, std::move(to_add));
7397 }
7398
7399 self->AllowThreadSuspension();
7400
7401 // Shrink iftable in case duplicates were found
7402 if (new_ifcount < ifcount) {
7403 DCHECK_NE(num_interfaces, 0U);
7404 iftable.Assign(ObjPtr<mirror::IfTable>::DownCast(
7405 mirror::IfTable::CopyOf(iftable, self, new_ifcount * mirror::IfTable::kMax)));
7406 if (UNLIKELY(iftable == nullptr)) {
7407 self->AssertPendingOOMException();
7408 return false;
7409 }
7410 ifcount = new_ifcount;
7411 } else {
7412 DCHECK_EQ(new_ifcount, ifcount);
7413 }
7414 klass->SetIfTable(iftable.Get());
7415 return true;
7416 }
7417
7418 // Finds the method with a name/signature that matches cmp in the given lists of methods. The list
7419 // of methods must be unique.
FindSameNameAndSignature(MethodNameAndSignatureComparator & cmp ATTRIBUTE_UNUSED)7420 static ArtMethod* FindSameNameAndSignature(MethodNameAndSignatureComparator& cmp ATTRIBUTE_UNUSED) {
7421 return nullptr;
7422 }
7423
7424 template <typename ... Types>
FindSameNameAndSignature(MethodNameAndSignatureComparator & cmp,const ScopedArenaVector<ArtMethod * > & list,const Types &...rest)7425 static ArtMethod* FindSameNameAndSignature(MethodNameAndSignatureComparator& cmp,
7426 const ScopedArenaVector<ArtMethod*>& list,
7427 const Types& ... rest)
7428 REQUIRES_SHARED(Locks::mutator_lock_) {
7429 for (ArtMethod* method : list) {
7430 if (cmp.HasSameNameAndSignature(method)) {
7431 return method;
7432 }
7433 }
7434 return FindSameNameAndSignature(cmp, rest...);
7435 }
7436
7437 namespace {
7438
7439 // Check that all vtable entries are present in this class's virtuals or are the same as a
7440 // superclasses vtable entry.
CheckClassOwnsVTableEntries(Thread * self,Handle<mirror::Class> klass,PointerSize pointer_size)7441 void CheckClassOwnsVTableEntries(Thread* self,
7442 Handle<mirror::Class> klass,
7443 PointerSize pointer_size)
7444 REQUIRES_SHARED(Locks::mutator_lock_) {
7445 StackHandleScope<2> hs(self);
7446 Handle<mirror::PointerArray> check_vtable(hs.NewHandle(klass->GetVTableDuringLinking()));
7447 ObjPtr<mirror::Class> super_temp = (klass->HasSuperClass()) ? klass->GetSuperClass() : nullptr;
7448 Handle<mirror::Class> superclass(hs.NewHandle(super_temp));
7449 int32_t super_vtable_length = (superclass != nullptr) ? superclass->GetVTableLength() : 0;
7450 for (int32_t i = 0; i < check_vtable->GetLength(); ++i) {
7451 ArtMethod* m = check_vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size);
7452 CHECK(m != nullptr);
7453
7454 if (m->GetMethodIndexDuringLinking() != i) {
7455 LOG(WARNING) << m->PrettyMethod()
7456 << " has an unexpected method index for its spot in the vtable for class"
7457 << klass->PrettyClass();
7458 }
7459 ArraySlice<ArtMethod> virtuals = klass->GetVirtualMethodsSliceUnchecked(pointer_size);
7460 auto is_same_method = [m] (const ArtMethod& meth) {
7461 return &meth == m;
7462 };
7463 if (!((super_vtable_length > i && superclass->GetVTableEntry(i, pointer_size) == m) ||
7464 std::find_if(virtuals.begin(), virtuals.end(), is_same_method) != virtuals.end())) {
7465 LOG(WARNING) << m->PrettyMethod() << " does not seem to be owned by current class "
7466 << klass->PrettyClass() << " or any of its superclasses!";
7467 }
7468 }
7469 }
7470
7471 // Check to make sure the vtable does not have duplicates. Duplicates could cause problems when a
7472 // method is overridden in a subclass.
7473 template <PointerSize kPointerSize>
CheckVTableHasNoDuplicates(Thread * self,Handle<mirror::Class> klass)7474 void CheckVTableHasNoDuplicates(Thread* self, Handle<mirror::Class> klass)
7475 REQUIRES_SHARED(Locks::mutator_lock_) {
7476 StackHandleScope<1> hs(self);
7477 Handle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking()));
7478 int32_t num_entries = vtable->GetLength();
7479
7480 // Observations:
7481 // * The older implementation was O(n^2) and got too expensive for apps with larger classes.
7482 // * Many classes do not override Object functions (e.g., equals/hashCode/toString). Thus,
7483 // for many classes outside of libcore a cross-dexfile check has to be run anyways.
7484 // * In the cross-dexfile case, with the O(n^2), in the best case O(n) cross checks would have
7485 // to be done. It is thus OK in a single-pass algorithm to read all data, anyways.
7486 // * The single-pass algorithm will trade memory for speed, but that is OK.
7487
7488 CHECK_GT(num_entries, 0);
7489
7490 auto log_fn = [&vtable, &klass](int32_t i, int32_t j) REQUIRES_SHARED(Locks::mutator_lock_) {
7491 ArtMethod* m1 = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(i);
7492 ArtMethod* m2 = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(j);
7493 LOG(WARNING) << "vtable entries " << i << " and " << j << " are identical for "
7494 << klass->PrettyClass() << " in method " << m1->PrettyMethod()
7495 << " (0x" << std::hex << reinterpret_cast<uintptr_t>(m2) << ") and "
7496 << m2->PrettyMethod() << " (0x" << std::hex
7497 << reinterpret_cast<uintptr_t>(m2) << ")";
7498 };
7499 struct BaseHashType {
7500 static size_t HashCombine(size_t seed, size_t val) {
7501 return seed ^ (val + 0x9e3779b9 + (seed << 6) + (seed >> 2));
7502 }
7503 };
7504
7505 // Check assuming all entries come from the same dex file.
7506 {
7507 // Find the first interesting method and its dex file.
7508 int32_t start = 0;
7509 for (; start < num_entries; ++start) {
7510 ArtMethod* vtable_entry = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(start);
7511 // Don't bother if we cannot 'see' the vtable entry (i.e. it is a package-private member
7512 // maybe).
7513 if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(),
7514 vtable_entry->GetAccessFlags())) {
7515 continue;
7516 }
7517 break;
7518 }
7519 if (start == num_entries) {
7520 return;
7521 }
7522 const DexFile* dex_file =
7523 vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(start)->
7524 GetInterfaceMethodIfProxy(kPointerSize)->GetDexFile();
7525
7526 // Helper function to avoid logging if we have to run the cross-file checks.
7527 auto check_fn = [&](bool log_warn) REQUIRES_SHARED(Locks::mutator_lock_) {
7528 // Use a map to store seen entries, as the storage space is too large for a bitvector.
7529 using PairType = std::pair<uint32_t, uint16_t>;
7530 struct PairHash : BaseHashType {
7531 size_t operator()(const PairType& key) const {
7532 return BaseHashType::HashCombine(BaseHashType::HashCombine(0, key.first), key.second);
7533 }
7534 };
7535 std::unordered_map<PairType, int32_t, PairHash> seen;
7536 seen.reserve(2 * num_entries);
7537 bool need_slow_path = false;
7538 bool found_dup = false;
7539 for (int i = start; i < num_entries; ++i) {
7540 // Can use Unchecked here as the start loop already ensured that the arrays are correct
7541 // wrt/ kPointerSize.
7542 ArtMethod* vtable_entry = vtable->GetElementPtrSizeUnchecked<ArtMethod*, kPointerSize>(i);
7543 if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(),
7544 vtable_entry->GetAccessFlags())) {
7545 continue;
7546 }
7547 ArtMethod* m = vtable_entry->GetInterfaceMethodIfProxy(kPointerSize);
7548 if (dex_file != m->GetDexFile()) {
7549 need_slow_path = true;
7550 break;
7551 }
7552 const dex::MethodId* m_mid = &dex_file->GetMethodId(m->GetDexMethodIndex());
7553 PairType pair = std::make_pair(m_mid->name_idx_.index_, m_mid->proto_idx_.index_);
7554 auto it = seen.find(pair);
7555 if (it != seen.end()) {
7556 found_dup = true;
7557 if (log_warn) {
7558 log_fn(it->second, i);
7559 }
7560 } else {
7561 seen.emplace(pair, i);
7562 }
7563 }
7564 return std::make_pair(need_slow_path, found_dup);
7565 };
7566 std::pair<bool, bool> result = check_fn(/* log_warn= */ false);
7567 if (!result.first) {
7568 if (result.second) {
7569 check_fn(/* log_warn= */ true);
7570 }
7571 return;
7572 }
7573 }
7574
7575 // Need to check across dex files.
7576 struct Entry {
7577 size_t cached_hash = 0;
7578 const char* name = nullptr;
7579 Signature signature = Signature::NoSignature();
7580 uint32_t name_len = 0;
7581
7582 Entry(const DexFile* dex_file, const dex::MethodId& mid)
7583 : name(dex_file->StringDataAndUtf16LengthByIdx(mid.name_idx_, &name_len)),
7584 signature(dex_file->GetMethodSignature(mid)) {
7585 }
7586
7587 bool operator==(const Entry& other) const {
7588 if (name_len != other.name_len || strcmp(name, other.name) != 0) {
7589 return false;
7590 }
7591 return signature == other.signature;
7592 }
7593 };
7594 struct EntryHash {
7595 size_t operator()(const Entry& key) const {
7596 return key.cached_hash;
7597 }
7598 };
7599 std::unordered_map<Entry, int32_t, EntryHash> map;
7600 for (int32_t i = 0; i < num_entries; ++i) {
7601 // Can use Unchecked here as the first loop already ensured that the arrays are correct
7602 // wrt/ kPointerSize.
7603 ArtMethod* vtable_entry = vtable->GetElementPtrSizeUnchecked<ArtMethod*, kPointerSize>(i);
7604 // Don't bother if we cannot 'see' the vtable entry (i.e. it is a package-private member
7605 // maybe).
7606 if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(),
7607 vtable_entry->GetAccessFlags())) {
7608 continue;
7609 }
7610 ArtMethod* m = vtable_entry->GetInterfaceMethodIfProxy(kPointerSize);
7611 const DexFile* dex_file = m->GetDexFile();
7612 const dex::MethodId& mid = dex_file->GetMethodId(m->GetDexMethodIndex());
7613
7614 Entry e(dex_file, mid);
7615
7616 size_t string_hash = std::hash<std::string_view>()(std::string_view(e.name, e.name_len));
7617 size_t sig_hash = std::hash<std::string>()(e.signature.ToString());
7618 e.cached_hash = BaseHashType::HashCombine(BaseHashType::HashCombine(0u, string_hash),
7619 sig_hash);
7620
7621 auto it = map.find(e);
7622 if (it != map.end()) {
7623 log_fn(it->second, i);
7624 } else {
7625 map.emplace(e, i);
7626 }
7627 }
7628 }
7629
CheckVTableHasNoDuplicates(Thread * self,Handle<mirror::Class> klass,PointerSize pointer_size)7630 void CheckVTableHasNoDuplicates(Thread* self,
7631 Handle<mirror::Class> klass,
7632 PointerSize pointer_size)
7633 REQUIRES_SHARED(Locks::mutator_lock_) {
7634 switch (pointer_size) {
7635 case PointerSize::k64:
7636 CheckVTableHasNoDuplicates<PointerSize::k64>(self, klass);
7637 break;
7638 case PointerSize::k32:
7639 CheckVTableHasNoDuplicates<PointerSize::k32>(self, klass);
7640 break;
7641 }
7642 }
7643
CheckVTable(Thread * self,Handle<mirror::Class> klass,PointerSize pointer_size)7644 static void CheckVTable(Thread* self, Handle<mirror::Class> klass, PointerSize pointer_size)
7645 REQUIRES_SHARED(Locks::mutator_lock_) {
7646 CheckClassOwnsVTableEntries(self, klass, pointer_size);
7647 CheckVTableHasNoDuplicates(self, klass, pointer_size);
7648 }
7649
7650 } // namespace
7651
FillImtFromSuperClass(Handle<mirror::Class> klass,ArtMethod * unimplemented_method,ArtMethod * imt_conflict_method,bool * new_conflict,ArtMethod ** imt)7652 void ClassLinker::FillImtFromSuperClass(Handle<mirror::Class> klass,
7653 ArtMethod* unimplemented_method,
7654 ArtMethod* imt_conflict_method,
7655 bool* new_conflict,
7656 ArtMethod** imt) {
7657 DCHECK(klass->HasSuperClass());
7658 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
7659 if (super_class->ShouldHaveImt()) {
7660 ImTable* super_imt = super_class->GetImt(image_pointer_size_);
7661 for (size_t i = 0; i < ImTable::kSize; ++i) {
7662 imt[i] = super_imt->Get(i, image_pointer_size_);
7663 }
7664 } else {
7665 // No imt in the super class, need to reconstruct from the iftable.
7666 ObjPtr<mirror::IfTable> if_table = super_class->GetIfTable();
7667 if (if_table->Count() != 0) {
7668 // Ignore copied methods since we will handle these in LinkInterfaceMethods.
7669 FillIMTFromIfTable(if_table,
7670 unimplemented_method,
7671 imt_conflict_method,
7672 klass.Get(),
7673 /*create_conflict_tables=*/false,
7674 /*ignore_copied_methods=*/true,
7675 /*out*/new_conflict,
7676 /*out*/imt);
7677 }
7678 }
7679 }
7680
7681 class ClassLinker::LinkInterfaceMethodsHelper {
7682 public:
LinkInterfaceMethodsHelper(ClassLinker * class_linker,Handle<mirror::Class> klass,Thread * self,Runtime * runtime)7683 LinkInterfaceMethodsHelper(ClassLinker* class_linker,
7684 Handle<mirror::Class> klass,
7685 Thread* self,
7686 Runtime* runtime)
7687 : class_linker_(class_linker),
7688 klass_(klass),
7689 method_alignment_(ArtMethod::Alignment(class_linker->GetImagePointerSize())),
7690 method_size_(ArtMethod::Size(class_linker->GetImagePointerSize())),
7691 self_(self),
7692 stack_(runtime->GetLinearAlloc()->GetArenaPool()),
7693 allocator_(&stack_),
7694 default_conflict_methods_(allocator_.Adapter()),
7695 overriding_default_conflict_methods_(allocator_.Adapter()),
7696 miranda_methods_(allocator_.Adapter()),
7697 default_methods_(allocator_.Adapter()),
7698 overriding_default_methods_(allocator_.Adapter()),
7699 move_table_(allocator_.Adapter()) {
7700 }
7701
7702 ArtMethod* FindMethod(ArtMethod* interface_method,
7703 MethodNameAndSignatureComparator& interface_name_comparator,
7704 ArtMethod* vtable_impl)
7705 REQUIRES_SHARED(Locks::mutator_lock_);
7706
7707 ArtMethod* GetOrCreateMirandaMethod(ArtMethod* interface_method,
7708 MethodNameAndSignatureComparator& interface_name_comparator)
7709 REQUIRES_SHARED(Locks::mutator_lock_);
7710
HasNewVirtuals() const7711 bool HasNewVirtuals() const {
7712 return !(miranda_methods_.empty() &&
7713 default_methods_.empty() &&
7714 overriding_default_methods_.empty() &&
7715 overriding_default_conflict_methods_.empty() &&
7716 default_conflict_methods_.empty());
7717 }
7718
7719 void ReallocMethods() REQUIRES_SHARED(Locks::mutator_lock_);
7720
7721 ObjPtr<mirror::PointerArray> UpdateVtable(
7722 const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations,
7723 Handle<mirror::PointerArray> old_vtable) REQUIRES_SHARED(Locks::mutator_lock_);
7724
7725 void UpdateIfTable(Handle<mirror::IfTable> iftable) REQUIRES_SHARED(Locks::mutator_lock_);
7726
7727 void UpdateIMT(ArtMethod** out_imt);
7728
CheckNoStaleMethodsInDexCache()7729 void CheckNoStaleMethodsInDexCache() REQUIRES_SHARED(Locks::mutator_lock_) {
7730 if (kIsDebugBuild) {
7731 PointerSize pointer_size = class_linker_->GetImagePointerSize();
7732 // Check that there are no stale methods are in the dex cache array.
7733 auto* resolved_methods = klass_->GetDexCache()->GetResolvedMethods();
7734 for (size_t i = 0, count = klass_->GetDexCache()->NumResolvedMethods(); i < count; ++i) {
7735 auto pair = mirror::DexCache::GetNativePairPtrSize(resolved_methods, i, pointer_size);
7736 ArtMethod* m = pair.object;
7737 CHECK(move_table_.find(m) == move_table_.end() ||
7738 // The original versions of copied methods will still be present so allow those too.
7739 // Note that if the first check passes this might fail to GetDeclaringClass().
7740 std::find_if(m->GetDeclaringClass()->GetMethods(pointer_size).begin(),
7741 m->GetDeclaringClass()->GetMethods(pointer_size).end(),
7742 [m] (ArtMethod& meth) {
7743 return &meth == m;
7744 }) != m->GetDeclaringClass()->GetMethods(pointer_size).end())
7745 << "Obsolete method " << m->PrettyMethod() << " is in dex cache!";
7746 }
7747 }
7748 }
7749
ClobberOldMethods(LengthPrefixedArray<ArtMethod> * old_methods,LengthPrefixedArray<ArtMethod> * methods)7750 void ClobberOldMethods(LengthPrefixedArray<ArtMethod>* old_methods,
7751 LengthPrefixedArray<ArtMethod>* methods) {
7752 if (kIsDebugBuild) {
7753 CHECK(methods != nullptr);
7754 // Put some random garbage in old methods to help find stale pointers.
7755 if (methods != old_methods && old_methods != nullptr) {
7756 // Need to make sure the GC is not running since it could be scanning the methods we are
7757 // about to overwrite.
7758 ScopedThreadStateChange tsc(self_, kSuspended);
7759 gc::ScopedGCCriticalSection gcs(self_,
7760 gc::kGcCauseClassLinker,
7761 gc::kCollectorTypeClassLinker);
7762 const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_methods->size(),
7763 method_size_,
7764 method_alignment_);
7765 memset(old_methods, 0xFEu, old_size);
7766 }
7767 }
7768 }
7769
7770 private:
NumberOfNewVirtuals() const7771 size_t NumberOfNewVirtuals() const {
7772 return miranda_methods_.size() +
7773 default_methods_.size() +
7774 overriding_default_conflict_methods_.size() +
7775 overriding_default_methods_.size() +
7776 default_conflict_methods_.size();
7777 }
7778
FillTables()7779 bool FillTables() REQUIRES_SHARED(Locks::mutator_lock_) {
7780 return !klass_->IsInterface();
7781 }
7782
LogNewVirtuals() const7783 void LogNewVirtuals() const REQUIRES_SHARED(Locks::mutator_lock_) {
7784 DCHECK(!klass_->IsInterface() || (default_methods_.empty() && miranda_methods_.empty()))
7785 << "Interfaces should only have default-conflict methods appended to them.";
7786 VLOG(class_linker) << mirror::Class::PrettyClass(klass_.Get()) << ": miranda_methods="
7787 << miranda_methods_.size()
7788 << " default_methods=" << default_methods_.size()
7789 << " overriding_default_methods=" << overriding_default_methods_.size()
7790 << " default_conflict_methods=" << default_conflict_methods_.size()
7791 << " overriding_default_conflict_methods="
7792 << overriding_default_conflict_methods_.size();
7793 }
7794
7795 ClassLinker* class_linker_;
7796 Handle<mirror::Class> klass_;
7797 size_t method_alignment_;
7798 size_t method_size_;
7799 Thread* const self_;
7800
7801 // These are allocated on the heap to begin, we then transfer to linear alloc when we re-create
7802 // the virtual methods array.
7803 // Need to use low 4GB arenas for compiler or else the pointers wont fit in 32 bit method array
7804 // during cross compilation.
7805 // Use the linear alloc pool since this one is in the low 4gb for the compiler.
7806 ArenaStack stack_;
7807 ScopedArenaAllocator allocator_;
7808
7809 ScopedArenaVector<ArtMethod*> default_conflict_methods_;
7810 ScopedArenaVector<ArtMethod*> overriding_default_conflict_methods_;
7811 ScopedArenaVector<ArtMethod*> miranda_methods_;
7812 ScopedArenaVector<ArtMethod*> default_methods_;
7813 ScopedArenaVector<ArtMethod*> overriding_default_methods_;
7814
7815 ScopedArenaUnorderedMap<ArtMethod*, ArtMethod*> move_table_;
7816 };
7817
FindMethod(ArtMethod * interface_method,MethodNameAndSignatureComparator & interface_name_comparator,ArtMethod * vtable_impl)7818 ArtMethod* ClassLinker::LinkInterfaceMethodsHelper::FindMethod(
7819 ArtMethod* interface_method,
7820 MethodNameAndSignatureComparator& interface_name_comparator,
7821 ArtMethod* vtable_impl) {
7822 ArtMethod* current_method = nullptr;
7823 switch (class_linker_->FindDefaultMethodImplementation(self_,
7824 interface_method,
7825 klass_,
7826 /*out*/¤t_method)) {
7827 case DefaultMethodSearchResult::kDefaultConflict: {
7828 // Default method conflict.
7829 DCHECK(current_method == nullptr);
7830 ArtMethod* default_conflict_method = nullptr;
7831 if (vtable_impl != nullptr && vtable_impl->IsDefaultConflicting()) {
7832 // We can reuse the method from the superclass, don't bother adding it to virtuals.
7833 default_conflict_method = vtable_impl;
7834 } else {
7835 // See if we already have a conflict method for this method.
7836 ArtMethod* preexisting_conflict = FindSameNameAndSignature(
7837 interface_name_comparator,
7838 default_conflict_methods_,
7839 overriding_default_conflict_methods_);
7840 if (LIKELY(preexisting_conflict != nullptr)) {
7841 // We already have another conflict we can reuse.
7842 default_conflict_method = preexisting_conflict;
7843 } else {
7844 // Note that we do this even if we are an interface since we need to create this and
7845 // cannot reuse another classes.
7846 // Create a new conflict method for this to use.
7847 default_conflict_method = reinterpret_cast<ArtMethod*>(allocator_.Alloc(method_size_));
7848 new(default_conflict_method) ArtMethod(interface_method,
7849 class_linker_->GetImagePointerSize());
7850 if (vtable_impl == nullptr) {
7851 // Save the conflict method. We need to add it to the vtable.
7852 default_conflict_methods_.push_back(default_conflict_method);
7853 } else {
7854 // Save the conflict method but it is already in the vtable.
7855 overriding_default_conflict_methods_.push_back(default_conflict_method);
7856 }
7857 }
7858 }
7859 current_method = default_conflict_method;
7860 break;
7861 } // case kDefaultConflict
7862 case DefaultMethodSearchResult::kDefaultFound: {
7863 DCHECK(current_method != nullptr);
7864 // Found a default method.
7865 if (vtable_impl != nullptr &&
7866 current_method->GetDeclaringClass() == vtable_impl->GetDeclaringClass()) {
7867 // We found a default method but it was the same one we already have from our
7868 // superclass. Don't bother adding it to our vtable again.
7869 current_method = vtable_impl;
7870 } else if (LIKELY(FillTables())) {
7871 // Interfaces don't need to copy default methods since they don't have vtables.
7872 // Only record this default method if it is new to save space.
7873 // TODO It might be worthwhile to copy default methods on interfaces anyway since it
7874 // would make lookup for interface super much faster. (We would only need to scan
7875 // the iftable to find if there is a NSME or AME.)
7876 ArtMethod* old = FindSameNameAndSignature(interface_name_comparator,
7877 default_methods_,
7878 overriding_default_methods_);
7879 if (old == nullptr) {
7880 // We found a default method implementation and there were no conflicts.
7881 if (vtable_impl == nullptr) {
7882 // Save the default method. We need to add it to the vtable.
7883 default_methods_.push_back(current_method);
7884 } else {
7885 // Save the default method but it is already in the vtable.
7886 overriding_default_methods_.push_back(current_method);
7887 }
7888 } else {
7889 CHECK(old == current_method) << "Multiple default implementations selected!";
7890 }
7891 }
7892 break;
7893 } // case kDefaultFound
7894 case DefaultMethodSearchResult::kAbstractFound: {
7895 DCHECK(current_method == nullptr);
7896 // Abstract method masks all defaults.
7897 if (vtable_impl != nullptr &&
7898 vtable_impl->IsAbstract() &&
7899 !vtable_impl->IsDefaultConflicting()) {
7900 // We need to make this an abstract method but the version in the vtable already is so
7901 // don't do anything.
7902 current_method = vtable_impl;
7903 }
7904 break;
7905 } // case kAbstractFound
7906 }
7907 return current_method;
7908 }
7909
GetOrCreateMirandaMethod(ArtMethod * interface_method,MethodNameAndSignatureComparator & interface_name_comparator)7910 ArtMethod* ClassLinker::LinkInterfaceMethodsHelper::GetOrCreateMirandaMethod(
7911 ArtMethod* interface_method,
7912 MethodNameAndSignatureComparator& interface_name_comparator) {
7913 // Find out if there is already a miranda method we can use.
7914 ArtMethod* miranda_method = FindSameNameAndSignature(interface_name_comparator,
7915 miranda_methods_);
7916 if (miranda_method == nullptr) {
7917 DCHECK(interface_method->IsAbstract()) << interface_method->PrettyMethod();
7918 miranda_method = reinterpret_cast<ArtMethod*>(allocator_.Alloc(method_size_));
7919 CHECK(miranda_method != nullptr);
7920 // Point the interface table at a phantom slot.
7921 new(miranda_method) ArtMethod(interface_method, class_linker_->GetImagePointerSize());
7922 miranda_methods_.push_back(miranda_method);
7923 }
7924 return miranda_method;
7925 }
7926
ReallocMethods()7927 void ClassLinker::LinkInterfaceMethodsHelper::ReallocMethods() {
7928 LogNewVirtuals();
7929
7930 const size_t old_method_count = klass_->NumMethods();
7931 const size_t new_method_count = old_method_count + NumberOfNewVirtuals();
7932 DCHECK_NE(old_method_count, new_method_count);
7933
7934 // Attempt to realloc to save RAM if possible.
7935 LengthPrefixedArray<ArtMethod>* old_methods = klass_->GetMethodsPtr();
7936 // The Realloced virtual methods aren't visible from the class roots, so there is no issue
7937 // where GCs could attempt to mark stale pointers due to memcpy. And since we overwrite the
7938 // realloced memory with out->CopyFrom, we are guaranteed to have objects in the to space since
7939 // CopyFrom has internal read barriers.
7940 //
7941 // TODO We should maybe move some of this into mirror::Class or at least into another method.
7942 const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_method_count,
7943 method_size_,
7944 method_alignment_);
7945 const size_t new_size = LengthPrefixedArray<ArtMethod>::ComputeSize(new_method_count,
7946 method_size_,
7947 method_alignment_);
7948 const size_t old_methods_ptr_size = (old_methods != nullptr) ? old_size : 0;
7949 auto* methods = reinterpret_cast<LengthPrefixedArray<ArtMethod>*>(
7950 class_linker_->GetAllocatorForClassLoader(klass_->GetClassLoader())->Realloc(
7951 self_, old_methods, old_methods_ptr_size, new_size));
7952 CHECK(methods != nullptr); // Native allocation failure aborts.
7953
7954 PointerSize pointer_size = class_linker_->GetImagePointerSize();
7955 if (methods != old_methods) {
7956 // Maps from heap allocated miranda method to linear alloc miranda method.
7957 StrideIterator<ArtMethod> out = methods->begin(method_size_, method_alignment_);
7958 // Copy over the old methods.
7959 for (auto& m : klass_->GetMethods(pointer_size)) {
7960 move_table_.emplace(&m, &*out);
7961 // The CopyFrom is only necessary to not miss read barriers since Realloc won't do read
7962 // barriers when it copies.
7963 out->CopyFrom(&m, pointer_size);
7964 ++out;
7965 }
7966 }
7967 StrideIterator<ArtMethod> out(methods->begin(method_size_, method_alignment_) + old_method_count);
7968 // Copy over miranda methods before copying vtable since CopyOf may cause thread suspension and
7969 // we want the roots of the miranda methods to get visited.
7970 for (size_t i = 0; i < miranda_methods_.size(); ++i) {
7971 ArtMethod* mir_method = miranda_methods_[i];
7972 ArtMethod& new_method = *out;
7973 new_method.CopyFrom(mir_method, pointer_size);
7974 new_method.SetAccessFlags(new_method.GetAccessFlags() | kAccMiranda | kAccCopied);
7975 DCHECK_NE(new_method.GetAccessFlags() & kAccAbstract, 0u)
7976 << "Miranda method should be abstract!";
7977 move_table_.emplace(mir_method, &new_method);
7978 // Update the entry in the method array, as the array will be used for future lookups,
7979 // where thread suspension is allowed.
7980 // As such, the array should not contain locally allocated ArtMethod, otherwise the GC
7981 // would not see them.
7982 miranda_methods_[i] = &new_method;
7983 ++out;
7984 }
7985 // We need to copy the default methods into our own method table since the runtime requires that
7986 // every method on a class's vtable be in that respective class's virtual method table.
7987 // NOTE This means that two classes might have the same implementation of a method from the same
7988 // interface but will have different ArtMethod*s for them. This also means we cannot compare a
7989 // default method found on a class with one found on the declaring interface directly and must
7990 // look at the declaring class to determine if they are the same.
7991 for (ScopedArenaVector<ArtMethod*>* methods_vec : {&default_methods_,
7992 &overriding_default_methods_}) {
7993 for (size_t i = 0; i < methods_vec->size(); ++i) {
7994 ArtMethod* def_method = (*methods_vec)[i];
7995 ArtMethod& new_method = *out;
7996 new_method.CopyFrom(def_method, pointer_size);
7997 // Clear the kAccSkipAccessChecks flag if it is present. Since this class hasn't been
7998 // verified yet it shouldn't have methods that are skipping access checks.
7999 // TODO This is rather arbitrary. We should maybe support classes where only some of its
8000 // methods are skip_access_checks.
8001 DCHECK_EQ(new_method.GetAccessFlags() & kAccNative, 0u);
8002 constexpr uint32_t kSetFlags = kAccDefault | kAccCopied;
8003 constexpr uint32_t kMaskFlags = ~kAccSkipAccessChecks;
8004 new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags);
8005 move_table_.emplace(def_method, &new_method);
8006 // Update the entry in the method array, as the array will be used for future lookups,
8007 // where thread suspension is allowed.
8008 // As such, the array should not contain locally allocated ArtMethod, otherwise the GC
8009 // would not see them.
8010 (*methods_vec)[i] = &new_method;
8011 ++out;
8012 }
8013 }
8014 for (ScopedArenaVector<ArtMethod*>* methods_vec : {&default_conflict_methods_,
8015 &overriding_default_conflict_methods_}) {
8016 for (size_t i = 0; i < methods_vec->size(); ++i) {
8017 ArtMethod* conf_method = (*methods_vec)[i];
8018 ArtMethod& new_method = *out;
8019 new_method.CopyFrom(conf_method, pointer_size);
8020 // This is a type of default method (there are default method impls, just a conflict) so
8021 // mark this as a default, non-abstract method, since thats what it is. Also clear the
8022 // kAccSkipAccessChecks bit since this class hasn't been verified yet it shouldn't have
8023 // methods that are skipping access checks.
8024 // Also clear potential kAccSingleImplementation to avoid CHA trying to inline
8025 // the default method.
8026 DCHECK_EQ(new_method.GetAccessFlags() & kAccNative, 0u);
8027 constexpr uint32_t kSetFlags = kAccDefault | kAccDefaultConflict | kAccCopied;
8028 constexpr uint32_t kMaskFlags =
8029 ~(kAccAbstract | kAccSkipAccessChecks | kAccSingleImplementation);
8030 new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags);
8031 DCHECK(new_method.IsDefaultConflicting());
8032 // The actual method might or might not be marked abstract since we just copied it from a
8033 // (possibly default) interface method. We need to set it entry point to be the bridge so
8034 // that the compiler will not invoke the implementation of whatever method we copied from.
8035 EnsureThrowsInvocationError(class_linker_, &new_method);
8036 move_table_.emplace(conf_method, &new_method);
8037 // Update the entry in the method array, as the array will be used for future lookups,
8038 // where thread suspension is allowed.
8039 // As such, the array should not contain locally allocated ArtMethod, otherwise the GC
8040 // would not see them.
8041 (*methods_vec)[i] = &new_method;
8042 ++out;
8043 }
8044 }
8045 methods->SetSize(new_method_count);
8046 class_linker_->UpdateClassMethods(klass_.Get(), methods);
8047 }
8048
UpdateVtable(const std::unordered_map<size_t,ClassLinker::MethodTranslation> & default_translations,Handle<mirror::PointerArray> old_vtable)8049 ObjPtr<mirror::PointerArray> ClassLinker::LinkInterfaceMethodsHelper::UpdateVtable(
8050 const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations,
8051 Handle<mirror::PointerArray> old_vtable) {
8052 // Update the vtable to the new method structures. We can skip this for interfaces since they
8053 // do not have vtables.
8054 const size_t old_vtable_count = old_vtable->GetLength();
8055 const size_t new_vtable_count = old_vtable_count +
8056 miranda_methods_.size() +
8057 default_methods_.size() +
8058 default_conflict_methods_.size();
8059
8060 ObjPtr<mirror::PointerArray> vtable = ObjPtr<mirror::PointerArray>::DownCast(
8061 mirror::Array::CopyOf(old_vtable, self_, new_vtable_count));
8062 if (UNLIKELY(vtable == nullptr)) {
8063 self_->AssertPendingOOMException();
8064 return nullptr;
8065 }
8066
8067 size_t vtable_pos = old_vtable_count;
8068 PointerSize pointer_size = class_linker_->GetImagePointerSize();
8069 // Update all the newly copied method's indexes so they denote their placement in the vtable.
8070 for (const ScopedArenaVector<ArtMethod*>& methods_vec : {default_methods_,
8071 default_conflict_methods_,
8072 miranda_methods_}) {
8073 // These are the functions that are not already in the vtable!
8074 for (ArtMethod* new_vtable_method : methods_vec) {
8075 // Leave the declaring class alone the method's dex_code_item_offset_ and dex_method_index_
8076 // fields are references into the dex file the method was defined in. Since the ArtMethod
8077 // does not store that information it uses declaring_class_->dex_cache_.
8078 new_vtable_method->SetMethodIndex(0xFFFF & vtable_pos);
8079 vtable->SetElementPtrSize(vtable_pos, new_vtable_method, pointer_size);
8080 ++vtable_pos;
8081 }
8082 }
8083 DCHECK_EQ(vtable_pos, new_vtable_count);
8084
8085 // Update old vtable methods. We use the default_translations map to figure out what each
8086 // vtable entry should be updated to, if they need to be at all.
8087 for (size_t i = 0; i < old_vtable_count; ++i) {
8088 ArtMethod* translated_method = vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size);
8089 // Try and find what we need to change this method to.
8090 auto translation_it = default_translations.find(i);
8091 if (translation_it != default_translations.end()) {
8092 if (translation_it->second.IsInConflict()) {
8093 // Find which conflict method we are to use for this method.
8094 MethodNameAndSignatureComparator old_method_comparator(
8095 translated_method->GetInterfaceMethodIfProxy(pointer_size));
8096 // We only need to look through overriding_default_conflict_methods since this is an
8097 // overridden method we are fixing up here.
8098 ArtMethod* new_conflict_method = FindSameNameAndSignature(
8099 old_method_comparator, overriding_default_conflict_methods_);
8100 CHECK(new_conflict_method != nullptr) << "Expected a conflict method!";
8101 translated_method = new_conflict_method;
8102 } else if (translation_it->second.IsAbstract()) {
8103 // Find which miranda method we are to use for this method.
8104 MethodNameAndSignatureComparator old_method_comparator(
8105 translated_method->GetInterfaceMethodIfProxy(pointer_size));
8106 ArtMethod* miranda_method = FindSameNameAndSignature(old_method_comparator,
8107 miranda_methods_);
8108 DCHECK(miranda_method != nullptr);
8109 translated_method = miranda_method;
8110 } else {
8111 // Normal default method (changed from an older default or abstract interface method).
8112 DCHECK(translation_it->second.IsTranslation());
8113 translated_method = translation_it->second.GetTranslation();
8114 auto it = move_table_.find(translated_method);
8115 DCHECK(it != move_table_.end());
8116 translated_method = it->second;
8117 }
8118 } else {
8119 auto it = move_table_.find(translated_method);
8120 translated_method = (it != move_table_.end()) ? it->second : nullptr;
8121 }
8122
8123 if (translated_method != nullptr) {
8124 // Make sure the new_methods index is set.
8125 if (translated_method->GetMethodIndexDuringLinking() != i) {
8126 if (kIsDebugBuild) {
8127 auto* methods = klass_->GetMethodsPtr();
8128 CHECK_LE(reinterpret_cast<uintptr_t>(&*methods->begin(method_size_, method_alignment_)),
8129 reinterpret_cast<uintptr_t>(translated_method));
8130 CHECK_LT(reinterpret_cast<uintptr_t>(translated_method),
8131 reinterpret_cast<uintptr_t>(&*methods->end(method_size_, method_alignment_)));
8132 }
8133 translated_method->SetMethodIndex(0xFFFF & i);
8134 }
8135 vtable->SetElementPtrSize(i, translated_method, pointer_size);
8136 }
8137 }
8138 klass_->SetVTable(vtable);
8139 return vtable;
8140 }
8141
UpdateIfTable(Handle<mirror::IfTable> iftable)8142 void ClassLinker::LinkInterfaceMethodsHelper::UpdateIfTable(Handle<mirror::IfTable> iftable) {
8143 PointerSize pointer_size = class_linker_->GetImagePointerSize();
8144 const size_t ifcount = klass_->GetIfTableCount();
8145 // Go fix up all the stale iftable pointers.
8146 for (size_t i = 0; i < ifcount; ++i) {
8147 for (size_t j = 0, count = iftable->GetMethodArrayCount(i); j < count; ++j) {
8148 ObjPtr<mirror::PointerArray> method_array = iftable->GetMethodArray(i);
8149 ArtMethod* m = method_array->GetElementPtrSize<ArtMethod*>(j, pointer_size);
8150 DCHECK(m != nullptr) << klass_->PrettyClass();
8151 auto it = move_table_.find(m);
8152 if (it != move_table_.end()) {
8153 auto* new_m = it->second;
8154 DCHECK(new_m != nullptr) << klass_->PrettyClass();
8155 method_array->SetElementPtrSize(j, new_m, pointer_size);
8156 }
8157 }
8158 }
8159 }
8160
UpdateIMT(ArtMethod ** out_imt)8161 void ClassLinker::LinkInterfaceMethodsHelper::UpdateIMT(ArtMethod** out_imt) {
8162 // Fix up IMT next.
8163 for (size_t i = 0; i < ImTable::kSize; ++i) {
8164 auto it = move_table_.find(out_imt[i]);
8165 if (it != move_table_.end()) {
8166 out_imt[i] = it->second;
8167 }
8168 }
8169 }
8170
8171 // TODO This method needs to be split up into several smaller methods.
LinkInterfaceMethods(Thread * self,Handle<mirror::Class> klass,const std::unordered_map<size_t,ClassLinker::MethodTranslation> & default_translations,bool * out_new_conflict,ArtMethod ** out_imt)8172 bool ClassLinker::LinkInterfaceMethods(
8173 Thread* self,
8174 Handle<mirror::Class> klass,
8175 const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations,
8176 bool* out_new_conflict,
8177 ArtMethod** out_imt) {
8178 StackHandleScope<3> hs(self);
8179 Runtime* const runtime = Runtime::Current();
8180
8181 const bool is_interface = klass->IsInterface();
8182 const bool has_superclass = klass->HasSuperClass();
8183 const bool fill_tables = !is_interface;
8184 const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U;
8185 const size_t ifcount = klass->GetIfTableCount();
8186
8187 Handle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable()));
8188
8189 MutableHandle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking()));
8190 ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod();
8191 ArtMethod* const imt_conflict_method = runtime->GetImtConflictMethod();
8192 // Copy the IMT from the super class if possible.
8193 const bool extend_super_iftable = has_superclass;
8194 if (has_superclass && fill_tables) {
8195 FillImtFromSuperClass(klass,
8196 unimplemented_method,
8197 imt_conflict_method,
8198 out_new_conflict,
8199 out_imt);
8200 }
8201 // Allocate method arrays before since we don't want miss visiting miranda method roots due to
8202 // thread suspension.
8203 if (fill_tables) {
8204 if (!AllocateIfTableMethodArrays(self, klass, iftable)) {
8205 return false;
8206 }
8207 }
8208
8209 LinkInterfaceMethodsHelper helper(this, klass, self, runtime);
8210
8211 auto* old_cause = self->StartAssertNoThreadSuspension(
8212 "Copying ArtMethods for LinkInterfaceMethods");
8213 // Going in reverse to ensure that we will hit abstract methods that override defaults before the
8214 // defaults. This means we don't need to do any trickery when creating the Miranda methods, since
8215 // they will already be null. This has the additional benefit that the declarer of a miranda
8216 // method will actually declare an abstract method.
8217 for (size_t i = ifcount; i != 0u; ) {
8218 --i;
8219 DCHECK_LT(i, ifcount);
8220
8221 size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods();
8222 if (num_methods > 0) {
8223 StackHandleScope<2> hs2(self);
8224 const bool is_super = i < super_ifcount;
8225 const bool super_interface = is_super && extend_super_iftable;
8226 // We don't actually create or fill these tables for interfaces, we just copy some methods for
8227 // conflict methods. Just set this as nullptr in those cases.
8228 Handle<mirror::PointerArray> method_array(fill_tables
8229 ? hs2.NewHandle(iftable->GetMethodArray(i))
8230 : hs2.NewHandle<mirror::PointerArray>(nullptr));
8231
8232 ArraySlice<ArtMethod> input_virtual_methods;
8233 ScopedNullHandle<mirror::PointerArray> null_handle;
8234 Handle<mirror::PointerArray> input_vtable_array(null_handle);
8235 int32_t input_array_length = 0;
8236
8237 // TODO Cleanup Needed: In the presence of default methods this optimization is rather dirty
8238 // and confusing. Default methods should always look through all the superclasses
8239 // because they are the last choice of an implementation. We get around this by looking
8240 // at the super-classes iftable methods (copied into method_array previously) when we are
8241 // looking for the implementation of a super-interface method but that is rather dirty.
8242 bool using_virtuals;
8243 if (super_interface || is_interface) {
8244 // If we are overwriting a super class interface, try to only virtual methods instead of the
8245 // whole vtable.
8246 using_virtuals = true;
8247 input_virtual_methods = klass->GetDeclaredVirtualMethodsSlice(image_pointer_size_);
8248 input_array_length = input_virtual_methods.size();
8249 } else {
8250 // For a new interface, however, we need the whole vtable in case a new
8251 // interface method is implemented in the whole superclass.
8252 using_virtuals = false;
8253 DCHECK(vtable != nullptr);
8254 input_vtable_array = vtable;
8255 input_array_length = input_vtable_array->GetLength();
8256 }
8257
8258 // For each method in interface
8259 for (size_t j = 0; j < num_methods; ++j) {
8260 auto* interface_method = iftable->GetInterface(i)->GetVirtualMethod(j, image_pointer_size_);
8261 MethodNameAndSignatureComparator interface_name_comparator(
8262 interface_method->GetInterfaceMethodIfProxy(image_pointer_size_));
8263 uint32_t imt_index = interface_method->GetImtIndex();
8264 ArtMethod** imt_ptr = &out_imt[imt_index];
8265 // For each method listed in the interface's method list, find the
8266 // matching method in our class's method list. We want to favor the
8267 // subclass over the superclass, which just requires walking
8268 // back from the end of the vtable. (This only matters if the
8269 // superclass defines a private method and this class redefines
8270 // it -- otherwise it would use the same vtable slot. In .dex files
8271 // those don't end up in the virtual method table, so it shouldn't
8272 // matter which direction we go. We walk it backward anyway.)
8273 //
8274 // To find defaults we need to do the same but also go over interfaces.
8275 bool found_impl = false;
8276 ArtMethod* vtable_impl = nullptr;
8277 for (int32_t k = input_array_length - 1; k >= 0; --k) {
8278 ArtMethod* vtable_method = using_virtuals ?
8279 &input_virtual_methods[k] :
8280 input_vtable_array->GetElementPtrSize<ArtMethod*>(k, image_pointer_size_);
8281 ArtMethod* vtable_method_for_name_comparison =
8282 vtable_method->GetInterfaceMethodIfProxy(image_pointer_size_);
8283 DCHECK(!vtable_method->IsStatic()) << vtable_method->PrettyMethod();
8284 if (interface_name_comparator.HasSameNameAndSignature(
8285 vtable_method_for_name_comparison)) {
8286 if (!vtable_method->IsAbstract() && !vtable_method->IsPublic()) {
8287 // Must do EndAssertNoThreadSuspension before throw since the throw can cause
8288 // allocations.
8289 self->EndAssertNoThreadSuspension(old_cause);
8290 ThrowIllegalAccessError(klass.Get(),
8291 "Method '%s' implementing interface method '%s' is not public",
8292 vtable_method->PrettyMethod().c_str(),
8293 interface_method->PrettyMethod().c_str());
8294 return false;
8295 } else if (UNLIKELY(vtable_method->IsOverridableByDefaultMethod())) {
8296 // We might have a newer, better, default method for this, so we just skip it. If we
8297 // are still using this we will select it again when scanning for default methods. To
8298 // obviate the need to copy the method again we will make a note that we already found
8299 // a default here.
8300 // TODO This should be much cleaner.
8301 vtable_impl = vtable_method;
8302 break;
8303 } else {
8304 found_impl = true;
8305 if (LIKELY(fill_tables)) {
8306 method_array->SetElementPtrSize(j, vtable_method, image_pointer_size_);
8307 // Place method in imt if entry is empty, place conflict otherwise.
8308 SetIMTRef(unimplemented_method,
8309 imt_conflict_method,
8310 vtable_method,
8311 /*out*/out_new_conflict,
8312 /*out*/imt_ptr);
8313 }
8314 break;
8315 }
8316 }
8317 }
8318 // Continue on to the next method if we are done.
8319 if (LIKELY(found_impl)) {
8320 continue;
8321 } else if (LIKELY(super_interface)) {
8322 // Don't look for a default implementation when the super-method is implemented directly
8323 // by the class.
8324 //
8325 // See if we can use the superclasses method and skip searching everything else.
8326 // Note: !found_impl && super_interface
8327 CHECK(extend_super_iftable);
8328 // If this is a super_interface method it is possible we shouldn't override it because a
8329 // superclass could have implemented it directly. We get the method the superclass used
8330 // to implement this to know if we can override it with a default method. Doing this is
8331 // safe since we know that the super_iftable is filled in so we can simply pull it from
8332 // there. We don't bother if this is not a super-classes interface since in that case we
8333 // have scanned the entire vtable anyway and would have found it.
8334 // TODO This is rather dirty but it is faster than searching through the entire vtable
8335 // every time.
8336 ArtMethod* supers_method =
8337 method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_);
8338 DCHECK(supers_method != nullptr);
8339 DCHECK(interface_name_comparator.HasSameNameAndSignature(supers_method));
8340 if (LIKELY(!supers_method->IsOverridableByDefaultMethod())) {
8341 // The method is not overridable by a default method (i.e. it is directly implemented
8342 // in some class). Therefore move onto the next interface method.
8343 continue;
8344 } else {
8345 // If the super-classes method is override-able by a default method we need to keep
8346 // track of it since though it is override-able it is not guaranteed to be 'overridden'.
8347 // If it turns out not to be overridden and we did not keep track of it we might add it
8348 // to the vtable twice, causing corruption (vtable entries having inconsistent and
8349 // illegal states, incorrect vtable size, and incorrect or inconsistent iftable entries)
8350 // in this class and any subclasses.
8351 DCHECK(vtable_impl == nullptr || vtable_impl == supers_method)
8352 << "vtable_impl was " << ArtMethod::PrettyMethod(vtable_impl)
8353 << " and not 'nullptr' or "
8354 << supers_method->PrettyMethod()
8355 << " as expected. IFTable appears to be corrupt!";
8356 vtable_impl = supers_method;
8357 }
8358 }
8359 // If we haven't found it yet we should search through the interfaces for default methods.
8360 ArtMethod* current_method = helper.FindMethod(interface_method,
8361 interface_name_comparator,
8362 vtable_impl);
8363 if (LIKELY(fill_tables)) {
8364 if (current_method == nullptr && !super_interface) {
8365 // We could not find an implementation for this method and since it is a brand new
8366 // interface we searched the entire vtable (and all default methods) for an
8367 // implementation but couldn't find one. We therefore need to make a miranda method.
8368 current_method = helper.GetOrCreateMirandaMethod(interface_method,
8369 interface_name_comparator);
8370 }
8371
8372 if (current_method != nullptr) {
8373 // We found a default method implementation. Record it in the iftable and IMT.
8374 method_array->SetElementPtrSize(j, current_method, image_pointer_size_);
8375 SetIMTRef(unimplemented_method,
8376 imt_conflict_method,
8377 current_method,
8378 /*out*/out_new_conflict,
8379 /*out*/imt_ptr);
8380 }
8381 }
8382 } // For each method in interface end.
8383 } // if (num_methods > 0)
8384 } // For each interface.
8385 // TODO don't extend virtuals of interface unless necessary (when is it?).
8386 if (helper.HasNewVirtuals()) {
8387 LengthPrefixedArray<ArtMethod>* old_methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr;
8388 helper.ReallocMethods(); // No return value to check. Native allocation failure aborts.
8389 LengthPrefixedArray<ArtMethod>* methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr;
8390
8391 // Done copying methods, they are all roots in the class now, so we can end the no thread
8392 // suspension assert.
8393 self->EndAssertNoThreadSuspension(old_cause);
8394
8395 if (fill_tables) {
8396 vtable.Assign(helper.UpdateVtable(default_translations, vtable));
8397 if (UNLIKELY(vtable == nullptr)) {
8398 // The helper has already called self->AssertPendingOOMException();
8399 return false;
8400 }
8401 helper.UpdateIfTable(iftable);
8402 helper.UpdateIMT(out_imt);
8403 }
8404
8405 helper.CheckNoStaleMethodsInDexCache();
8406 helper.ClobberOldMethods(old_methods, methods);
8407 } else {
8408 self->EndAssertNoThreadSuspension(old_cause);
8409 }
8410 if (kIsDebugBuild && !is_interface) {
8411 CheckVTable(self, klass, image_pointer_size_);
8412 }
8413 return true;
8414 }
8415
LinkInstanceFields(Thread * self,Handle<mirror::Class> klass)8416 bool ClassLinker::LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) {
8417 CHECK(klass != nullptr);
8418 return LinkFields(self, klass, false, nullptr);
8419 }
8420
LinkStaticFields(Thread * self,Handle<mirror::Class> klass,size_t * class_size)8421 bool ClassLinker::LinkStaticFields(Thread* self, Handle<mirror::Class> klass, size_t* class_size) {
8422 CHECK(klass != nullptr);
8423 return LinkFields(self, klass, true, class_size);
8424 }
8425
8426 struct LinkFieldsComparator {
REQUIRES_SHAREDart::LinkFieldsComparator8427 LinkFieldsComparator() REQUIRES_SHARED(Locks::mutator_lock_) {
8428 }
8429 // No thread safety analysis as will be called from STL. Checked lock held in constructor.
operator ()art::LinkFieldsComparator8430 bool operator()(ArtField* field1, ArtField* field2)
8431 NO_THREAD_SAFETY_ANALYSIS {
8432 // First come reference fields, then 64-bit, then 32-bit, and then 16-bit, then finally 8-bit.
8433 Primitive::Type type1 = field1->GetTypeAsPrimitiveType();
8434 Primitive::Type type2 = field2->GetTypeAsPrimitiveType();
8435 if (type1 != type2) {
8436 if (type1 == Primitive::kPrimNot) {
8437 // Reference always goes first.
8438 return true;
8439 }
8440 if (type2 == Primitive::kPrimNot) {
8441 // Reference always goes first.
8442 return false;
8443 }
8444 size_t size1 = Primitive::ComponentSize(type1);
8445 size_t size2 = Primitive::ComponentSize(type2);
8446 if (size1 != size2) {
8447 // Larger primitive types go first.
8448 return size1 > size2;
8449 }
8450 // Primitive types differ but sizes match. Arbitrarily order by primitive type.
8451 return type1 < type2;
8452 }
8453 // Same basic group? Then sort by dex field index. This is guaranteed to be sorted
8454 // by name and for equal names by type id index.
8455 // NOTE: This works also for proxies. Their static fields are assigned appropriate indexes.
8456 return field1->GetDexFieldIndex() < field2->GetDexFieldIndex();
8457 }
8458 };
8459
LinkFields(Thread * self,Handle<mirror::Class> klass,bool is_static,size_t * class_size)8460 bool ClassLinker::LinkFields(Thread* self,
8461 Handle<mirror::Class> klass,
8462 bool is_static,
8463 size_t* class_size) {
8464 self->AllowThreadSuspension();
8465 const size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
8466 LengthPrefixedArray<ArtField>* const fields = is_static ? klass->GetSFieldsPtr() :
8467 klass->GetIFieldsPtr();
8468
8469 // Initialize field_offset
8470 MemberOffset field_offset(0);
8471 if (is_static) {
8472 field_offset = klass->GetFirstReferenceStaticFieldOffsetDuringLinking(image_pointer_size_);
8473 } else {
8474 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
8475 if (super_class != nullptr) {
8476 CHECK(super_class->IsResolved())
8477 << klass->PrettyClass() << " " << super_class->PrettyClass();
8478 field_offset = MemberOffset(super_class->GetObjectSize());
8479 }
8480 }
8481
8482 CHECK_EQ(num_fields == 0, fields == nullptr) << klass->PrettyClass();
8483
8484 // we want a relatively stable order so that adding new fields
8485 // minimizes disruption of C++ version such as Class and Method.
8486 //
8487 // The overall sort order order is:
8488 // 1) All object reference fields, sorted alphabetically.
8489 // 2) All java long (64-bit) integer fields, sorted alphabetically.
8490 // 3) All java double (64-bit) floating point fields, sorted alphabetically.
8491 // 4) All java int (32-bit) integer fields, sorted alphabetically.
8492 // 5) All java float (32-bit) floating point fields, sorted alphabetically.
8493 // 6) All java char (16-bit) integer fields, sorted alphabetically.
8494 // 7) All java short (16-bit) integer fields, sorted alphabetically.
8495 // 8) All java boolean (8-bit) integer fields, sorted alphabetically.
8496 // 9) All java byte (8-bit) integer fields, sorted alphabetically.
8497 //
8498 // Once the fields are sorted in this order we will attempt to fill any gaps that might be present
8499 // in the memory layout of the structure. See ShuffleForward for how this is done.
8500 std::deque<ArtField*> grouped_and_sorted_fields;
8501 const char* old_no_suspend_cause = self->StartAssertNoThreadSuspension(
8502 "Naked ArtField references in deque");
8503 for (size_t i = 0; i < num_fields; i++) {
8504 grouped_and_sorted_fields.push_back(&fields->At(i));
8505 }
8506 std::sort(grouped_and_sorted_fields.begin(), grouped_and_sorted_fields.end(),
8507 LinkFieldsComparator());
8508
8509 // References should be at the front.
8510 size_t current_field = 0;
8511 size_t num_reference_fields = 0;
8512 FieldGaps gaps;
8513
8514 for (; current_field < num_fields; current_field++) {
8515 ArtField* field = grouped_and_sorted_fields.front();
8516 Primitive::Type type = field->GetTypeAsPrimitiveType();
8517 bool isPrimitive = type != Primitive::kPrimNot;
8518 if (isPrimitive) {
8519 break; // past last reference, move on to the next phase
8520 }
8521 if (UNLIKELY(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(
8522 field_offset.Uint32Value()))) {
8523 MemberOffset old_offset = field_offset;
8524 field_offset = MemberOffset(RoundUp(field_offset.Uint32Value(), 4));
8525 AddFieldGap(old_offset.Uint32Value(), field_offset.Uint32Value(), &gaps);
8526 }
8527 DCHECK_ALIGNED(field_offset.Uint32Value(), sizeof(mirror::HeapReference<mirror::Object>));
8528 grouped_and_sorted_fields.pop_front();
8529 num_reference_fields++;
8530 field->SetOffset(field_offset);
8531 field_offset = MemberOffset(field_offset.Uint32Value() +
8532 sizeof(mirror::HeapReference<mirror::Object>));
8533 }
8534 // Gaps are stored as a max heap which means that we must shuffle from largest to smallest
8535 // otherwise we could end up with suboptimal gap fills.
8536 ShuffleForward<8>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps);
8537 ShuffleForward<4>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps);
8538 ShuffleForward<2>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps);
8539 ShuffleForward<1>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps);
8540 CHECK(grouped_and_sorted_fields.empty()) << "Missed " << grouped_and_sorted_fields.size() <<
8541 " fields.";
8542 self->EndAssertNoThreadSuspension(old_no_suspend_cause);
8543
8544 // We lie to the GC about the java.lang.ref.Reference.referent field, so it doesn't scan it.
8545 if (!is_static && klass->DescriptorEquals("Ljava/lang/ref/Reference;")) {
8546 // We know there are no non-reference fields in the Reference classes, and we know
8547 // that 'referent' is alphabetically last, so this is easy...
8548 CHECK_EQ(num_reference_fields, num_fields) << klass->PrettyClass();
8549 CHECK_STREQ(fields->At(num_fields - 1).GetName(), "referent")
8550 << klass->PrettyClass();
8551 --num_reference_fields;
8552 }
8553
8554 size_t size = field_offset.Uint32Value();
8555 // Update klass
8556 if (is_static) {
8557 klass->SetNumReferenceStaticFields(num_reference_fields);
8558 *class_size = size;
8559 } else {
8560 klass->SetNumReferenceInstanceFields(num_reference_fields);
8561 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
8562 if (num_reference_fields == 0 || super_class == nullptr) {
8563 // object has one reference field, klass, but we ignore it since we always visit the class.
8564 // super_class is null iff the class is java.lang.Object.
8565 if (super_class == nullptr ||
8566 (super_class->GetClassFlags() & mirror::kClassFlagNoReferenceFields) != 0) {
8567 klass->SetClassFlags(klass->GetClassFlags() | mirror::kClassFlagNoReferenceFields);
8568 }
8569 }
8570 if (kIsDebugBuild) {
8571 DCHECK_EQ(super_class == nullptr, klass->DescriptorEquals("Ljava/lang/Object;"));
8572 size_t total_reference_instance_fields = 0;
8573 ObjPtr<mirror::Class> cur_super = klass.Get();
8574 while (cur_super != nullptr) {
8575 total_reference_instance_fields += cur_super->NumReferenceInstanceFieldsDuringLinking();
8576 cur_super = cur_super->GetSuperClass();
8577 }
8578 if (super_class == nullptr) {
8579 CHECK_EQ(total_reference_instance_fields, 1u) << klass->PrettyDescriptor();
8580 } else {
8581 // Check that there is at least num_reference_fields other than Object.class.
8582 CHECK_GE(total_reference_instance_fields, 1u + num_reference_fields)
8583 << klass->PrettyClass();
8584 }
8585 }
8586 if (!klass->IsVariableSize()) {
8587 std::string temp;
8588 DCHECK_GE(size, sizeof(mirror::Object)) << klass->GetDescriptor(&temp);
8589 size_t previous_size = klass->GetObjectSize();
8590 if (previous_size != 0) {
8591 // Make sure that we didn't originally have an incorrect size.
8592 CHECK_EQ(previous_size, size) << klass->GetDescriptor(&temp);
8593 }
8594 klass->SetObjectSize(size);
8595 }
8596 }
8597
8598 if (kIsDebugBuild) {
8599 // Make sure that the fields array is ordered by name but all reference
8600 // offsets are at the beginning as far as alignment allows.
8601 MemberOffset start_ref_offset = is_static
8602 ? klass->GetFirstReferenceStaticFieldOffsetDuringLinking(image_pointer_size_)
8603 : klass->GetFirstReferenceInstanceFieldOffset();
8604 MemberOffset end_ref_offset(start_ref_offset.Uint32Value() +
8605 num_reference_fields *
8606 sizeof(mirror::HeapReference<mirror::Object>));
8607 MemberOffset current_ref_offset = start_ref_offset;
8608 for (size_t i = 0; i < num_fields; i++) {
8609 ArtField* field = &fields->At(i);
8610 VLOG(class_linker) << "LinkFields: " << (is_static ? "static" : "instance")
8611 << " class=" << klass->PrettyClass() << " field=" << field->PrettyField()
8612 << " offset=" << field->GetOffsetDuringLinking();
8613 if (i != 0) {
8614 ArtField* const prev_field = &fields->At(i - 1);
8615 // NOTE: The field names can be the same. This is not possible in the Java language
8616 // but it's valid Java/dex bytecode and for example proguard can generate such bytecode.
8617 DCHECK_LE(strcmp(prev_field->GetName(), field->GetName()), 0);
8618 }
8619 Primitive::Type type = field->GetTypeAsPrimitiveType();
8620 bool is_primitive = type != Primitive::kPrimNot;
8621 if (klass->DescriptorEquals("Ljava/lang/ref/Reference;") &&
8622 strcmp("referent", field->GetName()) == 0) {
8623 is_primitive = true; // We lied above, so we have to expect a lie here.
8624 }
8625 MemberOffset offset = field->GetOffsetDuringLinking();
8626 if (is_primitive) {
8627 if (offset.Uint32Value() < end_ref_offset.Uint32Value()) {
8628 // Shuffled before references.
8629 size_t type_size = Primitive::ComponentSize(type);
8630 CHECK_LT(type_size, sizeof(mirror::HeapReference<mirror::Object>));
8631 CHECK_LT(offset.Uint32Value(), start_ref_offset.Uint32Value());
8632 CHECK_LE(offset.Uint32Value() + type_size, start_ref_offset.Uint32Value());
8633 CHECK(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(offset.Uint32Value()));
8634 }
8635 } else {
8636 CHECK_EQ(current_ref_offset.Uint32Value(), offset.Uint32Value());
8637 current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() +
8638 sizeof(mirror::HeapReference<mirror::Object>));
8639 }
8640 }
8641 CHECK_EQ(current_ref_offset.Uint32Value(), end_ref_offset.Uint32Value());
8642 }
8643 return true;
8644 }
8645
8646 // Set the bitmap of reference instance field offsets.
CreateReferenceInstanceOffsets(Handle<mirror::Class> klass)8647 void ClassLinker::CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) {
8648 uint32_t reference_offsets = 0;
8649 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
8650 // Leave the reference offsets as 0 for mirror::Object (the class field is handled specially).
8651 if (super_class != nullptr) {
8652 reference_offsets = super_class->GetReferenceInstanceOffsets();
8653 // Compute reference offsets unless our superclass overflowed.
8654 if (reference_offsets != mirror::Class::kClassWalkSuper) {
8655 size_t num_reference_fields = klass->NumReferenceInstanceFieldsDuringLinking();
8656 if (num_reference_fields != 0u) {
8657 // All of the fields that contain object references are guaranteed be grouped in memory
8658 // starting at an appropriately aligned address after super class object data.
8659 uint32_t start_offset = RoundUp(super_class->GetObjectSize(),
8660 sizeof(mirror::HeapReference<mirror::Object>));
8661 uint32_t start_bit = (start_offset - mirror::kObjectHeaderSize) /
8662 sizeof(mirror::HeapReference<mirror::Object>);
8663 if (start_bit + num_reference_fields > 32) {
8664 reference_offsets = mirror::Class::kClassWalkSuper;
8665 } else {
8666 reference_offsets |= (0xffffffffu << start_bit) &
8667 (0xffffffffu >> (32 - (start_bit + num_reference_fields)));
8668 }
8669 }
8670 }
8671 }
8672 klass->SetReferenceInstanceOffsets(reference_offsets);
8673 }
8674
DoResolveString(dex::StringIndex string_idx,ObjPtr<mirror::DexCache> dex_cache)8675 ObjPtr<mirror::String> ClassLinker::DoResolveString(dex::StringIndex string_idx,
8676 ObjPtr<mirror::DexCache> dex_cache) {
8677 StackHandleScope<1> hs(Thread::Current());
8678 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(dex_cache));
8679 return DoResolveString(string_idx, h_dex_cache);
8680 }
8681
DoResolveString(dex::StringIndex string_idx,Handle<mirror::DexCache> dex_cache)8682 ObjPtr<mirror::String> ClassLinker::DoResolveString(dex::StringIndex string_idx,
8683 Handle<mirror::DexCache> dex_cache) {
8684 const DexFile& dex_file = *dex_cache->GetDexFile();
8685 uint32_t utf16_length;
8686 const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length);
8687 ObjPtr<mirror::String> string = intern_table_->InternStrong(utf16_length, utf8_data);
8688 if (string != nullptr) {
8689 dex_cache->SetResolvedString(string_idx, string);
8690 }
8691 return string;
8692 }
8693
DoLookupString(dex::StringIndex string_idx,ObjPtr<mirror::DexCache> dex_cache)8694 ObjPtr<mirror::String> ClassLinker::DoLookupString(dex::StringIndex string_idx,
8695 ObjPtr<mirror::DexCache> dex_cache) {
8696 DCHECK(dex_cache != nullptr);
8697 const DexFile& dex_file = *dex_cache->GetDexFile();
8698 uint32_t utf16_length;
8699 const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length);
8700 ObjPtr<mirror::String> string =
8701 intern_table_->LookupStrong(Thread::Current(), utf16_length, utf8_data);
8702 if (string != nullptr) {
8703 dex_cache->SetResolvedString(string_idx, string);
8704 }
8705 return string;
8706 }
8707
DoLookupResolvedType(dex::TypeIndex type_idx,ObjPtr<mirror::Class> referrer)8708 ObjPtr<mirror::Class> ClassLinker::DoLookupResolvedType(dex::TypeIndex type_idx,
8709 ObjPtr<mirror::Class> referrer) {
8710 return DoLookupResolvedType(type_idx, referrer->GetDexCache(), referrer->GetClassLoader());
8711 }
8712
DoLookupResolvedType(dex::TypeIndex type_idx,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader)8713 ObjPtr<mirror::Class> ClassLinker::DoLookupResolvedType(dex::TypeIndex type_idx,
8714 ObjPtr<mirror::DexCache> dex_cache,
8715 ObjPtr<mirror::ClassLoader> class_loader) {
8716 const DexFile& dex_file = *dex_cache->GetDexFile();
8717 const char* descriptor = dex_file.StringByTypeIdx(type_idx);
8718 DCHECK_NE(*descriptor, '\0') << "descriptor is empty string";
8719 ObjPtr<mirror::Class> type = nullptr;
8720 if (descriptor[1] == '\0') {
8721 // only the descriptors of primitive types should be 1 character long, also avoid class lookup
8722 // for primitive classes that aren't backed by dex files.
8723 type = LookupPrimitiveClass(descriptor[0]);
8724 } else {
8725 Thread* const self = Thread::Current();
8726 DCHECK(self != nullptr);
8727 const size_t hash = ComputeModifiedUtf8Hash(descriptor);
8728 // Find the class in the loaded classes table.
8729 type = LookupClass(self, descriptor, hash, class_loader);
8730 }
8731 if (type != nullptr) {
8732 if (type->IsResolved()) {
8733 dex_cache->SetResolvedType(type_idx, type);
8734 } else {
8735 type = nullptr;
8736 }
8737 }
8738 return type;
8739 }
8740
8741 template <typename RefType>
DoResolveType(dex::TypeIndex type_idx,RefType referrer)8742 ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, RefType referrer) {
8743 StackHandleScope<2> hs(Thread::Current());
8744 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
8745 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader()));
8746 return DoResolveType(type_idx, dex_cache, class_loader);
8747 }
8748
8749 // Instantiate the above.
8750 template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx,
8751 ArtField* referrer);
8752 template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx,
8753 ArtMethod* referrer);
8754 template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx,
8755 ObjPtr<mirror::Class> referrer);
8756
DoResolveType(dex::TypeIndex type_idx,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader)8757 ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx,
8758 Handle<mirror::DexCache> dex_cache,
8759 Handle<mirror::ClassLoader> class_loader) {
8760 Thread* self = Thread::Current();
8761 const char* descriptor = dex_cache->GetDexFile()->StringByTypeIdx(type_idx);
8762 ObjPtr<mirror::Class> resolved = FindClass(self, descriptor, class_loader);
8763 if (resolved != nullptr) {
8764 // TODO: we used to throw here if resolved's class loader was not the
8765 // boot class loader. This was to permit different classes with the
8766 // same name to be loaded simultaneously by different loaders
8767 dex_cache->SetResolvedType(type_idx, resolved);
8768 } else {
8769 CHECK(self->IsExceptionPending())
8770 << "Expected pending exception for failed resolution of: " << descriptor;
8771 // Convert a ClassNotFoundException to a NoClassDefFoundError.
8772 StackHandleScope<1> hs(self);
8773 Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException()));
8774 if (cause->InstanceOf(GetClassRoot(ClassRoot::kJavaLangClassNotFoundException, this))) {
8775 DCHECK(resolved == nullptr); // No Handle needed to preserve resolved.
8776 self->ClearException();
8777 ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor);
8778 self->GetException()->SetCause(cause.Get());
8779 }
8780 }
8781 DCHECK((resolved == nullptr) || resolved->IsResolved())
8782 << resolved->PrettyDescriptor() << " " << resolved->GetStatus();
8783 return resolved;
8784 }
8785
FindResolvedMethod(ObjPtr<mirror::Class> klass,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader,uint32_t method_idx)8786 ArtMethod* ClassLinker::FindResolvedMethod(ObjPtr<mirror::Class> klass,
8787 ObjPtr<mirror::DexCache> dex_cache,
8788 ObjPtr<mirror::ClassLoader> class_loader,
8789 uint32_t method_idx) {
8790 // Search for the method using dex_cache and method_idx. The Class::Find*Method()
8791 // functions can optimize the search if the dex_cache is the same as the DexCache
8792 // of the class, with fall-back to name and signature search otherwise.
8793 ArtMethod* resolved = nullptr;
8794 if (klass->IsInterface()) {
8795 resolved = klass->FindInterfaceMethod(dex_cache, method_idx, image_pointer_size_);
8796 } else {
8797 resolved = klass->FindClassMethod(dex_cache, method_idx, image_pointer_size_);
8798 }
8799 DCHECK(resolved == nullptr || resolved->GetDeclaringClassUnchecked() != nullptr);
8800 if (resolved != nullptr &&
8801 hiddenapi::ShouldDenyAccessToMember(resolved,
8802 hiddenapi::AccessContext(class_loader, dex_cache),
8803 hiddenapi::AccessMethod::kLinking)) {
8804 resolved = nullptr;
8805 }
8806 if (resolved != nullptr) {
8807 // In case of jmvti, the dex file gets verified before being registered, so first
8808 // check if it's registered before checking class tables.
8809 const DexFile& dex_file = *dex_cache->GetDexFile();
8810 DCHECK(!IsDexFileRegistered(Thread::Current(), dex_file) ||
8811 FindClassTable(Thread::Current(), dex_cache) == ClassTableForClassLoader(class_loader))
8812 << "DexFile referrer: " << dex_file.GetLocation()
8813 << " ClassLoader: " << DescribeLoaders(class_loader, "");
8814 // Be a good citizen and update the dex cache to speed subsequent calls.
8815 dex_cache->SetResolvedMethod(method_idx, resolved, image_pointer_size_);
8816 // Disable the following invariant check as the verifier breaks it. b/73760543
8817 // const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
8818 // DCHECK(LookupResolvedType(method_id.class_idx_, dex_cache, class_loader) != nullptr)
8819 // << "Method: " << resolved->PrettyMethod() << ", "
8820 // << "Class: " << klass->PrettyClass() << " (" << klass->GetStatus() << "), "
8821 // << "DexFile referrer: " << dex_file.GetLocation();
8822 }
8823 return resolved;
8824 }
8825
8826 // Returns true if `method` is either null or hidden.
8827 // Does not print any warnings if it is hidden.
CheckNoSuchMethod(ArtMethod * method,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader)8828 static bool CheckNoSuchMethod(ArtMethod* method,
8829 ObjPtr<mirror::DexCache> dex_cache,
8830 ObjPtr<mirror::ClassLoader> class_loader)
8831 REQUIRES_SHARED(Locks::mutator_lock_) {
8832 return method == nullptr ||
8833 hiddenapi::ShouldDenyAccessToMember(method,
8834 hiddenapi::AccessContext(class_loader, dex_cache),
8835 hiddenapi::AccessMethod::kNone); // no warnings
8836 }
8837
FindIncompatibleMethod(ObjPtr<mirror::Class> klass,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader,uint32_t method_idx)8838 ArtMethod* ClassLinker::FindIncompatibleMethod(ObjPtr<mirror::Class> klass,
8839 ObjPtr<mirror::DexCache> dex_cache,
8840 ObjPtr<mirror::ClassLoader> class_loader,
8841 uint32_t method_idx) {
8842 if (klass->IsInterface()) {
8843 ArtMethod* method = klass->FindClassMethod(dex_cache, method_idx, image_pointer_size_);
8844 return CheckNoSuchMethod(method, dex_cache, class_loader) ? nullptr : method;
8845 } else {
8846 // If there was an interface method with the same signature, we would have
8847 // found it in the "copied" methods. Only DCHECK that the interface method
8848 // really does not exist.
8849 if (kIsDebugBuild) {
8850 ArtMethod* method =
8851 klass->FindInterfaceMethod(dex_cache, method_idx, image_pointer_size_);
8852 DCHECK(CheckNoSuchMethod(method, dex_cache, class_loader));
8853 }
8854 return nullptr;
8855 }
8856 }
8857
8858 template <ClassLinker::ResolveMode kResolveMode>
ResolveMethod(uint32_t method_idx,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader,ArtMethod * referrer,InvokeType type)8859 ArtMethod* ClassLinker::ResolveMethod(uint32_t method_idx,
8860 Handle<mirror::DexCache> dex_cache,
8861 Handle<mirror::ClassLoader> class_loader,
8862 ArtMethod* referrer,
8863 InvokeType type) {
8864 DCHECK(!Thread::Current()->IsExceptionPending()) << Thread::Current()->GetException()->Dump();
8865 DCHECK(dex_cache != nullptr);
8866 DCHECK(referrer == nullptr || !referrer->IsProxyMethod());
8867 // Check for hit in the dex cache.
8868 PointerSize pointer_size = image_pointer_size_;
8869 ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, pointer_size);
8870 Thread::PoisonObjectPointersIfDebug();
8871 DCHECK(resolved == nullptr || !resolved->IsRuntimeMethod());
8872 bool valid_dex_cache_method = resolved != nullptr;
8873 if (kResolveMode == ResolveMode::kNoChecks && valid_dex_cache_method) {
8874 // We have a valid method from the DexCache and no checks to perform.
8875 DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex();
8876 return resolved;
8877 }
8878 const DexFile& dex_file = *dex_cache->GetDexFile();
8879 const dex::MethodId& method_id = dex_file.GetMethodId(method_idx);
8880 ObjPtr<mirror::Class> klass = nullptr;
8881 if (valid_dex_cache_method) {
8882 // We have a valid method from the DexCache but we need to perform ICCE and IAE checks.
8883 DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex();
8884 klass = LookupResolvedType(method_id.class_idx_, dex_cache.Get(), class_loader.Get());
8885 if (UNLIKELY(klass == nullptr)) {
8886 // We normaly should not end up here. However the verifier currently doesn't guarantee
8887 // the invariant of having the klass in the class table. b/73760543
8888 klass = ResolveType(method_id.class_idx_, dex_cache, class_loader);
8889 if (klass == nullptr) {
8890 // This can only happen if the current thread is not allowed to load
8891 // classes.
8892 DCHECK(!Thread::Current()->CanLoadClasses());
8893 DCHECK(Thread::Current()->IsExceptionPending());
8894 return nullptr;
8895 }
8896 }
8897 } else {
8898 // The method was not in the DexCache, resolve the declaring class.
8899 klass = ResolveType(method_id.class_idx_, dex_cache, class_loader);
8900 if (klass == nullptr) {
8901 DCHECK(Thread::Current()->IsExceptionPending());
8902 return nullptr;
8903 }
8904 }
8905
8906 // Check if the invoke type matches the class type.
8907 if (kResolveMode == ResolveMode::kCheckICCEAndIAE &&
8908 CheckInvokeClassMismatch</* kThrow= */ true>(
8909 dex_cache.Get(), type, [klass]() { return klass; })) {
8910 DCHECK(Thread::Current()->IsExceptionPending());
8911 return nullptr;
8912 }
8913
8914 if (!valid_dex_cache_method) {
8915 resolved = FindResolvedMethod(klass, dex_cache.Get(), class_loader.Get(), method_idx);
8916 }
8917
8918 // Note: We can check for IllegalAccessError only if we have a referrer.
8919 if (kResolveMode == ResolveMode::kCheckICCEAndIAE && resolved != nullptr && referrer != nullptr) {
8920 ObjPtr<mirror::Class> methods_class = resolved->GetDeclaringClass();
8921 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
8922 if (!referring_class->CheckResolvedMethodAccess(methods_class,
8923 resolved,
8924 dex_cache.Get(),
8925 method_idx,
8926 type)) {
8927 DCHECK(Thread::Current()->IsExceptionPending());
8928 return nullptr;
8929 }
8930 }
8931
8932 // If we found a method, check for incompatible class changes.
8933 if (LIKELY(resolved != nullptr) &&
8934 LIKELY(kResolveMode == ResolveMode::kNoChecks ||
8935 !resolved->CheckIncompatibleClassChange(type))) {
8936 return resolved;
8937 } else {
8938 // If we had a method, or if we can find one with another lookup type,
8939 // it's an incompatible-class-change error.
8940 if (resolved == nullptr) {
8941 resolved = FindIncompatibleMethod(klass, dex_cache.Get(), class_loader.Get(), method_idx);
8942 }
8943 if (resolved != nullptr) {
8944 ThrowIncompatibleClassChangeError(type, resolved->GetInvokeType(), resolved, referrer);
8945 } else {
8946 // We failed to find the method (using all lookup types), so throw a NoSuchMethodError.
8947 const char* name = dex_file.StringDataByIdx(method_id.name_idx_);
8948 const Signature signature = dex_file.GetMethodSignature(method_id);
8949 ThrowNoSuchMethodError(type, klass, name, signature);
8950 }
8951 Thread::Current()->AssertPendingException();
8952 return nullptr;
8953 }
8954 }
8955
ResolveMethodWithoutInvokeType(uint32_t method_idx,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader)8956 ArtMethod* ClassLinker::ResolveMethodWithoutInvokeType(uint32_t method_idx,
8957 Handle<mirror::DexCache> dex_cache,
8958 Handle<mirror::ClassLoader> class_loader) {
8959 ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, image_pointer_size_);
8960 Thread::PoisonObjectPointersIfDebug();
8961 if (resolved != nullptr) {
8962 DCHECK(!resolved->IsRuntimeMethod());
8963 DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex();
8964 return resolved;
8965 }
8966 // Fail, get the declaring class.
8967 const dex::MethodId& method_id = dex_cache->GetDexFile()->GetMethodId(method_idx);
8968 ObjPtr<mirror::Class> klass = ResolveType(method_id.class_idx_, dex_cache, class_loader);
8969 if (klass == nullptr) {
8970 Thread::Current()->AssertPendingException();
8971 return nullptr;
8972 }
8973 if (klass->IsInterface()) {
8974 resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx, image_pointer_size_);
8975 } else {
8976 resolved = klass->FindClassMethod(dex_cache.Get(), method_idx, image_pointer_size_);
8977 }
8978 if (resolved != nullptr &&
8979 hiddenapi::ShouldDenyAccessToMember(
8980 resolved,
8981 hiddenapi::AccessContext(class_loader.Get(), dex_cache.Get()),
8982 hiddenapi::AccessMethod::kLinking)) {
8983 resolved = nullptr;
8984 }
8985 return resolved;
8986 }
8987
LookupResolvedField(uint32_t field_idx,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader,bool is_static)8988 ArtField* ClassLinker::LookupResolvedField(uint32_t field_idx,
8989 ObjPtr<mirror::DexCache> dex_cache,
8990 ObjPtr<mirror::ClassLoader> class_loader,
8991 bool is_static) {
8992 const DexFile& dex_file = *dex_cache->GetDexFile();
8993 const dex::FieldId& field_id = dex_file.GetFieldId(field_idx);
8994 ObjPtr<mirror::Class> klass = dex_cache->GetResolvedType(field_id.class_idx_);
8995 if (klass == nullptr) {
8996 klass = LookupResolvedType(field_id.class_idx_, dex_cache, class_loader);
8997 }
8998 if (klass == nullptr) {
8999 // The class has not been resolved yet, so the field is also unresolved.
9000 return nullptr;
9001 }
9002 DCHECK(klass->IsResolved());
9003
9004 return FindResolvedField(klass, dex_cache, class_loader, field_idx, is_static);
9005 }
9006
ResolveField(uint32_t field_idx,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader,bool is_static)9007 ArtField* ClassLinker::ResolveField(uint32_t field_idx,
9008 Handle<mirror::DexCache> dex_cache,
9009 Handle<mirror::ClassLoader> class_loader,
9010 bool is_static) {
9011 DCHECK(dex_cache != nullptr);
9012 DCHECK(!Thread::Current()->IsExceptionPending()) << Thread::Current()->GetException()->Dump();
9013 ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_);
9014 Thread::PoisonObjectPointersIfDebug();
9015 if (resolved != nullptr) {
9016 return resolved;
9017 }
9018 const DexFile& dex_file = *dex_cache->GetDexFile();
9019 const dex::FieldId& field_id = dex_file.GetFieldId(field_idx);
9020 ObjPtr<mirror::Class> klass = ResolveType(field_id.class_idx_, dex_cache, class_loader);
9021 if (klass == nullptr) {
9022 DCHECK(Thread::Current()->IsExceptionPending());
9023 return nullptr;
9024 }
9025
9026 resolved = FindResolvedField(klass, dex_cache.Get(), class_loader.Get(), field_idx, is_static);
9027 if (resolved == nullptr) {
9028 const char* name = dex_file.GetFieldName(field_id);
9029 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
9030 ThrowNoSuchFieldError(is_static ? "static " : "instance ", klass, type, name);
9031 }
9032 return resolved;
9033 }
9034
ResolveFieldJLS(uint32_t field_idx,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader)9035 ArtField* ClassLinker::ResolveFieldJLS(uint32_t field_idx,
9036 Handle<mirror::DexCache> dex_cache,
9037 Handle<mirror::ClassLoader> class_loader) {
9038 DCHECK(dex_cache != nullptr);
9039 ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_);
9040 Thread::PoisonObjectPointersIfDebug();
9041 if (resolved != nullptr) {
9042 return resolved;
9043 }
9044 const DexFile& dex_file = *dex_cache->GetDexFile();
9045 const dex::FieldId& field_id = dex_file.GetFieldId(field_idx);
9046 ObjPtr<mirror::Class> klass = ResolveType(field_id.class_idx_, dex_cache, class_loader);
9047 if (klass == nullptr) {
9048 DCHECK(Thread::Current()->IsExceptionPending());
9049 return nullptr;
9050 }
9051
9052 resolved = FindResolvedFieldJLS(klass, dex_cache.Get(), class_loader.Get(), field_idx);
9053 if (resolved == nullptr) {
9054 const char* name = dex_file.GetFieldName(field_id);
9055 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
9056 ThrowNoSuchFieldError("", klass, type, name);
9057 }
9058 return resolved;
9059 }
9060
FindResolvedField(ObjPtr<mirror::Class> klass,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader,uint32_t field_idx,bool is_static)9061 ArtField* ClassLinker::FindResolvedField(ObjPtr<mirror::Class> klass,
9062 ObjPtr<mirror::DexCache> dex_cache,
9063 ObjPtr<mirror::ClassLoader> class_loader,
9064 uint32_t field_idx,
9065 bool is_static) {
9066 ArtField* resolved = nullptr;
9067 Thread* self = is_static ? Thread::Current() : nullptr;
9068 const DexFile& dex_file = *dex_cache->GetDexFile();
9069
9070 resolved = is_static ? mirror::Class::FindStaticField(self, klass, dex_cache, field_idx)
9071 : klass->FindInstanceField(dex_cache, field_idx);
9072
9073 if (resolved == nullptr) {
9074 const dex::FieldId& field_id = dex_file.GetFieldId(field_idx);
9075 const char* name = dex_file.GetFieldName(field_id);
9076 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
9077 resolved = is_static ? mirror::Class::FindStaticField(self, klass, name, type)
9078 : klass->FindInstanceField(name, type);
9079 }
9080
9081 if (resolved != nullptr &&
9082 hiddenapi::ShouldDenyAccessToMember(resolved,
9083 hiddenapi::AccessContext(class_loader, dex_cache),
9084 hiddenapi::AccessMethod::kLinking)) {
9085 resolved = nullptr;
9086 }
9087
9088 if (resolved != nullptr) {
9089 dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_);
9090 }
9091
9092 return resolved;
9093 }
9094
FindResolvedFieldJLS(ObjPtr<mirror::Class> klass,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader,uint32_t field_idx)9095 ArtField* ClassLinker::FindResolvedFieldJLS(ObjPtr<mirror::Class> klass,
9096 ObjPtr<mirror::DexCache> dex_cache,
9097 ObjPtr<mirror::ClassLoader> class_loader,
9098 uint32_t field_idx) {
9099 ArtField* resolved = nullptr;
9100 Thread* self = Thread::Current();
9101 const DexFile& dex_file = *dex_cache->GetDexFile();
9102 const dex::FieldId& field_id = dex_file.GetFieldId(field_idx);
9103
9104 const char* name = dex_file.GetFieldName(field_id);
9105 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
9106 resolved = mirror::Class::FindField(self, klass, name, type);
9107
9108 if (resolved != nullptr &&
9109 hiddenapi::ShouldDenyAccessToMember(resolved,
9110 hiddenapi::AccessContext(class_loader, dex_cache),
9111 hiddenapi::AccessMethod::kLinking)) {
9112 resolved = nullptr;
9113 }
9114
9115 if (resolved != nullptr) {
9116 dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_);
9117 }
9118
9119 return resolved;
9120 }
9121
ResolveMethodType(Thread * self,dex::ProtoIndex proto_idx,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader)9122 ObjPtr<mirror::MethodType> ClassLinker::ResolveMethodType(
9123 Thread* self,
9124 dex::ProtoIndex proto_idx,
9125 Handle<mirror::DexCache> dex_cache,
9126 Handle<mirror::ClassLoader> class_loader) {
9127 DCHECK(Runtime::Current()->IsMethodHandlesEnabled());
9128 DCHECK(dex_cache != nullptr);
9129
9130 ObjPtr<mirror::MethodType> resolved = dex_cache->GetResolvedMethodType(proto_idx);
9131 if (resolved != nullptr) {
9132 return resolved;
9133 }
9134
9135 StackHandleScope<4> hs(self);
9136
9137 // First resolve the return type.
9138 const DexFile& dex_file = *dex_cache->GetDexFile();
9139 const dex::ProtoId& proto_id = dex_file.GetProtoId(proto_idx);
9140 Handle<mirror::Class> return_type(hs.NewHandle(
9141 ResolveType(proto_id.return_type_idx_, dex_cache, class_loader)));
9142 if (return_type == nullptr) {
9143 DCHECK(self->IsExceptionPending());
9144 return nullptr;
9145 }
9146
9147 // Then resolve the argument types.
9148 //
9149 // TODO: Is there a better way to figure out the number of method arguments
9150 // other than by looking at the shorty ?
9151 const size_t num_method_args = strlen(dex_file.StringDataByIdx(proto_id.shorty_idx_)) - 1;
9152
9153 ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this);
9154 Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle(
9155 mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_method_args)));
9156 if (method_params == nullptr) {
9157 DCHECK(self->IsExceptionPending());
9158 return nullptr;
9159 }
9160
9161 DexFileParameterIterator it(dex_file, proto_id);
9162 int32_t i = 0;
9163 MutableHandle<mirror::Class> param_class = hs.NewHandle<mirror::Class>(nullptr);
9164 for (; it.HasNext(); it.Next()) {
9165 const dex::TypeIndex type_idx = it.GetTypeIdx();
9166 param_class.Assign(ResolveType(type_idx, dex_cache, class_loader));
9167 if (param_class == nullptr) {
9168 DCHECK(self->IsExceptionPending());
9169 return nullptr;
9170 }
9171
9172 method_params->Set(i++, param_class.Get());
9173 }
9174
9175 DCHECK(!it.HasNext());
9176
9177 Handle<mirror::MethodType> type = hs.NewHandle(
9178 mirror::MethodType::Create(self, return_type, method_params));
9179 dex_cache->SetResolvedMethodType(proto_idx, type.Get());
9180
9181 return type.Get();
9182 }
9183
ResolveMethodType(Thread * self,dex::ProtoIndex proto_idx,ArtMethod * referrer)9184 ObjPtr<mirror::MethodType> ClassLinker::ResolveMethodType(Thread* self,
9185 dex::ProtoIndex proto_idx,
9186 ArtMethod* referrer) {
9187 StackHandleScope<2> hs(self);
9188 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
9189 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader()));
9190 return ResolveMethodType(self, proto_idx, dex_cache, class_loader);
9191 }
9192
ResolveMethodHandleForField(Thread * self,const dex::MethodHandleItem & method_handle,ArtMethod * referrer)9193 ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandleForField(
9194 Thread* self,
9195 const dex::MethodHandleItem& method_handle,
9196 ArtMethod* referrer) {
9197 DexFile::MethodHandleType handle_type =
9198 static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_);
9199 mirror::MethodHandle::Kind kind;
9200 bool is_put;
9201 bool is_static;
9202 int32_t num_params;
9203 switch (handle_type) {
9204 case DexFile::MethodHandleType::kStaticPut: {
9205 kind = mirror::MethodHandle::Kind::kStaticPut;
9206 is_put = true;
9207 is_static = true;
9208 num_params = 1;
9209 break;
9210 }
9211 case DexFile::MethodHandleType::kStaticGet: {
9212 kind = mirror::MethodHandle::Kind::kStaticGet;
9213 is_put = false;
9214 is_static = true;
9215 num_params = 0;
9216 break;
9217 }
9218 case DexFile::MethodHandleType::kInstancePut: {
9219 kind = mirror::MethodHandle::Kind::kInstancePut;
9220 is_put = true;
9221 is_static = false;
9222 num_params = 2;
9223 break;
9224 }
9225 case DexFile::MethodHandleType::kInstanceGet: {
9226 kind = mirror::MethodHandle::Kind::kInstanceGet;
9227 is_put = false;
9228 is_static = false;
9229 num_params = 1;
9230 break;
9231 }
9232 case DexFile::MethodHandleType::kInvokeStatic:
9233 case DexFile::MethodHandleType::kInvokeInstance:
9234 case DexFile::MethodHandleType::kInvokeConstructor:
9235 case DexFile::MethodHandleType::kInvokeDirect:
9236 case DexFile::MethodHandleType::kInvokeInterface:
9237 UNREACHABLE();
9238 }
9239
9240 ArtField* target_field =
9241 ResolveField(method_handle.field_or_method_idx_, referrer, is_static);
9242 if (LIKELY(target_field != nullptr)) {
9243 ObjPtr<mirror::Class> target_class = target_field->GetDeclaringClass();
9244 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
9245 if (UNLIKELY(!referring_class->CanAccessMember(target_class, target_field->GetAccessFlags()))) {
9246 ThrowIllegalAccessErrorField(referring_class, target_field);
9247 return nullptr;
9248 }
9249 if (UNLIKELY(is_put && target_field->IsFinal())) {
9250 ThrowIllegalAccessErrorField(referring_class, target_field);
9251 return nullptr;
9252 }
9253 } else {
9254 DCHECK(Thread::Current()->IsExceptionPending());
9255 return nullptr;
9256 }
9257
9258 StackHandleScope<4> hs(self);
9259 ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this);
9260 Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle(
9261 mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params)));
9262 if (UNLIKELY(method_params == nullptr)) {
9263 DCHECK(self->IsExceptionPending());
9264 return nullptr;
9265 }
9266
9267 Handle<mirror::Class> constructor_class;
9268 Handle<mirror::Class> return_type;
9269 switch (handle_type) {
9270 case DexFile::MethodHandleType::kStaticPut: {
9271 method_params->Set(0, target_field->ResolveType());
9272 return_type = hs.NewHandle(GetClassRoot(ClassRoot::kPrimitiveVoid, this));
9273 break;
9274 }
9275 case DexFile::MethodHandleType::kStaticGet: {
9276 return_type = hs.NewHandle(target_field->ResolveType());
9277 break;
9278 }
9279 case DexFile::MethodHandleType::kInstancePut: {
9280 method_params->Set(0, target_field->GetDeclaringClass());
9281 method_params->Set(1, target_field->ResolveType());
9282 return_type = hs.NewHandle(GetClassRoot(ClassRoot::kPrimitiveVoid, this));
9283 break;
9284 }
9285 case DexFile::MethodHandleType::kInstanceGet: {
9286 method_params->Set(0, target_field->GetDeclaringClass());
9287 return_type = hs.NewHandle(target_field->ResolveType());
9288 break;
9289 }
9290 case DexFile::MethodHandleType::kInvokeStatic:
9291 case DexFile::MethodHandleType::kInvokeInstance:
9292 case DexFile::MethodHandleType::kInvokeConstructor:
9293 case DexFile::MethodHandleType::kInvokeDirect:
9294 case DexFile::MethodHandleType::kInvokeInterface:
9295 UNREACHABLE();
9296 }
9297
9298 for (int32_t i = 0; i < num_params; ++i) {
9299 if (UNLIKELY(method_params->Get(i) == nullptr)) {
9300 DCHECK(self->IsExceptionPending());
9301 return nullptr;
9302 }
9303 }
9304
9305 if (UNLIKELY(return_type.IsNull())) {
9306 DCHECK(self->IsExceptionPending());
9307 return nullptr;
9308 }
9309
9310 Handle<mirror::MethodType>
9311 method_type(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params)));
9312 if (UNLIKELY(method_type.IsNull())) {
9313 DCHECK(self->IsExceptionPending());
9314 return nullptr;
9315 }
9316
9317 uintptr_t target = reinterpret_cast<uintptr_t>(target_field);
9318 return mirror::MethodHandleImpl::Create(self, target, kind, method_type);
9319 }
9320
ResolveMethodHandleForMethod(Thread * self,const dex::MethodHandleItem & method_handle,ArtMethod * referrer)9321 ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandleForMethod(
9322 Thread* self,
9323 const dex::MethodHandleItem& method_handle,
9324 ArtMethod* referrer) {
9325 DexFile::MethodHandleType handle_type =
9326 static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_);
9327 mirror::MethodHandle::Kind kind;
9328 uint32_t receiver_count = 0;
9329 ArtMethod* target_method = nullptr;
9330 switch (handle_type) {
9331 case DexFile::MethodHandleType::kStaticPut:
9332 case DexFile::MethodHandleType::kStaticGet:
9333 case DexFile::MethodHandleType::kInstancePut:
9334 case DexFile::MethodHandleType::kInstanceGet:
9335 UNREACHABLE();
9336 case DexFile::MethodHandleType::kInvokeStatic: {
9337 kind = mirror::MethodHandle::Kind::kInvokeStatic;
9338 receiver_count = 0;
9339 target_method = ResolveMethod<ResolveMode::kNoChecks>(self,
9340 method_handle.field_or_method_idx_,
9341 referrer,
9342 InvokeType::kStatic);
9343 break;
9344 }
9345 case DexFile::MethodHandleType::kInvokeInstance: {
9346 kind = mirror::MethodHandle::Kind::kInvokeVirtual;
9347 receiver_count = 1;
9348 target_method = ResolveMethod<ResolveMode::kNoChecks>(self,
9349 method_handle.field_or_method_idx_,
9350 referrer,
9351 InvokeType::kVirtual);
9352 break;
9353 }
9354 case DexFile::MethodHandleType::kInvokeConstructor: {
9355 // Constructors are currently implemented as a transform. They
9356 // are special cased later in this method.
9357 kind = mirror::MethodHandle::Kind::kInvokeTransform;
9358 receiver_count = 0;
9359 target_method = ResolveMethod<ResolveMode::kNoChecks>(self,
9360 method_handle.field_or_method_idx_,
9361 referrer,
9362 InvokeType::kDirect);
9363 break;
9364 }
9365 case DexFile::MethodHandleType::kInvokeDirect: {
9366 kind = mirror::MethodHandle::Kind::kInvokeDirect;
9367 receiver_count = 1;
9368 StackHandleScope<2> hs(self);
9369 // A constant method handle with type kInvokeDirect can refer to
9370 // a method that is private or to a method in a super class. To
9371 // disambiguate the two options, we resolve the method ignoring
9372 // the invocation type to determine if the method is private. We
9373 // then resolve again specifying the intended invocation type to
9374 // force the appropriate checks.
9375 target_method = ResolveMethodWithoutInvokeType(method_handle.field_or_method_idx_,
9376 hs.NewHandle(referrer->GetDexCache()),
9377 hs.NewHandle(referrer->GetClassLoader()));
9378 if (UNLIKELY(target_method == nullptr)) {
9379 break;
9380 }
9381
9382 if (target_method->IsPrivate()) {
9383 kind = mirror::MethodHandle::Kind::kInvokeDirect;
9384 target_method = ResolveMethod<ResolveMode::kNoChecks>(self,
9385 method_handle.field_or_method_idx_,
9386 referrer,
9387 InvokeType::kDirect);
9388 } else {
9389 kind = mirror::MethodHandle::Kind::kInvokeSuper;
9390 target_method = ResolveMethod<ResolveMode::kNoChecks>(self,
9391 method_handle.field_or_method_idx_,
9392 referrer,
9393 InvokeType::kSuper);
9394 if (UNLIKELY(target_method == nullptr)) {
9395 break;
9396 }
9397 // Find the method specified in the parent in referring class
9398 // so invoke-super invokes the method in the parent of the
9399 // referrer.
9400 target_method =
9401 referrer->GetDeclaringClass()->FindVirtualMethodForVirtual(target_method,
9402 kRuntimePointerSize);
9403 }
9404 break;
9405 }
9406 case DexFile::MethodHandleType::kInvokeInterface: {
9407 kind = mirror::MethodHandle::Kind::kInvokeInterface;
9408 receiver_count = 1;
9409 target_method = ResolveMethod<ResolveMode::kNoChecks>(self,
9410 method_handle.field_or_method_idx_,
9411 referrer,
9412 InvokeType::kInterface);
9413 break;
9414 }
9415 }
9416
9417 if (UNLIKELY(target_method == nullptr)) {
9418 DCHECK(Thread::Current()->IsExceptionPending());
9419 return nullptr;
9420 }
9421
9422 ObjPtr<mirror::Class> target_class = target_method->GetDeclaringClass();
9423 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
9424 uint32_t access_flags = target_method->GetAccessFlags();
9425 if (UNLIKELY(!referring_class->CanAccessMember(target_class, access_flags))) {
9426 ThrowIllegalAccessErrorMethod(referring_class, target_method);
9427 return nullptr;
9428 }
9429
9430 // Calculate the number of parameters from the method shorty. We add the
9431 // receiver count (0 or 1) and deduct one for the return value.
9432 uint32_t shorty_length;
9433 target_method->GetShorty(&shorty_length);
9434 int32_t num_params = static_cast<int32_t>(shorty_length + receiver_count - 1);
9435
9436 StackHandleScope<5> hs(self);
9437 ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this);
9438 Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle(
9439 mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params)));
9440 if (method_params.Get() == nullptr) {
9441 DCHECK(self->IsExceptionPending());
9442 return nullptr;
9443 }
9444
9445 const DexFile* dex_file = referrer->GetDexFile();
9446 const dex::MethodId& method_id = dex_file->GetMethodId(method_handle.field_or_method_idx_);
9447 int32_t index = 0;
9448 if (receiver_count != 0) {
9449 // Insert receiver. Use the class identified in the method handle rather than the declaring
9450 // class of the resolved method which may be super class or default interface method
9451 // (b/115964401).
9452 ObjPtr<mirror::Class> receiver_class = LookupResolvedType(method_id.class_idx_, referrer);
9453 // receiver_class should have been resolved when resolving the target method.
9454 DCHECK(receiver_class != nullptr);
9455 method_params->Set(index++, receiver_class);
9456 }
9457
9458 const dex::ProtoId& proto_id = dex_file->GetProtoId(method_id.proto_idx_);
9459 DexFileParameterIterator it(*dex_file, proto_id);
9460 while (it.HasNext()) {
9461 DCHECK_LT(index, num_params);
9462 const dex::TypeIndex type_idx = it.GetTypeIdx();
9463 ObjPtr<mirror::Class> klass = ResolveType(type_idx, referrer);
9464 if (nullptr == klass) {
9465 DCHECK(self->IsExceptionPending());
9466 return nullptr;
9467 }
9468 method_params->Set(index++, klass);
9469 it.Next();
9470 }
9471
9472 Handle<mirror::Class> return_type =
9473 hs.NewHandle(ResolveType(proto_id.return_type_idx_, referrer));
9474 if (UNLIKELY(return_type.IsNull())) {
9475 DCHECK(self->IsExceptionPending());
9476 return nullptr;
9477 }
9478
9479 Handle<mirror::MethodType>
9480 method_type(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params)));
9481 if (UNLIKELY(method_type.IsNull())) {
9482 DCHECK(self->IsExceptionPending());
9483 return nullptr;
9484 }
9485
9486 if (UNLIKELY(handle_type == DexFile::MethodHandleType::kInvokeConstructor)) {
9487 Handle<mirror::Class> constructor_class = hs.NewHandle(target_method->GetDeclaringClass());
9488 Handle<mirror::MethodHandlesLookup> lookup =
9489 hs.NewHandle(mirror::MethodHandlesLookup::GetDefault(self));
9490 return lookup->FindConstructor(self, constructor_class, method_type);
9491 }
9492
9493 uintptr_t target = reinterpret_cast<uintptr_t>(target_method);
9494 return mirror::MethodHandleImpl::Create(self, target, kind, method_type);
9495 }
9496
ResolveMethodHandle(Thread * self,uint32_t method_handle_idx,ArtMethod * referrer)9497 ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandle(Thread* self,
9498 uint32_t method_handle_idx,
9499 ArtMethod* referrer)
9500 REQUIRES_SHARED(Locks::mutator_lock_) {
9501 const DexFile* const dex_file = referrer->GetDexFile();
9502 const dex::MethodHandleItem& method_handle = dex_file->GetMethodHandle(method_handle_idx);
9503 switch (static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_)) {
9504 case DexFile::MethodHandleType::kStaticPut:
9505 case DexFile::MethodHandleType::kStaticGet:
9506 case DexFile::MethodHandleType::kInstancePut:
9507 case DexFile::MethodHandleType::kInstanceGet:
9508 return ResolveMethodHandleForField(self, method_handle, referrer);
9509 case DexFile::MethodHandleType::kInvokeStatic:
9510 case DexFile::MethodHandleType::kInvokeInstance:
9511 case DexFile::MethodHandleType::kInvokeConstructor:
9512 case DexFile::MethodHandleType::kInvokeDirect:
9513 case DexFile::MethodHandleType::kInvokeInterface:
9514 return ResolveMethodHandleForMethod(self, method_handle, referrer);
9515 }
9516 }
9517
IsQuickResolutionStub(const void * entry_point) const9518 bool ClassLinker::IsQuickResolutionStub(const void* entry_point) const {
9519 return (entry_point == GetQuickResolutionStub()) ||
9520 (quick_resolution_trampoline_ == entry_point);
9521 }
9522
IsQuickToInterpreterBridge(const void * entry_point) const9523 bool ClassLinker::IsQuickToInterpreterBridge(const void* entry_point) const {
9524 return (entry_point == GetQuickToInterpreterBridge()) ||
9525 (quick_to_interpreter_bridge_trampoline_ == entry_point);
9526 }
9527
IsQuickGenericJniStub(const void * entry_point) const9528 bool ClassLinker::IsQuickGenericJniStub(const void* entry_point) const {
9529 return (entry_point == GetQuickGenericJniStub()) ||
9530 (quick_generic_jni_trampoline_ == entry_point);
9531 }
9532
IsJniDlsymLookupStub(const void * entry_point) const9533 bool ClassLinker::IsJniDlsymLookupStub(const void* entry_point) const {
9534 return entry_point == GetJniDlsymLookupStub() ||
9535 (jni_dlsym_lookup_trampoline_ == entry_point);
9536 }
9537
IsJniDlsymLookupCriticalStub(const void * entry_point) const9538 bool ClassLinker::IsJniDlsymLookupCriticalStub(const void* entry_point) const {
9539 return entry_point == GetJniDlsymLookupCriticalStub() ||
9540 (jni_dlsym_lookup_critical_trampoline_ == entry_point);
9541 }
9542
GetRuntimeQuickGenericJniStub() const9543 const void* ClassLinker::GetRuntimeQuickGenericJniStub() const {
9544 return GetQuickGenericJniStub();
9545 }
9546
SetEntryPointsToInterpreter(ArtMethod * method) const9547 void ClassLinker::SetEntryPointsToInterpreter(ArtMethod* method) const {
9548 if (!method->IsNative()) {
9549 method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge());
9550 } else {
9551 method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub());
9552 }
9553 }
9554
SetEntryPointsForObsoleteMethod(ArtMethod * method) const9555 void ClassLinker::SetEntryPointsForObsoleteMethod(ArtMethod* method) const {
9556 DCHECK(method->IsObsolete());
9557 // We cannot mess with the entrypoints of native methods because they are used to determine how
9558 // large the method's quick stack frame is. Without this information we cannot walk the stacks.
9559 if (!method->IsNative()) {
9560 method->SetEntryPointFromQuickCompiledCode(GetInvokeObsoleteMethodStub());
9561 }
9562 }
9563
DumpForSigQuit(std::ostream & os)9564 void ClassLinker::DumpForSigQuit(std::ostream& os) {
9565 ScopedObjectAccess soa(Thread::Current());
9566 ReaderMutexLock mu(soa.Self(), *Locks::classlinker_classes_lock_);
9567 os << "Zygote loaded classes=" << NumZygoteClasses() << " post zygote classes="
9568 << NumNonZygoteClasses() << "\n";
9569 ReaderMutexLock mu2(soa.Self(), *Locks::dex_lock_);
9570 os << "Dumping registered class loaders\n";
9571 size_t class_loader_index = 0;
9572 for (const ClassLoaderData& class_loader : class_loaders_) {
9573 ObjPtr<mirror::ClassLoader> loader =
9574 ObjPtr<mirror::ClassLoader>::DownCast(soa.Self()->DecodeJObject(class_loader.weak_root));
9575 if (loader != nullptr) {
9576 os << "#" << class_loader_index++ << " " << loader->GetClass()->PrettyDescriptor() << ": [";
9577 bool saw_one_dex_file = false;
9578 for (const DexCacheData& dex_cache : dex_caches_) {
9579 if (dex_cache.IsValid() && dex_cache.class_table == class_loader.class_table) {
9580 if (saw_one_dex_file) {
9581 os << ":";
9582 }
9583 saw_one_dex_file = true;
9584 os << dex_cache.dex_file->GetLocation();
9585 }
9586 }
9587 os << "]";
9588 bool found_parent = false;
9589 if (loader->GetParent() != nullptr) {
9590 size_t parent_index = 0;
9591 for (const ClassLoaderData& class_loader2 : class_loaders_) {
9592 ObjPtr<mirror::ClassLoader> loader2 = ObjPtr<mirror::ClassLoader>::DownCast(
9593 soa.Self()->DecodeJObject(class_loader2.weak_root));
9594 if (loader2 == loader->GetParent()) {
9595 os << ", parent #" << parent_index;
9596 found_parent = true;
9597 break;
9598 }
9599 parent_index++;
9600 }
9601 if (!found_parent) {
9602 os << ", unregistered parent of type "
9603 << loader->GetParent()->GetClass()->PrettyDescriptor();
9604 }
9605 } else {
9606 os << ", no parent";
9607 }
9608 os << "\n";
9609 }
9610 }
9611 os << "Done dumping class loaders\n";
9612 Runtime* runtime = Runtime::Current();
9613 os << "Classes initialized: " << runtime->GetStat(KIND_GLOBAL_CLASS_INIT_COUNT) << " in "
9614 << PrettyDuration(runtime->GetStat(KIND_GLOBAL_CLASS_INIT_TIME)) << "\n";
9615 }
9616
9617 class CountClassesVisitor : public ClassLoaderVisitor {
9618 public:
CountClassesVisitor()9619 CountClassesVisitor() : num_zygote_classes(0), num_non_zygote_classes(0) {}
9620
Visit(ObjPtr<mirror::ClassLoader> class_loader)9621 void Visit(ObjPtr<mirror::ClassLoader> class_loader)
9622 REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override {
9623 ClassTable* const class_table = class_loader->GetClassTable();
9624 if (class_table != nullptr) {
9625 num_zygote_classes += class_table->NumZygoteClasses(class_loader);
9626 num_non_zygote_classes += class_table->NumNonZygoteClasses(class_loader);
9627 }
9628 }
9629
9630 size_t num_zygote_classes;
9631 size_t num_non_zygote_classes;
9632 };
9633
NumZygoteClasses() const9634 size_t ClassLinker::NumZygoteClasses() const {
9635 CountClassesVisitor visitor;
9636 VisitClassLoaders(&visitor);
9637 return visitor.num_zygote_classes + boot_class_table_->NumZygoteClasses(nullptr);
9638 }
9639
NumNonZygoteClasses() const9640 size_t ClassLinker::NumNonZygoteClasses() const {
9641 CountClassesVisitor visitor;
9642 VisitClassLoaders(&visitor);
9643 return visitor.num_non_zygote_classes + boot_class_table_->NumNonZygoteClasses(nullptr);
9644 }
9645
NumLoadedClasses()9646 size_t ClassLinker::NumLoadedClasses() {
9647 ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
9648 // Only return non zygote classes since these are the ones which apps which care about.
9649 return NumNonZygoteClasses();
9650 }
9651
GetClassesLockOwner()9652 pid_t ClassLinker::GetClassesLockOwner() {
9653 return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid();
9654 }
9655
GetDexLockOwner()9656 pid_t ClassLinker::GetDexLockOwner() {
9657 return Locks::dex_lock_->GetExclusiveOwnerTid();
9658 }
9659
SetClassRoot(ClassRoot class_root,ObjPtr<mirror::Class> klass)9660 void ClassLinker::SetClassRoot(ClassRoot class_root, ObjPtr<mirror::Class> klass) {
9661 DCHECK(!init_done_);
9662
9663 DCHECK(klass != nullptr);
9664 DCHECK(klass->GetClassLoader() == nullptr);
9665
9666 mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read();
9667 DCHECK(class_roots != nullptr);
9668 DCHECK_LT(static_cast<uint32_t>(class_root), static_cast<uint32_t>(ClassRoot::kMax));
9669 int32_t index = static_cast<int32_t>(class_root);
9670 DCHECK(class_roots->Get(index) == nullptr);
9671 class_roots->Set<false>(index, klass);
9672 }
9673
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)9674 ObjPtr<mirror::ClassLoader> ClassLinker::CreateWellKnownClassLoader(
9675 Thread* self,
9676 const std::vector<const DexFile*>& dex_files,
9677 Handle<mirror::Class> loader_class,
9678 Handle<mirror::ClassLoader> parent_loader,
9679 Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries) {
9680
9681 StackHandleScope<5> hs(self);
9682
9683 ArtField* dex_elements_field =
9684 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements);
9685
9686 Handle<mirror::Class> dex_elements_class(hs.NewHandle(dex_elements_field->ResolveType()));
9687 DCHECK(dex_elements_class != nullptr);
9688 DCHECK(dex_elements_class->IsArrayClass());
9689 Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements(hs.NewHandle(
9690 mirror::ObjectArray<mirror::Object>::Alloc(self,
9691 dex_elements_class.Get(),
9692 dex_files.size())));
9693 Handle<mirror::Class> h_dex_element_class =
9694 hs.NewHandle(dex_elements_class->GetComponentType());
9695
9696 ArtField* element_file_field =
9697 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile);
9698 DCHECK_EQ(h_dex_element_class.Get(), element_file_field->GetDeclaringClass());
9699
9700 ArtField* cookie_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie);
9701 DCHECK_EQ(cookie_field->GetDeclaringClass(), element_file_field->LookupResolvedType());
9702
9703 ArtField* file_name_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_fileName);
9704 DCHECK_EQ(file_name_field->GetDeclaringClass(), element_file_field->LookupResolvedType());
9705
9706 // Fill the elements array.
9707 int32_t index = 0;
9708 for (const DexFile* dex_file : dex_files) {
9709 StackHandleScope<4> hs2(self);
9710
9711 // CreateWellKnownClassLoader is only used by gtests and compiler.
9712 // Index 0 of h_long_array is supposed to be the oat file but we can leave it null.
9713 Handle<mirror::LongArray> h_long_array = hs2.NewHandle(mirror::LongArray::Alloc(
9714 self,
9715 kDexFileIndexStart + 1));
9716 DCHECK(h_long_array != nullptr);
9717 h_long_array->Set(kDexFileIndexStart, reinterpret_cast64<int64_t>(dex_file));
9718
9719 // Note that this creates a finalizable dalvik.system.DexFile object and a corresponding
9720 // FinalizerReference which will never get cleaned up without a started runtime.
9721 Handle<mirror::Object> h_dex_file = hs2.NewHandle(
9722 cookie_field->GetDeclaringClass()->AllocObject(self));
9723 DCHECK(h_dex_file != nullptr);
9724 cookie_field->SetObject<false>(h_dex_file.Get(), h_long_array.Get());
9725
9726 Handle<mirror::String> h_file_name = hs2.NewHandle(
9727 mirror::String::AllocFromModifiedUtf8(self, dex_file->GetLocation().c_str()));
9728 DCHECK(h_file_name != nullptr);
9729 file_name_field->SetObject<false>(h_dex_file.Get(), h_file_name.Get());
9730
9731 Handle<mirror::Object> h_element = hs2.NewHandle(h_dex_element_class->AllocObject(self));
9732 DCHECK(h_element != nullptr);
9733 element_file_field->SetObject<false>(h_element.Get(), h_dex_file.Get());
9734
9735 h_dex_elements->Set(index, h_element.Get());
9736 index++;
9737 }
9738 DCHECK_EQ(index, h_dex_elements->GetLength());
9739
9740 // Create DexPathList.
9741 Handle<mirror::Object> h_dex_path_list = hs.NewHandle(
9742 dex_elements_field->GetDeclaringClass()->AllocObject(self));
9743 DCHECK(h_dex_path_list != nullptr);
9744 // Set elements.
9745 dex_elements_field->SetObject<false>(h_dex_path_list.Get(), h_dex_elements.Get());
9746 // Create an empty List for the "nativeLibraryDirectories," required for native tests.
9747 // Note: this code is uncommon(oatdump)/testing-only, so don't add further WellKnownClasses
9748 // elements.
9749 {
9750 ArtField* native_lib_dirs = dex_elements_field->GetDeclaringClass()->
9751 FindDeclaredInstanceField("nativeLibraryDirectories", "Ljava/util/List;");
9752 DCHECK(native_lib_dirs != nullptr);
9753 ObjPtr<mirror::Class> list_class = FindSystemClass(self, "Ljava/util/ArrayList;");
9754 DCHECK(list_class != nullptr);
9755 {
9756 StackHandleScope<1> h_list_scope(self);
9757 Handle<mirror::Class> h_list_class(h_list_scope.NewHandle<mirror::Class>(list_class));
9758 bool list_init = EnsureInitialized(self, h_list_class, true, true);
9759 DCHECK(list_init);
9760 list_class = h_list_class.Get();
9761 }
9762 ObjPtr<mirror::Object> list_object = list_class->AllocObject(self);
9763 // Note: we leave the object uninitialized. This must never leak into any non-testing code, but
9764 // is fine for testing. While it violates a Java-code invariant (the elementData field is
9765 // normally never null), as long as one does not try to add elements, this will still
9766 // work.
9767 native_lib_dirs->SetObject<false>(h_dex_path_list.Get(), list_object);
9768 }
9769
9770 // Create the class loader..
9771 Handle<mirror::ClassLoader> h_class_loader = hs.NewHandle<mirror::ClassLoader>(
9772 ObjPtr<mirror::ClassLoader>::DownCast(loader_class->AllocObject(self)));
9773 DCHECK(h_class_loader != nullptr);
9774 // Set DexPathList.
9775 ArtField* path_list_field =
9776 jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList);
9777 DCHECK(path_list_field != nullptr);
9778 path_list_field->SetObject<false>(h_class_loader.Get(), h_dex_path_list.Get());
9779
9780 // Make a pretend boot-classpath.
9781 // TODO: Should we scan the image?
9782 ArtField* const parent_field =
9783 mirror::Class::FindField(self,
9784 h_class_loader->GetClass(),
9785 "parent",
9786 "Ljava/lang/ClassLoader;");
9787 DCHECK(parent_field != nullptr);
9788 if (parent_loader.Get() == nullptr) {
9789 ScopedObjectAccessUnchecked soa(self);
9790 ObjPtr<mirror::Object> boot_loader(soa.Decode<mirror::Class>(
9791 WellKnownClasses::java_lang_BootClassLoader)->AllocObject(self));
9792 parent_field->SetObject<false>(h_class_loader.Get(), boot_loader);
9793 } else {
9794 parent_field->SetObject<false>(h_class_loader.Get(), parent_loader.Get());
9795 }
9796
9797 ArtField* shared_libraries_field =
9798 jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders);
9799 DCHECK(shared_libraries_field != nullptr);
9800 shared_libraries_field->SetObject<false>(h_class_loader.Get(), shared_libraries.Get());
9801
9802 return h_class_loader.Get();
9803 }
9804
CreateWellKnownClassLoader(Thread * self,const std::vector<const DexFile * > & dex_files,jclass loader_class,jobject parent_loader,jobject shared_libraries)9805 jobject ClassLinker::CreateWellKnownClassLoader(Thread* self,
9806 const std::vector<const DexFile*>& dex_files,
9807 jclass loader_class,
9808 jobject parent_loader,
9809 jobject shared_libraries) {
9810 CHECK(self->GetJniEnv()->IsSameObject(loader_class,
9811 WellKnownClasses::dalvik_system_PathClassLoader) ||
9812 self->GetJniEnv()->IsSameObject(loader_class,
9813 WellKnownClasses::dalvik_system_DelegateLastClassLoader) ||
9814 self->GetJniEnv()->IsSameObject(loader_class,
9815 WellKnownClasses::dalvik_system_InMemoryDexClassLoader));
9816
9817 // SOAAlreadyRunnable is protected, and we need something to add a global reference.
9818 // We could move the jobject to the callers, but all call-sites do this...
9819 ScopedObjectAccessUnchecked soa(self);
9820
9821 // For now, create a libcore-level DexFile for each ART DexFile. This "explodes" multidex.
9822 StackHandleScope<4> hs(self);
9823
9824 Handle<mirror::Class> h_loader_class =
9825 hs.NewHandle<mirror::Class>(soa.Decode<mirror::Class>(loader_class));
9826 Handle<mirror::ClassLoader> h_parent =
9827 hs.NewHandle<mirror::ClassLoader>(soa.Decode<mirror::ClassLoader>(parent_loader));
9828 Handle<mirror::ObjectArray<mirror::ClassLoader>> h_shared_libraries =
9829 hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::ClassLoader>>(shared_libraries));
9830
9831 ObjPtr<mirror::ClassLoader> loader = CreateWellKnownClassLoader(
9832 self,
9833 dex_files,
9834 h_loader_class,
9835 h_parent,
9836 h_shared_libraries);
9837
9838 // Make it a global ref and return.
9839 ScopedLocalRef<jobject> local_ref(
9840 soa.Env(), soa.Env()->AddLocalReference<jobject>(loader));
9841 return soa.Env()->NewGlobalRef(local_ref.get());
9842 }
9843
CreatePathClassLoader(Thread * self,const std::vector<const DexFile * > & dex_files)9844 jobject ClassLinker::CreatePathClassLoader(Thread* self,
9845 const std::vector<const DexFile*>& dex_files) {
9846 return CreateWellKnownClassLoader(self,
9847 dex_files,
9848 WellKnownClasses::dalvik_system_PathClassLoader,
9849 nullptr);
9850 }
9851
DropFindArrayClassCache()9852 void ClassLinker::DropFindArrayClassCache() {
9853 std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr));
9854 find_array_class_cache_next_victim_ = 0;
9855 }
9856
VisitClassLoaders(ClassLoaderVisitor * visitor) const9857 void ClassLinker::VisitClassLoaders(ClassLoaderVisitor* visitor) const {
9858 Thread* const self = Thread::Current();
9859 for (const ClassLoaderData& data : class_loaders_) {
9860 // Need to use DecodeJObject so that we get null for cleared JNI weak globals.
9861 ObjPtr<mirror::ClassLoader> class_loader = ObjPtr<mirror::ClassLoader>::DownCast(
9862 self->DecodeJObject(data.weak_root));
9863 if (class_loader != nullptr) {
9864 visitor->Visit(class_loader);
9865 }
9866 }
9867 }
9868
VisitAllocators(AllocatorVisitor * visitor) const9869 void ClassLinker::VisitAllocators(AllocatorVisitor* visitor) const {
9870 for (const ClassLoaderData& data : class_loaders_) {
9871 LinearAlloc* alloc = data.allocator;
9872 if (alloc != nullptr && !visitor->Visit(alloc)) {
9873 break;
9874 }
9875 }
9876 }
9877
InsertDexFileInToClassLoader(ObjPtr<mirror::Object> dex_file,ObjPtr<mirror::ClassLoader> class_loader)9878 void ClassLinker::InsertDexFileInToClassLoader(ObjPtr<mirror::Object> dex_file,
9879 ObjPtr<mirror::ClassLoader> class_loader) {
9880 DCHECK(dex_file != nullptr);
9881 Thread* const self = Thread::Current();
9882 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
9883 ClassTable* const table = ClassTableForClassLoader(class_loader);
9884 DCHECK(table != nullptr);
9885 if (table->InsertStrongRoot(dex_file) && class_loader != nullptr) {
9886 // It was not already inserted, perform the write barrier to let the GC know the class loader's
9887 // class table was modified.
9888 WriteBarrier::ForEveryFieldWrite(class_loader);
9889 }
9890 }
9891
CleanupClassLoaders()9892 void ClassLinker::CleanupClassLoaders() {
9893 Thread* const self = Thread::Current();
9894 std::vector<ClassLoaderData> to_delete;
9895 // Do the delete outside the lock to avoid lock violation in jit code cache.
9896 {
9897 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
9898 for (auto it = class_loaders_.begin(); it != class_loaders_.end(); ) {
9899 const ClassLoaderData& data = *it;
9900 // Need to use DecodeJObject so that we get null for cleared JNI weak globals.
9901 ObjPtr<mirror::ClassLoader> class_loader =
9902 ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(data.weak_root));
9903 if (class_loader != nullptr) {
9904 ++it;
9905 } else {
9906 VLOG(class_linker) << "Freeing class loader";
9907 to_delete.push_back(data);
9908 it = class_loaders_.erase(it);
9909 }
9910 }
9911 }
9912 for (ClassLoaderData& data : to_delete) {
9913 // CHA unloading analysis and SingleImplementaion cleanups are required.
9914 DeleteClassLoader(self, data, /*cleanup_cha=*/ true);
9915 }
9916 }
9917
9918 class ClassLinker::FindVirtualMethodHolderVisitor : public ClassVisitor {
9919 public:
FindVirtualMethodHolderVisitor(const ArtMethod * method,PointerSize pointer_size)9920 FindVirtualMethodHolderVisitor(const ArtMethod* method, PointerSize pointer_size)
9921 : method_(method),
9922 pointer_size_(pointer_size) {}
9923
operator ()(ObjPtr<mirror::Class> klass)9924 bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) override {
9925 if (klass->GetVirtualMethodsSliceUnchecked(pointer_size_).Contains(method_)) {
9926 holder_ = klass;
9927 }
9928 // Return false to stop searching if holder_ is not null.
9929 return holder_ == nullptr;
9930 }
9931
9932 ObjPtr<mirror::Class> holder_ = nullptr;
9933 const ArtMethod* const method_;
9934 const PointerSize pointer_size_;
9935 };
9936
GetHoldingClassOfCopiedMethod(ArtMethod * method)9937 ObjPtr<mirror::Class> ClassLinker::GetHoldingClassOfCopiedMethod(ArtMethod* method) {
9938 ScopedTrace trace(__FUNCTION__); // Since this function is slow, have a trace to notify people.
9939 CHECK(method->IsCopied());
9940 FindVirtualMethodHolderVisitor visitor(method, image_pointer_size_);
9941 VisitClasses(&visitor);
9942 return visitor.holder_;
9943 }
9944
AllocIfTable(Thread * self,size_t ifcount)9945 ObjPtr<mirror::IfTable> ClassLinker::AllocIfTable(Thread* self, size_t ifcount) {
9946 return ObjPtr<mirror::IfTable>::DownCast(ObjPtr<mirror::ObjectArray<mirror::Object>>(
9947 mirror::IfTable::Alloc(self,
9948 GetClassRoot<mirror::ObjectArray<mirror::Object>>(this),
9949 ifcount * mirror::IfTable::kMax)));
9950 }
9951
IsUpdatableBootClassPathDescriptor(const char * descriptor ATTRIBUTE_UNUSED)9952 bool ClassLinker::IsUpdatableBootClassPathDescriptor(const char* descriptor ATTRIBUTE_UNUSED) {
9953 // Should not be called on ClassLinker, only on AotClassLinker that overrides this.
9954 LOG(FATAL) << "UNREACHABLE";
9955 UNREACHABLE();
9956 }
9957
9958 // Instantiate ClassLinker::ResolveMethod.
9959 template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
9960 uint32_t method_idx,
9961 Handle<mirror::DexCache> dex_cache,
9962 Handle<mirror::ClassLoader> class_loader,
9963 ArtMethod* referrer,
9964 InvokeType type);
9965 template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
9966 uint32_t method_idx,
9967 Handle<mirror::DexCache> dex_cache,
9968 Handle<mirror::ClassLoader> class_loader,
9969 ArtMethod* referrer,
9970 InvokeType type);
9971
9972 // Instantiate ClassLinker::AllocClass.
9973 template ObjPtr<mirror::Class> ClassLinker::AllocClass</* kMovable= */ true>(
9974 Thread* self,
9975 ObjPtr<mirror::Class> java_lang_Class,
9976 uint32_t class_size);
9977 template ObjPtr<mirror::Class> ClassLinker::AllocClass</* kMovable= */ false>(
9978 Thread* self,
9979 ObjPtr<mirror::Class> java_lang_Class,
9980 uint32_t class_size);
9981
9982 } // namespace art
9983