Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "class_linker.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 18 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 20 | #include <deque> |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 21 | #include <iostream> |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 22 | #include <map> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 23 | #include <memory> |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 24 | #include <queue> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 25 | #include <string> |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 26 | #include <tuple> |
Andreas Gampe | a696c0a | 2014-12-10 20:51:45 -0800 | [diff] [blame] | 27 | #include <unistd.h> |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 28 | #include <unordered_map> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 29 | #include <utility> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 30 | #include <vector> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 31 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 32 | #include "android-base/stringprintf.h" |
| 33 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 34 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 35 | #include "art_method-inl.h" |
| 36 | #include "base/arena_allocator.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 37 | #include "base/casts.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 38 | #include "base/logging.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 39 | #include "base/scoped_arena_containers.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 40 | #include "base/scoped_flock.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 41 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 42 | #include "base/systrace.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 43 | #include "base/time_utils.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 44 | #include "base/unix_file/fd_file.h" |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 45 | #include "base/value_object.h" |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 46 | #include "cha.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 47 | #include "class_linker-inl.h" |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 48 | #include "class_table-inl.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 49 | #include "compiler_callbacks.h" |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 50 | #include "debugger.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 51 | #include "dex_file-inl.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 52 | #include "entrypoints/entrypoint_utils.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 53 | #include "entrypoints/runtime_asm_entrypoints.h" |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 54 | #include "experimental_flags.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 55 | #include "gc_root-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 56 | #include "gc/accounting/card_table-inl.h" |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 57 | #include "gc/accounting/heap_bitmap-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 58 | #include "gc/heap.h" |
Mathieu Chartier | 1b1e31f | 2016-05-19 10:13:04 -0700 | [diff] [blame] | 59 | #include "gc/scoped_gc_critical_section.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 60 | #include "gc/space/image_space.h" |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 61 | #include "gc/space/space-inl.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 62 | #include "handle_scope-inl.h" |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 63 | #include "image-inl.h" |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 64 | #include "imt_conflict_table.h" |
| 65 | #include "imtable-inl.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 66 | #include "intern_table.h" |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 67 | #include "interpreter/interpreter.h" |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 68 | #include "java_vm_ext.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 69 | #include "jit/jit.h" |
| 70 | #include "jit/jit_code_cache.h" |
Calin Juravle | 33083d6 | 2017-01-18 15:29:12 -0800 | [diff] [blame] | 71 | #include "jit/profile_compilation_info.h" |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 72 | #include "jni_internal.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 73 | #include "leb128.h" |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 74 | #include "linear_alloc.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 75 | #include "mirror/call_site.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 76 | #include "mirror/class.h" |
| 77 | #include "mirror/class-inl.h" |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 78 | #include "mirror/class_ext.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 79 | #include "mirror/class_loader.h" |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 80 | #include "mirror/dex_cache.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 81 | #include "mirror/dex_cache-inl.h" |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 82 | #include "mirror/emulated_stack_frame.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 83 | #include "mirror/field.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 84 | #include "mirror/iftable-inl.h" |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 85 | #include "mirror/method.h" |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 86 | #include "mirror/method_type.h" |
| 87 | #include "mirror/method_handle_impl.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 88 | #include "mirror/method_handles_lookup.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 89 | #include "mirror/object-inl.h" |
| 90 | #include "mirror/object_array-inl.h" |
| 91 | #include "mirror/proxy.h" |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 92 | #include "mirror/reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 93 | #include "mirror/stack_trace_element.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 94 | #include "mirror/string-inl.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 95 | #include "native/dalvik_system_DexFile.h" |
| 96 | #include "oat.h" |
| 97 | #include "oat_file.h" |
| 98 | #include "oat_file-inl.h" |
| 99 | #include "oat_file_assistant.h" |
| 100 | #include "oat_file_manager.h" |
| 101 | #include "object_lock.h" |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 102 | #include "os.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 103 | #include "runtime.h" |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 104 | #include "runtime_callbacks.h" |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 105 | #include "ScopedLocalRef.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 106 | #include "scoped_thread_state_change-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 107 | #include "thread-inl.h" |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 108 | #include "thread_list.h" |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 109 | #include "trace.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 110 | #include "utils.h" |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 111 | #include "utils/dex_cache_arrays_layout-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 112 | #include "verifier/method_verifier.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 113 | #include "well_known_classes.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 114 | |
| 115 | namespace art { |
| 116 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 117 | using android::base::StringPrintf; |
| 118 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 119 | static constexpr bool kSanityCheckObjects = kIsDebugBuild; |
Mathieu Chartier | 8790c7f | 2016-03-31 15:05:45 -0700 | [diff] [blame] | 120 | static constexpr bool kVerifyArtMethodDeclaringClasses = kIsDebugBuild; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 121 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 122 | static void ThrowNoClassDefFoundError(const char* fmt, ...) |
| 123 | __attribute__((__format__(__printf__, 1, 2))) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 124 | REQUIRES_SHARED(Locks::mutator_lock_); |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 125 | static void ThrowNoClassDefFoundError(const char* fmt, ...) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 126 | va_list args; |
| 127 | va_start(args, fmt); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 128 | Thread* self = Thread::Current(); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 129 | self->ThrowNewExceptionV("Ljava/lang/NoClassDefFoundError;", fmt, args); |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 130 | va_end(args); |
| 131 | } |
| 132 | |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 133 | static bool HasInitWithString(Thread* self, ClassLinker* class_linker, const char* descriptor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 134 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 135 | ArtMethod* method = self->GetCurrentMethod(nullptr); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 136 | StackHandleScope<1> hs(self); |
| 137 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(method != nullptr ? |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 138 | method->GetDeclaringClass()->GetClassLoader() : nullptr)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 139 | ObjPtr<mirror::Class> exception_class = class_linker->FindClass(self, descriptor, class_loader); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 140 | |
| 141 | if (exception_class == nullptr) { |
| 142 | // No exc class ~ no <init>-with-string. |
| 143 | CHECK(self->IsExceptionPending()); |
| 144 | self->ClearException(); |
| 145 | return false; |
| 146 | } |
| 147 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 148 | ArtMethod* exception_init_method = exception_class->FindDeclaredDirectMethod( |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 149 | "<init>", "(Ljava/lang/String;)V", class_linker->GetImagePointerSize()); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 150 | return exception_init_method != nullptr; |
| 151 | } |
| 152 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 153 | static mirror::Object* GetVerifyError(ObjPtr<mirror::Class> c) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 154 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 155 | ObjPtr<mirror::ClassExt> ext(c->GetExtData()); |
| 156 | if (ext == nullptr) { |
| 157 | return nullptr; |
| 158 | } else { |
| 159 | return ext->GetVerifyError(); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // Helper for ThrowEarlierClassFailure. Throws the stored error. |
| 164 | static void HandleEarlierVerifyError(Thread* self, |
| 165 | ClassLinker* class_linker, |
| 166 | ObjPtr<mirror::Class> c) |
| 167 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 168 | ObjPtr<mirror::Object> obj = GetVerifyError(c); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 169 | DCHECK(obj != nullptr); |
| 170 | self->AssertNoPendingException(); |
| 171 | if (obj->IsClass()) { |
| 172 | // Previous error has been stored as class. Create a new exception of that type. |
| 173 | |
| 174 | // It's possible the exception doesn't have a <init>(String). |
| 175 | std::string temp; |
| 176 | const char* descriptor = obj->AsClass()->GetDescriptor(&temp); |
| 177 | |
| 178 | if (HasInitWithString(self, class_linker, descriptor)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 179 | self->ThrowNewException(descriptor, c->PrettyDescriptor().c_str()); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 180 | } else { |
| 181 | self->ThrowNewException(descriptor, nullptr); |
| 182 | } |
| 183 | } else { |
| 184 | // Previous error has been stored as an instance. Just rethrow. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 185 | ObjPtr<mirror::Class> throwable_class = |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 186 | self->DecodeJObject(WellKnownClasses::java_lang_Throwable)->AsClass(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 187 | ObjPtr<mirror::Class> error_class = obj->GetClass(); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 188 | CHECK(throwable_class->IsAssignableFrom(error_class)); |
| 189 | self->SetException(obj->AsThrowable()); |
| 190 | } |
| 191 | self->AssertPendingException(); |
| 192 | } |
| 193 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 194 | void ClassLinker::ThrowEarlierClassFailure(ObjPtr<mirror::Class> c, bool wrap_in_no_class_def) { |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 195 | // The class failed to initialize on a previous attempt, so we want to throw |
| 196 | // a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we |
| 197 | // failed in verification, in which case v2 5.4.1 says we need to re-throw |
| 198 | // the previous error. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 199 | Runtime* const runtime = Runtime::Current(); |
| 200 | if (!runtime->IsAotCompiler()) { // Give info if this occurs at runtime. |
Andreas Gampe | 3d6b470 | 2015-09-21 08:35:52 -0700 | [diff] [blame] | 201 | std::string extra; |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 202 | if (GetVerifyError(c) != nullptr) { |
| 203 | ObjPtr<mirror::Object> verify_error = GetVerifyError(c); |
Andreas Gampe | 369c851 | 2016-01-28 15:31:39 -0800 | [diff] [blame] | 204 | if (verify_error->IsClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 205 | extra = mirror::Class::PrettyDescriptor(verify_error->AsClass()); |
Andreas Gampe | 369c851 | 2016-01-28 15:31:39 -0800 | [diff] [blame] | 206 | } else { |
| 207 | extra = verify_error->AsThrowable()->Dump(); |
| 208 | } |
Andreas Gampe | 3d6b470 | 2015-09-21 08:35:52 -0700 | [diff] [blame] | 209 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 210 | LOG(INFO) << "Rejecting re-init on previously-failed class " << c->PrettyClass() |
| 211 | << ": " << extra; |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 212 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 213 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 214 | CHECK(c->IsErroneous()) << c->PrettyClass() << " " << c->GetStatus(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 215 | Thread* self = Thread::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 216 | if (runtime->IsAotCompiler()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 217 | // At compile time, accurate errors and NCDFE are disabled to speed compilation. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 218 | ObjPtr<mirror::Throwable> pre_allocated = runtime->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 219 | self->SetException(pre_allocated); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 220 | } else { |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 221 | if (GetVerifyError(c) != nullptr) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 222 | // Rethrow stored error. |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 223 | HandleEarlierVerifyError(self, this, c); |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 224 | } |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 225 | // TODO This might be wrong if we hit an OOME while allocating the ClassExt. In that case we |
| 226 | // might have meant to go down the earlier if statement with the original error but it got |
| 227 | // swallowed by the OOM so we end up here. |
| 228 | if (GetVerifyError(c) == nullptr || wrap_in_no_class_def) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 229 | // If there isn't a recorded earlier error, or this is a repeat throw from initialization, |
| 230 | // the top-level exception must be a NoClassDefFoundError. The potentially already pending |
| 231 | // exception will be a cause. |
| 232 | self->ThrowNewWrappedException("Ljava/lang/NoClassDefFoundError;", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 233 | c->PrettyDescriptor().c_str()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 234 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 238 | static void VlogClassInitializationFailure(Handle<mirror::Class> klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 239 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 240 | if (VLOG_IS_ON(class_linker)) { |
| 241 | std::string temp; |
| 242 | LOG(INFO) << "Failed to initialize class " << klass->GetDescriptor(&temp) << " from " |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 243 | << klass->GetLocation() << "\n" << Thread::Current()->GetException()->Dump(); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
| 247 | static void WrapExceptionInInitializer(Handle<mirror::Class> klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 248 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 249 | Thread* self = Thread::Current(); |
| 250 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 251 | |
| 252 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 253 | CHECK(cause.get() != nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 254 | |
Andreas Gampe | 1e8a395 | 2016-11-30 10:13:19 -0800 | [diff] [blame] | 255 | // Boot classpath classes should not fail initialization. This is a sanity debug check. This |
| 256 | // cannot in general be guaranteed, but in all likelihood leads to breakage down the line. |
| 257 | if (klass->GetClassLoader() == nullptr && !Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | 22f71d2 | 2016-11-21 10:10:08 -0800 | [diff] [blame] | 258 | std::string tmp; |
Andreas Gampe | 1e8a395 | 2016-11-30 10:13:19 -0800 | [diff] [blame] | 259 | LOG(kIsDebugBuild ? FATAL : WARNING) << klass->GetDescriptor(&tmp) << " failed initialization"; |
Andreas Gampe | 22f71d2 | 2016-11-21 10:10:08 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 262 | env->ExceptionClear(); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 263 | bool is_error = env->IsInstanceOf(cause.get(), WellKnownClasses::java_lang_Error); |
| 264 | env->Throw(cause.get()); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 265 | |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 266 | // We only wrap non-Error exceptions; an Error can just be used as-is. |
| 267 | if (!is_error) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 268 | self->ThrowNewWrappedException("Ljava/lang/ExceptionInInitializerError;", nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 269 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 270 | VlogClassInitializationFailure(klass); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 273 | // Gap between two fields in object layout. |
| 274 | struct FieldGap { |
| 275 | uint32_t start_offset; // The offset from the start of the object. |
| 276 | uint32_t size; // The gap size of 1, 2, or 4 bytes. |
| 277 | }; |
| 278 | struct FieldGapsComparator { |
| 279 | explicit FieldGapsComparator() { |
| 280 | } |
| 281 | bool operator() (const FieldGap& lhs, const FieldGap& rhs) |
| 282 | NO_THREAD_SAFETY_ANALYSIS { |
Andreas Gampe | f52857f | 2015-02-18 15:38:57 -0800 | [diff] [blame] | 283 | // Sort by gap size, largest first. Secondary sort by starting offset. |
Richard Uhler | fab6788 | 2015-07-13 17:00:35 -0700 | [diff] [blame] | 284 | // Note that the priority queue returns the largest element, so operator() |
| 285 | // should return true if lhs is less than rhs. |
| 286 | return lhs.size < rhs.size || (lhs.size == rhs.size && lhs.start_offset > rhs.start_offset); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 287 | } |
| 288 | }; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 289 | typedef std::priority_queue<FieldGap, std::vector<FieldGap>, FieldGapsComparator> FieldGaps; |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 290 | |
| 291 | // Adds largest aligned gaps to queue of gaps. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 292 | static void AddFieldGap(uint32_t gap_start, uint32_t gap_end, FieldGaps* gaps) { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 293 | DCHECK(gaps != nullptr); |
| 294 | |
| 295 | uint32_t current_offset = gap_start; |
| 296 | while (current_offset != gap_end) { |
| 297 | size_t remaining = gap_end - current_offset; |
| 298 | if (remaining >= sizeof(uint32_t) && IsAligned<4>(current_offset)) { |
| 299 | gaps->push(FieldGap {current_offset, sizeof(uint32_t)}); |
| 300 | current_offset += sizeof(uint32_t); |
| 301 | } else if (remaining >= sizeof(uint16_t) && IsAligned<2>(current_offset)) { |
| 302 | gaps->push(FieldGap {current_offset, sizeof(uint16_t)}); |
| 303 | current_offset += sizeof(uint16_t); |
| 304 | } else { |
| 305 | gaps->push(FieldGap {current_offset, sizeof(uint8_t)}); |
| 306 | current_offset += sizeof(uint8_t); |
| 307 | } |
| 308 | DCHECK_LE(current_offset, gap_end) << "Overran gap"; |
| 309 | } |
| 310 | } |
| 311 | // Shuffle fields forward, making use of gaps whenever possible. |
| 312 | template<int n> |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 313 | static void ShuffleForward(size_t* current_field_idx, |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 314 | MemberOffset* field_offset, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 315 | std::deque<ArtField*>* grouped_and_sorted_fields, |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 316 | FieldGaps* gaps) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 317 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 318 | DCHECK(current_field_idx != nullptr); |
| 319 | DCHECK(grouped_and_sorted_fields != nullptr); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 320 | DCHECK(gaps != nullptr); |
| 321 | DCHECK(field_offset != nullptr); |
| 322 | |
| 323 | DCHECK(IsPowerOfTwo(n)); |
| 324 | while (!grouped_and_sorted_fields->empty()) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 325 | ArtField* field = grouped_and_sorted_fields->front(); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 326 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 327 | if (Primitive::ComponentSize(type) < n) { |
| 328 | break; |
| 329 | } |
| 330 | if (!IsAligned<n>(field_offset->Uint32Value())) { |
| 331 | MemberOffset old_offset = *field_offset; |
| 332 | *field_offset = MemberOffset(RoundUp(field_offset->Uint32Value(), n)); |
| 333 | AddFieldGap(old_offset.Uint32Value(), field_offset->Uint32Value(), gaps); |
| 334 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 335 | CHECK(type != Primitive::kPrimNot) << field->PrettyField(); // should be primitive types |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 336 | grouped_and_sorted_fields->pop_front(); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 337 | if (!gaps->empty() && gaps->top().size >= n) { |
| 338 | FieldGap gap = gaps->top(); |
| 339 | gaps->pop(); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 340 | DCHECK_ALIGNED(gap.start_offset, n); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 341 | field->SetOffset(MemberOffset(gap.start_offset)); |
| 342 | if (gap.size > n) { |
| 343 | AddFieldGap(gap.start_offset + n, gap.start_offset + gap.size, gaps); |
| 344 | } |
| 345 | } else { |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 346 | DCHECK_ALIGNED(field_offset->Uint32Value(), n); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 347 | field->SetOffset(*field_offset); |
| 348 | *field_offset = MemberOffset(field_offset->Uint32Value() + n); |
| 349 | } |
| 350 | ++(*current_field_idx); |
| 351 | } |
| 352 | } |
| 353 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 354 | ClassLinker::ClassLinker(InternTable* intern_table) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame^] | 355 | : boot_class_table_(new ClassTable()), |
| 356 | failed_dex_cache_class_lookups_(0), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 357 | class_roots_(nullptr), |
| 358 | array_iftable_(nullptr), |
| 359 | find_array_class_cache_next_victim_(0), |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 360 | init_done_(false), |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 361 | log_new_roots_(false), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 362 | intern_table_(intern_table), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 363 | quick_resolution_trampoline_(nullptr), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 364 | quick_imt_conflict_trampoline_(nullptr), |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 365 | quick_generic_jni_trampoline_(nullptr), |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 366 | quick_to_interpreter_bridge_trampoline_(nullptr), |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 367 | image_pointer_size_(kRuntimePointerSize) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 368 | CHECK(intern_table_ != nullptr); |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 369 | static_assert(kFindArrayCacheSize == arraysize(find_array_class_cache_), |
| 370 | "Array cache size wrong."); |
| 371 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 372 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 373 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 374 | void ClassLinker::CheckSystemClass(Thread* self, Handle<mirror::Class> c1, const char* descriptor) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 375 | ObjPtr<mirror::Class> c2 = FindSystemClass(self, descriptor); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 376 | if (c2 == nullptr) { |
| 377 | LOG(FATAL) << "Could not find class " << descriptor; |
| 378 | UNREACHABLE(); |
| 379 | } |
| 380 | if (c1.Get() != c2) { |
| 381 | std::ostringstream os1, os2; |
| 382 | c1->DumpClass(os1, mirror::Class::kDumpClassFullDetail); |
| 383 | c2->DumpClass(os2, mirror::Class::kDumpClassFullDetail); |
| 384 | LOG(FATAL) << "InitWithoutImage: Class mismatch for " << descriptor |
| 385 | << ". This is most likely the result of a broken build. Make sure that " |
| 386 | << "libcore and art projects match.\n\n" |
| 387 | << os1.str() << "\n\n" << os2.str(); |
| 388 | UNREACHABLE(); |
| 389 | } |
| 390 | } |
| 391 | |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 392 | bool ClassLinker::InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path, |
| 393 | std::string* error_msg) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 394 | VLOG(startup) << "ClassLinker::Init"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 395 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 396 | Thread* const self = Thread::Current(); |
| 397 | Runtime* const runtime = Runtime::Current(); |
| 398 | gc::Heap* const heap = runtime->GetHeap(); |
| 399 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 400 | CHECK(!heap->HasBootImageSpace()) << "Runtime has image. We should use it."; |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 401 | CHECK(!init_done_); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 402 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 403 | // Use the pointer size from the runtime since we are probably creating the image. |
| 404 | image_pointer_size_ = InstructionSetPointerSize(runtime->GetInstructionSet()); |
| 405 | |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 406 | // java_lang_Class comes first, it's needed for AllocClass |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 407 | // The GC can't handle an object with a null class since we can't get the size of this object. |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 408 | heap->IncrementDisableMovingGC(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 409 | StackHandleScope<64> hs(self); // 64 is picked arbitrarily. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 410 | auto class_class_size = mirror::Class::ClassClassSize(image_pointer_size_); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 411 | Handle<mirror::Class> java_lang_Class(hs.NewHandle(down_cast<mirror::Class*>( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 412 | heap->AllocNonMovableObject<true>(self, nullptr, class_class_size, VoidFunctor())))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 413 | CHECK(java_lang_Class != nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 414 | mirror::Class::SetClassClass(java_lang_Class.Get()); |
| 415 | java_lang_Class->SetClass(java_lang_Class.Get()); |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 416 | if (kUseBakerReadBarrier) { |
| 417 | java_lang_Class->AssertReadBarrierState(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 418 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 419 | java_lang_Class->SetClassSize(class_class_size); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 420 | java_lang_Class->SetPrimitiveType(Primitive::kPrimNot); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 421 | heap->DecrementDisableMovingGC(self); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 422 | // AllocClass(ObjPtr<mirror::Class>) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 423 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 424 | // Class[] is used for reflection support. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 425 | auto class_array_class_size = mirror::ObjectArray<mirror::Class>::ClassSize(image_pointer_size_); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 426 | Handle<mirror::Class> class_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 427 | AllocClass(self, java_lang_Class.Get(), class_array_class_size))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 428 | class_array_class->SetComponentType(java_lang_Class.Get()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 429 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 430 | // java_lang_Object comes next so that object_array_class can be created. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 431 | Handle<mirror::Class> java_lang_Object(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 432 | AllocClass(self, java_lang_Class.Get(), mirror::Object::ClassSize(image_pointer_size_)))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 433 | CHECK(java_lang_Object != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 434 | // backfill Object as the super class of Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 435 | java_lang_Class->SetSuperClass(java_lang_Object.Get()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 436 | mirror::Class::SetStatus(java_lang_Object, mirror::Class::kStatusLoaded, self); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 437 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 438 | java_lang_Object->SetObjectSize(sizeof(mirror::Object)); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 439 | // Allocate in non-movable so that it's possible to check if a JNI weak global ref has been |
| 440 | // cleared without triggering the read barrier and unintentionally mark the sentinel alive. |
| 441 | runtime->SetSentinel(heap->AllocNonMovableObject<true>(self, |
| 442 | java_lang_Object.Get(), |
| 443 | java_lang_Object->GetObjectSize(), |
| 444 | VoidFunctor())); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 445 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 446 | // Object[] next to hold class roots. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 447 | Handle<mirror::Class> object_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 448 | AllocClass(self, java_lang_Class.Get(), |
| 449 | mirror::ObjectArray<mirror::Object>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 450 | object_array_class->SetComponentType(java_lang_Object.Get()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 451 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 452 | // Setup the char (primitive) class to be used for char[]. |
| 453 | Handle<mirror::Class> char_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 454 | AllocClass(self, java_lang_Class.Get(), |
| 455 | mirror::Class::PrimitiveClassSize(image_pointer_size_)))); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 456 | // The primitive char class won't be initialized by |
| 457 | // InitializePrimitiveClass until line 459, but strings (and |
| 458 | // internal char arrays) will be allocated before that and the |
| 459 | // component size, which is computed from the primitive type, needs |
| 460 | // to be set here. |
| 461 | char_class->SetPrimitiveType(Primitive::kPrimChar); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 462 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 463 | // Setup the char[] class to be used for String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 464 | Handle<mirror::Class> char_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 465 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 466 | char_array_class->SetComponentType(char_class.Get()); |
| 467 | mirror::CharArray::SetArrayClass(char_array_class.Get()); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 468 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 469 | // Setup String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 470 | Handle<mirror::Class> java_lang_String(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 471 | AllocClass(self, java_lang_Class.Get(), mirror::String::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 472 | java_lang_String->SetStringClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 473 | mirror::String::SetClass(java_lang_String.Get()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 474 | mirror::Class::SetStatus(java_lang_String, mirror::Class::kStatusResolved, self); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 475 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 476 | // Setup java.lang.ref.Reference. |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 477 | Handle<mirror::Class> java_lang_ref_Reference(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 478 | AllocClass(self, java_lang_Class.Get(), mirror::Reference::ClassSize(image_pointer_size_)))); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 479 | mirror::Reference::SetClass(java_lang_ref_Reference.Get()); |
| 480 | java_lang_ref_Reference->SetObjectSize(mirror::Reference::InstanceSize()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 481 | mirror::Class::SetStatus(java_lang_ref_Reference, mirror::Class::kStatusResolved, self); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 482 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 483 | // Create storage for root classes, save away our work so far (requires descriptors). |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 484 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 485 | mirror::ObjectArray<mirror::Class>::Alloc(self, object_array_class.Get(), |
| 486 | kClassRootsMax)); |
| 487 | CHECK(!class_roots_.IsNull()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 488 | SetClassRoot(kJavaLangClass, java_lang_Class.Get()); |
| 489 | SetClassRoot(kJavaLangObject, java_lang_Object.Get()); |
| 490 | SetClassRoot(kClassArrayClass, class_array_class.Get()); |
| 491 | SetClassRoot(kObjectArrayClass, object_array_class.Get()); |
| 492 | SetClassRoot(kCharArrayClass, char_array_class.Get()); |
| 493 | SetClassRoot(kJavaLangString, java_lang_String.Get()); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 494 | SetClassRoot(kJavaLangRefReference, java_lang_ref_Reference.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 495 | |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 496 | // Fill in the empty iftable. Needs to be done after the kObjectArrayClass root is set. |
| 497 | java_lang_Object->SetIfTable(AllocIfTable(self, 0)); |
| 498 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 499 | // Setup the primitive type classes. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 500 | SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass(self, Primitive::kPrimBoolean)); |
| 501 | SetClassRoot(kPrimitiveByte, CreatePrimitiveClass(self, Primitive::kPrimByte)); |
| 502 | SetClassRoot(kPrimitiveShort, CreatePrimitiveClass(self, Primitive::kPrimShort)); |
| 503 | SetClassRoot(kPrimitiveInt, CreatePrimitiveClass(self, Primitive::kPrimInt)); |
| 504 | SetClassRoot(kPrimitiveLong, CreatePrimitiveClass(self, Primitive::kPrimLong)); |
| 505 | SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass(self, Primitive::kPrimFloat)); |
| 506 | SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass(self, Primitive::kPrimDouble)); |
| 507 | SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass(self, Primitive::kPrimVoid)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 508 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 509 | // Create array interface entries to populate once we can load system classes. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 510 | array_iftable_ = GcRoot<mirror::IfTable>(AllocIfTable(self, 2)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 511 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 512 | // Create int array type for AllocDexCache (done in AppendToBootClassPath). |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 513 | Handle<mirror::Class> int_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 514 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize(image_pointer_size_)))); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 515 | int_array_class->SetComponentType(GetClassRoot(kPrimitiveInt)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 516 | mirror::IntArray::SetArrayClass(int_array_class.Get()); |
| 517 | SetClassRoot(kIntArrayClass, int_array_class.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 518 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 519 | // Create long array type for AllocDexCache (done in AppendToBootClassPath). |
| 520 | Handle<mirror::Class> long_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 521 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 522 | long_array_class->SetComponentType(GetClassRoot(kPrimitiveLong)); |
| 523 | mirror::LongArray::SetArrayClass(long_array_class.Get()); |
| 524 | SetClassRoot(kLongArrayClass, long_array_class.Get()); |
| 525 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 526 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 527 | |
Ian Rogers | 52813c9 | 2012-10-11 11:50:38 -0700 | [diff] [blame] | 528 | // Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 529 | Handle<mirror::Class> java_lang_DexCache(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 530 | AllocClass(self, java_lang_Class.Get(), mirror::DexCache::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 531 | SetClassRoot(kJavaLangDexCache, java_lang_DexCache.Get()); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 532 | java_lang_DexCache->SetDexCacheClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 533 | java_lang_DexCache->SetObjectSize(mirror::DexCache::InstanceSize()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 534 | mirror::Class::SetStatus(java_lang_DexCache, mirror::Class::kStatusResolved, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 535 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 536 | |
| 537 | // Setup dalvik.system.ClassExt |
| 538 | Handle<mirror::Class> dalvik_system_ClassExt(hs.NewHandle( |
| 539 | AllocClass(self, java_lang_Class.Get(), mirror::ClassExt::ClassSize(image_pointer_size_)))); |
| 540 | SetClassRoot(kDalvikSystemClassExt, dalvik_system_ClassExt.Get()); |
| 541 | mirror::ClassExt::SetClass(dalvik_system_ClassExt.Get()); |
| 542 | mirror::Class::SetStatus(dalvik_system_ClassExt, mirror::Class::kStatusResolved, self); |
| 543 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 544 | // Set up array classes for string, field, method |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 545 | Handle<mirror::Class> object_array_string(hs.NewHandle( |
| 546 | AllocClass(self, java_lang_Class.Get(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 547 | mirror::ObjectArray<mirror::String>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 548 | object_array_string->SetComponentType(java_lang_String.Get()); |
| 549 | SetClassRoot(kJavaLangStringArrayClass, object_array_string.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 550 | |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 551 | LinearAlloc* linear_alloc = runtime->GetLinearAlloc(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 552 | // Create runtime resolution and imt conflict methods. |
| 553 | runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 554 | runtime->SetImtConflictMethod(runtime->CreateImtConflictMethod(linear_alloc)); |
| 555 | runtime->SetImtUnimplementedMethod(runtime->CreateImtConflictMethod(linear_alloc)); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 556 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 557 | // Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create |
| 558 | // DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses |
| 559 | // these roots. |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 560 | if (boot_class_path.empty()) { |
| 561 | *error_msg = "Boot classpath is empty."; |
| 562 | return false; |
| 563 | } |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 564 | for (auto& dex_file : boot_class_path) { |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 565 | if (dex_file.get() == nullptr) { |
| 566 | *error_msg = "Null dex file."; |
| 567 | return false; |
| 568 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 569 | AppendToBootClassPath(self, *dex_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 570 | boot_dex_files_.push_back(std::move(dex_file)); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 571 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 572 | |
| 573 | // now we can use FindSystemClass |
| 574 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 575 | // run char class through InitializePrimitiveClass to finish init |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 576 | InitializePrimitiveClass(char_class.Get(), Primitive::kPrimChar); |
| 577 | SetClassRoot(kPrimitiveChar, char_class.Get()); // needs descriptor |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 578 | |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 579 | // Set up GenericJNI entrypoint. That is mainly a hack for common_compiler_test.h so that |
| 580 | // we do not need friend classes or a publicly exposed setter. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 581 | quick_generic_jni_trampoline_ = GetQuickGenericJniStub(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 582 | if (!runtime->IsAotCompiler()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 583 | // We need to set up the generic trampolines since we don't have an image. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 584 | quick_resolution_trampoline_ = GetQuickResolutionStub(); |
| 585 | quick_imt_conflict_trampoline_ = GetQuickImtConflictStub(); |
| 586 | quick_to_interpreter_bridge_trampoline_ = GetQuickToInterpreterBridge(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 587 | } |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 588 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 589 | // Object, String, ClassExt and DexCache need to be rerun through FindSystemClass to finish init |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 590 | mirror::Class::SetStatus(java_lang_Object, mirror::Class::kStatusNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 591 | CheckSystemClass(self, java_lang_Object, "Ljava/lang/Object;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 592 | CHECK_EQ(java_lang_Object->GetObjectSize(), mirror::Object::InstanceSize()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 593 | mirror::Class::SetStatus(java_lang_String, mirror::Class::kStatusNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 594 | CheckSystemClass(self, java_lang_String, "Ljava/lang/String;"); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 595 | mirror::Class::SetStatus(java_lang_DexCache, mirror::Class::kStatusNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 596 | CheckSystemClass(self, java_lang_DexCache, "Ljava/lang/DexCache;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 597 | CHECK_EQ(java_lang_DexCache->GetObjectSize(), mirror::DexCache::InstanceSize()); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 598 | mirror::Class::SetStatus(dalvik_system_ClassExt, mirror::Class::kStatusNotReady, self); |
| 599 | CheckSystemClass(self, dalvik_system_ClassExt, "Ldalvik/system/ClassExt;"); |
| 600 | CHECK_EQ(dalvik_system_ClassExt->GetObjectSize(), mirror::ClassExt::InstanceSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 601 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 602 | // Setup the primitive array type classes - can't be done until Object has a vtable. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 603 | SetClassRoot(kBooleanArrayClass, FindSystemClass(self, "[Z")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 604 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 605 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 606 | SetClassRoot(kByteArrayClass, FindSystemClass(self, "[B")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 607 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 608 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 609 | CheckSystemClass(self, char_array_class, "[C"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 610 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 611 | SetClassRoot(kShortArrayClass, FindSystemClass(self, "[S")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 612 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 613 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 614 | CheckSystemClass(self, int_array_class, "[I"); |
| 615 | CheckSystemClass(self, long_array_class, "[J"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 616 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 617 | SetClassRoot(kFloatArrayClass, FindSystemClass(self, "[F")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 618 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 619 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 620 | SetClassRoot(kDoubleArrayClass, FindSystemClass(self, "[D")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 621 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 622 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 623 | // Run Class through FindSystemClass. This initializes the dex_cache_ fields and register it |
| 624 | // in class_table_. |
| 625 | CheckSystemClass(self, java_lang_Class, "Ljava/lang/Class;"); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 626 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 627 | CheckSystemClass(self, class_array_class, "[Ljava/lang/Class;"); |
| 628 | CheckSystemClass(self, object_array_class, "[Ljava/lang/Object;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 629 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 630 | // Setup the single, global copy of "iftable". |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 631 | auto java_lang_Cloneable = hs.NewHandle(FindSystemClass(self, "Ljava/lang/Cloneable;")); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 632 | CHECK(java_lang_Cloneable != nullptr); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 633 | auto java_io_Serializable = hs.NewHandle(FindSystemClass(self, "Ljava/io/Serializable;")); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 634 | CHECK(java_io_Serializable != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 635 | // We assume that Cloneable/Serializable don't have superinterfaces -- normally we'd have to |
| 636 | // crawl up and explicitly list all of the supers as well. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 637 | array_iftable_.Read()->SetInterface(0, java_lang_Cloneable.Get()); |
| 638 | array_iftable_.Read()->SetInterface(1, java_io_Serializable.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 639 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 640 | // Sanity check Class[] and Object[]'s interfaces. GetDirectInterface may cause thread |
| 641 | // suspension. |
| 642 | CHECK_EQ(java_lang_Cloneable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 643 | mirror::Class::GetDirectInterface(self, class_array_class.Get(), 0)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 644 | CHECK_EQ(java_io_Serializable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 645 | mirror::Class::GetDirectInterface(self, class_array_class.Get(), 1)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 646 | CHECK_EQ(java_lang_Cloneable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 647 | mirror::Class::GetDirectInterface(self, object_array_class.Get(), 0)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 648 | CHECK_EQ(java_io_Serializable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 649 | mirror::Class::GetDirectInterface(self, object_array_class.Get(), 1)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 650 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 651 | CHECK_EQ(object_array_string.Get(), |
| 652 | FindSystemClass(self, GetClassRootDescriptor(kJavaLangStringArrayClass))); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 653 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 654 | // End of special init trickery, all subsequent classes may be loaded via FindSystemClass. |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 655 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 656 | // Create java.lang.reflect.Proxy root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 657 | SetClassRoot(kJavaLangReflectProxy, FindSystemClass(self, "Ljava/lang/reflect/Proxy;")); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 658 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 659 | // Create java.lang.reflect.Field.class root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 660 | auto* class_root = FindSystemClass(self, "Ljava/lang/reflect/Field;"); |
| 661 | CHECK(class_root != nullptr); |
| 662 | SetClassRoot(kJavaLangReflectField, class_root); |
| 663 | mirror::Field::SetClass(class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 664 | |
| 665 | // Create java.lang.reflect.Field array root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 666 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Field;"); |
| 667 | CHECK(class_root != nullptr); |
| 668 | SetClassRoot(kJavaLangReflectFieldArrayClass, class_root); |
| 669 | mirror::Field::SetArrayClass(class_root); |
| 670 | |
| 671 | // Create java.lang.reflect.Constructor.class root and array root. |
| 672 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Constructor;"); |
| 673 | CHECK(class_root != nullptr); |
| 674 | SetClassRoot(kJavaLangReflectConstructor, class_root); |
| 675 | mirror::Constructor::SetClass(class_root); |
| 676 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Constructor;"); |
| 677 | CHECK(class_root != nullptr); |
| 678 | SetClassRoot(kJavaLangReflectConstructorArrayClass, class_root); |
| 679 | mirror::Constructor::SetArrayClass(class_root); |
| 680 | |
| 681 | // Create java.lang.reflect.Method.class root and array root. |
| 682 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Method;"); |
| 683 | CHECK(class_root != nullptr); |
| 684 | SetClassRoot(kJavaLangReflectMethod, class_root); |
| 685 | mirror::Method::SetClass(class_root); |
| 686 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Method;"); |
| 687 | CHECK(class_root != nullptr); |
| 688 | SetClassRoot(kJavaLangReflectMethodArrayClass, class_root); |
| 689 | mirror::Method::SetArrayClass(class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 690 | |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 691 | // Create java.lang.invoke.MethodType.class root |
| 692 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodType;"); |
| 693 | CHECK(class_root != nullptr); |
| 694 | SetClassRoot(kJavaLangInvokeMethodType, class_root); |
| 695 | mirror::MethodType::SetClass(class_root); |
| 696 | |
| 697 | // Create java.lang.invoke.MethodHandleImpl.class root |
| 698 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandleImpl;"); |
| 699 | CHECK(class_root != nullptr); |
| 700 | SetClassRoot(kJavaLangInvokeMethodHandleImpl, class_root); |
| 701 | mirror::MethodHandleImpl::SetClass(class_root); |
| 702 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 703 | // Create java.lang.invoke.MethodHandles.Lookup.class root |
| 704 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandles$Lookup;"); |
| 705 | CHECK(class_root != nullptr); |
| 706 | SetClassRoot(kJavaLangInvokeMethodHandlesLookup, class_root); |
| 707 | mirror::MethodHandlesLookup::SetClass(class_root); |
| 708 | |
| 709 | // Create java.lang.invoke.CallSite.class root |
| 710 | class_root = FindSystemClass(self, "Ljava/lang/invoke/CallSite;"); |
| 711 | CHECK(class_root != nullptr); |
| 712 | SetClassRoot(kJavaLangInvokeCallSite, class_root); |
| 713 | mirror::CallSite::SetClass(class_root); |
| 714 | |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 715 | class_root = FindSystemClass(self, "Ldalvik/system/EmulatedStackFrame;"); |
| 716 | CHECK(class_root != nullptr); |
| 717 | SetClassRoot(kDalvikSystemEmulatedStackFrame, class_root); |
| 718 | mirror::EmulatedStackFrame::SetClass(class_root); |
| 719 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 720 | // java.lang.ref classes need to be specially flagged, but otherwise are normal classes |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 721 | // finish initializing Reference class |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 722 | mirror::Class::SetStatus(java_lang_ref_Reference, mirror::Class::kStatusNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 723 | CheckSystemClass(self, java_lang_ref_Reference, "Ljava/lang/ref/Reference;"); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 724 | CHECK_EQ(java_lang_ref_Reference->GetObjectSize(), mirror::Reference::InstanceSize()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 725 | CHECK_EQ(java_lang_ref_Reference->GetClassSize(), |
| 726 | mirror::Reference::ClassSize(image_pointer_size_)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 727 | class_root = FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 728 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 729 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagFinalizerReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 730 | class_root = FindSystemClass(self, "Ljava/lang/ref/PhantomReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 731 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 732 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagPhantomReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 733 | class_root = FindSystemClass(self, "Ljava/lang/ref/SoftReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 734 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 735 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagSoftReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 736 | class_root = FindSystemClass(self, "Ljava/lang/ref/WeakReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 737 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 738 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagWeakReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 739 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 740 | // Setup the ClassLoader, verifying the object_size_. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 741 | class_root = FindSystemClass(self, "Ljava/lang/ClassLoader;"); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 742 | class_root->SetClassLoaderClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 743 | CHECK_EQ(class_root->GetObjectSize(), mirror::ClassLoader::InstanceSize()); |
| 744 | SetClassRoot(kJavaLangClassLoader, class_root); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 745 | |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 746 | // Set up java.lang.Throwable, java.lang.ClassNotFoundException, and |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 747 | // java.lang.StackTraceElement as a convenience. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 748 | SetClassRoot(kJavaLangThrowable, FindSystemClass(self, "Ljava/lang/Throwable;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 749 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 750 | SetClassRoot(kJavaLangClassNotFoundException, |
| 751 | FindSystemClass(self, "Ljava/lang/ClassNotFoundException;")); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 752 | SetClassRoot(kJavaLangStackTraceElement, FindSystemClass(self, "Ljava/lang/StackTraceElement;")); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 753 | SetClassRoot(kJavaLangStackTraceElementArrayClass, |
| 754 | FindSystemClass(self, "[Ljava/lang/StackTraceElement;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 755 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 756 | |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 757 | // Create conflict tables that depend on the class linker. |
| 758 | runtime->FixupConflictTables(); |
| 759 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 760 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 761 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 762 | VLOG(startup) << "ClassLinker::InitFromCompiler exiting"; |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 763 | |
| 764 | return true; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 765 | } |
| 766 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 767 | void ClassLinker::FinishInit(Thread* self) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 768 | VLOG(startup) << "ClassLinker::FinishInit entering"; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 769 | |
| 770 | // Let the heap know some key offsets into java.lang.ref instances |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 771 | // Note: we hard code the field indexes here rather than using FindInstanceField |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 772 | // as the types of the field can't be resolved prior to the runtime being |
| 773 | // fully initialized |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 774 | StackHandleScope<2> hs(self); |
| 775 | Handle<mirror::Class> java_lang_ref_Reference = hs.NewHandle(GetClassRoot(kJavaLangRefReference)); |
| 776 | Handle<mirror::Class> java_lang_ref_FinalizerReference = |
| 777 | hs.NewHandle(FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;")); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 778 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 779 | ArtField* pendingNext = java_lang_ref_Reference->GetInstanceField(0); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 780 | CHECK_STREQ(pendingNext->GetName(), "pendingNext"); |
| 781 | CHECK_STREQ(pendingNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 782 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 783 | ArtField* queue = java_lang_ref_Reference->GetInstanceField(1); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 784 | CHECK_STREQ(queue->GetName(), "queue"); |
| 785 | CHECK_STREQ(queue->GetTypeDescriptor(), "Ljava/lang/ref/ReferenceQueue;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 786 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 787 | ArtField* queueNext = java_lang_ref_Reference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 788 | CHECK_STREQ(queueNext->GetName(), "queueNext"); |
| 789 | CHECK_STREQ(queueNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 790 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 791 | ArtField* referent = java_lang_ref_Reference->GetInstanceField(3); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 792 | CHECK_STREQ(referent->GetName(), "referent"); |
| 793 | CHECK_STREQ(referent->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 794 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 795 | ArtField* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 796 | CHECK_STREQ(zombie->GetName(), "zombie"); |
| 797 | CHECK_STREQ(zombie->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 798 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 799 | // ensure all class_roots_ are initialized |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 800 | for (size_t i = 0; i < kClassRootsMax; i++) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 801 | ClassRoot class_root = static_cast<ClassRoot>(i); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 802 | ObjPtr<mirror::Class> klass = GetClassRoot(class_root); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 803 | CHECK(klass != nullptr); |
| 804 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != nullptr); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 805 | // note SetClassRoot does additional validation. |
| 806 | // if possible add new checks there to catch errors early |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 809 | CHECK(!array_iftable_.IsNull()); |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 810 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 811 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 812 | // that Object, Class, and Object[] are setup |
| 813 | init_done_ = true; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 814 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 815 | VLOG(startup) << "ClassLinker::FinishInit exiting"; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 818 | void ClassLinker::RunRootClinits() { |
| 819 | Thread* self = Thread::Current(); |
| 820 | for (size_t i = 0; i < ClassLinker::kClassRootsMax; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 821 | ObjPtr<mirror::Class> c = GetClassRoot(ClassRoot(i)); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 822 | if (!c->IsArrayClass() && !c->IsPrimitive()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 823 | StackHandleScope<1> hs(self); |
| 824 | Handle<mirror::Class> h_class(hs.NewHandle(GetClassRoot(ClassRoot(i)))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 825 | EnsureInitialized(self, h_class, true, true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 826 | self->AssertNoPendingException(); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 827 | } |
| 828 | } |
| 829 | } |
| 830 | |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 831 | // Set image methods' entry point to interpreter. |
| 832 | class SetInterpreterEntrypointArtMethodVisitor : public ArtMethodVisitor { |
| 833 | public: |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 834 | explicit SetInterpreterEntrypointArtMethodVisitor(PointerSize image_pointer_size) |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 835 | : image_pointer_size_(image_pointer_size) {} |
| 836 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 837 | void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 838 | if (kIsDebugBuild && !method->IsRuntimeMethod()) { |
| 839 | CHECK(method->GetDeclaringClass() != nullptr); |
| 840 | } |
| 841 | if (!method->IsNative() && !method->IsRuntimeMethod() && !method->IsResolutionMethod()) { |
| 842 | method->SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(), |
| 843 | image_pointer_size_); |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | private: |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 848 | const PointerSize image_pointer_size_; |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 849 | |
| 850 | DISALLOW_COPY_AND_ASSIGN(SetInterpreterEntrypointArtMethodVisitor); |
| 851 | }; |
| 852 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 853 | struct TrampolineCheckData { |
| 854 | const void* quick_resolution_trampoline; |
| 855 | const void* quick_imt_conflict_trampoline; |
| 856 | const void* quick_generic_jni_trampoline; |
| 857 | const void* quick_to_interpreter_bridge_trampoline; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 858 | PointerSize pointer_size; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 859 | ArtMethod* m; |
| 860 | bool error; |
| 861 | }; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 862 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 863 | static void CheckTrampolines(mirror::Object* obj, void* arg) NO_THREAD_SAFETY_ANALYSIS { |
| 864 | if (obj->IsClass()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 865 | ObjPtr<mirror::Class> klass = obj->AsClass(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 866 | TrampolineCheckData* d = reinterpret_cast<TrampolineCheckData*>(arg); |
| 867 | for (ArtMethod& m : klass->GetMethods(d->pointer_size)) { |
| 868 | const void* entrypoint = m.GetEntryPointFromQuickCompiledCodePtrSize(d->pointer_size); |
| 869 | if (entrypoint == d->quick_resolution_trampoline || |
| 870 | entrypoint == d->quick_imt_conflict_trampoline || |
| 871 | entrypoint == d->quick_generic_jni_trampoline || |
| 872 | entrypoint == d->quick_to_interpreter_bridge_trampoline) { |
| 873 | d->m = &m; |
| 874 | d->error = true; |
| 875 | return; |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 881 | bool ClassLinker::InitFromBootImage(std::string* error_msg) { |
| 882 | VLOG(startup) << __FUNCTION__ << " entering"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 883 | CHECK(!init_done_); |
| 884 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 885 | Runtime* const runtime = Runtime::Current(); |
| 886 | Thread* const self = Thread::Current(); |
| 887 | gc::Heap* const heap = runtime->GetHeap(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 888 | std::vector<gc::space::ImageSpace*> spaces = heap->GetBootImageSpaces(); |
| 889 | CHECK(!spaces.empty()); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 890 | uint32_t pointer_size_unchecked = spaces[0]->GetImageHeader().GetPointerSizeUnchecked(); |
| 891 | if (!ValidPointerSize(pointer_size_unchecked)) { |
| 892 | *error_msg = StringPrintf("Invalid image pointer size: %u", pointer_size_unchecked); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 893 | return false; |
| 894 | } |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 895 | image_pointer_size_ = spaces[0]->GetImageHeader().GetPointerSize(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 896 | if (!runtime->IsAotCompiler()) { |
| 897 | // Only the Aot compiler supports having an image with a different pointer size than the |
| 898 | // runtime. This happens on the host for compiling 32 bit tests since we use a 64 bit libart |
| 899 | // compiler. We may also use 32 bit dex2oat on a system with 64 bit apps. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 900 | if (image_pointer_size_ != kRuntimePointerSize) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 901 | *error_msg = StringPrintf("Runtime must use current image pointer size: %zu vs %zu", |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 902 | static_cast<size_t>(image_pointer_size_), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 903 | sizeof(void*)); |
| 904 | return false; |
| 905 | } |
| 906 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 907 | std::vector<const OatFile*> oat_files = |
| 908 | runtime->GetOatFileManager().RegisterImageOatFiles(spaces); |
| 909 | DCHECK(!oat_files.empty()); |
| 910 | const OatHeader& default_oat_header = oat_files[0]->GetOatHeader(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 911 | CHECK_EQ(default_oat_header.GetImageFileLocationOatDataBegin(), 0U); |
| 912 | const char* image_file_location = oat_files[0]->GetOatHeader(). |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 913 | GetStoreValueByKey(OatHeader::kImageLocationKey); |
| 914 | CHECK(image_file_location == nullptr || *image_file_location == 0); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 915 | quick_resolution_trampoline_ = default_oat_header.GetQuickResolutionTrampoline(); |
| 916 | quick_imt_conflict_trampoline_ = default_oat_header.GetQuickImtConflictTrampoline(); |
| 917 | quick_generic_jni_trampoline_ = default_oat_header.GetQuickGenericJniTrampoline(); |
| 918 | quick_to_interpreter_bridge_trampoline_ = default_oat_header.GetQuickToInterpreterBridge(); |
| 919 | if (kIsDebugBuild) { |
| 920 | // Check that the other images use the same trampoline. |
| 921 | for (size_t i = 1; i < oat_files.size(); ++i) { |
| 922 | const OatHeader& ith_oat_header = oat_files[i]->GetOatHeader(); |
| 923 | const void* ith_quick_resolution_trampoline = |
| 924 | ith_oat_header.GetQuickResolutionTrampoline(); |
| 925 | const void* ith_quick_imt_conflict_trampoline = |
| 926 | ith_oat_header.GetQuickImtConflictTrampoline(); |
| 927 | const void* ith_quick_generic_jni_trampoline = |
| 928 | ith_oat_header.GetQuickGenericJniTrampoline(); |
| 929 | const void* ith_quick_to_interpreter_bridge_trampoline = |
| 930 | ith_oat_header.GetQuickToInterpreterBridge(); |
| 931 | if (ith_quick_resolution_trampoline != quick_resolution_trampoline_ || |
| 932 | ith_quick_imt_conflict_trampoline != quick_imt_conflict_trampoline_ || |
| 933 | ith_quick_generic_jni_trampoline != quick_generic_jni_trampoline_ || |
| 934 | ith_quick_to_interpreter_bridge_trampoline != quick_to_interpreter_bridge_trampoline_) { |
| 935 | // Make sure that all methods in this image do not contain those trampolines as |
| 936 | // entrypoints. Otherwise the class-linker won't be able to work with a single set. |
| 937 | TrampolineCheckData data; |
| 938 | data.error = false; |
| 939 | data.pointer_size = GetImagePointerSize(); |
| 940 | data.quick_resolution_trampoline = ith_quick_resolution_trampoline; |
| 941 | data.quick_imt_conflict_trampoline = ith_quick_imt_conflict_trampoline; |
| 942 | data.quick_generic_jni_trampoline = ith_quick_generic_jni_trampoline; |
| 943 | data.quick_to_interpreter_bridge_trampoline = ith_quick_to_interpreter_bridge_trampoline; |
| 944 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 945 | spaces[i]->GetLiveBitmap()->Walk(CheckTrampolines, &data); |
| 946 | if (data.error) { |
| 947 | ArtMethod* m = data.m; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 948 | LOG(ERROR) << "Found a broken ArtMethod: " << ArtMethod::PrettyMethod(m); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 949 | *error_msg = "Found an ArtMethod with a bad entrypoint"; |
| 950 | return false; |
| 951 | } |
| 952 | } |
| 953 | } |
| 954 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 955 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 956 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
| 957 | down_cast<mirror::ObjectArray<mirror::Class>*>( |
| 958 | spaces[0]->GetImageHeader().GetImageRoot(ImageHeader::kClassRoots))); |
| 959 | mirror::Class::SetClassClass(class_roots_.Read()->Get(kJavaLangClass)); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 960 | |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 961 | // Special case of setting up the String class early so that we can test arbitrary objects |
| 962 | // as being Strings or not |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 963 | mirror::String::SetClass(GetClassRoot(kJavaLangString)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 964 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 965 | ObjPtr<mirror::Class> java_lang_Object = GetClassRoot(kJavaLangObject); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 966 | java_lang_Object->SetObjectSize(sizeof(mirror::Object)); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 967 | // Allocate in non-movable so that it's possible to check if a JNI weak global ref has been |
| 968 | // cleared without triggering the read barrier and unintentionally mark the sentinel alive. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 969 | runtime->SetSentinel(heap->AllocNonMovableObject<true>( |
| 970 | self, java_lang_Object, java_lang_Object->GetObjectSize(), VoidFunctor())); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 971 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 972 | // reinit array_iftable_ from any array class instance, they should be == |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 973 | array_iftable_ = GcRoot<mirror::IfTable>(GetClassRoot(kObjectArrayClass)->GetIfTable()); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 974 | DCHECK_EQ(array_iftable_.Read(), GetClassRoot(kBooleanArrayClass)->GetIfTable()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 975 | // String class root was set above |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 976 | mirror::Field::SetClass(GetClassRoot(kJavaLangReflectField)); |
| 977 | mirror::Field::SetArrayClass(GetClassRoot(kJavaLangReflectFieldArrayClass)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 978 | mirror::Constructor::SetClass(GetClassRoot(kJavaLangReflectConstructor)); |
| 979 | mirror::Constructor::SetArrayClass(GetClassRoot(kJavaLangReflectConstructorArrayClass)); |
| 980 | mirror::Method::SetClass(GetClassRoot(kJavaLangReflectMethod)); |
| 981 | mirror::Method::SetArrayClass(GetClassRoot(kJavaLangReflectMethodArrayClass)); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 982 | mirror::MethodType::SetClass(GetClassRoot(kJavaLangInvokeMethodType)); |
| 983 | mirror::MethodHandleImpl::SetClass(GetClassRoot(kJavaLangInvokeMethodHandleImpl)); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 984 | mirror::MethodHandlesLookup::SetClass(GetClassRoot(kJavaLangInvokeMethodHandlesLookup)); |
| 985 | mirror::CallSite::SetClass(GetClassRoot(kJavaLangInvokeCallSite)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 986 | mirror::Reference::SetClass(GetClassRoot(kJavaLangRefReference)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 987 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
| 988 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
| 989 | mirror::CharArray::SetArrayClass(GetClassRoot(kCharArrayClass)); |
| 990 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
| 991 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
| 992 | mirror::IntArray::SetArrayClass(GetClassRoot(kIntArrayClass)); |
| 993 | mirror::LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
| 994 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
| 995 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
| 996 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 997 | mirror::EmulatedStackFrame::SetClass(GetClassRoot(kDalvikSystemEmulatedStackFrame)); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 998 | mirror::ClassExt::SetClass(GetClassRoot(kDalvikSystemClassExt)); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 999 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1000 | for (gc::space::ImageSpace* image_space : spaces) { |
| 1001 | // Boot class loader, use a null handle. |
| 1002 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1003 | if (!AddImageSpace(image_space, |
| 1004 | ScopedNullHandle<mirror::ClassLoader>(), |
| 1005 | /*dex_elements*/nullptr, |
| 1006 | /*dex_location*/nullptr, |
| 1007 | /*out*/&dex_files, |
| 1008 | error_msg)) { |
| 1009 | return false; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1010 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1011 | // Append opened dex files at the end. |
| 1012 | boot_dex_files_.insert(boot_dex_files_.end(), |
| 1013 | std::make_move_iterator(dex_files.begin()), |
| 1014 | std::make_move_iterator(dex_files.end())); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 1015 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1016 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1017 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1018 | VLOG(startup) << __FUNCTION__ << " exiting"; |
| 1019 | return true; |
| 1020 | } |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 1021 | |
Jeff Hao | 5872d7c | 2016-04-27 11:07:41 -0700 | [diff] [blame] | 1022 | bool ClassLinker::IsBootClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1023 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1024 | return class_loader == nullptr || |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1025 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader) == |
| 1026 | class_loader->GetClass(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1027 | } |
| 1028 | |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1029 | static bool GetDexPathListElementName(ObjPtr<mirror::Object> element, |
| 1030 | ObjPtr<mirror::String>* out_name) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1031 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1032 | ArtField* const dex_file_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 1033 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1034 | ArtField* const dex_file_name_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 1035 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_fileName); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1036 | DCHECK(dex_file_field != nullptr); |
| 1037 | DCHECK(dex_file_name_field != nullptr); |
| 1038 | DCHECK(element != nullptr); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1039 | CHECK_EQ(dex_file_field->GetDeclaringClass(), element->GetClass()) << element->PrettyTypeOf(); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1040 | ObjPtr<mirror::Object> dex_file = dex_file_field->GetObject(element); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1041 | if (dex_file == nullptr) { |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1042 | // Null dex file means it was probably a jar with no dex files, return a null string. |
| 1043 | *out_name = nullptr; |
| 1044 | return true; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1045 | } |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1046 | ObjPtr<mirror::Object> name_object = dex_file_name_field->GetObject(dex_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1047 | if (name_object != nullptr) { |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1048 | *out_name = name_object->AsString(); |
| 1049 | return true; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1050 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1051 | return false; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1052 | } |
| 1053 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1054 | static bool FlattenPathClassLoader(ObjPtr<mirror::ClassLoader> class_loader, |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1055 | std::list<ObjPtr<mirror::String>>* out_dex_file_names, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1056 | std::string* error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1057 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1058 | DCHECK(out_dex_file_names != nullptr); |
| 1059 | DCHECK(error_msg != nullptr); |
| 1060 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1061 | ArtField* const dex_path_list_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 1062 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1063 | ArtField* const dex_elements_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 1064 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1065 | CHECK(dex_path_list_field != nullptr); |
| 1066 | CHECK(dex_elements_field != nullptr); |
Jeff Hao | 5872d7c | 2016-04-27 11:07:41 -0700 | [diff] [blame] | 1067 | while (!ClassLinker::IsBootClassLoader(soa, class_loader)) { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1068 | if (soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader) != |
| 1069 | class_loader->GetClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1070 | *error_msg = StringPrintf("Unknown class loader type %s", |
| 1071 | class_loader->PrettyTypeOf().c_str()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1072 | // Unsupported class loader. |
| 1073 | return false; |
| 1074 | } |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1075 | ObjPtr<mirror::Object> dex_path_list = dex_path_list_field->GetObject(class_loader); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1076 | if (dex_path_list != nullptr) { |
| 1077 | // DexPathList has an array dexElements of Elements[] which each contain a dex file. |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1078 | ObjPtr<mirror::Object> dex_elements_obj = dex_elements_field->GetObject(dex_path_list); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1079 | // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look |
| 1080 | // at the mCookie which is a DexFile vector. |
| 1081 | if (dex_elements_obj != nullptr) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1082 | ObjPtr<mirror::ObjectArray<mirror::Object>> dex_elements = |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1083 | dex_elements_obj->AsObjectArray<mirror::Object>(); |
| 1084 | // Reverse order since we insert the parent at the front. |
| 1085 | for (int32_t i = dex_elements->GetLength() - 1; i >= 0; --i) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1086 | ObjPtr<mirror::Object> element = dex_elements->GetWithoutChecks(i); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1087 | if (element == nullptr) { |
| 1088 | *error_msg = StringPrintf("Null dex element at index %d", i); |
| 1089 | return false; |
| 1090 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1091 | ObjPtr<mirror::String> name; |
| 1092 | if (!GetDexPathListElementName(element, &name)) { |
| 1093 | *error_msg = StringPrintf("Invalid dex path list element at index %d", i); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1094 | return false; |
| 1095 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1096 | if (name != nullptr) { |
| 1097 | out_dex_file_names->push_front(name.Ptr()); |
| 1098 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | class_loader = class_loader->GetParent(); |
| 1103 | } |
| 1104 | return true; |
| 1105 | } |
| 1106 | |
| 1107 | class FixupArtMethodArrayVisitor : public ArtMethodVisitor { |
| 1108 | public: |
| 1109 | explicit FixupArtMethodArrayVisitor(const ImageHeader& header) : header_(header) {} |
| 1110 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1111 | virtual void Visit(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 1112 | const bool is_copied = method->IsCopied(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1113 | ArtMethod** resolved_methods = method->GetDexCacheResolvedMethods(kRuntimePointerSize); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1114 | if (resolved_methods != nullptr) { |
| 1115 | bool in_image_space = false; |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 1116 | if (kIsDebugBuild || is_copied) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1117 | in_image_space = header_.GetImageSection(ImageHeader::kSectionDexCacheArrays).Contains( |
| 1118 | reinterpret_cast<const uint8_t*>(resolved_methods) - header_.GetImageBegin()); |
| 1119 | } |
| 1120 | // Must be in image space for non-miranda method. |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 1121 | DCHECK(is_copied || in_image_space) |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1122 | << resolved_methods << " is not in image starting at " |
| 1123 | << reinterpret_cast<void*>(header_.GetImageBegin()); |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 1124 | if (!is_copied || in_image_space) { |
Vladimir Marko | 6dd1488 | 2016-10-25 11:51:35 +0100 | [diff] [blame] | 1125 | method->SetDexCacheResolvedMethods(method->GetDexCache()->GetResolvedMethods(), |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1126 | kRuntimePointerSize); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | private: |
| 1132 | const ImageHeader& header_; |
| 1133 | }; |
| 1134 | |
| 1135 | class VerifyClassInTableArtMethodVisitor : public ArtMethodVisitor { |
| 1136 | public: |
| 1137 | explicit VerifyClassInTableArtMethodVisitor(ClassTable* table) : table_(table) {} |
| 1138 | |
| 1139 | virtual void Visit(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1140 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::classlinker_classes_lock_) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1141 | ObjPtr<mirror::Class> klass = method->GetDeclaringClass(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1142 | if (klass != nullptr && !Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1143 | CHECK_EQ(table_->LookupByDescriptor(klass), klass) << mirror::Class::PrettyClass(klass); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | private: |
| 1148 | ClassTable* const table_; |
| 1149 | }; |
| 1150 | |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1151 | class VerifyDirectInterfacesInTableClassVisitor { |
| 1152 | public: |
| 1153 | explicit VerifyDirectInterfacesInTableClassVisitor(ObjPtr<mirror::ClassLoader> class_loader) |
| 1154 | : class_loader_(class_loader), self_(Thread::Current()) { } |
| 1155 | |
| 1156 | bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1157 | if (!klass->IsPrimitive() && klass->GetClassLoader() == class_loader_) { |
| 1158 | classes_.push_back(klass); |
| 1159 | } |
| 1160 | return true; |
| 1161 | } |
| 1162 | |
| 1163 | void Check() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1164 | for (ObjPtr<mirror::Class> klass : classes_) { |
| 1165 | for (uint32_t i = 0, num = klass->NumDirectInterfaces(); i != num; ++i) { |
| 1166 | CHECK(klass->GetDirectInterface(self_, klass, i) != nullptr) |
| 1167 | << klass->PrettyDescriptor() << " iface #" << i; |
| 1168 | } |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | private: |
| 1173 | ObjPtr<mirror::ClassLoader> class_loader_; |
| 1174 | Thread* self_; |
| 1175 | std::vector<ObjPtr<mirror::Class>> classes_; |
| 1176 | }; |
| 1177 | |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1178 | class VerifyDeclaringClassVisitor : public ArtMethodVisitor { |
| 1179 | public: |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1180 | VerifyDeclaringClassVisitor() REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1181 | : live_bitmap_(Runtime::Current()->GetHeap()->GetLiveBitmap()) {} |
| 1182 | |
| 1183 | virtual void Visit(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1184 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1185 | ObjPtr<mirror::Class> klass = method->GetDeclaringClassUnchecked(); |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1186 | if (klass != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1187 | CHECK(live_bitmap_->Test(klass.Ptr())) << "Image method has unmarked declaring class"; |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | private: |
| 1192 | gc::accounting::HeapBitmap* const live_bitmap_; |
| 1193 | }; |
| 1194 | |
Artem Udovichenko | b3ac45b | 2016-09-09 14:02:25 +0300 | [diff] [blame] | 1195 | // Copies data from one array to another array at the same position |
| 1196 | // if pred returns false. If there is a page of continuous data in |
| 1197 | // the src array for which pred consistently returns true then |
| 1198 | // corresponding page in the dst array will not be touched. |
| 1199 | // This should reduce number of allocated physical pages. |
| 1200 | template <class T, class NullPred> |
| 1201 | static void CopyNonNull(const T* src, size_t count, T* dst, const NullPred& pred) { |
| 1202 | for (size_t i = 0; i < count; ++i) { |
| 1203 | if (!pred(src[i])) { |
| 1204 | dst[i] = src[i]; |
| 1205 | } |
| 1206 | } |
| 1207 | } |
| 1208 | |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1209 | template <typename T> |
| 1210 | static void CopyDexCachePairs(const std::atomic<mirror::DexCachePair<T>>* src, |
| 1211 | size_t count, |
| 1212 | std::atomic<mirror::DexCachePair<T>>* dst) { |
| 1213 | DCHECK_NE(count, 0u); |
| 1214 | DCHECK(!src[0].load(std::memory_order_relaxed).object.IsNull() || |
| 1215 | src[0].load(std::memory_order_relaxed).index != 0u); |
| 1216 | for (size_t i = 0; i < count; ++i) { |
| 1217 | DCHECK_EQ(dst[i].load(std::memory_order_relaxed).index, 0u); |
| 1218 | DCHECK(dst[i].load(std::memory_order_relaxed).object.IsNull()); |
| 1219 | mirror::DexCachePair<T> source = src[i].load(std::memory_order_relaxed); |
| 1220 | if (source.index != 0u || !source.object.IsNull()) { |
| 1221 | dst[i].store(source, std::memory_order_relaxed); |
| 1222 | } |
| 1223 | } |
| 1224 | } |
| 1225 | |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame^] | 1226 | // new_class_set is the set of classes that were read from the class table section in the image. |
| 1227 | // If there was no class table section, it is null. |
| 1228 | // Note: using a class here to avoid having to make ClassLinker internals public. |
| 1229 | class AppImageClassLoadersAndDexCachesHelper { |
| 1230 | public: |
| 1231 | static bool Update( |
| 1232 | ClassLinker* class_linker, |
| 1233 | gc::space::ImageSpace* space, |
| 1234 | Handle<mirror::ClassLoader> class_loader, |
| 1235 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches, |
| 1236 | ClassTable::ClassSet* new_class_set, |
| 1237 | bool* out_forward_dex_cache_array, |
| 1238 | std::string* out_error_msg) |
| 1239 | REQUIRES(!Locks::dex_lock_) |
| 1240 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 1241 | }; |
| 1242 | |
| 1243 | bool AppImageClassLoadersAndDexCachesHelper::Update( |
| 1244 | ClassLinker* class_linker, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1245 | gc::space::ImageSpace* space, |
| 1246 | Handle<mirror::ClassLoader> class_loader, |
| 1247 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches, |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1248 | ClassTable::ClassSet* new_class_set, |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1249 | bool* out_forward_dex_cache_array, |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame^] | 1250 | std::string* out_error_msg) |
| 1251 | REQUIRES(!Locks::dex_lock_) |
| 1252 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1253 | DCHECK(out_forward_dex_cache_array != nullptr); |
| 1254 | DCHECK(out_error_msg != nullptr); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame^] | 1255 | PointerSize image_pointer_size = class_linker->GetImagePointerSize(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1256 | Thread* const self = Thread::Current(); |
| 1257 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
| 1258 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1259 | { |
| 1260 | // Add image classes into the class table for the class loader, and fixup the dex caches and |
| 1261 | // class loader fields. |
| 1262 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1263 | // Dex cache array fixup is all or nothing, we must reject app images that have mixed since we |
| 1264 | // rely on clobering the dex cache arrays in the image to forward to bss. |
| 1265 | size_t num_dex_caches_with_bss_arrays = 0; |
| 1266 | const size_t num_dex_caches = dex_caches->GetLength(); |
| 1267 | for (size_t i = 0; i < num_dex_caches; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1268 | ObjPtr<mirror::DexCache> const dex_cache = dex_caches->Get(i); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1269 | const DexFile* const dex_file = dex_cache->GetDexFile(); |
| 1270 | const OatFile::OatDexFile* oat_dex_file = dex_file->GetOatDexFile(); |
| 1271 | if (oat_dex_file != nullptr && oat_dex_file->GetDexCacheArrays() != nullptr) { |
| 1272 | ++num_dex_caches_with_bss_arrays; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1273 | } |
| 1274 | } |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1275 | *out_forward_dex_cache_array = num_dex_caches_with_bss_arrays != 0; |
| 1276 | if (*out_forward_dex_cache_array) { |
| 1277 | if (num_dex_caches_with_bss_arrays != num_dex_caches) { |
| 1278 | // Reject application image since we cannot forward only some of the dex cache arrays. |
| 1279 | // TODO: We could get around this by having a dedicated forwarding slot. It should be an |
| 1280 | // uncommon case. |
| 1281 | *out_error_msg = StringPrintf("Dex caches in bss does not match total: %zu vs %zu", |
| 1282 | num_dex_caches_with_bss_arrays, |
| 1283 | num_dex_caches); |
| 1284 | return false; |
| 1285 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1286 | } |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1287 | // Only add the classes to the class loader after the points where we can return false. |
| 1288 | for (size_t i = 0; i < num_dex_caches; i++) { |
Vladimir Marko | 1bc4b17 | 2016-10-24 16:53:39 +0000 | [diff] [blame] | 1289 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1290 | const DexFile* const dex_file = dex_cache->GetDexFile(); |
| 1291 | const OatFile::OatDexFile* oat_dex_file = dex_file->GetOatDexFile(); |
| 1292 | if (oat_dex_file != nullptr && oat_dex_file->GetDexCacheArrays() != nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1293 | // If the oat file expects the dex cache arrays to be in the BSS, then allocate there and |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1294 | // copy over the arrays. |
| 1295 | DCHECK(dex_file != nullptr); |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 1296 | size_t num_strings = mirror::DexCache::kDexCacheStringCacheSize; |
| 1297 | if (dex_file->NumStringIds() < num_strings) { |
| 1298 | num_strings = dex_file->NumStringIds(); |
| 1299 | } |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1300 | size_t num_types = mirror::DexCache::kDexCacheTypeCacheSize; |
| 1301 | if (dex_file->NumTypeIds() < num_types) { |
| 1302 | num_types = dex_file->NumTypeIds(); |
| 1303 | } |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1304 | const size_t num_methods = dex_file->NumMethodIds(); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1305 | size_t num_fields = mirror::DexCache::kDexCacheFieldCacheSize; |
| 1306 | if (dex_file->NumFieldIds() < num_fields) { |
| 1307 | num_fields = dex_file->NumFieldIds(); |
| 1308 | } |
Narayan Kamath | 269cb43 | 2016-10-28 10:19:54 +0100 | [diff] [blame] | 1309 | size_t num_method_types = mirror::DexCache::kDexCacheMethodTypeCacheSize; |
| 1310 | if (dex_file->NumProtoIds() < num_method_types) { |
| 1311 | num_method_types = dex_file->NumProtoIds(); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1312 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1313 | const size_t num_call_sites = dex_file->NumCallSiteIds(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1314 | CHECK_EQ(num_strings, dex_cache->NumStrings()); |
| 1315 | CHECK_EQ(num_types, dex_cache->NumResolvedTypes()); |
| 1316 | CHECK_EQ(num_methods, dex_cache->NumResolvedMethods()); |
| 1317 | CHECK_EQ(num_fields, dex_cache->NumResolvedFields()); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1318 | CHECK_EQ(num_method_types, dex_cache->NumResolvedMethodTypes()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1319 | CHECK_EQ(num_call_sites, dex_cache->NumResolvedCallSites()); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame^] | 1320 | DexCacheArraysLayout layout(image_pointer_size, dex_file); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1321 | uint8_t* const raw_arrays = oat_dex_file->GetDexCacheArrays(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1322 | if (num_strings != 0u) { |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 1323 | mirror::StringDexCacheType* const image_resolved_strings = dex_cache->GetStrings(); |
| 1324 | mirror::StringDexCacheType* const strings = |
| 1325 | reinterpret_cast<mirror::StringDexCacheType*>(raw_arrays + layout.StringsOffset()); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1326 | CopyDexCachePairs(image_resolved_strings, num_strings, strings); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1327 | dex_cache->SetStrings(strings); |
| 1328 | } |
| 1329 | if (num_types != 0u) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1330 | mirror::TypeDexCacheType* const image_resolved_types = dex_cache->GetResolvedTypes(); |
| 1331 | mirror::TypeDexCacheType* const types = |
| 1332 | reinterpret_cast<mirror::TypeDexCacheType*>(raw_arrays + layout.TypesOffset()); |
| 1333 | CopyDexCachePairs(image_resolved_types, num_types, types); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1334 | dex_cache->SetResolvedTypes(types); |
| 1335 | } |
| 1336 | if (num_methods != 0u) { |
| 1337 | ArtMethod** const methods = reinterpret_cast<ArtMethod**>( |
| 1338 | raw_arrays + layout.MethodsOffset()); |
| 1339 | ArtMethod** const image_resolved_methods = dex_cache->GetResolvedMethods(); |
| 1340 | for (size_t j = 0; kIsDebugBuild && j < num_methods; ++j) { |
| 1341 | DCHECK(methods[j] == nullptr); |
| 1342 | } |
Artem Udovichenko | b3ac45b | 2016-09-09 14:02:25 +0300 | [diff] [blame] | 1343 | CopyNonNull(image_resolved_methods, |
| 1344 | num_methods, |
| 1345 | methods, |
| 1346 | [] (const ArtMethod* method) { |
| 1347 | return method == nullptr; |
| 1348 | }); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1349 | dex_cache->SetResolvedMethods(methods); |
| 1350 | } |
| 1351 | if (num_fields != 0u) { |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1352 | mirror::FieldDexCacheType* const image_resolved_fields = dex_cache->GetResolvedFields(); |
| 1353 | mirror::FieldDexCacheType* const fields = |
| 1354 | reinterpret_cast<mirror::FieldDexCacheType*>(raw_arrays + layout.FieldsOffset()); |
| 1355 | for (size_t j = 0; j < num_fields; ++j) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame^] | 1356 | DCHECK_EQ(mirror::DexCache::GetNativePairPtrSize(fields, j, image_pointer_size).index, |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1357 | 0u); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame^] | 1358 | DCHECK(mirror::DexCache::GetNativePairPtrSize(fields, j, image_pointer_size).object == |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1359 | nullptr); |
| 1360 | mirror::DexCache::SetNativePairPtrSize( |
| 1361 | fields, |
| 1362 | j, |
| 1363 | mirror::DexCache::GetNativePairPtrSize(image_resolved_fields, |
| 1364 | j, |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame^] | 1365 | image_pointer_size), |
| 1366 | image_pointer_size); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1367 | } |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1368 | dex_cache->SetResolvedFields(fields); |
| 1369 | } |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1370 | if (num_method_types != 0u) { |
| 1371 | // NOTE: We currently (Sep 2016) do not resolve any method types at |
| 1372 | // compile time, but plan to in the future. This code exists for the |
| 1373 | // sake of completeness. |
| 1374 | mirror::MethodTypeDexCacheType* const image_resolved_method_types = |
| 1375 | dex_cache->GetResolvedMethodTypes(); |
| 1376 | mirror::MethodTypeDexCacheType* const method_types = |
| 1377 | reinterpret_cast<mirror::MethodTypeDexCacheType*>( |
| 1378 | raw_arrays + layout.MethodTypesOffset()); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1379 | CopyDexCachePairs(image_resolved_method_types, num_method_types, method_types); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1380 | dex_cache->SetResolvedMethodTypes(method_types); |
| 1381 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1382 | if (num_call_sites != 0u) { |
| 1383 | GcRoot<mirror::CallSite>* const image_resolved_call_sites = |
| 1384 | dex_cache->GetResolvedCallSites(); |
| 1385 | GcRoot<mirror::CallSite>* const call_sites = |
| 1386 | reinterpret_cast<GcRoot<mirror::CallSite>*>(raw_arrays + layout.CallSitesOffset()); |
| 1387 | for (size_t j = 0; kIsDebugBuild && j < num_call_sites; ++j) { |
| 1388 | DCHECK(call_sites[j].IsNull()); |
| 1389 | } |
| 1390 | CopyNonNull(image_resolved_call_sites, |
| 1391 | num_call_sites, |
| 1392 | call_sites, |
| 1393 | [](const GcRoot<mirror::CallSite>& elem) { |
| 1394 | return elem.IsNull(); |
| 1395 | }); |
| 1396 | dex_cache->SetResolvedCallSites(call_sites); |
| 1397 | } |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1398 | } |
| 1399 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 1400 | WriterMutexLock mu2(self, *Locks::dex_lock_); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1401 | // Make sure to do this after we update the arrays since we store the resolved types array |
| 1402 | // in DexCacheData in RegisterDexFileLocked. We need the array pointer to be the one in the |
| 1403 | // BSS. |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame^] | 1404 | CHECK(!class_linker->FindDexCacheDataLocked(*dex_file).IsValid()); |
| 1405 | class_linker->RegisterDexFileLocked(*dex_file, dex_cache, class_loader.Get()); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1406 | } |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1407 | if (kIsDebugBuild) { |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1408 | CHECK(new_class_set != nullptr); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1409 | mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes(); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1410 | const size_t num_types = dex_cache->NumResolvedTypes(); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1411 | for (size_t j = 0; j != num_types; ++j) { |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1412 | // The image space is not yet added to the heap, avoid read barriers. |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1413 | ObjPtr<mirror::Class> klass = types[j].load(std::memory_order_relaxed).object.Read(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1414 | if (space->HasAddress(klass.Ptr())) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 1415 | DCHECK(!klass->IsErroneous()) << klass->GetStatus(); |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 1416 | auto it = new_class_set->Find(ClassTable::TableSlot(klass)); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1417 | DCHECK(it != new_class_set->end()); |
| 1418 | DCHECK_EQ(it->Read(), klass); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1419 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1420 | if (super_class != nullptr && !heap->ObjectIsInBootImageSpace(super_class)) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 1421 | auto it2 = new_class_set->Find(ClassTable::TableSlot(super_class)); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1422 | DCHECK(it2 != new_class_set->end()); |
| 1423 | DCHECK_EQ(it2->Read(), super_class); |
| 1424 | } |
| 1425 | for (ArtMethod& m : klass->GetDirectMethods(kRuntimePointerSize)) { |
| 1426 | const void* code = m.GetEntryPointFromQuickCompiledCode(); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame^] | 1427 | const void* oat_code = m.IsInvokable() ? class_linker->GetQuickOatCodeFor(&m) : code; |
| 1428 | if (!class_linker->IsQuickResolutionStub(code) && |
| 1429 | !class_linker->IsQuickGenericJniStub(code) && |
| 1430 | !class_linker->IsQuickToInterpreterBridge(code) && |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1431 | !m.IsNative()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1432 | DCHECK_EQ(code, oat_code) << m.PrettyMethod(); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1433 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1434 | } |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1435 | for (ArtMethod& m : klass->GetVirtualMethods(kRuntimePointerSize)) { |
| 1436 | const void* code = m.GetEntryPointFromQuickCompiledCode(); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame^] | 1437 | const void* oat_code = m.IsInvokable() ? class_linker->GetQuickOatCodeFor(&m) : code; |
| 1438 | if (!class_linker->IsQuickResolutionStub(code) && |
| 1439 | !class_linker->IsQuickGenericJniStub(code) && |
| 1440 | !class_linker->IsQuickToInterpreterBridge(code) && |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1441 | !m.IsNative()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1442 | DCHECK_EQ(code, oat_code) << m.PrettyMethod(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1443 | } |
| 1444 | } |
| 1445 | } |
| 1446 | } |
| 1447 | } |
| 1448 | } |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1449 | } |
| 1450 | if (*out_forward_dex_cache_array) { |
| 1451 | ScopedTrace timing("Fixup ArtMethod dex cache arrays"); |
| 1452 | FixupArtMethodArrayVisitor visitor(header); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1453 | header.VisitPackedArtMethods(&visitor, space->Begin(), kRuntimePointerSize); |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1454 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader.Get()); |
| 1455 | } |
| 1456 | if (kVerifyArtMethodDeclaringClasses) { |
| 1457 | ScopedTrace timing("Verify declaring classes"); |
| 1458 | ReaderMutexLock rmu(self, *Locks::heap_bitmap_lock_); |
| 1459 | VerifyDeclaringClassVisitor visitor; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1460 | header.VisitPackedArtMethods(&visitor, space->Begin(), kRuntimePointerSize); |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1461 | } |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1462 | return true; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1463 | } |
| 1464 | |
Nicolas Geoffray | f9bf250 | 2016-12-14 14:59:04 +0000 | [diff] [blame] | 1465 | // Update the class loader. Should only be used on classes in the image space. |
| 1466 | class UpdateClassLoaderVisitor { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1467 | public: |
Nicolas Geoffray | f9bf250 | 2016-12-14 14:59:04 +0000 | [diff] [blame] | 1468 | UpdateClassLoaderVisitor(gc::space::ImageSpace* space, ObjPtr<mirror::ClassLoader> class_loader) |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1469 | : space_(space), |
Nicolas Geoffray | f9bf250 | 2016-12-14 14:59:04 +0000 | [diff] [blame] | 1470 | class_loader_(class_loader) {} |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1471 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1472 | bool operator()(ObjPtr<mirror::Class> klass) const REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 1473 | // Do not update class loader for boot image classes where the app image |
| 1474 | // class loader is only the initiating loader but not the defining loader. |
| 1475 | if (klass->GetClassLoader() != nullptr) { |
| 1476 | klass->SetClassLoader(class_loader_); |
| 1477 | } |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1478 | return true; |
| 1479 | } |
| 1480 | |
| 1481 | gc::space::ImageSpace* const space_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1482 | ObjPtr<mirror::ClassLoader> const class_loader_; |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1483 | }; |
| 1484 | |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1485 | static std::unique_ptr<const DexFile> OpenOatDexFile(const OatFile* oat_file, |
| 1486 | const char* location, |
| 1487 | std::string* error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1488 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1489 | DCHECK(error_msg != nullptr); |
| 1490 | std::unique_ptr<const DexFile> dex_file; |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 1491 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(location, nullptr, error_msg); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1492 | if (oat_dex_file == nullptr) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1493 | return std::unique_ptr<const DexFile>(); |
| 1494 | } |
| 1495 | std::string inner_error_msg; |
| 1496 | dex_file = oat_dex_file->OpenDexFile(&inner_error_msg); |
| 1497 | if (dex_file == nullptr) { |
| 1498 | *error_msg = StringPrintf("Failed to open dex file %s from within oat file %s error '%s'", |
| 1499 | location, |
| 1500 | oat_file->GetLocation().c_str(), |
| 1501 | inner_error_msg.c_str()); |
| 1502 | return std::unique_ptr<const DexFile>(); |
| 1503 | } |
| 1504 | |
| 1505 | if (dex_file->GetLocationChecksum() != oat_dex_file->GetDexFileLocationChecksum()) { |
| 1506 | *error_msg = StringPrintf("Checksums do not match for %s: %x vs %x", |
| 1507 | location, |
| 1508 | dex_file->GetLocationChecksum(), |
| 1509 | oat_dex_file->GetDexFileLocationChecksum()); |
| 1510 | return std::unique_ptr<const DexFile>(); |
| 1511 | } |
| 1512 | return dex_file; |
| 1513 | } |
| 1514 | |
| 1515 | bool ClassLinker::OpenImageDexFiles(gc::space::ImageSpace* space, |
| 1516 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 1517 | std::string* error_msg) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 1518 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1519 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1520 | ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1521 | DCHECK(dex_caches_object != nullptr); |
| 1522 | mirror::ObjectArray<mirror::DexCache>* dex_caches = |
| 1523 | dex_caches_object->AsObjectArray<mirror::DexCache>(); |
| 1524 | const OatFile* oat_file = space->GetOatFile(); |
| 1525 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1526 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1527 | std::string dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
| 1528 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 1529 | dex_file_location.c_str(), |
| 1530 | error_msg); |
| 1531 | if (dex_file == nullptr) { |
| 1532 | return false; |
| 1533 | } |
| 1534 | dex_cache->SetDexFile(dex_file.get()); |
| 1535 | out_dex_files->push_back(std::move(dex_file)); |
| 1536 | } |
| 1537 | return true; |
| 1538 | } |
| 1539 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1540 | // Helper class for ArtMethod checks when adding an image. Keeps all required functionality |
| 1541 | // together and caches some intermediate results. |
| 1542 | class ImageSanityChecks FINAL { |
| 1543 | public: |
| 1544 | static void CheckObjects(gc::Heap* heap, ClassLinker* class_linker) |
| 1545 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1546 | ImageSanityChecks isc(heap, class_linker); |
| 1547 | heap->VisitObjects(ImageSanityChecks::SanityCheckObjectsCallback, &isc); |
| 1548 | } |
| 1549 | |
| 1550 | static void CheckPointerArray(gc::Heap* heap, |
| 1551 | ClassLinker* class_linker, |
| 1552 | ArtMethod** arr, |
| 1553 | size_t size) |
| 1554 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1555 | ImageSanityChecks isc(heap, class_linker); |
| 1556 | isc.SanityCheckArtMethodPointerArray(arr, size); |
| 1557 | } |
| 1558 | |
| 1559 | static void SanityCheckObjectsCallback(mirror::Object* obj, void* arg) |
| 1560 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1561 | DCHECK(obj != nullptr); |
| 1562 | CHECK(obj->GetClass() != nullptr) << "Null class in object " << obj; |
| 1563 | CHECK(obj->GetClass()->GetClass() != nullptr) << "Null class class " << obj; |
| 1564 | if (obj->IsClass()) { |
| 1565 | ImageSanityChecks* isc = reinterpret_cast<ImageSanityChecks*>(arg); |
| 1566 | |
| 1567 | auto klass = obj->AsClass(); |
| 1568 | for (ArtField& field : klass->GetIFields()) { |
| 1569 | CHECK_EQ(field.GetDeclaringClass(), klass); |
| 1570 | } |
| 1571 | for (ArtField& field : klass->GetSFields()) { |
| 1572 | CHECK_EQ(field.GetDeclaringClass(), klass); |
| 1573 | } |
| 1574 | const auto pointer_size = isc->pointer_size_; |
| 1575 | for (auto& m : klass->GetMethods(pointer_size)) { |
| 1576 | isc->SanityCheckArtMethod(&m, klass); |
| 1577 | } |
| 1578 | auto* vtable = klass->GetVTable(); |
| 1579 | if (vtable != nullptr) { |
| 1580 | isc->SanityCheckArtMethodPointerArray(vtable, nullptr); |
| 1581 | } |
| 1582 | if (klass->ShouldHaveImt()) { |
| 1583 | ImTable* imt = klass->GetImt(pointer_size); |
| 1584 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 1585 | isc->SanityCheckArtMethod(imt->Get(i, pointer_size), nullptr); |
| 1586 | } |
| 1587 | } |
| 1588 | if (klass->ShouldHaveEmbeddedVTable()) { |
| 1589 | for (int32_t i = 0; i < klass->GetEmbeddedVTableLength(); ++i) { |
| 1590 | isc->SanityCheckArtMethod(klass->GetEmbeddedVTableEntry(i, pointer_size), nullptr); |
| 1591 | } |
| 1592 | } |
| 1593 | mirror::IfTable* iftable = klass->GetIfTable(); |
| 1594 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 1595 | if (iftable->GetMethodArrayCount(i) > 0) { |
| 1596 | isc->SanityCheckArtMethodPointerArray(iftable->GetMethodArray(i), nullptr); |
| 1597 | } |
| 1598 | } |
| 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | private: |
| 1603 | ImageSanityChecks(gc::Heap* heap, ClassLinker* class_linker) |
| 1604 | : spaces_(heap->GetBootImageSpaces()), |
| 1605 | pointer_size_(class_linker->GetImagePointerSize()) { |
| 1606 | space_begin_.reserve(spaces_.size()); |
| 1607 | method_sections_.reserve(spaces_.size()); |
| 1608 | runtime_method_sections_.reserve(spaces_.size()); |
| 1609 | for (gc::space::ImageSpace* space : spaces_) { |
| 1610 | space_begin_.push_back(space->Begin()); |
| 1611 | auto& header = space->GetImageHeader(); |
| 1612 | method_sections_.push_back(&header.GetMethodsSection()); |
| 1613 | runtime_method_sections_.push_back(&header.GetRuntimeMethodsSection()); |
| 1614 | } |
| 1615 | } |
| 1616 | |
| 1617 | void SanityCheckArtMethod(ArtMethod* m, ObjPtr<mirror::Class> expected_class) |
| 1618 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1619 | if (m->IsRuntimeMethod()) { |
| 1620 | ObjPtr<mirror::Class> declaring_class = m->GetDeclaringClassUnchecked(); |
| 1621 | CHECK(declaring_class == nullptr) << declaring_class << " " << m->PrettyMethod(); |
| 1622 | } else if (m->IsCopied()) { |
| 1623 | CHECK(m->GetDeclaringClass() != nullptr) << m->PrettyMethod(); |
| 1624 | } else if (expected_class != nullptr) { |
| 1625 | CHECK_EQ(m->GetDeclaringClassUnchecked(), expected_class) << m->PrettyMethod(); |
| 1626 | } |
| 1627 | if (!spaces_.empty()) { |
| 1628 | bool contains = false; |
| 1629 | for (size_t i = 0; !contains && i != space_begin_.size(); ++i) { |
| 1630 | const size_t offset = reinterpret_cast<uint8_t*>(m) - space_begin_[i]; |
| 1631 | contains = method_sections_[i]->Contains(offset) || |
| 1632 | runtime_method_sections_[i]->Contains(offset); |
| 1633 | } |
| 1634 | CHECK(contains) << m << " not found"; |
| 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | void SanityCheckArtMethodPointerArray(ObjPtr<mirror::PointerArray> arr, |
| 1639 | ObjPtr<mirror::Class> expected_class) |
| 1640 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1641 | CHECK(arr != nullptr); |
| 1642 | for (int32_t j = 0; j < arr->GetLength(); ++j) { |
| 1643 | auto* method = arr->GetElementPtrSize<ArtMethod*>(j, pointer_size_); |
| 1644 | // expected_class == null means we are a dex cache. |
| 1645 | if (expected_class != nullptr) { |
| 1646 | CHECK(method != nullptr); |
| 1647 | } |
| 1648 | if (method != nullptr) { |
| 1649 | SanityCheckArtMethod(method, expected_class); |
| 1650 | } |
| 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | void SanityCheckArtMethodPointerArray(ArtMethod** arr, size_t size) |
| 1655 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1656 | CHECK_EQ(arr != nullptr, size != 0u); |
| 1657 | if (arr != nullptr) { |
| 1658 | bool contains = false; |
| 1659 | for (auto space : spaces_) { |
| 1660 | auto offset = reinterpret_cast<uint8_t*>(arr) - space->Begin(); |
| 1661 | if (space->GetImageHeader().GetImageSection( |
| 1662 | ImageHeader::kSectionDexCacheArrays).Contains(offset)) { |
| 1663 | contains = true; |
| 1664 | break; |
| 1665 | } |
| 1666 | } |
| 1667 | CHECK(contains); |
| 1668 | } |
| 1669 | for (size_t j = 0; j < size; ++j) { |
| 1670 | ArtMethod* method = mirror::DexCache::GetElementPtrSize(arr, j, pointer_size_); |
| 1671 | // expected_class == null means we are a dex cache. |
| 1672 | if (method != nullptr) { |
| 1673 | SanityCheckArtMethod(method, nullptr); |
| 1674 | } |
| 1675 | } |
| 1676 | } |
| 1677 | |
| 1678 | const std::vector<gc::space::ImageSpace*>& spaces_; |
| 1679 | const PointerSize pointer_size_; |
| 1680 | |
| 1681 | // Cached sections from the spaces. |
| 1682 | std::vector<const uint8_t*> space_begin_; |
| 1683 | std::vector<const ImageSection*> method_sections_; |
| 1684 | std::vector<const ImageSection*> runtime_method_sections_; |
| 1685 | }; |
| 1686 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1687 | bool ClassLinker::AddImageSpace( |
| 1688 | gc::space::ImageSpace* space, |
| 1689 | Handle<mirror::ClassLoader> class_loader, |
| 1690 | jobjectArray dex_elements, |
| 1691 | const char* dex_location, |
| 1692 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 1693 | std::string* error_msg) { |
| 1694 | DCHECK(out_dex_files != nullptr); |
| 1695 | DCHECK(error_msg != nullptr); |
| 1696 | const uint64_t start_time = NanoTime(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1697 | const bool app_image = class_loader != nullptr; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1698 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1699 | ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1700 | DCHECK(dex_caches_object != nullptr); |
| 1701 | Runtime* const runtime = Runtime::Current(); |
| 1702 | gc::Heap* const heap = runtime->GetHeap(); |
| 1703 | Thread* const self = Thread::Current(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1704 | // Check that the image is what we are expecting. |
| 1705 | if (image_pointer_size_ != space->GetImageHeader().GetPointerSize()) { |
| 1706 | *error_msg = StringPrintf("Application image pointer size does not match runtime: %zu vs %zu", |
| 1707 | static_cast<size_t>(space->GetImageHeader().GetPointerSize()), |
| 1708 | image_pointer_size_); |
| 1709 | return false; |
| 1710 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1711 | size_t expected_image_roots = ImageHeader::NumberOfImageRoots(app_image); |
| 1712 | if (static_cast<size_t>(header.GetImageRoots()->GetLength()) != expected_image_roots) { |
| 1713 | *error_msg = StringPrintf("Expected %zu image roots but got %d", |
| 1714 | expected_image_roots, |
| 1715 | header.GetImageRoots()->GetLength()); |
| 1716 | return false; |
| 1717 | } |
| 1718 | StackHandleScope<3> hs(self); |
| 1719 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches( |
| 1720 | hs.NewHandle(dex_caches_object->AsObjectArray<mirror::DexCache>())); |
| 1721 | Handle<mirror::ObjectArray<mirror::Class>> class_roots(hs.NewHandle( |
| 1722 | header.GetImageRoot(ImageHeader::kClassRoots)->AsObjectArray<mirror::Class>())); |
| 1723 | static_assert(ImageHeader::kClassLoader + 1u == ImageHeader::kImageRootsMax, |
| 1724 | "Class loader should be the last image root."); |
| 1725 | MutableHandle<mirror::ClassLoader> image_class_loader(hs.NewHandle( |
| 1726 | app_image ? header.GetImageRoot(ImageHeader::kClassLoader)->AsClassLoader() : nullptr)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1727 | DCHECK(class_roots != nullptr); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1728 | if (class_roots->GetLength() != static_cast<int32_t>(kClassRootsMax)) { |
| 1729 | *error_msg = StringPrintf("Expected %d class roots but got %d", |
| 1730 | class_roots->GetLength(), |
| 1731 | static_cast<int32_t>(kClassRootsMax)); |
| 1732 | return false; |
| 1733 | } |
| 1734 | // Check against existing class roots to make sure they match the ones in the boot image. |
| 1735 | for (size_t i = 0; i < kClassRootsMax; i++) { |
| 1736 | if (class_roots->Get(i) != GetClassRoot(static_cast<ClassRoot>(i))) { |
| 1737 | *error_msg = "App image class roots must have pointer equality with runtime ones."; |
| 1738 | return false; |
| 1739 | } |
| 1740 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1741 | const OatFile* oat_file = space->GetOatFile(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1742 | if (oat_file->GetOatHeader().GetDexFileCount() != |
| 1743 | static_cast<uint32_t>(dex_caches->GetLength())) { |
| 1744 | *error_msg = "Dex cache count and dex file count mismatch while trying to initialize from " |
| 1745 | "image"; |
| 1746 | return false; |
| 1747 | } |
| 1748 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1749 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1750 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
| 1751 | std::string dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1752 | // TODO: Only store qualified paths. |
| 1753 | // If non qualified, qualify it. |
| 1754 | if (dex_file_location.find('/') == std::string::npos) { |
| 1755 | std::string dex_location_path = dex_location; |
| 1756 | const size_t pos = dex_location_path.find_last_of('/'); |
| 1757 | CHECK_NE(pos, std::string::npos); |
| 1758 | dex_location_path = dex_location_path.substr(0, pos + 1); // Keep trailing '/' |
| 1759 | dex_file_location = dex_location_path + dex_file_location; |
| 1760 | } |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1761 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 1762 | dex_file_location.c_str(), |
| 1763 | error_msg); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1764 | if (dex_file == nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1765 | return false; |
| 1766 | } |
| 1767 | |
| 1768 | if (app_image) { |
| 1769 | // The current dex file field is bogus, overwrite it so that we can get the dex file in the |
| 1770 | // loop below. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1771 | dex_cache->SetDexFile(dex_file.get()); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1772 | mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes(); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1773 | for (int32_t j = 0, num_types = dex_cache->NumResolvedTypes(); j < num_types; j++) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1774 | ObjPtr<mirror::Class> klass = types[j].load(std::memory_order_relaxed).object.Read(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1775 | if (klass != nullptr) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 1776 | DCHECK(!klass->IsErroneous()) << klass->GetStatus(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1777 | } |
| 1778 | } |
| 1779 | } else { |
| 1780 | if (kSanityCheckObjects) { |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1781 | ImageSanityChecks::CheckPointerArray(heap, |
| 1782 | this, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1783 | dex_cache->GetResolvedMethods(), |
| 1784 | dex_cache->NumResolvedMethods()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1785 | } |
| 1786 | // Register dex files, keep track of existing ones that are conflicts. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1787 | AppendToBootClassPath(*dex_file.get(), dex_cache); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1788 | } |
| 1789 | out_dex_files->push_back(std::move(dex_file)); |
| 1790 | } |
| 1791 | |
| 1792 | if (app_image) { |
| 1793 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1794 | // Check that the class loader resolves the same way as the ones in the image. |
| 1795 | // Image class loader [A][B][C][image dex files] |
| 1796 | // Class loader = [???][dex_elements][image dex files] |
| 1797 | // Need to ensure that [???][dex_elements] == [A][B][C]. |
| 1798 | // For each class loader, PathClassLoader, the laoder checks the parent first. Also the logic |
| 1799 | // for PathClassLoader does this by looping through the array of dex files. To ensure they |
| 1800 | // resolve the same way, simply flatten the hierarchy in the way the resolution order would be, |
| 1801 | // and check that the dex file names are the same. |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1802 | if (IsBootClassLoader(soa, image_class_loader.Get())) { |
| 1803 | *error_msg = "Unexpected BootClassLoader in app image"; |
| 1804 | return false; |
| 1805 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1806 | std::list<ObjPtr<mirror::String>> image_dex_file_names; |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1807 | std::string temp_error_msg; |
| 1808 | if (!FlattenPathClassLoader(image_class_loader.Get(), &image_dex_file_names, &temp_error_msg)) { |
| 1809 | *error_msg = StringPrintf("Failed to flatten image class loader hierarchy '%s'", |
| 1810 | temp_error_msg.c_str()); |
| 1811 | return false; |
| 1812 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1813 | std::list<ObjPtr<mirror::String>> loader_dex_file_names; |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1814 | if (!FlattenPathClassLoader(class_loader.Get(), &loader_dex_file_names, &temp_error_msg)) { |
| 1815 | *error_msg = StringPrintf("Failed to flatten class loader hierarchy '%s'", |
| 1816 | temp_error_msg.c_str()); |
| 1817 | return false; |
| 1818 | } |
| 1819 | // Add the temporary dex path list elements at the end. |
| 1820 | auto elements = soa.Decode<mirror::ObjectArray<mirror::Object>>(dex_elements); |
| 1821 | for (size_t i = 0, num_elems = elements->GetLength(); i < num_elems; ++i) { |
| 1822 | ObjPtr<mirror::Object> element = elements->GetWithoutChecks(i); |
| 1823 | if (element != nullptr) { |
| 1824 | // If we are somewhere in the middle of the array, there may be nulls at the end. |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1825 | ObjPtr<mirror::String> name; |
| 1826 | if (GetDexPathListElementName(element, &name) && name != nullptr) { |
| 1827 | loader_dex_file_names.push_back(name); |
| 1828 | } |
Nicolas Geoffray | 1df3b55 | 2016-04-26 18:30:31 +0100 | [diff] [blame] | 1829 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1830 | } |
| 1831 | // Ignore the number of image dex files since we are adding those to the class loader anyways. |
| 1832 | CHECK_GE(static_cast<size_t>(image_dex_file_names.size()), |
| 1833 | static_cast<size_t>(dex_caches->GetLength())); |
| 1834 | size_t image_count = image_dex_file_names.size() - dex_caches->GetLength(); |
| 1835 | // Check that the dex file names match. |
| 1836 | bool equal = image_count == loader_dex_file_names.size(); |
| 1837 | if (equal) { |
| 1838 | auto it1 = image_dex_file_names.begin(); |
| 1839 | auto it2 = loader_dex_file_names.begin(); |
| 1840 | for (size_t i = 0; equal && i < image_count; ++i, ++it1, ++it2) { |
| 1841 | equal = equal && (*it1)->Equals(*it2); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1842 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1843 | } |
| 1844 | if (!equal) { |
| 1845 | VLOG(image) << "Image dex files " << image_dex_file_names.size(); |
| 1846 | for (ObjPtr<mirror::String> name : image_dex_file_names) { |
| 1847 | VLOG(image) << name->ToModifiedUtf8(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1848 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1849 | VLOG(image) << "Loader dex files " << loader_dex_file_names.size(); |
| 1850 | for (ObjPtr<mirror::String> name : loader_dex_file_names) { |
| 1851 | VLOG(image) << name->ToModifiedUtf8(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1852 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1853 | *error_msg = "Rejecting application image due to class loader mismatch"; |
| 1854 | // Ignore class loader mismatch for now since these would just use possibly incorrect |
| 1855 | // oat code anyways. The structural class check should be done in the parent. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1856 | } |
| 1857 | } |
| 1858 | |
| 1859 | if (kSanityCheckObjects) { |
| 1860 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 1861 | auto* dex_cache = dex_caches->Get(i); |
| 1862 | for (size_t j = 0; j < dex_cache->NumResolvedFields(); ++j) { |
| 1863 | auto* field = dex_cache->GetResolvedField(j, image_pointer_size_); |
| 1864 | if (field != nullptr) { |
| 1865 | CHECK(field->GetDeclaringClass()->GetClass() != nullptr); |
| 1866 | } |
| 1867 | } |
| 1868 | } |
| 1869 | if (!app_image) { |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1870 | ImageSanityChecks::CheckObjects(heap, this); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1871 | } |
| 1872 | } |
| 1873 | |
| 1874 | // Set entry point to interpreter if in InterpretOnly mode. |
| 1875 | if (!runtime->IsAotCompiler() && runtime->GetInstrumentation()->InterpretOnly()) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1876 | SetInterpreterEntrypointArtMethodVisitor visitor(image_pointer_size_); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 1877 | header.VisitPackedArtMethods(&visitor, space->Begin(), image_pointer_size_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1878 | } |
| 1879 | |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1880 | ClassTable* class_table = nullptr; |
| 1881 | { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1882 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1883 | class_table = InsertClassTableForClassLoader(class_loader.Get()); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1884 | } |
| 1885 | // If we have a class table section, read it and use it for verification in |
| 1886 | // UpdateAppImageClassLoadersAndDexCaches. |
| 1887 | ClassTable::ClassSet temp_set; |
| 1888 | const ImageSection& class_table_section = header.GetImageSection(ImageHeader::kSectionClassTable); |
| 1889 | const bool added_class_table = class_table_section.Size() > 0u; |
| 1890 | if (added_class_table) { |
| 1891 | const uint64_t start_time2 = NanoTime(); |
| 1892 | size_t read_count = 0; |
| 1893 | temp_set = ClassTable::ClassSet(space->Begin() + class_table_section.Offset(), |
| 1894 | /*make copy*/false, |
| 1895 | &read_count); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1896 | VLOG(image) << "Adding class table classes took " << PrettyDuration(NanoTime() - start_time2); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1897 | } |
| 1898 | if (app_image) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1899 | bool forward_dex_cache_arrays = false; |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame^] | 1900 | if (!AppImageClassLoadersAndDexCachesHelper::Update(this, |
| 1901 | space, |
| 1902 | class_loader, |
| 1903 | dex_caches, |
| 1904 | &temp_set, |
| 1905 | /*out*/&forward_dex_cache_arrays, |
| 1906 | /*out*/error_msg)) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1907 | return false; |
| 1908 | } |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1909 | // Update class loader and resolved strings. If added_class_table is false, the resolved |
| 1910 | // strings were forwarded UpdateAppImageClassLoadersAndDexCaches. |
Nicolas Geoffray | f9bf250 | 2016-12-14 14:59:04 +0000 | [diff] [blame] | 1911 | UpdateClassLoaderVisitor visitor(space, class_loader.Get()); |
| 1912 | for (const ClassTable::TableSlot& root : temp_set) { |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1913 | visitor(root.Read()); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1914 | } |
Mathieu Chartier | 8c4f041 | 2016-02-03 16:40:20 -0800 | [diff] [blame] | 1915 | // forward_dex_cache_arrays is true iff we copied all of the dex cache arrays into the .bss. |
| 1916 | // In this case, madvise away the dex cache arrays section of the image to reduce RAM usage and |
| 1917 | // mark as PROT_NONE to catch any invalid accesses. |
| 1918 | if (forward_dex_cache_arrays) { |
| 1919 | const ImageSection& dex_cache_section = header.GetImageSection( |
| 1920 | ImageHeader::kSectionDexCacheArrays); |
| 1921 | uint8_t* section_begin = AlignUp(space->Begin() + dex_cache_section.Offset(), kPageSize); |
| 1922 | uint8_t* section_end = AlignDown(space->Begin() + dex_cache_section.End(), kPageSize); |
| 1923 | if (section_begin < section_end) { |
| 1924 | madvise(section_begin, section_end - section_begin, MADV_DONTNEED); |
| 1925 | mprotect(section_begin, section_end - section_begin, PROT_NONE); |
| 1926 | VLOG(image) << "Released and protected dex cache array image section from " |
| 1927 | << reinterpret_cast<const void*>(section_begin) << "-" |
| 1928 | << reinterpret_cast<const void*>(section_end); |
| 1929 | } |
| 1930 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 1931 | } |
| 1932 | if (!oat_file->GetBssGcRoots().empty()) { |
| 1933 | // Insert oat file to class table for visiting .bss GC roots. |
| 1934 | class_table->InsertOatFile(oat_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1935 | } |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1936 | if (added_class_table) { |
| 1937 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 1938 | class_table->AddClassSet(std::move(temp_set)); |
| 1939 | } |
| 1940 | if (kIsDebugBuild && app_image) { |
| 1941 | // This verification needs to happen after the classes have been added to the class loader. |
| 1942 | // Since it ensures classes are in the class table. |
| 1943 | VerifyClassInTableArtMethodVisitor visitor2(class_table); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1944 | header.VisitPackedArtMethods(&visitor2, space->Begin(), kRuntimePointerSize); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1945 | // Verify that all direct interfaces of classes in the class table are also resolved. |
| 1946 | VerifyDirectInterfacesInTableClassVisitor visitor(class_loader.Get()); |
| 1947 | class_table->Visit(visitor); |
| 1948 | visitor.Check(); |
| 1949 | // Check that all non-primitive classes in dex caches are also in the class table. |
| 1950 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 1951 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
| 1952 | mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes(); |
| 1953 | for (int32_t j = 0, num_types = dex_cache->NumResolvedTypes(); j < num_types; j++) { |
| 1954 | ObjPtr<mirror::Class> klass = types[j].load(std::memory_order_relaxed).object.Read(); |
| 1955 | if (klass != nullptr && !klass->IsPrimitive()) { |
| 1956 | CHECK(class_table->Contains(klass)) << klass->PrettyDescriptor() |
| 1957 | << " " << dex_cache->GetDexFile()->GetLocation(); |
| 1958 | } |
| 1959 | } |
| 1960 | } |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1961 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1962 | VLOG(class_linker) << "Adding image space took " << PrettyDuration(NanoTime() - start_time); |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 1963 | return true; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1964 | } |
| 1965 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1966 | bool ClassLinker::ClassInClassTable(ObjPtr<mirror::Class> klass) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 1967 | ClassTable* const class_table = ClassTableForClassLoader(klass->GetClassLoader()); |
| 1968 | return class_table != nullptr && class_table->Contains(klass); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1969 | } |
| 1970 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1971 | void ClassLinker::VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 1972 | // Acquire tracing_enabled before locking class linker lock to prevent lock order violation. Since |
| 1973 | // enabling tracing requires the mutator lock, there are no race conditions here. |
| 1974 | const bool tracing_enabled = Trace::IsTracingEnabled(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 1975 | Thread* const self = Thread::Current(); |
| 1976 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 1977 | if (kUseReadBarrier) { |
| 1978 | // We do not track new roots for CC. |
| 1979 | DCHECK_EQ(0, flags & (kVisitRootFlagNewRoots | |
| 1980 | kVisitRootFlagClearRootLog | |
| 1981 | kVisitRootFlagStartLoggingNewRoots | |
| 1982 | kVisitRootFlagStopLoggingNewRoots)); |
| 1983 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1984 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1985 | // Argument for how root visiting deals with ArtField and ArtMethod roots. |
| 1986 | // There is 3 GC cases to handle: |
| 1987 | // Non moving concurrent: |
| 1988 | // This case is easy to handle since the reference members of ArtMethod and ArtFields are held |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1989 | // live by the class and class roots. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1990 | // |
| 1991 | // Moving non-concurrent: |
| 1992 | // This case needs to call visit VisitNativeRoots in case the classes or dex cache arrays move. |
| 1993 | // To prevent missing roots, this case needs to ensure that there is no |
| 1994 | // suspend points between the point which we allocate ArtMethod arrays and place them in a |
| 1995 | // class which is in the class table. |
| 1996 | // |
| 1997 | // Moving concurrent: |
| 1998 | // Need to make sure to not copy ArtMethods without doing read barriers since the roots are |
| 1999 | // marked concurrently and we don't hold the classlinker_classes_lock_ when we do the copy. |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 2000 | // |
| 2001 | // Use an unbuffered visitor since the class table uses a temporary GcRoot for holding decoded |
| 2002 | // ClassTable::TableSlot. The buffered root visiting would access a stale stack location for |
| 2003 | // these objects. |
| 2004 | UnbufferedRootVisitor root_visitor(visitor, RootInfo(kRootStickyClass)); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame^] | 2005 | boot_class_table_->VisitRoots(root_visitor); |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 2006 | // If tracing is enabled, then mark all the class loaders to prevent unloading. |
neo.chae | a2d1b28 | 2016-11-08 08:40:46 +0900 | [diff] [blame] | 2007 | if ((flags & kVisitRootFlagClassLoader) != 0 || tracing_enabled) { |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 2008 | for (const ClassLoaderData& data : class_loaders_) { |
| 2009 | GcRoot<mirror::Object> root(GcRoot<mirror::Object>(self->DecodeJObject(data.weak_root))); |
| 2010 | root.VisitRoot(visitor, RootInfo(kRootVMInternal)); |
| 2011 | } |
| 2012 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2013 | } else if (!kUseReadBarrier && (flags & kVisitRootFlagNewRoots) != 0) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2014 | for (auto& root : new_class_roots_) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2015 | ObjPtr<mirror::Class> old_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2016 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2017 | ObjPtr<mirror::Class> new_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2018 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 2019 | CHECK_EQ(new_ref, old_ref); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2020 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2021 | for (const OatFile* oat_file : new_bss_roots_boot_oat_files_) { |
| 2022 | for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) { |
| 2023 | ObjPtr<mirror::Object> old_ref = root.Read<kWithoutReadBarrier>(); |
| 2024 | if (old_ref != nullptr) { |
| 2025 | DCHECK(old_ref->IsClass()); |
| 2026 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
| 2027 | ObjPtr<mirror::Object> new_ref = root.Read<kWithoutReadBarrier>(); |
| 2028 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 2029 | CHECK_EQ(new_ref, old_ref); |
| 2030 | } |
| 2031 | } |
| 2032 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2033 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2034 | if (!kUseReadBarrier && (flags & kVisitRootFlagClearRootLog) != 0) { |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2035 | new_class_roots_.clear(); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2036 | new_bss_roots_boot_oat_files_.clear(); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2037 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2038 | if (!kUseReadBarrier && (flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2039 | log_new_roots_ = true; |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2040 | } else if (!kUseReadBarrier && (flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2041 | log_new_roots_ = false; |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2042 | } |
| 2043 | // We deliberately ignore the class roots in the image since we |
| 2044 | // handle image roots by using the MS/CMS rescanning of dirty cards. |
| 2045 | } |
| 2046 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2047 | // Keep in sync with InitCallback. Anything we visit, we need to |
| 2048 | // reinit references to when reinitializing a ClassLinker from a |
| 2049 | // mapped image. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2050 | void ClassLinker::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 2051 | class_roots_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2052 | VisitClassRoots(visitor, flags); |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 2053 | array_iftable_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); |
Mathieu Chartier | 6cfc2c0 | 2015-10-12 15:06:16 -0700 | [diff] [blame] | 2054 | // Instead of visiting the find_array_class_cache_ drop it so that it doesn't prevent class |
| 2055 | // unloading if we are marking roots. |
| 2056 | DropFindArrayClassCache(); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2057 | } |
| 2058 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2059 | class VisitClassLoaderClassesVisitor : public ClassLoaderVisitor { |
| 2060 | public: |
| 2061 | explicit VisitClassLoaderClassesVisitor(ClassVisitor* visitor) |
| 2062 | : visitor_(visitor), |
| 2063 | done_(false) {} |
| 2064 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2065 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2066 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) OVERRIDE { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2067 | ClassTable* const class_table = class_loader->GetClassTable(); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2068 | if (!done_ && class_table != nullptr) { |
| 2069 | DefiningClassLoaderFilterVisitor visitor(class_loader, visitor_); |
| 2070 | if (!class_table->Visit(visitor)) { |
| 2071 | // If the visitor ClassTable returns false it means that we don't need to continue. |
| 2072 | done_ = true; |
| 2073 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2074 | } |
| 2075 | } |
| 2076 | |
| 2077 | private: |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2078 | // Class visitor that limits the class visits from a ClassTable to the classes with |
| 2079 | // the provided defining class loader. This filter is used to avoid multiple visits |
| 2080 | // of the same class which can be recorded for multiple initiating class loaders. |
| 2081 | class DefiningClassLoaderFilterVisitor : public ClassVisitor { |
| 2082 | public: |
| 2083 | DefiningClassLoaderFilterVisitor(ObjPtr<mirror::ClassLoader> defining_class_loader, |
| 2084 | ClassVisitor* visitor) |
| 2085 | : defining_class_loader_(defining_class_loader), visitor_(visitor) { } |
| 2086 | |
| 2087 | bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2088 | if (klass->GetClassLoader() != defining_class_loader_) { |
| 2089 | return true; |
| 2090 | } |
| 2091 | return (*visitor_)(klass); |
| 2092 | } |
| 2093 | |
| 2094 | ObjPtr<mirror::ClassLoader> const defining_class_loader_; |
| 2095 | ClassVisitor* const visitor_; |
| 2096 | }; |
| 2097 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2098 | ClassVisitor* const visitor_; |
| 2099 | // If done is true then we don't need to do any more visiting. |
| 2100 | bool done_; |
| 2101 | }; |
| 2102 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2103 | void ClassLinker::VisitClassesInternal(ClassVisitor* visitor) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame^] | 2104 | if (boot_class_table_->Visit(*visitor)) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2105 | VisitClassLoaderClassesVisitor loader_visitor(visitor); |
| 2106 | VisitClassLoaders(&loader_visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2107 | } |
| 2108 | } |
| 2109 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2110 | void ClassLinker::VisitClasses(ClassVisitor* visitor) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2111 | Thread* const self = Thread::Current(); |
| 2112 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2113 | // Not safe to have thread suspension when we are holding a lock. |
| 2114 | if (self != nullptr) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2115 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2116 | VisitClassesInternal(visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2117 | } else { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2118 | VisitClassesInternal(visitor); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2119 | } |
| 2120 | } |
| 2121 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2122 | class GetClassesInToVector : public ClassVisitor { |
| 2123 | public: |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2124 | bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2125 | classes_.push_back(klass); |
| 2126 | return true; |
| 2127 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2128 | std::vector<ObjPtr<mirror::Class>> classes_; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2129 | }; |
| 2130 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2131 | class GetClassInToObjectArray : public ClassVisitor { |
| 2132 | public: |
| 2133 | explicit GetClassInToObjectArray(mirror::ObjectArray<mirror::Class>* arr) |
| 2134 | : arr_(arr), index_(0) {} |
| 2135 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2136 | bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2137 | ++index_; |
| 2138 | if (index_ <= arr_->GetLength()) { |
| 2139 | arr_->Set(index_ - 1, klass); |
| 2140 | return true; |
| 2141 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2142 | return false; |
| 2143 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2144 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2145 | bool Succeeded() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2146 | return index_ <= arr_->GetLength(); |
| 2147 | } |
| 2148 | |
| 2149 | private: |
| 2150 | mirror::ObjectArray<mirror::Class>* const arr_; |
| 2151 | int32_t index_; |
| 2152 | }; |
| 2153 | |
| 2154 | void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2155 | // TODO: it may be possible to avoid secondary storage if we iterate over dex caches. The problem |
| 2156 | // is avoiding duplicates. |
| 2157 | if (!kMovingClasses) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2158 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2159 | GetClassesInToVector accumulator; |
| 2160 | VisitClasses(&accumulator); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2161 | for (ObjPtr<mirror::Class> klass : accumulator.classes_) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2162 | if (!visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2163 | return; |
| 2164 | } |
| 2165 | } |
| 2166 | } else { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2167 | Thread* const self = Thread::Current(); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2168 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2169 | auto classes = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2170 | // We size the array assuming classes won't be added to the class table during the visit. |
| 2171 | // If this assumption fails we iterate again. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2172 | while (true) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2173 | size_t class_table_size; |
| 2174 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2175 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2176 | // Add 100 in case new classes get loaded when we are filling in the object array. |
| 2177 | class_table_size = NumZygoteClasses() + NumNonZygoteClasses() + 100; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2178 | } |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 2179 | ObjPtr<mirror::Class> class_type = mirror::Class::GetJavaLangClass(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2180 | ObjPtr<mirror::Class> array_of_class = FindArrayClass(self, &class_type); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2181 | classes.Assign( |
| 2182 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, class_table_size)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2183 | CHECK(classes != nullptr); // OOME. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2184 | GetClassInToObjectArray accumulator(classes.Get()); |
| 2185 | VisitClasses(&accumulator); |
| 2186 | if (accumulator.Succeeded()) { |
| 2187 | break; |
| 2188 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2189 | } |
| 2190 | for (int32_t i = 0; i < classes->GetLength(); ++i) { |
| 2191 | // If the class table shrank during creation of the clases array we expect null elements. If |
| 2192 | // the class table grew then the loop repeats. If classes are created after the loop has |
| 2193 | // finished then we don't visit. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2194 | ObjPtr<mirror::Class> klass = classes->Get(i); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2195 | if (klass != nullptr && !visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2196 | return; |
| 2197 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2198 | } |
| 2199 | } |
| 2200 | } |
| 2201 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2202 | ClassLinker::~ClassLinker() { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2203 | mirror::Class::ResetClass(); |
| 2204 | mirror::Constructor::ResetClass(); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 2205 | mirror::Field::ResetClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2206 | mirror::Method::ResetClass(); |
| 2207 | mirror::Reference::ResetClass(); |
| 2208 | mirror::StackTraceElement::ResetClass(); |
| 2209 | mirror::String::ResetClass(); |
| 2210 | mirror::Throwable::ResetClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2211 | mirror::BooleanArray::ResetArrayClass(); |
| 2212 | mirror::ByteArray::ResetArrayClass(); |
| 2213 | mirror::CharArray::ResetArrayClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2214 | mirror::Constructor::ResetArrayClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2215 | mirror::DoubleArray::ResetArrayClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2216 | mirror::Field::ResetArrayClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2217 | mirror::FloatArray::ResetArrayClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2218 | mirror::Method::ResetArrayClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2219 | mirror::IntArray::ResetArrayClass(); |
| 2220 | mirror::LongArray::ResetArrayClass(); |
| 2221 | mirror::ShortArray::ResetArrayClass(); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 2222 | mirror::MethodType::ResetClass(); |
| 2223 | mirror::MethodHandleImpl::ResetClass(); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 2224 | mirror::MethodHandlesLookup::ResetClass(); |
| 2225 | mirror::CallSite::ResetClass(); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 2226 | mirror::EmulatedStackFrame::ResetClass(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2227 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 2228 | for (const ClassLoaderData& data : class_loaders_) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2229 | DeleteClassLoader(self, data); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 2230 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2231 | class_loaders_.clear(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2232 | } |
| 2233 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2234 | void ClassLinker::DeleteClassLoader(Thread* self, const ClassLoaderData& data) { |
| 2235 | Runtime* const runtime = Runtime::Current(); |
| 2236 | JavaVMExt* const vm = runtime->GetJavaVM(); |
| 2237 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 2238 | // Notify the JIT that we need to remove the methods and/or profiling info. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2239 | if (runtime->GetJit() != nullptr) { |
| 2240 | jit::JitCodeCache* code_cache = runtime->GetJit()->GetCodeCache(); |
| 2241 | if (code_cache != nullptr) { |
| 2242 | code_cache->RemoveMethodsIn(self, *data.allocator); |
| 2243 | } |
| 2244 | } |
| 2245 | delete data.allocator; |
| 2246 | delete data.class_table; |
| 2247 | } |
| 2248 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2249 | mirror::PointerArray* ClassLinker::AllocPointerArray(Thread* self, size_t length) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2250 | return down_cast<mirror::PointerArray*>( |
| 2251 | image_pointer_size_ == PointerSize::k64 |
| 2252 | ? static_cast<mirror::Array*>(mirror::LongArray::Alloc(self, length)) |
| 2253 | : static_cast<mirror::Array*>(mirror::IntArray::Alloc(self, length))); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2254 | } |
| 2255 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2256 | mirror::DexCache* ClassLinker::AllocDexCache(ObjPtr<mirror::String>* out_location, |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2257 | Thread* self, |
| 2258 | const DexFile& dex_file) { |
| 2259 | StackHandleScope<1> hs(self); |
| 2260 | DCHECK(out_location != nullptr); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 2261 | auto dex_cache(hs.NewHandle(ObjPtr<mirror::DexCache>::DownCast( |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2262 | GetClassRoot(kJavaLangDexCache)->AllocObject(self)))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2263 | if (dex_cache == nullptr) { |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2264 | self->AssertPendingOOMException(); |
| 2265 | return nullptr; |
| 2266 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2267 | ObjPtr<mirror::String> location = intern_table_->InternStrong(dex_file.GetLocation().c_str()); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2268 | if (location == nullptr) { |
| 2269 | self->AssertPendingOOMException(); |
| 2270 | return nullptr; |
| 2271 | } |
| 2272 | *out_location = location; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2273 | return dex_cache.Get(); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2274 | } |
| 2275 | |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2276 | mirror::DexCache* ClassLinker::AllocAndInitializeDexCache(Thread* self, |
| 2277 | const DexFile& dex_file, |
| 2278 | LinearAlloc* linear_alloc) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2279 | ObjPtr<mirror::String> location = nullptr; |
| 2280 | ObjPtr<mirror::DexCache> dex_cache = AllocDexCache(&location, self, dex_file); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2281 | if (dex_cache != nullptr) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 2282 | WriterMutexLock mu(self, *Locks::dex_lock_); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2283 | DCHECK(location != nullptr); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 2284 | mirror::DexCache::InitializeDexCache(self, |
| 2285 | dex_cache, |
| 2286 | location, |
| 2287 | &dex_file, |
| 2288 | linear_alloc, |
| 2289 | image_pointer_size_); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2290 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2291 | return dex_cache.Ptr(); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2292 | } |
| 2293 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2294 | mirror::Class* ClassLinker::AllocClass(Thread* self, |
| 2295 | ObjPtr<mirror::Class> java_lang_Class, |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2296 | uint32_t class_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2297 | DCHECK_GE(class_size, sizeof(mirror::Class)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2298 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2299 | mirror::Class::InitializeClassVisitor visitor(class_size); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2300 | ObjPtr<mirror::Object> k = kMovingClasses ? |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 2301 | heap->AllocObject<true>(self, java_lang_Class, class_size, visitor) : |
| 2302 | heap->AllocNonMovableObject<true>(self, java_lang_Class, class_size, visitor); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2303 | if (UNLIKELY(k == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2304 | self->AssertPendingOOMException(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2305 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2306 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2307 | return k->AsClass(); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 2308 | } |
| 2309 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2310 | mirror::Class* ClassLinker::AllocClass(Thread* self, uint32_t class_size) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2311 | return AllocClass(self, GetClassRoot(kJavaLangClass), class_size); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2312 | } |
| 2313 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2314 | mirror::ObjectArray<mirror::StackTraceElement>* ClassLinker::AllocStackTraceElementArray( |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2315 | Thread* self, |
| 2316 | size_t length) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2317 | return mirror::ObjectArray<mirror::StackTraceElement>::Alloc( |
| 2318 | self, GetClassRoot(kJavaLangStackTraceElementArrayClass), length); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2319 | } |
| 2320 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2321 | mirror::Class* ClassLinker::EnsureResolved(Thread* self, |
| 2322 | const char* descriptor, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2323 | ObjPtr<mirror::Class> klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2324 | DCHECK(klass != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2325 | if (kIsDebugBuild) { |
| 2326 | StackHandleScope<1> hs(self); |
| 2327 | HandleWrapperObjPtr<mirror::Class> h = hs.NewHandleWrapper(&klass); |
| 2328 | Thread::PoisonObjectPointersIfDebug(); |
| 2329 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2330 | |
| 2331 | // For temporary classes we must wait for them to be retired. |
| 2332 | if (init_done_ && klass->IsTemp()) { |
| 2333 | CHECK(!klass->IsResolved()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2334 | if (klass->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2335 | ThrowEarlierClassFailure(klass); |
| 2336 | return nullptr; |
| 2337 | } |
| 2338 | StackHandleScope<1> hs(self); |
| 2339 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 2340 | ObjectLock<mirror::Class> lock(self, h_class); |
| 2341 | // Loop and wait for the resolving thread to retire this class. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2342 | while (!h_class->IsRetired() && !h_class->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2343 | lock.WaitIgnoringInterrupts(); |
| 2344 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2345 | if (h_class->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2346 | ThrowEarlierClassFailure(h_class.Get()); |
| 2347 | return nullptr; |
| 2348 | } |
| 2349 | CHECK(h_class->IsRetired()); |
| 2350 | // Get the updated class from class table. |
Andreas Gampe | 34ee684 | 2014-12-02 15:43:52 -0800 | [diff] [blame] | 2351 | klass = LookupClass(self, descriptor, h_class.Get()->GetClassLoader()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2352 | } |
| 2353 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2354 | // Wait for the class if it has not already been linked. |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2355 | size_t index = 0; |
| 2356 | // Maximum number of yield iterations until we start sleeping. |
| 2357 | static const size_t kNumYieldIterations = 1000; |
| 2358 | // How long each sleep is in us. |
| 2359 | static const size_t kSleepDurationUS = 1000; // 1 ms. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2360 | while (!klass->IsResolved() && !klass->IsErroneousUnresolved()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2361 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2362 | HandleWrapperObjPtr<mirror::Class> h_class(hs.NewHandleWrapper(&klass)); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2363 | { |
| 2364 | ObjectTryLock<mirror::Class> lock(self, h_class); |
| 2365 | // Can not use a monitor wait here since it may block when returning and deadlock if another |
| 2366 | // thread has locked klass. |
| 2367 | if (lock.Acquired()) { |
| 2368 | // Check for circular dependencies between classes, the lock is required for SetStatus. |
| 2369 | if (!h_class->IsResolved() && h_class->GetClinitThreadId() == self->GetTid()) { |
| 2370 | ThrowClassCircularityError(h_class.Get()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2371 | mirror::Class::SetStatus(h_class, mirror::Class::kStatusErrorUnresolved, self); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2372 | return nullptr; |
| 2373 | } |
| 2374 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2375 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2376 | { |
| 2377 | // Handle wrapper deals with klass moving. |
| 2378 | ScopedThreadSuspension sts(self, kSuspended); |
| 2379 | if (index < kNumYieldIterations) { |
| 2380 | sched_yield(); |
| 2381 | } else { |
| 2382 | usleep(kSleepDurationUS); |
| 2383 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2384 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2385 | ++index; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2386 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2387 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2388 | if (klass->IsErroneousUnresolved()) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2389 | ThrowEarlierClassFailure(klass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2390 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2391 | } |
| 2392 | // Return the loaded class. No exceptions should be pending. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2393 | CHECK(klass->IsResolved()) << klass->PrettyClass(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2394 | self->AssertNoPendingException(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2395 | return klass.Ptr(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2396 | } |
| 2397 | |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2398 | typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry; |
| 2399 | |
| 2400 | // Search a collection of DexFiles for a descriptor |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2401 | ClassPathEntry FindInClassPath(const char* descriptor, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2402 | size_t hash, const std::vector<const DexFile*>& class_path) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2403 | for (const DexFile* dex_file : class_path) { |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 2404 | const DexFile::ClassDef* dex_class_def = OatDexFile::FindClassDef(*dex_file, descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2405 | if (dex_class_def != nullptr) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2406 | return ClassPathEntry(dex_file, dex_class_def); |
| 2407 | } |
| 2408 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2409 | return ClassPathEntry(nullptr, nullptr); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2410 | } |
| 2411 | |
Nicolas Geoffray | 7d8d8ff | 2016-11-02 12:38:05 +0000 | [diff] [blame] | 2412 | bool ClassLinker::FindClassInBaseDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
| 2413 | Thread* self, |
| 2414 | const char* descriptor, |
| 2415 | size_t hash, |
| 2416 | Handle<mirror::ClassLoader> class_loader, |
| 2417 | ObjPtr<mirror::Class>* result) { |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2418 | // Termination case: boot class-loader. |
| 2419 | if (IsBootClassLoader(soa, class_loader.Get())) { |
| 2420 | // The boot class loader, search the boot class path. |
| 2421 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
| 2422 | if (pair.second != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2423 | ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, nullptr); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2424 | if (klass != nullptr) { |
| 2425 | *result = EnsureResolved(self, descriptor, klass); |
| 2426 | } else { |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 2427 | *result = DefineClass(self, |
| 2428 | descriptor, |
| 2429 | hash, |
| 2430 | ScopedNullHandle<mirror::ClassLoader>(), |
| 2431 | *pair.first, |
| 2432 | *pair.second); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2433 | } |
| 2434 | if (*result == nullptr) { |
| 2435 | CHECK(self->IsExceptionPending()) << descriptor; |
| 2436 | self->ClearException(); |
| 2437 | } |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2438 | } else { |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2439 | *result = nullptr; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2440 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2441 | return true; |
| 2442 | } |
| 2443 | |
| 2444 | // Unsupported class-loader? |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 2445 | if (soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader) != |
| 2446 | class_loader->GetClass()) { |
Nicolas Geoffray | 7d8d8ff | 2016-11-02 12:38:05 +0000 | [diff] [blame] | 2447 | // PathClassLoader is the most common case, so it's the one we check first. For secondary dex |
| 2448 | // files, we also check DexClassLoader here. |
| 2449 | if (soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_DexClassLoader) != |
| 2450 | class_loader->GetClass()) { |
| 2451 | *result = nullptr; |
| 2452 | return false; |
| 2453 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2454 | } |
| 2455 | |
| 2456 | // Handles as RegisterDexFile may allocate dex caches (and cause thread suspension). |
| 2457 | StackHandleScope<4> hs(self); |
| 2458 | Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent())); |
Nicolas Geoffray | 7d8d8ff | 2016-11-02 12:38:05 +0000 | [diff] [blame] | 2459 | bool recursive_result = FindClassInBaseDexClassLoader(soa, |
| 2460 | self, |
| 2461 | descriptor, |
| 2462 | hash, |
| 2463 | h_parent, |
| 2464 | result); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2465 | |
| 2466 | if (!recursive_result) { |
| 2467 | // Something wrong up the chain. |
| 2468 | return false; |
| 2469 | } |
| 2470 | |
| 2471 | if (*result != nullptr) { |
| 2472 | // Found the class up the chain. |
| 2473 | return true; |
| 2474 | } |
| 2475 | |
| 2476 | // Handle this step. |
| 2477 | // Handle as if this is the child PathClassLoader. |
| 2478 | // The class loader is a PathClassLoader which inherits from BaseDexClassLoader. |
| 2479 | // We need to get the DexPathList and loop through it. |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2480 | ArtField* const cookie_field = |
| 2481 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2482 | ArtField* const dex_file_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2483 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2484 | ObjPtr<mirror::Object> dex_path_list = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2485 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList)-> |
| 2486 | GetObject(class_loader.Get()); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2487 | if (dex_path_list != nullptr && dex_file_field != nullptr && cookie_field != nullptr) { |
| 2488 | // DexPathList has an array dexElements of Elements[] which each contain a dex file. |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2489 | ObjPtr<mirror::Object> dex_elements_obj = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2490 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements)-> |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2491 | GetObject(dex_path_list); |
| 2492 | // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look |
| 2493 | // at the mCookie which is a DexFile vector. |
| 2494 | if (dex_elements_obj != nullptr) { |
| 2495 | Handle<mirror::ObjectArray<mirror::Object>> dex_elements = |
| 2496 | hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>()); |
| 2497 | for (int32_t i = 0; i < dex_elements->GetLength(); ++i) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2498 | ObjPtr<mirror::Object> element = dex_elements->GetWithoutChecks(i); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2499 | if (element == nullptr) { |
| 2500 | // Should never happen, fall back to java code to throw a NPE. |
| 2501 | break; |
| 2502 | } |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2503 | ObjPtr<mirror::Object> dex_file = dex_file_field->GetObject(element); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2504 | if (dex_file != nullptr) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2505 | ObjPtr<mirror::LongArray> long_array = cookie_field->GetObject(dex_file)->AsLongArray(); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2506 | if (long_array == nullptr) { |
| 2507 | // This should never happen so log a warning. |
| 2508 | LOG(WARNING) << "Null DexFile::mCookie for " << descriptor; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2509 | break; |
| 2510 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2511 | int32_t long_array_size = long_array->GetLength(); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 2512 | // First element is the oat file. |
| 2513 | for (int32_t j = kDexFileIndexStart; j < long_array_size; ++j) { |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2514 | const DexFile* cp_dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>( |
| 2515 | long_array->GetWithoutChecks(j))); |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 2516 | const DexFile::ClassDef* dex_class_def = |
| 2517 | OatDexFile::FindClassDef(*cp_dex_file, descriptor, hash); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2518 | if (dex_class_def != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2519 | ObjPtr<mirror::Class> klass = DefineClass(self, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 2520 | descriptor, |
| 2521 | hash, |
| 2522 | class_loader, |
| 2523 | *cp_dex_file, |
| 2524 | *dex_class_def); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2525 | if (klass == nullptr) { |
| 2526 | CHECK(self->IsExceptionPending()) << descriptor; |
| 2527 | self->ClearException(); |
| 2528 | // TODO: Is it really right to break here, and not check the other dex files? |
| 2529 | return true; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2530 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2531 | *result = klass; |
| 2532 | return true; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2533 | } |
| 2534 | } |
| 2535 | } |
| 2536 | } |
| 2537 | } |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2538 | self->AssertNoPendingException(); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2539 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2540 | |
| 2541 | // Result is still null from the parent call, no need to set it again... |
| 2542 | return true; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2543 | } |
| 2544 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2545 | mirror::Class* ClassLinker::FindClass(Thread* self, |
| 2546 | const char* descriptor, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2547 | Handle<mirror::ClassLoader> class_loader) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 2548 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2549 | DCHECK(self != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2550 | self->AssertNoPendingException(); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 2551 | self->PoisonObjectPointers(); // For DefineClass, CreateArrayClass, etc... |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 2552 | if (descriptor[1] == '\0') { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2553 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 2554 | // for primitive classes that aren't backed by dex files. |
| 2555 | return FindPrimitiveClass(descriptor[0]); |
| 2556 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2557 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2558 | // Find the class in the loaded classes table. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2559 | ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, class_loader.Get()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2560 | if (klass != nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2561 | return EnsureResolved(self, descriptor, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2562 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2563 | // Class is not yet loaded. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2564 | if (descriptor[0] != '[' && class_loader == nullptr) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2565 | // Non-array class and the boot class loader, search the boot class path. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2566 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2567 | if (pair.second != nullptr) { |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 2568 | return DefineClass(self, |
| 2569 | descriptor, |
| 2570 | hash, |
| 2571 | ScopedNullHandle<mirror::ClassLoader>(), |
| 2572 | *pair.first, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2573 | *pair.second); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2574 | } else { |
| 2575 | // The boot class loader is searched ahead of the application class loader, failures are |
| 2576 | // expected and will be wrapped in a ClassNotFoundException. Use the pre-allocated error to |
| 2577 | // trigger the chaining with a proper stack trace. |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2578 | ObjPtr<mirror::Throwable> pre_allocated = |
| 2579 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2580 | self->SetException(pre_allocated); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2581 | return nullptr; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2582 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2583 | } |
| 2584 | ObjPtr<mirror::Class> result_ptr; |
| 2585 | bool descriptor_equals; |
| 2586 | if (descriptor[0] == '[') { |
| 2587 | result_ptr = CreateArrayClass(self, descriptor, hash, class_loader); |
| 2588 | DCHECK_EQ(result_ptr == nullptr, self->IsExceptionPending()); |
| 2589 | DCHECK(result_ptr == nullptr || result_ptr->DescriptorEquals(descriptor)); |
| 2590 | descriptor_equals = true; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2591 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2592 | ScopedObjectAccessUnchecked soa(self); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2593 | bool known_hierarchy = |
| 2594 | FindClassInBaseDexClassLoader(soa, self, descriptor, hash, class_loader, &result_ptr); |
| 2595 | if (result_ptr != nullptr) { |
| 2596 | // The chain was understood and we found the class. We still need to add the class to |
| 2597 | // the class table to protect from racy programs that can try and redefine the path list |
| 2598 | // which would change the Class<?> returned for subsequent evaluation of const-class. |
| 2599 | DCHECK(known_hierarchy); |
| 2600 | DCHECK(result_ptr->DescriptorEquals(descriptor)); |
| 2601 | descriptor_equals = true; |
| 2602 | } else { |
| 2603 | // Either the chain wasn't understood or the class wasn't found. |
| 2604 | // |
| 2605 | // If the chain was understood but we did not find the class, let the Java-side |
| 2606 | // rediscover all this and throw the exception with the right stack trace. Note that |
| 2607 | // the Java-side could still succeed for racy programs if another thread is actively |
| 2608 | // modifying the class loader's path list. |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2609 | |
Calin Juravle | ccd5695 | 2016-12-15 17:57:38 +0000 | [diff] [blame] | 2610 | if (!self->CanCallIntoJava()) { |
| 2611 | // Oops, we can't call into java so we can't run actual class-loader code. |
| 2612 | // This is true for e.g. for the compiler (jit or aot). |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2613 | ObjPtr<mirror::Throwable> pre_allocated = |
| 2614 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 2615 | self->SetException(pre_allocated); |
Vladimir Marko | 2c8c6b6 | 2016-12-01 17:42:00 +0000 | [diff] [blame] | 2616 | return nullptr; |
| 2617 | } |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2618 | |
Vladimir Marko | 5fdd778 | 2017-04-20 11:26:03 +0100 | [diff] [blame] | 2619 | // Inlined DescriptorToDot(descriptor) with extra validation. |
| 2620 | // |
| 2621 | // Throw NoClassDefFoundError early rather than potentially load a class only to fail |
| 2622 | // the DescriptorEquals() check below and give a confusing error message. For example, |
| 2623 | // when native code erroneously calls JNI GetFieldId() with signature "java/lang/String" |
| 2624 | // instead of "Ljava/lang/String;", the message below using the "dot" names would be |
| 2625 | // "class loader [...] returned class java.lang.String instead of java.lang.String". |
| 2626 | size_t descriptor_length = strlen(descriptor); |
| 2627 | if (UNLIKELY(descriptor[0] != 'L') || |
| 2628 | UNLIKELY(descriptor[descriptor_length - 1] != ';') || |
| 2629 | UNLIKELY(memchr(descriptor + 1, '.', descriptor_length - 2) != nullptr)) { |
| 2630 | ThrowNoClassDefFoundError("Invalid descriptor: %s.", descriptor); |
| 2631 | return nullptr; |
| 2632 | } |
| 2633 | std::string class_name_string(descriptor + 1, descriptor_length - 2); |
| 2634 | std::replace(class_name_string.begin(), class_name_string.end(), '/', '.'); |
| 2635 | |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2636 | ScopedLocalRef<jobject> class_loader_object( |
| 2637 | soa.Env(), soa.AddLocalReference<jobject>(class_loader.Get())); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2638 | ScopedLocalRef<jobject> result(soa.Env(), nullptr); |
| 2639 | { |
| 2640 | ScopedThreadStateChange tsc(self, kNative); |
| 2641 | ScopedLocalRef<jobject> class_name_object( |
| 2642 | soa.Env(), soa.Env()->NewStringUTF(class_name_string.c_str())); |
| 2643 | if (class_name_object.get() == nullptr) { |
| 2644 | DCHECK(self->IsExceptionPending()); // OOME. |
| 2645 | return nullptr; |
| 2646 | } |
| 2647 | CHECK(class_loader_object.get() != nullptr); |
| 2648 | result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(), |
| 2649 | WellKnownClasses::java_lang_ClassLoader_loadClass, |
| 2650 | class_name_object.get())); |
| 2651 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2652 | if (result.get() == nullptr && !self->IsExceptionPending()) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2653 | // broken loader - throw NPE to be compatible with Dalvik |
| 2654 | ThrowNullPointerException(StringPrintf("ClassLoader.loadClass returned null for %s", |
| 2655 | class_name_string.c_str()).c_str()); |
| 2656 | return nullptr; |
| 2657 | } |
| 2658 | result_ptr = soa.Decode<mirror::Class>(result.get()); |
| 2659 | // Check the name of the returned class. |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2660 | descriptor_equals = (result_ptr != nullptr) && result_ptr->DescriptorEquals(descriptor); |
Vladimir Marko | 2c8c6b6 | 2016-12-01 17:42:00 +0000 | [diff] [blame] | 2661 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2662 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2663 | |
| 2664 | if (self->IsExceptionPending()) { |
| 2665 | // If the ClassLoader threw or array class allocation failed, pass that exception up. |
| 2666 | // However, to comply with the RI behavior, first check if another thread succeeded. |
| 2667 | result_ptr = LookupClass(self, descriptor, hash, class_loader.Get()); |
| 2668 | if (result_ptr != nullptr && !result_ptr->IsErroneous()) { |
| 2669 | self->ClearException(); |
| 2670 | return EnsureResolved(self, descriptor, result_ptr); |
| 2671 | } |
| 2672 | return nullptr; |
| 2673 | } |
| 2674 | |
| 2675 | // Try to insert the class to the class table, checking for mismatch. |
| 2676 | ObjPtr<mirror::Class> old; |
| 2677 | { |
| 2678 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2679 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader.Get()); |
| 2680 | old = class_table->Lookup(descriptor, hash); |
| 2681 | if (old == nullptr) { |
| 2682 | old = result_ptr; // For the comparison below, after releasing the lock. |
| 2683 | if (descriptor_equals) { |
| 2684 | class_table->InsertWithHash(result_ptr.Ptr(), hash); |
| 2685 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader.Get()); |
| 2686 | } // else throw below, after releasing the lock. |
| 2687 | } |
| 2688 | } |
| 2689 | if (UNLIKELY(old != result_ptr)) { |
| 2690 | // Return `old` (even if `!descriptor_equals`) to mimic the RI behavior for parallel |
| 2691 | // capable class loaders. (All class loaders are considered parallel capable on Android.) |
| 2692 | mirror::Class* loader_class = class_loader->GetClass(); |
| 2693 | const char* loader_class_name = |
| 2694 | loader_class->GetDexFile().StringByTypeIdx(loader_class->GetDexTypeIndex()); |
| 2695 | LOG(WARNING) << "Initiating class loader of type " << DescriptorToDot(loader_class_name) |
| 2696 | << " is not well-behaved; it returned a different Class for racing loadClass(\"" |
| 2697 | << DescriptorToDot(descriptor) << "\")."; |
| 2698 | return EnsureResolved(self, descriptor, old); |
| 2699 | } |
| 2700 | if (UNLIKELY(!descriptor_equals)) { |
| 2701 | std::string result_storage; |
| 2702 | const char* result_name = result_ptr->GetDescriptor(&result_storage); |
| 2703 | std::string loader_storage; |
| 2704 | const char* loader_class_name = class_loader->GetClass()->GetDescriptor(&loader_storage); |
| 2705 | ThrowNoClassDefFoundError( |
| 2706 | "Initiating class loader of type %s returned class %s instead of %s.", |
| 2707 | DescriptorToDot(loader_class_name).c_str(), |
| 2708 | DescriptorToDot(result_name).c_str(), |
| 2709 | DescriptorToDot(descriptor).c_str()); |
| 2710 | return nullptr; |
| 2711 | } |
| 2712 | // success, return mirror::Class* |
| 2713 | return result_ptr.Ptr(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2714 | } |
| 2715 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2716 | mirror::Class* ClassLinker::DefineClass(Thread* self, |
| 2717 | const char* descriptor, |
| 2718 | size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2719 | Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2720 | const DexFile& dex_file, |
| 2721 | const DexFile::ClassDef& dex_class_def) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2722 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2723 | auto klass = hs.NewHandle<mirror::Class>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2724 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2725 | // Load the class from the dex file. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2726 | if (UNLIKELY(!init_done_)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2727 | // finish up init of hand crafted class_roots_ |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2728 | if (strcmp(descriptor, "Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2729 | klass.Assign(GetClassRoot(kJavaLangObject)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2730 | } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2731 | klass.Assign(GetClassRoot(kJavaLangClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2732 | } else if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2733 | klass.Assign(GetClassRoot(kJavaLangString)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 2734 | } else if (strcmp(descriptor, "Ljava/lang/ref/Reference;") == 0) { |
| 2735 | klass.Assign(GetClassRoot(kJavaLangRefReference)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2736 | } else if (strcmp(descriptor, "Ljava/lang/DexCache;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2737 | klass.Assign(GetClassRoot(kJavaLangDexCache)); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 2738 | } else if (strcmp(descriptor, "Ldalvik/system/ClassExt;") == 0) { |
| 2739 | klass.Assign(GetClassRoot(kDalvikSystemClassExt)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2740 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2741 | } |
| 2742 | |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2743 | if (klass == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2744 | // Allocate a class with the status of not ready. |
| 2745 | // Interface object should get the right size here. Regular class will |
| 2746 | // figure out the right size later and be replaced with one of the right |
| 2747 | // size when the class becomes resolved. |
| 2748 | klass.Assign(AllocClass(self, SizeOfClassWithoutEmbeddedTables(dex_file, dex_class_def))); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2749 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2750 | if (UNLIKELY(klass == nullptr)) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2751 | self->AssertPendingOOMException(); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2752 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2753 | } |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2754 | // Get the real dex file. This will return the input if there aren't any callbacks or they do |
| 2755 | // nothing. |
| 2756 | DexFile const* new_dex_file = nullptr; |
| 2757 | DexFile::ClassDef const* new_class_def = nullptr; |
| 2758 | // TODO We should ideally figure out some way to move this after we get a lock on the klass so it |
| 2759 | // will only be called once. |
| 2760 | Runtime::Current()->GetRuntimeCallbacks()->ClassPreDefine(descriptor, |
| 2761 | klass, |
| 2762 | class_loader, |
| 2763 | dex_file, |
| 2764 | dex_class_def, |
| 2765 | &new_dex_file, |
| 2766 | &new_class_def); |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 2767 | // Check to see if an exception happened during runtime callbacks. Return if so. |
| 2768 | if (self->IsExceptionPending()) { |
| 2769 | return nullptr; |
| 2770 | } |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2771 | ObjPtr<mirror::DexCache> dex_cache = RegisterDexFile(*new_dex_file, class_loader.Get()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2772 | if (dex_cache == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 2773 | self->AssertPendingException(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2774 | return nullptr; |
| 2775 | } |
| 2776 | klass->SetDexCache(dex_cache); |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2777 | SetupClass(*new_dex_file, *new_class_def, klass, class_loader.Get()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2778 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 2779 | // Mark the string class by setting its access flag. |
| 2780 | if (UNLIKELY(!init_done_)) { |
| 2781 | if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
| 2782 | klass->SetStringClass(); |
| 2783 | } |
| 2784 | } |
| 2785 | |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 2786 | ObjectLock<mirror::Class> lock(self, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2787 | klass->SetClinitThreadId(self->GetTid()); |
Mathieu Chartier | 1e4841e | 2016-12-15 14:21:04 -0800 | [diff] [blame] | 2788 | // Make sure we have a valid empty iftable even if there are errors. |
| 2789 | klass->SetIfTable(GetClassRoot(kJavaLangObject)->GetIfTable()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2790 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2791 | // Add the newly loaded class to the loaded classes table. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2792 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, klass.Get(), hash); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2793 | if (existing != nullptr) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2794 | // We failed to insert because we raced with another thread. Calling EnsureResolved may cause |
| 2795 | // this thread to block. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2796 | return EnsureResolved(self, descriptor, existing); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2797 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2798 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2799 | // Load the fields and other things after we are inserted in the table. This is so that we don't |
| 2800 | // end up allocating unfree-able linear alloc resources and then lose the race condition. The |
| 2801 | // other reason is that the field roots are only visited from the class table. So we need to be |
| 2802 | // inserted before we allocate / fill in these fields. |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2803 | LoadClass(self, *new_dex_file, *new_class_def, klass); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2804 | if (self->IsExceptionPending()) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2805 | VLOG(class_linker) << self->GetException()->Dump(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2806 | // An exception occured during load, set status to erroneous while holding klass' lock in case |
| 2807 | // notification is necessary. |
| 2808 | if (!klass->IsErroneous()) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2809 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorUnresolved, self); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2810 | } |
| 2811 | return nullptr; |
| 2812 | } |
| 2813 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2814 | // Finish loading (if necessary) by finding parents |
| 2815 | CHECK(!klass->IsLoaded()); |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2816 | if (!LoadSuperAndInterfaces(klass, *new_dex_file)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2817 | // Loading failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2818 | if (!klass->IsErroneous()) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2819 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorUnresolved, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2820 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2821 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2822 | } |
| 2823 | CHECK(klass->IsLoaded()); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 2824 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 2825 | // At this point the class is loaded. Publish a ClassLoad event. |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 2826 | // Note: this may be a temporary class. It is a listener's responsibility to handle this. |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 2827 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(klass); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 2828 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2829 | // Link the class (if necessary) |
| 2830 | CHECK(!klass->IsResolved()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2831 | // TODO: Use fast jobjects? |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2832 | auto interfaces = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2833 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 2834 | MutableHandle<mirror::Class> h_new_class = hs.NewHandle<mirror::Class>(nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2835 | if (!LinkClass(self, descriptor, klass, interfaces, &h_new_class)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2836 | // Linking failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2837 | if (!klass->IsErroneous()) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2838 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorUnresolved, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2839 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2840 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2841 | } |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 2842 | self->AssertNoPendingException(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2843 | CHECK(h_new_class != nullptr) << descriptor; |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2844 | CHECK(h_new_class->IsResolved() && !h_new_class->IsErroneousResolved()) << descriptor; |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2845 | |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 2846 | // Instrumentation may have updated entrypoints for all methods of all |
| 2847 | // classes. However it could not update methods of this class while we |
| 2848 | // were loading it. Now the class is resolved, we can update entrypoints |
| 2849 | // as required by instrumentation. |
| 2850 | if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) { |
| 2851 | // We must be in the kRunnable state to prevent instrumentation from |
| 2852 | // suspending all threads to update entrypoints while we are doing it |
| 2853 | // for this class. |
| 2854 | DCHECK_EQ(self->GetState(), kRunnable); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 2855 | Runtime::Current()->GetInstrumentation()->InstallStubsForClass(h_new_class.Get()); |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 2856 | } |
| 2857 | |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2858 | /* |
| 2859 | * We send CLASS_PREPARE events to the debugger from here. The |
| 2860 | * definition of "preparation" is creating the static fields for a |
| 2861 | * class and initializing them to the standard default values, but not |
| 2862 | * executing any code (that comes later, during "initialization"). |
| 2863 | * |
| 2864 | * We did the static preparation in LinkClass. |
| 2865 | * |
| 2866 | * The class has been prepared and resolved but possibly not yet verified |
| 2867 | * at this point. |
| 2868 | */ |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 2869 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(klass, h_new_class); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2870 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 2871 | // Notify native debugger of the new class and its layout. |
| 2872 | jit::Jit::NewTypeLoadedIfUsingJit(h_new_class.Get()); |
| 2873 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 2874 | return h_new_class.Get(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2875 | } |
| 2876 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2877 | uint32_t ClassLinker::SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, |
| 2878 | const DexFile::ClassDef& dex_class_def) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2879 | const uint8_t* class_data = dex_file.GetClassData(dex_class_def); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2880 | size_t num_ref = 0; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2881 | size_t num_8 = 0; |
| 2882 | size_t num_16 = 0; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2883 | size_t num_32 = 0; |
| 2884 | size_t num_64 = 0; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2885 | if (class_data != nullptr) { |
Vladimir Marko | 879d27b | 2016-03-15 20:31:50 +0000 | [diff] [blame] | 2886 | // We allow duplicate definitions of the same field in a class_data_item |
| 2887 | // but ignore the repeated indexes here, b/21868015. |
| 2888 | uint32_t last_field_idx = DexFile::kDexNoIndex; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2889 | for (ClassDataItemIterator it(dex_file, class_data); it.HasNextStaticField(); it.Next()) { |
Vladimir Marko | 879d27b | 2016-03-15 20:31:50 +0000 | [diff] [blame] | 2890 | uint32_t field_idx = it.GetMemberIndex(); |
| 2891 | // Ordering enforced by DexFileVerifier. |
| 2892 | DCHECK(last_field_idx == DexFile::kDexNoIndex || last_field_idx <= field_idx); |
| 2893 | if (UNLIKELY(field_idx == last_field_idx)) { |
| 2894 | continue; |
| 2895 | } |
| 2896 | last_field_idx = field_idx; |
| 2897 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2898 | const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2899 | char c = descriptor[0]; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2900 | switch (c) { |
| 2901 | case 'L': |
| 2902 | case '[': |
| 2903 | num_ref++; |
| 2904 | break; |
| 2905 | case 'J': |
| 2906 | case 'D': |
| 2907 | num_64++; |
| 2908 | break; |
| 2909 | case 'I': |
| 2910 | case 'F': |
| 2911 | num_32++; |
| 2912 | break; |
| 2913 | case 'S': |
| 2914 | case 'C': |
| 2915 | num_16++; |
| 2916 | break; |
| 2917 | case 'B': |
| 2918 | case 'Z': |
| 2919 | num_8++; |
| 2920 | break; |
| 2921 | default: |
| 2922 | LOG(FATAL) << "Unknown descriptor: " << c; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 2923 | UNREACHABLE(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2924 | } |
| 2925 | } |
| 2926 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2927 | return mirror::Class::ComputeClassSize(false, |
| 2928 | 0, |
| 2929 | num_8, |
| 2930 | num_16, |
| 2931 | num_32, |
| 2932 | num_64, |
| 2933 | num_ref, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2934 | image_pointer_size_); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2935 | } |
| 2936 | |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2937 | // Special case to get oat code without overwriting a trampoline. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2938 | const void* ClassLinker::GetQuickOatCodeFor(ArtMethod* method) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2939 | CHECK(method->IsInvokable()) << method->PrettyMethod(); |
Nicolas Geoffray | a7a4759 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 2940 | if (method->IsProxyMethod()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2941 | return GetQuickProxyInvokeHandler(); |
Jeff Hao | 8df6cea | 2013-07-29 13:54:48 -0700 | [diff] [blame] | 2942 | } |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 2943 | auto* code = method->GetOatMethodQuickCode(GetImagePointerSize()); |
| 2944 | if (code != nullptr) { |
| 2945 | return code; |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame] | 2946 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2947 | if (method->IsNative()) { |
| 2948 | // No code and native? Use generic trampoline. |
| 2949 | return GetQuickGenericJniStub(); |
| 2950 | } |
| 2951 | return GetQuickToInterpreterBridge(); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2952 | } |
| 2953 | |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2954 | bool ClassLinker::ShouldUseInterpreterEntrypoint(ArtMethod* method, const void* quick_code) { |
| 2955 | if (UNLIKELY(method->IsNative() || method->IsProxyMethod())) { |
| 2956 | return false; |
| 2957 | } |
| 2958 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2959 | if (quick_code == nullptr) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2960 | return true; |
| 2961 | } |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2962 | |
| 2963 | Runtime* runtime = Runtime::Current(); |
| 2964 | instrumentation::Instrumentation* instr = runtime->GetInstrumentation(); |
| 2965 | if (instr->InterpretOnly()) { |
| 2966 | return true; |
| 2967 | } |
| 2968 | |
| 2969 | if (runtime->GetClassLinker()->IsQuickToInterpreterBridge(quick_code)) { |
| 2970 | // Doing this check avoids doing compiled/interpreter transitions. |
| 2971 | return true; |
| 2972 | } |
| 2973 | |
| 2974 | if (Dbg::IsForcedInterpreterNeededForCalling(Thread::Current(), method)) { |
| 2975 | // Force the use of interpreter when it is required by the debugger. |
| 2976 | return true; |
| 2977 | } |
| 2978 | |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 2979 | if (runtime->IsJavaDebuggable()) { |
| 2980 | // For simplicity, we ignore precompiled code and go to the interpreter |
| 2981 | // assuming we don't already have jitted code. |
| 2982 | // We could look at the oat file where `quick_code` is being defined, |
| 2983 | // and check whether it's been compiled debuggable, but we decided to |
| 2984 | // only rely on the JIT for debuggable apps. |
Alex Light | 6b16d89 | 2016-11-11 11:21:04 -0800 | [diff] [blame] | 2985 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 2986 | return (jit == nullptr) || !jit->GetCodeCache()->ContainsPc(quick_code); |
| 2987 | } |
| 2988 | |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 2989 | if (runtime->IsNativeDebuggable()) { |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 2990 | DCHECK(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse()); |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 2991 | // If we are doing native debugging, ignore application's AOT code, |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 2992 | // since we want to JIT it (at first use) with extra stackmaps for native |
| 2993 | // debugging. We keep however all AOT code from the boot image, |
| 2994 | // since the JIT-at-first-use is blocking and would result in non-negligible |
| 2995 | // startup performance impact. |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 2996 | return !runtime->GetHeap()->IsInBootImageOatFile(quick_code); |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | return false; |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3000 | } |
| 3001 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3002 | void ClassLinker::FixupStaticTrampolines(ObjPtr<mirror::Class> klass) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3003 | DCHECK(klass->IsInitialized()) << klass->PrettyDescriptor(); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 3004 | if (klass->NumDirectMethods() == 0) { |
| 3005 | return; // No direct methods => no static methods. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3006 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3007 | Runtime* runtime = Runtime::Current(); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 3008 | if (!runtime->IsStarted()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 3009 | if (runtime->IsAotCompiler() || runtime->GetHeap()->HasBootImageSpace()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 3010 | return; // OAT file unavailable. |
| 3011 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3012 | } |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 3013 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 3014 | const DexFile& dex_file = klass->GetDexFile(); |
| 3015 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 3016 | CHECK(dex_class_def != nullptr); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 3017 | const uint8_t* class_data = dex_file.GetClassData(*dex_class_def); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 3018 | // There should always be class data if there were direct methods. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3019 | CHECK(class_data != nullptr) << klass->PrettyDescriptor(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3020 | ClassDataItemIterator it(dex_file, class_data); |
| 3021 | // Skip fields |
| 3022 | while (it.HasNextStaticField()) { |
| 3023 | it.Next(); |
| 3024 | } |
| 3025 | while (it.HasNextInstanceField()) { |
| 3026 | it.Next(); |
| 3027 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 3028 | bool has_oat_class; |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3029 | OatFile::OatClass oat_class = OatFile::FindOatClass(dex_file, |
| 3030 | klass->GetDexClassDefIndex(), |
| 3031 | &has_oat_class); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 3032 | // Link the code of methods skipped by LinkCode. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3033 | for (size_t method_index = 0; it.HasNextDirectMethod(); ++method_index, it.Next()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3034 | ArtMethod* method = klass->GetDirectMethod(method_index, image_pointer_size_); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3035 | if (!method->IsStatic()) { |
| 3036 | // Only update static methods. |
| 3037 | continue; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3038 | } |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3039 | const void* quick_code = nullptr; |
| 3040 | if (has_oat_class) { |
| 3041 | OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3042 | quick_code = oat_method.GetQuickCode(); |
| 3043 | } |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3044 | // Check whether the method is native, in which case it's generic JNI. |
| 3045 | if (quick_code == nullptr && method->IsNative()) { |
| 3046 | quick_code = GetQuickGenericJniStub(); |
| 3047 | } else if (ShouldUseInterpreterEntrypoint(method, quick_code)) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3048 | // Use interpreter entry point. |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3049 | quick_code = GetQuickToInterpreterBridge(); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3050 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 3051 | runtime->GetInstrumentation()->UpdateMethodsCode(method, quick_code); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3052 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3053 | // Ignore virtual methods on the iterator. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3054 | } |
| 3055 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3056 | // Does anything needed to make sure that the compiler will not generate a direct invoke to this |
| 3057 | // method. Should only be called on non-invokable methods. |
| 3058 | inline void EnsureThrowsInvocationError(ClassLinker* class_linker, ArtMethod* method) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3059 | DCHECK(method != nullptr); |
| 3060 | DCHECK(!method->IsInvokable()); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3061 | method->SetEntryPointFromQuickCompiledCodePtrSize( |
| 3062 | class_linker->GetQuickToInterpreterBridgeTrampoline(), |
| 3063 | class_linker->GetImagePointerSize()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3064 | } |
| 3065 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3066 | static void LinkCode(ClassLinker* class_linker, |
| 3067 | ArtMethod* method, |
| 3068 | const OatFile::OatClass* oat_class, |
| 3069 | uint32_t class_def_method_index) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3070 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3071 | if (runtime->IsAotCompiler()) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3072 | // The following code only applies to a non-compiler runtime. |
| 3073 | return; |
| 3074 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3075 | // Method shouldn't have already been linked. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3076 | DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3077 | if (oat_class != nullptr) { |
| 3078 | // Every kind of method should at least get an invoke stub from the oat_method. |
| 3079 | // non-abstract methods also get their code pointers. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 3080 | const OatFile::OatMethod oat_method = oat_class->GetOatMethod(class_def_method_index); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3081 | oat_method.LinkMethod(method); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3082 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3083 | |
Tamas Berghammer | 3a98aae | 2016-02-08 20:21:54 +0000 | [diff] [blame] | 3084 | // Install entry point from interpreter. |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3085 | const void* quick_code = method->GetEntryPointFromQuickCompiledCode(); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3086 | bool enter_interpreter = class_linker->ShouldUseInterpreterEntrypoint(method, quick_code); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 3087 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3088 | if (!method->IsInvokable()) { |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3089 | EnsureThrowsInvocationError(class_linker, method); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3090 | return; |
| 3091 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3092 | |
| 3093 | if (method->IsStatic() && !method->IsConstructor()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3094 | // For static methods excluding the class initializer, install the trampoline. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3095 | // It will be replaced by the proper entry point by ClassLinker::FixupStaticTrampolines |
| 3096 | // after initializing class (see ClassLinker::InitializeClass method). |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3097 | method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionStub()); |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3098 | } else if (quick_code == nullptr && method->IsNative()) { |
| 3099 | method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub()); |
Tamas Berghammer | 3a98aae | 2016-02-08 20:21:54 +0000 | [diff] [blame] | 3100 | } else if (enter_interpreter) { |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3101 | // Set entry point from compiled code if there's no code or in interpreter only mode. |
| 3102 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
Ian Rogers | 0d6de04 | 2012-02-29 08:50:26 -0800 | [diff] [blame] | 3103 | } |
jeffhao | 26c0a1a | 2012-01-17 16:28:33 -0800 | [diff] [blame] | 3104 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3105 | if (method->IsNative()) { |
| 3106 | // Unregistering restores the dlsym lookup stub. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3107 | method->UnregisterNative(); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 3108 | |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3109 | if (enter_interpreter || quick_code == nullptr) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3110 | // We have a native method here without code. Then it should have either the generic JNI |
| 3111 | // trampoline as entrypoint (non-static), or the resolution trampoline (static). |
| 3112 | // TODO: this doesn't handle all the cases where trampolines may be installed. |
| 3113 | const void* entry_point = method->GetEntryPointFromQuickCompiledCode(); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3114 | DCHECK(class_linker->IsQuickGenericJniStub(entry_point) || |
| 3115 | class_linker->IsQuickResolutionStub(entry_point)); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 3116 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3117 | } |
| 3118 | } |
| 3119 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3120 | void ClassLinker::SetupClass(const DexFile& dex_file, |
| 3121 | const DexFile::ClassDef& dex_class_def, |
| 3122 | Handle<mirror::Class> klass, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3123 | ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3124 | CHECK(klass != nullptr); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3125 | CHECK(klass->GetDexCache() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3126 | CHECK_EQ(mirror::Class::kStatusNotReady, klass->GetStatus()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 3127 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3128 | CHECK(descriptor != nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3129 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3130 | klass->SetClass(GetClassRoot(kJavaLangClass)); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 3131 | uint32_t access_flags = dex_class_def.GetJavaAccessFlags(); |
Brian Carlstrom | 8e3fb14 | 2013-10-09 21:00:27 -0700 | [diff] [blame] | 3132 | CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3133 | klass->SetAccessFlags(access_flags); |
| 3134 | klass->SetClassLoader(class_loader); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3135 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3136 | mirror::Class::SetStatus(klass, mirror::Class::kStatusIdx, nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3137 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3138 | klass->SetDexClassDefIndex(dex_file.GetIndexForClassDef(dex_class_def)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3139 | klass->SetDexTypeIndex(dex_class_def.class_idx_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3140 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3141 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3142 | void ClassLinker::LoadClass(Thread* self, |
| 3143 | const DexFile& dex_file, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3144 | const DexFile::ClassDef& dex_class_def, |
| 3145 | Handle<mirror::Class> klass) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 3146 | const uint8_t* class_data = dex_file.GetClassData(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3147 | if (class_data == nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3148 | return; // no fields or methods - for example a marker interface |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3149 | } |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3150 | LoadClassMembers(self, dex_file, class_data, klass); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 3151 | } |
| 3152 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3153 | LengthPrefixedArray<ArtField>* ClassLinker::AllocArtFieldArray(Thread* self, |
| 3154 | LinearAlloc* allocator, |
| 3155 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3156 | if (length == 0) { |
| 3157 | return nullptr; |
| 3158 | } |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3159 | // If the ArtField alignment changes, review all uses of LengthPrefixedArray<ArtField>. |
| 3160 | static_assert(alignof(ArtField) == 4, "ArtField alignment is expected to be 4."); |
| 3161 | size_t storage_size = LengthPrefixedArray<ArtField>::ComputeSize(length); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3162 | void* array_storage = allocator->Alloc(self, storage_size); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3163 | auto* ret = new(array_storage) LengthPrefixedArray<ArtField>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3164 | CHECK(ret != nullptr); |
| 3165 | std::uninitialized_fill_n(&ret->At(0), length, ArtField()); |
| 3166 | return ret; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3167 | } |
| 3168 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3169 | LengthPrefixedArray<ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self, |
| 3170 | LinearAlloc* allocator, |
| 3171 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3172 | if (length == 0) { |
| 3173 | return nullptr; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3174 | } |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 3175 | const size_t method_alignment = ArtMethod::Alignment(image_pointer_size_); |
| 3176 | const size_t method_size = ArtMethod::Size(image_pointer_size_); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3177 | const size_t storage_size = |
| 3178 | LengthPrefixedArray<ArtMethod>::ComputeSize(length, method_size, method_alignment); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3179 | void* array_storage = allocator->Alloc(self, storage_size); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3180 | auto* ret = new (array_storage) LengthPrefixedArray<ArtMethod>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3181 | CHECK(ret != nullptr); |
| 3182 | for (size_t i = 0; i < length; ++i) { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3183 | new(reinterpret_cast<void*>(&ret->At(i, method_size, method_alignment))) ArtMethod; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3184 | } |
| 3185 | return ret; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3186 | } |
| 3187 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3188 | LinearAlloc* ClassLinker::GetAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3189 | if (class_loader == nullptr) { |
| 3190 | return Runtime::Current()->GetLinearAlloc(); |
| 3191 | } |
| 3192 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3193 | DCHECK(allocator != nullptr); |
| 3194 | return allocator; |
| 3195 | } |
| 3196 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3197 | LinearAlloc* ClassLinker::GetOrCreateAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3198 | if (class_loader == nullptr) { |
| 3199 | return Runtime::Current()->GetLinearAlloc(); |
| 3200 | } |
| 3201 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3202 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3203 | if (allocator == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 3204 | RegisterClassLoader(class_loader); |
| 3205 | allocator = class_loader->GetAllocator(); |
| 3206 | CHECK(allocator != nullptr); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3207 | } |
| 3208 | return allocator; |
| 3209 | } |
| 3210 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3211 | void ClassLinker::LoadClassMembers(Thread* self, |
| 3212 | const DexFile& dex_file, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 3213 | const uint8_t* class_data, |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3214 | Handle<mirror::Class> klass) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3215 | { |
| 3216 | // Note: We cannot have thread suspension until the field and method arrays are setup or else |
| 3217 | // Class::VisitFieldRoots may miss some fields or methods. |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3218 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3219 | // Load static fields. |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3220 | // We allow duplicate definitions of the same field in a class_data_item |
| 3221 | // but ignore the repeated indexes here, b/21868015. |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3222 | LinearAlloc* const allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3223 | ClassDataItemIterator it(dex_file, class_data); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3224 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, |
| 3225 | allocator, |
| 3226 | it.NumStaticFields()); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3227 | size_t num_sfields = 0; |
| 3228 | uint32_t last_field_idx = 0u; |
| 3229 | for (; it.HasNextStaticField(); it.Next()) { |
| 3230 | uint32_t field_idx = it.GetMemberIndex(); |
| 3231 | DCHECK_GE(field_idx, last_field_idx); // Ordering enforced by DexFileVerifier. |
| 3232 | if (num_sfields == 0 || LIKELY(field_idx > last_field_idx)) { |
| 3233 | DCHECK_LT(num_sfields, it.NumStaticFields()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3234 | LoadField(it, klass, &sfields->At(num_sfields)); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3235 | ++num_sfields; |
| 3236 | last_field_idx = field_idx; |
| 3237 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3238 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3239 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3240 | // Load instance fields. |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3241 | LengthPrefixedArray<ArtField>* ifields = AllocArtFieldArray(self, |
| 3242 | allocator, |
| 3243 | it.NumInstanceFields()); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3244 | size_t num_ifields = 0u; |
| 3245 | last_field_idx = 0u; |
| 3246 | for (; it.HasNextInstanceField(); it.Next()) { |
| 3247 | uint32_t field_idx = it.GetMemberIndex(); |
| 3248 | DCHECK_GE(field_idx, last_field_idx); // Ordering enforced by DexFileVerifier. |
| 3249 | if (num_ifields == 0 || LIKELY(field_idx > last_field_idx)) { |
| 3250 | DCHECK_LT(num_ifields, it.NumInstanceFields()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3251 | LoadField(it, klass, &ifields->At(num_ifields)); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3252 | ++num_ifields; |
| 3253 | last_field_idx = field_idx; |
| 3254 | } |
| 3255 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3256 | |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3257 | if (UNLIKELY(num_sfields != it.NumStaticFields()) || |
| 3258 | UNLIKELY(num_ifields != it.NumInstanceFields())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3259 | LOG(WARNING) << "Duplicate fields in class " << klass->PrettyDescriptor() |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3260 | << " (unique static fields: " << num_sfields << "/" << it.NumStaticFields() |
| 3261 | << ", unique instance fields: " << num_ifields << "/" << it.NumInstanceFields() << ")"; |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 3262 | // NOTE: Not shrinking the over-allocated sfields/ifields, just setting size. |
| 3263 | if (sfields != nullptr) { |
| 3264 | sfields->SetSize(num_sfields); |
| 3265 | } |
| 3266 | if (ifields != nullptr) { |
| 3267 | ifields->SetSize(num_ifields); |
| 3268 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3269 | } |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 3270 | // Set the field arrays. |
| 3271 | klass->SetSFieldsPtr(sfields); |
| 3272 | DCHECK_EQ(klass->NumStaticFields(), num_sfields); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3273 | klass->SetIFieldsPtr(ifields); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3274 | DCHECK_EQ(klass->NumInstanceFields(), num_ifields); |
| 3275 | // Load methods. |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3276 | bool has_oat_class = false; |
| 3277 | const OatFile::OatClass oat_class = |
| 3278 | (Runtime::Current()->IsStarted() && !Runtime::Current()->IsAotCompiler()) |
| 3279 | ? OatFile::FindOatClass(dex_file, klass->GetDexClassDefIndex(), &has_oat_class) |
| 3280 | : OatFile::OatClass::Invalid(); |
| 3281 | const OatFile::OatClass* oat_class_ptr = has_oat_class ? &oat_class : nullptr; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 3282 | klass->SetMethodsPtr( |
| 3283 | AllocArtMethodArray(self, allocator, it.NumDirectMethods() + it.NumVirtualMethods()), |
| 3284 | it.NumDirectMethods(), |
| 3285 | it.NumVirtualMethods()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3286 | size_t class_def_method_index = 0; |
| 3287 | uint32_t last_dex_method_index = DexFile::kDexNoIndex; |
| 3288 | size_t last_class_def_method_index = 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 3289 | // TODO These should really use the iterators. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3290 | for (size_t i = 0; it.HasNextDirectMethod(); i++, it.Next()) { |
| 3291 | ArtMethod* method = klass->GetDirectMethodUnchecked(i, image_pointer_size_); |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3292 | LoadMethod(dex_file, it, klass, method); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3293 | LinkCode(this, method, oat_class_ptr, class_def_method_index); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3294 | uint32_t it_method_index = it.GetMemberIndex(); |
| 3295 | if (last_dex_method_index == it_method_index) { |
| 3296 | // duplicate case |
| 3297 | method->SetMethodIndex(last_class_def_method_index); |
| 3298 | } else { |
| 3299 | method->SetMethodIndex(class_def_method_index); |
| 3300 | last_dex_method_index = it_method_index; |
| 3301 | last_class_def_method_index = class_def_method_index; |
| 3302 | } |
| 3303 | class_def_method_index++; |
| 3304 | } |
| 3305 | for (size_t i = 0; it.HasNextVirtualMethod(); i++, it.Next()) { |
| 3306 | ArtMethod* method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3307 | LoadMethod(dex_file, it, klass, method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3308 | DCHECK_EQ(class_def_method_index, it.NumDirectMethods() + i); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3309 | LinkCode(this, method, oat_class_ptr, class_def_method_index); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3310 | class_def_method_index++; |
| 3311 | } |
| 3312 | DCHECK(!it.HasNext()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3313 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 3314 | // Ensure that the card is marked so that remembered sets pick up native roots. |
| 3315 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(klass.Get()); |
Mathieu Chartier | f3f2a7a | 2015-04-14 15:43:10 -0700 | [diff] [blame] | 3316 | self->AllowThreadSuspension(); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3317 | } |
| 3318 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3319 | void ClassLinker::LoadField(const ClassDataItemIterator& it, |
| 3320 | Handle<mirror::Class> klass, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3321 | ArtField* dst) { |
| 3322 | const uint32_t field_idx = it.GetMemberIndex(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3323 | dst->SetDexFieldIndex(field_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3324 | dst->SetDeclaringClass(klass.Get()); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 3325 | dst->SetAccessFlags(it.GetFieldAccessFlags()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3326 | } |
| 3327 | |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3328 | void ClassLinker::LoadMethod(const DexFile& dex_file, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3329 | const ClassDataItemIterator& it, |
| 3330 | Handle<mirror::Class> klass, |
| 3331 | ArtMethod* dst) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3332 | uint32_t dex_method_idx = it.GetMemberIndex(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3333 | const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3334 | const char* method_name = dex_file.StringDataByIdx(method_id.name_idx_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3335 | |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3336 | ScopedAssertNoThreadSuspension ants("LoadMethod"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3337 | dst->SetDexMethodIndex(dex_method_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3338 | dst->SetDeclaringClass(klass.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3339 | dst->SetCodeItemOffset(it.GetMethodCodeItemOffset()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3340 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 3341 | dst->SetDexCacheResolvedMethods(klass->GetDexCache()->GetResolvedMethods(), image_pointer_size_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3342 | |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 3343 | uint32_t access_flags = it.GetMethodAccessFlags(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3344 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3345 | if (UNLIKELY(strcmp("finalize", method_name) == 0)) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3346 | // Set finalizable flag on declaring class. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3347 | if (strcmp("V", dex_file.GetShorty(method_id.proto_idx_)) == 0) { |
| 3348 | // Void return type. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3349 | if (klass->GetClassLoader() != nullptr) { // All non-boot finalizer methods are flagged. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3350 | klass->SetFinalizable(); |
| 3351 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3352 | std::string temp; |
| 3353 | const char* klass_descriptor = klass->GetDescriptor(&temp); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3354 | // The Enum class declares a "final" finalize() method to prevent subclasses from |
| 3355 | // introducing a finalizer. We don't want to set the finalizable flag for Enum or its |
| 3356 | // subclasses, so we exclude it here. |
| 3357 | // We also want to avoid setting the flag on Object, where we know that finalize() is |
| 3358 | // empty. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3359 | if (strcmp(klass_descriptor, "Ljava/lang/Object;") != 0 && |
| 3360 | strcmp(klass_descriptor, "Ljava/lang/Enum;") != 0) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3361 | klass->SetFinalizable(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3362 | } |
| 3363 | } |
| 3364 | } |
| 3365 | } else if (method_name[0] == '<') { |
| 3366 | // Fix broken access flags for initializers. Bug 11157540. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3367 | bool is_init = (strcmp("<init>", method_name) == 0); |
| 3368 | bool is_clinit = !is_init && (strcmp("<clinit>", method_name) == 0); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3369 | if (UNLIKELY(!is_init && !is_clinit)) { |
| 3370 | LOG(WARNING) << "Unexpected '<' at start of method name " << method_name; |
| 3371 | } else { |
| 3372 | if (UNLIKELY((access_flags & kAccConstructor) == 0)) { |
| 3373 | LOG(WARNING) << method_name << " didn't have expected constructor access flag in class " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3374 | << klass->PrettyDescriptor() << " in dex file " << dex_file.GetLocation(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3375 | access_flags |= kAccConstructor; |
| 3376 | } |
| 3377 | } |
| 3378 | } |
| 3379 | dst->SetAccessFlags(access_flags); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3380 | } |
| 3381 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3382 | void ClassLinker::AppendToBootClassPath(Thread* self, const DexFile& dex_file) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3383 | ObjPtr<mirror::DexCache> dex_cache = AllocAndInitializeDexCache( |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3384 | self, |
| 3385 | dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3386 | Runtime::Current()->GetLinearAlloc()); |
| 3387 | CHECK(dex_cache != nullptr) << "Failed to allocate dex cache for " << dex_file.GetLocation(); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 3388 | AppendToBootClassPath(dex_file, dex_cache); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 3389 | } |
| 3390 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3391 | void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3392 | ObjPtr<mirror::DexCache> dex_cache) { |
| 3393 | CHECK(dex_cache != nullptr) << dex_file.GetLocation(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 3394 | boot_class_path_.push_back(&dex_file); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3395 | RegisterBootClassPathDexFile(dex_file, dex_cache); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3396 | } |
| 3397 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3398 | void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3399 | ObjPtr<mirror::DexCache> dex_cache, |
| 3400 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3401 | Thread* const self = Thread::Current(); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3402 | Locks::dex_lock_->AssertExclusiveHeld(self); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3403 | CHECK(dex_cache != nullptr) << dex_file.GetLocation(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3404 | // For app images, the dex cache location may be a suffix of the dex file location since the |
| 3405 | // dex file location is an absolute path. |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 3406 | const std::string dex_cache_location = dex_cache->GetLocation()->ToModifiedUtf8(); |
| 3407 | const size_t dex_cache_length = dex_cache_location.length(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3408 | CHECK_GT(dex_cache_length, 0u) << dex_file.GetLocation(); |
| 3409 | std::string dex_file_location = dex_file.GetLocation(); |
| 3410 | CHECK_GE(dex_file_location.length(), dex_cache_length) |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 3411 | << dex_cache_location << " " << dex_file.GetLocation(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3412 | // Take suffix. |
| 3413 | const std::string dex_file_suffix = dex_file_location.substr( |
| 3414 | dex_file_location.length() - dex_cache_length, |
| 3415 | dex_cache_length); |
| 3416 | // Example dex_cache location is SettingsProvider.apk and |
| 3417 | // dex file location is /system/priv-app/SettingsProvider/SettingsProvider.apk |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 3418 | CHECK_EQ(dex_cache_location, dex_file_suffix); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3419 | // Clean up pass to remove null dex caches. |
| 3420 | // Null dex caches can occur due to class unloading and we are lazily removing null entries. |
| 3421 | JavaVMExt* const vm = self->GetJniEnv()->vm; |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3422 | for (auto it = dex_caches_.begin(); it != dex_caches_.end(); ) { |
| 3423 | DexCacheData data = *it; |
| 3424 | if (self->IsJWeakCleared(data.weak_root)) { |
| 3425 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3426 | it = dex_caches_.erase(it); |
| 3427 | } else { |
| 3428 | ++it; |
| 3429 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3430 | } |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3431 | jweak dex_cache_jweak = vm->AddWeakGlobalRef(self, dex_cache); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3432 | dex_cache->SetDexFile(&dex_file); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3433 | DexCacheData data; |
| 3434 | data.weak_root = dex_cache_jweak; |
| 3435 | data.dex_file = dex_cache->GetDexFile(); |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 3436 | data.resolved_methods = dex_cache->GetResolvedMethods(); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3437 | data.class_table = ClassTableForClassLoader(class_loader); |
| 3438 | DCHECK(data.class_table != nullptr); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3439 | dex_caches_.push_back(data); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3440 | } |
| 3441 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3442 | ObjPtr<mirror::DexCache> ClassLinker::DecodeDexCache(Thread* self, const DexCacheData& data) { |
| 3443 | return data.IsValid() |
| 3444 | ? ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root)) |
| 3445 | : nullptr; |
| 3446 | } |
| 3447 | |
| 3448 | ObjPtr<mirror::DexCache> ClassLinker::EnsureSameClassLoader( |
| 3449 | Thread* self, |
| 3450 | ObjPtr<mirror::DexCache> dex_cache, |
| 3451 | const DexCacheData& data, |
| 3452 | ObjPtr<mirror::ClassLoader> class_loader) { |
| 3453 | DCHECK_EQ(dex_cache->GetDexFile(), data.dex_file); |
| 3454 | if (data.class_table != ClassTableForClassLoader(class_loader)) { |
| 3455 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
| 3456 | "Attempt to register dex file %s with multiple class loaders", |
| 3457 | data.dex_file->GetLocation().c_str()); |
| 3458 | return nullptr; |
| 3459 | } |
| 3460 | return dex_cache; |
| 3461 | } |
| 3462 | |
| 3463 | ObjPtr<mirror::DexCache> ClassLinker::RegisterDexFile(const DexFile& dex_file, |
| 3464 | ObjPtr<mirror::ClassLoader> class_loader) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3465 | Thread* self = Thread::Current(); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3466 | DexCacheData old_data; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3467 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3468 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3469 | old_data = FindDexCacheDataLocked(dex_file); |
| 3470 | } |
| 3471 | ObjPtr<mirror::DexCache> old_dex_cache = DecodeDexCache(self, old_data); |
| 3472 | if (old_dex_cache != nullptr) { |
| 3473 | return EnsureSameClassLoader(self, old_dex_cache, old_data, class_loader); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3474 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3475 | LinearAlloc* const linear_alloc = GetOrCreateAllocatorForClassLoader(class_loader); |
| 3476 | DCHECK(linear_alloc != nullptr); |
| 3477 | ClassTable* table; |
| 3478 | { |
| 3479 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3480 | table = InsertClassTableForClassLoader(class_loader); |
| 3481 | } |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3482 | // Don't alloc while holding the lock, since allocation may need to |
| 3483 | // suspend all threads and another thread may need the dex_lock_ to |
| 3484 | // get to a suspend point. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3485 | StackHandleScope<3> hs(self); |
| 3486 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3487 | ObjPtr<mirror::String> location; |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 3488 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(AllocDexCache(/*out*/&location, |
| 3489 | self, |
| 3490 | dex_file))); |
| 3491 | Handle<mirror::String> h_location(hs.NewHandle(location)); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3492 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3493 | WriterMutexLock mu(self, *Locks::dex_lock_); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3494 | old_data = FindDexCacheDataLocked(dex_file); |
| 3495 | old_dex_cache = DecodeDexCache(self, old_data); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3496 | if (old_dex_cache == nullptr && h_dex_cache != nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3497 | // Do InitializeDexCache while holding dex lock to make sure two threads don't call it at the |
| 3498 | // same time with the same dex cache. Since the .bss is shared this can cause failing DCHECK |
| 3499 | // that the arrays are null. |
| 3500 | mirror::DexCache::InitializeDexCache(self, |
| 3501 | h_dex_cache.Get(), |
| 3502 | h_location.Get(), |
| 3503 | &dex_file, |
| 3504 | linear_alloc, |
| 3505 | image_pointer_size_); |
| 3506 | RegisterDexFileLocked(dex_file, h_dex_cache.Get(), h_class_loader.Get()); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3507 | } |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3508 | } |
| 3509 | if (old_dex_cache != nullptr) { |
| 3510 | // Another thread managed to initialize the dex cache faster, so use that DexCache. |
| 3511 | // If this thread encountered OOME, ignore it. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3512 | DCHECK_EQ(h_dex_cache == nullptr, self->IsExceptionPending()); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3513 | self->ClearException(); |
| 3514 | // We cannot call EnsureSameClassLoader() while holding the dex_lock_. |
| 3515 | return EnsureSameClassLoader(self, old_dex_cache, old_data, h_class_loader.Get()); |
| 3516 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3517 | if (h_dex_cache == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3518 | self->AssertPendingOOMException(); |
| 3519 | return nullptr; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3520 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3521 | table->InsertStrongRoot(h_dex_cache.Get()); |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 3522 | if (h_class_loader.Get() != nullptr) { |
| 3523 | // Since we added a strong root to the class table, do the write barrier as required for |
| 3524 | // remembered sets and generational GCs. |
| 3525 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(h_class_loader.Get()); |
| 3526 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3527 | return h_dex_cache.Get(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3528 | } |
| 3529 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3530 | void ClassLinker::RegisterBootClassPathDexFile(const DexFile& dex_file, |
| 3531 | ObjPtr<mirror::DexCache> dex_cache) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3532 | WriterMutexLock mu(Thread::Current(), *Locks::dex_lock_); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3533 | RegisterDexFileLocked(dex_file, dex_cache, /* class_loader */ nullptr); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3534 | } |
| 3535 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3536 | bool ClassLinker::IsDexFileRegistered(Thread* self, const DexFile& dex_file) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3537 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3538 | return DecodeDexCache(self, FindDexCacheDataLocked(dex_file)) != nullptr; |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3539 | } |
| 3540 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3541 | ObjPtr<mirror::DexCache> ClassLinker::FindDexCache(Thread* self, const DexFile& dex_file) { |
| 3542 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
| 3543 | ObjPtr<mirror::DexCache> dex_cache = DecodeDexCache(self, FindDexCacheDataLocked(dex_file)); |
| 3544 | if (dex_cache != nullptr) { |
| 3545 | return dex_cache; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3546 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3547 | // Failure, dump diagnostic and abort. |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3548 | for (const DexCacheData& data : dex_caches_) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3549 | if (DecodeDexCache(self, data) != nullptr) { |
Andreas Gampe | 37c5846 | 2017-03-27 15:14:27 -0700 | [diff] [blame] | 3550 | LOG(FATAL_WITHOUT_ABORT) << "Registered dex file " << data.dex_file->GetLocation(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3551 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3552 | } |
Andreas Gampe | 37c5846 | 2017-03-27 15:14:27 -0700 | [diff] [blame] | 3553 | LOG(FATAL) << "Failed to find DexCache for DexFile " << dex_file.GetLocation(); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 3554 | UNREACHABLE(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3555 | } |
| 3556 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3557 | ClassTable* ClassLinker::FindClassTable(Thread* self, ObjPtr<mirror::DexCache> dex_cache) { |
| 3558 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 3559 | DCHECK(dex_file != nullptr); |
| 3560 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
| 3561 | // Search assuming unique-ness of dex file. |
| 3562 | for (const DexCacheData& data : dex_caches_) { |
| 3563 | // Avoid decoding (and read barriers) other unrelated dex caches. |
| 3564 | if (data.dex_file == dex_file) { |
| 3565 | ObjPtr<mirror::DexCache> registered_dex_cache = DecodeDexCache(self, data); |
| 3566 | if (registered_dex_cache != nullptr) { |
| 3567 | CHECK_EQ(registered_dex_cache, dex_cache) << dex_file->GetLocation(); |
| 3568 | return data.class_table; |
| 3569 | } |
| 3570 | } |
| 3571 | } |
| 3572 | return nullptr; |
| 3573 | } |
| 3574 | |
| 3575 | ClassLinker::DexCacheData ClassLinker::FindDexCacheDataLocked(const DexFile& dex_file) { |
| 3576 | // Search assuming unique-ness of dex file. |
| 3577 | for (const DexCacheData& data : dex_caches_) { |
| 3578 | // Avoid decoding (and read barriers) other unrelated dex caches. |
| 3579 | if (data.dex_file == &dex_file) { |
| 3580 | return data; |
| 3581 | } |
| 3582 | } |
| 3583 | return DexCacheData(); |
| 3584 | } |
| 3585 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3586 | void ClassLinker::FixupDexCaches(ArtMethod* resolution_method) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3587 | Thread* const self = Thread::Current(); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3588 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3589 | for (const DexCacheData& data : dex_caches_) { |
| 3590 | if (!self->IsJWeakCleared(data.weak_root)) { |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 3591 | ObjPtr<mirror::DexCache> dex_cache = ObjPtr<mirror::DexCache>::DownCast( |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3592 | self->DecodeJObject(data.weak_root)); |
| 3593 | if (dex_cache != nullptr) { |
| 3594 | dex_cache->Fixup(resolution_method, image_pointer_size_); |
| 3595 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3596 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3597 | } |
| 3598 | } |
| 3599 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3600 | mirror::Class* ClassLinker::CreatePrimitiveClass(Thread* self, Primitive::Type type) { |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 3601 | ObjPtr<mirror::Class> klass = |
| 3602 | AllocClass(self, mirror::Class::PrimitiveClassSize(image_pointer_size_)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3603 | if (UNLIKELY(klass == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3604 | self->AssertPendingOOMException(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3605 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3606 | } |
| 3607 | return InitializePrimitiveClass(klass, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3608 | } |
| 3609 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3610 | mirror::Class* ClassLinker::InitializePrimitiveClass(ObjPtr<mirror::Class> primitive_class, |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3611 | Primitive::Type type) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3612 | CHECK(primitive_class != nullptr); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3613 | // Must hold lock on object when initializing. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3614 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3615 | StackHandleScope<1> hs(self); |
| 3616 | Handle<mirror::Class> h_class(hs.NewHandle(primitive_class)); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3617 | ObjectLock<mirror::Class> lock(self, h_class); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3618 | h_class->SetAccessFlags(kAccPublic | kAccFinal | kAccAbstract); |
| 3619 | h_class->SetPrimitiveType(type); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 3620 | h_class->SetIfTable(GetClassRoot(kJavaLangObject)->GetIfTable()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3621 | mirror::Class::SetStatus(h_class, mirror::Class::kStatusInitialized, self); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3622 | const char* descriptor = Primitive::Descriptor(type); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 3623 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, |
| 3624 | h_class.Get(), |
| 3625 | ComputeModifiedUtf8Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3626 | CHECK(existing == nullptr) << "InitPrimitiveClass(" << type << ") failed"; |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3627 | return h_class.Get(); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 3628 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3629 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3630 | // Create an array class (i.e. the class object for the array, not the |
| 3631 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 3632 | // "[Ljava/lang/String;". |
| 3633 | // |
| 3634 | // If "descriptor" refers to an array of primitives, look up the |
| 3635 | // primitive type's internally-generated class object. |
| 3636 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3637 | // "class_loader" is the class loader of the class that's referring to |
| 3638 | // us. It's used to ensure that we're looking for the element type in |
| 3639 | // the right context. It does NOT become the class loader for the |
| 3640 | // array class; that always comes from the base element class. |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3641 | // |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 3642 | // Returns null with an exception raised on failure. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 3643 | mirror::Class* ClassLinker::CreateArrayClass(Thread* self, const char* descriptor, size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3644 | Handle<mirror::ClassLoader> class_loader) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3645 | // Identify the underlying component type |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3646 | CHECK_EQ('[', descriptor[0]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3647 | StackHandleScope<2> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3648 | MutableHandle<mirror::Class> component_type(hs.NewHandle(FindClass(self, descriptor + 1, |
| 3649 | class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3650 | if (component_type == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3651 | DCHECK(self->IsExceptionPending()); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 3652 | // We need to accept erroneous classes as component types. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3653 | const size_t component_hash = ComputeModifiedUtf8Hash(descriptor + 1); |
| 3654 | component_type.Assign(LookupClass(self, descriptor + 1, component_hash, class_loader.Get())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3655 | if (component_type == nullptr) { |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 3656 | DCHECK(self->IsExceptionPending()); |
| 3657 | return nullptr; |
| 3658 | } else { |
| 3659 | self->ClearException(); |
| 3660 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3661 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 3662 | if (UNLIKELY(component_type->IsPrimitiveVoid())) { |
| 3663 | ThrowNoClassDefFoundError("Attempt to create array of void primitive type"); |
| 3664 | return nullptr; |
| 3665 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3666 | // See if the component type is already loaded. Array classes are |
| 3667 | // always associated with the class loader of their underlying |
| 3668 | // element type -- an array of Strings goes with the loader for |
| 3669 | // java/lang/String -- so we need to look for it there. (The |
| 3670 | // caller should have checked for the existence of the class |
| 3671 | // before calling here, but they did so with *their* class loader, |
| 3672 | // not the component type's loader.) |
| 3673 | // |
| 3674 | // If we find it, the caller adds "loader" to the class' initiating |
| 3675 | // loader list, which should prevent us from going through this again. |
| 3676 | // |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3677 | // This call is unnecessary if "loader" and "component_type->GetClassLoader()" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3678 | // are the same, because our caller (FindClass) just did the |
| 3679 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 3680 | // because we effectively do this lookup again when we add the new |
| 3681 | // class to the hash table --- necessary because of possible races with |
| 3682 | // other threads.) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3683 | if (class_loader.Get() != component_type->GetClassLoader()) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3684 | ObjPtr<mirror::Class> new_class = |
| 3685 | LookupClass(self, descriptor, hash, component_type->GetClassLoader()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3686 | if (new_class != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3687 | return new_class.Ptr(); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3688 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3689 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3690 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3691 | // Fill out the fields in the Class. |
| 3692 | // |
| 3693 | // It is possible to execute some methods against arrays, because |
| 3694 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 3695 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 3696 | // |
| 3697 | // Array classes are simple enough that we don't need to do a full |
| 3698 | // link step. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3699 | auto new_class = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3700 | if (UNLIKELY(!init_done_)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3701 | // Classes that were hand created, ie not by FindSystemClass |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3702 | if (strcmp(descriptor, "[Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3703 | new_class.Assign(GetClassRoot(kClassArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3704 | } else if (strcmp(descriptor, "[Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3705 | new_class.Assign(GetClassRoot(kObjectArrayClass)); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3706 | } else if (strcmp(descriptor, GetClassRootDescriptor(kJavaLangStringArrayClass)) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3707 | new_class.Assign(GetClassRoot(kJavaLangStringArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3708 | } else if (strcmp(descriptor, "[C") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3709 | new_class.Assign(GetClassRoot(kCharArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3710 | } else if (strcmp(descriptor, "[I") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3711 | new_class.Assign(GetClassRoot(kIntArrayClass)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3712 | } else if (strcmp(descriptor, "[J") == 0) { |
| 3713 | new_class.Assign(GetClassRoot(kLongArrayClass)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3714 | } |
| 3715 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3716 | if (new_class == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3717 | new_class.Assign(AllocClass(self, mirror::Array::ClassSize(image_pointer_size_))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3718 | if (new_class == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3719 | self->AssertPendingOOMException(); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3720 | return nullptr; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3721 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3722 | new_class->SetComponentType(component_type.Get()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3723 | } |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3724 | ObjectLock<mirror::Class> lock(self, new_class); // Must hold lock on object when initializing. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3725 | DCHECK(new_class->GetComponentType() != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3726 | ObjPtr<mirror::Class> java_lang_Object = GetClassRoot(kJavaLangObject); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3727 | new_class->SetSuperClass(java_lang_Object); |
| 3728 | new_class->SetVTable(java_lang_Object->GetVTable()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3729 | new_class->SetPrimitiveType(Primitive::kPrimNot); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3730 | new_class->SetClassLoader(component_type->GetClassLoader()); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 3731 | if (component_type->IsPrimitive()) { |
| 3732 | new_class->SetClassFlags(mirror::kClassFlagNoReferenceFields); |
| 3733 | } else { |
| 3734 | new_class->SetClassFlags(mirror::kClassFlagObjectArray); |
| 3735 | } |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3736 | mirror::Class::SetStatus(new_class, mirror::Class::kStatusLoaded, self); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 3737 | new_class->PopulateEmbeddedVTable(image_pointer_size_); |
| 3738 | ImTable* object_imt = java_lang_Object->GetImt(image_pointer_size_); |
| 3739 | new_class->SetImt(object_imt, image_pointer_size_); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3740 | mirror::Class::SetStatus(new_class, mirror::Class::kStatusInitialized, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3741 | // don't need to set new_class->SetObjectSize(..) |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 3742 | // because Object::SizeOf delegates to Array::SizeOf |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3743 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3744 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 3745 | // interfaces. We need to set that up here, so that stuff like |
| 3746 | // "instanceof" works right. |
| 3747 | // |
| 3748 | // Note: The GC could run during the call to FindSystemClass, |
| 3749 | // so we need to make sure the class object is GC-valid while we're in |
| 3750 | // there. Do this by clearing the interface list so the GC will just |
| 3751 | // think that the entries are null. |
| 3752 | |
| 3753 | |
| 3754 | // Use the single, global copies of "interfaces" and "iftable" |
| 3755 | // (remember not to free them for arrays). |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3756 | { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3757 | ObjPtr<mirror::IfTable> array_iftable = array_iftable_.Read(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3758 | CHECK(array_iftable != nullptr); |
| 3759 | new_class->SetIfTable(array_iftable); |
| 3760 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3761 | |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 3762 | // Inherit access flags from the component type. |
| 3763 | int access_flags = new_class->GetComponentType()->GetAccessFlags(); |
| 3764 | // Lose any implementation detail flags; in particular, arrays aren't finalizable. |
| 3765 | access_flags &= kAccJavaFlagsMask; |
| 3766 | // Arrays can't be used as a superclass or interface, so we want to add "abstract final" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3767 | // and remove "interface". |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 3768 | access_flags |= kAccAbstract | kAccFinal; |
| 3769 | access_flags &= ~kAccInterface; |
| 3770 | |
| 3771 | new_class->SetAccessFlags(access_flags); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3772 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3773 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, new_class.Get(), hash); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3774 | if (existing == nullptr) { |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 3775 | // We postpone ClassLoad and ClassPrepare events to this point in time to avoid |
| 3776 | // duplicate events in case of races. Array classes don't really follow dedicated |
| 3777 | // load and prepare, anyways. |
| 3778 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(new_class); |
| 3779 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(new_class, new_class); |
| 3780 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 3781 | jit::Jit::NewTypeLoadedIfUsingJit(new_class.Get()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3782 | return new_class.Get(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3783 | } |
| 3784 | // Another thread must have loaded the class after we |
| 3785 | // started but before we finished. Abandon what we've |
| 3786 | // done. |
| 3787 | // |
| 3788 | // (Yes, this happens.) |
| 3789 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3790 | return existing.Ptr(); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3791 | } |
| 3792 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3793 | mirror::Class* ClassLinker::FindPrimitiveClass(char type) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3794 | switch (type) { |
| 3795 | case 'B': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3796 | return GetClassRoot(kPrimitiveByte); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3797 | case 'C': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3798 | return GetClassRoot(kPrimitiveChar); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3799 | case 'D': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3800 | return GetClassRoot(kPrimitiveDouble); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3801 | case 'F': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3802 | return GetClassRoot(kPrimitiveFloat); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3803 | case 'I': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3804 | return GetClassRoot(kPrimitiveInt); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3805 | case 'J': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3806 | return GetClassRoot(kPrimitiveLong); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3807 | case 'S': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3808 | return GetClassRoot(kPrimitiveShort); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3809 | case 'Z': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3810 | return GetClassRoot(kPrimitiveBoolean); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3811 | case 'V': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3812 | return GetClassRoot(kPrimitiveVoid); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3813 | default: |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3814 | break; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 3815 | } |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 3816 | std::string printable_type(PrintableChar(type)); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 3817 | ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3818 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3819 | } |
| 3820 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3821 | mirror::Class* ClassLinker::InsertClass(const char* descriptor, ObjPtr<mirror::Class> klass, size_t hash) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3822 | if (VLOG_IS_ON(class_linker)) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3823 | ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3824 | std::string source; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3825 | if (dex_cache != nullptr) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3826 | source += " from "; |
| 3827 | source += dex_cache->GetLocation()->ToModifiedUtf8(); |
| 3828 | } |
| 3829 | LOG(INFO) << "Loaded class " << descriptor << source; |
| 3830 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3831 | { |
| 3832 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3833 | ObjPtr<mirror::ClassLoader> const class_loader = klass->GetClassLoader(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3834 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3835 | ObjPtr<mirror::Class> existing = class_table->Lookup(descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3836 | if (existing != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3837 | return existing.Ptr(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3838 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3839 | VerifyObject(klass); |
| 3840 | class_table->InsertWithHash(klass, hash); |
| 3841 | if (class_loader != nullptr) { |
| 3842 | // This is necessary because we need to have the card dirtied for remembered sets. |
| 3843 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader); |
| 3844 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 3845 | if (log_new_roots_) { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3846 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3847 | } |
| 3848 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3849 | if (kIsDebugBuild) { |
| 3850 | // Test that copied methods correctly can find their holder. |
| 3851 | for (ArtMethod& method : klass->GetCopiedMethods(image_pointer_size_)) { |
| 3852 | CHECK_EQ(GetHoldingClassOfCopiedMethod(&method), klass); |
| 3853 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3854 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3855 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3856 | } |
| 3857 | |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 3858 | void ClassLinker::WriteBarrierForBootOatFileBssRoots(const OatFile* oat_file) { |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 3859 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3860 | DCHECK(!oat_file->GetBssGcRoots().empty()) << oat_file->GetLocation(); |
| 3861 | if (log_new_roots_ && !ContainsElement(new_bss_roots_boot_oat_files_, oat_file)) { |
| 3862 | new_bss_roots_boot_oat_files_.push_back(oat_file); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 3863 | } |
| 3864 | } |
| 3865 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 3866 | // TODO This should really be in mirror::Class. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3867 | void ClassLinker::UpdateClassMethods(ObjPtr<mirror::Class> klass, |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 3868 | LengthPrefixedArray<ArtMethod>* new_methods) { |
| 3869 | klass->SetMethodsPtrUnchecked(new_methods, |
| 3870 | klass->NumDirectMethods(), |
| 3871 | klass->NumDeclaredVirtualMethods()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3872 | // Need to mark the card so that the remembered sets and mod union tables get updated. |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 3873 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(klass); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3874 | } |
| 3875 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3876 | mirror::Class* ClassLinker::LookupClass(Thread* self, |
| 3877 | const char* descriptor, |
| 3878 | size_t hash, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3879 | ObjPtr<mirror::ClassLoader> class_loader) { |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 3880 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3881 | ClassTable* const class_table = ClassTableForClassLoader(class_loader); |
| 3882 | if (class_table != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3883 | ObjPtr<mirror::Class> result = class_table->Lookup(descriptor, hash); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 3884 | if (result != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3885 | return result.Ptr(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3886 | } |
Sameer Abu Asal | 2c6de22 | 2013-05-02 17:38:59 -0700 | [diff] [blame] | 3887 | } |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 3888 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3889 | } |
| 3890 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3891 | class MoveClassTableToPreZygoteVisitor : public ClassLoaderVisitor { |
| 3892 | public: |
| 3893 | explicit MoveClassTableToPreZygoteVisitor() {} |
| 3894 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3895 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3896 | REQUIRES(Locks::classlinker_classes_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3897 | REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3898 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3899 | if (class_table != nullptr) { |
| 3900 | class_table->FreezeSnapshot(); |
| 3901 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 3902 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3903 | }; |
| 3904 | |
| 3905 | void ClassLinker::MoveClassTableToPreZygote() { |
| 3906 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame^] | 3907 | boot_class_table_->FreezeSnapshot(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3908 | MoveClassTableToPreZygoteVisitor visitor; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3909 | VisitClassLoaders(&visitor); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3910 | } |
| 3911 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3912 | // Look up classes by hash and descriptor and put all matching ones in the result array. |
| 3913 | class LookupClassesVisitor : public ClassLoaderVisitor { |
| 3914 | public: |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3915 | LookupClassesVisitor(const char* descriptor, |
| 3916 | size_t hash, |
| 3917 | std::vector<ObjPtr<mirror::Class>>* result) |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3918 | : descriptor_(descriptor), |
| 3919 | hash_(hash), |
| 3920 | result_(result) {} |
| 3921 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3922 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3923 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) OVERRIDE { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3924 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3925 | ObjPtr<mirror::Class> klass = class_table->Lookup(descriptor_, hash_); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 3926 | // Add `klass` only if `class_loader` is its defining (not just initiating) class loader. |
| 3927 | if (klass != nullptr && klass->GetClassLoader() == class_loader) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3928 | result_->push_back(klass); |
| 3929 | } |
| 3930 | } |
| 3931 | |
| 3932 | private: |
| 3933 | const char* const descriptor_; |
| 3934 | const size_t hash_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3935 | std::vector<ObjPtr<mirror::Class>>* const result_; |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3936 | }; |
| 3937 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3938 | void ClassLinker::LookupClasses(const char* descriptor, |
| 3939 | std::vector<ObjPtr<mirror::Class>>& result) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3940 | result.clear(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3941 | Thread* const self = Thread::Current(); |
| 3942 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3943 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame^] | 3944 | ObjPtr<mirror::Class> klass = boot_class_table_->Lookup(descriptor, hash); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3945 | if (klass != nullptr) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 3946 | DCHECK(klass->GetClassLoader() == nullptr); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3947 | result.push_back(klass); |
| 3948 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3949 | LookupClassesVisitor visitor(descriptor, hash, &result); |
| 3950 | VisitClassLoaders(&visitor); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 3951 | } |
| 3952 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3953 | bool ClassLinker::AttemptSupertypeVerification(Thread* self, |
| 3954 | Handle<mirror::Class> klass, |
| 3955 | Handle<mirror::Class> supertype) { |
| 3956 | DCHECK(self != nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3957 | DCHECK(klass != nullptr); |
| 3958 | DCHECK(supertype != nullptr); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3959 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3960 | if (!supertype->IsVerified() && !supertype->IsErroneous()) { |
| 3961 | VerifyClass(self, supertype); |
| 3962 | } |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 3963 | |
| 3964 | if (supertype->IsVerified() || supertype->ShouldVerifyAtRuntime()) { |
| 3965 | // The supertype is either verified, or we soft failed at AOT time. |
| 3966 | DCHECK(supertype->IsVerified() || Runtime::Current()->IsAotCompiler()); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3967 | return true; |
| 3968 | } |
| 3969 | // If we got this far then we have a hard failure. |
| 3970 | std::string error_msg = |
| 3971 | StringPrintf("Rejecting class %s that attempts to sub-type erroneous class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3972 | klass->PrettyDescriptor().c_str(), |
| 3973 | supertype->PrettyDescriptor().c_str()); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3974 | LOG(WARNING) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3975 | StackHandleScope<1> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3976 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3977 | if (cause != nullptr) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3978 | // Set during VerifyClass call (if at all). |
| 3979 | self->ClearException(); |
| 3980 | } |
| 3981 | // Change into a verify error. |
| 3982 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3983 | if (cause != nullptr) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3984 | self->GetException()->SetCause(cause.Get()); |
| 3985 | } |
| 3986 | ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex()); |
| 3987 | if (Runtime::Current()->IsAotCompiler()) { |
| 3988 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
| 3989 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3990 | // Need to grab the lock to change status. |
| 3991 | ObjectLock<mirror::Class> super_lock(self, klass); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 3992 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3993 | return false; |
| 3994 | } |
| 3995 | |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3996 | // Ensures that methods have the kAccSkipAccessChecks bit set. We use the |
| 3997 | // kAccVerificationAttempted bit on the class access flags to determine whether this has been done |
| 3998 | // before. |
| 3999 | static void EnsureSkipAccessChecksMethods(Handle<mirror::Class> klass, PointerSize pointer_size) |
| 4000 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 4001 | if (!klass->WasVerificationAttempted()) { |
| 4002 | klass->SetSkipAccessChecksFlagOnAllMethods(pointer_size); |
| 4003 | klass->SetVerificationAttempted(); |
| 4004 | } |
| 4005 | } |
| 4006 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4007 | verifier::FailureKind ClassLinker::VerifyClass( |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 4008 | Thread* self, Handle<mirror::Class> klass, verifier::HardFailLogMode log_level) { |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4009 | { |
| 4010 | // TODO: assert that the monitor on the Class is held |
| 4011 | ObjectLock<mirror::Class> lock(self, klass); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 4012 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4013 | // Is somebody verifying this now? |
| 4014 | mirror::Class::Status old_status = klass->GetStatus(); |
| 4015 | while (old_status == mirror::Class::kStatusVerifying || |
| 4016 | old_status == mirror::Class::kStatusVerifyingAtRuntime) { |
| 4017 | lock.WaitIgnoringInterrupts(); |
Alex Light | 06039ba | 2016-04-04 13:23:14 -0700 | [diff] [blame] | 4018 | CHECK(klass->IsErroneous() || (klass->GetStatus() > old_status)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4019 | << "Class '" << klass->PrettyClass() |
| 4020 | << "' performed an illegal verification state transition from " << old_status |
| 4021 | << " to " << klass->GetStatus(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4022 | old_status = klass->GetStatus(); |
| 4023 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 4024 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4025 | // The class might already be erroneous, for example at compile time if we attempted to verify |
| 4026 | // this class as a parent to another. |
| 4027 | if (klass->IsErroneous()) { |
| 4028 | ThrowEarlierClassFailure(klass.Get()); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4029 | return verifier::FailureKind::kHardFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4030 | } |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 4031 | |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4032 | // Don't attempt to re-verify if already verified. |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4033 | if (klass->IsVerified()) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4034 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4035 | return verifier::FailureKind::kNoFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4036 | } |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4037 | |
| 4038 | // For AOT, don't attempt to re-verify if we have already found we should |
| 4039 | // verify at runtime. |
| 4040 | if (Runtime::Current()->IsAotCompiler() && klass->ShouldVerifyAtRuntime()) { |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4041 | return verifier::FailureKind::kSoftFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4042 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 4043 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4044 | if (klass->GetStatus() == mirror::Class::kStatusResolved) { |
| 4045 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerifying, self); |
| 4046 | } else { |
| 4047 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4048 | << klass->PrettyClass(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4049 | CHECK(!Runtime::Current()->IsAotCompiler()); |
| 4050 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerifyingAtRuntime, self); |
| 4051 | } |
| 4052 | |
| 4053 | // Skip verification if disabled. |
| 4054 | if (!Runtime::Current()->IsVerificationEnabled()) { |
| 4055 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, self); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4056 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4057 | return verifier::FailureKind::kNoFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4058 | } |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 4059 | } |
| 4060 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 4061 | // Verify super class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4062 | StackHandleScope<2> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4063 | MutableHandle<mirror::Class> supertype(hs.NewHandle(klass->GetSuperClass())); |
| 4064 | // If we have a superclass and we get a hard verification failure we can return immediately. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4065 | if (supertype != nullptr && !AttemptSupertypeVerification(self, klass, supertype)) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4066 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4067 | return verifier::FailureKind::kHardFailure; |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4068 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4069 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4070 | // Verify all default super-interfaces. |
| 4071 | // |
| 4072 | // (1) Don't bother if the superclass has already had a soft verification failure. |
| 4073 | // |
| 4074 | // (2) Interfaces shouldn't bother to do this recursive verification because they cannot cause |
| 4075 | // recursive initialization by themselves. This is because when an interface is initialized |
| 4076 | // directly it must not initialize its superinterfaces. We are allowed to verify regardless |
| 4077 | // but choose not to for an optimization. If the interfaces is being verified due to a class |
| 4078 | // initialization (which would need all the default interfaces to be verified) the class code |
| 4079 | // will trigger the recursive verification anyway. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4080 | if ((supertype == nullptr || supertype->IsVerified()) // See (1) |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4081 | && !klass->IsInterface()) { // See (2) |
| 4082 | int32_t iftable_count = klass->GetIfTableCount(); |
| 4083 | MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 4084 | // Loop through all interfaces this class has defined. It doesn't matter the order. |
| 4085 | for (int32_t i = 0; i < iftable_count; i++) { |
| 4086 | iface.Assign(klass->GetIfTable()->GetInterface(i)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4087 | DCHECK(iface != nullptr); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4088 | // We only care if we have default interfaces and can skip if we are already verified... |
| 4089 | if (LIKELY(!iface->HasDefaultMethods() || iface->IsVerified())) { |
| 4090 | continue; |
| 4091 | } else if (UNLIKELY(!AttemptSupertypeVerification(self, klass, iface))) { |
| 4092 | // We had a hard failure while verifying this interface. Just return immediately. |
| 4093 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4094 | return verifier::FailureKind::kHardFailure; |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4095 | } else if (UNLIKELY(!iface->IsVerified())) { |
| 4096 | // We softly failed to verify the iface. Stop checking and clean up. |
| 4097 | // Put the iface into the supertype handle so we know what caused us to fail. |
| 4098 | supertype.Assign(iface.Get()); |
| 4099 | break; |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4100 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4101 | } |
| 4102 | } |
| 4103 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4104 | // At this point if verification failed, then supertype is the "first" supertype that failed |
| 4105 | // verification (without a specific order). If verification succeeded, then supertype is either |
| 4106 | // null or the original superclass of klass and is verified. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4107 | DCHECK(supertype == nullptr || |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4108 | supertype.Get() == klass->GetSuperClass() || |
| 4109 | !supertype->IsVerified()); |
| 4110 | |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 4111 | // Try to use verification information from the oat file, otherwise do runtime verification. |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 4112 | const DexFile& dex_file = *klass->GetDexCache()->GetDexFile(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4113 | mirror::Class::Status oat_file_class_status(mirror::Class::kStatusNotReady); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4114 | bool preverified = VerifyClassUsingOatFile(dex_file, klass.Get(), oat_file_class_status); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4115 | // If the oat file says the class had an error, re-run the verifier. That way we will get a |
| 4116 | // precise error message. To ensure a rerun, test: |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4117 | // mirror::Class::IsErroneous(oat_file_class_status) => !preverified |
| 4118 | DCHECK(!mirror::Class::IsErroneous(oat_file_class_status) || !preverified); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4119 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4120 | std::string error_msg; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4121 | verifier::FailureKind verifier_failure = verifier::FailureKind::kNoFailure; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 4122 | if (!preverified) { |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 4123 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4124 | verifier_failure = verifier::MethodVerifier::VerifyClass(self, |
| 4125 | klass.Get(), |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 4126 | runtime->GetCompilerCallbacks(), |
| 4127 | runtime->IsAotCompiler(), |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 4128 | log_level, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4129 | &error_msg); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 4130 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4131 | |
| 4132 | // Verification is done, grab the lock again. |
| 4133 | ObjectLock<mirror::Class> lock(self, klass); |
| 4134 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4135 | if (preverified || verifier_failure != verifier::FailureKind::kHardFailure) { |
| 4136 | if (!preverified && verifier_failure != verifier::FailureKind::kNoFailure) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4137 | VLOG(class_linker) << "Soft verification failure in class " |
| 4138 | << klass->PrettyDescriptor() |
| 4139 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4140 | << " because: " << error_msg; |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 4141 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 4142 | self->AssertNoPendingException(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4143 | // Make sure all classes referenced by catch blocks are resolved. |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4144 | ResolveClassExceptionHandlerTypes(klass); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4145 | if (verifier_failure == verifier::FailureKind::kNoFailure) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4146 | // Even though there were no verifier failures we need to respect whether the super-class and |
| 4147 | // super-default-interfaces were verified or requiring runtime reverification. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4148 | if (supertype == nullptr || supertype->IsVerified()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4149 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4150 | } else { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4151 | CHECK_EQ(supertype->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4152 | mirror::Class::SetStatus(klass, mirror::Class::kStatusRetryVerificationAtRuntime, self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4153 | // Pretend a soft failure occurred so that we don't consider the class verified below. |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4154 | verifier_failure = verifier::FailureKind::kSoftFailure; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4155 | } |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4156 | } else { |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4157 | CHECK_EQ(verifier_failure, verifier::FailureKind::kSoftFailure); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4158 | // Soft failures at compile time should be retried at runtime. Soft |
| 4159 | // failures at runtime will be handled by slow paths in the generated |
| 4160 | // code. Set status accordingly. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4161 | if (Runtime::Current()->IsAotCompiler()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4162 | mirror::Class::SetStatus(klass, mirror::Class::kStatusRetryVerificationAtRuntime, self); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4163 | } else { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4164 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, self); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4165 | // As this is a fake verified status, make sure the methods are _not_ marked |
| 4166 | // kAccSkipAccessChecks later. |
| 4167 | klass->SetVerificationAttempted(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4168 | } |
| 4169 | } |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 4170 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4171 | VLOG(verifier) << "Verification failed on class " << klass->PrettyDescriptor() |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4172 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4173 | << " because: " << error_msg; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4174 | self->AssertNoPendingException(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4175 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4176 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 4177 | } |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4178 | if (preverified || verifier_failure == verifier::FailureKind::kNoFailure) { |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 4179 | // Class is verified so we don't need to do any access check on its methods. |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4180 | // Let the interpreter know it by setting the kAccSkipAccessChecks flag onto each |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 4181 | // method. |
| 4182 | // Note: we're going here during compilation and at runtime. When we set the |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4183 | // kAccSkipAccessChecks flag when compiling image classes, the flag is recorded |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 4184 | // in the image and is set when loading the image. |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4185 | |
| 4186 | if (UNLIKELY(Runtime::Current()->IsVerificationSoftFail())) { |
| 4187 | // Never skip access checks if the verification soft fail is forced. |
| 4188 | // Mark the class as having a verification attempt to avoid re-running the verifier. |
| 4189 | klass->SetVerificationAttempted(); |
| 4190 | } else { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4191 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4192 | } |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4193 | } |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 4194 | return verifier_failure; |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4195 | } |
| 4196 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4197 | bool ClassLinker::VerifyClassUsingOatFile(const DexFile& dex_file, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4198 | ObjPtr<mirror::Class> klass, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4199 | mirror::Class::Status& oat_file_class_status) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4200 | // If we're compiling, we can only verify the class using the oat file if |
| 4201 | // we are not compiling the image or if the class we're verifying is not part of |
| 4202 | // the app. In other words, we will only check for preverification of bootclasspath |
| 4203 | // classes. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4204 | if (Runtime::Current()->IsAotCompiler()) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4205 | // Are we compiling the bootclasspath? |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 4206 | if (Runtime::Current()->GetCompilerCallbacks()->IsBootImage()) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4207 | return false; |
| 4208 | } |
| 4209 | // We are compiling an app (not the image). |
| 4210 | |
| 4211 | // Is this an app class? (I.e. not a bootclasspath class) |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4212 | if (klass->GetClassLoader() != nullptr) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4213 | return false; |
| 4214 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4215 | } |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4216 | |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 4217 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4218 | // In case we run without an image there won't be a backing oat file. |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 4219 | if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) { |
Anwar Ghuloum | ad256bb | 2013-07-18 14:58:55 -0700 | [diff] [blame] | 4220 | return false; |
| 4221 | } |
| 4222 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4223 | uint16_t class_def_index = klass->GetDexClassDefIndex(); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 4224 | oat_file_class_status = oat_dex_file->GetOatClass(class_def_index).GetStatus(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4225 | if (oat_file_class_status == mirror::Class::kStatusVerified || |
| 4226 | oat_file_class_status == mirror::Class::kStatusInitialized) { |
Mathieu Chartier | a079e3a | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 4227 | return true; |
| 4228 | } |
| 4229 | // If we only verified a subset of the classes at compile time, we can end up with classes that |
| 4230 | // were resolved by the verifier. |
| 4231 | if (oat_file_class_status == mirror::Class::kStatusResolved) { |
| 4232 | return false; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4233 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4234 | if (oat_file_class_status == mirror::Class::kStatusRetryVerificationAtRuntime) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 4235 | // Compile time verification failed with a soft error. Compile time verification can fail |
| 4236 | // because we have incomplete type information. Consider the following: |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 4237 | // class ... { |
| 4238 | // Foo x; |
| 4239 | // .... () { |
| 4240 | // if (...) { |
| 4241 | // v1 gets assigned a type of resolved class Foo |
| 4242 | // } else { |
| 4243 | // v1 gets assigned a type of unresolved class Bar |
| 4244 | // } |
| 4245 | // iput x = v1 |
| 4246 | // } } |
| 4247 | // when we merge v1 following the if-the-else it results in Conflict |
| 4248 | // (see verifier::RegType::Merge) as we can't know the type of Bar and we could possibly be |
| 4249 | // allowing an unsafe assignment to the field x in the iput (javac may have compiled this as |
| 4250 | // it knew Bar was a sub-class of Foo, but for us this may have been moved into a separate apk |
| 4251 | // at compile time). |
| 4252 | return false; |
| 4253 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4254 | if (mirror::Class::IsErroneous(oat_file_class_status)) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 4255 | // Compile time verification failed with a hard error. This is caused by invalid instructions |
| 4256 | // in the class. These errors are unrecoverable. |
| 4257 | return false; |
| 4258 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4259 | if (oat_file_class_status == mirror::Class::kStatusNotReady) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 4260 | // Status is uninitialized if we couldn't determine the status at compile time, for example, |
| 4261 | // not loading the class. |
| 4262 | // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy |
| 4263 | // isn't a problem and this case shouldn't occur |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4264 | return false; |
| 4265 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4266 | std::string temp; |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 4267 | LOG(FATAL) << "Unexpected class status: " << oat_file_class_status |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4268 | << " " << dex_file.GetLocation() << " " << klass->PrettyClass() << " " |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4269 | << klass->GetDescriptor(&temp); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4270 | UNREACHABLE(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4271 | } |
| 4272 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4273 | void ClassLinker::ResolveClassExceptionHandlerTypes(Handle<mirror::Class> klass) { |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 4274 | for (ArtMethod& method : klass->GetMethods(image_pointer_size_)) { |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4275 | ResolveMethodExceptionHandlerTypes(&method); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4276 | } |
| 4277 | } |
| 4278 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4279 | void ClassLinker::ResolveMethodExceptionHandlerTypes(ArtMethod* method) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4280 | // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod. |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4281 | const DexFile::CodeItem* code_item = |
| 4282 | method->GetDexFile()->GetCodeItem(method->GetCodeItemOffset()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4283 | if (code_item == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4284 | return; // native or abstract method |
| 4285 | } |
| 4286 | if (code_item->tries_size_ == 0) { |
| 4287 | return; // nothing to process |
| 4288 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 4289 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item, 0); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4290 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4291 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| 4292 | CatchHandlerIterator iterator(handlers_ptr); |
| 4293 | for (; iterator.HasNext(); iterator.Next()) { |
| 4294 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 4295 | // unresolved exception types will be ignored by exception delivery |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4296 | if (iterator.GetHandlerTypeIndex().IsValid()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4297 | ObjPtr<mirror::Class> exception_type = ResolveType(iterator.GetHandlerTypeIndex(), method); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4298 | if (exception_type == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4299 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4300 | Thread::Current()->ClearException(); |
| 4301 | } |
| 4302 | } |
| 4303 | } |
| 4304 | handlers_ptr = iterator.EndDataPointer(); |
| 4305 | } |
| 4306 | } |
| 4307 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4308 | mirror::Class* ClassLinker::CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, |
| 4309 | jstring name, |
| 4310 | jobjectArray interfaces, |
| 4311 | jobject loader, |
| 4312 | jobjectArray methods, |
| 4313 | jobjectArray throws) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4314 | Thread* self = soa.Self(); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4315 | StackHandleScope<10> hs(self); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4316 | MutableHandle<mirror::Class> temp_klass(hs.NewHandle( |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4317 | AllocClass(self, GetClassRoot(kJavaLangClass), sizeof(mirror::Class)))); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4318 | if (temp_klass == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4319 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4320 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4321 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4322 | DCHECK(temp_klass->GetClass() != nullptr); |
| 4323 | temp_klass->SetObjectSize(sizeof(mirror::Proxy)); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4324 | // Set the class access flags incl. VerificationAttempted, so we do not try to set the flag on |
| 4325 | // the methods. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4326 | temp_klass->SetAccessFlags(kAccClassIsProxy | kAccPublic | kAccFinal | kAccVerificationAttempted); |
| 4327 | temp_klass->SetClassLoader(soa.Decode<mirror::ClassLoader>(loader)); |
| 4328 | DCHECK_EQ(temp_klass->GetPrimitiveType(), Primitive::kPrimNot); |
| 4329 | temp_klass->SetName(soa.Decode<mirror::String>(name)); |
| 4330 | temp_klass->SetDexCache(GetClassRoot(kJavaLangReflectProxy)->GetDexCache()); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4331 | // Object has an empty iftable, copy it for that reason. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4332 | temp_klass->SetIfTable(GetClassRoot(kJavaLangObject)->GetIfTable()); |
| 4333 | mirror::Class::SetStatus(temp_klass, mirror::Class::kStatusIdx, self); |
| 4334 | std::string descriptor(GetDescriptorForProxy(temp_klass.Get())); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 4335 | const size_t hash = ComputeModifiedUtf8Hash(descriptor.c_str()); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4336 | |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 4337 | // Needs to be before we insert the class so that the allocator field is set. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4338 | LinearAlloc* const allocator = GetOrCreateAllocatorForClassLoader(temp_klass->GetClassLoader()); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 4339 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4340 | // Insert the class before loading the fields as the field roots |
| 4341 | // (ArtField::declaring_class_) are only visited from the class |
| 4342 | // table. There can't be any suspend points between inserting the |
| 4343 | // class and setting the field arrays below. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4344 | ObjPtr<mirror::Class> existing = InsertClass(descriptor.c_str(), temp_klass.Get(), hash); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4345 | CHECK(existing == nullptr); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4346 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4347 | // Instance fields are inherited, but we add a couple of static fields... |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4348 | const size_t num_fields = 2; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 4349 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, allocator, num_fields); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4350 | temp_klass->SetSFieldsPtr(sfields); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4351 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4352 | // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by |
| 4353 | // our proxy, so Class.getInterfaces doesn't return the flattened set. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4354 | ArtField& interfaces_sfield = sfields->At(0); |
| 4355 | interfaces_sfield.SetDexFieldIndex(0); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4356 | interfaces_sfield.SetDeclaringClass(temp_klass.Get()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4357 | interfaces_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4358 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4359 | // 2. Create a static field 'throws' that holds exceptions thrown by our methods. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4360 | ArtField& throws_sfield = sfields->At(1); |
| 4361 | throws_sfield.SetDexFieldIndex(1); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4362 | throws_sfield.SetDeclaringClass(temp_klass.Get()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4363 | throws_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4364 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4365 | // Proxies have 1 direct method, the constructor |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4366 | const size_t num_direct_methods = 1; |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4367 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4368 | // They have as many virtual methods as the array |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4369 | auto h_methods = hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Method>>(methods)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 4370 | DCHECK_EQ(h_methods->GetClass(), mirror::Method::ArrayClass()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4371 | << mirror::Class::PrettyClass(h_methods->GetClass()); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 4372 | const size_t num_virtual_methods = h_methods->GetLength(); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4373 | |
| 4374 | // Create the methods array. |
| 4375 | LengthPrefixedArray<ArtMethod>* proxy_class_methods = AllocArtMethodArray( |
| 4376 | self, allocator, num_direct_methods + num_virtual_methods); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4377 | // Currently AllocArtMethodArray cannot return null, but the OOM logic is left there in case we |
| 4378 | // want to throw OOM in the future. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4379 | if (UNLIKELY(proxy_class_methods == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4380 | self->AssertPendingOOMException(); |
| 4381 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4382 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4383 | temp_klass->SetMethodsPtr(proxy_class_methods, num_direct_methods, num_virtual_methods); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4384 | |
| 4385 | // Create the single direct method. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4386 | CreateProxyConstructor(temp_klass, temp_klass->GetDirectMethodUnchecked(0, image_pointer_size_)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4387 | |
| 4388 | // Create virtual method using specified prototypes. |
| 4389 | // TODO These should really use the iterators. |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4390 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4391 | auto* virtual_method = temp_klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4392 | auto* prototype = h_methods->Get(i)->GetArtMethod(); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4393 | CreateProxyMethod(temp_klass, prototype, virtual_method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4394 | DCHECK(virtual_method->GetDeclaringClass() != nullptr); |
| 4395 | DCHECK(prototype->GetDeclaringClass() != nullptr); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4396 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4397 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4398 | // The super class is java.lang.reflect.Proxy |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4399 | temp_klass->SetSuperClass(GetClassRoot(kJavaLangReflectProxy)); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4400 | // Now effectively in the loaded state. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4401 | mirror::Class::SetStatus(temp_klass, mirror::Class::kStatusLoaded, self); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4402 | self->AssertNoPendingException(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4403 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4404 | // At this point the class is loaded. Publish a ClassLoad event. |
| 4405 | // Note: this may be a temporary class. It is a listener's responsibility to handle this. |
| 4406 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(temp_klass); |
| 4407 | |
| 4408 | MutableHandle<mirror::Class> klass = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 4409 | { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4410 | // Must hold lock on object when resolved. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4411 | ObjectLock<mirror::Class> resolution_lock(self, temp_klass); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4412 | // Link the fields and virtual methods, creating vtable and iftables. |
| 4413 | // The new class will replace the old one in the class table. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4414 | Handle<mirror::ObjectArray<mirror::Class>> h_interfaces( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4415 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces))); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4416 | if (!LinkClass(self, descriptor.c_str(), temp_klass, h_interfaces, &klass)) { |
| 4417 | mirror::Class::SetStatus(temp_klass, mirror::Class::kStatusErrorUnresolved, self); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4418 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4419 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4420 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4421 | CHECK(temp_klass->IsRetired()); |
| 4422 | CHECK_NE(temp_klass.Get(), klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4423 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4424 | CHECK_EQ(interfaces_sfield.GetDeclaringClass(), klass.Get()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4425 | interfaces_sfield.SetObject<false>( |
| 4426 | klass.Get(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4427 | soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4428 | CHECK_EQ(throws_sfield.GetDeclaringClass(), klass.Get()); |
| 4429 | throws_sfield.SetObject<false>( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4430 | klass.Get(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4431 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>(throws)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4432 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4433 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(temp_klass, klass); |
| 4434 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4435 | { |
| 4436 | // Lock on klass is released. Lock new class object. |
| 4437 | ObjectLock<mirror::Class> initialization_lock(self, klass); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4438 | mirror::Class::SetStatus(klass, mirror::Class::kStatusInitialized, self); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 4439 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4440 | |
| 4441 | // sanity checks |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 4442 | if (kIsDebugBuild) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4443 | CHECK(klass->GetIFieldsPtr() == nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4444 | CheckProxyConstructor(klass->GetDirectMethod(0, image_pointer_size_)); |
| 4445 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4446 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4447 | auto* virtual_method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
| 4448 | auto* prototype = h_methods->Get(i++)->GetArtMethod(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4449 | CheckProxyMethod(virtual_method, prototype); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4450 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4451 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4452 | StackHandleScope<1> hs2(self); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4453 | Handle<mirror::String> decoded_name = hs2.NewHandle(soa.Decode<mirror::String>(name)); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4454 | std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4455 | decoded_name->ToModifiedUtf8().c_str())); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4456 | CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(0)), interfaces_field_name); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4457 | |
| 4458 | std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4459 | decoded_name->ToModifiedUtf8().c_str())); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4460 | CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(1)), throws_field_name); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4461 | |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 4462 | CHECK_EQ(klass.Get()->GetProxyInterfaces(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4463 | soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)); |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 4464 | CHECK_EQ(klass.Get()->GetProxyThrows(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4465 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>(throws)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4466 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4467 | return klass.Get(); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4468 | } |
| 4469 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4470 | std::string ClassLinker::GetDescriptorForProxy(ObjPtr<mirror::Class> proxy_class) { |
Nicolas Geoffray | a7a4759 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 4471 | DCHECK(proxy_class->IsProxyClass()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4472 | ObjPtr<mirror::String> name = proxy_class->GetName(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4473 | DCHECK(name != nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4474 | return DotToDescriptor(name->ToModifiedUtf8().c_str()); |
| 4475 | } |
| 4476 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4477 | void ClassLinker::CreateProxyConstructor(Handle<mirror::Class> klass, ArtMethod* out) { |
| 4478 | // Create constructor for Proxy that must initialize the method. |
Przemyslaw Szczepaniak | f11cd29 | 2016-08-17 17:46:38 +0100 | [diff] [blame] | 4479 | CHECK_EQ(GetClassRoot(kJavaLangReflectProxy)->NumDirectMethods(), 23u); |
| 4480 | |
Igor Murashkin | 9d3d752 | 2017-02-27 10:39:49 -0800 | [diff] [blame] | 4481 | // Find the <init>(InvocationHandler)V method. The exact method offset varies depending |
| 4482 | // on which front-end compiler was used to build the libcore DEX files. |
| 4483 | ArtMethod* proxy_constructor = GetClassRoot(kJavaLangReflectProxy)-> |
| 4484 | FindDeclaredDirectMethod("<init>", |
| 4485 | "(Ljava/lang/reflect/InvocationHandler;)V", |
| 4486 | image_pointer_size_); |
| 4487 | DCHECK(proxy_constructor != nullptr) |
| 4488 | << "Could not find <init> method in java.lang.reflect.Proxy"; |
| 4489 | |
Sebastien Hertz | ae94e35 | 2014-08-27 15:32:56 +0200 | [diff] [blame] | 4490 | // Ensure constructor is in dex cache so that we can use the dex cache to look up the overridden |
| 4491 | // constructor method. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4492 | GetClassRoot(kJavaLangReflectProxy)->GetDexCache()->SetResolvedMethod( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4493 | proxy_constructor->GetDexMethodIndex(), proxy_constructor, image_pointer_size_); |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 4494 | // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its |
| 4495 | // code_ too) |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4496 | DCHECK(out != nullptr); |
| 4497 | out->CopyFrom(proxy_constructor, image_pointer_size_); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4498 | // Make this constructor public and fix the class to be our Proxy version |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4499 | out->SetAccessFlags((out->GetAccessFlags() & ~kAccProtected) | kAccPublic); |
| 4500 | out->SetDeclaringClass(klass.Get()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4501 | } |
| 4502 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4503 | void ClassLinker::CheckProxyConstructor(ArtMethod* constructor) const { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4504 | CHECK(constructor->IsConstructor()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4505 | auto* np = constructor->GetInterfaceMethodIfProxy(image_pointer_size_); |
| 4506 | CHECK_STREQ(np->GetName(), "<init>"); |
| 4507 | CHECK_STREQ(np->GetSignature().ToString().c_str(), "(Ljava/lang/reflect/InvocationHandler;)V"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4508 | DCHECK(constructor->IsPublic()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4509 | } |
| 4510 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4511 | void ClassLinker::CreateProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4512 | ArtMethod* out) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4513 | // Ensure prototype is in dex cache so that we can use the dex cache to look up the overridden |
| 4514 | // prototype method |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 4515 | auto* dex_cache = prototype->GetDeclaringClass()->GetDexCache(); |
| 4516 | // Avoid dirtying the dex cache unless we need to. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4517 | if (dex_cache->GetResolvedMethod(prototype->GetDexMethodIndex(), image_pointer_size_) != |
| 4518 | prototype) { |
| 4519 | dex_cache->SetResolvedMethod( |
| 4520 | prototype->GetDexMethodIndex(), prototype, image_pointer_size_); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 4521 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4522 | // We steal everything from the prototype (such as DexCache, invoke stub, etc.) then specialize |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4523 | // as necessary |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4524 | DCHECK(out != nullptr); |
| 4525 | out->CopyFrom(prototype, image_pointer_size_); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4526 | |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 4527 | // Set class to be the concrete proxy class. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4528 | out->SetDeclaringClass(klass.Get()); |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 4529 | // Clear the abstract, default and conflict flags to ensure that defaults aren't picked in |
| 4530 | // preference to the invocation handler. |
| 4531 | const uint32_t kRemoveFlags = kAccAbstract | kAccDefault | kAccDefaultConflict; |
| 4532 | // Make the method final. |
| 4533 | const uint32_t kAddFlags = kAccFinal; |
| 4534 | out->SetAccessFlags((out->GetAccessFlags() & ~kRemoveFlags) | kAddFlags); |
| 4535 | |
| 4536 | // Clear the dex_code_item_offset_. It needs to be 0 since proxy methods have no CodeItems but the |
| 4537 | // method they copy might (if it's a default method). |
| 4538 | out->SetCodeItemOffset(0); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4539 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4540 | // At runtime the method looks like a reference and argument saving method, clone the code |
| 4541 | // related parameters from this method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4542 | out->SetEntryPointFromQuickCompiledCode(GetQuickProxyInvokeHandler()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4543 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4544 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4545 | void ClassLinker::CheckProxyMethod(ArtMethod* method, ArtMethod* prototype) const { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4546 | // Basic sanity |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4547 | CHECK(!prototype->IsFinal()); |
| 4548 | CHECK(method->IsFinal()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 4549 | CHECK(method->IsInvokable()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 4550 | |
| 4551 | // The proxy method doesn't have its own dex cache or dex file and so it steals those of its |
| 4552 | // interface prototype. The exception to this are Constructors and the Class of the Proxy itself. |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 4553 | CHECK(prototype->HasSameDexCacheResolvedMethods(method, image_pointer_size_)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4554 | auto* np = method->GetInterfaceMethodIfProxy(image_pointer_size_); |
| 4555 | CHECK_EQ(prototype->GetDeclaringClass()->GetDexCache(), np->GetDexCache()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 4556 | CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex()); |
| 4557 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4558 | CHECK_STREQ(np->GetName(), prototype->GetName()); |
| 4559 | CHECK_STREQ(np->GetShorty(), prototype->GetShorty()); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4560 | // More complex sanity - via dex cache |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 4561 | CHECK_EQ(np->GetReturnType(true /* resolve */), prototype->GetReturnType(true /* resolve */)); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4562 | } |
| 4563 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4564 | bool ClassLinker::CanWeInitializeClass(ObjPtr<mirror::Class> klass, bool can_init_statics, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4565 | bool can_init_parents) { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 4566 | if (can_init_statics && can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4567 | return true; |
| 4568 | } |
| 4569 | if (!can_init_statics) { |
| 4570 | // Check if there's a class initializer. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4571 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4572 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4573 | return false; |
| 4574 | } |
| 4575 | // Check if there are encoded static values needing initialization. |
| 4576 | if (klass->NumStaticFields() != 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4577 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4578 | DCHECK(dex_class_def != nullptr); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4579 | if (dex_class_def->static_values_off_ != 0) { |
| 4580 | return false; |
| 4581 | } |
| 4582 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4583 | // If we are a class we need to initialize all interfaces with default methods when we are |
| 4584 | // initialized. Check all of them. |
| 4585 | if (!klass->IsInterface()) { |
| 4586 | size_t num_interfaces = klass->GetIfTableCount(); |
| 4587 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4588 | ObjPtr<mirror::Class> iface = klass->GetIfTable()->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4589 | if (iface->HasDefaultMethods() && |
| 4590 | !CanWeInitializeClass(iface, can_init_statics, can_init_parents)) { |
| 4591 | return false; |
| 4592 | } |
| 4593 | } |
| 4594 | } |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4595 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4596 | if (klass->IsInterface() || !klass->HasSuperClass()) { |
| 4597 | return true; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4598 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4599 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4600 | if (!can_init_parents && !super_class->IsInitialized()) { |
| 4601 | return false; |
| 4602 | } |
| 4603 | return CanWeInitializeClass(super_class, can_init_statics, can_init_parents); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4604 | } |
| 4605 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4606 | bool ClassLinker::InitializeClass(Thread* self, Handle<mirror::Class> klass, |
| 4607 | bool can_init_statics, bool can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4608 | // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol |
| 4609 | |
| 4610 | // Are we already initialized and therefore done? |
| 4611 | // Note: we differ from the JLS here as we don't do this under the lock, this is benign as |
| 4612 | // an initialized class will never change its state. |
| 4613 | if (klass->IsInitialized()) { |
| 4614 | return true; |
| 4615 | } |
| 4616 | |
| 4617 | // Fast fail if initialization requires a full runtime. Not part of the JLS. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4618 | if (!CanWeInitializeClass(klass.Get(), can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4619 | return false; |
| 4620 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4621 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4622 | self->AllowThreadSuspension(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4623 | uint64_t t0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4624 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4625 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4626 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4627 | // Re-check under the lock in case another thread initialized ahead of us. |
| 4628 | if (klass->IsInitialized()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4629 | return true; |
| 4630 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4631 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4632 | // Was the class already found to be erroneous? Done under the lock to match the JLS. |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4633 | if (klass->IsErroneous()) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 4634 | ThrowEarlierClassFailure(klass.Get(), true); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4635 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4636 | return false; |
| 4637 | } |
| 4638 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4639 | CHECK(klass->IsResolved() && !klass->IsErroneousResolved()) |
| 4640 | << klass->PrettyClass() << ": state=" << klass->GetStatus(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4641 | |
| 4642 | if (!klass->IsVerified()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4643 | VerifyClass(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4644 | if (!klass->IsVerified()) { |
| 4645 | // We failed to verify, expect either the klass to be erroneous or verification failed at |
| 4646 | // compile time. |
| 4647 | if (klass->IsErroneous()) { |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 4648 | // The class is erroneous. This may be a verifier error, or another thread attempted |
| 4649 | // verification and/or initialization and failed. We can distinguish those cases by |
| 4650 | // whether an exception is already pending. |
| 4651 | if (self->IsExceptionPending()) { |
| 4652 | // Check that it's a VerifyError. |
| 4653 | DCHECK_EQ("java.lang.Class<java.lang.VerifyError>", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4654 | mirror::Class::PrettyClass(self->GetException()->GetClass())); |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 4655 | } else { |
| 4656 | // Check that another thread attempted initialization. |
| 4657 | DCHECK_NE(0, klass->GetClinitThreadId()); |
| 4658 | DCHECK_NE(self->GetTid(), klass->GetClinitThreadId()); |
| 4659 | // Need to rethrow the previous failure now. |
| 4660 | ThrowEarlierClassFailure(klass.Get(), true); |
| 4661 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4662 | VlogClassInitializationFailure(klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4663 | } else { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4664 | CHECK(Runtime::Current()->IsAotCompiler()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4665 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 4666 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4667 | return false; |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4668 | } else { |
| 4669 | self->AssertNoPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4670 | } |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 4671 | |
| 4672 | // A separate thread could have moved us all the way to initialized. A "simple" example |
| 4673 | // involves a subclass of the current class being initialized at the same time (which |
| 4674 | // will implicitly initialize the superclass, if scheduled that way). b/28254258 |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4675 | DCHECK(!klass->IsErroneous()) << klass->GetStatus(); |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 4676 | if (klass->IsInitialized()) { |
| 4677 | return true; |
| 4678 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4679 | } |
| 4680 | |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4681 | // If the class is kStatusInitializing, either this thread is |
| 4682 | // initializing higher up the stack or another thread has beat us |
| 4683 | // to initializing and we need to wait. Either way, this |
| 4684 | // invocation of InitializeClass will not be responsible for |
| 4685 | // running <clinit> and will return. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4686 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4687 | // Could have got an exception during verification. |
| 4688 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4689 | VlogClassInitializationFailure(klass); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4690 | return false; |
| 4691 | } |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 4692 | // We caught somebody else in the act; was it us? |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 4693 | if (klass->GetClinitThreadId() == self->GetTid()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4694 | // Yes. That's fine. Return so we can continue initializing. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4695 | return true; |
| 4696 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4697 | // No. That's fine. Wait for another thread to finish initializing. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4698 | return WaitForInitializeClass(klass, self, lock); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4699 | } |
| 4700 | |
| 4701 | if (!ValidateSuperClassDescriptors(klass)) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4702 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4703 | return false; |
| 4704 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4705 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4706 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4707 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusVerified) << klass->PrettyClass() |
Andreas Gampe | 9510ccd | 2016-04-20 09:55:25 -0700 | [diff] [blame] | 4708 | << " self.tid=" << self->GetTid() << " clinit.tid=" << klass->GetClinitThreadId(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4709 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4710 | // From here out other threads may observe that we're initializing and so changes of state |
| 4711 | // require the a notification. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 4712 | klass->SetClinitThreadId(self->GetTid()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4713 | mirror::Class::SetStatus(klass, mirror::Class::kStatusInitializing, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4714 | |
| 4715 | t0 = NanoTime(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4716 | } |
| 4717 | |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 4718 | // Initialize super classes, must be done while initializing for the JLS. |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4719 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4720 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4721 | if (!super_class->IsInitialized()) { |
| 4722 | CHECK(!super_class->IsInterface()); |
| 4723 | CHECK(can_init_parents); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4724 | StackHandleScope<1> hs(self); |
| 4725 | Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4726 | bool super_initialized = InitializeClass(self, handle_scope_super, can_init_statics, true); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4727 | if (!super_initialized) { |
| 4728 | // The super class was verified ahead of entering initializing, we should only be here if |
| 4729 | // the super class became erroneous due to initialization. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4730 | CHECK(handle_scope_super->IsErroneous() && self->IsExceptionPending()) |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4731 | << "Super class initialization failed for " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4732 | << handle_scope_super->PrettyDescriptor() |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4733 | << " that has unexpected status " << handle_scope_super->GetStatus() |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4734 | << "\nPending exception:\n" |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 4735 | << (self->GetException() != nullptr ? self->GetException()->Dump() : ""); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4736 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4737 | // Initialization failed because the super-class is erroneous. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4738 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4739 | return false; |
| 4740 | } |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 4741 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4742 | } |
| 4743 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4744 | if (!klass->IsInterface()) { |
| 4745 | // Initialize interfaces with default methods for the JLS. |
| 4746 | size_t num_direct_interfaces = klass->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4747 | // Only setup the (expensive) handle scope if we actually need to. |
| 4748 | if (UNLIKELY(num_direct_interfaces > 0)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4749 | StackHandleScope<1> hs_iface(self); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4750 | MutableHandle<mirror::Class> handle_scope_iface(hs_iface.NewHandle<mirror::Class>(nullptr)); |
| 4751 | for (size_t i = 0; i < num_direct_interfaces; i++) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 4752 | handle_scope_iface.Assign(mirror::Class::GetDirectInterface(self, klass.Get(), i)); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 4753 | CHECK(handle_scope_iface != nullptr) << klass->PrettyDescriptor() << " iface #" << i; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4754 | CHECK(handle_scope_iface->IsInterface()); |
| 4755 | if (handle_scope_iface->HasBeenRecursivelyInitialized()) { |
| 4756 | // We have already done this for this interface. Skip it. |
| 4757 | continue; |
| 4758 | } |
| 4759 | // We cannot just call initialize class directly because we need to ensure that ALL |
| 4760 | // interfaces with default methods are initialized. Non-default interface initialization |
| 4761 | // will not affect other non-default super-interfaces. |
| 4762 | bool iface_initialized = InitializeDefaultInterfaceRecursive(self, |
| 4763 | handle_scope_iface, |
| 4764 | can_init_statics, |
| 4765 | can_init_parents); |
| 4766 | if (!iface_initialized) { |
| 4767 | ObjectLock<mirror::Class> lock(self, klass); |
| 4768 | // Initialization failed because one of our interfaces with default methods is erroneous. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4769 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4770 | return false; |
| 4771 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4772 | } |
| 4773 | } |
| 4774 | } |
| 4775 | |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4776 | const size_t num_static_fields = klass->NumStaticFields(); |
| 4777 | if (num_static_fields > 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4778 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4779 | CHECK(dex_class_def != nullptr); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4780 | const DexFile& dex_file = klass->GetDexFile(); |
Hiroshi Yamauchi | 67ef46a | 2014-08-21 15:59:43 -0700 | [diff] [blame] | 4781 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4782 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4783 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4784 | |
| 4785 | // Eagerly fill in static fields so that the we don't have to do as many expensive |
| 4786 | // Class::FindStaticField in ResolveField. |
| 4787 | for (size_t i = 0; i < num_static_fields; ++i) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4788 | ArtField* field = klass->GetStaticField(i); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4789 | const uint32_t field_idx = field->GetDexFieldIndex(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4790 | ArtField* resolved_field = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4791 | if (resolved_field == nullptr) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4792 | dex_cache->SetResolvedField(field_idx, field, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4793 | } else { |
| 4794 | DCHECK_EQ(field, resolved_field); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4795 | } |
| 4796 | } |
| 4797 | |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 4798 | annotations::RuntimeEncodedStaticFieldValueIterator value_it(dex_file, |
| 4799 | &dex_cache, |
| 4800 | &class_loader, |
| 4801 | this, |
| 4802 | *dex_class_def); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 4803 | const uint8_t* class_data = dex_file.GetClassData(*dex_class_def); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4804 | ClassDataItemIterator field_it(dex_file, class_data); |
| 4805 | if (value_it.HasNext()) { |
| 4806 | DCHECK(field_it.HasNextStaticField()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4807 | CHECK(can_init_statics); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4808 | for ( ; value_it.HasNext(); value_it.Next(), field_it.Next()) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4809 | ArtField* field = ResolveField( |
| 4810 | dex_file, field_it.GetMemberIndex(), dex_cache, class_loader, true); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4811 | if (Runtime::Current()->IsActiveTransaction()) { |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4812 | value_it.ReadValueToField<true>(field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4813 | } else { |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4814 | value_it.ReadValueToField<false>(field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4815 | } |
Mathieu Chartier | da595be | 2016-08-10 13:57:39 -0700 | [diff] [blame] | 4816 | if (self->IsExceptionPending()) { |
| 4817 | break; |
| 4818 | } |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4819 | DCHECK(!value_it.HasNext() || field_it.HasNextStaticField()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4820 | } |
| 4821 | } |
| 4822 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4823 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4824 | |
Mathieu Chartier | da595be | 2016-08-10 13:57:39 -0700 | [diff] [blame] | 4825 | if (!self->IsExceptionPending()) { |
| 4826 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
| 4827 | if (clinit != nullptr) { |
| 4828 | CHECK(can_init_statics); |
| 4829 | JValue result; |
| 4830 | clinit->Invoke(self, nullptr, 0, &result, "V"); |
| 4831 | } |
| 4832 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4833 | self->AllowThreadSuspension(); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 4834 | uint64_t t1 = NanoTime(); |
| 4835 | |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4836 | bool success = true; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4837 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4838 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4839 | |
| 4840 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4841 | WrapExceptionInInitializer(klass); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4842 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4843 | success = false; |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 4844 | } else if (Runtime::Current()->IsTransactionAborted()) { |
| 4845 | // The exception thrown when the transaction aborted has been caught and cleared |
| 4846 | // so we need to throw it again now. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4847 | VLOG(compiler) << "Return from class initializer of " |
| 4848 | << mirror::Class::PrettyDescriptor(klass.Get()) |
Sebastien Hertz | bd9cf9f | 2015-03-03 12:16:13 +0100 | [diff] [blame] | 4849 | << " without exception while transaction was aborted: re-throw it now."; |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 4850 | Runtime::Current()->ThrowTransactionAbortError(self); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4851 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 4852 | success = false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4853 | } else { |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 4854 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 4855 | RuntimeStats* thread_stats = self->GetStats(); |
| 4856 | ++global_stats->class_init_count; |
| 4857 | ++thread_stats->class_init_count; |
| 4858 | global_stats->class_init_time_ns += (t1 - t0); |
| 4859 | thread_stats->class_init_time_ns += (t1 - t0); |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 4860 | // Set the class as initialized except if failed to initialize static fields. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4861 | mirror::Class::SetStatus(klass, mirror::Class::kStatusInitialized, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4862 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4863 | std::string temp; |
| 4864 | LOG(INFO) << "Initialized class " << klass->GetDescriptor(&temp) << " from " << |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4865 | klass->GetLocation(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 4866 | } |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 4867 | // Opportunistically set static method trampolines to their destination. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4868 | FixupStaticTrampolines(klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4869 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4870 | } |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4871 | return success; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4872 | } |
| 4873 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4874 | // We recursively run down the tree of interfaces. We need to do this in the order they are declared |
| 4875 | // and perform the initialization only on those interfaces that contain default methods. |
| 4876 | bool ClassLinker::InitializeDefaultInterfaceRecursive(Thread* self, |
| 4877 | Handle<mirror::Class> iface, |
| 4878 | bool can_init_statics, |
| 4879 | bool can_init_parents) { |
| 4880 | CHECK(iface->IsInterface()); |
| 4881 | size_t num_direct_ifaces = iface->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4882 | // Only create the (expensive) handle scope if we need it. |
| 4883 | if (UNLIKELY(num_direct_ifaces > 0)) { |
| 4884 | StackHandleScope<1> hs(self); |
| 4885 | MutableHandle<mirror::Class> handle_super_iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 4886 | // First we initialize all of iface's super-interfaces recursively. |
| 4887 | for (size_t i = 0; i < num_direct_ifaces; i++) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 4888 | ObjPtr<mirror::Class> super_iface = mirror::Class::GetDirectInterface(self, iface.Get(), i); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 4889 | CHECK(super_iface != nullptr) << iface->PrettyDescriptor() << " iface #" << i; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4890 | if (!super_iface->HasBeenRecursivelyInitialized()) { |
| 4891 | // Recursive step |
| 4892 | handle_super_iface.Assign(super_iface); |
| 4893 | if (!InitializeDefaultInterfaceRecursive(self, |
| 4894 | handle_super_iface, |
| 4895 | can_init_statics, |
| 4896 | can_init_parents)) { |
| 4897 | return false; |
| 4898 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4899 | } |
| 4900 | } |
| 4901 | } |
| 4902 | |
| 4903 | bool result = true; |
| 4904 | // Then we initialize 'iface' if it has default methods. We do not need to (and in fact must not) |
| 4905 | // initialize if we don't have default methods. |
| 4906 | if (iface->HasDefaultMethods()) { |
| 4907 | result = EnsureInitialized(self, iface, can_init_statics, can_init_parents); |
| 4908 | } |
| 4909 | |
| 4910 | // Mark that this interface has undergone recursive default interface initialization so we know we |
| 4911 | // can skip it on any later class initializations. We do this even if we are not a default |
| 4912 | // interface since we can still avoid the traversal. This is purely a performance optimization. |
| 4913 | if (result) { |
| 4914 | // TODO This should be done in a better way |
| 4915 | ObjectLock<mirror::Class> lock(self, iface); |
| 4916 | iface->SetRecursivelyInitialized(); |
| 4917 | } |
| 4918 | return result; |
| 4919 | } |
| 4920 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4921 | bool ClassLinker::WaitForInitializeClass(Handle<mirror::Class> klass, |
| 4922 | Thread* self, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4923 | ObjectLock<mirror::Class>& lock) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4924 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4925 | while (true) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4926 | self->AssertNoPendingException(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4927 | CHECK(!klass->IsInitialized()); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4928 | lock.WaitIgnoringInterrupts(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4929 | |
| 4930 | // When we wake up, repeat the test for init-in-progress. If |
| 4931 | // there's an exception pending (only possible if |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4932 | // we were not using WaitIgnoringInterrupts), bail out. |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4933 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4934 | WrapExceptionInInitializer(klass); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4935 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4936 | return false; |
| 4937 | } |
| 4938 | // Spurious wakeup? Go back to waiting. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4939 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4940 | continue; |
| 4941 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4942 | if (klass->GetStatus() == mirror::Class::kStatusVerified && |
| 4943 | Runtime::Current()->IsAotCompiler()) { |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 4944 | // Compile time initialization failed. |
| 4945 | return false; |
| 4946 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4947 | if (klass->IsErroneous()) { |
| 4948 | // The caller wants an exception, but it was thrown in a |
| 4949 | // different thread. Synthesize one here. |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 4950 | ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4951 | klass->PrettyDescriptor().c_str()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4952 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4953 | return false; |
| 4954 | } |
| 4955 | if (klass->IsInitialized()) { |
| 4956 | return true; |
| 4957 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4958 | LOG(FATAL) << "Unexpected class status. " << klass->PrettyClass() << " is " |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4959 | << klass->GetStatus(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4960 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 4961 | UNREACHABLE(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4962 | } |
| 4963 | |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4964 | static void ThrowSignatureCheckResolveReturnTypeException(Handle<mirror::Class> klass, |
| 4965 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4966 | ArtMethod* method, |
| 4967 | ArtMethod* m) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4968 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4969 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4970 | DCHECK(!m->IsProxyMethod()); |
| 4971 | const DexFile* dex_file = m->GetDexFile(); |
| 4972 | const DexFile::MethodId& method_id = dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 4973 | const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4974 | dex::TypeIndex return_type_idx = proto_id.return_type_idx_; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4975 | std::string return_type = dex_file->PrettyType(return_type_idx); |
| 4976 | std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4977 | ThrowWrappedLinkageError(klass.Get(), |
| 4978 | "While checking class %s method %s signature against %s %s: " |
| 4979 | "Failed to resolve return type %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4980 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 4981 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4982 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4983 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4984 | return_type.c_str(), class_loader.c_str()); |
| 4985 | } |
| 4986 | |
| 4987 | static void ThrowSignatureCheckResolveArgException(Handle<mirror::Class> klass, |
| 4988 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4989 | ArtMethod* method, |
| 4990 | ArtMethod* m, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4991 | uint32_t index, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4992 | dex::TypeIndex arg_type_idx) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4993 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4994 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4995 | DCHECK(!m->IsProxyMethod()); |
| 4996 | const DexFile* dex_file = m->GetDexFile(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4997 | std::string arg_type = dex_file->PrettyType(arg_type_idx); |
| 4998 | std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4999 | ThrowWrappedLinkageError(klass.Get(), |
| 5000 | "While checking class %s method %s signature against %s %s: " |
| 5001 | "Failed to resolve arg %u type %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5002 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5003 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5004 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5005 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5006 | index, arg_type.c_str(), class_loader.c_str()); |
| 5007 | } |
| 5008 | |
| 5009 | static void ThrowSignatureMismatch(Handle<mirror::Class> klass, |
| 5010 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5011 | ArtMethod* method, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5012 | const std::string& error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5013 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5014 | ThrowLinkageError(klass.Get(), |
| 5015 | "Class %s method %s resolves differently in %s %s: %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5016 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5017 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5018 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5019 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5020 | error_msg.c_str()); |
| 5021 | } |
| 5022 | |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5023 | static bool HasSameSignatureWithDifferentClassLoaders(Thread* self, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5024 | Handle<mirror::Class> klass, |
| 5025 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5026 | ArtMethod* method1, |
| 5027 | ArtMethod* method2) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5028 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5029 | { |
| 5030 | StackHandleScope<1> hs(self); |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5031 | Handle<mirror::Class> return_type(hs.NewHandle(method1->GetReturnType(true /* resolve */))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5032 | if (UNLIKELY(return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5033 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method1); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5034 | return false; |
| 5035 | } |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5036 | ObjPtr<mirror::Class> other_return_type = method2->GetReturnType(true /* resolve */); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5037 | if (UNLIKELY(other_return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5038 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method2); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5039 | return false; |
| 5040 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5041 | if (UNLIKELY(other_return_type != return_type.Get())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5042 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5043 | StringPrintf("Return types mismatch: %s(%p) vs %s(%p)", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5044 | return_type->PrettyClassAndClassLoader().c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5045 | return_type.Get(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5046 | other_return_type->PrettyClassAndClassLoader().c_str(), |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5047 | other_return_type.Ptr())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5048 | return false; |
| 5049 | } |
| 5050 | } |
| 5051 | const DexFile::TypeList* types1 = method1->GetParameterTypeList(); |
| 5052 | const DexFile::TypeList* types2 = method2->GetParameterTypeList(); |
| 5053 | if (types1 == nullptr) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5054 | if (types2 != nullptr && types2->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5055 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5056 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5057 | method2->PrettyMethod(true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5058 | return false; |
| 5059 | } |
| 5060 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5061 | } else if (UNLIKELY(types2 == nullptr)) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5062 | if (types1->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5063 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5064 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5065 | method2->PrettyMethod(true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5066 | return false; |
| 5067 | } |
| 5068 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5069 | } |
| 5070 | uint32_t num_types = types1->Size(); |
| 5071 | if (UNLIKELY(num_types != types2->Size())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5072 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5073 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5074 | method2->PrettyMethod(true).c_str())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5075 | return false; |
| 5076 | } |
| 5077 | for (uint32_t i = 0; i < num_types; ++i) { |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5078 | StackHandleScope<1> hs(self); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5079 | dex::TypeIndex param_type_idx = types1->GetTypeItem(i).type_idx_; |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5080 | Handle<mirror::Class> param_type(hs.NewHandle( |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5081 | method1->GetClassFromTypeIndex(param_type_idx, true /* resolve */))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5082 | if (UNLIKELY(param_type == nullptr)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5083 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5084 | method1, i, param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5085 | return false; |
| 5086 | } |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5087 | dex::TypeIndex other_param_type_idx = types2->GetTypeItem(i).type_idx_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5088 | ObjPtr<mirror::Class> other_param_type = |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5089 | method2->GetClassFromTypeIndex(other_param_type_idx, true /* resolve */); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5090 | if (UNLIKELY(other_param_type == nullptr)) { |
| 5091 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5092 | method2, i, other_param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5093 | return false; |
| 5094 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5095 | if (UNLIKELY(param_type.Get() != other_param_type)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5096 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5097 | StringPrintf("Parameter %u type mismatch: %s(%p) vs %s(%p)", |
| 5098 | i, |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5099 | param_type->PrettyClassAndClassLoader().c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5100 | param_type.Get(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5101 | other_param_type->PrettyClassAndClassLoader().c_str(), |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5102 | other_param_type.Ptr())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5103 | return false; |
| 5104 | } |
| 5105 | } |
| 5106 | return true; |
| 5107 | } |
| 5108 | |
| 5109 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5110 | bool ClassLinker::ValidateSuperClassDescriptors(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5111 | if (klass->IsInterface()) { |
| 5112 | return true; |
| 5113 | } |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 5114 | // Begin with the methods local to the superclass. |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 5115 | Thread* self = Thread::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5116 | StackHandleScope<1> hs(self); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5117 | MutableHandle<mirror::Class> super_klass(hs.NewHandle<mirror::Class>(nullptr)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5118 | if (klass->HasSuperClass() && |
| 5119 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5120 | super_klass.Assign(klass->GetSuperClass()); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5121 | for (int i = klass->GetSuperClass()->GetVTableLength() - 1; i >= 0; --i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5122 | auto* m = klass->GetVTableEntry(i, image_pointer_size_); |
| 5123 | auto* super_m = klass->GetSuperClass()->GetVTableEntry(i, image_pointer_size_); |
| 5124 | if (m != super_m) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5125 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, |
| 5126 | klass, |
| 5127 | super_klass, |
| 5128 | m, |
| 5129 | super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5130 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5131 | return false; |
| 5132 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5133 | } |
| 5134 | } |
| 5135 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 5136 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5137 | super_klass.Assign(klass->GetIfTable()->GetInterface(i)); |
| 5138 | if (klass->GetClassLoader() != super_klass->GetClassLoader()) { |
| 5139 | uint32_t num_methods = super_klass->NumVirtualMethods(); |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 5140 | for (uint32_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5141 | auto* m = klass->GetIfTable()->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>( |
| 5142 | j, image_pointer_size_); |
| 5143 | auto* super_m = super_klass->GetVirtualMethod(j, image_pointer_size_); |
| 5144 | if (m != super_m) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5145 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, |
| 5146 | klass, |
| 5147 | super_klass, |
| 5148 | m, |
| 5149 | super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5150 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5151 | return false; |
| 5152 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5153 | } |
| 5154 | } |
| 5155 | } |
| 5156 | } |
| 5157 | return true; |
| 5158 | } |
| 5159 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5160 | bool ClassLinker::EnsureInitialized(Thread* self, |
| 5161 | Handle<mirror::Class> c, |
| 5162 | bool can_init_fields, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5163 | bool can_init_parents) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5164 | DCHECK(c != nullptr); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5165 | if (c->IsInitialized()) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 5166 | EnsureSkipAccessChecksMethods(c, image_pointer_size_); |
Mathieu Chartier | 8502f72 | 2016-06-08 15:09:08 -0700 | [diff] [blame] | 5167 | self->AssertNoPendingException(); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5168 | return true; |
| 5169 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5170 | const bool success = InitializeClass(self, c, can_init_fields, can_init_parents); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5171 | if (!success) { |
| 5172 | if (can_init_fields && can_init_parents) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5173 | CHECK(self->IsExceptionPending()) << c->PrettyClass(); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5174 | } |
| 5175 | } else { |
| 5176 | self->AssertNoPendingException(); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 5177 | } |
| 5178 | return success; |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 5179 | } |
| 5180 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5181 | void ClassLinker::FixupTemporaryDeclaringClass(ObjPtr<mirror::Class> temp_class, |
| 5182 | ObjPtr<mirror::Class> new_class) { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5183 | DCHECK_EQ(temp_class->NumInstanceFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5184 | for (ArtField& field : new_class->GetIFields()) { |
| 5185 | if (field.GetDeclaringClass() == temp_class) { |
| 5186 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5187 | } |
| 5188 | } |
| 5189 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5190 | DCHECK_EQ(temp_class->NumStaticFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5191 | for (ArtField& field : new_class->GetSFields()) { |
| 5192 | if (field.GetDeclaringClass() == temp_class) { |
| 5193 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5194 | } |
| 5195 | } |
| 5196 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5197 | DCHECK_EQ(temp_class->NumDirectMethods(), 0u); |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5198 | DCHECK_EQ(temp_class->NumVirtualMethods(), 0u); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5199 | for (auto& method : new_class->GetMethods(image_pointer_size_)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5200 | if (method.GetDeclaringClass() == temp_class) { |
| 5201 | method.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5202 | } |
| 5203 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5204 | |
| 5205 | // Make sure the remembered set and mod-union tables know that we updated some of the native |
| 5206 | // roots. |
| 5207 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5208 | } |
| 5209 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5210 | void ClassLinker::RegisterClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5211 | CHECK(class_loader->GetAllocator() == nullptr); |
| 5212 | CHECK(class_loader->GetClassTable() == nullptr); |
| 5213 | Thread* const self = Thread::Current(); |
| 5214 | ClassLoaderData data; |
| 5215 | data.weak_root = self->GetJniEnv()->vm->AddWeakGlobalRef(self, class_loader); |
| 5216 | // Create and set the class table. |
| 5217 | data.class_table = new ClassTable; |
| 5218 | class_loader->SetClassTable(data.class_table); |
| 5219 | // Create and set the linear allocator. |
| 5220 | data.allocator = Runtime::Current()->CreateLinearAlloc(); |
| 5221 | class_loader->SetAllocator(data.allocator); |
| 5222 | // Add to the list so that we know to free the data later. |
| 5223 | class_loaders_.push_back(data); |
| 5224 | } |
| 5225 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5226 | ClassTable* ClassLinker::InsertClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5227 | if (class_loader == nullptr) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame^] | 5228 | return boot_class_table_.get(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5229 | } |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5230 | ClassTable* class_table = class_loader->GetClassTable(); |
| 5231 | if (class_table == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5232 | RegisterClassLoader(class_loader); |
| 5233 | class_table = class_loader->GetClassTable(); |
| 5234 | DCHECK(class_table != nullptr); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5235 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5236 | return class_table; |
| 5237 | } |
| 5238 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5239 | ClassTable* ClassLinker::ClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame^] | 5240 | return class_loader == nullptr ? boot_class_table_.get() : class_loader->GetClassTable(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5241 | } |
| 5242 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5243 | static ImTable* FindSuperImt(ObjPtr<mirror::Class> klass, PointerSize pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5244 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5245 | while (klass->HasSuperClass()) { |
| 5246 | klass = klass->GetSuperClass(); |
| 5247 | if (klass->ShouldHaveImt()) { |
| 5248 | return klass->GetImt(pointer_size); |
| 5249 | } |
| 5250 | } |
| 5251 | return nullptr; |
| 5252 | } |
| 5253 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5254 | bool ClassLinker::LinkClass(Thread* self, |
| 5255 | const char* descriptor, |
| 5256 | Handle<mirror::Class> klass, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5257 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5258 | MutableHandle<mirror::Class>* h_new_class_out) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5259 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5260 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5261 | if (!LinkSuperClass(klass)) { |
| 5262 | return false; |
| 5263 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5264 | ArtMethod* imt_data[ImTable::kSize]; |
| 5265 | // If there are any new conflicts compared to super class. |
| 5266 | bool new_conflict = false; |
| 5267 | std::fill_n(imt_data, arraysize(imt_data), Runtime::Current()->GetImtUnimplementedMethod()); |
| 5268 | if (!LinkMethods(self, klass, interfaces, &new_conflict, imt_data)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5269 | return false; |
| 5270 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5271 | if (!LinkInstanceFields(self, klass)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5272 | return false; |
| 5273 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5274 | size_t class_size; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5275 | if (!LinkStaticFields(self, klass, &class_size)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5276 | return false; |
| 5277 | } |
| 5278 | CreateReferenceInstanceOffsets(klass); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5279 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5280 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5281 | ImTable* imt = nullptr; |
| 5282 | if (klass->ShouldHaveImt()) { |
| 5283 | // If there are any new conflicts compared to the super class we can not make a copy. There |
| 5284 | // can be cases where both will have a conflict method at the same slot without having the same |
| 5285 | // set of conflicts. In this case, we can not share the IMT since the conflict table slow path |
| 5286 | // will possibly create a table that is incorrect for either of the classes. |
| 5287 | // Same IMT with new_conflict does not happen very often. |
| 5288 | if (!new_conflict) { |
| 5289 | ImTable* super_imt = FindSuperImt(klass.Get(), image_pointer_size_); |
| 5290 | if (super_imt != nullptr) { |
| 5291 | bool imt_equals = true; |
| 5292 | for (size_t i = 0; i < ImTable::kSize && imt_equals; ++i) { |
| 5293 | imt_equals = imt_equals && (super_imt->Get(i, image_pointer_size_) == imt_data[i]); |
| 5294 | } |
| 5295 | if (imt_equals) { |
| 5296 | imt = super_imt; |
| 5297 | } |
| 5298 | } |
| 5299 | } |
| 5300 | if (imt == nullptr) { |
| 5301 | LinearAlloc* allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
| 5302 | imt = reinterpret_cast<ImTable*>( |
| 5303 | allocator->Alloc(self, ImTable::SizeInBytes(image_pointer_size_))); |
| 5304 | if (imt == nullptr) { |
| 5305 | return false; |
| 5306 | } |
| 5307 | imt->Populate(imt_data, image_pointer_size_); |
| 5308 | } |
| 5309 | } |
| 5310 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5311 | if (!klass->IsTemp() || (!init_done_ && klass->GetClassSize() == class_size)) { |
| 5312 | // We don't need to retire this class as it has no embedded tables or it was created the |
| 5313 | // correct size during class linker initialization. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5314 | CHECK_EQ(klass->GetClassSize(), class_size) << klass->PrettyDescriptor(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5315 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5316 | if (klass->ShouldHaveEmbeddedVTable()) { |
| 5317 | klass->PopulateEmbeddedVTable(image_pointer_size_); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5318 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5319 | if (klass->ShouldHaveImt()) { |
| 5320 | klass->SetImt(imt, image_pointer_size_); |
| 5321 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 5322 | |
| 5323 | // Update CHA info based on whether we override methods. |
| 5324 | // Have to do this before setting the class as resolved which allows |
| 5325 | // instantiation of klass. |
| 5326 | Runtime::Current()->GetClassHierarchyAnalysis()->UpdateAfterLoadingOf(klass); |
| 5327 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5328 | // This will notify waiters on klass that saw the not yet resolved |
| 5329 | // class in the class_table_ during EnsureResolved. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 5330 | mirror::Class::SetStatus(klass, mirror::Class::kStatusResolved, self); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5331 | h_new_class_out->Assign(klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5332 | } else { |
| 5333 | CHECK(!klass->IsResolved()); |
| 5334 | // Retire the temporary class and create the correctly sized resolved class. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5335 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5336 | auto h_new_class = hs.NewHandle(klass->CopyOf(self, class_size, imt, image_pointer_size_)); |
Mathieu Chartier | 3ee25bb | 2015-08-10 10:13:02 -0700 | [diff] [blame] | 5337 | // Set arrays to null since we don't want to have multiple classes with the same ArtField or |
| 5338 | // ArtMethod array pointers. If this occurs, it causes bugs in remembered sets since the GC |
| 5339 | // may not see any references to the target space and clean the card for a class if another |
| 5340 | // class had the same array pointer. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5341 | klass->SetMethodsPtrUnchecked(nullptr, 0, 0); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5342 | klass->SetSFieldsPtrUnchecked(nullptr); |
| 5343 | klass->SetIFieldsPtrUnchecked(nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5344 | if (UNLIKELY(h_new_class == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5345 | self->AssertPendingOOMException(); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 5346 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorUnresolved, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5347 | return false; |
| 5348 | } |
| 5349 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5350 | CHECK_EQ(h_new_class->GetClassSize(), class_size); |
| 5351 | ObjectLock<mirror::Class> lock(self, h_new_class); |
| 5352 | FixupTemporaryDeclaringClass(klass.Get(), h_new_class.Get()); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5353 | |
| 5354 | { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5355 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5356 | ObjPtr<mirror::ClassLoader> const class_loader = h_new_class.Get()->GetClassLoader(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5357 | ClassTable* const table = InsertClassTableForClassLoader(class_loader); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5358 | ObjPtr<mirror::Class> existing = table->UpdateClass(descriptor, h_new_class.Get(), |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5359 | ComputeModifiedUtf8Hash(descriptor)); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 5360 | if (class_loader != nullptr) { |
| 5361 | // We updated the class in the class table, perform the write barrier so that the GC knows |
| 5362 | // about the change. |
| 5363 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader); |
| 5364 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5365 | CHECK_EQ(existing, klass.Get()); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 5366 | if (log_new_roots_) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5367 | new_class_roots_.push_back(GcRoot<mirror::Class>(h_new_class.Get())); |
| 5368 | } |
| 5369 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5370 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 5371 | // Update CHA info based on whether we override methods. |
| 5372 | // Have to do this before setting the class as resolved which allows |
| 5373 | // instantiation of klass. |
| 5374 | Runtime::Current()->GetClassHierarchyAnalysis()->UpdateAfterLoadingOf(h_new_class); |
| 5375 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5376 | // This will notify waiters on temp class that saw the not yet resolved class in the |
| 5377 | // class_table_ during EnsureResolved. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 5378 | mirror::Class::SetStatus(klass, mirror::Class::kStatusRetired, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5379 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5380 | CHECK_EQ(h_new_class->GetStatus(), mirror::Class::kStatusResolving); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5381 | // This will notify waiters on new_class that saw the not yet resolved |
| 5382 | // class in the class_table_ during EnsureResolved. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5383 | mirror::Class::SetStatus(h_new_class, mirror::Class::kStatusResolved, self); |
| 5384 | // Return the new class. |
| 5385 | h_new_class_out->Assign(h_new_class.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5386 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5387 | return true; |
| 5388 | } |
| 5389 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5390 | static void CountMethodsAndFields(ClassDataItemIterator& dex_data, |
| 5391 | size_t* virtual_methods, |
| 5392 | size_t* direct_methods, |
| 5393 | size_t* static_fields, |
| 5394 | size_t* instance_fields) { |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5395 | *virtual_methods = *direct_methods = *static_fields = *instance_fields = 0; |
| 5396 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5397 | while (dex_data.HasNextStaticField()) { |
| 5398 | dex_data.Next(); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5399 | (*static_fields)++; |
| 5400 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5401 | while (dex_data.HasNextInstanceField()) { |
| 5402 | dex_data.Next(); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5403 | (*instance_fields)++; |
| 5404 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5405 | while (dex_data.HasNextDirectMethod()) { |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5406 | (*direct_methods)++; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5407 | dex_data.Next(); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5408 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5409 | while (dex_data.HasNextVirtualMethod()) { |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5410 | (*virtual_methods)++; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5411 | dex_data.Next(); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5412 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5413 | DCHECK(!dex_data.HasNext()); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5414 | } |
| 5415 | |
| 5416 | static void DumpClass(std::ostream& os, |
| 5417 | const DexFile& dex_file, const DexFile::ClassDef& dex_class_def, |
| 5418 | const char* suffix) { |
| 5419 | ClassDataItemIterator dex_data(dex_file, dex_file.GetClassData(dex_class_def)); |
| 5420 | os << dex_file.GetClassDescriptor(dex_class_def) << suffix << ":\n"; |
| 5421 | os << " Static fields:\n"; |
| 5422 | while (dex_data.HasNextStaticField()) { |
| 5423 | const DexFile::FieldId& id = dex_file.GetFieldId(dex_data.GetMemberIndex()); |
| 5424 | os << " " << dex_file.GetFieldTypeDescriptor(id) << " " << dex_file.GetFieldName(id) << "\n"; |
| 5425 | dex_data.Next(); |
| 5426 | } |
| 5427 | os << " Instance fields:\n"; |
| 5428 | while (dex_data.HasNextInstanceField()) { |
| 5429 | const DexFile::FieldId& id = dex_file.GetFieldId(dex_data.GetMemberIndex()); |
| 5430 | os << " " << dex_file.GetFieldTypeDescriptor(id) << " " << dex_file.GetFieldName(id) << "\n"; |
| 5431 | dex_data.Next(); |
| 5432 | } |
| 5433 | os << " Direct methods:\n"; |
| 5434 | while (dex_data.HasNextDirectMethod()) { |
| 5435 | const DexFile::MethodId& id = dex_file.GetMethodId(dex_data.GetMemberIndex()); |
| 5436 | os << " " << dex_file.GetMethodName(id) << dex_file.GetMethodSignature(id).ToString() << "\n"; |
| 5437 | dex_data.Next(); |
| 5438 | } |
| 5439 | os << " Virtual methods:\n"; |
| 5440 | while (dex_data.HasNextVirtualMethod()) { |
| 5441 | const DexFile::MethodId& id = dex_file.GetMethodId(dex_data.GetMemberIndex()); |
| 5442 | os << " " << dex_file.GetMethodName(id) << dex_file.GetMethodSignature(id).ToString() << "\n"; |
| 5443 | dex_data.Next(); |
| 5444 | } |
| 5445 | } |
| 5446 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5447 | static std::string DumpClasses(const DexFile& dex_file1, |
| 5448 | const DexFile::ClassDef& dex_class_def1, |
| 5449 | const DexFile& dex_file2, |
| 5450 | const DexFile::ClassDef& dex_class_def2) { |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5451 | std::ostringstream os; |
| 5452 | DumpClass(os, dex_file1, dex_class_def1, " (Compile time)"); |
| 5453 | DumpClass(os, dex_file2, dex_class_def2, " (Runtime)"); |
| 5454 | return os.str(); |
| 5455 | } |
| 5456 | |
| 5457 | |
| 5458 | // Very simple structural check on whether the classes match. Only compares the number of |
| 5459 | // methods and fields. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5460 | static bool SimpleStructuralCheck(const DexFile& dex_file1, |
| 5461 | const DexFile::ClassDef& dex_class_def1, |
| 5462 | const DexFile& dex_file2, |
| 5463 | const DexFile::ClassDef& dex_class_def2, |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5464 | std::string* error_msg) { |
| 5465 | ClassDataItemIterator dex_data1(dex_file1, dex_file1.GetClassData(dex_class_def1)); |
| 5466 | ClassDataItemIterator dex_data2(dex_file2, dex_file2.GetClassData(dex_class_def2)); |
| 5467 | |
| 5468 | // Counters for current dex file. |
| 5469 | size_t dex_virtual_methods1, dex_direct_methods1, dex_static_fields1, dex_instance_fields1; |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5470 | CountMethodsAndFields(dex_data1, |
| 5471 | &dex_virtual_methods1, |
| 5472 | &dex_direct_methods1, |
| 5473 | &dex_static_fields1, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5474 | &dex_instance_fields1); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5475 | // Counters for compile-time dex file. |
| 5476 | size_t dex_virtual_methods2, dex_direct_methods2, dex_static_fields2, dex_instance_fields2; |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5477 | CountMethodsAndFields(dex_data2, |
| 5478 | &dex_virtual_methods2, |
| 5479 | &dex_direct_methods2, |
| 5480 | &dex_static_fields2, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5481 | &dex_instance_fields2); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5482 | |
| 5483 | if (dex_virtual_methods1 != dex_virtual_methods2) { |
| 5484 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5485 | *error_msg = StringPrintf("Virtual method count off: %zu vs %zu\n%s", |
| 5486 | dex_virtual_methods1, |
| 5487 | dex_virtual_methods2, |
| 5488 | class_dump.c_str()); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5489 | return false; |
| 5490 | } |
| 5491 | if (dex_direct_methods1 != dex_direct_methods2) { |
| 5492 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5493 | *error_msg = StringPrintf("Direct method count off: %zu vs %zu\n%s", |
| 5494 | dex_direct_methods1, |
| 5495 | dex_direct_methods2, |
| 5496 | class_dump.c_str()); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5497 | return false; |
| 5498 | } |
| 5499 | if (dex_static_fields1 != dex_static_fields2) { |
| 5500 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5501 | *error_msg = StringPrintf("Static field count off: %zu vs %zu\n%s", |
| 5502 | dex_static_fields1, |
| 5503 | dex_static_fields2, |
| 5504 | class_dump.c_str()); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5505 | return false; |
| 5506 | } |
| 5507 | if (dex_instance_fields1 != dex_instance_fields2) { |
| 5508 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5509 | *error_msg = StringPrintf("Instance field count off: %zu vs %zu\n%s", |
| 5510 | dex_instance_fields1, |
| 5511 | dex_instance_fields2, |
| 5512 | class_dump.c_str()); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5513 | return false; |
| 5514 | } |
| 5515 | |
| 5516 | return true; |
| 5517 | } |
| 5518 | |
| 5519 | // Checks whether a the super-class changed from what we had at compile-time. This would |
| 5520 | // invalidate quickening. |
| 5521 | static bool CheckSuperClassChange(Handle<mirror::Class> klass, |
| 5522 | const DexFile& dex_file, |
| 5523 | const DexFile::ClassDef& class_def, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5524 | ObjPtr<mirror::Class> super_class) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5525 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5526 | // Check for unexpected changes in the superclass. |
| 5527 | // Quick check 1) is the super_class class-loader the boot class loader? This always has |
| 5528 | // precedence. |
| 5529 | if (super_class->GetClassLoader() != nullptr && |
| 5530 | // Quick check 2) different dex cache? Breaks can only occur for different dex files, |
| 5531 | // which is implied by different dex cache. |
| 5532 | klass->GetDexCache() != super_class->GetDexCache()) { |
| 5533 | // Now comes the expensive part: things can be broken if (a) the klass' dex file has a |
| 5534 | // definition for the super-class, and (b) the files are in separate oat files. The oat files |
| 5535 | // are referenced from the dex file, so do (b) first. Only relevant if we have oat files. |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 5536 | const OatDexFile* class_oat_dex_file = dex_file.GetOatDexFile(); |
| 5537 | const OatFile* class_oat_file = nullptr; |
| 5538 | if (class_oat_dex_file != nullptr) { |
| 5539 | class_oat_file = class_oat_dex_file->GetOatFile(); |
| 5540 | } |
| 5541 | |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5542 | if (class_oat_file != nullptr) { |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 5543 | const OatDexFile* loaded_super_oat_dex_file = super_class->GetDexFile().GetOatDexFile(); |
| 5544 | const OatFile* loaded_super_oat_file = nullptr; |
| 5545 | if (loaded_super_oat_dex_file != nullptr) { |
| 5546 | loaded_super_oat_file = loaded_super_oat_dex_file->GetOatFile(); |
| 5547 | } |
| 5548 | |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5549 | if (loaded_super_oat_file != nullptr && class_oat_file != loaded_super_oat_file) { |
| 5550 | // Now check (a). |
| 5551 | const DexFile::ClassDef* super_class_def = dex_file.FindClassDef(class_def.superclass_idx_); |
| 5552 | if (super_class_def != nullptr) { |
| 5553 | // Uh-oh, we found something. Do our check. |
| 5554 | std::string error_msg; |
| 5555 | if (!SimpleStructuralCheck(dex_file, *super_class_def, |
| 5556 | super_class->GetDexFile(), *super_class->GetClassDef(), |
| 5557 | &error_msg)) { |
| 5558 | // Print a warning to the log. This exception might be caught, e.g., as common in test |
| 5559 | // drivers. When the class is later tried to be used, we re-throw a new instance, as we |
| 5560 | // only save the type of the exception. |
| 5561 | LOG(WARNING) << "Incompatible structural change detected: " << |
| 5562 | StringPrintf( |
| 5563 | "Structural change of %s is hazardous (%s at compile time, %s at runtime): %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5564 | dex_file.PrettyType(super_class_def->class_idx_).c_str(), |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5565 | class_oat_file->GetLocation().c_str(), |
| 5566 | loaded_super_oat_file->GetLocation().c_str(), |
| 5567 | error_msg.c_str()); |
| 5568 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 5569 | "Structural change of %s is hazardous (%s at compile time, %s at runtime): %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5570 | dex_file.PrettyType(super_class_def->class_idx_).c_str(), |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5571 | class_oat_file->GetLocation().c_str(), |
| 5572 | loaded_super_oat_file->GetLocation().c_str(), |
| 5573 | error_msg.c_str()); |
| 5574 | return false; |
| 5575 | } |
| 5576 | } |
| 5577 | } |
| 5578 | } |
| 5579 | } |
| 5580 | return true; |
| 5581 | } |
| 5582 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5583 | bool ClassLinker::LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5584 | CHECK_EQ(mirror::Class::kStatusIdx, klass->GetStatus()); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 5585 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5586 | dex::TypeIndex super_class_idx = class_def.superclass_idx_; |
| 5587 | if (super_class_idx.IsValid()) { |
Roland Levillain | 90328ac | 2016-05-18 12:25:38 +0100 | [diff] [blame] | 5588 | // Check that a class does not inherit from itself directly. |
| 5589 | // |
| 5590 | // TODO: This is a cheap check to detect the straightforward case |
| 5591 | // of a class extending itself (b/28685551), but we should do a |
| 5592 | // proper cycle detection on loaded classes, to detect all cases |
| 5593 | // of class circularity errors (b/28830038). |
| 5594 | if (super_class_idx == class_def.class_idx_) { |
| 5595 | ThrowClassCircularityError(klass.Get(), |
| 5596 | "Class %s extends itself", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5597 | klass->PrettyDescriptor().c_str()); |
Roland Levillain | 90328ac | 2016-05-18 12:25:38 +0100 | [diff] [blame] | 5598 | return false; |
| 5599 | } |
| 5600 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5601 | ObjPtr<mirror::Class> super_class = ResolveType(dex_file, super_class_idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5602 | if (super_class == nullptr) { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 5603 | DCHECK(Thread::Current()->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5604 | return false; |
| 5605 | } |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 5606 | // Verify |
| 5607 | if (!klass->CanAccess(super_class)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5608 | ThrowIllegalAccessError(klass.Get(), "Class %s extended by class %s is inaccessible", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5609 | super_class->PrettyDescriptor().c_str(), |
| 5610 | klass->PrettyDescriptor().c_str()); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 5611 | return false; |
| 5612 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5613 | CHECK(super_class->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5614 | klass->SetSuperClass(super_class); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5615 | |
| 5616 | if (!CheckSuperClassChange(klass, dex_file, class_def, super_class)) { |
| 5617 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5618 | return false; |
| 5619 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5620 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 5621 | const DexFile::TypeList* interfaces = dex_file.GetInterfacesList(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5622 | if (interfaces != nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5623 | for (size_t i = 0; i < interfaces->Size(); i++) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5624 | dex::TypeIndex idx = interfaces->GetTypeItem(i).type_idx_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5625 | ObjPtr<mirror::Class> interface = ResolveType(dex_file, idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5626 | if (interface == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5627 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5628 | return false; |
| 5629 | } |
| 5630 | // Verify |
| 5631 | if (!klass->CanAccess(interface)) { |
| 5632 | // TODO: the RI seemed to ignore this in my testing. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5633 | ThrowIllegalAccessError(klass.Get(), |
| 5634 | "Interface %s implemented by class %s is inaccessible", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5635 | interface->PrettyDescriptor().c_str(), |
| 5636 | klass->PrettyDescriptor().c_str()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5637 | return false; |
| 5638 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5639 | } |
| 5640 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 5641 | // Mark the class as loaded. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 5642 | mirror::Class::SetStatus(klass, mirror::Class::kStatusLoaded, nullptr); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5643 | return true; |
| 5644 | } |
| 5645 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5646 | bool ClassLinker::LinkSuperClass(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5647 | CHECK(!klass->IsPrimitive()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5648 | ObjPtr<mirror::Class> super = klass->GetSuperClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5649 | if (klass.Get() == GetClassRoot(kJavaLangObject)) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5650 | if (super != nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5651 | ThrowClassFormatError(klass.Get(), "java.lang.Object must not have a superclass"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5652 | return false; |
| 5653 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5654 | return true; |
| 5655 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5656 | if (super == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5657 | ThrowLinkageError(klass.Get(), "No superclass defined for class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5658 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5659 | return false; |
| 5660 | } |
| 5661 | // Verify |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 5662 | if (super->IsFinal() || super->IsInterface()) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5663 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 5664 | "Superclass %s of %s is %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5665 | super->PrettyDescriptor().c_str(), |
| 5666 | klass->PrettyDescriptor().c_str(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5667 | super->IsFinal() ? "declared final" : "an interface"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5668 | return false; |
| 5669 | } |
| 5670 | if (!klass->CanAccess(super)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5671 | ThrowIllegalAccessError(klass.Get(), "Superclass %s is inaccessible to class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5672 | super->PrettyDescriptor().c_str(), |
| 5673 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5674 | return false; |
| 5675 | } |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 5676 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5677 | // Inherit kAccClassIsFinalizable from the superclass in case this |
| 5678 | // class doesn't override finalize. |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 5679 | if (super->IsFinalizable()) { |
| 5680 | klass->SetFinalizable(); |
| 5681 | } |
| 5682 | |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 5683 | // Inherit class loader flag form super class. |
| 5684 | if (super->IsClassLoaderClass()) { |
| 5685 | klass->SetClassLoaderClass(); |
| 5686 | } |
| 5687 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5688 | // Inherit reference flags (if any) from the superclass. |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 5689 | uint32_t reference_flags = (super->GetClassFlags() & mirror::kClassFlagReference); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5690 | if (reference_flags != 0) { |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 5691 | CHECK_EQ(klass->GetClassFlags(), 0u); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 5692 | klass->SetClassFlags(klass->GetClassFlags() | reference_flags); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5693 | } |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 5694 | // Disallow custom direct subclasses of java.lang.ref.Reference. |
Elliott Hughes | bf61ba3 | 2011-10-11 10:53:09 -0700 | [diff] [blame] | 5695 | if (init_done_ && super == GetClassRoot(kJavaLangRefReference)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5696 | ThrowLinkageError(klass.Get(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5697 | "Class %s attempts to subclass java.lang.ref.Reference, which is not allowed", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5698 | klass->PrettyDescriptor().c_str()); |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 5699 | return false; |
| 5700 | } |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5701 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5702 | if (kIsDebugBuild) { |
| 5703 | // Ensure super classes are fully resolved prior to resolving fields.. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5704 | while (super != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5705 | CHECK(super->IsResolved()); |
| 5706 | super = super->GetSuperClass(); |
| 5707 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5708 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5709 | return true; |
| 5710 | } |
| 5711 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5712 | // Populate the class vtable and itable. Compute return type indices. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5713 | bool ClassLinker::LinkMethods(Thread* self, |
| 5714 | Handle<mirror::Class> klass, |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5715 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5716 | bool* out_new_conflict, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5717 | ArtMethod** out_imt) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5718 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5719 | // A map from vtable indexes to the method they need to be updated to point to. Used because we |
| 5720 | // need to have default methods be in the virtuals array of each class but we don't set that up |
| 5721 | // until LinkInterfaceMethods. |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5722 | std::unordered_map<size_t, ClassLinker::MethodTranslation> default_translations; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5723 | // Link virtual methods then interface methods. |
| 5724 | // We set up the interface lookup table first because we need it to determine if we need to update |
| 5725 | // any vtable entries with new default method implementations. |
| 5726 | return SetupInterfaceLookupTable(self, klass, interfaces) |
| 5727 | && LinkVirtualMethods(self, klass, /*out*/ &default_translations) |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5728 | && LinkInterfaceMethods(self, klass, default_translations, out_new_conflict, out_imt); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5729 | } |
| 5730 | |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5731 | // Comparator for name and signature of a method, used in finding overriding methods. Implementation |
| 5732 | // avoids the use of handles, if it didn't then rather than compare dex files we could compare dex |
| 5733 | // caches in the implementation below. |
| 5734 | class MethodNameAndSignatureComparator FINAL : public ValueObject { |
| 5735 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5736 | explicit MethodNameAndSignatureComparator(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5737 | REQUIRES_SHARED(Locks::mutator_lock_) : |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5738 | dex_file_(method->GetDexFile()), mid_(&dex_file_->GetMethodId(method->GetDexMethodIndex())), |
| 5739 | name_(nullptr), name_len_(0) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5740 | DCHECK(!method->IsProxyMethod()) << method->PrettyMethod(); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5741 | } |
| 5742 | |
| 5743 | const char* GetName() { |
| 5744 | if (name_ == nullptr) { |
| 5745 | name_ = dex_file_->StringDataAndUtf16LengthByIdx(mid_->name_idx_, &name_len_); |
| 5746 | } |
| 5747 | return name_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5748 | } |
| 5749 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5750 | bool HasSameNameAndSignature(ArtMethod* other) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5751 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5752 | DCHECK(!other->IsProxyMethod()) << other->PrettyMethod(); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5753 | const DexFile* other_dex_file = other->GetDexFile(); |
| 5754 | const DexFile::MethodId& other_mid = other_dex_file->GetMethodId(other->GetDexMethodIndex()); |
| 5755 | if (dex_file_ == other_dex_file) { |
| 5756 | return mid_->name_idx_ == other_mid.name_idx_ && mid_->proto_idx_ == other_mid.proto_idx_; |
| 5757 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5758 | GetName(); // Only used to make sure its calculated. |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5759 | uint32_t other_name_len; |
| 5760 | const char* other_name = other_dex_file->StringDataAndUtf16LengthByIdx(other_mid.name_idx_, |
| 5761 | &other_name_len); |
| 5762 | if (name_len_ != other_name_len || strcmp(name_, other_name) != 0) { |
| 5763 | return false; |
| 5764 | } |
| 5765 | return dex_file_->GetMethodSignature(*mid_) == other_dex_file->GetMethodSignature(other_mid); |
| 5766 | } |
| 5767 | |
| 5768 | private: |
| 5769 | // Dex file for the method to compare against. |
| 5770 | const DexFile* const dex_file_; |
| 5771 | // MethodId for the method to compare against. |
| 5772 | const DexFile::MethodId* const mid_; |
| 5773 | // Lazily computed name from the dex file's strings. |
| 5774 | const char* name_; |
| 5775 | // Lazily computed name length. |
| 5776 | uint32_t name_len_; |
| 5777 | }; |
| 5778 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5779 | class LinkVirtualHashTable { |
| 5780 | public: |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5781 | LinkVirtualHashTable(Handle<mirror::Class> klass, |
| 5782 | size_t hash_size, |
| 5783 | uint32_t* hash_table, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 5784 | PointerSize image_pointer_size) |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5785 | : klass_(klass), |
| 5786 | hash_size_(hash_size), |
| 5787 | hash_table_(hash_table), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5788 | image_pointer_size_(image_pointer_size) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5789 | std::fill(hash_table_, hash_table_ + hash_size_, invalid_index_); |
| 5790 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5791 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5792 | void Add(uint32_t virtual_method_index) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5793 | ArtMethod* local_method = klass_->GetVirtualMethodDuringLinking( |
| 5794 | virtual_method_index, image_pointer_size_); |
| 5795 | const char* name = local_method->GetInterfaceMethodIfProxy(image_pointer_size_)->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 5796 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5797 | uint32_t index = hash % hash_size_; |
| 5798 | // Linear probe until we have an empty slot. |
| 5799 | while (hash_table_[index] != invalid_index_) { |
| 5800 | if (++index == hash_size_) { |
| 5801 | index = 0; |
| 5802 | } |
| 5803 | } |
| 5804 | hash_table_[index] = virtual_method_index; |
| 5805 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5806 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5807 | uint32_t FindAndRemove(MethodNameAndSignatureComparator* comparator) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5808 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5809 | const char* name = comparator->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 5810 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5811 | size_t index = hash % hash_size_; |
| 5812 | while (true) { |
| 5813 | const uint32_t value = hash_table_[index]; |
| 5814 | // Since linear probe makes continuous blocks, hitting an invalid index means we are done |
| 5815 | // the block and can safely assume not found. |
| 5816 | if (value == invalid_index_) { |
| 5817 | break; |
| 5818 | } |
| 5819 | if (value != removed_index_) { // This signifies not already overriden. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5820 | ArtMethod* virtual_method = |
| 5821 | klass_->GetVirtualMethodDuringLinking(value, image_pointer_size_); |
| 5822 | if (comparator->HasSameNameAndSignature( |
| 5823 | virtual_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5824 | hash_table_[index] = removed_index_; |
| 5825 | return value; |
| 5826 | } |
| 5827 | } |
| 5828 | if (++index == hash_size_) { |
| 5829 | index = 0; |
| 5830 | } |
| 5831 | } |
| 5832 | return GetNotFoundIndex(); |
| 5833 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5834 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5835 | static uint32_t GetNotFoundIndex() { |
| 5836 | return invalid_index_; |
| 5837 | } |
| 5838 | |
| 5839 | private: |
| 5840 | static const uint32_t invalid_index_; |
| 5841 | static const uint32_t removed_index_; |
| 5842 | |
| 5843 | Handle<mirror::Class> klass_; |
| 5844 | const size_t hash_size_; |
| 5845 | uint32_t* const hash_table_; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 5846 | const PointerSize image_pointer_size_; |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5847 | }; |
| 5848 | |
| 5849 | const uint32_t LinkVirtualHashTable::invalid_index_ = std::numeric_limits<uint32_t>::max(); |
| 5850 | const uint32_t LinkVirtualHashTable::removed_index_ = std::numeric_limits<uint32_t>::max() - 1; |
| 5851 | |
Stephen Hines | 1ddd913 | 2017-02-08 01:51:18 -0800 | [diff] [blame] | 5852 | bool ClassLinker::LinkVirtualMethods( |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5853 | Thread* self, |
| 5854 | Handle<mirror::Class> klass, |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5855 | /*out*/std::unordered_map<size_t, ClassLinker::MethodTranslation>* default_translations) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5856 | const size_t num_virtual_methods = klass->NumVirtualMethods(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5857 | if (klass->IsInterface()) { |
| 5858 | // No vtable. |
| 5859 | if (!IsUint<16>(num_virtual_methods)) { |
| 5860 | ThrowClassFormatError(klass.Get(), "Too many methods on interface: %zu", num_virtual_methods); |
| 5861 | return false; |
| 5862 | } |
| 5863 | bool has_defaults = false; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5864 | // Assign each method an IMT index and set the default flag. |
| 5865 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
| 5866 | ArtMethod* m = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
| 5867 | m->SetMethodIndex(i); |
| 5868 | if (!m->IsAbstract()) { |
| 5869 | m->SetAccessFlags(m->GetAccessFlags() | kAccDefault); |
| 5870 | has_defaults = true; |
| 5871 | } |
| 5872 | } |
| 5873 | // Mark that we have default methods so that we won't need to scan the virtual_methods_ array |
| 5874 | // during initialization. This is a performance optimization. We could simply traverse the |
| 5875 | // virtual_methods_ array again during initialization. |
| 5876 | if (has_defaults) { |
| 5877 | klass->SetHasDefaultMethods(); |
| 5878 | } |
| 5879 | return true; |
| 5880 | } else if (klass->HasSuperClass()) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5881 | const size_t super_vtable_length = klass->GetSuperClass()->GetVTableLength(); |
| 5882 | const size_t max_count = num_virtual_methods + super_vtable_length; |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5883 | StackHandleScope<2> hs(self); |
Mingyao Yang | 38eecb0 | 2014-08-13 14:51:03 -0700 | [diff] [blame] | 5884 | Handle<mirror::Class> super_class(hs.NewHandle(klass->GetSuperClass())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5885 | MutableHandle<mirror::PointerArray> vtable; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5886 | if (super_class->ShouldHaveEmbeddedVTable()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5887 | vtable = hs.NewHandle(AllocPointerArray(self, max_count)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5888 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5889 | self->AssertPendingOOMException(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5890 | return false; |
| 5891 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5892 | for (size_t i = 0; i < super_vtable_length; i++) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5893 | vtable->SetElementPtrSize( |
| 5894 | i, super_class->GetEmbeddedVTableEntry(i, image_pointer_size_), image_pointer_size_); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5895 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5896 | // We might need to change vtable if we have new virtual methods or new interfaces (since that |
| 5897 | // might give us new default methods). If no new interfaces then we can skip the rest since |
| 5898 | // the class cannot override any of the super-class's methods. This is required for |
| 5899 | // correctness since without it we might not update overridden default method vtable entries |
| 5900 | // correctly. |
| 5901 | if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5902 | klass->SetVTable(vtable.Get()); |
| 5903 | return true; |
| 5904 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5905 | } else { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5906 | DCHECK(super_class->IsAbstract() && !super_class->IsArrayClass()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5907 | auto* super_vtable = super_class->GetVTable(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5908 | CHECK(super_vtable != nullptr) << super_class->PrettyClass(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5909 | // We might need to change vtable if we have new virtual methods or new interfaces (since that |
| 5910 | // might give us new default methods). See comment above. |
| 5911 | if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5912 | klass->SetVTable(super_vtable); |
| 5913 | return true; |
| 5914 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5915 | vtable = hs.NewHandle(down_cast<mirror::PointerArray*>( |
| 5916 | super_vtable->CopyOf(self, max_count))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5917 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5918 | self->AssertPendingOOMException(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5919 | return false; |
| 5920 | } |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5921 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5922 | // How the algorithm works: |
| 5923 | // 1. Populate hash table by adding num_virtual_methods from klass. The values in the hash |
| 5924 | // table are: invalid_index for unused slots, index super_vtable_length + i for a virtual |
| 5925 | // method which has not been matched to a vtable method, and j if the virtual method at the |
| 5926 | // index overrode the super virtual method at index j. |
| 5927 | // 2. Loop through super virtual methods, if they overwrite, update hash table to j |
| 5928 | // (j < super_vtable_length) to avoid redundant checks. (TODO maybe use this info for reducing |
| 5929 | // the need for the initial vtable which we later shrink back down). |
| 5930 | // 3. Add non overridden methods to the end of the vtable. |
| 5931 | static constexpr size_t kMaxStackHash = 250; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5932 | // + 1 so that even if we only have new default methods we will still be able to use this hash |
| 5933 | // table (i.e. it will never have 0 size). |
| 5934 | const size_t hash_table_size = num_virtual_methods * 3 + 1; |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5935 | uint32_t* hash_table_ptr; |
| 5936 | std::unique_ptr<uint32_t[]> hash_heap_storage; |
| 5937 | if (hash_table_size <= kMaxStackHash) { |
| 5938 | hash_table_ptr = reinterpret_cast<uint32_t*>( |
| 5939 | alloca(hash_table_size * sizeof(*hash_table_ptr))); |
| 5940 | } else { |
| 5941 | hash_heap_storage.reset(new uint32_t[hash_table_size]); |
| 5942 | hash_table_ptr = hash_heap_storage.get(); |
| 5943 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5944 | LinkVirtualHashTable hash_table(klass, hash_table_size, hash_table_ptr, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5945 | // Add virtual methods to the hash table. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5946 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5947 | DCHECK(klass->GetVirtualMethodDuringLinking( |
| 5948 | i, image_pointer_size_)->GetDeclaringClass() != nullptr); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5949 | hash_table.Add(i); |
| 5950 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5951 | // Loop through each super vtable method and see if they are overridden by a method we added to |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5952 | // the hash table. |
| 5953 | for (size_t j = 0; j < super_vtable_length; ++j) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5954 | // Search the hash table to see if we are overridden by any method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5955 | ArtMethod* super_method = vtable->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 5956 | if (!klass->CanAccessMember(super_method->GetDeclaringClass(), |
| 5957 | super_method->GetAccessFlags())) { |
| 5958 | // Continue on to the next method since this one is package private and canot be overridden. |
| 5959 | // Before Android 4.1, the package-private method super_method might have been incorrectly |
| 5960 | // overridden. |
| 5961 | continue; |
| 5962 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5963 | MethodNameAndSignatureComparator super_method_name_comparator( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5964 | super_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 5965 | // We remove the method so that subsequent lookups will be faster by making the hash-map |
| 5966 | // smaller as we go on. |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5967 | uint32_t hash_index = hash_table.FindAndRemove(&super_method_name_comparator); |
| 5968 | if (hash_index != hash_table.GetNotFoundIndex()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5969 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking( |
| 5970 | hash_index, image_pointer_size_); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 5971 | if (super_method->IsFinal()) { |
| 5972 | ThrowLinkageError(klass.Get(), "Method %s overrides final method in class %s", |
| 5973 | virtual_method->PrettyMethod().c_str(), |
| 5974 | super_method->GetDeclaringClassDescriptor()); |
| 5975 | return false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5976 | } |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 5977 | vtable->SetElementPtrSize(j, virtual_method, image_pointer_size_); |
| 5978 | virtual_method->SetMethodIndex(j); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5979 | } else if (super_method->IsOverridableByDefaultMethod()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5980 | // We didn't directly override this method but we might through default methods... |
| 5981 | // Check for default method update. |
| 5982 | ArtMethod* default_method = nullptr; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5983 | switch (FindDefaultMethodImplementation(self, |
| 5984 | super_method, |
| 5985 | klass, |
| 5986 | /*out*/&default_method)) { |
| 5987 | case DefaultMethodSearchResult::kDefaultConflict: { |
| 5988 | // A conflict was found looking for default methods. Note this (assuming it wasn't |
| 5989 | // pre-existing) in the translations map. |
| 5990 | if (UNLIKELY(!super_method->IsDefaultConflicting())) { |
| 5991 | // Don't generate another conflict method to reduce memory use as an optimization. |
| 5992 | default_translations->insert( |
| 5993 | {j, ClassLinker::MethodTranslation::CreateConflictingMethod()}); |
| 5994 | } |
| 5995 | break; |
| 5996 | } |
| 5997 | case DefaultMethodSearchResult::kAbstractFound: { |
| 5998 | // No conflict but method is abstract. |
| 5999 | // We note that this vtable entry must be made abstract. |
| 6000 | if (UNLIKELY(!super_method->IsAbstract())) { |
| 6001 | default_translations->insert( |
| 6002 | {j, ClassLinker::MethodTranslation::CreateAbstractMethod()}); |
| 6003 | } |
| 6004 | break; |
| 6005 | } |
| 6006 | case DefaultMethodSearchResult::kDefaultFound: { |
| 6007 | if (UNLIKELY(super_method->IsDefaultConflicting() || |
| 6008 | default_method->GetDeclaringClass() != super_method->GetDeclaringClass())) { |
| 6009 | // Found a default method implementation that is new. |
| 6010 | // TODO Refactor this add default methods to virtuals here and not in |
| 6011 | // LinkInterfaceMethods maybe. |
| 6012 | // The problem is default methods might override previously present |
| 6013 | // default-method or miranda-method vtable entries from the superclass. |
| 6014 | // Unfortunately we need these to be entries in this class's virtuals. We do not |
| 6015 | // give these entries there until LinkInterfaceMethods so we pass this map around |
| 6016 | // to let it know which vtable entries need to be updated. |
| 6017 | // Make a note that vtable entry j must be updated, store what it needs to be updated |
| 6018 | // to. We will allocate a virtual method slot in LinkInterfaceMethods and fix it up |
| 6019 | // then. |
| 6020 | default_translations->insert( |
| 6021 | {j, ClassLinker::MethodTranslation::CreateTranslatedMethod(default_method)}); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6022 | VLOG(class_linker) << "Method " << super_method->PrettyMethod() |
| 6023 | << " overridden by default " |
| 6024 | << default_method->PrettyMethod() |
| 6025 | << " in " << mirror::Class::PrettyClass(klass.Get()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6026 | } |
| 6027 | break; |
| 6028 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6029 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6030 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6031 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6032 | size_t actual_count = super_vtable_length; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6033 | // Add the non-overridden methods at the end. |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6034 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6035 | ArtMethod* local_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6036 | size_t method_idx = local_method->GetMethodIndexDuringLinking(); |
| 6037 | if (method_idx < super_vtable_length && |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6038 | local_method == vtable->GetElementPtrSize<ArtMethod*>(method_idx, image_pointer_size_)) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6039 | continue; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6040 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6041 | vtable->SetElementPtrSize(actual_count, local_method, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6042 | local_method->SetMethodIndex(actual_count); |
| 6043 | ++actual_count; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6044 | } |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 6045 | if (!IsUint<16>(actual_count)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6046 | ThrowClassFormatError(klass.Get(), "Too many methods defined on class: %zd", actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6047 | return false; |
| 6048 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6049 | // Shrink vtable if possible |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6050 | CHECK_LE(actual_count, max_count); |
| 6051 | if (actual_count < max_count) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6052 | vtable.Assign(down_cast<mirror::PointerArray*>(vtable->CopyOf(self, actual_count))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6053 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6054 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6055 | return false; |
| 6056 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6057 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6058 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6059 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 6060 | CHECK_EQ(klass.Get(), GetClassRoot(kJavaLangObject)); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 6061 | if (!IsUint<16>(num_virtual_methods)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6062 | ThrowClassFormatError(klass.Get(), "Too many methods: %d", |
| 6063 | static_cast<int>(num_virtual_methods)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6064 | return false; |
| 6065 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6066 | auto* vtable = AllocPointerArray(self, num_virtual_methods); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6067 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6068 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6069 | return false; |
| 6070 | } |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 6071 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6072 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
| 6073 | vtable->SetElementPtrSize(i, virtual_method, image_pointer_size_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6074 | virtual_method->SetMethodIndex(i & 0xFFFF); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6075 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6076 | klass->SetVTable(vtable); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6077 | } |
| 6078 | return true; |
| 6079 | } |
| 6080 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6081 | // Determine if the given iface has any subinterface in the given list that declares the method |
| 6082 | // specified by 'target'. |
| 6083 | // |
| 6084 | // Arguments |
| 6085 | // - self: The thread we are running on |
| 6086 | // - target: A comparator that will match any method that overrides the method we are checking for |
| 6087 | // - iftable: The iftable we are searching for an overriding method on. |
| 6088 | // - ifstart: The index of the interface we are checking to see if anything overrides |
| 6089 | // - iface: The interface we are checking to see if anything overrides. |
| 6090 | // - image_pointer_size: |
| 6091 | // The image pointer size. |
| 6092 | // |
| 6093 | // Returns |
| 6094 | // - True: There is some method that matches the target comparator defined in an interface that |
| 6095 | // is a subtype of iface. |
| 6096 | // - False: There is no method that matches the target comparator in any interface that is a subtype |
| 6097 | // of iface. |
| 6098 | static bool ContainsOverridingMethodOf(Thread* self, |
| 6099 | MethodNameAndSignatureComparator& target, |
| 6100 | Handle<mirror::IfTable> iftable, |
| 6101 | size_t ifstart, |
| 6102 | Handle<mirror::Class> iface, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6103 | PointerSize image_pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6104 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6105 | DCHECK(self != nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6106 | DCHECK(iface != nullptr); |
| 6107 | DCHECK(iftable != nullptr); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6108 | DCHECK_GE(ifstart, 0u); |
| 6109 | DCHECK_LT(ifstart, iftable->Count()); |
| 6110 | DCHECK_EQ(iface.Get(), iftable->GetInterface(ifstart)); |
| 6111 | DCHECK(iface->IsInterface()); |
| 6112 | |
| 6113 | size_t iftable_count = iftable->Count(); |
| 6114 | StackHandleScope<1> hs(self); |
| 6115 | MutableHandle<mirror::Class> current_iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 6116 | for (size_t k = ifstart + 1; k < iftable_count; k++) { |
| 6117 | // Skip ifstart since our current interface obviously cannot override itself. |
| 6118 | current_iface.Assign(iftable->GetInterface(k)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6119 | // Iterate through every method on this interface. The order does not matter. |
| 6120 | for (ArtMethod& current_method : current_iface->GetDeclaredVirtualMethods(image_pointer_size)) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6121 | if (UNLIKELY(target.HasSameNameAndSignature( |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6122 | current_method.GetInterfaceMethodIfProxy(image_pointer_size)))) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6123 | // Check if the i'th interface is a subtype of this one. |
| 6124 | if (iface->IsAssignableFrom(current_iface.Get())) { |
| 6125 | return true; |
| 6126 | } |
| 6127 | break; |
| 6128 | } |
| 6129 | } |
| 6130 | } |
| 6131 | return false; |
| 6132 | } |
| 6133 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6134 | // Find the default method implementation for 'interface_method' in 'klass'. Stores it into |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6135 | // out_default_method and returns kDefaultFound on success. If no default method was found return |
| 6136 | // kAbstractFound and store nullptr into out_default_method. If an error occurs (such as a |
| 6137 | // default_method conflict) it will return kDefaultConflict. |
| 6138 | ClassLinker::DefaultMethodSearchResult ClassLinker::FindDefaultMethodImplementation( |
| 6139 | Thread* self, |
| 6140 | ArtMethod* target_method, |
| 6141 | Handle<mirror::Class> klass, |
| 6142 | /*out*/ArtMethod** out_default_method) const { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6143 | DCHECK(self != nullptr); |
| 6144 | DCHECK(target_method != nullptr); |
| 6145 | DCHECK(out_default_method != nullptr); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6146 | |
| 6147 | *out_default_method = nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6148 | |
| 6149 | // We organize the interface table so that, for interface I any subinterfaces J follow it in the |
| 6150 | // table. This lets us walk the table backwards when searching for default methods. The first one |
| 6151 | // we encounter is the best candidate since it is the most specific. Once we have found it we keep |
| 6152 | // track of it and then continue checking all other interfaces, since we need to throw an error if |
| 6153 | // we encounter conflicting default method implementations (one is not a subtype of the other). |
| 6154 | // |
| 6155 | // The order of unrelated interfaces does not matter and is not defined. |
| 6156 | size_t iftable_count = klass->GetIfTableCount(); |
| 6157 | if (iftable_count == 0) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6158 | // No interfaces. We have already reset out to null so just return kAbstractFound. |
| 6159 | return DefaultMethodSearchResult::kAbstractFound; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6160 | } |
| 6161 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6162 | StackHandleScope<3> hs(self); |
| 6163 | MutableHandle<mirror::Class> chosen_iface(hs.NewHandle<mirror::Class>(nullptr)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6164 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable())); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6165 | MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6166 | MethodNameAndSignatureComparator target_name_comparator( |
| 6167 | target_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
| 6168 | // Iterates over the klass's iftable in reverse |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6169 | for (size_t k = iftable_count; k != 0; ) { |
| 6170 | --k; |
| 6171 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6172 | DCHECK_LT(k, iftable->Count()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6173 | |
| 6174 | iface.Assign(iftable->GetInterface(k)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6175 | // Iterate through every declared method on this interface. The order does not matter. |
| 6176 | for (auto& method_iter : iface->GetDeclaredVirtualMethods(image_pointer_size_)) { |
| 6177 | ArtMethod* current_method = &method_iter; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6178 | // Skip abstract methods and methods with different names. |
| 6179 | if (current_method->IsAbstract() || |
| 6180 | !target_name_comparator.HasSameNameAndSignature( |
| 6181 | current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
| 6182 | continue; |
Alex Light | d7c10c2 | 2016-03-31 10:03:07 -0700 | [diff] [blame] | 6183 | } else if (!current_method->IsPublic()) { |
| 6184 | // The verifier should have caught the non-public method for dex version 37. Just warn and |
| 6185 | // skip it since this is from before default-methods so we don't really need to care that it |
| 6186 | // has code. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6187 | LOG(WARNING) << "Interface method " << current_method->PrettyMethod() |
| 6188 | << " is not public! " |
Alex Light | d7c10c2 | 2016-03-31 10:03:07 -0700 | [diff] [blame] | 6189 | << "This will be a fatal error in subsequent versions of android. " |
| 6190 | << "Continuing anyway."; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6191 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6192 | if (UNLIKELY(chosen_iface != nullptr)) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6193 | // We have multiple default impls of the same method. This is a potential default conflict. |
| 6194 | // We need to check if this possibly conflicting method is either a superclass of the chosen |
| 6195 | // default implementation or is overridden by a non-default interface method. In either case |
| 6196 | // there is no conflict. |
| 6197 | if (!iface->IsAssignableFrom(chosen_iface.Get()) && |
| 6198 | !ContainsOverridingMethodOf(self, |
| 6199 | target_name_comparator, |
| 6200 | iftable, |
| 6201 | k, |
| 6202 | iface, |
| 6203 | image_pointer_size_)) { |
Nicolas Geoffray | 7f3e0db | 2016-01-28 09:29:31 +0000 | [diff] [blame] | 6204 | VLOG(class_linker) << "Conflicting default method implementations found: " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6205 | << current_method->PrettyMethod() << " and " |
| 6206 | << ArtMethod::PrettyMethod(*out_default_method) << " in class " |
| 6207 | << klass->PrettyClass() << " conflict."; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6208 | *out_default_method = nullptr; |
| 6209 | return DefaultMethodSearchResult::kDefaultConflict; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6210 | } else { |
| 6211 | break; // Continue checking at the next interface. |
| 6212 | } |
| 6213 | } else { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6214 | // chosen_iface == null |
| 6215 | if (!ContainsOverridingMethodOf(self, |
| 6216 | target_name_comparator, |
| 6217 | iftable, |
| 6218 | k, |
| 6219 | iface, |
| 6220 | image_pointer_size_)) { |
| 6221 | // Don't set this as the chosen interface if something else is overriding it (because that |
| 6222 | // other interface would be potentially chosen instead if it was default). If the other |
| 6223 | // interface was abstract then we wouldn't select this interface as chosen anyway since |
| 6224 | // the abstract method masks it. |
| 6225 | *out_default_method = current_method; |
| 6226 | chosen_iface.Assign(iface.Get()); |
| 6227 | // We should now finish traversing the graph to find if we have default methods that |
| 6228 | // conflict. |
| 6229 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6230 | VLOG(class_linker) << "A default method '" << current_method->PrettyMethod() |
| 6231 | << "' was " |
| 6232 | << "skipped because it was overridden by an abstract method in a " |
| 6233 | << "subinterface on class '" << klass->PrettyClass() << "'"; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6234 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6235 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6236 | break; |
| 6237 | } |
| 6238 | } |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6239 | if (*out_default_method != nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6240 | VLOG(class_linker) << "Default method '" << (*out_default_method)->PrettyMethod() |
| 6241 | << "' selected " |
| 6242 | << "as the implementation for '" << target_method->PrettyMethod() |
| 6243 | << "' in '" << klass->PrettyClass() << "'"; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6244 | return DefaultMethodSearchResult::kDefaultFound; |
| 6245 | } else { |
| 6246 | return DefaultMethodSearchResult::kAbstractFound; |
| 6247 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6248 | } |
| 6249 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6250 | ArtMethod* ClassLinker::AddMethodToConflictTable(ObjPtr<mirror::Class> klass, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6251 | ArtMethod* conflict_method, |
| 6252 | ArtMethod* interface_method, |
| 6253 | ArtMethod* method, |
| 6254 | bool force_new_conflict_method) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6255 | ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6256 | Runtime* const runtime = Runtime::Current(); |
| 6257 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
| 6258 | bool new_entry = conflict_method == runtime->GetImtConflictMethod() || force_new_conflict_method; |
| 6259 | |
| 6260 | // Create a new entry if the existing one is the shared conflict method. |
| 6261 | ArtMethod* new_conflict_method = new_entry |
| 6262 | ? runtime->CreateImtConflictMethod(linear_alloc) |
| 6263 | : conflict_method; |
| 6264 | |
| 6265 | // Allocate a new table. Note that we will leak this table at the next conflict, |
| 6266 | // but that's a tradeoff compared to making the table fixed size. |
| 6267 | void* data = linear_alloc->Alloc( |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6268 | Thread::Current(), ImtConflictTable::ComputeSizeWithOneMoreEntry(current_table, |
| 6269 | image_pointer_size_)); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6270 | if (data == nullptr) { |
| 6271 | LOG(ERROR) << "Failed to allocate conflict table"; |
| 6272 | return conflict_method; |
| 6273 | } |
| 6274 | ImtConflictTable* new_table = new (data) ImtConflictTable(current_table, |
| 6275 | interface_method, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6276 | method, |
| 6277 | image_pointer_size_); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6278 | |
| 6279 | // Do a fence to ensure threads see the data in the table before it is assigned |
| 6280 | // to the conflict method. |
| 6281 | // Note that there is a race in the presence of multiple threads and we may leak |
| 6282 | // memory from the LinearAlloc, but that's a tradeoff compared to using |
| 6283 | // atomic operations. |
| 6284 | QuasiAtomic::ThreadFenceRelease(); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6285 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6286 | return new_conflict_method; |
| 6287 | } |
| 6288 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6289 | bool ClassLinker::AllocateIfTableMethodArrays(Thread* self, |
| 6290 | Handle<mirror::Class> klass, |
| 6291 | Handle<mirror::IfTable> iftable) { |
| 6292 | DCHECK(!klass->IsInterface()); |
| 6293 | const bool has_superclass = klass->HasSuperClass(); |
| 6294 | const bool extend_super_iftable = has_superclass; |
| 6295 | const size_t ifcount = klass->GetIfTableCount(); |
| 6296 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
| 6297 | for (size_t i = 0; i < ifcount; ++i) { |
| 6298 | size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods(); |
| 6299 | if (num_methods > 0) { |
| 6300 | const bool is_super = i < super_ifcount; |
| 6301 | // This is an interface implemented by a super-class. Therefore we can just copy the method |
| 6302 | // array from the superclass. |
| 6303 | const bool super_interface = is_super && extend_super_iftable; |
| 6304 | ObjPtr<mirror::PointerArray> method_array; |
| 6305 | if (super_interface) { |
| 6306 | ObjPtr<mirror::IfTable> if_table = klass->GetSuperClass()->GetIfTable(); |
| 6307 | DCHECK(if_table != nullptr); |
| 6308 | DCHECK(if_table->GetMethodArray(i) != nullptr); |
| 6309 | // If we are working on a super interface, try extending the existing method array. |
| 6310 | method_array = down_cast<mirror::PointerArray*>(if_table->GetMethodArray(i)->Clone(self)); |
| 6311 | } else { |
| 6312 | method_array = AllocPointerArray(self, num_methods); |
| 6313 | } |
| 6314 | if (UNLIKELY(method_array == nullptr)) { |
| 6315 | self->AssertPendingOOMException(); |
| 6316 | return false; |
| 6317 | } |
| 6318 | iftable->SetMethodArray(i, method_array); |
| 6319 | } |
| 6320 | } |
| 6321 | return true; |
| 6322 | } |
| 6323 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6324 | void ClassLinker::SetIMTRef(ArtMethod* unimplemented_method, |
| 6325 | ArtMethod* imt_conflict_method, |
| 6326 | ArtMethod* current_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6327 | /*out*/bool* new_conflict, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6328 | /*out*/ArtMethod** imt_ref) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6329 | // Place method in imt if entry is empty, place conflict otherwise. |
| 6330 | if (*imt_ref == unimplemented_method) { |
| 6331 | *imt_ref = current_method; |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6332 | } else if (!(*imt_ref)->IsRuntimeMethod()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6333 | // If we are not a conflict and we have the same signature and name as the imt |
| 6334 | // entry, it must be that we overwrote a superclass vtable entry. |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6335 | // Note that we have checked IsRuntimeMethod, as there may be multiple different |
| 6336 | // conflict methods. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6337 | MethodNameAndSignatureComparator imt_comparator( |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6338 | (*imt_ref)->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6339 | if (imt_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6340 | current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6341 | *imt_ref = current_method; |
| 6342 | } else { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6343 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6344 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6345 | } |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6346 | } else { |
| 6347 | // Place the default conflict method. Note that there may be an existing conflict |
| 6348 | // method in the IMT, but it could be one tailored to the super class, with a |
| 6349 | // specific ImtConflictTable. |
| 6350 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6351 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6352 | } |
| 6353 | } |
| 6354 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6355 | void ClassLinker::FillIMTAndConflictTables(ObjPtr<mirror::Class> klass) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6356 | DCHECK(klass->ShouldHaveImt()) << klass->PrettyClass(); |
| 6357 | DCHECK(!klass->IsTemp()) << klass->PrettyClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6358 | ArtMethod* imt_data[ImTable::kSize]; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6359 | Runtime* const runtime = Runtime::Current(); |
| 6360 | ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod(); |
| 6361 | ArtMethod* const conflict_method = runtime->GetImtConflictMethod(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6362 | std::fill_n(imt_data, arraysize(imt_data), unimplemented_method); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6363 | if (klass->GetIfTable() != nullptr) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6364 | bool new_conflict = false; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6365 | FillIMTFromIfTable(klass->GetIfTable(), |
| 6366 | unimplemented_method, |
| 6367 | conflict_method, |
| 6368 | klass, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6369 | /*create_conflict_tables*/true, |
| 6370 | /*ignore_copied_methods*/false, |
| 6371 | &new_conflict, |
| 6372 | &imt_data[0]); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6373 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6374 | if (!klass->ShouldHaveImt()) { |
| 6375 | return; |
| 6376 | } |
| 6377 | // Compare the IMT with the super class including the conflict methods. If they are equivalent, |
| 6378 | // we can just use the same pointer. |
| 6379 | ImTable* imt = nullptr; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6380 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6381 | if (super_class != nullptr && super_class->ShouldHaveImt()) { |
| 6382 | ImTable* super_imt = super_class->GetImt(image_pointer_size_); |
| 6383 | bool same = true; |
| 6384 | for (size_t i = 0; same && i < ImTable::kSize; ++i) { |
| 6385 | ArtMethod* method = imt_data[i]; |
| 6386 | ArtMethod* super_method = super_imt->Get(i, image_pointer_size_); |
| 6387 | if (method != super_method) { |
| 6388 | bool is_conflict_table = method->IsRuntimeMethod() && |
| 6389 | method != unimplemented_method && |
| 6390 | method != conflict_method; |
| 6391 | // Verify conflict contents. |
| 6392 | bool super_conflict_table = super_method->IsRuntimeMethod() && |
| 6393 | super_method != unimplemented_method && |
| 6394 | super_method != conflict_method; |
| 6395 | if (!is_conflict_table || !super_conflict_table) { |
| 6396 | same = false; |
| 6397 | } else { |
| 6398 | ImtConflictTable* table1 = method->GetImtConflictTable(image_pointer_size_); |
| 6399 | ImtConflictTable* table2 = super_method->GetImtConflictTable(image_pointer_size_); |
| 6400 | same = same && table1->Equals(table2, image_pointer_size_); |
| 6401 | } |
| 6402 | } |
| 6403 | } |
| 6404 | if (same) { |
| 6405 | imt = super_imt; |
| 6406 | } |
| 6407 | } |
| 6408 | if (imt == nullptr) { |
| 6409 | imt = klass->GetImt(image_pointer_size_); |
| 6410 | DCHECK(imt != nullptr); |
| 6411 | imt->Populate(imt_data, image_pointer_size_); |
| 6412 | } else { |
| 6413 | klass->SetImt(imt, image_pointer_size_); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6414 | } |
| 6415 | } |
| 6416 | |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6417 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, |
| 6418 | LinearAlloc* linear_alloc, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6419 | PointerSize image_pointer_size) { |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6420 | void* data = linear_alloc->Alloc(Thread::Current(), |
| 6421 | ImtConflictTable::ComputeSize(count, |
| 6422 | image_pointer_size)); |
| 6423 | return (data != nullptr) ? new (data) ImtConflictTable(count, image_pointer_size) : nullptr; |
| 6424 | } |
| 6425 | |
| 6426 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, LinearAlloc* linear_alloc) { |
| 6427 | return CreateImtConflictTable(count, linear_alloc, image_pointer_size_); |
| 6428 | } |
| 6429 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6430 | void ClassLinker::FillIMTFromIfTable(ObjPtr<mirror::IfTable> if_table, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6431 | ArtMethod* unimplemented_method, |
| 6432 | ArtMethod* imt_conflict_method, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6433 | ObjPtr<mirror::Class> klass, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6434 | bool create_conflict_tables, |
| 6435 | bool ignore_copied_methods, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6436 | /*out*/bool* new_conflict, |
| 6437 | /*out*/ArtMethod** imt) { |
| 6438 | uint32_t conflict_counts[ImTable::kSize] = {}; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6439 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6440 | ObjPtr<mirror::Class> interface = if_table->GetInterface(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6441 | const size_t num_virtuals = interface->NumVirtualMethods(); |
| 6442 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 6443 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 6444 | DCHECK_GE(num_virtuals, method_array_count); |
| 6445 | if (kIsDebugBuild) { |
| 6446 | if (klass->IsInterface()) { |
| 6447 | DCHECK_EQ(method_array_count, 0u); |
| 6448 | } else { |
| 6449 | DCHECK_EQ(interface->NumDeclaredVirtualMethods(), method_array_count); |
| 6450 | } |
| 6451 | } |
| 6452 | if (method_array_count == 0) { |
| 6453 | continue; |
| 6454 | } |
| 6455 | auto* method_array = if_table->GetMethodArray(i); |
| 6456 | for (size_t j = 0; j < method_array_count; ++j) { |
| 6457 | ArtMethod* implementation_method = |
| 6458 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 6459 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 6460 | continue; |
| 6461 | } |
| 6462 | DCHECK(implementation_method != nullptr); |
| 6463 | // Miranda methods cannot be used to implement an interface method, but they are safe to put |
| 6464 | // in the IMT since their entrypoint is the interface trampoline. If we put any copied methods |
| 6465 | // or interface methods in the IMT here they will not create extra conflicts since we compare |
| 6466 | // names and signatures in SetIMTRef. |
| 6467 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 6468 | const uint32_t imt_index = ImTable::GetImtIndex(interface_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6469 | |
| 6470 | // There is only any conflicts if all of the interface methods for an IMT slot don't have |
| 6471 | // the same implementation method, keep track of this to avoid creating a conflict table in |
| 6472 | // this case. |
| 6473 | |
| 6474 | // Conflict table size for each IMT slot. |
| 6475 | ++conflict_counts[imt_index]; |
| 6476 | |
| 6477 | SetIMTRef(unimplemented_method, |
| 6478 | imt_conflict_method, |
| 6479 | implementation_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6480 | /*out*/new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6481 | /*out*/&imt[imt_index]); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6482 | } |
| 6483 | } |
| 6484 | |
| 6485 | if (create_conflict_tables) { |
| 6486 | // Create the conflict tables. |
| 6487 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6488 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6489 | size_t conflicts = conflict_counts[i]; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6490 | if (imt[i] == imt_conflict_method) { |
| 6491 | ImtConflictTable* new_table = CreateImtConflictTable(conflicts, linear_alloc); |
| 6492 | if (new_table != nullptr) { |
| 6493 | ArtMethod* new_conflict_method = |
| 6494 | Runtime::Current()->CreateImtConflictMethod(linear_alloc); |
| 6495 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
| 6496 | imt[i] = new_conflict_method; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6497 | } else { |
| 6498 | LOG(ERROR) << "Failed to allocate conflict table"; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6499 | imt[i] = imt_conflict_method; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6500 | } |
| 6501 | } else { |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6502 | DCHECK_NE(imt[i], imt_conflict_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6503 | } |
| 6504 | } |
| 6505 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6506 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6507 | ObjPtr<mirror::Class> interface = if_table->GetInterface(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6508 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 6509 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 6510 | if (method_array_count == 0) { |
| 6511 | continue; |
| 6512 | } |
| 6513 | auto* method_array = if_table->GetMethodArray(i); |
| 6514 | for (size_t j = 0; j < method_array_count; ++j) { |
| 6515 | ArtMethod* implementation_method = |
| 6516 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 6517 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 6518 | continue; |
| 6519 | } |
| 6520 | DCHECK(implementation_method != nullptr); |
| 6521 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 6522 | const uint32_t imt_index = ImTable::GetImtIndex(interface_method); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6523 | if (!imt[imt_index]->IsRuntimeMethod() || |
| 6524 | imt[imt_index] == unimplemented_method || |
| 6525 | imt[imt_index] == imt_conflict_method) { |
| 6526 | continue; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6527 | } |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6528 | ImtConflictTable* table = imt[imt_index]->GetImtConflictTable(image_pointer_size_); |
| 6529 | const size_t num_entries = table->NumEntries(image_pointer_size_); |
| 6530 | table->SetInterfaceMethod(num_entries, image_pointer_size_, interface_method); |
| 6531 | table->SetImplementationMethod(num_entries, image_pointer_size_, implementation_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6532 | } |
| 6533 | } |
| 6534 | } |
| 6535 | } |
| 6536 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6537 | // Simple helper function that checks that no subtypes of 'val' are contained within the 'classes' |
| 6538 | // set. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6539 | static bool NotSubinterfaceOfAny( |
| 6540 | const std::unordered_set<ObjPtr<mirror::Class>, HashObjPtr>& classes, |
| 6541 | ObjPtr<mirror::Class> val) |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6542 | REQUIRES(Roles::uninterruptible_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6543 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6544 | DCHECK(val != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6545 | for (ObjPtr<mirror::Class> c : classes) { |
| 6546 | if (val->IsAssignableFrom(c)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6547 | return false; |
| 6548 | } |
| 6549 | } |
| 6550 | return true; |
| 6551 | } |
| 6552 | |
| 6553 | // Fills in and flattens the interface inheritance hierarchy. |
| 6554 | // |
| 6555 | // By the end of this function all interfaces in the transitive closure of to_process are added to |
| 6556 | // the iftable and every interface precedes all of its sub-interfaces in this list. |
| 6557 | // |
| 6558 | // all I, J: Interface | I <: J implies J precedes I |
| 6559 | // |
| 6560 | // (note A <: B means that A is a subtype of B) |
| 6561 | // |
| 6562 | // This returns the total number of items in the iftable. The iftable might be resized down after |
| 6563 | // this call. |
| 6564 | // |
| 6565 | // We order this backwards so that we do not need to reorder superclass interfaces when new |
| 6566 | // interfaces are added in subclass's interface tables. |
| 6567 | // |
| 6568 | // Upon entry into this function iftable is a copy of the superclass's iftable with the first |
| 6569 | // super_ifcount entries filled in with the transitive closure of the interfaces of the superclass. |
| 6570 | // The other entries are uninitialized. We will fill in the remaining entries in this function. The |
| 6571 | // iftable must be large enough to hold all interfaces without changing its size. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6572 | static size_t FillIfTable(ObjPtr<mirror::IfTable> iftable, |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6573 | size_t super_ifcount, |
| 6574 | std::vector<mirror::Class*> to_process) |
| 6575 | REQUIRES(Roles::uninterruptible_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6576 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6577 | // This is the set of all class's already in the iftable. Used to make checking if a class has |
| 6578 | // already been added quicker. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6579 | std::unordered_set<ObjPtr<mirror::Class>, HashObjPtr> classes_in_iftable; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6580 | // The first super_ifcount elements are from the superclass. We note that they are already added. |
| 6581 | for (size_t i = 0; i < super_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6582 | ObjPtr<mirror::Class> iface = iftable->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6583 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, iface)) << "Bad ordering."; |
| 6584 | classes_in_iftable.insert(iface); |
| 6585 | } |
| 6586 | size_t filled_ifcount = super_ifcount; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6587 | for (ObjPtr<mirror::Class> interface : to_process) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6588 | // Let us call the first filled_ifcount elements of iftable the current-iface-list. |
| 6589 | // At this point in the loop current-iface-list has the invariant that: |
| 6590 | // for every pair of interfaces I,J within it: |
| 6591 | // if index_of(I) < index_of(J) then I is not a subtype of J |
| 6592 | |
| 6593 | // If we have already seen this element then all of its super-interfaces must already be in the |
| 6594 | // current-iface-list so we can skip adding it. |
| 6595 | if (!ContainsElement(classes_in_iftable, interface)) { |
| 6596 | // We haven't seen this interface so add all of its super-interfaces onto the |
| 6597 | // current-iface-list, skipping those already on it. |
| 6598 | int32_t ifcount = interface->GetIfTableCount(); |
| 6599 | for (int32_t j = 0; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6600 | ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6601 | if (!ContainsElement(classes_in_iftable, super_interface)) { |
| 6602 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, super_interface)) << "Bad ordering."; |
| 6603 | classes_in_iftable.insert(super_interface); |
| 6604 | iftable->SetInterface(filled_ifcount, super_interface); |
| 6605 | filled_ifcount++; |
| 6606 | } |
| 6607 | } |
| 6608 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, interface)) << "Bad ordering"; |
| 6609 | // Place this interface onto the current-iface-list after all of its super-interfaces. |
| 6610 | classes_in_iftable.insert(interface); |
| 6611 | iftable->SetInterface(filled_ifcount, interface); |
| 6612 | filled_ifcount++; |
| 6613 | } else if (kIsDebugBuild) { |
| 6614 | // Check all super-interfaces are already in the list. |
| 6615 | int32_t ifcount = interface->GetIfTableCount(); |
| 6616 | for (int32_t j = 0; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6617 | ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6618 | DCHECK(ContainsElement(classes_in_iftable, super_interface)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6619 | << "Iftable does not contain " << mirror::Class::PrettyClass(super_interface) |
| 6620 | << ", a superinterface of " << interface->PrettyClass(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6621 | } |
| 6622 | } |
| 6623 | } |
| 6624 | if (kIsDebugBuild) { |
| 6625 | // Check that the iftable is ordered correctly. |
| 6626 | for (size_t i = 0; i < filled_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6627 | ObjPtr<mirror::Class> if_a = iftable->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6628 | for (size_t j = i + 1; j < filled_ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6629 | ObjPtr<mirror::Class> if_b = iftable->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6630 | // !(if_a <: if_b) |
| 6631 | CHECK(!if_b->IsAssignableFrom(if_a)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6632 | << "Bad interface order: " << mirror::Class::PrettyClass(if_a) << " (index " << i |
| 6633 | << ") extends " |
| 6634 | << if_b->PrettyClass() << " (index " << j << ") and so should be after it in the " |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6635 | << "interface list."; |
| 6636 | } |
| 6637 | } |
| 6638 | } |
| 6639 | return filled_ifcount; |
| 6640 | } |
| 6641 | |
| 6642 | bool ClassLinker::SetupInterfaceLookupTable(Thread* self, Handle<mirror::Class> klass, |
| 6643 | Handle<mirror::ObjectArray<mirror::Class>> interfaces) { |
| 6644 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 6645 | const bool has_superclass = klass->HasSuperClass(); |
| 6646 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6647 | const bool have_interfaces = interfaces != nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6648 | const size_t num_interfaces = |
| 6649 | have_interfaces ? interfaces->GetLength() : klass->NumDirectInterfaces(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6650 | if (num_interfaces == 0) { |
| 6651 | if (super_ifcount == 0) { |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 6652 | if (LIKELY(has_superclass)) { |
| 6653 | klass->SetIfTable(klass->GetSuperClass()->GetIfTable()); |
| 6654 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6655 | // Class implements no interfaces. |
| 6656 | DCHECK_EQ(klass->GetIfTableCount(), 0); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6657 | return true; |
| 6658 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6659 | // Class implements same interfaces as parent, are any of these not marker interfaces? |
| 6660 | bool has_non_marker_interface = false; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6661 | ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6662 | for (size_t i = 0; i < super_ifcount; ++i) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6663 | if (super_iftable->GetMethodArrayCount(i) > 0) { |
| 6664 | has_non_marker_interface = true; |
| 6665 | break; |
| 6666 | } |
| 6667 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6668 | // Class just inherits marker interfaces from parent so recycle parent's iftable. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6669 | if (!has_non_marker_interface) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6670 | klass->SetIfTable(super_iftable); |
| 6671 | return true; |
| 6672 | } |
| 6673 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6674 | size_t ifcount = super_ifcount + num_interfaces; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6675 | // Check that every class being implemented is an interface. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6676 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 6677 | ObjPtr<mirror::Class> interface = have_interfaces |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6678 | ? interfaces->GetWithoutChecks(i) |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 6679 | : mirror::Class::GetDirectInterface(self, klass.Get(), i); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6680 | DCHECK(interface != nullptr); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6681 | if (UNLIKELY(!interface->IsInterface())) { |
| 6682 | std::string temp; |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6683 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 6684 | "Class %s implements non-interface class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6685 | klass->PrettyDescriptor().c_str(), |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6686 | PrettyDescriptor(interface->GetDescriptor(&temp)).c_str()); |
| 6687 | return false; |
| 6688 | } |
| 6689 | ifcount += interface->GetIfTableCount(); |
| 6690 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6691 | // Create the interface function table. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 6692 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(AllocIfTable(self, ifcount))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6693 | if (UNLIKELY(iftable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6694 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6695 | return false; |
| 6696 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6697 | // Fill in table with superclass's iftable. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6698 | if (super_ifcount != 0) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6699 | ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable(); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 6700 | for (size_t i = 0; i < super_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6701 | ObjPtr<mirror::Class> super_interface = super_iftable->GetInterface(i); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6702 | iftable->SetInterface(i, super_interface); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 6703 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6704 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6705 | |
| 6706 | // Note that AllowThreadSuspension is to thread suspension as pthread_testcancel is to pthread |
| 6707 | // cancellation. That is it will suspend if one has a pending suspend request but otherwise |
| 6708 | // doesn't really do anything. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 6709 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6710 | |
| 6711 | size_t new_ifcount; |
| 6712 | { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 6713 | ScopedAssertNoThreadSuspension nts("Copying mirror::Class*'s for FillIfTable"); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6714 | std::vector<mirror::Class*> to_add; |
| 6715 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 6716 | ObjPtr<mirror::Class> interface = have_interfaces ? interfaces->Get(i) : |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 6717 | mirror::Class::GetDirectInterface(self, klass.Get(), i); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 6718 | to_add.push_back(interface.Ptr()); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6719 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6720 | |
| 6721 | new_ifcount = FillIfTable(iftable.Get(), super_ifcount, std::move(to_add)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6722 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6723 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 6724 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6725 | |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6726 | // Shrink iftable in case duplicates were found |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6727 | if (new_ifcount < ifcount) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6728 | DCHECK_NE(num_interfaces, 0U); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6729 | iftable.Assign(down_cast<mirror::IfTable*>( |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6730 | iftable->CopyOf(self, new_ifcount * mirror::IfTable::kMax))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6731 | if (UNLIKELY(iftable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6732 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6733 | return false; |
| 6734 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6735 | ifcount = new_ifcount; |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6736 | } else { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6737 | DCHECK_EQ(new_ifcount, ifcount); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6738 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6739 | klass->SetIfTable(iftable.Get()); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6740 | return true; |
| 6741 | } |
| 6742 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6743 | // Finds the method with a name/signature that matches cmp in the given lists of methods. The list |
| 6744 | // of methods must be unique. |
| 6745 | static ArtMethod* FindSameNameAndSignature(MethodNameAndSignatureComparator& cmp ATTRIBUTE_UNUSED) { |
| 6746 | return nullptr; |
| 6747 | } |
| 6748 | |
| 6749 | template <typename ... Types> |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6750 | static ArtMethod* FindSameNameAndSignature(MethodNameAndSignatureComparator& cmp, |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6751 | const ScopedArenaVector<ArtMethod*>& list, |
| 6752 | const Types& ... rest) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6753 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6754 | for (ArtMethod* method : list) { |
| 6755 | if (cmp.HasSameNameAndSignature(method)) { |
| 6756 | return method; |
| 6757 | } |
| 6758 | } |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6759 | return FindSameNameAndSignature(cmp, rest...); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6760 | } |
| 6761 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6762 | // Check that all vtable entries are present in this class's virtuals or are the same as a |
| 6763 | // superclasses vtable entry. |
| 6764 | static void CheckClassOwnsVTableEntries(Thread* self, |
| 6765 | Handle<mirror::Class> klass, |
| 6766 | PointerSize pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6767 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6768 | StackHandleScope<2> hs(self); |
| 6769 | Handle<mirror::PointerArray> check_vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6770 | ObjPtr<mirror::Class> super_temp = (klass->HasSuperClass()) ? klass->GetSuperClass() : nullptr; |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6771 | Handle<mirror::Class> superclass(hs.NewHandle(super_temp)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6772 | int32_t super_vtable_length = (superclass != nullptr) ? superclass->GetVTableLength() : 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6773 | for (int32_t i = 0; i < check_vtable->GetLength(); ++i) { |
| 6774 | ArtMethod* m = check_vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 6775 | CHECK(m != nullptr); |
| 6776 | |
Alex Light | a41a3078 | 2017-03-29 11:33:19 -0700 | [diff] [blame] | 6777 | if (m->GetMethodIndexDuringLinking() != i) { |
| 6778 | LOG(WARNING) << m->PrettyMethod() |
| 6779 | << " has an unexpected method index for its spot in the vtable for class" |
| 6780 | << klass->PrettyClass(); |
| 6781 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6782 | ArraySlice<ArtMethod> virtuals = klass->GetVirtualMethodsSliceUnchecked(pointer_size); |
| 6783 | auto is_same_method = [m] (const ArtMethod& meth) { |
| 6784 | return &meth == m; |
| 6785 | }; |
Alex Light | 3f98053 | 2017-03-17 15:10:32 -0700 | [diff] [blame] | 6786 | if (!((super_vtable_length > i && superclass->GetVTableEntry(i, pointer_size) == m) || |
| 6787 | std::find_if(virtuals.begin(), virtuals.end(), is_same_method) != virtuals.end())) { |
| 6788 | LOG(WARNING) << m->PrettyMethod() << " does not seem to be owned by current class " |
| 6789 | << klass->PrettyClass() << " or any of its superclasses!"; |
| 6790 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6791 | } |
| 6792 | } |
| 6793 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6794 | // Check to make sure the vtable does not have duplicates. Duplicates could cause problems when a |
| 6795 | // method is overridden in a subclass. |
| 6796 | static void CheckVTableHasNoDuplicates(Thread* self, |
| 6797 | Handle<mirror::Class> klass, |
| 6798 | PointerSize pointer_size) |
| 6799 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6800 | StackHandleScope<1> hs(self); |
| 6801 | Handle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
| 6802 | int32_t num_entries = vtable->GetLength(); |
| 6803 | for (int32_t i = 0; i < num_entries; i++) { |
| 6804 | ArtMethod* vtable_entry = vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 6805 | // Don't bother if we cannot 'see' the vtable entry (i.e. it is a package-private member maybe). |
| 6806 | if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(), |
| 6807 | vtable_entry->GetAccessFlags())) { |
| 6808 | continue; |
| 6809 | } |
| 6810 | MethodNameAndSignatureComparator name_comparator( |
| 6811 | vtable_entry->GetInterfaceMethodIfProxy(pointer_size)); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 6812 | for (int32_t j = i + 1; j < num_entries; j++) { |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6813 | ArtMethod* other_entry = vtable->GetElementPtrSize<ArtMethod*>(j, pointer_size); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 6814 | if (!klass->CanAccessMember(other_entry->GetDeclaringClass(), |
| 6815 | other_entry->GetAccessFlags())) { |
| 6816 | continue; |
| 6817 | } |
Alex Light | 3f98053 | 2017-03-17 15:10:32 -0700 | [diff] [blame] | 6818 | if (vtable_entry == other_entry || |
| 6819 | name_comparator.HasSameNameAndSignature( |
| 6820 | other_entry->GetInterfaceMethodIfProxy(pointer_size))) { |
| 6821 | LOG(WARNING) << "vtable entries " << i << " and " << j << " are identical for " |
| 6822 | << klass->PrettyClass() << " in method " << vtable_entry->PrettyMethod() |
| 6823 | << " (0x" << std::hex << reinterpret_cast<uintptr_t>(vtable_entry) << ") and " |
| 6824 | << other_entry->PrettyMethod() << " (0x" << std::hex |
| 6825 | << reinterpret_cast<uintptr_t>(other_entry) << ")"; |
| 6826 | } |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6827 | } |
| 6828 | } |
| 6829 | } |
| 6830 | |
| 6831 | static void SanityCheckVTable(Thread* self, Handle<mirror::Class> klass, PointerSize pointer_size) |
| 6832 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6833 | CheckClassOwnsVTableEntries(self, klass, pointer_size); |
| 6834 | CheckVTableHasNoDuplicates(self, klass, pointer_size); |
| 6835 | } |
| 6836 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6837 | void ClassLinker::FillImtFromSuperClass(Handle<mirror::Class> klass, |
| 6838 | ArtMethod* unimplemented_method, |
| 6839 | ArtMethod* imt_conflict_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6840 | bool* new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6841 | ArtMethod** imt) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6842 | DCHECK(klass->HasSuperClass()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6843 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6844 | if (super_class->ShouldHaveImt()) { |
| 6845 | ImTable* super_imt = super_class->GetImt(image_pointer_size_); |
| 6846 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 6847 | imt[i] = super_imt->Get(i, image_pointer_size_); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6848 | } |
| 6849 | } else { |
| 6850 | // No imt in the super class, need to reconstruct from the iftable. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6851 | ObjPtr<mirror::IfTable> if_table = super_class->GetIfTable(); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 6852 | if (if_table->Count() != 0) { |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6853 | // Ignore copied methods since we will handle these in LinkInterfaceMethods. |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6854 | FillIMTFromIfTable(if_table, |
| 6855 | unimplemented_method, |
| 6856 | imt_conflict_method, |
| 6857 | klass.Get(), |
| 6858 | /*create_conflict_table*/false, |
| 6859 | /*ignore_copied_methods*/true, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6860 | /*out*/new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6861 | /*out*/imt); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6862 | } |
| 6863 | } |
| 6864 | } |
| 6865 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6866 | class ClassLinker::LinkInterfaceMethodsHelper { |
| 6867 | public: |
| 6868 | LinkInterfaceMethodsHelper(ClassLinker* class_linker, |
| 6869 | Handle<mirror::Class> klass, |
| 6870 | Thread* self, |
| 6871 | Runtime* runtime) |
| 6872 | : class_linker_(class_linker), |
| 6873 | klass_(klass), |
| 6874 | method_alignment_(ArtMethod::Alignment(class_linker->GetImagePointerSize())), |
| 6875 | method_size_(ArtMethod::Size(class_linker->GetImagePointerSize())), |
| 6876 | self_(self), |
| 6877 | stack_(runtime->GetLinearAlloc()->GetArenaPool()), |
| 6878 | allocator_(&stack_), |
| 6879 | default_conflict_methods_(allocator_.Adapter()), |
| 6880 | overriding_default_conflict_methods_(allocator_.Adapter()), |
| 6881 | miranda_methods_(allocator_.Adapter()), |
| 6882 | default_methods_(allocator_.Adapter()), |
| 6883 | overriding_default_methods_(allocator_.Adapter()), |
| 6884 | move_table_(allocator_.Adapter()) { |
| 6885 | } |
| 6886 | |
| 6887 | ArtMethod* FindMethod(ArtMethod* interface_method, |
| 6888 | MethodNameAndSignatureComparator& interface_name_comparator, |
| 6889 | ArtMethod* vtable_impl) |
| 6890 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 6891 | |
| 6892 | ArtMethod* GetOrCreateMirandaMethod(ArtMethod* interface_method, |
| 6893 | MethodNameAndSignatureComparator& interface_name_comparator) |
| 6894 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 6895 | |
| 6896 | bool HasNewVirtuals() const { |
| 6897 | return !(miranda_methods_.empty() && |
| 6898 | default_methods_.empty() && |
| 6899 | overriding_default_methods_.empty() && |
| 6900 | overriding_default_conflict_methods_.empty() && |
| 6901 | default_conflict_methods_.empty()); |
| 6902 | } |
| 6903 | |
| 6904 | void ReallocMethods() REQUIRES_SHARED(Locks::mutator_lock_); |
| 6905 | |
| 6906 | ObjPtr<mirror::PointerArray> UpdateVtable( |
| 6907 | const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations, |
| 6908 | ObjPtr<mirror::PointerArray> old_vtable) REQUIRES_SHARED(Locks::mutator_lock_); |
| 6909 | |
| 6910 | void UpdateIfTable(Handle<mirror::IfTable> iftable) REQUIRES_SHARED(Locks::mutator_lock_); |
| 6911 | |
| 6912 | void UpdateIMT(ArtMethod** out_imt); |
| 6913 | |
| 6914 | void CheckNoStaleMethodsInDexCache() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6915 | if (kIsDebugBuild) { |
| 6916 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 6917 | // Check that there are no stale methods are in the dex cache array. |
| 6918 | auto* resolved_methods = klass_->GetDexCache()->GetResolvedMethods(); |
| 6919 | for (size_t i = 0, count = klass_->GetDexCache()->NumResolvedMethods(); i < count; ++i) { |
| 6920 | auto* m = mirror::DexCache::GetElementPtrSize(resolved_methods, i, pointer_size); |
| 6921 | CHECK(move_table_.find(m) == move_table_.end() || |
| 6922 | // The original versions of copied methods will still be present so allow those too. |
| 6923 | // Note that if the first check passes this might fail to GetDeclaringClass(). |
| 6924 | std::find_if(m->GetDeclaringClass()->GetMethods(pointer_size).begin(), |
| 6925 | m->GetDeclaringClass()->GetMethods(pointer_size).end(), |
| 6926 | [m] (ArtMethod& meth) { |
| 6927 | return &meth == m; |
| 6928 | }) != m->GetDeclaringClass()->GetMethods(pointer_size).end()) |
| 6929 | << "Obsolete method " << m->PrettyMethod() << " is in dex cache!"; |
| 6930 | } |
| 6931 | } |
| 6932 | } |
| 6933 | |
| 6934 | void ClobberOldMethods(LengthPrefixedArray<ArtMethod>* old_methods, |
| 6935 | LengthPrefixedArray<ArtMethod>* methods) { |
| 6936 | if (kIsDebugBuild) { |
| 6937 | CHECK(methods != nullptr); |
| 6938 | // Put some random garbage in old methods to help find stale pointers. |
| 6939 | if (methods != old_methods && old_methods != nullptr) { |
| 6940 | // Need to make sure the GC is not running since it could be scanning the methods we are |
| 6941 | // about to overwrite. |
| 6942 | ScopedThreadStateChange tsc(self_, kSuspended); |
| 6943 | gc::ScopedGCCriticalSection gcs(self_, |
| 6944 | gc::kGcCauseClassLinker, |
| 6945 | gc::kCollectorTypeClassLinker); |
| 6946 | const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_methods->size(), |
| 6947 | method_size_, |
| 6948 | method_alignment_); |
| 6949 | memset(old_methods, 0xFEu, old_size); |
| 6950 | } |
| 6951 | } |
| 6952 | } |
| 6953 | |
| 6954 | private: |
| 6955 | size_t NumberOfNewVirtuals() const { |
| 6956 | return miranda_methods_.size() + |
| 6957 | default_methods_.size() + |
| 6958 | overriding_default_conflict_methods_.size() + |
| 6959 | overriding_default_methods_.size() + |
| 6960 | default_conflict_methods_.size(); |
| 6961 | } |
| 6962 | |
| 6963 | bool FillTables() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6964 | return !klass_->IsInterface(); |
| 6965 | } |
| 6966 | |
| 6967 | void LogNewVirtuals() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6968 | DCHECK(!klass_->IsInterface() || (default_methods_.empty() && miranda_methods_.empty())) |
| 6969 | << "Interfaces should only have default-conflict methods appended to them."; |
| 6970 | VLOG(class_linker) << mirror::Class::PrettyClass(klass_.Get()) << ": miranda_methods=" |
| 6971 | << miranda_methods_.size() |
| 6972 | << " default_methods=" << default_methods_.size() |
| 6973 | << " overriding_default_methods=" << overriding_default_methods_.size() |
| 6974 | << " default_conflict_methods=" << default_conflict_methods_.size() |
| 6975 | << " overriding_default_conflict_methods=" |
| 6976 | << overriding_default_conflict_methods_.size(); |
| 6977 | } |
| 6978 | |
| 6979 | ClassLinker* class_linker_; |
| 6980 | Handle<mirror::Class> klass_; |
| 6981 | size_t method_alignment_; |
| 6982 | size_t method_size_; |
| 6983 | Thread* const self_; |
| 6984 | |
| 6985 | // These are allocated on the heap to begin, we then transfer to linear alloc when we re-create |
| 6986 | // the virtual methods array. |
| 6987 | // Need to use low 4GB arenas for compiler or else the pointers wont fit in 32 bit method array |
| 6988 | // during cross compilation. |
| 6989 | // Use the linear alloc pool since this one is in the low 4gb for the compiler. |
| 6990 | ArenaStack stack_; |
| 6991 | ScopedArenaAllocator allocator_; |
| 6992 | |
| 6993 | ScopedArenaVector<ArtMethod*> default_conflict_methods_; |
| 6994 | ScopedArenaVector<ArtMethod*> overriding_default_conflict_methods_; |
| 6995 | ScopedArenaVector<ArtMethod*> miranda_methods_; |
| 6996 | ScopedArenaVector<ArtMethod*> default_methods_; |
| 6997 | ScopedArenaVector<ArtMethod*> overriding_default_methods_; |
| 6998 | |
| 6999 | ScopedArenaUnorderedMap<ArtMethod*, ArtMethod*> move_table_; |
| 7000 | }; |
| 7001 | |
| 7002 | ArtMethod* ClassLinker::LinkInterfaceMethodsHelper::FindMethod( |
| 7003 | ArtMethod* interface_method, |
| 7004 | MethodNameAndSignatureComparator& interface_name_comparator, |
| 7005 | ArtMethod* vtable_impl) { |
| 7006 | ArtMethod* current_method = nullptr; |
| 7007 | switch (class_linker_->FindDefaultMethodImplementation(self_, |
| 7008 | interface_method, |
| 7009 | klass_, |
| 7010 | /*out*/¤t_method)) { |
| 7011 | case DefaultMethodSearchResult::kDefaultConflict: { |
| 7012 | // Default method conflict. |
| 7013 | DCHECK(current_method == nullptr); |
| 7014 | ArtMethod* default_conflict_method = nullptr; |
| 7015 | if (vtable_impl != nullptr && vtable_impl->IsDefaultConflicting()) { |
| 7016 | // We can reuse the method from the superclass, don't bother adding it to virtuals. |
| 7017 | default_conflict_method = vtable_impl; |
| 7018 | } else { |
| 7019 | // See if we already have a conflict method for this method. |
| 7020 | ArtMethod* preexisting_conflict = FindSameNameAndSignature( |
| 7021 | interface_name_comparator, |
| 7022 | default_conflict_methods_, |
| 7023 | overriding_default_conflict_methods_); |
| 7024 | if (LIKELY(preexisting_conflict != nullptr)) { |
| 7025 | // We already have another conflict we can reuse. |
| 7026 | default_conflict_method = preexisting_conflict; |
| 7027 | } else { |
| 7028 | // Note that we do this even if we are an interface since we need to create this and |
| 7029 | // cannot reuse another classes. |
| 7030 | // Create a new conflict method for this to use. |
| 7031 | default_conflict_method = reinterpret_cast<ArtMethod*>(allocator_.Alloc(method_size_)); |
| 7032 | new(default_conflict_method) ArtMethod(interface_method, |
| 7033 | class_linker_->GetImagePointerSize()); |
| 7034 | if (vtable_impl == nullptr) { |
| 7035 | // Save the conflict method. We need to add it to the vtable. |
| 7036 | default_conflict_methods_.push_back(default_conflict_method); |
| 7037 | } else { |
| 7038 | // Save the conflict method but it is already in the vtable. |
| 7039 | overriding_default_conflict_methods_.push_back(default_conflict_method); |
| 7040 | } |
| 7041 | } |
| 7042 | } |
| 7043 | current_method = default_conflict_method; |
| 7044 | break; |
| 7045 | } // case kDefaultConflict |
| 7046 | case DefaultMethodSearchResult::kDefaultFound: { |
| 7047 | DCHECK(current_method != nullptr); |
| 7048 | // Found a default method. |
| 7049 | if (vtable_impl != nullptr && |
| 7050 | current_method->GetDeclaringClass() == vtable_impl->GetDeclaringClass()) { |
| 7051 | // We found a default method but it was the same one we already have from our |
| 7052 | // superclass. Don't bother adding it to our vtable again. |
| 7053 | current_method = vtable_impl; |
| 7054 | } else if (LIKELY(FillTables())) { |
| 7055 | // Interfaces don't need to copy default methods since they don't have vtables. |
| 7056 | // Only record this default method if it is new to save space. |
| 7057 | // TODO It might be worthwhile to copy default methods on interfaces anyway since it |
| 7058 | // would make lookup for interface super much faster. (We would only need to scan |
| 7059 | // the iftable to find if there is a NSME or AME.) |
| 7060 | ArtMethod* old = FindSameNameAndSignature(interface_name_comparator, |
| 7061 | default_methods_, |
| 7062 | overriding_default_methods_); |
| 7063 | if (old == nullptr) { |
| 7064 | // We found a default method implementation and there were no conflicts. |
| 7065 | if (vtable_impl == nullptr) { |
| 7066 | // Save the default method. We need to add it to the vtable. |
| 7067 | default_methods_.push_back(current_method); |
| 7068 | } else { |
| 7069 | // Save the default method but it is already in the vtable. |
| 7070 | overriding_default_methods_.push_back(current_method); |
| 7071 | } |
| 7072 | } else { |
| 7073 | CHECK(old == current_method) << "Multiple default implementations selected!"; |
| 7074 | } |
| 7075 | } |
| 7076 | break; |
| 7077 | } // case kDefaultFound |
| 7078 | case DefaultMethodSearchResult::kAbstractFound: { |
| 7079 | DCHECK(current_method == nullptr); |
| 7080 | // Abstract method masks all defaults. |
| 7081 | if (vtable_impl != nullptr && |
| 7082 | vtable_impl->IsAbstract() && |
| 7083 | !vtable_impl->IsDefaultConflicting()) { |
| 7084 | // We need to make this an abstract method but the version in the vtable already is so |
| 7085 | // don't do anything. |
| 7086 | current_method = vtable_impl; |
| 7087 | } |
| 7088 | break; |
| 7089 | } // case kAbstractFound |
| 7090 | } |
| 7091 | return current_method; |
| 7092 | } |
| 7093 | |
| 7094 | ArtMethod* ClassLinker::LinkInterfaceMethodsHelper::GetOrCreateMirandaMethod( |
| 7095 | ArtMethod* interface_method, |
| 7096 | MethodNameAndSignatureComparator& interface_name_comparator) { |
| 7097 | // Find out if there is already a miranda method we can use. |
| 7098 | ArtMethod* miranda_method = FindSameNameAndSignature(interface_name_comparator, |
| 7099 | miranda_methods_); |
| 7100 | if (miranda_method == nullptr) { |
| 7101 | DCHECK(interface_method->IsAbstract()) << interface_method->PrettyMethod(); |
| 7102 | miranda_method = reinterpret_cast<ArtMethod*>(allocator_.Alloc(method_size_)); |
| 7103 | CHECK(miranda_method != nullptr); |
| 7104 | // Point the interface table at a phantom slot. |
| 7105 | new(miranda_method) ArtMethod(interface_method, class_linker_->GetImagePointerSize()); |
| 7106 | miranda_methods_.push_back(miranda_method); |
| 7107 | } |
| 7108 | return miranda_method; |
| 7109 | } |
| 7110 | |
| 7111 | void ClassLinker::LinkInterfaceMethodsHelper::ReallocMethods() { |
| 7112 | LogNewVirtuals(); |
| 7113 | |
| 7114 | const size_t old_method_count = klass_->NumMethods(); |
| 7115 | const size_t new_method_count = old_method_count + NumberOfNewVirtuals(); |
| 7116 | DCHECK_NE(old_method_count, new_method_count); |
| 7117 | |
| 7118 | // Attempt to realloc to save RAM if possible. |
| 7119 | LengthPrefixedArray<ArtMethod>* old_methods = klass_->GetMethodsPtr(); |
| 7120 | // The Realloced virtual methods aren't visible from the class roots, so there is no issue |
| 7121 | // where GCs could attempt to mark stale pointers due to memcpy. And since we overwrite the |
| 7122 | // realloced memory with out->CopyFrom, we are guaranteed to have objects in the to space since |
| 7123 | // CopyFrom has internal read barriers. |
| 7124 | // |
| 7125 | // TODO We should maybe move some of this into mirror::Class or at least into another method. |
| 7126 | const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_method_count, |
| 7127 | method_size_, |
| 7128 | method_alignment_); |
| 7129 | const size_t new_size = LengthPrefixedArray<ArtMethod>::ComputeSize(new_method_count, |
| 7130 | method_size_, |
| 7131 | method_alignment_); |
| 7132 | const size_t old_methods_ptr_size = (old_methods != nullptr) ? old_size : 0; |
| 7133 | auto* methods = reinterpret_cast<LengthPrefixedArray<ArtMethod>*>( |
| 7134 | Runtime::Current()->GetLinearAlloc()->Realloc( |
| 7135 | self_, old_methods, old_methods_ptr_size, new_size)); |
| 7136 | CHECK(methods != nullptr); // Native allocation failure aborts. |
| 7137 | |
| 7138 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 7139 | if (methods != old_methods) { |
| 7140 | // Maps from heap allocated miranda method to linear alloc miranda method. |
| 7141 | StrideIterator<ArtMethod> out = methods->begin(method_size_, method_alignment_); |
| 7142 | // Copy over the old methods. |
| 7143 | for (auto& m : klass_->GetMethods(pointer_size)) { |
| 7144 | move_table_.emplace(&m, &*out); |
| 7145 | // The CopyFrom is only necessary to not miss read barriers since Realloc won't do read |
| 7146 | // barriers when it copies. |
| 7147 | out->CopyFrom(&m, pointer_size); |
| 7148 | ++out; |
| 7149 | } |
| 7150 | } |
| 7151 | StrideIterator<ArtMethod> out(methods->begin(method_size_, method_alignment_) + old_method_count); |
| 7152 | // Copy over miranda methods before copying vtable since CopyOf may cause thread suspension and |
| 7153 | // we want the roots of the miranda methods to get visited. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7154 | for (size_t i = 0; i < miranda_methods_.size(); ++i) { |
| 7155 | ArtMethod* mir_method = miranda_methods_[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7156 | ArtMethod& new_method = *out; |
| 7157 | new_method.CopyFrom(mir_method, pointer_size); |
| 7158 | new_method.SetAccessFlags(new_method.GetAccessFlags() | kAccMiranda | kAccCopied); |
| 7159 | DCHECK_NE(new_method.GetAccessFlags() & kAccAbstract, 0u) |
| 7160 | << "Miranda method should be abstract!"; |
| 7161 | move_table_.emplace(mir_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7162 | // Update the entry in the method array, as the array will be used for future lookups, |
| 7163 | // where thread suspension is allowed. |
| 7164 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 7165 | // would not see them. |
| 7166 | miranda_methods_[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7167 | ++out; |
| 7168 | } |
| 7169 | // We need to copy the default methods into our own method table since the runtime requires that |
| 7170 | // every method on a class's vtable be in that respective class's virtual method table. |
| 7171 | // NOTE This means that two classes might have the same implementation of a method from the same |
| 7172 | // interface but will have different ArtMethod*s for them. This also means we cannot compare a |
| 7173 | // default method found on a class with one found on the declaring interface directly and must |
| 7174 | // look at the declaring class to determine if they are the same. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7175 | for (ScopedArenaVector<ArtMethod*>* methods_vec : {&default_methods_, |
| 7176 | &overriding_default_methods_}) { |
| 7177 | for (size_t i = 0; i < methods_vec->size(); ++i) { |
| 7178 | ArtMethod* def_method = (*methods_vec)[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7179 | ArtMethod& new_method = *out; |
| 7180 | new_method.CopyFrom(def_method, pointer_size); |
| 7181 | // Clear the kAccSkipAccessChecks flag if it is present. Since this class hasn't been |
| 7182 | // verified yet it shouldn't have methods that are skipping access checks. |
| 7183 | // TODO This is rather arbitrary. We should maybe support classes where only some of its |
| 7184 | // methods are skip_access_checks. |
| 7185 | constexpr uint32_t kSetFlags = kAccDefault | kAccCopied; |
| 7186 | constexpr uint32_t kMaskFlags = ~kAccSkipAccessChecks; |
| 7187 | new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags); |
| 7188 | move_table_.emplace(def_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7189 | // Update the entry in the method array, as the array will be used for future lookups, |
| 7190 | // where thread suspension is allowed. |
| 7191 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 7192 | // would not see them. |
| 7193 | (*methods_vec)[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7194 | ++out; |
| 7195 | } |
| 7196 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7197 | for (ScopedArenaVector<ArtMethod*>* methods_vec : {&default_conflict_methods_, |
| 7198 | &overriding_default_conflict_methods_}) { |
| 7199 | for (size_t i = 0; i < methods_vec->size(); ++i) { |
| 7200 | ArtMethod* conf_method = (*methods_vec)[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7201 | ArtMethod& new_method = *out; |
| 7202 | new_method.CopyFrom(conf_method, pointer_size); |
| 7203 | // This is a type of default method (there are default method impls, just a conflict) so |
| 7204 | // mark this as a default, non-abstract method, since thats what it is. Also clear the |
| 7205 | // kAccSkipAccessChecks bit since this class hasn't been verified yet it shouldn't have |
| 7206 | // methods that are skipping access checks. |
| 7207 | constexpr uint32_t kSetFlags = kAccDefault | kAccDefaultConflict | kAccCopied; |
| 7208 | constexpr uint32_t kMaskFlags = ~(kAccAbstract | kAccSkipAccessChecks); |
| 7209 | new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags); |
| 7210 | DCHECK(new_method.IsDefaultConflicting()); |
| 7211 | // The actual method might or might not be marked abstract since we just copied it from a |
| 7212 | // (possibly default) interface method. We need to set it entry point to be the bridge so |
| 7213 | // that the compiler will not invoke the implementation of whatever method we copied from. |
| 7214 | EnsureThrowsInvocationError(class_linker_, &new_method); |
| 7215 | move_table_.emplace(conf_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7216 | // Update the entry in the method array, as the array will be used for future lookups, |
| 7217 | // where thread suspension is allowed. |
| 7218 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 7219 | // would not see them. |
| 7220 | (*methods_vec)[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7221 | ++out; |
| 7222 | } |
| 7223 | } |
| 7224 | methods->SetSize(new_method_count); |
| 7225 | class_linker_->UpdateClassMethods(klass_.Get(), methods); |
| 7226 | } |
| 7227 | |
| 7228 | ObjPtr<mirror::PointerArray> ClassLinker::LinkInterfaceMethodsHelper::UpdateVtable( |
| 7229 | const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations, |
| 7230 | ObjPtr<mirror::PointerArray> old_vtable) { |
| 7231 | // Update the vtable to the new method structures. We can skip this for interfaces since they |
| 7232 | // do not have vtables. |
| 7233 | const size_t old_vtable_count = old_vtable->GetLength(); |
| 7234 | const size_t new_vtable_count = old_vtable_count + |
| 7235 | miranda_methods_.size() + |
| 7236 | default_methods_.size() + |
| 7237 | default_conflict_methods_.size(); |
| 7238 | |
| 7239 | ObjPtr<mirror::PointerArray> vtable = |
| 7240 | down_cast<mirror::PointerArray*>(old_vtable->CopyOf(self_, new_vtable_count)); |
| 7241 | if (UNLIKELY(vtable == nullptr)) { |
| 7242 | self_->AssertPendingOOMException(); |
| 7243 | return nullptr; |
| 7244 | } |
| 7245 | |
| 7246 | size_t vtable_pos = old_vtable_count; |
| 7247 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 7248 | // Update all the newly copied method's indexes so they denote their placement in the vtable. |
| 7249 | for (const ScopedArenaVector<ArtMethod*>& methods_vec : {default_methods_, |
| 7250 | default_conflict_methods_, |
| 7251 | miranda_methods_}) { |
| 7252 | // These are the functions that are not already in the vtable! |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7253 | for (ArtMethod* new_vtable_method : methods_vec) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7254 | // Leave the declaring class alone the method's dex_code_item_offset_ and dex_method_index_ |
| 7255 | // fields are references into the dex file the method was defined in. Since the ArtMethod |
| 7256 | // does not store that information it uses declaring_class_->dex_cache_. |
| 7257 | new_vtable_method->SetMethodIndex(0xFFFF & vtable_pos); |
| 7258 | vtable->SetElementPtrSize(vtable_pos, new_vtable_method, pointer_size); |
| 7259 | ++vtable_pos; |
| 7260 | } |
| 7261 | } |
| 7262 | DCHECK_EQ(vtable_pos, new_vtable_count); |
| 7263 | |
| 7264 | // Update old vtable methods. We use the default_translations map to figure out what each |
| 7265 | // vtable entry should be updated to, if they need to be at all. |
| 7266 | for (size_t i = 0; i < old_vtable_count; ++i) { |
| 7267 | ArtMethod* translated_method = vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 7268 | // Try and find what we need to change this method to. |
| 7269 | auto translation_it = default_translations.find(i); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7270 | if (translation_it != default_translations.end()) { |
| 7271 | if (translation_it->second.IsInConflict()) { |
| 7272 | // Find which conflict method we are to use for this method. |
| 7273 | MethodNameAndSignatureComparator old_method_comparator( |
| 7274 | translated_method->GetInterfaceMethodIfProxy(pointer_size)); |
| 7275 | // We only need to look through overriding_default_conflict_methods since this is an |
| 7276 | // overridden method we are fixing up here. |
| 7277 | ArtMethod* new_conflict_method = FindSameNameAndSignature( |
| 7278 | old_method_comparator, overriding_default_conflict_methods_); |
| 7279 | CHECK(new_conflict_method != nullptr) << "Expected a conflict method!"; |
| 7280 | translated_method = new_conflict_method; |
| 7281 | } else if (translation_it->second.IsAbstract()) { |
| 7282 | // Find which miranda method we are to use for this method. |
| 7283 | MethodNameAndSignatureComparator old_method_comparator( |
| 7284 | translated_method->GetInterfaceMethodIfProxy(pointer_size)); |
| 7285 | ArtMethod* miranda_method = FindSameNameAndSignature(old_method_comparator, |
| 7286 | miranda_methods_); |
| 7287 | DCHECK(miranda_method != nullptr); |
| 7288 | translated_method = miranda_method; |
| 7289 | } else { |
| 7290 | // Normal default method (changed from an older default or abstract interface method). |
| 7291 | DCHECK(translation_it->second.IsTranslation()); |
| 7292 | translated_method = translation_it->second.GetTranslation(); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7293 | auto it = move_table_.find(translated_method); |
| 7294 | DCHECK(it != move_table_.end()); |
| 7295 | translated_method = it->second; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7296 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7297 | } else { |
| 7298 | auto it = move_table_.find(translated_method); |
| 7299 | translated_method = (it != move_table_.end()) ? it->second : nullptr; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7300 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7301 | |
| 7302 | if (translated_method != nullptr) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7303 | // Make sure the new_methods index is set. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7304 | if (translated_method->GetMethodIndexDuringLinking() != i) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7305 | if (kIsDebugBuild) { |
| 7306 | auto* methods = klass_->GetMethodsPtr(); |
| 7307 | CHECK_LE(reinterpret_cast<uintptr_t>(&*methods->begin(method_size_, method_alignment_)), |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7308 | reinterpret_cast<uintptr_t>(translated_method)); |
| 7309 | CHECK_LT(reinterpret_cast<uintptr_t>(translated_method), |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7310 | reinterpret_cast<uintptr_t>(&*methods->end(method_size_, method_alignment_))); |
| 7311 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7312 | translated_method->SetMethodIndex(0xFFFF & i); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7313 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7314 | vtable->SetElementPtrSize(i, translated_method, pointer_size); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7315 | } |
| 7316 | } |
| 7317 | klass_->SetVTable(vtable.Ptr()); |
| 7318 | return vtable; |
| 7319 | } |
| 7320 | |
| 7321 | void ClassLinker::LinkInterfaceMethodsHelper::UpdateIfTable(Handle<mirror::IfTable> iftable) { |
| 7322 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 7323 | const size_t ifcount = klass_->GetIfTableCount(); |
| 7324 | // Go fix up all the stale iftable pointers. |
| 7325 | for (size_t i = 0; i < ifcount; ++i) { |
| 7326 | for (size_t j = 0, count = iftable->GetMethodArrayCount(i); j < count; ++j) { |
| 7327 | auto* method_array = iftable->GetMethodArray(i); |
| 7328 | auto* m = method_array->GetElementPtrSize<ArtMethod*>(j, pointer_size); |
| 7329 | DCHECK(m != nullptr) << klass_->PrettyClass(); |
| 7330 | auto it = move_table_.find(m); |
| 7331 | if (it != move_table_.end()) { |
| 7332 | auto* new_m = it->second; |
| 7333 | DCHECK(new_m != nullptr) << klass_->PrettyClass(); |
| 7334 | method_array->SetElementPtrSize(j, new_m, pointer_size); |
| 7335 | } |
| 7336 | } |
| 7337 | } |
| 7338 | } |
| 7339 | |
| 7340 | void ClassLinker::LinkInterfaceMethodsHelper::UpdateIMT(ArtMethod** out_imt) { |
| 7341 | // Fix up IMT next. |
| 7342 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 7343 | auto it = move_table_.find(out_imt[i]); |
| 7344 | if (it != move_table_.end()) { |
| 7345 | out_imt[i] = it->second; |
| 7346 | } |
| 7347 | } |
| 7348 | } |
| 7349 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7350 | // TODO This method needs to be split up into several smaller methods. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7351 | bool ClassLinker::LinkInterfaceMethods( |
| 7352 | Thread* self, |
| 7353 | Handle<mirror::Class> klass, |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7354 | const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7355 | bool* out_new_conflict, |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7356 | ArtMethod** out_imt) { |
| 7357 | StackHandleScope<3> hs(self); |
| 7358 | Runtime* const runtime = Runtime::Current(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7359 | |
| 7360 | const bool is_interface = klass->IsInterface(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7361 | const bool has_superclass = klass->HasSuperClass(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7362 | const bool fill_tables = !is_interface; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7363 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7364 | const size_t ifcount = klass->GetIfTableCount(); |
| 7365 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7366 | Handle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7367 | |
| 7368 | MutableHandle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
| 7369 | ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod(); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7370 | ArtMethod* const imt_conflict_method = runtime->GetImtConflictMethod(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7371 | // Copy the IMT from the super class if possible. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7372 | const bool extend_super_iftable = has_superclass; |
| 7373 | if (has_superclass && fill_tables) { |
| 7374 | FillImtFromSuperClass(klass, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7375 | unimplemented_method, |
| 7376 | imt_conflict_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7377 | out_new_conflict, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7378 | out_imt); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7379 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7380 | // Allocate method arrays before since we don't want miss visiting miranda method roots due to |
| 7381 | // thread suspension. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7382 | if (fill_tables) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7383 | if (!AllocateIfTableMethodArrays(self, klass, iftable)) { |
| 7384 | return false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7385 | } |
| 7386 | } |
| 7387 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7388 | LinkInterfaceMethodsHelper helper(this, klass, self, runtime); |
| 7389 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7390 | auto* old_cause = self->StartAssertNoThreadSuspension( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7391 | "Copying ArtMethods for LinkInterfaceMethods"); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7392 | // Going in reverse to ensure that we will hit abstract methods that override defaults before the |
| 7393 | // defaults. This means we don't need to do any trickery when creating the Miranda methods, since |
| 7394 | // they will already be null. This has the additional benefit that the declarer of a miranda |
| 7395 | // method will actually declare an abstract method. |
| 7396 | for (size_t i = ifcount; i != 0; ) { |
| 7397 | --i; |
| 7398 | |
| 7399 | DCHECK_GE(i, 0u); |
| 7400 | DCHECK_LT(i, ifcount); |
| 7401 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7402 | size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7403 | if (num_methods > 0) { |
| 7404 | StackHandleScope<2> hs2(self); |
| 7405 | const bool is_super = i < super_ifcount; |
| 7406 | const bool super_interface = is_super && extend_super_iftable; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7407 | // We don't actually create or fill these tables for interfaces, we just copy some methods for |
| 7408 | // conflict methods. Just set this as nullptr in those cases. |
| 7409 | Handle<mirror::PointerArray> method_array(fill_tables |
| 7410 | ? hs2.NewHandle(iftable->GetMethodArray(i)) |
| 7411 | : hs2.NewHandle<mirror::PointerArray>(nullptr)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7412 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7413 | ArraySlice<ArtMethod> input_virtual_methods; |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 7414 | ScopedNullHandle<mirror::PointerArray> null_handle; |
| 7415 | Handle<mirror::PointerArray> input_vtable_array(null_handle); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7416 | int32_t input_array_length = 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7417 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7418 | // TODO Cleanup Needed: In the presence of default methods this optimization is rather dirty |
| 7419 | // and confusing. Default methods should always look through all the superclasses |
| 7420 | // because they are the last choice of an implementation. We get around this by looking |
| 7421 | // at the super-classes iftable methods (copied into method_array previously) when we are |
| 7422 | // looking for the implementation of a super-interface method but that is rather dirty. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7423 | bool using_virtuals; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7424 | if (super_interface || is_interface) { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7425 | // If we are overwriting a super class interface, try to only virtual methods instead of the |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7426 | // whole vtable. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7427 | using_virtuals = true; |
| 7428 | input_virtual_methods = klass->GetDeclaredMethodsSlice(image_pointer_size_); |
| 7429 | input_array_length = input_virtual_methods.size(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7430 | } else { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7431 | // For a new interface, however, we need the whole vtable in case a new |
| 7432 | // interface method is implemented in the whole superclass. |
| 7433 | using_virtuals = false; |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7434 | DCHECK(vtable != nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7435 | input_vtable_array = vtable; |
| 7436 | input_array_length = input_vtable_array->GetLength(); |
| 7437 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7438 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7439 | // For each method in interface |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 7440 | for (size_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7441 | auto* interface_method = iftable->GetInterface(i)->GetVirtualMethod(j, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 7442 | MethodNameAndSignatureComparator interface_name_comparator( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7443 | interface_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 7444 | uint32_t imt_index = ImTable::GetImtIndex(interface_method); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7445 | ArtMethod** imt_ptr = &out_imt[imt_index]; |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7446 | // For each method listed in the interface's method list, find the |
| 7447 | // matching method in our class's method list. We want to favor the |
| 7448 | // subclass over the superclass, which just requires walking |
| 7449 | // back from the end of the vtable. (This only matters if the |
| 7450 | // superclass defines a private method and this class redefines |
| 7451 | // it -- otherwise it would use the same vtable slot. In .dex files |
| 7452 | // those don't end up in the virtual method table, so it shouldn't |
| 7453 | // matter which direction we go. We walk it backward anyway.) |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7454 | // |
| 7455 | // To find defaults we need to do the same but also go over interfaces. |
| 7456 | bool found_impl = false; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7457 | ArtMethod* vtable_impl = nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7458 | for (int32_t k = input_array_length - 1; k >= 0; --k) { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7459 | ArtMethod* vtable_method = using_virtuals ? |
| 7460 | &input_virtual_methods[k] : |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7461 | input_vtable_array->GetElementPtrSize<ArtMethod*>(k, image_pointer_size_); |
| 7462 | ArtMethod* vtable_method_for_name_comparison = |
| 7463 | vtable_method->GetInterfaceMethodIfProxy(image_pointer_size_); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 7464 | if (interface_name_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 7465 | vtable_method_for_name_comparison)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7466 | if (!vtable_method->IsAbstract() && !vtable_method->IsPublic()) { |
Mathieu Chartier | 4d122c1 | 2015-06-17 14:14:36 -0700 | [diff] [blame] | 7467 | // Must do EndAssertNoThreadSuspension before throw since the throw can cause |
| 7468 | // allocations. |
| 7469 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7470 | ThrowIllegalAccessError(klass.Get(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 7471 | "Method '%s' implementing interface method '%s' is not public", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7472 | vtable_method->PrettyMethod().c_str(), |
| 7473 | interface_method->PrettyMethod().c_str()); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7474 | return false; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7475 | } else if (UNLIKELY(vtable_method->IsOverridableByDefaultMethod())) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7476 | // We might have a newer, better, default method for this, so we just skip it. If we |
| 7477 | // are still using this we will select it again when scanning for default methods. To |
| 7478 | // obviate the need to copy the method again we will make a note that we already found |
| 7479 | // a default here. |
| 7480 | // TODO This should be much cleaner. |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7481 | vtable_impl = vtable_method; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7482 | break; |
| 7483 | } else { |
| 7484 | found_impl = true; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7485 | if (LIKELY(fill_tables)) { |
| 7486 | method_array->SetElementPtrSize(j, vtable_method, image_pointer_size_); |
| 7487 | // Place method in imt if entry is empty, place conflict otherwise. |
| 7488 | SetIMTRef(unimplemented_method, |
| 7489 | imt_conflict_method, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7490 | vtable_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7491 | /*out*/out_new_conflict, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7492 | /*out*/imt_ptr); |
| 7493 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7494 | break; |
| 7495 | } |
| 7496 | } |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7497 | } |
| 7498 | // Continue on to the next method if we are done. |
| 7499 | if (LIKELY(found_impl)) { |
| 7500 | continue; |
| 7501 | } else if (LIKELY(super_interface)) { |
| 7502 | // Don't look for a default implementation when the super-method is implemented directly |
| 7503 | // by the class. |
| 7504 | // |
| 7505 | // See if we can use the superclasses method and skip searching everything else. |
| 7506 | // Note: !found_impl && super_interface |
| 7507 | CHECK(extend_super_iftable); |
| 7508 | // If this is a super_interface method it is possible we shouldn't override it because a |
| 7509 | // superclass could have implemented it directly. We get the method the superclass used |
| 7510 | // to implement this to know if we can override it with a default method. Doing this is |
| 7511 | // safe since we know that the super_iftable is filled in so we can simply pull it from |
| 7512 | // there. We don't bother if this is not a super-classes interface since in that case we |
| 7513 | // have scanned the entire vtable anyway and would have found it. |
| 7514 | // TODO This is rather dirty but it is faster than searching through the entire vtable |
| 7515 | // every time. |
| 7516 | ArtMethod* supers_method = |
| 7517 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 7518 | DCHECK(supers_method != nullptr); |
| 7519 | DCHECK(interface_name_comparator.HasSameNameAndSignature(supers_method)); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7520 | if (LIKELY(!supers_method->IsOverridableByDefaultMethod())) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7521 | // The method is not overridable by a default method (i.e. it is directly implemented |
| 7522 | // in some class). Therefore move onto the next interface method. |
| 7523 | continue; |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 7524 | } else { |
| 7525 | // If the super-classes method is override-able by a default method we need to keep |
| 7526 | // track of it since though it is override-able it is not guaranteed to be 'overridden'. |
| 7527 | // If it turns out not to be overridden and we did not keep track of it we might add it |
Alex Light | 66630be | 2016-05-04 09:23:09 -0700 | [diff] [blame] | 7528 | // to the vtable twice, causing corruption (vtable entries having inconsistent and |
| 7529 | // illegal states, incorrect vtable size, and incorrect or inconsistent iftable entries) |
| 7530 | // in this class and any subclasses. |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 7531 | DCHECK(vtable_impl == nullptr || vtable_impl == supers_method) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7532 | << "vtable_impl was " << ArtMethod::PrettyMethod(vtable_impl) |
| 7533 | << " and not 'nullptr' or " |
| 7534 | << supers_method->PrettyMethod() |
| 7535 | << " as expected. IFTable appears to be corrupt!"; |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 7536 | vtable_impl = supers_method; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7537 | } |
| 7538 | } |
| 7539 | // If we haven't found it yet we should search through the interfaces for default methods. |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7540 | ArtMethod* current_method = helper.FindMethod(interface_method, |
| 7541 | interface_name_comparator, |
| 7542 | vtable_impl); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7543 | if (LIKELY(fill_tables)) { |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 7544 | if (current_method == nullptr && !super_interface) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7545 | // We could not find an implementation for this method and since it is a brand new |
| 7546 | // interface we searched the entire vtable (and all default methods) for an |
| 7547 | // implementation but couldn't find one. We therefore need to make a miranda method. |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7548 | current_method = helper.GetOrCreateMirandaMethod(interface_method, |
| 7549 | interface_name_comparator); |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 7550 | } |
| 7551 | |
| 7552 | if (current_method != nullptr) { |
| 7553 | // We found a default method implementation. Record it in the iftable and IMT. |
| 7554 | method_array->SetElementPtrSize(j, current_method, image_pointer_size_); |
| 7555 | SetIMTRef(unimplemented_method, |
| 7556 | imt_conflict_method, |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 7557 | current_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7558 | /*out*/out_new_conflict, |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 7559 | /*out*/imt_ptr); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7560 | } |
| 7561 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7562 | } // For each method in interface end. |
| 7563 | } // if (num_methods > 0) |
| 7564 | } // For each interface. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7565 | // TODO don't extend virtuals of interface unless necessary (when is it?). |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7566 | if (helper.HasNewVirtuals()) { |
| 7567 | LengthPrefixedArray<ArtMethod>* old_methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr; |
| 7568 | helper.ReallocMethods(); // No return value to check. Native allocation failure aborts. |
| 7569 | LengthPrefixedArray<ArtMethod>* methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr; |
| 7570 | |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 7571 | // Done copying methods, they are all roots in the class now, so we can end the no thread |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7572 | // suspension assert. |
| 7573 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 7574 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7575 | if (fill_tables) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7576 | vtable.Assign(helper.UpdateVtable(default_translations, vtable.Get())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7577 | if (UNLIKELY(vtable == nullptr)) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7578 | // The helper has already called self->AssertPendingOOMException(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7579 | return false; |
| 7580 | } |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7581 | helper.UpdateIfTable(iftable); |
| 7582 | helper.UpdateIMT(out_imt); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7583 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7584 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7585 | helper.CheckNoStaleMethodsInDexCache(); |
| 7586 | helper.ClobberOldMethods(old_methods, methods); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7587 | } else { |
| 7588 | self->EndAssertNoThreadSuspension(old_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7589 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7590 | if (kIsDebugBuild && !is_interface) { |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7591 | SanityCheckVTable(self, klass, image_pointer_size_); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 7592 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7593 | return true; |
| 7594 | } |
| 7595 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7596 | bool ClassLinker::LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7597 | CHECK(klass != nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7598 | return LinkFields(self, klass, false, nullptr); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 7599 | } |
| 7600 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7601 | bool ClassLinker::LinkStaticFields(Thread* self, Handle<mirror::Class> klass, size_t* class_size) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7602 | CHECK(klass != nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7603 | return LinkFields(self, klass, true, class_size); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 7604 | } |
| 7605 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7606 | struct LinkFieldsComparator { |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 7607 | explicit LinkFieldsComparator() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 7608 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 7609 | // No thread safety analysis as will be called from STL. Checked lock held in constructor. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7610 | bool operator()(ArtField* field1, ArtField* field2) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 7611 | NO_THREAD_SAFETY_ANALYSIS { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 7612 | // First come reference fields, then 64-bit, then 32-bit, and then 16-bit, then finally 8-bit. |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 7613 | Primitive::Type type1 = field1->GetTypeAsPrimitiveType(); |
| 7614 | Primitive::Type type2 = field2->GetTypeAsPrimitiveType(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 7615 | if (type1 != type2) { |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 7616 | if (type1 == Primitive::kPrimNot) { |
| 7617 | // Reference always goes first. |
| 7618 | return true; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 7619 | } |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 7620 | if (type2 == Primitive::kPrimNot) { |
| 7621 | // Reference always goes first. |
| 7622 | return false; |
| 7623 | } |
| 7624 | size_t size1 = Primitive::ComponentSize(type1); |
| 7625 | size_t size2 = Primitive::ComponentSize(type2); |
| 7626 | if (size1 != size2) { |
| 7627 | // Larger primitive types go first. |
| 7628 | return size1 > size2; |
| 7629 | } |
| 7630 | // Primitive types differ but sizes match. Arbitrarily order by primitive type. |
| 7631 | return type1 < type2; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7632 | } |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 7633 | // Same basic group? Then sort by dex field index. This is guaranteed to be sorted |
| 7634 | // by name and for equal names by type id index. |
| 7635 | // NOTE: This works also for proxies. Their static fields are assigned appropriate indexes. |
| 7636 | return field1->GetDexFieldIndex() < field2->GetDexFieldIndex(); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7637 | } |
| 7638 | }; |
| 7639 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7640 | bool ClassLinker::LinkFields(Thread* self, |
| 7641 | Handle<mirror::Class> klass, |
| 7642 | bool is_static, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7643 | size_t* class_size) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 7644 | self->AllowThreadSuspension(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7645 | const size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7646 | LengthPrefixedArray<ArtField>* const fields = is_static ? klass->GetSFieldsPtr() : |
| 7647 | klass->GetIFieldsPtr(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7648 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 7649 | // Initialize field_offset |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 7650 | MemberOffset field_offset(0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7651 | if (is_static) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7652 | field_offset = klass->GetFirstReferenceStaticFieldOffsetDuringLinking(image_pointer_size_); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7653 | } else { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7654 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7655 | if (super_class != nullptr) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 7656 | CHECK(super_class->IsResolved()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7657 | << klass->PrettyClass() << " " << super_class->PrettyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7658 | field_offset = MemberOffset(super_class->GetObjectSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7659 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7660 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7661 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7662 | CHECK_EQ(num_fields == 0, fields == nullptr) << klass->PrettyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7663 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7664 | // we want a relatively stable order so that adding new fields |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7665 | // minimizes disruption of C++ version such as Class and Method. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7666 | // |
| 7667 | // The overall sort order order is: |
| 7668 | // 1) All object reference fields, sorted alphabetically. |
| 7669 | // 2) All java long (64-bit) integer fields, sorted alphabetically. |
| 7670 | // 3) All java double (64-bit) floating point fields, sorted alphabetically. |
| 7671 | // 4) All java int (32-bit) integer fields, sorted alphabetically. |
| 7672 | // 5) All java float (32-bit) floating point fields, sorted alphabetically. |
| 7673 | // 6) All java char (16-bit) integer fields, sorted alphabetically. |
| 7674 | // 7) All java short (16-bit) integer fields, sorted alphabetically. |
| 7675 | // 8) All java boolean (8-bit) integer fields, sorted alphabetically. |
| 7676 | // 9) All java byte (8-bit) integer fields, sorted alphabetically. |
| 7677 | // |
| 7678 | // Once the fields are sorted in this order we will attempt to fill any gaps that might be present |
| 7679 | // in the memory layout of the structure. See ShuffleForward for how this is done. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7680 | std::deque<ArtField*> grouped_and_sorted_fields; |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 7681 | const char* old_no_suspend_cause = self->StartAssertNoThreadSuspension( |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 7682 | "Naked ArtField references in deque"); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7683 | for (size_t i = 0; i < num_fields; i++) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7684 | grouped_and_sorted_fields.push_back(&fields->At(i)); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7685 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 7686 | std::sort(grouped_and_sorted_fields.begin(), grouped_and_sorted_fields.end(), |
| 7687 | LinkFieldsComparator()); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7688 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7689 | // References should be at the front. |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7690 | size_t current_field = 0; |
| 7691 | size_t num_reference_fields = 0; |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7692 | FieldGaps gaps; |
| 7693 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7694 | for (; current_field < num_fields; current_field++) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7695 | ArtField* field = grouped_and_sorted_fields.front(); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 7696 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 7697 | bool isPrimitive = type != Primitive::kPrimNot; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7698 | if (isPrimitive) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 7699 | break; // past last reference, move on to the next phase |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7700 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7701 | if (UNLIKELY(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>( |
| 7702 | field_offset.Uint32Value()))) { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7703 | MemberOffset old_offset = field_offset; |
| 7704 | field_offset = MemberOffset(RoundUp(field_offset.Uint32Value(), 4)); |
| 7705 | AddFieldGap(old_offset.Uint32Value(), field_offset.Uint32Value(), &gaps); |
| 7706 | } |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 7707 | DCHECK_ALIGNED(field_offset.Uint32Value(), sizeof(mirror::HeapReference<mirror::Object>)); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7708 | grouped_and_sorted_fields.pop_front(); |
| 7709 | num_reference_fields++; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7710 | field->SetOffset(field_offset); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7711 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 7712 | sizeof(mirror::HeapReference<mirror::Object>)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7713 | } |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7714 | // Gaps are stored as a max heap which means that we must shuffle from largest to smallest |
| 7715 | // otherwise we could end up with suboptimal gap fills. |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7716 | ShuffleForward<8>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 7717 | ShuffleForward<4>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 7718 | ShuffleForward<2>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 7719 | ShuffleForward<1>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 7720 | CHECK(grouped_and_sorted_fields.empty()) << "Missed " << grouped_and_sorted_fields.size() << |
| 7721 | " fields."; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 7722 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7723 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7724 | // We lie to the GC about the java.lang.ref.Reference.referent field, so it doesn't scan it. |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 7725 | if (!is_static && klass->DescriptorEquals("Ljava/lang/ref/Reference;")) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7726 | // We know there are no non-reference fields in the Reference classes, and we know |
| 7727 | // that 'referent' is alphabetically last, so this is easy... |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7728 | CHECK_EQ(num_reference_fields, num_fields) << klass->PrettyClass(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7729 | CHECK_STREQ(fields->At(num_fields - 1).GetName(), "referent") |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7730 | << klass->PrettyClass(); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7731 | --num_reference_fields; |
| 7732 | } |
| 7733 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 7734 | size_t size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7735 | // Update klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7736 | if (is_static) { |
| 7737 | klass->SetNumReferenceStaticFields(num_reference_fields); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 7738 | *class_size = size; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7739 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7740 | klass->SetNumReferenceInstanceFields(num_reference_fields); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7741 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 7742 | if (num_reference_fields == 0 || super_class == nullptr) { |
| 7743 | // object has one reference field, klass, but we ignore it since we always visit the class. |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 7744 | // super_class is null iff the class is java.lang.Object. |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 7745 | if (super_class == nullptr || |
| 7746 | (super_class->GetClassFlags() & mirror::kClassFlagNoReferenceFields) != 0) { |
| 7747 | klass->SetClassFlags(klass->GetClassFlags() | mirror::kClassFlagNoReferenceFields); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 7748 | } |
| 7749 | } |
| 7750 | if (kIsDebugBuild) { |
| 7751 | DCHECK_EQ(super_class == nullptr, klass->DescriptorEquals("Ljava/lang/Object;")); |
| 7752 | size_t total_reference_instance_fields = 0; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7753 | ObjPtr<mirror::Class> cur_super = klass.Get(); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 7754 | while (cur_super != nullptr) { |
| 7755 | total_reference_instance_fields += cur_super->NumReferenceInstanceFieldsDuringLinking(); |
| 7756 | cur_super = cur_super->GetSuperClass(); |
| 7757 | } |
| 7758 | if (super_class == nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7759 | CHECK_EQ(total_reference_instance_fields, 1u) << klass->PrettyDescriptor(); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 7760 | } else { |
| 7761 | // Check that there is at least num_reference_fields other than Object.class. |
| 7762 | CHECK_GE(total_reference_instance_fields, 1u + num_reference_fields) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7763 | << klass->PrettyClass(); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 7764 | } |
| 7765 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7766 | if (!klass->IsVariableSize()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7767 | std::string temp; |
| 7768 | DCHECK_GE(size, sizeof(mirror::Object)) << klass->GetDescriptor(&temp); |
| 7769 | size_t previous_size = klass->GetObjectSize(); |
| 7770 | if (previous_size != 0) { |
| 7771 | // Make sure that we didn't originally have an incorrect size. |
| 7772 | CHECK_EQ(previous_size, size) << klass->GetDescriptor(&temp); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 7773 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7774 | klass->SetObjectSize(size); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7775 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7776 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7777 | |
| 7778 | if (kIsDebugBuild) { |
| 7779 | // Make sure that the fields array is ordered by name but all reference |
| 7780 | // offsets are at the beginning as far as alignment allows. |
| 7781 | MemberOffset start_ref_offset = is_static |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7782 | ? klass->GetFirstReferenceStaticFieldOffsetDuringLinking(image_pointer_size_) |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7783 | : klass->GetFirstReferenceInstanceFieldOffset(); |
| 7784 | MemberOffset end_ref_offset(start_ref_offset.Uint32Value() + |
| 7785 | num_reference_fields * |
| 7786 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 7787 | MemberOffset current_ref_offset = start_ref_offset; |
| 7788 | for (size_t i = 0; i < num_fields; i++) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7789 | ArtField* field = &fields->At(i); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7790 | VLOG(class_linker) << "LinkFields: " << (is_static ? "static" : "instance") |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7791 | << " class=" << klass->PrettyClass() << " field=" << field->PrettyField() |
| 7792 | << " offset=" << field->GetOffsetDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7793 | if (i != 0) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7794 | ArtField* const prev_field = &fields->At(i - 1); |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 7795 | // NOTE: The field names can be the same. This is not possible in the Java language |
| 7796 | // but it's valid Java/dex bytecode and for example proguard can generate such bytecode. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7797 | DCHECK_LE(strcmp(prev_field->GetName(), field->GetName()), 0); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7798 | } |
| 7799 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 7800 | bool is_primitive = type != Primitive::kPrimNot; |
| 7801 | if (klass->DescriptorEquals("Ljava/lang/ref/Reference;") && |
| 7802 | strcmp("referent", field->GetName()) == 0) { |
| 7803 | is_primitive = true; // We lied above, so we have to expect a lie here. |
| 7804 | } |
| 7805 | MemberOffset offset = field->GetOffsetDuringLinking(); |
| 7806 | if (is_primitive) { |
| 7807 | if (offset.Uint32Value() < end_ref_offset.Uint32Value()) { |
| 7808 | // Shuffled before references. |
| 7809 | size_t type_size = Primitive::ComponentSize(type); |
| 7810 | CHECK_LT(type_size, sizeof(mirror::HeapReference<mirror::Object>)); |
| 7811 | CHECK_LT(offset.Uint32Value(), start_ref_offset.Uint32Value()); |
| 7812 | CHECK_LE(offset.Uint32Value() + type_size, start_ref_offset.Uint32Value()); |
| 7813 | CHECK(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(offset.Uint32Value())); |
| 7814 | } |
| 7815 | } else { |
| 7816 | CHECK_EQ(current_ref_offset.Uint32Value(), offset.Uint32Value()); |
| 7817 | current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() + |
| 7818 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 7819 | } |
| 7820 | } |
| 7821 | CHECK_EQ(current_ref_offset.Uint32Value(), end_ref_offset.Uint32Value()); |
| 7822 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7823 | return true; |
| 7824 | } |
| 7825 | |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7826 | // Set the bitmap of reference instance field offsets. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7827 | void ClassLinker::CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7828 | uint32_t reference_offsets = 0; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7829 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7830 | // Leave the reference offsets as 0 for mirror::Object (the class field is handled specially). |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7831 | if (super_class != nullptr) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7832 | reference_offsets = super_class->GetReferenceInstanceOffsets(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7833 | // Compute reference offsets unless our superclass overflowed. |
| 7834 | if (reference_offsets != mirror::Class::kClassWalkSuper) { |
| 7835 | size_t num_reference_fields = klass->NumReferenceInstanceFieldsDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7836 | if (num_reference_fields != 0u) { |
| 7837 | // All of the fields that contain object references are guaranteed be grouped in memory |
| 7838 | // starting at an appropriately aligned address after super class object data. |
| 7839 | uint32_t start_offset = RoundUp(super_class->GetObjectSize(), |
| 7840 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 7841 | uint32_t start_bit = (start_offset - mirror::kObjectHeaderSize) / |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7842 | sizeof(mirror::HeapReference<mirror::Object>); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7843 | if (start_bit + num_reference_fields > 32) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7844 | reference_offsets = mirror::Class::kClassWalkSuper; |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7845 | } else { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7846 | reference_offsets |= (0xffffffffu << start_bit) & |
| 7847 | (0xffffffffu >> (32 - (start_bit + num_reference_fields))); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7848 | } |
| 7849 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 7850 | } |
| 7851 | } |
Mingyao Yang | faff0f0 | 2014-09-10 12:03:22 -0700 | [diff] [blame] | 7852 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7853 | } |
| 7854 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7855 | mirror::String* ClassLinker::ResolveString(const DexFile& dex_file, |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 7856 | dex::StringIndex string_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7857 | Handle<mirror::DexCache> dex_cache) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7858 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 7859 | Thread::PoisonObjectPointersIfDebug(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7860 | ObjPtr<mirror::String> resolved = dex_cache->GetResolvedString(string_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7861 | if (resolved != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7862 | return resolved.Ptr(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7863 | } |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 7864 | uint32_t utf16_length; |
| 7865 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7866 | ObjPtr<mirror::String> string = intern_table_->InternStrong(utf16_length, utf8_data); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 7867 | if (string != nullptr) { |
| 7868 | dex_cache->SetResolvedString(string_idx, string); |
| 7869 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7870 | return string.Ptr(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7871 | } |
| 7872 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7873 | mirror::String* ClassLinker::LookupString(const DexFile& dex_file, |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 7874 | dex::StringIndex string_idx, |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 7875 | ObjPtr<mirror::DexCache> dex_cache) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7876 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7877 | ObjPtr<mirror::String> resolved = dex_cache->GetResolvedString(string_idx); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7878 | if (resolved != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7879 | return resolved.Ptr(); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7880 | } |
| 7881 | uint32_t utf16_length; |
| 7882 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 7883 | ObjPtr<mirror::String> string = |
| 7884 | intern_table_->LookupStrong(Thread::Current(), utf16_length, utf8_data); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7885 | if (string != nullptr) { |
| 7886 | dex_cache->SetResolvedString(string_idx, string); |
| 7887 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7888 | return string.Ptr(); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7889 | } |
| 7890 | |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 7891 | ObjPtr<mirror::Class> ClassLinker::LookupResolvedType(const DexFile& dex_file, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 7892 | dex::TypeIndex type_idx, |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 7893 | ObjPtr<mirror::DexCache> dex_cache, |
| 7894 | ObjPtr<mirror::ClassLoader> class_loader) { |
| 7895 | ObjPtr<mirror::Class> type = dex_cache->GetResolvedType(type_idx); |
| 7896 | if (type == nullptr) { |
| 7897 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
| 7898 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
| 7899 | if (descriptor[1] == '\0') { |
| 7900 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 7901 | // for primitive classes that aren't backed by dex files. |
| 7902 | type = FindPrimitiveClass(descriptor[0]); |
| 7903 | } else { |
| 7904 | Thread* const self = Thread::Current(); |
| 7905 | DCHECK(self != nullptr); |
| 7906 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
| 7907 | // Find the class in the loaded classes table. |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 7908 | type = LookupClass(self, descriptor, hash, class_loader.Ptr()); |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 7909 | } |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 7910 | if (type != nullptr) { |
| 7911 | if (type->IsResolved()) { |
| 7912 | dex_cache->SetResolvedType(type_idx, type); |
| 7913 | } else { |
| 7914 | type = nullptr; |
| 7915 | } |
| 7916 | } |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 7917 | } |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 7918 | DCHECK(type == nullptr || type->IsResolved()); |
| 7919 | return type; |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 7920 | } |
| 7921 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7922 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 7923 | dex::TypeIndex type_idx, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7924 | ObjPtr<mirror::Class> referrer) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 7925 | StackHandleScope<2> hs(Thread::Current()); |
| 7926 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 7927 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 7928 | return ResolveType(dex_file, type_idx, dex_cache, class_loader); |
| 7929 | } |
| 7930 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7931 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 7932 | dex::TypeIndex type_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7933 | Handle<mirror::DexCache> dex_cache, |
| 7934 | Handle<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7935 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 7936 | Thread::PoisonObjectPointersIfDebug(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7937 | ObjPtr<mirror::Class> resolved = dex_cache->GetResolvedType(type_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7938 | if (resolved == nullptr) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 7939 | // TODO: Avoid this lookup as it duplicates work done in FindClass(). It is here |
| 7940 | // as a workaround for FastNative JNI to avoid AssertNoPendingException() when |
| 7941 | // trying to resolve annotations while an exception may be pending. Bug: 34659969 |
| 7942 | resolved = LookupResolvedType(dex_file, type_idx, dex_cache.Get(), class_loader.Get()); |
| 7943 | } |
| 7944 | if (resolved == nullptr) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 7945 | Thread* self = Thread::Current(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 7946 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 7947 | resolved = FindClass(self, descriptor, class_loader); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7948 | if (resolved != nullptr) { |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 7949 | // TODO: we used to throw here if resolved's class loader was not the |
| 7950 | // boot class loader. This was to permit different classes with the |
| 7951 | // same name to be loaded simultaneously by different loaders |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7952 | dex_cache->SetResolvedType(type_idx, resolved); |
| 7953 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 7954 | CHECK(self->IsExceptionPending()) |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 7955 | << "Expected pending exception for failed resolution of: " << descriptor; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 7956 | // Convert a ClassNotFoundException to a NoClassDefFoundError. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 7957 | StackHandleScope<1> hs(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 7958 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 7959 | if (cause->InstanceOf(GetClassRoot(kJavaLangClassNotFoundException))) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7960 | DCHECK(resolved == nullptr); // No Handle needed to preserve resolved. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 7961 | self->ClearException(); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 7962 | ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 7963 | self->GetException()->SetCause(cause.Get()); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 7964 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7965 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7966 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 7967 | DCHECK((resolved == nullptr) || resolved->IsResolved()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7968 | << resolved->PrettyDescriptor() << " " << resolved->GetStatus(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7969 | return resolved.Ptr(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7970 | } |
| 7971 | |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 7972 | template <ClassLinker::ResolveMode kResolveMode> |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7973 | ArtMethod* ClassLinker::ResolveMethod(const DexFile& dex_file, |
| 7974 | uint32_t method_idx, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7975 | Handle<mirror::DexCache> dex_cache, |
| 7976 | Handle<mirror::ClassLoader> class_loader, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7977 | ArtMethod* referrer, |
| 7978 | InvokeType type) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7979 | DCHECK(dex_cache != nullptr); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7980 | // Check for hit in the dex cache. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7981 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 7982 | Thread::PoisonObjectPointersIfDebug(); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 7983 | if (resolved != nullptr && !resolved->IsRuntimeMethod()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7984 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 7985 | if (kResolveMode == ClassLinker::kForceICCECheck) { |
| 7986 | if (resolved->CheckIncompatibleClassChange(type)) { |
| 7987 | ThrowIncompatibleClassChangeError(type, resolved->GetInvokeType(), resolved, referrer); |
| 7988 | return nullptr; |
| 7989 | } |
| 7990 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7991 | return resolved; |
| 7992 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7993 | // Fail, get the declaring class. |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7994 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7995 | ObjPtr<mirror::Class> klass = ResolveType(dex_file, method_id.class_idx_, dex_cache, class_loader); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7996 | if (klass == nullptr) { |
Elliott Hughes | cc5f9a9 | 2011-09-28 19:17:29 -0700 | [diff] [blame] | 7997 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7998 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7999 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8000 | // Scan using method_idx, this saves string compares but will only hit for matching dex |
| 8001 | // caches/files. |
| 8002 | switch (type) { |
| 8003 | case kDirect: // Fall-through. |
| 8004 | case kStatic: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8005 | resolved = klass->FindDirectMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 8006 | DCHECK(resolved == nullptr || resolved->GetDeclaringClassUnchecked() != nullptr); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8007 | break; |
| 8008 | case kInterface: |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 8009 | // We have to check whether the method id really belongs to an interface (dex static bytecode |
| 8010 | // constraint A15). Otherwise you must not invoke-interface on it. |
| 8011 | // |
| 8012 | // This is not symmetric to A12-A14 (direct, static, virtual), as using FindInterfaceMethod |
| 8013 | // assumes that the given type is an interface, and will check the interface table if the |
| 8014 | // method isn't declared in the class. So it may find an interface method (usually by name |
| 8015 | // in the handling below, but we do the constraint check early). In that case, |
| 8016 | // CheckIncompatibleClassChange will succeed (as it is called on an interface method) |
| 8017 | // unexpectedly. |
| 8018 | // Example: |
| 8019 | // interface I { |
| 8020 | // foo() |
| 8021 | // } |
| 8022 | // class A implements I { |
| 8023 | // ... |
| 8024 | // } |
| 8025 | // class B extends A { |
| 8026 | // ... |
| 8027 | // } |
| 8028 | // invoke-interface B.foo |
| 8029 | // -> FindInterfaceMethod finds I.foo (interface method), not A.foo (miranda method) |
| 8030 | if (UNLIKELY(!klass->IsInterface())) { |
| 8031 | ThrowIncompatibleClassChangeError(klass, |
| 8032 | "Found class %s, but interface was expected", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8033 | klass->PrettyDescriptor().c_str()); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 8034 | return nullptr; |
| 8035 | } else { |
| 8036 | resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 8037 | DCHECK(resolved == nullptr || resolved->GetDeclaringClass()->IsInterface()); |
| 8038 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8039 | break; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8040 | case kSuper: |
| 8041 | if (klass->IsInterface()) { |
| 8042 | resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 8043 | } else { |
| 8044 | resolved = klass->FindVirtualMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 8045 | } |
| 8046 | break; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8047 | case kVirtual: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8048 | resolved = klass->FindVirtualMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8049 | break; |
| 8050 | default: |
| 8051 | LOG(FATAL) << "Unreachable - invocation type: " << type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 8052 | UNREACHABLE(); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 8053 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8054 | if (resolved == nullptr) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8055 | // Search by name, which works across dex files. |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 8056 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 8057 | const Signature signature = dex_file.GetMethodSignature(method_id); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8058 | switch (type) { |
| 8059 | case kDirect: // Fall-through. |
| 8060 | case kStatic: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8061 | resolved = klass->FindDirectMethod(name, signature, image_pointer_size_); |
| 8062 | DCHECK(resolved == nullptr || resolved->GetDeclaringClassUnchecked() != nullptr); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8063 | break; |
| 8064 | case kInterface: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8065 | resolved = klass->FindInterfaceMethod(name, signature, image_pointer_size_); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8066 | DCHECK(resolved == nullptr || resolved->GetDeclaringClass()->IsInterface()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8067 | break; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8068 | case kSuper: |
| 8069 | if (klass->IsInterface()) { |
| 8070 | resolved = klass->FindInterfaceMethod(name, signature, image_pointer_size_); |
| 8071 | } else { |
| 8072 | resolved = klass->FindVirtualMethod(name, signature, image_pointer_size_); |
| 8073 | } |
| 8074 | break; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8075 | case kVirtual: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8076 | resolved = klass->FindVirtualMethod(name, signature, image_pointer_size_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8077 | break; |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 8078 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8079 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8080 | // If we found a method, check for incompatible class changes. |
| 8081 | if (LIKELY(resolved != nullptr && !resolved->CheckIncompatibleClassChange(type))) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8082 | // Be a good citizen and update the dex cache to speed subsequent calls. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8083 | dex_cache->SetResolvedMethod(method_idx, resolved, image_pointer_size_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8084 | return resolved; |
| 8085 | } else { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8086 | // If we had a method, it's an incompatible-class-change error. |
| 8087 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8088 | ThrowIncompatibleClassChangeError(type, resolved->GetInvokeType(), resolved, referrer); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8089 | } else { |
| 8090 | // We failed to find the method which means either an access error, an incompatible class |
| 8091 | // change, or no such method. First try to find the method among direct and virtual methods. |
| 8092 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
| 8093 | const Signature signature = dex_file.GetMethodSignature(method_id); |
| 8094 | switch (type) { |
| 8095 | case kDirect: |
| 8096 | case kStatic: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8097 | resolved = klass->FindVirtualMethod(name, signature, image_pointer_size_); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8098 | // Note: kDirect and kStatic are also mutually exclusive, but in that case we would |
| 8099 | // have had a resolved method before, which triggers the "true" branch above. |
| 8100 | break; |
| 8101 | case kInterface: |
| 8102 | case kVirtual: |
| 8103 | case kSuper: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8104 | resolved = klass->FindDirectMethod(name, signature, image_pointer_size_); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8105 | break; |
| 8106 | } |
| 8107 | |
| 8108 | // If we found something, check that it can be accessed by the referrer. |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8109 | bool exception_generated = false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8110 | if (resolved != nullptr && referrer != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8111 | ObjPtr<mirror::Class> methods_class = resolved->GetDeclaringClass(); |
| 8112 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8113 | if (!referring_class->CanAccess(methods_class)) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 8114 | ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, |
| 8115 | methods_class, |
| 8116 | resolved, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8117 | type); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8118 | exception_generated = true; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8119 | } else if (!referring_class->CanAccessMember(methods_class, resolved->GetAccessFlags())) { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8120 | ThrowIllegalAccessErrorMethod(referring_class, resolved); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8121 | exception_generated = true; |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8122 | } |
| 8123 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8124 | if (!exception_generated) { |
| 8125 | // Otherwise, throw an IncompatibleClassChangeError if we found something, and check |
| 8126 | // interface methods and throw if we find the method there. If we find nothing, throw a |
| 8127 | // NoSuchMethodError. |
| 8128 | switch (type) { |
| 8129 | case kDirect: |
| 8130 | case kStatic: |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8131 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8132 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8133 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8134 | resolved = klass->FindInterfaceMethod(name, signature, image_pointer_size_); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8135 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8136 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8137 | } else { |
| 8138 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 8139 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8140 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8141 | break; |
| 8142 | case kInterface: |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8143 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8144 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8145 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8146 | resolved = klass->FindVirtualMethod(name, signature, image_pointer_size_); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8147 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8148 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8149 | } else { |
| 8150 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 8151 | } |
| 8152 | } |
| 8153 | break; |
| 8154 | case kSuper: |
| 8155 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8156 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8157 | } else { |
| 8158 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 8159 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8160 | break; |
| 8161 | case kVirtual: |
| 8162 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8163 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8164 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8165 | resolved = klass->FindInterfaceMethod(name, signature, image_pointer_size_); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8166 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8167 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8168 | } else { |
| 8169 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 8170 | } |
| 8171 | } |
| 8172 | break; |
| 8173 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8174 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8175 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8176 | Thread::Current()->AssertPendingException(); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8177 | return nullptr; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8178 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8179 | } |
| 8180 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 8181 | ArtMethod* ClassLinker::ResolveMethodWithoutInvokeType(const DexFile& dex_file, |
| 8182 | uint32_t method_idx, |
| 8183 | Handle<mirror::DexCache> dex_cache, |
| 8184 | Handle<mirror::ClassLoader> class_loader) { |
| 8185 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 8186 | Thread::PoisonObjectPointersIfDebug(); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 8187 | if (resolved != nullptr && !resolved->IsRuntimeMethod()) { |
| 8188 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
| 8189 | return resolved; |
| 8190 | } |
| 8191 | // Fail, get the declaring class. |
| 8192 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8193 | ObjPtr<mirror::Class> klass = ResolveType(dex_file, method_id.class_idx_, dex_cache, class_loader); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 8194 | if (klass == nullptr) { |
| 8195 | Thread::Current()->AssertPendingException(); |
| 8196 | return nullptr; |
| 8197 | } |
| 8198 | if (klass->IsInterface()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8199 | LOG(FATAL) << "ResolveAmbiguousMethod: unexpected method in interface: " |
| 8200 | << klass->PrettyClass(); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 8201 | return nullptr; |
| 8202 | } |
| 8203 | |
| 8204 | // Search both direct and virtual methods |
| 8205 | resolved = klass->FindDirectMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 8206 | if (resolved == nullptr) { |
| 8207 | resolved = klass->FindVirtualMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 8208 | } |
| 8209 | |
| 8210 | return resolved; |
| 8211 | } |
| 8212 | |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 8213 | ArtField* ClassLinker::LookupResolvedField(uint32_t field_idx, |
| 8214 | ObjPtr<mirror::DexCache> dex_cache, |
| 8215 | ObjPtr<mirror::ClassLoader> class_loader, |
| 8216 | bool is_static) { |
| 8217 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 8218 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
| 8219 | ObjPtr<mirror::Class> klass = dex_cache->GetResolvedType(field_id.class_idx_); |
| 8220 | if (klass == nullptr) { |
| 8221 | klass = LookupResolvedType(dex_file, field_id.class_idx_, dex_cache, class_loader); |
| 8222 | } |
| 8223 | if (klass == nullptr) { |
| 8224 | // The class has not been resolved yet, so the field is also unresolved. |
| 8225 | return nullptr; |
| 8226 | } |
| 8227 | DCHECK(klass->IsResolved()); |
| 8228 | Thread* self = is_static ? Thread::Current() : nullptr; |
| 8229 | |
| 8230 | // First try to find a field declared directly by `klass` by the field index. |
| 8231 | ArtField* resolved_field = is_static |
| 8232 | ? mirror::Class::FindStaticField(self, klass, dex_cache, field_idx) |
| 8233 | : klass->FindInstanceField(dex_cache, field_idx); |
| 8234 | |
| 8235 | if (resolved_field == nullptr) { |
| 8236 | // If not found in `klass` by field index, search the class hierarchy using the name and type. |
| 8237 | const char* name = dex_file.GetFieldName(field_id); |
| 8238 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 8239 | resolved_field = is_static |
| 8240 | ? mirror::Class::FindStaticField(self, klass, name, type) |
| 8241 | : klass->FindInstanceField(name, type); |
| 8242 | } |
| 8243 | |
| 8244 | if (resolved_field != nullptr) { |
| 8245 | dex_cache->SetResolvedField(field_idx, resolved_field, image_pointer_size_); |
| 8246 | } |
| 8247 | return resolved_field; |
| 8248 | } |
| 8249 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 8250 | ArtField* ClassLinker::ResolveField(const DexFile& dex_file, |
| 8251 | uint32_t field_idx, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8252 | Handle<mirror::DexCache> dex_cache, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 8253 | Handle<mirror::ClassLoader> class_loader, |
| 8254 | bool is_static) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8255 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8256 | ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 8257 | Thread::PoisonObjectPointersIfDebug(); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 8258 | if (resolved != nullptr) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8259 | return resolved; |
| 8260 | } |
| 8261 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 8262 | Thread* const self = Thread::Current(); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8263 | ObjPtr<mirror::Class> klass = ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader); |
| 8264 | if (klass == nullptr) { |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 8265 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 8266 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8267 | } |
| 8268 | |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 8269 | if (is_static) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8270 | resolved = mirror::Class::FindStaticField(self, klass, dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 8271 | } else { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 8272 | resolved = klass->FindInstanceField(dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 8273 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 8274 | |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 8275 | if (resolved == nullptr) { |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 8276 | const char* name = dex_file.GetFieldName(field_id); |
| 8277 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 8278 | if (is_static) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 8279 | resolved = mirror::Class::FindStaticField(self, klass, name, type); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 8280 | } else { |
| 8281 | resolved = klass->FindInstanceField(name, type); |
| 8282 | } |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 8283 | if (resolved == nullptr) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8284 | ThrowNoSuchFieldError(is_static ? "static " : "instance ", klass, type, name); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8285 | return nullptr; |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 8286 | } |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 8287 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8288 | dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 8289 | return resolved; |
| 8290 | } |
| 8291 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 8292 | ArtField* ClassLinker::ResolveFieldJLS(const DexFile& dex_file, |
| 8293 | uint32_t field_idx, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8294 | Handle<mirror::DexCache> dex_cache, |
| 8295 | Handle<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8296 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8297 | ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 8298 | Thread::PoisonObjectPointersIfDebug(); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 8299 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 8300 | return resolved; |
| 8301 | } |
| 8302 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 8303 | Thread* self = Thread::Current(); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8304 | ObjPtr<mirror::Class> klass(ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader)); |
| 8305 | if (klass == nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 8306 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8307 | return nullptr; |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 8308 | } |
| 8309 | |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 8310 | StringPiece name(dex_file.GetFieldName(field_id)); |
| 8311 | StringPiece type(dex_file.GetFieldTypeDescriptor(field_id)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 8312 | resolved = mirror::Class::FindField(self, klass, name, type); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8313 | if (resolved != nullptr) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8314 | dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 8315 | } else { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8316 | ThrowNoSuchFieldError("", klass, type, name); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8317 | } |
| 8318 | return resolved; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 8319 | } |
| 8320 | |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8321 | mirror::MethodType* ClassLinker::ResolveMethodType(const DexFile& dex_file, |
| 8322 | uint32_t proto_idx, |
| 8323 | Handle<mirror::DexCache> dex_cache, |
| 8324 | Handle<mirror::ClassLoader> class_loader) { |
| 8325 | DCHECK(Runtime::Current()->IsMethodHandlesEnabled()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8326 | DCHECK(dex_cache != nullptr); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8327 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8328 | ObjPtr<mirror::MethodType> resolved = dex_cache->GetResolvedMethodType(proto_idx); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8329 | if (resolved != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8330 | return resolved.Ptr(); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8331 | } |
| 8332 | |
| 8333 | Thread* const self = Thread::Current(); |
| 8334 | StackHandleScope<4> hs(self); |
| 8335 | |
| 8336 | // First resolve the return type. |
| 8337 | const DexFile::ProtoId& proto_id = dex_file.GetProtoId(proto_idx); |
| 8338 | Handle<mirror::Class> return_type(hs.NewHandle( |
| 8339 | ResolveType(dex_file, proto_id.return_type_idx_, dex_cache, class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8340 | if (return_type == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8341 | DCHECK(self->IsExceptionPending()); |
| 8342 | return nullptr; |
| 8343 | } |
| 8344 | |
| 8345 | // Then resolve the argument types. |
| 8346 | // |
| 8347 | // TODO: Is there a better way to figure out the number of method arguments |
| 8348 | // other than by looking at the shorty ? |
| 8349 | const size_t num_method_args = strlen(dex_file.StringDataByIdx(proto_id.shorty_idx_)) - 1; |
| 8350 | |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 8351 | ObjPtr<mirror::Class> class_type = mirror::Class::GetJavaLangClass(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8352 | ObjPtr<mirror::Class> array_of_class = FindArrayClass(self, &class_type); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8353 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 8354 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_method_args))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8355 | if (method_params == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8356 | DCHECK(self->IsExceptionPending()); |
| 8357 | return nullptr; |
| 8358 | } |
| 8359 | |
| 8360 | DexFileParameterIterator it(dex_file, proto_id); |
| 8361 | int32_t i = 0; |
| 8362 | MutableHandle<mirror::Class> param_class = hs.NewHandle<mirror::Class>(nullptr); |
| 8363 | for (; it.HasNext(); it.Next()) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 8364 | const dex::TypeIndex type_idx = it.GetTypeIdx(); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8365 | param_class.Assign(ResolveType(dex_file, type_idx, dex_cache, class_loader)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8366 | if (param_class == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8367 | DCHECK(self->IsExceptionPending()); |
| 8368 | return nullptr; |
| 8369 | } |
| 8370 | |
| 8371 | method_params->Set(i++, param_class.Get()); |
| 8372 | } |
| 8373 | |
| 8374 | DCHECK(!it.HasNext()); |
| 8375 | |
| 8376 | Handle<mirror::MethodType> type = hs.NewHandle( |
| 8377 | mirror::MethodType::Create(self, return_type, method_params)); |
| 8378 | dex_cache->SetResolvedMethodType(proto_idx, type.Get()); |
| 8379 | |
| 8380 | return type.Get(); |
| 8381 | } |
| 8382 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8383 | mirror::MethodHandle* ClassLinker::ResolveMethodHandle(uint32_t method_handle_idx, |
| 8384 | ArtMethod* referrer) |
| 8385 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8386 | Thread* const self = Thread::Current(); |
| 8387 | const DexFile* const dex_file = referrer->GetDexFile(); |
| 8388 | const DexFile::MethodHandleItem& mh = dex_file->GetMethodHandle(method_handle_idx); |
| 8389 | |
| 8390 | union { |
| 8391 | ArtField* field; |
| 8392 | ArtMethod* method; |
| 8393 | uintptr_t field_or_method; |
| 8394 | } target; |
| 8395 | uint32_t num_params; |
| 8396 | mirror::MethodHandle::Kind kind; |
| 8397 | DexFile::MethodHandleType handle_type = |
| 8398 | static_cast<DexFile::MethodHandleType>(mh.method_handle_type_); |
| 8399 | switch (handle_type) { |
| 8400 | case DexFile::MethodHandleType::kStaticPut: { |
| 8401 | kind = mirror::MethodHandle::Kind::kStaticPut; |
| 8402 | target.field = ResolveField(mh.field_or_method_idx_, referrer, true /* is_static */); |
| 8403 | num_params = 1; |
| 8404 | break; |
| 8405 | } |
| 8406 | case DexFile::MethodHandleType::kStaticGet: { |
| 8407 | kind = mirror::MethodHandle::Kind::kStaticGet; |
| 8408 | target.field = ResolveField(mh.field_or_method_idx_, referrer, true /* is_static */); |
| 8409 | num_params = 0; |
| 8410 | break; |
| 8411 | } |
| 8412 | case DexFile::MethodHandleType::kInstancePut: { |
| 8413 | kind = mirror::MethodHandle::Kind::kInstancePut; |
| 8414 | target.field = ResolveField(mh.field_or_method_idx_, referrer, false /* is_static */); |
| 8415 | num_params = 2; |
| 8416 | break; |
| 8417 | } |
| 8418 | case DexFile::MethodHandleType::kInstanceGet: { |
| 8419 | kind = mirror::MethodHandle::Kind::kInstanceGet; |
| 8420 | target.field = ResolveField(mh.field_or_method_idx_, referrer, false /* is_static */); |
| 8421 | num_params = 1; |
| 8422 | break; |
| 8423 | } |
| 8424 | case DexFile::MethodHandleType::kInvokeStatic: { |
| 8425 | kind = mirror::MethodHandle::Kind::kInvokeStatic; |
| 8426 | target.method = ResolveMethod<kNoICCECheckForCache>(self, |
| 8427 | mh.field_or_method_idx_, |
| 8428 | referrer, |
| 8429 | InvokeType::kStatic); |
| 8430 | uint32_t shorty_length; |
| 8431 | target.method->GetShorty(&shorty_length); |
| 8432 | num_params = shorty_length - 1; // Remove 1 for return value. |
| 8433 | break; |
| 8434 | } |
| 8435 | case DexFile::MethodHandleType::kInvokeInstance: { |
| 8436 | kind = mirror::MethodHandle::Kind::kInvokeVirtual; |
| 8437 | target.method = ResolveMethod<kNoICCECheckForCache>(self, |
| 8438 | mh.field_or_method_idx_, |
| 8439 | referrer, |
| 8440 | InvokeType::kVirtual); |
| 8441 | uint32_t shorty_length; |
| 8442 | target.method->GetShorty(&shorty_length); |
| 8443 | num_params = shorty_length - 1; // Remove 1 for return value. |
| 8444 | break; |
| 8445 | } |
| 8446 | case DexFile::MethodHandleType::kInvokeConstructor: { |
| 8447 | UNIMPLEMENTED(FATAL) << "Invoke constructor is implemented as a transform."; |
| 8448 | num_params = 0; |
| 8449 | } |
| 8450 | } |
| 8451 | |
| 8452 | StackHandleScope<5> hs(self); |
| 8453 | ObjPtr<mirror::Class> class_type = mirror::Class::GetJavaLangClass(); |
| 8454 | ObjPtr<mirror::Class> array_of_class = FindArrayClass(self, &class_type); |
| 8455 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 8456 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params))); |
| 8457 | if (method_params.Get() == nullptr) { |
| 8458 | DCHECK(self->IsExceptionPending()); |
| 8459 | return nullptr; |
| 8460 | } |
| 8461 | |
| 8462 | Handle<mirror::Class> return_type; |
| 8463 | switch (handle_type) { |
| 8464 | case DexFile::MethodHandleType::kStaticPut: { |
| 8465 | method_params->Set(0, target.field->GetType<true>()); |
| 8466 | return_type = hs.NewHandle(FindPrimitiveClass('V')); |
| 8467 | break; |
| 8468 | } |
| 8469 | case DexFile::MethodHandleType::kStaticGet: { |
| 8470 | return_type = hs.NewHandle(target.field->GetType<true>()); |
| 8471 | break; |
| 8472 | } |
| 8473 | case DexFile::MethodHandleType::kInstancePut: { |
| 8474 | method_params->Set(0, target.field->GetDeclaringClass()); |
| 8475 | method_params->Set(1, target.field->GetType<true>()); |
| 8476 | return_type = hs.NewHandle(FindPrimitiveClass('V')); |
| 8477 | break; |
| 8478 | } |
| 8479 | case DexFile::MethodHandleType::kInstanceGet: { |
| 8480 | method_params->Set(0, target.field->GetDeclaringClass()); |
| 8481 | return_type = hs.NewHandle(target.field->GetType<true>()); |
| 8482 | break; |
| 8483 | } |
| 8484 | case DexFile::MethodHandleType::kInvokeStatic: |
| 8485 | case DexFile::MethodHandleType::kInvokeInstance: { |
| 8486 | // TODO(oth): This will not work for varargs methods as this |
| 8487 | // requires instantiating a Transformer. This resolution step |
| 8488 | // would be best done in managed code rather than in the run |
| 8489 | // time (b/35235705) |
| 8490 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 8491 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
| 8492 | DexFileParameterIterator it(*dex_file, target.method->GetPrototype()); |
| 8493 | for (int32_t i = 0; it.HasNext(); i++, it.Next()) { |
| 8494 | const dex::TypeIndex type_idx = it.GetTypeIdx(); |
| 8495 | mirror::Class* klass = ResolveType(*dex_file, type_idx, dex_cache, class_loader); |
| 8496 | if (nullptr == klass) { |
| 8497 | DCHECK(self->IsExceptionPending()); |
| 8498 | return nullptr; |
| 8499 | } |
| 8500 | method_params->Set(i, klass); |
| 8501 | } |
| 8502 | return_type = hs.NewHandle(target.method->GetReturnType(true)); |
| 8503 | break; |
| 8504 | } |
| 8505 | case DexFile::MethodHandleType::kInvokeConstructor: { |
| 8506 | // TODO(oth): b/35235705 |
| 8507 | UNIMPLEMENTED(FATAL) << "Invoke constructor is implemented as a transform."; |
| 8508 | } |
| 8509 | } |
| 8510 | |
| 8511 | if (return_type.IsNull()) { |
| 8512 | DCHECK(self->IsExceptionPending()); |
| 8513 | return nullptr; |
| 8514 | } |
| 8515 | |
| 8516 | Handle<mirror::MethodType> |
| 8517 | mt(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params))); |
| 8518 | if (mt.IsNull()) { |
| 8519 | DCHECK(self->IsExceptionPending()); |
| 8520 | return nullptr; |
| 8521 | } |
| 8522 | return mirror::MethodHandleImpl::Create(self, target.field_or_method, kind, mt); |
| 8523 | } |
| 8524 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8525 | bool ClassLinker::IsQuickResolutionStub(const void* entry_point) const { |
| 8526 | return (entry_point == GetQuickResolutionStub()) || |
| 8527 | (quick_resolution_trampoline_ == entry_point); |
| 8528 | } |
| 8529 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8530 | bool ClassLinker::IsQuickToInterpreterBridge(const void* entry_point) const { |
| 8531 | return (entry_point == GetQuickToInterpreterBridge()) || |
| 8532 | (quick_to_interpreter_bridge_trampoline_ == entry_point); |
| 8533 | } |
| 8534 | |
| 8535 | bool ClassLinker::IsQuickGenericJniStub(const void* entry_point) const { |
| 8536 | return (entry_point == GetQuickGenericJniStub()) || |
| 8537 | (quick_generic_jni_trampoline_ == entry_point); |
| 8538 | } |
| 8539 | |
| 8540 | const void* ClassLinker::GetRuntimeQuickGenericJniStub() const { |
| 8541 | return GetQuickGenericJniStub(); |
| 8542 | } |
| 8543 | |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 8544 | void ClassLinker::SetEntryPointsToCompiledCode(ArtMethod* method, const void* code) const { |
| 8545 | CHECK(code != nullptr); |
| 8546 | const uint8_t* base = reinterpret_cast<const uint8_t*>(code); // Base of data points at code. |
| 8547 | base -= sizeof(void*); // Move backward so that code_offset != 0. |
| 8548 | const uint32_t code_offset = sizeof(void*); |
| 8549 | OatFile::OatMethod oat_method(base, code_offset); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8550 | oat_method.LinkMethod(method); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8551 | } |
| 8552 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8553 | void ClassLinker::SetEntryPointsToInterpreter(ArtMethod* method) const { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8554 | if (!method->IsNative()) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8555 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 8556 | } else { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 8557 | SetEntryPointsToCompiledCode(method, GetQuickGenericJniStub()); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8558 | } |
| 8559 | } |
| 8560 | |
Alex Light | db01a09 | 2017-04-03 15:39:55 -0700 | [diff] [blame] | 8561 | void ClassLinker::SetEntryPointsForObsoleteMethod(ArtMethod* method) const { |
| 8562 | DCHECK(method->IsObsolete()); |
| 8563 | // We cannot mess with the entrypoints of native methods because they are used to determine how |
| 8564 | // large the method's quick stack frame is. Without this information we cannot walk the stacks. |
| 8565 | if (!method->IsNative()) { |
| 8566 | method->SetEntryPointFromQuickCompiledCode(GetInvokeObsoleteMethodStub()); |
| 8567 | } |
| 8568 | } |
| 8569 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 8570 | void ClassLinker::DumpForSigQuit(std::ostream& os) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 8571 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 8572 | ReaderMutexLock mu(soa.Self(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 8573 | os << "Zygote loaded classes=" << NumZygoteClasses() << " post zygote classes=" |
| 8574 | << NumNonZygoteClasses() << "\n"; |
| 8575 | } |
| 8576 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8577 | class CountClassesVisitor : public ClassLoaderVisitor { |
| 8578 | public: |
| 8579 | CountClassesVisitor() : num_zygote_classes(0), num_non_zygote_classes(0) {} |
| 8580 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8581 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 8582 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) OVERRIDE { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8583 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 8584 | if (class_table != nullptr) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 8585 | num_zygote_classes += class_table->NumZygoteClasses(class_loader); |
| 8586 | num_non_zygote_classes += class_table->NumNonZygoteClasses(class_loader); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 8587 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 8588 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8589 | |
| 8590 | size_t num_zygote_classes; |
| 8591 | size_t num_non_zygote_classes; |
| 8592 | }; |
| 8593 | |
| 8594 | size_t ClassLinker::NumZygoteClasses() const { |
| 8595 | CountClassesVisitor visitor; |
| 8596 | VisitClassLoaders(&visitor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame^] | 8597 | return visitor.num_zygote_classes + boot_class_table_->NumZygoteClasses(nullptr); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 8598 | } |
| 8599 | |
| 8600 | size_t ClassLinker::NumNonZygoteClasses() const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8601 | CountClassesVisitor visitor; |
| 8602 | VisitClassLoaders(&visitor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame^] | 8603 | return visitor.num_non_zygote_classes + boot_class_table_->NumNonZygoteClasses(nullptr); |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 8604 | } |
| 8605 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 8606 | size_t ClassLinker::NumLoadedClasses() { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 8607 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 8608 | // Only return non zygote classes since these are the ones which apps which care about. |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 8609 | return NumNonZygoteClasses(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 8610 | } |
| 8611 | |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 8612 | pid_t ClassLinker::GetClassesLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 8613 | return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 8614 | } |
| 8615 | |
| 8616 | pid_t ClassLinker::GetDexLockOwner() { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 8617 | return Locks::dex_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 8618 | } |
| 8619 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8620 | void ClassLinker::SetClassRoot(ClassRoot class_root, ObjPtr<mirror::Class> klass) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 8621 | DCHECK(!init_done_); |
| 8622 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8623 | DCHECK(klass != nullptr); |
| 8624 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 8625 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 8626 | mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8627 | DCHECK(class_roots != nullptr); |
| 8628 | DCHECK(class_roots->Get(class_root) == nullptr); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 8629 | class_roots->Set<false>(class_root, klass); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 8630 | } |
| 8631 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8632 | const char* ClassLinker::GetClassRootDescriptor(ClassRoot class_root) { |
| 8633 | static const char* class_roots_descriptors[] = { |
| 8634 | "Ljava/lang/Class;", |
| 8635 | "Ljava/lang/Object;", |
| 8636 | "[Ljava/lang/Class;", |
| 8637 | "[Ljava/lang/Object;", |
| 8638 | "Ljava/lang/String;", |
| 8639 | "Ljava/lang/DexCache;", |
| 8640 | "Ljava/lang/ref/Reference;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 8641 | "Ljava/lang/reflect/Constructor;", |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 8642 | "Ljava/lang/reflect/Field;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 8643 | "Ljava/lang/reflect/Method;", |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8644 | "Ljava/lang/reflect/Proxy;", |
| 8645 | "[Ljava/lang/String;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 8646 | "[Ljava/lang/reflect/Constructor;", |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 8647 | "[Ljava/lang/reflect/Field;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 8648 | "[Ljava/lang/reflect/Method;", |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8649 | "Ljava/lang/invoke/CallSite;", |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8650 | "Ljava/lang/invoke/MethodHandleImpl;", |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8651 | "Ljava/lang/invoke/MethodHandles$Lookup;", |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8652 | "Ljava/lang/invoke/MethodType;", |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8653 | "Ljava/lang/ClassLoader;", |
| 8654 | "Ljava/lang/Throwable;", |
| 8655 | "Ljava/lang/ClassNotFoundException;", |
| 8656 | "Ljava/lang/StackTraceElement;", |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 8657 | "Ldalvik/system/EmulatedStackFrame;", |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8658 | "Z", |
| 8659 | "B", |
| 8660 | "C", |
| 8661 | "D", |
| 8662 | "F", |
| 8663 | "I", |
| 8664 | "J", |
| 8665 | "S", |
| 8666 | "V", |
| 8667 | "[Z", |
| 8668 | "[B", |
| 8669 | "[C", |
| 8670 | "[D", |
| 8671 | "[F", |
| 8672 | "[I", |
| 8673 | "[J", |
| 8674 | "[S", |
| 8675 | "[Ljava/lang/StackTraceElement;", |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 8676 | "Ldalvik/system/ClassExt;", |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8677 | }; |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 8678 | static_assert(arraysize(class_roots_descriptors) == size_t(kClassRootsMax), |
| 8679 | "Mismatch between class descriptors and class-root enum"); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8680 | |
| 8681 | const char* descriptor = class_roots_descriptors[class_root]; |
| 8682 | CHECK(descriptor != nullptr); |
| 8683 | return descriptor; |
| 8684 | } |
| 8685 | |
Jeff Hao | 5872d7c | 2016-04-27 11:07:41 -0700 | [diff] [blame] | 8686 | jobject ClassLinker::CreatePathClassLoader(Thread* self, |
| 8687 | const std::vector<const DexFile*>& dex_files) { |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8688 | // SOAAlreadyRunnable is protected, and we need something to add a global reference. |
| 8689 | // We could move the jobject to the callers, but all call-sites do this... |
| 8690 | ScopedObjectAccessUnchecked soa(self); |
| 8691 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8692 | // For now, create a libcore-level DexFile for each ART DexFile. This "explodes" multidex. |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8693 | StackHandleScope<6> hs(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8694 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8695 | ArtField* dex_elements_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8696 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8697 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 8698 | Handle<mirror::Class> dex_elements_class(hs.NewHandle(dex_elements_field->GetType<true>())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8699 | DCHECK(dex_elements_class != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8700 | DCHECK(dex_elements_class->IsArrayClass()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 8701 | Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements(hs.NewHandle( |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 8702 | mirror::ObjectArray<mirror::Object>::Alloc(self, |
| 8703 | dex_elements_class.Get(), |
| 8704 | dex_files.size()))); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8705 | Handle<mirror::Class> h_dex_element_class = |
| 8706 | hs.NewHandle(dex_elements_class->GetComponentType()); |
| 8707 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8708 | ArtField* element_file_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8709 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8710 | DCHECK_EQ(h_dex_element_class.Get(), element_file_field->GetDeclaringClass()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8711 | |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8712 | ArtField* cookie_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8713 | DCHECK_EQ(cookie_field->GetDeclaringClass(), element_file_field->GetType<false>()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8714 | |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8715 | ArtField* file_name_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_fileName); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 8716 | DCHECK_EQ(file_name_field->GetDeclaringClass(), element_file_field->GetType<false>()); |
| 8717 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8718 | // Fill the elements array. |
| 8719 | int32_t index = 0; |
| 8720 | for (const DexFile* dex_file : dex_files) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 8721 | StackHandleScope<4> hs2(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8722 | |
Mathieu Chartier | 966878d | 2016-01-14 14:33:29 -0800 | [diff] [blame] | 8723 | // CreatePathClassLoader is only used by gtests. Index 0 of h_long_array is supposed to be the |
| 8724 | // oat file but we can leave it null. |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 8725 | Handle<mirror::LongArray> h_long_array = hs2.NewHandle(mirror::LongArray::Alloc( |
| 8726 | self, |
| 8727 | kDexFileIndexStart + 1)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8728 | DCHECK(h_long_array != nullptr); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 8729 | h_long_array->Set(kDexFileIndexStart, reinterpret_cast<intptr_t>(dex_file)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8730 | |
Mathieu Chartier | 3738e98 | 2017-05-12 16:07:28 -0700 | [diff] [blame] | 8731 | // Note that this creates a finalizable dalvik.system.DexFile object and a corresponding |
| 8732 | // FinalizerReference which will never get cleaned up without a started runtime. |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8733 | Handle<mirror::Object> h_dex_file = hs2.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8734 | cookie_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8735 | DCHECK(h_dex_file != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8736 | cookie_field->SetObject<false>(h_dex_file.Get(), h_long_array.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8737 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 8738 | Handle<mirror::String> h_file_name = hs2.NewHandle( |
| 8739 | mirror::String::AllocFromModifiedUtf8(self, dex_file->GetLocation().c_str())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8740 | DCHECK(h_file_name != nullptr); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 8741 | file_name_field->SetObject<false>(h_dex_file.Get(), h_file_name.Get()); |
| 8742 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8743 | Handle<mirror::Object> h_element = hs2.NewHandle(h_dex_element_class->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8744 | DCHECK(h_element != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8745 | element_file_field->SetObject<false>(h_element.Get(), h_dex_file.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8746 | |
| 8747 | h_dex_elements->Set(index, h_element.Get()); |
| 8748 | index++; |
| 8749 | } |
| 8750 | DCHECK_EQ(index, h_dex_elements->GetLength()); |
| 8751 | |
| 8752 | // Create DexPathList. |
| 8753 | Handle<mirror::Object> h_dex_path_list = hs.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8754 | dex_elements_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8755 | DCHECK(h_dex_path_list != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8756 | // Set elements. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8757 | dex_elements_field->SetObject<false>(h_dex_path_list.Get(), h_dex_elements.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8758 | |
| 8759 | // Create PathClassLoader. |
| 8760 | Handle<mirror::Class> h_path_class_class = hs.NewHandle( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 8761 | soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8762 | Handle<mirror::Object> h_path_class_loader = hs.NewHandle( |
| 8763 | h_path_class_class->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8764 | DCHECK(h_path_class_loader != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8765 | // Set DexPathList. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8766 | ArtField* path_list_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8767 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8768 | DCHECK(path_list_field != nullptr); |
| 8769 | path_list_field->SetObject<false>(h_path_class_loader.Get(), h_dex_path_list.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8770 | |
| 8771 | // Make a pretend boot-classpath. |
| 8772 | // TODO: Should we scan the image? |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8773 | ArtField* const parent_field = |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8774 | mirror::Class::FindField(self, |
| 8775 | h_path_class_loader->GetClass(), |
| 8776 | "parent", |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8777 | "Ljava/lang/ClassLoader;"); |
Roland Levillain | f39c9eb | 2015-05-26 15:02:07 +0100 | [diff] [blame] | 8778 | DCHECK(parent_field != nullptr); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 8779 | ObjPtr<mirror::Object> boot_cl = |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 8780 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader)->AllocObject(self); |
Mathieu Chartier | 966878d | 2016-01-14 14:33:29 -0800 | [diff] [blame] | 8781 | parent_field->SetObject<false>(h_path_class_loader.Get(), boot_cl); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8782 | |
| 8783 | // Make it a global ref and return. |
| 8784 | ScopedLocalRef<jobject> local_ref( |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 8785 | soa.Env(), soa.Env()->AddLocalReference<jobject>(h_path_class_loader.Get())); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8786 | return soa.Env()->NewGlobalRef(local_ref.get()); |
| 8787 | } |
| 8788 | |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 8789 | void ClassLinker::DropFindArrayClassCache() { |
| 8790 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
| 8791 | find_array_class_cache_next_victim_ = 0; |
| 8792 | } |
| 8793 | |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 8794 | void ClassLinker::ClearClassTableStrongRoots() const { |
| 8795 | Thread* const self = Thread::Current(); |
| 8796 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 8797 | for (const ClassLoaderData& data : class_loaders_) { |
| 8798 | if (data.class_table != nullptr) { |
| 8799 | data.class_table->ClearStrongRoots(); |
| 8800 | } |
| 8801 | } |
| 8802 | } |
| 8803 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 8804 | void ClassLinker::VisitClassLoaders(ClassLoaderVisitor* visitor) const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8805 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 8806 | for (const ClassLoaderData& data : class_loaders_) { |
Mathieu Chartier | 4843bd5 | 2015-10-01 17:08:44 -0700 | [diff] [blame] | 8807 | // Need to use DecodeJObject so that we get null for cleared JNI weak globals. |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 8808 | ObjPtr<mirror::ClassLoader> class_loader = ObjPtr<mirror::ClassLoader>::DownCast( |
| 8809 | self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8810 | if (class_loader != nullptr) { |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 8811 | visitor->Visit(class_loader.Ptr()); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8812 | } |
| 8813 | } |
| 8814 | } |
| 8815 | |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 8816 | void ClassLinker::InsertDexFileInToClassLoader(ObjPtr<mirror::Object> dex_file, |
| 8817 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8818 | DCHECK(dex_file != nullptr); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8819 | Thread* const self = Thread::Current(); |
| 8820 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 8821 | ClassTable* const table = ClassTableForClassLoader(class_loader.Ptr()); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8822 | DCHECK(table != nullptr); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 8823 | if (table->InsertStrongRoot(dex_file) && class_loader != nullptr) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8824 | // It was not already inserted, perform the write barrier to let the GC know the class loader's |
| 8825 | // class table was modified. |
| 8826 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader); |
| 8827 | } |
| 8828 | } |
| 8829 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 8830 | void ClassLinker::CleanupClassLoaders() { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8831 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8832 | std::vector<ClassLoaderData> to_delete; |
| 8833 | // Do the delete outside the lock to avoid lock violation in jit code cache. |
| 8834 | { |
| 8835 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 8836 | for (auto it = class_loaders_.begin(); it != class_loaders_.end(); ) { |
| 8837 | const ClassLoaderData& data = *it; |
| 8838 | // Need to use DecodeJObject so that we get null for cleared JNI weak globals. |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 8839 | ObjPtr<mirror::ClassLoader> class_loader = |
| 8840 | ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8841 | if (class_loader != nullptr) { |
| 8842 | ++it; |
| 8843 | } else { |
| 8844 | VLOG(class_linker) << "Freeing class loader"; |
| 8845 | to_delete.push_back(data); |
| 8846 | it = class_loaders_.erase(it); |
| 8847 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8848 | } |
| 8849 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8850 | for (ClassLoaderData& data : to_delete) { |
| 8851 | DeleteClassLoader(self, data); |
| 8852 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8853 | } |
| 8854 | |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 8855 | class GetResolvedClassesVisitor : public ClassVisitor { |
| 8856 | public: |
| 8857 | GetResolvedClassesVisitor(std::set<DexCacheResolvedClasses>* result, bool ignore_boot_classes) |
| 8858 | : result_(result), |
| 8859 | ignore_boot_classes_(ignore_boot_classes), |
| 8860 | last_resolved_classes_(result->end()), |
| 8861 | last_dex_file_(nullptr), |
| 8862 | vlog_is_on_(VLOG_IS_ON(class_linker)), |
| 8863 | extra_stats_(), |
| 8864 | last_extra_stats_(extra_stats_.end()) { } |
| 8865 | |
| 8866 | bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8867 | if (!klass->IsProxyClass() && |
| 8868 | !klass->IsArrayClass() && |
| 8869 | klass->IsResolved() && |
| 8870 | !klass->IsErroneousResolved() && |
| 8871 | (!ignore_boot_classes_ || klass->GetClassLoader() != nullptr)) { |
| 8872 | const DexFile& dex_file = klass->GetDexFile(); |
| 8873 | if (&dex_file != last_dex_file_) { |
| 8874 | last_dex_file_ = &dex_file; |
| 8875 | DexCacheResolvedClasses resolved_classes(dex_file.GetLocation(), |
| 8876 | dex_file.GetBaseLocation(), |
| 8877 | dex_file.GetLocationChecksum()); |
| 8878 | last_resolved_classes_ = result_->find(resolved_classes); |
| 8879 | if (last_resolved_classes_ == result_->end()) { |
| 8880 | last_resolved_classes_ = result_->insert(resolved_classes).first; |
| 8881 | } |
| 8882 | } |
| 8883 | bool added = last_resolved_classes_->AddClass(klass->GetDexTypeIndex()); |
| 8884 | if (UNLIKELY(vlog_is_on_) && added) { |
| 8885 | const DexCacheResolvedClasses* resolved_classes = std::addressof(*last_resolved_classes_); |
| 8886 | if (last_extra_stats_ == extra_stats_.end() || |
| 8887 | last_extra_stats_->first != resolved_classes) { |
| 8888 | last_extra_stats_ = extra_stats_.find(resolved_classes); |
| 8889 | if (last_extra_stats_ == extra_stats_.end()) { |
| 8890 | last_extra_stats_ = |
| 8891 | extra_stats_.emplace(resolved_classes, ExtraStats(dex_file.NumClassDefs())).first; |
| 8892 | } |
| 8893 | } |
| 8894 | } |
| 8895 | } |
| 8896 | return true; |
| 8897 | } |
| 8898 | |
| 8899 | void PrintStatistics() const { |
| 8900 | if (vlog_is_on_) { |
| 8901 | for (const DexCacheResolvedClasses& resolved_classes : *result_) { |
| 8902 | auto it = extra_stats_.find(std::addressof(resolved_classes)); |
| 8903 | DCHECK(it != extra_stats_.end()); |
| 8904 | const ExtraStats& extra_stats = it->second; |
| 8905 | LOG(INFO) << "Dex location " << resolved_classes.GetDexLocation() |
| 8906 | << " has " << resolved_classes.GetClasses().size() << " / " |
| 8907 | << extra_stats.number_of_class_defs_ << " resolved classes"; |
| 8908 | } |
| 8909 | } |
| 8910 | } |
| 8911 | |
| 8912 | private: |
| 8913 | struct ExtraStats { |
| 8914 | explicit ExtraStats(uint32_t number_of_class_defs) |
| 8915 | : number_of_class_defs_(number_of_class_defs) {} |
| 8916 | uint32_t number_of_class_defs_; |
| 8917 | }; |
| 8918 | |
| 8919 | std::set<DexCacheResolvedClasses>* result_; |
| 8920 | bool ignore_boot_classes_; |
| 8921 | std::set<DexCacheResolvedClasses>::iterator last_resolved_classes_; |
| 8922 | const DexFile* last_dex_file_; |
| 8923 | |
| 8924 | // Statistics. |
| 8925 | bool vlog_is_on_; |
| 8926 | std::map<const DexCacheResolvedClasses*, ExtraStats> extra_stats_; |
| 8927 | std::map<const DexCacheResolvedClasses*, ExtraStats>::iterator last_extra_stats_; |
| 8928 | }; |
| 8929 | |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8930 | std::set<DexCacheResolvedClasses> ClassLinker::GetResolvedClasses(bool ignore_boot_classes) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 8931 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8932 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 8933 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8934 | std::set<DexCacheResolvedClasses> ret; |
| 8935 | VLOG(class_linker) << "Collecting resolved classes"; |
| 8936 | const uint64_t start_time = NanoTime(); |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 8937 | GetResolvedClassesVisitor visitor(&ret, ignore_boot_classes); |
| 8938 | VisitClasses(&visitor); |
| 8939 | if (VLOG_IS_ON(class_linker)) { |
| 8940 | visitor.PrintStatistics(); |
| 8941 | LOG(INFO) << "Collecting class profile took " << PrettyDuration(NanoTime() - start_time); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8942 | } |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8943 | return ret; |
| 8944 | } |
| 8945 | |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 8946 | std::unordered_set<std::string> ClassLinker::GetClassDescriptorsForResolvedClasses( |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8947 | const std::set<DexCacheResolvedClasses>& classes) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 8948 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8949 | std::unordered_set<std::string> ret; |
| 8950 | Thread* const self = Thread::Current(); |
| 8951 | std::unordered_map<std::string, const DexFile*> location_to_dex_file; |
| 8952 | ScopedObjectAccess soa(self); |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 8953 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 8954 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8955 | for (const ClassLinker::DexCacheData& data : GetDexCachesData()) { |
| 8956 | if (!self->IsJWeakCleared(data.weak_root)) { |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 8957 | ObjPtr<mirror::DexCache> dex_cache = soa.Decode<mirror::DexCache>(data.weak_root); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8958 | if (dex_cache != nullptr) { |
| 8959 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 8960 | // There could be duplicates if two dex files with the same location are mapped. |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 8961 | location_to_dex_file.emplace(dex_file->GetLocation(), dex_file); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8962 | } |
| 8963 | } |
| 8964 | } |
| 8965 | for (const DexCacheResolvedClasses& info : classes) { |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 8966 | const std::string& location = info.GetDexLocation(); |
| 8967 | auto found = location_to_dex_file.find(location); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8968 | if (found != location_to_dex_file.end()) { |
| 8969 | const DexFile* dex_file = found->second; |
| 8970 | VLOG(profiler) << "Found opened dex file for " << dex_file->GetLocation() << " with " |
| 8971 | << info.GetClasses().size() << " classes"; |
| 8972 | DCHECK_EQ(dex_file->GetLocationChecksum(), info.GetLocationChecksum()); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 8973 | for (dex::TypeIndex type_idx : info.GetClasses()) { |
Jeff Hao | 54b5855 | 2016-11-16 15:15:04 -0800 | [diff] [blame] | 8974 | const DexFile::TypeId& type_id = dex_file->GetTypeId(type_idx); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8975 | const char* descriptor = dex_file->GetTypeDescriptor(type_id); |
| 8976 | ret.insert(descriptor); |
| 8977 | } |
| 8978 | } else { |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 8979 | VLOG(class_linker) << "Failed to find opened dex file for location " << location; |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8980 | } |
| 8981 | } |
| 8982 | return ret; |
| 8983 | } |
| 8984 | |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8985 | class ClassLinker::FindVirtualMethodHolderVisitor : public ClassVisitor { |
| 8986 | public: |
| 8987 | FindVirtualMethodHolderVisitor(const ArtMethod* method, PointerSize pointer_size) |
| 8988 | : method_(method), |
| 8989 | pointer_size_(pointer_size) {} |
| 8990 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8991 | bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8992 | if (klass->GetVirtualMethodsSliceUnchecked(pointer_size_).Contains(method_)) { |
| 8993 | holder_ = klass; |
| 8994 | } |
| 8995 | // Return false to stop searching if holder_ is not null. |
| 8996 | return holder_ == nullptr; |
| 8997 | } |
| 8998 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8999 | ObjPtr<mirror::Class> holder_ = nullptr; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 9000 | const ArtMethod* const method_; |
| 9001 | const PointerSize pointer_size_; |
| 9002 | }; |
| 9003 | |
| 9004 | mirror::Class* ClassLinker::GetHoldingClassOfCopiedMethod(ArtMethod* method) { |
| 9005 | ScopedTrace trace(__FUNCTION__); // Since this function is slow, have a trace to notify people. |
| 9006 | CHECK(method->IsCopied()); |
| 9007 | FindVirtualMethodHolderVisitor visitor(method, image_pointer_size_); |
| 9008 | VisitClasses(&visitor); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9009 | return visitor.holder_.Ptr(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 9010 | } |
| 9011 | |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 9012 | mirror::IfTable* ClassLinker::AllocIfTable(Thread* self, size_t ifcount) { |
| 9013 | return down_cast<mirror::IfTable*>( |
| 9014 | mirror::IfTable::Alloc(self, |
| 9015 | GetClassRoot(kObjectArrayClass), |
| 9016 | ifcount * mirror::IfTable::kMax)); |
| 9017 | } |
| 9018 | |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 9019 | // Instantiate ResolveMethod. |
| 9020 | template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::kForceICCECheck>( |
| 9021 | const DexFile& dex_file, |
| 9022 | uint32_t method_idx, |
| 9023 | Handle<mirror::DexCache> dex_cache, |
| 9024 | Handle<mirror::ClassLoader> class_loader, |
| 9025 | ArtMethod* referrer, |
| 9026 | InvokeType type); |
| 9027 | template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::kNoICCECheckForCache>( |
| 9028 | const DexFile& dex_file, |
| 9029 | uint32_t method_idx, |
| 9030 | Handle<mirror::DexCache> dex_cache, |
| 9031 | Handle<mirror::ClassLoader> class_loader, |
| 9032 | ArtMethod* referrer, |
| 9033 | InvokeType type); |
| 9034 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 9035 | } // namespace art |