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.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 "jit/debugger_interface.h"
92 #include "jit/jit.h"
93 #include "jit/jit_code_cache.h"
94 #include "jni/java_vm_ext.h"
95 #include "jni/jni_internal.h"
96 #include "linear_alloc.h"
97 #include "mirror/array-alloc-inl.h"
98 #include "mirror/array-inl.h"
99 #include "mirror/call_site.h"
100 #include "mirror/class-alloc-inl.h"
101 #include "mirror/class-inl.h"
102 #include "mirror/class.h"
103 #include "mirror/class_ext.h"
104 #include "mirror/class_loader.h"
105 #include "mirror/dex_cache-inl.h"
106 #include "mirror/dex_cache.h"
107 #include "mirror/emulated_stack_frame.h"
108 #include "mirror/field.h"
109 #include "mirror/iftable-inl.h"
110 #include "mirror/method.h"
111 #include "mirror/method_handle_impl.h"
112 #include "mirror/method_handles_lookup.h"
113 #include "mirror/method_type.h"
114 #include "mirror/object-inl.h"
115 #include "mirror/object-refvisitor-inl.h"
116 #include "mirror/object.h"
117 #include "mirror/object_array-alloc-inl.h"
118 #include "mirror/object_array-inl.h"
119 #include "mirror/object_array.h"
120 #include "mirror/object_reference.h"
121 #include "mirror/object_reference-inl.h"
122 #include "mirror/proxy.h"
123 #include "mirror/reference-inl.h"
124 #include "mirror/stack_trace_element.h"
125 #include "mirror/string-inl.h"
126 #include "mirror/throwable.h"
127 #include "mirror/var_handle.h"
128 #include "native/dalvik_system_DexFile.h"
129 #include "nativehelper/scoped_local_ref.h"
130 #include "oat.h"
131 #include "oat_file-inl.h"
132 #include "oat_file.h"
133 #include "oat_file_assistant.h"
134 #include "oat_file_manager.h"
135 #include "object_lock.h"
136 #include "profile/profile_compilation_info.h"
137 #include "runtime.h"
138 #include "runtime_callbacks.h"
139 #include "scoped_thread_state_change-inl.h"
140 #include "thread-inl.h"
141 #include "thread.h"
142 #include "thread_list.h"
143 #include "trace.h"
144 #include "transaction.h"
145 #include "utils/dex_cache_arrays_layout-inl.h"
146 #include "verifier/class_verifier.h"
147 #include "well_known_classes.h"
148
149 #include "interpreter/interpreter_mterp_impl.h"
150
151 namespace art {
152
153 using android::base::StringPrintf;
154
155 static constexpr bool kSanityCheckObjects = kIsDebugBuild;
156 static constexpr bool kVerifyArtMethodDeclaringClasses = kIsDebugBuild;
157
158 static void ThrowNoClassDefFoundError(const char* fmt, ...)
159 __attribute__((__format__(__printf__, 1, 2)))
160 REQUIRES_SHARED(Locks::mutator_lock_);
ThrowNoClassDefFoundError(const char * fmt,...)161 static void ThrowNoClassDefFoundError(const char* fmt, ...) {
162 va_list args;
163 va_start(args, fmt);
164 Thread* self = Thread::Current();
165 self->ThrowNewExceptionV("Ljava/lang/NoClassDefFoundError;", fmt, args);
166 va_end(args);
167 }
168
HasInitWithString(Thread * self,ClassLinker * class_linker,const char * descriptor)169 static bool HasInitWithString(Thread* self, ClassLinker* class_linker, const char* descriptor)
170 REQUIRES_SHARED(Locks::mutator_lock_) {
171 ArtMethod* method = self->GetCurrentMethod(nullptr);
172 StackHandleScope<1> hs(self);
173 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(method != nullptr ?
174 method->GetDeclaringClass()->GetClassLoader() : nullptr));
175 ObjPtr<mirror::Class> exception_class = class_linker->FindClass(self, descriptor, class_loader);
176
177 if (exception_class == nullptr) {
178 // No exc class ~ no <init>-with-string.
179 CHECK(self->IsExceptionPending());
180 self->ClearException();
181 return false;
182 }
183
184 ArtMethod* exception_init_method = exception_class->FindConstructor(
185 "(Ljava/lang/String;)V", class_linker->GetImagePointerSize());
186 return exception_init_method != nullptr;
187 }
188
GetVerifyError(ObjPtr<mirror::Class> c)189 static ObjPtr<mirror::Object> GetVerifyError(ObjPtr<mirror::Class> c)
190 REQUIRES_SHARED(Locks::mutator_lock_) {
191 ObjPtr<mirror::ClassExt> ext(c->GetExtData());
192 if (ext == nullptr) {
193 return nullptr;
194 } else {
195 return ext->GetVerifyError();
196 }
197 }
198
199 // Helper for ThrowEarlierClassFailure. Throws the stored error.
HandleEarlierVerifyError(Thread * self,ClassLinker * class_linker,ObjPtr<mirror::Class> c)200 static void HandleEarlierVerifyError(Thread* self,
201 ClassLinker* class_linker,
202 ObjPtr<mirror::Class> c)
203 REQUIRES_SHARED(Locks::mutator_lock_) {
204 ObjPtr<mirror::Object> obj = GetVerifyError(c);
205 DCHECK(obj != nullptr);
206 self->AssertNoPendingException();
207 if (obj->IsClass()) {
208 // Previous error has been stored as class. Create a new exception of that type.
209
210 // It's possible the exception doesn't have a <init>(String).
211 std::string temp;
212 const char* descriptor = obj->AsClass()->GetDescriptor(&temp);
213
214 if (HasInitWithString(self, class_linker, descriptor)) {
215 self->ThrowNewException(descriptor, c->PrettyDescriptor().c_str());
216 } else {
217 self->ThrowNewException(descriptor, nullptr);
218 }
219 } else {
220 // Previous error has been stored as an instance. Just rethrow.
221 ObjPtr<mirror::Class> throwable_class = GetClassRoot<mirror::Throwable>(class_linker);
222 ObjPtr<mirror::Class> error_class = obj->GetClass();
223 CHECK(throwable_class->IsAssignableFrom(error_class));
224 self->SetException(obj->AsThrowable());
225 }
226 self->AssertPendingException();
227 }
228
229 // Ensures that methods have the kAccSkipAccessChecks bit set. We use the
230 // kAccVerificationAttempted bit on the class access flags to determine whether this has been done
231 // before.
EnsureSkipAccessChecksMethods(Handle<mirror::Class> klass,PointerSize pointer_size)232 static void EnsureSkipAccessChecksMethods(Handle<mirror::Class> klass, PointerSize pointer_size)
233 REQUIRES_SHARED(Locks::mutator_lock_) {
234 Runtime* runtime = Runtime::Current();
235 ClassLinker* class_linker = runtime->GetClassLinker();
236 if (!klass->WasVerificationAttempted()) {
237 klass->SetSkipAccessChecksFlagOnAllMethods(pointer_size);
238 klass->SetVerificationAttempted();
239 // Now that the class has passed verification, try to set nterp entrypoints
240 // to methods that currently use the switch interpreter.
241 if (interpreter::CanRuntimeUseNterp()) {
242 for (ArtMethod& m : klass->GetMethods(pointer_size)) {
243 if (class_linker->IsQuickToInterpreterBridge(m.GetEntryPointFromQuickCompiledCode()) &&
244 interpreter::CanMethodUseNterp(&m)) {
245 if (klass->IsVisiblyInitialized() || !NeedsClinitCheckBeforeCall(&m)) {
246 runtime->GetInstrumentation()->UpdateMethodsCode(&m, interpreter::GetNterpEntryPoint());
247 } else {
248 // Put the resolution stub, which will initialize the class and then
249 // call the method with nterp.
250 runtime->GetInstrumentation()->UpdateMethodsCode(&m, GetQuickResolutionStub());
251 }
252 }
253 }
254 }
255 }
256 }
257
258 // Callback responsible for making a batch of classes visibly initialized
259 // after all threads have called it from a checkpoint, ensuring visibility.
260 class ClassLinker::VisiblyInitializedCallback final
261 : public Closure, public IntrusiveForwardListNode<VisiblyInitializedCallback> {
262 public:
VisiblyInitializedCallback(ClassLinker * class_linker)263 explicit VisiblyInitializedCallback(ClassLinker* class_linker)
264 : class_linker_(class_linker),
265 num_classes_(0u),
266 thread_visibility_counter_(0),
267 barriers_() {
268 std::fill_n(classes_, kMaxClasses, nullptr);
269 }
270
IsEmpty() const271 bool IsEmpty() const {
272 DCHECK_LE(num_classes_, kMaxClasses);
273 return num_classes_ == 0u;
274 }
275
IsFull() const276 bool IsFull() const {
277 DCHECK_LE(num_classes_, kMaxClasses);
278 return num_classes_ == kMaxClasses;
279 }
280
AddClass(Thread * self,ObjPtr<mirror::Class> klass)281 void AddClass(Thread* self, ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) {
282 DCHECK_EQ(klass->GetStatus(), ClassStatus::kInitialized);
283 DCHECK(!IsFull());
284 classes_[num_classes_] = self->GetJniEnv()->GetVm()->AddWeakGlobalRef(self, klass);
285 ++num_classes_;
286 }
287
AddBarrier(Barrier * barrier)288 void AddBarrier(Barrier* barrier) {
289 barriers_.push_front(barrier);
290 }
291
GetAndClearBarriers()292 std::forward_list<Barrier*> GetAndClearBarriers() {
293 std::forward_list<Barrier*> result;
294 result.swap(barriers_);
295 result.reverse(); // Return barriers in insertion order.
296 return result;
297 }
298
MakeVisible(Thread * self)299 void MakeVisible(Thread* self) {
300 DCHECK_EQ(thread_visibility_counter_.load(std::memory_order_relaxed), 0);
301 size_t count = Runtime::Current()->GetThreadList()->RunCheckpoint(this);
302 AdjustThreadVisibilityCounter(self, count);
303 }
304
Run(Thread * self)305 void Run(Thread* self) override {
306 self->ClearMakeVisiblyInitializedCounter();
307 AdjustThreadVisibilityCounter(self, -1);
308 }
309
310 private:
AdjustThreadVisibilityCounter(Thread * self,ssize_t adjustment)311 void AdjustThreadVisibilityCounter(Thread* self, ssize_t adjustment) {
312 ssize_t old = thread_visibility_counter_.fetch_add(adjustment, std::memory_order_relaxed);
313 if (old + adjustment == 0) {
314 // All threads passed the checkpoint. Mark classes as visibly initialized.
315 {
316 ScopedObjectAccess soa(self);
317 StackHandleScope<1u> hs(self);
318 MutableHandle<mirror::Class> klass = hs.NewHandle<mirror::Class>(nullptr);
319 JavaVMExt* vm = self->GetJniEnv()->GetVm();
320 for (size_t i = 0, num = num_classes_; i != num; ++i) {
321 klass.Assign(ObjPtr<mirror::Class>::DownCast(self->DecodeJObject(classes_[i])));
322 vm->DeleteWeakGlobalRef(self, classes_[i]);
323 if (klass != nullptr) {
324 mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self);
325 class_linker_->FixupStaticTrampolines(klass.Get());
326 }
327 }
328 num_classes_ = 0u;
329 }
330 class_linker_->VisiblyInitializedCallbackDone(self, this);
331 }
332 }
333
334 static constexpr size_t kMaxClasses = 16;
335
336 ClassLinker* const class_linker_;
337 size_t num_classes_;
338 jweak classes_[kMaxClasses];
339
340 // The thread visibility counter starts at 0 and it is incremented by the number of
341 // threads that need to run this callback (by the thread that request the callback
342 // to be run) and decremented once for each `Run()` execution. When it reaches 0,
343 // whether after the increment or after a decrement, we know that `Run()` was executed
344 // for all threads and therefore we can mark the classes as visibly initialized.
345 std::atomic<ssize_t> thread_visibility_counter_;
346
347 // List of barries to `Pass()` for threads that wait for the callback to complete.
348 std::forward_list<Barrier*> barriers_;
349 };
350
MakeInitializedClassesVisiblyInitialized(Thread * self,bool wait)351 void ClassLinker::MakeInitializedClassesVisiblyInitialized(Thread* self, bool wait) {
352 if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) {
353 return; // Nothing to do. Thanks to the x86 memory model classes skip the initialized status.
354 }
355 std::optional<Barrier> maybe_barrier; // Avoid constructing the Barrier for `wait == false`.
356 if (wait) {
357 maybe_barrier.emplace(0);
358 }
359 int wait_count = 0;
360 VisiblyInitializedCallback* callback = nullptr;
361 {
362 MutexLock lock(self, visibly_initialized_callback_lock_);
363 if (visibly_initialized_callback_ != nullptr && !visibly_initialized_callback_->IsEmpty()) {
364 callback = visibly_initialized_callback_.release();
365 running_visibly_initialized_callbacks_.push_front(*callback);
366 }
367 if (wait) {
368 DCHECK(maybe_barrier.has_value());
369 Barrier* barrier = std::addressof(*maybe_barrier);
370 for (VisiblyInitializedCallback& cb : running_visibly_initialized_callbacks_) {
371 cb.AddBarrier(barrier);
372 ++wait_count;
373 }
374 }
375 }
376 if (callback != nullptr) {
377 callback->MakeVisible(self);
378 }
379 if (wait_count != 0) {
380 DCHECK(maybe_barrier.has_value());
381 maybe_barrier->Increment(self, wait_count);
382 }
383 }
384
VisiblyInitializedCallbackDone(Thread * self,VisiblyInitializedCallback * callback)385 void ClassLinker::VisiblyInitializedCallbackDone(Thread* self,
386 VisiblyInitializedCallback* callback) {
387 MutexLock lock(self, visibly_initialized_callback_lock_);
388 // Pass the barriers if requested.
389 for (Barrier* barrier : callback->GetAndClearBarriers()) {
390 barrier->Pass(self);
391 }
392 // Remove the callback from the list of running callbacks.
393 auto before = running_visibly_initialized_callbacks_.before_begin();
394 auto it = running_visibly_initialized_callbacks_.begin();
395 DCHECK(it != running_visibly_initialized_callbacks_.end());
396 while (std::addressof(*it) != callback) {
397 before = it;
398 ++it;
399 DCHECK(it != running_visibly_initialized_callbacks_.end());
400 }
401 running_visibly_initialized_callbacks_.erase_after(before);
402 // Reuse or destroy the callback object.
403 if (visibly_initialized_callback_ == nullptr) {
404 visibly_initialized_callback_.reset(callback);
405 } else {
406 delete callback;
407 }
408 }
409
ForceClassInitialized(Thread * self,Handle<mirror::Class> klass)410 void ClassLinker::ForceClassInitialized(Thread* self, Handle<mirror::Class> klass) {
411 ClassLinker::VisiblyInitializedCallback* cb = MarkClassInitialized(self, klass);
412 if (cb != nullptr) {
413 cb->MakeVisible(self);
414 }
415 ScopedThreadSuspension sts(self, ThreadState::kSuspended);
416 MakeInitializedClassesVisiblyInitialized(self, /*wait=*/true);
417 }
418
MarkClassInitialized(Thread * self,Handle<mirror::Class> klass)419 ClassLinker::VisiblyInitializedCallback* ClassLinker::MarkClassInitialized(
420 Thread* self, Handle<mirror::Class> klass) {
421 if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) {
422 // Thanks to the x86 memory model, we do not need any memory fences and
423 // we can immediately mark the class as visibly initialized.
424 mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self);
425 FixupStaticTrampolines(klass.Get());
426 return nullptr;
427 }
428 if (Runtime::Current()->IsActiveTransaction()) {
429 // Transactions are single-threaded, so we can mark the class as visibly intialized.
430 // (Otherwise we'd need to track the callback's entry in the transaction for rollback.)
431 mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self);
432 FixupStaticTrampolines(klass.Get());
433 return nullptr;
434 }
435 mirror::Class::SetStatus(klass, ClassStatus::kInitialized, self);
436 MutexLock lock(self, visibly_initialized_callback_lock_);
437 if (visibly_initialized_callback_ == nullptr) {
438 visibly_initialized_callback_.reset(new VisiblyInitializedCallback(this));
439 }
440 DCHECK(!visibly_initialized_callback_->IsFull());
441 visibly_initialized_callback_->AddClass(self, klass.Get());
442
443 if (visibly_initialized_callback_->IsFull()) {
444 VisiblyInitializedCallback* callback = visibly_initialized_callback_.release();
445 running_visibly_initialized_callbacks_.push_front(*callback);
446 return callback;
447 } else {
448 return nullptr;
449 }
450 }
451
ThrowEarlierClassFailure(ObjPtr<mirror::Class> c,bool wrap_in_no_class_def,bool log)452 void ClassLinker::ThrowEarlierClassFailure(ObjPtr<mirror::Class> c,
453 bool wrap_in_no_class_def,
454 bool log) {
455 // The class failed to initialize on a previous attempt, so we want to throw
456 // a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we
457 // failed in verification, in which case v2 5.4.1 says we need to re-throw
458 // the previous error.
459 Runtime* const runtime = Runtime::Current();
460 if (!runtime->IsAotCompiler()) { // Give info if this occurs at runtime.
461 std::string extra;
462 ObjPtr<mirror::Object> verify_error = GetVerifyError(c);
463 if (verify_error != nullptr) {
464 if (verify_error->IsClass()) {
465 extra = mirror::Class::PrettyDescriptor(verify_error->AsClass());
466 } else {
467 extra = verify_error->AsThrowable()->Dump();
468 }
469 }
470 if (log) {
471 LOG(INFO) << "Rejecting re-init on previously-failed class " << c->PrettyClass()
472 << ": " << extra;
473 }
474 }
475
476 CHECK(c->IsErroneous()) << c->PrettyClass() << " " << c->GetStatus();
477 Thread* self = Thread::Current();
478 if (runtime->IsAotCompiler()) {
479 // At compile time, accurate errors and NCDFE are disabled to speed compilation.
480 ObjPtr<mirror::Throwable> pre_allocated = runtime->GetPreAllocatedNoClassDefFoundError();
481 self->SetException(pre_allocated);
482 } else {
483 ObjPtr<mirror::Object> verify_error = GetVerifyError(c);
484 if (verify_error != nullptr) {
485 // Rethrow stored error.
486 HandleEarlierVerifyError(self, this, c);
487 }
488 // TODO This might be wrong if we hit an OOME while allocating the ClassExt. In that case we
489 // might have meant to go down the earlier if statement with the original error but it got
490 // swallowed by the OOM so we end up here.
491 if (verify_error == nullptr || wrap_in_no_class_def) {
492 // If there isn't a recorded earlier error, or this is a repeat throw from initialization,
493 // the top-level exception must be a NoClassDefFoundError. The potentially already pending
494 // exception will be a cause.
495 self->ThrowNewWrappedException("Ljava/lang/NoClassDefFoundError;",
496 c->PrettyDescriptor().c_str());
497 }
498 }
499 }
500
VlogClassInitializationFailure(Handle<mirror::Class> klass)501 static void VlogClassInitializationFailure(Handle<mirror::Class> klass)
502 REQUIRES_SHARED(Locks::mutator_lock_) {
503 if (VLOG_IS_ON(class_linker)) {
504 std::string temp;
505 LOG(INFO) << "Failed to initialize class " << klass->GetDescriptor(&temp) << " from "
506 << klass->GetLocation() << "\n" << Thread::Current()->GetException()->Dump();
507 }
508 }
509
WrapExceptionInInitializer(Handle<mirror::Class> klass)510 static void WrapExceptionInInitializer(Handle<mirror::Class> klass)
511 REQUIRES_SHARED(Locks::mutator_lock_) {
512 Thread* self = Thread::Current();
513 JNIEnv* env = self->GetJniEnv();
514
515 ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred());
516 CHECK(cause.get() != nullptr);
517
518 // Boot classpath classes should not fail initialization. This is a sanity debug check. This
519 // cannot in general be guaranteed, but in all likelihood leads to breakage down the line.
520 if (klass->GetClassLoader() == nullptr && !Runtime::Current()->IsAotCompiler()) {
521 std::string tmp;
522 // We want to LOG(FATAL) on debug builds since this really shouldn't be happening but we need to
523 // make sure to only do it if we don't have AsyncExceptions being thrown around since those
524 // could have caused the error.
525 bool known_impossible = kIsDebugBuild && !Runtime::Current()->AreAsyncExceptionsThrown();
526 LOG(known_impossible ? FATAL : WARNING) << klass->GetDescriptor(&tmp)
527 << " failed initialization: "
528 << self->GetException()->Dump();
529 }
530
531 env->ExceptionClear();
532 bool is_error = env->IsInstanceOf(cause.get(), WellKnownClasses::java_lang_Error);
533 env->Throw(cause.get());
534
535 // We only wrap non-Error exceptions; an Error can just be used as-is.
536 if (!is_error) {
537 self->ThrowNewWrappedException("Ljava/lang/ExceptionInInitializerError;", nullptr);
538 }
539 VlogClassInitializationFailure(klass);
540 }
541
542 // Gap between two fields in object layout.
543 struct FieldGap {
544 uint32_t start_offset; // The offset from the start of the object.
545 uint32_t size; // The gap size of 1, 2, or 4 bytes.
546 };
547 struct FieldGapsComparator {
FieldGapsComparatorart::FieldGapsComparator548 FieldGapsComparator() {
549 }
operator ()art::FieldGapsComparator550 bool operator() (const FieldGap& lhs, const FieldGap& rhs)
551 NO_THREAD_SAFETY_ANALYSIS {
552 // Sort by gap size, largest first. Secondary sort by starting offset.
553 // Note that the priority queue returns the largest element, so operator()
554 // should return true if lhs is less than rhs.
555 return lhs.size < rhs.size || (lhs.size == rhs.size && lhs.start_offset > rhs.start_offset);
556 }
557 };
558 using FieldGaps = std::priority_queue<FieldGap, std::vector<FieldGap>, FieldGapsComparator>;
559
560 // Adds largest aligned gaps to queue of gaps.
AddFieldGap(uint32_t gap_start,uint32_t gap_end,FieldGaps * gaps)561 static void AddFieldGap(uint32_t gap_start, uint32_t gap_end, FieldGaps* gaps) {
562 DCHECK(gaps != nullptr);
563
564 uint32_t current_offset = gap_start;
565 while (current_offset != gap_end) {
566 size_t remaining = gap_end - current_offset;
567 if (remaining >= sizeof(uint32_t) && IsAligned<4>(current_offset)) {
568 gaps->push(FieldGap {current_offset, sizeof(uint32_t)});
569 current_offset += sizeof(uint32_t);
570 } else if (remaining >= sizeof(uint16_t) && IsAligned<2>(current_offset)) {
571 gaps->push(FieldGap {current_offset, sizeof(uint16_t)});
572 current_offset += sizeof(uint16_t);
573 } else {
574 gaps->push(FieldGap {current_offset, sizeof(uint8_t)});
575 current_offset += sizeof(uint8_t);
576 }
577 DCHECK_LE(current_offset, gap_end) << "Overran gap";
578 }
579 }
580 // Shuffle fields forward, making use of gaps whenever possible.
581 template<int n>
ShuffleForward(size_t * current_field_idx,MemberOffset * field_offset,std::deque<ArtField * > * grouped_and_sorted_fields,FieldGaps * gaps)582 static void ShuffleForward(size_t* current_field_idx,
583 MemberOffset* field_offset,
584 std::deque<ArtField*>* grouped_and_sorted_fields,
585 FieldGaps* gaps)
586 REQUIRES_SHARED(Locks::mutator_lock_) {
587 DCHECK(current_field_idx != nullptr);
588 DCHECK(grouped_and_sorted_fields != nullptr);
589 DCHECK(gaps != nullptr);
590 DCHECK(field_offset != nullptr);
591
592 DCHECK(IsPowerOfTwo(n));
593 while (!grouped_and_sorted_fields->empty()) {
594 ArtField* field = grouped_and_sorted_fields->front();
595 Primitive::Type type = field->GetTypeAsPrimitiveType();
596 if (Primitive::ComponentSize(type) < n) {
597 break;
598 }
599 if (!IsAligned<n>(field_offset->Uint32Value())) {
600 MemberOffset old_offset = *field_offset;
601 *field_offset = MemberOffset(RoundUp(field_offset->Uint32Value(), n));
602 AddFieldGap(old_offset.Uint32Value(), field_offset->Uint32Value(), gaps);
603 }
604 CHECK(type != Primitive::kPrimNot) << field->PrettyField(); // should be primitive types
605 grouped_and_sorted_fields->pop_front();
606 if (!gaps->empty() && gaps->top().size >= n) {
607 FieldGap gap = gaps->top();
608 gaps->pop();
609 DCHECK_ALIGNED(gap.start_offset, n);
610 field->SetOffset(MemberOffset(gap.start_offset));
611 if (gap.size > n) {
612 AddFieldGap(gap.start_offset + n, gap.start_offset + gap.size, gaps);
613 }
614 } else {
615 DCHECK_ALIGNED(field_offset->Uint32Value(), n);
616 field->SetOffset(*field_offset);
617 *field_offset = MemberOffset(field_offset->Uint32Value() + n);
618 }
619 ++(*current_field_idx);
620 }
621 }
622
ClassLinker(InternTable * intern_table,bool fast_class_not_found_exceptions)623 ClassLinker::ClassLinker(InternTable* intern_table, bool fast_class_not_found_exceptions)
624 : boot_class_table_(new ClassTable()),
625 failed_dex_cache_class_lookups_(0),
626 class_roots_(nullptr),
627 find_array_class_cache_next_victim_(0),
628 init_done_(false),
629 log_new_roots_(false),
630 intern_table_(intern_table),
631 fast_class_not_found_exceptions_(fast_class_not_found_exceptions),
632 jni_dlsym_lookup_trampoline_(nullptr),
633 jni_dlsym_lookup_critical_trampoline_(nullptr),
634 quick_resolution_trampoline_(nullptr),
635 quick_imt_conflict_trampoline_(nullptr),
636 quick_generic_jni_trampoline_(nullptr),
637 quick_to_interpreter_bridge_trampoline_(nullptr),
638 image_pointer_size_(kRuntimePointerSize),
639 visibly_initialized_callback_lock_("visibly initialized callback lock"),
640 visibly_initialized_callback_(nullptr),
641 cha_(Runtime::Current()->IsAotCompiler() ? nullptr : new ClassHierarchyAnalysis()) {
642 // For CHA disabled during Aot, see b/34193647.
643
644 CHECK(intern_table_ != nullptr);
645 static_assert(kFindArrayCacheSize == arraysize(find_array_class_cache_),
646 "Array cache size wrong.");
647 std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr));
648 }
649
CheckSystemClass(Thread * self,Handle<mirror::Class> c1,const char * descriptor)650 void ClassLinker::CheckSystemClass(Thread* self, Handle<mirror::Class> c1, const char* descriptor) {
651 ObjPtr<mirror::Class> c2 = FindSystemClass(self, descriptor);
652 if (c2 == nullptr) {
653 LOG(FATAL) << "Could not find class " << descriptor;
654 UNREACHABLE();
655 }
656 if (c1.Get() != c2) {
657 std::ostringstream os1, os2;
658 c1->DumpClass(os1, mirror::Class::kDumpClassFullDetail);
659 c2->DumpClass(os2, mirror::Class::kDumpClassFullDetail);
660 LOG(FATAL) << "InitWithoutImage: Class mismatch for " << descriptor
661 << ". This is most likely the result of a broken build. Make sure that "
662 << "libcore and art projects match.\n\n"
663 << os1.str() << "\n\n" << os2.str();
664 UNREACHABLE();
665 }
666 }
667
InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path,std::string * error_msg)668 bool ClassLinker::InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path,
669 std::string* error_msg) {
670 VLOG(startup) << "ClassLinker::Init";
671
672 Thread* const self = Thread::Current();
673 Runtime* const runtime = Runtime::Current();
674 gc::Heap* const heap = runtime->GetHeap();
675
676 CHECK(!heap->HasBootImageSpace()) << "Runtime has image. We should use it.";
677 CHECK(!init_done_);
678
679 // Use the pointer size from the runtime since we are probably creating the image.
680 image_pointer_size_ = InstructionSetPointerSize(runtime->GetInstructionSet());
681
682 // java_lang_Class comes first, it's needed for AllocClass
683 // The GC can't handle an object with a null class since we can't get the size of this object.
684 heap->IncrementDisableMovingGC(self);
685 StackHandleScope<64> hs(self); // 64 is picked arbitrarily.
686 auto class_class_size = mirror::Class::ClassClassSize(image_pointer_size_);
687 // Allocate the object as non-movable so that there are no cases where Object::IsClass returns
688 // the incorrect result when comparing to-space vs from-space.
689 Handle<mirror::Class> java_lang_Class(hs.NewHandle(ObjPtr<mirror::Class>::DownCast(
690 heap->AllocNonMovableObject(self, nullptr, class_class_size, VoidFunctor()))));
691 CHECK(java_lang_Class != nullptr);
692 java_lang_Class->SetClassFlags(mirror::kClassFlagClass);
693 java_lang_Class->SetClass(java_lang_Class.Get());
694 if (kUseBakerReadBarrier) {
695 java_lang_Class->AssertReadBarrierState();
696 }
697 java_lang_Class->SetClassSize(class_class_size);
698 java_lang_Class->SetPrimitiveType(Primitive::kPrimNot);
699 heap->DecrementDisableMovingGC(self);
700 // AllocClass(ObjPtr<mirror::Class>) can now be used
701
702 // Class[] is used for reflection support.
703 auto class_array_class_size = mirror::ObjectArray<mirror::Class>::ClassSize(image_pointer_size_);
704 Handle<mirror::Class> class_array_class(hs.NewHandle(
705 AllocClass(self, java_lang_Class.Get(), class_array_class_size)));
706 class_array_class->SetComponentType(java_lang_Class.Get());
707
708 // java_lang_Object comes next so that object_array_class can be created.
709 Handle<mirror::Class> java_lang_Object(hs.NewHandle(
710 AllocClass(self, java_lang_Class.Get(), mirror::Object::ClassSize(image_pointer_size_))));
711 CHECK(java_lang_Object != nullptr);
712 // backfill Object as the super class of Class.
713 java_lang_Class->SetSuperClass(java_lang_Object.Get());
714 mirror::Class::SetStatus(java_lang_Object, ClassStatus::kLoaded, self);
715
716 java_lang_Object->SetObjectSize(sizeof(mirror::Object));
717 // Allocate in non-movable so that it's possible to check if a JNI weak global ref has been
718 // cleared without triggering the read barrier and unintentionally mark the sentinel alive.
719 runtime->SetSentinel(heap->AllocNonMovableObject(self,
720 java_lang_Object.Get(),
721 java_lang_Object->GetObjectSize(),
722 VoidFunctor()));
723
724 // Initialize the SubtypeCheck bitstring for java.lang.Object and java.lang.Class.
725 if (kBitstringSubtypeCheckEnabled) {
726 // It might seem the lock here is unnecessary, however all the SubtypeCheck
727 // functions are annotated to require locks all the way down.
728 //
729 // We take the lock here to avoid using NO_THREAD_SAFETY_ANALYSIS.
730 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
731 SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(java_lang_Object.Get());
732 SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(java_lang_Class.Get());
733 }
734
735 // Object[] next to hold class roots.
736 Handle<mirror::Class> object_array_class(hs.NewHandle(
737 AllocClass(self, java_lang_Class.Get(),
738 mirror::ObjectArray<mirror::Object>::ClassSize(image_pointer_size_))));
739 object_array_class->SetComponentType(java_lang_Object.Get());
740
741 // Setup java.lang.String.
742 //
743 // We make this class non-movable for the unlikely case where it were to be
744 // moved by a sticky-bit (minor) collection when using the Generational
745 // Concurrent Copying (CC) collector, potentially creating a stale reference
746 // in the `klass_` field of one of its instances allocated in the Large-Object
747 // Space (LOS) -- see the comment about the dirty card scanning logic in
748 // art::gc::collector::ConcurrentCopying::MarkingPhase.
749 Handle<mirror::Class> java_lang_String(hs.NewHandle(
750 AllocClass</* kMovable= */ false>(
751 self, java_lang_Class.Get(), mirror::String::ClassSize(image_pointer_size_))));
752 java_lang_String->SetStringClass();
753 mirror::Class::SetStatus(java_lang_String, ClassStatus::kResolved, self);
754
755 // Setup java.lang.ref.Reference.
756 Handle<mirror::Class> java_lang_ref_Reference(hs.NewHandle(
757 AllocClass(self, java_lang_Class.Get(), mirror::Reference::ClassSize(image_pointer_size_))));
758 java_lang_ref_Reference->SetObjectSize(mirror::Reference::InstanceSize());
759 mirror::Class::SetStatus(java_lang_ref_Reference, ClassStatus::kResolved, self);
760
761 // Create storage for root classes, save away our work so far (requires descriptors).
762 class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>(
763 mirror::ObjectArray<mirror::Class>::Alloc(self,
764 object_array_class.Get(),
765 static_cast<int32_t>(ClassRoot::kMax)));
766 CHECK(!class_roots_.IsNull());
767 SetClassRoot(ClassRoot::kJavaLangClass, java_lang_Class.Get());
768 SetClassRoot(ClassRoot::kJavaLangObject, java_lang_Object.Get());
769 SetClassRoot(ClassRoot::kClassArrayClass, class_array_class.Get());
770 SetClassRoot(ClassRoot::kObjectArrayClass, object_array_class.Get());
771 SetClassRoot(ClassRoot::kJavaLangString, java_lang_String.Get());
772 SetClassRoot(ClassRoot::kJavaLangRefReference, java_lang_ref_Reference.Get());
773
774 // Fill in the empty iftable. Needs to be done after the kObjectArrayClass root is set.
775 java_lang_Object->SetIfTable(AllocIfTable(self, 0));
776
777 // Create array interface entries to populate once we can load system classes.
778 object_array_class->SetIfTable(AllocIfTable(self, 2));
779 DCHECK_EQ(GetArrayIfTable(), object_array_class->GetIfTable());
780
781 // Setup the primitive type classes.
782 CreatePrimitiveClass(self, Primitive::kPrimBoolean, ClassRoot::kPrimitiveBoolean);
783 CreatePrimitiveClass(self, Primitive::kPrimByte, ClassRoot::kPrimitiveByte);
784 CreatePrimitiveClass(self, Primitive::kPrimChar, ClassRoot::kPrimitiveChar);
785 CreatePrimitiveClass(self, Primitive::kPrimShort, ClassRoot::kPrimitiveShort);
786 CreatePrimitiveClass(self, Primitive::kPrimInt, ClassRoot::kPrimitiveInt);
787 CreatePrimitiveClass(self, Primitive::kPrimLong, ClassRoot::kPrimitiveLong);
788 CreatePrimitiveClass(self, Primitive::kPrimFloat, ClassRoot::kPrimitiveFloat);
789 CreatePrimitiveClass(self, Primitive::kPrimDouble, ClassRoot::kPrimitiveDouble);
790 CreatePrimitiveClass(self, Primitive::kPrimVoid, ClassRoot::kPrimitiveVoid);
791
792 // Allocate the primitive array classes. We need only the native pointer
793 // array at this point (int[] or long[], depending on architecture) but
794 // we shall perform the same setup steps for all primitive array classes.
795 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveBoolean, ClassRoot::kBooleanArrayClass);
796 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveByte, ClassRoot::kByteArrayClass);
797 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveChar, ClassRoot::kCharArrayClass);
798 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveShort, ClassRoot::kShortArrayClass);
799 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveInt, ClassRoot::kIntArrayClass);
800 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveLong, ClassRoot::kLongArrayClass);
801 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveFloat, ClassRoot::kFloatArrayClass);
802 AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveDouble, ClassRoot::kDoubleArrayClass);
803
804 // now that these are registered, we can use AllocClass() and AllocObjectArray
805
806 // Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache.
807 Handle<mirror::Class> java_lang_DexCache(hs.NewHandle(
808 AllocClass(self, java_lang_Class.Get(), mirror::DexCache::ClassSize(image_pointer_size_))));
809 SetClassRoot(ClassRoot::kJavaLangDexCache, java_lang_DexCache.Get());
810 java_lang_DexCache->SetDexCacheClass();
811 java_lang_DexCache->SetObjectSize(mirror::DexCache::InstanceSize());
812 mirror::Class::SetStatus(java_lang_DexCache, ClassStatus::kResolved, self);
813
814
815 // Setup dalvik.system.ClassExt
816 Handle<mirror::Class> dalvik_system_ClassExt(hs.NewHandle(
817 AllocClass(self, java_lang_Class.Get(), mirror::ClassExt::ClassSize(image_pointer_size_))));
818 SetClassRoot(ClassRoot::kDalvikSystemClassExt, dalvik_system_ClassExt.Get());
819 mirror::Class::SetStatus(dalvik_system_ClassExt, ClassStatus::kResolved, self);
820
821 // Set up array classes for string, field, method
822 Handle<mirror::Class> object_array_string(hs.NewHandle(
823 AllocClass(self, java_lang_Class.Get(),
824 mirror::ObjectArray<mirror::String>::ClassSize(image_pointer_size_))));
825 object_array_string->SetComponentType(java_lang_String.Get());
826 SetClassRoot(ClassRoot::kJavaLangStringArrayClass, object_array_string.Get());
827
828 LinearAlloc* linear_alloc = runtime->GetLinearAlloc();
829 // Create runtime resolution and imt conflict methods.
830 runtime->SetResolutionMethod(runtime->CreateResolutionMethod());
831 runtime->SetImtConflictMethod(runtime->CreateImtConflictMethod(linear_alloc));
832 runtime->SetImtUnimplementedMethod(runtime->CreateImtConflictMethod(linear_alloc));
833
834 // Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create
835 // DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses
836 // these roots.
837 if (boot_class_path.empty()) {
838 *error_msg = "Boot classpath is empty.";
839 return false;
840 }
841 for (auto& dex_file : boot_class_path) {
842 if (dex_file == nullptr) {
843 *error_msg = "Null dex file.";
844 return false;
845 }
846 AppendToBootClassPath(self, dex_file.get());
847 boot_dex_files_.push_back(std::move(dex_file));
848 }
849
850 // now we can use FindSystemClass
851
852 // Set up GenericJNI entrypoint. That is mainly a hack for common_compiler_test.h so that
853 // we do not need friend classes or a publicly exposed setter.
854 quick_generic_jni_trampoline_ = GetQuickGenericJniStub();
855 if (!runtime->IsAotCompiler()) {
856 // We need to set up the generic trampolines since we don't have an image.
857 jni_dlsym_lookup_trampoline_ = GetJniDlsymLookupStub();
858 jni_dlsym_lookup_critical_trampoline_ = GetJniDlsymLookupCriticalStub();
859 quick_resolution_trampoline_ = GetQuickResolutionStub();
860 quick_imt_conflict_trampoline_ = GetQuickImtConflictStub();
861 quick_generic_jni_trampoline_ = GetQuickGenericJniStub();
862 quick_to_interpreter_bridge_trampoline_ = GetQuickToInterpreterBridge();
863 }
864
865 // Object, String, ClassExt and DexCache need to be rerun through FindSystemClass to finish init
866 mirror::Class::SetStatus(java_lang_Object, ClassStatus::kNotReady, self);
867 CheckSystemClass(self, java_lang_Object, "Ljava/lang/Object;");
868 CHECK_EQ(java_lang_Object->GetObjectSize(), mirror::Object::InstanceSize());
869 mirror::Class::SetStatus(java_lang_String, ClassStatus::kNotReady, self);
870 CheckSystemClass(self, java_lang_String, "Ljava/lang/String;");
871 mirror::Class::SetStatus(java_lang_DexCache, ClassStatus::kNotReady, self);
872 CheckSystemClass(self, java_lang_DexCache, "Ljava/lang/DexCache;");
873 CHECK_EQ(java_lang_DexCache->GetObjectSize(), mirror::DexCache::InstanceSize());
874 mirror::Class::SetStatus(dalvik_system_ClassExt, ClassStatus::kNotReady, self);
875 CheckSystemClass(self, dalvik_system_ClassExt, "Ldalvik/system/ClassExt;");
876 CHECK_EQ(dalvik_system_ClassExt->GetObjectSize(), mirror::ClassExt::InstanceSize());
877
878 // Run Class through FindSystemClass. This initializes the dex_cache_ fields and register it
879 // in class_table_.
880 CheckSystemClass(self, java_lang_Class, "Ljava/lang/Class;");
881
882 // Setup core array classes, i.e. Object[], String[] and Class[] and primitive
883 // arrays - can't be done until Object has a vtable and component classes are loaded.
884 FinishCoreArrayClassSetup(ClassRoot::kObjectArrayClass);
885 FinishCoreArrayClassSetup(ClassRoot::kClassArrayClass);
886 FinishCoreArrayClassSetup(ClassRoot::kJavaLangStringArrayClass);
887 FinishCoreArrayClassSetup(ClassRoot::kBooleanArrayClass);
888 FinishCoreArrayClassSetup(ClassRoot::kByteArrayClass);
889 FinishCoreArrayClassSetup(ClassRoot::kCharArrayClass);
890 FinishCoreArrayClassSetup(ClassRoot::kShortArrayClass);
891 FinishCoreArrayClassSetup(ClassRoot::kIntArrayClass);
892 FinishCoreArrayClassSetup(ClassRoot::kLongArrayClass);
893 FinishCoreArrayClassSetup(ClassRoot::kFloatArrayClass);
894 FinishCoreArrayClassSetup(ClassRoot::kDoubleArrayClass);
895
896 // Setup the single, global copy of "iftable".
897 auto java_lang_Cloneable = hs.NewHandle(FindSystemClass(self, "Ljava/lang/Cloneable;"));
898 CHECK(java_lang_Cloneable != nullptr);
899 auto java_io_Serializable = hs.NewHandle(FindSystemClass(self, "Ljava/io/Serializable;"));
900 CHECK(java_io_Serializable != nullptr);
901 // We assume that Cloneable/Serializable don't have superinterfaces -- normally we'd have to
902 // crawl up and explicitly list all of the supers as well.
903 object_array_class->GetIfTable()->SetInterface(0, java_lang_Cloneable.Get());
904 object_array_class->GetIfTable()->SetInterface(1, java_io_Serializable.Get());
905
906 // Sanity check Class[] and Object[]'s interfaces. GetDirectInterface may cause thread
907 // suspension.
908 CHECK_EQ(java_lang_Cloneable.Get(),
909 mirror::Class::GetDirectInterface(self, class_array_class.Get(), 0));
910 CHECK_EQ(java_io_Serializable.Get(),
911 mirror::Class::GetDirectInterface(self, class_array_class.Get(), 1));
912 CHECK_EQ(java_lang_Cloneable.Get(),
913 mirror::Class::GetDirectInterface(self, object_array_class.Get(), 0));
914 CHECK_EQ(java_io_Serializable.Get(),
915 mirror::Class::GetDirectInterface(self, object_array_class.Get(), 1));
916
917 CHECK_EQ(object_array_string.Get(),
918 FindSystemClass(self, GetClassRootDescriptor(ClassRoot::kJavaLangStringArrayClass)));
919
920 // End of special init trickery, all subsequent classes may be loaded via FindSystemClass.
921
922 // Create java.lang.reflect.Proxy root.
923 SetClassRoot(ClassRoot::kJavaLangReflectProxy,
924 FindSystemClass(self, "Ljava/lang/reflect/Proxy;"));
925
926 // Create java.lang.reflect.Field.class root.
927 ObjPtr<mirror::Class> class_root = FindSystemClass(self, "Ljava/lang/reflect/Field;");
928 CHECK(class_root != nullptr);
929 SetClassRoot(ClassRoot::kJavaLangReflectField, class_root);
930
931 // Create java.lang.reflect.Field array root.
932 class_root = FindSystemClass(self, "[Ljava/lang/reflect/Field;");
933 CHECK(class_root != nullptr);
934 SetClassRoot(ClassRoot::kJavaLangReflectFieldArrayClass, class_root);
935
936 // Create java.lang.reflect.Constructor.class root and array root.
937 class_root = FindSystemClass(self, "Ljava/lang/reflect/Constructor;");
938 CHECK(class_root != nullptr);
939 SetClassRoot(ClassRoot::kJavaLangReflectConstructor, class_root);
940 class_root = FindSystemClass(self, "[Ljava/lang/reflect/Constructor;");
941 CHECK(class_root != nullptr);
942 SetClassRoot(ClassRoot::kJavaLangReflectConstructorArrayClass, class_root);
943
944 // Create java.lang.reflect.Method.class root and array root.
945 class_root = FindSystemClass(self, "Ljava/lang/reflect/Method;");
946 CHECK(class_root != nullptr);
947 SetClassRoot(ClassRoot::kJavaLangReflectMethod, class_root);
948 class_root = FindSystemClass(self, "[Ljava/lang/reflect/Method;");
949 CHECK(class_root != nullptr);
950 SetClassRoot(ClassRoot::kJavaLangReflectMethodArrayClass, class_root);
951
952 // Create java.lang.invoke.CallSite.class root
953 class_root = FindSystemClass(self, "Ljava/lang/invoke/CallSite;");
954 CHECK(class_root != nullptr);
955 SetClassRoot(ClassRoot::kJavaLangInvokeCallSite, class_root);
956
957 // Create java.lang.invoke.MethodType.class root
958 class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodType;");
959 CHECK(class_root != nullptr);
960 SetClassRoot(ClassRoot::kJavaLangInvokeMethodType, class_root);
961
962 // Create java.lang.invoke.MethodHandleImpl.class root
963 class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandleImpl;");
964 CHECK(class_root != nullptr);
965 SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandleImpl, class_root);
966 SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandle, class_root->GetSuperClass());
967
968 // Create java.lang.invoke.MethodHandles.Lookup.class root
969 class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandles$Lookup;");
970 CHECK(class_root != nullptr);
971 SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandlesLookup, class_root);
972
973 // Create java.lang.invoke.VarHandle.class root
974 class_root = FindSystemClass(self, "Ljava/lang/invoke/VarHandle;");
975 CHECK(class_root != nullptr);
976 SetClassRoot(ClassRoot::kJavaLangInvokeVarHandle, class_root);
977
978 // Create java.lang.invoke.FieldVarHandle.class root
979 class_root = FindSystemClass(self, "Ljava/lang/invoke/FieldVarHandle;");
980 CHECK(class_root != nullptr);
981 SetClassRoot(ClassRoot::kJavaLangInvokeFieldVarHandle, class_root);
982
983 // Create java.lang.invoke.ArrayElementVarHandle.class root
984 class_root = FindSystemClass(self, "Ljava/lang/invoke/ArrayElementVarHandle;");
985 CHECK(class_root != nullptr);
986 SetClassRoot(ClassRoot::kJavaLangInvokeArrayElementVarHandle, class_root);
987
988 // Create java.lang.invoke.ByteArrayViewVarHandle.class root
989 class_root = FindSystemClass(self, "Ljava/lang/invoke/ByteArrayViewVarHandle;");
990 CHECK(class_root != nullptr);
991 SetClassRoot(ClassRoot::kJavaLangInvokeByteArrayViewVarHandle, class_root);
992
993 // Create java.lang.invoke.ByteBufferViewVarHandle.class root
994 class_root = FindSystemClass(self, "Ljava/lang/invoke/ByteBufferViewVarHandle;");
995 CHECK(class_root != nullptr);
996 SetClassRoot(ClassRoot::kJavaLangInvokeByteBufferViewVarHandle, class_root);
997
998 class_root = FindSystemClass(self, "Ldalvik/system/EmulatedStackFrame;");
999 CHECK(class_root != nullptr);
1000 SetClassRoot(ClassRoot::kDalvikSystemEmulatedStackFrame, class_root);
1001
1002 // java.lang.ref classes need to be specially flagged, but otherwise are normal classes
1003 // finish initializing Reference class
1004 mirror::Class::SetStatus(java_lang_ref_Reference, ClassStatus::kNotReady, self);
1005 CheckSystemClass(self, java_lang_ref_Reference, "Ljava/lang/ref/Reference;");
1006 CHECK_EQ(java_lang_ref_Reference->GetObjectSize(), mirror::Reference::InstanceSize());
1007 CHECK_EQ(java_lang_ref_Reference->GetClassSize(),
1008 mirror::Reference::ClassSize(image_pointer_size_));
1009 class_root = FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;");
1010 CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal);
1011 class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagFinalizerReference);
1012 class_root = FindSystemClass(self, "Ljava/lang/ref/PhantomReference;");
1013 CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal);
1014 class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagPhantomReference);
1015 class_root = FindSystemClass(self, "Ljava/lang/ref/SoftReference;");
1016 CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal);
1017 class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagSoftReference);
1018 class_root = FindSystemClass(self, "Ljava/lang/ref/WeakReference;");
1019 CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal);
1020 class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagWeakReference);
1021
1022 // Setup the ClassLoader, verifying the object_size_.
1023 class_root = FindSystemClass(self, "Ljava/lang/ClassLoader;");
1024 class_root->SetClassLoaderClass();
1025 CHECK_EQ(class_root->GetObjectSize(), mirror::ClassLoader::InstanceSize());
1026 SetClassRoot(ClassRoot::kJavaLangClassLoader, class_root);
1027
1028 // Set up java.lang.Throwable, java.lang.ClassNotFoundException, and
1029 // java.lang.StackTraceElement as a convenience.
1030 SetClassRoot(ClassRoot::kJavaLangThrowable, FindSystemClass(self, "Ljava/lang/Throwable;"));
1031 SetClassRoot(ClassRoot::kJavaLangClassNotFoundException,
1032 FindSystemClass(self, "Ljava/lang/ClassNotFoundException;"));
1033 SetClassRoot(ClassRoot::kJavaLangStackTraceElement,
1034 FindSystemClass(self, "Ljava/lang/StackTraceElement;"));
1035 SetClassRoot(ClassRoot::kJavaLangStackTraceElementArrayClass,
1036 FindSystemClass(self, "[Ljava/lang/StackTraceElement;"));
1037 SetClassRoot(ClassRoot::kJavaLangClassLoaderArrayClass,
1038 FindSystemClass(self, "[Ljava/lang/ClassLoader;"));
1039
1040 // Create conflict tables that depend on the class linker.
1041 runtime->FixupConflictTables();
1042
1043 FinishInit(self);
1044
1045 VLOG(startup) << "ClassLinker::InitFromCompiler exiting";
1046
1047 return true;
1048 }
1049
CreateStringInitBindings(Thread * self,ClassLinker * class_linker)1050 static void CreateStringInitBindings(Thread* self, ClassLinker* class_linker)
1051 REQUIRES_SHARED(Locks::mutator_lock_) {
1052 // Find String.<init> -> StringFactory bindings.
1053 ObjPtr<mirror::Class> string_factory_class =
1054 class_linker->FindSystemClass(self, "Ljava/lang/StringFactory;");
1055 CHECK(string_factory_class != nullptr);
1056 ObjPtr<mirror::Class> string_class = GetClassRoot<mirror::String>(class_linker);
1057 WellKnownClasses::InitStringInit(string_class, string_factory_class);
1058 // Update the primordial thread.
1059 self->InitStringEntryPoints();
1060 }
1061
FinishInit(Thread * self)1062 void ClassLinker::FinishInit(Thread* self) {
1063 VLOG(startup) << "ClassLinker::FinishInit entering";
1064
1065 CreateStringInitBindings(self, this);
1066
1067 // Let the heap know some key offsets into java.lang.ref instances
1068 // Note: we hard code the field indexes here rather than using FindInstanceField
1069 // as the types of the field can't be resolved prior to the runtime being
1070 // fully initialized
1071 StackHandleScope<3> hs(self);
1072 Handle<mirror::Class> java_lang_ref_Reference =
1073 hs.NewHandle(GetClassRoot<mirror::Reference>(this));
1074 Handle<mirror::Class> java_lang_ref_FinalizerReference =
1075 hs.NewHandle(FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"));
1076
1077 ArtField* pendingNext = java_lang_ref_Reference->GetInstanceField(0);
1078 CHECK_STREQ(pendingNext->GetName(), "pendingNext");
1079 CHECK_STREQ(pendingNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;");
1080
1081 ArtField* queue = java_lang_ref_Reference->GetInstanceField(1);
1082 CHECK_STREQ(queue->GetName(), "queue");
1083 CHECK_STREQ(queue->GetTypeDescriptor(), "Ljava/lang/ref/ReferenceQueue;");
1084
1085 ArtField* queueNext = java_lang_ref_Reference->GetInstanceField(2);
1086 CHECK_STREQ(queueNext->GetName(), "queueNext");
1087 CHECK_STREQ(queueNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;");
1088
1089 ArtField* referent = java_lang_ref_Reference->GetInstanceField(3);
1090 CHECK_STREQ(referent->GetName(), "referent");
1091 CHECK_STREQ(referent->GetTypeDescriptor(), "Ljava/lang/Object;");
1092
1093 ArtField* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2);
1094 CHECK_STREQ(zombie->GetName(), "zombie");
1095 CHECK_STREQ(zombie->GetTypeDescriptor(), "Ljava/lang/Object;");
1096
1097 // ensure all class_roots_ are initialized
1098 for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); i++) {
1099 ClassRoot class_root = static_cast<ClassRoot>(i);
1100 ObjPtr<mirror::Class> klass = GetClassRoot(class_root);
1101 CHECK(klass != nullptr);
1102 DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != nullptr);
1103 // note SetClassRoot does additional validation.
1104 // if possible add new checks there to catch errors early
1105 }
1106
1107 CHECK(GetArrayIfTable() != nullptr);
1108
1109 // disable the slow paths in FindClass and CreatePrimitiveClass now
1110 // that Object, Class, and Object[] are setup
1111 init_done_ = true;
1112
1113 // Under sanitization, the small carve-out to handle stack overflow might not be enough to
1114 // initialize the StackOverflowError class (as it might require running the verifier). Instead,
1115 // ensure that the class will be initialized.
1116 if (kMemoryToolIsAvailable && !Runtime::Current()->IsAotCompiler()) {
1117 verifier::ClassVerifier::Init(this); // Need to prepare the verifier.
1118
1119 ObjPtr<mirror::Class> soe_klass = FindSystemClass(self, "Ljava/lang/StackOverflowError;");
1120 if (soe_klass == nullptr || !EnsureInitialized(self, hs.NewHandle(soe_klass), true, true)) {
1121 // Strange, but don't crash.
1122 LOG(WARNING) << "Could not prepare StackOverflowError.";
1123 self->ClearException();
1124 }
1125 }
1126
1127 VLOG(startup) << "ClassLinker::FinishInit exiting";
1128 }
1129
RunRootClinits(Thread * self)1130 void ClassLinker::RunRootClinits(Thread* self) {
1131 for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); ++i) {
1132 ObjPtr<mirror::Class> c = GetClassRoot(ClassRoot(i), this);
1133 if (!c->IsArrayClass() && !c->IsPrimitive()) {
1134 StackHandleScope<1> hs(self);
1135 Handle<mirror::Class> h_class(hs.NewHandle(c));
1136 EnsureInitialized(self, h_class, true, true);
1137 self->AssertNoPendingException();
1138 } else {
1139 DCHECK(c->IsInitialized());
1140 }
1141 }
1142 }
1143
1144 struct TrampolineCheckData {
1145 const void* quick_resolution_trampoline;
1146 const void* quick_imt_conflict_trampoline;
1147 const void* quick_generic_jni_trampoline;
1148 const void* quick_to_interpreter_bridge_trampoline;
1149 PointerSize pointer_size;
1150 ArtMethod* m;
1151 bool error;
1152 };
1153
InitFromBootImage(std::string * error_msg)1154 bool ClassLinker::InitFromBootImage(std::string* error_msg) {
1155 VLOG(startup) << __FUNCTION__ << " entering";
1156 CHECK(!init_done_);
1157
1158 Runtime* const runtime = Runtime::Current();
1159 Thread* const self = Thread::Current();
1160 gc::Heap* const heap = runtime->GetHeap();
1161 std::vector<gc::space::ImageSpace*> spaces = heap->GetBootImageSpaces();
1162 CHECK(!spaces.empty());
1163 const ImageHeader& image_header = spaces[0]->GetImageHeader();
1164 uint32_t pointer_size_unchecked = image_header.GetPointerSizeUnchecked();
1165 if (!ValidPointerSize(pointer_size_unchecked)) {
1166 *error_msg = StringPrintf("Invalid image pointer size: %u", pointer_size_unchecked);
1167 return false;
1168 }
1169 image_pointer_size_ = image_header.GetPointerSize();
1170 if (!runtime->IsAotCompiler()) {
1171 // Only the Aot compiler supports having an image with a different pointer size than the
1172 // runtime. This happens on the host for compiling 32 bit tests since we use a 64 bit libart
1173 // compiler. We may also use 32 bit dex2oat on a system with 64 bit apps.
1174 if (image_pointer_size_ != kRuntimePointerSize) {
1175 *error_msg = StringPrintf("Runtime must use current image pointer size: %zu vs %zu",
1176 static_cast<size_t>(image_pointer_size_),
1177 sizeof(void*));
1178 return false;
1179 }
1180 }
1181 DCHECK(!runtime->HasResolutionMethod());
1182 runtime->SetResolutionMethod(image_header.GetImageMethod(ImageHeader::kResolutionMethod));
1183 runtime->SetImtConflictMethod(image_header.GetImageMethod(ImageHeader::kImtConflictMethod));
1184 runtime->SetImtUnimplementedMethod(
1185 image_header.GetImageMethod(ImageHeader::kImtUnimplementedMethod));
1186 runtime->SetCalleeSaveMethod(
1187 image_header.GetImageMethod(ImageHeader::kSaveAllCalleeSavesMethod),
1188 CalleeSaveType::kSaveAllCalleeSaves);
1189 runtime->SetCalleeSaveMethod(
1190 image_header.GetImageMethod(ImageHeader::kSaveRefsOnlyMethod),
1191 CalleeSaveType::kSaveRefsOnly);
1192 runtime->SetCalleeSaveMethod(
1193 image_header.GetImageMethod(ImageHeader::kSaveRefsAndArgsMethod),
1194 CalleeSaveType::kSaveRefsAndArgs);
1195 runtime->SetCalleeSaveMethod(
1196 image_header.GetImageMethod(ImageHeader::kSaveEverythingMethod),
1197 CalleeSaveType::kSaveEverything);
1198 runtime->SetCalleeSaveMethod(
1199 image_header.GetImageMethod(ImageHeader::kSaveEverythingMethodForClinit),
1200 CalleeSaveType::kSaveEverythingForClinit);
1201 runtime->SetCalleeSaveMethod(
1202 image_header.GetImageMethod(ImageHeader::kSaveEverythingMethodForSuspendCheck),
1203 CalleeSaveType::kSaveEverythingForSuspendCheck);
1204
1205 std::vector<const OatFile*> oat_files =
1206 runtime->GetOatFileManager().RegisterImageOatFiles(spaces);
1207 DCHECK(!oat_files.empty());
1208 const OatHeader& default_oat_header = oat_files[0]->GetOatHeader();
1209 jni_dlsym_lookup_trampoline_ = default_oat_header.GetJniDlsymLookupTrampoline();
1210 jni_dlsym_lookup_critical_trampoline_ = default_oat_header.GetJniDlsymLookupCriticalTrampoline();
1211 quick_resolution_trampoline_ = default_oat_header.GetQuickResolutionTrampoline();
1212 quick_imt_conflict_trampoline_ = default_oat_header.GetQuickImtConflictTrampoline();
1213 quick_generic_jni_trampoline_ = default_oat_header.GetQuickGenericJniTrampoline();
1214 quick_to_interpreter_bridge_trampoline_ = default_oat_header.GetQuickToInterpreterBridge();
1215 if (kIsDebugBuild) {
1216 // Check that the other images use the same trampoline.
1217 for (size_t i = 1; i < oat_files.size(); ++i) {
1218 const OatHeader& ith_oat_header = oat_files[i]->GetOatHeader();
1219 const void* ith_jni_dlsym_lookup_trampoline_ =
1220 ith_oat_header.GetJniDlsymLookupTrampoline();
1221 const void* ith_jni_dlsym_lookup_critical_trampoline_ =
1222 ith_oat_header.GetJniDlsymLookupCriticalTrampoline();
1223 const void* ith_quick_resolution_trampoline =
1224 ith_oat_header.GetQuickResolutionTrampoline();
1225 const void* ith_quick_imt_conflict_trampoline =
1226 ith_oat_header.GetQuickImtConflictTrampoline();
1227 const void* ith_quick_generic_jni_trampoline =
1228 ith_oat_header.GetQuickGenericJniTrampoline();
1229 const void* ith_quick_to_interpreter_bridge_trampoline =
1230 ith_oat_header.GetQuickToInterpreterBridge();
1231 if (ith_jni_dlsym_lookup_trampoline_ != jni_dlsym_lookup_trampoline_ ||
1232 ith_jni_dlsym_lookup_critical_trampoline_ != jni_dlsym_lookup_critical_trampoline_ ||
1233 ith_quick_resolution_trampoline != quick_resolution_trampoline_ ||
1234 ith_quick_imt_conflict_trampoline != quick_imt_conflict_trampoline_ ||
1235 ith_quick_generic_jni_trampoline != quick_generic_jni_trampoline_ ||
1236 ith_quick_to_interpreter_bridge_trampoline != quick_to_interpreter_bridge_trampoline_) {
1237 // Make sure that all methods in this image do not contain those trampolines as
1238 // entrypoints. Otherwise the class-linker won't be able to work with a single set.
1239 TrampolineCheckData data;
1240 data.error = false;
1241 data.pointer_size = GetImagePointerSize();
1242 data.quick_resolution_trampoline = ith_quick_resolution_trampoline;
1243 data.quick_imt_conflict_trampoline = ith_quick_imt_conflict_trampoline;
1244 data.quick_generic_jni_trampoline = ith_quick_generic_jni_trampoline;
1245 data.quick_to_interpreter_bridge_trampoline = ith_quick_to_interpreter_bridge_trampoline;
1246 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
1247 auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
1248 if (obj->IsClass()) {
1249 ObjPtr<mirror::Class> klass = obj->AsClass();
1250 for (ArtMethod& m : klass->GetMethods(data.pointer_size)) {
1251 const void* entrypoint =
1252 m.GetEntryPointFromQuickCompiledCodePtrSize(data.pointer_size);
1253 if (entrypoint == data.quick_resolution_trampoline ||
1254 entrypoint == data.quick_imt_conflict_trampoline ||
1255 entrypoint == data.quick_generic_jni_trampoline ||
1256 entrypoint == data.quick_to_interpreter_bridge_trampoline) {
1257 data.m = &m;
1258 data.error = true;
1259 return;
1260 }
1261 }
1262 }
1263 };
1264 spaces[i]->GetLiveBitmap()->Walk(visitor);
1265 if (data.error) {
1266 ArtMethod* m = data.m;
1267 LOG(ERROR) << "Found a broken ArtMethod: " << ArtMethod::PrettyMethod(m);
1268 *error_msg = "Found an ArtMethod with a bad entrypoint";
1269 return false;
1270 }
1271 }
1272 }
1273 }
1274
1275 class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>(
1276 ObjPtr<mirror::ObjectArray<mirror::Class>>::DownCast(
1277 image_header.GetImageRoot(ImageHeader::kClassRoots)));
1278 DCHECK_EQ(GetClassRoot<mirror::Class>(this)->GetClassFlags(), mirror::kClassFlagClass);
1279
1280 DCHECK_EQ(GetClassRoot<mirror::Object>(this)->GetObjectSize(), sizeof(mirror::Object));
1281 ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects =
1282 ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast(
1283 image_header.GetImageRoot(ImageHeader::kBootImageLiveObjects));
1284 runtime->SetSentinel(boot_image_live_objects->Get(ImageHeader::kClearedJniWeakSentinel));
1285 DCHECK(runtime->GetSentinel().Read()->GetClass() == GetClassRoot<mirror::Object>(this));
1286
1287 for (size_t i = 0u, size = spaces.size(); i != size; ++i) {
1288 // Boot class loader, use a null handle.
1289 std::vector<std::unique_ptr<const DexFile>> dex_files;
1290 if (!AddImageSpace(spaces[i],
1291 ScopedNullHandle<mirror::ClassLoader>(),
1292 /*out*/&dex_files,
1293 error_msg)) {
1294 return false;
1295 }
1296 // Append opened dex files at the end.
1297 boot_dex_files_.insert(boot_dex_files_.end(),
1298 std::make_move_iterator(dex_files.begin()),
1299 std::make_move_iterator(dex_files.end()));
1300 }
1301 for (const std::unique_ptr<const DexFile>& dex_file : boot_dex_files_) {
1302 OatDexFile::MadviseDexFile(*dex_file, MadviseState::kMadviseStateAtLoad);
1303 }
1304 FinishInit(self);
1305
1306 VLOG(startup) << __FUNCTION__ << " exiting";
1307 return true;
1308 }
1309
AddExtraBootDexFiles(Thread * self,std::vector<std::unique_ptr<const DexFile>> && additional_dex_files)1310 void ClassLinker::AddExtraBootDexFiles(
1311 Thread* self,
1312 std::vector<std::unique_ptr<const DexFile>>&& additional_dex_files) {
1313 for (std::unique_ptr<const DexFile>& dex_file : additional_dex_files) {
1314 AppendToBootClassPath(self, dex_file.get());
1315 boot_dex_files_.push_back(std::move(dex_file));
1316 }
1317 }
1318
IsBootClassLoader(ScopedObjectAccessAlreadyRunnable & soa,ObjPtr<mirror::ClassLoader> class_loader)1319 bool ClassLinker::IsBootClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
1320 ObjPtr<mirror::ClassLoader> class_loader) {
1321 return class_loader == nullptr ||
1322 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader) ==
1323 class_loader->GetClass();
1324 }
1325
1326 class CHAOnDeleteUpdateClassVisitor {
1327 public:
CHAOnDeleteUpdateClassVisitor(LinearAlloc * alloc)1328 explicit CHAOnDeleteUpdateClassVisitor(LinearAlloc* alloc)
1329 : allocator_(alloc), cha_(Runtime::Current()->GetClassLinker()->GetClassHierarchyAnalysis()),
1330 pointer_size_(Runtime::Current()->GetClassLinker()->GetImagePointerSize()),
1331 self_(Thread::Current()) {}
1332
operator ()(ObjPtr<mirror::Class> klass)1333 bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) {
1334 // This class is going to be unloaded. Tell CHA about it.
1335 cha_->ResetSingleImplementationInHierarchy(klass, allocator_, pointer_size_);
1336 return true;
1337 }
1338 private:
1339 const LinearAlloc* allocator_;
1340 const ClassHierarchyAnalysis* cha_;
1341 const PointerSize pointer_size_;
1342 const Thread* self_;
1343 };
1344
1345 /*
1346 * A class used to ensure that all references to strings interned in an AppImage have been
1347 * properly recorded in the interned references list, and is only ever run in debug mode.
1348 */
1349 class CountInternedStringReferencesVisitor {
1350 public:
CountInternedStringReferencesVisitor(const gc::space::ImageSpace & space,const InternTable::UnorderedSet & image_interns)1351 CountInternedStringReferencesVisitor(const gc::space::ImageSpace& space,
1352 const InternTable::UnorderedSet& image_interns)
1353 : space_(space),
1354 image_interns_(image_interns),
1355 count_(0u) {}
1356
TestObject(ObjPtr<mirror::Object> referred_obj) const1357 void TestObject(ObjPtr<mirror::Object> referred_obj) const
1358 REQUIRES_SHARED(Locks::mutator_lock_) {
1359 if (referred_obj != nullptr &&
1360 space_.HasAddress(referred_obj.Ptr()) &&
1361 referred_obj->IsString()) {
1362 ObjPtr<mirror::String> referred_str = referred_obj->AsString();
1363 auto it = image_interns_.find(GcRoot<mirror::String>(referred_str));
1364 if (it != image_interns_.end() && it->Read() == referred_str) {
1365 ++count_;
1366 }
1367 }
1368 }
1369
VisitRootIfNonNull(mirror::CompressedReference<mirror::Object> * root) const1370 void VisitRootIfNonNull(
1371 mirror::CompressedReference<mirror::Object>* root) const
1372 REQUIRES_SHARED(Locks::mutator_lock_) {
1373 if (!root->IsNull()) {
1374 VisitRoot(root);
1375 }
1376 }
1377
VisitRoot(mirror::CompressedReference<mirror::Object> * root) const1378 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
1379 REQUIRES_SHARED(Locks::mutator_lock_) {
1380 TestObject(root->AsMirrorPtr());
1381 }
1382
1383 // Visit Class Fields
operator ()(ObjPtr<mirror::Object> obj,MemberOffset offset,bool is_static ATTRIBUTE_UNUSED) const1384 void operator()(ObjPtr<mirror::Object> obj,
1385 MemberOffset offset,
1386 bool is_static ATTRIBUTE_UNUSED) const
1387 REQUIRES_SHARED(Locks::mutator_lock_) {
1388 // References within image or across images don't need a read barrier.
1389 ObjPtr<mirror::Object> referred_obj =
1390 obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(offset);
1391 TestObject(referred_obj);
1392 }
1393
operator ()(ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED,ObjPtr<mirror::Reference> ref) const1394 void operator()(ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED,
1395 ObjPtr<mirror::Reference> ref) const
1396 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) {
1397 operator()(ref, mirror::Reference::ReferentOffset(), /*is_static=*/ false);
1398 }
1399
GetCount() const1400 size_t GetCount() const {
1401 return count_;
1402 }
1403
1404 private:
1405 const gc::space::ImageSpace& space_;
1406 const InternTable::UnorderedSet& image_interns_;
1407 mutable size_t count_; // Modified from the `const` callbacks.
1408 };
1409
1410 /*
1411 * This function counts references to strings interned in the AppImage.
1412 * This is used in debug build to check against the number of the recorded references.
1413 */
CountInternedStringReferences(gc::space::ImageSpace & space,const InternTable::UnorderedSet & image_interns)1414 size_t CountInternedStringReferences(gc::space::ImageSpace& space,
1415 const InternTable::UnorderedSet& image_interns)
1416 REQUIRES_SHARED(Locks::mutator_lock_) {
1417 const gc::accounting::ContinuousSpaceBitmap* bitmap = space.GetMarkBitmap();
1418 const ImageHeader& image_header = space.GetImageHeader();
1419 const uint8_t* target_base = space.GetMemMap()->Begin();
1420 const ImageSection& objects_section = image_header.GetObjectsSection();
1421
1422 auto objects_begin = reinterpret_cast<uintptr_t>(target_base + objects_section.Offset());
1423 auto objects_end = reinterpret_cast<uintptr_t>(target_base + objects_section.End());
1424
1425 CountInternedStringReferencesVisitor visitor(space, image_interns);
1426 bitmap->VisitMarkedRange(objects_begin,
1427 objects_end,
1428 [&space, &visitor](mirror::Object* obj)
1429 REQUIRES_SHARED(Locks::mutator_lock_) {
1430 if (space.HasAddress(obj)) {
1431 if (obj->IsDexCache()) {
1432 obj->VisitReferences</* kVisitNativeRoots= */ true,
1433 kVerifyNone,
1434 kWithoutReadBarrier>(visitor, visitor);
1435 } else {
1436 // Don't visit native roots for non-dex-cache as they can't contain
1437 // native references to strings. This is verified during compilation
1438 // by ImageWriter::VerifyNativeGCRootInvariants.
1439 obj->VisitReferences</* kVisitNativeRoots= */ false,
1440 kVerifyNone,
1441 kWithoutReadBarrier>(visitor, visitor);
1442 }
1443 }
1444 });
1445 return visitor.GetCount();
1446 }
1447
1448 template <typename Visitor>
VisitInternedStringReferences(gc::space::ImageSpace * space,bool use_preresolved_strings,const Visitor & visitor)1449 static void VisitInternedStringReferences(
1450 gc::space::ImageSpace* space,
1451 bool use_preresolved_strings,
1452 const Visitor& visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
1453 const uint8_t* target_base = space->Begin();
1454 const ImageSection& sro_section =
1455 space->GetImageHeader().GetImageStringReferenceOffsetsSection();
1456 const size_t num_string_offsets = sro_section.Size() / sizeof(AppImageReferenceOffsetInfo);
1457
1458 VLOG(image)
1459 << "ClassLinker:AppImage:InternStrings:imageStringReferenceOffsetCount = "
1460 << num_string_offsets;
1461
1462 const auto* sro_base =
1463 reinterpret_cast<const AppImageReferenceOffsetInfo*>(target_base + sro_section.Offset());
1464
1465 for (size_t offset_index = 0; offset_index < num_string_offsets; ++offset_index) {
1466 uint32_t base_offset = sro_base[offset_index].first;
1467
1468 if (HasDexCacheStringNativeRefTag(base_offset)) {
1469 base_offset = ClearDexCacheNativeRefTags(base_offset);
1470 DCHECK_ALIGNED(base_offset, 2);
1471
1472 ObjPtr<mirror::DexCache> dex_cache =
1473 reinterpret_cast<mirror::DexCache*>(space->Begin() + base_offset);
1474 uint32_t string_slot_index = sro_base[offset_index].second;
1475
1476 mirror::StringDexCachePair source =
1477 dex_cache->GetStrings()[string_slot_index].load(std::memory_order_relaxed);
1478 ObjPtr<mirror::String> referred_string = source.object.Read();
1479 DCHECK(referred_string != nullptr);
1480
1481 ObjPtr<mirror::String> visited = visitor(referred_string);
1482 if (visited != referred_string) {
1483 // Because we are not using a helper function we need to mark the GC card manually.
1484 WriteBarrier::ForEveryFieldWrite(dex_cache);
1485 dex_cache->GetStrings()[string_slot_index].store(
1486 mirror::StringDexCachePair(visited, source.index), std::memory_order_relaxed);
1487 }
1488 } else if (HasDexCachePreResolvedStringNativeRefTag(base_offset)) {
1489 if (use_preresolved_strings) {
1490 base_offset = ClearDexCacheNativeRefTags(base_offset);
1491 DCHECK_ALIGNED(base_offset, 2);
1492
1493 ObjPtr<mirror::DexCache> dex_cache =
1494 reinterpret_cast<mirror::DexCache*>(space->Begin() + base_offset);
1495 uint32_t string_index = sro_base[offset_index].second;
1496
1497 GcRoot<mirror::String>* preresolved_strings =
1498 dex_cache->GetPreResolvedStrings();
1499 // Handle calls to ClearPreResolvedStrings that might occur concurrently by the profile
1500 // saver that runs shortly after startup. In case the strings are cleared, there is nothing
1501 // to fix up.
1502 if (preresolved_strings != nullptr) {
1503 ObjPtr<mirror::String> referred_string =
1504 preresolved_strings[string_index].Read();
1505 if (referred_string != nullptr) {
1506 ObjPtr<mirror::String> visited = visitor(referred_string);
1507 if (visited != referred_string) {
1508 // Because we are not using a helper function we need to mark the GC card manually.
1509 WriteBarrier::ForEveryFieldWrite(dex_cache);
1510 preresolved_strings[string_index] = GcRoot<mirror::String>(visited);
1511 }
1512 }
1513 }
1514 }
1515 } else {
1516 uint32_t raw_member_offset = sro_base[offset_index].second;
1517 DCHECK_ALIGNED(base_offset, 2);
1518 DCHECK_ALIGNED(raw_member_offset, 2);
1519
1520 ObjPtr<mirror::Object> obj_ptr =
1521 reinterpret_cast<mirror::Object*>(space->Begin() + base_offset);
1522 MemberOffset member_offset(raw_member_offset);
1523 ObjPtr<mirror::String> referred_string =
1524 obj_ptr->GetFieldObject<mirror::String,
1525 kVerifyNone,
1526 kWithoutReadBarrier,
1527 /* kIsVolatile= */ false>(member_offset);
1528 DCHECK(referred_string != nullptr);
1529
1530 ObjPtr<mirror::String> visited = visitor(referred_string);
1531 if (visited != referred_string) {
1532 obj_ptr->SetFieldObject</* kTransactionActive= */ false,
1533 /* kCheckTransaction= */ false,
1534 kVerifyNone,
1535 /* kIsVolatile= */ false>(member_offset, visited);
1536 }
1537 }
1538 }
1539 }
1540
VerifyInternedStringReferences(gc::space::ImageSpace * space)1541 static void VerifyInternedStringReferences(gc::space::ImageSpace* space)
1542 REQUIRES_SHARED(Locks::mutator_lock_) {
1543 InternTable::UnorderedSet image_interns;
1544 const ImageSection& section = space->GetImageHeader().GetInternedStringsSection();
1545 if (section.Size() > 0) {
1546 size_t read_count;
1547 const uint8_t* data = space->Begin() + section.Offset();
1548 InternTable::UnorderedSet image_set(data, /*make_copy_of_data=*/ false, &read_count);
1549 image_set.swap(image_interns);
1550 }
1551 size_t num_recorded_refs = 0u;
1552 VisitInternedStringReferences(
1553 space,
1554 /*use_preresolved_strings=*/ true,
1555 [&image_interns, &num_recorded_refs](ObjPtr<mirror::String> str)
1556 REQUIRES_SHARED(Locks::mutator_lock_) {
1557 auto it = image_interns.find(GcRoot<mirror::String>(str));
1558 CHECK(it != image_interns.end());
1559 CHECK(it->Read() == str);
1560 ++num_recorded_refs;
1561 return str;
1562 });
1563 size_t num_found_refs = CountInternedStringReferences(*space, image_interns);
1564 CHECK_EQ(num_recorded_refs, num_found_refs);
1565 }
1566
1567 // new_class_set is the set of classes that were read from the class table section in the image.
1568 // If there was no class table section, it is null.
1569 // Note: using a class here to avoid having to make ClassLinker internals public.
1570 class AppImageLoadingHelper {
1571 public:
1572 static void Update(
1573 ClassLinker* class_linker,
1574 gc::space::ImageSpace* space,
1575 Handle<mirror::ClassLoader> class_loader,
1576 Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches,
1577 ClassTable::ClassSet* new_class_set)
1578 REQUIRES(!Locks::dex_lock_)
1579 REQUIRES_SHARED(Locks::mutator_lock_);
1580
1581 static void HandleAppImageStrings(gc::space::ImageSpace* space)
1582 REQUIRES_SHARED(Locks::mutator_lock_);
1583 };
1584
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)1585 void AppImageLoadingHelper::Update(
1586 ClassLinker* class_linker,
1587 gc::space::ImageSpace* space,
1588 Handle<mirror::ClassLoader> class_loader,
1589 Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches,
1590 ClassTable::ClassSet* new_class_set)
1591 REQUIRES(!Locks::dex_lock_)
1592 REQUIRES_SHARED(Locks::mutator_lock_) {
1593 ScopedTrace app_image_timing("AppImage:Updating");
1594
1595 if (kIsDebugBuild && ClassLinker::kAppImageMayContainStrings) {
1596 // In debug build, verify the string references before applying
1597 // the Runtime::LoadAppImageStartupCache() option.
1598 VerifyInternedStringReferences(space);
1599 }
1600
1601 Thread* const self = Thread::Current();
1602 Runtime* const runtime = Runtime::Current();
1603 gc::Heap* const heap = runtime->GetHeap();
1604 const ImageHeader& header = space->GetImageHeader();
1605 bool load_app_image_startup_cache = runtime->LoadAppImageStartupCache();
1606 {
1607 // Register dex caches with the class loader.
1608 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
1609 for (auto dex_cache : dex_caches.Iterate<mirror::DexCache>()) {
1610 const DexFile* const dex_file = dex_cache->GetDexFile();
1611 {
1612 WriterMutexLock mu2(self, *Locks::dex_lock_);
1613 CHECK(class_linker->FindDexCacheDataLocked(*dex_file) == nullptr);
1614 class_linker->RegisterDexFileLocked(*dex_file, dex_cache, class_loader.Get());
1615 }
1616
1617 if (!load_app_image_startup_cache) {
1618 dex_cache->ClearPreResolvedStrings();
1619 }
1620
1621 if (kIsDebugBuild) {
1622 CHECK(new_class_set != nullptr);
1623 mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes();
1624 const size_t num_types = dex_cache->NumResolvedTypes();
1625 for (size_t j = 0; j != num_types; ++j) {
1626 // The image space is not yet added to the heap, avoid read barriers.
1627 ObjPtr<mirror::Class> klass = types[j].load(std::memory_order_relaxed).object.Read();
1628
1629 if (space->HasAddress(klass.Ptr())) {
1630 DCHECK(!klass->IsErroneous()) << klass->GetStatus();
1631 auto it = new_class_set->find(ClassTable::TableSlot(klass));
1632 DCHECK(it != new_class_set->end());
1633 DCHECK_EQ(it->Read(), klass);
1634 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
1635
1636 if (super_class != nullptr && !heap->ObjectIsInBootImageSpace(super_class)) {
1637 auto it2 = new_class_set->find(ClassTable::TableSlot(super_class));
1638 DCHECK(it2 != new_class_set->end());
1639 DCHECK_EQ(it2->Read(), super_class);
1640 }
1641
1642 for (ArtMethod& m : klass->GetDirectMethods(kRuntimePointerSize)) {
1643 const void* code = m.GetEntryPointFromQuickCompiledCode();
1644 const void* oat_code = m.IsInvokable() ? class_linker->GetQuickOatCodeFor(&m) : code;
1645 if (!class_linker->IsQuickResolutionStub(code) &&
1646 !class_linker->IsQuickGenericJniStub(code) &&
1647 !class_linker->IsQuickToInterpreterBridge(code) &&
1648 !m.IsNative()) {
1649 DCHECK_EQ(code, oat_code) << m.PrettyMethod();
1650 }
1651 }
1652
1653 for (ArtMethod& m : klass->GetVirtualMethods(kRuntimePointerSize)) {
1654 const void* code = m.GetEntryPointFromQuickCompiledCode();
1655 const void* oat_code = m.IsInvokable() ? class_linker->GetQuickOatCodeFor(&m) : code;
1656 if (!class_linker->IsQuickResolutionStub(code) &&
1657 !class_linker->IsQuickGenericJniStub(code) &&
1658 !class_linker->IsQuickToInterpreterBridge(code) &&
1659 !m.IsNative()) {
1660 DCHECK_EQ(code, oat_code) << m.PrettyMethod();
1661 }
1662 }
1663 }
1664 }
1665 }
1666 }
1667 }
1668
1669 if (ClassLinker::kAppImageMayContainStrings) {
1670 HandleAppImageStrings(space);
1671 }
1672
1673 if (kVerifyArtMethodDeclaringClasses) {
1674 ScopedTrace timing("AppImage:VerifyDeclaringClasses");
1675 ReaderMutexLock rmu(self, *Locks::heap_bitmap_lock_);
1676 gc::accounting::HeapBitmap* live_bitmap = heap->GetLiveBitmap();
1677 header.VisitPackedArtMethods([&](ArtMethod& method)
1678 REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
1679 ObjPtr<mirror::Class> klass = method.GetDeclaringClassUnchecked();
1680 if (klass != nullptr) {
1681 CHECK(live_bitmap->Test(klass.Ptr())) << "Image method has unmarked declaring class";
1682 }
1683 }, space->Begin(), kRuntimePointerSize);
1684 }
1685 }
1686
HandleAppImageStrings(gc::space::ImageSpace * space)1687 void AppImageLoadingHelper::HandleAppImageStrings(gc::space::ImageSpace* space) {
1688 // Iterate over the string reference offsets stored in the image and intern
1689 // the strings they point to.
1690 ScopedTrace timing("AppImage:InternString");
1691
1692 Runtime* const runtime = Runtime::Current();
1693 InternTable* const intern_table = runtime->GetInternTable();
1694
1695 const bool load_startup_cache = runtime->LoadAppImageStartupCache();
1696
1697 // Add the intern table, removing any conflicts. For conflicts, store the new address in a map
1698 // for faster lookup.
1699 // TODO: Optimize with a bitmap or bloom filter
1700 SafeMap<mirror::String*, mirror::String*> intern_remap;
1701 auto func = [&](InternTable::UnorderedSet& interns)
1702 REQUIRES_SHARED(Locks::mutator_lock_)
1703 REQUIRES(Locks::intern_table_lock_) {
1704 const size_t non_boot_image_strings = intern_table->CountInterns(
1705 /*visit_boot_images=*/false,
1706 /*visit_non_boot_images=*/true);
1707 VLOG(image) << "AppImage:stringsInInternTableSize = " << interns.size();
1708 VLOG(image) << "AppImage:nonBootImageInternStrings = " << non_boot_image_strings;
1709 // Visit the smaller of the two sets to compute the intersection.
1710 if (interns.size() < non_boot_image_strings) {
1711 for (auto it = interns.begin(); it != interns.end(); ) {
1712 ObjPtr<mirror::String> string = it->Read();
1713 ObjPtr<mirror::String> existing = intern_table->LookupWeakLocked(string);
1714 if (existing == nullptr) {
1715 existing = intern_table->LookupStrongLocked(string);
1716 }
1717 if (existing != nullptr) {
1718 intern_remap.Put(string.Ptr(), existing.Ptr());
1719 it = interns.erase(it);
1720 } else {
1721 ++it;
1722 }
1723 }
1724 } else {
1725 intern_table->VisitInterns([&](const GcRoot<mirror::String>& root)
1726 REQUIRES_SHARED(Locks::mutator_lock_)
1727 REQUIRES(Locks::intern_table_lock_) {
1728 auto it = interns.find(root);
1729 if (it != interns.end()) {
1730 ObjPtr<mirror::String> existing = root.Read();
1731 intern_remap.Put(it->Read(), existing.Ptr());
1732 it = interns.erase(it);
1733 }
1734 }, /*visit_boot_images=*/false, /*visit_non_boot_images=*/true);
1735 }
1736 // Sanity check to ensure correctness.
1737 if (kIsDebugBuild) {
1738 for (GcRoot<mirror::String>& root : interns) {
1739 ObjPtr<mirror::String> string = root.Read();
1740 CHECK(intern_table->LookupWeakLocked(string) == nullptr) << string->ToModifiedUtf8();
1741 CHECK(intern_table->LookupStrongLocked(string) == nullptr) << string->ToModifiedUtf8();
1742 }
1743 }
1744 };
1745 intern_table->AddImageStringsToTable(space, func);
1746 if (!intern_remap.empty()) {
1747 VLOG(image) << "AppImage:conflictingInternStrings = " << intern_remap.size();
1748 VisitInternedStringReferences(
1749 space,
1750 load_startup_cache,
1751 [&intern_remap](ObjPtr<mirror::String> str) REQUIRES_SHARED(Locks::mutator_lock_) {
1752 auto it = intern_remap.find(str.Ptr());
1753 if (it != intern_remap.end()) {
1754 return ObjPtr<mirror::String>(it->second);
1755 }
1756 return str;
1757 });
1758 }
1759 }
1760
OpenOatDexFile(const OatFile * oat_file,const char * location,std::string * error_msg)1761 static std::unique_ptr<const DexFile> OpenOatDexFile(const OatFile* oat_file,
1762 const char* location,
1763 std::string* error_msg)
1764 REQUIRES_SHARED(Locks::mutator_lock_) {
1765 DCHECK(error_msg != nullptr);
1766 std::unique_ptr<const DexFile> dex_file;
1767 const OatDexFile* oat_dex_file = oat_file->GetOatDexFile(location, nullptr, error_msg);
1768 if (oat_dex_file == nullptr) {
1769 return std::unique_ptr<const DexFile>();
1770 }
1771 std::string inner_error_msg;
1772 dex_file = oat_dex_file->OpenDexFile(&inner_error_msg);
1773 if (dex_file == nullptr) {
1774 *error_msg = StringPrintf("Failed to open dex file %s from within oat file %s error '%s'",
1775 location,
1776 oat_file->GetLocation().c_str(),
1777 inner_error_msg.c_str());
1778 return std::unique_ptr<const DexFile>();
1779 }
1780
1781 if (dex_file->GetLocationChecksum() != oat_dex_file->GetDexFileLocationChecksum()) {
1782 *error_msg = StringPrintf("Checksums do not match for %s: %x vs %x",
1783 location,
1784 dex_file->GetLocationChecksum(),
1785 oat_dex_file->GetDexFileLocationChecksum());
1786 return std::unique_ptr<const DexFile>();
1787 }
1788 return dex_file;
1789 }
1790
OpenImageDexFiles(gc::space::ImageSpace * space,std::vector<std::unique_ptr<const DexFile>> * out_dex_files,std::string * error_msg)1791 bool ClassLinker::OpenImageDexFiles(gc::space::ImageSpace* space,
1792 std::vector<std::unique_ptr<const DexFile>>* out_dex_files,
1793 std::string* error_msg) {
1794 ScopedAssertNoThreadSuspension nts(__FUNCTION__);
1795 const ImageHeader& header = space->GetImageHeader();
1796 ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches);
1797 DCHECK(dex_caches_object != nullptr);
1798 ObjPtr<mirror::ObjectArray<mirror::DexCache>> dex_caches =
1799 dex_caches_object->AsObjectArray<mirror::DexCache>();
1800 const OatFile* oat_file = space->GetOatFile();
1801 for (auto dex_cache : dex_caches->Iterate()) {
1802 std::string dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8());
1803 std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file,
1804 dex_file_location.c_str(),
1805 error_msg);
1806 if (dex_file == nullptr) {
1807 return false;
1808 }
1809 dex_cache->SetDexFile(dex_file.get());
1810 out_dex_files->push_back(std::move(dex_file));
1811 }
1812 return true;
1813 }
1814
1815 // Helper class for ArtMethod checks when adding an image. Keeps all required functionality
1816 // together and caches some intermediate results.
1817 class ImageSanityChecks final {
1818 public:
CheckObjects(gc::Heap * heap,ClassLinker * class_linker)1819 static void CheckObjects(gc::Heap* heap, ClassLinker* class_linker)
1820 REQUIRES_SHARED(Locks::mutator_lock_) {
1821 ImageSanityChecks isc(heap, class_linker);
1822 auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
1823 DCHECK(obj != nullptr);
1824 CHECK(obj->GetClass() != nullptr) << "Null class in object " << obj;
1825 CHECK(obj->GetClass()->GetClass() != nullptr) << "Null class class " << obj;
1826 if (obj->IsClass()) {
1827 auto klass = obj->AsClass();
1828 for (ArtField& field : klass->GetIFields()) {
1829 CHECK_EQ(field.GetDeclaringClass(), klass);
1830 }
1831 for (ArtField& field : klass->GetSFields()) {
1832 CHECK_EQ(field.GetDeclaringClass(), klass);
1833 }
1834 const PointerSize pointer_size = isc.pointer_size_;
1835 for (ArtMethod& m : klass->GetMethods(pointer_size)) {
1836 isc.SanityCheckArtMethod(&m, klass);
1837 }
1838 ObjPtr<mirror::PointerArray> vtable = klass->GetVTable();
1839 if (vtable != nullptr) {
1840 isc.SanityCheckArtMethodPointerArray(vtable, nullptr);
1841 }
1842 if (klass->ShouldHaveImt()) {
1843 ImTable* imt = klass->GetImt(pointer_size);
1844 for (size_t i = 0; i < ImTable::kSize; ++i) {
1845 isc.SanityCheckArtMethod(imt->Get(i, pointer_size), nullptr);
1846 }
1847 }
1848 if (klass->ShouldHaveEmbeddedVTable()) {
1849 for (int32_t i = 0; i < klass->GetEmbeddedVTableLength(); ++i) {
1850 isc.SanityCheckArtMethod(klass->GetEmbeddedVTableEntry(i, pointer_size), nullptr);
1851 }
1852 }
1853 ObjPtr<mirror::IfTable> iftable = klass->GetIfTable();
1854 for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) {
1855 if (iftable->GetMethodArrayCount(i) > 0) {
1856 isc.SanityCheckArtMethodPointerArray(iftable->GetMethodArray(i), nullptr);
1857 }
1858 }
1859 }
1860 };
1861 heap->VisitObjects(visitor);
1862 }
1863
CheckArtMethodDexCacheArray(gc::Heap * heap,ClassLinker * class_linker,mirror::MethodDexCacheType * arr,size_t size)1864 static void CheckArtMethodDexCacheArray(gc::Heap* heap,
1865 ClassLinker* class_linker,
1866 mirror::MethodDexCacheType* arr,
1867 size_t size)
1868 REQUIRES_SHARED(Locks::mutator_lock_) {
1869 ImageSanityChecks isc(heap, class_linker);
1870 isc.SanityCheckArtMethodDexCacheArray(arr, size);
1871 }
1872
1873 private:
ImageSanityChecks(gc::Heap * heap,ClassLinker * class_linker)1874 ImageSanityChecks(gc::Heap* heap, ClassLinker* class_linker)
1875 : spaces_(heap->GetBootImageSpaces()),
1876 pointer_size_(class_linker->GetImagePointerSize()) {
1877 space_begin_.reserve(spaces_.size());
1878 method_sections_.reserve(spaces_.size());
1879 runtime_method_sections_.reserve(spaces_.size());
1880 for (gc::space::ImageSpace* space : spaces_) {
1881 space_begin_.push_back(space->Begin());
1882 auto& header = space->GetImageHeader();
1883 method_sections_.push_back(&header.GetMethodsSection());
1884 runtime_method_sections_.push_back(&header.GetRuntimeMethodsSection());
1885 }
1886 }
1887
SanityCheckArtMethod(ArtMethod * m,ObjPtr<mirror::Class> expected_class)1888 void SanityCheckArtMethod(ArtMethod* m, ObjPtr<mirror::Class> expected_class)
1889 REQUIRES_SHARED(Locks::mutator_lock_) {
1890 if (m->IsRuntimeMethod()) {
1891 ObjPtr<mirror::Class> declaring_class = m->GetDeclaringClassUnchecked();
1892 CHECK(declaring_class == nullptr) << declaring_class << " " << m->PrettyMethod();
1893 } else if (m->IsCopied()) {
1894 CHECK(m->GetDeclaringClass() != nullptr) << m->PrettyMethod();
1895 } else if (expected_class != nullptr) {
1896 CHECK_EQ(m->GetDeclaringClassUnchecked(), expected_class) << m->PrettyMethod();
1897 }
1898 if (!spaces_.empty()) {
1899 bool contains = false;
1900 for (size_t i = 0; !contains && i != space_begin_.size(); ++i) {
1901 const size_t offset = reinterpret_cast<uint8_t*>(m) - space_begin_[i];
1902 contains = method_sections_[i]->Contains(offset) ||
1903 runtime_method_sections_[i]->Contains(offset);
1904 }
1905 CHECK(contains) << m << " not found";
1906 }
1907 }
1908
SanityCheckArtMethodPointerArray(ObjPtr<mirror::PointerArray> arr,ObjPtr<mirror::Class> expected_class)1909 void SanityCheckArtMethodPointerArray(ObjPtr<mirror::PointerArray> arr,
1910 ObjPtr<mirror::Class> expected_class)
1911 REQUIRES_SHARED(Locks::mutator_lock_) {
1912 CHECK(arr != nullptr);
1913 for (int32_t j = 0; j < arr->GetLength(); ++j) {
1914 auto* method = arr->GetElementPtrSize<ArtMethod*>(j, pointer_size_);
1915 // expected_class == null means we are a dex cache.
1916 if (expected_class != nullptr) {
1917 CHECK(method != nullptr);
1918 }
1919 if (method != nullptr) {
1920 SanityCheckArtMethod(method, expected_class);
1921 }
1922 }
1923 }
1924
SanityCheckArtMethodDexCacheArray(mirror::MethodDexCacheType * arr,size_t size)1925 void SanityCheckArtMethodDexCacheArray(mirror::MethodDexCacheType* arr, size_t size)
1926 REQUIRES_SHARED(Locks::mutator_lock_) {
1927 CHECK_EQ(arr != nullptr, size != 0u);
1928 if (arr != nullptr) {
1929 bool contains = false;
1930 for (auto space : spaces_) {
1931 auto offset = reinterpret_cast<uint8_t*>(arr) - space->Begin();
1932 if (space->GetImageHeader().GetDexCacheArraysSection().Contains(offset)) {
1933 contains = true;
1934 break;
1935 }
1936 }
1937 CHECK(contains);
1938 }
1939 for (size_t j = 0; j < size; ++j) {
1940 auto pair = mirror::DexCache::GetNativePairPtrSize(arr, j, pointer_size_);
1941 ArtMethod* method = pair.object;
1942 // expected_class == null means we are a dex cache.
1943 if (method != nullptr) {
1944 SanityCheckArtMethod(method, nullptr);
1945 }
1946 }
1947 }
1948
1949 const std::vector<gc::space::ImageSpace*>& spaces_;
1950 const PointerSize pointer_size_;
1951
1952 // Cached sections from the spaces.
1953 std::vector<const uint8_t*> space_begin_;
1954 std::vector<const ImageSection*> method_sections_;
1955 std::vector<const ImageSection*> runtime_method_sections_;
1956 };
1957
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)1958 static void VerifyAppImage(const ImageHeader& header,
1959 const Handle<mirror::ClassLoader>& class_loader,
1960 const Handle<mirror::ObjectArray<mirror::DexCache> >& dex_caches,
1961 ClassTable* class_table, gc::space::ImageSpace* space)
1962 REQUIRES_SHARED(Locks::mutator_lock_) {
1963 header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) {
1964 ObjPtr<mirror::Class> klass = method.GetDeclaringClass();
1965 if (klass != nullptr && !Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) {
1966 CHECK_EQ(class_table->LookupByDescriptor(klass), klass)
1967 << mirror::Class::PrettyClass(klass);
1968 }
1969 }, space->Begin(), kRuntimePointerSize);
1970 {
1971 // Verify that all direct interfaces of classes in the class table are also resolved.
1972 std::vector<ObjPtr<mirror::Class>> classes;
1973 auto verify_direct_interfaces_in_table = [&](ObjPtr<mirror::Class> klass)
1974 REQUIRES_SHARED(Locks::mutator_lock_) {
1975 if (!klass->IsPrimitive() && klass->GetClassLoader() == class_loader.Get()) {
1976 classes.push_back(klass);
1977 }
1978 return true;
1979 };
1980 class_table->Visit(verify_direct_interfaces_in_table);
1981 Thread* self = Thread::Current();
1982 for (ObjPtr<mirror::Class> klass : classes) {
1983 for (uint32_t i = 0, num = klass->NumDirectInterfaces(); i != num; ++i) {
1984 CHECK(klass->GetDirectInterface(self, klass, i) != nullptr)
1985 << klass->PrettyDescriptor() << " iface #" << i;
1986 }
1987 }
1988 }
1989 // Check that all non-primitive classes in dex caches are also in the class table.
1990 for (auto dex_cache : dex_caches.ConstIterate<mirror::DexCache>()) {
1991 mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes();
1992 for (int32_t j = 0, num_types = dex_cache->NumResolvedTypes(); j < num_types; j++) {
1993 ObjPtr<mirror::Class> klass = types[j].load(std::memory_order_relaxed).object.Read();
1994 if (klass != nullptr && !klass->IsPrimitive()) {
1995 CHECK(class_table->Contains(klass))
1996 << klass->PrettyDescriptor() << " " << dex_cache->GetDexFile()->GetLocation();
1997 }
1998 }
1999 }
2000 }
2001
AddImageSpace(gc::space::ImageSpace * space,Handle<mirror::ClassLoader> class_loader,std::vector<std::unique_ptr<const DexFile>> * out_dex_files,std::string * error_msg)2002 bool ClassLinker::AddImageSpace(
2003 gc::space::ImageSpace* space,
2004 Handle<mirror::ClassLoader> class_loader,
2005 std::vector<std::unique_ptr<const DexFile>>* out_dex_files,
2006 std::string* error_msg) {
2007 DCHECK(out_dex_files != nullptr);
2008 DCHECK(error_msg != nullptr);
2009 const uint64_t start_time = NanoTime();
2010 const bool app_image = class_loader != nullptr;
2011 const ImageHeader& header = space->GetImageHeader();
2012 ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches);
2013 DCHECK(dex_caches_object != nullptr);
2014 Runtime* const runtime = Runtime::Current();
2015 gc::Heap* const heap = runtime->GetHeap();
2016 Thread* const self = Thread::Current();
2017 // Check that the image is what we are expecting.
2018 if (image_pointer_size_ != space->GetImageHeader().GetPointerSize()) {
2019 *error_msg = StringPrintf("Application image pointer size does not match runtime: %zu vs %zu",
2020 static_cast<size_t>(space->GetImageHeader().GetPointerSize()),
2021 image_pointer_size_);
2022 return false;
2023 }
2024 size_t expected_image_roots = ImageHeader::NumberOfImageRoots(app_image);
2025 if (static_cast<size_t>(header.GetImageRoots()->GetLength()) != expected_image_roots) {
2026 *error_msg = StringPrintf("Expected %zu image roots but got %d",
2027 expected_image_roots,
2028 header.GetImageRoots()->GetLength());
2029 return false;
2030 }
2031 StackHandleScope<3> hs(self);
2032 Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches(
2033 hs.NewHandle(dex_caches_object->AsObjectArray<mirror::DexCache>()));
2034 Handle<mirror::ObjectArray<mirror::Class>> class_roots(hs.NewHandle(
2035 header.GetImageRoot(ImageHeader::kClassRoots)->AsObjectArray<mirror::Class>()));
2036 MutableHandle<mirror::ClassLoader> image_class_loader(hs.NewHandle(
2037 app_image ? header.GetImageRoot(ImageHeader::kAppImageClassLoader)->AsClassLoader()
2038 : nullptr));
2039 DCHECK(class_roots != nullptr);
2040 if (class_roots->GetLength() != static_cast<int32_t>(ClassRoot::kMax)) {
2041 *error_msg = StringPrintf("Expected %d class roots but got %d",
2042 class_roots->GetLength(),
2043 static_cast<int32_t>(ClassRoot::kMax));
2044 return false;
2045 }
2046 // Check against existing class roots to make sure they match the ones in the boot image.
2047 ObjPtr<mirror::ObjectArray<mirror::Class>> existing_class_roots = GetClassRoots();
2048 for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); i++) {
2049 if (class_roots->Get(i) != GetClassRoot(static_cast<ClassRoot>(i), existing_class_roots)) {
2050 *error_msg = "App image class roots must have pointer equality with runtime ones.";
2051 return false;
2052 }
2053 }
2054 const OatFile* oat_file = space->GetOatFile();
2055 if (oat_file->GetOatHeader().GetDexFileCount() !=
2056 static_cast<uint32_t>(dex_caches->GetLength())) {
2057 *error_msg = "Dex cache count and dex file count mismatch while trying to initialize from "
2058 "image";
2059 return false;
2060 }
2061
2062 for (auto dex_cache : dex_caches.Iterate<mirror::DexCache>()) {
2063 std::string dex_file_location = dex_cache->GetLocation()->ToModifiedUtf8();
2064 std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file,
2065 dex_file_location.c_str(),
2066 error_msg);
2067 if (dex_file == nullptr) {
2068 return false;
2069 }
2070
2071 if (app_image) {
2072 // The current dex file field is bogus, overwrite it so that we can get the dex file in the
2073 // loop below.
2074 dex_cache->SetDexFile(dex_file.get());
2075 mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes();
2076 for (int32_t j = 0, num_types = dex_cache->NumResolvedTypes(); j < num_types; j++) {
2077 ObjPtr<mirror::Class> klass = types[j].load(std::memory_order_relaxed).object.Read();
2078 if (klass != nullptr) {
2079 DCHECK(!klass->IsErroneous()) << klass->GetStatus();
2080 }
2081 }
2082 } else {
2083 if (kSanityCheckObjects) {
2084 ImageSanityChecks::CheckArtMethodDexCacheArray(heap,
2085 this,
2086 dex_cache->GetResolvedMethods(),
2087 dex_cache->NumResolvedMethods());
2088 }
2089 // Register dex files, keep track of existing ones that are conflicts.
2090 AppendToBootClassPath(dex_file.get(), dex_cache);
2091 }
2092 out_dex_files->push_back(std::move(dex_file));
2093 }
2094
2095 if (app_image) {
2096 ScopedObjectAccessUnchecked soa(Thread::Current());
2097 ScopedAssertNoThreadSuspension sants("Checking app image", soa.Self());
2098 if (IsBootClassLoader(soa, image_class_loader.Get())) {
2099 *error_msg = "Unexpected BootClassLoader in app image";
2100 return false;
2101 }
2102 }
2103
2104 if (kSanityCheckObjects) {
2105 for (auto dex_cache : dex_caches.Iterate<mirror::DexCache>()) {
2106 for (size_t j = 0; j < dex_cache->NumResolvedFields(); ++j) {
2107 auto* field = dex_cache->GetResolvedField(j, image_pointer_size_);
2108 if (field != nullptr) {
2109 CHECK(field->GetDeclaringClass()->GetClass() != nullptr);
2110 }
2111 }
2112 }
2113 if (!app_image) {
2114 ImageSanityChecks::CheckObjects(heap, this);
2115 }
2116 }
2117
2118 // Set entry point to interpreter if in InterpretOnly mode.
2119 if (!runtime->IsAotCompiler() && runtime->GetInstrumentation()->InterpretOnly()) {
2120 // Set image methods' entry point to interpreter.
2121 header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) {
2122 if (!method.IsRuntimeMethod()) {
2123 DCHECK(method.GetDeclaringClass() != nullptr);
2124 if (!method.IsNative() && !method.IsResolutionMethod()) {
2125 method.SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(),
2126 image_pointer_size_);
2127 }
2128 }
2129 }, space->Begin(), image_pointer_size_);
2130 }
2131
2132 if (interpreter::CanRuntimeUseNterp()) {
2133 // Set image methods' entry point that point to the interpreter bridge to the nterp entry point.
2134 header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) {
2135 if (IsQuickToInterpreterBridge(method.GetEntryPointFromQuickCompiledCode()) &&
2136 interpreter::CanMethodUseNterp(&method)) {
2137 method.SetEntryPointFromQuickCompiledCodePtrSize(interpreter::GetNterpEntryPoint(),
2138 image_pointer_size_);
2139 }
2140 }, space->Begin(), image_pointer_size_);
2141 }
2142
2143 if (runtime->IsVerificationSoftFail()) {
2144 header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) {
2145 if (!method.IsNative() && method.IsInvokable()) {
2146 method.ClearSkipAccessChecks();
2147 }
2148 }, space->Begin(), image_pointer_size_);
2149 }
2150
2151 ClassTable* class_table = nullptr;
2152 {
2153 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
2154 class_table = InsertClassTableForClassLoader(class_loader.Get());
2155 }
2156 // If we have a class table section, read it and use it for verification in
2157 // UpdateAppImageClassLoadersAndDexCaches.
2158 ClassTable::ClassSet temp_set;
2159 const ImageSection& class_table_section = header.GetClassTableSection();
2160 const bool added_class_table = class_table_section.Size() > 0u;
2161 if (added_class_table) {
2162 const uint64_t start_time2 = NanoTime();
2163 size_t read_count = 0;
2164 temp_set = ClassTable::ClassSet(space->Begin() + class_table_section.Offset(),
2165 /*make copy*/false,
2166 &read_count);
2167 VLOG(image) << "Adding class table classes took " << PrettyDuration(NanoTime() - start_time2);
2168 }
2169 if (app_image) {
2170 AppImageLoadingHelper::Update(this, space, class_loader, dex_caches, &temp_set);
2171
2172 {
2173 ScopedTrace trace("AppImage:UpdateClassLoaders");
2174 // Update class loader and resolved strings. If added_class_table is false, the resolved
2175 // strings were forwarded UpdateAppImageClassLoadersAndDexCaches.
2176 ObjPtr<mirror::ClassLoader> loader(class_loader.Get());
2177 for (const ClassTable::TableSlot& root : temp_set) {
2178 // Note: We probably don't need the read barrier unless we copy the app image objects into
2179 // the region space.
2180 ObjPtr<mirror::Class> klass(root.Read());
2181 // Do not update class loader for boot image classes where the app image
2182 // class loader is only the initiating loader but not the defining loader.
2183 // Avoid read barrier since we are comparing against null.
2184 if (klass->GetClassLoader<kDefaultVerifyFlags, kWithoutReadBarrier>() != nullptr) {
2185 klass->SetClassLoader(loader);
2186 }
2187 }
2188 }
2189
2190 if (kBitstringSubtypeCheckEnabled) {
2191 // Every class in the app image has initially SubtypeCheckInfo in the
2192 // Uninitialized state.
2193 //
2194 // The SubtypeCheck invariants imply that a SubtypeCheckInfo is at least Initialized
2195 // after class initialization is complete. The app image ClassStatus as-is
2196 // are almost all ClassStatus::Initialized, and being in the
2197 // SubtypeCheckInfo::kUninitialized state is violating that invariant.
2198 //
2199 // Force every app image class's SubtypeCheck to be at least kIninitialized.
2200 //
2201 // See also ImageWriter::FixupClass.
2202 ScopedTrace trace("AppImage:RecacluateSubtypeCheckBitstrings");
2203 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
2204 for (const ClassTable::TableSlot& root : temp_set) {
2205 SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(root.Read());
2206 }
2207 }
2208 }
2209 if (!oat_file->GetBssGcRoots().empty()) {
2210 // Insert oat file to class table for visiting .bss GC roots.
2211 class_table->InsertOatFile(oat_file);
2212 }
2213
2214 if (added_class_table) {
2215 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
2216 class_table->AddClassSet(std::move(temp_set));
2217 }
2218
2219 if (kIsDebugBuild && app_image) {
2220 // This verification needs to happen after the classes have been added to the class loader.
2221 // Since it ensures classes are in the class table.
2222 ScopedTrace trace("AppImage:Verify");
2223 VerifyAppImage(header, class_loader, dex_caches, class_table, space);
2224 }
2225
2226 VLOG(class_linker) << "Adding image space took " << PrettyDuration(NanoTime() - start_time);
2227 return true;
2228 }
2229
ClassInClassTable(ObjPtr<mirror::Class> klass)2230 bool ClassLinker::ClassInClassTable(ObjPtr<mirror::Class> klass) {
2231 ClassTable* const class_table = ClassTableForClassLoader(klass->GetClassLoader());
2232 return class_table != nullptr && class_table->Contains(klass);
2233 }
2234
VisitClassRoots(RootVisitor * visitor,VisitRootFlags flags)2235 void ClassLinker::VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags) {
2236 // Acquire tracing_enabled before locking class linker lock to prevent lock order violation. Since
2237 // enabling tracing requires the mutator lock, there are no race conditions here.
2238 const bool tracing_enabled = Trace::IsTracingEnabled();
2239 Thread* const self = Thread::Current();
2240 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
2241 if (kUseReadBarrier) {
2242 // We do not track new roots for CC.
2243 DCHECK_EQ(0, flags & (kVisitRootFlagNewRoots |
2244 kVisitRootFlagClearRootLog |
2245 kVisitRootFlagStartLoggingNewRoots |
2246 kVisitRootFlagStopLoggingNewRoots));
2247 }
2248 if ((flags & kVisitRootFlagAllRoots) != 0) {
2249 // Argument for how root visiting deals with ArtField and ArtMethod roots.
2250 // There is 3 GC cases to handle:
2251 // Non moving concurrent:
2252 // This case is easy to handle since the reference members of ArtMethod and ArtFields are held
2253 // live by the class and class roots.
2254 //
2255 // Moving non-concurrent:
2256 // This case needs to call visit VisitNativeRoots in case the classes or dex cache arrays move.
2257 // To prevent missing roots, this case needs to ensure that there is no
2258 // suspend points between the point which we allocate ArtMethod arrays and place them in a
2259 // class which is in the class table.
2260 //
2261 // Moving concurrent:
2262 // Need to make sure to not copy ArtMethods without doing read barriers since the roots are
2263 // marked concurrently and we don't hold the classlinker_classes_lock_ when we do the copy.
2264 //
2265 // Use an unbuffered visitor since the class table uses a temporary GcRoot for holding decoded
2266 // ClassTable::TableSlot. The buffered root visiting would access a stale stack location for
2267 // these objects.
2268 UnbufferedRootVisitor root_visitor(visitor, RootInfo(kRootStickyClass));
2269 boot_class_table_->VisitRoots(root_visitor);
2270 // If tracing is enabled, then mark all the class loaders to prevent unloading.
2271 if ((flags & kVisitRootFlagClassLoader) != 0 || tracing_enabled) {
2272 for (const ClassLoaderData& data : class_loaders_) {
2273 GcRoot<mirror::Object> root(GcRoot<mirror::Object>(self->DecodeJObject(data.weak_root)));
2274 root.VisitRoot(visitor, RootInfo(kRootVMInternal));
2275 }
2276 }
2277 } else if (!kUseReadBarrier && (flags & kVisitRootFlagNewRoots) != 0) {
2278 for (auto& root : new_class_roots_) {
2279 ObjPtr<mirror::Class> old_ref = root.Read<kWithoutReadBarrier>();
2280 root.VisitRoot(visitor, RootInfo(kRootStickyClass));
2281 ObjPtr<mirror::Class> new_ref = root.Read<kWithoutReadBarrier>();
2282 // Concurrent moving GC marked new roots through the to-space invariant.
2283 CHECK_EQ(new_ref, old_ref);
2284 }
2285 for (const OatFile* oat_file : new_bss_roots_boot_oat_files_) {
2286 for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) {
2287 ObjPtr<mirror::Object> old_ref = root.Read<kWithoutReadBarrier>();
2288 if (old_ref != nullptr) {
2289 DCHECK(old_ref->IsClass());
2290 root.VisitRoot(visitor, RootInfo(kRootStickyClass));
2291 ObjPtr<mirror::Object> new_ref = root.Read<kWithoutReadBarrier>();
2292 // Concurrent moving GC marked new roots through the to-space invariant.
2293 CHECK_EQ(new_ref, old_ref);
2294 }
2295 }
2296 }
2297 }
2298 if (!kUseReadBarrier && (flags & kVisitRootFlagClearRootLog) != 0) {
2299 new_class_roots_.clear();
2300 new_bss_roots_boot_oat_files_.clear();
2301 }
2302 if (!kUseReadBarrier && (flags & kVisitRootFlagStartLoggingNewRoots) != 0) {
2303 log_new_roots_ = true;
2304 } else if (!kUseReadBarrier && (flags & kVisitRootFlagStopLoggingNewRoots) != 0) {
2305 log_new_roots_ = false;
2306 }
2307 // We deliberately ignore the class roots in the image since we
2308 // handle image roots by using the MS/CMS rescanning of dirty cards.
2309 }
2310
2311 // Keep in sync with InitCallback. Anything we visit, we need to
2312 // reinit references to when reinitializing a ClassLinker from a
2313 // mapped image.
VisitRoots(RootVisitor * visitor,VisitRootFlags flags)2314 void ClassLinker::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) {
2315 class_roots_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal));
2316 VisitClassRoots(visitor, flags);
2317 // Instead of visiting the find_array_class_cache_ drop it so that it doesn't prevent class
2318 // unloading if we are marking roots.
2319 DropFindArrayClassCache();
2320 }
2321
2322 class VisitClassLoaderClassesVisitor : public ClassLoaderVisitor {
2323 public:
VisitClassLoaderClassesVisitor(ClassVisitor * visitor)2324 explicit VisitClassLoaderClassesVisitor(ClassVisitor* visitor)
2325 : visitor_(visitor),
2326 done_(false) {}
2327
Visit(ObjPtr<mirror::ClassLoader> class_loader)2328 void Visit(ObjPtr<mirror::ClassLoader> class_loader)
2329 REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override {
2330 ClassTable* const class_table = class_loader->GetClassTable();
2331 if (!done_ && class_table != nullptr) {
2332 DefiningClassLoaderFilterVisitor visitor(class_loader, visitor_);
2333 if (!class_table->Visit(visitor)) {
2334 // If the visitor ClassTable returns false it means that we don't need to continue.
2335 done_ = true;
2336 }
2337 }
2338 }
2339
2340 private:
2341 // Class visitor that limits the class visits from a ClassTable to the classes with
2342 // the provided defining class loader. This filter is used to avoid multiple visits
2343 // of the same class which can be recorded for multiple initiating class loaders.
2344 class DefiningClassLoaderFilterVisitor : public ClassVisitor {
2345 public:
DefiningClassLoaderFilterVisitor(ObjPtr<mirror::ClassLoader> defining_class_loader,ClassVisitor * visitor)2346 DefiningClassLoaderFilterVisitor(ObjPtr<mirror::ClassLoader> defining_class_loader,
2347 ClassVisitor* visitor)
2348 : defining_class_loader_(defining_class_loader), visitor_(visitor) { }
2349
operator ()(ObjPtr<mirror::Class> klass)2350 bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) {
2351 if (klass->GetClassLoader() != defining_class_loader_) {
2352 return true;
2353 }
2354 return (*visitor_)(klass);
2355 }
2356
2357 const ObjPtr<mirror::ClassLoader> defining_class_loader_;
2358 ClassVisitor* const visitor_;
2359 };
2360
2361 ClassVisitor* const visitor_;
2362 // If done is true then we don't need to do any more visiting.
2363 bool done_;
2364 };
2365
VisitClassesInternal(ClassVisitor * visitor)2366 void ClassLinker::VisitClassesInternal(ClassVisitor* visitor) {
2367 if (boot_class_table_->Visit(*visitor)) {
2368 VisitClassLoaderClassesVisitor loader_visitor(visitor);
2369 VisitClassLoaders(&loader_visitor);
2370 }
2371 }
2372
VisitClasses(ClassVisitor * visitor)2373 void ClassLinker::VisitClasses(ClassVisitor* visitor) {
2374 Thread* const self = Thread::Current();
2375 ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_);
2376 // Not safe to have thread suspension when we are holding a lock.
2377 if (self != nullptr) {
2378 ScopedAssertNoThreadSuspension nts(__FUNCTION__);
2379 VisitClassesInternal(visitor);
2380 } else {
2381 VisitClassesInternal(visitor);
2382 }
2383 }
2384
2385 class GetClassesInToVector : public ClassVisitor {
2386 public:
operator ()(ObjPtr<mirror::Class> klass)2387 bool operator()(ObjPtr<mirror::Class> klass) override {
2388 classes_.push_back(klass);
2389 return true;
2390 }
2391 std::vector<ObjPtr<mirror::Class>> classes_;
2392 };
2393
2394 class GetClassInToObjectArray : public ClassVisitor {
2395 public:
GetClassInToObjectArray(mirror::ObjectArray<mirror::Class> * arr)2396 explicit GetClassInToObjectArray(mirror::ObjectArray<mirror::Class>* arr)
2397 : arr_(arr), index_(0) {}
2398
operator ()(ObjPtr<mirror::Class> klass)2399 bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) {
2400 ++index_;
2401 if (index_ <= arr_->GetLength()) {
2402 arr_->Set(index_ - 1, klass);
2403 return true;
2404 }
2405 return false;
2406 }
2407
Succeeded() const2408 bool Succeeded() const REQUIRES_SHARED(Locks::mutator_lock_) {
2409 return index_ <= arr_->GetLength();
2410 }
2411
2412 private:
2413 mirror::ObjectArray<mirror::Class>* const arr_;
2414 int32_t index_;
2415 };
2416
VisitClassesWithoutClassesLock(ClassVisitor * visitor)2417 void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor) {
2418 // TODO: it may be possible to avoid secondary storage if we iterate over dex caches. The problem
2419 // is avoiding duplicates.
2420 if (!kMovingClasses) {
2421 ScopedAssertNoThreadSuspension nts(__FUNCTION__);
2422 GetClassesInToVector accumulator;
2423 VisitClasses(&accumulator);
2424 for (ObjPtr<mirror::Class> klass : accumulator.classes_) {
2425 if (!visitor->operator()(klass)) {
2426 return;
2427 }
2428 }
2429 } else {
2430 Thread* const self = Thread::Current();
2431 StackHandleScope<1> hs(self);
2432 auto classes = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr);
2433 // We size the array assuming classes won't be added to the class table during the visit.
2434 // If this assumption fails we iterate again.
2435 while (true) {
2436 size_t class_table_size;
2437 {
2438 ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_);
2439 // Add 100 in case new classes get loaded when we are filling in the object array.
2440 class_table_size = NumZygoteClasses() + NumNonZygoteClasses() + 100;
2441 }
2442 ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this);
2443 classes.Assign(
2444 mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, class_table_size));
2445 CHECK(classes != nullptr); // OOME.
2446 GetClassInToObjectArray accumulator(classes.Get());
2447 VisitClasses(&accumulator);
2448 if (accumulator.Succeeded()) {
2449 break;
2450 }
2451 }
2452 for (int32_t i = 0; i < classes->GetLength(); ++i) {
2453 // If the class table shrank during creation of the clases array we expect null elements. If
2454 // the class table grew then the loop repeats. If classes are created after the loop has
2455 // finished then we don't visit.
2456 ObjPtr<mirror::Class> klass = classes->Get(i);
2457 if (klass != nullptr && !visitor->operator()(klass)) {
2458 return;
2459 }
2460 }
2461 }
2462 }
2463
~ClassLinker()2464 ClassLinker::~ClassLinker() {
2465 Thread* const self = Thread::Current();
2466 for (const ClassLoaderData& data : class_loaders_) {
2467 // CHA unloading analysis is not needed. No negative consequences are expected because
2468 // all the classloaders are deleted at the same time.
2469 DeleteClassLoader(self, data, /*cleanup_cha=*/ false);
2470 }
2471 class_loaders_.clear();
2472 while (!running_visibly_initialized_callbacks_.empty()) {
2473 std::unique_ptr<VisiblyInitializedCallback> callback(
2474 std::addressof(running_visibly_initialized_callbacks_.front()));
2475 running_visibly_initialized_callbacks_.pop_front();
2476 }
2477 }
2478
DeleteClassLoader(Thread * self,const ClassLoaderData & data,bool cleanup_cha)2479 void ClassLinker::DeleteClassLoader(Thread* self, const ClassLoaderData& data, bool cleanup_cha) {
2480 Runtime* const runtime = Runtime::Current();
2481 JavaVMExt* const vm = runtime->GetJavaVM();
2482 vm->DeleteWeakGlobalRef(self, data.weak_root);
2483 // Notify the JIT that we need to remove the methods and/or profiling info.
2484 if (runtime->GetJit() != nullptr) {
2485 jit::JitCodeCache* code_cache = runtime->GetJit()->GetCodeCache();
2486 if (code_cache != nullptr) {
2487 // For the JIT case, RemoveMethodsIn removes the CHA dependencies.
2488 code_cache->RemoveMethodsIn(self, *data.allocator);
2489 }
2490 } else if (cha_ != nullptr) {
2491 // If we don't have a JIT, we need to manually remove the CHA dependencies manually.
2492 cha_->RemoveDependenciesForLinearAlloc(data.allocator);
2493 }
2494 // Cleanup references to single implementation ArtMethods that will be deleted.
2495 if (cleanup_cha) {
2496 CHAOnDeleteUpdateClassVisitor visitor(data.allocator);
2497 data.class_table->Visit<CHAOnDeleteUpdateClassVisitor, kWithoutReadBarrier>(visitor);
2498 }
2499
2500 delete data.allocator;
2501 delete data.class_table;
2502 }
2503
AllocPointerArray(Thread * self,size_t length)2504 ObjPtr<mirror::PointerArray> ClassLinker::AllocPointerArray(Thread* self, size_t length) {
2505 return ObjPtr<mirror::PointerArray>::DownCast(
2506 image_pointer_size_ == PointerSize::k64
2507 ? ObjPtr<mirror::Array>(mirror::LongArray::Alloc(self, length))
2508 : ObjPtr<mirror::Array>(mirror::IntArray::Alloc(self, length)));
2509 }
2510
AllocDexCache(ObjPtr<mirror::String> * out_location,Thread * self,const DexFile & dex_file)2511 ObjPtr<mirror::DexCache> ClassLinker::AllocDexCache(/*out*/ ObjPtr<mirror::String>* out_location,
2512 Thread* self,
2513 const DexFile& dex_file) {
2514 StackHandleScope<1> hs(self);
2515 DCHECK(out_location != nullptr);
2516 auto dex_cache(hs.NewHandle(ObjPtr<mirror::DexCache>::DownCast(
2517 GetClassRoot<mirror::DexCache>(this)->AllocObject(self))));
2518 if (dex_cache == nullptr) {
2519 self->AssertPendingOOMException();
2520 return nullptr;
2521 }
2522 // Use InternWeak() so that the location String can be collected when the ClassLoader
2523 // with this DexCache is collected.
2524 ObjPtr<mirror::String> location = intern_table_->InternWeak(dex_file.GetLocation().c_str());
2525 if (location == nullptr) {
2526 self->AssertPendingOOMException();
2527 return nullptr;
2528 }
2529 *out_location = location;
2530 return dex_cache.Get();
2531 }
2532
AllocAndInitializeDexCache(Thread * self,const DexFile & dex_file,LinearAlloc * linear_alloc)2533 ObjPtr<mirror::DexCache> ClassLinker::AllocAndInitializeDexCache(Thread* self,
2534 const DexFile& dex_file,
2535 LinearAlloc* linear_alloc) {
2536 ObjPtr<mirror::String> location = nullptr;
2537 ObjPtr<mirror::DexCache> dex_cache = AllocDexCache(&location, self, dex_file);
2538 if (dex_cache != nullptr) {
2539 WriterMutexLock mu(self, *Locks::dex_lock_);
2540 DCHECK(location != nullptr);
2541 mirror::DexCache::InitializeDexCache(self,
2542 dex_cache,
2543 location,
2544 &dex_file,
2545 linear_alloc,
2546 image_pointer_size_);
2547 }
2548 return dex_cache;
2549 }
2550
2551 template <bool kMovable, typename PreFenceVisitor>
AllocClass(Thread * self,ObjPtr<mirror::Class> java_lang_Class,uint32_t class_size,const PreFenceVisitor & pre_fence_visitor)2552 ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self,
2553 ObjPtr<mirror::Class> java_lang_Class,
2554 uint32_t class_size,
2555 const PreFenceVisitor& pre_fence_visitor) {
2556 DCHECK_GE(class_size, sizeof(mirror::Class));
2557 gc::Heap* heap = Runtime::Current()->GetHeap();
2558 ObjPtr<mirror::Object> k = (kMovingClasses && kMovable) ?
2559 heap->AllocObject(self, java_lang_Class, class_size, pre_fence_visitor) :
2560 heap->AllocNonMovableObject(self, java_lang_Class, class_size, pre_fence_visitor);
2561 if (UNLIKELY(k == nullptr)) {
2562 self->AssertPendingOOMException();
2563 return nullptr;
2564 }
2565 return k->AsClass();
2566 }
2567
2568 template <bool kMovable>
AllocClass(Thread * self,ObjPtr<mirror::Class> java_lang_Class,uint32_t class_size)2569 ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self,
2570 ObjPtr<mirror::Class> java_lang_Class,
2571 uint32_t class_size) {
2572 mirror::Class::InitializeClassVisitor visitor(class_size);
2573 return AllocClass<kMovable>(self, java_lang_Class, class_size, visitor);
2574 }
2575
AllocClass(Thread * self,uint32_t class_size)2576 ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self, uint32_t class_size) {
2577 return AllocClass(self, GetClassRoot<mirror::Class>(this), class_size);
2578 }
2579
AllocPrimitiveArrayClass(Thread * self,ClassRoot primitive_root,ClassRoot array_root)2580 void ClassLinker::AllocPrimitiveArrayClass(Thread* self,
2581 ClassRoot primitive_root,
2582 ClassRoot array_root) {
2583 // We make this class non-movable for the unlikely case where it were to be
2584 // moved by a sticky-bit (minor) collection when using the Generational
2585 // Concurrent Copying (CC) collector, potentially creating a stale reference
2586 // in the `klass_` field of one of its instances allocated in the Large-Object
2587 // Space (LOS) -- see the comment about the dirty card scanning logic in
2588 // art::gc::collector::ConcurrentCopying::MarkingPhase.
2589 ObjPtr<mirror::Class> array_class = AllocClass</* kMovable= */ false>(
2590 self, GetClassRoot<mirror::Class>(this), mirror::Array::ClassSize(image_pointer_size_));
2591 ObjPtr<mirror::Class> component_type = GetClassRoot(primitive_root, this);
2592 DCHECK(component_type->IsPrimitive());
2593 array_class->SetComponentType(component_type);
2594 SetClassRoot(array_root, array_class);
2595 }
2596
FinishArrayClassSetup(ObjPtr<mirror::Class> array_class)2597 void ClassLinker::FinishArrayClassSetup(ObjPtr<mirror::Class> array_class) {
2598 ObjPtr<mirror::Class> java_lang_Object = GetClassRoot<mirror::Object>(this);
2599 array_class->SetSuperClass(java_lang_Object);
2600 array_class->SetVTable(java_lang_Object->GetVTable());
2601 array_class->SetPrimitiveType(Primitive::kPrimNot);
2602 ObjPtr<mirror::Class> component_type = array_class->GetComponentType();
2603 array_class->SetClassFlags(component_type->IsPrimitive()
2604 ? mirror::kClassFlagNoReferenceFields
2605 : mirror::kClassFlagObjectArray);
2606 array_class->SetClassLoader(component_type->GetClassLoader());
2607 array_class->SetStatusForPrimitiveOrArray(ClassStatus::kLoaded);
2608 array_class->PopulateEmbeddedVTable(image_pointer_size_);
2609 ImTable* object_imt = java_lang_Object->GetImt(image_pointer_size_);
2610 array_class->SetImt(object_imt, image_pointer_size_);
2611 // Skip EnsureSkipAccessChecksMethods(). We can skip the verified status,
2612 // the kAccVerificationAttempted flag is added below, and there are no
2613 // methods that need the kAccSkipAccessChecks flag.
2614 DCHECK_EQ(array_class->NumMethods(), 0u);
2615
2616 // don't need to set new_class->SetObjectSize(..)
2617 // because Object::SizeOf delegates to Array::SizeOf
2618
2619 // All arrays have java/lang/Cloneable and java/io/Serializable as
2620 // interfaces. We need to set that up here, so that stuff like
2621 // "instanceof" works right.
2622
2623 // Use the single, global copies of "interfaces" and "iftable"
2624 // (remember not to free them for arrays).
2625 {
2626 ObjPtr<mirror::IfTable> array_iftable = GetArrayIfTable();
2627 CHECK(array_iftable != nullptr);
2628 array_class->SetIfTable(array_iftable);
2629 }
2630
2631 // Inherit access flags from the component type.
2632 int access_flags = component_type->GetAccessFlags();
2633 // Lose any implementation detail flags; in particular, arrays aren't finalizable.
2634 access_flags &= kAccJavaFlagsMask;
2635 // Arrays can't be used as a superclass or interface, so we want to add "abstract final"
2636 // and remove "interface".
2637 access_flags |= kAccAbstract | kAccFinal;
2638 access_flags &= ~kAccInterface;
2639 // Arrays are access-checks-clean and preverified.
2640 access_flags |= kAccVerificationAttempted;
2641
2642 array_class->SetAccessFlagsDuringLinking(access_flags);
2643
2644 // Array classes are fully initialized either during single threaded startup,
2645 // or from a pre-fence visitor, so visibly initialized.
2646 array_class->SetStatusForPrimitiveOrArray(ClassStatus::kVisiblyInitialized);
2647 }
2648
FinishCoreArrayClassSetup(ClassRoot array_root)2649 void ClassLinker::FinishCoreArrayClassSetup(ClassRoot array_root) {
2650 // Do not hold lock on the array class object, the initialization of
2651 // core array classes is done while the process is still single threaded.
2652 ObjPtr<mirror::Class> array_class = GetClassRoot(array_root, this);
2653 FinishArrayClassSetup(array_class);
2654
2655 std::string temp;
2656 const char* descriptor = array_class->GetDescriptor(&temp);
2657 size_t hash = ComputeModifiedUtf8Hash(descriptor);
2658 ObjPtr<mirror::Class> existing = InsertClass(descriptor, array_class, hash);
2659 CHECK(existing == nullptr);
2660 }
2661
AllocStackTraceElementArray(Thread * self,size_t length)2662 ObjPtr<mirror::ObjectArray<mirror::StackTraceElement>> ClassLinker::AllocStackTraceElementArray(
2663 Thread* self,
2664 size_t length) {
2665 return mirror::ObjectArray<mirror::StackTraceElement>::Alloc(
2666 self, GetClassRoot<mirror::ObjectArray<mirror::StackTraceElement>>(this), length);
2667 }
2668
EnsureResolved(Thread * self,const char * descriptor,ObjPtr<mirror::Class> klass)2669 ObjPtr<mirror::Class> ClassLinker::EnsureResolved(Thread* self,
2670 const char* descriptor,
2671 ObjPtr<mirror::Class> klass) {
2672 DCHECK(klass != nullptr);
2673 if (kIsDebugBuild) {
2674 StackHandleScope<1> hs(self);
2675 HandleWrapperObjPtr<mirror::Class> h = hs.NewHandleWrapper(&klass);
2676 Thread::PoisonObjectPointersIfDebug();
2677 }
2678
2679 // For temporary classes we must wait for them to be retired.
2680 if (init_done_ && klass->IsTemp()) {
2681 CHECK(!klass->IsResolved());
2682 if (klass->IsErroneousUnresolved()) {
2683 ThrowEarlierClassFailure(klass);
2684 return nullptr;
2685 }
2686 StackHandleScope<1> hs(self);
2687 Handle<mirror::Class> h_class(hs.NewHandle(klass));
2688 ObjectLock<mirror::Class> lock(self, h_class);
2689 // Loop and wait for the resolving thread to retire this class.
2690 while (!h_class->IsRetired() && !h_class->IsErroneousUnresolved()) {
2691 lock.WaitIgnoringInterrupts();
2692 }
2693 if (h_class->IsErroneousUnresolved()) {
2694 ThrowEarlierClassFailure(h_class.Get());
2695 return nullptr;
2696 }
2697 CHECK(h_class->IsRetired());
2698 // Get the updated class from class table.
2699 klass = LookupClass(self, descriptor, h_class.Get()->GetClassLoader());
2700 }
2701
2702 // Wait for the class if it has not already been linked.
2703 size_t index = 0;
2704 // Maximum number of yield iterations until we start sleeping.
2705 static const size_t kNumYieldIterations = 1000;
2706 // How long each sleep is in us.
2707 static const size_t kSleepDurationUS = 1000; // 1 ms.
2708 while (!klass->IsResolved() && !klass->IsErroneousUnresolved()) {
2709 StackHandleScope<1> hs(self);
2710 HandleWrapperObjPtr<mirror::Class> h_class(hs.NewHandleWrapper(&klass));
2711 {
2712 ObjectTryLock<mirror::Class> lock(self, h_class);
2713 // Can not use a monitor wait here since it may block when returning and deadlock if another
2714 // thread has locked klass.
2715 if (lock.Acquired()) {
2716 // Check for circular dependencies between classes, the lock is required for SetStatus.
2717 if (!h_class->IsResolved() && h_class->GetClinitThreadId() == self->GetTid()) {
2718 ThrowClassCircularityError(h_class.Get());
2719 mirror::Class::SetStatus(h_class, ClassStatus::kErrorUnresolved, self);
2720 return nullptr;
2721 }
2722 }
2723 }
2724 {
2725 // Handle wrapper deals with klass moving.
2726 ScopedThreadSuspension sts(self, kSuspended);
2727 if (index < kNumYieldIterations) {
2728 sched_yield();
2729 } else {
2730 usleep(kSleepDurationUS);
2731 }
2732 }
2733 ++index;
2734 }
2735
2736 if (klass->IsErroneousUnresolved()) {
2737 ThrowEarlierClassFailure(klass);
2738 return nullptr;
2739 }
2740 // Return the loaded class. No exceptions should be pending.
2741 CHECK(klass->IsResolved()) << klass->PrettyClass();
2742 self->AssertNoPendingException();
2743 return klass;
2744 }
2745
2746 using ClassPathEntry = std::pair<const DexFile*, const dex::ClassDef*>;
2747
2748 // Search a collection of DexFiles for a descriptor
FindInClassPath(const char * descriptor,size_t hash,const std::vector<const DexFile * > & class_path)2749 ClassPathEntry FindInClassPath(const char* descriptor,
2750 size_t hash, const std::vector<const DexFile*>& class_path) {
2751 for (const DexFile* dex_file : class_path) {
2752 DCHECK(dex_file != nullptr);
2753 const dex::ClassDef* dex_class_def = OatDexFile::FindClassDef(*dex_file, descriptor, hash);
2754 if (dex_class_def != nullptr) {
2755 return ClassPathEntry(dex_file, dex_class_def);
2756 }
2757 }
2758 return ClassPathEntry(nullptr, nullptr);
2759 }
2760
FindClassInSharedLibraries(ScopedObjectAccessAlreadyRunnable & soa,Thread * self,const char * descriptor,size_t hash,Handle<mirror::ClassLoader> class_loader,ObjPtr<mirror::Class> * result)2761 bool ClassLinker::FindClassInSharedLibraries(ScopedObjectAccessAlreadyRunnable& soa,
2762 Thread* self,
2763 const char* descriptor,
2764 size_t hash,
2765 Handle<mirror::ClassLoader> class_loader,
2766 /*out*/ ObjPtr<mirror::Class>* result) {
2767 ArtField* field =
2768 jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders);
2769 ObjPtr<mirror::Object> raw_shared_libraries = field->GetObject(class_loader.Get());
2770 if (raw_shared_libraries == nullptr) {
2771 return true;
2772 }
2773
2774 StackHandleScope<2> hs(self);
2775 Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries(
2776 hs.NewHandle(raw_shared_libraries->AsObjectArray<mirror::ClassLoader>()));
2777 MutableHandle<mirror::ClassLoader> temp_loader = hs.NewHandle<mirror::ClassLoader>(nullptr);
2778 for (auto loader : shared_libraries.Iterate<mirror::ClassLoader>()) {
2779 temp_loader.Assign(loader);
2780 if (!FindClassInBaseDexClassLoader(soa, self, descriptor, hash, temp_loader, result)) {
2781 return false; // One of the shared libraries is not supported.
2782 }
2783 if (*result != nullptr) {
2784 return true; // Found the class up the chain.
2785 }
2786 }
2787 return true;
2788 }
2789
FindClassInBaseDexClassLoader(ScopedObjectAccessAlreadyRunnable & soa,Thread * self,const char * descriptor,size_t hash,Handle<mirror::ClassLoader> class_loader,ObjPtr<mirror::Class> * result)2790 bool ClassLinker::FindClassInBaseDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
2791 Thread* self,
2792 const char* descriptor,
2793 size_t hash,
2794 Handle<mirror::ClassLoader> class_loader,
2795 /*out*/ ObjPtr<mirror::Class>* result) {
2796 // Termination case: boot class loader.
2797 if (IsBootClassLoader(soa, class_loader.Get())) {
2798 *result = FindClassInBootClassLoaderClassPath(self, descriptor, hash);
2799 return true;
2800 }
2801
2802 if (IsPathOrDexClassLoader(soa, class_loader) || IsInMemoryDexClassLoader(soa, class_loader)) {
2803 // For regular path or dex class loader the search order is:
2804 // - parent
2805 // - shared libraries
2806 // - class loader dex files
2807
2808 // Handles as RegisterDexFile may allocate dex caches (and cause thread suspension).
2809 StackHandleScope<1> hs(self);
2810 Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent()));
2811 if (!FindClassInBaseDexClassLoader(soa, self, descriptor, hash, h_parent, result)) {
2812 return false; // One of the parents is not supported.
2813 }
2814 if (*result != nullptr) {
2815 return true; // Found the class up the chain.
2816 }
2817
2818 if (!FindClassInSharedLibraries(soa, self, descriptor, hash, class_loader, result)) {
2819 return false; // One of the shared library loader is not supported.
2820 }
2821 if (*result != nullptr) {
2822 return true; // Found the class in a shared library.
2823 }
2824
2825 // Search the current class loader classpath.
2826 *result = FindClassInBaseDexClassLoaderClassPath(soa, descriptor, hash, class_loader);
2827 return !soa.Self()->IsExceptionPending();
2828 }
2829
2830 if (IsDelegateLastClassLoader(soa, class_loader)) {
2831 // For delegate last, the search order is:
2832 // - boot class path
2833 // - shared libraries
2834 // - class loader dex files
2835 // - parent
2836 *result = FindClassInBootClassLoaderClassPath(self, descriptor, hash);
2837 if (*result != nullptr) {
2838 return true; // The class is part of the boot class path.
2839 }
2840 if (self->IsExceptionPending()) {
2841 // Pending exception means there was an error other than ClassNotFound that must be returned
2842 // to the caller.
2843 return false;
2844 }
2845
2846 if (!FindClassInSharedLibraries(soa, self, descriptor, hash, class_loader, result)) {
2847 return false; // One of the shared library loader is not supported.
2848 }
2849 if (*result != nullptr) {
2850 return true; // Found the class in a shared library.
2851 }
2852
2853 *result = FindClassInBaseDexClassLoaderClassPath(soa, descriptor, hash, class_loader);
2854 if (*result != nullptr) {
2855 return true; // Found the class in the current class loader
2856 }
2857 if (self->IsExceptionPending()) {
2858 // Pending exception means there was an error other than ClassNotFound that must be returned
2859 // to the caller.
2860 return false;
2861 }
2862
2863 // Handles as RegisterDexFile may allocate dex caches (and cause thread suspension).
2864 StackHandleScope<1> hs(self);
2865 Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent()));
2866 return FindClassInBaseDexClassLoader(soa, self, descriptor, hash, h_parent, result);
2867 }
2868
2869 // Unsupported class loader.
2870 *result = nullptr;
2871 return false;
2872 }
2873
2874 namespace {
2875
2876 // Matches exceptions caught in DexFile.defineClass.
MatchesDexFileCaughtExceptions(ObjPtr<mirror::Throwable> throwable,ClassLinker * class_linker)2877 ALWAYS_INLINE bool MatchesDexFileCaughtExceptions(ObjPtr<mirror::Throwable> throwable,
2878 ClassLinker* class_linker)
2879 REQUIRES_SHARED(Locks::mutator_lock_) {
2880 return
2881 // ClassNotFoundException.
2882 throwable->InstanceOf(GetClassRoot(ClassRoot::kJavaLangClassNotFoundException,
2883 class_linker))
2884 ||
2885 // NoClassDefFoundError. TODO: Reconsider this. b/130746382.
2886 throwable->InstanceOf(Runtime::Current()->GetPreAllocatedNoClassDefFoundError()->GetClass());
2887 }
2888
2889 // Clear exceptions caught in DexFile.defineClass.
FilterDexFileCaughtExceptions(Thread * self,ClassLinker * class_linker)2890 ALWAYS_INLINE void FilterDexFileCaughtExceptions(Thread* self, ClassLinker* class_linker)
2891 REQUIRES_SHARED(Locks::mutator_lock_) {
2892 if (MatchesDexFileCaughtExceptions(self->GetException(), class_linker)) {
2893 self->ClearException();
2894 }
2895 }
2896
2897 } // namespace
2898
2899 // Finds the class in the boot class loader.
2900 // If the class is found the method returns the resolved class. Otherwise it returns null.
FindClassInBootClassLoaderClassPath(Thread * self,const char * descriptor,size_t hash)2901 ObjPtr<mirror::Class> ClassLinker::FindClassInBootClassLoaderClassPath(Thread* self,
2902 const char* descriptor,
2903 size_t hash) {
2904 ObjPtr<mirror::Class> result = nullptr;
2905 ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_);
2906 if (pair.second != nullptr) {
2907 ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, nullptr);
2908 if (klass != nullptr) {
2909 result = EnsureResolved(self, descriptor, klass);
2910 } else {
2911 result = DefineClass(self,
2912 descriptor,
2913 hash,
2914 ScopedNullHandle<mirror::ClassLoader>(),
2915 *pair.first,
2916 *pair.second);
2917 }
2918 if (result == nullptr) {
2919 CHECK(self->IsExceptionPending()) << descriptor;
2920 FilterDexFileCaughtExceptions(self, this);
2921 }
2922 }
2923 return result;
2924 }
2925
FindClassInBaseDexClassLoaderClassPath(ScopedObjectAccessAlreadyRunnable & soa,const char * descriptor,size_t hash,Handle<mirror::ClassLoader> class_loader)2926 ObjPtr<mirror::Class> ClassLinker::FindClassInBaseDexClassLoaderClassPath(
2927 ScopedObjectAccessAlreadyRunnable& soa,
2928 const char* descriptor,
2929 size_t hash,
2930 Handle<mirror::ClassLoader> class_loader) {
2931 DCHECK(IsPathOrDexClassLoader(soa, class_loader) ||
2932 IsInMemoryDexClassLoader(soa, class_loader) ||
2933 IsDelegateLastClassLoader(soa, class_loader))
2934 << "Unexpected class loader for descriptor " << descriptor;
2935
2936 ObjPtr<mirror::Class> ret;
2937 auto define_class = [&](const DexFile* cp_dex_file) REQUIRES_SHARED(Locks::mutator_lock_) {
2938 const dex::ClassDef* dex_class_def = OatDexFile::FindClassDef(*cp_dex_file, descriptor, hash);
2939 if (dex_class_def != nullptr) {
2940 ObjPtr<mirror::Class> klass = DefineClass(soa.Self(),
2941 descriptor,
2942 hash,
2943 class_loader,
2944 *cp_dex_file,
2945 *dex_class_def);
2946 if (klass == nullptr) {
2947 CHECK(soa.Self()->IsExceptionPending()) << descriptor;
2948 FilterDexFileCaughtExceptions(soa.Self(), this);
2949 // TODO: Is it really right to break here, and not check the other dex files?
2950 } else {
2951 DCHECK(!soa.Self()->IsExceptionPending());
2952 }
2953 ret = klass;
2954 return false; // Found a Class (or error == nullptr), stop visit.
2955 }
2956 return true; // Continue with the next DexFile.
2957 };
2958
2959 VisitClassLoaderDexFiles(soa, class_loader, define_class);
2960 return ret;
2961 }
2962
FindClass(Thread * self,const char * descriptor,Handle<mirror::ClassLoader> class_loader)2963 ObjPtr<mirror::Class> ClassLinker::FindClass(Thread* self,
2964 const char* descriptor,
2965 Handle<mirror::ClassLoader> class_loader) {
2966 DCHECK_NE(*descriptor, '\0') << "descriptor is empty string";
2967 DCHECK(self != nullptr);
2968 self->AssertNoPendingException();
2969 self->PoisonObjectPointers(); // For DefineClass, CreateArrayClass, etc...
2970 if (descriptor[1] == '\0') {
2971 // only the descriptors of primitive types should be 1 character long, also avoid class lookup
2972 // for primitive classes that aren't backed by dex files.
2973 return FindPrimitiveClass(descriptor[0]);
2974 }
2975 const size_t hash = ComputeModifiedUtf8Hash(descriptor);
2976 // Find the class in the loaded classes table.
2977 ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, class_loader.Get());
2978 if (klass != nullptr) {
2979 return EnsureResolved(self, descriptor, klass);
2980 }
2981 // Class is not yet loaded.
2982 if (descriptor[0] != '[' && class_loader == nullptr) {
2983 // Non-array class and the boot class loader, search the boot class path.
2984 ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_);
2985 if (pair.second != nullptr) {
2986 return DefineClass(self,
2987 descriptor,
2988 hash,
2989 ScopedNullHandle<mirror::ClassLoader>(),
2990 *pair.first,
2991 *pair.second);
2992 } else {
2993 // The boot class loader is searched ahead of the application class loader, failures are
2994 // expected and will be wrapped in a ClassNotFoundException. Use the pre-allocated error to
2995 // trigger the chaining with a proper stack trace.
2996 ObjPtr<mirror::Throwable> pre_allocated =
2997 Runtime::Current()->GetPreAllocatedNoClassDefFoundError();
2998 self->SetException(pre_allocated);
2999 return nullptr;
3000 }
3001 }
3002 ObjPtr<mirror::Class> result_ptr;
3003 bool descriptor_equals;
3004 if (descriptor[0] == '[') {
3005 result_ptr = CreateArrayClass(self, descriptor, hash, class_loader);
3006 DCHECK_EQ(result_ptr == nullptr, self->IsExceptionPending());
3007 DCHECK(result_ptr == nullptr || result_ptr->DescriptorEquals(descriptor));
3008 descriptor_equals = true;
3009 } else {
3010 ScopedObjectAccessUnchecked soa(self);
3011 bool known_hierarchy =
3012 FindClassInBaseDexClassLoader(soa, self, descriptor, hash, class_loader, &result_ptr);
3013 if (result_ptr != nullptr) {
3014 // The chain was understood and we found the class. We still need to add the class to
3015 // the class table to protect from racy programs that can try and redefine the path list
3016 // which would change the Class<?> returned for subsequent evaluation of const-class.
3017 DCHECK(known_hierarchy);
3018 DCHECK(result_ptr->DescriptorEquals(descriptor));
3019 descriptor_equals = true;
3020 } else if (!self->IsExceptionPending()) {
3021 // Either the chain wasn't understood or the class wasn't found.
3022 // If there is a pending exception we didn't clear, it is a not a ClassNotFoundException and
3023 // we should return it instead of silently clearing and retrying.
3024 //
3025 // If the chain was understood but we did not find the class, let the Java-side
3026 // rediscover all this and throw the exception with the right stack trace. Note that
3027 // the Java-side could still succeed for racy programs if another thread is actively
3028 // modifying the class loader's path list.
3029
3030 // The runtime is not allowed to call into java from a runtime-thread so just abort.
3031 if (self->IsRuntimeThread()) {
3032 // Oops, we can't call into java so we can't run actual class-loader code.
3033 // This is true for e.g. for the compiler (jit or aot).
3034 ObjPtr<mirror::Throwable> pre_allocated =
3035 Runtime::Current()->GetPreAllocatedNoClassDefFoundError();
3036 self->SetException(pre_allocated);
3037 return nullptr;
3038 }
3039
3040 // Inlined DescriptorToDot(descriptor) with extra validation.
3041 //
3042 // Throw NoClassDefFoundError early rather than potentially load a class only to fail
3043 // the DescriptorEquals() check below and give a confusing error message. For example,
3044 // when native code erroneously calls JNI GetFieldId() with signature "java/lang/String"
3045 // instead of "Ljava/lang/String;", the message below using the "dot" names would be
3046 // "class loader [...] returned class java.lang.String instead of java.lang.String".
3047 size_t descriptor_length = strlen(descriptor);
3048 if (UNLIKELY(descriptor[0] != 'L') ||
3049 UNLIKELY(descriptor[descriptor_length - 1] != ';') ||
3050 UNLIKELY(memchr(descriptor + 1, '.', descriptor_length - 2) != nullptr)) {
3051 ThrowNoClassDefFoundError("Invalid descriptor: %s.", descriptor);
3052 return nullptr;
3053 }
3054
3055 std::string class_name_string(descriptor + 1, descriptor_length - 2);
3056 std::replace(class_name_string.begin(), class_name_string.end(), '/', '.');
3057 if (known_hierarchy &&
3058 fast_class_not_found_exceptions_ &&
3059 !Runtime::Current()->IsJavaDebuggable()) {
3060 // For known hierarchy, we know that the class is going to throw an exception. If we aren't
3061 // debuggable, optimize this path by throwing directly here without going back to Java
3062 // language. This reduces how many ClassNotFoundExceptions happen.
3063 self->ThrowNewExceptionF("Ljava/lang/ClassNotFoundException;",
3064 "%s",
3065 class_name_string.c_str());
3066 } else {
3067 ScopedLocalRef<jobject> class_loader_object(
3068 soa.Env(), soa.AddLocalReference<jobject>(class_loader.Get()));
3069 ScopedLocalRef<jobject> result(soa.Env(), nullptr);
3070 {
3071 ScopedThreadStateChange tsc(self, kNative);
3072 ScopedLocalRef<jobject> class_name_object(
3073 soa.Env(), soa.Env()->NewStringUTF(class_name_string.c_str()));
3074 if (class_name_object.get() == nullptr) {
3075 DCHECK(self->IsExceptionPending()); // OOME.
3076 return nullptr;
3077 }
3078 CHECK(class_loader_object.get() != nullptr);
3079 result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(),
3080 WellKnownClasses::java_lang_ClassLoader_loadClass,
3081 class_name_object.get()));
3082 }
3083 if (result.get() == nullptr && !self->IsExceptionPending()) {
3084 // broken loader - throw NPE to be compatible with Dalvik
3085 ThrowNullPointerException(StringPrintf("ClassLoader.loadClass returned null for %s",
3086 class_name_string.c_str()).c_str());
3087 return nullptr;
3088 }
3089 result_ptr = soa.Decode<mirror::Class>(result.get());
3090 // Check the name of the returned class.
3091 descriptor_equals = (result_ptr != nullptr) && result_ptr->DescriptorEquals(descriptor);
3092 }
3093 } else {
3094 DCHECK(!MatchesDexFileCaughtExceptions(self->GetException(), this));
3095 }
3096 }
3097
3098 if (self->IsExceptionPending()) {
3099 // If the ClassLoader threw or array class allocation failed, pass that exception up.
3100 // However, to comply with the RI behavior, first check if another thread succeeded.
3101 result_ptr = LookupClass(self, descriptor, hash, class_loader.Get());
3102 if (result_ptr != nullptr && !result_ptr->IsErroneous()) {
3103 self->ClearException();
3104 return EnsureResolved(self, descriptor, result_ptr);
3105 }
3106 return nullptr;
3107 }
3108
3109 // Try to insert the class to the class table, checking for mismatch.
3110 ObjPtr<mirror::Class> old;
3111 {
3112 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
3113 ClassTable* const class_table = InsertClassTableForClassLoader(class_loader.Get());
3114 old = class_table->Lookup(descriptor, hash);
3115 if (old == nullptr) {
3116 old = result_ptr; // For the comparison below, after releasing the lock.
3117 if (descriptor_equals) {
3118 class_table->InsertWithHash(result_ptr, hash);
3119 WriteBarrier::ForEveryFieldWrite(class_loader.Get());
3120 } // else throw below, after releasing the lock.
3121 }
3122 }
3123 if (UNLIKELY(old != result_ptr)) {
3124 // Return `old` (even if `!descriptor_equals`) to mimic the RI behavior for parallel
3125 // capable class loaders. (All class loaders are considered parallel capable on Android.)
3126 ObjPtr<mirror::Class> loader_class = class_loader->GetClass();
3127 const char* loader_class_name =
3128 loader_class->GetDexFile().StringByTypeIdx(loader_class->GetDexTypeIndex());
3129 LOG(WARNING) << "Initiating class loader of type " << DescriptorToDot(loader_class_name)
3130 << " is not well-behaved; it returned a different Class for racing loadClass(\""
3131 << DescriptorToDot(descriptor) << "\").";
3132 return EnsureResolved(self, descriptor, old);
3133 }
3134 if (UNLIKELY(!descriptor_equals)) {
3135 std::string result_storage;
3136 const char* result_name = result_ptr->GetDescriptor(&result_storage);
3137 std::string loader_storage;
3138 const char* loader_class_name = class_loader->GetClass()->GetDescriptor(&loader_storage);
3139 ThrowNoClassDefFoundError(
3140 "Initiating class loader of type %s returned class %s instead of %s.",
3141 DescriptorToDot(loader_class_name).c_str(),
3142 DescriptorToDot(result_name).c_str(),
3143 DescriptorToDot(descriptor).c_str());
3144 return nullptr;
3145 }
3146 // Success.
3147 return result_ptr;
3148 }
3149
3150 // Helper for maintaining DefineClass counting. We need to notify callbacks when we start/end a
3151 // define-class and how many recursive DefineClasses we are at in order to allow for doing things
3152 // like pausing class definition.
3153 struct ScopedDefiningClass {
3154 public:
REQUIRES_SHAREDart::ScopedDefiningClass3155 explicit ScopedDefiningClass(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_)
3156 : self_(self), returned_(false) {
3157 Locks::mutator_lock_->AssertSharedHeld(self_);
3158 Runtime::Current()->GetRuntimeCallbacks()->BeginDefineClass();
3159 self_->IncrDefineClassCount();
3160 }
REQUIRES_SHAREDart::ScopedDefiningClass3161 ~ScopedDefiningClass() REQUIRES_SHARED(Locks::mutator_lock_) {
3162 Locks::mutator_lock_->AssertSharedHeld(self_);
3163 CHECK(returned_);
3164 }
3165
Finishart::ScopedDefiningClass3166 ObjPtr<mirror::Class> Finish(Handle<mirror::Class> h_klass)
3167 REQUIRES_SHARED(Locks::mutator_lock_) {
3168 CHECK(!returned_);
3169 self_->DecrDefineClassCount();
3170 Runtime::Current()->GetRuntimeCallbacks()->EndDefineClass();
3171 Thread::PoisonObjectPointersIfDebug();
3172 returned_ = true;
3173 return h_klass.Get();
3174 }
3175
Finishart::ScopedDefiningClass3176 ObjPtr<mirror::Class> Finish(ObjPtr<mirror::Class> klass)
3177 REQUIRES_SHARED(Locks::mutator_lock_) {
3178 StackHandleScope<1> hs(self_);
3179 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
3180 return Finish(h_klass);
3181 }
3182
Finishart::ScopedDefiningClass3183 ObjPtr<mirror::Class> Finish(nullptr_t np ATTRIBUTE_UNUSED)
3184 REQUIRES_SHARED(Locks::mutator_lock_) {
3185 ScopedNullHandle<mirror::Class> snh;
3186 return Finish(snh);
3187 }
3188
3189 private:
3190 Thread* self_;
3191 bool returned_;
3192 };
3193
DefineClass(Thread * self,const char * descriptor,size_t hash,Handle<mirror::ClassLoader> class_loader,const DexFile & dex_file,const dex::ClassDef & dex_class_def)3194 ObjPtr<mirror::Class> ClassLinker::DefineClass(Thread* self,
3195 const char* descriptor,
3196 size_t hash,
3197 Handle<mirror::ClassLoader> class_loader,
3198 const DexFile& dex_file,
3199 const dex::ClassDef& dex_class_def) {
3200 ScopedDefiningClass sdc(self);
3201 StackHandleScope<3> hs(self);
3202 auto klass = hs.NewHandle<mirror::Class>(nullptr);
3203
3204 // Load the class from the dex file.
3205 if (UNLIKELY(!init_done_)) {
3206 // finish up init of hand crafted class_roots_
3207 if (strcmp(descriptor, "Ljava/lang/Object;") == 0) {
3208 klass.Assign(GetClassRoot<mirror::Object>(this));
3209 } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) {
3210 klass.Assign(GetClassRoot<mirror::Class>(this));
3211 } else if (strcmp(descriptor, "Ljava/lang/String;") == 0) {
3212 klass.Assign(GetClassRoot<mirror::String>(this));
3213 } else if (strcmp(descriptor, "Ljava/lang/ref/Reference;") == 0) {
3214 klass.Assign(GetClassRoot<mirror::Reference>(this));
3215 } else if (strcmp(descriptor, "Ljava/lang/DexCache;") == 0) {
3216 klass.Assign(GetClassRoot<mirror::DexCache>(this));
3217 } else if (strcmp(descriptor, "Ldalvik/system/ClassExt;") == 0) {
3218 klass.Assign(GetClassRoot<mirror::ClassExt>(this));
3219 }
3220 }
3221
3222 // For AOT-compilation of an app, we may use a shortened boot class path that excludes
3223 // some runtime modules. Prevent definition of classes in app class loader that could clash
3224 // with these modules as these classes could be resolved differently during execution.
3225 if (class_loader != nullptr &&
3226 Runtime::Current()->IsAotCompiler() &&
3227 IsUpdatableBootClassPathDescriptor(descriptor)) {
3228 ObjPtr<mirror::Throwable> pre_allocated =
3229 Runtime::Current()->GetPreAllocatedNoClassDefFoundError();
3230 self->SetException(pre_allocated);
3231 return sdc.Finish(nullptr);
3232 }
3233
3234 // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied
3235 // code to be executed. We put it up here so we can avoid all the allocations associated with
3236 // creating the class. This can happen with (eg) jit threads.
3237 if (!self->CanLoadClasses()) {
3238 // Make sure we don't try to load anything, potentially causing an infinite loop.
3239 ObjPtr<mirror::Throwable> pre_allocated =
3240 Runtime::Current()->GetPreAllocatedNoClassDefFoundError();
3241 self->SetException(pre_allocated);
3242 return sdc.Finish(nullptr);
3243 }
3244
3245 if (klass == nullptr) {
3246 // Allocate a class with the status of not ready.
3247 // Interface object should get the right size here. Regular class will
3248 // figure out the right size later and be replaced with one of the right
3249 // size when the class becomes resolved.
3250 if (CanAllocClass()) {
3251 klass.Assign(AllocClass(self, SizeOfClassWithoutEmbeddedTables(dex_file, dex_class_def)));
3252 } else {
3253 return sdc.Finish(nullptr);
3254 }
3255 }
3256 if (UNLIKELY(klass == nullptr)) {
3257 self->AssertPendingOOMException();
3258 return sdc.Finish(nullptr);
3259 }
3260 // Get the real dex file. This will return the input if there aren't any callbacks or they do
3261 // nothing.
3262 DexFile const* new_dex_file = nullptr;
3263 dex::ClassDef const* new_class_def = nullptr;
3264 // TODO We should ideally figure out some way to move this after we get a lock on the klass so it
3265 // will only be called once.
3266 Runtime::Current()->GetRuntimeCallbacks()->ClassPreDefine(descriptor,
3267 klass,
3268 class_loader,
3269 dex_file,
3270 dex_class_def,
3271 &new_dex_file,
3272 &new_class_def);
3273 // Check to see if an exception happened during runtime callbacks. Return if so.
3274 if (self->IsExceptionPending()) {
3275 return sdc.Finish(nullptr);
3276 }
3277 ObjPtr<mirror::DexCache> dex_cache = RegisterDexFile(*new_dex_file, class_loader.Get());
3278 if (dex_cache == nullptr) {
3279 self->AssertPendingException();
3280 return sdc.Finish(nullptr);
3281 }
3282 klass->SetDexCache(dex_cache);
3283 SetupClass(*new_dex_file, *new_class_def, klass, class_loader.Get());
3284
3285 // Mark the string class by setting its access flag.
3286 if (UNLIKELY(!init_done_)) {
3287 if (strcmp(descriptor, "Ljava/lang/String;") == 0) {
3288 klass->SetStringClass();
3289 }
3290 }
3291
3292 ObjectLock<mirror::Class> lock(self, klass);
3293 klass->SetClinitThreadId(self->GetTid());
3294 // Make sure we have a valid empty iftable even if there are errors.
3295 klass->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable());
3296
3297 // Add the newly loaded class to the loaded classes table.
3298 ObjPtr<mirror::Class> existing = InsertClass(descriptor, klass.Get(), hash);
3299 if (existing != nullptr) {
3300 // We failed to insert because we raced with another thread. Calling EnsureResolved may cause
3301 // this thread to block.
3302 return sdc.Finish(EnsureResolved(self, descriptor, existing));
3303 }
3304
3305 // Load the fields and other things after we are inserted in the table. This is so that we don't
3306 // end up allocating unfree-able linear alloc resources and then lose the race condition. The
3307 // other reason is that the field roots are only visited from the class table. So we need to be
3308 // inserted before we allocate / fill in these fields.
3309 LoadClass(self, *new_dex_file, *new_class_def, klass);
3310 if (self->IsExceptionPending()) {
3311 VLOG(class_linker) << self->GetException()->Dump();
3312 // An exception occured during load, set status to erroneous while holding klass' lock in case
3313 // notification is necessary.
3314 if (!klass->IsErroneous()) {
3315 mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self);
3316 }
3317 return sdc.Finish(nullptr);
3318 }
3319
3320 // Finish loading (if necessary) by finding parents
3321 CHECK(!klass->IsLoaded());
3322 if (!LoadSuperAndInterfaces(klass, *new_dex_file)) {
3323 // Loading failed.
3324 if (!klass->IsErroneous()) {
3325 mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self);
3326 }
3327 return sdc.Finish(nullptr);
3328 }
3329 CHECK(klass->IsLoaded());
3330
3331 // At this point the class is loaded. Publish a ClassLoad event.
3332 // Note: this may be a temporary class. It is a listener's responsibility to handle this.
3333 Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(klass);
3334
3335 // Link the class (if necessary)
3336 CHECK(!klass->IsResolved());
3337 // TODO: Use fast jobjects?
3338 auto interfaces = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr);
3339
3340 MutableHandle<mirror::Class> h_new_class = hs.NewHandle<mirror::Class>(nullptr);
3341 if (!LinkClass(self, descriptor, klass, interfaces, &h_new_class)) {
3342 // Linking failed.
3343 if (!klass->IsErroneous()) {
3344 mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self);
3345 }
3346 return sdc.Finish(nullptr);
3347 }
3348 self->AssertNoPendingException();
3349 CHECK(h_new_class != nullptr) << descriptor;
3350 CHECK(h_new_class->IsResolved() && !h_new_class->IsErroneousResolved()) << descriptor;
3351
3352 // Instrumentation may have updated entrypoints for all methods of all
3353 // classes. However it could not update methods of this class while we
3354 // were loading it. Now the class is resolved, we can update entrypoints
3355 // as required by instrumentation.
3356 if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) {
3357 // We must be in the kRunnable state to prevent instrumentation from
3358 // suspending all threads to update entrypoints while we are doing it
3359 // for this class.
3360 DCHECK_EQ(self->GetState(), kRunnable);
3361 Runtime::Current()->GetInstrumentation()->InstallStubsForClass(h_new_class.Get());
3362 }
3363
3364 /*
3365 * We send CLASS_PREPARE events to the debugger from here. The
3366 * definition of "preparation" is creating the static fields for a
3367 * class and initializing them to the standard default values, but not
3368 * executing any code (that comes later, during "initialization").
3369 *
3370 * We did the static preparation in LinkClass.
3371 *
3372 * The class has been prepared and resolved but possibly not yet verified
3373 * at this point.
3374 */
3375 Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(klass, h_new_class);
3376
3377 // Notify native debugger of the new class and its layout.
3378 jit::Jit::NewTypeLoadedIfUsingJit(h_new_class.Get());
3379
3380 return sdc.Finish(h_new_class);
3381 }
3382
SizeOfClassWithoutEmbeddedTables(const DexFile & dex_file,const dex::ClassDef & dex_class_def)3383 uint32_t ClassLinker::SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file,
3384 const dex::ClassDef& dex_class_def) {
3385 size_t num_ref = 0;
3386 size_t num_8 = 0;
3387 size_t num_16 = 0;
3388 size_t num_32 = 0;
3389 size_t num_64 = 0;
3390 ClassAccessor accessor(dex_file, dex_class_def);
3391 // We allow duplicate definitions of the same field in a class_data_item
3392 // but ignore the repeated indexes here, b/21868015.
3393 uint32_t last_field_idx = dex::kDexNoIndex;
3394 for (const ClassAccessor::Field& field : accessor.GetStaticFields()) {
3395 uint32_t field_idx = field.GetIndex();
3396 // Ordering enforced by DexFileVerifier.
3397 DCHECK(last_field_idx == dex::kDexNoIndex || last_field_idx <= field_idx);
3398 if (UNLIKELY(field_idx == last_field_idx)) {
3399 continue;
3400 }
3401 last_field_idx = field_idx;
3402 const dex::FieldId& field_id = dex_file.GetFieldId(field_idx);
3403 const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id);
3404 char c = descriptor[0];
3405 switch (c) {
3406 case 'L':
3407 case '[':
3408 num_ref++;
3409 break;
3410 case 'J':
3411 case 'D':
3412 num_64++;
3413 break;
3414 case 'I':
3415 case 'F':
3416 num_32++;
3417 break;
3418 case 'S':
3419 case 'C':
3420 num_16++;
3421 break;
3422 case 'B':
3423 case 'Z':
3424 num_8++;
3425 break;
3426 default:
3427 LOG(FATAL) << "Unknown descriptor: " << c;
3428 UNREACHABLE();
3429 }
3430 }
3431 return mirror::Class::ComputeClassSize(false,
3432 0,
3433 num_8,
3434 num_16,
3435 num_32,
3436 num_64,
3437 num_ref,
3438 image_pointer_size_);
3439 }
3440
3441 // Special case to get oat code without overwriting a trampoline.
GetQuickOatCodeFor(ArtMethod * method)3442 const void* ClassLinker::GetQuickOatCodeFor(ArtMethod* method) {
3443 CHECK(method->IsInvokable()) << method->PrettyMethod();
3444 if (method->IsProxyMethod()) {
3445 return GetQuickProxyInvokeHandler();
3446 }
3447 const void* code = method->GetOatMethodQuickCode(GetImagePointerSize());
3448 if (code != nullptr) {
3449 return code;
3450 }
3451
3452 jit::Jit* jit = Runtime::Current()->GetJit();
3453 if (jit != nullptr) {
3454 code = jit->GetCodeCache()->GetSavedEntryPointOfPreCompiledMethod(method);
3455 if (code != nullptr) {
3456 return code;
3457 }
3458 }
3459
3460 if (method->IsNative()) {
3461 // No code and native? Use generic trampoline.
3462 return GetQuickGenericJniStub();
3463 }
3464
3465 if (interpreter::CanRuntimeUseNterp() && interpreter::CanMethodUseNterp(method)) {
3466 return interpreter::GetNterpEntryPoint();
3467 }
3468
3469 return GetQuickToInterpreterBridge();
3470 }
3471
ShouldUseInterpreterEntrypoint(ArtMethod * method,const void * quick_code)3472 bool ClassLinker::ShouldUseInterpreterEntrypoint(ArtMethod* method, const void* quick_code) {
3473 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
3474 if (UNLIKELY(method->IsNative() || method->IsProxyMethod())) {
3475 return false;
3476 }
3477
3478 if (quick_code == nullptr) {
3479 return true;
3480 }
3481
3482 Runtime* runtime = Runtime::Current();
3483 instrumentation::Instrumentation* instr = runtime->GetInstrumentation();
3484 if (instr->InterpretOnly()) {
3485 return true;
3486 }
3487
3488 if (runtime->GetClassLinker()->IsQuickToInterpreterBridge(quick_code)) {
3489 // Doing this check avoids doing compiled/interpreter transitions.
3490 return true;
3491 }
3492
3493 if (Thread::Current()->IsForceInterpreter()) {
3494 // Force the use of interpreter when it is required by the debugger.
3495 return true;
3496 }
3497
3498 if (Thread::Current()->IsAsyncExceptionPending()) {
3499 // Force use of interpreter to handle async-exceptions
3500 return true;
3501 }
3502
3503 if (quick_code == GetQuickInstrumentationEntryPoint()) {
3504 const void* instr_target = instr->GetCodeForInvoke(method);
3505 DCHECK_NE(instr_target, GetQuickInstrumentationEntryPoint()) << method->PrettyMethod();
3506 return ShouldUseInterpreterEntrypoint(method, instr_target);
3507 }
3508
3509 if (runtime->IsJavaDebuggable()) {
3510 // For simplicity, we ignore precompiled code and go to the interpreter
3511 // assuming we don't already have jitted code.
3512 // We could look at the oat file where `quick_code` is being defined,
3513 // and check whether it's been compiled debuggable, but we decided to
3514 // only rely on the JIT for debuggable apps.
3515 jit::Jit* jit = Runtime::Current()->GetJit();
3516 return (jit == nullptr) || !jit->GetCodeCache()->ContainsPc(quick_code);
3517 }
3518
3519 if (runtime->IsNativeDebuggable()) {
3520 DCHECK(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse());
3521 // If we are doing native debugging, ignore application's AOT code,
3522 // since we want to JIT it (at first use) with extra stackmaps for native
3523 // debugging. We keep however all AOT code from the boot image,
3524 // since the JIT-at-first-use is blocking and would result in non-negligible
3525 // startup performance impact.
3526 return !runtime->GetHeap()->IsInBootImageOatFile(quick_code);
3527 }
3528
3529 return false;
3530 }
3531
FixupStaticTrampolines(ObjPtr<mirror::Class> klass)3532 void ClassLinker::FixupStaticTrampolines(ObjPtr<mirror::Class> klass) {
3533 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
3534 DCHECK(klass->IsVisiblyInitialized()) << klass->PrettyDescriptor();
3535 if (klass->NumDirectMethods() == 0) {
3536 return; // No direct methods => no static methods.
3537 }
3538 if (UNLIKELY(klass->IsProxyClass())) {
3539 return;
3540 }
3541 Runtime* runtime = Runtime::Current();
3542 if (!runtime->IsStarted()) {
3543 if (runtime->IsAotCompiler() || runtime->GetHeap()->HasBootImageSpace()) {
3544 return; // OAT file unavailable.
3545 }
3546 }
3547
3548 const DexFile& dex_file = klass->GetDexFile();
3549 const uint16_t class_def_idx = klass->GetDexClassDefIndex();
3550 CHECK_NE(class_def_idx, DexFile::kDexNoIndex16);
3551 ClassAccessor accessor(dex_file, class_def_idx);
3552 // There should always be class data if there were direct methods.
3553 CHECK(accessor.HasClassData()) << klass->PrettyDescriptor();
3554 bool has_oat_class;
3555 OatFile::OatClass oat_class = OatFile::FindOatClass(dex_file,
3556 klass->GetDexClassDefIndex(),
3557 &has_oat_class);
3558 // Link the code of methods skipped by LinkCode.
3559 for (size_t method_index = 0; method_index < accessor.NumDirectMethods(); ++method_index) {
3560 ArtMethod* method = klass->GetDirectMethod(method_index, image_pointer_size_);
3561 if (!method->IsStatic()) {
3562 // Only update static methods.
3563 continue;
3564 }
3565 const void* quick_code = nullptr;
3566
3567 // In order:
3568 // 1) Check if we have AOT Code.
3569 // 2) Check if we have JIT Code.
3570 // 3) Check if we can use Nterp.
3571 if (has_oat_class) {
3572 OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index);
3573 quick_code = oat_method.GetQuickCode();
3574 }
3575
3576 jit::Jit* jit = runtime->GetJit();
3577 if (quick_code == nullptr && jit != nullptr) {
3578 quick_code = jit->GetCodeCache()->GetSavedEntryPointOfPreCompiledMethod(method);
3579 }
3580
3581 if (quick_code == nullptr &&
3582 interpreter::CanRuntimeUseNterp() &&
3583 interpreter::CanMethodUseNterp(method)) {
3584 quick_code = interpreter::GetNterpEntryPoint();
3585 }
3586
3587 // Check whether the method is native, in which case it's generic JNI.
3588 if (quick_code == nullptr && method->IsNative()) {
3589 quick_code = GetQuickGenericJniStub();
3590 } else if (ShouldUseInterpreterEntrypoint(method, quick_code)) {
3591 // Use interpreter entry point.
3592 if (IsQuickToInterpreterBridge(method->GetEntryPointFromQuickCompiledCode())) {
3593 // If we have the trampoline or the bridge already, no need to update.
3594 // This saves in not dirtying boot image memory.
3595 continue;
3596 }
3597 quick_code = GetQuickToInterpreterBridge();
3598 }
3599 CHECK(quick_code != nullptr);
3600 runtime->GetInstrumentation()->UpdateMethodsCode(method, quick_code);
3601 }
3602 // Ignore virtual methods on the iterator.
3603 }
3604
3605 // Does anything needed to make sure that the compiler will not generate a direct invoke to this
3606 // method. Should only be called on non-invokable methods.
EnsureThrowsInvocationError(ClassLinker * class_linker,ArtMethod * method)3607 inline void EnsureThrowsInvocationError(ClassLinker* class_linker, ArtMethod* method)
3608 REQUIRES_SHARED(Locks::mutator_lock_) {
3609 DCHECK(method != nullptr);
3610 DCHECK(!method->IsInvokable());
3611 method->SetEntryPointFromQuickCompiledCodePtrSize(
3612 class_linker->GetQuickToInterpreterBridgeTrampoline(),
3613 class_linker->GetImagePointerSize());
3614 }
3615
LinkCode(ClassLinker * class_linker,ArtMethod * method,const OatFile::OatClass * oat_class,uint32_t class_def_method_index)3616 static void LinkCode(ClassLinker* class_linker,
3617 ArtMethod* method,
3618 const OatFile::OatClass* oat_class,
3619 uint32_t class_def_method_index) REQUIRES_SHARED(Locks::mutator_lock_) {
3620 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
3621 Runtime* const runtime = Runtime::Current();
3622 if (runtime->IsAotCompiler()) {
3623 // The following code only applies to a non-compiler runtime.
3624 return;
3625 }
3626
3627 // Method shouldn't have already been linked.
3628 DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr);
3629
3630 if (!method->IsInvokable()) {
3631 EnsureThrowsInvocationError(class_linker, method);
3632 return;
3633 }
3634
3635 const void* quick_code = nullptr;
3636 if (oat_class != nullptr) {
3637 // Every kind of method should at least get an invoke stub from the oat_method.
3638 // non-abstract methods also get their code pointers.
3639 const OatFile::OatMethod oat_method = oat_class->GetOatMethod(class_def_method_index);
3640 quick_code = oat_method.GetQuickCode();
3641 }
3642
3643 bool enter_interpreter = class_linker->ShouldUseInterpreterEntrypoint(method, quick_code);
3644
3645 // Note: this mimics the logic in image_writer.cc that installs the resolution
3646 // stub only if we have compiled code and the method needs a class initialization
3647 // check.
3648 if (quick_code == nullptr) {
3649 method->SetEntryPointFromQuickCompiledCode(
3650 method->IsNative() ? GetQuickGenericJniStub() : GetQuickToInterpreterBridge());
3651 } else if (enter_interpreter) {
3652 method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge());
3653 } else if (NeedsClinitCheckBeforeCall(method)) {
3654 DCHECK(!method->GetDeclaringClass()->IsVisiblyInitialized()); // Actually ClassStatus::Idx.
3655 // If we do have code but the method needs a class initialization check before calling
3656 // that code, install the resolution stub that will perform the check.
3657 // It will be replaced by the proper entry point by ClassLinker::FixupStaticTrampolines
3658 // after initializing class (see ClassLinker::InitializeClass method).
3659 method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionStub());
3660 } else {
3661 method->SetEntryPointFromQuickCompiledCode(quick_code);
3662 }
3663
3664 if (method->IsNative()) {
3665 // Unregistering restores the dlsym lookup stub.
3666 method->UnregisterNative();
3667
3668 if (enter_interpreter || quick_code == nullptr) {
3669 // We have a native method here without code. Then it should have the generic JNI
3670 // trampoline as entrypoint.
3671 // TODO: this doesn't handle all the cases where trampolines may be installed.
3672 DCHECK(class_linker->IsQuickGenericJniStub(method->GetEntryPointFromQuickCompiledCode()));
3673 }
3674 }
3675 }
3676
SetupClass(const DexFile & dex_file,const dex::ClassDef & dex_class_def,Handle<mirror::Class> klass,ObjPtr<mirror::ClassLoader> class_loader)3677 void ClassLinker::SetupClass(const DexFile& dex_file,
3678 const dex::ClassDef& dex_class_def,
3679 Handle<mirror::Class> klass,
3680 ObjPtr<mirror::ClassLoader> class_loader) {
3681 CHECK(klass != nullptr);
3682 CHECK(klass->GetDexCache() != nullptr);
3683 CHECK_EQ(ClassStatus::kNotReady, klass->GetStatus());
3684 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
3685 CHECK(descriptor != nullptr);
3686
3687 klass->SetClass(GetClassRoot<mirror::Class>(this));
3688 uint32_t access_flags = dex_class_def.GetJavaAccessFlags();
3689 CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U);
3690 klass->SetAccessFlagsDuringLinking(access_flags);
3691 klass->SetClassLoader(class_loader);
3692 DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot);
3693 mirror::Class::SetStatus(klass, ClassStatus::kIdx, nullptr);
3694
3695 klass->SetDexClassDefIndex(dex_file.GetIndexForClassDef(dex_class_def));
3696 klass->SetDexTypeIndex(dex_class_def.class_idx_);
3697 }
3698
AllocArtFieldArray(Thread * self,LinearAlloc * allocator,size_t length)3699 LengthPrefixedArray<ArtField>* ClassLinker::AllocArtFieldArray(Thread* self,
3700 LinearAlloc* allocator,
3701 size_t length) {
3702 if (length == 0) {
3703 return nullptr;
3704 }
3705 // If the ArtField alignment changes, review all uses of LengthPrefixedArray<ArtField>.
3706 static_assert(alignof(ArtField) == 4, "ArtField alignment is expected to be 4.");
3707 size_t storage_size = LengthPrefixedArray<ArtField>::ComputeSize(length);
3708 void* array_storage = allocator->Alloc(self, storage_size);
3709 auto* ret = new(array_storage) LengthPrefixedArray<ArtField>(length);
3710 CHECK(ret != nullptr);
3711 std::uninitialized_fill_n(&ret->At(0), length, ArtField());
3712 return ret;
3713 }
3714
AllocArtMethodArray(Thread * self,LinearAlloc * allocator,size_t length)3715 LengthPrefixedArray<ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self,
3716 LinearAlloc* allocator,
3717 size_t length) {
3718 if (length == 0) {
3719 return nullptr;
3720 }
3721 const size_t method_alignment = ArtMethod::Alignment(image_pointer_size_);
3722 const size_t method_size = ArtMethod::Size(image_pointer_size_);
3723 const size_t storage_size =
3724 LengthPrefixedArray<ArtMethod>::ComputeSize(length, method_size, method_alignment);
3725 void* array_storage = allocator->Alloc(self, storage_size);
3726 auto* ret = new (array_storage) LengthPrefixedArray<ArtMethod>(length);
3727 CHECK(ret != nullptr);
3728 for (size_t i = 0; i < length; ++i) {
3729 new(reinterpret_cast<void*>(&ret->At(i, method_size, method_alignment))) ArtMethod;
3730 }
3731 return ret;
3732 }
3733
GetAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader)3734 LinearAlloc* ClassLinker::GetAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) {
3735 if (class_loader == nullptr) {
3736 return Runtime::Current()->GetLinearAlloc();
3737 }
3738 LinearAlloc* allocator = class_loader->GetAllocator();
3739 DCHECK(allocator != nullptr);
3740 return allocator;
3741 }
3742
GetOrCreateAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader)3743 LinearAlloc* ClassLinker::GetOrCreateAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) {
3744 if (class_loader == nullptr) {
3745 return Runtime::Current()->GetLinearAlloc();
3746 }
3747 WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
3748 LinearAlloc* allocator = class_loader->GetAllocator();
3749 if (allocator == nullptr) {
3750 RegisterClassLoader(class_loader);
3751 allocator = class_loader->GetAllocator();
3752 CHECK(allocator != nullptr);
3753 }
3754 return allocator;
3755 }
3756
LoadClass(Thread * self,const DexFile & dex_file,const dex::ClassDef & dex_class_def,Handle<mirror::Class> klass)3757 void ClassLinker::LoadClass(Thread* self,
3758 const DexFile& dex_file,
3759 const dex::ClassDef& dex_class_def,
3760 Handle<mirror::Class> klass) {
3761 ClassAccessor accessor(dex_file,
3762 dex_class_def,
3763 /* parse_hiddenapi_class_data= */ klass->IsBootStrapClassLoaded());
3764 if (!accessor.HasClassData()) {
3765 return;
3766 }
3767 Runtime* const runtime = Runtime::Current();
3768 {
3769 // Note: We cannot have thread suspension until the field and method arrays are setup or else
3770 // Class::VisitFieldRoots may miss some fields or methods.
3771 ScopedAssertNoThreadSuspension nts(__FUNCTION__);
3772 // Load static fields.
3773 // We allow duplicate definitions of the same field in a class_data_item
3774 // but ignore the repeated indexes here, b/21868015.
3775 LinearAlloc* const allocator = GetAllocatorForClassLoader(klass->GetClassLoader());
3776 LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self,
3777 allocator,
3778 accessor.NumStaticFields());
3779 LengthPrefixedArray<ArtField>* ifields = AllocArtFieldArray(self,
3780 allocator,
3781 accessor.NumInstanceFields());
3782 size_t num_sfields = 0u;
3783 size_t num_ifields = 0u;
3784 uint32_t last_static_field_idx = 0u;
3785 uint32_t last_instance_field_idx = 0u;
3786
3787 // Methods
3788 bool has_oat_class = false;
3789 const OatFile::OatClass oat_class = (runtime->IsStarted() && !runtime->IsAotCompiler())
3790 ? OatFile::FindOatClass(dex_file, klass->GetDexClassDefIndex(), &has_oat_class)
3791 : OatFile::OatClass::Invalid();
3792 const OatFile::OatClass* oat_class_ptr = has_oat_class ? &oat_class : nullptr;
3793 klass->SetMethodsPtr(
3794 AllocArtMethodArray(self, allocator, accessor.NumMethods()),
3795 accessor.NumDirectMethods(),
3796 accessor.NumVirtualMethods());
3797 size_t class_def_method_index = 0;
3798 uint32_t last_dex_method_index = dex::kDexNoIndex;
3799 size_t last_class_def_method_index = 0;
3800
3801 // Use the visitor since the ranged based loops are bit slower from seeking. Seeking to the
3802 // methods needs to decode all of the fields.
3803 accessor.VisitFieldsAndMethods([&](
3804 const ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) {
3805 uint32_t field_idx = field.GetIndex();
3806 DCHECK_GE(field_idx, last_static_field_idx); // Ordering enforced by DexFileVerifier.
3807 if (num_sfields == 0 || LIKELY(field_idx > last_static_field_idx)) {
3808 LoadField(field, klass, &sfields->At(num_sfields));
3809 ++num_sfields;
3810 last_static_field_idx = field_idx;
3811 }
3812 }, [&](const ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) {
3813 uint32_t field_idx = field.GetIndex();
3814 DCHECK_GE(field_idx, last_instance_field_idx); // Ordering enforced by DexFileVerifier.
3815 if (num_ifields == 0 || LIKELY(field_idx > last_instance_field_idx)) {
3816 LoadField(field, klass, &ifields->At(num_ifields));
3817 ++num_ifields;
3818 last_instance_field_idx = field_idx;
3819 }
3820 }, [&](const ClassAccessor::Method& method) REQUIRES_SHARED(Locks::mutator_lock_) {
3821 ArtMethod* art_method = klass->GetDirectMethodUnchecked(class_def_method_index,
3822 image_pointer_size_);
3823 LoadMethod(dex_file, method, klass, art_method);
3824 LinkCode(this, art_method, oat_class_ptr, class_def_method_index);
3825 uint32_t it_method_index = method.GetIndex();
3826 if (last_dex_method_index == it_method_index) {
3827 // duplicate case
3828 art_method->SetMethodIndex(last_class_def_method_index);
3829 } else {
3830 art_method->SetMethodIndex(class_def_method_index);
3831 last_dex_method_index = it_method_index;
3832 last_class_def_method_index = class_def_method_index;
3833 }
3834 ++class_def_method_index;
3835 }, [&](const ClassAccessor::Method& method) REQUIRES_SHARED(Locks::mutator_lock_) {
3836 ArtMethod* art_method = klass->GetVirtualMethodUnchecked(
3837 class_def_method_index - accessor.NumDirectMethods(),
3838 image_pointer_size_);
3839 LoadMethod(dex_file, method, klass, art_method);
3840 LinkCode(this, art_method, oat_class_ptr, class_def_method_index);
3841 ++class_def_method_index;
3842 });
3843
3844 if (UNLIKELY(num_ifields + num_sfields != accessor.NumFields())) {
3845 LOG(WARNING) << "Duplicate fields in class " << klass->PrettyDescriptor()
3846 << " (unique static fields: " << num_sfields << "/" << accessor.NumStaticFields()
3847 << ", unique instance fields: " << num_ifields << "/" << accessor.NumInstanceFields()
3848 << ")";
3849 // NOTE: Not shrinking the over-allocated sfields/ifields, just setting size.
3850 if (sfields != nullptr) {
3851 sfields->SetSize(num_sfields);
3852 }
3853 if (ifields != nullptr) {
3854 ifields->SetSize(num_ifields);
3855 }
3856 }
3857 // Set the field arrays.
3858 klass->SetSFieldsPtr(sfields);
3859 DCHECK_EQ(klass->NumStaticFields(), num_sfields);
3860 klass->SetIFieldsPtr(ifields);
3861 DCHECK_EQ(klass->NumInstanceFields(), num_ifields);
3862 }
3863 // Ensure that the card is marked so that remembered sets pick up native roots.
3864 WriteBarrier::ForEveryFieldWrite(klass.Get());
3865 self->AllowThreadSuspension();
3866 }
3867
LoadField(const ClassAccessor::Field & field,Handle<mirror::Class> klass,ArtField * dst)3868 void ClassLinker::LoadField(const ClassAccessor::Field& field,
3869 Handle<mirror::Class> klass,
3870 ArtField* dst) {
3871 const uint32_t field_idx = field.GetIndex();
3872 dst->SetDexFieldIndex(field_idx);
3873 dst->SetDeclaringClass(klass.Get());
3874
3875 // Get access flags from the DexFile and set hiddenapi runtime access flags.
3876 dst->SetAccessFlags(field.GetAccessFlags() | hiddenapi::CreateRuntimeFlags(field));
3877 }
3878
LoadMethod(const DexFile & dex_file,const ClassAccessor::Method & method,Handle<mirror::Class> klass,ArtMethod * dst)3879 void ClassLinker::LoadMethod(const DexFile& dex_file,
3880 const ClassAccessor::Method& method,
3881 Handle<mirror::Class> klass,
3882 ArtMethod* dst) {
3883 const uint32_t dex_method_idx = method.GetIndex();
3884 const dex::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
3885 const char* method_name = dex_file.StringDataByIdx(method_id.name_idx_);
3886
3887 ScopedAssertNoThreadSuspension ants("LoadMethod");
3888 dst->SetDexMethodIndex(dex_method_idx);
3889 dst->SetDeclaringClass(klass.Get());
3890 dst->SetCodeItemOffset(method.GetCodeItemOffset());
3891
3892 // Get access flags from the DexFile and set hiddenapi runtime access flags.
3893 uint32_t access_flags = method.GetAccessFlags() | hiddenapi::CreateRuntimeFlags(method);
3894
3895 if (UNLIKELY(strcmp("finalize", method_name) == 0)) {
3896 // Set finalizable flag on declaring class.
3897 if (strcmp("V", dex_file.GetShorty(method_id.proto_idx_)) == 0) {
3898 // Void return type.
3899 if (klass->GetClassLoader() != nullptr) { // All non-boot finalizer methods are flagged.
3900 klass->SetFinalizable();
3901 } else {
3902 std::string temp;
3903 const char* klass_descriptor = klass->GetDescriptor(&temp);
3904 // The Enum class declares a "final" finalize() method to prevent subclasses from
3905 // introducing a finalizer. We don't want to set the finalizable flag for Enum or its
3906 // subclasses, so we exclude it here.
3907 // We also want to avoid setting the flag on Object, where we know that finalize() is
3908 // empty.
3909 if (strcmp(klass_descriptor, "Ljava/lang/Object;") != 0 &&
3910 strcmp(klass_descriptor, "Ljava/lang/Enum;") != 0) {
3911 klass->SetFinalizable();
3912 }
3913 }
3914 }
3915 } else if (method_name[0] == '<') {
3916 // Fix broken access flags for initializers. Bug 11157540.
3917 bool is_init = (strcmp("<init>", method_name) == 0);
3918 bool is_clinit = !is_init && (strcmp("<clinit>", method_name) == 0);
3919 if (UNLIKELY(!is_init && !is_clinit)) {
3920 LOG(WARNING) << "Unexpected '<' at start of method name " << method_name;
3921 } else {
3922 if (UNLIKELY((access_flags & kAccConstructor) == 0)) {
3923 LOG(WARNING) << method_name << " didn't have expected constructor access flag in class "
3924 << klass->PrettyDescriptor() << " in dex file " << dex_file.GetLocation();
3925 access_flags |= kAccConstructor;
3926 }
3927 }
3928 }
3929 if (UNLIKELY((access_flags & kAccNative) != 0u)) {
3930 // Check if the native method is annotated with @FastNative or @CriticalNative.
3931 access_flags |= annotations::GetNativeMethodAnnotationAccessFlags(
3932 dex_file, dst->GetClassDef(), dex_method_idx);
3933 }
3934 dst->SetAccessFlags(access_flags);
3935 // Must be done after SetAccessFlags since IsAbstract depends on it.
3936 if (klass->IsInterface() && dst->IsAbstract()) {
3937 dst->CalculateAndSetImtIndex();
3938 }
3939 }
3940
AppendToBootClassPath(Thread * self,const DexFile * dex_file)3941 void ClassLinker::AppendToBootClassPath(Thread* self, const DexFile* dex_file) {
3942 ObjPtr<mirror::DexCache> dex_cache = AllocAndInitializeDexCache(
3943 self,
3944 *dex_file,
3945 Runtime::Current()->GetLinearAlloc());
3946 CHECK(dex_cache != nullptr) << "Failed to allocate dex cache for " << dex_file->GetLocation();
3947 AppendToBootClassPath(dex_file, dex_cache);
3948 }
3949
AppendToBootClassPath(const DexFile * dex_file,ObjPtr<mirror::DexCache> dex_cache)3950 void ClassLinker::AppendToBootClassPath(const DexFile* dex_file,
3951 ObjPtr<mirror::DexCache> dex_cache) {
3952 CHECK(dex_file != nullptr);
3953 CHECK(dex_cache != nullptr) << dex_file->GetLocation();
3954 boot_class_path_.push_back(dex_file);
3955 WriterMutexLock mu(Thread::Current(), *Locks::dex_lock_);
3956 RegisterDexFileLocked(*dex_file, dex_cache, /* class_loader= */ nullptr);
3957 }
3958
RegisterDexFileLocked(const DexFile & dex_file,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader)3959 void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file,
3960 ObjPtr<mirror::DexCache> dex_cache,
3961 ObjPtr<mirror::ClassLoader> class_loader) {
3962 Thread* const self = Thread::Current();
3963 Locks::dex_lock_->AssertExclusiveHeld(self);
3964 CHECK(dex_cache != nullptr) << dex_file.GetLocation();
3965 // For app images, the dex cache location may be a suffix of the dex file location since the
3966 // dex file location is an absolute path.
3967 const std::string dex_cache_location = dex_cache->GetLocation()->ToModifiedUtf8();
3968 const size_t dex_cache_length = dex_cache_location.length();
3969 CHECK_GT(dex_cache_length, 0u) << dex_file.GetLocation();
3970 std::string dex_file_location = dex_file.GetLocation();
3971 // The following paths checks don't work on preopt when using boot dex files, where the dex
3972 // cache location is the one on device, and the dex_file's location is the one on host.
3973 if (!(Runtime::Current()->IsAotCompiler() && class_loader == nullptr && !kIsTargetBuild)) {
3974 CHECK_GE(dex_file_location.length(), dex_cache_length)
3975 << dex_cache_location << " " << dex_file.GetLocation();
3976 const std::string dex_file_suffix = dex_file_location.substr(
3977 dex_file_location.length() - dex_cache_length,
3978 dex_cache_length);
3979 // Example dex_cache location is SettingsProvider.apk and
3980 // dex file location is /system/priv-app/SettingsProvider/SettingsProvider.apk
3981 CHECK_EQ(dex_cache_location, dex_file_suffix);
3982 }
3983 const OatFile* oat_file =
3984 (dex_file.GetOatDexFile() != nullptr) ? dex_file.GetOatDexFile()->GetOatFile() : nullptr;
3985 // Clean up pass to remove null dex caches; null dex caches can occur due to class unloading
3986 // and we are lazily removing null entries. Also check if we need to initialize OatFile data
3987 // (.data.bimg.rel.ro and .bss sections) needed for code execution.
3988 bool initialize_oat_file_data = (oat_file != nullptr) && oat_file->IsExecutable();
3989 JavaVMExt* const vm = self->GetJniEnv()->GetVm();
3990 for (auto it = dex_caches_.begin(); it != dex_caches_.end(); ) {
3991 DexCacheData data = *it;
3992 if (self->IsJWeakCleared(data.weak_root)) {
3993 vm->DeleteWeakGlobalRef(self, data.weak_root);
3994 it = dex_caches_.erase(it);
3995 } else {
3996 if (initialize_oat_file_data &&
3997 it->dex_file->GetOatDexFile() != nullptr &&
3998 it->dex_file->GetOatDexFile()->GetOatFile() == oat_file) {
3999 initialize_oat_file_data = false; // Already initialized.
4000 }
4001 ++it;
4002 }
4003 }
4004 if (initialize_oat_file_data) {
4005 oat_file->InitializeRelocations();
4006 }
4007 // Let hiddenapi assign a domain to the newly registered dex file.
4008 hiddenapi::InitializeDexFileDomain(dex_file, class_loader);
4009
4010 jweak dex_cache_jweak = vm->AddWeakGlobalRef(self, dex_cache);
4011 dex_cache->SetDexFile(&dex_file);
4012 DexCacheData data;
4013 data.weak_root = dex_cache_jweak;
4014 data.dex_file = dex_cache->GetDexFile();
4015 data.class_table = ClassTableForClassLoader(class_loader);
4016 AddNativeDebugInfoForDex(self, data.dex_file);
4017 DCHECK(data.class_table != nullptr);
4018 // Make sure to hold the dex cache live in the class table. This case happens for the boot class
4019 // path dex caches without an image.
4020 data.class_table->InsertStrongRoot(dex_cache);
4021 // Make sure that the dex cache holds the classloader live.
4022 dex_cache->SetClassLoader(class_loader);
4023 if (class_loader != nullptr) {
4024 // Since we added a strong root to the class table, do the write barrier as required for
4025 // remembered sets and generational GCs.
4026 WriteBarrier::ForEveryFieldWrite(class_loader);
4027 }
4028 dex_caches_.push_back(data);
4029 }
4030
DecodeDexCacheLocked(Thread * self,const DexCacheData * data)4031 ObjPtr<mirror::DexCache> ClassLinker::DecodeDexCacheLocked(Thread* self, const DexCacheData* data) {
4032 return data != nullptr
4033 ? ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data->weak_root))
4034 : nullptr;
4035 }
4036
IsSameClassLoader(ObjPtr<mirror::DexCache> dex_cache,const DexCacheData * data,ObjPtr<mirror::ClassLoader> class_loader)4037 bool ClassLinker::IsSameClassLoader(
4038 ObjPtr<mirror::DexCache> dex_cache,
4039 const DexCacheData* data,
4040 ObjPtr<mirror::ClassLoader> class_loader) {
4041 CHECK(data != nullptr);
4042 DCHECK_EQ(dex_cache->GetDexFile(), data->dex_file);
4043 return data->class_table == ClassTableForClassLoader(class_loader);
4044 }
4045
RegisterExistingDexCache(ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader)4046 void ClassLinker::RegisterExistingDexCache(ObjPtr<mirror::DexCache> dex_cache,
4047 ObjPtr<mirror::ClassLoader> class_loader) {
4048 SCOPED_TRACE << __FUNCTION__ << " " << dex_cache->GetDexFile()->GetLocation();
4049 Thread* self = Thread::Current();
4050 StackHandleScope<2> hs(self);
4051 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(dex_cache));
4052 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
4053 const DexFile* dex_file = dex_cache->GetDexFile();
4054 DCHECK(dex_file != nullptr) << "Attempt to register uninitialized dex_cache object!";
4055 if (kIsDebugBuild) {
4056 ReaderMutexLock mu(self, *Locks::dex_lock_);
4057 const DexCacheData* old_data = FindDexCacheDataLocked(*dex_file);
4058 ObjPtr<mirror::DexCache> old_dex_cache = DecodeDexCacheLocked(self, old_data);
4059 DCHECK(old_dex_cache.IsNull()) << "Attempt to manually register a dex cache thats already "
4060 << "been registered on dex file " << dex_file->GetLocation();
4061 }
4062 ClassTable* table;
4063 {
4064 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
4065 table = InsertClassTableForClassLoader(h_class_loader.Get());
4066 }
4067 // Avoid a deadlock between a garbage collecting thread running a checkpoint,
4068 // a thread holding the dex lock and blocking on a condition variable regarding
4069 // weak references access, and a thread blocking on the dex lock.
4070 gc::ScopedGCCriticalSection gcs(self, gc::kGcCauseClassLinker, gc::kCollectorTypeClassLinker);
4071 WriterMutexLock mu(self, *Locks::dex_lock_);
4072 RegisterDexFileLocked(*dex_file, h_dex_cache.Get(), h_class_loader.Get());
4073 table->InsertStrongRoot(h_dex_cache.Get());
4074 if (h_class_loader.Get() != nullptr) {
4075 // Since we added a strong root to the class table, do the write barrier as required for
4076 // remembered sets and generational GCs.
4077 WriteBarrier::ForEveryFieldWrite(h_class_loader.Get());
4078 }
4079 }
4080
ThrowDexFileAlreadyRegisteredError(Thread * self,const DexFile & dex_file)4081 static void ThrowDexFileAlreadyRegisteredError(Thread* self, const DexFile& dex_file) REQUIRES_SHARED(Locks::mutator_lock_) {
4082 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
4083 "Attempt to register dex file %s with multiple class loaders",
4084 dex_file.GetLocation().c_str());
4085 }
4086
RegisterDexFile(const DexFile & dex_file,ObjPtr<mirror::ClassLoader> class_loader)4087 ObjPtr<mirror::DexCache> ClassLinker::RegisterDexFile(const DexFile& dex_file,
4088 ObjPtr<mirror::ClassLoader> class_loader) {
4089 Thread* self = Thread::Current();
4090 ObjPtr<mirror::DexCache> old_dex_cache;
4091 bool registered_with_another_class_loader = false;
4092 {
4093 ReaderMutexLock mu(self, *Locks::dex_lock_);
4094 const DexCacheData* old_data = FindDexCacheDataLocked(dex_file);
4095 old_dex_cache = DecodeDexCacheLocked(self, old_data);
4096 if (old_dex_cache != nullptr) {
4097 if (IsSameClassLoader(old_dex_cache, old_data, class_loader)) {
4098 return old_dex_cache;
4099 } else {
4100 // TODO This is not very clean looking. Should maybe try to make a way to request exceptions
4101 // be thrown when it's safe to do so to simplify this.
4102 registered_with_another_class_loader = true;
4103 }
4104 }
4105 }
4106 // We need to have released the dex_lock_ to allocate safely.
4107 if (registered_with_another_class_loader) {
4108 ThrowDexFileAlreadyRegisteredError(self, dex_file);
4109 return nullptr;
4110 }
4111 SCOPED_TRACE << __FUNCTION__ << " " << dex_file.GetLocation();
4112 LinearAlloc* const linear_alloc = GetOrCreateAllocatorForClassLoader(class_loader);
4113 DCHECK(linear_alloc != nullptr);
4114 ClassTable* table;
4115 {
4116 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
4117 table = InsertClassTableForClassLoader(class_loader);
4118 }
4119 // Don't alloc while holding the lock, since allocation may need to
4120 // suspend all threads and another thread may need the dex_lock_ to
4121 // get to a suspend point.
4122 StackHandleScope<3> hs(self);
4123 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
4124 ObjPtr<mirror::String> location;
4125 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(AllocDexCache(/*out*/&location,
4126 self,
4127 dex_file)));
4128 Handle<mirror::String> h_location(hs.NewHandle(location));
4129 {
4130 // Avoid a deadlock between a garbage collecting thread running a checkpoint,
4131 // a thread holding the dex lock and blocking on a condition variable regarding
4132 // weak references access, and a thread blocking on the dex lock.
4133 gc::ScopedGCCriticalSection gcs(self, gc::kGcCauseClassLinker, gc::kCollectorTypeClassLinker);
4134 WriterMutexLock mu(self, *Locks::dex_lock_);
4135 const DexCacheData* old_data = FindDexCacheDataLocked(dex_file);
4136 old_dex_cache = DecodeDexCacheLocked(self, old_data);
4137 if (old_dex_cache == nullptr && h_dex_cache != nullptr) {
4138 // Do InitializeDexCache while holding dex lock to make sure two threads don't call it at the
4139 // same time with the same dex cache. Since the .bss is shared this can cause failing DCHECK
4140 // that the arrays are null.
4141 mirror::DexCache::InitializeDexCache(self,
4142 h_dex_cache.Get(),
4143 h_location.Get(),
4144 &dex_file,
4145 linear_alloc,
4146 image_pointer_size_);
4147 RegisterDexFileLocked(dex_file, h_dex_cache.Get(), h_class_loader.Get());
4148 }
4149 if (old_dex_cache != nullptr) {
4150 // Another thread managed to initialize the dex cache faster, so use that DexCache.
4151 // If this thread encountered OOME, ignore it.
4152 DCHECK_EQ(h_dex_cache == nullptr, self->IsExceptionPending());
4153 self->ClearException();
4154 // We cannot call EnsureSameClassLoader() or allocate an exception while holding the
4155 // dex_lock_.
4156 if (IsSameClassLoader(old_dex_cache, old_data, h_class_loader.Get())) {
4157 return old_dex_cache;
4158 } else {
4159 registered_with_another_class_loader = true;
4160 }
4161 }
4162 }
4163 if (registered_with_another_class_loader) {
4164 ThrowDexFileAlreadyRegisteredError(self, dex_file);
4165 return nullptr;
4166 }
4167 if (h_dex_cache == nullptr) {
4168 self->AssertPendingOOMException();
4169 return nullptr;
4170 }
4171 table->InsertStrongRoot(h_dex_cache.Get());
4172 if (h_class_loader.Get() != nullptr) {
4173 // Since we added a strong root to the class table, do the write barrier as required for
4174 // remembered sets and generational GCs.
4175 WriteBarrier::ForEveryFieldWrite(h_class_loader.Get());
4176 }
4177 return h_dex_cache.Get();
4178 }
4179
IsDexFileRegistered(Thread * self,const DexFile & dex_file)4180 bool ClassLinker::IsDexFileRegistered(Thread* self, const DexFile& dex_file) {
4181 ReaderMutexLock mu(self, *Locks::dex_lock_);
4182 return DecodeDexCacheLocked(self, FindDexCacheDataLocked(dex_file)) != nullptr;
4183 }
4184
FindDexCache(Thread * self,const DexFile & dex_file)4185 ObjPtr<mirror::DexCache> ClassLinker::FindDexCache(Thread* self, const DexFile& dex_file) {
4186 ReaderMutexLock mu(self, *Locks::dex_lock_);
4187 const DexCacheData* dex_cache_data = FindDexCacheDataLocked(dex_file);
4188 ObjPtr<mirror::DexCache> dex_cache = DecodeDexCacheLocked(self, dex_cache_data);
4189 if (dex_cache != nullptr) {
4190 return dex_cache;
4191 }
4192 // Failure, dump diagnostic and abort.
4193 for (const DexCacheData& data : dex_caches_) {
4194 if (DecodeDexCacheLocked(self, &data) != nullptr) {
4195 LOG(FATAL_WITHOUT_ABORT) << "Registered dex file " << data.dex_file->GetLocation();
4196 }
4197 }
4198 LOG(FATAL) << "Failed to find DexCache for DexFile " << dex_file.GetLocation()
4199 << " " << &dex_file << " " << dex_cache_data->dex_file;
4200 UNREACHABLE();
4201 }
4202
FindClassTable(Thread * self,ObjPtr<mirror::DexCache> dex_cache)4203 ClassTable* ClassLinker::FindClassTable(Thread* self, ObjPtr<mirror::DexCache> dex_cache) {
4204 const DexFile* dex_file = dex_cache->GetDexFile();
4205 DCHECK(dex_file != nullptr);
4206 ReaderMutexLock mu(self, *Locks::dex_lock_);
4207 // Search assuming unique-ness of dex file.
4208 for (const DexCacheData& data : dex_caches_) {
4209 // Avoid decoding (and read barriers) other unrelated dex caches.
4210 if (data.dex_file == dex_file) {
4211 ObjPtr<mirror::DexCache> registered_dex_cache = DecodeDexCacheLocked(self, &data);
4212 if (registered_dex_cache != nullptr) {
4213 CHECK_EQ(registered_dex_cache, dex_cache) << dex_file->GetLocation();
4214 return data.class_table;
4215 }
4216 }
4217 }
4218 return nullptr;
4219 }
4220
FindDexCacheDataLocked(const DexFile & dex_file)4221 const ClassLinker::DexCacheData* ClassLinker::FindDexCacheDataLocked(const DexFile& dex_file) {
4222 // Search assuming unique-ness of dex file.
4223 for (const DexCacheData& data : dex_caches_) {
4224 // Avoid decoding (and read barriers) other unrelated dex caches.
4225 if (data.dex_file == &dex_file) {
4226 return &data;
4227 }
4228 }
4229 return nullptr;
4230 }
4231
CreatePrimitiveClass(Thread * self,Primitive::Type type,ClassRoot primitive_root)4232 void ClassLinker::CreatePrimitiveClass(Thread* self,
4233 Primitive::Type type,
4234 ClassRoot primitive_root) {
4235 ObjPtr<mirror::Class> primitive_class =
4236 AllocClass(self, mirror::Class::PrimitiveClassSize(image_pointer_size_));
4237 CHECK(primitive_class != nullptr) << "OOM for primitive class " << type;
4238 // Do not hold lock on the primitive class object, the initialization of
4239 // primitive classes is done while the process is still single threaded.
4240 primitive_class->SetAccessFlagsDuringLinking(
4241 kAccPublic | kAccFinal | kAccAbstract | kAccVerificationAttempted);
4242 primitive_class->SetPrimitiveType(type);
4243 primitive_class->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable());
4244 // Skip EnsureSkipAccessChecksMethods(). We can skip the verified status,
4245 // the kAccVerificationAttempted flag was added above, and there are no
4246 // methods that need the kAccSkipAccessChecks flag.
4247 DCHECK_EQ(primitive_class->NumMethods(), 0u);
4248 // Primitive classes are initialized during single threaded startup, so visibly initialized.
4249 primitive_class->SetStatusForPrimitiveOrArray(ClassStatus::kVisiblyInitialized);
4250 const char* descriptor = Primitive::Descriptor(type);
4251 ObjPtr<mirror::Class> existing = InsertClass(descriptor,
4252 primitive_class,
4253 ComputeModifiedUtf8Hash(descriptor));
4254 CHECK(existing == nullptr) << "InitPrimitiveClass(" << type << ") failed";
4255 SetClassRoot(primitive_root, primitive_class);
4256 }
4257
GetArrayIfTable()4258 inline ObjPtr<mirror::IfTable> ClassLinker::GetArrayIfTable() {
4259 return GetClassRoot<mirror::ObjectArray<mirror::Object>>(this)->GetIfTable();
4260 }
4261
4262 // Create an array class (i.e. the class object for the array, not the
4263 // array itself). "descriptor" looks like "[C" or "[[[[B" or
4264 // "[Ljava/lang/String;".
4265 //
4266 // If "descriptor" refers to an array of primitives, look up the
4267 // primitive type's internally-generated class object.
4268 //
4269 // "class_loader" is the class loader of the class that's referring to
4270 // us. It's used to ensure that we're looking for the element type in
4271 // the right context. It does NOT become the class loader for the
4272 // array class; that always comes from the base element class.
4273 //
4274 // Returns null with an exception raised on failure.
CreateArrayClass(Thread * self,const char * descriptor,size_t hash,Handle<mirror::ClassLoader> class_loader)4275 ObjPtr<mirror::Class> ClassLinker::CreateArrayClass(Thread* self,
4276 const char* descriptor,
4277 size_t hash,
4278 Handle<mirror::ClassLoader> class_loader) {
4279 // Identify the underlying component type
4280 CHECK_EQ('[', descriptor[0]);
4281 StackHandleScope<2> hs(self);
4282
4283 // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied
4284 // code to be executed. We put it up here so we can avoid all the allocations associated with
4285 // creating the class. This can happen with (eg) jit threads.
4286 if (!self->CanLoadClasses()) {
4287 // Make sure we don't try to load anything, potentially causing an infinite loop.
4288 ObjPtr<mirror::Throwable> pre_allocated =
4289 Runtime::Current()->GetPreAllocatedNoClassDefFoundError();
4290 self->SetException(pre_allocated);
4291 return nullptr;
4292 }
4293
4294 MutableHandle<mirror::Class> component_type(hs.NewHandle(FindClass(self, descriptor + 1,
4295 class_loader)));
4296 if (component_type == nullptr) {
4297 DCHECK(self->IsExceptionPending());
4298 // We need to accept erroneous classes as component types.
4299 const size_t component_hash = ComputeModifiedUtf8Hash(descriptor + 1);
4300 component_type.Assign(LookupClass(self, descriptor + 1, component_hash, class_loader.Get()));
4301 if (component_type == nullptr) {
4302 DCHECK(self->IsExceptionPending());
4303 return nullptr;
4304 } else {
4305 self->ClearException();
4306 }
4307 }
4308 if (UNLIKELY(component_type->IsPrimitiveVoid())) {
4309 ThrowNoClassDefFoundError("Attempt to create array of void primitive type");
4310 return nullptr;
4311 }
4312 // See if the component type is already loaded. Array classes are
4313 // always associated with the class loader of their underlying
4314 // element type -- an array of Strings goes with the loader for
4315 // java/lang/String -- so we need to look for it there. (The
4316 // caller should have checked for the existence of the class
4317 // before calling here, but they did so with *their* class loader,
4318 // not the component type's loader.)
4319 //
4320 // If we find it, the caller adds "loader" to the class' initiating
4321 // loader list, which should prevent us from going through this again.
4322 //
4323 // This call is unnecessary if "loader" and "component_type->GetClassLoader()"
4324 // are the same, because our caller (FindClass) just did the
4325 // lookup. (Even if we get this wrong we still have correct behavior,
4326 // because we effectively do this lookup again when we add the new
4327 // class to the hash table --- necessary because of possible races with
4328 // other threads.)
4329 if (class_loader.Get() != component_type->GetClassLoader()) {
4330 ObjPtr<mirror::Class> new_class =
4331 LookupClass(self, descriptor, hash, component_type->GetClassLoader());
4332 if (new_class != nullptr) {
4333 return new_class;
4334 }
4335 }
4336 // Core array classes, i.e. Object[], Class[], String[] and primitive
4337 // arrays, have special initialization and they should be found above.
4338 DCHECK(!component_type->IsObjectClass() ||
4339 // Guard from false positives for errors before setting superclass.
4340 component_type->IsErroneousUnresolved());
4341 DCHECK(!component_type->IsStringClass());
4342 DCHECK(!component_type->IsClassClass());
4343 DCHECK(!component_type->IsPrimitive());
4344
4345 // Fill out the fields in the Class.
4346 //
4347 // It is possible to execute some methods against arrays, because
4348 // all arrays are subclasses of java_lang_Object_, so we need to set
4349 // up a vtable. We can just point at the one in java_lang_Object_.
4350 //
4351 // Array classes are simple enough that we don't need to do a full
4352 // link step.
4353 size_t array_class_size = mirror::Array::ClassSize(image_pointer_size_);
4354 auto visitor = [this, array_class_size, component_type](ObjPtr<mirror::Object> obj,
4355 size_t usable_size)
4356 REQUIRES_SHARED(Locks::mutator_lock_) {
4357 ScopedAssertNoNewTransactionRecords sanntr("CreateArrayClass");
4358 mirror::Class::InitializeClassVisitor init_class(array_class_size);
4359 init_class(obj, usable_size);
4360 ObjPtr<mirror::Class> klass = ObjPtr<mirror::Class>::DownCast(obj);
4361 klass->SetComponentType(component_type.Get());
4362 // Do not hold lock for initialization, the fence issued after the visitor
4363 // returns ensures memory visibility together with the implicit consume
4364 // semantics (for all supported architectures) for any thread that loads
4365 // the array class reference from any memory locations afterwards.
4366 FinishArrayClassSetup(klass);
4367 };
4368 auto new_class = hs.NewHandle<mirror::Class>(
4369 AllocClass(self, GetClassRoot<mirror::Class>(this), array_class_size, visitor));
4370 if (new_class == nullptr) {
4371 self->AssertPendingOOMException();
4372 return nullptr;
4373 }
4374
4375 ObjPtr<mirror::Class> existing = InsertClass(descriptor, new_class.Get(), hash);
4376 if (existing == nullptr) {
4377 // We postpone ClassLoad and ClassPrepare events to this point in time to avoid
4378 // duplicate events in case of races. Array classes don't really follow dedicated
4379 // load and prepare, anyways.
4380 Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(new_class);
4381 Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(new_class, new_class);
4382
4383 jit::Jit::NewTypeLoadedIfUsingJit(new_class.Get());
4384 return new_class.Get();
4385 }
4386 // Another thread must have loaded the class after we
4387 // started but before we finished. Abandon what we've
4388 // done.
4389 //
4390 // (Yes, this happens.)
4391
4392 return existing;
4393 }
4394
LookupPrimitiveClass(char type)4395 ObjPtr<mirror::Class> ClassLinker::LookupPrimitiveClass(char type) {
4396 ClassRoot class_root;
4397 switch (type) {
4398 case 'B': class_root = ClassRoot::kPrimitiveByte; break;
4399 case 'C': class_root = ClassRoot::kPrimitiveChar; break;
4400 case 'D': class_root = ClassRoot::kPrimitiveDouble; break;
4401 case 'F': class_root = ClassRoot::kPrimitiveFloat; break;
4402 case 'I': class_root = ClassRoot::kPrimitiveInt; break;
4403 case 'J': class_root = ClassRoot::kPrimitiveLong; break;
4404 case 'S': class_root = ClassRoot::kPrimitiveShort; break;
4405 case 'Z': class_root = ClassRoot::kPrimitiveBoolean; break;
4406 case 'V': class_root = ClassRoot::kPrimitiveVoid; break;
4407 default:
4408 return nullptr;
4409 }
4410 return GetClassRoot(class_root, this);
4411 }
4412
FindPrimitiveClass(char type)4413 ObjPtr<mirror::Class> ClassLinker::FindPrimitiveClass(char type) {
4414 ObjPtr<mirror::Class> result = LookupPrimitiveClass(type);
4415 if (UNLIKELY(result == nullptr)) {
4416 std::string printable_type(PrintableChar(type));
4417 ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str());
4418 }
4419 return result;
4420 }
4421
InsertClass(const char * descriptor,ObjPtr<mirror::Class> klass,size_t hash)4422 ObjPtr<mirror::Class> ClassLinker::InsertClass(const char* descriptor,
4423 ObjPtr<mirror::Class> klass,
4424 size_t hash) {
4425 DCHECK(Thread::Current()->CanLoadClasses());
4426 if (VLOG_IS_ON(class_linker)) {
4427 ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache();
4428 std::string source;
4429 if (dex_cache != nullptr) {
4430 source += " from ";
4431 source += dex_cache->GetLocation()->ToModifiedUtf8();
4432 }
4433 LOG(INFO) << "Loaded class " << descriptor << source;
4434 }
4435 {
4436 WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
4437 const ObjPtr<mirror::ClassLoader> class_loader = klass->GetClassLoader();
4438 ClassTable* const class_table = InsertClassTableForClassLoader(class_loader);
4439 ObjPtr<mirror::Class> existing = class_table->Lookup(descriptor, hash);
4440 if (existing != nullptr) {
4441 return existing;
4442 }
4443 VerifyObject(klass);
4444 class_table->InsertWithHash(klass, hash);
4445 if (class_loader != nullptr) {
4446 // This is necessary because we need to have the card dirtied for remembered sets.
4447 WriteBarrier::ForEveryFieldWrite(class_loader);
4448 }
4449 if (log_new_roots_) {
4450 new_class_roots_.push_back(GcRoot<mirror::Class>(klass));
4451 }
4452 }
4453 if (kIsDebugBuild) {
4454 // Test that copied methods correctly can find their holder.
4455 for (ArtMethod& method : klass->GetCopiedMethods(image_pointer_size_)) {
4456 CHECK_EQ(GetHoldingClassOfCopiedMethod(&method), klass);
4457 }
4458 }
4459 return nullptr;
4460 }
4461
WriteBarrierForBootOatFileBssRoots(const OatFile * oat_file)4462 void ClassLinker::WriteBarrierForBootOatFileBssRoots(const OatFile* oat_file) {
4463 WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
4464 DCHECK(!oat_file->GetBssGcRoots().empty()) << oat_file->GetLocation();
4465 if (log_new_roots_ && !ContainsElement(new_bss_roots_boot_oat_files_, oat_file)) {
4466 new_bss_roots_boot_oat_files_.push_back(oat_file);
4467 }
4468 }
4469
4470 // TODO This should really be in mirror::Class.
UpdateClassMethods(ObjPtr<mirror::Class> klass,LengthPrefixedArray<ArtMethod> * new_methods)4471 void ClassLinker::UpdateClassMethods(ObjPtr<mirror::Class> klass,
4472 LengthPrefixedArray<ArtMethod>* new_methods) {
4473 klass->SetMethodsPtrUnchecked(new_methods,
4474 klass->NumDirectMethods(),
4475 klass->NumDeclaredVirtualMethods());
4476 // Need to mark the card so that the remembered sets and mod union tables get updated.
4477 WriteBarrier::ForEveryFieldWrite(klass);
4478 }
4479
LookupClass(Thread * self,const char * descriptor,ObjPtr<mirror::ClassLoader> class_loader)4480 ObjPtr<mirror::Class> ClassLinker::LookupClass(Thread* self,
4481 const char* descriptor,
4482 ObjPtr<mirror::ClassLoader> class_loader) {
4483 return LookupClass(self, descriptor, ComputeModifiedUtf8Hash(descriptor), class_loader);
4484 }
4485
LookupClass(Thread * self,const char * descriptor,size_t hash,ObjPtr<mirror::ClassLoader> class_loader)4486 ObjPtr<mirror::Class> ClassLinker::LookupClass(Thread* self,
4487 const char* descriptor,
4488 size_t hash,
4489 ObjPtr<mirror::ClassLoader> class_loader) {
4490 ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_);
4491 ClassTable* const class_table = ClassTableForClassLoader(class_loader);
4492 if (class_table != nullptr) {
4493 ObjPtr<mirror::Class> result = class_table->Lookup(descriptor, hash);
4494 if (result != nullptr) {
4495 return result;
4496 }
4497 }
4498 return nullptr;
4499 }
4500
4501 class MoveClassTableToPreZygoteVisitor : public ClassLoaderVisitor {
4502 public:
MoveClassTableToPreZygoteVisitor()4503 MoveClassTableToPreZygoteVisitor() {}
4504
Visit(ObjPtr<mirror::ClassLoader> class_loader)4505 void Visit(ObjPtr<mirror::ClassLoader> class_loader)
4506 REQUIRES(Locks::classlinker_classes_lock_)
4507 REQUIRES_SHARED(Locks::mutator_lock_) override {
4508 ClassTable* const class_table = class_loader->GetClassTable();
4509 if (class_table != nullptr) {
4510 class_table->FreezeSnapshot();
4511 }
4512 }
4513 };
4514
MoveClassTableToPreZygote()4515 void ClassLinker::MoveClassTableToPreZygote() {
4516 WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
4517 boot_class_table_->FreezeSnapshot();
4518 MoveClassTableToPreZygoteVisitor visitor;
4519 VisitClassLoaders(&visitor);
4520 }
4521
4522 // Look up classes by hash and descriptor and put all matching ones in the result array.
4523 class LookupClassesVisitor : public ClassLoaderVisitor {
4524 public:
LookupClassesVisitor(const char * descriptor,size_t hash,std::vector<ObjPtr<mirror::Class>> * result)4525 LookupClassesVisitor(const char* descriptor,
4526 size_t hash,
4527 std::vector<ObjPtr<mirror::Class>>* result)
4528 : descriptor_(descriptor),
4529 hash_(hash),
4530 result_(result) {}
4531
Visit(ObjPtr<mirror::ClassLoader> class_loader)4532 void Visit(ObjPtr<mirror::ClassLoader> class_loader)
4533 REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override {
4534 ClassTable* const class_table = class_loader->GetClassTable();
4535 ObjPtr<mirror::Class> klass = class_table->Lookup(descriptor_, hash_);
4536 // Add `klass` only if `class_loader` is its defining (not just initiating) class loader.
4537 if (klass != nullptr && klass->GetClassLoader() == class_loader) {
4538 result_->push_back(klass);
4539 }
4540 }
4541
4542 private:
4543 const char* const descriptor_;
4544 const size_t hash_;
4545 std::vector<ObjPtr<mirror::Class>>* const result_;
4546 };
4547
LookupClasses(const char * descriptor,std::vector<ObjPtr<mirror::Class>> & result)4548 void ClassLinker::LookupClasses(const char* descriptor,
4549 std::vector<ObjPtr<mirror::Class>>& result) {
4550 result.clear();
4551 Thread* const self = Thread::Current();
4552 ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_);
4553 const size_t hash = ComputeModifiedUtf8Hash(descriptor);
4554 ObjPtr<mirror::Class> klass = boot_class_table_->Lookup(descriptor, hash);
4555 if (klass != nullptr) {
4556 DCHECK(klass->GetClassLoader() == nullptr);
4557 result.push_back(klass);
4558 }
4559 LookupClassesVisitor visitor(descriptor, hash, &result);
4560 VisitClassLoaders(&visitor);
4561 }
4562
AttemptSupertypeVerification(Thread * self,Handle<mirror::Class> klass,Handle<mirror::Class> supertype)4563 bool ClassLinker::AttemptSupertypeVerification(Thread* self,
4564 Handle<mirror::Class> klass,
4565 Handle<mirror::Class> supertype) {
4566 DCHECK(self != nullptr);
4567 DCHECK(klass != nullptr);
4568 DCHECK(supertype != nullptr);
4569
4570 if (!supertype->IsVerified() && !supertype->IsErroneous()) {
4571 VerifyClass(self, supertype);
4572 }
4573
4574 if (supertype->IsVerified()
4575 || supertype->ShouldVerifyAtRuntime()
4576 || supertype->IsVerifiedNeedsAccessChecks()) {
4577 // The supertype is either verified, or we soft failed at AOT time.
4578 DCHECK(supertype->IsVerified() || Runtime::Current()->IsAotCompiler());
4579 return true;
4580 }
4581 // If we got this far then we have a hard failure.
4582 std::string error_msg =
4583 StringPrintf("Rejecting class %s that attempts to sub-type erroneous class %s",
4584 klass->PrettyDescriptor().c_str(),
4585 supertype->PrettyDescriptor().c_str());
4586 LOG(WARNING) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8();
4587 StackHandleScope<1> hs(self);
4588 Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException()));
4589 if (cause != nullptr) {
4590 // Set during VerifyClass call (if at all).
4591 self->ClearException();
4592 }
4593 // Change into a verify error.
4594 ThrowVerifyError(klass.Get(), "%s", error_msg.c_str());
4595 if (cause != nullptr) {
4596 self->GetException()->SetCause(cause.Get());
4597 }
4598 ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex());
4599 if (Runtime::Current()->IsAotCompiler()) {
4600 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
4601 }
4602 // Need to grab the lock to change status.
4603 ObjectLock<mirror::Class> super_lock(self, klass);
4604 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
4605 return false;
4606 }
4607
VerifyClass(Thread * self,Handle<mirror::Class> klass,verifier::HardFailLogMode log_level)4608 verifier::FailureKind ClassLinker::VerifyClass(
4609 Thread* self, Handle<mirror::Class> klass, verifier::HardFailLogMode log_level) {
4610 {
4611 // TODO: assert that the monitor on the Class is held
4612 ObjectLock<mirror::Class> lock(self, klass);
4613
4614 // Is somebody verifying this now?
4615 ClassStatus old_status = klass->GetStatus();
4616 while (old_status == ClassStatus::kVerifying) {
4617 lock.WaitIgnoringInterrupts();
4618 // WaitIgnoringInterrupts can still receive an interrupt and return early, in this
4619 // case we may see the same status again. b/62912904. This is why the check is
4620 // greater or equal.
4621 CHECK(klass->IsErroneous() || (klass->GetStatus() >= old_status))
4622 << "Class '" << klass->PrettyClass()
4623 << "' performed an illegal verification state transition from " << old_status
4624 << " to " << klass->GetStatus();
4625 old_status = klass->GetStatus();
4626 }
4627
4628 // The class might already be erroneous, for example at compile time if we attempted to verify
4629 // this class as a parent to another.
4630 if (klass->IsErroneous()) {
4631 ThrowEarlierClassFailure(klass.Get());
4632 return verifier::FailureKind::kHardFailure;
4633 }
4634
4635 // Don't attempt to re-verify if already verified.
4636 if (klass->IsVerified()) {
4637 EnsureSkipAccessChecksMethods(klass, image_pointer_size_);
4638 return verifier::FailureKind::kNoFailure;
4639 }
4640
4641 if (klass->IsVerifiedNeedsAccessChecks()) {
4642 if (!Runtime::Current()->IsAotCompiler()) {
4643 // Mark the class as having a verification attempt to avoid re-running
4644 // the verifier and avoid calling EnsureSkipAccessChecksMethods.
4645 klass->SetVerificationAttempted();
4646 mirror::Class::SetStatus(klass, ClassStatus::kVerified, self);
4647 }
4648 return verifier::FailureKind::kAccessChecksFailure;
4649 }
4650
4651 // For AOT, don't attempt to re-verify if we have already found we should
4652 // verify at runtime.
4653 if (klass->ShouldVerifyAtRuntime()) {
4654 CHECK(Runtime::Current()->IsAotCompiler());
4655 return verifier::FailureKind::kSoftFailure;
4656 }
4657
4658 DCHECK_EQ(klass->GetStatus(), ClassStatus::kResolved);
4659 mirror::Class::SetStatus(klass, ClassStatus::kVerifying, self);
4660
4661 // Skip verification if disabled.
4662 if (!Runtime::Current()->IsVerificationEnabled()) {
4663 mirror::Class::SetStatus(klass, ClassStatus::kVerified, self);
4664 EnsureSkipAccessChecksMethods(klass, image_pointer_size_);
4665 return verifier::FailureKind::kNoFailure;
4666 }
4667 }
4668
4669 VLOG(class_linker) << "Beginning verification for class: "
4670 << klass->PrettyDescriptor()
4671 << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8();
4672
4673 // Verify super class.
4674 StackHandleScope<2> hs(self);
4675 MutableHandle<mirror::Class> supertype(hs.NewHandle(klass->GetSuperClass()));
4676 // If we have a superclass and we get a hard verification failure we can return immediately.
4677 if (supertype != nullptr && !AttemptSupertypeVerification(self, klass, supertype)) {
4678 CHECK(self->IsExceptionPending()) << "Verification error should be pending.";
4679 return verifier::FailureKind::kHardFailure;
4680 }
4681
4682 // Verify all default super-interfaces.
4683 //
4684 // (1) Don't bother if the superclass has already had a soft verification failure.
4685 //
4686 // (2) Interfaces shouldn't bother to do this recursive verification because they cannot cause
4687 // recursive initialization by themselves. This is because when an interface is initialized
4688 // directly it must not initialize its superinterfaces. We are allowed to verify regardless
4689 // but choose not to for an optimization. If the interfaces is being verified due to a class
4690 // initialization (which would need all the default interfaces to be verified) the class code
4691 // will trigger the recursive verification anyway.
4692 if ((supertype == nullptr || supertype->IsVerified()) // See (1)
4693 && !klass->IsInterface()) { // See (2)
4694 int32_t iftable_count = klass->GetIfTableCount();
4695 MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr));
4696 // Loop through all interfaces this class has defined. It doesn't matter the order.
4697 for (int32_t i = 0; i < iftable_count; i++) {
4698 iface.Assign(klass->GetIfTable()->GetInterface(i));
4699 DCHECK(iface != nullptr);
4700 // We only care if we have default interfaces and can skip if we are already verified...
4701 if (LIKELY(!iface->HasDefaultMethods() || iface->IsVerified())) {
4702 continue;
4703 } else if (UNLIKELY(!AttemptSupertypeVerification(self, klass, iface))) {
4704 // We had a hard failure while verifying this interface. Just return immediately.
4705 CHECK(self->IsExceptionPending()) << "Verification error should be pending.";
4706 return verifier::FailureKind::kHardFailure;
4707 } else if (UNLIKELY(!iface->IsVerified())) {
4708 // We softly failed to verify the iface. Stop checking and clean up.
4709 // Put the iface into the supertype handle so we know what caused us to fail.
4710 supertype.Assign(iface.Get());
4711 break;
4712 }
4713 }
4714 }
4715
4716 // At this point if verification failed, then supertype is the "first" supertype that failed
4717 // verification (without a specific order). If verification succeeded, then supertype is either
4718 // null or the original superclass of klass and is verified.
4719 DCHECK(supertype == nullptr ||
4720 supertype.Get() == klass->GetSuperClass() ||
4721 !supertype->IsVerified());
4722
4723 // Try to use verification information from the oat file, otherwise do runtime verification.
4724 const DexFile& dex_file = *klass->GetDexCache()->GetDexFile();
4725 ClassStatus oat_file_class_status(ClassStatus::kNotReady);
4726 bool preverified = VerifyClassUsingOatFile(dex_file, klass.Get(), oat_file_class_status);
4727
4728 VLOG(class_linker) << "Class preverified status for class "
4729 << klass->PrettyDescriptor()
4730 << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8()
4731 << ": "
4732 << preverified
4733 << "( " << oat_file_class_status << ")";
4734
4735 // If the oat file says the class had an error, re-run the verifier. That way we will get a
4736 // precise error message. To ensure a rerun, test:
4737 // mirror::Class::IsErroneous(oat_file_class_status) => !preverified
4738 DCHECK(!mirror::Class::IsErroneous(oat_file_class_status) || !preverified);
4739
4740 std::string error_msg;
4741 verifier::FailureKind verifier_failure = verifier::FailureKind::kNoFailure;
4742 if (!preverified) {
4743 verifier_failure = PerformClassVerification(self, klass, log_level, &error_msg);
4744 }
4745
4746 // Verification is done, grab the lock again.
4747 ObjectLock<mirror::Class> lock(self, klass);
4748
4749 if (preverified || verifier_failure != verifier::FailureKind::kHardFailure) {
4750 if (!preverified && verifier_failure != verifier::FailureKind::kNoFailure) {
4751 VLOG(class_linker) << "Soft verification failure in class "
4752 << klass->PrettyDescriptor()
4753 << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8()
4754 << " because: " << error_msg;
4755 }
4756 self->AssertNoPendingException();
4757 // Make sure all classes referenced by catch blocks are resolved.
4758 ResolveClassExceptionHandlerTypes(klass);
4759 if (verifier_failure == verifier::FailureKind::kNoFailure) {
4760 // Even though there were no verifier failures we need to respect whether the super-class and
4761 // super-default-interfaces were verified or requiring runtime reverification.
4762 if (supertype == nullptr
4763 || supertype->IsVerified()
4764 || supertype->IsVerifiedNeedsAccessChecks()) {
4765 mirror::Class::SetStatus(klass, ClassStatus::kVerified, self);
4766 } else {
4767 CHECK(Runtime::Current()->IsAotCompiler());
4768 CHECK_EQ(supertype->GetStatus(), ClassStatus::kRetryVerificationAtRuntime);
4769 mirror::Class::SetStatus(klass, ClassStatus::kRetryVerificationAtRuntime, self);
4770 // Pretend a soft failure occurred so that we don't consider the class verified below.
4771 verifier_failure = verifier::FailureKind::kSoftFailure;
4772 }
4773 } else {
4774 CHECK(verifier_failure == verifier::FailureKind::kSoftFailure ||
4775 verifier_failure == verifier::FailureKind::kAccessChecksFailure);
4776 // Soft failures at compile time should be retried at runtime. Soft
4777 // failures at runtime will be handled by slow paths in the generated
4778 // code. Set status accordingly.
4779 if (Runtime::Current()->IsAotCompiler()) {
4780 if (verifier_failure == verifier::FailureKind::kSoftFailure) {
4781 mirror::Class::SetStatus(klass, ClassStatus::kRetryVerificationAtRuntime, self);
4782 } else {
4783 mirror::Class::SetStatus(klass, ClassStatus::kVerifiedNeedsAccessChecks, self);
4784 }
4785 } else {
4786 mirror::Class::SetStatus(klass, ClassStatus::kVerified, self);
4787 // As this is a fake verified status, make sure the methods are _not_ marked
4788 // kAccSkipAccessChecks later.
4789 klass->SetVerificationAttempted();
4790 }
4791 }
4792 } else {
4793 VLOG(verifier) << "Verification failed on class " << klass->PrettyDescriptor()
4794 << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8()
4795 << " because: " << error_msg;
4796 self->AssertNoPendingException();
4797 ThrowVerifyError(klass.Get(), "%s", error_msg.c_str());
4798 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
4799 }
4800 if (preverified || verifier_failure == verifier::FailureKind::kNoFailure) {
4801 if (oat_file_class_status == ClassStatus::kVerifiedNeedsAccessChecks ||
4802 UNLIKELY(Runtime::Current()->IsVerificationSoftFail())) {
4803 // Never skip access checks if the verification soft fail is forced.
4804 // Mark the class as having a verification attempt to avoid re-running the verifier.
4805 klass->SetVerificationAttempted();
4806 } else {
4807 // Class is verified so we don't need to do any access check on its methods.
4808 // Let the interpreter know it by setting the kAccSkipAccessChecks flag onto each
4809 // method.
4810 // Note: we're going here during compilation and at runtime. When we set the
4811 // kAccSkipAccessChecks flag when compiling image classes, the flag is recorded
4812 // in the image and is set when loading the image.
4813 EnsureSkipAccessChecksMethods(klass, image_pointer_size_);
4814 }
4815 }
4816 // Done verifying. Notify the compiler about the verification status, in case the class
4817 // was verified implicitly (eg super class of a compiled class).
4818 if (Runtime::Current()->IsAotCompiler()) {
4819 Runtime::Current()->GetCompilerCallbacks()->UpdateClassState(
4820 ClassReference(&klass->GetDexFile(), klass->GetDexClassDefIndex()), klass->GetStatus());
4821 }
4822 return verifier_failure;
4823 }
4824
PerformClassVerification(Thread * self,Handle<mirror::Class> klass,verifier::HardFailLogMode log_level,std::string * error_msg)4825 verifier::FailureKind ClassLinker::PerformClassVerification(Thread* self,
4826 Handle<mirror::Class> klass,
4827 verifier::HardFailLogMode log_level,
4828 std::string* error_msg) {
4829 Runtime* const runtime = Runtime::Current();
4830 return verifier::ClassVerifier::VerifyClass(self,
4831 klass.Get(),
4832 runtime->GetCompilerCallbacks(),
4833 runtime->IsAotCompiler(),
4834 log_level,
4835 Runtime::Current()->GetTargetSdkVersion(),
4836 error_msg);
4837 }
4838
VerifyClassUsingOatFile(const DexFile & dex_file,ObjPtr<mirror::Class> klass,ClassStatus & oat_file_class_status)4839 bool ClassLinker::VerifyClassUsingOatFile(const DexFile& dex_file,
4840 ObjPtr<mirror::Class> klass,
4841 ClassStatus& oat_file_class_status) {
4842 // If we're compiling, we can only verify the class using the oat file if
4843 // we are not compiling the image or if the class we're verifying is not part of
4844 // the compilation unit (app - dependencies). We will let the compiler callback
4845 // tell us about the latter.
4846 if (Runtime::Current()->IsAotCompiler()) {
4847 CompilerCallbacks* callbacks = Runtime::Current()->GetCompilerCallbacks();
4848 // We are compiling an app (not the image).
4849 if (!callbacks->CanUseOatStatusForVerification(klass.Ptr())) {
4850 return false;
4851 }
4852 }
4853
4854 const OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
4855 // In case we run without an image there won't be a backing oat file.
4856 if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) {
4857 return false;
4858 }
4859
4860 uint16_t class_def_index = klass->GetDexClassDefIndex();
4861 oat_file_class_status = oat_dex_file->GetOatClass(class_def_index).GetStatus();
4862 if (oat_file_class_status >= ClassStatus::kVerified) {
4863 return true;
4864 }
4865 if (oat_file_class_status >= ClassStatus::kVerifiedNeedsAccessChecks) {
4866 // We return that the clas has already been verified, and the caller should
4867 // check the class status to ensure we run with access checks.
4868 return true;
4869 }
4870 // If we only verified a subset of the classes at compile time, we can end up with classes that
4871 // were resolved by the verifier.
4872 if (oat_file_class_status == ClassStatus::kResolved) {
4873 return false;
4874 }
4875 // We never expect a .oat file to have kRetryVerificationAtRuntime statuses.
4876 CHECK_NE(oat_file_class_status, ClassStatus::kRetryVerificationAtRuntime)
4877 << klass->PrettyClass() << " " << dex_file.GetLocation();
4878
4879 if (mirror::Class::IsErroneous(oat_file_class_status)) {
4880 // Compile time verification failed with a hard error. This is caused by invalid instructions
4881 // in the class. These errors are unrecoverable.
4882 return false;
4883 }
4884 if (oat_file_class_status == ClassStatus::kNotReady) {
4885 // Status is uninitialized if we couldn't determine the status at compile time, for example,
4886 // not loading the class.
4887 // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy
4888 // isn't a problem and this case shouldn't occur
4889 return false;
4890 }
4891 std::string temp;
4892 LOG(FATAL) << "Unexpected class status: " << oat_file_class_status
4893 << " " << dex_file.GetLocation() << " " << klass->PrettyClass() << " "
4894 << klass->GetDescriptor(&temp);
4895 UNREACHABLE();
4896 }
4897
ResolveClassExceptionHandlerTypes(Handle<mirror::Class> klass)4898 void ClassLinker::ResolveClassExceptionHandlerTypes(Handle<mirror::Class> klass) {
4899 for (ArtMethod& method : klass->GetMethods(image_pointer_size_)) {
4900 ResolveMethodExceptionHandlerTypes(&method);
4901 }
4902 }
4903
ResolveMethodExceptionHandlerTypes(ArtMethod * method)4904 void ClassLinker::ResolveMethodExceptionHandlerTypes(ArtMethod* method) {
4905 // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod.
4906 CodeItemDataAccessor accessor(method->DexInstructionData());
4907 if (!accessor.HasCodeItem()) {
4908 return; // native or abstract method
4909 }
4910 if (accessor.TriesSize() == 0) {
4911 return; // nothing to process
4912 }
4913 const uint8_t* handlers_ptr = accessor.GetCatchHandlerData(0);
4914 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
4915 for (uint32_t idx = 0; idx < handlers_size; idx++) {
4916 CatchHandlerIterator iterator(handlers_ptr);
4917 for (; iterator.HasNext(); iterator.Next()) {
4918 // Ensure exception types are resolved so that they don't need resolution to be delivered,
4919 // unresolved exception types will be ignored by exception delivery
4920 if (iterator.GetHandlerTypeIndex().IsValid()) {
4921 ObjPtr<mirror::Class> exception_type = ResolveType(iterator.GetHandlerTypeIndex(), method);
4922 if (exception_type == nullptr) {
4923 DCHECK(Thread::Current()->IsExceptionPending());
4924 Thread::Current()->ClearException();
4925 }
4926 }
4927 }
4928 handlers_ptr = iterator.EndDataPointer();
4929 }
4930 }
4931
CreateProxyClass(ScopedObjectAccessAlreadyRunnable & soa,jstring name,jobjectArray interfaces,jobject loader,jobjectArray methods,jobjectArray throws)4932 ObjPtr<mirror::Class> ClassLinker::CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa,
4933 jstring name,
4934 jobjectArray interfaces,
4935 jobject loader,
4936 jobjectArray methods,
4937 jobjectArray throws) {
4938 Thread* self = soa.Self();
4939
4940 // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied
4941 // code to be executed. We put it up here so we can avoid all the allocations associated with
4942 // creating the class. This can happen with (eg) jit-threads.
4943 if (!self->CanLoadClasses()) {
4944 // Make sure we don't try to load anything, potentially causing an infinite loop.
4945 ObjPtr<mirror::Throwable> pre_allocated =
4946 Runtime::Current()->GetPreAllocatedNoClassDefFoundError();
4947 self->SetException(pre_allocated);
4948 return nullptr;
4949 }
4950
4951 StackHandleScope<12> hs(self);
4952 MutableHandle<mirror::Class> temp_klass(hs.NewHandle(
4953 AllocClass(self, GetClassRoot<mirror::Class>(this), sizeof(mirror::Class))));
4954 if (temp_klass == nullptr) {
4955 CHECK(self->IsExceptionPending()); // OOME.
4956 return nullptr;
4957 }
4958 DCHECK(temp_klass->GetClass() != nullptr);
4959 temp_klass->SetObjectSize(sizeof(mirror::Proxy));
4960 // Set the class access flags incl. VerificationAttempted, so we do not try to set the flag on
4961 // the methods.
4962 temp_klass->SetAccessFlagsDuringLinking(
4963 kAccClassIsProxy | kAccPublic | kAccFinal | kAccVerificationAttempted);
4964 temp_klass->SetClassLoader(soa.Decode<mirror::ClassLoader>(loader));
4965 DCHECK_EQ(temp_klass->GetPrimitiveType(), Primitive::kPrimNot);
4966 temp_klass->SetName(soa.Decode<mirror::String>(name));
4967 temp_klass->SetDexCache(GetClassRoot<mirror::Proxy>(this)->GetDexCache());
4968 // Object has an empty iftable, copy it for that reason.
4969 temp_klass->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable());
4970 mirror::Class::SetStatus(temp_klass, ClassStatus::kIdx, self);
4971 std::string storage;
4972 const char* descriptor = temp_klass->GetDescriptor(&storage);
4973 const size_t hash = ComputeModifiedUtf8Hash(descriptor);
4974
4975 // Needs to be before we insert the class so that the allocator field is set.
4976 LinearAlloc* const allocator = GetOrCreateAllocatorForClassLoader(temp_klass->GetClassLoader());
4977
4978 // Insert the class before loading the fields as the field roots
4979 // (ArtField::declaring_class_) are only visited from the class
4980 // table. There can't be any suspend points between inserting the
4981 // class and setting the field arrays below.
4982 ObjPtr<mirror::Class> existing = InsertClass(descriptor, temp_klass.Get(), hash);
4983 CHECK(existing == nullptr);
4984
4985 // Instance fields are inherited, but we add a couple of static fields...
4986 const size_t num_fields = 2;
4987 LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, allocator, num_fields);
4988 temp_klass->SetSFieldsPtr(sfields);
4989
4990 // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by
4991 // our proxy, so Class.getInterfaces doesn't return the flattened set.
4992 ArtField& interfaces_sfield = sfields->At(0);
4993 interfaces_sfield.SetDexFieldIndex(0);
4994 interfaces_sfield.SetDeclaringClass(temp_klass.Get());
4995 interfaces_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal);
4996
4997 // 2. Create a static field 'throws' that holds exceptions thrown by our methods.
4998 ArtField& throws_sfield = sfields->At(1);
4999 throws_sfield.SetDexFieldIndex(1);
5000 throws_sfield.SetDeclaringClass(temp_klass.Get());
5001 throws_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal);
5002
5003 // Proxies have 1 direct method, the constructor
5004 const size_t num_direct_methods = 1;
5005
5006 // The array we get passed contains all methods, including private and static
5007 // ones that aren't proxied. We need to filter those out since only interface
5008 // methods (non-private & virtual) are actually proxied.
5009 Handle<mirror::ObjectArray<mirror::Method>> h_methods =
5010 hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Method>>(methods));
5011 DCHECK_EQ(h_methods->GetClass(), GetClassRoot<mirror::ObjectArray<mirror::Method>>())
5012 << mirror::Class::PrettyClass(h_methods->GetClass());
5013 // List of the actual virtual methods this class will have.
5014 std::vector<ArtMethod*> proxied_methods;
5015 std::vector<size_t> proxied_throws_idx;
5016 proxied_methods.reserve(h_methods->GetLength());
5017 proxied_throws_idx.reserve(h_methods->GetLength());
5018 // Filter out to only the non-private virtual methods.
5019 for (auto [mirror, idx] : ZipCount(h_methods.Iterate<mirror::Method>())) {
5020 ArtMethod* m = mirror->GetArtMethod();
5021 if (!m->IsPrivate() && !m->IsStatic()) {
5022 proxied_methods.push_back(m);
5023 proxied_throws_idx.push_back(idx);
5024 }
5025 }
5026 const size_t num_virtual_methods = proxied_methods.size();
5027 // We also need to filter out the 'throws'. The 'throws' are a Class[][] that
5028 // contains an array of all the classes each function is declared to throw.
5029 // This is used to wrap unexpected exceptions in a
5030 // UndeclaredThrowableException exception. This array is in the same order as
5031 // the methods array and like the methods array must be filtered to remove any
5032 // non-proxied methods.
5033 const bool has_filtered_methods =
5034 static_cast<int32_t>(num_virtual_methods) != h_methods->GetLength();
5035 MutableHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>> original_proxied_throws(
5036 hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>(throws)));
5037 MutableHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>> proxied_throws(
5038 hs.NewHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>(
5039 (has_filtered_methods)
5040 ? mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>::Alloc(
5041 self, original_proxied_throws->GetClass(), num_virtual_methods)
5042 : original_proxied_throws.Get()));
5043 if (proxied_throws.IsNull() && !original_proxied_throws.IsNull()) {
5044 self->AssertPendingOOMException();
5045 return nullptr;
5046 }
5047 if (has_filtered_methods) {
5048 for (auto [orig_idx, new_idx] : ZipCount(MakeIterationRange(proxied_throws_idx))) {
5049 DCHECK_LE(new_idx, orig_idx);
5050 proxied_throws->Set(new_idx, original_proxied_throws->Get(orig_idx));
5051 }
5052 }
5053
5054 // Create the methods array.
5055 LengthPrefixedArray<ArtMethod>* proxy_class_methods = AllocArtMethodArray(
5056 self, allocator, num_direct_methods + num_virtual_methods);
5057 // Currently AllocArtMethodArray cannot return null, but the OOM logic is left there in case we
5058 // want to throw OOM in the future.
5059 if (UNLIKELY(proxy_class_methods == nullptr)) {
5060 self->AssertPendingOOMException();
5061 return nullptr;
5062 }
5063 temp_klass->SetMethodsPtr(proxy_class_methods, num_direct_methods, num_virtual_methods);
5064
5065 // Create the single direct method.
5066 CreateProxyConstructor(temp_klass, temp_klass->GetDirectMethodUnchecked(0, image_pointer_size_));
5067
5068 // Create virtual method using specified prototypes.
5069 // TODO These should really use the iterators.
5070 for (size_t i = 0; i < num_virtual_methods; ++i) {
5071 auto* virtual_method = temp_klass->GetVirtualMethodUnchecked(i, image_pointer_size_);
5072 auto* prototype = proxied_methods[i];
5073 CreateProxyMethod(temp_klass, prototype, virtual_method);
5074 DCHECK(virtual_method->GetDeclaringClass() != nullptr);
5075 DCHECK(prototype->GetDeclaringClass() != nullptr);
5076 }
5077
5078 // The super class is java.lang.reflect.Proxy
5079 temp_klass->SetSuperClass(GetClassRoot<mirror::Proxy>(this));
5080 // Now effectively in the loaded state.
5081 mirror::Class::SetStatus(temp_klass, ClassStatus::kLoaded, self);
5082 self->AssertNoPendingException();
5083
5084 // At this point the class is loaded. Publish a ClassLoad event.
5085 // Note: this may be a temporary class. It is a listener's responsibility to handle this.
5086 Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(temp_klass);
5087
5088 MutableHandle<mirror::Class> klass = hs.NewHandle<mirror::Class>(nullptr);
5089 {
5090 // Must hold lock on object when resolved.
5091 ObjectLock<mirror::Class> resolution_lock(self, temp_klass);
5092 // Link the fields and virtual methods, creating vtable and iftables.
5093 // The new class will replace the old one in the class table.
5094 Handle<mirror::ObjectArray<mirror::Class>> h_interfaces(
5095 hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)));
5096 if (!LinkClass(self, descriptor, temp_klass, h_interfaces, &klass)) {
5097 mirror::Class::SetStatus(temp_klass, ClassStatus::kErrorUnresolved, self);
5098 return nullptr;
5099 }
5100 }
5101 CHECK(temp_klass->IsRetired());
5102 CHECK_NE(temp_klass.Get(), klass.Get());
5103
5104 CHECK_EQ(interfaces_sfield.GetDeclaringClass(), klass.Get());
5105 interfaces_sfield.SetObject<false>(
5106 klass.Get(),
5107 soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces));
5108 CHECK_EQ(throws_sfield.GetDeclaringClass(), klass.Get());
5109 throws_sfield.SetObject<false>(
5110 klass.Get(),
5111 proxied_throws.Get());
5112
5113 Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(temp_klass, klass);
5114
5115 // SubtypeCheckInfo::Initialized must happen-before any new-instance for that type.
5116 // See also ClassLinker::EnsureInitialized().
5117 if (kBitstringSubtypeCheckEnabled) {
5118 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
5119 SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(klass.Get());
5120 // TODO: Avoid taking subtype_check_lock_ if SubtypeCheck for j.l.r.Proxy is already assigned.
5121 }
5122
5123 VisiblyInitializedCallback* callback = nullptr;
5124 {
5125 // Lock on klass is released. Lock new class object.
5126 ObjectLock<mirror::Class> initialization_lock(self, klass);
5127 EnsureSkipAccessChecksMethods(klass, image_pointer_size_);
5128 // Conservatively go through the ClassStatus::kInitialized state.
5129 callback = MarkClassInitialized(self, klass);
5130 }
5131 if (callback != nullptr) {
5132 callback->MakeVisible(self);
5133 }
5134
5135 // sanity checks
5136 if (kIsDebugBuild) {
5137 CHECK(klass->GetIFieldsPtr() == nullptr);
5138 CheckProxyConstructor(klass->GetDirectMethod(0, image_pointer_size_));
5139
5140 for (size_t i = 0; i < num_virtual_methods; ++i) {
5141 auto* virtual_method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_);
5142 CheckProxyMethod(virtual_method, proxied_methods[i]);
5143 }
5144
5145 StackHandleScope<1> hs2(self);
5146 Handle<mirror::String> decoded_name = hs2.NewHandle(soa.Decode<mirror::String>(name));
5147 std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces",
5148 decoded_name->ToModifiedUtf8().c_str()));
5149 CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(0)), interfaces_field_name);
5150
5151 std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws",
5152 decoded_name->ToModifiedUtf8().c_str()));
5153 CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(1)), throws_field_name);
5154
5155 CHECK_EQ(klass.Get()->GetProxyInterfaces(),
5156 soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces));
5157 CHECK_EQ(klass.Get()->GetProxyThrows(),
5158 proxied_throws.Get());
5159 }
5160 return klass.Get();
5161 }
5162
CreateProxyConstructor(Handle<mirror::Class> klass,ArtMethod * out)5163 void ClassLinker::CreateProxyConstructor(Handle<mirror::Class> klass, ArtMethod* out) {
5164 // Create constructor for Proxy that must initialize the method.
5165 ObjPtr<mirror::Class> proxy_class = GetClassRoot<mirror::Proxy>(this);
5166 CHECK_EQ(proxy_class->NumDirectMethods(), 21u);
5167
5168 // Find the <init>(InvocationHandler)V method. The exact method offset varies depending
5169 // on which front-end compiler was used to build the libcore DEX files.
5170 ArtMethod* proxy_constructor =
5171 jni::DecodeArtMethod(WellKnownClasses::java_lang_reflect_Proxy_init);
5172 DCHECK(proxy_constructor != nullptr)
5173 << "Could not find <init> method in java.lang.reflect.Proxy";
5174
5175 // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its
5176 // code_ too)
5177 DCHECK(out != nullptr);
5178 out->CopyFrom(proxy_constructor, image_pointer_size_);
5179 // Make this constructor public and fix the class to be our Proxy version.
5180 // Mark kAccCompileDontBother so that we don't take JIT samples for the method. b/62349349
5181 // Note that the compiler calls a ResolveMethod() overload that does not handle a Proxy referrer.
5182 out->SetAccessFlags((out->GetAccessFlags() & ~kAccProtected) |
5183 kAccPublic |
5184 kAccCompileDontBother);
5185 out->SetDeclaringClass(klass.Get());
5186
5187 // Set the original constructor method.
5188 out->SetDataPtrSize(proxy_constructor, image_pointer_size_);
5189 }
5190
CheckProxyConstructor(ArtMethod * constructor) const5191 void ClassLinker::CheckProxyConstructor(ArtMethod* constructor) const {
5192 CHECK(constructor->IsConstructor());
5193 auto* np = constructor->GetInterfaceMethodIfProxy(image_pointer_size_);
5194 CHECK_STREQ(np->GetName(), "<init>");
5195 CHECK_STREQ(np->GetSignature().ToString().c_str(), "(Ljava/lang/reflect/InvocationHandler;)V");
5196 DCHECK(constructor->IsPublic());
5197 }
5198
CreateProxyMethod(Handle<mirror::Class> klass,ArtMethod * prototype,ArtMethod * out)5199 void ClassLinker::CreateProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype,
5200 ArtMethod* out) {
5201 // We steal everything from the prototype (such as DexCache, invoke stub, etc.) then specialize
5202 // as necessary
5203 DCHECK(out != nullptr);
5204 out->CopyFrom(prototype, image_pointer_size_);
5205
5206 // Set class to be the concrete proxy class.
5207 out->SetDeclaringClass(klass.Get());
5208 // Clear the abstract, default and conflict flags to ensure that defaults aren't picked in
5209 // preference to the invocation handler.
5210 const uint32_t kRemoveFlags = kAccAbstract | kAccDefault | kAccDefaultConflict;
5211 // Make the method final.
5212 // Mark kAccCompileDontBother so that we don't take JIT samples for the method. b/62349349
5213 const uint32_t kAddFlags = kAccFinal | kAccCompileDontBother;
5214 out->SetAccessFlags((out->GetAccessFlags() & ~kRemoveFlags) | kAddFlags);
5215
5216 // Clear the dex_code_item_offset_. It needs to be 0 since proxy methods have no CodeItems but the
5217 // method they copy might (if it's a default method).
5218 out->SetCodeItemOffset(0);
5219
5220 // Set the original interface method.
5221 out->SetDataPtrSize(prototype, image_pointer_size_);
5222
5223 // At runtime the method looks like a reference and argument saving method, clone the code
5224 // related parameters from this method.
5225 out->SetEntryPointFromQuickCompiledCode(GetQuickProxyInvokeHandler());
5226 }
5227
CheckProxyMethod(ArtMethod * method,ArtMethod * prototype) const5228 void ClassLinker::CheckProxyMethod(ArtMethod* method, ArtMethod* prototype) const {
5229 // Basic sanity
5230 CHECK(!prototype->IsFinal());
5231 CHECK(method->IsFinal());
5232 CHECK(method->IsInvokable());
5233
5234 // The proxy method doesn't have its own dex cache or dex file and so it steals those of its
5235 // interface prototype. The exception to this are Constructors and the Class of the Proxy itself.
5236 CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex());
5237 CHECK_EQ(prototype, method->GetInterfaceMethodIfProxy(image_pointer_size_));
5238 }
5239
CanWeInitializeClass(ObjPtr<mirror::Class> klass,bool can_init_statics,bool can_init_parents)5240 bool ClassLinker::CanWeInitializeClass(ObjPtr<mirror::Class> klass, bool can_init_statics,
5241 bool can_init_parents) {
5242 if (can_init_statics && can_init_parents) {
5243 return true;
5244 }
5245 if (!can_init_statics) {
5246 // Check if there's a class initializer.
5247 ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_);
5248 if (clinit != nullptr) {
5249 return false;
5250 }
5251 // Check if there are encoded static values needing initialization.
5252 if (klass->NumStaticFields() != 0) {
5253 const dex::ClassDef* dex_class_def = klass->GetClassDef();
5254 DCHECK(dex_class_def != nullptr);
5255 if (dex_class_def->static_values_off_ != 0) {
5256 return false;
5257 }
5258 }
5259 }
5260 // If we are a class we need to initialize all interfaces with default methods when we are
5261 // initialized. Check all of them.
5262 if (!klass->IsInterface()) {
5263 size_t num_interfaces = klass->GetIfTableCount();
5264 for (size_t i = 0; i < num_interfaces; i++) {
5265 ObjPtr<mirror::Class> iface = klass->GetIfTable()->GetInterface(i);
5266 if (iface->HasDefaultMethods() && !iface->IsInitialized()) {
5267 if (!can_init_parents || !CanWeInitializeClass(iface, can_init_statics, can_init_parents)) {
5268 return false;
5269 }
5270 }
5271 }
5272 }
5273 if (klass->IsInterface() || !klass->HasSuperClass()) {
5274 return true;
5275 }
5276 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
5277 if (super_class->IsInitialized()) {
5278 return true;
5279 }
5280 return can_init_parents && CanWeInitializeClass(super_class, can_init_statics, can_init_parents);
5281 }
5282
InitializeClass(Thread * self,Handle<mirror::Class> klass,bool can_init_statics,bool can_init_parents)5283 bool ClassLinker::InitializeClass(Thread* self, Handle<mirror::Class> klass,
5284 bool can_init_statics, bool can_init_parents) {
5285 // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol
5286
5287 // Are we already initialized and therefore done?
5288 // Note: we differ from the JLS here as we don't do this under the lock, this is benign as
5289 // an initialized class will never change its state.
5290 if (klass->IsInitialized()) {
5291 return true;
5292 }
5293
5294 // Fast fail if initialization requires a full runtime. Not part of the JLS.
5295 if (!CanWeInitializeClass(klass.Get(), can_init_statics, can_init_parents)) {
5296 return false;
5297 }
5298
5299 self->AllowThreadSuspension();
5300 uint64_t t0;
5301 {
5302 ObjectLock<mirror::Class> lock(self, klass);
5303
5304 // Re-check under the lock in case another thread initialized ahead of us.
5305 if (klass->IsInitialized()) {
5306 return true;
5307 }
5308
5309 // Was the class already found to be erroneous? Done under the lock to match the JLS.
5310 if (klass->IsErroneous()) {
5311 ThrowEarlierClassFailure(klass.Get(), true, /* log= */ true);
5312 VlogClassInitializationFailure(klass);
5313 return false;
5314 }
5315
5316 CHECK(klass->IsResolved() && !klass->IsErroneousResolved())
5317 << klass->PrettyClass() << ": state=" << klass->GetStatus();
5318
5319 if (!klass->IsVerified()) {
5320 VerifyClass(self, klass);
5321 if (!klass->IsVerified()) {
5322 // We failed to verify, expect either the klass to be erroneous or verification failed at
5323 // compile time.
5324 if (klass->IsErroneous()) {
5325 // The class is erroneous. This may be a verifier error, or another thread attempted
5326 // verification and/or initialization and failed. We can distinguish those cases by
5327 // whether an exception is already pending.
5328 if (self->IsExceptionPending()) {
5329 // Check that it's a VerifyError.
5330 DCHECK_EQ("java.lang.Class<java.lang.VerifyError>",
5331 mirror::Class::PrettyClass(self->GetException()->GetClass()));
5332 } else {
5333 // Check that another thread attempted initialization.
5334 DCHECK_NE(0, klass->GetClinitThreadId());
5335 DCHECK_NE(self->GetTid(), klass->GetClinitThreadId());
5336 // Need to rethrow the previous failure now.
5337 ThrowEarlierClassFailure(klass.Get(), true);
5338 }
5339 VlogClassInitializationFailure(klass);
5340 } else {
5341 CHECK(Runtime::Current()->IsAotCompiler());
5342 CHECK(klass->ShouldVerifyAtRuntime() || klass->IsVerifiedNeedsAccessChecks());
5343 self->AssertNoPendingException();
5344 self->SetException(Runtime::Current()->GetPreAllocatedNoClassDefFoundError());
5345 }
5346 self->AssertPendingException();
5347 return false;
5348 } else {
5349 self->AssertNoPendingException();
5350 }
5351
5352 // A separate thread could have moved us all the way to initialized. A "simple" example
5353 // involves a subclass of the current class being initialized at the same time (which
5354 // will implicitly initialize the superclass, if scheduled that way). b/28254258
5355 DCHECK(!klass->IsErroneous()) << klass->GetStatus();
5356 if (klass->IsInitialized()) {
5357 return true;
5358 }
5359 }
5360
5361 // If the class is ClassStatus::kInitializing, either this thread is
5362 // initializing higher up the stack or another thread has beat us
5363 // to initializing and we need to wait. Either way, this
5364 // invocation of InitializeClass will not be responsible for
5365 // running <clinit> and will return.
5366 if (klass->GetStatus() == ClassStatus::kInitializing) {
5367 // Could have got an exception during verification.
5368 if (self->IsExceptionPending()) {
5369 VlogClassInitializationFailure(klass);
5370 return false;
5371 }
5372 // We caught somebody else in the act; was it us?
5373 if (klass->GetClinitThreadId() == self->GetTid()) {
5374 // Yes. That's fine. Return so we can continue initializing.
5375 return true;
5376 }
5377 // No. That's fine. Wait for another thread to finish initializing.
5378 return WaitForInitializeClass(klass, self, lock);
5379 }
5380
5381 // Try to get the oat class's status for this class if the oat file is present. The compiler
5382 // tries to validate superclass descriptors, and writes the result into the oat file.
5383 // Runtime correctness is guaranteed by classpath checks done on loading. If the classpath
5384 // is different at runtime than it was at compile time, the oat file is rejected. So if the
5385 // oat file is present, the classpaths must match, and the runtime time check can be skipped.
5386 bool has_oat_class = false;
5387 const Runtime* runtime = Runtime::Current();
5388 const OatFile::OatClass oat_class = (runtime->IsStarted() && !runtime->IsAotCompiler())
5389 ? OatFile::FindOatClass(klass->GetDexFile(), klass->GetDexClassDefIndex(), &has_oat_class)
5390 : OatFile::OatClass::Invalid();
5391 if (oat_class.GetStatus() < ClassStatus::kSuperclassValidated &&
5392 !ValidateSuperClassDescriptors(klass)) {
5393 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
5394 return false;
5395 }
5396 self->AllowThreadSuspension();
5397
5398 CHECK_EQ(klass->GetStatus(), ClassStatus::kVerified) << klass->PrettyClass()
5399 << " self.tid=" << self->GetTid() << " clinit.tid=" << klass->GetClinitThreadId();
5400
5401 // From here out other threads may observe that we're initializing and so changes of state
5402 // require the a notification.
5403 klass->SetClinitThreadId(self->GetTid());
5404 mirror::Class::SetStatus(klass, ClassStatus::kInitializing, self);
5405
5406 t0 = NanoTime();
5407 }
5408
5409 uint64_t t_sub = 0;
5410
5411 // Initialize super classes, must be done while initializing for the JLS.
5412 if (!klass->IsInterface() && klass->HasSuperClass()) {
5413 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
5414 if (!super_class->IsInitialized()) {
5415 CHECK(!super_class->IsInterface());
5416 CHECK(can_init_parents);
5417 StackHandleScope<1> hs(self);
5418 Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class));
5419 uint64_t super_t0 = NanoTime();
5420 bool super_initialized = InitializeClass(self, handle_scope_super, can_init_statics, true);
5421 uint64_t super_t1 = NanoTime();
5422 if (!super_initialized) {
5423 // The super class was verified ahead of entering initializing, we should only be here if
5424 // the super class became erroneous due to initialization.
5425 // For the case of aot compiler, the super class might also be initializing but we don't
5426 // want to process circular dependencies in pre-compile.
5427 CHECK(self->IsExceptionPending())
5428 << "Super class initialization failed for "
5429 << handle_scope_super->PrettyDescriptor()
5430 << " that has unexpected status " << handle_scope_super->GetStatus()
5431 << "\nPending exception:\n"
5432 << (self->GetException() != nullptr ? self->GetException()->Dump() : "");
5433 ObjectLock<mirror::Class> lock(self, klass);
5434 // Initialization failed because the super-class is erroneous.
5435 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
5436 return false;
5437 }
5438 t_sub = super_t1 - super_t0;
5439 }
5440 }
5441
5442 if (!klass->IsInterface()) {
5443 // Initialize interfaces with default methods for the JLS.
5444 size_t num_direct_interfaces = klass->NumDirectInterfaces();
5445 // Only setup the (expensive) handle scope if we actually need to.
5446 if (UNLIKELY(num_direct_interfaces > 0)) {
5447 StackHandleScope<1> hs_iface(self);
5448 MutableHandle<mirror::Class> handle_scope_iface(hs_iface.NewHandle<mirror::Class>(nullptr));
5449 for (size_t i = 0; i < num_direct_interfaces; i++) {
5450 handle_scope_iface.Assign(mirror::Class::GetDirectInterface(self, klass.Get(), i));
5451 CHECK(handle_scope_iface != nullptr) << klass->PrettyDescriptor() << " iface #" << i;
5452 CHECK(handle_scope_iface->IsInterface());
5453 if (handle_scope_iface->HasBeenRecursivelyInitialized()) {
5454 // We have already done this for this interface. Skip it.
5455 continue;
5456 }
5457 // We cannot just call initialize class directly because we need to ensure that ALL
5458 // interfaces with default methods are initialized. Non-default interface initialization
5459 // will not affect other non-default super-interfaces.
5460 uint64_t inf_t0 = NanoTime(); // This is not very precise, misses all walking.
5461 bool iface_initialized = InitializeDefaultInterfaceRecursive(self,
5462 handle_scope_iface,
5463 can_init_statics,
5464 can_init_parents);
5465 uint64_t inf_t1 = NanoTime();
5466 if (!iface_initialized) {
5467 ObjectLock<mirror::Class> lock(self, klass);
5468 // Initialization failed because one of our interfaces with default methods is erroneous.
5469 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
5470 return false;
5471 }
5472 t_sub += inf_t1 - inf_t0;
5473 }
5474 }
5475 }
5476
5477 const size_t num_static_fields = klass->NumStaticFields();
5478 if (num_static_fields > 0) {
5479 const dex::ClassDef* dex_class_def = klass->GetClassDef();
5480 CHECK(dex_class_def != nullptr);
5481 StackHandleScope<3> hs(self);
5482 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
5483 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
5484
5485 // Eagerly fill in static fields so that the we don't have to do as many expensive
5486 // Class::FindStaticField in ResolveField.
5487 for (size_t i = 0; i < num_static_fields; ++i) {
5488 ArtField* field = klass->GetStaticField(i);
5489 const uint32_t field_idx = field->GetDexFieldIndex();
5490 ArtField* resolved_field = dex_cache->GetResolvedField(field_idx, image_pointer_size_);
5491 if (resolved_field == nullptr) {
5492 // Populating cache of a dex file which defines `klass` should always be allowed.
5493 DCHECK(!hiddenapi::ShouldDenyAccessToMember(
5494 field,
5495 hiddenapi::AccessContext(class_loader.Get(), dex_cache.Get()),
5496 hiddenapi::AccessMethod::kNone));
5497 dex_cache->SetResolvedField(field_idx, field, image_pointer_size_);
5498 } else {
5499 DCHECK_EQ(field, resolved_field);
5500 }
5501 }
5502
5503 annotations::RuntimeEncodedStaticFieldValueIterator value_it(dex_cache,
5504 class_loader,
5505 this,
5506 *dex_class_def);
5507 const DexFile& dex_file = *dex_cache->GetDexFile();
5508
5509 if (value_it.HasNext()) {
5510 ClassAccessor accessor(dex_file, *dex_class_def);
5511 CHECK(can_init_statics);
5512 for (const ClassAccessor::Field& field : accessor.GetStaticFields()) {
5513 if (!value_it.HasNext()) {
5514 break;
5515 }
5516 ArtField* art_field = ResolveField(field.GetIndex(),
5517 dex_cache,
5518 class_loader,
5519 /* is_static= */ true);
5520 if (Runtime::Current()->IsActiveTransaction()) {
5521 value_it.ReadValueToField<true>(art_field);
5522 } else {
5523 value_it.ReadValueToField<false>(art_field);
5524 }
5525 if (self->IsExceptionPending()) {
5526 break;
5527 }
5528 value_it.Next();
5529 }
5530 DCHECK(self->IsExceptionPending() || !value_it.HasNext());
5531 }
5532 }
5533
5534
5535 if (!self->IsExceptionPending()) {
5536 ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_);
5537 if (clinit != nullptr) {
5538 CHECK(can_init_statics);
5539 JValue result;
5540 clinit->Invoke(self, nullptr, 0, &result, "V");
5541 }
5542 }
5543 self->AllowThreadSuspension();
5544 uint64_t t1 = NanoTime();
5545
5546 VisiblyInitializedCallback* callback = nullptr;
5547 bool success = true;
5548 {
5549 ObjectLock<mirror::Class> lock(self, klass);
5550
5551 if (self->IsExceptionPending()) {
5552 WrapExceptionInInitializer(klass);
5553 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
5554 success = false;
5555 } else if (Runtime::Current()->IsTransactionAborted()) {
5556 // The exception thrown when the transaction aborted has been caught and cleared
5557 // so we need to throw it again now.
5558 VLOG(compiler) << "Return from class initializer of "
5559 << mirror::Class::PrettyDescriptor(klass.Get())
5560 << " without exception while transaction was aborted: re-throw it now.";
5561 Runtime::Current()->ThrowTransactionAbortError(self);
5562 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
5563 success = false;
5564 } else {
5565 RuntimeStats* global_stats = Runtime::Current()->GetStats();
5566 RuntimeStats* thread_stats = self->GetStats();
5567 ++global_stats->class_init_count;
5568 ++thread_stats->class_init_count;
5569 global_stats->class_init_time_ns += (t1 - t0 - t_sub);
5570 thread_stats->class_init_time_ns += (t1 - t0 - t_sub);
5571 // Set the class as initialized except if failed to initialize static fields.
5572 callback = MarkClassInitialized(self, klass);
5573 if (VLOG_IS_ON(class_linker)) {
5574 std::string temp;
5575 LOG(INFO) << "Initialized class " << klass->GetDescriptor(&temp) << " from " <<
5576 klass->GetLocation();
5577 }
5578 }
5579 }
5580 if (callback != nullptr) {
5581 callback->MakeVisible(self);
5582 }
5583 return success;
5584 }
5585
5586 // We recursively run down the tree of interfaces. We need to do this in the order they are declared
5587 // 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)5588 bool ClassLinker::InitializeDefaultInterfaceRecursive(Thread* self,
5589 Handle<mirror::Class> iface,
5590 bool can_init_statics,
5591 bool can_init_parents) {
5592 CHECK(iface->IsInterface());
5593 size_t num_direct_ifaces = iface->NumDirectInterfaces();
5594 // Only create the (expensive) handle scope if we need it.
5595 if (UNLIKELY(num_direct_ifaces > 0)) {
5596 StackHandleScope<1> hs(self);
5597 MutableHandle<mirror::Class> handle_super_iface(hs.NewHandle<mirror::Class>(nullptr));
5598 // First we initialize all of iface's super-interfaces recursively.
5599 for (size_t i = 0; i < num_direct_ifaces; i++) {
5600 ObjPtr<mirror::Class> super_iface = mirror::Class::GetDirectInterface(self, iface.Get(), i);
5601 CHECK(super_iface != nullptr) << iface->PrettyDescriptor() << " iface #" << i;
5602 if (!super_iface->HasBeenRecursivelyInitialized()) {
5603 // Recursive step
5604 handle_super_iface.Assign(super_iface);
5605 if (!InitializeDefaultInterfaceRecursive(self,
5606 handle_super_iface,
5607 can_init_statics,
5608 can_init_parents)) {
5609 return false;
5610 }
5611 }
5612 }
5613 }
5614
5615 bool result = true;
5616 // Then we initialize 'iface' if it has default methods. We do not need to (and in fact must not)
5617 // initialize if we don't have default methods.
5618 if (iface->HasDefaultMethods()) {
5619 result = EnsureInitialized(self, iface, can_init_statics, can_init_parents);
5620 }
5621
5622 // Mark that this interface has undergone recursive default interface initialization so we know we
5623 // can skip it on any later class initializations. We do this even if we are not a default
5624 // interface since we can still avoid the traversal. This is purely a performance optimization.
5625 if (result) {
5626 // TODO This should be done in a better way
5627 // Note: Use a try-lock to avoid blocking when someone else is holding the lock on this
5628 // interface. It is bad (Java) style, but not impossible. Marking the recursive
5629 // initialization is a performance optimization (to avoid another idempotent visit
5630 // for other implementing classes/interfaces), and can be revisited later.
5631 ObjectTryLock<mirror::Class> lock(self, iface);
5632 if (lock.Acquired()) {
5633 iface->SetRecursivelyInitialized();
5634 }
5635 }
5636 return result;
5637 }
5638
WaitForInitializeClass(Handle<mirror::Class> klass,Thread * self,ObjectLock<mirror::Class> & lock)5639 bool ClassLinker::WaitForInitializeClass(Handle<mirror::Class> klass,
5640 Thread* self,
5641 ObjectLock<mirror::Class>& lock)
5642 REQUIRES_SHARED(Locks::mutator_lock_) {
5643 while (true) {
5644 self->AssertNoPendingException();
5645 CHECK(!klass->IsInitialized());
5646 lock.WaitIgnoringInterrupts();
5647
5648 // When we wake up, repeat the test for init-in-progress. If
5649 // there's an exception pending (only possible if
5650 // we were not using WaitIgnoringInterrupts), bail out.
5651 if (self->IsExceptionPending()) {
5652 WrapExceptionInInitializer(klass);
5653 mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self);
5654 return false;
5655 }
5656 // Spurious wakeup? Go back to waiting.
5657 if (klass->GetStatus() == ClassStatus::kInitializing) {
5658 continue;
5659 }
5660 if (klass->GetStatus() == ClassStatus::kVerified &&
5661 Runtime::Current()->IsAotCompiler()) {
5662 // Compile time initialization failed.
5663 return false;
5664 }
5665 if (klass->IsErroneous()) {
5666 // The caller wants an exception, but it was thrown in a
5667 // different thread. Synthesize one here.
5668 ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread",
5669 klass->PrettyDescriptor().c_str());
5670 VlogClassInitializationFailure(klass);
5671 return false;
5672 }
5673 if (klass->IsInitialized()) {
5674 return true;
5675 }
5676 LOG(FATAL) << "Unexpected class status. " << klass->PrettyClass() << " is "
5677 << klass->GetStatus();
5678 }
5679 UNREACHABLE();
5680 }
5681
ThrowSignatureCheckResolveReturnTypeException(Handle<mirror::Class> klass,Handle<mirror::Class> super_klass,ArtMethod * method,ArtMethod * m)5682 static void ThrowSignatureCheckResolveReturnTypeException(Handle<mirror::Class> klass,
5683 Handle<mirror::Class> super_klass,
5684 ArtMethod* method,
5685 ArtMethod* m)
5686 REQUIRES_SHARED(Locks::mutator_lock_) {
5687 DCHECK(Thread::Current()->IsExceptionPending());
5688 DCHECK(!m->IsProxyMethod());
5689 const DexFile* dex_file = m->GetDexFile();
5690 const dex::MethodId& method_id = dex_file->GetMethodId(m->GetDexMethodIndex());
5691 const dex::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
5692 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
5693 std::string return_type = dex_file->PrettyType(return_type_idx);
5694 std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader());
5695 ThrowWrappedLinkageError(klass.Get(),
5696 "While checking class %s method %s signature against %s %s: "
5697 "Failed to resolve return type %s with %s",
5698 mirror::Class::PrettyDescriptor(klass.Get()).c_str(),
5699 ArtMethod::PrettyMethod(method).c_str(),
5700 super_klass->IsInterface() ? "interface" : "superclass",
5701 mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(),
5702 return_type.c_str(), class_loader.c_str());
5703 }
5704
ThrowSignatureCheckResolveArgException(Handle<mirror::Class> klass,Handle<mirror::Class> super_klass,ArtMethod * method,ArtMethod * m,uint32_t index,dex::TypeIndex arg_type_idx)5705 static void ThrowSignatureCheckResolveArgException(Handle<mirror::Class> klass,
5706 Handle<mirror::Class> super_klass,
5707 ArtMethod* method,
5708 ArtMethod* m,
5709 uint32_t index,
5710 dex::TypeIndex arg_type_idx)
5711 REQUIRES_SHARED(Locks::mutator_lock_) {
5712 DCHECK(Thread::Current()->IsExceptionPending());
5713 DCHECK(!m->IsProxyMethod());
5714 const DexFile* dex_file = m->GetDexFile();
5715 std::string arg_type = dex_file->PrettyType(arg_type_idx);
5716 std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader());
5717 ThrowWrappedLinkageError(klass.Get(),
5718 "While checking class %s method %s signature against %s %s: "
5719 "Failed to resolve arg %u type %s with %s",
5720 mirror::Class::PrettyDescriptor(klass.Get()).c_str(),
5721 ArtMethod::PrettyMethod(method).c_str(),
5722 super_klass->IsInterface() ? "interface" : "superclass",
5723 mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(),
5724 index, arg_type.c_str(), class_loader.c_str());
5725 }
5726
ThrowSignatureMismatch(Handle<mirror::Class> klass,Handle<mirror::Class> super_klass,ArtMethod * method,const std::string & error_msg)5727 static void ThrowSignatureMismatch(Handle<mirror::Class> klass,
5728 Handle<mirror::Class> super_klass,
5729 ArtMethod* method,
5730 const std::string& error_msg)
5731 REQUIRES_SHARED(Locks::mutator_lock_) {
5732 ThrowLinkageError(klass.Get(),
5733 "Class %s method %s resolves differently in %s %s: %s",
5734 mirror::Class::PrettyDescriptor(klass.Get()).c_str(),
5735 ArtMethod::PrettyMethod(method).c_str(),
5736 super_klass->IsInterface() ? "interface" : "superclass",
5737 mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(),
5738 error_msg.c_str());
5739 }
5740
HasSameSignatureWithDifferentClassLoaders(Thread * self,Handle<mirror::Class> klass,Handle<mirror::Class> super_klass,ArtMethod * method1,ArtMethod * method2)5741 static bool HasSameSignatureWithDifferentClassLoaders(Thread* self,
5742 Handle<mirror::Class> klass,
5743 Handle<mirror::Class> super_klass,
5744 ArtMethod* method1,
5745 ArtMethod* method2)
5746 REQUIRES_SHARED(Locks::mutator_lock_) {
5747 {
5748 StackHandleScope<1> hs(self);
5749 Handle<mirror::Class> return_type(hs.NewHandle(method1->ResolveReturnType()));
5750 if (UNLIKELY(return_type == nullptr)) {
5751 ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method1);
5752 return false;
5753 }
5754 ObjPtr<mirror::Class> other_return_type = method2->ResolveReturnType();
5755 if (UNLIKELY(other_return_type == nullptr)) {
5756 ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method2);
5757 return false;
5758 }
5759 if (UNLIKELY(other_return_type != return_type.Get())) {
5760 ThrowSignatureMismatch(klass, super_klass, method1,
5761 StringPrintf("Return types mismatch: %s(%p) vs %s(%p)",
5762 return_type->PrettyClassAndClassLoader().c_str(),
5763 return_type.Get(),
5764 other_return_type->PrettyClassAndClassLoader().c_str(),
5765 other_return_type.Ptr()));
5766 return false;
5767 }
5768 }
5769 const dex::TypeList* types1 = method1->GetParameterTypeList();
5770 const dex::TypeList* types2 = method2->GetParameterTypeList();
5771 if (types1 == nullptr) {
5772 if (types2 != nullptr && types2->Size() != 0) {
5773 ThrowSignatureMismatch(klass, super_klass, method1,
5774 StringPrintf("Type list mismatch with %s",
5775 method2->PrettyMethod(true).c_str()));
5776 return false;
5777 }
5778 return true;
5779 } else if (UNLIKELY(types2 == nullptr)) {
5780 if (types1->Size() != 0) {
5781 ThrowSignatureMismatch(klass, super_klass, method1,
5782 StringPrintf("Type list mismatch with %s",
5783 method2->PrettyMethod(true).c_str()));
5784 return false;
5785 }
5786 return true;
5787 }
5788 uint32_t num_types = types1->Size();
5789 if (UNLIKELY(num_types != types2->Size())) {
5790 ThrowSignatureMismatch(klass, super_klass, method1,
5791 StringPrintf("Type list mismatch with %s",
5792 method2->PrettyMethod(true).c_str()));
5793 return false;
5794 }
5795 for (uint32_t i = 0; i < num_types; ++i) {
5796 StackHandleScope<1> hs(self);
5797 dex::TypeIndex param_type_idx = types1->GetTypeItem(i).type_idx_;
5798 Handle<mirror::Class> param_type(hs.NewHandle(
5799 method1->ResolveClassFromTypeIndex(param_type_idx)));
5800 if (UNLIKELY(param_type == nullptr)) {
5801 ThrowSignatureCheckResolveArgException(klass, super_klass, method1,
5802 method1, i, param_type_idx);
5803 return false;
5804 }
5805 dex::TypeIndex other_param_type_idx = types2->GetTypeItem(i).type_idx_;
5806 ObjPtr<mirror::Class> other_param_type =
5807 method2->ResolveClassFromTypeIndex(other_param_type_idx);
5808 if (UNLIKELY(other_param_type == nullptr)) {
5809 ThrowSignatureCheckResolveArgException(klass, super_klass, method1,
5810 method2, i, other_param_type_idx);
5811 return false;
5812 }
5813 if (UNLIKELY(param_type.Get() != other_param_type)) {
5814 ThrowSignatureMismatch(klass, super_klass, method1,
5815 StringPrintf("Parameter %u type mismatch: %s(%p) vs %s(%p)",
5816 i,
5817 param_type->PrettyClassAndClassLoader().c_str(),
5818 param_type.Get(),
5819 other_param_type->PrettyClassAndClassLoader().c_str(),
5820 other_param_type.Ptr()));
5821 return false;
5822 }
5823 }
5824 return true;
5825 }
5826
5827
ValidateSuperClassDescriptors(Handle<mirror::Class> klass)5828 bool ClassLinker::ValidateSuperClassDescriptors(Handle<mirror::Class> klass) {
5829 if (klass->IsInterface()) {
5830 return true;
5831 }
5832 // Begin with the methods local to the superclass.
5833 Thread* self = Thread::Current();
5834 StackHandleScope<1> hs(self);
5835 MutableHandle<mirror::Class> super_klass(hs.NewHandle<mirror::Class>(nullptr));
5836 if (klass->HasSuperClass() &&
5837 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
5838 super_klass.Assign(klass->GetSuperClass());
5839 for (int i = klass->GetSuperClass()->GetVTableLength() - 1; i >= 0; --i) {
5840 auto* m = klass->GetVTableEntry(i, image_pointer_size_);
5841 auto* super_m = klass->GetSuperClass()->GetVTableEntry(i, image_pointer_size_);
5842 if (m != super_m) {
5843 if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self,
5844 klass,
5845 super_klass,
5846 m,
5847 super_m))) {
5848 self->AssertPendingException();
5849 return false;
5850 }
5851 }
5852 }
5853 }
5854 for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) {
5855 super_klass.Assign(klass->GetIfTable()->GetInterface(i));
5856 if (klass->GetClassLoader() != super_klass->GetClassLoader()) {
5857 uint32_t num_methods = super_klass->NumVirtualMethods();
5858 for (uint32_t j = 0; j < num_methods; ++j) {
5859 auto* m = klass->GetIfTable()->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>(
5860 j, image_pointer_size_);
5861 auto* super_m = super_klass->GetVirtualMethod(j, image_pointer_size_);
5862 if (m != super_m) {
5863 if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self,
5864 klass,
5865 super_klass,
5866 m,
5867 super_m))) {
5868 self->AssertPendingException();
5869 return false;
5870 }
5871 }
5872 }
5873 }
5874 }
5875 return true;
5876 }
5877
EnsureInitialized(Thread * self,Handle<mirror::Class> c,bool can_init_fields,bool can_init_parents)5878 bool ClassLinker::EnsureInitialized(Thread* self,
5879 Handle<mirror::Class> c,
5880 bool can_init_fields,
5881 bool can_init_parents) {
5882 DCHECK(c != nullptr);
5883
5884 if (c->IsInitialized()) {
5885 // If we've seen an initialized but not visibly initialized class
5886 // many times, request visible initialization.
5887 if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) {
5888 // Thanks to the x86 memory model classes skip the initialized status.
5889 DCHECK(c->IsVisiblyInitialized());
5890 } else if (UNLIKELY(!c->IsVisiblyInitialized())) {
5891 if (self->IncrementMakeVisiblyInitializedCounter()) {
5892 MakeInitializedClassesVisiblyInitialized(self, /*wait=*/ false);
5893 }
5894 }
5895 DCHECK(c->WasVerificationAttempted()) << c->PrettyClassAndClassLoader();
5896 return true;
5897 }
5898 // SubtypeCheckInfo::Initialized must happen-before any new-instance for that type.
5899 //
5900 // Ensure the bitstring is initialized before any of the class initialization
5901 // logic occurs. Once a class initializer starts running, objects can
5902 // escape into the heap and use the subtype checking code.
5903 //
5904 // Note: A class whose SubtypeCheckInfo is at least Initialized means it
5905 // can be used as a source for the IsSubClass check, and that all ancestors
5906 // of the class are Assigned (can be used as a target for IsSubClass check)
5907 // or Overflowed (can be used as a source for IsSubClass check).
5908 if (kBitstringSubtypeCheckEnabled) {
5909 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
5910 SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(c.Get());
5911 // TODO: Avoid taking subtype_check_lock_ if SubtypeCheck is already initialized.
5912 }
5913 const bool success = InitializeClass(self, c, can_init_fields, can_init_parents);
5914 if (!success) {
5915 if (can_init_fields && can_init_parents) {
5916 CHECK(self->IsExceptionPending()) << c->PrettyClass();
5917 }
5918 } else {
5919 self->AssertNoPendingException();
5920 }
5921 return success;
5922 }
5923
FixupTemporaryDeclaringClass(ObjPtr<mirror::Class> temp_class,ObjPtr<mirror::Class> new_class)5924 void ClassLinker::FixupTemporaryDeclaringClass(ObjPtr<mirror::Class> temp_class,
5925 ObjPtr<mirror::Class> new_class) {
5926 DCHECK_EQ(temp_class->NumInstanceFields(), 0u);
5927 for (ArtField& field : new_class->GetIFields()) {
5928 if (field.GetDeclaringClass() == temp_class) {
5929 field.SetDeclaringClass(new_class);
5930 }
5931 }
5932
5933 DCHECK_EQ(temp_class->NumStaticFields(), 0u);
5934 for (ArtField& field : new_class->GetSFields()) {
5935 if (field.GetDeclaringClass() == temp_class) {
5936 field.SetDeclaringClass(new_class);
5937 }
5938 }
5939
5940 DCHECK_EQ(temp_class->NumDirectMethods(), 0u);
5941 DCHECK_EQ(temp_class->NumVirtualMethods(), 0u);
5942 for (auto& method : new_class->GetMethods(image_pointer_size_)) {
5943 if (method.GetDeclaringClass() == temp_class) {
5944 method.SetDeclaringClass(new_class);
5945 }
5946 }
5947
5948 // Make sure the remembered set and mod-union tables know that we updated some of the native
5949 // roots.
5950 WriteBarrier::ForEveryFieldWrite(new_class);
5951 }
5952
RegisterClassLoader(ObjPtr<mirror::ClassLoader> class_loader)5953 void ClassLinker::RegisterClassLoader(ObjPtr<mirror::ClassLoader> class_loader) {
5954 CHECK(class_loader->GetAllocator() == nullptr);
5955 CHECK(class_loader->GetClassTable() == nullptr);
5956 Thread* const self = Thread::Current();
5957 ClassLoaderData data;
5958 data.weak_root = self->GetJniEnv()->GetVm()->AddWeakGlobalRef(self, class_loader);
5959 // Create and set the class table.
5960 data.class_table = new ClassTable;
5961 class_loader->SetClassTable(data.class_table);
5962 // Create and set the linear allocator.
5963 data.allocator = Runtime::Current()->CreateLinearAlloc();
5964 class_loader->SetAllocator(data.allocator);
5965 // Add to the list so that we know to free the data later.
5966 class_loaders_.push_back(data);
5967 }
5968
InsertClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader)5969 ClassTable* ClassLinker::InsertClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) {
5970 if (class_loader == nullptr) {
5971 return boot_class_table_.get();
5972 }
5973 ClassTable* class_table = class_loader->GetClassTable();
5974 if (class_table == nullptr) {
5975 RegisterClassLoader(class_loader);
5976 class_table = class_loader->GetClassTable();
5977 DCHECK(class_table != nullptr);
5978 }
5979 return class_table;
5980 }
5981
ClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader)5982 ClassTable* ClassLinker::ClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) {
5983 return class_loader == nullptr ? boot_class_table_.get() : class_loader->GetClassTable();
5984 }
5985
FindSuperImt(ObjPtr<mirror::Class> klass,PointerSize pointer_size)5986 static ImTable* FindSuperImt(ObjPtr<mirror::Class> klass, PointerSize pointer_size)
5987 REQUIRES_SHARED(Locks::mutator_lock_) {
5988 while (klass->HasSuperClass()) {
5989 klass = klass->GetSuperClass();
5990 if (klass->ShouldHaveImt()) {
5991 return klass->GetImt(pointer_size);
5992 }
5993 }
5994 return nullptr;
5995 }
5996
LinkClass(Thread * self,const char * descriptor,Handle<mirror::Class> klass,Handle<mirror::ObjectArray<mirror::Class>> interfaces,MutableHandle<mirror::Class> * h_new_class_out)5997 bool ClassLinker::LinkClass(Thread* self,
5998 const char* descriptor,
5999 Handle<mirror::Class> klass,
6000 Handle<mirror::ObjectArray<mirror::Class>> interfaces,
6001 MutableHandle<mirror::Class>* h_new_class_out) {
6002 CHECK_EQ(ClassStatus::kLoaded, klass->GetStatus());
6003
6004 if (!LinkSuperClass(klass)) {
6005 return false;
6006 }
6007 ArtMethod* imt_data[ImTable::kSize];
6008 // If there are any new conflicts compared to super class.
6009 bool new_conflict = false;
6010 std::fill_n(imt_data, arraysize(imt_data), Runtime::Current()->GetImtUnimplementedMethod());
6011 if (!LinkMethods(self, klass, interfaces, &new_conflict, imt_data)) {
6012 return false;
6013 }
6014 if (!LinkInstanceFields(self, klass)) {
6015 return false;
6016 }
6017 size_t class_size;
6018 if (!LinkStaticFields(self, klass, &class_size)) {
6019 return false;
6020 }
6021 CreateReferenceInstanceOffsets(klass);
6022 CHECK_EQ(ClassStatus::kLoaded, klass->GetStatus());
6023
6024 ImTable* imt = nullptr;
6025 if (klass->ShouldHaveImt()) {
6026 // If there are any new conflicts compared to the super class we can not make a copy. There
6027 // can be cases where both will have a conflict method at the same slot without having the same
6028 // set of conflicts. In this case, we can not share the IMT since the conflict table slow path
6029 // will possibly create a table that is incorrect for either of the classes.
6030 // Same IMT with new_conflict does not happen very often.
6031 if (!new_conflict) {
6032 ImTable* super_imt = FindSuperImt(klass.Get(), image_pointer_size_);
6033 if (super_imt != nullptr) {
6034 bool imt_equals = true;
6035 for (size_t i = 0; i < ImTable::kSize && imt_equals; ++i) {
6036 imt_equals = imt_equals && (super_imt->Get(i, image_pointer_size_) == imt_data[i]);
6037 }
6038 if (imt_equals) {
6039 imt = super_imt;
6040 }
6041 }
6042 }
6043 if (imt == nullptr) {
6044 LinearAlloc* allocator = GetAllocatorForClassLoader(klass->GetClassLoader());
6045 imt = reinterpret_cast<ImTable*>(
6046 allocator->Alloc(self, ImTable::SizeInBytes(image_pointer_size_)));
6047 if (imt == nullptr) {
6048 return false;
6049 }
6050 imt->Populate(imt_data, image_pointer_size_);
6051 }
6052 }
6053
6054 if (!klass->IsTemp() || (!init_done_ && klass->GetClassSize() == class_size)) {
6055 // We don't need to retire this class as it has no embedded tables or it was created the
6056 // correct size during class linker initialization.
6057 CHECK_EQ(klass->GetClassSize(), class_size) << klass->PrettyDescriptor();
6058
6059 if (klass->ShouldHaveEmbeddedVTable()) {
6060 klass->PopulateEmbeddedVTable(image_pointer_size_);
6061 }
6062 if (klass->ShouldHaveImt()) {
6063 klass->SetImt(imt, image_pointer_size_);
6064 }
6065
6066 // Update CHA info based on whether we override methods.
6067 // Have to do this before setting the class as resolved which allows
6068 // instantiation of klass.
6069 if (LIKELY(descriptor != nullptr) && cha_ != nullptr) {
6070 cha_->UpdateAfterLoadingOf(klass);
6071 }
6072
6073 // This will notify waiters on klass that saw the not yet resolved
6074 // class in the class_table_ during EnsureResolved.
6075 mirror::Class::SetStatus(klass, ClassStatus::kResolved, self);
6076 h_new_class_out->Assign(klass.Get());
6077 } else {
6078 CHECK(!klass->IsResolved());
6079 // Retire the temporary class and create the correctly sized resolved class.
6080 StackHandleScope<1> hs(self);
6081 Handle<mirror::Class> h_new_class =
6082 hs.NewHandle(mirror::Class::CopyOf(klass, self, class_size, imt, image_pointer_size_));
6083 // Set arrays to null since we don't want to have multiple classes with the same ArtField or
6084 // ArtMethod array pointers. If this occurs, it causes bugs in remembered sets since the GC
6085 // may not see any references to the target space and clean the card for a class if another
6086 // class had the same array pointer.
6087 klass->SetMethodsPtrUnchecked(nullptr, 0, 0);
6088 klass->SetSFieldsPtrUnchecked(nullptr);
6089 klass->SetIFieldsPtrUnchecked(nullptr);
6090 if (UNLIKELY(h_new_class == nullptr)) {
6091 self->AssertPendingOOMException();
6092 mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self);
6093 return false;
6094 }
6095
6096 CHECK_EQ(h_new_class->GetClassSize(), class_size);
6097 ObjectLock<mirror::Class> lock(self, h_new_class);
6098 FixupTemporaryDeclaringClass(klass.Get(), h_new_class.Get());
6099
6100 if (LIKELY(descriptor != nullptr)) {
6101 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
6102 const ObjPtr<mirror::ClassLoader> class_loader = h_new_class.Get()->GetClassLoader();
6103 ClassTable* const table = InsertClassTableForClassLoader(class_loader);
6104 const ObjPtr<mirror::Class> existing =
6105 table->UpdateClass(descriptor, h_new_class.Get(), ComputeModifiedUtf8Hash(descriptor));
6106 if (class_loader != nullptr) {
6107 // We updated the class in the class table, perform the write barrier so that the GC knows
6108 // about the change.
6109 WriteBarrier::ForEveryFieldWrite(class_loader);
6110 }
6111 CHECK_EQ(existing, klass.Get());
6112 if (log_new_roots_) {
6113 new_class_roots_.push_back(GcRoot<mirror::Class>(h_new_class.Get()));
6114 }
6115 }
6116
6117 // Update CHA info based on whether we override methods.
6118 // Have to do this before setting the class as resolved which allows
6119 // instantiation of klass.
6120 if (LIKELY(descriptor != nullptr) && cha_ != nullptr) {
6121 cha_->UpdateAfterLoadingOf(h_new_class);
6122 }
6123
6124 // This will notify waiters on temp class that saw the not yet resolved class in the
6125 // class_table_ during EnsureResolved.
6126 mirror::Class::SetStatus(klass, ClassStatus::kRetired, self);
6127
6128 CHECK_EQ(h_new_class->GetStatus(), ClassStatus::kResolving);
6129 // This will notify waiters on new_class that saw the not yet resolved
6130 // class in the class_table_ during EnsureResolved.
6131 mirror::Class::SetStatus(h_new_class, ClassStatus::kResolved, self);
6132 // Return the new class.
6133 h_new_class_out->Assign(h_new_class.Get());
6134 }
6135 return true;
6136 }
6137
LoadSuperAndInterfaces(Handle<mirror::Class> klass,const DexFile & dex_file)6138 bool ClassLinker::LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) {
6139 CHECK_EQ(ClassStatus::kIdx, klass->GetStatus());
6140 const dex::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex());
6141 dex::TypeIndex super_class_idx = class_def.superclass_idx_;
6142 if (super_class_idx.IsValid()) {
6143 // Check that a class does not inherit from itself directly.
6144 //
6145 // TODO: This is a cheap check to detect the straightforward case
6146 // of a class extending itself (b/28685551), but we should do a
6147 // proper cycle detection on loaded classes, to detect all cases
6148 // of class circularity errors (b/28830038).
6149 if (super_class_idx == class_def.class_idx_) {
6150 ThrowClassCircularityError(klass.Get(),
6151 "Class %s extends itself",
6152 klass->PrettyDescriptor().c_str());
6153 return false;
6154 }
6155
6156 ObjPtr<mirror::Class> super_class = ResolveType(super_class_idx, klass.Get());
6157 if (super_class == nullptr) {
6158 DCHECK(Thread::Current()->IsExceptionPending());
6159 return false;
6160 }
6161 // Verify
6162 if (!klass->CanAccess(super_class)) {
6163 ThrowIllegalAccessError(klass.Get(), "Class %s extended by class %s is inaccessible",
6164 super_class->PrettyDescriptor().c_str(),
6165 klass->PrettyDescriptor().c_str());
6166 return false;
6167 }
6168 CHECK(super_class->IsResolved());
6169 klass->SetSuperClass(super_class);
6170 }
6171 const dex::TypeList* interfaces = dex_file.GetInterfacesList(class_def);
6172 if (interfaces != nullptr) {
6173 for (size_t i = 0; i < interfaces->Size(); i++) {
6174 dex::TypeIndex idx = interfaces->GetTypeItem(i).type_idx_;
6175 ObjPtr<mirror::Class> interface = ResolveType(idx, klass.Get());
6176 if (interface == nullptr) {
6177 DCHECK(Thread::Current()->IsExceptionPending());
6178 return false;
6179 }
6180 // Verify
6181 if (!klass->CanAccess(interface)) {
6182 // TODO: the RI seemed to ignore this in my testing.
6183 ThrowIllegalAccessError(klass.Get(),
6184 "Interface %s implemented by class %s is inaccessible",
6185 interface->PrettyDescriptor().c_str(),
6186 klass->PrettyDescriptor().c_str());
6187 return false;
6188 }
6189 }
6190 }
6191 // Mark the class as loaded.
6192 mirror::Class::SetStatus(klass, ClassStatus::kLoaded, nullptr);
6193 return true;
6194 }
6195
LinkSuperClass(Handle<mirror::Class> klass)6196 bool ClassLinker::LinkSuperClass(Handle<mirror::Class> klass) {
6197 CHECK(!klass->IsPrimitive());
6198 ObjPtr<mirror::Class> super = klass->GetSuperClass();
6199 ObjPtr<mirror::Class> object_class = GetClassRoot<mirror::Object>(this);
6200 if (klass.Get() == object_class) {
6201 if (super != nullptr) {
6202 ThrowClassFormatError(klass.Get(), "java.lang.Object must not have a superclass");
6203 return false;
6204 }
6205 return true;
6206 }
6207 if (super == nullptr) {
6208 ThrowLinkageError(klass.Get(), "No superclass defined for class %s",
6209 klass->PrettyDescriptor().c_str());
6210 return false;
6211 }
6212 // Verify
6213 if (klass->IsInterface() && super != object_class) {
6214 ThrowClassFormatError(klass.Get(), "Interfaces must have java.lang.Object as superclass");
6215 return false;
6216 }
6217 if (super->IsFinal()) {
6218 ThrowVerifyError(klass.Get(),
6219 "Superclass %s of %s is declared final",
6220 super->PrettyDescriptor().c_str(),
6221 klass->PrettyDescriptor().c_str());
6222 return false;
6223 }
6224 if (super->IsInterface()) {
6225 ThrowIncompatibleClassChangeError(klass.Get(),
6226 "Superclass %s of %s is an interface",
6227 super->PrettyDescriptor().c_str(),
6228 klass->PrettyDescriptor().c_str());
6229 return false;
6230 }
6231 if (!klass->CanAccess(super)) {
6232 ThrowIllegalAccessError(klass.Get(), "Superclass %s is inaccessible to class %s",
6233 super->PrettyDescriptor().c_str(),
6234 klass->PrettyDescriptor().c_str());
6235 return false;
6236 }
6237
6238 // Inherit kAccClassIsFinalizable from the superclass in case this
6239 // class doesn't override finalize.
6240 if (super->IsFinalizable()) {
6241 klass->SetFinalizable();
6242 }
6243
6244 // Inherit class loader flag form super class.
6245 if (super->IsClassLoaderClass()) {
6246 klass->SetClassLoaderClass();
6247 }
6248
6249 // Inherit reference flags (if any) from the superclass.
6250 uint32_t reference_flags = (super->GetClassFlags() & mirror::kClassFlagReference);
6251 if (reference_flags != 0) {
6252 CHECK_EQ(klass->GetClassFlags(), 0u);
6253 klass->SetClassFlags(klass->GetClassFlags() | reference_flags);
6254 }
6255 // Disallow custom direct subclasses of java.lang.ref.Reference.
6256 if (init_done_ && super == GetClassRoot<mirror::Reference>(this)) {
6257 ThrowLinkageError(klass.Get(),
6258 "Class %s attempts to subclass java.lang.ref.Reference, which is not allowed",
6259 klass->PrettyDescriptor().c_str());
6260 return false;
6261 }
6262
6263 if (kIsDebugBuild) {
6264 // Ensure super classes are fully resolved prior to resolving fields..
6265 while (super != nullptr) {
6266 CHECK(super->IsResolved());
6267 super = super->GetSuperClass();
6268 }
6269 }
6270 return true;
6271 }
6272
6273 // 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)6274 bool ClassLinker::LinkMethods(Thread* self,
6275 Handle<mirror::Class> klass,
6276 Handle<mirror::ObjectArray<mirror::Class>> interfaces,
6277 bool* out_new_conflict,
6278 ArtMethod** out_imt) {
6279 self->AllowThreadSuspension();
6280 // A map from vtable indexes to the method they need to be updated to point to. Used because we
6281 // need to have default methods be in the virtuals array of each class but we don't set that up
6282 // until LinkInterfaceMethods.
6283 std::unordered_map<size_t, ClassLinker::MethodTranslation> default_translations;
6284 // Link virtual methods then interface methods.
6285 // We set up the interface lookup table first because we need it to determine if we need to update
6286 // any vtable entries with new default method implementations.
6287 return SetupInterfaceLookupTable(self, klass, interfaces)
6288 && LinkVirtualMethods(self, klass, /*out*/ &default_translations)
6289 && LinkInterfaceMethods(self, klass, default_translations, out_new_conflict, out_imt);
6290 }
6291
6292 // Comparator for name and signature of a method, used in finding overriding methods. Implementation
6293 // avoids the use of handles, if it didn't then rather than compare dex files we could compare dex
6294 // caches in the implementation below.
6295 class MethodNameAndSignatureComparator final : public ValueObject {
6296 public:
6297 explicit MethodNameAndSignatureComparator(ArtMethod* method)
REQUIRES_SHARED(Locks::mutator_lock_)6298 REQUIRES_SHARED(Locks::mutator_lock_) :
6299 dex_file_(method->GetDexFile()), mid_(&dex_file_->GetMethodId(method->GetDexMethodIndex())),
6300 name_(nullptr), name_len_(0) {
6301 DCHECK(!method->IsProxyMethod()) << method->PrettyMethod();
6302 }
6303
GetName()6304 const char* GetName() {
6305 if (name_ == nullptr) {
6306 name_ = dex_file_->StringDataAndUtf16LengthByIdx(mid_->name_idx_, &name_len_);
6307 }
6308 return name_;
6309 }
6310
HasSameNameAndSignature(ArtMethod * other)6311 bool HasSameNameAndSignature(ArtMethod* other)
6312 REQUIRES_SHARED(Locks::mutator_lock_) {
6313 DCHECK(!other->IsProxyMethod()) << other->PrettyMethod();
6314 const DexFile* other_dex_file = other->GetDexFile();
6315 const dex::MethodId& other_mid = other_dex_file->GetMethodId(other->GetDexMethodIndex());
6316 if (dex_file_ == other_dex_file) {
6317 return mid_->name_idx_ == other_mid.name_idx_ && mid_->proto_idx_ == other_mid.proto_idx_;
6318 }
6319 GetName(); // Only used to make sure its calculated.
6320 uint32_t other_name_len;
6321 const char* other_name = other_dex_file->StringDataAndUtf16LengthByIdx(other_mid.name_idx_,
6322 &other_name_len);
6323 if (name_len_ != other_name_len || strcmp(name_, other_name) != 0) {
6324 return false;
6325 }
6326 return dex_file_->GetMethodSignature(*mid_) == other_dex_file->GetMethodSignature(other_mid);
6327 }
6328
6329 private:
6330 // Dex file for the method to compare against.
6331 const DexFile* const dex_file_;
6332 // MethodId for the method to compare against.
6333 const dex::MethodId* const mid_;
6334 // Lazily computed name from the dex file's strings.
6335 const char* name_;
6336 // Lazily computed name length.
6337 uint32_t name_len_;
6338 };
6339
6340 class LinkVirtualHashTable {
6341 public:
LinkVirtualHashTable(Handle<mirror::Class> klass,size_t hash_size,uint32_t * hash_table,PointerSize image_pointer_size)6342 LinkVirtualHashTable(Handle<mirror::Class> klass,
6343 size_t hash_size,
6344 uint32_t* hash_table,
6345 PointerSize image_pointer_size)
6346 : klass_(klass),
6347 hash_size_(hash_size),
6348 hash_table_(hash_table),
6349 image_pointer_size_(image_pointer_size) {
6350 std::fill(hash_table_, hash_table_ + hash_size_, invalid_index_);
6351 }
6352
Add(uint32_t virtual_method_index)6353 void Add(uint32_t virtual_method_index) REQUIRES_SHARED(Locks::mutator_lock_) {
6354 ArtMethod* local_method = klass_->GetVirtualMethodDuringLinking(
6355 virtual_method_index, image_pointer_size_);
6356 const char* name = local_method->GetInterfaceMethodIfProxy(image_pointer_size_)->GetName();
6357 uint32_t hash = ComputeModifiedUtf8Hash(name);
6358 uint32_t index = hash % hash_size_;
6359 // Linear probe until we have an empty slot.
6360 while (hash_table_[index] != invalid_index_) {
6361 if (++index == hash_size_) {
6362 index = 0;
6363 }
6364 }
6365 hash_table_[index] = virtual_method_index;
6366 }
6367
FindAndRemove(MethodNameAndSignatureComparator * comparator)6368 uint32_t FindAndRemove(MethodNameAndSignatureComparator* comparator)
6369 REQUIRES_SHARED(Locks::mutator_lock_) {
6370 const char* name = comparator->GetName();
6371 uint32_t hash = ComputeModifiedUtf8Hash(name);
6372 size_t index = hash % hash_size_;
6373 while (true) {
6374 const uint32_t value = hash_table_[index];
6375 // Since linear probe makes continuous blocks, hitting an invalid index means we are done
6376 // the block and can safely assume not found.
6377 if (value == invalid_index_) {
6378 break;
6379 }
6380 if (value != removed_index_) { // This signifies not already overriden.
6381 ArtMethod* virtual_method =
6382 klass_->GetVirtualMethodDuringLinking(value, image_pointer_size_);
6383 if (comparator->HasSameNameAndSignature(
6384 virtual_method->GetInterfaceMethodIfProxy(image_pointer_size_))) {
6385 hash_table_[index] = removed_index_;
6386 return value;
6387 }
6388 }
6389 if (++index == hash_size_) {
6390 index = 0;
6391 }
6392 }
6393 return GetNotFoundIndex();
6394 }
6395
GetNotFoundIndex()6396 static uint32_t GetNotFoundIndex() {
6397 return invalid_index_;
6398 }
6399
6400 private:
6401 static const uint32_t invalid_index_;
6402 static const uint32_t removed_index_;
6403
6404 Handle<mirror::Class> klass_;
6405 const size_t hash_size_;
6406 uint32_t* const hash_table_;
6407 const PointerSize image_pointer_size_;
6408 };
6409
6410 const uint32_t LinkVirtualHashTable::invalid_index_ = std::numeric_limits<uint32_t>::max();
6411 const uint32_t LinkVirtualHashTable::removed_index_ = std::numeric_limits<uint32_t>::max() - 1;
6412
LinkVirtualMethods(Thread * self,Handle<mirror::Class> klass,std::unordered_map<size_t,ClassLinker::MethodTranslation> * default_translations)6413 bool ClassLinker::LinkVirtualMethods(
6414 Thread* self,
6415 Handle<mirror::Class> klass,
6416 /*out*/std::unordered_map<size_t, ClassLinker::MethodTranslation>* default_translations) {
6417 const size_t num_virtual_methods = klass->NumVirtualMethods();
6418 if (klass->IsInterface()) {
6419 // No vtable.
6420 if (!IsUint<16>(num_virtual_methods)) {
6421 ThrowClassFormatError(klass.Get(), "Too many methods on interface: %zu", num_virtual_methods);
6422 return false;
6423 }
6424 bool has_defaults = false;
6425 // Assign each method an IMT index and set the default flag.
6426 for (size_t i = 0; i < num_virtual_methods; ++i) {
6427 ArtMethod* m = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_);
6428 m->SetMethodIndex(i);
6429 if (!m->IsAbstract()) {
6430 // If the dex file does not support default methods, throw ClassFormatError.
6431 // This check is necessary to protect from odd cases, such as native default
6432 // methods, that the dex file verifier permits for old dex file versions. b/157170505
6433 // FIXME: This should be `if (!m->GetDexFile()->SupportsDefaultMethods())` but we're
6434 // currently running CTS tests for default methods with dex file version 035 which
6435 // does not support default methods. So, we limit this to native methods. b/157718952
6436 if (m->IsNative()) {
6437 DCHECK(!m->GetDexFile()->SupportsDefaultMethods());
6438 ThrowClassFormatError(klass.Get(),
6439 "Dex file does not support default method '%s'",
6440 m->PrettyMethod().c_str());
6441 return false;
6442 }
6443 m->SetAccessFlags(m->GetAccessFlags() | kAccDefault);
6444 has_defaults = true;
6445 }
6446 }
6447 // Mark that we have default methods so that we won't need to scan the virtual_methods_ array
6448 // during initialization. This is a performance optimization. We could simply traverse the
6449 // virtual_methods_ array again during initialization.
6450 if (has_defaults) {
6451 klass->SetHasDefaultMethods();
6452 }
6453 return true;
6454 } else if (klass->HasSuperClass()) {
6455 const size_t super_vtable_length = klass->GetSuperClass()->GetVTableLength();
6456 const size_t max_count = num_virtual_methods + super_vtable_length;
6457 StackHandleScope<3> hs(self);
6458 Handle<mirror::Class> super_class(hs.NewHandle(klass->GetSuperClass()));
6459 MutableHandle<mirror::PointerArray> vtable;
6460 if (super_class->ShouldHaveEmbeddedVTable()) {
6461 vtable = hs.NewHandle(AllocPointerArray(self, max_count));
6462 if (UNLIKELY(vtable == nullptr)) {
6463 self->AssertPendingOOMException();
6464 return false;
6465 }
6466 for (size_t i = 0; i < super_vtable_length; i++) {
6467 vtable->SetElementPtrSize(
6468 i, super_class->GetEmbeddedVTableEntry(i, image_pointer_size_), image_pointer_size_);
6469 }
6470 // We might need to change vtable if we have new virtual methods or new interfaces (since that
6471 // might give us new default methods). If no new interfaces then we can skip the rest since
6472 // the class cannot override any of the super-class's methods. This is required for
6473 // correctness since without it we might not update overridden default method vtable entries
6474 // correctly.
6475 if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) {
6476 klass->SetVTable(vtable.Get());
6477 return true;
6478 }
6479 } else {
6480 DCHECK(super_class->IsAbstract() && !super_class->IsArrayClass());
6481 Handle<mirror::PointerArray> super_vtable = hs.NewHandle(super_class->GetVTable());
6482 CHECK(super_vtable != nullptr) << super_class->PrettyClass();
6483 // We might need to change vtable if we have new virtual methods or new interfaces (since that
6484 // might give us new default methods). See comment above.
6485 if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) {
6486 klass->SetVTable(super_vtable.Get());
6487 return true;
6488 }
6489 vtable = hs.NewHandle(ObjPtr<mirror::PointerArray>::DownCast(
6490 mirror::Array::CopyOf(super_vtable, self, max_count)));
6491 if (UNLIKELY(vtable == nullptr)) {
6492 self->AssertPendingOOMException();
6493 return false;
6494 }
6495 }
6496 // How the algorithm works:
6497 // 1. Populate hash table by adding num_virtual_methods from klass. The values in the hash
6498 // table are: invalid_index for unused slots, index super_vtable_length + i for a virtual
6499 // method which has not been matched to a vtable method, and j if the virtual method at the
6500 // index overrode the super virtual method at index j.
6501 // 2. Loop through super virtual methods, if they overwrite, update hash table to j
6502 // (j < super_vtable_length) to avoid redundant checks. (TODO maybe use this info for reducing
6503 // the need for the initial vtable which we later shrink back down).
6504 // 3. Add non overridden methods to the end of the vtable.
6505 static constexpr size_t kMaxStackHash = 250;
6506 // + 1 so that even if we only have new default methods we will still be able to use this hash
6507 // table (i.e. it will never have 0 size).
6508 const size_t hash_table_size = num_virtual_methods * 3 + 1;
6509 uint32_t* hash_table_ptr;
6510 std::unique_ptr<uint32_t[]> hash_heap_storage;
6511 if (hash_table_size <= kMaxStackHash) {
6512 hash_table_ptr = reinterpret_cast<uint32_t*>(
6513 alloca(hash_table_size * sizeof(*hash_table_ptr)));
6514 } else {
6515 hash_heap_storage.reset(new uint32_t[hash_table_size]);
6516 hash_table_ptr = hash_heap_storage.get();
6517 }
6518 LinkVirtualHashTable hash_table(klass, hash_table_size, hash_table_ptr, image_pointer_size_);
6519 // Add virtual methods to the hash table.
6520 for (size_t i = 0; i < num_virtual_methods; ++i) {
6521 DCHECK(klass->GetVirtualMethodDuringLinking(
6522 i, image_pointer_size_)->GetDeclaringClass() != nullptr);
6523 hash_table.Add(i);
6524 }
6525 // Loop through each super vtable method and see if they are overridden by a method we added to
6526 // the hash table.
6527 for (size_t j = 0; j < super_vtable_length; ++j) {
6528 // Search the hash table to see if we are overridden by any method.
6529 ArtMethod* super_method = vtable->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_);
6530 if (!klass->CanAccessMember(super_method->GetDeclaringClass(),
6531 super_method->GetAccessFlags())) {
6532 // Continue on to the next method since this one is package private and canot be overridden.
6533 // Before Android 4.1, the package-private method super_method might have been incorrectly
6534 // overridden.
6535 continue;
6536 }
6537 MethodNameAndSignatureComparator super_method_name_comparator(
6538 super_method->GetInterfaceMethodIfProxy(image_pointer_size_));
6539 // We remove the method so that subsequent lookups will be faster by making the hash-map
6540 // smaller as we go on.
6541 uint32_t hash_index = hash_table.FindAndRemove(&super_method_name_comparator);
6542 if (hash_index != hash_table.GetNotFoundIndex()) {
6543 ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(
6544 hash_index, image_pointer_size_);
6545 if (super_method->IsFinal()) {
6546 ThrowLinkageError(klass.Get(), "Method %s overrides final method in class %s",
6547 virtual_method->PrettyMethod().c_str(),
6548 super_method->GetDeclaringClassDescriptor());
6549 return false;
6550 }
6551 vtable->SetElementPtrSize(j, virtual_method, image_pointer_size_);
6552 virtual_method->SetMethodIndex(j);
6553 } else if (super_method->IsOverridableByDefaultMethod()) {
6554 // We didn't directly override this method but we might through default methods...
6555 // Check for default method update.
6556 ArtMethod* default_method = nullptr;
6557 switch (FindDefaultMethodImplementation(self,
6558 super_method,
6559 klass,
6560 /*out*/&default_method)) {
6561 case DefaultMethodSearchResult::kDefaultConflict: {
6562 // A conflict was found looking for default methods. Note this (assuming it wasn't
6563 // pre-existing) in the translations map.
6564 if (UNLIKELY(!super_method->IsDefaultConflicting())) {
6565 // Don't generate another conflict method to reduce memory use as an optimization.
6566 default_translations->insert(
6567 {j, ClassLinker::MethodTranslation::CreateConflictingMethod()});
6568 }
6569 break;
6570 }
6571 case DefaultMethodSearchResult::kAbstractFound: {
6572 // No conflict but method is abstract.
6573 // We note that this vtable entry must be made abstract.
6574 if (UNLIKELY(!super_method->IsAbstract())) {
6575 default_translations->insert(
6576 {j, ClassLinker::MethodTranslation::CreateAbstractMethod()});
6577 }
6578 break;
6579 }
6580 case DefaultMethodSearchResult::kDefaultFound: {
6581 if (UNLIKELY(super_method->IsDefaultConflicting() ||
6582 default_method->GetDeclaringClass() != super_method->GetDeclaringClass())) {
6583 // Found a default method implementation that is new.
6584 // TODO Refactor this add default methods to virtuals here and not in
6585 // LinkInterfaceMethods maybe.
6586 // The problem is default methods might override previously present
6587 // default-method or miranda-method vtable entries from the superclass.
6588 // Unfortunately we need these to be entries in this class's virtuals. We do not
6589 // give these entries there until LinkInterfaceMethods so we pass this map around
6590 // to let it know which vtable entries need to be updated.
6591 // Make a note that vtable entry j must be updated, store what it needs to be updated
6592 // to. We will allocate a virtual method slot in LinkInterfaceMethods and fix it up
6593 // then.
6594 default_translations->insert(
6595 {j, ClassLinker::MethodTranslation::CreateTranslatedMethod(default_method)});
6596 VLOG(class_linker) << "Method " << super_method->PrettyMethod()
6597 << " overridden by default "
6598 << default_method->PrettyMethod()
6599 << " in " << mirror::Class::PrettyClass(klass.Get());
6600 }
6601 break;
6602 }
6603 }
6604 }
6605 }
6606 size_t actual_count = super_vtable_length;
6607 // Add the non-overridden methods at the end.
6608 for (size_t i = 0; i < num_virtual_methods; ++i) {
6609 ArtMethod* local_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_);
6610 size_t method_idx = local_method->GetMethodIndexDuringLinking();
6611 if (method_idx < super_vtable_length &&
6612 local_method == vtable->GetElementPtrSize<ArtMethod*>(method_idx, image_pointer_size_)) {
6613 continue;
6614 }
6615 vtable->SetElementPtrSize(actual_count, local_method, image_pointer_size_);
6616 local_method->SetMethodIndex(actual_count);
6617 ++actual_count;
6618 }
6619 if (!IsUint<16>(actual_count)) {
6620 ThrowClassFormatError(klass.Get(), "Too many methods defined on class: %zd", actual_count);
6621 return false;
6622 }
6623 // Shrink vtable if possible
6624 CHECK_LE(actual_count, max_count);
6625 if (actual_count < max_count) {
6626 vtable.Assign(ObjPtr<mirror::PointerArray>::DownCast(
6627 mirror::Array::CopyOf(vtable, self, actual_count)));
6628 if (UNLIKELY(vtable == nullptr)) {
6629 self->AssertPendingOOMException();
6630 return false;
6631 }
6632 }
6633 klass->SetVTable(vtable.Get());
6634 } else {
6635 CHECK_EQ(klass.Get(), GetClassRoot<mirror::Object>(this));
6636 if (!IsUint<16>(num_virtual_methods)) {
6637 ThrowClassFormatError(klass.Get(), "Too many methods: %d",
6638 static_cast<int>(num_virtual_methods));
6639 return false;
6640 }
6641 ObjPtr<mirror::PointerArray> vtable = AllocPointerArray(self, num_virtual_methods);
6642 if (UNLIKELY(vtable == nullptr)) {
6643 self->AssertPendingOOMException();
6644 return false;
6645 }
6646 for (size_t i = 0; i < num_virtual_methods; ++i) {
6647 ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_);
6648 vtable->SetElementPtrSize(i, virtual_method, image_pointer_size_);
6649 virtual_method->SetMethodIndex(i & 0xFFFF);
6650 }
6651 klass->SetVTable(vtable);
6652 }
6653 return true;
6654 }
6655
6656 // Determine if the given iface has any subinterface in the given list that declares the method
6657 // specified by 'target'.
6658 //
6659 // Arguments
6660 // - self: The thread we are running on
6661 // - target: A comparator that will match any method that overrides the method we are checking for
6662 // - iftable: The iftable we are searching for an overriding method on.
6663 // - ifstart: The index of the interface we are checking to see if anything overrides
6664 // - iface: The interface we are checking to see if anything overrides.
6665 // - image_pointer_size:
6666 // The image pointer size.
6667 //
6668 // Returns
6669 // - True: There is some method that matches the target comparator defined in an interface that
6670 // is a subtype of iface.
6671 // - False: There is no method that matches the target comparator in any interface that is a subtype
6672 // of iface.
ContainsOverridingMethodOf(Thread * self,MethodNameAndSignatureComparator & target,Handle<mirror::IfTable> iftable,size_t ifstart,Handle<mirror::Class> iface,PointerSize image_pointer_size)6673 static bool ContainsOverridingMethodOf(Thread* self,
6674 MethodNameAndSignatureComparator& target,
6675 Handle<mirror::IfTable> iftable,
6676 size_t ifstart,
6677 Handle<mirror::Class> iface,
6678 PointerSize image_pointer_size)
6679 REQUIRES_SHARED(Locks::mutator_lock_) {
6680 DCHECK(self != nullptr);
6681 DCHECK(iface != nullptr);
6682 DCHECK(iftable != nullptr);
6683 DCHECK_GE(ifstart, 0u);
6684 DCHECK_LT(ifstart, iftable->Count());
6685 DCHECK_EQ(iface.Get(), iftable->GetInterface(ifstart));
6686 DCHECK(iface->IsInterface());
6687
6688 size_t iftable_count = iftable->Count();
6689 StackHandleScope<1> hs(self);
6690 MutableHandle<mirror::Class> current_iface(hs.NewHandle<mirror::Class>(nullptr));
6691 for (size_t k = ifstart + 1; k < iftable_count; k++) {
6692 // Skip ifstart since our current interface obviously cannot override itself.
6693 current_iface.Assign(iftable->GetInterface(k));
6694 // Iterate through every method on this interface. The order does not matter.
6695 for (ArtMethod& current_method : current_iface->GetDeclaredVirtualMethods(image_pointer_size)) {
6696 if (UNLIKELY(target.HasSameNameAndSignature(
6697 current_method.GetInterfaceMethodIfProxy(image_pointer_size)))) {
6698 // Check if the i'th interface is a subtype of this one.
6699 if (iface->IsAssignableFrom(current_iface.Get())) {
6700 return true;
6701 }
6702 break;
6703 }
6704 }
6705 }
6706 return false;
6707 }
6708
6709 // Find the default method implementation for 'interface_method' in 'klass'. Stores it into
6710 // out_default_method and returns kDefaultFound on success. If no default method was found return
6711 // kAbstractFound and store nullptr into out_default_method. If an error occurs (such as a
6712 // default_method conflict) it will return kDefaultConflict.
FindDefaultMethodImplementation(Thread * self,ArtMethod * target_method,Handle<mirror::Class> klass,ArtMethod ** out_default_method) const6713 ClassLinker::DefaultMethodSearchResult ClassLinker::FindDefaultMethodImplementation(
6714 Thread* self,
6715 ArtMethod* target_method,
6716 Handle<mirror::Class> klass,
6717 /*out*/ArtMethod** out_default_method) const {
6718 DCHECK(self != nullptr);
6719 DCHECK(target_method != nullptr);
6720 DCHECK(out_default_method != nullptr);
6721
6722 *out_default_method = nullptr;
6723
6724 // We organize the interface table so that, for interface I any subinterfaces J follow it in the
6725 // table. This lets us walk the table backwards when searching for default methods. The first one
6726 // we encounter is the best candidate since it is the most specific. Once we have found it we keep
6727 // track of it and then continue checking all other interfaces, since we need to throw an error if
6728 // we encounter conflicting default method implementations (one is not a subtype of the other).
6729 //
6730 // The order of unrelated interfaces does not matter and is not defined.
6731 size_t iftable_count = klass->GetIfTableCount();
6732 if (iftable_count == 0) {
6733 // No interfaces. We have already reset out to null so just return kAbstractFound.
6734 return DefaultMethodSearchResult::kAbstractFound;
6735 }
6736
6737 StackHandleScope<3> hs(self);
6738 MutableHandle<mirror::Class> chosen_iface(hs.NewHandle<mirror::Class>(nullptr));
6739 MutableHandle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable()));
6740 MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr));
6741 MethodNameAndSignatureComparator target_name_comparator(
6742 target_method->GetInterfaceMethodIfProxy(image_pointer_size_));
6743 // Iterates over the klass's iftable in reverse
6744 for (size_t k = iftable_count; k != 0; ) {
6745 --k;
6746
6747 DCHECK_LT(k, iftable->Count());
6748
6749 iface.Assign(iftable->GetInterface(k));
6750 // Iterate through every declared method on this interface. The order does not matter.
6751 for (auto& method_iter : iface->GetDeclaredVirtualMethods(image_pointer_size_)) {
6752 ArtMethod* current_method = &method_iter;
6753 // Skip abstract methods and methods with different names.
6754 if (current_method->IsAbstract() ||
6755 !target_name_comparator.HasSameNameAndSignature(
6756 current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) {
6757 continue;
6758 } else if (!current_method->IsPublic()) {
6759 // The verifier should have caught the non-public method for dex version 37. Just warn and
6760 // skip it since this is from before default-methods so we don't really need to care that it
6761 // has code.
6762 LOG(WARNING) << "Interface method " << current_method->PrettyMethod()
6763 << " is not public! "
6764 << "This will be a fatal error in subsequent versions of android. "
6765 << "Continuing anyway.";
6766 }
6767 if (UNLIKELY(chosen_iface != nullptr)) {
6768 // We have multiple default impls of the same method. This is a potential default conflict.
6769 // We need to check if this possibly conflicting method is either a superclass of the chosen
6770 // default implementation or is overridden by a non-default interface method. In either case
6771 // there is no conflict.
6772 if (!iface->IsAssignableFrom(chosen_iface.Get()) &&
6773 !ContainsOverridingMethodOf(self,
6774 target_name_comparator,
6775 iftable,
6776 k,
6777 iface,
6778 image_pointer_size_)) {
6779 VLOG(class_linker) << "Conflicting default method implementations found: "
6780 << current_method->PrettyMethod() << " and "
6781 << ArtMethod::PrettyMethod(*out_default_method) << " in class "
6782 << klass->PrettyClass() << " conflict.";
6783 *out_default_method = nullptr;
6784 return DefaultMethodSearchResult::kDefaultConflict;
6785 } else {
6786 break; // Continue checking at the next interface.
6787 }
6788 } else {
6789 // chosen_iface == null
6790 if (!ContainsOverridingMethodOf(self,
6791 target_name_comparator,
6792 iftable,
6793 k,
6794 iface,
6795 image_pointer_size_)) {
6796 // Don't set this as the chosen interface if something else is overriding it (because that
6797 // other interface would be potentially chosen instead if it was default). If the other
6798 // interface was abstract then we wouldn't select this interface as chosen anyway since
6799 // the abstract method masks it.
6800 *out_default_method = current_method;
6801 chosen_iface.Assign(iface.Get());
6802 // We should now finish traversing the graph to find if we have default methods that
6803 // conflict.
6804 } else {
6805 VLOG(class_linker) << "A default method '" << current_method->PrettyMethod()
6806 << "' was "
6807 << "skipped because it was overridden by an abstract method in a "
6808 << "subinterface on class '" << klass->PrettyClass() << "'";
6809 }
6810 }
6811 break;
6812 }
6813 }
6814 if (*out_default_method != nullptr) {
6815 VLOG(class_linker) << "Default method '" << (*out_default_method)->PrettyMethod()
6816 << "' selected "
6817 << "as the implementation for '" << target_method->PrettyMethod()
6818 << "' in '" << klass->PrettyClass() << "'";
6819 return DefaultMethodSearchResult::kDefaultFound;
6820 } else {
6821 return DefaultMethodSearchResult::kAbstractFound;
6822 }
6823 }
6824
AddMethodToConflictTable(ObjPtr<mirror::Class> klass,ArtMethod * conflict_method,ArtMethod * interface_method,ArtMethod * method,bool force_new_conflict_method)6825 ArtMethod* ClassLinker::AddMethodToConflictTable(ObjPtr<mirror::Class> klass,
6826 ArtMethod* conflict_method,
6827 ArtMethod* interface_method,
6828 ArtMethod* method,
6829 bool force_new_conflict_method) {
6830 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
6831 Runtime* const runtime = Runtime::Current();
6832 LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader());
6833 bool new_entry = conflict_method == runtime->GetImtConflictMethod() || force_new_conflict_method;
6834
6835 // Create a new entry if the existing one is the shared conflict method.
6836 ArtMethod* new_conflict_method = new_entry
6837 ? runtime->CreateImtConflictMethod(linear_alloc)
6838 : conflict_method;
6839
6840 // Allocate a new table. Note that we will leak this table at the next conflict,
6841 // but that's a tradeoff compared to making the table fixed size.
6842 void* data = linear_alloc->Alloc(
6843 Thread::Current(), ImtConflictTable::ComputeSizeWithOneMoreEntry(current_table,
6844 image_pointer_size_));
6845 if (data == nullptr) {
6846 LOG(ERROR) << "Failed to allocate conflict table";
6847 return conflict_method;
6848 }
6849 ImtConflictTable* new_table = new (data) ImtConflictTable(current_table,
6850 interface_method,
6851 method,
6852 image_pointer_size_);
6853
6854 // Do a fence to ensure threads see the data in the table before it is assigned
6855 // to the conflict method.
6856 // Note that there is a race in the presence of multiple threads and we may leak
6857 // memory from the LinearAlloc, but that's a tradeoff compared to using
6858 // atomic operations.
6859 std::atomic_thread_fence(std::memory_order_release);
6860 new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_);
6861 return new_conflict_method;
6862 }
6863
AllocateIfTableMethodArrays(Thread * self,Handle<mirror::Class> klass,Handle<mirror::IfTable> iftable)6864 bool ClassLinker::AllocateIfTableMethodArrays(Thread* self,
6865 Handle<mirror::Class> klass,
6866 Handle<mirror::IfTable> iftable) {
6867 DCHECK(!klass->IsInterface());
6868 const bool has_superclass = klass->HasSuperClass();
6869 const bool extend_super_iftable = has_superclass;
6870 const size_t ifcount = klass->GetIfTableCount();
6871 const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U;
6872 for (size_t i = 0; i < ifcount; ++i) {
6873 size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods();
6874 if (num_methods > 0) {
6875 const bool is_super = i < super_ifcount;
6876 // This is an interface implemented by a super-class. Therefore we can just copy the method
6877 // array from the superclass.
6878 const bool super_interface = is_super && extend_super_iftable;
6879 ObjPtr<mirror::PointerArray> method_array;
6880 if (super_interface) {
6881 ObjPtr<mirror::IfTable> if_table = klass->GetSuperClass()->GetIfTable();
6882 DCHECK(if_table != nullptr);
6883 DCHECK(if_table->GetMethodArray(i) != nullptr);
6884 // If we are working on a super interface, try extending the existing method array.
6885 StackHandleScope<1u> hs(self);
6886 Handle<mirror::PointerArray> old_array = hs.NewHandle(if_table->GetMethodArray(i));
6887 method_array =
6888 ObjPtr<mirror::PointerArray>::DownCast(mirror::Object::Clone(old_array, self));
6889 } else {
6890 method_array = AllocPointerArray(self, num_methods);
6891 }
6892 if (UNLIKELY(method_array == nullptr)) {
6893 self->AssertPendingOOMException();
6894 return false;
6895 }
6896 iftable->SetMethodArray(i, method_array);
6897 }
6898 }
6899 return true;
6900 }
6901
SetIMTRef(ArtMethod * unimplemented_method,ArtMethod * imt_conflict_method,ArtMethod * current_method,bool * new_conflict,ArtMethod ** imt_ref)6902 void ClassLinker::SetIMTRef(ArtMethod* unimplemented_method,
6903 ArtMethod* imt_conflict_method,
6904 ArtMethod* current_method,
6905 /*out*/bool* new_conflict,
6906 /*out*/ArtMethod** imt_ref) {
6907 // Place method in imt if entry is empty, place conflict otherwise.
6908 if (*imt_ref == unimplemented_method) {
6909 *imt_ref = current_method;
6910 } else if (!(*imt_ref)->IsRuntimeMethod()) {
6911 // If we are not a conflict and we have the same signature and name as the imt
6912 // entry, it must be that we overwrote a superclass vtable entry.
6913 // Note that we have checked IsRuntimeMethod, as there may be multiple different
6914 // conflict methods.
6915 MethodNameAndSignatureComparator imt_comparator(
6916 (*imt_ref)->GetInterfaceMethodIfProxy(image_pointer_size_));
6917 if (imt_comparator.HasSameNameAndSignature(
6918 current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) {
6919 *imt_ref = current_method;
6920 } else {
6921 *imt_ref = imt_conflict_method;
6922 *new_conflict = true;
6923 }
6924 } else {
6925 // Place the default conflict method. Note that there may be an existing conflict
6926 // method in the IMT, but it could be one tailored to the super class, with a
6927 // specific ImtConflictTable.
6928 *imt_ref = imt_conflict_method;
6929 *new_conflict = true;
6930 }
6931 }
6932
FillIMTAndConflictTables(ObjPtr<mirror::Class> klass)6933 void ClassLinker::FillIMTAndConflictTables(ObjPtr<mirror::Class> klass) {
6934 DCHECK(klass->ShouldHaveImt()) << klass->PrettyClass();
6935 DCHECK(!klass->IsTemp()) << klass->PrettyClass();
6936 ArtMethod* imt_data[ImTable::kSize];
6937 Runtime* const runtime = Runtime::Current();
6938 ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod();
6939 ArtMethod* const conflict_method = runtime->GetImtConflictMethod();
6940 std::fill_n(imt_data, arraysize(imt_data), unimplemented_method);
6941 if (klass->GetIfTable() != nullptr) {
6942 bool new_conflict = false;
6943 FillIMTFromIfTable(klass->GetIfTable(),
6944 unimplemented_method,
6945 conflict_method,
6946 klass,
6947 /*create_conflict_tables=*/true,
6948 /*ignore_copied_methods=*/false,
6949 &new_conflict,
6950 &imt_data[0]);
6951 }
6952 if (!klass->ShouldHaveImt()) {
6953 return;
6954 }
6955 // Compare the IMT with the super class including the conflict methods. If they are equivalent,
6956 // we can just use the same pointer.
6957 ImTable* imt = nullptr;
6958 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
6959 if (super_class != nullptr && super_class->ShouldHaveImt()) {
6960 ImTable* super_imt = super_class->GetImt(image_pointer_size_);
6961 bool same = true;
6962 for (size_t i = 0; same && i < ImTable::kSize; ++i) {
6963 ArtMethod* method = imt_data[i];
6964 ArtMethod* super_method = super_imt->Get(i, image_pointer_size_);
6965 if (method != super_method) {
6966 bool is_conflict_table = method->IsRuntimeMethod() &&
6967 method != unimplemented_method &&
6968 method != conflict_method;
6969 // Verify conflict contents.
6970 bool super_conflict_table = super_method->IsRuntimeMethod() &&
6971 super_method != unimplemented_method &&
6972 super_method != conflict_method;
6973 if (!is_conflict_table || !super_conflict_table) {
6974 same = false;
6975 } else {
6976 ImtConflictTable* table1 = method->GetImtConflictTable(image_pointer_size_);
6977 ImtConflictTable* table2 = super_method->GetImtConflictTable(image_pointer_size_);
6978 same = same && table1->Equals(table2, image_pointer_size_);
6979 }
6980 }
6981 }
6982 if (same) {
6983 imt = super_imt;
6984 }
6985 }
6986 if (imt == nullptr) {
6987 imt = klass->GetImt(image_pointer_size_);
6988 DCHECK(imt != nullptr);
6989 imt->Populate(imt_data, image_pointer_size_);
6990 } else {
6991 klass->SetImt(imt, image_pointer_size_);
6992 }
6993 }
6994
CreateImtConflictTable(size_t count,LinearAlloc * linear_alloc,PointerSize image_pointer_size)6995 ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count,
6996 LinearAlloc* linear_alloc,
6997 PointerSize image_pointer_size) {
6998 void* data = linear_alloc->Alloc(Thread::Current(),
6999 ImtConflictTable::ComputeSize(count,
7000 image_pointer_size));
7001 return (data != nullptr) ? new (data) ImtConflictTable(count, image_pointer_size) : nullptr;
7002 }
7003
CreateImtConflictTable(size_t count,LinearAlloc * linear_alloc)7004 ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, LinearAlloc* linear_alloc) {
7005 return CreateImtConflictTable(count, linear_alloc, image_pointer_size_);
7006 }
7007
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)7008 void ClassLinker::FillIMTFromIfTable(ObjPtr<mirror::IfTable> if_table,
7009 ArtMethod* unimplemented_method,
7010 ArtMethod* imt_conflict_method,
7011 ObjPtr<mirror::Class> klass,
7012 bool create_conflict_tables,
7013 bool ignore_copied_methods,
7014 /*out*/bool* new_conflict,
7015 /*out*/ArtMethod** imt) {
7016 uint32_t conflict_counts[ImTable::kSize] = {};
7017 for (size_t i = 0, length = if_table->Count(); i < length; ++i) {
7018 ObjPtr<mirror::Class> interface = if_table->GetInterface(i);
7019 const size_t num_virtuals = interface->NumVirtualMethods();
7020 const size_t method_array_count = if_table->GetMethodArrayCount(i);
7021 // Virtual methods can be larger than the if table methods if there are default methods.
7022 DCHECK_GE(num_virtuals, method_array_count);
7023 if (kIsDebugBuild) {
7024 if (klass->IsInterface()) {
7025 DCHECK_EQ(method_array_count, 0u);
7026 } else {
7027 DCHECK_EQ(interface->NumDeclaredVirtualMethods(), method_array_count);
7028 }
7029 }
7030 if (method_array_count == 0) {
7031 continue;
7032 }
7033 ObjPtr<mirror::PointerArray> method_array = if_table->GetMethodArray(i);
7034 for (size_t j = 0; j < method_array_count; ++j) {
7035 ArtMethod* implementation_method =
7036 method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_);
7037 if (ignore_copied_methods && implementation_method->IsCopied()) {
7038 continue;
7039 }
7040 DCHECK(implementation_method != nullptr);
7041 // Miranda methods cannot be used to implement an interface method, but they are safe to put
7042 // in the IMT since their entrypoint is the interface trampoline. If we put any copied methods
7043 // or interface methods in the IMT here they will not create extra conflicts since we compare
7044 // names and signatures in SetIMTRef.
7045 ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_);
7046 const uint32_t imt_index = interface_method->GetImtIndex();
7047
7048 // There is only any conflicts if all of the interface methods for an IMT slot don't have
7049 // the same implementation method, keep track of this to avoid creating a conflict table in
7050 // this case.
7051
7052 // Conflict table size for each IMT slot.
7053 ++conflict_counts[imt_index];
7054
7055 SetIMTRef(unimplemented_method,
7056 imt_conflict_method,
7057 implementation_method,
7058 /*out*/new_conflict,
7059 /*out*/&imt[imt_index]);
7060 }
7061 }
7062
7063 if (create_conflict_tables) {
7064 // Create the conflict tables.
7065 LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader());
7066 for (size_t i = 0; i < ImTable::kSize; ++i) {
7067 size_t conflicts = conflict_counts[i];
7068 if (imt[i] == imt_conflict_method) {
7069 ImtConflictTable* new_table = CreateImtConflictTable(conflicts, linear_alloc);
7070 if (new_table != nullptr) {
7071 ArtMethod* new_conflict_method =
7072 Runtime::Current()->CreateImtConflictMethod(linear_alloc);
7073 new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_);
7074 imt[i] = new_conflict_method;
7075 } else {
7076 LOG(ERROR) << "Failed to allocate conflict table";
7077 imt[i] = imt_conflict_method;
7078 }
7079 } else {
7080 DCHECK_NE(imt[i], imt_conflict_method);
7081 }
7082 }
7083
7084 for (size_t i = 0, length = if_table->Count(); i < length; ++i) {
7085 ObjPtr<mirror::Class> interface = if_table->GetInterface(i);
7086 const size_t method_array_count = if_table->GetMethodArrayCount(i);
7087 // Virtual methods can be larger than the if table methods if there are default methods.
7088 if (method_array_count == 0) {
7089 continue;
7090 }
7091 ObjPtr<mirror::PointerArray> method_array = if_table->GetMethodArray(i);
7092 for (size_t j = 0; j < method_array_count; ++j) {
7093 ArtMethod* implementation_method =
7094 method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_);
7095 if (ignore_copied_methods && implementation_method->IsCopied()) {
7096 continue;
7097 }
7098 DCHECK(implementation_method != nullptr);
7099 ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_);
7100 const uint32_t imt_index = interface_method->GetImtIndex();
7101 if (!imt[imt_index]->IsRuntimeMethod() ||
7102 imt[imt_index] == unimplemented_method ||
7103 imt[imt_index] == imt_conflict_method) {
7104 continue;
7105 }
7106 ImtConflictTable* table = imt[imt_index]->GetImtConflictTable(image_pointer_size_);
7107 const size_t num_entries = table->NumEntries(image_pointer_size_);
7108 table->SetInterfaceMethod(num_entries, image_pointer_size_, interface_method);
7109 table->SetImplementationMethod(num_entries, image_pointer_size_, implementation_method);
7110 }
7111 }
7112 }
7113 }
7114
7115 // Simple helper function that checks that no subtypes of 'val' are contained within the 'classes'
7116 // set.
NotSubinterfaceOfAny(const std::unordered_set<ObjPtr<mirror::Class>,HashObjPtr> & classes,ObjPtr<mirror::Class> val)7117 static bool NotSubinterfaceOfAny(
7118 const std::unordered_set<ObjPtr<mirror::Class>, HashObjPtr>& classes,
7119 ObjPtr<mirror::Class> val)
7120 REQUIRES(Roles::uninterruptible_)
7121 REQUIRES_SHARED(Locks::mutator_lock_) {
7122 DCHECK(val != nullptr);
7123 for (ObjPtr<mirror::Class> c : classes) {
7124 if (val->IsAssignableFrom(c)) {
7125 return false;
7126 }
7127 }
7128 return true;
7129 }
7130
7131 // Fills in and flattens the interface inheritance hierarchy.
7132 //
7133 // By the end of this function all interfaces in the transitive closure of to_process are added to
7134 // the iftable and every interface precedes all of its sub-interfaces in this list.
7135 //
7136 // all I, J: Interface | I <: J implies J precedes I
7137 //
7138 // (note A <: B means that A is a subtype of B)
7139 //
7140 // This returns the total number of items in the iftable. The iftable might be resized down after
7141 // this call.
7142 //
7143 // We order this backwards so that we do not need to reorder superclass interfaces when new
7144 // interfaces are added in subclass's interface tables.
7145 //
7146 // Upon entry into this function iftable is a copy of the superclass's iftable with the first
7147 // super_ifcount entries filled in with the transitive closure of the interfaces of the superclass.
7148 // The other entries are uninitialized. We will fill in the remaining entries in this function. The
7149 // 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)7150 static size_t FillIfTable(ObjPtr<mirror::IfTable> iftable,
7151 size_t super_ifcount,
7152 const std::vector<ObjPtr<mirror::Class>>& to_process)
7153 REQUIRES(Roles::uninterruptible_)
7154 REQUIRES_SHARED(Locks::mutator_lock_) {
7155 // This is the set of all class's already in the iftable. Used to make checking if a class has
7156 // already been added quicker.
7157 std::unordered_set<ObjPtr<mirror::Class>, HashObjPtr> classes_in_iftable;
7158 // The first super_ifcount elements are from the superclass. We note that they are already added.
7159 for (size_t i = 0; i < super_ifcount; i++) {
7160 ObjPtr<mirror::Class> iface = iftable->GetInterface(i);
7161 DCHECK(NotSubinterfaceOfAny(classes_in_iftable, iface)) << "Bad ordering.";
7162 classes_in_iftable.insert(iface);
7163 }
7164 size_t filled_ifcount = super_ifcount;
7165 for (ObjPtr<mirror::Class> interface : to_process) {
7166 // Let us call the first filled_ifcount elements of iftable the current-iface-list.
7167 // At this point in the loop current-iface-list has the invariant that:
7168 // for every pair of interfaces I,J within it:
7169 // if index_of(I) < index_of(J) then I is not a subtype of J
7170
7171 // If we have already seen this element then all of its super-interfaces must already be in the
7172 // current-iface-list so we can skip adding it.
7173 if (!ContainsElement(classes_in_iftable, interface)) {
7174 // We haven't seen this interface so add all of its super-interfaces onto the
7175 // current-iface-list, skipping those already on it.
7176 int32_t ifcount = interface->GetIfTableCount();
7177 for (int32_t j = 0; j < ifcount; j++) {
7178 ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j);
7179 if (!ContainsElement(classes_in_iftable, super_interface)) {
7180 DCHECK(NotSubinterfaceOfAny(classes_in_iftable, super_interface)) << "Bad ordering.";
7181 classes_in_iftable.insert(super_interface);
7182 iftable->SetInterface(filled_ifcount, super_interface);
7183 filled_ifcount++;
7184 }
7185 }
7186 DCHECK(NotSubinterfaceOfAny(classes_in_iftable, interface)) << "Bad ordering";
7187 // Place this interface onto the current-iface-list after all of its super-interfaces.
7188 classes_in_iftable.insert(interface);
7189 iftable->SetInterface(filled_ifcount, interface);
7190 filled_ifcount++;
7191 } else if (kIsDebugBuild) {
7192 // Check all super-interfaces are already in the list.
7193 int32_t ifcount = interface->GetIfTableCount();
7194 for (int32_t j = 0; j < ifcount; j++) {
7195 ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j);
7196 DCHECK(ContainsElement(classes_in_iftable, super_interface))
7197 << "Iftable does not contain " << mirror::Class::PrettyClass(super_interface)
7198 << ", a superinterface of " << interface->PrettyClass();
7199 }
7200 }
7201 }
7202 if (kIsDebugBuild) {
7203 // Check that the iftable is ordered correctly.
7204 for (size_t i = 0; i < filled_ifcount; i++) {
7205 ObjPtr<mirror::Class> if_a = iftable->GetInterface(i);
7206 for (size_t j = i + 1; j < filled_ifcount; j++) {
7207 ObjPtr<mirror::Class> if_b = iftable->GetInterface(j);
7208 // !(if_a <: if_b)
7209 CHECK(!if_b->IsAssignableFrom(if_a))
7210 << "Bad interface order: " << mirror::Class::PrettyClass(if_a) << " (index " << i
7211 << ") extends "
7212 << if_b->PrettyClass() << " (index " << j << ") and so should be after it in the "
7213 << "interface list.";
7214 }
7215 }
7216 }
7217 return filled_ifcount;
7218 }
7219
SetupInterfaceLookupTable(Thread * self,Handle<mirror::Class> klass,Handle<mirror::ObjectArray<mirror::Class>> interfaces)7220 bool ClassLinker::SetupInterfaceLookupTable(Thread* self, Handle<mirror::Class> klass,
7221 Handle<mirror::ObjectArray<mirror::Class>> interfaces) {
7222 StackHandleScope<1> hs(self);
7223 const bool has_superclass = klass->HasSuperClass();
7224 const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U;
7225 const bool have_interfaces = interfaces != nullptr;
7226 const size_t num_interfaces =
7227 have_interfaces ? interfaces->GetLength() : klass->NumDirectInterfaces();
7228 if (num_interfaces == 0) {
7229 if (super_ifcount == 0) {
7230 if (LIKELY(has_superclass)) {
7231 klass->SetIfTable(klass->GetSuperClass()->GetIfTable());
7232 }
7233 // Class implements no interfaces.
7234 DCHECK_EQ(klass->GetIfTableCount(), 0);
7235 return true;
7236 }
7237 // Class implements same interfaces as parent, are any of these not marker interfaces?
7238 bool has_non_marker_interface = false;
7239 ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable();
7240 for (size_t i = 0; i < super_ifcount; ++i) {
7241 if (super_iftable->GetMethodArrayCount(i) > 0) {
7242 has_non_marker_interface = true;
7243 break;
7244 }
7245 }
7246 // Class just inherits marker interfaces from parent so recycle parent's iftable.
7247 if (!has_non_marker_interface) {
7248 klass->SetIfTable(super_iftable);
7249 return true;
7250 }
7251 }
7252 size_t ifcount = super_ifcount + num_interfaces;
7253 // Check that every class being implemented is an interface.
7254 for (size_t i = 0; i < num_interfaces; i++) {
7255 ObjPtr<mirror::Class> interface = have_interfaces
7256 ? interfaces->GetWithoutChecks(i)
7257 : mirror::Class::GetDirectInterface(self, klass.Get(), i);
7258 DCHECK(interface != nullptr);
7259 if (UNLIKELY(!interface->IsInterface())) {
7260 std::string temp;
7261 ThrowIncompatibleClassChangeError(klass.Get(),
7262 "Class %s implements non-interface class %s",
7263 klass->PrettyDescriptor().c_str(),
7264 PrettyDescriptor(interface->GetDescriptor(&temp)).c_str());
7265 return false;
7266 }
7267 ifcount += interface->GetIfTableCount();
7268 }
7269 // Create the interface function table.
7270 MutableHandle<mirror::IfTable> iftable(hs.NewHandle(AllocIfTable(self, ifcount)));
7271 if (UNLIKELY(iftable == nullptr)) {
7272 self->AssertPendingOOMException();
7273 return false;
7274 }
7275 // Fill in table with superclass's iftable.
7276 if (super_ifcount != 0) {
7277 ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable();
7278 for (size_t i = 0; i < super_ifcount; i++) {
7279 ObjPtr<mirror::Class> super_interface = super_iftable->GetInterface(i);
7280 iftable->SetInterface(i, super_interface);
7281 }
7282 }
7283
7284 // Note that AllowThreadSuspension is to thread suspension as pthread_testcancel is to pthread
7285 // cancellation. That is it will suspend if one has a pending suspend request but otherwise
7286 // doesn't really do anything.
7287 self->AllowThreadSuspension();
7288
7289 size_t new_ifcount;
7290 {
7291 ScopedAssertNoThreadSuspension nts("Copying mirror::Class*'s for FillIfTable");
7292 std::vector<ObjPtr<mirror::Class>> to_add;
7293 for (size_t i = 0; i < num_interfaces; i++) {
7294 ObjPtr<mirror::Class> interface = have_interfaces ? interfaces->Get(i) :
7295 mirror::Class::GetDirectInterface(self, klass.Get(), i);
7296 to_add.push_back(interface);
7297 }
7298
7299 new_ifcount = FillIfTable(iftable.Get(), super_ifcount, std::move(to_add));
7300 }
7301
7302 self->AllowThreadSuspension();
7303
7304 // Shrink iftable in case duplicates were found
7305 if (new_ifcount < ifcount) {
7306 DCHECK_NE(num_interfaces, 0U);
7307 iftable.Assign(ObjPtr<mirror::IfTable>::DownCast(
7308 mirror::IfTable::CopyOf(iftable, self, new_ifcount * mirror::IfTable::kMax)));
7309 if (UNLIKELY(iftable == nullptr)) {
7310 self->AssertPendingOOMException();
7311 return false;
7312 }
7313 ifcount = new_ifcount;
7314 } else {
7315 DCHECK_EQ(new_ifcount, ifcount);
7316 }
7317 klass->SetIfTable(iftable.Get());
7318 return true;
7319 }
7320
7321 // Finds the method with a name/signature that matches cmp in the given lists of methods. The list
7322 // of methods must be unique.
FindSameNameAndSignature(MethodNameAndSignatureComparator & cmp ATTRIBUTE_UNUSED)7323 static ArtMethod* FindSameNameAndSignature(MethodNameAndSignatureComparator& cmp ATTRIBUTE_UNUSED) {
7324 return nullptr;
7325 }
7326
7327 template <typename ... Types>
FindSameNameAndSignature(MethodNameAndSignatureComparator & cmp,const ScopedArenaVector<ArtMethod * > & list,const Types &...rest)7328 static ArtMethod* FindSameNameAndSignature(MethodNameAndSignatureComparator& cmp,
7329 const ScopedArenaVector<ArtMethod*>& list,
7330 const Types& ... rest)
7331 REQUIRES_SHARED(Locks::mutator_lock_) {
7332 for (ArtMethod* method : list) {
7333 if (cmp.HasSameNameAndSignature(method)) {
7334 return method;
7335 }
7336 }
7337 return FindSameNameAndSignature(cmp, rest...);
7338 }
7339
7340 namespace {
7341
7342 // Check that all vtable entries are present in this class's virtuals or are the same as a
7343 // superclasses vtable entry.
CheckClassOwnsVTableEntries(Thread * self,Handle<mirror::Class> klass,PointerSize pointer_size)7344 void CheckClassOwnsVTableEntries(Thread* self,
7345 Handle<mirror::Class> klass,
7346 PointerSize pointer_size)
7347 REQUIRES_SHARED(Locks::mutator_lock_) {
7348 StackHandleScope<2> hs(self);
7349 Handle<mirror::PointerArray> check_vtable(hs.NewHandle(klass->GetVTableDuringLinking()));
7350 ObjPtr<mirror::Class> super_temp = (klass->HasSuperClass()) ? klass->GetSuperClass() : nullptr;
7351 Handle<mirror::Class> superclass(hs.NewHandle(super_temp));
7352 int32_t super_vtable_length = (superclass != nullptr) ? superclass->GetVTableLength() : 0;
7353 for (int32_t i = 0; i < check_vtable->GetLength(); ++i) {
7354 ArtMethod* m = check_vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size);
7355 CHECK(m != nullptr);
7356
7357 if (m->GetMethodIndexDuringLinking() != i) {
7358 LOG(WARNING) << m->PrettyMethod()
7359 << " has an unexpected method index for its spot in the vtable for class"
7360 << klass->PrettyClass();
7361 }
7362 ArraySlice<ArtMethod> virtuals = klass->GetVirtualMethodsSliceUnchecked(pointer_size);
7363 auto is_same_method = [m] (const ArtMethod& meth) {
7364 return &meth == m;
7365 };
7366 if (!((super_vtable_length > i && superclass->GetVTableEntry(i, pointer_size) == m) ||
7367 std::find_if(virtuals.begin(), virtuals.end(), is_same_method) != virtuals.end())) {
7368 LOG(WARNING) << m->PrettyMethod() << " does not seem to be owned by current class "
7369 << klass->PrettyClass() << " or any of its superclasses!";
7370 }
7371 }
7372 }
7373
7374 // Check to make sure the vtable does not have duplicates. Duplicates could cause problems when a
7375 // method is overridden in a subclass.
7376 template <PointerSize kPointerSize>
CheckVTableHasNoDuplicates(Thread * self,Handle<mirror::Class> klass)7377 void CheckVTableHasNoDuplicates(Thread* self, Handle<mirror::Class> klass)
7378 REQUIRES_SHARED(Locks::mutator_lock_) {
7379 StackHandleScope<1> hs(self);
7380 Handle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking()));
7381 int32_t num_entries = vtable->GetLength();
7382
7383 // Observations:
7384 // * The older implementation was O(n^2) and got too expensive for apps with larger classes.
7385 // * Many classes do not override Object functions (e.g., equals/hashCode/toString). Thus,
7386 // for many classes outside of libcore a cross-dexfile check has to be run anyways.
7387 // * In the cross-dexfile case, with the O(n^2), in the best case O(n) cross checks would have
7388 // to be done. It is thus OK in a single-pass algorithm to read all data, anyways.
7389 // * The single-pass algorithm will trade memory for speed, but that is OK.
7390
7391 CHECK_GT(num_entries, 0);
7392
7393 auto log_fn = [&vtable, &klass](int32_t i, int32_t j) REQUIRES_SHARED(Locks::mutator_lock_) {
7394 ArtMethod* m1 = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(i);
7395 ArtMethod* m2 = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(j);
7396 LOG(WARNING) << "vtable entries " << i << " and " << j << " are identical for "
7397 << klass->PrettyClass() << " in method " << m1->PrettyMethod()
7398 << " (0x" << std::hex << reinterpret_cast<uintptr_t>(m2) << ") and "
7399 << m2->PrettyMethod() << " (0x" << std::hex
7400 << reinterpret_cast<uintptr_t>(m2) << ")";
7401 };
7402 struct BaseHashType {
7403 static size_t HashCombine(size_t seed, size_t val) {
7404 return seed ^ (val + 0x9e3779b9 + (seed << 6) + (seed >> 2));
7405 }
7406 };
7407
7408 // Check assuming all entries come from the same dex file.
7409 {
7410 // Find the first interesting method and its dex file.
7411 int32_t start = 0;
7412 for (; start < num_entries; ++start) {
7413 ArtMethod* vtable_entry = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(start);
7414 // Don't bother if we cannot 'see' the vtable entry (i.e. it is a package-private member
7415 // maybe).
7416 if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(),
7417 vtable_entry->GetAccessFlags())) {
7418 continue;
7419 }
7420 break;
7421 }
7422 if (start == num_entries) {
7423 return;
7424 }
7425 const DexFile* dex_file =
7426 vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(start)->
7427 GetInterfaceMethodIfProxy(kPointerSize)->GetDexFile();
7428
7429 // Helper function to avoid logging if we have to run the cross-file checks.
7430 auto check_fn = [&](bool log_warn) REQUIRES_SHARED(Locks::mutator_lock_) {
7431 // Use a map to store seen entries, as the storage space is too large for a bitvector.
7432 using PairType = std::pair<uint32_t, uint16_t>;
7433 struct PairHash : BaseHashType {
7434 size_t operator()(const PairType& key) const {
7435 return BaseHashType::HashCombine(BaseHashType::HashCombine(0, key.first), key.second);
7436 }
7437 };
7438 std::unordered_map<PairType, int32_t, PairHash> seen;
7439 seen.reserve(2 * num_entries);
7440 bool need_slow_path = false;
7441 bool found_dup = false;
7442 for (int i = start; i < num_entries; ++i) {
7443 // Can use Unchecked here as the start loop already ensured that the arrays are correct
7444 // wrt/ kPointerSize.
7445 ArtMethod* vtable_entry = vtable->GetElementPtrSizeUnchecked<ArtMethod*, kPointerSize>(i);
7446 if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(),
7447 vtable_entry->GetAccessFlags())) {
7448 continue;
7449 }
7450 ArtMethod* m = vtable_entry->GetInterfaceMethodIfProxy(kPointerSize);
7451 if (dex_file != m->GetDexFile()) {
7452 need_slow_path = true;
7453 break;
7454 }
7455 const dex::MethodId* m_mid = &dex_file->GetMethodId(m->GetDexMethodIndex());
7456 PairType pair = std::make_pair(m_mid->name_idx_.index_, m_mid->proto_idx_.index_);
7457 auto it = seen.find(pair);
7458 if (it != seen.end()) {
7459 found_dup = true;
7460 if (log_warn) {
7461 log_fn(it->second, i);
7462 }
7463 } else {
7464 seen.emplace(pair, i);
7465 }
7466 }
7467 return std::make_pair(need_slow_path, found_dup);
7468 };
7469 std::pair<bool, bool> result = check_fn(/* log_warn= */ false);
7470 if (!result.first) {
7471 if (result.second) {
7472 check_fn(/* log_warn= */ true);
7473 }
7474 return;
7475 }
7476 }
7477
7478 // Need to check across dex files.
7479 struct Entry {
7480 size_t cached_hash = 0;
7481 const char* name = nullptr;
7482 Signature signature = Signature::NoSignature();
7483 uint32_t name_len = 0;
7484
7485 Entry(const DexFile* dex_file, const dex::MethodId& mid)
7486 : name(dex_file->StringDataAndUtf16LengthByIdx(mid.name_idx_, &name_len)),
7487 signature(dex_file->GetMethodSignature(mid)) {
7488 }
7489
7490 bool operator==(const Entry& other) const {
7491 if (name_len != other.name_len || strcmp(name, other.name) != 0) {
7492 return false;
7493 }
7494 return signature == other.signature;
7495 }
7496 };
7497 struct EntryHash {
7498 size_t operator()(const Entry& key) const {
7499 return key.cached_hash;
7500 }
7501 };
7502 std::unordered_map<Entry, int32_t, EntryHash> map;
7503 for (int32_t i = 0; i < num_entries; ++i) {
7504 // Can use Unchecked here as the first loop already ensured that the arrays are correct
7505 // wrt/ kPointerSize.
7506 ArtMethod* vtable_entry = vtable->GetElementPtrSizeUnchecked<ArtMethod*, kPointerSize>(i);
7507 // Don't bother if we cannot 'see' the vtable entry (i.e. it is a package-private member
7508 // maybe).
7509 if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(),
7510 vtable_entry->GetAccessFlags())) {
7511 continue;
7512 }
7513 ArtMethod* m = vtable_entry->GetInterfaceMethodIfProxy(kPointerSize);
7514 const DexFile* dex_file = m->GetDexFile();
7515 const dex::MethodId& mid = dex_file->GetMethodId(m->GetDexMethodIndex());
7516
7517 Entry e(dex_file, mid);
7518
7519 size_t string_hash = std::hash<std::string_view>()(std::string_view(e.name, e.name_len));
7520 size_t sig_hash = std::hash<std::string>()(e.signature.ToString());
7521 e.cached_hash = BaseHashType::HashCombine(BaseHashType::HashCombine(0u, string_hash),
7522 sig_hash);
7523
7524 auto it = map.find(e);
7525 if (it != map.end()) {
7526 log_fn(it->second, i);
7527 } else {
7528 map.emplace(e, i);
7529 }
7530 }
7531 }
7532
CheckVTableHasNoDuplicates(Thread * self,Handle<mirror::Class> klass,PointerSize pointer_size)7533 void CheckVTableHasNoDuplicates(Thread* self,
7534 Handle<mirror::Class> klass,
7535 PointerSize pointer_size)
7536 REQUIRES_SHARED(Locks::mutator_lock_) {
7537 switch (pointer_size) {
7538 case PointerSize::k64:
7539 CheckVTableHasNoDuplicates<PointerSize::k64>(self, klass);
7540 break;
7541 case PointerSize::k32:
7542 CheckVTableHasNoDuplicates<PointerSize::k32>(self, klass);
7543 break;
7544 }
7545 }
7546
SanityCheckVTable(Thread * self,Handle<mirror::Class> klass,PointerSize pointer_size)7547 static void SanityCheckVTable(Thread* self, Handle<mirror::Class> klass, PointerSize pointer_size)
7548 REQUIRES_SHARED(Locks::mutator_lock_) {
7549 CheckClassOwnsVTableEntries(self, klass, pointer_size);
7550 CheckVTableHasNoDuplicates(self, klass, pointer_size);
7551 }
7552
7553 } // namespace
7554
FillImtFromSuperClass(Handle<mirror::Class> klass,ArtMethod * unimplemented_method,ArtMethod * imt_conflict_method,bool * new_conflict,ArtMethod ** imt)7555 void ClassLinker::FillImtFromSuperClass(Handle<mirror::Class> klass,
7556 ArtMethod* unimplemented_method,
7557 ArtMethod* imt_conflict_method,
7558 bool* new_conflict,
7559 ArtMethod** imt) {
7560 DCHECK(klass->HasSuperClass());
7561 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
7562 if (super_class->ShouldHaveImt()) {
7563 ImTable* super_imt = super_class->GetImt(image_pointer_size_);
7564 for (size_t i = 0; i < ImTable::kSize; ++i) {
7565 imt[i] = super_imt->Get(i, image_pointer_size_);
7566 }
7567 } else {
7568 // No imt in the super class, need to reconstruct from the iftable.
7569 ObjPtr<mirror::IfTable> if_table = super_class->GetIfTable();
7570 if (if_table->Count() != 0) {
7571 // Ignore copied methods since we will handle these in LinkInterfaceMethods.
7572 FillIMTFromIfTable(if_table,
7573 unimplemented_method,
7574 imt_conflict_method,
7575 klass.Get(),
7576 /*create_conflict_tables=*/false,
7577 /*ignore_copied_methods=*/true,
7578 /*out*/new_conflict,
7579 /*out*/imt);
7580 }
7581 }
7582 }
7583
7584 class ClassLinker::LinkInterfaceMethodsHelper {
7585 public:
LinkInterfaceMethodsHelper(ClassLinker * class_linker,Handle<mirror::Class> klass,Thread * self,Runtime * runtime)7586 LinkInterfaceMethodsHelper(ClassLinker* class_linker,
7587 Handle<mirror::Class> klass,
7588 Thread* self,
7589 Runtime* runtime)
7590 : class_linker_(class_linker),
7591 klass_(klass),
7592 method_alignment_(ArtMethod::Alignment(class_linker->GetImagePointerSize())),
7593 method_size_(ArtMethod::Size(class_linker->GetImagePointerSize())),
7594 self_(self),
7595 stack_(runtime->GetLinearAlloc()->GetArenaPool()),
7596 allocator_(&stack_),
7597 default_conflict_methods_(allocator_.Adapter()),
7598 overriding_default_conflict_methods_(allocator_.Adapter()),
7599 miranda_methods_(allocator_.Adapter()),
7600 default_methods_(allocator_.Adapter()),
7601 overriding_default_methods_(allocator_.Adapter()),
7602 move_table_(allocator_.Adapter()) {
7603 }
7604
7605 ArtMethod* FindMethod(ArtMethod* interface_method,
7606 MethodNameAndSignatureComparator& interface_name_comparator,
7607 ArtMethod* vtable_impl)
7608 REQUIRES_SHARED(Locks::mutator_lock_);
7609
7610 ArtMethod* GetOrCreateMirandaMethod(ArtMethod* interface_method,
7611 MethodNameAndSignatureComparator& interface_name_comparator)
7612 REQUIRES_SHARED(Locks::mutator_lock_);
7613
HasNewVirtuals() const7614 bool HasNewVirtuals() const {
7615 return !(miranda_methods_.empty() &&
7616 default_methods_.empty() &&
7617 overriding_default_methods_.empty() &&
7618 overriding_default_conflict_methods_.empty() &&
7619 default_conflict_methods_.empty());
7620 }
7621
7622 void ReallocMethods() REQUIRES_SHARED(Locks::mutator_lock_);
7623
7624 ObjPtr<mirror::PointerArray> UpdateVtable(
7625 const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations,
7626 Handle<mirror::PointerArray> old_vtable) REQUIRES_SHARED(Locks::mutator_lock_);
7627
7628 void UpdateIfTable(Handle<mirror::IfTable> iftable) REQUIRES_SHARED(Locks::mutator_lock_);
7629
7630 void UpdateIMT(ArtMethod** out_imt);
7631
CheckNoStaleMethodsInDexCache()7632 void CheckNoStaleMethodsInDexCache() REQUIRES_SHARED(Locks::mutator_lock_) {
7633 if (kIsDebugBuild) {
7634 PointerSize pointer_size = class_linker_->GetImagePointerSize();
7635 // Check that there are no stale methods are in the dex cache array.
7636 auto* resolved_methods = klass_->GetDexCache()->GetResolvedMethods();
7637 for (size_t i = 0, count = klass_->GetDexCache()->NumResolvedMethods(); i < count; ++i) {
7638 auto pair = mirror::DexCache::GetNativePairPtrSize(resolved_methods, i, pointer_size);
7639 ArtMethod* m = pair.object;
7640 CHECK(move_table_.find(m) == move_table_.end() ||
7641 // The original versions of copied methods will still be present so allow those too.
7642 // Note that if the first check passes this might fail to GetDeclaringClass().
7643 std::find_if(m->GetDeclaringClass()->GetMethods(pointer_size).begin(),
7644 m->GetDeclaringClass()->GetMethods(pointer_size).end(),
7645 [m] (ArtMethod& meth) {
7646 return &meth == m;
7647 }) != m->GetDeclaringClass()->GetMethods(pointer_size).end())
7648 << "Obsolete method " << m->PrettyMethod() << " is in dex cache!";
7649 }
7650 }
7651 }
7652
ClobberOldMethods(LengthPrefixedArray<ArtMethod> * old_methods,LengthPrefixedArray<ArtMethod> * methods)7653 void ClobberOldMethods(LengthPrefixedArray<ArtMethod>* old_methods,
7654 LengthPrefixedArray<ArtMethod>* methods) {
7655 if (kIsDebugBuild) {
7656 CHECK(methods != nullptr);
7657 // Put some random garbage in old methods to help find stale pointers.
7658 if (methods != old_methods && old_methods != nullptr) {
7659 // Need to make sure the GC is not running since it could be scanning the methods we are
7660 // about to overwrite.
7661 ScopedThreadStateChange tsc(self_, kSuspended);
7662 gc::ScopedGCCriticalSection gcs(self_,
7663 gc::kGcCauseClassLinker,
7664 gc::kCollectorTypeClassLinker);
7665 const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_methods->size(),
7666 method_size_,
7667 method_alignment_);
7668 memset(old_methods, 0xFEu, old_size);
7669 }
7670 }
7671 }
7672
7673 private:
NumberOfNewVirtuals() const7674 size_t NumberOfNewVirtuals() const {
7675 return miranda_methods_.size() +
7676 default_methods_.size() +
7677 overriding_default_conflict_methods_.size() +
7678 overriding_default_methods_.size() +
7679 default_conflict_methods_.size();
7680 }
7681
FillTables()7682 bool FillTables() REQUIRES_SHARED(Locks::mutator_lock_) {
7683 return !klass_->IsInterface();
7684 }
7685
LogNewVirtuals() const7686 void LogNewVirtuals() const REQUIRES_SHARED(Locks::mutator_lock_) {
7687 DCHECK(!klass_->IsInterface() || (default_methods_.empty() && miranda_methods_.empty()))
7688 << "Interfaces should only have default-conflict methods appended to them.";
7689 VLOG(class_linker) << mirror::Class::PrettyClass(klass_.Get()) << ": miranda_methods="
7690 << miranda_methods_.size()
7691 << " default_methods=" << default_methods_.size()
7692 << " overriding_default_methods=" << overriding_default_methods_.size()
7693 << " default_conflict_methods=" << default_conflict_methods_.size()
7694 << " overriding_default_conflict_methods="
7695 << overriding_default_conflict_methods_.size();
7696 }
7697
7698 ClassLinker* class_linker_;
7699 Handle<mirror::Class> klass_;
7700 size_t method_alignment_;
7701 size_t method_size_;
7702 Thread* const self_;
7703
7704 // These are allocated on the heap to begin, we then transfer to linear alloc when we re-create
7705 // the virtual methods array.
7706 // Need to use low 4GB arenas for compiler or else the pointers wont fit in 32 bit method array
7707 // during cross compilation.
7708 // Use the linear alloc pool since this one is in the low 4gb for the compiler.
7709 ArenaStack stack_;
7710 ScopedArenaAllocator allocator_;
7711
7712 ScopedArenaVector<ArtMethod*> default_conflict_methods_;
7713 ScopedArenaVector<ArtMethod*> overriding_default_conflict_methods_;
7714 ScopedArenaVector<ArtMethod*> miranda_methods_;
7715 ScopedArenaVector<ArtMethod*> default_methods_;
7716 ScopedArenaVector<ArtMethod*> overriding_default_methods_;
7717
7718 ScopedArenaUnorderedMap<ArtMethod*, ArtMethod*> move_table_;
7719 };
7720
FindMethod(ArtMethod * interface_method,MethodNameAndSignatureComparator & interface_name_comparator,ArtMethod * vtable_impl)7721 ArtMethod* ClassLinker::LinkInterfaceMethodsHelper::FindMethod(
7722 ArtMethod* interface_method,
7723 MethodNameAndSignatureComparator& interface_name_comparator,
7724 ArtMethod* vtable_impl) {
7725 ArtMethod* current_method = nullptr;
7726 switch (class_linker_->FindDefaultMethodImplementation(self_,
7727 interface_method,
7728 klass_,
7729 /*out*/¤t_method)) {
7730 case DefaultMethodSearchResult::kDefaultConflict: {
7731 // Default method conflict.
7732 DCHECK(current_method == nullptr);
7733 ArtMethod* default_conflict_method = nullptr;
7734 if (vtable_impl != nullptr && vtable_impl->IsDefaultConflicting()) {
7735 // We can reuse the method from the superclass, don't bother adding it to virtuals.
7736 default_conflict_method = vtable_impl;
7737 } else {
7738 // See if we already have a conflict method for this method.
7739 ArtMethod* preexisting_conflict = FindSameNameAndSignature(
7740 interface_name_comparator,
7741 default_conflict_methods_,
7742 overriding_default_conflict_methods_);
7743 if (LIKELY(preexisting_conflict != nullptr)) {
7744 // We already have another conflict we can reuse.
7745 default_conflict_method = preexisting_conflict;
7746 } else {
7747 // Note that we do this even if we are an interface since we need to create this and
7748 // cannot reuse another classes.
7749 // Create a new conflict method for this to use.
7750 default_conflict_method = reinterpret_cast<ArtMethod*>(allocator_.Alloc(method_size_));
7751 new(default_conflict_method) ArtMethod(interface_method,
7752 class_linker_->GetImagePointerSize());
7753 if (vtable_impl == nullptr) {
7754 // Save the conflict method. We need to add it to the vtable.
7755 default_conflict_methods_.push_back(default_conflict_method);
7756 } else {
7757 // Save the conflict method but it is already in the vtable.
7758 overriding_default_conflict_methods_.push_back(default_conflict_method);
7759 }
7760 }
7761 }
7762 current_method = default_conflict_method;
7763 break;
7764 } // case kDefaultConflict
7765 case DefaultMethodSearchResult::kDefaultFound: {
7766 DCHECK(current_method != nullptr);
7767 // Found a default method.
7768 if (vtable_impl != nullptr &&
7769 current_method->GetDeclaringClass() == vtable_impl->GetDeclaringClass()) {
7770 // We found a default method but it was the same one we already have from our
7771 // superclass. Don't bother adding it to our vtable again.
7772 current_method = vtable_impl;
7773 } else if (LIKELY(FillTables())) {
7774 // Interfaces don't need to copy default methods since they don't have vtables.
7775 // Only record this default method if it is new to save space.
7776 // TODO It might be worthwhile to copy default methods on interfaces anyway since it
7777 // would make lookup for interface super much faster. (We would only need to scan
7778 // the iftable to find if there is a NSME or AME.)
7779 ArtMethod* old = FindSameNameAndSignature(interface_name_comparator,
7780 default_methods_,
7781 overriding_default_methods_);
7782 if (old == nullptr) {
7783 // We found a default method implementation and there were no conflicts.
7784 if (vtable_impl == nullptr) {
7785 // Save the default method. We need to add it to the vtable.
7786 default_methods_.push_back(current_method);
7787 } else {
7788 // Save the default method but it is already in the vtable.
7789 overriding_default_methods_.push_back(current_method);
7790 }
7791 } else {
7792 CHECK(old == current_method) << "Multiple default implementations selected!";
7793 }
7794 }
7795 break;
7796 } // case kDefaultFound
7797 case DefaultMethodSearchResult::kAbstractFound: {
7798 DCHECK(current_method == nullptr);
7799 // Abstract method masks all defaults.
7800 if (vtable_impl != nullptr &&
7801 vtable_impl->IsAbstract() &&
7802 !vtable_impl->IsDefaultConflicting()) {
7803 // We need to make this an abstract method but the version in the vtable already is so
7804 // don't do anything.
7805 current_method = vtable_impl;
7806 }
7807 break;
7808 } // case kAbstractFound
7809 }
7810 return current_method;
7811 }
7812
GetOrCreateMirandaMethod(ArtMethod * interface_method,MethodNameAndSignatureComparator & interface_name_comparator)7813 ArtMethod* ClassLinker::LinkInterfaceMethodsHelper::GetOrCreateMirandaMethod(
7814 ArtMethod* interface_method,
7815 MethodNameAndSignatureComparator& interface_name_comparator) {
7816 // Find out if there is already a miranda method we can use.
7817 ArtMethod* miranda_method = FindSameNameAndSignature(interface_name_comparator,
7818 miranda_methods_);
7819 if (miranda_method == nullptr) {
7820 DCHECK(interface_method->IsAbstract()) << interface_method->PrettyMethod();
7821 miranda_method = reinterpret_cast<ArtMethod*>(allocator_.Alloc(method_size_));
7822 CHECK(miranda_method != nullptr);
7823 // Point the interface table at a phantom slot.
7824 new(miranda_method) ArtMethod(interface_method, class_linker_->GetImagePointerSize());
7825 miranda_methods_.push_back(miranda_method);
7826 }
7827 return miranda_method;
7828 }
7829
ReallocMethods()7830 void ClassLinker::LinkInterfaceMethodsHelper::ReallocMethods() {
7831 LogNewVirtuals();
7832
7833 const size_t old_method_count = klass_->NumMethods();
7834 const size_t new_method_count = old_method_count + NumberOfNewVirtuals();
7835 DCHECK_NE(old_method_count, new_method_count);
7836
7837 // Attempt to realloc to save RAM if possible.
7838 LengthPrefixedArray<ArtMethod>* old_methods = klass_->GetMethodsPtr();
7839 // The Realloced virtual methods aren't visible from the class roots, so there is no issue
7840 // where GCs could attempt to mark stale pointers due to memcpy. And since we overwrite the
7841 // realloced memory with out->CopyFrom, we are guaranteed to have objects in the to space since
7842 // CopyFrom has internal read barriers.
7843 //
7844 // TODO We should maybe move some of this into mirror::Class or at least into another method.
7845 const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_method_count,
7846 method_size_,
7847 method_alignment_);
7848 const size_t new_size = LengthPrefixedArray<ArtMethod>::ComputeSize(new_method_count,
7849 method_size_,
7850 method_alignment_);
7851 const size_t old_methods_ptr_size = (old_methods != nullptr) ? old_size : 0;
7852 auto* methods = reinterpret_cast<LengthPrefixedArray<ArtMethod>*>(
7853 class_linker_->GetAllocatorForClassLoader(klass_->GetClassLoader())->Realloc(
7854 self_, old_methods, old_methods_ptr_size, new_size));
7855 CHECK(methods != nullptr); // Native allocation failure aborts.
7856
7857 PointerSize pointer_size = class_linker_->GetImagePointerSize();
7858 if (methods != old_methods) {
7859 // Maps from heap allocated miranda method to linear alloc miranda method.
7860 StrideIterator<ArtMethod> out = methods->begin(method_size_, method_alignment_);
7861 // Copy over the old methods.
7862 for (auto& m : klass_->GetMethods(pointer_size)) {
7863 move_table_.emplace(&m, &*out);
7864 // The CopyFrom is only necessary to not miss read barriers since Realloc won't do read
7865 // barriers when it copies.
7866 out->CopyFrom(&m, pointer_size);
7867 ++out;
7868 }
7869 }
7870 StrideIterator<ArtMethod> out(methods->begin(method_size_, method_alignment_) + old_method_count);
7871 // Copy over miranda methods before copying vtable since CopyOf may cause thread suspension and
7872 // we want the roots of the miranda methods to get visited.
7873 for (size_t i = 0; i < miranda_methods_.size(); ++i) {
7874 ArtMethod* mir_method = miranda_methods_[i];
7875 ArtMethod& new_method = *out;
7876 new_method.CopyFrom(mir_method, pointer_size);
7877 new_method.SetAccessFlags(new_method.GetAccessFlags() | kAccMiranda | kAccCopied);
7878 DCHECK_NE(new_method.GetAccessFlags() & kAccAbstract, 0u)
7879 << "Miranda method should be abstract!";
7880 move_table_.emplace(mir_method, &new_method);
7881 // Update the entry in the method array, as the array will be used for future lookups,
7882 // where thread suspension is allowed.
7883 // As such, the array should not contain locally allocated ArtMethod, otherwise the GC
7884 // would not see them.
7885 miranda_methods_[i] = &new_method;
7886 ++out;
7887 }
7888 // We need to copy the default methods into our own method table since the runtime requires that
7889 // every method on a class's vtable be in that respective class's virtual method table.
7890 // NOTE This means that two classes might have the same implementation of a method from the same
7891 // interface but will have different ArtMethod*s for them. This also means we cannot compare a
7892 // default method found on a class with one found on the declaring interface directly and must
7893 // look at the declaring class to determine if they are the same.
7894 for (ScopedArenaVector<ArtMethod*>* methods_vec : {&default_methods_,
7895 &overriding_default_methods_}) {
7896 for (size_t i = 0; i < methods_vec->size(); ++i) {
7897 ArtMethod* def_method = (*methods_vec)[i];
7898 ArtMethod& new_method = *out;
7899 new_method.CopyFrom(def_method, pointer_size);
7900 // Clear the kAccSkipAccessChecks flag if it is present. Since this class hasn't been
7901 // verified yet it shouldn't have methods that are skipping access checks.
7902 // TODO This is rather arbitrary. We should maybe support classes where only some of its
7903 // methods are skip_access_checks.
7904 DCHECK_EQ(new_method.GetAccessFlags() & kAccNative, 0u);
7905 constexpr uint32_t kSetFlags = kAccDefault | kAccCopied;
7906 constexpr uint32_t kMaskFlags = ~kAccSkipAccessChecks;
7907 new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags);
7908 move_table_.emplace(def_method, &new_method);
7909 // Update the entry in the method array, as the array will be used for future lookups,
7910 // where thread suspension is allowed.
7911 // As such, the array should not contain locally allocated ArtMethod, otherwise the GC
7912 // would not see them.
7913 (*methods_vec)[i] = &new_method;
7914 ++out;
7915 }
7916 }
7917 for (ScopedArenaVector<ArtMethod*>* methods_vec : {&default_conflict_methods_,
7918 &overriding_default_conflict_methods_}) {
7919 for (size_t i = 0; i < methods_vec->size(); ++i) {
7920 ArtMethod* conf_method = (*methods_vec)[i];
7921 ArtMethod& new_method = *out;
7922 new_method.CopyFrom(conf_method, pointer_size);
7923 // This is a type of default method (there are default method impls, just a conflict) so
7924 // mark this as a default, non-abstract method, since thats what it is. Also clear the
7925 // kAccSkipAccessChecks bit since this class hasn't been verified yet it shouldn't have
7926 // methods that are skipping access checks.
7927 // Also clear potential kAccSingleImplementation to avoid CHA trying to inline
7928 // the default method.
7929 DCHECK_EQ(new_method.GetAccessFlags() & kAccNative, 0u);
7930 constexpr uint32_t kSetFlags = kAccDefault | kAccDefaultConflict | kAccCopied;
7931 constexpr uint32_t kMaskFlags =
7932 ~(kAccAbstract | kAccSkipAccessChecks | kAccSingleImplementation);
7933 new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags);
7934 DCHECK(new_method.IsDefaultConflicting());
7935 // The actual method might or might not be marked abstract since we just copied it from a
7936 // (possibly default) interface method. We need to set it entry point to be the bridge so
7937 // that the compiler will not invoke the implementation of whatever method we copied from.
7938 EnsureThrowsInvocationError(class_linker_, &new_method);
7939 move_table_.emplace(conf_method, &new_method);
7940 // Update the entry in the method array, as the array will be used for future lookups,
7941 // where thread suspension is allowed.
7942 // As such, the array should not contain locally allocated ArtMethod, otherwise the GC
7943 // would not see them.
7944 (*methods_vec)[i] = &new_method;
7945 ++out;
7946 }
7947 }
7948 methods->SetSize(new_method_count);
7949 class_linker_->UpdateClassMethods(klass_.Get(), methods);
7950 }
7951
UpdateVtable(const std::unordered_map<size_t,ClassLinker::MethodTranslation> & default_translations,Handle<mirror::PointerArray> old_vtable)7952 ObjPtr<mirror::PointerArray> ClassLinker::LinkInterfaceMethodsHelper::UpdateVtable(
7953 const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations,
7954 Handle<mirror::PointerArray> old_vtable) {
7955 // Update the vtable to the new method structures. We can skip this for interfaces since they
7956 // do not have vtables.
7957 const size_t old_vtable_count = old_vtable->GetLength();
7958 const size_t new_vtable_count = old_vtable_count +
7959 miranda_methods_.size() +
7960 default_methods_.size() +
7961 default_conflict_methods_.size();
7962
7963 ObjPtr<mirror::PointerArray> vtable = ObjPtr<mirror::PointerArray>::DownCast(
7964 mirror::Array::CopyOf(old_vtable, self_, new_vtable_count));
7965 if (UNLIKELY(vtable == nullptr)) {
7966 self_->AssertPendingOOMException();
7967 return nullptr;
7968 }
7969
7970 size_t vtable_pos = old_vtable_count;
7971 PointerSize pointer_size = class_linker_->GetImagePointerSize();
7972 // Update all the newly copied method's indexes so they denote their placement in the vtable.
7973 for (const ScopedArenaVector<ArtMethod*>& methods_vec : {default_methods_,
7974 default_conflict_methods_,
7975 miranda_methods_}) {
7976 // These are the functions that are not already in the vtable!
7977 for (ArtMethod* new_vtable_method : methods_vec) {
7978 // Leave the declaring class alone the method's dex_code_item_offset_ and dex_method_index_
7979 // fields are references into the dex file the method was defined in. Since the ArtMethod
7980 // does not store that information it uses declaring_class_->dex_cache_.
7981 new_vtable_method->SetMethodIndex(0xFFFF & vtable_pos);
7982 vtable->SetElementPtrSize(vtable_pos, new_vtable_method, pointer_size);
7983 ++vtable_pos;
7984 }
7985 }
7986 DCHECK_EQ(vtable_pos, new_vtable_count);
7987
7988 // Update old vtable methods. We use the default_translations map to figure out what each
7989 // vtable entry should be updated to, if they need to be at all.
7990 for (size_t i = 0; i < old_vtable_count; ++i) {
7991 ArtMethod* translated_method = vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size);
7992 // Try and find what we need to change this method to.
7993 auto translation_it = default_translations.find(i);
7994 if (translation_it != default_translations.end()) {
7995 if (translation_it->second.IsInConflict()) {
7996 // Find which conflict method we are to use for this method.
7997 MethodNameAndSignatureComparator old_method_comparator(
7998 translated_method->GetInterfaceMethodIfProxy(pointer_size));
7999 // We only need to look through overriding_default_conflict_methods since this is an
8000 // overridden method we are fixing up here.
8001 ArtMethod* new_conflict_method = FindSameNameAndSignature(
8002 old_method_comparator, overriding_default_conflict_methods_);
8003 CHECK(new_conflict_method != nullptr) << "Expected a conflict method!";
8004 translated_method = new_conflict_method;
8005 } else if (translation_it->second.IsAbstract()) {
8006 // Find which miranda method we are to use for this method.
8007 MethodNameAndSignatureComparator old_method_comparator(
8008 translated_method->GetInterfaceMethodIfProxy(pointer_size));
8009 ArtMethod* miranda_method = FindSameNameAndSignature(old_method_comparator,
8010 miranda_methods_);
8011 DCHECK(miranda_method != nullptr);
8012 translated_method = miranda_method;
8013 } else {
8014 // Normal default method (changed from an older default or abstract interface method).
8015 DCHECK(translation_it->second.IsTranslation());
8016 translated_method = translation_it->second.GetTranslation();
8017 auto it = move_table_.find(translated_method);
8018 DCHECK(it != move_table_.end());
8019 translated_method = it->second;
8020 }
8021 } else {
8022 auto it = move_table_.find(translated_method);
8023 translated_method = (it != move_table_.end()) ? it->second : nullptr;
8024 }
8025
8026 if (translated_method != nullptr) {
8027 // Make sure the new_methods index is set.
8028 if (translated_method->GetMethodIndexDuringLinking() != i) {
8029 if (kIsDebugBuild) {
8030 auto* methods = klass_->GetMethodsPtr();
8031 CHECK_LE(reinterpret_cast<uintptr_t>(&*methods->begin(method_size_, method_alignment_)),
8032 reinterpret_cast<uintptr_t>(translated_method));
8033 CHECK_LT(reinterpret_cast<uintptr_t>(translated_method),
8034 reinterpret_cast<uintptr_t>(&*methods->end(method_size_, method_alignment_)));
8035 }
8036 translated_method->SetMethodIndex(0xFFFF & i);
8037 }
8038 vtable->SetElementPtrSize(i, translated_method, pointer_size);
8039 }
8040 }
8041 klass_->SetVTable(vtable);
8042 return vtable;
8043 }
8044
UpdateIfTable(Handle<mirror::IfTable> iftable)8045 void ClassLinker::LinkInterfaceMethodsHelper::UpdateIfTable(Handle<mirror::IfTable> iftable) {
8046 PointerSize pointer_size = class_linker_->GetImagePointerSize();
8047 const size_t ifcount = klass_->GetIfTableCount();
8048 // Go fix up all the stale iftable pointers.
8049 for (size_t i = 0; i < ifcount; ++i) {
8050 for (size_t j = 0, count = iftable->GetMethodArrayCount(i); j < count; ++j) {
8051 ObjPtr<mirror::PointerArray> method_array = iftable->GetMethodArray(i);
8052 ArtMethod* m = method_array->GetElementPtrSize<ArtMethod*>(j, pointer_size);
8053 DCHECK(m != nullptr) << klass_->PrettyClass();
8054 auto it = move_table_.find(m);
8055 if (it != move_table_.end()) {
8056 auto* new_m = it->second;
8057 DCHECK(new_m != nullptr) << klass_->PrettyClass();
8058 method_array->SetElementPtrSize(j, new_m, pointer_size);
8059 }
8060 }
8061 }
8062 }
8063
UpdateIMT(ArtMethod ** out_imt)8064 void ClassLinker::LinkInterfaceMethodsHelper::UpdateIMT(ArtMethod** out_imt) {
8065 // Fix up IMT next.
8066 for (size_t i = 0; i < ImTable::kSize; ++i) {
8067 auto it = move_table_.find(out_imt[i]);
8068 if (it != move_table_.end()) {
8069 out_imt[i] = it->second;
8070 }
8071 }
8072 }
8073
8074 // 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)8075 bool ClassLinker::LinkInterfaceMethods(
8076 Thread* self,
8077 Handle<mirror::Class> klass,
8078 const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations,
8079 bool* out_new_conflict,
8080 ArtMethod** out_imt) {
8081 StackHandleScope<3> hs(self);
8082 Runtime* const runtime = Runtime::Current();
8083
8084 const bool is_interface = klass->IsInterface();
8085 const bool has_superclass = klass->HasSuperClass();
8086 const bool fill_tables = !is_interface;
8087 const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U;
8088 const size_t ifcount = klass->GetIfTableCount();
8089
8090 Handle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable()));
8091
8092 MutableHandle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking()));
8093 ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod();
8094 ArtMethod* const imt_conflict_method = runtime->GetImtConflictMethod();
8095 // Copy the IMT from the super class if possible.
8096 const bool extend_super_iftable = has_superclass;
8097 if (has_superclass && fill_tables) {
8098 FillImtFromSuperClass(klass,
8099 unimplemented_method,
8100 imt_conflict_method,
8101 out_new_conflict,
8102 out_imt);
8103 }
8104 // Allocate method arrays before since we don't want miss visiting miranda method roots due to
8105 // thread suspension.
8106 if (fill_tables) {
8107 if (!AllocateIfTableMethodArrays(self, klass, iftable)) {
8108 return false;
8109 }
8110 }
8111
8112 LinkInterfaceMethodsHelper helper(this, klass, self, runtime);
8113
8114 auto* old_cause = self->StartAssertNoThreadSuspension(
8115 "Copying ArtMethods for LinkInterfaceMethods");
8116 // Going in reverse to ensure that we will hit abstract methods that override defaults before the
8117 // defaults. This means we don't need to do any trickery when creating the Miranda methods, since
8118 // they will already be null. This has the additional benefit that the declarer of a miranda
8119 // method will actually declare an abstract method.
8120 for (size_t i = ifcount; i != 0u; ) {
8121 --i;
8122 DCHECK_LT(i, ifcount);
8123
8124 size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods();
8125 if (num_methods > 0) {
8126 StackHandleScope<2> hs2(self);
8127 const bool is_super = i < super_ifcount;
8128 const bool super_interface = is_super && extend_super_iftable;
8129 // We don't actually create or fill these tables for interfaces, we just copy some methods for
8130 // conflict methods. Just set this as nullptr in those cases.
8131 Handle<mirror::PointerArray> method_array(fill_tables
8132 ? hs2.NewHandle(iftable->GetMethodArray(i))
8133 : hs2.NewHandle<mirror::PointerArray>(nullptr));
8134
8135 ArraySlice<ArtMethod> input_virtual_methods;
8136 ScopedNullHandle<mirror::PointerArray> null_handle;
8137 Handle<mirror::PointerArray> input_vtable_array(null_handle);
8138 int32_t input_array_length = 0;
8139
8140 // TODO Cleanup Needed: In the presence of default methods this optimization is rather dirty
8141 // and confusing. Default methods should always look through all the superclasses
8142 // because they are the last choice of an implementation. We get around this by looking
8143 // at the super-classes iftable methods (copied into method_array previously) when we are
8144 // looking for the implementation of a super-interface method but that is rather dirty.
8145 bool using_virtuals;
8146 if (super_interface || is_interface) {
8147 // If we are overwriting a super class interface, try to only virtual methods instead of the
8148 // whole vtable.
8149 using_virtuals = true;
8150 input_virtual_methods = klass->GetDeclaredVirtualMethodsSlice(image_pointer_size_);
8151 input_array_length = input_virtual_methods.size();
8152 } else {
8153 // For a new interface, however, we need the whole vtable in case a new
8154 // interface method is implemented in the whole superclass.
8155 using_virtuals = false;
8156 DCHECK(vtable != nullptr);
8157 input_vtable_array = vtable;
8158 input_array_length = input_vtable_array->GetLength();
8159 }
8160
8161 // For each method in interface
8162 for (size_t j = 0; j < num_methods; ++j) {
8163 auto* interface_method = iftable->GetInterface(i)->GetVirtualMethod(j, image_pointer_size_);
8164 MethodNameAndSignatureComparator interface_name_comparator(
8165 interface_method->GetInterfaceMethodIfProxy(image_pointer_size_));
8166 uint32_t imt_index = interface_method->GetImtIndex();
8167 ArtMethod** imt_ptr = &out_imt[imt_index];
8168 // For each method listed in the interface's method list, find the
8169 // matching method in our class's method list. We want to favor the
8170 // subclass over the superclass, which just requires walking
8171 // back from the end of the vtable. (This only matters if the
8172 // superclass defines a private method and this class redefines
8173 // it -- otherwise it would use the same vtable slot. In .dex files
8174 // those don't end up in the virtual method table, so it shouldn't
8175 // matter which direction we go. We walk it backward anyway.)
8176 //
8177 // To find defaults we need to do the same but also go over interfaces.
8178 bool found_impl = false;
8179 ArtMethod* vtable_impl = nullptr;
8180 for (int32_t k = input_array_length - 1; k >= 0; --k) {
8181 ArtMethod* vtable_method = using_virtuals ?
8182 &input_virtual_methods[k] :
8183 input_vtable_array->GetElementPtrSize<ArtMethod*>(k, image_pointer_size_);
8184 ArtMethod* vtable_method_for_name_comparison =
8185 vtable_method->GetInterfaceMethodIfProxy(image_pointer_size_);
8186 DCHECK(!vtable_method->IsStatic()) << vtable_method->PrettyMethod();
8187 if (interface_name_comparator.HasSameNameAndSignature(
8188 vtable_method_for_name_comparison)) {
8189 if (!vtable_method->IsAbstract() && !vtable_method->IsPublic()) {
8190 // Must do EndAssertNoThreadSuspension before throw since the throw can cause
8191 // allocations.
8192 self->EndAssertNoThreadSuspension(old_cause);
8193 ThrowIllegalAccessError(klass.Get(),
8194 "Method '%s' implementing interface method '%s' is not public",
8195 vtable_method->PrettyMethod().c_str(),
8196 interface_method->PrettyMethod().c_str());
8197 return false;
8198 } else if (UNLIKELY(vtable_method->IsOverridableByDefaultMethod())) {
8199 // We might have a newer, better, default method for this, so we just skip it. If we
8200 // are still using this we will select it again when scanning for default methods. To
8201 // obviate the need to copy the method again we will make a note that we already found
8202 // a default here.
8203 // TODO This should be much cleaner.
8204 vtable_impl = vtable_method;
8205 break;
8206 } else {
8207 found_impl = true;
8208 if (LIKELY(fill_tables)) {
8209 method_array->SetElementPtrSize(j, vtable_method, image_pointer_size_);
8210 // Place method in imt if entry is empty, place conflict otherwise.
8211 SetIMTRef(unimplemented_method,
8212 imt_conflict_method,
8213 vtable_method,
8214 /*out*/out_new_conflict,
8215 /*out*/imt_ptr);
8216 }
8217 break;
8218 }
8219 }
8220 }
8221 // Continue on to the next method if we are done.
8222 if (LIKELY(found_impl)) {
8223 continue;
8224 } else if (LIKELY(super_interface)) {
8225 // Don't look for a default implementation when the super-method is implemented directly
8226 // by the class.
8227 //
8228 // See if we can use the superclasses method and skip searching everything else.
8229 // Note: !found_impl && super_interface
8230 CHECK(extend_super_iftable);
8231 // If this is a super_interface method it is possible we shouldn't override it because a
8232 // superclass could have implemented it directly. We get the method the superclass used
8233 // to implement this to know if we can override it with a default method. Doing this is
8234 // safe since we know that the super_iftable is filled in so we can simply pull it from
8235 // there. We don't bother if this is not a super-classes interface since in that case we
8236 // have scanned the entire vtable anyway and would have found it.
8237 // TODO This is rather dirty but it is faster than searching through the entire vtable
8238 // every time.
8239 ArtMethod* supers_method =
8240 method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_);
8241 DCHECK(supers_method != nullptr);
8242 DCHECK(interface_name_comparator.HasSameNameAndSignature(supers_method));
8243 if (LIKELY(!supers_method->IsOverridableByDefaultMethod())) {
8244 // The method is not overridable by a default method (i.e. it is directly implemented
8245 // in some class). Therefore move onto the next interface method.
8246 continue;
8247 } else {
8248 // If the super-classes method is override-able by a default method we need to keep
8249 // track of it since though it is override-able it is not guaranteed to be 'overridden'.
8250 // If it turns out not to be overridden and we did not keep track of it we might add it
8251 // to the vtable twice, causing corruption (vtable entries having inconsistent and
8252 // illegal states, incorrect vtable size, and incorrect or inconsistent iftable entries)
8253 // in this class and any subclasses.
8254 DCHECK(vtable_impl == nullptr || vtable_impl == supers_method)
8255 << "vtable_impl was " << ArtMethod::PrettyMethod(vtable_impl)
8256 << " and not 'nullptr' or "
8257 << supers_method->PrettyMethod()
8258 << " as expected. IFTable appears to be corrupt!";
8259 vtable_impl = supers_method;
8260 }
8261 }
8262 // If we haven't found it yet we should search through the interfaces for default methods.
8263 ArtMethod* current_method = helper.FindMethod(interface_method,
8264 interface_name_comparator,
8265 vtable_impl);
8266 if (LIKELY(fill_tables)) {
8267 if (current_method == nullptr && !super_interface) {
8268 // We could not find an implementation for this method and since it is a brand new
8269 // interface we searched the entire vtable (and all default methods) for an
8270 // implementation but couldn't find one. We therefore need to make a miranda method.
8271 current_method = helper.GetOrCreateMirandaMethod(interface_method,
8272 interface_name_comparator);
8273 }
8274
8275 if (current_method != nullptr) {
8276 // We found a default method implementation. Record it in the iftable and IMT.
8277 method_array->SetElementPtrSize(j, current_method, image_pointer_size_);
8278 SetIMTRef(unimplemented_method,
8279 imt_conflict_method,
8280 current_method,
8281 /*out*/out_new_conflict,
8282 /*out*/imt_ptr);
8283 }
8284 }
8285 } // For each method in interface end.
8286 } // if (num_methods > 0)
8287 } // For each interface.
8288 // TODO don't extend virtuals of interface unless necessary (when is it?).
8289 if (helper.HasNewVirtuals()) {
8290 LengthPrefixedArray<ArtMethod>* old_methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr;
8291 helper.ReallocMethods(); // No return value to check. Native allocation failure aborts.
8292 LengthPrefixedArray<ArtMethod>* methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr;
8293
8294 // Done copying methods, they are all roots in the class now, so we can end the no thread
8295 // suspension assert.
8296 self->EndAssertNoThreadSuspension(old_cause);
8297
8298 if (fill_tables) {
8299 vtable.Assign(helper.UpdateVtable(default_translations, vtable));
8300 if (UNLIKELY(vtable == nullptr)) {
8301 // The helper has already called self->AssertPendingOOMException();
8302 return false;
8303 }
8304 helper.UpdateIfTable(iftable);
8305 helper.UpdateIMT(out_imt);
8306 }
8307
8308 helper.CheckNoStaleMethodsInDexCache();
8309 helper.ClobberOldMethods(old_methods, methods);
8310 } else {
8311 self->EndAssertNoThreadSuspension(old_cause);
8312 }
8313 if (kIsDebugBuild && !is_interface) {
8314 SanityCheckVTable(self, klass, image_pointer_size_);
8315 }
8316 return true;
8317 }
8318
LinkInstanceFields(Thread * self,Handle<mirror::Class> klass)8319 bool ClassLinker::LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) {
8320 CHECK(klass != nullptr);
8321 return LinkFields(self, klass, false, nullptr);
8322 }
8323
LinkStaticFields(Thread * self,Handle<mirror::Class> klass,size_t * class_size)8324 bool ClassLinker::LinkStaticFields(Thread* self, Handle<mirror::Class> klass, size_t* class_size) {
8325 CHECK(klass != nullptr);
8326 return LinkFields(self, klass, true, class_size);
8327 }
8328
8329 struct LinkFieldsComparator {
REQUIRES_SHAREDart::LinkFieldsComparator8330 LinkFieldsComparator() REQUIRES_SHARED(Locks::mutator_lock_) {
8331 }
8332 // No thread safety analysis as will be called from STL. Checked lock held in constructor.
operator ()art::LinkFieldsComparator8333 bool operator()(ArtField* field1, ArtField* field2)
8334 NO_THREAD_SAFETY_ANALYSIS {
8335 // First come reference fields, then 64-bit, then 32-bit, and then 16-bit, then finally 8-bit.
8336 Primitive::Type type1 = field1->GetTypeAsPrimitiveType();
8337 Primitive::Type type2 = field2->GetTypeAsPrimitiveType();
8338 if (type1 != type2) {
8339 if (type1 == Primitive::kPrimNot) {
8340 // Reference always goes first.
8341 return true;
8342 }
8343 if (type2 == Primitive::kPrimNot) {
8344 // Reference always goes first.
8345 return false;
8346 }
8347 size_t size1 = Primitive::ComponentSize(type1);
8348 size_t size2 = Primitive::ComponentSize(type2);
8349 if (size1 != size2) {
8350 // Larger primitive types go first.
8351 return size1 > size2;
8352 }
8353 // Primitive types differ but sizes match. Arbitrarily order by primitive type.
8354 return type1 < type2;
8355 }
8356 // Same basic group? Then sort by dex field index. This is guaranteed to be sorted
8357 // by name and for equal names by type id index.
8358 // NOTE: This works also for proxies. Their static fields are assigned appropriate indexes.
8359 return field1->GetDexFieldIndex() < field2->GetDexFieldIndex();
8360 }
8361 };
8362
LinkFields(Thread * self,Handle<mirror::Class> klass,bool is_static,size_t * class_size)8363 bool ClassLinker::LinkFields(Thread* self,
8364 Handle<mirror::Class> klass,
8365 bool is_static,
8366 size_t* class_size) {
8367 self->AllowThreadSuspension();
8368 const size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
8369 LengthPrefixedArray<ArtField>* const fields = is_static ? klass->GetSFieldsPtr() :
8370 klass->GetIFieldsPtr();
8371
8372 // Initialize field_offset
8373 MemberOffset field_offset(0);
8374 if (is_static) {
8375 field_offset = klass->GetFirstReferenceStaticFieldOffsetDuringLinking(image_pointer_size_);
8376 } else {
8377 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
8378 if (super_class != nullptr) {
8379 CHECK(super_class->IsResolved())
8380 << klass->PrettyClass() << " " << super_class->PrettyClass();
8381 field_offset = MemberOffset(super_class->GetObjectSize());
8382 }
8383 }
8384
8385 CHECK_EQ(num_fields == 0, fields == nullptr) << klass->PrettyClass();
8386
8387 // we want a relatively stable order so that adding new fields
8388 // minimizes disruption of C++ version such as Class and Method.
8389 //
8390 // The overall sort order order is:
8391 // 1) All object reference fields, sorted alphabetically.
8392 // 2) All java long (64-bit) integer fields, sorted alphabetically.
8393 // 3) All java double (64-bit) floating point fields, sorted alphabetically.
8394 // 4) All java int (32-bit) integer fields, sorted alphabetically.
8395 // 5) All java float (32-bit) floating point fields, sorted alphabetically.
8396 // 6) All java char (16-bit) integer fields, sorted alphabetically.
8397 // 7) All java short (16-bit) integer fields, sorted alphabetically.
8398 // 8) All java boolean (8-bit) integer fields, sorted alphabetically.
8399 // 9) All java byte (8-bit) integer fields, sorted alphabetically.
8400 //
8401 // Once the fields are sorted in this order we will attempt to fill any gaps that might be present
8402 // in the memory layout of the structure. See ShuffleForward for how this is done.
8403 std::deque<ArtField*> grouped_and_sorted_fields;
8404 const char* old_no_suspend_cause = self->StartAssertNoThreadSuspension(
8405 "Naked ArtField references in deque");
8406 for (size_t i = 0; i < num_fields; i++) {
8407 grouped_and_sorted_fields.push_back(&fields->At(i));
8408 }
8409 std::sort(grouped_and_sorted_fields.begin(), grouped_and_sorted_fields.end(),
8410 LinkFieldsComparator());
8411
8412 // References should be at the front.
8413 size_t current_field = 0;
8414 size_t num_reference_fields = 0;
8415 FieldGaps gaps;
8416
8417 for (; current_field < num_fields; current_field++) {
8418 ArtField* field = grouped_and_sorted_fields.front();
8419 Primitive::Type type = field->GetTypeAsPrimitiveType();
8420 bool isPrimitive = type != Primitive::kPrimNot;
8421 if (isPrimitive) {
8422 break; // past last reference, move on to the next phase
8423 }
8424 if (UNLIKELY(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(
8425 field_offset.Uint32Value()))) {
8426 MemberOffset old_offset = field_offset;
8427 field_offset = MemberOffset(RoundUp(field_offset.Uint32Value(), 4));
8428 AddFieldGap(old_offset.Uint32Value(), field_offset.Uint32Value(), &gaps);
8429 }
8430 DCHECK_ALIGNED(field_offset.Uint32Value(), sizeof(mirror::HeapReference<mirror::Object>));
8431 grouped_and_sorted_fields.pop_front();
8432 num_reference_fields++;
8433 field->SetOffset(field_offset);
8434 field_offset = MemberOffset(field_offset.Uint32Value() +
8435 sizeof(mirror::HeapReference<mirror::Object>));
8436 }
8437 // Gaps are stored as a max heap which means that we must shuffle from largest to smallest
8438 // otherwise we could end up with suboptimal gap fills.
8439 ShuffleForward<8>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps);
8440 ShuffleForward<4>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps);
8441 ShuffleForward<2>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps);
8442 ShuffleForward<1>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps);
8443 CHECK(grouped_and_sorted_fields.empty()) << "Missed " << grouped_and_sorted_fields.size() <<
8444 " fields.";
8445 self->EndAssertNoThreadSuspension(old_no_suspend_cause);
8446
8447 // We lie to the GC about the java.lang.ref.Reference.referent field, so it doesn't scan it.
8448 if (!is_static && klass->DescriptorEquals("Ljava/lang/ref/Reference;")) {
8449 // We know there are no non-reference fields in the Reference classes, and we know
8450 // that 'referent' is alphabetically last, so this is easy...
8451 CHECK_EQ(num_reference_fields, num_fields) << klass->PrettyClass();
8452 CHECK_STREQ(fields->At(num_fields - 1).GetName(), "referent")
8453 << klass->PrettyClass();
8454 --num_reference_fields;
8455 }
8456
8457 size_t size = field_offset.Uint32Value();
8458 // Update klass
8459 if (is_static) {
8460 klass->SetNumReferenceStaticFields(num_reference_fields);
8461 *class_size = size;
8462 } else {
8463 klass->SetNumReferenceInstanceFields(num_reference_fields);
8464 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
8465 if (num_reference_fields == 0 || super_class == nullptr) {
8466 // object has one reference field, klass, but we ignore it since we always visit the class.
8467 // super_class is null iff the class is java.lang.Object.
8468 if (super_class == nullptr ||
8469 (super_class->GetClassFlags() & mirror::kClassFlagNoReferenceFields) != 0) {
8470 klass->SetClassFlags(klass->GetClassFlags() | mirror::kClassFlagNoReferenceFields);
8471 }
8472 }
8473 if (kIsDebugBuild) {
8474 DCHECK_EQ(super_class == nullptr, klass->DescriptorEquals("Ljava/lang/Object;"));
8475 size_t total_reference_instance_fields = 0;
8476 ObjPtr<mirror::Class> cur_super = klass.Get();
8477 while (cur_super != nullptr) {
8478 total_reference_instance_fields += cur_super->NumReferenceInstanceFieldsDuringLinking();
8479 cur_super = cur_super->GetSuperClass();
8480 }
8481 if (super_class == nullptr) {
8482 CHECK_EQ(total_reference_instance_fields, 1u) << klass->PrettyDescriptor();
8483 } else {
8484 // Check that there is at least num_reference_fields other than Object.class.
8485 CHECK_GE(total_reference_instance_fields, 1u + num_reference_fields)
8486 << klass->PrettyClass();
8487 }
8488 }
8489 if (!klass->IsVariableSize()) {
8490 std::string temp;
8491 DCHECK_GE(size, sizeof(mirror::Object)) << klass->GetDescriptor(&temp);
8492 size_t previous_size = klass->GetObjectSize();
8493 if (previous_size != 0) {
8494 // Make sure that we didn't originally have an incorrect size.
8495 CHECK_EQ(previous_size, size) << klass->GetDescriptor(&temp);
8496 }
8497 klass->SetObjectSize(size);
8498 }
8499 }
8500
8501 if (kIsDebugBuild) {
8502 // Make sure that the fields array is ordered by name but all reference
8503 // offsets are at the beginning as far as alignment allows.
8504 MemberOffset start_ref_offset = is_static
8505 ? klass->GetFirstReferenceStaticFieldOffsetDuringLinking(image_pointer_size_)
8506 : klass->GetFirstReferenceInstanceFieldOffset();
8507 MemberOffset end_ref_offset(start_ref_offset.Uint32Value() +
8508 num_reference_fields *
8509 sizeof(mirror::HeapReference<mirror::Object>));
8510 MemberOffset current_ref_offset = start_ref_offset;
8511 for (size_t i = 0; i < num_fields; i++) {
8512 ArtField* field = &fields->At(i);
8513 VLOG(class_linker) << "LinkFields: " << (is_static ? "static" : "instance")
8514 << " class=" << klass->PrettyClass() << " field=" << field->PrettyField()
8515 << " offset=" << field->GetOffsetDuringLinking();
8516 if (i != 0) {
8517 ArtField* const prev_field = &fields->At(i - 1);
8518 // NOTE: The field names can be the same. This is not possible in the Java language
8519 // but it's valid Java/dex bytecode and for example proguard can generate such bytecode.
8520 DCHECK_LE(strcmp(prev_field->GetName(), field->GetName()), 0);
8521 }
8522 Primitive::Type type = field->GetTypeAsPrimitiveType();
8523 bool is_primitive = type != Primitive::kPrimNot;
8524 if (klass->DescriptorEquals("Ljava/lang/ref/Reference;") &&
8525 strcmp("referent", field->GetName()) == 0) {
8526 is_primitive = true; // We lied above, so we have to expect a lie here.
8527 }
8528 MemberOffset offset = field->GetOffsetDuringLinking();
8529 if (is_primitive) {
8530 if (offset.Uint32Value() < end_ref_offset.Uint32Value()) {
8531 // Shuffled before references.
8532 size_t type_size = Primitive::ComponentSize(type);
8533 CHECK_LT(type_size, sizeof(mirror::HeapReference<mirror::Object>));
8534 CHECK_LT(offset.Uint32Value(), start_ref_offset.Uint32Value());
8535 CHECK_LE(offset.Uint32Value() + type_size, start_ref_offset.Uint32Value());
8536 CHECK(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(offset.Uint32Value()));
8537 }
8538 } else {
8539 CHECK_EQ(current_ref_offset.Uint32Value(), offset.Uint32Value());
8540 current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() +
8541 sizeof(mirror::HeapReference<mirror::Object>));
8542 }
8543 }
8544 CHECK_EQ(current_ref_offset.Uint32Value(), end_ref_offset.Uint32Value());
8545 }
8546 return true;
8547 }
8548
8549 // Set the bitmap of reference instance field offsets.
CreateReferenceInstanceOffsets(Handle<mirror::Class> klass)8550 void ClassLinker::CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) {
8551 uint32_t reference_offsets = 0;
8552 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
8553 // Leave the reference offsets as 0 for mirror::Object (the class field is handled specially).
8554 if (super_class != nullptr) {
8555 reference_offsets = super_class->GetReferenceInstanceOffsets();
8556 // Compute reference offsets unless our superclass overflowed.
8557 if (reference_offsets != mirror::Class::kClassWalkSuper) {
8558 size_t num_reference_fields = klass->NumReferenceInstanceFieldsDuringLinking();
8559 if (num_reference_fields != 0u) {
8560 // All of the fields that contain object references are guaranteed be grouped in memory
8561 // starting at an appropriately aligned address after super class object data.
8562 uint32_t start_offset = RoundUp(super_class->GetObjectSize(),
8563 sizeof(mirror::HeapReference<mirror::Object>));
8564 uint32_t start_bit = (start_offset - mirror::kObjectHeaderSize) /
8565 sizeof(mirror::HeapReference<mirror::Object>);
8566 if (start_bit + num_reference_fields > 32) {
8567 reference_offsets = mirror::Class::kClassWalkSuper;
8568 } else {
8569 reference_offsets |= (0xffffffffu << start_bit) &
8570 (0xffffffffu >> (32 - (start_bit + num_reference_fields)));
8571 }
8572 }
8573 }
8574 }
8575 klass->SetReferenceInstanceOffsets(reference_offsets);
8576 }
8577
DoResolveString(dex::StringIndex string_idx,ObjPtr<mirror::DexCache> dex_cache)8578 ObjPtr<mirror::String> ClassLinker::DoResolveString(dex::StringIndex string_idx,
8579 ObjPtr<mirror::DexCache> dex_cache) {
8580 StackHandleScope<1> hs(Thread::Current());
8581 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(dex_cache));
8582 return DoResolveString(string_idx, h_dex_cache);
8583 }
8584
DoResolveString(dex::StringIndex string_idx,Handle<mirror::DexCache> dex_cache)8585 ObjPtr<mirror::String> ClassLinker::DoResolveString(dex::StringIndex string_idx,
8586 Handle<mirror::DexCache> dex_cache) {
8587 const DexFile& dex_file = *dex_cache->GetDexFile();
8588 uint32_t utf16_length;
8589 const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length);
8590 ObjPtr<mirror::String> string = intern_table_->InternStrong(utf16_length, utf8_data);
8591 if (string != nullptr) {
8592 dex_cache->SetResolvedString(string_idx, string);
8593 }
8594 return string;
8595 }
8596
DoLookupString(dex::StringIndex string_idx,ObjPtr<mirror::DexCache> dex_cache)8597 ObjPtr<mirror::String> ClassLinker::DoLookupString(dex::StringIndex string_idx,
8598 ObjPtr<mirror::DexCache> dex_cache) {
8599 DCHECK(dex_cache != nullptr);
8600 const DexFile& dex_file = *dex_cache->GetDexFile();
8601 uint32_t utf16_length;
8602 const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length);
8603 ObjPtr<mirror::String> string =
8604 intern_table_->LookupStrong(Thread::Current(), utf16_length, utf8_data);
8605 if (string != nullptr) {
8606 dex_cache->SetResolvedString(string_idx, string);
8607 }
8608 return string;
8609 }
8610
DoLookupResolvedType(dex::TypeIndex type_idx,ObjPtr<mirror::Class> referrer)8611 ObjPtr<mirror::Class> ClassLinker::DoLookupResolvedType(dex::TypeIndex type_idx,
8612 ObjPtr<mirror::Class> referrer) {
8613 return DoLookupResolvedType(type_idx, referrer->GetDexCache(), referrer->GetClassLoader());
8614 }
8615
DoLookupResolvedType(dex::TypeIndex type_idx,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader)8616 ObjPtr<mirror::Class> ClassLinker::DoLookupResolvedType(dex::TypeIndex type_idx,
8617 ObjPtr<mirror::DexCache> dex_cache,
8618 ObjPtr<mirror::ClassLoader> class_loader) {
8619 const DexFile& dex_file = *dex_cache->GetDexFile();
8620 const char* descriptor = dex_file.StringByTypeIdx(type_idx);
8621 DCHECK_NE(*descriptor, '\0') << "descriptor is empty string";
8622 ObjPtr<mirror::Class> type = nullptr;
8623 if (descriptor[1] == '\0') {
8624 // only the descriptors of primitive types should be 1 character long, also avoid class lookup
8625 // for primitive classes that aren't backed by dex files.
8626 type = LookupPrimitiveClass(descriptor[0]);
8627 } else {
8628 Thread* const self = Thread::Current();
8629 DCHECK(self != nullptr);
8630 const size_t hash = ComputeModifiedUtf8Hash(descriptor);
8631 // Find the class in the loaded classes table.
8632 type = LookupClass(self, descriptor, hash, class_loader);
8633 }
8634 if (type != nullptr) {
8635 if (type->IsResolved()) {
8636 dex_cache->SetResolvedType(type_idx, type);
8637 } else {
8638 type = nullptr;
8639 }
8640 }
8641 return type;
8642 }
8643
8644 template <typename RefType>
DoResolveType(dex::TypeIndex type_idx,RefType referrer)8645 ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, RefType referrer) {
8646 StackHandleScope<2> hs(Thread::Current());
8647 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
8648 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader()));
8649 return DoResolveType(type_idx, dex_cache, class_loader);
8650 }
8651
8652 // Instantiate the above.
8653 template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx,
8654 ArtField* referrer);
8655 template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx,
8656 ArtMethod* referrer);
8657 template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx,
8658 ObjPtr<mirror::Class> referrer);
8659
DoResolveType(dex::TypeIndex type_idx,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader)8660 ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx,
8661 Handle<mirror::DexCache> dex_cache,
8662 Handle<mirror::ClassLoader> class_loader) {
8663 Thread* self = Thread::Current();
8664 const char* descriptor = dex_cache->GetDexFile()->StringByTypeIdx(type_idx);
8665 ObjPtr<mirror::Class> resolved = FindClass(self, descriptor, class_loader);
8666 if (resolved != nullptr) {
8667 // TODO: we used to throw here if resolved's class loader was not the
8668 // boot class loader. This was to permit different classes with the
8669 // same name to be loaded simultaneously by different loaders
8670 dex_cache->SetResolvedType(type_idx, resolved);
8671 } else {
8672 CHECK(self->IsExceptionPending())
8673 << "Expected pending exception for failed resolution of: " << descriptor;
8674 // Convert a ClassNotFoundException to a NoClassDefFoundError.
8675 StackHandleScope<1> hs(self);
8676 Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException()));
8677 if (cause->InstanceOf(GetClassRoot(ClassRoot::kJavaLangClassNotFoundException, this))) {
8678 DCHECK(resolved == nullptr); // No Handle needed to preserve resolved.
8679 self->ClearException();
8680 ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor);
8681 self->GetException()->SetCause(cause.Get());
8682 }
8683 }
8684 DCHECK((resolved == nullptr) || resolved->IsResolved())
8685 << resolved->PrettyDescriptor() << " " << resolved->GetStatus();
8686 return resolved;
8687 }
8688
FindResolvedMethod(ObjPtr<mirror::Class> klass,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader,uint32_t method_idx)8689 ArtMethod* ClassLinker::FindResolvedMethod(ObjPtr<mirror::Class> klass,
8690 ObjPtr<mirror::DexCache> dex_cache,
8691 ObjPtr<mirror::ClassLoader> class_loader,
8692 uint32_t method_idx) {
8693 // Search for the method using dex_cache and method_idx. The Class::Find*Method()
8694 // functions can optimize the search if the dex_cache is the same as the DexCache
8695 // of the class, with fall-back to name and signature search otherwise.
8696 ArtMethod* resolved = nullptr;
8697 if (klass->IsInterface()) {
8698 resolved = klass->FindInterfaceMethod(dex_cache, method_idx, image_pointer_size_);
8699 } else {
8700 resolved = klass->FindClassMethod(dex_cache, method_idx, image_pointer_size_);
8701 }
8702 DCHECK(resolved == nullptr || resolved->GetDeclaringClassUnchecked() != nullptr);
8703 if (resolved != nullptr &&
8704 hiddenapi::ShouldDenyAccessToMember(resolved,
8705 hiddenapi::AccessContext(class_loader, dex_cache),
8706 hiddenapi::AccessMethod::kLinking)) {
8707 resolved = nullptr;
8708 }
8709 if (resolved != nullptr) {
8710 // In case of jmvti, the dex file gets verified before being registered, so first
8711 // check if it's registered before checking class tables.
8712 const DexFile& dex_file = *dex_cache->GetDexFile();
8713 DCHECK(!IsDexFileRegistered(Thread::Current(), dex_file) ||
8714 FindClassTable(Thread::Current(), dex_cache) == ClassTableForClassLoader(class_loader))
8715 << "DexFile referrer: " << dex_file.GetLocation()
8716 << " ClassLoader: " << DescribeLoaders(class_loader, "");
8717 // Be a good citizen and update the dex cache to speed subsequent calls.
8718 dex_cache->SetResolvedMethod(method_idx, resolved, image_pointer_size_);
8719 // Disable the following invariant check as the verifier breaks it. b/73760543
8720 // const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
8721 // DCHECK(LookupResolvedType(method_id.class_idx_, dex_cache, class_loader) != nullptr)
8722 // << "Method: " << resolved->PrettyMethod() << ", "
8723 // << "Class: " << klass->PrettyClass() << " (" << klass->GetStatus() << "), "
8724 // << "DexFile referrer: " << dex_file.GetLocation();
8725 }
8726 return resolved;
8727 }
8728
8729 // Returns true if `method` is either null or hidden.
8730 // Does not print any warnings if it is hidden.
CheckNoSuchMethod(ArtMethod * method,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader)8731 static bool CheckNoSuchMethod(ArtMethod* method,
8732 ObjPtr<mirror::DexCache> dex_cache,
8733 ObjPtr<mirror::ClassLoader> class_loader)
8734 REQUIRES_SHARED(Locks::mutator_lock_) {
8735 return method == nullptr ||
8736 hiddenapi::ShouldDenyAccessToMember(method,
8737 hiddenapi::AccessContext(class_loader, dex_cache),
8738 hiddenapi::AccessMethod::kNone); // no warnings
8739 }
8740
FindIncompatibleMethod(ObjPtr<mirror::Class> klass,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader,uint32_t method_idx)8741 ArtMethod* ClassLinker::FindIncompatibleMethod(ObjPtr<mirror::Class> klass,
8742 ObjPtr<mirror::DexCache> dex_cache,
8743 ObjPtr<mirror::ClassLoader> class_loader,
8744 uint32_t method_idx) {
8745 if (klass->IsInterface()) {
8746 ArtMethod* method = klass->FindClassMethod(dex_cache, method_idx, image_pointer_size_);
8747 return CheckNoSuchMethod(method, dex_cache, class_loader) ? nullptr : method;
8748 } else {
8749 // If there was an interface method with the same signature, we would have
8750 // found it in the "copied" methods. Only DCHECK that the interface method
8751 // really does not exist.
8752 if (kIsDebugBuild) {
8753 ArtMethod* method =
8754 klass->FindInterfaceMethod(dex_cache, method_idx, image_pointer_size_);
8755 DCHECK(CheckNoSuchMethod(method, dex_cache, class_loader));
8756 }
8757 return nullptr;
8758 }
8759 }
8760
8761 template <ClassLinker::ResolveMode kResolveMode>
ResolveMethod(uint32_t method_idx,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader,ArtMethod * referrer,InvokeType type)8762 ArtMethod* ClassLinker::ResolveMethod(uint32_t method_idx,
8763 Handle<mirror::DexCache> dex_cache,
8764 Handle<mirror::ClassLoader> class_loader,
8765 ArtMethod* referrer,
8766 InvokeType type) {
8767 DCHECK(dex_cache != nullptr);
8768 DCHECK(referrer == nullptr || !referrer->IsProxyMethod());
8769 // Check for hit in the dex cache.
8770 PointerSize pointer_size = image_pointer_size_;
8771 ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, pointer_size);
8772 Thread::PoisonObjectPointersIfDebug();
8773 DCHECK(resolved == nullptr || !resolved->IsRuntimeMethod());
8774 bool valid_dex_cache_method = resolved != nullptr;
8775 if (kResolveMode == ResolveMode::kNoChecks && valid_dex_cache_method) {
8776 // We have a valid method from the DexCache and no checks to perform.
8777 DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex();
8778 return resolved;
8779 }
8780 const DexFile& dex_file = *dex_cache->GetDexFile();
8781 const dex::MethodId& method_id = dex_file.GetMethodId(method_idx);
8782 ObjPtr<mirror::Class> klass = nullptr;
8783 if (valid_dex_cache_method) {
8784 // We have a valid method from the DexCache but we need to perform ICCE and IAE checks.
8785 DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex();
8786 klass = LookupResolvedType(method_id.class_idx_, dex_cache.Get(), class_loader.Get());
8787 if (UNLIKELY(klass == nullptr)) {
8788 // We normaly should not end up here. However the verifier currently doesn't guarantee
8789 // the invariant of having the klass in the class table. b/73760543
8790 klass = ResolveType(method_id.class_idx_, dex_cache, class_loader);
8791 if (klass == nullptr) {
8792 // This can only happen if the current thread is not allowed to load
8793 // classes.
8794 DCHECK(!Thread::Current()->CanLoadClasses());
8795 DCHECK(Thread::Current()->IsExceptionPending());
8796 return nullptr;
8797 }
8798 }
8799 } else {
8800 // The method was not in the DexCache, resolve the declaring class.
8801 klass = ResolveType(method_id.class_idx_, dex_cache, class_loader);
8802 if (klass == nullptr) {
8803 DCHECK(Thread::Current()->IsExceptionPending());
8804 return nullptr;
8805 }
8806 }
8807
8808 // Check if the invoke type matches the class type.
8809 if (kResolveMode == ResolveMode::kCheckICCEAndIAE &&
8810 CheckInvokeClassMismatch</* kThrow= */ true>(
8811 dex_cache.Get(), type, [klass]() { return klass; })) {
8812 DCHECK(Thread::Current()->IsExceptionPending());
8813 return nullptr;
8814 }
8815
8816 if (!valid_dex_cache_method) {
8817 resolved = FindResolvedMethod(klass, dex_cache.Get(), class_loader.Get(), method_idx);
8818 }
8819
8820 // Note: We can check for IllegalAccessError only if we have a referrer.
8821 if (kResolveMode == ResolveMode::kCheckICCEAndIAE && resolved != nullptr && referrer != nullptr) {
8822 ObjPtr<mirror::Class> methods_class = resolved->GetDeclaringClass();
8823 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
8824 if (!referring_class->CheckResolvedMethodAccess(methods_class,
8825 resolved,
8826 dex_cache.Get(),
8827 method_idx,
8828 type)) {
8829 DCHECK(Thread::Current()->IsExceptionPending());
8830 return nullptr;
8831 }
8832 }
8833
8834 // If we found a method, check for incompatible class changes.
8835 if (LIKELY(resolved != nullptr) &&
8836 LIKELY(kResolveMode == ResolveMode::kNoChecks ||
8837 !resolved->CheckIncompatibleClassChange(type))) {
8838 return resolved;
8839 } else {
8840 // If we had a method, or if we can find one with another lookup type,
8841 // it's an incompatible-class-change error.
8842 if (resolved == nullptr) {
8843 resolved = FindIncompatibleMethod(klass, dex_cache.Get(), class_loader.Get(), method_idx);
8844 }
8845 if (resolved != nullptr) {
8846 ThrowIncompatibleClassChangeError(type, resolved->GetInvokeType(), resolved, referrer);
8847 } else {
8848 // We failed to find the method (using all lookup types), so throw a NoSuchMethodError.
8849 const char* name = dex_file.StringDataByIdx(method_id.name_idx_);
8850 const Signature signature = dex_file.GetMethodSignature(method_id);
8851 ThrowNoSuchMethodError(type, klass, name, signature);
8852 }
8853 Thread::Current()->AssertPendingException();
8854 return nullptr;
8855 }
8856 }
8857
ResolveMethodWithoutInvokeType(uint32_t method_idx,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader)8858 ArtMethod* ClassLinker::ResolveMethodWithoutInvokeType(uint32_t method_idx,
8859 Handle<mirror::DexCache> dex_cache,
8860 Handle<mirror::ClassLoader> class_loader) {
8861 ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, image_pointer_size_);
8862 Thread::PoisonObjectPointersIfDebug();
8863 if (resolved != nullptr) {
8864 DCHECK(!resolved->IsRuntimeMethod());
8865 DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex();
8866 return resolved;
8867 }
8868 // Fail, get the declaring class.
8869 const dex::MethodId& method_id = dex_cache->GetDexFile()->GetMethodId(method_idx);
8870 ObjPtr<mirror::Class> klass = ResolveType(method_id.class_idx_, dex_cache, class_loader);
8871 if (klass == nullptr) {
8872 Thread::Current()->AssertPendingException();
8873 return nullptr;
8874 }
8875 if (klass->IsInterface()) {
8876 resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx, image_pointer_size_);
8877 } else {
8878 resolved = klass->FindClassMethod(dex_cache.Get(), method_idx, image_pointer_size_);
8879 }
8880 if (resolved != nullptr &&
8881 hiddenapi::ShouldDenyAccessToMember(
8882 resolved,
8883 hiddenapi::AccessContext(class_loader.Get(), dex_cache.Get()),
8884 hiddenapi::AccessMethod::kLinking)) {
8885 resolved = nullptr;
8886 }
8887 return resolved;
8888 }
8889
LookupResolvedField(uint32_t field_idx,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader,bool is_static)8890 ArtField* ClassLinker::LookupResolvedField(uint32_t field_idx,
8891 ObjPtr<mirror::DexCache> dex_cache,
8892 ObjPtr<mirror::ClassLoader> class_loader,
8893 bool is_static) {
8894 const DexFile& dex_file = *dex_cache->GetDexFile();
8895 const dex::FieldId& field_id = dex_file.GetFieldId(field_idx);
8896 ObjPtr<mirror::Class> klass = dex_cache->GetResolvedType(field_id.class_idx_);
8897 if (klass == nullptr) {
8898 klass = LookupResolvedType(field_id.class_idx_, dex_cache, class_loader);
8899 }
8900 if (klass == nullptr) {
8901 // The class has not been resolved yet, so the field is also unresolved.
8902 return nullptr;
8903 }
8904 DCHECK(klass->IsResolved());
8905
8906 return FindResolvedField(klass, dex_cache, class_loader, field_idx, is_static);
8907 }
8908
ResolveField(uint32_t field_idx,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader,bool is_static)8909 ArtField* ClassLinker::ResolveField(uint32_t field_idx,
8910 Handle<mirror::DexCache> dex_cache,
8911 Handle<mirror::ClassLoader> class_loader,
8912 bool is_static) {
8913 DCHECK(dex_cache != nullptr);
8914 ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_);
8915 Thread::PoisonObjectPointersIfDebug();
8916 if (resolved != nullptr) {
8917 return resolved;
8918 }
8919 const DexFile& dex_file = *dex_cache->GetDexFile();
8920 const dex::FieldId& field_id = dex_file.GetFieldId(field_idx);
8921 ObjPtr<mirror::Class> klass = ResolveType(field_id.class_idx_, dex_cache, class_loader);
8922 if (klass == nullptr) {
8923 DCHECK(Thread::Current()->IsExceptionPending());
8924 return nullptr;
8925 }
8926
8927 resolved = FindResolvedField(klass, dex_cache.Get(), class_loader.Get(), field_idx, is_static);
8928 if (resolved == nullptr) {
8929 const char* name = dex_file.GetFieldName(field_id);
8930 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
8931 ThrowNoSuchFieldError(is_static ? "static " : "instance ", klass, type, name);
8932 }
8933 return resolved;
8934 }
8935
ResolveFieldJLS(uint32_t field_idx,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader)8936 ArtField* ClassLinker::ResolveFieldJLS(uint32_t field_idx,
8937 Handle<mirror::DexCache> dex_cache,
8938 Handle<mirror::ClassLoader> class_loader) {
8939 DCHECK(dex_cache != nullptr);
8940 ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_);
8941 Thread::PoisonObjectPointersIfDebug();
8942 if (resolved != nullptr) {
8943 return resolved;
8944 }
8945 const DexFile& dex_file = *dex_cache->GetDexFile();
8946 const dex::FieldId& field_id = dex_file.GetFieldId(field_idx);
8947 ObjPtr<mirror::Class> klass = ResolveType(field_id.class_idx_, dex_cache, class_loader);
8948 if (klass == nullptr) {
8949 DCHECK(Thread::Current()->IsExceptionPending());
8950 return nullptr;
8951 }
8952
8953 resolved = FindResolvedFieldJLS(klass, dex_cache.Get(), class_loader.Get(), field_idx);
8954 if (resolved == nullptr) {
8955 const char* name = dex_file.GetFieldName(field_id);
8956 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
8957 ThrowNoSuchFieldError("", klass, type, name);
8958 }
8959 return resolved;
8960 }
8961
FindResolvedField(ObjPtr<mirror::Class> klass,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader,uint32_t field_idx,bool is_static)8962 ArtField* ClassLinker::FindResolvedField(ObjPtr<mirror::Class> klass,
8963 ObjPtr<mirror::DexCache> dex_cache,
8964 ObjPtr<mirror::ClassLoader> class_loader,
8965 uint32_t field_idx,
8966 bool is_static) {
8967 ArtField* resolved = nullptr;
8968 Thread* self = is_static ? Thread::Current() : nullptr;
8969 const DexFile& dex_file = *dex_cache->GetDexFile();
8970
8971 resolved = is_static ? mirror::Class::FindStaticField(self, klass, dex_cache, field_idx)
8972 : klass->FindInstanceField(dex_cache, field_idx);
8973
8974 if (resolved == nullptr) {
8975 const dex::FieldId& field_id = dex_file.GetFieldId(field_idx);
8976 const char* name = dex_file.GetFieldName(field_id);
8977 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
8978 resolved = is_static ? mirror::Class::FindStaticField(self, klass, name, type)
8979 : klass->FindInstanceField(name, type);
8980 }
8981
8982 if (resolved != nullptr &&
8983 hiddenapi::ShouldDenyAccessToMember(resolved,
8984 hiddenapi::AccessContext(class_loader, dex_cache),
8985 hiddenapi::AccessMethod::kLinking)) {
8986 resolved = nullptr;
8987 }
8988
8989 if (resolved != nullptr) {
8990 dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_);
8991 }
8992
8993 return resolved;
8994 }
8995
FindResolvedFieldJLS(ObjPtr<mirror::Class> klass,ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::ClassLoader> class_loader,uint32_t field_idx)8996 ArtField* ClassLinker::FindResolvedFieldJLS(ObjPtr<mirror::Class> klass,
8997 ObjPtr<mirror::DexCache> dex_cache,
8998 ObjPtr<mirror::ClassLoader> class_loader,
8999 uint32_t field_idx) {
9000 ArtField* resolved = nullptr;
9001 Thread* self = Thread::Current();
9002 const DexFile& dex_file = *dex_cache->GetDexFile();
9003 const dex::FieldId& field_id = dex_file.GetFieldId(field_idx);
9004
9005 const char* name = dex_file.GetFieldName(field_id);
9006 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
9007 resolved = mirror::Class::FindField(self, klass, name, type);
9008
9009 if (resolved != nullptr &&
9010 hiddenapi::ShouldDenyAccessToMember(resolved,
9011 hiddenapi::AccessContext(class_loader, dex_cache),
9012 hiddenapi::AccessMethod::kLinking)) {
9013 resolved = nullptr;
9014 }
9015
9016 if (resolved != nullptr) {
9017 dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_);
9018 }
9019
9020 return resolved;
9021 }
9022
ResolveMethodType(Thread * self,dex::ProtoIndex proto_idx,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader)9023 ObjPtr<mirror::MethodType> ClassLinker::ResolveMethodType(
9024 Thread* self,
9025 dex::ProtoIndex proto_idx,
9026 Handle<mirror::DexCache> dex_cache,
9027 Handle<mirror::ClassLoader> class_loader) {
9028 DCHECK(Runtime::Current()->IsMethodHandlesEnabled());
9029 DCHECK(dex_cache != nullptr);
9030
9031 ObjPtr<mirror::MethodType> resolved = dex_cache->GetResolvedMethodType(proto_idx);
9032 if (resolved != nullptr) {
9033 return resolved;
9034 }
9035
9036 StackHandleScope<4> hs(self);
9037
9038 // First resolve the return type.
9039 const DexFile& dex_file = *dex_cache->GetDexFile();
9040 const dex::ProtoId& proto_id = dex_file.GetProtoId(proto_idx);
9041 Handle<mirror::Class> return_type(hs.NewHandle(
9042 ResolveType(proto_id.return_type_idx_, dex_cache, class_loader)));
9043 if (return_type == nullptr) {
9044 DCHECK(self->IsExceptionPending());
9045 return nullptr;
9046 }
9047
9048 // Then resolve the argument types.
9049 //
9050 // TODO: Is there a better way to figure out the number of method arguments
9051 // other than by looking at the shorty ?
9052 const size_t num_method_args = strlen(dex_file.StringDataByIdx(proto_id.shorty_idx_)) - 1;
9053
9054 ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this);
9055 Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle(
9056 mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_method_args)));
9057 if (method_params == nullptr) {
9058 DCHECK(self->IsExceptionPending());
9059 return nullptr;
9060 }
9061
9062 DexFileParameterIterator it(dex_file, proto_id);
9063 int32_t i = 0;
9064 MutableHandle<mirror::Class> param_class = hs.NewHandle<mirror::Class>(nullptr);
9065 for (; it.HasNext(); it.Next()) {
9066 const dex::TypeIndex type_idx = it.GetTypeIdx();
9067 param_class.Assign(ResolveType(type_idx, dex_cache, class_loader));
9068 if (param_class == nullptr) {
9069 DCHECK(self->IsExceptionPending());
9070 return nullptr;
9071 }
9072
9073 method_params->Set(i++, param_class.Get());
9074 }
9075
9076 DCHECK(!it.HasNext());
9077
9078 Handle<mirror::MethodType> type = hs.NewHandle(
9079 mirror::MethodType::Create(self, return_type, method_params));
9080 dex_cache->SetResolvedMethodType(proto_idx, type.Get());
9081
9082 return type.Get();
9083 }
9084
ResolveMethodType(Thread * self,dex::ProtoIndex proto_idx,ArtMethod * referrer)9085 ObjPtr<mirror::MethodType> ClassLinker::ResolveMethodType(Thread* self,
9086 dex::ProtoIndex proto_idx,
9087 ArtMethod* referrer) {
9088 StackHandleScope<2> hs(self);
9089 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
9090 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader()));
9091 return ResolveMethodType(self, proto_idx, dex_cache, class_loader);
9092 }
9093
ResolveMethodHandleForField(Thread * self,const dex::MethodHandleItem & method_handle,ArtMethod * referrer)9094 ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandleForField(
9095 Thread* self,
9096 const dex::MethodHandleItem& method_handle,
9097 ArtMethod* referrer) {
9098 DexFile::MethodHandleType handle_type =
9099 static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_);
9100 mirror::MethodHandle::Kind kind;
9101 bool is_put;
9102 bool is_static;
9103 int32_t num_params;
9104 switch (handle_type) {
9105 case DexFile::MethodHandleType::kStaticPut: {
9106 kind = mirror::MethodHandle::Kind::kStaticPut;
9107 is_put = true;
9108 is_static = true;
9109 num_params = 1;
9110 break;
9111 }
9112 case DexFile::MethodHandleType::kStaticGet: {
9113 kind = mirror::MethodHandle::Kind::kStaticGet;
9114 is_put = false;
9115 is_static = true;
9116 num_params = 0;
9117 break;
9118 }
9119 case DexFile::MethodHandleType::kInstancePut: {
9120 kind = mirror::MethodHandle::Kind::kInstancePut;
9121 is_put = true;
9122 is_static = false;
9123 num_params = 2;
9124 break;
9125 }
9126 case DexFile::MethodHandleType::kInstanceGet: {
9127 kind = mirror::MethodHandle::Kind::kInstanceGet;
9128 is_put = false;
9129 is_static = false;
9130 num_params = 1;
9131 break;
9132 }
9133 case DexFile::MethodHandleType::kInvokeStatic:
9134 case DexFile::MethodHandleType::kInvokeInstance:
9135 case DexFile::MethodHandleType::kInvokeConstructor:
9136 case DexFile::MethodHandleType::kInvokeDirect:
9137 case DexFile::MethodHandleType::kInvokeInterface:
9138 UNREACHABLE();
9139 }
9140
9141 ArtField* target_field =
9142 ResolveField(method_handle.field_or_method_idx_, referrer, is_static);
9143 if (LIKELY(target_field != nullptr)) {
9144 ObjPtr<mirror::Class> target_class = target_field->GetDeclaringClass();
9145 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
9146 if (UNLIKELY(!referring_class->CanAccessMember(target_class, target_field->GetAccessFlags()))) {
9147 ThrowIllegalAccessErrorField(referring_class, target_field);
9148 return nullptr;
9149 }
9150 if (UNLIKELY(is_put && target_field->IsFinal())) {
9151 ThrowIllegalAccessErrorField(referring_class, target_field);
9152 return nullptr;
9153 }
9154 } else {
9155 DCHECK(Thread::Current()->IsExceptionPending());
9156 return nullptr;
9157 }
9158
9159 StackHandleScope<4> hs(self);
9160 ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this);
9161 Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle(
9162 mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params)));
9163 if (UNLIKELY(method_params == nullptr)) {
9164 DCHECK(self->IsExceptionPending());
9165 return nullptr;
9166 }
9167
9168 Handle<mirror::Class> constructor_class;
9169 Handle<mirror::Class> return_type;
9170 switch (handle_type) {
9171 case DexFile::MethodHandleType::kStaticPut: {
9172 method_params->Set(0, target_field->ResolveType());
9173 return_type = hs.NewHandle(GetClassRoot(ClassRoot::kPrimitiveVoid, this));
9174 break;
9175 }
9176 case DexFile::MethodHandleType::kStaticGet: {
9177 return_type = hs.NewHandle(target_field->ResolveType());
9178 break;
9179 }
9180 case DexFile::MethodHandleType::kInstancePut: {
9181 method_params->Set(0, target_field->GetDeclaringClass());
9182 method_params->Set(1, target_field->ResolveType());
9183 return_type = hs.NewHandle(GetClassRoot(ClassRoot::kPrimitiveVoid, this));
9184 break;
9185 }
9186 case DexFile::MethodHandleType::kInstanceGet: {
9187 method_params->Set(0, target_field->GetDeclaringClass());
9188 return_type = hs.NewHandle(target_field->ResolveType());
9189 break;
9190 }
9191 case DexFile::MethodHandleType::kInvokeStatic:
9192 case DexFile::MethodHandleType::kInvokeInstance:
9193 case DexFile::MethodHandleType::kInvokeConstructor:
9194 case DexFile::MethodHandleType::kInvokeDirect:
9195 case DexFile::MethodHandleType::kInvokeInterface:
9196 UNREACHABLE();
9197 }
9198
9199 for (int32_t i = 0; i < num_params; ++i) {
9200 if (UNLIKELY(method_params->Get(i) == nullptr)) {
9201 DCHECK(self->IsExceptionPending());
9202 return nullptr;
9203 }
9204 }
9205
9206 if (UNLIKELY(return_type.IsNull())) {
9207 DCHECK(self->IsExceptionPending());
9208 return nullptr;
9209 }
9210
9211 Handle<mirror::MethodType>
9212 method_type(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params)));
9213 if (UNLIKELY(method_type.IsNull())) {
9214 DCHECK(self->IsExceptionPending());
9215 return nullptr;
9216 }
9217
9218 uintptr_t target = reinterpret_cast<uintptr_t>(target_field);
9219 return mirror::MethodHandleImpl::Create(self, target, kind, method_type);
9220 }
9221
ResolveMethodHandleForMethod(Thread * self,const dex::MethodHandleItem & method_handle,ArtMethod * referrer)9222 ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandleForMethod(
9223 Thread* self,
9224 const dex::MethodHandleItem& method_handle,
9225 ArtMethod* referrer) {
9226 DexFile::MethodHandleType handle_type =
9227 static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_);
9228 mirror::MethodHandle::Kind kind;
9229 uint32_t receiver_count = 0;
9230 ArtMethod* target_method = nullptr;
9231 switch (handle_type) {
9232 case DexFile::MethodHandleType::kStaticPut:
9233 case DexFile::MethodHandleType::kStaticGet:
9234 case DexFile::MethodHandleType::kInstancePut:
9235 case DexFile::MethodHandleType::kInstanceGet:
9236 UNREACHABLE();
9237 case DexFile::MethodHandleType::kInvokeStatic: {
9238 kind = mirror::MethodHandle::Kind::kInvokeStatic;
9239 receiver_count = 0;
9240 target_method = ResolveMethod<ResolveMode::kNoChecks>(self,
9241 method_handle.field_or_method_idx_,
9242 referrer,
9243 InvokeType::kStatic);
9244 break;
9245 }
9246 case DexFile::MethodHandleType::kInvokeInstance: {
9247 kind = mirror::MethodHandle::Kind::kInvokeVirtual;
9248 receiver_count = 1;
9249 target_method = ResolveMethod<ResolveMode::kNoChecks>(self,
9250 method_handle.field_or_method_idx_,
9251 referrer,
9252 InvokeType::kVirtual);
9253 break;
9254 }
9255 case DexFile::MethodHandleType::kInvokeConstructor: {
9256 // Constructors are currently implemented as a transform. They
9257 // are special cased later in this method.
9258 kind = mirror::MethodHandle::Kind::kInvokeTransform;
9259 receiver_count = 0;
9260 target_method = ResolveMethod<ResolveMode::kNoChecks>(self,
9261 method_handle.field_or_method_idx_,
9262 referrer,
9263 InvokeType::kDirect);
9264 break;
9265 }
9266 case DexFile::MethodHandleType::kInvokeDirect: {
9267 kind = mirror::MethodHandle::Kind::kInvokeDirect;
9268 receiver_count = 1;
9269 StackHandleScope<2> hs(self);
9270 // A constant method handle with type kInvokeDirect can refer to
9271 // a method that is private or to a method in a super class. To
9272 // disambiguate the two options, we resolve the method ignoring
9273 // the invocation type to determine if the method is private. We
9274 // then resolve again specifying the intended invocation type to
9275 // force the appropriate checks.
9276 target_method = ResolveMethodWithoutInvokeType(method_handle.field_or_method_idx_,
9277 hs.NewHandle(referrer->GetDexCache()),
9278 hs.NewHandle(referrer->GetClassLoader()));
9279 if (UNLIKELY(target_method == nullptr)) {
9280 break;
9281 }
9282
9283 if (target_method->IsPrivate()) {
9284 kind = mirror::MethodHandle::Kind::kInvokeDirect;
9285 target_method = ResolveMethod<ResolveMode::kNoChecks>(self,
9286 method_handle.field_or_method_idx_,
9287 referrer,
9288 InvokeType::kDirect);
9289 } else {
9290 kind = mirror::MethodHandle::Kind::kInvokeSuper;
9291 target_method = ResolveMethod<ResolveMode::kNoChecks>(self,
9292 method_handle.field_or_method_idx_,
9293 referrer,
9294 InvokeType::kSuper);
9295 if (UNLIKELY(target_method == nullptr)) {
9296 break;
9297 }
9298 // Find the method specified in the parent in referring class
9299 // so invoke-super invokes the method in the parent of the
9300 // referrer.
9301 target_method =
9302 referrer->GetDeclaringClass()->FindVirtualMethodForVirtual(target_method,
9303 kRuntimePointerSize);
9304 }
9305 break;
9306 }
9307 case DexFile::MethodHandleType::kInvokeInterface: {
9308 kind = mirror::MethodHandle::Kind::kInvokeInterface;
9309 receiver_count = 1;
9310 target_method = ResolveMethod<ResolveMode::kNoChecks>(self,
9311 method_handle.field_or_method_idx_,
9312 referrer,
9313 InvokeType::kInterface);
9314 break;
9315 }
9316 }
9317
9318 if (UNLIKELY(target_method == nullptr)) {
9319 DCHECK(Thread::Current()->IsExceptionPending());
9320 return nullptr;
9321 }
9322
9323 ObjPtr<mirror::Class> target_class = target_method->GetDeclaringClass();
9324 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
9325 uint32_t access_flags = target_method->GetAccessFlags();
9326 if (UNLIKELY(!referring_class->CanAccessMember(target_class, access_flags))) {
9327 ThrowIllegalAccessErrorMethod(referring_class, target_method);
9328 return nullptr;
9329 }
9330
9331 // Calculate the number of parameters from the method shorty. We add the
9332 // receiver count (0 or 1) and deduct one for the return value.
9333 uint32_t shorty_length;
9334 target_method->GetShorty(&shorty_length);
9335 int32_t num_params = static_cast<int32_t>(shorty_length + receiver_count - 1);
9336
9337 StackHandleScope<5> hs(self);
9338 ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this);
9339 Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle(
9340 mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params)));
9341 if (method_params.Get() == nullptr) {
9342 DCHECK(self->IsExceptionPending());
9343 return nullptr;
9344 }
9345
9346 const DexFile* dex_file = referrer->GetDexFile();
9347 const dex::MethodId& method_id = dex_file->GetMethodId(method_handle.field_or_method_idx_);
9348 int32_t index = 0;
9349 if (receiver_count != 0) {
9350 // Insert receiver. Use the class identified in the method handle rather than the declaring
9351 // class of the resolved method which may be super class or default interface method
9352 // (b/115964401).
9353 ObjPtr<mirror::Class> receiver_class = LookupResolvedType(method_id.class_idx_, referrer);
9354 // receiver_class should have been resolved when resolving the target method.
9355 DCHECK(receiver_class != nullptr);
9356 method_params->Set(index++, receiver_class);
9357 }
9358
9359 const dex::ProtoId& proto_id = dex_file->GetProtoId(method_id.proto_idx_);
9360 DexFileParameterIterator it(*dex_file, proto_id);
9361 while (it.HasNext()) {
9362 DCHECK_LT(index, num_params);
9363 const dex::TypeIndex type_idx = it.GetTypeIdx();
9364 ObjPtr<mirror::Class> klass = ResolveType(type_idx, referrer);
9365 if (nullptr == klass) {
9366 DCHECK(self->IsExceptionPending());
9367 return nullptr;
9368 }
9369 method_params->Set(index++, klass);
9370 it.Next();
9371 }
9372
9373 Handle<mirror::Class> return_type =
9374 hs.NewHandle(ResolveType(proto_id.return_type_idx_, referrer));
9375 if (UNLIKELY(return_type.IsNull())) {
9376 DCHECK(self->IsExceptionPending());
9377 return nullptr;
9378 }
9379
9380 Handle<mirror::MethodType>
9381 method_type(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params)));
9382 if (UNLIKELY(method_type.IsNull())) {
9383 DCHECK(self->IsExceptionPending());
9384 return nullptr;
9385 }
9386
9387 if (UNLIKELY(handle_type == DexFile::MethodHandleType::kInvokeConstructor)) {
9388 Handle<mirror::Class> constructor_class = hs.NewHandle(target_method->GetDeclaringClass());
9389 Handle<mirror::MethodHandlesLookup> lookup =
9390 hs.NewHandle(mirror::MethodHandlesLookup::GetDefault(self));
9391 return lookup->FindConstructor(self, constructor_class, method_type);
9392 }
9393
9394 uintptr_t target = reinterpret_cast<uintptr_t>(target_method);
9395 return mirror::MethodHandleImpl::Create(self, target, kind, method_type);
9396 }
9397
ResolveMethodHandle(Thread * self,uint32_t method_handle_idx,ArtMethod * referrer)9398 ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandle(Thread* self,
9399 uint32_t method_handle_idx,
9400 ArtMethod* referrer)
9401 REQUIRES_SHARED(Locks::mutator_lock_) {
9402 const DexFile* const dex_file = referrer->GetDexFile();
9403 const dex::MethodHandleItem& method_handle = dex_file->GetMethodHandle(method_handle_idx);
9404 switch (static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_)) {
9405 case DexFile::MethodHandleType::kStaticPut:
9406 case DexFile::MethodHandleType::kStaticGet:
9407 case DexFile::MethodHandleType::kInstancePut:
9408 case DexFile::MethodHandleType::kInstanceGet:
9409 return ResolveMethodHandleForField(self, method_handle, referrer);
9410 case DexFile::MethodHandleType::kInvokeStatic:
9411 case DexFile::MethodHandleType::kInvokeInstance:
9412 case DexFile::MethodHandleType::kInvokeConstructor:
9413 case DexFile::MethodHandleType::kInvokeDirect:
9414 case DexFile::MethodHandleType::kInvokeInterface:
9415 return ResolveMethodHandleForMethod(self, method_handle, referrer);
9416 }
9417 }
9418
IsQuickResolutionStub(const void * entry_point) const9419 bool ClassLinker::IsQuickResolutionStub(const void* entry_point) const {
9420 return (entry_point == GetQuickResolutionStub()) ||
9421 (quick_resolution_trampoline_ == entry_point);
9422 }
9423
IsQuickToInterpreterBridge(const void * entry_point) const9424 bool ClassLinker::IsQuickToInterpreterBridge(const void* entry_point) const {
9425 return (entry_point == GetQuickToInterpreterBridge()) ||
9426 (quick_to_interpreter_bridge_trampoline_ == entry_point);
9427 }
9428
IsQuickGenericJniStub(const void * entry_point) const9429 bool ClassLinker::IsQuickGenericJniStub(const void* entry_point) const {
9430 return (entry_point == GetQuickGenericJniStub()) ||
9431 (quick_generic_jni_trampoline_ == entry_point);
9432 }
9433
IsJniDlsymLookupStub(const void * entry_point) const9434 bool ClassLinker::IsJniDlsymLookupStub(const void* entry_point) const {
9435 return entry_point == GetJniDlsymLookupStub() ||
9436 (jni_dlsym_lookup_trampoline_ == entry_point);
9437 }
9438
IsJniDlsymLookupCriticalStub(const void * entry_point) const9439 bool ClassLinker::IsJniDlsymLookupCriticalStub(const void* entry_point) const {
9440 return entry_point == GetJniDlsymLookupCriticalStub() ||
9441 (jni_dlsym_lookup_critical_trampoline_ == entry_point);
9442 }
9443
GetRuntimeQuickGenericJniStub() const9444 const void* ClassLinker::GetRuntimeQuickGenericJniStub() const {
9445 return GetQuickGenericJniStub();
9446 }
9447
SetEntryPointsToInterpreter(ArtMethod * method) const9448 void ClassLinker::SetEntryPointsToInterpreter(ArtMethod* method) const {
9449 if (!method->IsNative()) {
9450 method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge());
9451 } else {
9452 method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub());
9453 }
9454 }
9455
SetEntryPointsForObsoleteMethod(ArtMethod * method) const9456 void ClassLinker::SetEntryPointsForObsoleteMethod(ArtMethod* method) const {
9457 DCHECK(method->IsObsolete());
9458 // We cannot mess with the entrypoints of native methods because they are used to determine how
9459 // large the method's quick stack frame is. Without this information we cannot walk the stacks.
9460 if (!method->IsNative()) {
9461 method->SetEntryPointFromQuickCompiledCode(GetInvokeObsoleteMethodStub());
9462 }
9463 }
9464
DumpForSigQuit(std::ostream & os)9465 void ClassLinker::DumpForSigQuit(std::ostream& os) {
9466 ScopedObjectAccess soa(Thread::Current());
9467 ReaderMutexLock mu(soa.Self(), *Locks::classlinker_classes_lock_);
9468 os << "Zygote loaded classes=" << NumZygoteClasses() << " post zygote classes="
9469 << NumNonZygoteClasses() << "\n";
9470 ReaderMutexLock mu2(soa.Self(), *Locks::dex_lock_);
9471 os << "Dumping registered class loaders\n";
9472 size_t class_loader_index = 0;
9473 for (const ClassLoaderData& class_loader : class_loaders_) {
9474 ObjPtr<mirror::ClassLoader> loader =
9475 ObjPtr<mirror::ClassLoader>::DownCast(soa.Self()->DecodeJObject(class_loader.weak_root));
9476 if (loader != nullptr) {
9477 os << "#" << class_loader_index++ << " " << loader->GetClass()->PrettyDescriptor() << ": [";
9478 bool saw_one_dex_file = false;
9479 for (const DexCacheData& dex_cache : dex_caches_) {
9480 if (dex_cache.IsValid() && dex_cache.class_table == class_loader.class_table) {
9481 if (saw_one_dex_file) {
9482 os << ":";
9483 }
9484 saw_one_dex_file = true;
9485 os << dex_cache.dex_file->GetLocation();
9486 }
9487 }
9488 os << "]";
9489 bool found_parent = false;
9490 if (loader->GetParent() != nullptr) {
9491 size_t parent_index = 0;
9492 for (const ClassLoaderData& class_loader2 : class_loaders_) {
9493 ObjPtr<mirror::ClassLoader> loader2 = ObjPtr<mirror::ClassLoader>::DownCast(
9494 soa.Self()->DecodeJObject(class_loader2.weak_root));
9495 if (loader2 == loader->GetParent()) {
9496 os << ", parent #" << parent_index;
9497 found_parent = true;
9498 break;
9499 }
9500 parent_index++;
9501 }
9502 if (!found_parent) {
9503 os << ", unregistered parent of type "
9504 << loader->GetParent()->GetClass()->PrettyDescriptor();
9505 }
9506 } else {
9507 os << ", no parent";
9508 }
9509 os << "\n";
9510 }
9511 }
9512 os << "Done dumping class loaders\n";
9513 Runtime* runtime = Runtime::Current();
9514 os << "Classes initialized: " << runtime->GetStat(KIND_GLOBAL_CLASS_INIT_COUNT) << " in "
9515 << PrettyDuration(runtime->GetStat(KIND_GLOBAL_CLASS_INIT_TIME)) << "\n";
9516 }
9517
9518 class CountClassesVisitor : public ClassLoaderVisitor {
9519 public:
CountClassesVisitor()9520 CountClassesVisitor() : num_zygote_classes(0), num_non_zygote_classes(0) {}
9521
Visit(ObjPtr<mirror::ClassLoader> class_loader)9522 void Visit(ObjPtr<mirror::ClassLoader> class_loader)
9523 REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override {
9524 ClassTable* const class_table = class_loader->GetClassTable();
9525 if (class_table != nullptr) {
9526 num_zygote_classes += class_table->NumZygoteClasses(class_loader);
9527 num_non_zygote_classes += class_table->NumNonZygoteClasses(class_loader);
9528 }
9529 }
9530
9531 size_t num_zygote_classes;
9532 size_t num_non_zygote_classes;
9533 };
9534
NumZygoteClasses() const9535 size_t ClassLinker::NumZygoteClasses() const {
9536 CountClassesVisitor visitor;
9537 VisitClassLoaders(&visitor);
9538 return visitor.num_zygote_classes + boot_class_table_->NumZygoteClasses(nullptr);
9539 }
9540
NumNonZygoteClasses() const9541 size_t ClassLinker::NumNonZygoteClasses() const {
9542 CountClassesVisitor visitor;
9543 VisitClassLoaders(&visitor);
9544 return visitor.num_non_zygote_classes + boot_class_table_->NumNonZygoteClasses(nullptr);
9545 }
9546
NumLoadedClasses()9547 size_t ClassLinker::NumLoadedClasses() {
9548 ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
9549 // Only return non zygote classes since these are the ones which apps which care about.
9550 return NumNonZygoteClasses();
9551 }
9552
GetClassesLockOwner()9553 pid_t ClassLinker::GetClassesLockOwner() {
9554 return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid();
9555 }
9556
GetDexLockOwner()9557 pid_t ClassLinker::GetDexLockOwner() {
9558 return Locks::dex_lock_->GetExclusiveOwnerTid();
9559 }
9560
SetClassRoot(ClassRoot class_root,ObjPtr<mirror::Class> klass)9561 void ClassLinker::SetClassRoot(ClassRoot class_root, ObjPtr<mirror::Class> klass) {
9562 DCHECK(!init_done_);
9563
9564 DCHECK(klass != nullptr);
9565 DCHECK(klass->GetClassLoader() == nullptr);
9566
9567 mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read();
9568 DCHECK(class_roots != nullptr);
9569 DCHECK_LT(static_cast<uint32_t>(class_root), static_cast<uint32_t>(ClassRoot::kMax));
9570 int32_t index = static_cast<int32_t>(class_root);
9571 DCHECK(class_roots->Get(index) == nullptr);
9572 class_roots->Set<false>(index, klass);
9573 }
9574
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)9575 ObjPtr<mirror::ClassLoader> ClassLinker::CreateWellKnownClassLoader(
9576 Thread* self,
9577 const std::vector<const DexFile*>& dex_files,
9578 Handle<mirror::Class> loader_class,
9579 Handle<mirror::ClassLoader> parent_loader,
9580 Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries) {
9581
9582 StackHandleScope<5> hs(self);
9583
9584 ArtField* dex_elements_field =
9585 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements);
9586
9587 Handle<mirror::Class> dex_elements_class(hs.NewHandle(dex_elements_field->ResolveType()));
9588 DCHECK(dex_elements_class != nullptr);
9589 DCHECK(dex_elements_class->IsArrayClass());
9590 Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements(hs.NewHandle(
9591 mirror::ObjectArray<mirror::Object>::Alloc(self,
9592 dex_elements_class.Get(),
9593 dex_files.size())));
9594 Handle<mirror::Class> h_dex_element_class =
9595 hs.NewHandle(dex_elements_class->GetComponentType());
9596
9597 ArtField* element_file_field =
9598 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile);
9599 DCHECK_EQ(h_dex_element_class.Get(), element_file_field->GetDeclaringClass());
9600
9601 ArtField* cookie_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie);
9602 DCHECK_EQ(cookie_field->GetDeclaringClass(), element_file_field->LookupResolvedType());
9603
9604 ArtField* file_name_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_fileName);
9605 DCHECK_EQ(file_name_field->GetDeclaringClass(), element_file_field->LookupResolvedType());
9606
9607 // Fill the elements array.
9608 int32_t index = 0;
9609 for (const DexFile* dex_file : dex_files) {
9610 StackHandleScope<4> hs2(self);
9611
9612 // CreateWellKnownClassLoader is only used by gtests and compiler.
9613 // Index 0 of h_long_array is supposed to be the oat file but we can leave it null.
9614 Handle<mirror::LongArray> h_long_array = hs2.NewHandle(mirror::LongArray::Alloc(
9615 self,
9616 kDexFileIndexStart + 1));
9617 DCHECK(h_long_array != nullptr);
9618 h_long_array->Set(kDexFileIndexStart, reinterpret_cast64<int64_t>(dex_file));
9619
9620 // Note that this creates a finalizable dalvik.system.DexFile object and a corresponding
9621 // FinalizerReference which will never get cleaned up without a started runtime.
9622 Handle<mirror::Object> h_dex_file = hs2.NewHandle(
9623 cookie_field->GetDeclaringClass()->AllocObject(self));
9624 DCHECK(h_dex_file != nullptr);
9625 cookie_field->SetObject<false>(h_dex_file.Get(), h_long_array.Get());
9626
9627 Handle<mirror::String> h_file_name = hs2.NewHandle(
9628 mirror::String::AllocFromModifiedUtf8(self, dex_file->GetLocation().c_str()));
9629 DCHECK(h_file_name != nullptr);
9630 file_name_field->SetObject<false>(h_dex_file.Get(), h_file_name.Get());
9631
9632 Handle<mirror::Object> h_element = hs2.NewHandle(h_dex_element_class->AllocObject(self));
9633 DCHECK(h_element != nullptr);
9634 element_file_field->SetObject<false>(h_element.Get(), h_dex_file.Get());
9635
9636 h_dex_elements->Set(index, h_element.Get());
9637 index++;
9638 }
9639 DCHECK_EQ(index, h_dex_elements->GetLength());
9640
9641 // Create DexPathList.
9642 Handle<mirror::Object> h_dex_path_list = hs.NewHandle(
9643 dex_elements_field->GetDeclaringClass()->AllocObject(self));
9644 DCHECK(h_dex_path_list != nullptr);
9645 // Set elements.
9646 dex_elements_field->SetObject<false>(h_dex_path_list.Get(), h_dex_elements.Get());
9647 // Create an empty List for the "nativeLibraryDirectories," required for native tests.
9648 // Note: this code is uncommon(oatdump)/testing-only, so don't add further WellKnownClasses
9649 // elements.
9650 {
9651 ArtField* native_lib_dirs = dex_elements_field->GetDeclaringClass()->
9652 FindDeclaredInstanceField("nativeLibraryDirectories", "Ljava/util/List;");
9653 DCHECK(native_lib_dirs != nullptr);
9654 ObjPtr<mirror::Class> list_class = FindSystemClass(self, "Ljava/util/ArrayList;");
9655 DCHECK(list_class != nullptr);
9656 {
9657 StackHandleScope<1> h_list_scope(self);
9658 Handle<mirror::Class> h_list_class(h_list_scope.NewHandle<mirror::Class>(list_class));
9659 bool list_init = EnsureInitialized(self, h_list_class, true, true);
9660 DCHECK(list_init);
9661 list_class = h_list_class.Get();
9662 }
9663 ObjPtr<mirror::Object> list_object = list_class->AllocObject(self);
9664 // Note: we leave the object uninitialized. This must never leak into any non-testing code, but
9665 // is fine for testing. While it violates a Java-code invariant (the elementData field is
9666 // normally never null), as long as one does not try to add elements, this will still
9667 // work.
9668 native_lib_dirs->SetObject<false>(h_dex_path_list.Get(), list_object);
9669 }
9670
9671 // Create the class loader..
9672 Handle<mirror::ClassLoader> h_class_loader = hs.NewHandle<mirror::ClassLoader>(
9673 ObjPtr<mirror::ClassLoader>::DownCast(loader_class->AllocObject(self)));
9674 DCHECK(h_class_loader != nullptr);
9675 // Set DexPathList.
9676 ArtField* path_list_field =
9677 jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList);
9678 DCHECK(path_list_field != nullptr);
9679 path_list_field->SetObject<false>(h_class_loader.Get(), h_dex_path_list.Get());
9680
9681 // Make a pretend boot-classpath.
9682 // TODO: Should we scan the image?
9683 ArtField* const parent_field =
9684 mirror::Class::FindField(self,
9685 h_class_loader->GetClass(),
9686 "parent",
9687 "Ljava/lang/ClassLoader;");
9688 DCHECK(parent_field != nullptr);
9689 if (parent_loader.Get() == nullptr) {
9690 ScopedObjectAccessUnchecked soa(self);
9691 ObjPtr<mirror::Object> boot_loader(soa.Decode<mirror::Class>(
9692 WellKnownClasses::java_lang_BootClassLoader)->AllocObject(self));
9693 parent_field->SetObject<false>(h_class_loader.Get(), boot_loader);
9694 } else {
9695 parent_field->SetObject<false>(h_class_loader.Get(), parent_loader.Get());
9696 }
9697
9698 ArtField* shared_libraries_field =
9699 jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders);
9700 DCHECK(shared_libraries_field != nullptr);
9701 shared_libraries_field->SetObject<false>(h_class_loader.Get(), shared_libraries.Get());
9702
9703 return h_class_loader.Get();
9704 }
9705
CreateWellKnownClassLoader(Thread * self,const std::vector<const DexFile * > & dex_files,jclass loader_class,jobject parent_loader,jobject shared_libraries)9706 jobject ClassLinker::CreateWellKnownClassLoader(Thread* self,
9707 const std::vector<const DexFile*>& dex_files,
9708 jclass loader_class,
9709 jobject parent_loader,
9710 jobject shared_libraries) {
9711 CHECK(self->GetJniEnv()->IsSameObject(loader_class,
9712 WellKnownClasses::dalvik_system_PathClassLoader) ||
9713 self->GetJniEnv()->IsSameObject(loader_class,
9714 WellKnownClasses::dalvik_system_DelegateLastClassLoader) ||
9715 self->GetJniEnv()->IsSameObject(loader_class,
9716 WellKnownClasses::dalvik_system_InMemoryDexClassLoader));
9717
9718 // SOAAlreadyRunnable is protected, and we need something to add a global reference.
9719 // We could move the jobject to the callers, but all call-sites do this...
9720 ScopedObjectAccessUnchecked soa(self);
9721
9722 // For now, create a libcore-level DexFile for each ART DexFile. This "explodes" multidex.
9723 StackHandleScope<4> hs(self);
9724
9725 Handle<mirror::Class> h_loader_class =
9726 hs.NewHandle<mirror::Class>(soa.Decode<mirror::Class>(loader_class));
9727 Handle<mirror::ClassLoader> h_parent =
9728 hs.NewHandle<mirror::ClassLoader>(soa.Decode<mirror::ClassLoader>(parent_loader));
9729 Handle<mirror::ObjectArray<mirror::ClassLoader>> h_shared_libraries =
9730 hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::ClassLoader>>(shared_libraries));
9731
9732 ObjPtr<mirror::ClassLoader> loader = CreateWellKnownClassLoader(
9733 self,
9734 dex_files,
9735 h_loader_class,
9736 h_parent,
9737 h_shared_libraries);
9738
9739 // Make it a global ref and return.
9740 ScopedLocalRef<jobject> local_ref(
9741 soa.Env(), soa.Env()->AddLocalReference<jobject>(loader));
9742 return soa.Env()->NewGlobalRef(local_ref.get());
9743 }
9744
CreatePathClassLoader(Thread * self,const std::vector<const DexFile * > & dex_files)9745 jobject ClassLinker::CreatePathClassLoader(Thread* self,
9746 const std::vector<const DexFile*>& dex_files) {
9747 return CreateWellKnownClassLoader(self,
9748 dex_files,
9749 WellKnownClasses::dalvik_system_PathClassLoader,
9750 nullptr);
9751 }
9752
DropFindArrayClassCache()9753 void ClassLinker::DropFindArrayClassCache() {
9754 std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr));
9755 find_array_class_cache_next_victim_ = 0;
9756 }
9757
VisitClassLoaders(ClassLoaderVisitor * visitor) const9758 void ClassLinker::VisitClassLoaders(ClassLoaderVisitor* visitor) const {
9759 Thread* const self = Thread::Current();
9760 for (const ClassLoaderData& data : class_loaders_) {
9761 // Need to use DecodeJObject so that we get null for cleared JNI weak globals.
9762 ObjPtr<mirror::ClassLoader> class_loader = ObjPtr<mirror::ClassLoader>::DownCast(
9763 self->DecodeJObject(data.weak_root));
9764 if (class_loader != nullptr) {
9765 visitor->Visit(class_loader);
9766 }
9767 }
9768 }
9769
VisitAllocators(AllocatorVisitor * visitor) const9770 void ClassLinker::VisitAllocators(AllocatorVisitor* visitor) const {
9771 for (const ClassLoaderData& data : class_loaders_) {
9772 LinearAlloc* alloc = data.allocator;
9773 if (alloc != nullptr && !visitor->Visit(alloc)) {
9774 break;
9775 }
9776 }
9777 }
9778
InsertDexFileInToClassLoader(ObjPtr<mirror::Object> dex_file,ObjPtr<mirror::ClassLoader> class_loader)9779 void ClassLinker::InsertDexFileInToClassLoader(ObjPtr<mirror::Object> dex_file,
9780 ObjPtr<mirror::ClassLoader> class_loader) {
9781 DCHECK(dex_file != nullptr);
9782 Thread* const self = Thread::Current();
9783 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
9784 ClassTable* const table = ClassTableForClassLoader(class_loader);
9785 DCHECK(table != nullptr);
9786 if (table->InsertStrongRoot(dex_file) && class_loader != nullptr) {
9787 // It was not already inserted, perform the write barrier to let the GC know the class loader's
9788 // class table was modified.
9789 WriteBarrier::ForEveryFieldWrite(class_loader);
9790 }
9791 }
9792
CleanupClassLoaders()9793 void ClassLinker::CleanupClassLoaders() {
9794 Thread* const self = Thread::Current();
9795 std::vector<ClassLoaderData> to_delete;
9796 // Do the delete outside the lock to avoid lock violation in jit code cache.
9797 {
9798 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
9799 for (auto it = class_loaders_.begin(); it != class_loaders_.end(); ) {
9800 const ClassLoaderData& data = *it;
9801 // Need to use DecodeJObject so that we get null for cleared JNI weak globals.
9802 ObjPtr<mirror::ClassLoader> class_loader =
9803 ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(data.weak_root));
9804 if (class_loader != nullptr) {
9805 ++it;
9806 } else {
9807 VLOG(class_linker) << "Freeing class loader";
9808 to_delete.push_back(data);
9809 it = class_loaders_.erase(it);
9810 }
9811 }
9812 }
9813 for (ClassLoaderData& data : to_delete) {
9814 // CHA unloading analysis and SingleImplementaion cleanups are required.
9815 DeleteClassLoader(self, data, /*cleanup_cha=*/ true);
9816 }
9817 }
9818
9819 class ClassLinker::FindVirtualMethodHolderVisitor : public ClassVisitor {
9820 public:
FindVirtualMethodHolderVisitor(const ArtMethod * method,PointerSize pointer_size)9821 FindVirtualMethodHolderVisitor(const ArtMethod* method, PointerSize pointer_size)
9822 : method_(method),
9823 pointer_size_(pointer_size) {}
9824
operator ()(ObjPtr<mirror::Class> klass)9825 bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) override {
9826 if (klass->GetVirtualMethodsSliceUnchecked(pointer_size_).Contains(method_)) {
9827 holder_ = klass;
9828 }
9829 // Return false to stop searching if holder_ is not null.
9830 return holder_ == nullptr;
9831 }
9832
9833 ObjPtr<mirror::Class> holder_ = nullptr;
9834 const ArtMethod* const method_;
9835 const PointerSize pointer_size_;
9836 };
9837
GetHoldingClassOfCopiedMethod(ArtMethod * method)9838 ObjPtr<mirror::Class> ClassLinker::GetHoldingClassOfCopiedMethod(ArtMethod* method) {
9839 ScopedTrace trace(__FUNCTION__); // Since this function is slow, have a trace to notify people.
9840 CHECK(method->IsCopied());
9841 FindVirtualMethodHolderVisitor visitor(method, image_pointer_size_);
9842 VisitClasses(&visitor);
9843 return visitor.holder_;
9844 }
9845
AllocIfTable(Thread * self,size_t ifcount)9846 ObjPtr<mirror::IfTable> ClassLinker::AllocIfTable(Thread* self, size_t ifcount) {
9847 return ObjPtr<mirror::IfTable>::DownCast(ObjPtr<mirror::ObjectArray<mirror::Object>>(
9848 mirror::IfTable::Alloc(self,
9849 GetClassRoot<mirror::ObjectArray<mirror::Object>>(this),
9850 ifcount * mirror::IfTable::kMax)));
9851 }
9852
IsUpdatableBootClassPathDescriptor(const char * descriptor ATTRIBUTE_UNUSED)9853 bool ClassLinker::IsUpdatableBootClassPathDescriptor(const char* descriptor ATTRIBUTE_UNUSED) {
9854 // Should not be called on ClassLinker, only on AotClassLinker that overrides this.
9855 LOG(FATAL) << "UNREACHABLE";
9856 UNREACHABLE();
9857 }
9858
9859 // Instantiate ClassLinker::ResolveMethod.
9860 template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
9861 uint32_t method_idx,
9862 Handle<mirror::DexCache> dex_cache,
9863 Handle<mirror::ClassLoader> class_loader,
9864 ArtMethod* referrer,
9865 InvokeType type);
9866 template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
9867 uint32_t method_idx,
9868 Handle<mirror::DexCache> dex_cache,
9869 Handle<mirror::ClassLoader> class_loader,
9870 ArtMethod* referrer,
9871 InvokeType type);
9872
9873 // Instantiate ClassLinker::AllocClass.
9874 template ObjPtr<mirror::Class> ClassLinker::AllocClass</* kMovable= */ true>(
9875 Thread* self,
9876 ObjPtr<mirror::Class> java_lang_Class,
9877 uint32_t class_size);
9878 template ObjPtr<mirror::Class> ClassLinker::AllocClass</* kMovable= */ false>(
9879 Thread* self,
9880 ObjPtr<mirror::Class> java_lang_Class,
9881 uint32_t class_size);
9882
9883 } // namespace art
9884