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" |
Andreas Gampe | 2ff3b97 | 2017-06-05 18:14:53 -0700 | [diff] [blame] | 110 | #include "utf.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 111 | #include "utils.h" |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 112 | #include "utils/dex_cache_arrays_layout-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 113 | #include "verifier/method_verifier.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 114 | #include "well_known_classes.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 115 | |
| 116 | namespace art { |
| 117 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 118 | using android::base::StringPrintf; |
| 119 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 120 | static constexpr bool kSanityCheckObjects = kIsDebugBuild; |
Mathieu Chartier | 8790c7f | 2016-03-31 15:05:45 -0700 | [diff] [blame] | 121 | static constexpr bool kVerifyArtMethodDeclaringClasses = kIsDebugBuild; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 122 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 123 | static void ThrowNoClassDefFoundError(const char* fmt, ...) |
| 124 | __attribute__((__format__(__printf__, 1, 2))) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 125 | REQUIRES_SHARED(Locks::mutator_lock_); |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 126 | static void ThrowNoClassDefFoundError(const char* fmt, ...) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 127 | va_list args; |
| 128 | va_start(args, fmt); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 129 | Thread* self = Thread::Current(); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 130 | self->ThrowNewExceptionV("Ljava/lang/NoClassDefFoundError;", fmt, args); |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 131 | va_end(args); |
| 132 | } |
| 133 | |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 134 | static bool HasInitWithString(Thread* self, ClassLinker* class_linker, const char* descriptor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 135 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 136 | ArtMethod* method = self->GetCurrentMethod(nullptr); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 137 | StackHandleScope<1> hs(self); |
| 138 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(method != nullptr ? |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 139 | method->GetDeclaringClass()->GetClassLoader() : nullptr)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 140 | ObjPtr<mirror::Class> exception_class = class_linker->FindClass(self, descriptor, class_loader); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 141 | |
| 142 | if (exception_class == nullptr) { |
| 143 | // No exc class ~ no <init>-with-string. |
| 144 | CHECK(self->IsExceptionPending()); |
| 145 | self->ClearException(); |
| 146 | return false; |
| 147 | } |
| 148 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 149 | ArtMethod* exception_init_method = exception_class->FindDeclaredDirectMethod( |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 150 | "<init>", "(Ljava/lang/String;)V", class_linker->GetImagePointerSize()); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 151 | return exception_init_method != nullptr; |
| 152 | } |
| 153 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 154 | static mirror::Object* GetVerifyError(ObjPtr<mirror::Class> c) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 155 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 156 | ObjPtr<mirror::ClassExt> ext(c->GetExtData()); |
| 157 | if (ext == nullptr) { |
| 158 | return nullptr; |
| 159 | } else { |
| 160 | return ext->GetVerifyError(); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // Helper for ThrowEarlierClassFailure. Throws the stored error. |
| 165 | static void HandleEarlierVerifyError(Thread* self, |
| 166 | ClassLinker* class_linker, |
| 167 | ObjPtr<mirror::Class> c) |
| 168 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 169 | ObjPtr<mirror::Object> obj = GetVerifyError(c); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 170 | DCHECK(obj != nullptr); |
| 171 | self->AssertNoPendingException(); |
| 172 | if (obj->IsClass()) { |
| 173 | // Previous error has been stored as class. Create a new exception of that type. |
| 174 | |
| 175 | // It's possible the exception doesn't have a <init>(String). |
| 176 | std::string temp; |
| 177 | const char* descriptor = obj->AsClass()->GetDescriptor(&temp); |
| 178 | |
| 179 | if (HasInitWithString(self, class_linker, descriptor)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 180 | self->ThrowNewException(descriptor, c->PrettyDescriptor().c_str()); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 181 | } else { |
| 182 | self->ThrowNewException(descriptor, nullptr); |
| 183 | } |
| 184 | } else { |
| 185 | // Previous error has been stored as an instance. Just rethrow. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 186 | ObjPtr<mirror::Class> throwable_class = |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 187 | self->DecodeJObject(WellKnownClasses::java_lang_Throwable)->AsClass(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 188 | ObjPtr<mirror::Class> error_class = obj->GetClass(); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 189 | CHECK(throwable_class->IsAssignableFrom(error_class)); |
| 190 | self->SetException(obj->AsThrowable()); |
| 191 | } |
| 192 | self->AssertPendingException(); |
| 193 | } |
| 194 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 195 | 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] | 196 | // The class failed to initialize on a previous attempt, so we want to throw |
| 197 | // a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we |
| 198 | // failed in verification, in which case v2 5.4.1 says we need to re-throw |
| 199 | // the previous error. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 200 | Runtime* const runtime = Runtime::Current(); |
| 201 | if (!runtime->IsAotCompiler()) { // Give info if this occurs at runtime. |
Andreas Gampe | 3d6b470 | 2015-09-21 08:35:52 -0700 | [diff] [blame] | 202 | std::string extra; |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 203 | if (GetVerifyError(c) != nullptr) { |
| 204 | ObjPtr<mirror::Object> verify_error = GetVerifyError(c); |
Andreas Gampe | 369c851 | 2016-01-28 15:31:39 -0800 | [diff] [blame] | 205 | if (verify_error->IsClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 206 | extra = mirror::Class::PrettyDescriptor(verify_error->AsClass()); |
Andreas Gampe | 369c851 | 2016-01-28 15:31:39 -0800 | [diff] [blame] | 207 | } else { |
| 208 | extra = verify_error->AsThrowable()->Dump(); |
| 209 | } |
Andreas Gampe | 3d6b470 | 2015-09-21 08:35:52 -0700 | [diff] [blame] | 210 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 211 | LOG(INFO) << "Rejecting re-init on previously-failed class " << c->PrettyClass() |
| 212 | << ": " << extra; |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 213 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 214 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 215 | CHECK(c->IsErroneous()) << c->PrettyClass() << " " << c->GetStatus(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 216 | Thread* self = Thread::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 217 | if (runtime->IsAotCompiler()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 218 | // At compile time, accurate errors and NCDFE are disabled to speed compilation. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 219 | ObjPtr<mirror::Throwable> pre_allocated = runtime->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 220 | self->SetException(pre_allocated); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 221 | } else { |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 222 | if (GetVerifyError(c) != nullptr) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 223 | // Rethrow stored error. |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 224 | HandleEarlierVerifyError(self, this, c); |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 225 | } |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 226 | // TODO This might be wrong if we hit an OOME while allocating the ClassExt. In that case we |
| 227 | // might have meant to go down the earlier if statement with the original error but it got |
| 228 | // swallowed by the OOM so we end up here. |
| 229 | if (GetVerifyError(c) == nullptr || wrap_in_no_class_def) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 230 | // If there isn't a recorded earlier error, or this is a repeat throw from initialization, |
| 231 | // the top-level exception must be a NoClassDefFoundError. The potentially already pending |
| 232 | // exception will be a cause. |
| 233 | self->ThrowNewWrappedException("Ljava/lang/NoClassDefFoundError;", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 234 | c->PrettyDescriptor().c_str()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 235 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 239 | static void VlogClassInitializationFailure(Handle<mirror::Class> klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 240 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 241 | if (VLOG_IS_ON(class_linker)) { |
| 242 | std::string temp; |
| 243 | LOG(INFO) << "Failed to initialize class " << klass->GetDescriptor(&temp) << " from " |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 244 | << klass->GetLocation() << "\n" << Thread::Current()->GetException()->Dump(); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | static void WrapExceptionInInitializer(Handle<mirror::Class> klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 249 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 250 | Thread* self = Thread::Current(); |
| 251 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 252 | |
| 253 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 254 | CHECK(cause.get() != nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 255 | |
Andreas Gampe | 1e8a395 | 2016-11-30 10:13:19 -0800 | [diff] [blame] | 256 | // Boot classpath classes should not fail initialization. This is a sanity debug check. This |
| 257 | // cannot in general be guaranteed, but in all likelihood leads to breakage down the line. |
| 258 | if (klass->GetClassLoader() == nullptr && !Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | 22f71d2 | 2016-11-21 10:10:08 -0800 | [diff] [blame] | 259 | std::string tmp; |
Andreas Gampe | 1e8a395 | 2016-11-30 10:13:19 -0800 | [diff] [blame] | 260 | LOG(kIsDebugBuild ? FATAL : WARNING) << klass->GetDescriptor(&tmp) << " failed initialization"; |
Andreas Gampe | 22f71d2 | 2016-11-21 10:10:08 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 263 | env->ExceptionClear(); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 264 | bool is_error = env->IsInstanceOf(cause.get(), WellKnownClasses::java_lang_Error); |
| 265 | env->Throw(cause.get()); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 266 | |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 267 | // We only wrap non-Error exceptions; an Error can just be used as-is. |
| 268 | if (!is_error) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 269 | self->ThrowNewWrappedException("Ljava/lang/ExceptionInInitializerError;", nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 270 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 271 | VlogClassInitializationFailure(klass); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 274 | // Gap between two fields in object layout. |
| 275 | struct FieldGap { |
| 276 | uint32_t start_offset; // The offset from the start of the object. |
| 277 | uint32_t size; // The gap size of 1, 2, or 4 bytes. |
| 278 | }; |
| 279 | struct FieldGapsComparator { |
| 280 | explicit FieldGapsComparator() { |
| 281 | } |
| 282 | bool operator() (const FieldGap& lhs, const FieldGap& rhs) |
| 283 | NO_THREAD_SAFETY_ANALYSIS { |
Andreas Gampe | f52857f | 2015-02-18 15:38:57 -0800 | [diff] [blame] | 284 | // Sort by gap size, largest first. Secondary sort by starting offset. |
Richard Uhler | fab6788 | 2015-07-13 17:00:35 -0700 | [diff] [blame] | 285 | // Note that the priority queue returns the largest element, so operator() |
| 286 | // should return true if lhs is less than rhs. |
| 287 | 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] | 288 | } |
| 289 | }; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 290 | typedef std::priority_queue<FieldGap, std::vector<FieldGap>, FieldGapsComparator> FieldGaps; |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 291 | |
| 292 | // Adds largest aligned gaps to queue of gaps. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 293 | 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] | 294 | DCHECK(gaps != nullptr); |
| 295 | |
| 296 | uint32_t current_offset = gap_start; |
| 297 | while (current_offset != gap_end) { |
| 298 | size_t remaining = gap_end - current_offset; |
| 299 | if (remaining >= sizeof(uint32_t) && IsAligned<4>(current_offset)) { |
| 300 | gaps->push(FieldGap {current_offset, sizeof(uint32_t)}); |
| 301 | current_offset += sizeof(uint32_t); |
| 302 | } else if (remaining >= sizeof(uint16_t) && IsAligned<2>(current_offset)) { |
| 303 | gaps->push(FieldGap {current_offset, sizeof(uint16_t)}); |
| 304 | current_offset += sizeof(uint16_t); |
| 305 | } else { |
| 306 | gaps->push(FieldGap {current_offset, sizeof(uint8_t)}); |
| 307 | current_offset += sizeof(uint8_t); |
| 308 | } |
| 309 | DCHECK_LE(current_offset, gap_end) << "Overran gap"; |
| 310 | } |
| 311 | } |
| 312 | // Shuffle fields forward, making use of gaps whenever possible. |
| 313 | template<int n> |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 314 | static void ShuffleForward(size_t* current_field_idx, |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 315 | MemberOffset* field_offset, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 316 | std::deque<ArtField*>* grouped_and_sorted_fields, |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 317 | FieldGaps* gaps) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 318 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 319 | DCHECK(current_field_idx != nullptr); |
| 320 | DCHECK(grouped_and_sorted_fields != nullptr); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 321 | DCHECK(gaps != nullptr); |
| 322 | DCHECK(field_offset != nullptr); |
| 323 | |
| 324 | DCHECK(IsPowerOfTwo(n)); |
| 325 | while (!grouped_and_sorted_fields->empty()) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 326 | ArtField* field = grouped_and_sorted_fields->front(); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 327 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 328 | if (Primitive::ComponentSize(type) < n) { |
| 329 | break; |
| 330 | } |
| 331 | if (!IsAligned<n>(field_offset->Uint32Value())) { |
| 332 | MemberOffset old_offset = *field_offset; |
| 333 | *field_offset = MemberOffset(RoundUp(field_offset->Uint32Value(), n)); |
| 334 | AddFieldGap(old_offset.Uint32Value(), field_offset->Uint32Value(), gaps); |
| 335 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 336 | CHECK(type != Primitive::kPrimNot) << field->PrettyField(); // should be primitive types |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 337 | grouped_and_sorted_fields->pop_front(); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 338 | if (!gaps->empty() && gaps->top().size >= n) { |
| 339 | FieldGap gap = gaps->top(); |
| 340 | gaps->pop(); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 341 | DCHECK_ALIGNED(gap.start_offset, n); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 342 | field->SetOffset(MemberOffset(gap.start_offset)); |
| 343 | if (gap.size > n) { |
| 344 | AddFieldGap(gap.start_offset + n, gap.start_offset + gap.size, gaps); |
| 345 | } |
| 346 | } else { |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 347 | DCHECK_ALIGNED(field_offset->Uint32Value(), n); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 348 | field->SetOffset(*field_offset); |
| 349 | *field_offset = MemberOffset(field_offset->Uint32Value() + n); |
| 350 | } |
| 351 | ++(*current_field_idx); |
| 352 | } |
| 353 | } |
| 354 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 355 | ClassLinker::ClassLinker(InternTable* intern_table) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 356 | : boot_class_table_(new ClassTable()), |
| 357 | failed_dex_cache_class_lookups_(0), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 358 | class_roots_(nullptr), |
| 359 | array_iftable_(nullptr), |
| 360 | find_array_class_cache_next_victim_(0), |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 361 | init_done_(false), |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 362 | log_new_roots_(false), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 363 | intern_table_(intern_table), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 364 | quick_resolution_trampoline_(nullptr), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 365 | quick_imt_conflict_trampoline_(nullptr), |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 366 | quick_generic_jni_trampoline_(nullptr), |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 367 | quick_to_interpreter_bridge_trampoline_(nullptr), |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 368 | image_pointer_size_(kRuntimePointerSize) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 369 | CHECK(intern_table_ != nullptr); |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 370 | static_assert(kFindArrayCacheSize == arraysize(find_array_class_cache_), |
| 371 | "Array cache size wrong."); |
| 372 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 373 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 374 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 375 | void ClassLinker::CheckSystemClass(Thread* self, Handle<mirror::Class> c1, const char* descriptor) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 376 | ObjPtr<mirror::Class> c2 = FindSystemClass(self, descriptor); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 377 | if (c2 == nullptr) { |
| 378 | LOG(FATAL) << "Could not find class " << descriptor; |
| 379 | UNREACHABLE(); |
| 380 | } |
| 381 | if (c1.Get() != c2) { |
| 382 | std::ostringstream os1, os2; |
| 383 | c1->DumpClass(os1, mirror::Class::kDumpClassFullDetail); |
| 384 | c2->DumpClass(os2, mirror::Class::kDumpClassFullDetail); |
| 385 | LOG(FATAL) << "InitWithoutImage: Class mismatch for " << descriptor |
| 386 | << ". This is most likely the result of a broken build. Make sure that " |
| 387 | << "libcore and art projects match.\n\n" |
| 388 | << os1.str() << "\n\n" << os2.str(); |
| 389 | UNREACHABLE(); |
| 390 | } |
| 391 | } |
| 392 | |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 393 | bool ClassLinker::InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path, |
| 394 | std::string* error_msg) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 395 | VLOG(startup) << "ClassLinker::Init"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 396 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 397 | Thread* const self = Thread::Current(); |
| 398 | Runtime* const runtime = Runtime::Current(); |
| 399 | gc::Heap* const heap = runtime->GetHeap(); |
| 400 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 401 | CHECK(!heap->HasBootImageSpace()) << "Runtime has image. We should use it."; |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 402 | CHECK(!init_done_); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 403 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 404 | // Use the pointer size from the runtime since we are probably creating the image. |
| 405 | image_pointer_size_ = InstructionSetPointerSize(runtime->GetInstructionSet()); |
| 406 | |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 407 | // java_lang_Class comes first, it's needed for AllocClass |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 408 | // 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] | 409 | heap->IncrementDisableMovingGC(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 410 | StackHandleScope<64> hs(self); // 64 is picked arbitrarily. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 411 | auto class_class_size = mirror::Class::ClassClassSize(image_pointer_size_); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 412 | Handle<mirror::Class> java_lang_Class(hs.NewHandle(down_cast<mirror::Class*>( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 413 | heap->AllocNonMovableObject<true>(self, nullptr, class_class_size, VoidFunctor())))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 414 | CHECK(java_lang_Class != nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 415 | mirror::Class::SetClassClass(java_lang_Class.Get()); |
| 416 | java_lang_Class->SetClass(java_lang_Class.Get()); |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 417 | if (kUseBakerReadBarrier) { |
| 418 | java_lang_Class->AssertReadBarrierState(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 419 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 420 | java_lang_Class->SetClassSize(class_class_size); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 421 | java_lang_Class->SetPrimitiveType(Primitive::kPrimNot); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 422 | heap->DecrementDisableMovingGC(self); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 423 | // AllocClass(ObjPtr<mirror::Class>) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 424 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 425 | // Class[] is used for reflection support. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 426 | 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] | 427 | Handle<mirror::Class> class_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 428 | AllocClass(self, java_lang_Class.Get(), class_array_class_size))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 429 | class_array_class->SetComponentType(java_lang_Class.Get()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 430 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 431 | // 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] | 432 | Handle<mirror::Class> java_lang_Object(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 433 | AllocClass(self, java_lang_Class.Get(), mirror::Object::ClassSize(image_pointer_size_)))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 434 | CHECK(java_lang_Object != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 435 | // backfill Object as the super class of Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 436 | java_lang_Class->SetSuperClass(java_lang_Object.Get()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 437 | mirror::Class::SetStatus(java_lang_Object, mirror::Class::kStatusLoaded, self); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 438 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 439 | java_lang_Object->SetObjectSize(sizeof(mirror::Object)); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 440 | // Allocate in non-movable so that it's possible to check if a JNI weak global ref has been |
| 441 | // cleared without triggering the read barrier and unintentionally mark the sentinel alive. |
| 442 | runtime->SetSentinel(heap->AllocNonMovableObject<true>(self, |
| 443 | java_lang_Object.Get(), |
| 444 | java_lang_Object->GetObjectSize(), |
| 445 | VoidFunctor())); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 446 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 447 | // Object[] next to hold class roots. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 448 | Handle<mirror::Class> object_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 449 | AllocClass(self, java_lang_Class.Get(), |
| 450 | mirror::ObjectArray<mirror::Object>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 451 | object_array_class->SetComponentType(java_lang_Object.Get()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 452 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 453 | // Setup the char (primitive) class to be used for char[]. |
| 454 | Handle<mirror::Class> char_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 455 | AllocClass(self, java_lang_Class.Get(), |
| 456 | mirror::Class::PrimitiveClassSize(image_pointer_size_)))); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 457 | // The primitive char class won't be initialized by |
| 458 | // InitializePrimitiveClass until line 459, but strings (and |
| 459 | // internal char arrays) will be allocated before that and the |
| 460 | // component size, which is computed from the primitive type, needs |
| 461 | // to be set here. |
| 462 | char_class->SetPrimitiveType(Primitive::kPrimChar); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 463 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 464 | // Setup the char[] class to be used for String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 465 | Handle<mirror::Class> char_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 466 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 467 | char_array_class->SetComponentType(char_class.Get()); |
| 468 | mirror::CharArray::SetArrayClass(char_array_class.Get()); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 469 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 470 | // Setup String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 471 | Handle<mirror::Class> java_lang_String(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 472 | AllocClass(self, java_lang_Class.Get(), mirror::String::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 473 | java_lang_String->SetStringClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 474 | mirror::String::SetClass(java_lang_String.Get()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 475 | mirror::Class::SetStatus(java_lang_String, mirror::Class::kStatusResolved, self); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 476 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 477 | // Setup java.lang.ref.Reference. |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 478 | Handle<mirror::Class> java_lang_ref_Reference(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 479 | AllocClass(self, java_lang_Class.Get(), mirror::Reference::ClassSize(image_pointer_size_)))); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 480 | mirror::Reference::SetClass(java_lang_ref_Reference.Get()); |
| 481 | java_lang_ref_Reference->SetObjectSize(mirror::Reference::InstanceSize()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 482 | mirror::Class::SetStatus(java_lang_ref_Reference, mirror::Class::kStatusResolved, self); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 483 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 484 | // 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] | 485 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 486 | mirror::ObjectArray<mirror::Class>::Alloc(self, object_array_class.Get(), |
| 487 | kClassRootsMax)); |
| 488 | CHECK(!class_roots_.IsNull()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 489 | SetClassRoot(kJavaLangClass, java_lang_Class.Get()); |
| 490 | SetClassRoot(kJavaLangObject, java_lang_Object.Get()); |
| 491 | SetClassRoot(kClassArrayClass, class_array_class.Get()); |
| 492 | SetClassRoot(kObjectArrayClass, object_array_class.Get()); |
| 493 | SetClassRoot(kCharArrayClass, char_array_class.Get()); |
| 494 | SetClassRoot(kJavaLangString, java_lang_String.Get()); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 495 | SetClassRoot(kJavaLangRefReference, java_lang_ref_Reference.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 496 | |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 497 | // Fill in the empty iftable. Needs to be done after the kObjectArrayClass root is set. |
| 498 | java_lang_Object->SetIfTable(AllocIfTable(self, 0)); |
| 499 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 500 | // Setup the primitive type classes. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 501 | SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass(self, Primitive::kPrimBoolean)); |
| 502 | SetClassRoot(kPrimitiveByte, CreatePrimitiveClass(self, Primitive::kPrimByte)); |
| 503 | SetClassRoot(kPrimitiveShort, CreatePrimitiveClass(self, Primitive::kPrimShort)); |
| 504 | SetClassRoot(kPrimitiveInt, CreatePrimitiveClass(self, Primitive::kPrimInt)); |
| 505 | SetClassRoot(kPrimitiveLong, CreatePrimitiveClass(self, Primitive::kPrimLong)); |
| 506 | SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass(self, Primitive::kPrimFloat)); |
| 507 | SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass(self, Primitive::kPrimDouble)); |
| 508 | SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass(self, Primitive::kPrimVoid)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 509 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 510 | // Create array interface entries to populate once we can load system classes. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 511 | array_iftable_ = GcRoot<mirror::IfTable>(AllocIfTable(self, 2)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 512 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 513 | // Create int array type for AllocDexCache (done in AppendToBootClassPath). |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 514 | Handle<mirror::Class> int_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 515 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize(image_pointer_size_)))); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 516 | int_array_class->SetComponentType(GetClassRoot(kPrimitiveInt)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 517 | mirror::IntArray::SetArrayClass(int_array_class.Get()); |
| 518 | SetClassRoot(kIntArrayClass, int_array_class.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 519 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 520 | // Create long array type for AllocDexCache (done in AppendToBootClassPath). |
| 521 | Handle<mirror::Class> long_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 522 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 523 | long_array_class->SetComponentType(GetClassRoot(kPrimitiveLong)); |
| 524 | mirror::LongArray::SetArrayClass(long_array_class.Get()); |
| 525 | SetClassRoot(kLongArrayClass, long_array_class.Get()); |
| 526 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 527 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 528 | |
Ian Rogers | 52813c9 | 2012-10-11 11:50:38 -0700 | [diff] [blame] | 529 | // Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 530 | Handle<mirror::Class> java_lang_DexCache(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 531 | AllocClass(self, java_lang_Class.Get(), mirror::DexCache::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 532 | SetClassRoot(kJavaLangDexCache, java_lang_DexCache.Get()); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 533 | java_lang_DexCache->SetDexCacheClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 534 | java_lang_DexCache->SetObjectSize(mirror::DexCache::InstanceSize()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 535 | mirror::Class::SetStatus(java_lang_DexCache, mirror::Class::kStatusResolved, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 536 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 537 | |
| 538 | // Setup dalvik.system.ClassExt |
| 539 | Handle<mirror::Class> dalvik_system_ClassExt(hs.NewHandle( |
| 540 | AllocClass(self, java_lang_Class.Get(), mirror::ClassExt::ClassSize(image_pointer_size_)))); |
| 541 | SetClassRoot(kDalvikSystemClassExt, dalvik_system_ClassExt.Get()); |
| 542 | mirror::ClassExt::SetClass(dalvik_system_ClassExt.Get()); |
| 543 | mirror::Class::SetStatus(dalvik_system_ClassExt, mirror::Class::kStatusResolved, self); |
| 544 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 545 | // Set up array classes for string, field, method |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 546 | Handle<mirror::Class> object_array_string(hs.NewHandle( |
| 547 | AllocClass(self, java_lang_Class.Get(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 548 | mirror::ObjectArray<mirror::String>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 549 | object_array_string->SetComponentType(java_lang_String.Get()); |
| 550 | SetClassRoot(kJavaLangStringArrayClass, object_array_string.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 551 | |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 552 | LinearAlloc* linear_alloc = runtime->GetLinearAlloc(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 553 | // Create runtime resolution and imt conflict methods. |
| 554 | runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 555 | runtime->SetImtConflictMethod(runtime->CreateImtConflictMethod(linear_alloc)); |
| 556 | runtime->SetImtUnimplementedMethod(runtime->CreateImtConflictMethod(linear_alloc)); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 557 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 558 | // Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create |
| 559 | // DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses |
| 560 | // these roots. |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 561 | if (boot_class_path.empty()) { |
| 562 | *error_msg = "Boot classpath is empty."; |
| 563 | return false; |
| 564 | } |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 565 | for (auto& dex_file : boot_class_path) { |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 566 | if (dex_file.get() == nullptr) { |
| 567 | *error_msg = "Null dex file."; |
| 568 | return false; |
| 569 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 570 | AppendToBootClassPath(self, *dex_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 571 | boot_dex_files_.push_back(std::move(dex_file)); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 572 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 573 | |
| 574 | // now we can use FindSystemClass |
| 575 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 576 | // run char class through InitializePrimitiveClass to finish init |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 577 | InitializePrimitiveClass(char_class.Get(), Primitive::kPrimChar); |
| 578 | SetClassRoot(kPrimitiveChar, char_class.Get()); // needs descriptor |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 579 | |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 580 | // Set up GenericJNI entrypoint. That is mainly a hack for common_compiler_test.h so that |
| 581 | // we do not need friend classes or a publicly exposed setter. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 582 | quick_generic_jni_trampoline_ = GetQuickGenericJniStub(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 583 | if (!runtime->IsAotCompiler()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 584 | // 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] | 585 | quick_resolution_trampoline_ = GetQuickResolutionStub(); |
| 586 | quick_imt_conflict_trampoline_ = GetQuickImtConflictStub(); |
| 587 | quick_to_interpreter_bridge_trampoline_ = GetQuickToInterpreterBridge(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 588 | } |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 589 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 590 | // 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] | 591 | mirror::Class::SetStatus(java_lang_Object, mirror::Class::kStatusNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 592 | CheckSystemClass(self, java_lang_Object, "Ljava/lang/Object;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 593 | CHECK_EQ(java_lang_Object->GetObjectSize(), mirror::Object::InstanceSize()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 594 | mirror::Class::SetStatus(java_lang_String, mirror::Class::kStatusNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 595 | CheckSystemClass(self, java_lang_String, "Ljava/lang/String;"); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 596 | mirror::Class::SetStatus(java_lang_DexCache, mirror::Class::kStatusNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 597 | CheckSystemClass(self, java_lang_DexCache, "Ljava/lang/DexCache;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 598 | CHECK_EQ(java_lang_DexCache->GetObjectSize(), mirror::DexCache::InstanceSize()); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 599 | mirror::Class::SetStatus(dalvik_system_ClassExt, mirror::Class::kStatusNotReady, self); |
| 600 | CheckSystemClass(self, dalvik_system_ClassExt, "Ldalvik/system/ClassExt;"); |
| 601 | CHECK_EQ(dalvik_system_ClassExt->GetObjectSize(), mirror::ClassExt::InstanceSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 602 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 603 | // 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] | 604 | SetClassRoot(kBooleanArrayClass, FindSystemClass(self, "[Z")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 605 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 606 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 607 | SetClassRoot(kByteArrayClass, FindSystemClass(self, "[B")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 608 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 609 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 610 | CheckSystemClass(self, char_array_class, "[C"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 611 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 612 | SetClassRoot(kShortArrayClass, FindSystemClass(self, "[S")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 613 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 614 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 615 | CheckSystemClass(self, int_array_class, "[I"); |
| 616 | CheckSystemClass(self, long_array_class, "[J"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 617 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 618 | SetClassRoot(kFloatArrayClass, FindSystemClass(self, "[F")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 619 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 620 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 621 | SetClassRoot(kDoubleArrayClass, FindSystemClass(self, "[D")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 622 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 623 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 624 | // Run Class through FindSystemClass. This initializes the dex_cache_ fields and register it |
| 625 | // in class_table_. |
| 626 | CheckSystemClass(self, java_lang_Class, "Ljava/lang/Class;"); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 627 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 628 | CheckSystemClass(self, class_array_class, "[Ljava/lang/Class;"); |
| 629 | CheckSystemClass(self, object_array_class, "[Ljava/lang/Object;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 630 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 631 | // Setup the single, global copy of "iftable". |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 632 | auto java_lang_Cloneable = hs.NewHandle(FindSystemClass(self, "Ljava/lang/Cloneable;")); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 633 | CHECK(java_lang_Cloneable != nullptr); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 634 | auto java_io_Serializable = hs.NewHandle(FindSystemClass(self, "Ljava/io/Serializable;")); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 635 | CHECK(java_io_Serializable != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 636 | // We assume that Cloneable/Serializable don't have superinterfaces -- normally we'd have to |
| 637 | // crawl up and explicitly list all of the supers as well. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 638 | array_iftable_.Read()->SetInterface(0, java_lang_Cloneable.Get()); |
| 639 | array_iftable_.Read()->SetInterface(1, java_io_Serializable.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 640 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 641 | // Sanity check Class[] and Object[]'s interfaces. GetDirectInterface may cause thread |
| 642 | // suspension. |
| 643 | CHECK_EQ(java_lang_Cloneable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 644 | mirror::Class::GetDirectInterface(self, class_array_class.Get(), 0)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 645 | CHECK_EQ(java_io_Serializable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 646 | mirror::Class::GetDirectInterface(self, class_array_class.Get(), 1)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 647 | CHECK_EQ(java_lang_Cloneable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 648 | mirror::Class::GetDirectInterface(self, object_array_class.Get(), 0)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 649 | CHECK_EQ(java_io_Serializable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 650 | mirror::Class::GetDirectInterface(self, object_array_class.Get(), 1)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 651 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 652 | CHECK_EQ(object_array_string.Get(), |
| 653 | FindSystemClass(self, GetClassRootDescriptor(kJavaLangStringArrayClass))); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 654 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 655 | // 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] | 656 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 657 | // Create java.lang.reflect.Proxy root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 658 | SetClassRoot(kJavaLangReflectProxy, FindSystemClass(self, "Ljava/lang/reflect/Proxy;")); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 659 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 660 | // Create java.lang.reflect.Field.class root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 661 | auto* class_root = FindSystemClass(self, "Ljava/lang/reflect/Field;"); |
| 662 | CHECK(class_root != nullptr); |
| 663 | SetClassRoot(kJavaLangReflectField, class_root); |
| 664 | mirror::Field::SetClass(class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 665 | |
| 666 | // Create java.lang.reflect.Field array root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 667 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Field;"); |
| 668 | CHECK(class_root != nullptr); |
| 669 | SetClassRoot(kJavaLangReflectFieldArrayClass, class_root); |
| 670 | mirror::Field::SetArrayClass(class_root); |
| 671 | |
| 672 | // Create java.lang.reflect.Constructor.class root and array root. |
| 673 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Constructor;"); |
| 674 | CHECK(class_root != nullptr); |
| 675 | SetClassRoot(kJavaLangReflectConstructor, class_root); |
| 676 | mirror::Constructor::SetClass(class_root); |
| 677 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Constructor;"); |
| 678 | CHECK(class_root != nullptr); |
| 679 | SetClassRoot(kJavaLangReflectConstructorArrayClass, class_root); |
| 680 | mirror::Constructor::SetArrayClass(class_root); |
| 681 | |
| 682 | // Create java.lang.reflect.Method.class root and array root. |
| 683 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Method;"); |
| 684 | CHECK(class_root != nullptr); |
| 685 | SetClassRoot(kJavaLangReflectMethod, class_root); |
| 686 | mirror::Method::SetClass(class_root); |
| 687 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Method;"); |
| 688 | CHECK(class_root != nullptr); |
| 689 | SetClassRoot(kJavaLangReflectMethodArrayClass, class_root); |
| 690 | mirror::Method::SetArrayClass(class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 691 | |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 692 | // Create java.lang.invoke.MethodType.class root |
| 693 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodType;"); |
| 694 | CHECK(class_root != nullptr); |
| 695 | SetClassRoot(kJavaLangInvokeMethodType, class_root); |
| 696 | mirror::MethodType::SetClass(class_root); |
| 697 | |
| 698 | // Create java.lang.invoke.MethodHandleImpl.class root |
| 699 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandleImpl;"); |
| 700 | CHECK(class_root != nullptr); |
| 701 | SetClassRoot(kJavaLangInvokeMethodHandleImpl, class_root); |
| 702 | mirror::MethodHandleImpl::SetClass(class_root); |
| 703 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 704 | // Create java.lang.invoke.MethodHandles.Lookup.class root |
| 705 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandles$Lookup;"); |
| 706 | CHECK(class_root != nullptr); |
| 707 | SetClassRoot(kJavaLangInvokeMethodHandlesLookup, class_root); |
| 708 | mirror::MethodHandlesLookup::SetClass(class_root); |
| 709 | |
| 710 | // Create java.lang.invoke.CallSite.class root |
| 711 | class_root = FindSystemClass(self, "Ljava/lang/invoke/CallSite;"); |
| 712 | CHECK(class_root != nullptr); |
| 713 | SetClassRoot(kJavaLangInvokeCallSite, class_root); |
| 714 | mirror::CallSite::SetClass(class_root); |
| 715 | |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 716 | class_root = FindSystemClass(self, "Ldalvik/system/EmulatedStackFrame;"); |
| 717 | CHECK(class_root != nullptr); |
| 718 | SetClassRoot(kDalvikSystemEmulatedStackFrame, class_root); |
| 719 | mirror::EmulatedStackFrame::SetClass(class_root); |
| 720 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 721 | // 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] | 722 | // finish initializing Reference class |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 723 | mirror::Class::SetStatus(java_lang_ref_Reference, mirror::Class::kStatusNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 724 | CheckSystemClass(self, java_lang_ref_Reference, "Ljava/lang/ref/Reference;"); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 725 | CHECK_EQ(java_lang_ref_Reference->GetObjectSize(), mirror::Reference::InstanceSize()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 726 | CHECK_EQ(java_lang_ref_Reference->GetClassSize(), |
| 727 | mirror::Reference::ClassSize(image_pointer_size_)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 728 | class_root = FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 729 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 730 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagFinalizerReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 731 | class_root = FindSystemClass(self, "Ljava/lang/ref/PhantomReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 732 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 733 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagPhantomReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 734 | class_root = FindSystemClass(self, "Ljava/lang/ref/SoftReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 735 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 736 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagSoftReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 737 | class_root = FindSystemClass(self, "Ljava/lang/ref/WeakReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 738 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 739 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagWeakReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 740 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 741 | // Setup the ClassLoader, verifying the object_size_. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 742 | class_root = FindSystemClass(self, "Ljava/lang/ClassLoader;"); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 743 | class_root->SetClassLoaderClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 744 | CHECK_EQ(class_root->GetObjectSize(), mirror::ClassLoader::InstanceSize()); |
| 745 | SetClassRoot(kJavaLangClassLoader, class_root); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 746 | |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 747 | // Set up java.lang.Throwable, java.lang.ClassNotFoundException, and |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 748 | // java.lang.StackTraceElement as a convenience. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 749 | SetClassRoot(kJavaLangThrowable, FindSystemClass(self, "Ljava/lang/Throwable;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 750 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 751 | SetClassRoot(kJavaLangClassNotFoundException, |
| 752 | FindSystemClass(self, "Ljava/lang/ClassNotFoundException;")); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 753 | SetClassRoot(kJavaLangStackTraceElement, FindSystemClass(self, "Ljava/lang/StackTraceElement;")); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 754 | SetClassRoot(kJavaLangStackTraceElementArrayClass, |
| 755 | FindSystemClass(self, "[Ljava/lang/StackTraceElement;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 756 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 757 | |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 758 | // Create conflict tables that depend on the class linker. |
| 759 | runtime->FixupConflictTables(); |
| 760 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 761 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 762 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 763 | VLOG(startup) << "ClassLinker::InitFromCompiler exiting"; |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 764 | |
| 765 | return true; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 768 | void ClassLinker::FinishInit(Thread* self) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 769 | VLOG(startup) << "ClassLinker::FinishInit entering"; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 770 | |
| 771 | // Let the heap know some key offsets into java.lang.ref instances |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 772 | // Note: we hard code the field indexes here rather than using FindInstanceField |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 773 | // as the types of the field can't be resolved prior to the runtime being |
| 774 | // fully initialized |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 775 | StackHandleScope<2> hs(self); |
| 776 | Handle<mirror::Class> java_lang_ref_Reference = hs.NewHandle(GetClassRoot(kJavaLangRefReference)); |
| 777 | Handle<mirror::Class> java_lang_ref_FinalizerReference = |
| 778 | hs.NewHandle(FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;")); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 779 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 780 | ArtField* pendingNext = java_lang_ref_Reference->GetInstanceField(0); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 781 | CHECK_STREQ(pendingNext->GetName(), "pendingNext"); |
| 782 | CHECK_STREQ(pendingNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 783 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 784 | ArtField* queue = java_lang_ref_Reference->GetInstanceField(1); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 785 | CHECK_STREQ(queue->GetName(), "queue"); |
| 786 | CHECK_STREQ(queue->GetTypeDescriptor(), "Ljava/lang/ref/ReferenceQueue;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 787 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 788 | ArtField* queueNext = java_lang_ref_Reference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 789 | CHECK_STREQ(queueNext->GetName(), "queueNext"); |
| 790 | CHECK_STREQ(queueNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 791 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 792 | ArtField* referent = java_lang_ref_Reference->GetInstanceField(3); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 793 | CHECK_STREQ(referent->GetName(), "referent"); |
| 794 | CHECK_STREQ(referent->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 795 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 796 | ArtField* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 797 | CHECK_STREQ(zombie->GetName(), "zombie"); |
| 798 | CHECK_STREQ(zombie->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 799 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 800 | // ensure all class_roots_ are initialized |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 801 | for (size_t i = 0; i < kClassRootsMax; i++) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 802 | ClassRoot class_root = static_cast<ClassRoot>(i); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 803 | ObjPtr<mirror::Class> klass = GetClassRoot(class_root); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 804 | CHECK(klass != nullptr); |
| 805 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != nullptr); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 806 | // note SetClassRoot does additional validation. |
| 807 | // if possible add new checks there to catch errors early |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 810 | CHECK(!array_iftable_.IsNull()); |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 811 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 812 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 813 | // that Object, Class, and Object[] are setup |
| 814 | init_done_ = true; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 815 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 816 | VLOG(startup) << "ClassLinker::FinishInit exiting"; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 819 | void ClassLinker::RunRootClinits() { |
| 820 | Thread* self = Thread::Current(); |
| 821 | for (size_t i = 0; i < ClassLinker::kClassRootsMax; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 822 | ObjPtr<mirror::Class> c = GetClassRoot(ClassRoot(i)); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 823 | if (!c->IsArrayClass() && !c->IsPrimitive()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 824 | StackHandleScope<1> hs(self); |
| 825 | Handle<mirror::Class> h_class(hs.NewHandle(GetClassRoot(ClassRoot(i)))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 826 | EnsureInitialized(self, h_class, true, true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 827 | self->AssertNoPendingException(); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | } |
| 831 | |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 832 | // Set image methods' entry point to interpreter. |
| 833 | class SetInterpreterEntrypointArtMethodVisitor : public ArtMethodVisitor { |
| 834 | public: |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 835 | explicit SetInterpreterEntrypointArtMethodVisitor(PointerSize image_pointer_size) |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 836 | : image_pointer_size_(image_pointer_size) {} |
| 837 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 838 | void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 839 | if (kIsDebugBuild && !method->IsRuntimeMethod()) { |
| 840 | CHECK(method->GetDeclaringClass() != nullptr); |
| 841 | } |
| 842 | if (!method->IsNative() && !method->IsRuntimeMethod() && !method->IsResolutionMethod()) { |
| 843 | method->SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(), |
| 844 | image_pointer_size_); |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | private: |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 849 | const PointerSize image_pointer_size_; |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 850 | |
| 851 | DISALLOW_COPY_AND_ASSIGN(SetInterpreterEntrypointArtMethodVisitor); |
| 852 | }; |
| 853 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 854 | struct TrampolineCheckData { |
| 855 | const void* quick_resolution_trampoline; |
| 856 | const void* quick_imt_conflict_trampoline; |
| 857 | const void* quick_generic_jni_trampoline; |
| 858 | const void* quick_to_interpreter_bridge_trampoline; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 859 | PointerSize pointer_size; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 860 | ArtMethod* m; |
| 861 | bool error; |
| 862 | }; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 863 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 864 | static void CheckTrampolines(mirror::Object* obj, void* arg) NO_THREAD_SAFETY_ANALYSIS { |
| 865 | if (obj->IsClass()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 866 | ObjPtr<mirror::Class> klass = obj->AsClass(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 867 | TrampolineCheckData* d = reinterpret_cast<TrampolineCheckData*>(arg); |
| 868 | for (ArtMethod& m : klass->GetMethods(d->pointer_size)) { |
| 869 | const void* entrypoint = m.GetEntryPointFromQuickCompiledCodePtrSize(d->pointer_size); |
| 870 | if (entrypoint == d->quick_resolution_trampoline || |
| 871 | entrypoint == d->quick_imt_conflict_trampoline || |
| 872 | entrypoint == d->quick_generic_jni_trampoline || |
| 873 | entrypoint == d->quick_to_interpreter_bridge_trampoline) { |
| 874 | d->m = &m; |
| 875 | d->error = true; |
| 876 | return; |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | } |
| 881 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 882 | bool ClassLinker::InitFromBootImage(std::string* error_msg) { |
| 883 | VLOG(startup) << __FUNCTION__ << " entering"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 884 | CHECK(!init_done_); |
| 885 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 886 | Runtime* const runtime = Runtime::Current(); |
| 887 | Thread* const self = Thread::Current(); |
| 888 | gc::Heap* const heap = runtime->GetHeap(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 889 | std::vector<gc::space::ImageSpace*> spaces = heap->GetBootImageSpaces(); |
| 890 | CHECK(!spaces.empty()); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 891 | uint32_t pointer_size_unchecked = spaces[0]->GetImageHeader().GetPointerSizeUnchecked(); |
| 892 | if (!ValidPointerSize(pointer_size_unchecked)) { |
| 893 | *error_msg = StringPrintf("Invalid image pointer size: %u", pointer_size_unchecked); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 894 | return false; |
| 895 | } |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 896 | image_pointer_size_ = spaces[0]->GetImageHeader().GetPointerSize(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 897 | if (!runtime->IsAotCompiler()) { |
| 898 | // Only the Aot compiler supports having an image with a different pointer size than the |
| 899 | // runtime. This happens on the host for compiling 32 bit tests since we use a 64 bit libart |
| 900 | // 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] | 901 | if (image_pointer_size_ != kRuntimePointerSize) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 902 | *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] | 903 | static_cast<size_t>(image_pointer_size_), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 904 | sizeof(void*)); |
| 905 | return false; |
| 906 | } |
| 907 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 908 | std::vector<const OatFile*> oat_files = |
| 909 | runtime->GetOatFileManager().RegisterImageOatFiles(spaces); |
| 910 | DCHECK(!oat_files.empty()); |
| 911 | const OatHeader& default_oat_header = oat_files[0]->GetOatHeader(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 912 | CHECK_EQ(default_oat_header.GetImageFileLocationOatDataBegin(), 0U); |
| 913 | const char* image_file_location = oat_files[0]->GetOatHeader(). |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 914 | GetStoreValueByKey(OatHeader::kImageLocationKey); |
| 915 | CHECK(image_file_location == nullptr || *image_file_location == 0); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 916 | quick_resolution_trampoline_ = default_oat_header.GetQuickResolutionTrampoline(); |
| 917 | quick_imt_conflict_trampoline_ = default_oat_header.GetQuickImtConflictTrampoline(); |
| 918 | quick_generic_jni_trampoline_ = default_oat_header.GetQuickGenericJniTrampoline(); |
| 919 | quick_to_interpreter_bridge_trampoline_ = default_oat_header.GetQuickToInterpreterBridge(); |
| 920 | if (kIsDebugBuild) { |
| 921 | // Check that the other images use the same trampoline. |
| 922 | for (size_t i = 1; i < oat_files.size(); ++i) { |
| 923 | const OatHeader& ith_oat_header = oat_files[i]->GetOatHeader(); |
| 924 | const void* ith_quick_resolution_trampoline = |
| 925 | ith_oat_header.GetQuickResolutionTrampoline(); |
| 926 | const void* ith_quick_imt_conflict_trampoline = |
| 927 | ith_oat_header.GetQuickImtConflictTrampoline(); |
| 928 | const void* ith_quick_generic_jni_trampoline = |
| 929 | ith_oat_header.GetQuickGenericJniTrampoline(); |
| 930 | const void* ith_quick_to_interpreter_bridge_trampoline = |
| 931 | ith_oat_header.GetQuickToInterpreterBridge(); |
| 932 | if (ith_quick_resolution_trampoline != quick_resolution_trampoline_ || |
| 933 | ith_quick_imt_conflict_trampoline != quick_imt_conflict_trampoline_ || |
| 934 | ith_quick_generic_jni_trampoline != quick_generic_jni_trampoline_ || |
| 935 | ith_quick_to_interpreter_bridge_trampoline != quick_to_interpreter_bridge_trampoline_) { |
| 936 | // Make sure that all methods in this image do not contain those trampolines as |
| 937 | // entrypoints. Otherwise the class-linker won't be able to work with a single set. |
| 938 | TrampolineCheckData data; |
| 939 | data.error = false; |
| 940 | data.pointer_size = GetImagePointerSize(); |
| 941 | data.quick_resolution_trampoline = ith_quick_resolution_trampoline; |
| 942 | data.quick_imt_conflict_trampoline = ith_quick_imt_conflict_trampoline; |
| 943 | data.quick_generic_jni_trampoline = ith_quick_generic_jni_trampoline; |
| 944 | data.quick_to_interpreter_bridge_trampoline = ith_quick_to_interpreter_bridge_trampoline; |
| 945 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 946 | spaces[i]->GetLiveBitmap()->Walk(CheckTrampolines, &data); |
| 947 | if (data.error) { |
| 948 | ArtMethod* m = data.m; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 949 | LOG(ERROR) << "Found a broken ArtMethod: " << ArtMethod::PrettyMethod(m); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 950 | *error_msg = "Found an ArtMethod with a bad entrypoint"; |
| 951 | return false; |
| 952 | } |
| 953 | } |
| 954 | } |
| 955 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 956 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 957 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
| 958 | down_cast<mirror::ObjectArray<mirror::Class>*>( |
| 959 | spaces[0]->GetImageHeader().GetImageRoot(ImageHeader::kClassRoots))); |
| 960 | mirror::Class::SetClassClass(class_roots_.Read()->Get(kJavaLangClass)); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 961 | |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 962 | // Special case of setting up the String class early so that we can test arbitrary objects |
| 963 | // as being Strings or not |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 964 | mirror::String::SetClass(GetClassRoot(kJavaLangString)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 965 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 966 | ObjPtr<mirror::Class> java_lang_Object = GetClassRoot(kJavaLangObject); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 967 | java_lang_Object->SetObjectSize(sizeof(mirror::Object)); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 968 | // Allocate in non-movable so that it's possible to check if a JNI weak global ref has been |
| 969 | // cleared without triggering the read barrier and unintentionally mark the sentinel alive. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 970 | runtime->SetSentinel(heap->AllocNonMovableObject<true>( |
| 971 | self, java_lang_Object, java_lang_Object->GetObjectSize(), VoidFunctor())); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 972 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 973 | // reinit array_iftable_ from any array class instance, they should be == |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 974 | array_iftable_ = GcRoot<mirror::IfTable>(GetClassRoot(kObjectArrayClass)->GetIfTable()); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 975 | DCHECK_EQ(array_iftable_.Read(), GetClassRoot(kBooleanArrayClass)->GetIfTable()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 976 | // String class root was set above |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 977 | mirror::Field::SetClass(GetClassRoot(kJavaLangReflectField)); |
| 978 | mirror::Field::SetArrayClass(GetClassRoot(kJavaLangReflectFieldArrayClass)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 979 | mirror::Constructor::SetClass(GetClassRoot(kJavaLangReflectConstructor)); |
| 980 | mirror::Constructor::SetArrayClass(GetClassRoot(kJavaLangReflectConstructorArrayClass)); |
| 981 | mirror::Method::SetClass(GetClassRoot(kJavaLangReflectMethod)); |
| 982 | mirror::Method::SetArrayClass(GetClassRoot(kJavaLangReflectMethodArrayClass)); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 983 | mirror::MethodType::SetClass(GetClassRoot(kJavaLangInvokeMethodType)); |
| 984 | mirror::MethodHandleImpl::SetClass(GetClassRoot(kJavaLangInvokeMethodHandleImpl)); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 985 | mirror::MethodHandlesLookup::SetClass(GetClassRoot(kJavaLangInvokeMethodHandlesLookup)); |
| 986 | mirror::CallSite::SetClass(GetClassRoot(kJavaLangInvokeCallSite)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 987 | mirror::Reference::SetClass(GetClassRoot(kJavaLangRefReference)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 988 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
| 989 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
| 990 | mirror::CharArray::SetArrayClass(GetClassRoot(kCharArrayClass)); |
| 991 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
| 992 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
| 993 | mirror::IntArray::SetArrayClass(GetClassRoot(kIntArrayClass)); |
| 994 | mirror::LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
| 995 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
| 996 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
| 997 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 998 | mirror::EmulatedStackFrame::SetClass(GetClassRoot(kDalvikSystemEmulatedStackFrame)); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 999 | mirror::ClassExt::SetClass(GetClassRoot(kDalvikSystemClassExt)); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1000 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1001 | for (gc::space::ImageSpace* image_space : spaces) { |
| 1002 | // Boot class loader, use a null handle. |
| 1003 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1004 | if (!AddImageSpace(image_space, |
| 1005 | ScopedNullHandle<mirror::ClassLoader>(), |
| 1006 | /*dex_elements*/nullptr, |
| 1007 | /*dex_location*/nullptr, |
| 1008 | /*out*/&dex_files, |
| 1009 | error_msg)) { |
| 1010 | return false; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1011 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1012 | // Append opened dex files at the end. |
| 1013 | boot_dex_files_.insert(boot_dex_files_.end(), |
| 1014 | std::make_move_iterator(dex_files.begin()), |
| 1015 | std::make_move_iterator(dex_files.end())); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 1016 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1017 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1018 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1019 | VLOG(startup) << __FUNCTION__ << " exiting"; |
| 1020 | return true; |
| 1021 | } |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 1022 | |
Jeff Hao | 5872d7c | 2016-04-27 11:07:41 -0700 | [diff] [blame] | 1023 | bool ClassLinker::IsBootClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1024 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1025 | return class_loader == nullptr || |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1026 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader) == |
| 1027 | class_loader->GetClass(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1028 | } |
| 1029 | |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1030 | static bool GetDexPathListElementName(ObjPtr<mirror::Object> element, |
| 1031 | ObjPtr<mirror::String>* out_name) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1032 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1033 | ArtField* const dex_file_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 1034 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1035 | ArtField* const dex_file_name_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 1036 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_fileName); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1037 | DCHECK(dex_file_field != nullptr); |
| 1038 | DCHECK(dex_file_name_field != nullptr); |
| 1039 | DCHECK(element != nullptr); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1040 | CHECK_EQ(dex_file_field->GetDeclaringClass(), element->GetClass()) << element->PrettyTypeOf(); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1041 | ObjPtr<mirror::Object> dex_file = dex_file_field->GetObject(element); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1042 | if (dex_file == nullptr) { |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1043 | // Null dex file means it was probably a jar with no dex files, return a null string. |
| 1044 | *out_name = nullptr; |
| 1045 | return true; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1046 | } |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1047 | ObjPtr<mirror::Object> name_object = dex_file_name_field->GetObject(dex_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1048 | if (name_object != nullptr) { |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1049 | *out_name = name_object->AsString(); |
| 1050 | return true; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1051 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1052 | return false; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1053 | } |
| 1054 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1055 | static bool FlattenPathClassLoader(ObjPtr<mirror::ClassLoader> class_loader, |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1056 | std::list<ObjPtr<mirror::String>>* out_dex_file_names, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1057 | std::string* error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1058 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1059 | DCHECK(out_dex_file_names != nullptr); |
| 1060 | DCHECK(error_msg != nullptr); |
| 1061 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1062 | ArtField* const dex_path_list_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 1063 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1064 | ArtField* const dex_elements_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 1065 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1066 | CHECK(dex_path_list_field != nullptr); |
| 1067 | CHECK(dex_elements_field != nullptr); |
Jeff Hao | 5872d7c | 2016-04-27 11:07:41 -0700 | [diff] [blame] | 1068 | while (!ClassLinker::IsBootClassLoader(soa, class_loader)) { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1069 | if (soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader) != |
| 1070 | class_loader->GetClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1071 | *error_msg = StringPrintf("Unknown class loader type %s", |
| 1072 | class_loader->PrettyTypeOf().c_str()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1073 | // Unsupported class loader. |
| 1074 | return false; |
| 1075 | } |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1076 | 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] | 1077 | if (dex_path_list != nullptr) { |
| 1078 | // 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] | 1079 | 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] | 1080 | // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look |
| 1081 | // at the mCookie which is a DexFile vector. |
| 1082 | if (dex_elements_obj != nullptr) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1083 | ObjPtr<mirror::ObjectArray<mirror::Object>> dex_elements = |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1084 | dex_elements_obj->AsObjectArray<mirror::Object>(); |
| 1085 | // Reverse order since we insert the parent at the front. |
| 1086 | for (int32_t i = dex_elements->GetLength() - 1; i >= 0; --i) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1087 | ObjPtr<mirror::Object> element = dex_elements->GetWithoutChecks(i); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1088 | if (element == nullptr) { |
| 1089 | *error_msg = StringPrintf("Null dex element at index %d", i); |
| 1090 | return false; |
| 1091 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1092 | ObjPtr<mirror::String> name; |
| 1093 | if (!GetDexPathListElementName(element, &name)) { |
| 1094 | *error_msg = StringPrintf("Invalid dex path list element at index %d", i); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1095 | return false; |
| 1096 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1097 | if (name != nullptr) { |
| 1098 | out_dex_file_names->push_front(name.Ptr()); |
| 1099 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | class_loader = class_loader->GetParent(); |
| 1104 | } |
| 1105 | return true; |
| 1106 | } |
| 1107 | |
| 1108 | class FixupArtMethodArrayVisitor : public ArtMethodVisitor { |
| 1109 | public: |
| 1110 | explicit FixupArtMethodArrayVisitor(const ImageHeader& header) : header_(header) {} |
| 1111 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1112 | virtual void Visit(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 1113 | const bool is_copied = method->IsCopied(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1114 | ArtMethod** resolved_methods = method->GetDexCacheResolvedMethods(kRuntimePointerSize); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1115 | if (resolved_methods != nullptr) { |
| 1116 | bool in_image_space = false; |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 1117 | if (kIsDebugBuild || is_copied) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1118 | in_image_space = header_.GetImageSection(ImageHeader::kSectionDexCacheArrays).Contains( |
| 1119 | reinterpret_cast<const uint8_t*>(resolved_methods) - header_.GetImageBegin()); |
| 1120 | } |
| 1121 | // Must be in image space for non-miranda method. |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 1122 | DCHECK(is_copied || in_image_space) |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1123 | << resolved_methods << " is not in image starting at " |
| 1124 | << reinterpret_cast<void*>(header_.GetImageBegin()); |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 1125 | if (!is_copied || in_image_space) { |
Vladimir Marko | 6dd1488 | 2016-10-25 11:51:35 +0100 | [diff] [blame] | 1126 | method->SetDexCacheResolvedMethods(method->GetDexCache()->GetResolvedMethods(), |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1127 | kRuntimePointerSize); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1128 | } |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | private: |
| 1133 | const ImageHeader& header_; |
| 1134 | }; |
| 1135 | |
| 1136 | class VerifyClassInTableArtMethodVisitor : public ArtMethodVisitor { |
| 1137 | public: |
| 1138 | explicit VerifyClassInTableArtMethodVisitor(ClassTable* table) : table_(table) {} |
| 1139 | |
| 1140 | virtual void Visit(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1141 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::classlinker_classes_lock_) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1142 | ObjPtr<mirror::Class> klass = method->GetDeclaringClass(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1143 | if (klass != nullptr && !Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1144 | CHECK_EQ(table_->LookupByDescriptor(klass), klass) << mirror::Class::PrettyClass(klass); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | private: |
| 1149 | ClassTable* const table_; |
| 1150 | }; |
| 1151 | |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1152 | class VerifyDirectInterfacesInTableClassVisitor { |
| 1153 | public: |
| 1154 | explicit VerifyDirectInterfacesInTableClassVisitor(ObjPtr<mirror::ClassLoader> class_loader) |
| 1155 | : class_loader_(class_loader), self_(Thread::Current()) { } |
| 1156 | |
| 1157 | bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1158 | if (!klass->IsPrimitive() && klass->GetClassLoader() == class_loader_) { |
| 1159 | classes_.push_back(klass); |
| 1160 | } |
| 1161 | return true; |
| 1162 | } |
| 1163 | |
| 1164 | void Check() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1165 | for (ObjPtr<mirror::Class> klass : classes_) { |
| 1166 | for (uint32_t i = 0, num = klass->NumDirectInterfaces(); i != num; ++i) { |
| 1167 | CHECK(klass->GetDirectInterface(self_, klass, i) != nullptr) |
| 1168 | << klass->PrettyDescriptor() << " iface #" << i; |
| 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | private: |
| 1174 | ObjPtr<mirror::ClassLoader> class_loader_; |
| 1175 | Thread* self_; |
| 1176 | std::vector<ObjPtr<mirror::Class>> classes_; |
| 1177 | }; |
| 1178 | |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1179 | class VerifyDeclaringClassVisitor : public ArtMethodVisitor { |
| 1180 | public: |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1181 | VerifyDeclaringClassVisitor() REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1182 | : live_bitmap_(Runtime::Current()->GetHeap()->GetLiveBitmap()) {} |
| 1183 | |
| 1184 | virtual void Visit(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1185 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1186 | ObjPtr<mirror::Class> klass = method->GetDeclaringClassUnchecked(); |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1187 | if (klass != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1188 | CHECK(live_bitmap_->Test(klass.Ptr())) << "Image method has unmarked declaring class"; |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | private: |
| 1193 | gc::accounting::HeapBitmap* const live_bitmap_; |
| 1194 | }; |
| 1195 | |
Artem Udovichenko | b3ac45b | 2016-09-09 14:02:25 +0300 | [diff] [blame] | 1196 | // Copies data from one array to another array at the same position |
| 1197 | // if pred returns false. If there is a page of continuous data in |
| 1198 | // the src array for which pred consistently returns true then |
| 1199 | // corresponding page in the dst array will not be touched. |
| 1200 | // This should reduce number of allocated physical pages. |
| 1201 | template <class T, class NullPred> |
| 1202 | static void CopyNonNull(const T* src, size_t count, T* dst, const NullPred& pred) { |
| 1203 | for (size_t i = 0; i < count; ++i) { |
| 1204 | if (!pred(src[i])) { |
| 1205 | dst[i] = src[i]; |
| 1206 | } |
| 1207 | } |
| 1208 | } |
| 1209 | |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1210 | template <typename T> |
| 1211 | static void CopyDexCachePairs(const std::atomic<mirror::DexCachePair<T>>* src, |
| 1212 | size_t count, |
| 1213 | std::atomic<mirror::DexCachePair<T>>* dst) { |
| 1214 | DCHECK_NE(count, 0u); |
| 1215 | DCHECK(!src[0].load(std::memory_order_relaxed).object.IsNull() || |
| 1216 | src[0].load(std::memory_order_relaxed).index != 0u); |
| 1217 | for (size_t i = 0; i < count; ++i) { |
| 1218 | DCHECK_EQ(dst[i].load(std::memory_order_relaxed).index, 0u); |
| 1219 | DCHECK(dst[i].load(std::memory_order_relaxed).object.IsNull()); |
| 1220 | mirror::DexCachePair<T> source = src[i].load(std::memory_order_relaxed); |
| 1221 | if (source.index != 0u || !source.object.IsNull()) { |
| 1222 | dst[i].store(source, std::memory_order_relaxed); |
| 1223 | } |
| 1224 | } |
| 1225 | } |
| 1226 | |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1227 | // new_class_set is the set of classes that were read from the class table section in the image. |
| 1228 | // If there was no class table section, it is null. |
| 1229 | // Note: using a class here to avoid having to make ClassLinker internals public. |
| 1230 | class AppImageClassLoadersAndDexCachesHelper { |
| 1231 | public: |
| 1232 | static bool Update( |
| 1233 | ClassLinker* class_linker, |
| 1234 | gc::space::ImageSpace* space, |
| 1235 | Handle<mirror::ClassLoader> class_loader, |
| 1236 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches, |
| 1237 | ClassTable::ClassSet* new_class_set, |
| 1238 | bool* out_forward_dex_cache_array, |
| 1239 | std::string* out_error_msg) |
| 1240 | REQUIRES(!Locks::dex_lock_) |
| 1241 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 1242 | }; |
| 1243 | |
| 1244 | bool AppImageClassLoadersAndDexCachesHelper::Update( |
| 1245 | ClassLinker* class_linker, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1246 | gc::space::ImageSpace* space, |
| 1247 | Handle<mirror::ClassLoader> class_loader, |
| 1248 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches, |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1249 | ClassTable::ClassSet* new_class_set, |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1250 | bool* out_forward_dex_cache_array, |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1251 | std::string* out_error_msg) |
| 1252 | REQUIRES(!Locks::dex_lock_) |
| 1253 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1254 | DCHECK(out_forward_dex_cache_array != nullptr); |
| 1255 | DCHECK(out_error_msg != nullptr); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1256 | PointerSize image_pointer_size = class_linker->GetImagePointerSize(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1257 | Thread* const self = Thread::Current(); |
| 1258 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
| 1259 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1260 | { |
| 1261 | // Add image classes into the class table for the class loader, and fixup the dex caches and |
| 1262 | // class loader fields. |
| 1263 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1264 | // Dex cache array fixup is all or nothing, we must reject app images that have mixed since we |
| 1265 | // rely on clobering the dex cache arrays in the image to forward to bss. |
| 1266 | size_t num_dex_caches_with_bss_arrays = 0; |
| 1267 | const size_t num_dex_caches = dex_caches->GetLength(); |
| 1268 | for (size_t i = 0; i < num_dex_caches; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1269 | ObjPtr<mirror::DexCache> const dex_cache = dex_caches->Get(i); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1270 | const DexFile* const dex_file = dex_cache->GetDexFile(); |
| 1271 | const OatFile::OatDexFile* oat_dex_file = dex_file->GetOatDexFile(); |
| 1272 | if (oat_dex_file != nullptr && oat_dex_file->GetDexCacheArrays() != nullptr) { |
| 1273 | ++num_dex_caches_with_bss_arrays; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1274 | } |
| 1275 | } |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1276 | *out_forward_dex_cache_array = num_dex_caches_with_bss_arrays != 0; |
| 1277 | if (*out_forward_dex_cache_array) { |
| 1278 | if (num_dex_caches_with_bss_arrays != num_dex_caches) { |
| 1279 | // Reject application image since we cannot forward only some of the dex cache arrays. |
| 1280 | // TODO: We could get around this by having a dedicated forwarding slot. It should be an |
| 1281 | // uncommon case. |
| 1282 | *out_error_msg = StringPrintf("Dex caches in bss does not match total: %zu vs %zu", |
| 1283 | num_dex_caches_with_bss_arrays, |
| 1284 | num_dex_caches); |
| 1285 | return false; |
| 1286 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1287 | } |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1288 | // Only add the classes to the class loader after the points where we can return false. |
| 1289 | for (size_t i = 0; i < num_dex_caches; i++) { |
Vladimir Marko | 1bc4b17 | 2016-10-24 16:53:39 +0000 | [diff] [blame] | 1290 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1291 | const DexFile* const dex_file = dex_cache->GetDexFile(); |
| 1292 | const OatFile::OatDexFile* oat_dex_file = dex_file->GetOatDexFile(); |
| 1293 | if (oat_dex_file != nullptr && oat_dex_file->GetDexCacheArrays() != nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1294 | // 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] | 1295 | // copy over the arrays. |
| 1296 | DCHECK(dex_file != nullptr); |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 1297 | size_t num_strings = mirror::DexCache::kDexCacheStringCacheSize; |
| 1298 | if (dex_file->NumStringIds() < num_strings) { |
| 1299 | num_strings = dex_file->NumStringIds(); |
| 1300 | } |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1301 | size_t num_types = mirror::DexCache::kDexCacheTypeCacheSize; |
| 1302 | if (dex_file->NumTypeIds() < num_types) { |
| 1303 | num_types = dex_file->NumTypeIds(); |
| 1304 | } |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1305 | const size_t num_methods = dex_file->NumMethodIds(); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1306 | size_t num_fields = mirror::DexCache::kDexCacheFieldCacheSize; |
| 1307 | if (dex_file->NumFieldIds() < num_fields) { |
| 1308 | num_fields = dex_file->NumFieldIds(); |
| 1309 | } |
Narayan Kamath | 269cb43 | 2016-10-28 10:19:54 +0100 | [diff] [blame] | 1310 | size_t num_method_types = mirror::DexCache::kDexCacheMethodTypeCacheSize; |
| 1311 | if (dex_file->NumProtoIds() < num_method_types) { |
| 1312 | num_method_types = dex_file->NumProtoIds(); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1313 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1314 | const size_t num_call_sites = dex_file->NumCallSiteIds(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1315 | CHECK_EQ(num_strings, dex_cache->NumStrings()); |
| 1316 | CHECK_EQ(num_types, dex_cache->NumResolvedTypes()); |
| 1317 | CHECK_EQ(num_methods, dex_cache->NumResolvedMethods()); |
| 1318 | CHECK_EQ(num_fields, dex_cache->NumResolvedFields()); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1319 | CHECK_EQ(num_method_types, dex_cache->NumResolvedMethodTypes()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1320 | CHECK_EQ(num_call_sites, dex_cache->NumResolvedCallSites()); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1321 | DexCacheArraysLayout layout(image_pointer_size, dex_file); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1322 | uint8_t* const raw_arrays = oat_dex_file->GetDexCacheArrays(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1323 | if (num_strings != 0u) { |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 1324 | mirror::StringDexCacheType* const image_resolved_strings = dex_cache->GetStrings(); |
| 1325 | mirror::StringDexCacheType* const strings = |
| 1326 | reinterpret_cast<mirror::StringDexCacheType*>(raw_arrays + layout.StringsOffset()); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1327 | CopyDexCachePairs(image_resolved_strings, num_strings, strings); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1328 | dex_cache->SetStrings(strings); |
| 1329 | } |
| 1330 | if (num_types != 0u) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1331 | mirror::TypeDexCacheType* const image_resolved_types = dex_cache->GetResolvedTypes(); |
| 1332 | mirror::TypeDexCacheType* const types = |
| 1333 | reinterpret_cast<mirror::TypeDexCacheType*>(raw_arrays + layout.TypesOffset()); |
| 1334 | CopyDexCachePairs(image_resolved_types, num_types, types); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1335 | dex_cache->SetResolvedTypes(types); |
| 1336 | } |
| 1337 | if (num_methods != 0u) { |
| 1338 | ArtMethod** const methods = reinterpret_cast<ArtMethod**>( |
| 1339 | raw_arrays + layout.MethodsOffset()); |
| 1340 | ArtMethod** const image_resolved_methods = dex_cache->GetResolvedMethods(); |
| 1341 | for (size_t j = 0; kIsDebugBuild && j < num_methods; ++j) { |
| 1342 | DCHECK(methods[j] == nullptr); |
| 1343 | } |
Artem Udovichenko | b3ac45b | 2016-09-09 14:02:25 +0300 | [diff] [blame] | 1344 | CopyNonNull(image_resolved_methods, |
| 1345 | num_methods, |
| 1346 | methods, |
| 1347 | [] (const ArtMethod* method) { |
| 1348 | return method == nullptr; |
| 1349 | }); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1350 | dex_cache->SetResolvedMethods(methods); |
| 1351 | } |
| 1352 | if (num_fields != 0u) { |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1353 | mirror::FieldDexCacheType* const image_resolved_fields = dex_cache->GetResolvedFields(); |
| 1354 | mirror::FieldDexCacheType* const fields = |
| 1355 | reinterpret_cast<mirror::FieldDexCacheType*>(raw_arrays + layout.FieldsOffset()); |
| 1356 | for (size_t j = 0; j < num_fields; ++j) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1357 | DCHECK_EQ(mirror::DexCache::GetNativePairPtrSize(fields, j, image_pointer_size).index, |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1358 | 0u); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1359 | DCHECK(mirror::DexCache::GetNativePairPtrSize(fields, j, image_pointer_size).object == |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1360 | nullptr); |
| 1361 | mirror::DexCache::SetNativePairPtrSize( |
| 1362 | fields, |
| 1363 | j, |
| 1364 | mirror::DexCache::GetNativePairPtrSize(image_resolved_fields, |
| 1365 | j, |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1366 | image_pointer_size), |
| 1367 | image_pointer_size); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1368 | } |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1369 | dex_cache->SetResolvedFields(fields); |
| 1370 | } |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1371 | if (num_method_types != 0u) { |
| 1372 | // NOTE: We currently (Sep 2016) do not resolve any method types at |
| 1373 | // compile time, but plan to in the future. This code exists for the |
| 1374 | // sake of completeness. |
| 1375 | mirror::MethodTypeDexCacheType* const image_resolved_method_types = |
| 1376 | dex_cache->GetResolvedMethodTypes(); |
| 1377 | mirror::MethodTypeDexCacheType* const method_types = |
| 1378 | reinterpret_cast<mirror::MethodTypeDexCacheType*>( |
| 1379 | raw_arrays + layout.MethodTypesOffset()); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1380 | CopyDexCachePairs(image_resolved_method_types, num_method_types, method_types); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1381 | dex_cache->SetResolvedMethodTypes(method_types); |
| 1382 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1383 | if (num_call_sites != 0u) { |
| 1384 | GcRoot<mirror::CallSite>* const image_resolved_call_sites = |
| 1385 | dex_cache->GetResolvedCallSites(); |
| 1386 | GcRoot<mirror::CallSite>* const call_sites = |
| 1387 | reinterpret_cast<GcRoot<mirror::CallSite>*>(raw_arrays + layout.CallSitesOffset()); |
| 1388 | for (size_t j = 0; kIsDebugBuild && j < num_call_sites; ++j) { |
| 1389 | DCHECK(call_sites[j].IsNull()); |
| 1390 | } |
| 1391 | CopyNonNull(image_resolved_call_sites, |
| 1392 | num_call_sites, |
| 1393 | call_sites, |
| 1394 | [](const GcRoot<mirror::CallSite>& elem) { |
| 1395 | return elem.IsNull(); |
| 1396 | }); |
| 1397 | dex_cache->SetResolvedCallSites(call_sites); |
| 1398 | } |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1399 | } |
| 1400 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 1401 | WriterMutexLock mu2(self, *Locks::dex_lock_); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1402 | // Make sure to do this after we update the arrays since we store the resolved types array |
| 1403 | // in DexCacheData in RegisterDexFileLocked. We need the array pointer to be the one in the |
| 1404 | // BSS. |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1405 | CHECK(!class_linker->FindDexCacheDataLocked(*dex_file).IsValid()); |
| 1406 | class_linker->RegisterDexFileLocked(*dex_file, dex_cache, class_loader.Get()); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1407 | } |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1408 | if (kIsDebugBuild) { |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1409 | CHECK(new_class_set != nullptr); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1410 | mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes(); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1411 | const size_t num_types = dex_cache->NumResolvedTypes(); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1412 | for (size_t j = 0; j != num_types; ++j) { |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1413 | // 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] | 1414 | 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] | 1415 | if (space->HasAddress(klass.Ptr())) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 1416 | DCHECK(!klass->IsErroneous()) << klass->GetStatus(); |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 1417 | auto it = new_class_set->Find(ClassTable::TableSlot(klass)); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1418 | DCHECK(it != new_class_set->end()); |
| 1419 | DCHECK_EQ(it->Read(), klass); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1420 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1421 | if (super_class != nullptr && !heap->ObjectIsInBootImageSpace(super_class)) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 1422 | auto it2 = new_class_set->Find(ClassTable::TableSlot(super_class)); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1423 | DCHECK(it2 != new_class_set->end()); |
| 1424 | DCHECK_EQ(it2->Read(), super_class); |
| 1425 | } |
| 1426 | for (ArtMethod& m : klass->GetDirectMethods(kRuntimePointerSize)) { |
| 1427 | const void* code = m.GetEntryPointFromQuickCompiledCode(); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1428 | const void* oat_code = m.IsInvokable() ? class_linker->GetQuickOatCodeFor(&m) : code; |
| 1429 | if (!class_linker->IsQuickResolutionStub(code) && |
| 1430 | !class_linker->IsQuickGenericJniStub(code) && |
| 1431 | !class_linker->IsQuickToInterpreterBridge(code) && |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1432 | !m.IsNative()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1433 | DCHECK_EQ(code, oat_code) << m.PrettyMethod(); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1434 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1435 | } |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1436 | for (ArtMethod& m : klass->GetVirtualMethods(kRuntimePointerSize)) { |
| 1437 | const void* code = m.GetEntryPointFromQuickCompiledCode(); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1438 | const void* oat_code = m.IsInvokable() ? class_linker->GetQuickOatCodeFor(&m) : code; |
| 1439 | if (!class_linker->IsQuickResolutionStub(code) && |
| 1440 | !class_linker->IsQuickGenericJniStub(code) && |
| 1441 | !class_linker->IsQuickToInterpreterBridge(code) && |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1442 | !m.IsNative()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1443 | DCHECK_EQ(code, oat_code) << m.PrettyMethod(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1444 | } |
| 1445 | } |
| 1446 | } |
| 1447 | } |
| 1448 | } |
| 1449 | } |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1450 | } |
| 1451 | if (*out_forward_dex_cache_array) { |
| 1452 | ScopedTrace timing("Fixup ArtMethod dex cache arrays"); |
| 1453 | FixupArtMethodArrayVisitor visitor(header); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1454 | header.VisitPackedArtMethods(&visitor, space->Begin(), kRuntimePointerSize); |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1455 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader.Get()); |
| 1456 | } |
| 1457 | if (kVerifyArtMethodDeclaringClasses) { |
| 1458 | ScopedTrace timing("Verify declaring classes"); |
| 1459 | ReaderMutexLock rmu(self, *Locks::heap_bitmap_lock_); |
| 1460 | VerifyDeclaringClassVisitor visitor; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1461 | header.VisitPackedArtMethods(&visitor, space->Begin(), kRuntimePointerSize); |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1462 | } |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1463 | return true; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1464 | } |
| 1465 | |
Nicolas Geoffray | f9bf250 | 2016-12-14 14:59:04 +0000 | [diff] [blame] | 1466 | // Update the class loader. Should only be used on classes in the image space. |
| 1467 | class UpdateClassLoaderVisitor { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1468 | public: |
Nicolas Geoffray | f9bf250 | 2016-12-14 14:59:04 +0000 | [diff] [blame] | 1469 | UpdateClassLoaderVisitor(gc::space::ImageSpace* space, ObjPtr<mirror::ClassLoader> class_loader) |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1470 | : space_(space), |
Nicolas Geoffray | f9bf250 | 2016-12-14 14:59:04 +0000 | [diff] [blame] | 1471 | class_loader_(class_loader) {} |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1472 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1473 | bool operator()(ObjPtr<mirror::Class> klass) const REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 1474 | // Do not update class loader for boot image classes where the app image |
| 1475 | // class loader is only the initiating loader but not the defining loader. |
| 1476 | if (klass->GetClassLoader() != nullptr) { |
| 1477 | klass->SetClassLoader(class_loader_); |
| 1478 | } |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1479 | return true; |
| 1480 | } |
| 1481 | |
| 1482 | gc::space::ImageSpace* const space_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1483 | ObjPtr<mirror::ClassLoader> const class_loader_; |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1484 | }; |
| 1485 | |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1486 | static std::unique_ptr<const DexFile> OpenOatDexFile(const OatFile* oat_file, |
| 1487 | const char* location, |
| 1488 | std::string* error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1489 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1490 | DCHECK(error_msg != nullptr); |
| 1491 | std::unique_ptr<const DexFile> dex_file; |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 1492 | 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] | 1493 | if (oat_dex_file == nullptr) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1494 | return std::unique_ptr<const DexFile>(); |
| 1495 | } |
| 1496 | std::string inner_error_msg; |
| 1497 | dex_file = oat_dex_file->OpenDexFile(&inner_error_msg); |
| 1498 | if (dex_file == nullptr) { |
| 1499 | *error_msg = StringPrintf("Failed to open dex file %s from within oat file %s error '%s'", |
| 1500 | location, |
| 1501 | oat_file->GetLocation().c_str(), |
| 1502 | inner_error_msg.c_str()); |
| 1503 | return std::unique_ptr<const DexFile>(); |
| 1504 | } |
| 1505 | |
| 1506 | if (dex_file->GetLocationChecksum() != oat_dex_file->GetDexFileLocationChecksum()) { |
| 1507 | *error_msg = StringPrintf("Checksums do not match for %s: %x vs %x", |
| 1508 | location, |
| 1509 | dex_file->GetLocationChecksum(), |
| 1510 | oat_dex_file->GetDexFileLocationChecksum()); |
| 1511 | return std::unique_ptr<const DexFile>(); |
| 1512 | } |
| 1513 | return dex_file; |
| 1514 | } |
| 1515 | |
| 1516 | bool ClassLinker::OpenImageDexFiles(gc::space::ImageSpace* space, |
| 1517 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 1518 | std::string* error_msg) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 1519 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1520 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1521 | ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1522 | DCHECK(dex_caches_object != nullptr); |
| 1523 | mirror::ObjectArray<mirror::DexCache>* dex_caches = |
| 1524 | dex_caches_object->AsObjectArray<mirror::DexCache>(); |
| 1525 | const OatFile* oat_file = space->GetOatFile(); |
| 1526 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1527 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1528 | std::string dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
| 1529 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 1530 | dex_file_location.c_str(), |
| 1531 | error_msg); |
| 1532 | if (dex_file == nullptr) { |
| 1533 | return false; |
| 1534 | } |
| 1535 | dex_cache->SetDexFile(dex_file.get()); |
| 1536 | out_dex_files->push_back(std::move(dex_file)); |
| 1537 | } |
| 1538 | return true; |
| 1539 | } |
| 1540 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1541 | // Helper class for ArtMethod checks when adding an image. Keeps all required functionality |
| 1542 | // together and caches some intermediate results. |
| 1543 | class ImageSanityChecks FINAL { |
| 1544 | public: |
| 1545 | static void CheckObjects(gc::Heap* heap, ClassLinker* class_linker) |
| 1546 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1547 | ImageSanityChecks isc(heap, class_linker); |
| 1548 | heap->VisitObjects(ImageSanityChecks::SanityCheckObjectsCallback, &isc); |
| 1549 | } |
| 1550 | |
| 1551 | static void CheckPointerArray(gc::Heap* heap, |
| 1552 | ClassLinker* class_linker, |
| 1553 | ArtMethod** arr, |
| 1554 | size_t size) |
| 1555 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1556 | ImageSanityChecks isc(heap, class_linker); |
| 1557 | isc.SanityCheckArtMethodPointerArray(arr, size); |
| 1558 | } |
| 1559 | |
| 1560 | static void SanityCheckObjectsCallback(mirror::Object* obj, void* arg) |
| 1561 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1562 | DCHECK(obj != nullptr); |
| 1563 | CHECK(obj->GetClass() != nullptr) << "Null class in object " << obj; |
| 1564 | CHECK(obj->GetClass()->GetClass() != nullptr) << "Null class class " << obj; |
| 1565 | if (obj->IsClass()) { |
| 1566 | ImageSanityChecks* isc = reinterpret_cast<ImageSanityChecks*>(arg); |
| 1567 | |
| 1568 | auto klass = obj->AsClass(); |
| 1569 | for (ArtField& field : klass->GetIFields()) { |
| 1570 | CHECK_EQ(field.GetDeclaringClass(), klass); |
| 1571 | } |
| 1572 | for (ArtField& field : klass->GetSFields()) { |
| 1573 | CHECK_EQ(field.GetDeclaringClass(), klass); |
| 1574 | } |
| 1575 | const auto pointer_size = isc->pointer_size_; |
| 1576 | for (auto& m : klass->GetMethods(pointer_size)) { |
| 1577 | isc->SanityCheckArtMethod(&m, klass); |
| 1578 | } |
| 1579 | auto* vtable = klass->GetVTable(); |
| 1580 | if (vtable != nullptr) { |
| 1581 | isc->SanityCheckArtMethodPointerArray(vtable, nullptr); |
| 1582 | } |
| 1583 | if (klass->ShouldHaveImt()) { |
| 1584 | ImTable* imt = klass->GetImt(pointer_size); |
| 1585 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 1586 | isc->SanityCheckArtMethod(imt->Get(i, pointer_size), nullptr); |
| 1587 | } |
| 1588 | } |
| 1589 | if (klass->ShouldHaveEmbeddedVTable()) { |
| 1590 | for (int32_t i = 0; i < klass->GetEmbeddedVTableLength(); ++i) { |
| 1591 | isc->SanityCheckArtMethod(klass->GetEmbeddedVTableEntry(i, pointer_size), nullptr); |
| 1592 | } |
| 1593 | } |
| 1594 | mirror::IfTable* iftable = klass->GetIfTable(); |
| 1595 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 1596 | if (iftable->GetMethodArrayCount(i) > 0) { |
| 1597 | isc->SanityCheckArtMethodPointerArray(iftable->GetMethodArray(i), nullptr); |
| 1598 | } |
| 1599 | } |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | private: |
| 1604 | ImageSanityChecks(gc::Heap* heap, ClassLinker* class_linker) |
| 1605 | : spaces_(heap->GetBootImageSpaces()), |
| 1606 | pointer_size_(class_linker->GetImagePointerSize()) { |
| 1607 | space_begin_.reserve(spaces_.size()); |
| 1608 | method_sections_.reserve(spaces_.size()); |
| 1609 | runtime_method_sections_.reserve(spaces_.size()); |
| 1610 | for (gc::space::ImageSpace* space : spaces_) { |
| 1611 | space_begin_.push_back(space->Begin()); |
| 1612 | auto& header = space->GetImageHeader(); |
| 1613 | method_sections_.push_back(&header.GetMethodsSection()); |
| 1614 | runtime_method_sections_.push_back(&header.GetRuntimeMethodsSection()); |
| 1615 | } |
| 1616 | } |
| 1617 | |
| 1618 | void SanityCheckArtMethod(ArtMethod* m, ObjPtr<mirror::Class> expected_class) |
| 1619 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1620 | if (m->IsRuntimeMethod()) { |
| 1621 | ObjPtr<mirror::Class> declaring_class = m->GetDeclaringClassUnchecked(); |
| 1622 | CHECK(declaring_class == nullptr) << declaring_class << " " << m->PrettyMethod(); |
| 1623 | } else if (m->IsCopied()) { |
| 1624 | CHECK(m->GetDeclaringClass() != nullptr) << m->PrettyMethod(); |
| 1625 | } else if (expected_class != nullptr) { |
| 1626 | CHECK_EQ(m->GetDeclaringClassUnchecked(), expected_class) << m->PrettyMethod(); |
| 1627 | } |
| 1628 | if (!spaces_.empty()) { |
| 1629 | bool contains = false; |
| 1630 | for (size_t i = 0; !contains && i != space_begin_.size(); ++i) { |
| 1631 | const size_t offset = reinterpret_cast<uint8_t*>(m) - space_begin_[i]; |
| 1632 | contains = method_sections_[i]->Contains(offset) || |
| 1633 | runtime_method_sections_[i]->Contains(offset); |
| 1634 | } |
| 1635 | CHECK(contains) << m << " not found"; |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | void SanityCheckArtMethodPointerArray(ObjPtr<mirror::PointerArray> arr, |
| 1640 | ObjPtr<mirror::Class> expected_class) |
| 1641 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1642 | CHECK(arr != nullptr); |
| 1643 | for (int32_t j = 0; j < arr->GetLength(); ++j) { |
| 1644 | auto* method = arr->GetElementPtrSize<ArtMethod*>(j, pointer_size_); |
| 1645 | // expected_class == null means we are a dex cache. |
| 1646 | if (expected_class != nullptr) { |
| 1647 | CHECK(method != nullptr); |
| 1648 | } |
| 1649 | if (method != nullptr) { |
| 1650 | SanityCheckArtMethod(method, expected_class); |
| 1651 | } |
| 1652 | } |
| 1653 | } |
| 1654 | |
| 1655 | void SanityCheckArtMethodPointerArray(ArtMethod** arr, size_t size) |
| 1656 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1657 | CHECK_EQ(arr != nullptr, size != 0u); |
| 1658 | if (arr != nullptr) { |
| 1659 | bool contains = false; |
| 1660 | for (auto space : spaces_) { |
| 1661 | auto offset = reinterpret_cast<uint8_t*>(arr) - space->Begin(); |
| 1662 | if (space->GetImageHeader().GetImageSection( |
| 1663 | ImageHeader::kSectionDexCacheArrays).Contains(offset)) { |
| 1664 | contains = true; |
| 1665 | break; |
| 1666 | } |
| 1667 | } |
| 1668 | CHECK(contains); |
| 1669 | } |
| 1670 | for (size_t j = 0; j < size; ++j) { |
| 1671 | ArtMethod* method = mirror::DexCache::GetElementPtrSize(arr, j, pointer_size_); |
| 1672 | // expected_class == null means we are a dex cache. |
| 1673 | if (method != nullptr) { |
| 1674 | SanityCheckArtMethod(method, nullptr); |
| 1675 | } |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | const std::vector<gc::space::ImageSpace*>& spaces_; |
| 1680 | const PointerSize pointer_size_; |
| 1681 | |
| 1682 | // Cached sections from the spaces. |
| 1683 | std::vector<const uint8_t*> space_begin_; |
| 1684 | std::vector<const ImageSection*> method_sections_; |
| 1685 | std::vector<const ImageSection*> runtime_method_sections_; |
| 1686 | }; |
| 1687 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1688 | bool ClassLinker::AddImageSpace( |
| 1689 | gc::space::ImageSpace* space, |
| 1690 | Handle<mirror::ClassLoader> class_loader, |
| 1691 | jobjectArray dex_elements, |
| 1692 | const char* dex_location, |
| 1693 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 1694 | std::string* error_msg) { |
| 1695 | DCHECK(out_dex_files != nullptr); |
| 1696 | DCHECK(error_msg != nullptr); |
| 1697 | const uint64_t start_time = NanoTime(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1698 | const bool app_image = class_loader != nullptr; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1699 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1700 | ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1701 | DCHECK(dex_caches_object != nullptr); |
| 1702 | Runtime* const runtime = Runtime::Current(); |
| 1703 | gc::Heap* const heap = runtime->GetHeap(); |
| 1704 | Thread* const self = Thread::Current(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1705 | // Check that the image is what we are expecting. |
| 1706 | if (image_pointer_size_ != space->GetImageHeader().GetPointerSize()) { |
| 1707 | *error_msg = StringPrintf("Application image pointer size does not match runtime: %zu vs %zu", |
| 1708 | static_cast<size_t>(space->GetImageHeader().GetPointerSize()), |
| 1709 | image_pointer_size_); |
| 1710 | return false; |
| 1711 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1712 | size_t expected_image_roots = ImageHeader::NumberOfImageRoots(app_image); |
| 1713 | if (static_cast<size_t>(header.GetImageRoots()->GetLength()) != expected_image_roots) { |
| 1714 | *error_msg = StringPrintf("Expected %zu image roots but got %d", |
| 1715 | expected_image_roots, |
| 1716 | header.GetImageRoots()->GetLength()); |
| 1717 | return false; |
| 1718 | } |
| 1719 | StackHandleScope<3> hs(self); |
| 1720 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches( |
| 1721 | hs.NewHandle(dex_caches_object->AsObjectArray<mirror::DexCache>())); |
| 1722 | Handle<mirror::ObjectArray<mirror::Class>> class_roots(hs.NewHandle( |
| 1723 | header.GetImageRoot(ImageHeader::kClassRoots)->AsObjectArray<mirror::Class>())); |
| 1724 | static_assert(ImageHeader::kClassLoader + 1u == ImageHeader::kImageRootsMax, |
| 1725 | "Class loader should be the last image root."); |
| 1726 | MutableHandle<mirror::ClassLoader> image_class_loader(hs.NewHandle( |
| 1727 | app_image ? header.GetImageRoot(ImageHeader::kClassLoader)->AsClassLoader() : nullptr)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1728 | DCHECK(class_roots != nullptr); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1729 | if (class_roots->GetLength() != static_cast<int32_t>(kClassRootsMax)) { |
| 1730 | *error_msg = StringPrintf("Expected %d class roots but got %d", |
| 1731 | class_roots->GetLength(), |
| 1732 | static_cast<int32_t>(kClassRootsMax)); |
| 1733 | return false; |
| 1734 | } |
| 1735 | // Check against existing class roots to make sure they match the ones in the boot image. |
| 1736 | for (size_t i = 0; i < kClassRootsMax; i++) { |
| 1737 | if (class_roots->Get(i) != GetClassRoot(static_cast<ClassRoot>(i))) { |
| 1738 | *error_msg = "App image class roots must have pointer equality with runtime ones."; |
| 1739 | return false; |
| 1740 | } |
| 1741 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1742 | const OatFile* oat_file = space->GetOatFile(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1743 | if (oat_file->GetOatHeader().GetDexFileCount() != |
| 1744 | static_cast<uint32_t>(dex_caches->GetLength())) { |
| 1745 | *error_msg = "Dex cache count and dex file count mismatch while trying to initialize from " |
| 1746 | "image"; |
| 1747 | return false; |
| 1748 | } |
| 1749 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1750 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1751 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
| 1752 | std::string dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1753 | // TODO: Only store qualified paths. |
| 1754 | // If non qualified, qualify it. |
| 1755 | if (dex_file_location.find('/') == std::string::npos) { |
| 1756 | std::string dex_location_path = dex_location; |
| 1757 | const size_t pos = dex_location_path.find_last_of('/'); |
| 1758 | CHECK_NE(pos, std::string::npos); |
| 1759 | dex_location_path = dex_location_path.substr(0, pos + 1); // Keep trailing '/' |
| 1760 | dex_file_location = dex_location_path + dex_file_location; |
| 1761 | } |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1762 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 1763 | dex_file_location.c_str(), |
| 1764 | error_msg); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1765 | if (dex_file == nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1766 | return false; |
| 1767 | } |
| 1768 | |
| 1769 | if (app_image) { |
| 1770 | // The current dex file field is bogus, overwrite it so that we can get the dex file in the |
| 1771 | // loop below. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1772 | dex_cache->SetDexFile(dex_file.get()); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1773 | mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes(); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1774 | 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] | 1775 | 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] | 1776 | if (klass != nullptr) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 1777 | DCHECK(!klass->IsErroneous()) << klass->GetStatus(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1778 | } |
| 1779 | } |
| 1780 | } else { |
| 1781 | if (kSanityCheckObjects) { |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1782 | ImageSanityChecks::CheckPointerArray(heap, |
| 1783 | this, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1784 | dex_cache->GetResolvedMethods(), |
| 1785 | dex_cache->NumResolvedMethods()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1786 | } |
| 1787 | // Register dex files, keep track of existing ones that are conflicts. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1788 | AppendToBootClassPath(*dex_file.get(), dex_cache); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1789 | } |
| 1790 | out_dex_files->push_back(std::move(dex_file)); |
| 1791 | } |
| 1792 | |
| 1793 | if (app_image) { |
| 1794 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1795 | // Check that the class loader resolves the same way as the ones in the image. |
| 1796 | // Image class loader [A][B][C][image dex files] |
| 1797 | // Class loader = [???][dex_elements][image dex files] |
| 1798 | // Need to ensure that [???][dex_elements] == [A][B][C]. |
| 1799 | // For each class loader, PathClassLoader, the laoder checks the parent first. Also the logic |
| 1800 | // for PathClassLoader does this by looping through the array of dex files. To ensure they |
| 1801 | // resolve the same way, simply flatten the hierarchy in the way the resolution order would be, |
| 1802 | // and check that the dex file names are the same. |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1803 | if (IsBootClassLoader(soa, image_class_loader.Get())) { |
| 1804 | *error_msg = "Unexpected BootClassLoader in app image"; |
| 1805 | return false; |
| 1806 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1807 | std::list<ObjPtr<mirror::String>> image_dex_file_names; |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1808 | std::string temp_error_msg; |
| 1809 | if (!FlattenPathClassLoader(image_class_loader.Get(), &image_dex_file_names, &temp_error_msg)) { |
| 1810 | *error_msg = StringPrintf("Failed to flatten image class loader hierarchy '%s'", |
| 1811 | temp_error_msg.c_str()); |
| 1812 | return false; |
| 1813 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1814 | std::list<ObjPtr<mirror::String>> loader_dex_file_names; |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1815 | if (!FlattenPathClassLoader(class_loader.Get(), &loader_dex_file_names, &temp_error_msg)) { |
| 1816 | *error_msg = StringPrintf("Failed to flatten class loader hierarchy '%s'", |
| 1817 | temp_error_msg.c_str()); |
| 1818 | return false; |
| 1819 | } |
| 1820 | // Add the temporary dex path list elements at the end. |
| 1821 | auto elements = soa.Decode<mirror::ObjectArray<mirror::Object>>(dex_elements); |
| 1822 | for (size_t i = 0, num_elems = elements->GetLength(); i < num_elems; ++i) { |
| 1823 | ObjPtr<mirror::Object> element = elements->GetWithoutChecks(i); |
| 1824 | if (element != nullptr) { |
| 1825 | // 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] | 1826 | ObjPtr<mirror::String> name; |
| 1827 | if (GetDexPathListElementName(element, &name) && name != nullptr) { |
| 1828 | loader_dex_file_names.push_back(name); |
| 1829 | } |
Nicolas Geoffray | 1df3b55 | 2016-04-26 18:30:31 +0100 | [diff] [blame] | 1830 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1831 | } |
| 1832 | // Ignore the number of image dex files since we are adding those to the class loader anyways. |
| 1833 | CHECK_GE(static_cast<size_t>(image_dex_file_names.size()), |
| 1834 | static_cast<size_t>(dex_caches->GetLength())); |
| 1835 | size_t image_count = image_dex_file_names.size() - dex_caches->GetLength(); |
| 1836 | // Check that the dex file names match. |
| 1837 | bool equal = image_count == loader_dex_file_names.size(); |
| 1838 | if (equal) { |
| 1839 | auto it1 = image_dex_file_names.begin(); |
| 1840 | auto it2 = loader_dex_file_names.begin(); |
| 1841 | for (size_t i = 0; equal && i < image_count; ++i, ++it1, ++it2) { |
| 1842 | equal = equal && (*it1)->Equals(*it2); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1843 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1844 | } |
| 1845 | if (!equal) { |
| 1846 | VLOG(image) << "Image dex files " << image_dex_file_names.size(); |
| 1847 | for (ObjPtr<mirror::String> name : image_dex_file_names) { |
| 1848 | VLOG(image) << name->ToModifiedUtf8(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1849 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1850 | VLOG(image) << "Loader dex files " << loader_dex_file_names.size(); |
| 1851 | for (ObjPtr<mirror::String> name : loader_dex_file_names) { |
| 1852 | VLOG(image) << name->ToModifiedUtf8(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1853 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1854 | *error_msg = "Rejecting application image due to class loader mismatch"; |
| 1855 | // Ignore class loader mismatch for now since these would just use possibly incorrect |
| 1856 | // 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] | 1857 | } |
| 1858 | } |
| 1859 | |
| 1860 | if (kSanityCheckObjects) { |
| 1861 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 1862 | auto* dex_cache = dex_caches->Get(i); |
| 1863 | for (size_t j = 0; j < dex_cache->NumResolvedFields(); ++j) { |
| 1864 | auto* field = dex_cache->GetResolvedField(j, image_pointer_size_); |
| 1865 | if (field != nullptr) { |
| 1866 | CHECK(field->GetDeclaringClass()->GetClass() != nullptr); |
| 1867 | } |
| 1868 | } |
| 1869 | } |
| 1870 | if (!app_image) { |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1871 | ImageSanityChecks::CheckObjects(heap, this); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | // Set entry point to interpreter if in InterpretOnly mode. |
| 1876 | if (!runtime->IsAotCompiler() && runtime->GetInstrumentation()->InterpretOnly()) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1877 | SetInterpreterEntrypointArtMethodVisitor visitor(image_pointer_size_); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 1878 | header.VisitPackedArtMethods(&visitor, space->Begin(), image_pointer_size_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1879 | } |
| 1880 | |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1881 | ClassTable* class_table = nullptr; |
| 1882 | { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1883 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1884 | class_table = InsertClassTableForClassLoader(class_loader.Get()); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1885 | } |
| 1886 | // If we have a class table section, read it and use it for verification in |
| 1887 | // UpdateAppImageClassLoadersAndDexCaches. |
| 1888 | ClassTable::ClassSet temp_set; |
| 1889 | const ImageSection& class_table_section = header.GetImageSection(ImageHeader::kSectionClassTable); |
| 1890 | const bool added_class_table = class_table_section.Size() > 0u; |
| 1891 | if (added_class_table) { |
| 1892 | const uint64_t start_time2 = NanoTime(); |
| 1893 | size_t read_count = 0; |
| 1894 | temp_set = ClassTable::ClassSet(space->Begin() + class_table_section.Offset(), |
| 1895 | /*make copy*/false, |
| 1896 | &read_count); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1897 | VLOG(image) << "Adding class table classes took " << PrettyDuration(NanoTime() - start_time2); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1898 | } |
| 1899 | if (app_image) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1900 | bool forward_dex_cache_arrays = false; |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1901 | if (!AppImageClassLoadersAndDexCachesHelper::Update(this, |
| 1902 | space, |
| 1903 | class_loader, |
| 1904 | dex_caches, |
| 1905 | &temp_set, |
| 1906 | /*out*/&forward_dex_cache_arrays, |
| 1907 | /*out*/error_msg)) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1908 | return false; |
| 1909 | } |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1910 | // Update class loader and resolved strings. If added_class_table is false, the resolved |
| 1911 | // strings were forwarded UpdateAppImageClassLoadersAndDexCaches. |
Nicolas Geoffray | f9bf250 | 2016-12-14 14:59:04 +0000 | [diff] [blame] | 1912 | UpdateClassLoaderVisitor visitor(space, class_loader.Get()); |
| 1913 | for (const ClassTable::TableSlot& root : temp_set) { |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1914 | visitor(root.Read()); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1915 | } |
Mathieu Chartier | 8c4f041 | 2016-02-03 16:40:20 -0800 | [diff] [blame] | 1916 | // forward_dex_cache_arrays is true iff we copied all of the dex cache arrays into the .bss. |
| 1917 | // In this case, madvise away the dex cache arrays section of the image to reduce RAM usage and |
| 1918 | // mark as PROT_NONE to catch any invalid accesses. |
| 1919 | if (forward_dex_cache_arrays) { |
| 1920 | const ImageSection& dex_cache_section = header.GetImageSection( |
| 1921 | ImageHeader::kSectionDexCacheArrays); |
| 1922 | uint8_t* section_begin = AlignUp(space->Begin() + dex_cache_section.Offset(), kPageSize); |
| 1923 | uint8_t* section_end = AlignDown(space->Begin() + dex_cache_section.End(), kPageSize); |
| 1924 | if (section_begin < section_end) { |
| 1925 | madvise(section_begin, section_end - section_begin, MADV_DONTNEED); |
| 1926 | mprotect(section_begin, section_end - section_begin, PROT_NONE); |
| 1927 | VLOG(image) << "Released and protected dex cache array image section from " |
| 1928 | << reinterpret_cast<const void*>(section_begin) << "-" |
| 1929 | << reinterpret_cast<const void*>(section_end); |
| 1930 | } |
| 1931 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 1932 | } |
| 1933 | if (!oat_file->GetBssGcRoots().empty()) { |
| 1934 | // Insert oat file to class table for visiting .bss GC roots. |
| 1935 | class_table->InsertOatFile(oat_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1936 | } |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1937 | if (added_class_table) { |
| 1938 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 1939 | class_table->AddClassSet(std::move(temp_set)); |
| 1940 | } |
| 1941 | if (kIsDebugBuild && app_image) { |
| 1942 | // This verification needs to happen after the classes have been added to the class loader. |
| 1943 | // Since it ensures classes are in the class table. |
| 1944 | VerifyClassInTableArtMethodVisitor visitor2(class_table); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1945 | header.VisitPackedArtMethods(&visitor2, space->Begin(), kRuntimePointerSize); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1946 | // Verify that all direct interfaces of classes in the class table are also resolved. |
| 1947 | VerifyDirectInterfacesInTableClassVisitor visitor(class_loader.Get()); |
| 1948 | class_table->Visit(visitor); |
| 1949 | visitor.Check(); |
| 1950 | // Check that all non-primitive classes in dex caches are also in the class table. |
| 1951 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 1952 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
| 1953 | mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes(); |
| 1954 | for (int32_t j = 0, num_types = dex_cache->NumResolvedTypes(); j < num_types; j++) { |
| 1955 | ObjPtr<mirror::Class> klass = types[j].load(std::memory_order_relaxed).object.Read(); |
| 1956 | if (klass != nullptr && !klass->IsPrimitive()) { |
| 1957 | CHECK(class_table->Contains(klass)) << klass->PrettyDescriptor() |
| 1958 | << " " << dex_cache->GetDexFile()->GetLocation(); |
| 1959 | } |
| 1960 | } |
| 1961 | } |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1962 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1963 | VLOG(class_linker) << "Adding image space took " << PrettyDuration(NanoTime() - start_time); |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 1964 | return true; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1965 | } |
| 1966 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1967 | bool ClassLinker::ClassInClassTable(ObjPtr<mirror::Class> klass) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 1968 | ClassTable* const class_table = ClassTableForClassLoader(klass->GetClassLoader()); |
| 1969 | return class_table != nullptr && class_table->Contains(klass); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1970 | } |
| 1971 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1972 | void ClassLinker::VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 1973 | // Acquire tracing_enabled before locking class linker lock to prevent lock order violation. Since |
| 1974 | // enabling tracing requires the mutator lock, there are no race conditions here. |
| 1975 | const bool tracing_enabled = Trace::IsTracingEnabled(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 1976 | Thread* const self = Thread::Current(); |
| 1977 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 1978 | if (kUseReadBarrier) { |
| 1979 | // We do not track new roots for CC. |
| 1980 | DCHECK_EQ(0, flags & (kVisitRootFlagNewRoots | |
| 1981 | kVisitRootFlagClearRootLog | |
| 1982 | kVisitRootFlagStartLoggingNewRoots | |
| 1983 | kVisitRootFlagStopLoggingNewRoots)); |
| 1984 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1985 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1986 | // Argument for how root visiting deals with ArtField and ArtMethod roots. |
| 1987 | // There is 3 GC cases to handle: |
| 1988 | // Non moving concurrent: |
| 1989 | // 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] | 1990 | // live by the class and class roots. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1991 | // |
| 1992 | // Moving non-concurrent: |
| 1993 | // This case needs to call visit VisitNativeRoots in case the classes or dex cache arrays move. |
| 1994 | // To prevent missing roots, this case needs to ensure that there is no |
| 1995 | // suspend points between the point which we allocate ArtMethod arrays and place them in a |
| 1996 | // class which is in the class table. |
| 1997 | // |
| 1998 | // Moving concurrent: |
| 1999 | // Need to make sure to not copy ArtMethods without doing read barriers since the roots are |
| 2000 | // 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] | 2001 | // |
| 2002 | // Use an unbuffered visitor since the class table uses a temporary GcRoot for holding decoded |
| 2003 | // ClassTable::TableSlot. The buffered root visiting would access a stale stack location for |
| 2004 | // these objects. |
| 2005 | UnbufferedRootVisitor root_visitor(visitor, RootInfo(kRootStickyClass)); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 2006 | boot_class_table_->VisitRoots(root_visitor); |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 2007 | // 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] | 2008 | if ((flags & kVisitRootFlagClassLoader) != 0 || tracing_enabled) { |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 2009 | for (const ClassLoaderData& data : class_loaders_) { |
| 2010 | GcRoot<mirror::Object> root(GcRoot<mirror::Object>(self->DecodeJObject(data.weak_root))); |
| 2011 | root.VisitRoot(visitor, RootInfo(kRootVMInternal)); |
| 2012 | } |
| 2013 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2014 | } else if (!kUseReadBarrier && (flags & kVisitRootFlagNewRoots) != 0) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2015 | for (auto& root : new_class_roots_) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2016 | ObjPtr<mirror::Class> old_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2017 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2018 | ObjPtr<mirror::Class> new_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2019 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 2020 | CHECK_EQ(new_ref, old_ref); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2021 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2022 | for (const OatFile* oat_file : new_bss_roots_boot_oat_files_) { |
| 2023 | for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) { |
| 2024 | ObjPtr<mirror::Object> old_ref = root.Read<kWithoutReadBarrier>(); |
| 2025 | if (old_ref != nullptr) { |
| 2026 | DCHECK(old_ref->IsClass()); |
| 2027 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
| 2028 | ObjPtr<mirror::Object> new_ref = root.Read<kWithoutReadBarrier>(); |
| 2029 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 2030 | CHECK_EQ(new_ref, old_ref); |
| 2031 | } |
| 2032 | } |
| 2033 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2034 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2035 | if (!kUseReadBarrier && (flags & kVisitRootFlagClearRootLog) != 0) { |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2036 | new_class_roots_.clear(); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2037 | new_bss_roots_boot_oat_files_.clear(); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2038 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2039 | if (!kUseReadBarrier && (flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2040 | log_new_roots_ = true; |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2041 | } else if (!kUseReadBarrier && (flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2042 | log_new_roots_ = false; |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2043 | } |
| 2044 | // We deliberately ignore the class roots in the image since we |
| 2045 | // handle image roots by using the MS/CMS rescanning of dirty cards. |
| 2046 | } |
| 2047 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2048 | // Keep in sync with InitCallback. Anything we visit, we need to |
| 2049 | // reinit references to when reinitializing a ClassLinker from a |
| 2050 | // mapped image. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2051 | void ClassLinker::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 2052 | class_roots_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2053 | VisitClassRoots(visitor, flags); |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 2054 | array_iftable_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); |
Mathieu Chartier | 6cfc2c0 | 2015-10-12 15:06:16 -0700 | [diff] [blame] | 2055 | // Instead of visiting the find_array_class_cache_ drop it so that it doesn't prevent class |
| 2056 | // unloading if we are marking roots. |
| 2057 | DropFindArrayClassCache(); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2058 | } |
| 2059 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2060 | class VisitClassLoaderClassesVisitor : public ClassLoaderVisitor { |
| 2061 | public: |
| 2062 | explicit VisitClassLoaderClassesVisitor(ClassVisitor* visitor) |
| 2063 | : visitor_(visitor), |
| 2064 | done_(false) {} |
| 2065 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2066 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2067 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) OVERRIDE { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2068 | ClassTable* const class_table = class_loader->GetClassTable(); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2069 | if (!done_ && class_table != nullptr) { |
| 2070 | DefiningClassLoaderFilterVisitor visitor(class_loader, visitor_); |
| 2071 | if (!class_table->Visit(visitor)) { |
| 2072 | // If the visitor ClassTable returns false it means that we don't need to continue. |
| 2073 | done_ = true; |
| 2074 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2075 | } |
| 2076 | } |
| 2077 | |
| 2078 | private: |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2079 | // Class visitor that limits the class visits from a ClassTable to the classes with |
| 2080 | // the provided defining class loader. This filter is used to avoid multiple visits |
| 2081 | // of the same class which can be recorded for multiple initiating class loaders. |
| 2082 | class DefiningClassLoaderFilterVisitor : public ClassVisitor { |
| 2083 | public: |
| 2084 | DefiningClassLoaderFilterVisitor(ObjPtr<mirror::ClassLoader> defining_class_loader, |
| 2085 | ClassVisitor* visitor) |
| 2086 | : defining_class_loader_(defining_class_loader), visitor_(visitor) { } |
| 2087 | |
| 2088 | bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2089 | if (klass->GetClassLoader() != defining_class_loader_) { |
| 2090 | return true; |
| 2091 | } |
| 2092 | return (*visitor_)(klass); |
| 2093 | } |
| 2094 | |
| 2095 | ObjPtr<mirror::ClassLoader> const defining_class_loader_; |
| 2096 | ClassVisitor* const visitor_; |
| 2097 | }; |
| 2098 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2099 | ClassVisitor* const visitor_; |
| 2100 | // If done is true then we don't need to do any more visiting. |
| 2101 | bool done_; |
| 2102 | }; |
| 2103 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2104 | void ClassLinker::VisitClassesInternal(ClassVisitor* visitor) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 2105 | if (boot_class_table_->Visit(*visitor)) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2106 | VisitClassLoaderClassesVisitor loader_visitor(visitor); |
| 2107 | VisitClassLoaders(&loader_visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2108 | } |
| 2109 | } |
| 2110 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2111 | void ClassLinker::VisitClasses(ClassVisitor* visitor) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2112 | Thread* const self = Thread::Current(); |
| 2113 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2114 | // Not safe to have thread suspension when we are holding a lock. |
| 2115 | if (self != nullptr) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2116 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2117 | VisitClassesInternal(visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2118 | } else { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2119 | VisitClassesInternal(visitor); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2120 | } |
| 2121 | } |
| 2122 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2123 | class GetClassesInToVector : public ClassVisitor { |
| 2124 | public: |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2125 | bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2126 | classes_.push_back(klass); |
| 2127 | return true; |
| 2128 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2129 | std::vector<ObjPtr<mirror::Class>> classes_; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2130 | }; |
| 2131 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2132 | class GetClassInToObjectArray : public ClassVisitor { |
| 2133 | public: |
| 2134 | explicit GetClassInToObjectArray(mirror::ObjectArray<mirror::Class>* arr) |
| 2135 | : arr_(arr), index_(0) {} |
| 2136 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2137 | bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2138 | ++index_; |
| 2139 | if (index_ <= arr_->GetLength()) { |
| 2140 | arr_->Set(index_ - 1, klass); |
| 2141 | return true; |
| 2142 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2143 | return false; |
| 2144 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2145 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2146 | bool Succeeded() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2147 | return index_ <= arr_->GetLength(); |
| 2148 | } |
| 2149 | |
| 2150 | private: |
| 2151 | mirror::ObjectArray<mirror::Class>* const arr_; |
| 2152 | int32_t index_; |
| 2153 | }; |
| 2154 | |
| 2155 | void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2156 | // TODO: it may be possible to avoid secondary storage if we iterate over dex caches. The problem |
| 2157 | // is avoiding duplicates. |
| 2158 | if (!kMovingClasses) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2159 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2160 | GetClassesInToVector accumulator; |
| 2161 | VisitClasses(&accumulator); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2162 | for (ObjPtr<mirror::Class> klass : accumulator.classes_) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2163 | if (!visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2164 | return; |
| 2165 | } |
| 2166 | } |
| 2167 | } else { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2168 | Thread* const self = Thread::Current(); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2169 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2170 | auto classes = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2171 | // We size the array assuming classes won't be added to the class table during the visit. |
| 2172 | // If this assumption fails we iterate again. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2173 | while (true) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2174 | size_t class_table_size; |
| 2175 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2176 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2177 | // Add 100 in case new classes get loaded when we are filling in the object array. |
| 2178 | class_table_size = NumZygoteClasses() + NumNonZygoteClasses() + 100; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2179 | } |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 2180 | ObjPtr<mirror::Class> class_type = mirror::Class::GetJavaLangClass(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2181 | ObjPtr<mirror::Class> array_of_class = FindArrayClass(self, &class_type); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2182 | classes.Assign( |
| 2183 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, class_table_size)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2184 | CHECK(classes != nullptr); // OOME. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2185 | GetClassInToObjectArray accumulator(classes.Get()); |
| 2186 | VisitClasses(&accumulator); |
| 2187 | if (accumulator.Succeeded()) { |
| 2188 | break; |
| 2189 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2190 | } |
| 2191 | for (int32_t i = 0; i < classes->GetLength(); ++i) { |
| 2192 | // If the class table shrank during creation of the clases array we expect null elements. If |
| 2193 | // the class table grew then the loop repeats. If classes are created after the loop has |
| 2194 | // finished then we don't visit. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2195 | ObjPtr<mirror::Class> klass = classes->Get(i); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2196 | if (klass != nullptr && !visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2197 | return; |
| 2198 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2199 | } |
| 2200 | } |
| 2201 | } |
| 2202 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2203 | ClassLinker::~ClassLinker() { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2204 | mirror::Class::ResetClass(); |
| 2205 | mirror::Constructor::ResetClass(); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 2206 | mirror::Field::ResetClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2207 | mirror::Method::ResetClass(); |
| 2208 | mirror::Reference::ResetClass(); |
| 2209 | mirror::StackTraceElement::ResetClass(); |
| 2210 | mirror::String::ResetClass(); |
| 2211 | mirror::Throwable::ResetClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2212 | mirror::BooleanArray::ResetArrayClass(); |
| 2213 | mirror::ByteArray::ResetArrayClass(); |
| 2214 | mirror::CharArray::ResetArrayClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2215 | mirror::Constructor::ResetArrayClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2216 | mirror::DoubleArray::ResetArrayClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2217 | mirror::Field::ResetArrayClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2218 | mirror::FloatArray::ResetArrayClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2219 | mirror::Method::ResetArrayClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2220 | mirror::IntArray::ResetArrayClass(); |
| 2221 | mirror::LongArray::ResetArrayClass(); |
| 2222 | mirror::ShortArray::ResetArrayClass(); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 2223 | mirror::MethodType::ResetClass(); |
| 2224 | mirror::MethodHandleImpl::ResetClass(); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 2225 | mirror::MethodHandlesLookup::ResetClass(); |
| 2226 | mirror::CallSite::ResetClass(); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 2227 | mirror::EmulatedStackFrame::ResetClass(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2228 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 2229 | for (const ClassLoaderData& data : class_loaders_) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2230 | DeleteClassLoader(self, data); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 2231 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2232 | class_loaders_.clear(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2233 | } |
| 2234 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2235 | void ClassLinker::DeleteClassLoader(Thread* self, const ClassLoaderData& data) { |
| 2236 | Runtime* const runtime = Runtime::Current(); |
| 2237 | JavaVMExt* const vm = runtime->GetJavaVM(); |
| 2238 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 2239 | // 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] | 2240 | if (runtime->GetJit() != nullptr) { |
| 2241 | jit::JitCodeCache* code_cache = runtime->GetJit()->GetCodeCache(); |
| 2242 | if (code_cache != nullptr) { |
| 2243 | code_cache->RemoveMethodsIn(self, *data.allocator); |
| 2244 | } |
| 2245 | } |
| 2246 | delete data.allocator; |
| 2247 | delete data.class_table; |
| 2248 | } |
| 2249 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2250 | mirror::PointerArray* ClassLinker::AllocPointerArray(Thread* self, size_t length) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2251 | return down_cast<mirror::PointerArray*>( |
| 2252 | image_pointer_size_ == PointerSize::k64 |
| 2253 | ? static_cast<mirror::Array*>(mirror::LongArray::Alloc(self, length)) |
| 2254 | : static_cast<mirror::Array*>(mirror::IntArray::Alloc(self, length))); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2255 | } |
| 2256 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2257 | mirror::DexCache* ClassLinker::AllocDexCache(ObjPtr<mirror::String>* out_location, |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2258 | Thread* self, |
| 2259 | const DexFile& dex_file) { |
| 2260 | StackHandleScope<1> hs(self); |
| 2261 | DCHECK(out_location != nullptr); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 2262 | auto dex_cache(hs.NewHandle(ObjPtr<mirror::DexCache>::DownCast( |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2263 | GetClassRoot(kJavaLangDexCache)->AllocObject(self)))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2264 | if (dex_cache == nullptr) { |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2265 | self->AssertPendingOOMException(); |
| 2266 | return nullptr; |
| 2267 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2268 | ObjPtr<mirror::String> location = intern_table_->InternStrong(dex_file.GetLocation().c_str()); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2269 | if (location == nullptr) { |
| 2270 | self->AssertPendingOOMException(); |
| 2271 | return nullptr; |
| 2272 | } |
| 2273 | *out_location = location; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2274 | return dex_cache.Get(); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2275 | } |
| 2276 | |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2277 | mirror::DexCache* ClassLinker::AllocAndInitializeDexCache(Thread* self, |
| 2278 | const DexFile& dex_file, |
| 2279 | LinearAlloc* linear_alloc) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2280 | ObjPtr<mirror::String> location = nullptr; |
| 2281 | ObjPtr<mirror::DexCache> dex_cache = AllocDexCache(&location, self, dex_file); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2282 | if (dex_cache != nullptr) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 2283 | WriterMutexLock mu(self, *Locks::dex_lock_); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2284 | DCHECK(location != nullptr); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 2285 | mirror::DexCache::InitializeDexCache(self, |
| 2286 | dex_cache, |
| 2287 | location, |
| 2288 | &dex_file, |
| 2289 | linear_alloc, |
| 2290 | image_pointer_size_); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2291 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2292 | return dex_cache.Ptr(); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2293 | } |
| 2294 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2295 | mirror::Class* ClassLinker::AllocClass(Thread* self, |
| 2296 | ObjPtr<mirror::Class> java_lang_Class, |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2297 | uint32_t class_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2298 | DCHECK_GE(class_size, sizeof(mirror::Class)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2299 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2300 | mirror::Class::InitializeClassVisitor visitor(class_size); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2301 | ObjPtr<mirror::Object> k = kMovingClasses ? |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 2302 | heap->AllocObject<true>(self, java_lang_Class, class_size, visitor) : |
| 2303 | heap->AllocNonMovableObject<true>(self, java_lang_Class, class_size, visitor); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2304 | if (UNLIKELY(k == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2305 | self->AssertPendingOOMException(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2306 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2307 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2308 | return k->AsClass(); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 2309 | } |
| 2310 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2311 | mirror::Class* ClassLinker::AllocClass(Thread* self, uint32_t class_size) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2312 | return AllocClass(self, GetClassRoot(kJavaLangClass), class_size); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2313 | } |
| 2314 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2315 | mirror::ObjectArray<mirror::StackTraceElement>* ClassLinker::AllocStackTraceElementArray( |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2316 | Thread* self, |
| 2317 | size_t length) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2318 | return mirror::ObjectArray<mirror::StackTraceElement>::Alloc( |
| 2319 | self, GetClassRoot(kJavaLangStackTraceElementArrayClass), length); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2320 | } |
| 2321 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2322 | mirror::Class* ClassLinker::EnsureResolved(Thread* self, |
| 2323 | const char* descriptor, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2324 | ObjPtr<mirror::Class> klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2325 | DCHECK(klass != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2326 | if (kIsDebugBuild) { |
| 2327 | StackHandleScope<1> hs(self); |
| 2328 | HandleWrapperObjPtr<mirror::Class> h = hs.NewHandleWrapper(&klass); |
| 2329 | Thread::PoisonObjectPointersIfDebug(); |
| 2330 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2331 | |
| 2332 | // For temporary classes we must wait for them to be retired. |
| 2333 | if (init_done_ && klass->IsTemp()) { |
| 2334 | CHECK(!klass->IsResolved()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2335 | if (klass->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2336 | ThrowEarlierClassFailure(klass); |
| 2337 | return nullptr; |
| 2338 | } |
| 2339 | StackHandleScope<1> hs(self); |
| 2340 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 2341 | ObjectLock<mirror::Class> lock(self, h_class); |
| 2342 | // Loop and wait for the resolving thread to retire this class. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2343 | while (!h_class->IsRetired() && !h_class->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2344 | lock.WaitIgnoringInterrupts(); |
| 2345 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2346 | if (h_class->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2347 | ThrowEarlierClassFailure(h_class.Get()); |
| 2348 | return nullptr; |
| 2349 | } |
| 2350 | CHECK(h_class->IsRetired()); |
| 2351 | // Get the updated class from class table. |
Andreas Gampe | 34ee684 | 2014-12-02 15:43:52 -0800 | [diff] [blame] | 2352 | klass = LookupClass(self, descriptor, h_class.Get()->GetClassLoader()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2353 | } |
| 2354 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2355 | // Wait for the class if it has not already been linked. |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2356 | size_t index = 0; |
| 2357 | // Maximum number of yield iterations until we start sleeping. |
| 2358 | static const size_t kNumYieldIterations = 1000; |
| 2359 | // How long each sleep is in us. |
| 2360 | static const size_t kSleepDurationUS = 1000; // 1 ms. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2361 | while (!klass->IsResolved() && !klass->IsErroneousUnresolved()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2362 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2363 | HandleWrapperObjPtr<mirror::Class> h_class(hs.NewHandleWrapper(&klass)); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2364 | { |
| 2365 | ObjectTryLock<mirror::Class> lock(self, h_class); |
| 2366 | // Can not use a monitor wait here since it may block when returning and deadlock if another |
| 2367 | // thread has locked klass. |
| 2368 | if (lock.Acquired()) { |
| 2369 | // Check for circular dependencies between classes, the lock is required for SetStatus. |
| 2370 | if (!h_class->IsResolved() && h_class->GetClinitThreadId() == self->GetTid()) { |
| 2371 | ThrowClassCircularityError(h_class.Get()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2372 | mirror::Class::SetStatus(h_class, mirror::Class::kStatusErrorUnresolved, self); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2373 | return nullptr; |
| 2374 | } |
| 2375 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2376 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2377 | { |
| 2378 | // Handle wrapper deals with klass moving. |
| 2379 | ScopedThreadSuspension sts(self, kSuspended); |
| 2380 | if (index < kNumYieldIterations) { |
| 2381 | sched_yield(); |
| 2382 | } else { |
| 2383 | usleep(kSleepDurationUS); |
| 2384 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2385 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2386 | ++index; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2387 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2388 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2389 | if (klass->IsErroneousUnresolved()) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2390 | ThrowEarlierClassFailure(klass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2391 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2392 | } |
| 2393 | // Return the loaded class. No exceptions should be pending. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2394 | CHECK(klass->IsResolved()) << klass->PrettyClass(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2395 | self->AssertNoPendingException(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2396 | return klass.Ptr(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2397 | } |
| 2398 | |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2399 | typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry; |
| 2400 | |
| 2401 | // Search a collection of DexFiles for a descriptor |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2402 | ClassPathEntry FindInClassPath(const char* descriptor, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2403 | size_t hash, const std::vector<const DexFile*>& class_path) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2404 | for (const DexFile* dex_file : class_path) { |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 2405 | const DexFile::ClassDef* dex_class_def = OatDexFile::FindClassDef(*dex_file, descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2406 | if (dex_class_def != nullptr) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2407 | return ClassPathEntry(dex_file, dex_class_def); |
| 2408 | } |
| 2409 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2410 | return ClassPathEntry(nullptr, nullptr); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2411 | } |
| 2412 | |
Nicolas Geoffray | 7d8d8ff | 2016-11-02 12:38:05 +0000 | [diff] [blame] | 2413 | bool ClassLinker::FindClassInBaseDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
| 2414 | Thread* self, |
| 2415 | const char* descriptor, |
| 2416 | size_t hash, |
| 2417 | Handle<mirror::ClassLoader> class_loader, |
| 2418 | ObjPtr<mirror::Class>* result) { |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2419 | // Termination case: boot class-loader. |
| 2420 | if (IsBootClassLoader(soa, class_loader.Get())) { |
| 2421 | // The boot class loader, search the boot class path. |
| 2422 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
| 2423 | if (pair.second != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2424 | ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, nullptr); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2425 | if (klass != nullptr) { |
| 2426 | *result = EnsureResolved(self, descriptor, klass); |
| 2427 | } else { |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 2428 | *result = DefineClass(self, |
| 2429 | descriptor, |
| 2430 | hash, |
| 2431 | ScopedNullHandle<mirror::ClassLoader>(), |
| 2432 | *pair.first, |
| 2433 | *pair.second); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2434 | } |
| 2435 | if (*result == nullptr) { |
| 2436 | CHECK(self->IsExceptionPending()) << descriptor; |
| 2437 | self->ClearException(); |
| 2438 | } |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2439 | } else { |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2440 | *result = nullptr; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2441 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2442 | return true; |
| 2443 | } |
| 2444 | |
| 2445 | // Unsupported class-loader? |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 2446 | if (soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader) != |
| 2447 | class_loader->GetClass()) { |
Nicolas Geoffray | 7d8d8ff | 2016-11-02 12:38:05 +0000 | [diff] [blame] | 2448 | // PathClassLoader is the most common case, so it's the one we check first. For secondary dex |
| 2449 | // files, we also check DexClassLoader here. |
| 2450 | if (soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_DexClassLoader) != |
| 2451 | class_loader->GetClass()) { |
| 2452 | *result = nullptr; |
| 2453 | return false; |
| 2454 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2455 | } |
| 2456 | |
| 2457 | // Handles as RegisterDexFile may allocate dex caches (and cause thread suspension). |
| 2458 | StackHandleScope<4> hs(self); |
| 2459 | Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent())); |
Nicolas Geoffray | 7d8d8ff | 2016-11-02 12:38:05 +0000 | [diff] [blame] | 2460 | bool recursive_result = FindClassInBaseDexClassLoader(soa, |
| 2461 | self, |
| 2462 | descriptor, |
| 2463 | hash, |
| 2464 | h_parent, |
| 2465 | result); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2466 | |
| 2467 | if (!recursive_result) { |
| 2468 | // Something wrong up the chain. |
| 2469 | return false; |
| 2470 | } |
| 2471 | |
| 2472 | if (*result != nullptr) { |
| 2473 | // Found the class up the chain. |
| 2474 | return true; |
| 2475 | } |
| 2476 | |
| 2477 | // Handle this step. |
| 2478 | // Handle as if this is the child PathClassLoader. |
| 2479 | // The class loader is a PathClassLoader which inherits from BaseDexClassLoader. |
| 2480 | // We need to get the DexPathList and loop through it. |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2481 | ArtField* const cookie_field = |
| 2482 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2483 | ArtField* const dex_file_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2484 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2485 | ObjPtr<mirror::Object> dex_path_list = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2486 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList)-> |
| 2487 | GetObject(class_loader.Get()); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2488 | if (dex_path_list != nullptr && dex_file_field != nullptr && cookie_field != nullptr) { |
| 2489 | // 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] | 2490 | ObjPtr<mirror::Object> dex_elements_obj = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2491 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements)-> |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2492 | GetObject(dex_path_list); |
| 2493 | // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look |
| 2494 | // at the mCookie which is a DexFile vector. |
| 2495 | if (dex_elements_obj != nullptr) { |
| 2496 | Handle<mirror::ObjectArray<mirror::Object>> dex_elements = |
| 2497 | hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>()); |
| 2498 | for (int32_t i = 0; i < dex_elements->GetLength(); ++i) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2499 | ObjPtr<mirror::Object> element = dex_elements->GetWithoutChecks(i); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2500 | if (element == nullptr) { |
| 2501 | // Should never happen, fall back to java code to throw a NPE. |
| 2502 | break; |
| 2503 | } |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2504 | ObjPtr<mirror::Object> dex_file = dex_file_field->GetObject(element); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2505 | if (dex_file != nullptr) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2506 | ObjPtr<mirror::LongArray> long_array = cookie_field->GetObject(dex_file)->AsLongArray(); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2507 | if (long_array == nullptr) { |
| 2508 | // This should never happen so log a warning. |
| 2509 | LOG(WARNING) << "Null DexFile::mCookie for " << descriptor; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2510 | break; |
| 2511 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2512 | int32_t long_array_size = long_array->GetLength(); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 2513 | // First element is the oat file. |
| 2514 | for (int32_t j = kDexFileIndexStart; j < long_array_size; ++j) { |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2515 | const DexFile* cp_dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>( |
| 2516 | long_array->GetWithoutChecks(j))); |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 2517 | const DexFile::ClassDef* dex_class_def = |
| 2518 | OatDexFile::FindClassDef(*cp_dex_file, descriptor, hash); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2519 | if (dex_class_def != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2520 | ObjPtr<mirror::Class> klass = DefineClass(self, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 2521 | descriptor, |
| 2522 | hash, |
| 2523 | class_loader, |
| 2524 | *cp_dex_file, |
| 2525 | *dex_class_def); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2526 | if (klass == nullptr) { |
| 2527 | CHECK(self->IsExceptionPending()) << descriptor; |
| 2528 | self->ClearException(); |
| 2529 | // TODO: Is it really right to break here, and not check the other dex files? |
| 2530 | return true; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2531 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2532 | *result = klass; |
| 2533 | return true; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2534 | } |
| 2535 | } |
| 2536 | } |
| 2537 | } |
| 2538 | } |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2539 | self->AssertNoPendingException(); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2540 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2541 | |
| 2542 | // Result is still null from the parent call, no need to set it again... |
| 2543 | return true; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2544 | } |
| 2545 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2546 | mirror::Class* ClassLinker::FindClass(Thread* self, |
| 2547 | const char* descriptor, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2548 | Handle<mirror::ClassLoader> class_loader) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 2549 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2550 | DCHECK(self != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2551 | self->AssertNoPendingException(); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 2552 | self->PoisonObjectPointers(); // For DefineClass, CreateArrayClass, etc... |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 2553 | if (descriptor[1] == '\0') { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2554 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 2555 | // for primitive classes that aren't backed by dex files. |
| 2556 | return FindPrimitiveClass(descriptor[0]); |
| 2557 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2558 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2559 | // Find the class in the loaded classes table. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2560 | ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, class_loader.Get()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2561 | if (klass != nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2562 | return EnsureResolved(self, descriptor, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2563 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2564 | // Class is not yet loaded. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2565 | if (descriptor[0] != '[' && class_loader == nullptr) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2566 | // 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] | 2567 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2568 | if (pair.second != nullptr) { |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 2569 | return DefineClass(self, |
| 2570 | descriptor, |
| 2571 | hash, |
| 2572 | ScopedNullHandle<mirror::ClassLoader>(), |
| 2573 | *pair.first, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2574 | *pair.second); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2575 | } else { |
| 2576 | // The boot class loader is searched ahead of the application class loader, failures are |
| 2577 | // expected and will be wrapped in a ClassNotFoundException. Use the pre-allocated error to |
| 2578 | // trigger the chaining with a proper stack trace. |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2579 | ObjPtr<mirror::Throwable> pre_allocated = |
| 2580 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2581 | self->SetException(pre_allocated); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2582 | return nullptr; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2583 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2584 | } |
| 2585 | ObjPtr<mirror::Class> result_ptr; |
| 2586 | bool descriptor_equals; |
| 2587 | if (descriptor[0] == '[') { |
| 2588 | result_ptr = CreateArrayClass(self, descriptor, hash, class_loader); |
| 2589 | DCHECK_EQ(result_ptr == nullptr, self->IsExceptionPending()); |
| 2590 | DCHECK(result_ptr == nullptr || result_ptr->DescriptorEquals(descriptor)); |
| 2591 | descriptor_equals = true; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2592 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2593 | ScopedObjectAccessUnchecked soa(self); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2594 | bool known_hierarchy = |
| 2595 | FindClassInBaseDexClassLoader(soa, self, descriptor, hash, class_loader, &result_ptr); |
| 2596 | if (result_ptr != nullptr) { |
| 2597 | // The chain was understood and we found the class. We still need to add the class to |
| 2598 | // the class table to protect from racy programs that can try and redefine the path list |
| 2599 | // which would change the Class<?> returned for subsequent evaluation of const-class. |
| 2600 | DCHECK(known_hierarchy); |
| 2601 | DCHECK(result_ptr->DescriptorEquals(descriptor)); |
| 2602 | descriptor_equals = true; |
| 2603 | } else { |
| 2604 | // Either the chain wasn't understood or the class wasn't found. |
| 2605 | // |
| 2606 | // If the chain was understood but we did not find the class, let the Java-side |
| 2607 | // rediscover all this and throw the exception with the right stack trace. Note that |
| 2608 | // the Java-side could still succeed for racy programs if another thread is actively |
| 2609 | // modifying the class loader's path list. |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2610 | |
Calin Juravle | ccd5695 | 2016-12-15 17:57:38 +0000 | [diff] [blame] | 2611 | if (!self->CanCallIntoJava()) { |
| 2612 | // Oops, we can't call into java so we can't run actual class-loader code. |
| 2613 | // This is true for e.g. for the compiler (jit or aot). |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2614 | ObjPtr<mirror::Throwable> pre_allocated = |
| 2615 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 2616 | self->SetException(pre_allocated); |
Vladimir Marko | 2c8c6b6 | 2016-12-01 17:42:00 +0000 | [diff] [blame] | 2617 | return nullptr; |
| 2618 | } |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2619 | |
Vladimir Marko | 5fdd778 | 2017-04-20 11:26:03 +0100 | [diff] [blame] | 2620 | // Inlined DescriptorToDot(descriptor) with extra validation. |
| 2621 | // |
| 2622 | // Throw NoClassDefFoundError early rather than potentially load a class only to fail |
| 2623 | // the DescriptorEquals() check below and give a confusing error message. For example, |
| 2624 | // when native code erroneously calls JNI GetFieldId() with signature "java/lang/String" |
| 2625 | // instead of "Ljava/lang/String;", the message below using the "dot" names would be |
| 2626 | // "class loader [...] returned class java.lang.String instead of java.lang.String". |
| 2627 | size_t descriptor_length = strlen(descriptor); |
| 2628 | if (UNLIKELY(descriptor[0] != 'L') || |
| 2629 | UNLIKELY(descriptor[descriptor_length - 1] != ';') || |
| 2630 | UNLIKELY(memchr(descriptor + 1, '.', descriptor_length - 2) != nullptr)) { |
| 2631 | ThrowNoClassDefFoundError("Invalid descriptor: %s.", descriptor); |
| 2632 | return nullptr; |
| 2633 | } |
| 2634 | std::string class_name_string(descriptor + 1, descriptor_length - 2); |
| 2635 | std::replace(class_name_string.begin(), class_name_string.end(), '/', '.'); |
| 2636 | |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2637 | ScopedLocalRef<jobject> class_loader_object( |
| 2638 | soa.Env(), soa.AddLocalReference<jobject>(class_loader.Get())); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2639 | ScopedLocalRef<jobject> result(soa.Env(), nullptr); |
| 2640 | { |
| 2641 | ScopedThreadStateChange tsc(self, kNative); |
| 2642 | ScopedLocalRef<jobject> class_name_object( |
| 2643 | soa.Env(), soa.Env()->NewStringUTF(class_name_string.c_str())); |
| 2644 | if (class_name_object.get() == nullptr) { |
| 2645 | DCHECK(self->IsExceptionPending()); // OOME. |
| 2646 | return nullptr; |
| 2647 | } |
| 2648 | CHECK(class_loader_object.get() != nullptr); |
| 2649 | result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(), |
| 2650 | WellKnownClasses::java_lang_ClassLoader_loadClass, |
| 2651 | class_name_object.get())); |
| 2652 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2653 | if (result.get() == nullptr && !self->IsExceptionPending()) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2654 | // broken loader - throw NPE to be compatible with Dalvik |
| 2655 | ThrowNullPointerException(StringPrintf("ClassLoader.loadClass returned null for %s", |
| 2656 | class_name_string.c_str()).c_str()); |
| 2657 | return nullptr; |
| 2658 | } |
| 2659 | result_ptr = soa.Decode<mirror::Class>(result.get()); |
| 2660 | // Check the name of the returned class. |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2661 | descriptor_equals = (result_ptr != nullptr) && result_ptr->DescriptorEquals(descriptor); |
Vladimir Marko | 2c8c6b6 | 2016-12-01 17:42:00 +0000 | [diff] [blame] | 2662 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2663 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2664 | |
| 2665 | if (self->IsExceptionPending()) { |
| 2666 | // If the ClassLoader threw or array class allocation failed, pass that exception up. |
| 2667 | // However, to comply with the RI behavior, first check if another thread succeeded. |
| 2668 | result_ptr = LookupClass(self, descriptor, hash, class_loader.Get()); |
| 2669 | if (result_ptr != nullptr && !result_ptr->IsErroneous()) { |
| 2670 | self->ClearException(); |
| 2671 | return EnsureResolved(self, descriptor, result_ptr); |
| 2672 | } |
| 2673 | return nullptr; |
| 2674 | } |
| 2675 | |
| 2676 | // Try to insert the class to the class table, checking for mismatch. |
| 2677 | ObjPtr<mirror::Class> old; |
| 2678 | { |
| 2679 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2680 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader.Get()); |
| 2681 | old = class_table->Lookup(descriptor, hash); |
| 2682 | if (old == nullptr) { |
| 2683 | old = result_ptr; // For the comparison below, after releasing the lock. |
| 2684 | if (descriptor_equals) { |
| 2685 | class_table->InsertWithHash(result_ptr.Ptr(), hash); |
| 2686 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader.Get()); |
| 2687 | } // else throw below, after releasing the lock. |
| 2688 | } |
| 2689 | } |
| 2690 | if (UNLIKELY(old != result_ptr)) { |
| 2691 | // Return `old` (even if `!descriptor_equals`) to mimic the RI behavior for parallel |
| 2692 | // capable class loaders. (All class loaders are considered parallel capable on Android.) |
| 2693 | mirror::Class* loader_class = class_loader->GetClass(); |
| 2694 | const char* loader_class_name = |
| 2695 | loader_class->GetDexFile().StringByTypeIdx(loader_class->GetDexTypeIndex()); |
| 2696 | LOG(WARNING) << "Initiating class loader of type " << DescriptorToDot(loader_class_name) |
| 2697 | << " is not well-behaved; it returned a different Class for racing loadClass(\"" |
| 2698 | << DescriptorToDot(descriptor) << "\")."; |
| 2699 | return EnsureResolved(self, descriptor, old); |
| 2700 | } |
| 2701 | if (UNLIKELY(!descriptor_equals)) { |
| 2702 | std::string result_storage; |
| 2703 | const char* result_name = result_ptr->GetDescriptor(&result_storage); |
| 2704 | std::string loader_storage; |
| 2705 | const char* loader_class_name = class_loader->GetClass()->GetDescriptor(&loader_storage); |
| 2706 | ThrowNoClassDefFoundError( |
| 2707 | "Initiating class loader of type %s returned class %s instead of %s.", |
| 2708 | DescriptorToDot(loader_class_name).c_str(), |
| 2709 | DescriptorToDot(result_name).c_str(), |
| 2710 | DescriptorToDot(descriptor).c_str()); |
| 2711 | return nullptr; |
| 2712 | } |
| 2713 | // success, return mirror::Class* |
| 2714 | return result_ptr.Ptr(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2715 | } |
| 2716 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2717 | mirror::Class* ClassLinker::DefineClass(Thread* self, |
| 2718 | const char* descriptor, |
| 2719 | size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2720 | Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2721 | const DexFile& dex_file, |
| 2722 | const DexFile::ClassDef& dex_class_def) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2723 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2724 | auto klass = hs.NewHandle<mirror::Class>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2725 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2726 | // Load the class from the dex file. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2727 | if (UNLIKELY(!init_done_)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2728 | // finish up init of hand crafted class_roots_ |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2729 | if (strcmp(descriptor, "Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2730 | klass.Assign(GetClassRoot(kJavaLangObject)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2731 | } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2732 | klass.Assign(GetClassRoot(kJavaLangClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2733 | } else if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2734 | klass.Assign(GetClassRoot(kJavaLangString)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 2735 | } else if (strcmp(descriptor, "Ljava/lang/ref/Reference;") == 0) { |
| 2736 | klass.Assign(GetClassRoot(kJavaLangRefReference)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2737 | } else if (strcmp(descriptor, "Ljava/lang/DexCache;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2738 | klass.Assign(GetClassRoot(kJavaLangDexCache)); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 2739 | } else if (strcmp(descriptor, "Ldalvik/system/ClassExt;") == 0) { |
| 2740 | klass.Assign(GetClassRoot(kDalvikSystemClassExt)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2741 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2742 | } |
| 2743 | |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2744 | if (klass == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2745 | // Allocate a class with the status of not ready. |
| 2746 | // Interface object should get the right size here. Regular class will |
| 2747 | // figure out the right size later and be replaced with one of the right |
| 2748 | // size when the class becomes resolved. |
| 2749 | klass.Assign(AllocClass(self, SizeOfClassWithoutEmbeddedTables(dex_file, dex_class_def))); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2750 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2751 | if (UNLIKELY(klass == nullptr)) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2752 | self->AssertPendingOOMException(); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2753 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2754 | } |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2755 | // Get the real dex file. This will return the input if there aren't any callbacks or they do |
| 2756 | // nothing. |
| 2757 | DexFile const* new_dex_file = nullptr; |
| 2758 | DexFile::ClassDef const* new_class_def = nullptr; |
| 2759 | // TODO We should ideally figure out some way to move this after we get a lock on the klass so it |
| 2760 | // will only be called once. |
| 2761 | Runtime::Current()->GetRuntimeCallbacks()->ClassPreDefine(descriptor, |
| 2762 | klass, |
| 2763 | class_loader, |
| 2764 | dex_file, |
| 2765 | dex_class_def, |
| 2766 | &new_dex_file, |
| 2767 | &new_class_def); |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 2768 | // Check to see if an exception happened during runtime callbacks. Return if so. |
| 2769 | if (self->IsExceptionPending()) { |
| 2770 | return nullptr; |
| 2771 | } |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2772 | ObjPtr<mirror::DexCache> dex_cache = RegisterDexFile(*new_dex_file, class_loader.Get()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2773 | if (dex_cache == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 2774 | self->AssertPendingException(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2775 | return nullptr; |
| 2776 | } |
| 2777 | klass->SetDexCache(dex_cache); |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2778 | SetupClass(*new_dex_file, *new_class_def, klass, class_loader.Get()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2779 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 2780 | // Mark the string class by setting its access flag. |
| 2781 | if (UNLIKELY(!init_done_)) { |
| 2782 | if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
| 2783 | klass->SetStringClass(); |
| 2784 | } |
| 2785 | } |
| 2786 | |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 2787 | ObjectLock<mirror::Class> lock(self, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2788 | klass->SetClinitThreadId(self->GetTid()); |
Mathieu Chartier | 1e4841e | 2016-12-15 14:21:04 -0800 | [diff] [blame] | 2789 | // Make sure we have a valid empty iftable even if there are errors. |
| 2790 | klass->SetIfTable(GetClassRoot(kJavaLangObject)->GetIfTable()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2791 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2792 | // Add the newly loaded class to the loaded classes table. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2793 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, klass.Get(), hash); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2794 | if (existing != nullptr) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2795 | // We failed to insert because we raced with another thread. Calling EnsureResolved may cause |
| 2796 | // this thread to block. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2797 | return EnsureResolved(self, descriptor, existing); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2798 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2799 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2800 | // Load the fields and other things after we are inserted in the table. This is so that we don't |
| 2801 | // end up allocating unfree-able linear alloc resources and then lose the race condition. The |
| 2802 | // other reason is that the field roots are only visited from the class table. So we need to be |
| 2803 | // inserted before we allocate / fill in these fields. |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2804 | LoadClass(self, *new_dex_file, *new_class_def, klass); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2805 | if (self->IsExceptionPending()) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2806 | VLOG(class_linker) << self->GetException()->Dump(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2807 | // An exception occured during load, set status to erroneous while holding klass' lock in case |
| 2808 | // notification is necessary. |
| 2809 | if (!klass->IsErroneous()) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2810 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorUnresolved, self); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2811 | } |
| 2812 | return nullptr; |
| 2813 | } |
| 2814 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2815 | // Finish loading (if necessary) by finding parents |
| 2816 | CHECK(!klass->IsLoaded()); |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2817 | if (!LoadSuperAndInterfaces(klass, *new_dex_file)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2818 | // Loading failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2819 | if (!klass->IsErroneous()) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2820 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorUnresolved, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2821 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2822 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2823 | } |
| 2824 | CHECK(klass->IsLoaded()); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 2825 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 2826 | // At this point the class is loaded. Publish a ClassLoad event. |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 2827 | // 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] | 2828 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(klass); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 2829 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2830 | // Link the class (if necessary) |
| 2831 | CHECK(!klass->IsResolved()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2832 | // TODO: Use fast jobjects? |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2833 | auto interfaces = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2834 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 2835 | MutableHandle<mirror::Class> h_new_class = hs.NewHandle<mirror::Class>(nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2836 | if (!LinkClass(self, descriptor, klass, interfaces, &h_new_class)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2837 | // Linking failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2838 | if (!klass->IsErroneous()) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2839 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorUnresolved, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2840 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2841 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2842 | } |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 2843 | self->AssertNoPendingException(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2844 | CHECK(h_new_class != nullptr) << descriptor; |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2845 | CHECK(h_new_class->IsResolved() && !h_new_class->IsErroneousResolved()) << descriptor; |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2846 | |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 2847 | // Instrumentation may have updated entrypoints for all methods of all |
| 2848 | // classes. However it could not update methods of this class while we |
| 2849 | // were loading it. Now the class is resolved, we can update entrypoints |
| 2850 | // as required by instrumentation. |
| 2851 | if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) { |
| 2852 | // We must be in the kRunnable state to prevent instrumentation from |
| 2853 | // suspending all threads to update entrypoints while we are doing it |
| 2854 | // for this class. |
| 2855 | DCHECK_EQ(self->GetState(), kRunnable); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 2856 | Runtime::Current()->GetInstrumentation()->InstallStubsForClass(h_new_class.Get()); |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 2857 | } |
| 2858 | |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2859 | /* |
| 2860 | * We send CLASS_PREPARE events to the debugger from here. The |
| 2861 | * definition of "preparation" is creating the static fields for a |
| 2862 | * class and initializing them to the standard default values, but not |
| 2863 | * executing any code (that comes later, during "initialization"). |
| 2864 | * |
| 2865 | * We did the static preparation in LinkClass. |
| 2866 | * |
| 2867 | * The class has been prepared and resolved but possibly not yet verified |
| 2868 | * at this point. |
| 2869 | */ |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 2870 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(klass, h_new_class); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2871 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 2872 | // Notify native debugger of the new class and its layout. |
| 2873 | jit::Jit::NewTypeLoadedIfUsingJit(h_new_class.Get()); |
| 2874 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 2875 | return h_new_class.Get(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2876 | } |
| 2877 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2878 | uint32_t ClassLinker::SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, |
| 2879 | const DexFile::ClassDef& dex_class_def) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2880 | const uint8_t* class_data = dex_file.GetClassData(dex_class_def); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2881 | size_t num_ref = 0; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2882 | size_t num_8 = 0; |
| 2883 | size_t num_16 = 0; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2884 | size_t num_32 = 0; |
| 2885 | size_t num_64 = 0; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2886 | if (class_data != nullptr) { |
Vladimir Marko | 879d27b | 2016-03-15 20:31:50 +0000 | [diff] [blame] | 2887 | // We allow duplicate definitions of the same field in a class_data_item |
| 2888 | // but ignore the repeated indexes here, b/21868015. |
| 2889 | uint32_t last_field_idx = DexFile::kDexNoIndex; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2890 | for (ClassDataItemIterator it(dex_file, class_data); it.HasNextStaticField(); it.Next()) { |
Vladimir Marko | 879d27b | 2016-03-15 20:31:50 +0000 | [diff] [blame] | 2891 | uint32_t field_idx = it.GetMemberIndex(); |
| 2892 | // Ordering enforced by DexFileVerifier. |
| 2893 | DCHECK(last_field_idx == DexFile::kDexNoIndex || last_field_idx <= field_idx); |
| 2894 | if (UNLIKELY(field_idx == last_field_idx)) { |
| 2895 | continue; |
| 2896 | } |
| 2897 | last_field_idx = field_idx; |
| 2898 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2899 | const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2900 | char c = descriptor[0]; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2901 | switch (c) { |
| 2902 | case 'L': |
| 2903 | case '[': |
| 2904 | num_ref++; |
| 2905 | break; |
| 2906 | case 'J': |
| 2907 | case 'D': |
| 2908 | num_64++; |
| 2909 | break; |
| 2910 | case 'I': |
| 2911 | case 'F': |
| 2912 | num_32++; |
| 2913 | break; |
| 2914 | case 'S': |
| 2915 | case 'C': |
| 2916 | num_16++; |
| 2917 | break; |
| 2918 | case 'B': |
| 2919 | case 'Z': |
| 2920 | num_8++; |
| 2921 | break; |
| 2922 | default: |
| 2923 | LOG(FATAL) << "Unknown descriptor: " << c; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 2924 | UNREACHABLE(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2925 | } |
| 2926 | } |
| 2927 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2928 | return mirror::Class::ComputeClassSize(false, |
| 2929 | 0, |
| 2930 | num_8, |
| 2931 | num_16, |
| 2932 | num_32, |
| 2933 | num_64, |
| 2934 | num_ref, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2935 | image_pointer_size_); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2936 | } |
| 2937 | |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2938 | // Special case to get oat code without overwriting a trampoline. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2939 | const void* ClassLinker::GetQuickOatCodeFor(ArtMethod* method) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2940 | CHECK(method->IsInvokable()) << method->PrettyMethod(); |
Nicolas Geoffray | a7a4759 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 2941 | if (method->IsProxyMethod()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2942 | return GetQuickProxyInvokeHandler(); |
Jeff Hao | 8df6cea | 2013-07-29 13:54:48 -0700 | [diff] [blame] | 2943 | } |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 2944 | auto* code = method->GetOatMethodQuickCode(GetImagePointerSize()); |
| 2945 | if (code != nullptr) { |
| 2946 | return code; |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame] | 2947 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2948 | if (method->IsNative()) { |
| 2949 | // No code and native? Use generic trampoline. |
| 2950 | return GetQuickGenericJniStub(); |
| 2951 | } |
| 2952 | return GetQuickToInterpreterBridge(); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2953 | } |
| 2954 | |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2955 | bool ClassLinker::ShouldUseInterpreterEntrypoint(ArtMethod* method, const void* quick_code) { |
| 2956 | if (UNLIKELY(method->IsNative() || method->IsProxyMethod())) { |
| 2957 | return false; |
| 2958 | } |
| 2959 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2960 | if (quick_code == nullptr) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2961 | return true; |
| 2962 | } |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2963 | |
| 2964 | Runtime* runtime = Runtime::Current(); |
| 2965 | instrumentation::Instrumentation* instr = runtime->GetInstrumentation(); |
| 2966 | if (instr->InterpretOnly()) { |
| 2967 | return true; |
| 2968 | } |
| 2969 | |
| 2970 | if (runtime->GetClassLinker()->IsQuickToInterpreterBridge(quick_code)) { |
| 2971 | // Doing this check avoids doing compiled/interpreter transitions. |
| 2972 | return true; |
| 2973 | } |
| 2974 | |
| 2975 | if (Dbg::IsForcedInterpreterNeededForCalling(Thread::Current(), method)) { |
| 2976 | // Force the use of interpreter when it is required by the debugger. |
| 2977 | return true; |
| 2978 | } |
| 2979 | |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 2980 | if (runtime->IsJavaDebuggable()) { |
| 2981 | // For simplicity, we ignore precompiled code and go to the interpreter |
| 2982 | // assuming we don't already have jitted code. |
| 2983 | // We could look at the oat file where `quick_code` is being defined, |
| 2984 | // and check whether it's been compiled debuggable, but we decided to |
| 2985 | // only rely on the JIT for debuggable apps. |
Alex Light | 6b16d89 | 2016-11-11 11:21:04 -0800 | [diff] [blame] | 2986 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 2987 | return (jit == nullptr) || !jit->GetCodeCache()->ContainsPc(quick_code); |
| 2988 | } |
| 2989 | |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 2990 | if (runtime->IsNativeDebuggable()) { |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 2991 | DCHECK(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse()); |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 2992 | // If we are doing native debugging, ignore application's AOT code, |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 2993 | // since we want to JIT it (at first use) with extra stackmaps for native |
| 2994 | // debugging. We keep however all AOT code from the boot image, |
| 2995 | // since the JIT-at-first-use is blocking and would result in non-negligible |
| 2996 | // startup performance impact. |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 2997 | return !runtime->GetHeap()->IsInBootImageOatFile(quick_code); |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | return false; |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3001 | } |
| 3002 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3003 | void ClassLinker::FixupStaticTrampolines(ObjPtr<mirror::Class> klass) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3004 | DCHECK(klass->IsInitialized()) << klass->PrettyDescriptor(); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 3005 | if (klass->NumDirectMethods() == 0) { |
| 3006 | return; // No direct methods => no static methods. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3007 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3008 | Runtime* runtime = Runtime::Current(); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 3009 | if (!runtime->IsStarted()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 3010 | if (runtime->IsAotCompiler() || runtime->GetHeap()->HasBootImageSpace()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 3011 | return; // OAT file unavailable. |
| 3012 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3013 | } |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 3014 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 3015 | const DexFile& dex_file = klass->GetDexFile(); |
| 3016 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 3017 | CHECK(dex_class_def != nullptr); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 3018 | const uint8_t* class_data = dex_file.GetClassData(*dex_class_def); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 3019 | // There should always be class data if there were direct methods. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3020 | CHECK(class_data != nullptr) << klass->PrettyDescriptor(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3021 | ClassDataItemIterator it(dex_file, class_data); |
Mathieu Chartier | e17cf24 | 2017-06-19 11:05:51 -0700 | [diff] [blame] | 3022 | it.SkipAllFields(); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 3023 | bool has_oat_class; |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3024 | OatFile::OatClass oat_class = OatFile::FindOatClass(dex_file, |
| 3025 | klass->GetDexClassDefIndex(), |
| 3026 | &has_oat_class); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 3027 | // Link the code of methods skipped by LinkCode. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3028 | for (size_t method_index = 0; it.HasNextDirectMethod(); ++method_index, it.Next()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3029 | ArtMethod* method = klass->GetDirectMethod(method_index, image_pointer_size_); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3030 | if (!method->IsStatic()) { |
| 3031 | // Only update static methods. |
| 3032 | continue; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3033 | } |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3034 | const void* quick_code = nullptr; |
| 3035 | if (has_oat_class) { |
| 3036 | OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3037 | quick_code = oat_method.GetQuickCode(); |
| 3038 | } |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3039 | // Check whether the method is native, in which case it's generic JNI. |
| 3040 | if (quick_code == nullptr && method->IsNative()) { |
| 3041 | quick_code = GetQuickGenericJniStub(); |
| 3042 | } else if (ShouldUseInterpreterEntrypoint(method, quick_code)) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3043 | // Use interpreter entry point. |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3044 | quick_code = GetQuickToInterpreterBridge(); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3045 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 3046 | runtime->GetInstrumentation()->UpdateMethodsCode(method, quick_code); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3047 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3048 | // Ignore virtual methods on the iterator. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3049 | } |
| 3050 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3051 | // Does anything needed to make sure that the compiler will not generate a direct invoke to this |
| 3052 | // method. Should only be called on non-invokable methods. |
| 3053 | inline void EnsureThrowsInvocationError(ClassLinker* class_linker, ArtMethod* method) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3054 | DCHECK(method != nullptr); |
| 3055 | DCHECK(!method->IsInvokable()); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3056 | method->SetEntryPointFromQuickCompiledCodePtrSize( |
| 3057 | class_linker->GetQuickToInterpreterBridgeTrampoline(), |
| 3058 | class_linker->GetImagePointerSize()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3059 | } |
| 3060 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3061 | static void LinkCode(ClassLinker* class_linker, |
| 3062 | ArtMethod* method, |
| 3063 | const OatFile::OatClass* oat_class, |
| 3064 | uint32_t class_def_method_index) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3065 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3066 | if (runtime->IsAotCompiler()) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3067 | // The following code only applies to a non-compiler runtime. |
| 3068 | return; |
| 3069 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3070 | // Method shouldn't have already been linked. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3071 | DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3072 | if (oat_class != nullptr) { |
| 3073 | // Every kind of method should at least get an invoke stub from the oat_method. |
| 3074 | // non-abstract methods also get their code pointers. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 3075 | const OatFile::OatMethod oat_method = oat_class->GetOatMethod(class_def_method_index); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3076 | oat_method.LinkMethod(method); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3077 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3078 | |
Tamas Berghammer | 3a98aae | 2016-02-08 20:21:54 +0000 | [diff] [blame] | 3079 | // Install entry point from interpreter. |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3080 | const void* quick_code = method->GetEntryPointFromQuickCompiledCode(); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3081 | bool enter_interpreter = class_linker->ShouldUseInterpreterEntrypoint(method, quick_code); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 3082 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3083 | if (!method->IsInvokable()) { |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3084 | EnsureThrowsInvocationError(class_linker, method); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3085 | return; |
| 3086 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3087 | |
| 3088 | if (method->IsStatic() && !method->IsConstructor()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3089 | // For static methods excluding the class initializer, install the trampoline. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3090 | // It will be replaced by the proper entry point by ClassLinker::FixupStaticTrampolines |
| 3091 | // after initializing class (see ClassLinker::InitializeClass method). |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3092 | method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionStub()); |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3093 | } else if (quick_code == nullptr && method->IsNative()) { |
| 3094 | method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub()); |
Tamas Berghammer | 3a98aae | 2016-02-08 20:21:54 +0000 | [diff] [blame] | 3095 | } else if (enter_interpreter) { |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3096 | // Set entry point from compiled code if there's no code or in interpreter only mode. |
| 3097 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
Ian Rogers | 0d6de04 | 2012-02-29 08:50:26 -0800 | [diff] [blame] | 3098 | } |
jeffhao | 26c0a1a | 2012-01-17 16:28:33 -0800 | [diff] [blame] | 3099 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3100 | if (method->IsNative()) { |
| 3101 | // Unregistering restores the dlsym lookup stub. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3102 | method->UnregisterNative(); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 3103 | |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3104 | if (enter_interpreter || quick_code == nullptr) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3105 | // We have a native method here without code. Then it should have either the generic JNI |
| 3106 | // trampoline as entrypoint (non-static), or the resolution trampoline (static). |
| 3107 | // TODO: this doesn't handle all the cases where trampolines may be installed. |
| 3108 | const void* entry_point = method->GetEntryPointFromQuickCompiledCode(); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3109 | DCHECK(class_linker->IsQuickGenericJniStub(entry_point) || |
| 3110 | class_linker->IsQuickResolutionStub(entry_point)); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 3111 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3112 | } |
| 3113 | } |
| 3114 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3115 | void ClassLinker::SetupClass(const DexFile& dex_file, |
| 3116 | const DexFile::ClassDef& dex_class_def, |
| 3117 | Handle<mirror::Class> klass, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3118 | ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3119 | CHECK(klass != nullptr); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3120 | CHECK(klass->GetDexCache() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3121 | CHECK_EQ(mirror::Class::kStatusNotReady, klass->GetStatus()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 3122 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3123 | CHECK(descriptor != nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3124 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3125 | klass->SetClass(GetClassRoot(kJavaLangClass)); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 3126 | uint32_t access_flags = dex_class_def.GetJavaAccessFlags(); |
Brian Carlstrom | 8e3fb14 | 2013-10-09 21:00:27 -0700 | [diff] [blame] | 3127 | CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3128 | klass->SetAccessFlags(access_flags); |
| 3129 | klass->SetClassLoader(class_loader); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3130 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3131 | mirror::Class::SetStatus(klass, mirror::Class::kStatusIdx, nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3132 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3133 | klass->SetDexClassDefIndex(dex_file.GetIndexForClassDef(dex_class_def)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3134 | klass->SetDexTypeIndex(dex_class_def.class_idx_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3135 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3136 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3137 | void ClassLinker::LoadClass(Thread* self, |
| 3138 | const DexFile& dex_file, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3139 | const DexFile::ClassDef& dex_class_def, |
| 3140 | Handle<mirror::Class> klass) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 3141 | const uint8_t* class_data = dex_file.GetClassData(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3142 | if (class_data == nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3143 | return; // no fields or methods - for example a marker interface |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3144 | } |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3145 | LoadClassMembers(self, dex_file, class_data, klass); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 3146 | } |
| 3147 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3148 | LengthPrefixedArray<ArtField>* ClassLinker::AllocArtFieldArray(Thread* self, |
| 3149 | LinearAlloc* allocator, |
| 3150 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3151 | if (length == 0) { |
| 3152 | return nullptr; |
| 3153 | } |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3154 | // If the ArtField alignment changes, review all uses of LengthPrefixedArray<ArtField>. |
| 3155 | static_assert(alignof(ArtField) == 4, "ArtField alignment is expected to be 4."); |
| 3156 | size_t storage_size = LengthPrefixedArray<ArtField>::ComputeSize(length); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3157 | void* array_storage = allocator->Alloc(self, storage_size); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3158 | auto* ret = new(array_storage) LengthPrefixedArray<ArtField>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3159 | CHECK(ret != nullptr); |
| 3160 | std::uninitialized_fill_n(&ret->At(0), length, ArtField()); |
| 3161 | return ret; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3162 | } |
| 3163 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3164 | LengthPrefixedArray<ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self, |
| 3165 | LinearAlloc* allocator, |
| 3166 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3167 | if (length == 0) { |
| 3168 | return nullptr; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3169 | } |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 3170 | const size_t method_alignment = ArtMethod::Alignment(image_pointer_size_); |
| 3171 | const size_t method_size = ArtMethod::Size(image_pointer_size_); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3172 | const size_t storage_size = |
| 3173 | LengthPrefixedArray<ArtMethod>::ComputeSize(length, method_size, method_alignment); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3174 | void* array_storage = allocator->Alloc(self, storage_size); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3175 | auto* ret = new (array_storage) LengthPrefixedArray<ArtMethod>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3176 | CHECK(ret != nullptr); |
| 3177 | for (size_t i = 0; i < length; ++i) { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3178 | new(reinterpret_cast<void*>(&ret->At(i, method_size, method_alignment))) ArtMethod; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3179 | } |
| 3180 | return ret; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3181 | } |
| 3182 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3183 | LinearAlloc* ClassLinker::GetAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3184 | if (class_loader == nullptr) { |
| 3185 | return Runtime::Current()->GetLinearAlloc(); |
| 3186 | } |
| 3187 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3188 | DCHECK(allocator != nullptr); |
| 3189 | return allocator; |
| 3190 | } |
| 3191 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3192 | LinearAlloc* ClassLinker::GetOrCreateAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3193 | if (class_loader == nullptr) { |
| 3194 | return Runtime::Current()->GetLinearAlloc(); |
| 3195 | } |
| 3196 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3197 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3198 | if (allocator == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 3199 | RegisterClassLoader(class_loader); |
| 3200 | allocator = class_loader->GetAllocator(); |
| 3201 | CHECK(allocator != nullptr); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3202 | } |
| 3203 | return allocator; |
| 3204 | } |
| 3205 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3206 | void ClassLinker::LoadClassMembers(Thread* self, |
| 3207 | const DexFile& dex_file, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 3208 | const uint8_t* class_data, |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3209 | Handle<mirror::Class> klass) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3210 | { |
| 3211 | // Note: We cannot have thread suspension until the field and method arrays are setup or else |
| 3212 | // Class::VisitFieldRoots may miss some fields or methods. |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3213 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3214 | // Load static fields. |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3215 | // We allow duplicate definitions of the same field in a class_data_item |
| 3216 | // but ignore the repeated indexes here, b/21868015. |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3217 | LinearAlloc* const allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3218 | ClassDataItemIterator it(dex_file, class_data); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3219 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, |
| 3220 | allocator, |
| 3221 | it.NumStaticFields()); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3222 | size_t num_sfields = 0; |
| 3223 | uint32_t last_field_idx = 0u; |
| 3224 | for (; it.HasNextStaticField(); it.Next()) { |
| 3225 | uint32_t field_idx = it.GetMemberIndex(); |
| 3226 | DCHECK_GE(field_idx, last_field_idx); // Ordering enforced by DexFileVerifier. |
| 3227 | if (num_sfields == 0 || LIKELY(field_idx > last_field_idx)) { |
| 3228 | DCHECK_LT(num_sfields, it.NumStaticFields()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3229 | LoadField(it, klass, &sfields->At(num_sfields)); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3230 | ++num_sfields; |
| 3231 | last_field_idx = field_idx; |
| 3232 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3233 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3234 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3235 | // Load instance fields. |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3236 | LengthPrefixedArray<ArtField>* ifields = AllocArtFieldArray(self, |
| 3237 | allocator, |
| 3238 | it.NumInstanceFields()); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3239 | size_t num_ifields = 0u; |
| 3240 | last_field_idx = 0u; |
| 3241 | for (; it.HasNextInstanceField(); it.Next()) { |
| 3242 | uint32_t field_idx = it.GetMemberIndex(); |
| 3243 | DCHECK_GE(field_idx, last_field_idx); // Ordering enforced by DexFileVerifier. |
| 3244 | if (num_ifields == 0 || LIKELY(field_idx > last_field_idx)) { |
| 3245 | DCHECK_LT(num_ifields, it.NumInstanceFields()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3246 | LoadField(it, klass, &ifields->At(num_ifields)); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3247 | ++num_ifields; |
| 3248 | last_field_idx = field_idx; |
| 3249 | } |
| 3250 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3251 | |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3252 | if (UNLIKELY(num_sfields != it.NumStaticFields()) || |
| 3253 | UNLIKELY(num_ifields != it.NumInstanceFields())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3254 | LOG(WARNING) << "Duplicate fields in class " << klass->PrettyDescriptor() |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3255 | << " (unique static fields: " << num_sfields << "/" << it.NumStaticFields() |
| 3256 | << ", unique instance fields: " << num_ifields << "/" << it.NumInstanceFields() << ")"; |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 3257 | // NOTE: Not shrinking the over-allocated sfields/ifields, just setting size. |
| 3258 | if (sfields != nullptr) { |
| 3259 | sfields->SetSize(num_sfields); |
| 3260 | } |
| 3261 | if (ifields != nullptr) { |
| 3262 | ifields->SetSize(num_ifields); |
| 3263 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3264 | } |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 3265 | // Set the field arrays. |
| 3266 | klass->SetSFieldsPtr(sfields); |
| 3267 | DCHECK_EQ(klass->NumStaticFields(), num_sfields); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3268 | klass->SetIFieldsPtr(ifields); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3269 | DCHECK_EQ(klass->NumInstanceFields(), num_ifields); |
| 3270 | // Load methods. |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3271 | bool has_oat_class = false; |
| 3272 | const OatFile::OatClass oat_class = |
| 3273 | (Runtime::Current()->IsStarted() && !Runtime::Current()->IsAotCompiler()) |
| 3274 | ? OatFile::FindOatClass(dex_file, klass->GetDexClassDefIndex(), &has_oat_class) |
| 3275 | : OatFile::OatClass::Invalid(); |
| 3276 | const OatFile::OatClass* oat_class_ptr = has_oat_class ? &oat_class : nullptr; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 3277 | klass->SetMethodsPtr( |
| 3278 | AllocArtMethodArray(self, allocator, it.NumDirectMethods() + it.NumVirtualMethods()), |
| 3279 | it.NumDirectMethods(), |
| 3280 | it.NumVirtualMethods()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3281 | size_t class_def_method_index = 0; |
| 3282 | uint32_t last_dex_method_index = DexFile::kDexNoIndex; |
| 3283 | size_t last_class_def_method_index = 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 3284 | // TODO These should really use the iterators. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3285 | for (size_t i = 0; it.HasNextDirectMethod(); i++, it.Next()) { |
| 3286 | ArtMethod* method = klass->GetDirectMethodUnchecked(i, image_pointer_size_); |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3287 | LoadMethod(dex_file, it, klass, method); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3288 | LinkCode(this, method, oat_class_ptr, class_def_method_index); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3289 | uint32_t it_method_index = it.GetMemberIndex(); |
| 3290 | if (last_dex_method_index == it_method_index) { |
| 3291 | // duplicate case |
| 3292 | method->SetMethodIndex(last_class_def_method_index); |
| 3293 | } else { |
| 3294 | method->SetMethodIndex(class_def_method_index); |
| 3295 | last_dex_method_index = it_method_index; |
| 3296 | last_class_def_method_index = class_def_method_index; |
| 3297 | } |
| 3298 | class_def_method_index++; |
| 3299 | } |
| 3300 | for (size_t i = 0; it.HasNextVirtualMethod(); i++, it.Next()) { |
| 3301 | ArtMethod* method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3302 | LoadMethod(dex_file, it, klass, method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3303 | DCHECK_EQ(class_def_method_index, it.NumDirectMethods() + i); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3304 | LinkCode(this, method, oat_class_ptr, class_def_method_index); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3305 | class_def_method_index++; |
| 3306 | } |
| 3307 | DCHECK(!it.HasNext()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3308 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 3309 | // Ensure that the card is marked so that remembered sets pick up native roots. |
| 3310 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(klass.Get()); |
Mathieu Chartier | f3f2a7a | 2015-04-14 15:43:10 -0700 | [diff] [blame] | 3311 | self->AllowThreadSuspension(); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3312 | } |
| 3313 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3314 | void ClassLinker::LoadField(const ClassDataItemIterator& it, |
| 3315 | Handle<mirror::Class> klass, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3316 | ArtField* dst) { |
| 3317 | const uint32_t field_idx = it.GetMemberIndex(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3318 | dst->SetDexFieldIndex(field_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3319 | dst->SetDeclaringClass(klass.Get()); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 3320 | dst->SetAccessFlags(it.GetFieldAccessFlags()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3321 | } |
| 3322 | |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3323 | void ClassLinker::LoadMethod(const DexFile& dex_file, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3324 | const ClassDataItemIterator& it, |
| 3325 | Handle<mirror::Class> klass, |
| 3326 | ArtMethod* dst) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3327 | uint32_t dex_method_idx = it.GetMemberIndex(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3328 | const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3329 | const char* method_name = dex_file.StringDataByIdx(method_id.name_idx_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3330 | |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3331 | ScopedAssertNoThreadSuspension ants("LoadMethod"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3332 | dst->SetDexMethodIndex(dex_method_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3333 | dst->SetDeclaringClass(klass.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3334 | dst->SetCodeItemOffset(it.GetMethodCodeItemOffset()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3335 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 3336 | dst->SetDexCacheResolvedMethods(klass->GetDexCache()->GetResolvedMethods(), image_pointer_size_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3337 | |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 3338 | uint32_t access_flags = it.GetMethodAccessFlags(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3339 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3340 | if (UNLIKELY(strcmp("finalize", method_name) == 0)) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3341 | // Set finalizable flag on declaring class. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3342 | if (strcmp("V", dex_file.GetShorty(method_id.proto_idx_)) == 0) { |
| 3343 | // Void return type. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3344 | if (klass->GetClassLoader() != nullptr) { // All non-boot finalizer methods are flagged. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3345 | klass->SetFinalizable(); |
| 3346 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3347 | std::string temp; |
| 3348 | const char* klass_descriptor = klass->GetDescriptor(&temp); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3349 | // The Enum class declares a "final" finalize() method to prevent subclasses from |
| 3350 | // introducing a finalizer. We don't want to set the finalizable flag for Enum or its |
| 3351 | // subclasses, so we exclude it here. |
| 3352 | // We also want to avoid setting the flag on Object, where we know that finalize() is |
| 3353 | // empty. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3354 | if (strcmp(klass_descriptor, "Ljava/lang/Object;") != 0 && |
| 3355 | strcmp(klass_descriptor, "Ljava/lang/Enum;") != 0) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3356 | klass->SetFinalizable(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3357 | } |
| 3358 | } |
| 3359 | } |
| 3360 | } else if (method_name[0] == '<') { |
| 3361 | // Fix broken access flags for initializers. Bug 11157540. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3362 | bool is_init = (strcmp("<init>", method_name) == 0); |
| 3363 | bool is_clinit = !is_init && (strcmp("<clinit>", method_name) == 0); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3364 | if (UNLIKELY(!is_init && !is_clinit)) { |
| 3365 | LOG(WARNING) << "Unexpected '<' at start of method name " << method_name; |
| 3366 | } else { |
| 3367 | if (UNLIKELY((access_flags & kAccConstructor) == 0)) { |
| 3368 | 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] | 3369 | << klass->PrettyDescriptor() << " in dex file " << dex_file.GetLocation(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3370 | access_flags |= kAccConstructor; |
| 3371 | } |
| 3372 | } |
| 3373 | } |
| 3374 | dst->SetAccessFlags(access_flags); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3375 | } |
| 3376 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3377 | void ClassLinker::AppendToBootClassPath(Thread* self, const DexFile& dex_file) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3378 | ObjPtr<mirror::DexCache> dex_cache = AllocAndInitializeDexCache( |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3379 | self, |
| 3380 | dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3381 | Runtime::Current()->GetLinearAlloc()); |
| 3382 | 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] | 3383 | AppendToBootClassPath(dex_file, dex_cache); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 3384 | } |
| 3385 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3386 | void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3387 | ObjPtr<mirror::DexCache> dex_cache) { |
| 3388 | CHECK(dex_cache != nullptr) << dex_file.GetLocation(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 3389 | boot_class_path_.push_back(&dex_file); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3390 | RegisterBootClassPathDexFile(dex_file, dex_cache); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3391 | } |
| 3392 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3393 | void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3394 | ObjPtr<mirror::DexCache> dex_cache, |
| 3395 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3396 | Thread* const self = Thread::Current(); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3397 | Locks::dex_lock_->AssertExclusiveHeld(self); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3398 | CHECK(dex_cache != nullptr) << dex_file.GetLocation(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3399 | // For app images, the dex cache location may be a suffix of the dex file location since the |
| 3400 | // dex file location is an absolute path. |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 3401 | const std::string dex_cache_location = dex_cache->GetLocation()->ToModifiedUtf8(); |
| 3402 | const size_t dex_cache_length = dex_cache_location.length(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3403 | CHECK_GT(dex_cache_length, 0u) << dex_file.GetLocation(); |
| 3404 | std::string dex_file_location = dex_file.GetLocation(); |
| 3405 | CHECK_GE(dex_file_location.length(), dex_cache_length) |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 3406 | << dex_cache_location << " " << dex_file.GetLocation(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3407 | // Take suffix. |
| 3408 | const std::string dex_file_suffix = dex_file_location.substr( |
| 3409 | dex_file_location.length() - dex_cache_length, |
| 3410 | dex_cache_length); |
| 3411 | // Example dex_cache location is SettingsProvider.apk and |
| 3412 | // dex file location is /system/priv-app/SettingsProvider/SettingsProvider.apk |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 3413 | CHECK_EQ(dex_cache_location, dex_file_suffix); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3414 | const OatFile* oat_file = |
| 3415 | (dex_file.GetOatDexFile() != nullptr) ? dex_file.GetOatDexFile()->GetOatFile() : nullptr; |
| 3416 | // Clean up pass to remove null dex caches. Also check if we need to initialize OatFile .bss. |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3417 | // Null dex caches can occur due to class unloading and we are lazily removing null entries. |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3418 | bool initialize_oat_file_bss = (oat_file != nullptr); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3419 | JavaVMExt* const vm = self->GetJniEnv()->vm; |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3420 | for (auto it = dex_caches_.begin(); it != dex_caches_.end(); ) { |
| 3421 | DexCacheData data = *it; |
| 3422 | if (self->IsJWeakCleared(data.weak_root)) { |
| 3423 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3424 | it = dex_caches_.erase(it); |
| 3425 | } else { |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3426 | if (initialize_oat_file_bss && |
| 3427 | it->dex_file->GetOatDexFile() != nullptr && |
| 3428 | it->dex_file->GetOatDexFile()->GetOatFile() == oat_file) { |
| 3429 | initialize_oat_file_bss = false; // Already initialized. |
| 3430 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3431 | ++it; |
| 3432 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3433 | } |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3434 | if (initialize_oat_file_bss) { |
| 3435 | // TODO: Pre-initialize from boot/app image? |
| 3436 | ArtMethod* resolution_method = Runtime::Current()->GetResolutionMethod(); |
| 3437 | for (ArtMethod*& entry : oat_file->GetBssMethods()) { |
| 3438 | entry = resolution_method; |
| 3439 | } |
| 3440 | } |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3441 | jweak dex_cache_jweak = vm->AddWeakGlobalRef(self, dex_cache); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3442 | dex_cache->SetDexFile(&dex_file); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3443 | DexCacheData data; |
| 3444 | data.weak_root = dex_cache_jweak; |
| 3445 | data.dex_file = dex_cache->GetDexFile(); |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 3446 | data.resolved_methods = dex_cache->GetResolvedMethods(); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3447 | data.class_table = ClassTableForClassLoader(class_loader); |
| 3448 | DCHECK(data.class_table != nullptr); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3449 | dex_caches_.push_back(data); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3450 | } |
| 3451 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3452 | ObjPtr<mirror::DexCache> ClassLinker::DecodeDexCache(Thread* self, const DexCacheData& data) { |
| 3453 | return data.IsValid() |
| 3454 | ? ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root)) |
| 3455 | : nullptr; |
| 3456 | } |
| 3457 | |
| 3458 | ObjPtr<mirror::DexCache> ClassLinker::EnsureSameClassLoader( |
| 3459 | Thread* self, |
| 3460 | ObjPtr<mirror::DexCache> dex_cache, |
| 3461 | const DexCacheData& data, |
| 3462 | ObjPtr<mirror::ClassLoader> class_loader) { |
| 3463 | DCHECK_EQ(dex_cache->GetDexFile(), data.dex_file); |
| 3464 | if (data.class_table != ClassTableForClassLoader(class_loader)) { |
| 3465 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
| 3466 | "Attempt to register dex file %s with multiple class loaders", |
| 3467 | data.dex_file->GetLocation().c_str()); |
| 3468 | return nullptr; |
| 3469 | } |
| 3470 | return dex_cache; |
| 3471 | } |
| 3472 | |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3473 | void ClassLinker::RegisterExistingDexCache(ObjPtr<mirror::DexCache> dex_cache, |
| 3474 | ObjPtr<mirror::ClassLoader> class_loader) { |
| 3475 | Thread* self = Thread::Current(); |
| 3476 | StackHandleScope<2> hs(self); |
| 3477 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(dex_cache)); |
| 3478 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 3479 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 3480 | DCHECK(dex_file != nullptr) << "Attempt to register uninitialized dex_cache object!"; |
| 3481 | if (kIsDebugBuild) { |
| 3482 | DexCacheData old_data; |
| 3483 | { |
| 3484 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
| 3485 | old_data = FindDexCacheDataLocked(*dex_file); |
| 3486 | } |
| 3487 | ObjPtr<mirror::DexCache> old_dex_cache = DecodeDexCache(self, old_data); |
| 3488 | DCHECK(old_dex_cache.IsNull()) << "Attempt to manually register a dex cache thats already " |
| 3489 | << "been registered on dex file " << dex_file->GetLocation(); |
| 3490 | } |
| 3491 | ClassTable* table; |
| 3492 | { |
| 3493 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3494 | table = InsertClassTableForClassLoader(h_class_loader.Get()); |
| 3495 | } |
| 3496 | WriterMutexLock mu(self, *Locks::dex_lock_); |
| 3497 | RegisterDexFileLocked(*dex_file, h_dex_cache.Get(), h_class_loader.Get()); |
| 3498 | table->InsertStrongRoot(h_dex_cache.Get()); |
| 3499 | if (h_class_loader.Get() != nullptr) { |
| 3500 | // Since we added a strong root to the class table, do the write barrier as required for |
| 3501 | // remembered sets and generational GCs. |
| 3502 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(h_class_loader.Get()); |
| 3503 | } |
| 3504 | } |
| 3505 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3506 | ObjPtr<mirror::DexCache> ClassLinker::RegisterDexFile(const DexFile& dex_file, |
| 3507 | ObjPtr<mirror::ClassLoader> class_loader) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3508 | Thread* self = Thread::Current(); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3509 | DexCacheData old_data; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3510 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3511 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3512 | old_data = FindDexCacheDataLocked(dex_file); |
| 3513 | } |
| 3514 | ObjPtr<mirror::DexCache> old_dex_cache = DecodeDexCache(self, old_data); |
| 3515 | if (old_dex_cache != nullptr) { |
| 3516 | return EnsureSameClassLoader(self, old_dex_cache, old_data, class_loader); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3517 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3518 | LinearAlloc* const linear_alloc = GetOrCreateAllocatorForClassLoader(class_loader); |
| 3519 | DCHECK(linear_alloc != nullptr); |
| 3520 | ClassTable* table; |
| 3521 | { |
| 3522 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3523 | table = InsertClassTableForClassLoader(class_loader); |
| 3524 | } |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3525 | // Don't alloc while holding the lock, since allocation may need to |
| 3526 | // suspend all threads and another thread may need the dex_lock_ to |
| 3527 | // get to a suspend point. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3528 | StackHandleScope<3> hs(self); |
| 3529 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3530 | ObjPtr<mirror::String> location; |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 3531 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(AllocDexCache(/*out*/&location, |
| 3532 | self, |
| 3533 | dex_file))); |
| 3534 | Handle<mirror::String> h_location(hs.NewHandle(location)); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3535 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3536 | WriterMutexLock mu(self, *Locks::dex_lock_); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3537 | old_data = FindDexCacheDataLocked(dex_file); |
| 3538 | old_dex_cache = DecodeDexCache(self, old_data); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3539 | if (old_dex_cache == nullptr && h_dex_cache != nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3540 | // Do InitializeDexCache while holding dex lock to make sure two threads don't call it at the |
| 3541 | // same time with the same dex cache. Since the .bss is shared this can cause failing DCHECK |
| 3542 | // that the arrays are null. |
| 3543 | mirror::DexCache::InitializeDexCache(self, |
| 3544 | h_dex_cache.Get(), |
| 3545 | h_location.Get(), |
| 3546 | &dex_file, |
| 3547 | linear_alloc, |
| 3548 | image_pointer_size_); |
| 3549 | RegisterDexFileLocked(dex_file, h_dex_cache.Get(), h_class_loader.Get()); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3550 | } |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3551 | } |
| 3552 | if (old_dex_cache != nullptr) { |
| 3553 | // Another thread managed to initialize the dex cache faster, so use that DexCache. |
| 3554 | // If this thread encountered OOME, ignore it. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3555 | DCHECK_EQ(h_dex_cache == nullptr, self->IsExceptionPending()); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3556 | self->ClearException(); |
| 3557 | // We cannot call EnsureSameClassLoader() while holding the dex_lock_. |
| 3558 | return EnsureSameClassLoader(self, old_dex_cache, old_data, h_class_loader.Get()); |
| 3559 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3560 | if (h_dex_cache == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3561 | self->AssertPendingOOMException(); |
| 3562 | return nullptr; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3563 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3564 | table->InsertStrongRoot(h_dex_cache.Get()); |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 3565 | if (h_class_loader.Get() != nullptr) { |
| 3566 | // Since we added a strong root to the class table, do the write barrier as required for |
| 3567 | // remembered sets and generational GCs. |
| 3568 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(h_class_loader.Get()); |
| 3569 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3570 | return h_dex_cache.Get(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3571 | } |
| 3572 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3573 | void ClassLinker::RegisterBootClassPathDexFile(const DexFile& dex_file, |
| 3574 | ObjPtr<mirror::DexCache> dex_cache) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3575 | WriterMutexLock mu(Thread::Current(), *Locks::dex_lock_); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3576 | RegisterDexFileLocked(dex_file, dex_cache, /* class_loader */ nullptr); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3577 | } |
| 3578 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3579 | bool ClassLinker::IsDexFileRegistered(Thread* self, const DexFile& dex_file) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3580 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3581 | return DecodeDexCache(self, FindDexCacheDataLocked(dex_file)) != nullptr; |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3582 | } |
| 3583 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3584 | ObjPtr<mirror::DexCache> ClassLinker::FindDexCache(Thread* self, const DexFile& dex_file) { |
| 3585 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
| 3586 | ObjPtr<mirror::DexCache> dex_cache = DecodeDexCache(self, FindDexCacheDataLocked(dex_file)); |
| 3587 | if (dex_cache != nullptr) { |
| 3588 | return dex_cache; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3589 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3590 | // Failure, dump diagnostic and abort. |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3591 | for (const DexCacheData& data : dex_caches_) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3592 | if (DecodeDexCache(self, data) != nullptr) { |
Andreas Gampe | 37c5846 | 2017-03-27 15:14:27 -0700 | [diff] [blame] | 3593 | LOG(FATAL_WITHOUT_ABORT) << "Registered dex file " << data.dex_file->GetLocation(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3594 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3595 | } |
Andreas Gampe | 37c5846 | 2017-03-27 15:14:27 -0700 | [diff] [blame] | 3596 | LOG(FATAL) << "Failed to find DexCache for DexFile " << dex_file.GetLocation(); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 3597 | UNREACHABLE(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3598 | } |
| 3599 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3600 | ClassTable* ClassLinker::FindClassTable(Thread* self, ObjPtr<mirror::DexCache> dex_cache) { |
| 3601 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 3602 | DCHECK(dex_file != nullptr); |
| 3603 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
| 3604 | // Search assuming unique-ness of dex file. |
| 3605 | for (const DexCacheData& data : dex_caches_) { |
| 3606 | // Avoid decoding (and read barriers) other unrelated dex caches. |
| 3607 | if (data.dex_file == dex_file) { |
| 3608 | ObjPtr<mirror::DexCache> registered_dex_cache = DecodeDexCache(self, data); |
| 3609 | if (registered_dex_cache != nullptr) { |
| 3610 | CHECK_EQ(registered_dex_cache, dex_cache) << dex_file->GetLocation(); |
| 3611 | return data.class_table; |
| 3612 | } |
| 3613 | } |
| 3614 | } |
| 3615 | return nullptr; |
| 3616 | } |
| 3617 | |
| 3618 | ClassLinker::DexCacheData ClassLinker::FindDexCacheDataLocked(const DexFile& dex_file) { |
| 3619 | // Search assuming unique-ness of dex file. |
| 3620 | for (const DexCacheData& data : dex_caches_) { |
| 3621 | // Avoid decoding (and read barriers) other unrelated dex caches. |
| 3622 | if (data.dex_file == &dex_file) { |
| 3623 | return data; |
| 3624 | } |
| 3625 | } |
| 3626 | return DexCacheData(); |
| 3627 | } |
| 3628 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3629 | void ClassLinker::FixupDexCaches(ArtMethod* resolution_method) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3630 | Thread* const self = Thread::Current(); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3631 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3632 | for (const DexCacheData& data : dex_caches_) { |
| 3633 | if (!self->IsJWeakCleared(data.weak_root)) { |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 3634 | ObjPtr<mirror::DexCache> dex_cache = ObjPtr<mirror::DexCache>::DownCast( |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3635 | self->DecodeJObject(data.weak_root)); |
| 3636 | if (dex_cache != nullptr) { |
| 3637 | dex_cache->Fixup(resolution_method, image_pointer_size_); |
| 3638 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3639 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3640 | } |
| 3641 | } |
| 3642 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3643 | mirror::Class* ClassLinker::CreatePrimitiveClass(Thread* self, Primitive::Type type) { |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 3644 | ObjPtr<mirror::Class> klass = |
| 3645 | AllocClass(self, mirror::Class::PrimitiveClassSize(image_pointer_size_)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3646 | if (UNLIKELY(klass == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3647 | self->AssertPendingOOMException(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3648 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3649 | } |
| 3650 | return InitializePrimitiveClass(klass, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3651 | } |
| 3652 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3653 | mirror::Class* ClassLinker::InitializePrimitiveClass(ObjPtr<mirror::Class> primitive_class, |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3654 | Primitive::Type type) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3655 | CHECK(primitive_class != nullptr); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3656 | // Must hold lock on object when initializing. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3657 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3658 | StackHandleScope<1> hs(self); |
| 3659 | Handle<mirror::Class> h_class(hs.NewHandle(primitive_class)); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3660 | ObjectLock<mirror::Class> lock(self, h_class); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3661 | h_class->SetAccessFlags(kAccPublic | kAccFinal | kAccAbstract); |
| 3662 | h_class->SetPrimitiveType(type); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 3663 | h_class->SetIfTable(GetClassRoot(kJavaLangObject)->GetIfTable()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3664 | mirror::Class::SetStatus(h_class, mirror::Class::kStatusInitialized, self); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3665 | const char* descriptor = Primitive::Descriptor(type); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 3666 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, |
| 3667 | h_class.Get(), |
| 3668 | ComputeModifiedUtf8Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3669 | CHECK(existing == nullptr) << "InitPrimitiveClass(" << type << ") failed"; |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3670 | return h_class.Get(); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 3671 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3672 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3673 | // Create an array class (i.e. the class object for the array, not the |
| 3674 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 3675 | // "[Ljava/lang/String;". |
| 3676 | // |
| 3677 | // If "descriptor" refers to an array of primitives, look up the |
| 3678 | // primitive type's internally-generated class object. |
| 3679 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3680 | // "class_loader" is the class loader of the class that's referring to |
| 3681 | // us. It's used to ensure that we're looking for the element type in |
| 3682 | // the right context. It does NOT become the class loader for the |
| 3683 | // array class; that always comes from the base element class. |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3684 | // |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 3685 | // Returns null with an exception raised on failure. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 3686 | mirror::Class* ClassLinker::CreateArrayClass(Thread* self, const char* descriptor, size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3687 | Handle<mirror::ClassLoader> class_loader) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3688 | // Identify the underlying component type |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3689 | CHECK_EQ('[', descriptor[0]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3690 | StackHandleScope<2> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3691 | MutableHandle<mirror::Class> component_type(hs.NewHandle(FindClass(self, descriptor + 1, |
| 3692 | class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3693 | if (component_type == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3694 | DCHECK(self->IsExceptionPending()); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 3695 | // We need to accept erroneous classes as component types. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3696 | const size_t component_hash = ComputeModifiedUtf8Hash(descriptor + 1); |
| 3697 | component_type.Assign(LookupClass(self, descriptor + 1, component_hash, class_loader.Get())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3698 | if (component_type == nullptr) { |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 3699 | DCHECK(self->IsExceptionPending()); |
| 3700 | return nullptr; |
| 3701 | } else { |
| 3702 | self->ClearException(); |
| 3703 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3704 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 3705 | if (UNLIKELY(component_type->IsPrimitiveVoid())) { |
| 3706 | ThrowNoClassDefFoundError("Attempt to create array of void primitive type"); |
| 3707 | return nullptr; |
| 3708 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3709 | // See if the component type is already loaded. Array classes are |
| 3710 | // always associated with the class loader of their underlying |
| 3711 | // element type -- an array of Strings goes with the loader for |
| 3712 | // java/lang/String -- so we need to look for it there. (The |
| 3713 | // caller should have checked for the existence of the class |
| 3714 | // before calling here, but they did so with *their* class loader, |
| 3715 | // not the component type's loader.) |
| 3716 | // |
| 3717 | // If we find it, the caller adds "loader" to the class' initiating |
| 3718 | // loader list, which should prevent us from going through this again. |
| 3719 | // |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3720 | // This call is unnecessary if "loader" and "component_type->GetClassLoader()" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3721 | // are the same, because our caller (FindClass) just did the |
| 3722 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 3723 | // because we effectively do this lookup again when we add the new |
| 3724 | // class to the hash table --- necessary because of possible races with |
| 3725 | // other threads.) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3726 | if (class_loader.Get() != component_type->GetClassLoader()) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3727 | ObjPtr<mirror::Class> new_class = |
| 3728 | LookupClass(self, descriptor, hash, component_type->GetClassLoader()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3729 | if (new_class != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3730 | return new_class.Ptr(); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3731 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3732 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3733 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3734 | // Fill out the fields in the Class. |
| 3735 | // |
| 3736 | // It is possible to execute some methods against arrays, because |
| 3737 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 3738 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 3739 | // |
| 3740 | // Array classes are simple enough that we don't need to do a full |
| 3741 | // link step. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3742 | auto new_class = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3743 | if (UNLIKELY(!init_done_)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3744 | // Classes that were hand created, ie not by FindSystemClass |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3745 | if (strcmp(descriptor, "[Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3746 | new_class.Assign(GetClassRoot(kClassArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3747 | } else if (strcmp(descriptor, "[Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3748 | new_class.Assign(GetClassRoot(kObjectArrayClass)); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3749 | } else if (strcmp(descriptor, GetClassRootDescriptor(kJavaLangStringArrayClass)) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3750 | new_class.Assign(GetClassRoot(kJavaLangStringArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3751 | } else if (strcmp(descriptor, "[C") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3752 | new_class.Assign(GetClassRoot(kCharArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3753 | } else if (strcmp(descriptor, "[I") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3754 | new_class.Assign(GetClassRoot(kIntArrayClass)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3755 | } else if (strcmp(descriptor, "[J") == 0) { |
| 3756 | new_class.Assign(GetClassRoot(kLongArrayClass)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3757 | } |
| 3758 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3759 | if (new_class == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3760 | new_class.Assign(AllocClass(self, mirror::Array::ClassSize(image_pointer_size_))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3761 | if (new_class == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3762 | self->AssertPendingOOMException(); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3763 | return nullptr; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3764 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3765 | new_class->SetComponentType(component_type.Get()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3766 | } |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3767 | 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] | 3768 | DCHECK(new_class->GetComponentType() != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3769 | ObjPtr<mirror::Class> java_lang_Object = GetClassRoot(kJavaLangObject); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3770 | new_class->SetSuperClass(java_lang_Object); |
| 3771 | new_class->SetVTable(java_lang_Object->GetVTable()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3772 | new_class->SetPrimitiveType(Primitive::kPrimNot); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3773 | new_class->SetClassLoader(component_type->GetClassLoader()); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 3774 | if (component_type->IsPrimitive()) { |
| 3775 | new_class->SetClassFlags(mirror::kClassFlagNoReferenceFields); |
| 3776 | } else { |
| 3777 | new_class->SetClassFlags(mirror::kClassFlagObjectArray); |
| 3778 | } |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3779 | mirror::Class::SetStatus(new_class, mirror::Class::kStatusLoaded, self); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 3780 | new_class->PopulateEmbeddedVTable(image_pointer_size_); |
| 3781 | ImTable* object_imt = java_lang_Object->GetImt(image_pointer_size_); |
| 3782 | new_class->SetImt(object_imt, image_pointer_size_); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3783 | mirror::Class::SetStatus(new_class, mirror::Class::kStatusInitialized, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3784 | // don't need to set new_class->SetObjectSize(..) |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 3785 | // because Object::SizeOf delegates to Array::SizeOf |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3786 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3787 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 3788 | // interfaces. We need to set that up here, so that stuff like |
| 3789 | // "instanceof" works right. |
| 3790 | // |
| 3791 | // Note: The GC could run during the call to FindSystemClass, |
| 3792 | // so we need to make sure the class object is GC-valid while we're in |
| 3793 | // there. Do this by clearing the interface list so the GC will just |
| 3794 | // think that the entries are null. |
| 3795 | |
| 3796 | |
| 3797 | // Use the single, global copies of "interfaces" and "iftable" |
| 3798 | // (remember not to free them for arrays). |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3799 | { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3800 | ObjPtr<mirror::IfTable> array_iftable = array_iftable_.Read(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3801 | CHECK(array_iftable != nullptr); |
| 3802 | new_class->SetIfTable(array_iftable); |
| 3803 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3804 | |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 3805 | // Inherit access flags from the component type. |
| 3806 | int access_flags = new_class->GetComponentType()->GetAccessFlags(); |
| 3807 | // Lose any implementation detail flags; in particular, arrays aren't finalizable. |
| 3808 | access_flags &= kAccJavaFlagsMask; |
| 3809 | // 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] | 3810 | // and remove "interface". |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 3811 | access_flags |= kAccAbstract | kAccFinal; |
| 3812 | access_flags &= ~kAccInterface; |
| 3813 | |
| 3814 | new_class->SetAccessFlags(access_flags); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3815 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3816 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, new_class.Get(), hash); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3817 | if (existing == nullptr) { |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 3818 | // We postpone ClassLoad and ClassPrepare events to this point in time to avoid |
| 3819 | // duplicate events in case of races. Array classes don't really follow dedicated |
| 3820 | // load and prepare, anyways. |
| 3821 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(new_class); |
| 3822 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(new_class, new_class); |
| 3823 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 3824 | jit::Jit::NewTypeLoadedIfUsingJit(new_class.Get()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3825 | return new_class.Get(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3826 | } |
| 3827 | // Another thread must have loaded the class after we |
| 3828 | // started but before we finished. Abandon what we've |
| 3829 | // done. |
| 3830 | // |
| 3831 | // (Yes, this happens.) |
| 3832 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3833 | return existing.Ptr(); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3834 | } |
| 3835 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3836 | mirror::Class* ClassLinker::FindPrimitiveClass(char type) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3837 | switch (type) { |
| 3838 | case 'B': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3839 | return GetClassRoot(kPrimitiveByte); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3840 | case 'C': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3841 | return GetClassRoot(kPrimitiveChar); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3842 | case 'D': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3843 | return GetClassRoot(kPrimitiveDouble); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3844 | case 'F': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3845 | return GetClassRoot(kPrimitiveFloat); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3846 | case 'I': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3847 | return GetClassRoot(kPrimitiveInt); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3848 | case 'J': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3849 | return GetClassRoot(kPrimitiveLong); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3850 | case 'S': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3851 | return GetClassRoot(kPrimitiveShort); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3852 | case 'Z': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3853 | return GetClassRoot(kPrimitiveBoolean); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3854 | case 'V': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3855 | return GetClassRoot(kPrimitiveVoid); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3856 | default: |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3857 | break; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 3858 | } |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 3859 | std::string printable_type(PrintableChar(type)); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 3860 | ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3861 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3862 | } |
| 3863 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3864 | 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] | 3865 | if (VLOG_IS_ON(class_linker)) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3866 | ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3867 | std::string source; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3868 | if (dex_cache != nullptr) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3869 | source += " from "; |
| 3870 | source += dex_cache->GetLocation()->ToModifiedUtf8(); |
| 3871 | } |
| 3872 | LOG(INFO) << "Loaded class " << descriptor << source; |
| 3873 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3874 | { |
| 3875 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3876 | ObjPtr<mirror::ClassLoader> const class_loader = klass->GetClassLoader(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3877 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3878 | ObjPtr<mirror::Class> existing = class_table->Lookup(descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3879 | if (existing != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3880 | return existing.Ptr(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3881 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3882 | VerifyObject(klass); |
| 3883 | class_table->InsertWithHash(klass, hash); |
| 3884 | if (class_loader != nullptr) { |
| 3885 | // This is necessary because we need to have the card dirtied for remembered sets. |
| 3886 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader); |
| 3887 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 3888 | if (log_new_roots_) { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3889 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3890 | } |
| 3891 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3892 | if (kIsDebugBuild) { |
| 3893 | // Test that copied methods correctly can find their holder. |
| 3894 | for (ArtMethod& method : klass->GetCopiedMethods(image_pointer_size_)) { |
| 3895 | CHECK_EQ(GetHoldingClassOfCopiedMethod(&method), klass); |
| 3896 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3897 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3898 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3899 | } |
| 3900 | |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 3901 | void ClassLinker::WriteBarrierForBootOatFileBssRoots(const OatFile* oat_file) { |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 3902 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3903 | DCHECK(!oat_file->GetBssGcRoots().empty()) << oat_file->GetLocation(); |
| 3904 | if (log_new_roots_ && !ContainsElement(new_bss_roots_boot_oat_files_, oat_file)) { |
| 3905 | new_bss_roots_boot_oat_files_.push_back(oat_file); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 3906 | } |
| 3907 | } |
| 3908 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 3909 | // TODO This should really be in mirror::Class. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3910 | void ClassLinker::UpdateClassMethods(ObjPtr<mirror::Class> klass, |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 3911 | LengthPrefixedArray<ArtMethod>* new_methods) { |
| 3912 | klass->SetMethodsPtrUnchecked(new_methods, |
| 3913 | klass->NumDirectMethods(), |
| 3914 | klass->NumDeclaredVirtualMethods()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3915 | // 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] | 3916 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(klass); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3917 | } |
| 3918 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3919 | mirror::Class* ClassLinker::LookupClass(Thread* self, |
Andreas Gampe | 2ff3b97 | 2017-06-05 18:14:53 -0700 | [diff] [blame] | 3920 | const char* descriptor, |
| 3921 | ObjPtr<mirror::ClassLoader> class_loader) { |
| 3922 | return LookupClass(self, descriptor, ComputeModifiedUtf8Hash(descriptor), class_loader); |
| 3923 | } |
| 3924 | |
| 3925 | mirror::Class* ClassLinker::LookupClass(Thread* self, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3926 | const char* descriptor, |
| 3927 | size_t hash, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3928 | ObjPtr<mirror::ClassLoader> class_loader) { |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 3929 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3930 | ClassTable* const class_table = ClassTableForClassLoader(class_loader); |
| 3931 | if (class_table != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3932 | ObjPtr<mirror::Class> result = class_table->Lookup(descriptor, hash); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 3933 | if (result != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3934 | return result.Ptr(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3935 | } |
Sameer Abu Asal | 2c6de22 | 2013-05-02 17:38:59 -0700 | [diff] [blame] | 3936 | } |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 3937 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3938 | } |
| 3939 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3940 | class MoveClassTableToPreZygoteVisitor : public ClassLoaderVisitor { |
| 3941 | public: |
| 3942 | explicit MoveClassTableToPreZygoteVisitor() {} |
| 3943 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3944 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3945 | REQUIRES(Locks::classlinker_classes_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3946 | REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3947 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3948 | if (class_table != nullptr) { |
| 3949 | class_table->FreezeSnapshot(); |
| 3950 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 3951 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3952 | }; |
| 3953 | |
| 3954 | void ClassLinker::MoveClassTableToPreZygote() { |
| 3955 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 3956 | boot_class_table_->FreezeSnapshot(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3957 | MoveClassTableToPreZygoteVisitor visitor; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3958 | VisitClassLoaders(&visitor); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3959 | } |
| 3960 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3961 | // Look up classes by hash and descriptor and put all matching ones in the result array. |
| 3962 | class LookupClassesVisitor : public ClassLoaderVisitor { |
| 3963 | public: |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3964 | LookupClassesVisitor(const char* descriptor, |
| 3965 | size_t hash, |
| 3966 | std::vector<ObjPtr<mirror::Class>>* result) |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3967 | : descriptor_(descriptor), |
| 3968 | hash_(hash), |
| 3969 | result_(result) {} |
| 3970 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3971 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3972 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) OVERRIDE { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3973 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3974 | ObjPtr<mirror::Class> klass = class_table->Lookup(descriptor_, hash_); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 3975 | // Add `klass` only if `class_loader` is its defining (not just initiating) class loader. |
| 3976 | if (klass != nullptr && klass->GetClassLoader() == class_loader) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3977 | result_->push_back(klass); |
| 3978 | } |
| 3979 | } |
| 3980 | |
| 3981 | private: |
| 3982 | const char* const descriptor_; |
| 3983 | const size_t hash_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3984 | std::vector<ObjPtr<mirror::Class>>* const result_; |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3985 | }; |
| 3986 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3987 | void ClassLinker::LookupClasses(const char* descriptor, |
| 3988 | std::vector<ObjPtr<mirror::Class>>& result) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3989 | result.clear(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3990 | Thread* const self = Thread::Current(); |
| 3991 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3992 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 3993 | ObjPtr<mirror::Class> klass = boot_class_table_->Lookup(descriptor, hash); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3994 | if (klass != nullptr) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 3995 | DCHECK(klass->GetClassLoader() == nullptr); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3996 | result.push_back(klass); |
| 3997 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3998 | LookupClassesVisitor visitor(descriptor, hash, &result); |
| 3999 | VisitClassLoaders(&visitor); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 4000 | } |
| 4001 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4002 | bool ClassLinker::AttemptSupertypeVerification(Thread* self, |
| 4003 | Handle<mirror::Class> klass, |
| 4004 | Handle<mirror::Class> supertype) { |
| 4005 | DCHECK(self != nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4006 | DCHECK(klass != nullptr); |
| 4007 | DCHECK(supertype != nullptr); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4008 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4009 | if (!supertype->IsVerified() && !supertype->IsErroneous()) { |
| 4010 | VerifyClass(self, supertype); |
| 4011 | } |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4012 | |
| 4013 | if (supertype->IsVerified() || supertype->ShouldVerifyAtRuntime()) { |
| 4014 | // The supertype is either verified, or we soft failed at AOT time. |
| 4015 | DCHECK(supertype->IsVerified() || Runtime::Current()->IsAotCompiler()); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4016 | return true; |
| 4017 | } |
| 4018 | // If we got this far then we have a hard failure. |
| 4019 | std::string error_msg = |
| 4020 | StringPrintf("Rejecting class %s that attempts to sub-type erroneous class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4021 | klass->PrettyDescriptor().c_str(), |
| 4022 | supertype->PrettyDescriptor().c_str()); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4023 | LOG(WARNING) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4024 | StackHandleScope<1> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4025 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4026 | if (cause != nullptr) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4027 | // Set during VerifyClass call (if at all). |
| 4028 | self->ClearException(); |
| 4029 | } |
| 4030 | // Change into a verify error. |
| 4031 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4032 | if (cause != nullptr) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4033 | self->GetException()->SetCause(cause.Get()); |
| 4034 | } |
| 4035 | ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex()); |
| 4036 | if (Runtime::Current()->IsAotCompiler()) { |
| 4037 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
| 4038 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4039 | // Need to grab the lock to change status. |
| 4040 | ObjectLock<mirror::Class> super_lock(self, klass); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4041 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4042 | return false; |
| 4043 | } |
| 4044 | |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4045 | // Ensures that methods have the kAccSkipAccessChecks bit set. We use the |
| 4046 | // kAccVerificationAttempted bit on the class access flags to determine whether this has been done |
| 4047 | // before. |
| 4048 | static void EnsureSkipAccessChecksMethods(Handle<mirror::Class> klass, PointerSize pointer_size) |
| 4049 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 4050 | if (!klass->WasVerificationAttempted()) { |
| 4051 | klass->SetSkipAccessChecksFlagOnAllMethods(pointer_size); |
| 4052 | klass->SetVerificationAttempted(); |
| 4053 | } |
| 4054 | } |
| 4055 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4056 | verifier::FailureKind ClassLinker::VerifyClass( |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 4057 | Thread* self, Handle<mirror::Class> klass, verifier::HardFailLogMode log_level) { |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4058 | { |
| 4059 | // TODO: assert that the monitor on the Class is held |
| 4060 | ObjectLock<mirror::Class> lock(self, klass); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 4061 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4062 | // Is somebody verifying this now? |
| 4063 | mirror::Class::Status old_status = klass->GetStatus(); |
| 4064 | while (old_status == mirror::Class::kStatusVerifying || |
| 4065 | old_status == mirror::Class::kStatusVerifyingAtRuntime) { |
| 4066 | lock.WaitIgnoringInterrupts(); |
Mathieu Chartier | 5ef7020 | 2017-06-29 10:45:10 -0700 | [diff] [blame] | 4067 | // WaitIgnoringInterrupts can still receive an interrupt and return early, in this |
| 4068 | // case we may see the same status again. b/62912904. This is why the check is |
| 4069 | // greater or equal. |
| 4070 | CHECK(klass->IsErroneous() || (klass->GetStatus() >= old_status)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4071 | << "Class '" << klass->PrettyClass() |
| 4072 | << "' performed an illegal verification state transition from " << old_status |
| 4073 | << " to " << klass->GetStatus(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4074 | old_status = klass->GetStatus(); |
| 4075 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 4076 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4077 | // The class might already be erroneous, for example at compile time if we attempted to verify |
| 4078 | // this class as a parent to another. |
| 4079 | if (klass->IsErroneous()) { |
| 4080 | ThrowEarlierClassFailure(klass.Get()); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4081 | return verifier::FailureKind::kHardFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4082 | } |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 4083 | |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4084 | // Don't attempt to re-verify if already verified. |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4085 | if (klass->IsVerified()) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4086 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4087 | return verifier::FailureKind::kNoFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4088 | } |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4089 | |
| 4090 | // For AOT, don't attempt to re-verify if we have already found we should |
| 4091 | // verify at runtime. |
| 4092 | if (Runtime::Current()->IsAotCompiler() && klass->ShouldVerifyAtRuntime()) { |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4093 | return verifier::FailureKind::kSoftFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4094 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 4095 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4096 | if (klass->GetStatus() == mirror::Class::kStatusResolved) { |
| 4097 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerifying, self); |
| 4098 | } else { |
| 4099 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4100 | << klass->PrettyClass(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4101 | CHECK(!Runtime::Current()->IsAotCompiler()); |
| 4102 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerifyingAtRuntime, self); |
| 4103 | } |
| 4104 | |
| 4105 | // Skip verification if disabled. |
| 4106 | if (!Runtime::Current()->IsVerificationEnabled()) { |
| 4107 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, self); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4108 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4109 | return verifier::FailureKind::kNoFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4110 | } |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 4111 | } |
| 4112 | |
Bharadwaj Kalandhabhatta | 271c1e1 | 2017-06-27 11:14:49 -0700 | [diff] [blame] | 4113 | VLOG(class_linker) << "Beginning verification for class: " |
| 4114 | << klass->PrettyDescriptor() |
| 4115 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
| 4116 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 4117 | // Verify super class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4118 | StackHandleScope<2> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4119 | MutableHandle<mirror::Class> supertype(hs.NewHandle(klass->GetSuperClass())); |
| 4120 | // 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] | 4121 | if (supertype != nullptr && !AttemptSupertypeVerification(self, klass, supertype)) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4122 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4123 | return verifier::FailureKind::kHardFailure; |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4124 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4125 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4126 | // Verify all default super-interfaces. |
| 4127 | // |
| 4128 | // (1) Don't bother if the superclass has already had a soft verification failure. |
| 4129 | // |
| 4130 | // (2) Interfaces shouldn't bother to do this recursive verification because they cannot cause |
| 4131 | // recursive initialization by themselves. This is because when an interface is initialized |
| 4132 | // directly it must not initialize its superinterfaces. We are allowed to verify regardless |
| 4133 | // but choose not to for an optimization. If the interfaces is being verified due to a class |
| 4134 | // initialization (which would need all the default interfaces to be verified) the class code |
| 4135 | // will trigger the recursive verification anyway. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4136 | if ((supertype == nullptr || supertype->IsVerified()) // See (1) |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4137 | && !klass->IsInterface()) { // See (2) |
| 4138 | int32_t iftable_count = klass->GetIfTableCount(); |
| 4139 | MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 4140 | // Loop through all interfaces this class has defined. It doesn't matter the order. |
| 4141 | for (int32_t i = 0; i < iftable_count; i++) { |
| 4142 | iface.Assign(klass->GetIfTable()->GetInterface(i)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4143 | DCHECK(iface != nullptr); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4144 | // We only care if we have default interfaces and can skip if we are already verified... |
| 4145 | if (LIKELY(!iface->HasDefaultMethods() || iface->IsVerified())) { |
| 4146 | continue; |
| 4147 | } else if (UNLIKELY(!AttemptSupertypeVerification(self, klass, iface))) { |
| 4148 | // We had a hard failure while verifying this interface. Just return immediately. |
| 4149 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4150 | return verifier::FailureKind::kHardFailure; |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4151 | } else if (UNLIKELY(!iface->IsVerified())) { |
| 4152 | // We softly failed to verify the iface. Stop checking and clean up. |
| 4153 | // Put the iface into the supertype handle so we know what caused us to fail. |
| 4154 | supertype.Assign(iface.Get()); |
| 4155 | break; |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4156 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4157 | } |
| 4158 | } |
| 4159 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4160 | // At this point if verification failed, then supertype is the "first" supertype that failed |
| 4161 | // verification (without a specific order). If verification succeeded, then supertype is either |
| 4162 | // null or the original superclass of klass and is verified. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4163 | DCHECK(supertype == nullptr || |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4164 | supertype.Get() == klass->GetSuperClass() || |
| 4165 | !supertype->IsVerified()); |
| 4166 | |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 4167 | // 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] | 4168 | const DexFile& dex_file = *klass->GetDexCache()->GetDexFile(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4169 | mirror::Class::Status oat_file_class_status(mirror::Class::kStatusNotReady); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4170 | bool preverified = VerifyClassUsingOatFile(dex_file, klass.Get(), oat_file_class_status); |
Bharadwaj Kalandhabhatta | 271c1e1 | 2017-06-27 11:14:49 -0700 | [diff] [blame] | 4171 | |
| 4172 | VLOG(class_linker) << "Class preverified status for class " |
| 4173 | << klass->PrettyDescriptor() |
| 4174 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4175 | << ": " |
| 4176 | << preverified; |
| 4177 | |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4178 | // If the oat file says the class had an error, re-run the verifier. That way we will get a |
| 4179 | // precise error message. To ensure a rerun, test: |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4180 | // mirror::Class::IsErroneous(oat_file_class_status) => !preverified |
| 4181 | DCHECK(!mirror::Class::IsErroneous(oat_file_class_status) || !preverified); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4182 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4183 | std::string error_msg; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4184 | verifier::FailureKind verifier_failure = verifier::FailureKind::kNoFailure; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 4185 | if (!preverified) { |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 4186 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4187 | verifier_failure = verifier::MethodVerifier::VerifyClass(self, |
| 4188 | klass.Get(), |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 4189 | runtime->GetCompilerCallbacks(), |
| 4190 | runtime->IsAotCompiler(), |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 4191 | log_level, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4192 | &error_msg); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 4193 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4194 | |
| 4195 | // Verification is done, grab the lock again. |
| 4196 | ObjectLock<mirror::Class> lock(self, klass); |
| 4197 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4198 | if (preverified || verifier_failure != verifier::FailureKind::kHardFailure) { |
| 4199 | if (!preverified && verifier_failure != verifier::FailureKind::kNoFailure) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4200 | VLOG(class_linker) << "Soft verification failure in class " |
| 4201 | << klass->PrettyDescriptor() |
| 4202 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4203 | << " because: " << error_msg; |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 4204 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 4205 | self->AssertNoPendingException(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4206 | // Make sure all classes referenced by catch blocks are resolved. |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4207 | ResolveClassExceptionHandlerTypes(klass); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4208 | if (verifier_failure == verifier::FailureKind::kNoFailure) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4209 | // Even though there were no verifier failures we need to respect whether the super-class and |
| 4210 | // super-default-interfaces were verified or requiring runtime reverification. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4211 | if (supertype == nullptr || supertype->IsVerified()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4212 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4213 | } else { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4214 | CHECK_EQ(supertype->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4215 | mirror::Class::SetStatus(klass, mirror::Class::kStatusRetryVerificationAtRuntime, self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4216 | // 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] | 4217 | verifier_failure = verifier::FailureKind::kSoftFailure; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4218 | } |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4219 | } else { |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4220 | CHECK_EQ(verifier_failure, verifier::FailureKind::kSoftFailure); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4221 | // Soft failures at compile time should be retried at runtime. Soft |
| 4222 | // failures at runtime will be handled by slow paths in the generated |
| 4223 | // code. Set status accordingly. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4224 | if (Runtime::Current()->IsAotCompiler()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4225 | mirror::Class::SetStatus(klass, mirror::Class::kStatusRetryVerificationAtRuntime, self); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4226 | } else { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4227 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, self); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4228 | // As this is a fake verified status, make sure the methods are _not_ marked |
| 4229 | // kAccSkipAccessChecks later. |
| 4230 | klass->SetVerificationAttempted(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4231 | } |
| 4232 | } |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 4233 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4234 | VLOG(verifier) << "Verification failed on class " << klass->PrettyDescriptor() |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4235 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4236 | << " because: " << error_msg; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4237 | self->AssertNoPendingException(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4238 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4239 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 4240 | } |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4241 | if (preverified || verifier_failure == verifier::FailureKind::kNoFailure) { |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 4242 | // 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] | 4243 | // Let the interpreter know it by setting the kAccSkipAccessChecks flag onto each |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 4244 | // method. |
| 4245 | // 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] | 4246 | // kAccSkipAccessChecks flag when compiling image classes, the flag is recorded |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 4247 | // in the image and is set when loading the image. |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4248 | |
| 4249 | if (UNLIKELY(Runtime::Current()->IsVerificationSoftFail())) { |
| 4250 | // Never skip access checks if the verification soft fail is forced. |
| 4251 | // Mark the class as having a verification attempt to avoid re-running the verifier. |
| 4252 | klass->SetVerificationAttempted(); |
| 4253 | } else { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4254 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4255 | } |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4256 | } |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 4257 | return verifier_failure; |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4258 | } |
| 4259 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4260 | bool ClassLinker::VerifyClassUsingOatFile(const DexFile& dex_file, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4261 | ObjPtr<mirror::Class> klass, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4262 | mirror::Class::Status& oat_file_class_status) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4263 | // If we're compiling, we can only verify the class using the oat file if |
| 4264 | // we are not compiling the image or if the class we're verifying is not part of |
| 4265 | // the app. In other words, we will only check for preverification of bootclasspath |
| 4266 | // classes. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4267 | if (Runtime::Current()->IsAotCompiler()) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4268 | // Are we compiling the bootclasspath? |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 4269 | if (Runtime::Current()->GetCompilerCallbacks()->IsBootImage()) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4270 | return false; |
| 4271 | } |
| 4272 | // We are compiling an app (not the image). |
| 4273 | |
| 4274 | // Is this an app class? (I.e. not a bootclasspath class) |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4275 | if (klass->GetClassLoader() != nullptr) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4276 | return false; |
| 4277 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4278 | } |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4279 | |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 4280 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4281 | // 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] | 4282 | if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) { |
Anwar Ghuloum | ad256bb | 2013-07-18 14:58:55 -0700 | [diff] [blame] | 4283 | return false; |
| 4284 | } |
| 4285 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4286 | uint16_t class_def_index = klass->GetDexClassDefIndex(); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 4287 | oat_file_class_status = oat_dex_file->GetOatClass(class_def_index).GetStatus(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4288 | if (oat_file_class_status == mirror::Class::kStatusVerified || |
| 4289 | oat_file_class_status == mirror::Class::kStatusInitialized) { |
Mathieu Chartier | a079e3a | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 4290 | return true; |
| 4291 | } |
| 4292 | // If we only verified a subset of the classes at compile time, we can end up with classes that |
| 4293 | // were resolved by the verifier. |
| 4294 | if (oat_file_class_status == mirror::Class::kStatusResolved) { |
| 4295 | return false; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4296 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4297 | if (oat_file_class_status == mirror::Class::kStatusRetryVerificationAtRuntime) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 4298 | // Compile time verification failed with a soft error. Compile time verification can fail |
| 4299 | // because we have incomplete type information. Consider the following: |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 4300 | // class ... { |
| 4301 | // Foo x; |
| 4302 | // .... () { |
| 4303 | // if (...) { |
| 4304 | // v1 gets assigned a type of resolved class Foo |
| 4305 | // } else { |
| 4306 | // v1 gets assigned a type of unresolved class Bar |
| 4307 | // } |
| 4308 | // iput x = v1 |
| 4309 | // } } |
| 4310 | // when we merge v1 following the if-the-else it results in Conflict |
| 4311 | // (see verifier::RegType::Merge) as we can't know the type of Bar and we could possibly be |
| 4312 | // allowing an unsafe assignment to the field x in the iput (javac may have compiled this as |
| 4313 | // it knew Bar was a sub-class of Foo, but for us this may have been moved into a separate apk |
| 4314 | // at compile time). |
| 4315 | return false; |
| 4316 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4317 | if (mirror::Class::IsErroneous(oat_file_class_status)) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 4318 | // Compile time verification failed with a hard error. This is caused by invalid instructions |
| 4319 | // in the class. These errors are unrecoverable. |
| 4320 | return false; |
| 4321 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4322 | if (oat_file_class_status == mirror::Class::kStatusNotReady) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 4323 | // Status is uninitialized if we couldn't determine the status at compile time, for example, |
| 4324 | // not loading the class. |
| 4325 | // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy |
| 4326 | // isn't a problem and this case shouldn't occur |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4327 | return false; |
| 4328 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4329 | std::string temp; |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 4330 | LOG(FATAL) << "Unexpected class status: " << oat_file_class_status |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4331 | << " " << dex_file.GetLocation() << " " << klass->PrettyClass() << " " |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4332 | << klass->GetDescriptor(&temp); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4333 | UNREACHABLE(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4334 | } |
| 4335 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4336 | void ClassLinker::ResolveClassExceptionHandlerTypes(Handle<mirror::Class> klass) { |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 4337 | for (ArtMethod& method : klass->GetMethods(image_pointer_size_)) { |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4338 | ResolveMethodExceptionHandlerTypes(&method); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4339 | } |
| 4340 | } |
| 4341 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4342 | void ClassLinker::ResolveMethodExceptionHandlerTypes(ArtMethod* method) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4343 | // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod. |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4344 | const DexFile::CodeItem* code_item = |
| 4345 | method->GetDexFile()->GetCodeItem(method->GetCodeItemOffset()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4346 | if (code_item == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4347 | return; // native or abstract method |
| 4348 | } |
| 4349 | if (code_item->tries_size_ == 0) { |
| 4350 | return; // nothing to process |
| 4351 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 4352 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item, 0); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4353 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4354 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| 4355 | CatchHandlerIterator iterator(handlers_ptr); |
| 4356 | for (; iterator.HasNext(); iterator.Next()) { |
| 4357 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 4358 | // unresolved exception types will be ignored by exception delivery |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4359 | if (iterator.GetHandlerTypeIndex().IsValid()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4360 | ObjPtr<mirror::Class> exception_type = ResolveType(iterator.GetHandlerTypeIndex(), method); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4361 | if (exception_type == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4362 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4363 | Thread::Current()->ClearException(); |
| 4364 | } |
| 4365 | } |
| 4366 | } |
| 4367 | handlers_ptr = iterator.EndDataPointer(); |
| 4368 | } |
| 4369 | } |
| 4370 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4371 | mirror::Class* ClassLinker::CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, |
| 4372 | jstring name, |
| 4373 | jobjectArray interfaces, |
| 4374 | jobject loader, |
| 4375 | jobjectArray methods, |
| 4376 | jobjectArray throws) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4377 | Thread* self = soa.Self(); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4378 | StackHandleScope<10> hs(self); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4379 | MutableHandle<mirror::Class> temp_klass(hs.NewHandle( |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4380 | AllocClass(self, GetClassRoot(kJavaLangClass), sizeof(mirror::Class)))); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4381 | if (temp_klass == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4382 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4383 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4384 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4385 | DCHECK(temp_klass->GetClass() != nullptr); |
| 4386 | temp_klass->SetObjectSize(sizeof(mirror::Proxy)); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4387 | // Set the class access flags incl. VerificationAttempted, so we do not try to set the flag on |
| 4388 | // the methods. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4389 | temp_klass->SetAccessFlags(kAccClassIsProxy | kAccPublic | kAccFinal | kAccVerificationAttempted); |
| 4390 | temp_klass->SetClassLoader(soa.Decode<mirror::ClassLoader>(loader)); |
| 4391 | DCHECK_EQ(temp_klass->GetPrimitiveType(), Primitive::kPrimNot); |
| 4392 | temp_klass->SetName(soa.Decode<mirror::String>(name)); |
| 4393 | temp_klass->SetDexCache(GetClassRoot(kJavaLangReflectProxy)->GetDexCache()); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4394 | // Object has an empty iftable, copy it for that reason. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4395 | temp_klass->SetIfTable(GetClassRoot(kJavaLangObject)->GetIfTable()); |
| 4396 | mirror::Class::SetStatus(temp_klass, mirror::Class::kStatusIdx, self); |
| 4397 | std::string descriptor(GetDescriptorForProxy(temp_klass.Get())); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 4398 | const size_t hash = ComputeModifiedUtf8Hash(descriptor.c_str()); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4399 | |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 4400 | // 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] | 4401 | LinearAlloc* const allocator = GetOrCreateAllocatorForClassLoader(temp_klass->GetClassLoader()); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 4402 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4403 | // Insert the class before loading the fields as the field roots |
| 4404 | // (ArtField::declaring_class_) are only visited from the class |
| 4405 | // table. There can't be any suspend points between inserting the |
| 4406 | // class and setting the field arrays below. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4407 | ObjPtr<mirror::Class> existing = InsertClass(descriptor.c_str(), temp_klass.Get(), hash); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4408 | CHECK(existing == nullptr); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4409 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4410 | // Instance fields are inherited, but we add a couple of static fields... |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4411 | const size_t num_fields = 2; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 4412 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, allocator, num_fields); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4413 | temp_klass->SetSFieldsPtr(sfields); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4414 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4415 | // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by |
| 4416 | // our proxy, so Class.getInterfaces doesn't return the flattened set. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4417 | ArtField& interfaces_sfield = sfields->At(0); |
| 4418 | interfaces_sfield.SetDexFieldIndex(0); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4419 | interfaces_sfield.SetDeclaringClass(temp_klass.Get()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4420 | interfaces_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4421 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4422 | // 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] | 4423 | ArtField& throws_sfield = sfields->At(1); |
| 4424 | throws_sfield.SetDexFieldIndex(1); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4425 | throws_sfield.SetDeclaringClass(temp_klass.Get()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4426 | throws_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4427 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4428 | // Proxies have 1 direct method, the constructor |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4429 | const size_t num_direct_methods = 1; |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4430 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4431 | // They have as many virtual methods as the array |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4432 | auto h_methods = hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Method>>(methods)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 4433 | DCHECK_EQ(h_methods->GetClass(), mirror::Method::ArrayClass()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4434 | << mirror::Class::PrettyClass(h_methods->GetClass()); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 4435 | const size_t num_virtual_methods = h_methods->GetLength(); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4436 | |
| 4437 | // Create the methods array. |
| 4438 | LengthPrefixedArray<ArtMethod>* proxy_class_methods = AllocArtMethodArray( |
| 4439 | self, allocator, num_direct_methods + num_virtual_methods); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4440 | // Currently AllocArtMethodArray cannot return null, but the OOM logic is left there in case we |
| 4441 | // want to throw OOM in the future. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4442 | if (UNLIKELY(proxy_class_methods == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4443 | self->AssertPendingOOMException(); |
| 4444 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4445 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4446 | temp_klass->SetMethodsPtr(proxy_class_methods, num_direct_methods, num_virtual_methods); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4447 | |
| 4448 | // Create the single direct method. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4449 | CreateProxyConstructor(temp_klass, temp_klass->GetDirectMethodUnchecked(0, image_pointer_size_)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4450 | |
| 4451 | // Create virtual method using specified prototypes. |
| 4452 | // TODO These should really use the iterators. |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4453 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4454 | auto* virtual_method = temp_klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4455 | auto* prototype = h_methods->Get(i)->GetArtMethod(); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4456 | CreateProxyMethod(temp_klass, prototype, virtual_method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4457 | DCHECK(virtual_method->GetDeclaringClass() != nullptr); |
| 4458 | DCHECK(prototype->GetDeclaringClass() != nullptr); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4459 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4460 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4461 | // The super class is java.lang.reflect.Proxy |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4462 | temp_klass->SetSuperClass(GetClassRoot(kJavaLangReflectProxy)); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4463 | // Now effectively in the loaded state. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4464 | mirror::Class::SetStatus(temp_klass, mirror::Class::kStatusLoaded, self); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4465 | self->AssertNoPendingException(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4466 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4467 | // At this point the class is loaded. Publish a ClassLoad event. |
| 4468 | // Note: this may be a temporary class. It is a listener's responsibility to handle this. |
| 4469 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(temp_klass); |
| 4470 | |
| 4471 | MutableHandle<mirror::Class> klass = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 4472 | { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4473 | // Must hold lock on object when resolved. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4474 | ObjectLock<mirror::Class> resolution_lock(self, temp_klass); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4475 | // Link the fields and virtual methods, creating vtable and iftables. |
| 4476 | // The new class will replace the old one in the class table. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4477 | Handle<mirror::ObjectArray<mirror::Class>> h_interfaces( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4478 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces))); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4479 | if (!LinkClass(self, descriptor.c_str(), temp_klass, h_interfaces, &klass)) { |
| 4480 | mirror::Class::SetStatus(temp_klass, mirror::Class::kStatusErrorUnresolved, self); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4481 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4482 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4483 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4484 | CHECK(temp_klass->IsRetired()); |
| 4485 | CHECK_NE(temp_klass.Get(), klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4486 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4487 | CHECK_EQ(interfaces_sfield.GetDeclaringClass(), klass.Get()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4488 | interfaces_sfield.SetObject<false>( |
| 4489 | klass.Get(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4490 | soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4491 | CHECK_EQ(throws_sfield.GetDeclaringClass(), klass.Get()); |
| 4492 | throws_sfield.SetObject<false>( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4493 | klass.Get(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4494 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>(throws)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4495 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4496 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(temp_klass, klass); |
| 4497 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4498 | { |
| 4499 | // Lock on klass is released. Lock new class object. |
| 4500 | ObjectLock<mirror::Class> initialization_lock(self, klass); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4501 | mirror::Class::SetStatus(klass, mirror::Class::kStatusInitialized, self); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 4502 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4503 | |
| 4504 | // sanity checks |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 4505 | if (kIsDebugBuild) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4506 | CHECK(klass->GetIFieldsPtr() == nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4507 | CheckProxyConstructor(klass->GetDirectMethod(0, image_pointer_size_)); |
| 4508 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4509 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4510 | auto* virtual_method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
| 4511 | auto* prototype = h_methods->Get(i++)->GetArtMethod(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4512 | CheckProxyMethod(virtual_method, prototype); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4513 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4514 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4515 | StackHandleScope<1> hs2(self); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4516 | Handle<mirror::String> decoded_name = hs2.NewHandle(soa.Decode<mirror::String>(name)); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4517 | std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4518 | decoded_name->ToModifiedUtf8().c_str())); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4519 | CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(0)), interfaces_field_name); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4520 | |
| 4521 | std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4522 | decoded_name->ToModifiedUtf8().c_str())); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4523 | CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(1)), throws_field_name); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4524 | |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 4525 | CHECK_EQ(klass.Get()->GetProxyInterfaces(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4526 | soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)); |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 4527 | CHECK_EQ(klass.Get()->GetProxyThrows(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4528 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>(throws)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4529 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4530 | return klass.Get(); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4531 | } |
| 4532 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4533 | std::string ClassLinker::GetDescriptorForProxy(ObjPtr<mirror::Class> proxy_class) { |
Nicolas Geoffray | a7a4759 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 4534 | DCHECK(proxy_class->IsProxyClass()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4535 | ObjPtr<mirror::String> name = proxy_class->GetName(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4536 | DCHECK(name != nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4537 | return DotToDescriptor(name->ToModifiedUtf8().c_str()); |
| 4538 | } |
| 4539 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4540 | void ClassLinker::CreateProxyConstructor(Handle<mirror::Class> klass, ArtMethod* out) { |
| 4541 | // Create constructor for Proxy that must initialize the method. |
Przemyslaw Szczepaniak | f11cd29 | 2016-08-17 17:46:38 +0100 | [diff] [blame] | 4542 | CHECK_EQ(GetClassRoot(kJavaLangReflectProxy)->NumDirectMethods(), 23u); |
| 4543 | |
Igor Murashkin | 9d3d752 | 2017-02-27 10:39:49 -0800 | [diff] [blame] | 4544 | // Find the <init>(InvocationHandler)V method. The exact method offset varies depending |
| 4545 | // on which front-end compiler was used to build the libcore DEX files. |
| 4546 | ArtMethod* proxy_constructor = GetClassRoot(kJavaLangReflectProxy)-> |
| 4547 | FindDeclaredDirectMethod("<init>", |
| 4548 | "(Ljava/lang/reflect/InvocationHandler;)V", |
| 4549 | image_pointer_size_); |
| 4550 | DCHECK(proxy_constructor != nullptr) |
| 4551 | << "Could not find <init> method in java.lang.reflect.Proxy"; |
| 4552 | |
Sebastien Hertz | ae94e35 | 2014-08-27 15:32:56 +0200 | [diff] [blame] | 4553 | // Ensure constructor is in dex cache so that we can use the dex cache to look up the overridden |
| 4554 | // constructor method. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4555 | GetClassRoot(kJavaLangReflectProxy)->GetDexCache()->SetResolvedMethod( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4556 | proxy_constructor->GetDexMethodIndex(), proxy_constructor, image_pointer_size_); |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 4557 | // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its |
| 4558 | // code_ too) |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4559 | DCHECK(out != nullptr); |
| 4560 | out->CopyFrom(proxy_constructor, image_pointer_size_); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4561 | // Make this constructor public and fix the class to be our Proxy version |
Mathieu Chartier | 201e297 | 2017-06-05 18:34:53 -0700 | [diff] [blame] | 4562 | // Mark kAccCompileDontBother so that we don't take JIT samples for the method. b/62349349 |
| 4563 | out->SetAccessFlags((out->GetAccessFlags() & ~kAccProtected) | |
| 4564 | kAccPublic | |
| 4565 | kAccCompileDontBother); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4566 | out->SetDeclaringClass(klass.Get()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4567 | } |
| 4568 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4569 | void ClassLinker::CheckProxyConstructor(ArtMethod* constructor) const { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4570 | CHECK(constructor->IsConstructor()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4571 | auto* np = constructor->GetInterfaceMethodIfProxy(image_pointer_size_); |
| 4572 | CHECK_STREQ(np->GetName(), "<init>"); |
| 4573 | CHECK_STREQ(np->GetSignature().ToString().c_str(), "(Ljava/lang/reflect/InvocationHandler;)V"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4574 | DCHECK(constructor->IsPublic()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4575 | } |
| 4576 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4577 | void ClassLinker::CreateProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4578 | ArtMethod* out) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4579 | // Ensure prototype is in dex cache so that we can use the dex cache to look up the overridden |
| 4580 | // prototype method |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 4581 | auto* dex_cache = prototype->GetDeclaringClass()->GetDexCache(); |
| 4582 | // Avoid dirtying the dex cache unless we need to. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4583 | if (dex_cache->GetResolvedMethod(prototype->GetDexMethodIndex(), image_pointer_size_) != |
| 4584 | prototype) { |
| 4585 | dex_cache->SetResolvedMethod( |
| 4586 | prototype->GetDexMethodIndex(), prototype, image_pointer_size_); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 4587 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4588 | // 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] | 4589 | // as necessary |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4590 | DCHECK(out != nullptr); |
| 4591 | out->CopyFrom(prototype, image_pointer_size_); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4592 | |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 4593 | // Set class to be the concrete proxy class. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4594 | out->SetDeclaringClass(klass.Get()); |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 4595 | // Clear the abstract, default and conflict flags to ensure that defaults aren't picked in |
| 4596 | // preference to the invocation handler. |
| 4597 | const uint32_t kRemoveFlags = kAccAbstract | kAccDefault | kAccDefaultConflict; |
| 4598 | // Make the method final. |
Mathieu Chartier | 201e297 | 2017-06-05 18:34:53 -0700 | [diff] [blame] | 4599 | // Mark kAccCompileDontBother so that we don't take JIT samples for the method. b/62349349 |
| 4600 | const uint32_t kAddFlags = kAccFinal | kAccCompileDontBother; |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 4601 | out->SetAccessFlags((out->GetAccessFlags() & ~kRemoveFlags) | kAddFlags); |
| 4602 | |
| 4603 | // Clear the dex_code_item_offset_. It needs to be 0 since proxy methods have no CodeItems but the |
| 4604 | // method they copy might (if it's a default method). |
| 4605 | out->SetCodeItemOffset(0); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4606 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4607 | // At runtime the method looks like a reference and argument saving method, clone the code |
| 4608 | // related parameters from this method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4609 | out->SetEntryPointFromQuickCompiledCode(GetQuickProxyInvokeHandler()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4610 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4611 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4612 | void ClassLinker::CheckProxyMethod(ArtMethod* method, ArtMethod* prototype) const { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4613 | // Basic sanity |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4614 | CHECK(!prototype->IsFinal()); |
| 4615 | CHECK(method->IsFinal()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 4616 | CHECK(method->IsInvokable()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 4617 | |
| 4618 | // The proxy method doesn't have its own dex cache or dex file and so it steals those of its |
| 4619 | // 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] | 4620 | CHECK(prototype->HasSameDexCacheResolvedMethods(method, image_pointer_size_)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4621 | auto* np = method->GetInterfaceMethodIfProxy(image_pointer_size_); |
| 4622 | CHECK_EQ(prototype->GetDeclaringClass()->GetDexCache(), np->GetDexCache()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 4623 | CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex()); |
| 4624 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4625 | CHECK_STREQ(np->GetName(), prototype->GetName()); |
| 4626 | CHECK_STREQ(np->GetShorty(), prototype->GetShorty()); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4627 | // More complex sanity - via dex cache |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 4628 | CHECK_EQ(np->GetReturnType(true /* resolve */), prototype->GetReturnType(true /* resolve */)); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4629 | } |
| 4630 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4631 | bool ClassLinker::CanWeInitializeClass(ObjPtr<mirror::Class> klass, bool can_init_statics, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4632 | bool can_init_parents) { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 4633 | if (can_init_statics && can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4634 | return true; |
| 4635 | } |
| 4636 | if (!can_init_statics) { |
| 4637 | // Check if there's a class initializer. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4638 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4639 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4640 | return false; |
| 4641 | } |
| 4642 | // Check if there are encoded static values needing initialization. |
| 4643 | if (klass->NumStaticFields() != 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4644 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4645 | DCHECK(dex_class_def != nullptr); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4646 | if (dex_class_def->static_values_off_ != 0) { |
| 4647 | return false; |
| 4648 | } |
| 4649 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4650 | // If we are a class we need to initialize all interfaces with default methods when we are |
| 4651 | // initialized. Check all of them. |
| 4652 | if (!klass->IsInterface()) { |
| 4653 | size_t num_interfaces = klass->GetIfTableCount(); |
| 4654 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4655 | ObjPtr<mirror::Class> iface = klass->GetIfTable()->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4656 | if (iface->HasDefaultMethods() && |
| 4657 | !CanWeInitializeClass(iface, can_init_statics, can_init_parents)) { |
| 4658 | return false; |
| 4659 | } |
| 4660 | } |
| 4661 | } |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4662 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4663 | if (klass->IsInterface() || !klass->HasSuperClass()) { |
| 4664 | return true; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4665 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4666 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4667 | if (!can_init_parents && !super_class->IsInitialized()) { |
| 4668 | return false; |
| 4669 | } |
| 4670 | return CanWeInitializeClass(super_class, can_init_statics, can_init_parents); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4671 | } |
| 4672 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4673 | bool ClassLinker::InitializeClass(Thread* self, Handle<mirror::Class> klass, |
| 4674 | bool can_init_statics, bool can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4675 | // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol |
| 4676 | |
| 4677 | // Are we already initialized and therefore done? |
| 4678 | // Note: we differ from the JLS here as we don't do this under the lock, this is benign as |
| 4679 | // an initialized class will never change its state. |
| 4680 | if (klass->IsInitialized()) { |
| 4681 | return true; |
| 4682 | } |
| 4683 | |
| 4684 | // 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] | 4685 | if (!CanWeInitializeClass(klass.Get(), can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4686 | return false; |
| 4687 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4688 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4689 | self->AllowThreadSuspension(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4690 | uint64_t t0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4691 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4692 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4693 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4694 | // Re-check under the lock in case another thread initialized ahead of us. |
| 4695 | if (klass->IsInitialized()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4696 | return true; |
| 4697 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4698 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4699 | // 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] | 4700 | if (klass->IsErroneous()) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 4701 | ThrowEarlierClassFailure(klass.Get(), true); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4702 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4703 | return false; |
| 4704 | } |
| 4705 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4706 | CHECK(klass->IsResolved() && !klass->IsErroneousResolved()) |
| 4707 | << klass->PrettyClass() << ": state=" << klass->GetStatus(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4708 | |
| 4709 | if (!klass->IsVerified()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4710 | VerifyClass(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4711 | if (!klass->IsVerified()) { |
| 4712 | // We failed to verify, expect either the klass to be erroneous or verification failed at |
| 4713 | // compile time. |
| 4714 | if (klass->IsErroneous()) { |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 4715 | // The class is erroneous. This may be a verifier error, or another thread attempted |
| 4716 | // verification and/or initialization and failed. We can distinguish those cases by |
| 4717 | // whether an exception is already pending. |
| 4718 | if (self->IsExceptionPending()) { |
| 4719 | // Check that it's a VerifyError. |
| 4720 | DCHECK_EQ("java.lang.Class<java.lang.VerifyError>", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4721 | mirror::Class::PrettyClass(self->GetException()->GetClass())); |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 4722 | } else { |
| 4723 | // Check that another thread attempted initialization. |
| 4724 | DCHECK_NE(0, klass->GetClinitThreadId()); |
| 4725 | DCHECK_NE(self->GetTid(), klass->GetClinitThreadId()); |
| 4726 | // Need to rethrow the previous failure now. |
| 4727 | ThrowEarlierClassFailure(klass.Get(), true); |
| 4728 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4729 | VlogClassInitializationFailure(klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4730 | } else { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4731 | CHECK(Runtime::Current()->IsAotCompiler()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4732 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 4733 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4734 | return false; |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4735 | } else { |
| 4736 | self->AssertNoPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4737 | } |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 4738 | |
| 4739 | // A separate thread could have moved us all the way to initialized. A "simple" example |
| 4740 | // involves a subclass of the current class being initialized at the same time (which |
| 4741 | // will implicitly initialize the superclass, if scheduled that way). b/28254258 |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4742 | DCHECK(!klass->IsErroneous()) << klass->GetStatus(); |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 4743 | if (klass->IsInitialized()) { |
| 4744 | return true; |
| 4745 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4746 | } |
| 4747 | |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4748 | // If the class is kStatusInitializing, either this thread is |
| 4749 | // initializing higher up the stack or another thread has beat us |
| 4750 | // to initializing and we need to wait. Either way, this |
| 4751 | // invocation of InitializeClass will not be responsible for |
| 4752 | // running <clinit> and will return. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4753 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4754 | // Could have got an exception during verification. |
| 4755 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4756 | VlogClassInitializationFailure(klass); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4757 | return false; |
| 4758 | } |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 4759 | // We caught somebody else in the act; was it us? |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 4760 | if (klass->GetClinitThreadId() == self->GetTid()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4761 | // Yes. That's fine. Return so we can continue initializing. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4762 | return true; |
| 4763 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4764 | // No. That's fine. Wait for another thread to finish initializing. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4765 | return WaitForInitializeClass(klass, self, lock); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4766 | } |
| 4767 | |
| 4768 | if (!ValidateSuperClassDescriptors(klass)) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4769 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4770 | return false; |
| 4771 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4772 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4773 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4774 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusVerified) << klass->PrettyClass() |
Andreas Gampe | 9510ccd | 2016-04-20 09:55:25 -0700 | [diff] [blame] | 4775 | << " self.tid=" << self->GetTid() << " clinit.tid=" << klass->GetClinitThreadId(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4776 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4777 | // From here out other threads may observe that we're initializing and so changes of state |
| 4778 | // require the a notification. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 4779 | klass->SetClinitThreadId(self->GetTid()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4780 | mirror::Class::SetStatus(klass, mirror::Class::kStatusInitializing, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4781 | |
| 4782 | t0 = NanoTime(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4783 | } |
| 4784 | |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 4785 | // Initialize super classes, must be done while initializing for the JLS. |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4786 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4787 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4788 | if (!super_class->IsInitialized()) { |
| 4789 | CHECK(!super_class->IsInterface()); |
| 4790 | CHECK(can_init_parents); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4791 | StackHandleScope<1> hs(self); |
| 4792 | Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4793 | bool super_initialized = InitializeClass(self, handle_scope_super, can_init_statics, true); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4794 | if (!super_initialized) { |
| 4795 | // The super class was verified ahead of entering initializing, we should only be here if |
| 4796 | // the super class became erroneous due to initialization. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4797 | CHECK(handle_scope_super->IsErroneous() && self->IsExceptionPending()) |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4798 | << "Super class initialization failed for " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4799 | << handle_scope_super->PrettyDescriptor() |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4800 | << " that has unexpected status " << handle_scope_super->GetStatus() |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4801 | << "\nPending exception:\n" |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 4802 | << (self->GetException() != nullptr ? self->GetException()->Dump() : ""); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4803 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4804 | // Initialization failed because the super-class is erroneous. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4805 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4806 | return false; |
| 4807 | } |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 4808 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4809 | } |
| 4810 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4811 | if (!klass->IsInterface()) { |
| 4812 | // Initialize interfaces with default methods for the JLS. |
| 4813 | size_t num_direct_interfaces = klass->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4814 | // Only setup the (expensive) handle scope if we actually need to. |
| 4815 | if (UNLIKELY(num_direct_interfaces > 0)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4816 | StackHandleScope<1> hs_iface(self); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4817 | MutableHandle<mirror::Class> handle_scope_iface(hs_iface.NewHandle<mirror::Class>(nullptr)); |
| 4818 | for (size_t i = 0; i < num_direct_interfaces; i++) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 4819 | handle_scope_iface.Assign(mirror::Class::GetDirectInterface(self, klass.Get(), i)); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 4820 | CHECK(handle_scope_iface != nullptr) << klass->PrettyDescriptor() << " iface #" << i; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4821 | CHECK(handle_scope_iface->IsInterface()); |
| 4822 | if (handle_scope_iface->HasBeenRecursivelyInitialized()) { |
| 4823 | // We have already done this for this interface. Skip it. |
| 4824 | continue; |
| 4825 | } |
| 4826 | // We cannot just call initialize class directly because we need to ensure that ALL |
| 4827 | // interfaces with default methods are initialized. Non-default interface initialization |
| 4828 | // will not affect other non-default super-interfaces. |
| 4829 | bool iface_initialized = InitializeDefaultInterfaceRecursive(self, |
| 4830 | handle_scope_iface, |
| 4831 | can_init_statics, |
| 4832 | can_init_parents); |
| 4833 | if (!iface_initialized) { |
| 4834 | ObjectLock<mirror::Class> lock(self, klass); |
| 4835 | // Initialization failed because one of our interfaces with default methods is erroneous. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4836 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4837 | return false; |
| 4838 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4839 | } |
| 4840 | } |
| 4841 | } |
| 4842 | |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4843 | const size_t num_static_fields = klass->NumStaticFields(); |
| 4844 | if (num_static_fields > 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4845 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4846 | CHECK(dex_class_def != nullptr); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4847 | const DexFile& dex_file = klass->GetDexFile(); |
Hiroshi Yamauchi | 67ef46a | 2014-08-21 15:59:43 -0700 | [diff] [blame] | 4848 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4849 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4850 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4851 | |
| 4852 | // Eagerly fill in static fields so that the we don't have to do as many expensive |
| 4853 | // Class::FindStaticField in ResolveField. |
| 4854 | for (size_t i = 0; i < num_static_fields; ++i) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4855 | ArtField* field = klass->GetStaticField(i); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4856 | const uint32_t field_idx = field->GetDexFieldIndex(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4857 | ArtField* resolved_field = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4858 | if (resolved_field == nullptr) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4859 | dex_cache->SetResolvedField(field_idx, field, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4860 | } else { |
| 4861 | DCHECK_EQ(field, resolved_field); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4862 | } |
| 4863 | } |
| 4864 | |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 4865 | annotations::RuntimeEncodedStaticFieldValueIterator value_it(dex_file, |
| 4866 | &dex_cache, |
| 4867 | &class_loader, |
| 4868 | this, |
| 4869 | *dex_class_def); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 4870 | const uint8_t* class_data = dex_file.GetClassData(*dex_class_def); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4871 | ClassDataItemIterator field_it(dex_file, class_data); |
| 4872 | if (value_it.HasNext()) { |
| 4873 | DCHECK(field_it.HasNextStaticField()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4874 | CHECK(can_init_statics); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4875 | for ( ; value_it.HasNext(); value_it.Next(), field_it.Next()) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4876 | ArtField* field = ResolveField( |
| 4877 | dex_file, field_it.GetMemberIndex(), dex_cache, class_loader, true); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4878 | if (Runtime::Current()->IsActiveTransaction()) { |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4879 | value_it.ReadValueToField<true>(field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4880 | } else { |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4881 | value_it.ReadValueToField<false>(field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4882 | } |
Mathieu Chartier | da595be | 2016-08-10 13:57:39 -0700 | [diff] [blame] | 4883 | if (self->IsExceptionPending()) { |
| 4884 | break; |
| 4885 | } |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4886 | DCHECK(!value_it.HasNext() || field_it.HasNextStaticField()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4887 | } |
| 4888 | } |
| 4889 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4890 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4891 | |
Mathieu Chartier | da595be | 2016-08-10 13:57:39 -0700 | [diff] [blame] | 4892 | if (!self->IsExceptionPending()) { |
| 4893 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
| 4894 | if (clinit != nullptr) { |
| 4895 | CHECK(can_init_statics); |
| 4896 | JValue result; |
| 4897 | clinit->Invoke(self, nullptr, 0, &result, "V"); |
| 4898 | } |
| 4899 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4900 | self->AllowThreadSuspension(); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 4901 | uint64_t t1 = NanoTime(); |
| 4902 | |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4903 | bool success = true; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4904 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4905 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4906 | |
| 4907 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4908 | WrapExceptionInInitializer(klass); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4909 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4910 | success = false; |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 4911 | } else if (Runtime::Current()->IsTransactionAborted()) { |
| 4912 | // The exception thrown when the transaction aborted has been caught and cleared |
| 4913 | // so we need to throw it again now. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4914 | VLOG(compiler) << "Return from class initializer of " |
| 4915 | << mirror::Class::PrettyDescriptor(klass.Get()) |
Sebastien Hertz | bd9cf9f | 2015-03-03 12:16:13 +0100 | [diff] [blame] | 4916 | << " without exception while transaction was aborted: re-throw it now."; |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 4917 | Runtime::Current()->ThrowTransactionAbortError(self); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4918 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 4919 | success = false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4920 | } else { |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 4921 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 4922 | RuntimeStats* thread_stats = self->GetStats(); |
| 4923 | ++global_stats->class_init_count; |
| 4924 | ++thread_stats->class_init_count; |
| 4925 | global_stats->class_init_time_ns += (t1 - t0); |
| 4926 | thread_stats->class_init_time_ns += (t1 - t0); |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 4927 | // Set the class as initialized except if failed to initialize static fields. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4928 | mirror::Class::SetStatus(klass, mirror::Class::kStatusInitialized, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4929 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4930 | std::string temp; |
| 4931 | LOG(INFO) << "Initialized class " << klass->GetDescriptor(&temp) << " from " << |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4932 | klass->GetLocation(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 4933 | } |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 4934 | // Opportunistically set static method trampolines to their destination. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4935 | FixupStaticTrampolines(klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4936 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4937 | } |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4938 | return success; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4939 | } |
| 4940 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4941 | // We recursively run down the tree of interfaces. We need to do this in the order they are declared |
| 4942 | // and perform the initialization only on those interfaces that contain default methods. |
| 4943 | bool ClassLinker::InitializeDefaultInterfaceRecursive(Thread* self, |
| 4944 | Handle<mirror::Class> iface, |
| 4945 | bool can_init_statics, |
| 4946 | bool can_init_parents) { |
| 4947 | CHECK(iface->IsInterface()); |
| 4948 | size_t num_direct_ifaces = iface->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4949 | // Only create the (expensive) handle scope if we need it. |
| 4950 | if (UNLIKELY(num_direct_ifaces > 0)) { |
| 4951 | StackHandleScope<1> hs(self); |
| 4952 | MutableHandle<mirror::Class> handle_super_iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 4953 | // First we initialize all of iface's super-interfaces recursively. |
| 4954 | for (size_t i = 0; i < num_direct_ifaces; i++) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 4955 | ObjPtr<mirror::Class> super_iface = mirror::Class::GetDirectInterface(self, iface.Get(), i); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 4956 | CHECK(super_iface != nullptr) << iface->PrettyDescriptor() << " iface #" << i; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4957 | if (!super_iface->HasBeenRecursivelyInitialized()) { |
| 4958 | // Recursive step |
| 4959 | handle_super_iface.Assign(super_iface); |
| 4960 | if (!InitializeDefaultInterfaceRecursive(self, |
| 4961 | handle_super_iface, |
| 4962 | can_init_statics, |
| 4963 | can_init_parents)) { |
| 4964 | return false; |
| 4965 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4966 | } |
| 4967 | } |
| 4968 | } |
| 4969 | |
| 4970 | bool result = true; |
| 4971 | // Then we initialize 'iface' if it has default methods. We do not need to (and in fact must not) |
| 4972 | // initialize if we don't have default methods. |
| 4973 | if (iface->HasDefaultMethods()) { |
| 4974 | result = EnsureInitialized(self, iface, can_init_statics, can_init_parents); |
| 4975 | } |
| 4976 | |
| 4977 | // Mark that this interface has undergone recursive default interface initialization so we know we |
| 4978 | // can skip it on any later class initializations. We do this even if we are not a default |
| 4979 | // interface since we can still avoid the traversal. This is purely a performance optimization. |
| 4980 | if (result) { |
| 4981 | // TODO This should be done in a better way |
| 4982 | ObjectLock<mirror::Class> lock(self, iface); |
| 4983 | iface->SetRecursivelyInitialized(); |
| 4984 | } |
| 4985 | return result; |
| 4986 | } |
| 4987 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4988 | bool ClassLinker::WaitForInitializeClass(Handle<mirror::Class> klass, |
| 4989 | Thread* self, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4990 | ObjectLock<mirror::Class>& lock) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4991 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4992 | while (true) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4993 | self->AssertNoPendingException(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4994 | CHECK(!klass->IsInitialized()); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4995 | lock.WaitIgnoringInterrupts(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4996 | |
| 4997 | // When we wake up, repeat the test for init-in-progress. If |
| 4998 | // there's an exception pending (only possible if |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4999 | // we were not using WaitIgnoringInterrupts), bail out. |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5000 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5001 | WrapExceptionInInitializer(klass); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 5002 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5003 | return false; |
| 5004 | } |
| 5005 | // Spurious wakeup? Go back to waiting. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5006 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5007 | continue; |
| 5008 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 5009 | if (klass->GetStatus() == mirror::Class::kStatusVerified && |
| 5010 | Runtime::Current()->IsAotCompiler()) { |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 5011 | // Compile time initialization failed. |
| 5012 | return false; |
| 5013 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5014 | if (klass->IsErroneous()) { |
| 5015 | // The caller wants an exception, but it was thrown in a |
| 5016 | // different thread. Synthesize one here. |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 5017 | ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5018 | klass->PrettyDescriptor().c_str()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5019 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5020 | return false; |
| 5021 | } |
| 5022 | if (klass->IsInitialized()) { |
| 5023 | return true; |
| 5024 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5025 | LOG(FATAL) << "Unexpected class status. " << klass->PrettyClass() << " is " |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 5026 | << klass->GetStatus(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5027 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 5028 | UNREACHABLE(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5029 | } |
| 5030 | |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5031 | static void ThrowSignatureCheckResolveReturnTypeException(Handle<mirror::Class> klass, |
| 5032 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5033 | ArtMethod* method, |
| 5034 | ArtMethod* m) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5035 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5036 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5037 | DCHECK(!m->IsProxyMethod()); |
| 5038 | const DexFile* dex_file = m->GetDexFile(); |
| 5039 | const DexFile::MethodId& method_id = dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 5040 | const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5041 | dex::TypeIndex return_type_idx = proto_id.return_type_idx_; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5042 | std::string return_type = dex_file->PrettyType(return_type_idx); |
| 5043 | std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5044 | ThrowWrappedLinkageError(klass.Get(), |
| 5045 | "While checking class %s method %s signature against %s %s: " |
| 5046 | "Failed to resolve return type %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5047 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5048 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5049 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5050 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5051 | return_type.c_str(), class_loader.c_str()); |
| 5052 | } |
| 5053 | |
| 5054 | static void ThrowSignatureCheckResolveArgException(Handle<mirror::Class> klass, |
| 5055 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5056 | ArtMethod* method, |
| 5057 | ArtMethod* m, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5058 | uint32_t index, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5059 | dex::TypeIndex arg_type_idx) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5060 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5061 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5062 | DCHECK(!m->IsProxyMethod()); |
| 5063 | const DexFile* dex_file = m->GetDexFile(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5064 | std::string arg_type = dex_file->PrettyType(arg_type_idx); |
| 5065 | std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5066 | ThrowWrappedLinkageError(klass.Get(), |
| 5067 | "While checking class %s method %s signature against %s %s: " |
| 5068 | "Failed to resolve arg %u type %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5069 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5070 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5071 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5072 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5073 | index, arg_type.c_str(), class_loader.c_str()); |
| 5074 | } |
| 5075 | |
| 5076 | static void ThrowSignatureMismatch(Handle<mirror::Class> klass, |
| 5077 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5078 | ArtMethod* method, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5079 | const std::string& error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5080 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5081 | ThrowLinkageError(klass.Get(), |
| 5082 | "Class %s method %s resolves differently in %s %s: %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5083 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5084 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5085 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5086 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5087 | error_msg.c_str()); |
| 5088 | } |
| 5089 | |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5090 | static bool HasSameSignatureWithDifferentClassLoaders(Thread* self, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5091 | Handle<mirror::Class> klass, |
| 5092 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5093 | ArtMethod* method1, |
| 5094 | ArtMethod* method2) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5095 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5096 | { |
| 5097 | StackHandleScope<1> hs(self); |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5098 | Handle<mirror::Class> return_type(hs.NewHandle(method1->GetReturnType(true /* resolve */))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5099 | if (UNLIKELY(return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5100 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method1); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5101 | return false; |
| 5102 | } |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5103 | ObjPtr<mirror::Class> other_return_type = method2->GetReturnType(true /* resolve */); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5104 | if (UNLIKELY(other_return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5105 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method2); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5106 | return false; |
| 5107 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5108 | if (UNLIKELY(other_return_type != return_type.Get())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5109 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5110 | StringPrintf("Return types mismatch: %s(%p) vs %s(%p)", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5111 | return_type->PrettyClassAndClassLoader().c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5112 | return_type.Get(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5113 | other_return_type->PrettyClassAndClassLoader().c_str(), |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5114 | other_return_type.Ptr())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5115 | return false; |
| 5116 | } |
| 5117 | } |
| 5118 | const DexFile::TypeList* types1 = method1->GetParameterTypeList(); |
| 5119 | const DexFile::TypeList* types2 = method2->GetParameterTypeList(); |
| 5120 | if (types1 == nullptr) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5121 | if (types2 != nullptr && types2->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5122 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5123 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5124 | method2->PrettyMethod(true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5125 | return false; |
| 5126 | } |
| 5127 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5128 | } else if (UNLIKELY(types2 == nullptr)) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5129 | if (types1->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5130 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5131 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5132 | method2->PrettyMethod(true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5133 | return false; |
| 5134 | } |
| 5135 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5136 | } |
| 5137 | uint32_t num_types = types1->Size(); |
| 5138 | if (UNLIKELY(num_types != types2->Size())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5139 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5140 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5141 | method2->PrettyMethod(true).c_str())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5142 | return false; |
| 5143 | } |
| 5144 | for (uint32_t i = 0; i < num_types; ++i) { |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5145 | StackHandleScope<1> hs(self); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5146 | dex::TypeIndex param_type_idx = types1->GetTypeItem(i).type_idx_; |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5147 | Handle<mirror::Class> param_type(hs.NewHandle( |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5148 | method1->GetClassFromTypeIndex(param_type_idx, true /* resolve */))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5149 | if (UNLIKELY(param_type == nullptr)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5150 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5151 | method1, i, param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5152 | return false; |
| 5153 | } |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5154 | dex::TypeIndex other_param_type_idx = types2->GetTypeItem(i).type_idx_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5155 | ObjPtr<mirror::Class> other_param_type = |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5156 | method2->GetClassFromTypeIndex(other_param_type_idx, true /* resolve */); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5157 | if (UNLIKELY(other_param_type == nullptr)) { |
| 5158 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5159 | method2, i, other_param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5160 | return false; |
| 5161 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5162 | if (UNLIKELY(param_type.Get() != other_param_type)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5163 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5164 | StringPrintf("Parameter %u type mismatch: %s(%p) vs %s(%p)", |
| 5165 | i, |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5166 | param_type->PrettyClassAndClassLoader().c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5167 | param_type.Get(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5168 | other_param_type->PrettyClassAndClassLoader().c_str(), |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5169 | other_param_type.Ptr())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5170 | return false; |
| 5171 | } |
| 5172 | } |
| 5173 | return true; |
| 5174 | } |
| 5175 | |
| 5176 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5177 | bool ClassLinker::ValidateSuperClassDescriptors(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5178 | if (klass->IsInterface()) { |
| 5179 | return true; |
| 5180 | } |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 5181 | // Begin with the methods local to the superclass. |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 5182 | Thread* self = Thread::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5183 | StackHandleScope<1> hs(self); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5184 | MutableHandle<mirror::Class> super_klass(hs.NewHandle<mirror::Class>(nullptr)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5185 | if (klass->HasSuperClass() && |
| 5186 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5187 | super_klass.Assign(klass->GetSuperClass()); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5188 | for (int i = klass->GetSuperClass()->GetVTableLength() - 1; i >= 0; --i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5189 | auto* m = klass->GetVTableEntry(i, image_pointer_size_); |
| 5190 | auto* super_m = klass->GetSuperClass()->GetVTableEntry(i, image_pointer_size_); |
| 5191 | if (m != super_m) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5192 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, |
| 5193 | klass, |
| 5194 | super_klass, |
| 5195 | m, |
| 5196 | super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5197 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5198 | return false; |
| 5199 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5200 | } |
| 5201 | } |
| 5202 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 5203 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5204 | super_klass.Assign(klass->GetIfTable()->GetInterface(i)); |
| 5205 | if (klass->GetClassLoader() != super_klass->GetClassLoader()) { |
| 5206 | uint32_t num_methods = super_klass->NumVirtualMethods(); |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 5207 | for (uint32_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5208 | auto* m = klass->GetIfTable()->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>( |
| 5209 | j, image_pointer_size_); |
| 5210 | auto* super_m = super_klass->GetVirtualMethod(j, image_pointer_size_); |
| 5211 | if (m != super_m) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5212 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, |
| 5213 | klass, |
| 5214 | super_klass, |
| 5215 | m, |
| 5216 | super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5217 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5218 | return false; |
| 5219 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5220 | } |
| 5221 | } |
| 5222 | } |
| 5223 | } |
| 5224 | return true; |
| 5225 | } |
| 5226 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5227 | bool ClassLinker::EnsureInitialized(Thread* self, |
| 5228 | Handle<mirror::Class> c, |
| 5229 | bool can_init_fields, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5230 | bool can_init_parents) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5231 | DCHECK(c != nullptr); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5232 | if (c->IsInitialized()) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 5233 | EnsureSkipAccessChecksMethods(c, image_pointer_size_); |
Mathieu Chartier | 8502f72 | 2016-06-08 15:09:08 -0700 | [diff] [blame] | 5234 | self->AssertNoPendingException(); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5235 | return true; |
| 5236 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5237 | const bool success = InitializeClass(self, c, can_init_fields, can_init_parents); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5238 | if (!success) { |
| 5239 | if (can_init_fields && can_init_parents) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5240 | CHECK(self->IsExceptionPending()) << c->PrettyClass(); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5241 | } |
| 5242 | } else { |
| 5243 | self->AssertNoPendingException(); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 5244 | } |
| 5245 | return success; |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 5246 | } |
| 5247 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5248 | void ClassLinker::FixupTemporaryDeclaringClass(ObjPtr<mirror::Class> temp_class, |
| 5249 | ObjPtr<mirror::Class> new_class) { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5250 | DCHECK_EQ(temp_class->NumInstanceFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5251 | for (ArtField& field : new_class->GetIFields()) { |
| 5252 | if (field.GetDeclaringClass() == temp_class) { |
| 5253 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5254 | } |
| 5255 | } |
| 5256 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5257 | DCHECK_EQ(temp_class->NumStaticFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5258 | for (ArtField& field : new_class->GetSFields()) { |
| 5259 | if (field.GetDeclaringClass() == temp_class) { |
| 5260 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5261 | } |
| 5262 | } |
| 5263 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5264 | DCHECK_EQ(temp_class->NumDirectMethods(), 0u); |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5265 | DCHECK_EQ(temp_class->NumVirtualMethods(), 0u); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5266 | for (auto& method : new_class->GetMethods(image_pointer_size_)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5267 | if (method.GetDeclaringClass() == temp_class) { |
| 5268 | method.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5269 | } |
| 5270 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5271 | |
| 5272 | // Make sure the remembered set and mod-union tables know that we updated some of the native |
| 5273 | // roots. |
| 5274 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5275 | } |
| 5276 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5277 | void ClassLinker::RegisterClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5278 | CHECK(class_loader->GetAllocator() == nullptr); |
| 5279 | CHECK(class_loader->GetClassTable() == nullptr); |
| 5280 | Thread* const self = Thread::Current(); |
| 5281 | ClassLoaderData data; |
| 5282 | data.weak_root = self->GetJniEnv()->vm->AddWeakGlobalRef(self, class_loader); |
| 5283 | // Create and set the class table. |
| 5284 | data.class_table = new ClassTable; |
| 5285 | class_loader->SetClassTable(data.class_table); |
| 5286 | // Create and set the linear allocator. |
| 5287 | data.allocator = Runtime::Current()->CreateLinearAlloc(); |
| 5288 | class_loader->SetAllocator(data.allocator); |
| 5289 | // Add to the list so that we know to free the data later. |
| 5290 | class_loaders_.push_back(data); |
| 5291 | } |
| 5292 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5293 | ClassTable* ClassLinker::InsertClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5294 | if (class_loader == nullptr) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 5295 | return boot_class_table_.get(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5296 | } |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5297 | ClassTable* class_table = class_loader->GetClassTable(); |
| 5298 | if (class_table == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5299 | RegisterClassLoader(class_loader); |
| 5300 | class_table = class_loader->GetClassTable(); |
| 5301 | DCHECK(class_table != nullptr); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5302 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5303 | return class_table; |
| 5304 | } |
| 5305 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5306 | ClassTable* ClassLinker::ClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 5307 | return class_loader == nullptr ? boot_class_table_.get() : class_loader->GetClassTable(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5308 | } |
| 5309 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5310 | static ImTable* FindSuperImt(ObjPtr<mirror::Class> klass, PointerSize pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5311 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5312 | while (klass->HasSuperClass()) { |
| 5313 | klass = klass->GetSuperClass(); |
| 5314 | if (klass->ShouldHaveImt()) { |
| 5315 | return klass->GetImt(pointer_size); |
| 5316 | } |
| 5317 | } |
| 5318 | return nullptr; |
| 5319 | } |
| 5320 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5321 | bool ClassLinker::LinkClass(Thread* self, |
| 5322 | const char* descriptor, |
| 5323 | Handle<mirror::Class> klass, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5324 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5325 | MutableHandle<mirror::Class>* h_new_class_out) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5326 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5327 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5328 | if (!LinkSuperClass(klass)) { |
| 5329 | return false; |
| 5330 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5331 | ArtMethod* imt_data[ImTable::kSize]; |
| 5332 | // If there are any new conflicts compared to super class. |
| 5333 | bool new_conflict = false; |
| 5334 | std::fill_n(imt_data, arraysize(imt_data), Runtime::Current()->GetImtUnimplementedMethod()); |
| 5335 | if (!LinkMethods(self, klass, interfaces, &new_conflict, imt_data)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5336 | return false; |
| 5337 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5338 | if (!LinkInstanceFields(self, klass)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5339 | return false; |
| 5340 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5341 | size_t class_size; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5342 | if (!LinkStaticFields(self, klass, &class_size)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5343 | return false; |
| 5344 | } |
| 5345 | CreateReferenceInstanceOffsets(klass); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5346 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5347 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5348 | ImTable* imt = nullptr; |
| 5349 | if (klass->ShouldHaveImt()) { |
| 5350 | // If there are any new conflicts compared to the super class we can not make a copy. There |
| 5351 | // can be cases where both will have a conflict method at the same slot without having the same |
| 5352 | // set of conflicts. In this case, we can not share the IMT since the conflict table slow path |
| 5353 | // will possibly create a table that is incorrect for either of the classes. |
| 5354 | // Same IMT with new_conflict does not happen very often. |
| 5355 | if (!new_conflict) { |
| 5356 | ImTable* super_imt = FindSuperImt(klass.Get(), image_pointer_size_); |
| 5357 | if (super_imt != nullptr) { |
| 5358 | bool imt_equals = true; |
| 5359 | for (size_t i = 0; i < ImTable::kSize && imt_equals; ++i) { |
| 5360 | imt_equals = imt_equals && (super_imt->Get(i, image_pointer_size_) == imt_data[i]); |
| 5361 | } |
| 5362 | if (imt_equals) { |
| 5363 | imt = super_imt; |
| 5364 | } |
| 5365 | } |
| 5366 | } |
| 5367 | if (imt == nullptr) { |
| 5368 | LinearAlloc* allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
| 5369 | imt = reinterpret_cast<ImTable*>( |
| 5370 | allocator->Alloc(self, ImTable::SizeInBytes(image_pointer_size_))); |
| 5371 | if (imt == nullptr) { |
| 5372 | return false; |
| 5373 | } |
| 5374 | imt->Populate(imt_data, image_pointer_size_); |
| 5375 | } |
| 5376 | } |
| 5377 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5378 | if (!klass->IsTemp() || (!init_done_ && klass->GetClassSize() == class_size)) { |
| 5379 | // We don't need to retire this class as it has no embedded tables or it was created the |
| 5380 | // correct size during class linker initialization. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5381 | CHECK_EQ(klass->GetClassSize(), class_size) << klass->PrettyDescriptor(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5382 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5383 | if (klass->ShouldHaveEmbeddedVTable()) { |
| 5384 | klass->PopulateEmbeddedVTable(image_pointer_size_); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5385 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5386 | if (klass->ShouldHaveImt()) { |
| 5387 | klass->SetImt(imt, image_pointer_size_); |
| 5388 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 5389 | |
| 5390 | // Update CHA info based on whether we override methods. |
| 5391 | // Have to do this before setting the class as resolved which allows |
| 5392 | // instantiation of klass. |
| 5393 | Runtime::Current()->GetClassHierarchyAnalysis()->UpdateAfterLoadingOf(klass); |
| 5394 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5395 | // This will notify waiters on klass that saw the not yet resolved |
| 5396 | // class in the class_table_ during EnsureResolved. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 5397 | mirror::Class::SetStatus(klass, mirror::Class::kStatusResolved, self); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5398 | h_new_class_out->Assign(klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5399 | } else { |
| 5400 | CHECK(!klass->IsResolved()); |
| 5401 | // Retire the temporary class and create the correctly sized resolved class. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5402 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5403 | 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] | 5404 | // Set arrays to null since we don't want to have multiple classes with the same ArtField or |
| 5405 | // ArtMethod array pointers. If this occurs, it causes bugs in remembered sets since the GC |
| 5406 | // may not see any references to the target space and clean the card for a class if another |
| 5407 | // class had the same array pointer. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5408 | klass->SetMethodsPtrUnchecked(nullptr, 0, 0); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5409 | klass->SetSFieldsPtrUnchecked(nullptr); |
| 5410 | klass->SetIFieldsPtrUnchecked(nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5411 | if (UNLIKELY(h_new_class == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5412 | self->AssertPendingOOMException(); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 5413 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorUnresolved, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5414 | return false; |
| 5415 | } |
| 5416 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5417 | CHECK_EQ(h_new_class->GetClassSize(), class_size); |
| 5418 | ObjectLock<mirror::Class> lock(self, h_new_class); |
| 5419 | FixupTemporaryDeclaringClass(klass.Get(), h_new_class.Get()); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5420 | |
| 5421 | { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5422 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5423 | ObjPtr<mirror::ClassLoader> const class_loader = h_new_class.Get()->GetClassLoader(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5424 | ClassTable* const table = InsertClassTableForClassLoader(class_loader); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5425 | ObjPtr<mirror::Class> existing = table->UpdateClass(descriptor, h_new_class.Get(), |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5426 | ComputeModifiedUtf8Hash(descriptor)); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 5427 | if (class_loader != nullptr) { |
| 5428 | // We updated the class in the class table, perform the write barrier so that the GC knows |
| 5429 | // about the change. |
| 5430 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader); |
| 5431 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5432 | CHECK_EQ(existing, klass.Get()); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 5433 | if (log_new_roots_) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5434 | new_class_roots_.push_back(GcRoot<mirror::Class>(h_new_class.Get())); |
| 5435 | } |
| 5436 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5437 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 5438 | // Update CHA info based on whether we override methods. |
| 5439 | // Have to do this before setting the class as resolved which allows |
| 5440 | // instantiation of klass. |
| 5441 | Runtime::Current()->GetClassHierarchyAnalysis()->UpdateAfterLoadingOf(h_new_class); |
| 5442 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5443 | // This will notify waiters on temp class that saw the not yet resolved class in the |
| 5444 | // class_table_ during EnsureResolved. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 5445 | mirror::Class::SetStatus(klass, mirror::Class::kStatusRetired, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5446 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5447 | CHECK_EQ(h_new_class->GetStatus(), mirror::Class::kStatusResolving); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5448 | // This will notify waiters on new_class that saw the not yet resolved |
| 5449 | // class in the class_table_ during EnsureResolved. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5450 | mirror::Class::SetStatus(h_new_class, mirror::Class::kStatusResolved, self); |
| 5451 | // Return the new class. |
| 5452 | h_new_class_out->Assign(h_new_class.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5453 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5454 | return true; |
| 5455 | } |
| 5456 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5457 | bool ClassLinker::LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5458 | CHECK_EQ(mirror::Class::kStatusIdx, klass->GetStatus()); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 5459 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5460 | dex::TypeIndex super_class_idx = class_def.superclass_idx_; |
| 5461 | if (super_class_idx.IsValid()) { |
Roland Levillain | 90328ac | 2016-05-18 12:25:38 +0100 | [diff] [blame] | 5462 | // Check that a class does not inherit from itself directly. |
| 5463 | // |
| 5464 | // TODO: This is a cheap check to detect the straightforward case |
| 5465 | // of a class extending itself (b/28685551), but we should do a |
| 5466 | // proper cycle detection on loaded classes, to detect all cases |
| 5467 | // of class circularity errors (b/28830038). |
| 5468 | if (super_class_idx == class_def.class_idx_) { |
| 5469 | ThrowClassCircularityError(klass.Get(), |
| 5470 | "Class %s extends itself", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5471 | klass->PrettyDescriptor().c_str()); |
Roland Levillain | 90328ac | 2016-05-18 12:25:38 +0100 | [diff] [blame] | 5472 | return false; |
| 5473 | } |
| 5474 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5475 | 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] | 5476 | if (super_class == nullptr) { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 5477 | DCHECK(Thread::Current()->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5478 | return false; |
| 5479 | } |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 5480 | // Verify |
| 5481 | if (!klass->CanAccess(super_class)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5482 | ThrowIllegalAccessError(klass.Get(), "Class %s extended by class %s is inaccessible", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5483 | super_class->PrettyDescriptor().c_str(), |
| 5484 | klass->PrettyDescriptor().c_str()); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 5485 | return false; |
| 5486 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5487 | CHECK(super_class->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5488 | klass->SetSuperClass(super_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5489 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 5490 | const DexFile::TypeList* interfaces = dex_file.GetInterfacesList(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5491 | if (interfaces != nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5492 | for (size_t i = 0; i < interfaces->Size(); i++) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5493 | dex::TypeIndex idx = interfaces->GetTypeItem(i).type_idx_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5494 | ObjPtr<mirror::Class> interface = ResolveType(dex_file, idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5495 | if (interface == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5496 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5497 | return false; |
| 5498 | } |
| 5499 | // Verify |
| 5500 | if (!klass->CanAccess(interface)) { |
| 5501 | // TODO: the RI seemed to ignore this in my testing. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5502 | ThrowIllegalAccessError(klass.Get(), |
| 5503 | "Interface %s implemented by class %s is inaccessible", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5504 | interface->PrettyDescriptor().c_str(), |
| 5505 | klass->PrettyDescriptor().c_str()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5506 | return false; |
| 5507 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5508 | } |
| 5509 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 5510 | // Mark the class as loaded. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 5511 | mirror::Class::SetStatus(klass, mirror::Class::kStatusLoaded, nullptr); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5512 | return true; |
| 5513 | } |
| 5514 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5515 | bool ClassLinker::LinkSuperClass(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5516 | CHECK(!klass->IsPrimitive()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5517 | ObjPtr<mirror::Class> super = klass->GetSuperClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5518 | if (klass.Get() == GetClassRoot(kJavaLangObject)) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5519 | if (super != nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5520 | ThrowClassFormatError(klass.Get(), "java.lang.Object must not have a superclass"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5521 | return false; |
| 5522 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5523 | return true; |
| 5524 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5525 | if (super == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5526 | ThrowLinkageError(klass.Get(), "No superclass defined for class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5527 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5528 | return false; |
| 5529 | } |
| 5530 | // Verify |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 5531 | if (super->IsFinal() || super->IsInterface()) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5532 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 5533 | "Superclass %s of %s is %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5534 | super->PrettyDescriptor().c_str(), |
| 5535 | klass->PrettyDescriptor().c_str(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5536 | super->IsFinal() ? "declared final" : "an interface"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5537 | return false; |
| 5538 | } |
| 5539 | if (!klass->CanAccess(super)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5540 | ThrowIllegalAccessError(klass.Get(), "Superclass %s is inaccessible to class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5541 | super->PrettyDescriptor().c_str(), |
| 5542 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5543 | return false; |
| 5544 | } |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 5545 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5546 | // Inherit kAccClassIsFinalizable from the superclass in case this |
| 5547 | // class doesn't override finalize. |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 5548 | if (super->IsFinalizable()) { |
| 5549 | klass->SetFinalizable(); |
| 5550 | } |
| 5551 | |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 5552 | // Inherit class loader flag form super class. |
| 5553 | if (super->IsClassLoaderClass()) { |
| 5554 | klass->SetClassLoaderClass(); |
| 5555 | } |
| 5556 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5557 | // Inherit reference flags (if any) from the superclass. |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 5558 | uint32_t reference_flags = (super->GetClassFlags() & mirror::kClassFlagReference); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5559 | if (reference_flags != 0) { |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 5560 | CHECK_EQ(klass->GetClassFlags(), 0u); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 5561 | klass->SetClassFlags(klass->GetClassFlags() | reference_flags); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5562 | } |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 5563 | // Disallow custom direct subclasses of java.lang.ref.Reference. |
Elliott Hughes | bf61ba3 | 2011-10-11 10:53:09 -0700 | [diff] [blame] | 5564 | if (init_done_ && super == GetClassRoot(kJavaLangRefReference)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5565 | ThrowLinkageError(klass.Get(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5566 | "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] | 5567 | klass->PrettyDescriptor().c_str()); |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 5568 | return false; |
| 5569 | } |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5570 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5571 | if (kIsDebugBuild) { |
| 5572 | // Ensure super classes are fully resolved prior to resolving fields.. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5573 | while (super != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5574 | CHECK(super->IsResolved()); |
| 5575 | super = super->GetSuperClass(); |
| 5576 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5577 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5578 | return true; |
| 5579 | } |
| 5580 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5581 | // Populate the class vtable and itable. Compute return type indices. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5582 | bool ClassLinker::LinkMethods(Thread* self, |
| 5583 | Handle<mirror::Class> klass, |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5584 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5585 | bool* out_new_conflict, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5586 | ArtMethod** out_imt) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5587 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5588 | // A map from vtable indexes to the method they need to be updated to point to. Used because we |
| 5589 | // need to have default methods be in the virtuals array of each class but we don't set that up |
| 5590 | // until LinkInterfaceMethods. |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5591 | std::unordered_map<size_t, ClassLinker::MethodTranslation> default_translations; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5592 | // Link virtual methods then interface methods. |
| 5593 | // We set up the interface lookup table first because we need it to determine if we need to update |
| 5594 | // any vtable entries with new default method implementations. |
| 5595 | return SetupInterfaceLookupTable(self, klass, interfaces) |
| 5596 | && LinkVirtualMethods(self, klass, /*out*/ &default_translations) |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5597 | && LinkInterfaceMethods(self, klass, default_translations, out_new_conflict, out_imt); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5598 | } |
| 5599 | |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5600 | // Comparator for name and signature of a method, used in finding overriding methods. Implementation |
| 5601 | // avoids the use of handles, if it didn't then rather than compare dex files we could compare dex |
| 5602 | // caches in the implementation below. |
| 5603 | class MethodNameAndSignatureComparator FINAL : public ValueObject { |
| 5604 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5605 | explicit MethodNameAndSignatureComparator(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5606 | REQUIRES_SHARED(Locks::mutator_lock_) : |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5607 | dex_file_(method->GetDexFile()), mid_(&dex_file_->GetMethodId(method->GetDexMethodIndex())), |
| 5608 | name_(nullptr), name_len_(0) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5609 | DCHECK(!method->IsProxyMethod()) << method->PrettyMethod(); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5610 | } |
| 5611 | |
| 5612 | const char* GetName() { |
| 5613 | if (name_ == nullptr) { |
| 5614 | name_ = dex_file_->StringDataAndUtf16LengthByIdx(mid_->name_idx_, &name_len_); |
| 5615 | } |
| 5616 | return name_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5617 | } |
| 5618 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5619 | bool HasSameNameAndSignature(ArtMethod* other) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5620 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5621 | DCHECK(!other->IsProxyMethod()) << other->PrettyMethod(); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5622 | const DexFile* other_dex_file = other->GetDexFile(); |
| 5623 | const DexFile::MethodId& other_mid = other_dex_file->GetMethodId(other->GetDexMethodIndex()); |
| 5624 | if (dex_file_ == other_dex_file) { |
| 5625 | return mid_->name_idx_ == other_mid.name_idx_ && mid_->proto_idx_ == other_mid.proto_idx_; |
| 5626 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5627 | GetName(); // Only used to make sure its calculated. |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5628 | uint32_t other_name_len; |
| 5629 | const char* other_name = other_dex_file->StringDataAndUtf16LengthByIdx(other_mid.name_idx_, |
| 5630 | &other_name_len); |
| 5631 | if (name_len_ != other_name_len || strcmp(name_, other_name) != 0) { |
| 5632 | return false; |
| 5633 | } |
| 5634 | return dex_file_->GetMethodSignature(*mid_) == other_dex_file->GetMethodSignature(other_mid); |
| 5635 | } |
| 5636 | |
| 5637 | private: |
| 5638 | // Dex file for the method to compare against. |
| 5639 | const DexFile* const dex_file_; |
| 5640 | // MethodId for the method to compare against. |
| 5641 | const DexFile::MethodId* const mid_; |
| 5642 | // Lazily computed name from the dex file's strings. |
| 5643 | const char* name_; |
| 5644 | // Lazily computed name length. |
| 5645 | uint32_t name_len_; |
| 5646 | }; |
| 5647 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5648 | class LinkVirtualHashTable { |
| 5649 | public: |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5650 | LinkVirtualHashTable(Handle<mirror::Class> klass, |
| 5651 | size_t hash_size, |
| 5652 | uint32_t* hash_table, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 5653 | PointerSize image_pointer_size) |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5654 | : klass_(klass), |
| 5655 | hash_size_(hash_size), |
| 5656 | hash_table_(hash_table), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5657 | image_pointer_size_(image_pointer_size) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5658 | std::fill(hash_table_, hash_table_ + hash_size_, invalid_index_); |
| 5659 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5660 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5661 | void Add(uint32_t virtual_method_index) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5662 | ArtMethod* local_method = klass_->GetVirtualMethodDuringLinking( |
| 5663 | virtual_method_index, image_pointer_size_); |
| 5664 | const char* name = local_method->GetInterfaceMethodIfProxy(image_pointer_size_)->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 5665 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5666 | uint32_t index = hash % hash_size_; |
| 5667 | // Linear probe until we have an empty slot. |
| 5668 | while (hash_table_[index] != invalid_index_) { |
| 5669 | if (++index == hash_size_) { |
| 5670 | index = 0; |
| 5671 | } |
| 5672 | } |
| 5673 | hash_table_[index] = virtual_method_index; |
| 5674 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5675 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5676 | uint32_t FindAndRemove(MethodNameAndSignatureComparator* comparator) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5677 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5678 | const char* name = comparator->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 5679 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5680 | size_t index = hash % hash_size_; |
| 5681 | while (true) { |
| 5682 | const uint32_t value = hash_table_[index]; |
| 5683 | // Since linear probe makes continuous blocks, hitting an invalid index means we are done |
| 5684 | // the block and can safely assume not found. |
| 5685 | if (value == invalid_index_) { |
| 5686 | break; |
| 5687 | } |
| 5688 | if (value != removed_index_) { // This signifies not already overriden. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5689 | ArtMethod* virtual_method = |
| 5690 | klass_->GetVirtualMethodDuringLinking(value, image_pointer_size_); |
| 5691 | if (comparator->HasSameNameAndSignature( |
| 5692 | virtual_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5693 | hash_table_[index] = removed_index_; |
| 5694 | return value; |
| 5695 | } |
| 5696 | } |
| 5697 | if (++index == hash_size_) { |
| 5698 | index = 0; |
| 5699 | } |
| 5700 | } |
| 5701 | return GetNotFoundIndex(); |
| 5702 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5703 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5704 | static uint32_t GetNotFoundIndex() { |
| 5705 | return invalid_index_; |
| 5706 | } |
| 5707 | |
| 5708 | private: |
| 5709 | static const uint32_t invalid_index_; |
| 5710 | static const uint32_t removed_index_; |
| 5711 | |
| 5712 | Handle<mirror::Class> klass_; |
| 5713 | const size_t hash_size_; |
| 5714 | uint32_t* const hash_table_; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 5715 | const PointerSize image_pointer_size_; |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5716 | }; |
| 5717 | |
| 5718 | const uint32_t LinkVirtualHashTable::invalid_index_ = std::numeric_limits<uint32_t>::max(); |
| 5719 | const uint32_t LinkVirtualHashTable::removed_index_ = std::numeric_limits<uint32_t>::max() - 1; |
| 5720 | |
Stephen Hines | 1ddd913 | 2017-02-08 01:51:18 -0800 | [diff] [blame] | 5721 | bool ClassLinker::LinkVirtualMethods( |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5722 | Thread* self, |
| 5723 | Handle<mirror::Class> klass, |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5724 | /*out*/std::unordered_map<size_t, ClassLinker::MethodTranslation>* default_translations) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5725 | const size_t num_virtual_methods = klass->NumVirtualMethods(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5726 | if (klass->IsInterface()) { |
| 5727 | // No vtable. |
| 5728 | if (!IsUint<16>(num_virtual_methods)) { |
| 5729 | ThrowClassFormatError(klass.Get(), "Too many methods on interface: %zu", num_virtual_methods); |
| 5730 | return false; |
| 5731 | } |
| 5732 | bool has_defaults = false; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5733 | // Assign each method an IMT index and set the default flag. |
| 5734 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
| 5735 | ArtMethod* m = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
| 5736 | m->SetMethodIndex(i); |
| 5737 | if (!m->IsAbstract()) { |
| 5738 | m->SetAccessFlags(m->GetAccessFlags() | kAccDefault); |
| 5739 | has_defaults = true; |
| 5740 | } |
| 5741 | } |
| 5742 | // Mark that we have default methods so that we won't need to scan the virtual_methods_ array |
| 5743 | // during initialization. This is a performance optimization. We could simply traverse the |
| 5744 | // virtual_methods_ array again during initialization. |
| 5745 | if (has_defaults) { |
| 5746 | klass->SetHasDefaultMethods(); |
| 5747 | } |
| 5748 | return true; |
| 5749 | } else if (klass->HasSuperClass()) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5750 | const size_t super_vtable_length = klass->GetSuperClass()->GetVTableLength(); |
| 5751 | const size_t max_count = num_virtual_methods + super_vtable_length; |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5752 | StackHandleScope<2> hs(self); |
Mingyao Yang | 38eecb0 | 2014-08-13 14:51:03 -0700 | [diff] [blame] | 5753 | Handle<mirror::Class> super_class(hs.NewHandle(klass->GetSuperClass())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5754 | MutableHandle<mirror::PointerArray> vtable; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5755 | if (super_class->ShouldHaveEmbeddedVTable()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5756 | vtable = hs.NewHandle(AllocPointerArray(self, max_count)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5757 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5758 | self->AssertPendingOOMException(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5759 | return false; |
| 5760 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5761 | for (size_t i = 0; i < super_vtable_length; i++) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5762 | vtable->SetElementPtrSize( |
| 5763 | i, super_class->GetEmbeddedVTableEntry(i, image_pointer_size_), image_pointer_size_); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5764 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5765 | // We might need to change vtable if we have new virtual methods or new interfaces (since that |
| 5766 | // might give us new default methods). If no new interfaces then we can skip the rest since |
| 5767 | // the class cannot override any of the super-class's methods. This is required for |
| 5768 | // correctness since without it we might not update overridden default method vtable entries |
| 5769 | // correctly. |
| 5770 | if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5771 | klass->SetVTable(vtable.Get()); |
| 5772 | return true; |
| 5773 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5774 | } else { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5775 | DCHECK(super_class->IsAbstract() && !super_class->IsArrayClass()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5776 | auto* super_vtable = super_class->GetVTable(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5777 | CHECK(super_vtable != nullptr) << super_class->PrettyClass(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5778 | // We might need to change vtable if we have new virtual methods or new interfaces (since that |
| 5779 | // might give us new default methods). See comment above. |
| 5780 | if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5781 | klass->SetVTable(super_vtable); |
| 5782 | return true; |
| 5783 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5784 | vtable = hs.NewHandle(down_cast<mirror::PointerArray*>( |
| 5785 | super_vtable->CopyOf(self, max_count))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5786 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5787 | self->AssertPendingOOMException(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5788 | return false; |
| 5789 | } |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5790 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5791 | // How the algorithm works: |
| 5792 | // 1. Populate hash table by adding num_virtual_methods from klass. The values in the hash |
| 5793 | // table are: invalid_index for unused slots, index super_vtable_length + i for a virtual |
| 5794 | // method which has not been matched to a vtable method, and j if the virtual method at the |
| 5795 | // index overrode the super virtual method at index j. |
| 5796 | // 2. Loop through super virtual methods, if they overwrite, update hash table to j |
| 5797 | // (j < super_vtable_length) to avoid redundant checks. (TODO maybe use this info for reducing |
| 5798 | // the need for the initial vtable which we later shrink back down). |
| 5799 | // 3. Add non overridden methods to the end of the vtable. |
| 5800 | static constexpr size_t kMaxStackHash = 250; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5801 | // + 1 so that even if we only have new default methods we will still be able to use this hash |
| 5802 | // table (i.e. it will never have 0 size). |
| 5803 | const size_t hash_table_size = num_virtual_methods * 3 + 1; |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5804 | uint32_t* hash_table_ptr; |
| 5805 | std::unique_ptr<uint32_t[]> hash_heap_storage; |
| 5806 | if (hash_table_size <= kMaxStackHash) { |
| 5807 | hash_table_ptr = reinterpret_cast<uint32_t*>( |
| 5808 | alloca(hash_table_size * sizeof(*hash_table_ptr))); |
| 5809 | } else { |
| 5810 | hash_heap_storage.reset(new uint32_t[hash_table_size]); |
| 5811 | hash_table_ptr = hash_heap_storage.get(); |
| 5812 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5813 | 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] | 5814 | // Add virtual methods to the hash table. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5815 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5816 | DCHECK(klass->GetVirtualMethodDuringLinking( |
| 5817 | i, image_pointer_size_)->GetDeclaringClass() != nullptr); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5818 | hash_table.Add(i); |
| 5819 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5820 | // 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] | 5821 | // the hash table. |
| 5822 | for (size_t j = 0; j < super_vtable_length; ++j) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5823 | // 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] | 5824 | ArtMethod* super_method = vtable->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 5825 | if (!klass->CanAccessMember(super_method->GetDeclaringClass(), |
| 5826 | super_method->GetAccessFlags())) { |
| 5827 | // Continue on to the next method since this one is package private and canot be overridden. |
| 5828 | // Before Android 4.1, the package-private method super_method might have been incorrectly |
| 5829 | // overridden. |
| 5830 | continue; |
| 5831 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5832 | MethodNameAndSignatureComparator super_method_name_comparator( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5833 | super_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 5834 | // We remove the method so that subsequent lookups will be faster by making the hash-map |
| 5835 | // smaller as we go on. |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5836 | uint32_t hash_index = hash_table.FindAndRemove(&super_method_name_comparator); |
| 5837 | if (hash_index != hash_table.GetNotFoundIndex()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5838 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking( |
| 5839 | hash_index, image_pointer_size_); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 5840 | if (super_method->IsFinal()) { |
| 5841 | ThrowLinkageError(klass.Get(), "Method %s overrides final method in class %s", |
| 5842 | virtual_method->PrettyMethod().c_str(), |
| 5843 | super_method->GetDeclaringClassDescriptor()); |
| 5844 | return false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5845 | } |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 5846 | vtable->SetElementPtrSize(j, virtual_method, image_pointer_size_); |
| 5847 | virtual_method->SetMethodIndex(j); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5848 | } else if (super_method->IsOverridableByDefaultMethod()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5849 | // We didn't directly override this method but we might through default methods... |
| 5850 | // Check for default method update. |
| 5851 | ArtMethod* default_method = nullptr; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5852 | switch (FindDefaultMethodImplementation(self, |
| 5853 | super_method, |
| 5854 | klass, |
| 5855 | /*out*/&default_method)) { |
| 5856 | case DefaultMethodSearchResult::kDefaultConflict: { |
| 5857 | // A conflict was found looking for default methods. Note this (assuming it wasn't |
| 5858 | // pre-existing) in the translations map. |
| 5859 | if (UNLIKELY(!super_method->IsDefaultConflicting())) { |
| 5860 | // Don't generate another conflict method to reduce memory use as an optimization. |
| 5861 | default_translations->insert( |
| 5862 | {j, ClassLinker::MethodTranslation::CreateConflictingMethod()}); |
| 5863 | } |
| 5864 | break; |
| 5865 | } |
| 5866 | case DefaultMethodSearchResult::kAbstractFound: { |
| 5867 | // No conflict but method is abstract. |
| 5868 | // We note that this vtable entry must be made abstract. |
| 5869 | if (UNLIKELY(!super_method->IsAbstract())) { |
| 5870 | default_translations->insert( |
| 5871 | {j, ClassLinker::MethodTranslation::CreateAbstractMethod()}); |
| 5872 | } |
| 5873 | break; |
| 5874 | } |
| 5875 | case DefaultMethodSearchResult::kDefaultFound: { |
| 5876 | if (UNLIKELY(super_method->IsDefaultConflicting() || |
| 5877 | default_method->GetDeclaringClass() != super_method->GetDeclaringClass())) { |
| 5878 | // Found a default method implementation that is new. |
| 5879 | // TODO Refactor this add default methods to virtuals here and not in |
| 5880 | // LinkInterfaceMethods maybe. |
| 5881 | // The problem is default methods might override previously present |
| 5882 | // default-method or miranda-method vtable entries from the superclass. |
| 5883 | // Unfortunately we need these to be entries in this class's virtuals. We do not |
| 5884 | // give these entries there until LinkInterfaceMethods so we pass this map around |
| 5885 | // to let it know which vtable entries need to be updated. |
| 5886 | // Make a note that vtable entry j must be updated, store what it needs to be updated |
| 5887 | // to. We will allocate a virtual method slot in LinkInterfaceMethods and fix it up |
| 5888 | // then. |
| 5889 | default_translations->insert( |
| 5890 | {j, ClassLinker::MethodTranslation::CreateTranslatedMethod(default_method)}); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5891 | VLOG(class_linker) << "Method " << super_method->PrettyMethod() |
| 5892 | << " overridden by default " |
| 5893 | << default_method->PrettyMethod() |
| 5894 | << " in " << mirror::Class::PrettyClass(klass.Get()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5895 | } |
| 5896 | break; |
| 5897 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5898 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5899 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5900 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5901 | size_t actual_count = super_vtable_length; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5902 | // Add the non-overridden methods at the end. |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5903 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5904 | ArtMethod* local_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5905 | size_t method_idx = local_method->GetMethodIndexDuringLinking(); |
| 5906 | if (method_idx < super_vtable_length && |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5907 | local_method == vtable->GetElementPtrSize<ArtMethod*>(method_idx, image_pointer_size_)) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5908 | continue; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5909 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5910 | vtable->SetElementPtrSize(actual_count, local_method, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5911 | local_method->SetMethodIndex(actual_count); |
| 5912 | ++actual_count; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5913 | } |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 5914 | if (!IsUint<16>(actual_count)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5915 | ThrowClassFormatError(klass.Get(), "Too many methods defined on class: %zd", actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5916 | return false; |
| 5917 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5918 | // Shrink vtable if possible |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5919 | CHECK_LE(actual_count, max_count); |
| 5920 | if (actual_count < max_count) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5921 | vtable.Assign(down_cast<mirror::PointerArray*>(vtable->CopyOf(self, actual_count))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5922 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5923 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5924 | return false; |
| 5925 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5926 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5927 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5928 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 5929 | CHECK_EQ(klass.Get(), GetClassRoot(kJavaLangObject)); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 5930 | if (!IsUint<16>(num_virtual_methods)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5931 | ThrowClassFormatError(klass.Get(), "Too many methods: %d", |
| 5932 | static_cast<int>(num_virtual_methods)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5933 | return false; |
| 5934 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5935 | auto* vtable = AllocPointerArray(self, num_virtual_methods); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5936 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5937 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5938 | return false; |
| 5939 | } |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 5940 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5941 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
| 5942 | vtable->SetElementPtrSize(i, virtual_method, image_pointer_size_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5943 | virtual_method->SetMethodIndex(i & 0xFFFF); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5944 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5945 | klass->SetVTable(vtable); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5946 | } |
| 5947 | return true; |
| 5948 | } |
| 5949 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5950 | // Determine if the given iface has any subinterface in the given list that declares the method |
| 5951 | // specified by 'target'. |
| 5952 | // |
| 5953 | // Arguments |
| 5954 | // - self: The thread we are running on |
| 5955 | // - target: A comparator that will match any method that overrides the method we are checking for |
| 5956 | // - iftable: The iftable we are searching for an overriding method on. |
| 5957 | // - ifstart: The index of the interface we are checking to see if anything overrides |
| 5958 | // - iface: The interface we are checking to see if anything overrides. |
| 5959 | // - image_pointer_size: |
| 5960 | // The image pointer size. |
| 5961 | // |
| 5962 | // Returns |
| 5963 | // - True: There is some method that matches the target comparator defined in an interface that |
| 5964 | // is a subtype of iface. |
| 5965 | // - False: There is no method that matches the target comparator in any interface that is a subtype |
| 5966 | // of iface. |
| 5967 | static bool ContainsOverridingMethodOf(Thread* self, |
| 5968 | MethodNameAndSignatureComparator& target, |
| 5969 | Handle<mirror::IfTable> iftable, |
| 5970 | size_t ifstart, |
| 5971 | Handle<mirror::Class> iface, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 5972 | PointerSize image_pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5973 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5974 | DCHECK(self != nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5975 | DCHECK(iface != nullptr); |
| 5976 | DCHECK(iftable != nullptr); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5977 | DCHECK_GE(ifstart, 0u); |
| 5978 | DCHECK_LT(ifstart, iftable->Count()); |
| 5979 | DCHECK_EQ(iface.Get(), iftable->GetInterface(ifstart)); |
| 5980 | DCHECK(iface->IsInterface()); |
| 5981 | |
| 5982 | size_t iftable_count = iftable->Count(); |
| 5983 | StackHandleScope<1> hs(self); |
| 5984 | MutableHandle<mirror::Class> current_iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 5985 | for (size_t k = ifstart + 1; k < iftable_count; k++) { |
| 5986 | // Skip ifstart since our current interface obviously cannot override itself. |
| 5987 | current_iface.Assign(iftable->GetInterface(k)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5988 | // Iterate through every method on this interface. The order does not matter. |
| 5989 | for (ArtMethod& current_method : current_iface->GetDeclaredVirtualMethods(image_pointer_size)) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5990 | if (UNLIKELY(target.HasSameNameAndSignature( |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5991 | current_method.GetInterfaceMethodIfProxy(image_pointer_size)))) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5992 | // Check if the i'th interface is a subtype of this one. |
| 5993 | if (iface->IsAssignableFrom(current_iface.Get())) { |
| 5994 | return true; |
| 5995 | } |
| 5996 | break; |
| 5997 | } |
| 5998 | } |
| 5999 | } |
| 6000 | return false; |
| 6001 | } |
| 6002 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6003 | // 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] | 6004 | // out_default_method and returns kDefaultFound on success. If no default method was found return |
| 6005 | // kAbstractFound and store nullptr into out_default_method. If an error occurs (such as a |
| 6006 | // default_method conflict) it will return kDefaultConflict. |
| 6007 | ClassLinker::DefaultMethodSearchResult ClassLinker::FindDefaultMethodImplementation( |
| 6008 | Thread* self, |
| 6009 | ArtMethod* target_method, |
| 6010 | Handle<mirror::Class> klass, |
| 6011 | /*out*/ArtMethod** out_default_method) const { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6012 | DCHECK(self != nullptr); |
| 6013 | DCHECK(target_method != nullptr); |
| 6014 | DCHECK(out_default_method != nullptr); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6015 | |
| 6016 | *out_default_method = nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6017 | |
| 6018 | // We organize the interface table so that, for interface I any subinterfaces J follow it in the |
| 6019 | // table. This lets us walk the table backwards when searching for default methods. The first one |
| 6020 | // we encounter is the best candidate since it is the most specific. Once we have found it we keep |
| 6021 | // track of it and then continue checking all other interfaces, since we need to throw an error if |
| 6022 | // we encounter conflicting default method implementations (one is not a subtype of the other). |
| 6023 | // |
| 6024 | // The order of unrelated interfaces does not matter and is not defined. |
| 6025 | size_t iftable_count = klass->GetIfTableCount(); |
| 6026 | if (iftable_count == 0) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6027 | // No interfaces. We have already reset out to null so just return kAbstractFound. |
| 6028 | return DefaultMethodSearchResult::kAbstractFound; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6029 | } |
| 6030 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6031 | StackHandleScope<3> hs(self); |
| 6032 | MutableHandle<mirror::Class> chosen_iface(hs.NewHandle<mirror::Class>(nullptr)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6033 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable())); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6034 | MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6035 | MethodNameAndSignatureComparator target_name_comparator( |
| 6036 | target_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
| 6037 | // Iterates over the klass's iftable in reverse |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6038 | for (size_t k = iftable_count; k != 0; ) { |
| 6039 | --k; |
| 6040 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6041 | DCHECK_LT(k, iftable->Count()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6042 | |
| 6043 | iface.Assign(iftable->GetInterface(k)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6044 | // Iterate through every declared method on this interface. The order does not matter. |
| 6045 | for (auto& method_iter : iface->GetDeclaredVirtualMethods(image_pointer_size_)) { |
| 6046 | ArtMethod* current_method = &method_iter; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6047 | // Skip abstract methods and methods with different names. |
| 6048 | if (current_method->IsAbstract() || |
| 6049 | !target_name_comparator.HasSameNameAndSignature( |
| 6050 | current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
| 6051 | continue; |
Alex Light | d7c10c2 | 2016-03-31 10:03:07 -0700 | [diff] [blame] | 6052 | } else if (!current_method->IsPublic()) { |
| 6053 | // The verifier should have caught the non-public method for dex version 37. Just warn and |
| 6054 | // skip it since this is from before default-methods so we don't really need to care that it |
| 6055 | // has code. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6056 | LOG(WARNING) << "Interface method " << current_method->PrettyMethod() |
| 6057 | << " is not public! " |
Alex Light | d7c10c2 | 2016-03-31 10:03:07 -0700 | [diff] [blame] | 6058 | << "This will be a fatal error in subsequent versions of android. " |
| 6059 | << "Continuing anyway."; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6060 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6061 | if (UNLIKELY(chosen_iface != nullptr)) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6062 | // We have multiple default impls of the same method. This is a potential default conflict. |
| 6063 | // We need to check if this possibly conflicting method is either a superclass of the chosen |
| 6064 | // default implementation or is overridden by a non-default interface method. In either case |
| 6065 | // there is no conflict. |
| 6066 | if (!iface->IsAssignableFrom(chosen_iface.Get()) && |
| 6067 | !ContainsOverridingMethodOf(self, |
| 6068 | target_name_comparator, |
| 6069 | iftable, |
| 6070 | k, |
| 6071 | iface, |
| 6072 | image_pointer_size_)) { |
Nicolas Geoffray | 7f3e0db | 2016-01-28 09:29:31 +0000 | [diff] [blame] | 6073 | VLOG(class_linker) << "Conflicting default method implementations found: " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6074 | << current_method->PrettyMethod() << " and " |
| 6075 | << ArtMethod::PrettyMethod(*out_default_method) << " in class " |
| 6076 | << klass->PrettyClass() << " conflict."; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6077 | *out_default_method = nullptr; |
| 6078 | return DefaultMethodSearchResult::kDefaultConflict; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6079 | } else { |
| 6080 | break; // Continue checking at the next interface. |
| 6081 | } |
| 6082 | } else { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6083 | // chosen_iface == null |
| 6084 | if (!ContainsOverridingMethodOf(self, |
| 6085 | target_name_comparator, |
| 6086 | iftable, |
| 6087 | k, |
| 6088 | iface, |
| 6089 | image_pointer_size_)) { |
| 6090 | // Don't set this as the chosen interface if something else is overriding it (because that |
| 6091 | // other interface would be potentially chosen instead if it was default). If the other |
| 6092 | // interface was abstract then we wouldn't select this interface as chosen anyway since |
| 6093 | // the abstract method masks it. |
| 6094 | *out_default_method = current_method; |
| 6095 | chosen_iface.Assign(iface.Get()); |
| 6096 | // We should now finish traversing the graph to find if we have default methods that |
| 6097 | // conflict. |
| 6098 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6099 | VLOG(class_linker) << "A default method '" << current_method->PrettyMethod() |
| 6100 | << "' was " |
| 6101 | << "skipped because it was overridden by an abstract method in a " |
| 6102 | << "subinterface on class '" << klass->PrettyClass() << "'"; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6103 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6104 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6105 | break; |
| 6106 | } |
| 6107 | } |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6108 | if (*out_default_method != nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6109 | VLOG(class_linker) << "Default method '" << (*out_default_method)->PrettyMethod() |
| 6110 | << "' selected " |
| 6111 | << "as the implementation for '" << target_method->PrettyMethod() |
| 6112 | << "' in '" << klass->PrettyClass() << "'"; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6113 | return DefaultMethodSearchResult::kDefaultFound; |
| 6114 | } else { |
| 6115 | return DefaultMethodSearchResult::kAbstractFound; |
| 6116 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6117 | } |
| 6118 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6119 | ArtMethod* ClassLinker::AddMethodToConflictTable(ObjPtr<mirror::Class> klass, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6120 | ArtMethod* conflict_method, |
| 6121 | ArtMethod* interface_method, |
| 6122 | ArtMethod* method, |
| 6123 | bool force_new_conflict_method) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6124 | ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6125 | Runtime* const runtime = Runtime::Current(); |
| 6126 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
| 6127 | bool new_entry = conflict_method == runtime->GetImtConflictMethod() || force_new_conflict_method; |
| 6128 | |
| 6129 | // Create a new entry if the existing one is the shared conflict method. |
| 6130 | ArtMethod* new_conflict_method = new_entry |
| 6131 | ? runtime->CreateImtConflictMethod(linear_alloc) |
| 6132 | : conflict_method; |
| 6133 | |
| 6134 | // Allocate a new table. Note that we will leak this table at the next conflict, |
| 6135 | // but that's a tradeoff compared to making the table fixed size. |
| 6136 | void* data = linear_alloc->Alloc( |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6137 | Thread::Current(), ImtConflictTable::ComputeSizeWithOneMoreEntry(current_table, |
| 6138 | image_pointer_size_)); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6139 | if (data == nullptr) { |
| 6140 | LOG(ERROR) << "Failed to allocate conflict table"; |
| 6141 | return conflict_method; |
| 6142 | } |
| 6143 | ImtConflictTable* new_table = new (data) ImtConflictTable(current_table, |
| 6144 | interface_method, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6145 | method, |
| 6146 | image_pointer_size_); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6147 | |
| 6148 | // Do a fence to ensure threads see the data in the table before it is assigned |
| 6149 | // to the conflict method. |
| 6150 | // Note that there is a race in the presence of multiple threads and we may leak |
| 6151 | // memory from the LinearAlloc, but that's a tradeoff compared to using |
| 6152 | // atomic operations. |
| 6153 | QuasiAtomic::ThreadFenceRelease(); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6154 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6155 | return new_conflict_method; |
| 6156 | } |
| 6157 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6158 | bool ClassLinker::AllocateIfTableMethodArrays(Thread* self, |
| 6159 | Handle<mirror::Class> klass, |
| 6160 | Handle<mirror::IfTable> iftable) { |
| 6161 | DCHECK(!klass->IsInterface()); |
| 6162 | const bool has_superclass = klass->HasSuperClass(); |
| 6163 | const bool extend_super_iftable = has_superclass; |
| 6164 | const size_t ifcount = klass->GetIfTableCount(); |
| 6165 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
| 6166 | for (size_t i = 0; i < ifcount; ++i) { |
| 6167 | size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods(); |
| 6168 | if (num_methods > 0) { |
| 6169 | const bool is_super = i < super_ifcount; |
| 6170 | // This is an interface implemented by a super-class. Therefore we can just copy the method |
| 6171 | // array from the superclass. |
| 6172 | const bool super_interface = is_super && extend_super_iftable; |
| 6173 | ObjPtr<mirror::PointerArray> method_array; |
| 6174 | if (super_interface) { |
| 6175 | ObjPtr<mirror::IfTable> if_table = klass->GetSuperClass()->GetIfTable(); |
| 6176 | DCHECK(if_table != nullptr); |
| 6177 | DCHECK(if_table->GetMethodArray(i) != nullptr); |
| 6178 | // If we are working on a super interface, try extending the existing method array. |
| 6179 | method_array = down_cast<mirror::PointerArray*>(if_table->GetMethodArray(i)->Clone(self)); |
| 6180 | } else { |
| 6181 | method_array = AllocPointerArray(self, num_methods); |
| 6182 | } |
| 6183 | if (UNLIKELY(method_array == nullptr)) { |
| 6184 | self->AssertPendingOOMException(); |
| 6185 | return false; |
| 6186 | } |
| 6187 | iftable->SetMethodArray(i, method_array); |
| 6188 | } |
| 6189 | } |
| 6190 | return true; |
| 6191 | } |
| 6192 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6193 | void ClassLinker::SetIMTRef(ArtMethod* unimplemented_method, |
| 6194 | ArtMethod* imt_conflict_method, |
| 6195 | ArtMethod* current_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6196 | /*out*/bool* new_conflict, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6197 | /*out*/ArtMethod** imt_ref) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6198 | // Place method in imt if entry is empty, place conflict otherwise. |
| 6199 | if (*imt_ref == unimplemented_method) { |
| 6200 | *imt_ref = current_method; |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6201 | } else if (!(*imt_ref)->IsRuntimeMethod()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6202 | // If we are not a conflict and we have the same signature and name as the imt |
| 6203 | // entry, it must be that we overwrote a superclass vtable entry. |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6204 | // Note that we have checked IsRuntimeMethod, as there may be multiple different |
| 6205 | // conflict methods. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6206 | MethodNameAndSignatureComparator imt_comparator( |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6207 | (*imt_ref)->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6208 | if (imt_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6209 | current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6210 | *imt_ref = current_method; |
| 6211 | } else { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6212 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6213 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6214 | } |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6215 | } else { |
| 6216 | // Place the default conflict method. Note that there may be an existing conflict |
| 6217 | // method in the IMT, but it could be one tailored to the super class, with a |
| 6218 | // specific ImtConflictTable. |
| 6219 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6220 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6221 | } |
| 6222 | } |
| 6223 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6224 | void ClassLinker::FillIMTAndConflictTables(ObjPtr<mirror::Class> klass) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6225 | DCHECK(klass->ShouldHaveImt()) << klass->PrettyClass(); |
| 6226 | DCHECK(!klass->IsTemp()) << klass->PrettyClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6227 | ArtMethod* imt_data[ImTable::kSize]; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6228 | Runtime* const runtime = Runtime::Current(); |
| 6229 | ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod(); |
| 6230 | ArtMethod* const conflict_method = runtime->GetImtConflictMethod(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6231 | std::fill_n(imt_data, arraysize(imt_data), unimplemented_method); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6232 | if (klass->GetIfTable() != nullptr) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6233 | bool new_conflict = false; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6234 | FillIMTFromIfTable(klass->GetIfTable(), |
| 6235 | unimplemented_method, |
| 6236 | conflict_method, |
| 6237 | klass, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6238 | /*create_conflict_tables*/true, |
| 6239 | /*ignore_copied_methods*/false, |
| 6240 | &new_conflict, |
| 6241 | &imt_data[0]); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6242 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6243 | if (!klass->ShouldHaveImt()) { |
| 6244 | return; |
| 6245 | } |
| 6246 | // Compare the IMT with the super class including the conflict methods. If they are equivalent, |
| 6247 | // we can just use the same pointer. |
| 6248 | ImTable* imt = nullptr; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6249 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6250 | if (super_class != nullptr && super_class->ShouldHaveImt()) { |
| 6251 | ImTable* super_imt = super_class->GetImt(image_pointer_size_); |
| 6252 | bool same = true; |
| 6253 | for (size_t i = 0; same && i < ImTable::kSize; ++i) { |
| 6254 | ArtMethod* method = imt_data[i]; |
| 6255 | ArtMethod* super_method = super_imt->Get(i, image_pointer_size_); |
| 6256 | if (method != super_method) { |
| 6257 | bool is_conflict_table = method->IsRuntimeMethod() && |
| 6258 | method != unimplemented_method && |
| 6259 | method != conflict_method; |
| 6260 | // Verify conflict contents. |
| 6261 | bool super_conflict_table = super_method->IsRuntimeMethod() && |
| 6262 | super_method != unimplemented_method && |
| 6263 | super_method != conflict_method; |
| 6264 | if (!is_conflict_table || !super_conflict_table) { |
| 6265 | same = false; |
| 6266 | } else { |
| 6267 | ImtConflictTable* table1 = method->GetImtConflictTable(image_pointer_size_); |
| 6268 | ImtConflictTable* table2 = super_method->GetImtConflictTable(image_pointer_size_); |
| 6269 | same = same && table1->Equals(table2, image_pointer_size_); |
| 6270 | } |
| 6271 | } |
| 6272 | } |
| 6273 | if (same) { |
| 6274 | imt = super_imt; |
| 6275 | } |
| 6276 | } |
| 6277 | if (imt == nullptr) { |
| 6278 | imt = klass->GetImt(image_pointer_size_); |
| 6279 | DCHECK(imt != nullptr); |
| 6280 | imt->Populate(imt_data, image_pointer_size_); |
| 6281 | } else { |
| 6282 | klass->SetImt(imt, image_pointer_size_); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6283 | } |
| 6284 | } |
| 6285 | |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6286 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, |
| 6287 | LinearAlloc* linear_alloc, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6288 | PointerSize image_pointer_size) { |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6289 | void* data = linear_alloc->Alloc(Thread::Current(), |
| 6290 | ImtConflictTable::ComputeSize(count, |
| 6291 | image_pointer_size)); |
| 6292 | return (data != nullptr) ? new (data) ImtConflictTable(count, image_pointer_size) : nullptr; |
| 6293 | } |
| 6294 | |
| 6295 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, LinearAlloc* linear_alloc) { |
| 6296 | return CreateImtConflictTable(count, linear_alloc, image_pointer_size_); |
| 6297 | } |
| 6298 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6299 | void ClassLinker::FillIMTFromIfTable(ObjPtr<mirror::IfTable> if_table, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6300 | ArtMethod* unimplemented_method, |
| 6301 | ArtMethod* imt_conflict_method, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6302 | ObjPtr<mirror::Class> klass, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6303 | bool create_conflict_tables, |
| 6304 | bool ignore_copied_methods, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6305 | /*out*/bool* new_conflict, |
| 6306 | /*out*/ArtMethod** imt) { |
| 6307 | uint32_t conflict_counts[ImTable::kSize] = {}; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6308 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6309 | ObjPtr<mirror::Class> interface = if_table->GetInterface(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6310 | const size_t num_virtuals = interface->NumVirtualMethods(); |
| 6311 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 6312 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 6313 | DCHECK_GE(num_virtuals, method_array_count); |
| 6314 | if (kIsDebugBuild) { |
| 6315 | if (klass->IsInterface()) { |
| 6316 | DCHECK_EQ(method_array_count, 0u); |
| 6317 | } else { |
| 6318 | DCHECK_EQ(interface->NumDeclaredVirtualMethods(), method_array_count); |
| 6319 | } |
| 6320 | } |
| 6321 | if (method_array_count == 0) { |
| 6322 | continue; |
| 6323 | } |
| 6324 | auto* method_array = if_table->GetMethodArray(i); |
| 6325 | for (size_t j = 0; j < method_array_count; ++j) { |
| 6326 | ArtMethod* implementation_method = |
| 6327 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 6328 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 6329 | continue; |
| 6330 | } |
| 6331 | DCHECK(implementation_method != nullptr); |
| 6332 | // Miranda methods cannot be used to implement an interface method, but they are safe to put |
| 6333 | // in the IMT since their entrypoint is the interface trampoline. If we put any copied methods |
| 6334 | // or interface methods in the IMT here they will not create extra conflicts since we compare |
| 6335 | // names and signatures in SetIMTRef. |
| 6336 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 6337 | const uint32_t imt_index = ImTable::GetImtIndex(interface_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6338 | |
| 6339 | // There is only any conflicts if all of the interface methods for an IMT slot don't have |
| 6340 | // the same implementation method, keep track of this to avoid creating a conflict table in |
| 6341 | // this case. |
| 6342 | |
| 6343 | // Conflict table size for each IMT slot. |
| 6344 | ++conflict_counts[imt_index]; |
| 6345 | |
| 6346 | SetIMTRef(unimplemented_method, |
| 6347 | imt_conflict_method, |
| 6348 | implementation_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6349 | /*out*/new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6350 | /*out*/&imt[imt_index]); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6351 | } |
| 6352 | } |
| 6353 | |
| 6354 | if (create_conflict_tables) { |
| 6355 | // Create the conflict tables. |
| 6356 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6357 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6358 | size_t conflicts = conflict_counts[i]; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6359 | if (imt[i] == imt_conflict_method) { |
| 6360 | ImtConflictTable* new_table = CreateImtConflictTable(conflicts, linear_alloc); |
| 6361 | if (new_table != nullptr) { |
| 6362 | ArtMethod* new_conflict_method = |
| 6363 | Runtime::Current()->CreateImtConflictMethod(linear_alloc); |
| 6364 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
| 6365 | imt[i] = new_conflict_method; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6366 | } else { |
| 6367 | LOG(ERROR) << "Failed to allocate conflict table"; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6368 | imt[i] = imt_conflict_method; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6369 | } |
| 6370 | } else { |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6371 | DCHECK_NE(imt[i], imt_conflict_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6372 | } |
| 6373 | } |
| 6374 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6375 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6376 | ObjPtr<mirror::Class> interface = if_table->GetInterface(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6377 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 6378 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 6379 | if (method_array_count == 0) { |
| 6380 | continue; |
| 6381 | } |
| 6382 | auto* method_array = if_table->GetMethodArray(i); |
| 6383 | for (size_t j = 0; j < method_array_count; ++j) { |
| 6384 | ArtMethod* implementation_method = |
| 6385 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 6386 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 6387 | continue; |
| 6388 | } |
| 6389 | DCHECK(implementation_method != nullptr); |
| 6390 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 6391 | const uint32_t imt_index = ImTable::GetImtIndex(interface_method); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6392 | if (!imt[imt_index]->IsRuntimeMethod() || |
| 6393 | imt[imt_index] == unimplemented_method || |
| 6394 | imt[imt_index] == imt_conflict_method) { |
| 6395 | continue; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6396 | } |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6397 | ImtConflictTable* table = imt[imt_index]->GetImtConflictTable(image_pointer_size_); |
| 6398 | const size_t num_entries = table->NumEntries(image_pointer_size_); |
| 6399 | table->SetInterfaceMethod(num_entries, image_pointer_size_, interface_method); |
| 6400 | table->SetImplementationMethod(num_entries, image_pointer_size_, implementation_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6401 | } |
| 6402 | } |
| 6403 | } |
| 6404 | } |
| 6405 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6406 | // Simple helper function that checks that no subtypes of 'val' are contained within the 'classes' |
| 6407 | // set. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6408 | static bool NotSubinterfaceOfAny( |
| 6409 | const std::unordered_set<ObjPtr<mirror::Class>, HashObjPtr>& classes, |
| 6410 | ObjPtr<mirror::Class> val) |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6411 | REQUIRES(Roles::uninterruptible_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6412 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6413 | DCHECK(val != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6414 | for (ObjPtr<mirror::Class> c : classes) { |
| 6415 | if (val->IsAssignableFrom(c)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6416 | return false; |
| 6417 | } |
| 6418 | } |
| 6419 | return true; |
| 6420 | } |
| 6421 | |
| 6422 | // Fills in and flattens the interface inheritance hierarchy. |
| 6423 | // |
| 6424 | // By the end of this function all interfaces in the transitive closure of to_process are added to |
| 6425 | // the iftable and every interface precedes all of its sub-interfaces in this list. |
| 6426 | // |
| 6427 | // all I, J: Interface | I <: J implies J precedes I |
| 6428 | // |
| 6429 | // (note A <: B means that A is a subtype of B) |
| 6430 | // |
| 6431 | // This returns the total number of items in the iftable. The iftable might be resized down after |
| 6432 | // this call. |
| 6433 | // |
| 6434 | // We order this backwards so that we do not need to reorder superclass interfaces when new |
| 6435 | // interfaces are added in subclass's interface tables. |
| 6436 | // |
| 6437 | // Upon entry into this function iftable is a copy of the superclass's iftable with the first |
| 6438 | // super_ifcount entries filled in with the transitive closure of the interfaces of the superclass. |
| 6439 | // The other entries are uninitialized. We will fill in the remaining entries in this function. The |
| 6440 | // 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] | 6441 | static size_t FillIfTable(ObjPtr<mirror::IfTable> iftable, |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6442 | size_t super_ifcount, |
| 6443 | std::vector<mirror::Class*> to_process) |
| 6444 | REQUIRES(Roles::uninterruptible_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6445 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6446 | // This is the set of all class's already in the iftable. Used to make checking if a class has |
| 6447 | // already been added quicker. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6448 | std::unordered_set<ObjPtr<mirror::Class>, HashObjPtr> classes_in_iftable; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6449 | // The first super_ifcount elements are from the superclass. We note that they are already added. |
| 6450 | for (size_t i = 0; i < super_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6451 | ObjPtr<mirror::Class> iface = iftable->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6452 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, iface)) << "Bad ordering."; |
| 6453 | classes_in_iftable.insert(iface); |
| 6454 | } |
| 6455 | size_t filled_ifcount = super_ifcount; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6456 | for (ObjPtr<mirror::Class> interface : to_process) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6457 | // Let us call the first filled_ifcount elements of iftable the current-iface-list. |
| 6458 | // At this point in the loop current-iface-list has the invariant that: |
| 6459 | // for every pair of interfaces I,J within it: |
| 6460 | // if index_of(I) < index_of(J) then I is not a subtype of J |
| 6461 | |
| 6462 | // If we have already seen this element then all of its super-interfaces must already be in the |
| 6463 | // current-iface-list so we can skip adding it. |
| 6464 | if (!ContainsElement(classes_in_iftable, interface)) { |
| 6465 | // We haven't seen this interface so add all of its super-interfaces onto the |
| 6466 | // current-iface-list, skipping those already on it. |
| 6467 | int32_t ifcount = interface->GetIfTableCount(); |
| 6468 | for (int32_t j = 0; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6469 | ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6470 | if (!ContainsElement(classes_in_iftable, super_interface)) { |
| 6471 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, super_interface)) << "Bad ordering."; |
| 6472 | classes_in_iftable.insert(super_interface); |
| 6473 | iftable->SetInterface(filled_ifcount, super_interface); |
| 6474 | filled_ifcount++; |
| 6475 | } |
| 6476 | } |
| 6477 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, interface)) << "Bad ordering"; |
| 6478 | // Place this interface onto the current-iface-list after all of its super-interfaces. |
| 6479 | classes_in_iftable.insert(interface); |
| 6480 | iftable->SetInterface(filled_ifcount, interface); |
| 6481 | filled_ifcount++; |
| 6482 | } else if (kIsDebugBuild) { |
| 6483 | // Check all super-interfaces are already in the list. |
| 6484 | int32_t ifcount = interface->GetIfTableCount(); |
| 6485 | for (int32_t j = 0; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6486 | ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6487 | DCHECK(ContainsElement(classes_in_iftable, super_interface)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6488 | << "Iftable does not contain " << mirror::Class::PrettyClass(super_interface) |
| 6489 | << ", a superinterface of " << interface->PrettyClass(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6490 | } |
| 6491 | } |
| 6492 | } |
| 6493 | if (kIsDebugBuild) { |
| 6494 | // Check that the iftable is ordered correctly. |
| 6495 | for (size_t i = 0; i < filled_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6496 | ObjPtr<mirror::Class> if_a = iftable->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6497 | for (size_t j = i + 1; j < filled_ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6498 | ObjPtr<mirror::Class> if_b = iftable->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6499 | // !(if_a <: if_b) |
| 6500 | CHECK(!if_b->IsAssignableFrom(if_a)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6501 | << "Bad interface order: " << mirror::Class::PrettyClass(if_a) << " (index " << i |
| 6502 | << ") extends " |
| 6503 | << 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] | 6504 | << "interface list."; |
| 6505 | } |
| 6506 | } |
| 6507 | } |
| 6508 | return filled_ifcount; |
| 6509 | } |
| 6510 | |
| 6511 | bool ClassLinker::SetupInterfaceLookupTable(Thread* self, Handle<mirror::Class> klass, |
| 6512 | Handle<mirror::ObjectArray<mirror::Class>> interfaces) { |
| 6513 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 6514 | const bool has_superclass = klass->HasSuperClass(); |
| 6515 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6516 | const bool have_interfaces = interfaces != nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6517 | const size_t num_interfaces = |
| 6518 | have_interfaces ? interfaces->GetLength() : klass->NumDirectInterfaces(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6519 | if (num_interfaces == 0) { |
| 6520 | if (super_ifcount == 0) { |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 6521 | if (LIKELY(has_superclass)) { |
| 6522 | klass->SetIfTable(klass->GetSuperClass()->GetIfTable()); |
| 6523 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6524 | // Class implements no interfaces. |
| 6525 | DCHECK_EQ(klass->GetIfTableCount(), 0); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6526 | return true; |
| 6527 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6528 | // Class implements same interfaces as parent, are any of these not marker interfaces? |
| 6529 | bool has_non_marker_interface = false; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6530 | ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6531 | for (size_t i = 0; i < super_ifcount; ++i) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6532 | if (super_iftable->GetMethodArrayCount(i) > 0) { |
| 6533 | has_non_marker_interface = true; |
| 6534 | break; |
| 6535 | } |
| 6536 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6537 | // Class just inherits marker interfaces from parent so recycle parent's iftable. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6538 | if (!has_non_marker_interface) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6539 | klass->SetIfTable(super_iftable); |
| 6540 | return true; |
| 6541 | } |
| 6542 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6543 | size_t ifcount = super_ifcount + num_interfaces; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6544 | // Check that every class being implemented is an interface. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6545 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 6546 | ObjPtr<mirror::Class> interface = have_interfaces |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6547 | ? interfaces->GetWithoutChecks(i) |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 6548 | : mirror::Class::GetDirectInterface(self, klass.Get(), i); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6549 | DCHECK(interface != nullptr); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6550 | if (UNLIKELY(!interface->IsInterface())) { |
| 6551 | std::string temp; |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6552 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 6553 | "Class %s implements non-interface class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6554 | klass->PrettyDescriptor().c_str(), |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6555 | PrettyDescriptor(interface->GetDescriptor(&temp)).c_str()); |
| 6556 | return false; |
| 6557 | } |
| 6558 | ifcount += interface->GetIfTableCount(); |
| 6559 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6560 | // Create the interface function table. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 6561 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(AllocIfTable(self, ifcount))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6562 | if (UNLIKELY(iftable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6563 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6564 | return false; |
| 6565 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6566 | // Fill in table with superclass's iftable. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6567 | if (super_ifcount != 0) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6568 | ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable(); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 6569 | for (size_t i = 0; i < super_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6570 | ObjPtr<mirror::Class> super_interface = super_iftable->GetInterface(i); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6571 | iftable->SetInterface(i, super_interface); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 6572 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6573 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6574 | |
| 6575 | // Note that AllowThreadSuspension is to thread suspension as pthread_testcancel is to pthread |
| 6576 | // cancellation. That is it will suspend if one has a pending suspend request but otherwise |
| 6577 | // doesn't really do anything. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 6578 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6579 | |
| 6580 | size_t new_ifcount; |
| 6581 | { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 6582 | ScopedAssertNoThreadSuspension nts("Copying mirror::Class*'s for FillIfTable"); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6583 | std::vector<mirror::Class*> to_add; |
| 6584 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 6585 | ObjPtr<mirror::Class> interface = have_interfaces ? interfaces->Get(i) : |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 6586 | mirror::Class::GetDirectInterface(self, klass.Get(), i); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 6587 | to_add.push_back(interface.Ptr()); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6588 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6589 | |
| 6590 | new_ifcount = FillIfTable(iftable.Get(), super_ifcount, std::move(to_add)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6591 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6592 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 6593 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6594 | |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6595 | // Shrink iftable in case duplicates were found |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6596 | if (new_ifcount < ifcount) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6597 | DCHECK_NE(num_interfaces, 0U); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6598 | iftable.Assign(down_cast<mirror::IfTable*>( |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6599 | iftable->CopyOf(self, new_ifcount * mirror::IfTable::kMax))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6600 | if (UNLIKELY(iftable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6601 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6602 | return false; |
| 6603 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6604 | ifcount = new_ifcount; |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6605 | } else { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6606 | DCHECK_EQ(new_ifcount, ifcount); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6607 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6608 | klass->SetIfTable(iftable.Get()); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6609 | return true; |
| 6610 | } |
| 6611 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6612 | // Finds the method with a name/signature that matches cmp in the given lists of methods. The list |
| 6613 | // of methods must be unique. |
| 6614 | static ArtMethod* FindSameNameAndSignature(MethodNameAndSignatureComparator& cmp ATTRIBUTE_UNUSED) { |
| 6615 | return nullptr; |
| 6616 | } |
| 6617 | |
| 6618 | template <typename ... Types> |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6619 | static ArtMethod* FindSameNameAndSignature(MethodNameAndSignatureComparator& cmp, |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6620 | const ScopedArenaVector<ArtMethod*>& list, |
| 6621 | const Types& ... rest) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6622 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6623 | for (ArtMethod* method : list) { |
| 6624 | if (cmp.HasSameNameAndSignature(method)) { |
| 6625 | return method; |
| 6626 | } |
| 6627 | } |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6628 | return FindSameNameAndSignature(cmp, rest...); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6629 | } |
| 6630 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6631 | // Check that all vtable entries are present in this class's virtuals or are the same as a |
| 6632 | // superclasses vtable entry. |
| 6633 | static void CheckClassOwnsVTableEntries(Thread* self, |
| 6634 | Handle<mirror::Class> klass, |
| 6635 | PointerSize pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6636 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6637 | StackHandleScope<2> hs(self); |
| 6638 | Handle<mirror::PointerArray> check_vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6639 | ObjPtr<mirror::Class> super_temp = (klass->HasSuperClass()) ? klass->GetSuperClass() : nullptr; |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6640 | Handle<mirror::Class> superclass(hs.NewHandle(super_temp)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6641 | int32_t super_vtable_length = (superclass != nullptr) ? superclass->GetVTableLength() : 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6642 | for (int32_t i = 0; i < check_vtable->GetLength(); ++i) { |
| 6643 | ArtMethod* m = check_vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 6644 | CHECK(m != nullptr); |
| 6645 | |
Alex Light | a41a3078 | 2017-03-29 11:33:19 -0700 | [diff] [blame] | 6646 | if (m->GetMethodIndexDuringLinking() != i) { |
| 6647 | LOG(WARNING) << m->PrettyMethod() |
| 6648 | << " has an unexpected method index for its spot in the vtable for class" |
| 6649 | << klass->PrettyClass(); |
| 6650 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6651 | ArraySlice<ArtMethod> virtuals = klass->GetVirtualMethodsSliceUnchecked(pointer_size); |
| 6652 | auto is_same_method = [m] (const ArtMethod& meth) { |
| 6653 | return &meth == m; |
| 6654 | }; |
Alex Light | 3f98053 | 2017-03-17 15:10:32 -0700 | [diff] [blame] | 6655 | if (!((super_vtable_length > i && superclass->GetVTableEntry(i, pointer_size) == m) || |
| 6656 | std::find_if(virtuals.begin(), virtuals.end(), is_same_method) != virtuals.end())) { |
| 6657 | LOG(WARNING) << m->PrettyMethod() << " does not seem to be owned by current class " |
| 6658 | << klass->PrettyClass() << " or any of its superclasses!"; |
| 6659 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6660 | } |
| 6661 | } |
| 6662 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6663 | // Check to make sure the vtable does not have duplicates. Duplicates could cause problems when a |
| 6664 | // method is overridden in a subclass. |
| 6665 | static void CheckVTableHasNoDuplicates(Thread* self, |
| 6666 | Handle<mirror::Class> klass, |
| 6667 | PointerSize pointer_size) |
| 6668 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6669 | StackHandleScope<1> hs(self); |
| 6670 | Handle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
| 6671 | int32_t num_entries = vtable->GetLength(); |
| 6672 | for (int32_t i = 0; i < num_entries; i++) { |
| 6673 | ArtMethod* vtable_entry = vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 6674 | // Don't bother if we cannot 'see' the vtable entry (i.e. it is a package-private member maybe). |
| 6675 | if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(), |
| 6676 | vtable_entry->GetAccessFlags())) { |
| 6677 | continue; |
| 6678 | } |
| 6679 | MethodNameAndSignatureComparator name_comparator( |
| 6680 | vtable_entry->GetInterfaceMethodIfProxy(pointer_size)); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 6681 | for (int32_t j = i + 1; j < num_entries; j++) { |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6682 | ArtMethod* other_entry = vtable->GetElementPtrSize<ArtMethod*>(j, pointer_size); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 6683 | if (!klass->CanAccessMember(other_entry->GetDeclaringClass(), |
| 6684 | other_entry->GetAccessFlags())) { |
| 6685 | continue; |
| 6686 | } |
Alex Light | 3f98053 | 2017-03-17 15:10:32 -0700 | [diff] [blame] | 6687 | if (vtable_entry == other_entry || |
| 6688 | name_comparator.HasSameNameAndSignature( |
| 6689 | other_entry->GetInterfaceMethodIfProxy(pointer_size))) { |
| 6690 | LOG(WARNING) << "vtable entries " << i << " and " << j << " are identical for " |
| 6691 | << klass->PrettyClass() << " in method " << vtable_entry->PrettyMethod() |
| 6692 | << " (0x" << std::hex << reinterpret_cast<uintptr_t>(vtable_entry) << ") and " |
| 6693 | << other_entry->PrettyMethod() << " (0x" << std::hex |
| 6694 | << reinterpret_cast<uintptr_t>(other_entry) << ")"; |
| 6695 | } |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6696 | } |
| 6697 | } |
| 6698 | } |
| 6699 | |
| 6700 | static void SanityCheckVTable(Thread* self, Handle<mirror::Class> klass, PointerSize pointer_size) |
| 6701 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6702 | CheckClassOwnsVTableEntries(self, klass, pointer_size); |
| 6703 | CheckVTableHasNoDuplicates(self, klass, pointer_size); |
| 6704 | } |
| 6705 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6706 | void ClassLinker::FillImtFromSuperClass(Handle<mirror::Class> klass, |
| 6707 | ArtMethod* unimplemented_method, |
| 6708 | ArtMethod* imt_conflict_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6709 | bool* new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6710 | ArtMethod** imt) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6711 | DCHECK(klass->HasSuperClass()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6712 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6713 | if (super_class->ShouldHaveImt()) { |
| 6714 | ImTable* super_imt = super_class->GetImt(image_pointer_size_); |
| 6715 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 6716 | imt[i] = super_imt->Get(i, image_pointer_size_); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6717 | } |
| 6718 | } else { |
| 6719 | // No imt in the super class, need to reconstruct from the iftable. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6720 | ObjPtr<mirror::IfTable> if_table = super_class->GetIfTable(); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 6721 | if (if_table->Count() != 0) { |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6722 | // Ignore copied methods since we will handle these in LinkInterfaceMethods. |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6723 | FillIMTFromIfTable(if_table, |
| 6724 | unimplemented_method, |
| 6725 | imt_conflict_method, |
| 6726 | klass.Get(), |
| 6727 | /*create_conflict_table*/false, |
| 6728 | /*ignore_copied_methods*/true, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6729 | /*out*/new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6730 | /*out*/imt); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6731 | } |
| 6732 | } |
| 6733 | } |
| 6734 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6735 | class ClassLinker::LinkInterfaceMethodsHelper { |
| 6736 | public: |
| 6737 | LinkInterfaceMethodsHelper(ClassLinker* class_linker, |
| 6738 | Handle<mirror::Class> klass, |
| 6739 | Thread* self, |
| 6740 | Runtime* runtime) |
| 6741 | : class_linker_(class_linker), |
| 6742 | klass_(klass), |
| 6743 | method_alignment_(ArtMethod::Alignment(class_linker->GetImagePointerSize())), |
| 6744 | method_size_(ArtMethod::Size(class_linker->GetImagePointerSize())), |
| 6745 | self_(self), |
| 6746 | stack_(runtime->GetLinearAlloc()->GetArenaPool()), |
| 6747 | allocator_(&stack_), |
| 6748 | default_conflict_methods_(allocator_.Adapter()), |
| 6749 | overriding_default_conflict_methods_(allocator_.Adapter()), |
| 6750 | miranda_methods_(allocator_.Adapter()), |
| 6751 | default_methods_(allocator_.Adapter()), |
| 6752 | overriding_default_methods_(allocator_.Adapter()), |
| 6753 | move_table_(allocator_.Adapter()) { |
| 6754 | } |
| 6755 | |
| 6756 | ArtMethod* FindMethod(ArtMethod* interface_method, |
| 6757 | MethodNameAndSignatureComparator& interface_name_comparator, |
| 6758 | ArtMethod* vtable_impl) |
| 6759 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 6760 | |
| 6761 | ArtMethod* GetOrCreateMirandaMethod(ArtMethod* interface_method, |
| 6762 | MethodNameAndSignatureComparator& interface_name_comparator) |
| 6763 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 6764 | |
| 6765 | bool HasNewVirtuals() const { |
| 6766 | return !(miranda_methods_.empty() && |
| 6767 | default_methods_.empty() && |
| 6768 | overriding_default_methods_.empty() && |
| 6769 | overriding_default_conflict_methods_.empty() && |
| 6770 | default_conflict_methods_.empty()); |
| 6771 | } |
| 6772 | |
| 6773 | void ReallocMethods() REQUIRES_SHARED(Locks::mutator_lock_); |
| 6774 | |
| 6775 | ObjPtr<mirror::PointerArray> UpdateVtable( |
| 6776 | const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations, |
| 6777 | ObjPtr<mirror::PointerArray> old_vtable) REQUIRES_SHARED(Locks::mutator_lock_); |
| 6778 | |
| 6779 | void UpdateIfTable(Handle<mirror::IfTable> iftable) REQUIRES_SHARED(Locks::mutator_lock_); |
| 6780 | |
| 6781 | void UpdateIMT(ArtMethod** out_imt); |
| 6782 | |
| 6783 | void CheckNoStaleMethodsInDexCache() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6784 | if (kIsDebugBuild) { |
| 6785 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 6786 | // Check that there are no stale methods are in the dex cache array. |
| 6787 | auto* resolved_methods = klass_->GetDexCache()->GetResolvedMethods(); |
| 6788 | for (size_t i = 0, count = klass_->GetDexCache()->NumResolvedMethods(); i < count; ++i) { |
| 6789 | auto* m = mirror::DexCache::GetElementPtrSize(resolved_methods, i, pointer_size); |
| 6790 | CHECK(move_table_.find(m) == move_table_.end() || |
| 6791 | // The original versions of copied methods will still be present so allow those too. |
| 6792 | // Note that if the first check passes this might fail to GetDeclaringClass(). |
| 6793 | std::find_if(m->GetDeclaringClass()->GetMethods(pointer_size).begin(), |
| 6794 | m->GetDeclaringClass()->GetMethods(pointer_size).end(), |
| 6795 | [m] (ArtMethod& meth) { |
| 6796 | return &meth == m; |
| 6797 | }) != m->GetDeclaringClass()->GetMethods(pointer_size).end()) |
| 6798 | << "Obsolete method " << m->PrettyMethod() << " is in dex cache!"; |
| 6799 | } |
| 6800 | } |
| 6801 | } |
| 6802 | |
| 6803 | void ClobberOldMethods(LengthPrefixedArray<ArtMethod>* old_methods, |
| 6804 | LengthPrefixedArray<ArtMethod>* methods) { |
| 6805 | if (kIsDebugBuild) { |
| 6806 | CHECK(methods != nullptr); |
| 6807 | // Put some random garbage in old methods to help find stale pointers. |
| 6808 | if (methods != old_methods && old_methods != nullptr) { |
| 6809 | // Need to make sure the GC is not running since it could be scanning the methods we are |
| 6810 | // about to overwrite. |
| 6811 | ScopedThreadStateChange tsc(self_, kSuspended); |
| 6812 | gc::ScopedGCCriticalSection gcs(self_, |
| 6813 | gc::kGcCauseClassLinker, |
| 6814 | gc::kCollectorTypeClassLinker); |
| 6815 | const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_methods->size(), |
| 6816 | method_size_, |
| 6817 | method_alignment_); |
| 6818 | memset(old_methods, 0xFEu, old_size); |
| 6819 | } |
| 6820 | } |
| 6821 | } |
| 6822 | |
| 6823 | private: |
| 6824 | size_t NumberOfNewVirtuals() const { |
| 6825 | return miranda_methods_.size() + |
| 6826 | default_methods_.size() + |
| 6827 | overriding_default_conflict_methods_.size() + |
| 6828 | overriding_default_methods_.size() + |
| 6829 | default_conflict_methods_.size(); |
| 6830 | } |
| 6831 | |
| 6832 | bool FillTables() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6833 | return !klass_->IsInterface(); |
| 6834 | } |
| 6835 | |
| 6836 | void LogNewVirtuals() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6837 | DCHECK(!klass_->IsInterface() || (default_methods_.empty() && miranda_methods_.empty())) |
| 6838 | << "Interfaces should only have default-conflict methods appended to them."; |
| 6839 | VLOG(class_linker) << mirror::Class::PrettyClass(klass_.Get()) << ": miranda_methods=" |
| 6840 | << miranda_methods_.size() |
| 6841 | << " default_methods=" << default_methods_.size() |
| 6842 | << " overriding_default_methods=" << overriding_default_methods_.size() |
| 6843 | << " default_conflict_methods=" << default_conflict_methods_.size() |
| 6844 | << " overriding_default_conflict_methods=" |
| 6845 | << overriding_default_conflict_methods_.size(); |
| 6846 | } |
| 6847 | |
| 6848 | ClassLinker* class_linker_; |
| 6849 | Handle<mirror::Class> klass_; |
| 6850 | size_t method_alignment_; |
| 6851 | size_t method_size_; |
| 6852 | Thread* const self_; |
| 6853 | |
| 6854 | // These are allocated on the heap to begin, we then transfer to linear alloc when we re-create |
| 6855 | // the virtual methods array. |
| 6856 | // Need to use low 4GB arenas for compiler or else the pointers wont fit in 32 bit method array |
| 6857 | // during cross compilation. |
| 6858 | // Use the linear alloc pool since this one is in the low 4gb for the compiler. |
| 6859 | ArenaStack stack_; |
| 6860 | ScopedArenaAllocator allocator_; |
| 6861 | |
| 6862 | ScopedArenaVector<ArtMethod*> default_conflict_methods_; |
| 6863 | ScopedArenaVector<ArtMethod*> overriding_default_conflict_methods_; |
| 6864 | ScopedArenaVector<ArtMethod*> miranda_methods_; |
| 6865 | ScopedArenaVector<ArtMethod*> default_methods_; |
| 6866 | ScopedArenaVector<ArtMethod*> overriding_default_methods_; |
| 6867 | |
| 6868 | ScopedArenaUnorderedMap<ArtMethod*, ArtMethod*> move_table_; |
| 6869 | }; |
| 6870 | |
| 6871 | ArtMethod* ClassLinker::LinkInterfaceMethodsHelper::FindMethod( |
| 6872 | ArtMethod* interface_method, |
| 6873 | MethodNameAndSignatureComparator& interface_name_comparator, |
| 6874 | ArtMethod* vtable_impl) { |
| 6875 | ArtMethod* current_method = nullptr; |
| 6876 | switch (class_linker_->FindDefaultMethodImplementation(self_, |
| 6877 | interface_method, |
| 6878 | klass_, |
| 6879 | /*out*/¤t_method)) { |
| 6880 | case DefaultMethodSearchResult::kDefaultConflict: { |
| 6881 | // Default method conflict. |
| 6882 | DCHECK(current_method == nullptr); |
| 6883 | ArtMethod* default_conflict_method = nullptr; |
| 6884 | if (vtable_impl != nullptr && vtable_impl->IsDefaultConflicting()) { |
| 6885 | // We can reuse the method from the superclass, don't bother adding it to virtuals. |
| 6886 | default_conflict_method = vtable_impl; |
| 6887 | } else { |
| 6888 | // See if we already have a conflict method for this method. |
| 6889 | ArtMethod* preexisting_conflict = FindSameNameAndSignature( |
| 6890 | interface_name_comparator, |
| 6891 | default_conflict_methods_, |
| 6892 | overriding_default_conflict_methods_); |
| 6893 | if (LIKELY(preexisting_conflict != nullptr)) { |
| 6894 | // We already have another conflict we can reuse. |
| 6895 | default_conflict_method = preexisting_conflict; |
| 6896 | } else { |
| 6897 | // Note that we do this even if we are an interface since we need to create this and |
| 6898 | // cannot reuse another classes. |
| 6899 | // Create a new conflict method for this to use. |
| 6900 | default_conflict_method = reinterpret_cast<ArtMethod*>(allocator_.Alloc(method_size_)); |
| 6901 | new(default_conflict_method) ArtMethod(interface_method, |
| 6902 | class_linker_->GetImagePointerSize()); |
| 6903 | if (vtable_impl == nullptr) { |
| 6904 | // Save the conflict method. We need to add it to the vtable. |
| 6905 | default_conflict_methods_.push_back(default_conflict_method); |
| 6906 | } else { |
| 6907 | // Save the conflict method but it is already in the vtable. |
| 6908 | overriding_default_conflict_methods_.push_back(default_conflict_method); |
| 6909 | } |
| 6910 | } |
| 6911 | } |
| 6912 | current_method = default_conflict_method; |
| 6913 | break; |
| 6914 | } // case kDefaultConflict |
| 6915 | case DefaultMethodSearchResult::kDefaultFound: { |
| 6916 | DCHECK(current_method != nullptr); |
| 6917 | // Found a default method. |
| 6918 | if (vtable_impl != nullptr && |
| 6919 | current_method->GetDeclaringClass() == vtable_impl->GetDeclaringClass()) { |
| 6920 | // We found a default method but it was the same one we already have from our |
| 6921 | // superclass. Don't bother adding it to our vtable again. |
| 6922 | current_method = vtable_impl; |
| 6923 | } else if (LIKELY(FillTables())) { |
| 6924 | // Interfaces don't need to copy default methods since they don't have vtables. |
| 6925 | // Only record this default method if it is new to save space. |
| 6926 | // TODO It might be worthwhile to copy default methods on interfaces anyway since it |
| 6927 | // would make lookup for interface super much faster. (We would only need to scan |
| 6928 | // the iftable to find if there is a NSME or AME.) |
| 6929 | ArtMethod* old = FindSameNameAndSignature(interface_name_comparator, |
| 6930 | default_methods_, |
| 6931 | overriding_default_methods_); |
| 6932 | if (old == nullptr) { |
| 6933 | // We found a default method implementation and there were no conflicts. |
| 6934 | if (vtable_impl == nullptr) { |
| 6935 | // Save the default method. We need to add it to the vtable. |
| 6936 | default_methods_.push_back(current_method); |
| 6937 | } else { |
| 6938 | // Save the default method but it is already in the vtable. |
| 6939 | overriding_default_methods_.push_back(current_method); |
| 6940 | } |
| 6941 | } else { |
| 6942 | CHECK(old == current_method) << "Multiple default implementations selected!"; |
| 6943 | } |
| 6944 | } |
| 6945 | break; |
| 6946 | } // case kDefaultFound |
| 6947 | case DefaultMethodSearchResult::kAbstractFound: { |
| 6948 | DCHECK(current_method == nullptr); |
| 6949 | // Abstract method masks all defaults. |
| 6950 | if (vtable_impl != nullptr && |
| 6951 | vtable_impl->IsAbstract() && |
| 6952 | !vtable_impl->IsDefaultConflicting()) { |
| 6953 | // We need to make this an abstract method but the version in the vtable already is so |
| 6954 | // don't do anything. |
| 6955 | current_method = vtable_impl; |
| 6956 | } |
| 6957 | break; |
| 6958 | } // case kAbstractFound |
| 6959 | } |
| 6960 | return current_method; |
| 6961 | } |
| 6962 | |
| 6963 | ArtMethod* ClassLinker::LinkInterfaceMethodsHelper::GetOrCreateMirandaMethod( |
| 6964 | ArtMethod* interface_method, |
| 6965 | MethodNameAndSignatureComparator& interface_name_comparator) { |
| 6966 | // Find out if there is already a miranda method we can use. |
| 6967 | ArtMethod* miranda_method = FindSameNameAndSignature(interface_name_comparator, |
| 6968 | miranda_methods_); |
| 6969 | if (miranda_method == nullptr) { |
| 6970 | DCHECK(interface_method->IsAbstract()) << interface_method->PrettyMethod(); |
| 6971 | miranda_method = reinterpret_cast<ArtMethod*>(allocator_.Alloc(method_size_)); |
| 6972 | CHECK(miranda_method != nullptr); |
| 6973 | // Point the interface table at a phantom slot. |
| 6974 | new(miranda_method) ArtMethod(interface_method, class_linker_->GetImagePointerSize()); |
| 6975 | miranda_methods_.push_back(miranda_method); |
| 6976 | } |
| 6977 | return miranda_method; |
| 6978 | } |
| 6979 | |
| 6980 | void ClassLinker::LinkInterfaceMethodsHelper::ReallocMethods() { |
| 6981 | LogNewVirtuals(); |
| 6982 | |
| 6983 | const size_t old_method_count = klass_->NumMethods(); |
| 6984 | const size_t new_method_count = old_method_count + NumberOfNewVirtuals(); |
| 6985 | DCHECK_NE(old_method_count, new_method_count); |
| 6986 | |
| 6987 | // Attempt to realloc to save RAM if possible. |
| 6988 | LengthPrefixedArray<ArtMethod>* old_methods = klass_->GetMethodsPtr(); |
| 6989 | // The Realloced virtual methods aren't visible from the class roots, so there is no issue |
| 6990 | // where GCs could attempt to mark stale pointers due to memcpy. And since we overwrite the |
| 6991 | // realloced memory with out->CopyFrom, we are guaranteed to have objects in the to space since |
| 6992 | // CopyFrom has internal read barriers. |
| 6993 | // |
| 6994 | // TODO We should maybe move some of this into mirror::Class or at least into another method. |
| 6995 | const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_method_count, |
| 6996 | method_size_, |
| 6997 | method_alignment_); |
| 6998 | const size_t new_size = LengthPrefixedArray<ArtMethod>::ComputeSize(new_method_count, |
| 6999 | method_size_, |
| 7000 | method_alignment_); |
| 7001 | const size_t old_methods_ptr_size = (old_methods != nullptr) ? old_size : 0; |
| 7002 | auto* methods = reinterpret_cast<LengthPrefixedArray<ArtMethod>*>( |
| 7003 | Runtime::Current()->GetLinearAlloc()->Realloc( |
| 7004 | self_, old_methods, old_methods_ptr_size, new_size)); |
| 7005 | CHECK(methods != nullptr); // Native allocation failure aborts. |
| 7006 | |
| 7007 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 7008 | if (methods != old_methods) { |
| 7009 | // Maps from heap allocated miranda method to linear alloc miranda method. |
| 7010 | StrideIterator<ArtMethod> out = methods->begin(method_size_, method_alignment_); |
| 7011 | // Copy over the old methods. |
| 7012 | for (auto& m : klass_->GetMethods(pointer_size)) { |
| 7013 | move_table_.emplace(&m, &*out); |
| 7014 | // The CopyFrom is only necessary to not miss read barriers since Realloc won't do read |
| 7015 | // barriers when it copies. |
| 7016 | out->CopyFrom(&m, pointer_size); |
| 7017 | ++out; |
| 7018 | } |
| 7019 | } |
| 7020 | StrideIterator<ArtMethod> out(methods->begin(method_size_, method_alignment_) + old_method_count); |
| 7021 | // Copy over miranda methods before copying vtable since CopyOf may cause thread suspension and |
| 7022 | // we want the roots of the miranda methods to get visited. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7023 | for (size_t i = 0; i < miranda_methods_.size(); ++i) { |
| 7024 | ArtMethod* mir_method = miranda_methods_[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7025 | ArtMethod& new_method = *out; |
| 7026 | new_method.CopyFrom(mir_method, pointer_size); |
| 7027 | new_method.SetAccessFlags(new_method.GetAccessFlags() | kAccMiranda | kAccCopied); |
| 7028 | DCHECK_NE(new_method.GetAccessFlags() & kAccAbstract, 0u) |
| 7029 | << "Miranda method should be abstract!"; |
| 7030 | move_table_.emplace(mir_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7031 | // Update the entry in the method array, as the array will be used for future lookups, |
| 7032 | // where thread suspension is allowed. |
| 7033 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 7034 | // would not see them. |
| 7035 | miranda_methods_[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7036 | ++out; |
| 7037 | } |
| 7038 | // We need to copy the default methods into our own method table since the runtime requires that |
| 7039 | // every method on a class's vtable be in that respective class's virtual method table. |
| 7040 | // NOTE This means that two classes might have the same implementation of a method from the same |
| 7041 | // interface but will have different ArtMethod*s for them. This also means we cannot compare a |
| 7042 | // default method found on a class with one found on the declaring interface directly and must |
| 7043 | // look at the declaring class to determine if they are the same. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7044 | for (ScopedArenaVector<ArtMethod*>* methods_vec : {&default_methods_, |
| 7045 | &overriding_default_methods_}) { |
| 7046 | for (size_t i = 0; i < methods_vec->size(); ++i) { |
| 7047 | ArtMethod* def_method = (*methods_vec)[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7048 | ArtMethod& new_method = *out; |
| 7049 | new_method.CopyFrom(def_method, pointer_size); |
| 7050 | // Clear the kAccSkipAccessChecks flag if it is present. Since this class hasn't been |
| 7051 | // verified yet it shouldn't have methods that are skipping access checks. |
| 7052 | // TODO This is rather arbitrary. We should maybe support classes where only some of its |
| 7053 | // methods are skip_access_checks. |
| 7054 | constexpr uint32_t kSetFlags = kAccDefault | kAccCopied; |
| 7055 | constexpr uint32_t kMaskFlags = ~kAccSkipAccessChecks; |
| 7056 | new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags); |
| 7057 | move_table_.emplace(def_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7058 | // Update the entry in the method array, as the array will be used for future lookups, |
| 7059 | // where thread suspension is allowed. |
| 7060 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 7061 | // would not see them. |
| 7062 | (*methods_vec)[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7063 | ++out; |
| 7064 | } |
| 7065 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7066 | for (ScopedArenaVector<ArtMethod*>* methods_vec : {&default_conflict_methods_, |
| 7067 | &overriding_default_conflict_methods_}) { |
| 7068 | for (size_t i = 0; i < methods_vec->size(); ++i) { |
| 7069 | ArtMethod* conf_method = (*methods_vec)[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7070 | ArtMethod& new_method = *out; |
| 7071 | new_method.CopyFrom(conf_method, pointer_size); |
| 7072 | // This is a type of default method (there are default method impls, just a conflict) so |
| 7073 | // mark this as a default, non-abstract method, since thats what it is. Also clear the |
| 7074 | // kAccSkipAccessChecks bit since this class hasn't been verified yet it shouldn't have |
| 7075 | // methods that are skipping access checks. |
| 7076 | constexpr uint32_t kSetFlags = kAccDefault | kAccDefaultConflict | kAccCopied; |
| 7077 | constexpr uint32_t kMaskFlags = ~(kAccAbstract | kAccSkipAccessChecks); |
| 7078 | new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags); |
| 7079 | DCHECK(new_method.IsDefaultConflicting()); |
| 7080 | // The actual method might or might not be marked abstract since we just copied it from a |
| 7081 | // (possibly default) interface method. We need to set it entry point to be the bridge so |
| 7082 | // that the compiler will not invoke the implementation of whatever method we copied from. |
| 7083 | EnsureThrowsInvocationError(class_linker_, &new_method); |
| 7084 | move_table_.emplace(conf_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7085 | // Update the entry in the method array, as the array will be used for future lookups, |
| 7086 | // where thread suspension is allowed. |
| 7087 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 7088 | // would not see them. |
| 7089 | (*methods_vec)[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7090 | ++out; |
| 7091 | } |
| 7092 | } |
| 7093 | methods->SetSize(new_method_count); |
| 7094 | class_linker_->UpdateClassMethods(klass_.Get(), methods); |
| 7095 | } |
| 7096 | |
| 7097 | ObjPtr<mirror::PointerArray> ClassLinker::LinkInterfaceMethodsHelper::UpdateVtable( |
| 7098 | const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations, |
| 7099 | ObjPtr<mirror::PointerArray> old_vtable) { |
| 7100 | // Update the vtable to the new method structures. We can skip this for interfaces since they |
| 7101 | // do not have vtables. |
| 7102 | const size_t old_vtable_count = old_vtable->GetLength(); |
| 7103 | const size_t new_vtable_count = old_vtable_count + |
| 7104 | miranda_methods_.size() + |
| 7105 | default_methods_.size() + |
| 7106 | default_conflict_methods_.size(); |
| 7107 | |
| 7108 | ObjPtr<mirror::PointerArray> vtable = |
| 7109 | down_cast<mirror::PointerArray*>(old_vtable->CopyOf(self_, new_vtable_count)); |
| 7110 | if (UNLIKELY(vtable == nullptr)) { |
| 7111 | self_->AssertPendingOOMException(); |
| 7112 | return nullptr; |
| 7113 | } |
| 7114 | |
| 7115 | size_t vtable_pos = old_vtable_count; |
| 7116 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 7117 | // Update all the newly copied method's indexes so they denote their placement in the vtable. |
| 7118 | for (const ScopedArenaVector<ArtMethod*>& methods_vec : {default_methods_, |
| 7119 | default_conflict_methods_, |
| 7120 | miranda_methods_}) { |
| 7121 | // These are the functions that are not already in the vtable! |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7122 | for (ArtMethod* new_vtable_method : methods_vec) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7123 | // Leave the declaring class alone the method's dex_code_item_offset_ and dex_method_index_ |
| 7124 | // fields are references into the dex file the method was defined in. Since the ArtMethod |
| 7125 | // does not store that information it uses declaring_class_->dex_cache_. |
| 7126 | new_vtable_method->SetMethodIndex(0xFFFF & vtable_pos); |
| 7127 | vtable->SetElementPtrSize(vtable_pos, new_vtable_method, pointer_size); |
| 7128 | ++vtable_pos; |
| 7129 | } |
| 7130 | } |
| 7131 | DCHECK_EQ(vtable_pos, new_vtable_count); |
| 7132 | |
| 7133 | // Update old vtable methods. We use the default_translations map to figure out what each |
| 7134 | // vtable entry should be updated to, if they need to be at all. |
| 7135 | for (size_t i = 0; i < old_vtable_count; ++i) { |
| 7136 | ArtMethod* translated_method = vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 7137 | // Try and find what we need to change this method to. |
| 7138 | auto translation_it = default_translations.find(i); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7139 | if (translation_it != default_translations.end()) { |
| 7140 | if (translation_it->second.IsInConflict()) { |
| 7141 | // Find which conflict method we are to use for this method. |
| 7142 | MethodNameAndSignatureComparator old_method_comparator( |
| 7143 | translated_method->GetInterfaceMethodIfProxy(pointer_size)); |
| 7144 | // We only need to look through overriding_default_conflict_methods since this is an |
| 7145 | // overridden method we are fixing up here. |
| 7146 | ArtMethod* new_conflict_method = FindSameNameAndSignature( |
| 7147 | old_method_comparator, overriding_default_conflict_methods_); |
| 7148 | CHECK(new_conflict_method != nullptr) << "Expected a conflict method!"; |
| 7149 | translated_method = new_conflict_method; |
| 7150 | } else if (translation_it->second.IsAbstract()) { |
| 7151 | // Find which miranda method we are to use for this method. |
| 7152 | MethodNameAndSignatureComparator old_method_comparator( |
| 7153 | translated_method->GetInterfaceMethodIfProxy(pointer_size)); |
| 7154 | ArtMethod* miranda_method = FindSameNameAndSignature(old_method_comparator, |
| 7155 | miranda_methods_); |
| 7156 | DCHECK(miranda_method != nullptr); |
| 7157 | translated_method = miranda_method; |
| 7158 | } else { |
| 7159 | // Normal default method (changed from an older default or abstract interface method). |
| 7160 | DCHECK(translation_it->second.IsTranslation()); |
| 7161 | translated_method = translation_it->second.GetTranslation(); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7162 | auto it = move_table_.find(translated_method); |
| 7163 | DCHECK(it != move_table_.end()); |
| 7164 | translated_method = it->second; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7165 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7166 | } else { |
| 7167 | auto it = move_table_.find(translated_method); |
| 7168 | translated_method = (it != move_table_.end()) ? it->second : nullptr; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7169 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7170 | |
| 7171 | if (translated_method != nullptr) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7172 | // Make sure the new_methods index is set. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7173 | if (translated_method->GetMethodIndexDuringLinking() != i) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7174 | if (kIsDebugBuild) { |
| 7175 | auto* methods = klass_->GetMethodsPtr(); |
| 7176 | CHECK_LE(reinterpret_cast<uintptr_t>(&*methods->begin(method_size_, method_alignment_)), |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7177 | reinterpret_cast<uintptr_t>(translated_method)); |
| 7178 | CHECK_LT(reinterpret_cast<uintptr_t>(translated_method), |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7179 | reinterpret_cast<uintptr_t>(&*methods->end(method_size_, method_alignment_))); |
| 7180 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7181 | translated_method->SetMethodIndex(0xFFFF & i); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7182 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7183 | vtable->SetElementPtrSize(i, translated_method, pointer_size); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7184 | } |
| 7185 | } |
| 7186 | klass_->SetVTable(vtable.Ptr()); |
| 7187 | return vtable; |
| 7188 | } |
| 7189 | |
| 7190 | void ClassLinker::LinkInterfaceMethodsHelper::UpdateIfTable(Handle<mirror::IfTable> iftable) { |
| 7191 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 7192 | const size_t ifcount = klass_->GetIfTableCount(); |
| 7193 | // Go fix up all the stale iftable pointers. |
| 7194 | for (size_t i = 0; i < ifcount; ++i) { |
| 7195 | for (size_t j = 0, count = iftable->GetMethodArrayCount(i); j < count; ++j) { |
| 7196 | auto* method_array = iftable->GetMethodArray(i); |
| 7197 | auto* m = method_array->GetElementPtrSize<ArtMethod*>(j, pointer_size); |
| 7198 | DCHECK(m != nullptr) << klass_->PrettyClass(); |
| 7199 | auto it = move_table_.find(m); |
| 7200 | if (it != move_table_.end()) { |
| 7201 | auto* new_m = it->second; |
| 7202 | DCHECK(new_m != nullptr) << klass_->PrettyClass(); |
| 7203 | method_array->SetElementPtrSize(j, new_m, pointer_size); |
| 7204 | } |
| 7205 | } |
| 7206 | } |
| 7207 | } |
| 7208 | |
| 7209 | void ClassLinker::LinkInterfaceMethodsHelper::UpdateIMT(ArtMethod** out_imt) { |
| 7210 | // Fix up IMT next. |
| 7211 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 7212 | auto it = move_table_.find(out_imt[i]); |
| 7213 | if (it != move_table_.end()) { |
| 7214 | out_imt[i] = it->second; |
| 7215 | } |
| 7216 | } |
| 7217 | } |
| 7218 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7219 | // TODO This method needs to be split up into several smaller methods. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7220 | bool ClassLinker::LinkInterfaceMethods( |
| 7221 | Thread* self, |
| 7222 | Handle<mirror::Class> klass, |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7223 | const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7224 | bool* out_new_conflict, |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7225 | ArtMethod** out_imt) { |
| 7226 | StackHandleScope<3> hs(self); |
| 7227 | Runtime* const runtime = Runtime::Current(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7228 | |
| 7229 | const bool is_interface = klass->IsInterface(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7230 | const bool has_superclass = klass->HasSuperClass(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7231 | const bool fill_tables = !is_interface; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7232 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7233 | const size_t ifcount = klass->GetIfTableCount(); |
| 7234 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7235 | Handle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7236 | |
| 7237 | MutableHandle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
| 7238 | ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod(); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7239 | ArtMethod* const imt_conflict_method = runtime->GetImtConflictMethod(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7240 | // Copy the IMT from the super class if possible. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7241 | const bool extend_super_iftable = has_superclass; |
| 7242 | if (has_superclass && fill_tables) { |
| 7243 | FillImtFromSuperClass(klass, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7244 | unimplemented_method, |
| 7245 | imt_conflict_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7246 | out_new_conflict, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7247 | out_imt); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7248 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7249 | // Allocate method arrays before since we don't want miss visiting miranda method roots due to |
| 7250 | // thread suspension. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7251 | if (fill_tables) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7252 | if (!AllocateIfTableMethodArrays(self, klass, iftable)) { |
| 7253 | return false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7254 | } |
| 7255 | } |
| 7256 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7257 | LinkInterfaceMethodsHelper helper(this, klass, self, runtime); |
| 7258 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7259 | auto* old_cause = self->StartAssertNoThreadSuspension( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7260 | "Copying ArtMethods for LinkInterfaceMethods"); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7261 | // Going in reverse to ensure that we will hit abstract methods that override defaults before the |
| 7262 | // defaults. This means we don't need to do any trickery when creating the Miranda methods, since |
| 7263 | // they will already be null. This has the additional benefit that the declarer of a miranda |
| 7264 | // method will actually declare an abstract method. |
| 7265 | for (size_t i = ifcount; i != 0; ) { |
| 7266 | --i; |
| 7267 | |
| 7268 | DCHECK_GE(i, 0u); |
| 7269 | DCHECK_LT(i, ifcount); |
| 7270 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7271 | size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7272 | if (num_methods > 0) { |
| 7273 | StackHandleScope<2> hs2(self); |
| 7274 | const bool is_super = i < super_ifcount; |
| 7275 | const bool super_interface = is_super && extend_super_iftable; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7276 | // We don't actually create or fill these tables for interfaces, we just copy some methods for |
| 7277 | // conflict methods. Just set this as nullptr in those cases. |
| 7278 | Handle<mirror::PointerArray> method_array(fill_tables |
| 7279 | ? hs2.NewHandle(iftable->GetMethodArray(i)) |
| 7280 | : hs2.NewHandle<mirror::PointerArray>(nullptr)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7281 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7282 | ArraySlice<ArtMethod> input_virtual_methods; |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 7283 | ScopedNullHandle<mirror::PointerArray> null_handle; |
| 7284 | Handle<mirror::PointerArray> input_vtable_array(null_handle); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7285 | int32_t input_array_length = 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7286 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7287 | // TODO Cleanup Needed: In the presence of default methods this optimization is rather dirty |
| 7288 | // and confusing. Default methods should always look through all the superclasses |
| 7289 | // because they are the last choice of an implementation. We get around this by looking |
| 7290 | // at the super-classes iftable methods (copied into method_array previously) when we are |
| 7291 | // 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] | 7292 | bool using_virtuals; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7293 | if (super_interface || is_interface) { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7294 | // 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] | 7295 | // whole vtable. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7296 | using_virtuals = true; |
| 7297 | input_virtual_methods = klass->GetDeclaredMethodsSlice(image_pointer_size_); |
| 7298 | input_array_length = input_virtual_methods.size(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7299 | } else { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7300 | // For a new interface, however, we need the whole vtable in case a new |
| 7301 | // interface method is implemented in the whole superclass. |
| 7302 | using_virtuals = false; |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7303 | DCHECK(vtable != nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7304 | input_vtable_array = vtable; |
| 7305 | input_array_length = input_vtable_array->GetLength(); |
| 7306 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7307 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7308 | // For each method in interface |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 7309 | for (size_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7310 | auto* interface_method = iftable->GetInterface(i)->GetVirtualMethod(j, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 7311 | MethodNameAndSignatureComparator interface_name_comparator( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7312 | interface_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 7313 | uint32_t imt_index = ImTable::GetImtIndex(interface_method); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7314 | ArtMethod** imt_ptr = &out_imt[imt_index]; |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7315 | // For each method listed in the interface's method list, find the |
| 7316 | // matching method in our class's method list. We want to favor the |
| 7317 | // subclass over the superclass, which just requires walking |
| 7318 | // back from the end of the vtable. (This only matters if the |
| 7319 | // superclass defines a private method and this class redefines |
| 7320 | // it -- otherwise it would use the same vtable slot. In .dex files |
| 7321 | // those don't end up in the virtual method table, so it shouldn't |
| 7322 | // matter which direction we go. We walk it backward anyway.) |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7323 | // |
| 7324 | // To find defaults we need to do the same but also go over interfaces. |
| 7325 | bool found_impl = false; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7326 | ArtMethod* vtable_impl = nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7327 | for (int32_t k = input_array_length - 1; k >= 0; --k) { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7328 | ArtMethod* vtable_method = using_virtuals ? |
| 7329 | &input_virtual_methods[k] : |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7330 | input_vtable_array->GetElementPtrSize<ArtMethod*>(k, image_pointer_size_); |
| 7331 | ArtMethod* vtable_method_for_name_comparison = |
| 7332 | vtable_method->GetInterfaceMethodIfProxy(image_pointer_size_); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 7333 | if (interface_name_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 7334 | vtable_method_for_name_comparison)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7335 | if (!vtable_method->IsAbstract() && !vtable_method->IsPublic()) { |
Mathieu Chartier | 4d122c1 | 2015-06-17 14:14:36 -0700 | [diff] [blame] | 7336 | // Must do EndAssertNoThreadSuspension before throw since the throw can cause |
| 7337 | // allocations. |
| 7338 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7339 | ThrowIllegalAccessError(klass.Get(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 7340 | "Method '%s' implementing interface method '%s' is not public", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7341 | vtable_method->PrettyMethod().c_str(), |
| 7342 | interface_method->PrettyMethod().c_str()); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7343 | return false; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7344 | } else if (UNLIKELY(vtable_method->IsOverridableByDefaultMethod())) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7345 | // We might have a newer, better, default method for this, so we just skip it. If we |
| 7346 | // are still using this we will select it again when scanning for default methods. To |
| 7347 | // obviate the need to copy the method again we will make a note that we already found |
| 7348 | // a default here. |
| 7349 | // TODO This should be much cleaner. |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7350 | vtable_impl = vtable_method; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7351 | break; |
| 7352 | } else { |
| 7353 | found_impl = true; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7354 | if (LIKELY(fill_tables)) { |
| 7355 | method_array->SetElementPtrSize(j, vtable_method, image_pointer_size_); |
| 7356 | // Place method in imt if entry is empty, place conflict otherwise. |
| 7357 | SetIMTRef(unimplemented_method, |
| 7358 | imt_conflict_method, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7359 | vtable_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7360 | /*out*/out_new_conflict, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7361 | /*out*/imt_ptr); |
| 7362 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7363 | break; |
| 7364 | } |
| 7365 | } |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7366 | } |
| 7367 | // Continue on to the next method if we are done. |
| 7368 | if (LIKELY(found_impl)) { |
| 7369 | continue; |
| 7370 | } else if (LIKELY(super_interface)) { |
| 7371 | // Don't look for a default implementation when the super-method is implemented directly |
| 7372 | // by the class. |
| 7373 | // |
| 7374 | // See if we can use the superclasses method and skip searching everything else. |
| 7375 | // Note: !found_impl && super_interface |
| 7376 | CHECK(extend_super_iftable); |
| 7377 | // If this is a super_interface method it is possible we shouldn't override it because a |
| 7378 | // superclass could have implemented it directly. We get the method the superclass used |
| 7379 | // to implement this to know if we can override it with a default method. Doing this is |
| 7380 | // safe since we know that the super_iftable is filled in so we can simply pull it from |
| 7381 | // there. We don't bother if this is not a super-classes interface since in that case we |
| 7382 | // have scanned the entire vtable anyway and would have found it. |
| 7383 | // TODO This is rather dirty but it is faster than searching through the entire vtable |
| 7384 | // every time. |
| 7385 | ArtMethod* supers_method = |
| 7386 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 7387 | DCHECK(supers_method != nullptr); |
| 7388 | DCHECK(interface_name_comparator.HasSameNameAndSignature(supers_method)); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7389 | if (LIKELY(!supers_method->IsOverridableByDefaultMethod())) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7390 | // The method is not overridable by a default method (i.e. it is directly implemented |
| 7391 | // in some class). Therefore move onto the next interface method. |
| 7392 | continue; |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 7393 | } else { |
| 7394 | // If the super-classes method is override-able by a default method we need to keep |
| 7395 | // track of it since though it is override-able it is not guaranteed to be 'overridden'. |
| 7396 | // 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] | 7397 | // to the vtable twice, causing corruption (vtable entries having inconsistent and |
| 7398 | // illegal states, incorrect vtable size, and incorrect or inconsistent iftable entries) |
| 7399 | // in this class and any subclasses. |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 7400 | DCHECK(vtable_impl == nullptr || vtable_impl == supers_method) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7401 | << "vtable_impl was " << ArtMethod::PrettyMethod(vtable_impl) |
| 7402 | << " and not 'nullptr' or " |
| 7403 | << supers_method->PrettyMethod() |
| 7404 | << " as expected. IFTable appears to be corrupt!"; |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 7405 | vtable_impl = supers_method; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7406 | } |
| 7407 | } |
| 7408 | // 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] | 7409 | ArtMethod* current_method = helper.FindMethod(interface_method, |
| 7410 | interface_name_comparator, |
| 7411 | vtable_impl); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7412 | if (LIKELY(fill_tables)) { |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 7413 | if (current_method == nullptr && !super_interface) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7414 | // We could not find an implementation for this method and since it is a brand new |
| 7415 | // interface we searched the entire vtable (and all default methods) for an |
| 7416 | // 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] | 7417 | current_method = helper.GetOrCreateMirandaMethod(interface_method, |
| 7418 | interface_name_comparator); |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 7419 | } |
| 7420 | |
| 7421 | if (current_method != nullptr) { |
| 7422 | // We found a default method implementation. Record it in the iftable and IMT. |
| 7423 | method_array->SetElementPtrSize(j, current_method, image_pointer_size_); |
| 7424 | SetIMTRef(unimplemented_method, |
| 7425 | imt_conflict_method, |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 7426 | current_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7427 | /*out*/out_new_conflict, |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 7428 | /*out*/imt_ptr); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7429 | } |
| 7430 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7431 | } // For each method in interface end. |
| 7432 | } // if (num_methods > 0) |
| 7433 | } // For each interface. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7434 | // TODO don't extend virtuals of interface unless necessary (when is it?). |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7435 | if (helper.HasNewVirtuals()) { |
| 7436 | LengthPrefixedArray<ArtMethod>* old_methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr; |
| 7437 | helper.ReallocMethods(); // No return value to check. Native allocation failure aborts. |
| 7438 | LengthPrefixedArray<ArtMethod>* methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr; |
| 7439 | |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 7440 | // 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] | 7441 | // suspension assert. |
| 7442 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 7443 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7444 | if (fill_tables) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7445 | vtable.Assign(helper.UpdateVtable(default_translations, vtable.Get())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7446 | if (UNLIKELY(vtable == nullptr)) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7447 | // The helper has already called self->AssertPendingOOMException(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7448 | return false; |
| 7449 | } |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7450 | helper.UpdateIfTable(iftable); |
| 7451 | helper.UpdateIMT(out_imt); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7452 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7453 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7454 | helper.CheckNoStaleMethodsInDexCache(); |
| 7455 | helper.ClobberOldMethods(old_methods, methods); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7456 | } else { |
| 7457 | self->EndAssertNoThreadSuspension(old_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7458 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7459 | if (kIsDebugBuild && !is_interface) { |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7460 | SanityCheckVTable(self, klass, image_pointer_size_); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 7461 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7462 | return true; |
| 7463 | } |
| 7464 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7465 | bool ClassLinker::LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7466 | CHECK(klass != nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7467 | return LinkFields(self, klass, false, nullptr); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 7468 | } |
| 7469 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7470 | 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] | 7471 | CHECK(klass != nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7472 | return LinkFields(self, klass, true, class_size); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 7473 | } |
| 7474 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7475 | struct LinkFieldsComparator { |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 7476 | explicit LinkFieldsComparator() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 7477 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 7478 | // 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] | 7479 | bool operator()(ArtField* field1, ArtField* field2) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 7480 | NO_THREAD_SAFETY_ANALYSIS { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 7481 | // 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] | 7482 | Primitive::Type type1 = field1->GetTypeAsPrimitiveType(); |
| 7483 | Primitive::Type type2 = field2->GetTypeAsPrimitiveType(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 7484 | if (type1 != type2) { |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 7485 | if (type1 == Primitive::kPrimNot) { |
| 7486 | // Reference always goes first. |
| 7487 | return true; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 7488 | } |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 7489 | if (type2 == Primitive::kPrimNot) { |
| 7490 | // Reference always goes first. |
| 7491 | return false; |
| 7492 | } |
| 7493 | size_t size1 = Primitive::ComponentSize(type1); |
| 7494 | size_t size2 = Primitive::ComponentSize(type2); |
| 7495 | if (size1 != size2) { |
| 7496 | // Larger primitive types go first. |
| 7497 | return size1 > size2; |
| 7498 | } |
| 7499 | // Primitive types differ but sizes match. Arbitrarily order by primitive type. |
| 7500 | return type1 < type2; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7501 | } |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 7502 | // Same basic group? Then sort by dex field index. This is guaranteed to be sorted |
| 7503 | // by name and for equal names by type id index. |
| 7504 | // NOTE: This works also for proxies. Their static fields are assigned appropriate indexes. |
| 7505 | return field1->GetDexFieldIndex() < field2->GetDexFieldIndex(); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7506 | } |
| 7507 | }; |
| 7508 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7509 | bool ClassLinker::LinkFields(Thread* self, |
| 7510 | Handle<mirror::Class> klass, |
| 7511 | bool is_static, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7512 | size_t* class_size) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 7513 | self->AllowThreadSuspension(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7514 | const size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7515 | LengthPrefixedArray<ArtField>* const fields = is_static ? klass->GetSFieldsPtr() : |
| 7516 | klass->GetIFieldsPtr(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7517 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 7518 | // Initialize field_offset |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 7519 | MemberOffset field_offset(0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7520 | if (is_static) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7521 | field_offset = klass->GetFirstReferenceStaticFieldOffsetDuringLinking(image_pointer_size_); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7522 | } else { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7523 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7524 | if (super_class != nullptr) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 7525 | CHECK(super_class->IsResolved()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7526 | << klass->PrettyClass() << " " << super_class->PrettyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7527 | field_offset = MemberOffset(super_class->GetObjectSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7528 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7529 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7530 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7531 | CHECK_EQ(num_fields == 0, fields == nullptr) << klass->PrettyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7532 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7533 | // we want a relatively stable order so that adding new fields |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7534 | // minimizes disruption of C++ version such as Class and Method. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7535 | // |
| 7536 | // The overall sort order order is: |
| 7537 | // 1) All object reference fields, sorted alphabetically. |
| 7538 | // 2) All java long (64-bit) integer fields, sorted alphabetically. |
| 7539 | // 3) All java double (64-bit) floating point fields, sorted alphabetically. |
| 7540 | // 4) All java int (32-bit) integer fields, sorted alphabetically. |
| 7541 | // 5) All java float (32-bit) floating point fields, sorted alphabetically. |
| 7542 | // 6) All java char (16-bit) integer fields, sorted alphabetically. |
| 7543 | // 7) All java short (16-bit) integer fields, sorted alphabetically. |
| 7544 | // 8) All java boolean (8-bit) integer fields, sorted alphabetically. |
| 7545 | // 9) All java byte (8-bit) integer fields, sorted alphabetically. |
| 7546 | // |
| 7547 | // Once the fields are sorted in this order we will attempt to fill any gaps that might be present |
| 7548 | // 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] | 7549 | std::deque<ArtField*> grouped_and_sorted_fields; |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 7550 | const char* old_no_suspend_cause = self->StartAssertNoThreadSuspension( |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 7551 | "Naked ArtField references in deque"); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7552 | for (size_t i = 0; i < num_fields; i++) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7553 | grouped_and_sorted_fields.push_back(&fields->At(i)); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7554 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 7555 | std::sort(grouped_and_sorted_fields.begin(), grouped_and_sorted_fields.end(), |
| 7556 | LinkFieldsComparator()); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7557 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7558 | // References should be at the front. |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7559 | size_t current_field = 0; |
| 7560 | size_t num_reference_fields = 0; |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7561 | FieldGaps gaps; |
| 7562 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7563 | for (; current_field < num_fields; current_field++) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7564 | ArtField* field = grouped_and_sorted_fields.front(); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 7565 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 7566 | bool isPrimitive = type != Primitive::kPrimNot; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7567 | if (isPrimitive) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 7568 | break; // past last reference, move on to the next phase |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7569 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7570 | if (UNLIKELY(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>( |
| 7571 | field_offset.Uint32Value()))) { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7572 | MemberOffset old_offset = field_offset; |
| 7573 | field_offset = MemberOffset(RoundUp(field_offset.Uint32Value(), 4)); |
| 7574 | AddFieldGap(old_offset.Uint32Value(), field_offset.Uint32Value(), &gaps); |
| 7575 | } |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 7576 | DCHECK_ALIGNED(field_offset.Uint32Value(), sizeof(mirror::HeapReference<mirror::Object>)); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7577 | grouped_and_sorted_fields.pop_front(); |
| 7578 | num_reference_fields++; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7579 | field->SetOffset(field_offset); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7580 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 7581 | sizeof(mirror::HeapReference<mirror::Object>)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7582 | } |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7583 | // Gaps are stored as a max heap which means that we must shuffle from largest to smallest |
| 7584 | // otherwise we could end up with suboptimal gap fills. |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7585 | ShuffleForward<8>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 7586 | ShuffleForward<4>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 7587 | ShuffleForward<2>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 7588 | ShuffleForward<1>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 7589 | CHECK(grouped_and_sorted_fields.empty()) << "Missed " << grouped_and_sorted_fields.size() << |
| 7590 | " fields."; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 7591 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7592 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7593 | // 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] | 7594 | if (!is_static && klass->DescriptorEquals("Ljava/lang/ref/Reference;")) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7595 | // We know there are no non-reference fields in the Reference classes, and we know |
| 7596 | // that 'referent' is alphabetically last, so this is easy... |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7597 | CHECK_EQ(num_reference_fields, num_fields) << klass->PrettyClass(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7598 | CHECK_STREQ(fields->At(num_fields - 1).GetName(), "referent") |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7599 | << klass->PrettyClass(); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7600 | --num_reference_fields; |
| 7601 | } |
| 7602 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 7603 | size_t size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7604 | // Update klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7605 | if (is_static) { |
| 7606 | klass->SetNumReferenceStaticFields(num_reference_fields); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 7607 | *class_size = size; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7608 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7609 | klass->SetNumReferenceInstanceFields(num_reference_fields); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7610 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 7611 | if (num_reference_fields == 0 || super_class == nullptr) { |
| 7612 | // 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] | 7613 | // super_class is null iff the class is java.lang.Object. |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 7614 | if (super_class == nullptr || |
| 7615 | (super_class->GetClassFlags() & mirror::kClassFlagNoReferenceFields) != 0) { |
| 7616 | klass->SetClassFlags(klass->GetClassFlags() | mirror::kClassFlagNoReferenceFields); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 7617 | } |
| 7618 | } |
| 7619 | if (kIsDebugBuild) { |
| 7620 | DCHECK_EQ(super_class == nullptr, klass->DescriptorEquals("Ljava/lang/Object;")); |
| 7621 | size_t total_reference_instance_fields = 0; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7622 | ObjPtr<mirror::Class> cur_super = klass.Get(); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 7623 | while (cur_super != nullptr) { |
| 7624 | total_reference_instance_fields += cur_super->NumReferenceInstanceFieldsDuringLinking(); |
| 7625 | cur_super = cur_super->GetSuperClass(); |
| 7626 | } |
| 7627 | if (super_class == nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7628 | CHECK_EQ(total_reference_instance_fields, 1u) << klass->PrettyDescriptor(); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 7629 | } else { |
| 7630 | // Check that there is at least num_reference_fields other than Object.class. |
| 7631 | CHECK_GE(total_reference_instance_fields, 1u + num_reference_fields) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7632 | << klass->PrettyClass(); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 7633 | } |
| 7634 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7635 | if (!klass->IsVariableSize()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7636 | std::string temp; |
| 7637 | DCHECK_GE(size, sizeof(mirror::Object)) << klass->GetDescriptor(&temp); |
| 7638 | size_t previous_size = klass->GetObjectSize(); |
| 7639 | if (previous_size != 0) { |
| 7640 | // Make sure that we didn't originally have an incorrect size. |
| 7641 | CHECK_EQ(previous_size, size) << klass->GetDescriptor(&temp); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 7642 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7643 | klass->SetObjectSize(size); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7644 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7645 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7646 | |
| 7647 | if (kIsDebugBuild) { |
| 7648 | // Make sure that the fields array is ordered by name but all reference |
| 7649 | // offsets are at the beginning as far as alignment allows. |
| 7650 | MemberOffset start_ref_offset = is_static |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7651 | ? klass->GetFirstReferenceStaticFieldOffsetDuringLinking(image_pointer_size_) |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7652 | : klass->GetFirstReferenceInstanceFieldOffset(); |
| 7653 | MemberOffset end_ref_offset(start_ref_offset.Uint32Value() + |
| 7654 | num_reference_fields * |
| 7655 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 7656 | MemberOffset current_ref_offset = start_ref_offset; |
| 7657 | for (size_t i = 0; i < num_fields; i++) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7658 | ArtField* field = &fields->At(i); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7659 | VLOG(class_linker) << "LinkFields: " << (is_static ? "static" : "instance") |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7660 | << " class=" << klass->PrettyClass() << " field=" << field->PrettyField() |
| 7661 | << " offset=" << field->GetOffsetDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7662 | if (i != 0) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7663 | ArtField* const prev_field = &fields->At(i - 1); |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 7664 | // NOTE: The field names can be the same. This is not possible in the Java language |
| 7665 | // 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] | 7666 | DCHECK_LE(strcmp(prev_field->GetName(), field->GetName()), 0); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7667 | } |
| 7668 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 7669 | bool is_primitive = type != Primitive::kPrimNot; |
| 7670 | if (klass->DescriptorEquals("Ljava/lang/ref/Reference;") && |
| 7671 | strcmp("referent", field->GetName()) == 0) { |
| 7672 | is_primitive = true; // We lied above, so we have to expect a lie here. |
| 7673 | } |
| 7674 | MemberOffset offset = field->GetOffsetDuringLinking(); |
| 7675 | if (is_primitive) { |
| 7676 | if (offset.Uint32Value() < end_ref_offset.Uint32Value()) { |
| 7677 | // Shuffled before references. |
| 7678 | size_t type_size = Primitive::ComponentSize(type); |
| 7679 | CHECK_LT(type_size, sizeof(mirror::HeapReference<mirror::Object>)); |
| 7680 | CHECK_LT(offset.Uint32Value(), start_ref_offset.Uint32Value()); |
| 7681 | CHECK_LE(offset.Uint32Value() + type_size, start_ref_offset.Uint32Value()); |
| 7682 | CHECK(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(offset.Uint32Value())); |
| 7683 | } |
| 7684 | } else { |
| 7685 | CHECK_EQ(current_ref_offset.Uint32Value(), offset.Uint32Value()); |
| 7686 | current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() + |
| 7687 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 7688 | } |
| 7689 | } |
| 7690 | CHECK_EQ(current_ref_offset.Uint32Value(), end_ref_offset.Uint32Value()); |
| 7691 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7692 | return true; |
| 7693 | } |
| 7694 | |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7695 | // Set the bitmap of reference instance field offsets. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7696 | void ClassLinker::CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7697 | uint32_t reference_offsets = 0; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7698 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7699 | // 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] | 7700 | if (super_class != nullptr) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7701 | reference_offsets = super_class->GetReferenceInstanceOffsets(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7702 | // Compute reference offsets unless our superclass overflowed. |
| 7703 | if (reference_offsets != mirror::Class::kClassWalkSuper) { |
| 7704 | size_t num_reference_fields = klass->NumReferenceInstanceFieldsDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7705 | if (num_reference_fields != 0u) { |
| 7706 | // All of the fields that contain object references are guaranteed be grouped in memory |
| 7707 | // starting at an appropriately aligned address after super class object data. |
| 7708 | uint32_t start_offset = RoundUp(super_class->GetObjectSize(), |
| 7709 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 7710 | uint32_t start_bit = (start_offset - mirror::kObjectHeaderSize) / |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7711 | sizeof(mirror::HeapReference<mirror::Object>); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7712 | if (start_bit + num_reference_fields > 32) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7713 | reference_offsets = mirror::Class::kClassWalkSuper; |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7714 | } else { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7715 | reference_offsets |= (0xffffffffu << start_bit) & |
| 7716 | (0xffffffffu >> (32 - (start_bit + num_reference_fields))); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7717 | } |
| 7718 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 7719 | } |
| 7720 | } |
Mingyao Yang | faff0f0 | 2014-09-10 12:03:22 -0700 | [diff] [blame] | 7721 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7722 | } |
| 7723 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7724 | mirror::String* ClassLinker::ResolveString(const DexFile& dex_file, |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 7725 | dex::StringIndex string_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7726 | Handle<mirror::DexCache> dex_cache) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7727 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 7728 | Thread::PoisonObjectPointersIfDebug(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7729 | ObjPtr<mirror::String> resolved = dex_cache->GetResolvedString(string_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7730 | if (resolved != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7731 | return resolved.Ptr(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7732 | } |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 7733 | uint32_t utf16_length; |
| 7734 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7735 | ObjPtr<mirror::String> string = intern_table_->InternStrong(utf16_length, utf8_data); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 7736 | if (string != nullptr) { |
| 7737 | dex_cache->SetResolvedString(string_idx, string); |
| 7738 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7739 | return string.Ptr(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7740 | } |
| 7741 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7742 | mirror::String* ClassLinker::LookupString(const DexFile& dex_file, |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 7743 | dex::StringIndex string_idx, |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 7744 | ObjPtr<mirror::DexCache> dex_cache) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7745 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7746 | ObjPtr<mirror::String> resolved = dex_cache->GetResolvedString(string_idx); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7747 | if (resolved != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7748 | return resolved.Ptr(); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7749 | } |
| 7750 | uint32_t utf16_length; |
| 7751 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 7752 | ObjPtr<mirror::String> string = |
| 7753 | intern_table_->LookupStrong(Thread::Current(), utf16_length, utf8_data); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7754 | if (string != nullptr) { |
| 7755 | dex_cache->SetResolvedString(string_idx, string); |
| 7756 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7757 | return string.Ptr(); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7758 | } |
| 7759 | |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 7760 | ObjPtr<mirror::Class> ClassLinker::LookupResolvedType(const DexFile& dex_file, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 7761 | dex::TypeIndex type_idx, |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 7762 | ObjPtr<mirror::DexCache> dex_cache, |
| 7763 | ObjPtr<mirror::ClassLoader> class_loader) { |
| 7764 | ObjPtr<mirror::Class> type = dex_cache->GetResolvedType(type_idx); |
| 7765 | if (type == nullptr) { |
| 7766 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
| 7767 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
| 7768 | if (descriptor[1] == '\0') { |
| 7769 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 7770 | // for primitive classes that aren't backed by dex files. |
| 7771 | type = FindPrimitiveClass(descriptor[0]); |
| 7772 | } else { |
| 7773 | Thread* const self = Thread::Current(); |
| 7774 | DCHECK(self != nullptr); |
| 7775 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
| 7776 | // Find the class in the loaded classes table. |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 7777 | type = LookupClass(self, descriptor, hash, class_loader.Ptr()); |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 7778 | } |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 7779 | if (type != nullptr) { |
| 7780 | if (type->IsResolved()) { |
| 7781 | dex_cache->SetResolvedType(type_idx, type); |
| 7782 | } else { |
| 7783 | type = nullptr; |
| 7784 | } |
| 7785 | } |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 7786 | } |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 7787 | DCHECK(type == nullptr || type->IsResolved()); |
| 7788 | return type; |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 7789 | } |
| 7790 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7791 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 7792 | dex::TypeIndex type_idx, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7793 | ObjPtr<mirror::Class> referrer) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 7794 | StackHandleScope<2> hs(Thread::Current()); |
| 7795 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 7796 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 7797 | return ResolveType(dex_file, type_idx, dex_cache, class_loader); |
| 7798 | } |
| 7799 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7800 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 7801 | dex::TypeIndex type_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7802 | Handle<mirror::DexCache> dex_cache, |
| 7803 | Handle<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7804 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 7805 | Thread::PoisonObjectPointersIfDebug(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7806 | ObjPtr<mirror::Class> resolved = dex_cache->GetResolvedType(type_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7807 | if (resolved == nullptr) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 7808 | // TODO: Avoid this lookup as it duplicates work done in FindClass(). It is here |
| 7809 | // as a workaround for FastNative JNI to avoid AssertNoPendingException() when |
| 7810 | // trying to resolve annotations while an exception may be pending. Bug: 34659969 |
| 7811 | resolved = LookupResolvedType(dex_file, type_idx, dex_cache.Get(), class_loader.Get()); |
| 7812 | } |
| 7813 | if (resolved == nullptr) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 7814 | Thread* self = Thread::Current(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 7815 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 7816 | resolved = FindClass(self, descriptor, class_loader); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7817 | if (resolved != nullptr) { |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 7818 | // TODO: we used to throw here if resolved's class loader was not the |
| 7819 | // boot class loader. This was to permit different classes with the |
| 7820 | // same name to be loaded simultaneously by different loaders |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7821 | dex_cache->SetResolvedType(type_idx, resolved); |
| 7822 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 7823 | CHECK(self->IsExceptionPending()) |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 7824 | << "Expected pending exception for failed resolution of: " << descriptor; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 7825 | // Convert a ClassNotFoundException to a NoClassDefFoundError. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 7826 | StackHandleScope<1> hs(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 7827 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 7828 | if (cause->InstanceOf(GetClassRoot(kJavaLangClassNotFoundException))) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7829 | DCHECK(resolved == nullptr); // No Handle needed to preserve resolved. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 7830 | self->ClearException(); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 7831 | ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 7832 | self->GetException()->SetCause(cause.Get()); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 7833 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7834 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7835 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 7836 | DCHECK((resolved == nullptr) || resolved->IsResolved()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7837 | << resolved->PrettyDescriptor() << " " << resolved->GetStatus(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7838 | return resolved.Ptr(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7839 | } |
| 7840 | |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 7841 | template <ClassLinker::ResolveMode kResolveMode> |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7842 | ArtMethod* ClassLinker::ResolveMethod(const DexFile& dex_file, |
| 7843 | uint32_t method_idx, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7844 | Handle<mirror::DexCache> dex_cache, |
| 7845 | Handle<mirror::ClassLoader> class_loader, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7846 | ArtMethod* referrer, |
| 7847 | InvokeType type) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7848 | DCHECK(dex_cache != nullptr); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7849 | // Check for hit in the dex cache. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7850 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 7851 | Thread::PoisonObjectPointersIfDebug(); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 7852 | if (resolved != nullptr && !resolved->IsRuntimeMethod()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7853 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 7854 | if (kResolveMode == ClassLinker::kForceICCECheck) { |
| 7855 | if (resolved->CheckIncompatibleClassChange(type)) { |
| 7856 | ThrowIncompatibleClassChangeError(type, resolved->GetInvokeType(), resolved, referrer); |
| 7857 | return nullptr; |
| 7858 | } |
| 7859 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7860 | return resolved; |
| 7861 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7862 | // Fail, get the declaring class. |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7863 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7864 | 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] | 7865 | if (klass == nullptr) { |
Elliott Hughes | cc5f9a9 | 2011-09-28 19:17:29 -0700 | [diff] [blame] | 7866 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7867 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7868 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7869 | // Scan using method_idx, this saves string compares but will only hit for matching dex |
| 7870 | // caches/files. |
| 7871 | switch (type) { |
| 7872 | case kDirect: // Fall-through. |
| 7873 | case kStatic: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7874 | resolved = klass->FindDirectMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 7875 | DCHECK(resolved == nullptr || resolved->GetDeclaringClassUnchecked() != nullptr); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7876 | break; |
| 7877 | case kInterface: |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 7878 | // We have to check whether the method id really belongs to an interface (dex static bytecode |
| 7879 | // constraint A15). Otherwise you must not invoke-interface on it. |
| 7880 | // |
| 7881 | // This is not symmetric to A12-A14 (direct, static, virtual), as using FindInterfaceMethod |
| 7882 | // assumes that the given type is an interface, and will check the interface table if the |
| 7883 | // method isn't declared in the class. So it may find an interface method (usually by name |
| 7884 | // in the handling below, but we do the constraint check early). In that case, |
| 7885 | // CheckIncompatibleClassChange will succeed (as it is called on an interface method) |
| 7886 | // unexpectedly. |
| 7887 | // Example: |
| 7888 | // interface I { |
| 7889 | // foo() |
| 7890 | // } |
| 7891 | // class A implements I { |
| 7892 | // ... |
| 7893 | // } |
| 7894 | // class B extends A { |
| 7895 | // ... |
| 7896 | // } |
| 7897 | // invoke-interface B.foo |
| 7898 | // -> FindInterfaceMethod finds I.foo (interface method), not A.foo (miranda method) |
| 7899 | if (UNLIKELY(!klass->IsInterface())) { |
| 7900 | ThrowIncompatibleClassChangeError(klass, |
| 7901 | "Found class %s, but interface was expected", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7902 | klass->PrettyDescriptor().c_str()); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 7903 | return nullptr; |
| 7904 | } else { |
| 7905 | resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 7906 | DCHECK(resolved == nullptr || resolved->GetDeclaringClass()->IsInterface()); |
| 7907 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7908 | break; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7909 | case kSuper: |
| 7910 | if (klass->IsInterface()) { |
| 7911 | resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 7912 | } else { |
| 7913 | resolved = klass->FindVirtualMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 7914 | } |
| 7915 | break; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7916 | case kVirtual: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7917 | resolved = klass->FindVirtualMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7918 | break; |
| 7919 | default: |
| 7920 | LOG(FATAL) << "Unreachable - invocation type: " << type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 7921 | UNREACHABLE(); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 7922 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7923 | if (resolved == nullptr) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7924 | // Search by name, which works across dex files. |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 7925 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 7926 | const Signature signature = dex_file.GetMethodSignature(method_id); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7927 | switch (type) { |
| 7928 | case kDirect: // Fall-through. |
| 7929 | case kStatic: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7930 | resolved = klass->FindDirectMethod(name, signature, image_pointer_size_); |
| 7931 | DCHECK(resolved == nullptr || resolved->GetDeclaringClassUnchecked() != nullptr); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7932 | break; |
| 7933 | case kInterface: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7934 | resolved = klass->FindInterfaceMethod(name, signature, image_pointer_size_); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7935 | DCHECK(resolved == nullptr || resolved->GetDeclaringClass()->IsInterface()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7936 | break; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7937 | case kSuper: |
| 7938 | if (klass->IsInterface()) { |
| 7939 | resolved = klass->FindInterfaceMethod(name, signature, image_pointer_size_); |
| 7940 | } else { |
| 7941 | resolved = klass->FindVirtualMethod(name, signature, image_pointer_size_); |
| 7942 | } |
| 7943 | break; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7944 | case kVirtual: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7945 | resolved = klass->FindVirtualMethod(name, signature, image_pointer_size_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7946 | break; |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 7947 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7948 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7949 | // If we found a method, check for incompatible class changes. |
| 7950 | if (LIKELY(resolved != nullptr && !resolved->CheckIncompatibleClassChange(type))) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7951 | // 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] | 7952 | dex_cache->SetResolvedMethod(method_idx, resolved, image_pointer_size_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7953 | return resolved; |
| 7954 | } else { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7955 | // If we had a method, it's an incompatible-class-change error. |
| 7956 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7957 | ThrowIncompatibleClassChangeError(type, resolved->GetInvokeType(), resolved, referrer); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7958 | } else { |
| 7959 | // We failed to find the method which means either an access error, an incompatible class |
| 7960 | // change, or no such method. First try to find the method among direct and virtual methods. |
| 7961 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
| 7962 | const Signature signature = dex_file.GetMethodSignature(method_id); |
| 7963 | switch (type) { |
| 7964 | case kDirect: |
| 7965 | case kStatic: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7966 | resolved = klass->FindVirtualMethod(name, signature, image_pointer_size_); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7967 | // Note: kDirect and kStatic are also mutually exclusive, but in that case we would |
| 7968 | // have had a resolved method before, which triggers the "true" branch above. |
| 7969 | break; |
| 7970 | case kInterface: |
| 7971 | case kVirtual: |
| 7972 | case kSuper: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7973 | resolved = klass->FindDirectMethod(name, signature, image_pointer_size_); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7974 | break; |
| 7975 | } |
| 7976 | |
| 7977 | // 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] | 7978 | bool exception_generated = false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7979 | if (resolved != nullptr && referrer != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7980 | ObjPtr<mirror::Class> methods_class = resolved->GetDeclaringClass(); |
| 7981 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7982 | if (!referring_class->CanAccess(methods_class)) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7983 | ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, |
| 7984 | methods_class, |
| 7985 | resolved, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7986 | type); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 7987 | exception_generated = true; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7988 | } else if (!referring_class->CanAccessMember(methods_class, resolved->GetAccessFlags())) { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7989 | ThrowIllegalAccessErrorMethod(referring_class, resolved); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 7990 | exception_generated = true; |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7991 | } |
| 7992 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 7993 | if (!exception_generated) { |
| 7994 | // Otherwise, throw an IncompatibleClassChangeError if we found something, and check |
| 7995 | // interface methods and throw if we find the method there. If we find nothing, throw a |
| 7996 | // NoSuchMethodError. |
| 7997 | switch (type) { |
| 7998 | case kDirect: |
| 7999 | case kStatic: |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8000 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8001 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8002 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8003 | resolved = klass->FindInterfaceMethod(name, signature, image_pointer_size_); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8004 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8005 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8006 | } else { |
| 8007 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 8008 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8009 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8010 | break; |
| 8011 | case kInterface: |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8012 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8013 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8014 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8015 | resolved = klass->FindVirtualMethod(name, signature, image_pointer_size_); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8016 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8017 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8018 | } else { |
| 8019 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 8020 | } |
| 8021 | } |
| 8022 | break; |
| 8023 | case kSuper: |
| 8024 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8025 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8026 | } else { |
| 8027 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 8028 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8029 | break; |
| 8030 | case kVirtual: |
| 8031 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8032 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8033 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8034 | resolved = klass->FindInterfaceMethod(name, signature, image_pointer_size_); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8035 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8036 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8037 | } else { |
| 8038 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 8039 | } |
| 8040 | } |
| 8041 | break; |
| 8042 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8043 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8044 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8045 | Thread::Current()->AssertPendingException(); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8046 | return nullptr; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8047 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8048 | } |
| 8049 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 8050 | ArtMethod* ClassLinker::ResolveMethodWithoutInvokeType(const DexFile& dex_file, |
| 8051 | uint32_t method_idx, |
| 8052 | Handle<mirror::DexCache> dex_cache, |
| 8053 | Handle<mirror::ClassLoader> class_loader) { |
| 8054 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 8055 | Thread::PoisonObjectPointersIfDebug(); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 8056 | if (resolved != nullptr && !resolved->IsRuntimeMethod()) { |
| 8057 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
| 8058 | return resolved; |
| 8059 | } |
| 8060 | // Fail, get the declaring class. |
| 8061 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8062 | 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] | 8063 | if (klass == nullptr) { |
| 8064 | Thread::Current()->AssertPendingException(); |
| 8065 | return nullptr; |
| 8066 | } |
| 8067 | if (klass->IsInterface()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8068 | LOG(FATAL) << "ResolveAmbiguousMethod: unexpected method in interface: " |
| 8069 | << klass->PrettyClass(); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 8070 | return nullptr; |
| 8071 | } |
| 8072 | |
| 8073 | // Search both direct and virtual methods |
| 8074 | resolved = klass->FindDirectMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 8075 | if (resolved == nullptr) { |
| 8076 | resolved = klass->FindVirtualMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 8077 | } |
| 8078 | |
| 8079 | return resolved; |
| 8080 | } |
| 8081 | |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 8082 | ArtField* ClassLinker::LookupResolvedField(uint32_t field_idx, |
| 8083 | ObjPtr<mirror::DexCache> dex_cache, |
| 8084 | ObjPtr<mirror::ClassLoader> class_loader, |
| 8085 | bool is_static) { |
| 8086 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 8087 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
| 8088 | ObjPtr<mirror::Class> klass = dex_cache->GetResolvedType(field_id.class_idx_); |
| 8089 | if (klass == nullptr) { |
| 8090 | klass = LookupResolvedType(dex_file, field_id.class_idx_, dex_cache, class_loader); |
| 8091 | } |
| 8092 | if (klass == nullptr) { |
| 8093 | // The class has not been resolved yet, so the field is also unresolved. |
| 8094 | return nullptr; |
| 8095 | } |
| 8096 | DCHECK(klass->IsResolved()); |
| 8097 | Thread* self = is_static ? Thread::Current() : nullptr; |
| 8098 | |
| 8099 | // First try to find a field declared directly by `klass` by the field index. |
| 8100 | ArtField* resolved_field = is_static |
| 8101 | ? mirror::Class::FindStaticField(self, klass, dex_cache, field_idx) |
| 8102 | : klass->FindInstanceField(dex_cache, field_idx); |
| 8103 | |
| 8104 | if (resolved_field == nullptr) { |
| 8105 | // If not found in `klass` by field index, search the class hierarchy using the name and type. |
| 8106 | const char* name = dex_file.GetFieldName(field_id); |
| 8107 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 8108 | resolved_field = is_static |
| 8109 | ? mirror::Class::FindStaticField(self, klass, name, type) |
| 8110 | : klass->FindInstanceField(name, type); |
| 8111 | } |
| 8112 | |
| 8113 | if (resolved_field != nullptr) { |
| 8114 | dex_cache->SetResolvedField(field_idx, resolved_field, image_pointer_size_); |
| 8115 | } |
| 8116 | return resolved_field; |
| 8117 | } |
| 8118 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 8119 | ArtField* ClassLinker::ResolveField(const DexFile& dex_file, |
| 8120 | uint32_t field_idx, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8121 | Handle<mirror::DexCache> dex_cache, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 8122 | Handle<mirror::ClassLoader> class_loader, |
| 8123 | bool is_static) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8124 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8125 | ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 8126 | Thread::PoisonObjectPointersIfDebug(); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 8127 | if (resolved != nullptr) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8128 | return resolved; |
| 8129 | } |
| 8130 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 8131 | Thread* const self = Thread::Current(); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8132 | ObjPtr<mirror::Class> klass = ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader); |
| 8133 | if (klass == nullptr) { |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 8134 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 8135 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8136 | } |
| 8137 | |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 8138 | if (is_static) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8139 | resolved = mirror::Class::FindStaticField(self, klass, dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 8140 | } else { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 8141 | resolved = klass->FindInstanceField(dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 8142 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 8143 | |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 8144 | if (resolved == nullptr) { |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 8145 | const char* name = dex_file.GetFieldName(field_id); |
| 8146 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 8147 | if (is_static) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 8148 | resolved = mirror::Class::FindStaticField(self, klass, name, type); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 8149 | } else { |
| 8150 | resolved = klass->FindInstanceField(name, type); |
| 8151 | } |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 8152 | if (resolved == nullptr) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8153 | ThrowNoSuchFieldError(is_static ? "static " : "instance ", klass, type, name); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8154 | return nullptr; |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 8155 | } |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 8156 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8157 | dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 8158 | return resolved; |
| 8159 | } |
| 8160 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 8161 | ArtField* ClassLinker::ResolveFieldJLS(const DexFile& dex_file, |
| 8162 | uint32_t field_idx, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8163 | Handle<mirror::DexCache> dex_cache, |
| 8164 | Handle<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8165 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8166 | ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 8167 | Thread::PoisonObjectPointersIfDebug(); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 8168 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 8169 | return resolved; |
| 8170 | } |
| 8171 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 8172 | Thread* self = Thread::Current(); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8173 | ObjPtr<mirror::Class> klass(ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader)); |
| 8174 | if (klass == nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 8175 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8176 | return nullptr; |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 8177 | } |
| 8178 | |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 8179 | StringPiece name(dex_file.GetFieldName(field_id)); |
| 8180 | StringPiece type(dex_file.GetFieldTypeDescriptor(field_id)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 8181 | resolved = mirror::Class::FindField(self, klass, name, type); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8182 | if (resolved != nullptr) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8183 | dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 8184 | } else { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8185 | ThrowNoSuchFieldError("", klass, type, name); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8186 | } |
| 8187 | return resolved; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 8188 | } |
| 8189 | |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8190 | mirror::MethodType* ClassLinker::ResolveMethodType(const DexFile& dex_file, |
| 8191 | uint32_t proto_idx, |
| 8192 | Handle<mirror::DexCache> dex_cache, |
| 8193 | Handle<mirror::ClassLoader> class_loader) { |
| 8194 | DCHECK(Runtime::Current()->IsMethodHandlesEnabled()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8195 | DCHECK(dex_cache != nullptr); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8196 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8197 | ObjPtr<mirror::MethodType> resolved = dex_cache->GetResolvedMethodType(proto_idx); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8198 | if (resolved != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8199 | return resolved.Ptr(); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8200 | } |
| 8201 | |
| 8202 | Thread* const self = Thread::Current(); |
| 8203 | StackHandleScope<4> hs(self); |
| 8204 | |
| 8205 | // First resolve the return type. |
| 8206 | const DexFile::ProtoId& proto_id = dex_file.GetProtoId(proto_idx); |
| 8207 | Handle<mirror::Class> return_type(hs.NewHandle( |
| 8208 | ResolveType(dex_file, proto_id.return_type_idx_, dex_cache, class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8209 | if (return_type == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8210 | DCHECK(self->IsExceptionPending()); |
| 8211 | return nullptr; |
| 8212 | } |
| 8213 | |
| 8214 | // Then resolve the argument types. |
| 8215 | // |
| 8216 | // TODO: Is there a better way to figure out the number of method arguments |
| 8217 | // other than by looking at the shorty ? |
| 8218 | const size_t num_method_args = strlen(dex_file.StringDataByIdx(proto_id.shorty_idx_)) - 1; |
| 8219 | |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 8220 | ObjPtr<mirror::Class> class_type = mirror::Class::GetJavaLangClass(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8221 | ObjPtr<mirror::Class> array_of_class = FindArrayClass(self, &class_type); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8222 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 8223 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_method_args))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8224 | if (method_params == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8225 | DCHECK(self->IsExceptionPending()); |
| 8226 | return nullptr; |
| 8227 | } |
| 8228 | |
| 8229 | DexFileParameterIterator it(dex_file, proto_id); |
| 8230 | int32_t i = 0; |
| 8231 | MutableHandle<mirror::Class> param_class = hs.NewHandle<mirror::Class>(nullptr); |
| 8232 | for (; it.HasNext(); it.Next()) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 8233 | const dex::TypeIndex type_idx = it.GetTypeIdx(); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8234 | param_class.Assign(ResolveType(dex_file, type_idx, dex_cache, class_loader)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8235 | if (param_class == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8236 | DCHECK(self->IsExceptionPending()); |
| 8237 | return nullptr; |
| 8238 | } |
| 8239 | |
| 8240 | method_params->Set(i++, param_class.Get()); |
| 8241 | } |
| 8242 | |
| 8243 | DCHECK(!it.HasNext()); |
| 8244 | |
| 8245 | Handle<mirror::MethodType> type = hs.NewHandle( |
| 8246 | mirror::MethodType::Create(self, return_type, method_params)); |
| 8247 | dex_cache->SetResolvedMethodType(proto_idx, type.Get()); |
| 8248 | |
| 8249 | return type.Get(); |
| 8250 | } |
| 8251 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8252 | mirror::MethodHandle* ClassLinker::ResolveMethodHandle(uint32_t method_handle_idx, |
| 8253 | ArtMethod* referrer) |
| 8254 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8255 | Thread* const self = Thread::Current(); |
| 8256 | const DexFile* const dex_file = referrer->GetDexFile(); |
| 8257 | const DexFile::MethodHandleItem& mh = dex_file->GetMethodHandle(method_handle_idx); |
| 8258 | |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame^] | 8259 | ArtField* target_field = nullptr; |
| 8260 | ArtMethod* target_method = nullptr; |
| 8261 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8262 | DexFile::MethodHandleType handle_type = |
| 8263 | static_cast<DexFile::MethodHandleType>(mh.method_handle_type_); |
| 8264 | switch (handle_type) { |
| 8265 | case DexFile::MethodHandleType::kStaticPut: { |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame^] | 8266 | target_field = ResolveField(mh.field_or_method_idx_, referrer, true /* is_static */); |
| 8267 | break; |
| 8268 | } |
| 8269 | case DexFile::MethodHandleType::kStaticGet: { |
| 8270 | target_field = ResolveField(mh.field_or_method_idx_, referrer, true /* is_static */); |
| 8271 | break; |
| 8272 | } |
| 8273 | case DexFile::MethodHandleType::kInstancePut: { |
| 8274 | target_field = ResolveField(mh.field_or_method_idx_, referrer, false /* is_static */); |
| 8275 | break; |
| 8276 | } |
| 8277 | case DexFile::MethodHandleType::kInstanceGet: { |
| 8278 | target_field = ResolveField(mh.field_or_method_idx_, referrer, false /* is_static */); |
| 8279 | break; |
| 8280 | } |
| 8281 | case DexFile::MethodHandleType::kInvokeStatic: { |
| 8282 | target_method = ResolveMethod<kNoICCECheckForCache>(self, |
| 8283 | mh.field_or_method_idx_, |
| 8284 | referrer, |
| 8285 | InvokeType::kStatic); |
| 8286 | break; |
| 8287 | } |
| 8288 | case DexFile::MethodHandleType::kInvokeInstance: { |
| 8289 | target_method = ResolveMethod<kNoICCECheckForCache>(self, |
| 8290 | mh.field_or_method_idx_, |
| 8291 | referrer, |
| 8292 | InvokeType::kVirtual); |
| 8293 | break; |
| 8294 | } |
| 8295 | case DexFile::MethodHandleType::kInvokeConstructor: { |
| 8296 | UNIMPLEMENTED(FATAL) << "Invoke constructor is implemented as a transform."; |
| 8297 | break; |
| 8298 | } |
| 8299 | case DexFile::MethodHandleType::kInvokeDirect: { |
| 8300 | target_method = ResolveMethod<kNoICCECheckForCache>(self, |
| 8301 | mh.field_or_method_idx_, |
| 8302 | referrer, |
| 8303 | InvokeType::kDirect); |
| 8304 | break; |
| 8305 | } |
| 8306 | case DexFile::MethodHandleType::kInvokeInterface: { |
| 8307 | target_method = ResolveMethod<kNoICCECheckForCache>(self, |
| 8308 | mh.field_or_method_idx_, |
| 8309 | referrer, |
| 8310 | InvokeType::kInterface); |
| 8311 | break; |
| 8312 | } |
| 8313 | } |
| 8314 | |
| 8315 | if (target_field != nullptr) { |
| 8316 | ObjPtr<mirror::Class> target_class = target_field->GetDeclaringClass(); |
| 8317 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 8318 | if (!referring_class->CanAccessMember(target_class, target_field->GetAccessFlags())) { |
| 8319 | ThrowIllegalAccessErrorField(referring_class, target_field); |
| 8320 | return nullptr; |
| 8321 | } |
| 8322 | } else if (target_method != nullptr) { |
| 8323 | ObjPtr<mirror::Class> target_class = target_method->GetDeclaringClass(); |
| 8324 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 8325 | if (!referring_class->CanAccessMember(target_class, target_method->GetAccessFlags())) { |
| 8326 | ThrowIllegalAccessErrorMethod(referring_class, target_method); |
| 8327 | return nullptr; |
| 8328 | } |
| 8329 | } else { |
| 8330 | // Common check for resolution failure. |
| 8331 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 8332 | return nullptr; |
| 8333 | } |
| 8334 | |
| 8335 | // Determine the kind and number of parameters after it's safe to |
| 8336 | // follow the field or method pointer. |
| 8337 | mirror::MethodHandle::Kind kind; |
| 8338 | uint32_t num_params; |
| 8339 | switch (handle_type) { |
| 8340 | case DexFile::MethodHandleType::kStaticPut: { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8341 | kind = mirror::MethodHandle::Kind::kStaticPut; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8342 | num_params = 1; |
| 8343 | break; |
| 8344 | } |
| 8345 | case DexFile::MethodHandleType::kStaticGet: { |
| 8346 | kind = mirror::MethodHandle::Kind::kStaticGet; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8347 | num_params = 0; |
| 8348 | break; |
| 8349 | } |
| 8350 | case DexFile::MethodHandleType::kInstancePut: { |
| 8351 | kind = mirror::MethodHandle::Kind::kInstancePut; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8352 | num_params = 2; |
| 8353 | break; |
| 8354 | } |
| 8355 | case DexFile::MethodHandleType::kInstanceGet: { |
| 8356 | kind = mirror::MethodHandle::Kind::kInstanceGet; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8357 | num_params = 1; |
| 8358 | break; |
| 8359 | } |
| 8360 | case DexFile::MethodHandleType::kInvokeStatic: { |
| 8361 | kind = mirror::MethodHandle::Kind::kInvokeStatic; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8362 | uint32_t shorty_length; |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame^] | 8363 | target_method->GetShorty(&shorty_length); |
| 8364 | num_params = shorty_length - 1; // Remove 1 for the return value. |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8365 | break; |
| 8366 | } |
| 8367 | case DexFile::MethodHandleType::kInvokeInstance: { |
| 8368 | kind = mirror::MethodHandle::Kind::kInvokeVirtual; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8369 | uint32_t shorty_length; |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame^] | 8370 | target_method->GetShorty(&shorty_length); |
| 8371 | num_params = shorty_length; // Add 1 for the receiver, remove 1 for the return value. |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8372 | break; |
| 8373 | } |
| 8374 | case DexFile::MethodHandleType::kInvokeConstructor: { |
| 8375 | UNIMPLEMENTED(FATAL) << "Invoke constructor is implemented as a transform."; |
| 8376 | num_params = 0; |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame^] | 8377 | break; |
| 8378 | } |
| 8379 | case DexFile::MethodHandleType::kInvokeDirect: { |
| 8380 | kind = mirror::MethodHandle::Kind::kInvokeDirect; |
| 8381 | uint32_t shorty_length; |
| 8382 | target_method->GetShorty(&shorty_length); |
| 8383 | num_params = shorty_length; // Add 1 for the receiver, remove 1 for the return value. |
| 8384 | break; |
| 8385 | } |
| 8386 | case DexFile::MethodHandleType::kInvokeInterface: { |
| 8387 | kind = mirror::MethodHandle::Kind::kInvokeInterface; |
| 8388 | uint32_t shorty_length; |
| 8389 | target_method->GetShorty(&shorty_length); |
| 8390 | num_params = shorty_length; // Add 1 for the receiver, remove 1 for the return value. |
| 8391 | break; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8392 | } |
| 8393 | } |
| 8394 | |
| 8395 | StackHandleScope<5> hs(self); |
| 8396 | ObjPtr<mirror::Class> class_type = mirror::Class::GetJavaLangClass(); |
| 8397 | ObjPtr<mirror::Class> array_of_class = FindArrayClass(self, &class_type); |
| 8398 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 8399 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params))); |
| 8400 | if (method_params.Get() == nullptr) { |
| 8401 | DCHECK(self->IsExceptionPending()); |
| 8402 | return nullptr; |
| 8403 | } |
| 8404 | |
| 8405 | Handle<mirror::Class> return_type; |
| 8406 | switch (handle_type) { |
| 8407 | case DexFile::MethodHandleType::kStaticPut: { |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame^] | 8408 | method_params->Set(0, target_field->GetType<true>()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8409 | return_type = hs.NewHandle(FindPrimitiveClass('V')); |
| 8410 | break; |
| 8411 | } |
| 8412 | case DexFile::MethodHandleType::kStaticGet: { |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame^] | 8413 | return_type = hs.NewHandle(target_field->GetType<true>()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8414 | break; |
| 8415 | } |
| 8416 | case DexFile::MethodHandleType::kInstancePut: { |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame^] | 8417 | method_params->Set(0, target_field->GetDeclaringClass()); |
| 8418 | method_params->Set(1, target_field->GetType<true>()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8419 | return_type = hs.NewHandle(FindPrimitiveClass('V')); |
| 8420 | break; |
| 8421 | } |
| 8422 | case DexFile::MethodHandleType::kInstanceGet: { |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame^] | 8423 | method_params->Set(0, target_field->GetDeclaringClass()); |
| 8424 | return_type = hs.NewHandle(target_field->GetType<true>()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8425 | break; |
| 8426 | } |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame^] | 8427 | case DexFile::MethodHandleType::kInvokeDirect: |
| 8428 | case DexFile::MethodHandleType::kInvokeInstance: |
| 8429 | case DexFile::MethodHandleType::kInvokeInterface: |
| 8430 | case DexFile::MethodHandleType::kInvokeStatic: { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8431 | // TODO(oth): This will not work for varargs methods as this |
| 8432 | // requires instantiating a Transformer. This resolution step |
| 8433 | // would be best done in managed code rather than in the run |
| 8434 | // time (b/35235705) |
| 8435 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 8436 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame^] | 8437 | DexFileParameterIterator it(*dex_file, target_method->GetPrototype()); |
| 8438 | int32_t index = 0; |
| 8439 | if (handle_type != DexFile::MethodHandleType::kInvokeStatic) { |
| 8440 | method_params->Set(index++, target_method->GetDeclaringClass()); |
| 8441 | } |
| 8442 | while (it.HasNext()) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8443 | const dex::TypeIndex type_idx = it.GetTypeIdx(); |
| 8444 | mirror::Class* klass = ResolveType(*dex_file, type_idx, dex_cache, class_loader); |
| 8445 | if (nullptr == klass) { |
| 8446 | DCHECK(self->IsExceptionPending()); |
| 8447 | return nullptr; |
| 8448 | } |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame^] | 8449 | method_params->Set(index++, klass); |
| 8450 | it.Next(); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8451 | } |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame^] | 8452 | return_type = hs.NewHandle(target_method->GetReturnType(true)); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8453 | break; |
| 8454 | } |
| 8455 | case DexFile::MethodHandleType::kInvokeConstructor: { |
| 8456 | // TODO(oth): b/35235705 |
| 8457 | UNIMPLEMENTED(FATAL) << "Invoke constructor is implemented as a transform."; |
| 8458 | } |
| 8459 | } |
| 8460 | |
| 8461 | if (return_type.IsNull()) { |
| 8462 | DCHECK(self->IsExceptionPending()); |
| 8463 | return nullptr; |
| 8464 | } |
| 8465 | |
| 8466 | Handle<mirror::MethodType> |
| 8467 | mt(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params))); |
| 8468 | if (mt.IsNull()) { |
| 8469 | DCHECK(self->IsExceptionPending()); |
| 8470 | return nullptr; |
| 8471 | } |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame^] | 8472 | |
| 8473 | uintptr_t target; |
| 8474 | if (target_field != nullptr) { |
| 8475 | target = reinterpret_cast<uintptr_t>(target_field); |
| 8476 | } else { |
| 8477 | target = reinterpret_cast<uintptr_t>(target_method); |
| 8478 | } |
| 8479 | return mirror::MethodHandleImpl::Create(self, target, kind, mt); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8480 | } |
| 8481 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8482 | bool ClassLinker::IsQuickResolutionStub(const void* entry_point) const { |
| 8483 | return (entry_point == GetQuickResolutionStub()) || |
| 8484 | (quick_resolution_trampoline_ == entry_point); |
| 8485 | } |
| 8486 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8487 | bool ClassLinker::IsQuickToInterpreterBridge(const void* entry_point) const { |
| 8488 | return (entry_point == GetQuickToInterpreterBridge()) || |
| 8489 | (quick_to_interpreter_bridge_trampoline_ == entry_point); |
| 8490 | } |
| 8491 | |
| 8492 | bool ClassLinker::IsQuickGenericJniStub(const void* entry_point) const { |
| 8493 | return (entry_point == GetQuickGenericJniStub()) || |
| 8494 | (quick_generic_jni_trampoline_ == entry_point); |
| 8495 | } |
| 8496 | |
| 8497 | const void* ClassLinker::GetRuntimeQuickGenericJniStub() const { |
| 8498 | return GetQuickGenericJniStub(); |
| 8499 | } |
| 8500 | |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 8501 | void ClassLinker::SetEntryPointsToCompiledCode(ArtMethod* method, const void* code) const { |
| 8502 | CHECK(code != nullptr); |
| 8503 | const uint8_t* base = reinterpret_cast<const uint8_t*>(code); // Base of data points at code. |
| 8504 | base -= sizeof(void*); // Move backward so that code_offset != 0. |
| 8505 | const uint32_t code_offset = sizeof(void*); |
| 8506 | OatFile::OatMethod oat_method(base, code_offset); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8507 | oat_method.LinkMethod(method); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8508 | } |
| 8509 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8510 | void ClassLinker::SetEntryPointsToInterpreter(ArtMethod* method) const { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8511 | if (!method->IsNative()) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8512 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 8513 | } else { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 8514 | SetEntryPointsToCompiledCode(method, GetQuickGenericJniStub()); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8515 | } |
| 8516 | } |
| 8517 | |
Alex Light | db01a09 | 2017-04-03 15:39:55 -0700 | [diff] [blame] | 8518 | void ClassLinker::SetEntryPointsForObsoleteMethod(ArtMethod* method) const { |
| 8519 | DCHECK(method->IsObsolete()); |
| 8520 | // We cannot mess with the entrypoints of native methods because they are used to determine how |
| 8521 | // large the method's quick stack frame is. Without this information we cannot walk the stacks. |
| 8522 | if (!method->IsNative()) { |
| 8523 | method->SetEntryPointFromQuickCompiledCode(GetInvokeObsoleteMethodStub()); |
| 8524 | } |
| 8525 | } |
| 8526 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 8527 | void ClassLinker::DumpForSigQuit(std::ostream& os) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 8528 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 8529 | ReaderMutexLock mu(soa.Self(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 8530 | os << "Zygote loaded classes=" << NumZygoteClasses() << " post zygote classes=" |
| 8531 | << NumNonZygoteClasses() << "\n"; |
| 8532 | } |
| 8533 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8534 | class CountClassesVisitor : public ClassLoaderVisitor { |
| 8535 | public: |
| 8536 | CountClassesVisitor() : num_zygote_classes(0), num_non_zygote_classes(0) {} |
| 8537 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8538 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 8539 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) OVERRIDE { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8540 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 8541 | if (class_table != nullptr) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 8542 | num_zygote_classes += class_table->NumZygoteClasses(class_loader); |
| 8543 | num_non_zygote_classes += class_table->NumNonZygoteClasses(class_loader); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 8544 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 8545 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8546 | |
| 8547 | size_t num_zygote_classes; |
| 8548 | size_t num_non_zygote_classes; |
| 8549 | }; |
| 8550 | |
| 8551 | size_t ClassLinker::NumZygoteClasses() const { |
| 8552 | CountClassesVisitor visitor; |
| 8553 | VisitClassLoaders(&visitor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 8554 | return visitor.num_zygote_classes + boot_class_table_->NumZygoteClasses(nullptr); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 8555 | } |
| 8556 | |
| 8557 | size_t ClassLinker::NumNonZygoteClasses() const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8558 | CountClassesVisitor visitor; |
| 8559 | VisitClassLoaders(&visitor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 8560 | return visitor.num_non_zygote_classes + boot_class_table_->NumNonZygoteClasses(nullptr); |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 8561 | } |
| 8562 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 8563 | size_t ClassLinker::NumLoadedClasses() { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 8564 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 8565 | // 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] | 8566 | return NumNonZygoteClasses(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 8567 | } |
| 8568 | |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 8569 | pid_t ClassLinker::GetClassesLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 8570 | return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 8571 | } |
| 8572 | |
| 8573 | pid_t ClassLinker::GetDexLockOwner() { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 8574 | return Locks::dex_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 8575 | } |
| 8576 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8577 | void ClassLinker::SetClassRoot(ClassRoot class_root, ObjPtr<mirror::Class> klass) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 8578 | DCHECK(!init_done_); |
| 8579 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8580 | DCHECK(klass != nullptr); |
| 8581 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 8582 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 8583 | mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8584 | DCHECK(class_roots != nullptr); |
| 8585 | DCHECK(class_roots->Get(class_root) == nullptr); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 8586 | class_roots->Set<false>(class_root, klass); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 8587 | } |
| 8588 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8589 | const char* ClassLinker::GetClassRootDescriptor(ClassRoot class_root) { |
| 8590 | static const char* class_roots_descriptors[] = { |
| 8591 | "Ljava/lang/Class;", |
| 8592 | "Ljava/lang/Object;", |
| 8593 | "[Ljava/lang/Class;", |
| 8594 | "[Ljava/lang/Object;", |
| 8595 | "Ljava/lang/String;", |
| 8596 | "Ljava/lang/DexCache;", |
| 8597 | "Ljava/lang/ref/Reference;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 8598 | "Ljava/lang/reflect/Constructor;", |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 8599 | "Ljava/lang/reflect/Field;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 8600 | "Ljava/lang/reflect/Method;", |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8601 | "Ljava/lang/reflect/Proxy;", |
| 8602 | "[Ljava/lang/String;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 8603 | "[Ljava/lang/reflect/Constructor;", |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 8604 | "[Ljava/lang/reflect/Field;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 8605 | "[Ljava/lang/reflect/Method;", |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8606 | "Ljava/lang/invoke/CallSite;", |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8607 | "Ljava/lang/invoke/MethodHandleImpl;", |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8608 | "Ljava/lang/invoke/MethodHandles$Lookup;", |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8609 | "Ljava/lang/invoke/MethodType;", |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8610 | "Ljava/lang/ClassLoader;", |
| 8611 | "Ljava/lang/Throwable;", |
| 8612 | "Ljava/lang/ClassNotFoundException;", |
| 8613 | "Ljava/lang/StackTraceElement;", |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 8614 | "Ldalvik/system/EmulatedStackFrame;", |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8615 | "Z", |
| 8616 | "B", |
| 8617 | "C", |
| 8618 | "D", |
| 8619 | "F", |
| 8620 | "I", |
| 8621 | "J", |
| 8622 | "S", |
| 8623 | "V", |
| 8624 | "[Z", |
| 8625 | "[B", |
| 8626 | "[C", |
| 8627 | "[D", |
| 8628 | "[F", |
| 8629 | "[I", |
| 8630 | "[J", |
| 8631 | "[S", |
| 8632 | "[Ljava/lang/StackTraceElement;", |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 8633 | "Ldalvik/system/ClassExt;", |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8634 | }; |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 8635 | static_assert(arraysize(class_roots_descriptors) == size_t(kClassRootsMax), |
| 8636 | "Mismatch between class descriptors and class-root enum"); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8637 | |
| 8638 | const char* descriptor = class_roots_descriptors[class_root]; |
| 8639 | CHECK(descriptor != nullptr); |
| 8640 | return descriptor; |
| 8641 | } |
| 8642 | |
Jeff Hao | 5872d7c | 2016-04-27 11:07:41 -0700 | [diff] [blame] | 8643 | jobject ClassLinker::CreatePathClassLoader(Thread* self, |
| 8644 | const std::vector<const DexFile*>& dex_files) { |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8645 | // SOAAlreadyRunnable is protected, and we need something to add a global reference. |
| 8646 | // We could move the jobject to the callers, but all call-sites do this... |
| 8647 | ScopedObjectAccessUnchecked soa(self); |
| 8648 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8649 | // 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] | 8650 | StackHandleScope<6> hs(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8651 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8652 | ArtField* dex_elements_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8653 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8654 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 8655 | 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] | 8656 | DCHECK(dex_elements_class != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8657 | DCHECK(dex_elements_class->IsArrayClass()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 8658 | Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements(hs.NewHandle( |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 8659 | mirror::ObjectArray<mirror::Object>::Alloc(self, |
| 8660 | dex_elements_class.Get(), |
| 8661 | dex_files.size()))); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8662 | Handle<mirror::Class> h_dex_element_class = |
| 8663 | hs.NewHandle(dex_elements_class->GetComponentType()); |
| 8664 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8665 | ArtField* element_file_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8666 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8667 | DCHECK_EQ(h_dex_element_class.Get(), element_file_field->GetDeclaringClass()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8668 | |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8669 | ArtField* cookie_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8670 | DCHECK_EQ(cookie_field->GetDeclaringClass(), element_file_field->GetType<false>()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8671 | |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8672 | ArtField* file_name_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_fileName); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 8673 | DCHECK_EQ(file_name_field->GetDeclaringClass(), element_file_field->GetType<false>()); |
| 8674 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8675 | // Fill the elements array. |
| 8676 | int32_t index = 0; |
| 8677 | for (const DexFile* dex_file : dex_files) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 8678 | StackHandleScope<4> hs2(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8679 | |
Mathieu Chartier | 966878d | 2016-01-14 14:33:29 -0800 | [diff] [blame] | 8680 | // CreatePathClassLoader is only used by gtests. Index 0 of h_long_array is supposed to be the |
| 8681 | // oat file but we can leave it null. |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 8682 | Handle<mirror::LongArray> h_long_array = hs2.NewHandle(mirror::LongArray::Alloc( |
| 8683 | self, |
| 8684 | kDexFileIndexStart + 1)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8685 | DCHECK(h_long_array != nullptr); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 8686 | h_long_array->Set(kDexFileIndexStart, reinterpret_cast<intptr_t>(dex_file)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8687 | |
Mathieu Chartier | 3738e98 | 2017-05-12 16:07:28 -0700 | [diff] [blame] | 8688 | // Note that this creates a finalizable dalvik.system.DexFile object and a corresponding |
| 8689 | // FinalizerReference which will never get cleaned up without a started runtime. |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8690 | Handle<mirror::Object> h_dex_file = hs2.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8691 | cookie_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8692 | DCHECK(h_dex_file != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8693 | cookie_field->SetObject<false>(h_dex_file.Get(), h_long_array.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8694 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 8695 | Handle<mirror::String> h_file_name = hs2.NewHandle( |
| 8696 | mirror::String::AllocFromModifiedUtf8(self, dex_file->GetLocation().c_str())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8697 | DCHECK(h_file_name != nullptr); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 8698 | file_name_field->SetObject<false>(h_dex_file.Get(), h_file_name.Get()); |
| 8699 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8700 | 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] | 8701 | DCHECK(h_element != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8702 | element_file_field->SetObject<false>(h_element.Get(), h_dex_file.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8703 | |
| 8704 | h_dex_elements->Set(index, h_element.Get()); |
| 8705 | index++; |
| 8706 | } |
| 8707 | DCHECK_EQ(index, h_dex_elements->GetLength()); |
| 8708 | |
| 8709 | // Create DexPathList. |
| 8710 | Handle<mirror::Object> h_dex_path_list = hs.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8711 | dex_elements_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8712 | DCHECK(h_dex_path_list != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8713 | // Set elements. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8714 | 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] | 8715 | |
| 8716 | // Create PathClassLoader. |
| 8717 | Handle<mirror::Class> h_path_class_class = hs.NewHandle( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 8718 | soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8719 | Handle<mirror::Object> h_path_class_loader = hs.NewHandle( |
| 8720 | h_path_class_class->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8721 | DCHECK(h_path_class_loader != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8722 | // Set DexPathList. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8723 | ArtField* path_list_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8724 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8725 | DCHECK(path_list_field != nullptr); |
| 8726 | 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] | 8727 | |
| 8728 | // Make a pretend boot-classpath. |
| 8729 | // TODO: Should we scan the image? |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8730 | ArtField* const parent_field = |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8731 | mirror::Class::FindField(self, |
| 8732 | h_path_class_loader->GetClass(), |
| 8733 | "parent", |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8734 | "Ljava/lang/ClassLoader;"); |
Roland Levillain | f39c9eb | 2015-05-26 15:02:07 +0100 | [diff] [blame] | 8735 | DCHECK(parent_field != nullptr); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 8736 | ObjPtr<mirror::Object> boot_cl = |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 8737 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader)->AllocObject(self); |
Mathieu Chartier | 966878d | 2016-01-14 14:33:29 -0800 | [diff] [blame] | 8738 | parent_field->SetObject<false>(h_path_class_loader.Get(), boot_cl); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8739 | |
| 8740 | // Make it a global ref and return. |
| 8741 | ScopedLocalRef<jobject> local_ref( |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 8742 | soa.Env(), soa.Env()->AddLocalReference<jobject>(h_path_class_loader.Get())); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8743 | return soa.Env()->NewGlobalRef(local_ref.get()); |
| 8744 | } |
| 8745 | |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 8746 | void ClassLinker::DropFindArrayClassCache() { |
| 8747 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
| 8748 | find_array_class_cache_next_victim_ = 0; |
| 8749 | } |
| 8750 | |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 8751 | void ClassLinker::ClearClassTableStrongRoots() const { |
| 8752 | Thread* const self = Thread::Current(); |
| 8753 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 8754 | for (const ClassLoaderData& data : class_loaders_) { |
| 8755 | if (data.class_table != nullptr) { |
| 8756 | data.class_table->ClearStrongRoots(); |
| 8757 | } |
| 8758 | } |
| 8759 | } |
| 8760 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 8761 | void ClassLinker::VisitClassLoaders(ClassLoaderVisitor* visitor) const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8762 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 8763 | for (const ClassLoaderData& data : class_loaders_) { |
Mathieu Chartier | 4843bd5 | 2015-10-01 17:08:44 -0700 | [diff] [blame] | 8764 | // 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] | 8765 | ObjPtr<mirror::ClassLoader> class_loader = ObjPtr<mirror::ClassLoader>::DownCast( |
| 8766 | self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8767 | if (class_loader != nullptr) { |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 8768 | visitor->Visit(class_loader.Ptr()); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8769 | } |
| 8770 | } |
| 8771 | } |
| 8772 | |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 8773 | void ClassLinker::InsertDexFileInToClassLoader(ObjPtr<mirror::Object> dex_file, |
| 8774 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8775 | DCHECK(dex_file != nullptr); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8776 | Thread* const self = Thread::Current(); |
| 8777 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 8778 | ClassTable* const table = ClassTableForClassLoader(class_loader.Ptr()); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8779 | DCHECK(table != nullptr); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 8780 | if (table->InsertStrongRoot(dex_file) && class_loader != nullptr) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8781 | // It was not already inserted, perform the write barrier to let the GC know the class loader's |
| 8782 | // class table was modified. |
| 8783 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader); |
| 8784 | } |
| 8785 | } |
| 8786 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 8787 | void ClassLinker::CleanupClassLoaders() { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8788 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8789 | std::vector<ClassLoaderData> to_delete; |
| 8790 | // Do the delete outside the lock to avoid lock violation in jit code cache. |
| 8791 | { |
| 8792 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 8793 | for (auto it = class_loaders_.begin(); it != class_loaders_.end(); ) { |
| 8794 | const ClassLoaderData& data = *it; |
| 8795 | // 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] | 8796 | ObjPtr<mirror::ClassLoader> class_loader = |
| 8797 | ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8798 | if (class_loader != nullptr) { |
| 8799 | ++it; |
| 8800 | } else { |
| 8801 | VLOG(class_linker) << "Freeing class loader"; |
| 8802 | to_delete.push_back(data); |
| 8803 | it = class_loaders_.erase(it); |
| 8804 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8805 | } |
| 8806 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8807 | for (ClassLoaderData& data : to_delete) { |
| 8808 | DeleteClassLoader(self, data); |
| 8809 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8810 | } |
| 8811 | |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 8812 | class GetResolvedClassesVisitor : public ClassVisitor { |
| 8813 | public: |
| 8814 | GetResolvedClassesVisitor(std::set<DexCacheResolvedClasses>* result, bool ignore_boot_classes) |
| 8815 | : result_(result), |
| 8816 | ignore_boot_classes_(ignore_boot_classes), |
| 8817 | last_resolved_classes_(result->end()), |
| 8818 | last_dex_file_(nullptr), |
| 8819 | vlog_is_on_(VLOG_IS_ON(class_linker)), |
| 8820 | extra_stats_(), |
| 8821 | last_extra_stats_(extra_stats_.end()) { } |
| 8822 | |
| 8823 | bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8824 | if (!klass->IsProxyClass() && |
| 8825 | !klass->IsArrayClass() && |
| 8826 | klass->IsResolved() && |
| 8827 | !klass->IsErroneousResolved() && |
| 8828 | (!ignore_boot_classes_ || klass->GetClassLoader() != nullptr)) { |
| 8829 | const DexFile& dex_file = klass->GetDexFile(); |
| 8830 | if (&dex_file != last_dex_file_) { |
| 8831 | last_dex_file_ = &dex_file; |
| 8832 | DexCacheResolvedClasses resolved_classes(dex_file.GetLocation(), |
| 8833 | dex_file.GetBaseLocation(), |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 8834 | dex_file.GetLocationChecksum(), |
| 8835 | dex_file.NumMethodIds()); |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 8836 | last_resolved_classes_ = result_->find(resolved_classes); |
| 8837 | if (last_resolved_classes_ == result_->end()) { |
| 8838 | last_resolved_classes_ = result_->insert(resolved_classes).first; |
| 8839 | } |
| 8840 | } |
| 8841 | bool added = last_resolved_classes_->AddClass(klass->GetDexTypeIndex()); |
| 8842 | if (UNLIKELY(vlog_is_on_) && added) { |
| 8843 | const DexCacheResolvedClasses* resolved_classes = std::addressof(*last_resolved_classes_); |
| 8844 | if (last_extra_stats_ == extra_stats_.end() || |
| 8845 | last_extra_stats_->first != resolved_classes) { |
| 8846 | last_extra_stats_ = extra_stats_.find(resolved_classes); |
| 8847 | if (last_extra_stats_ == extra_stats_.end()) { |
| 8848 | last_extra_stats_ = |
| 8849 | extra_stats_.emplace(resolved_classes, ExtraStats(dex_file.NumClassDefs())).first; |
| 8850 | } |
| 8851 | } |
| 8852 | } |
| 8853 | } |
| 8854 | return true; |
| 8855 | } |
| 8856 | |
| 8857 | void PrintStatistics() const { |
| 8858 | if (vlog_is_on_) { |
| 8859 | for (const DexCacheResolvedClasses& resolved_classes : *result_) { |
| 8860 | auto it = extra_stats_.find(std::addressof(resolved_classes)); |
| 8861 | DCHECK(it != extra_stats_.end()); |
| 8862 | const ExtraStats& extra_stats = it->second; |
| 8863 | LOG(INFO) << "Dex location " << resolved_classes.GetDexLocation() |
| 8864 | << " has " << resolved_classes.GetClasses().size() << " / " |
| 8865 | << extra_stats.number_of_class_defs_ << " resolved classes"; |
| 8866 | } |
| 8867 | } |
| 8868 | } |
| 8869 | |
| 8870 | private: |
| 8871 | struct ExtraStats { |
| 8872 | explicit ExtraStats(uint32_t number_of_class_defs) |
| 8873 | : number_of_class_defs_(number_of_class_defs) {} |
| 8874 | uint32_t number_of_class_defs_; |
| 8875 | }; |
| 8876 | |
| 8877 | std::set<DexCacheResolvedClasses>* result_; |
| 8878 | bool ignore_boot_classes_; |
| 8879 | std::set<DexCacheResolvedClasses>::iterator last_resolved_classes_; |
| 8880 | const DexFile* last_dex_file_; |
| 8881 | |
| 8882 | // Statistics. |
| 8883 | bool vlog_is_on_; |
| 8884 | std::map<const DexCacheResolvedClasses*, ExtraStats> extra_stats_; |
| 8885 | std::map<const DexCacheResolvedClasses*, ExtraStats>::iterator last_extra_stats_; |
| 8886 | }; |
| 8887 | |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8888 | std::set<DexCacheResolvedClasses> ClassLinker::GetResolvedClasses(bool ignore_boot_classes) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 8889 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8890 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 8891 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8892 | std::set<DexCacheResolvedClasses> ret; |
| 8893 | VLOG(class_linker) << "Collecting resolved classes"; |
| 8894 | const uint64_t start_time = NanoTime(); |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 8895 | GetResolvedClassesVisitor visitor(&ret, ignore_boot_classes); |
| 8896 | VisitClasses(&visitor); |
| 8897 | if (VLOG_IS_ON(class_linker)) { |
| 8898 | visitor.PrintStatistics(); |
| 8899 | LOG(INFO) << "Collecting class profile took " << PrettyDuration(NanoTime() - start_time); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8900 | } |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8901 | return ret; |
| 8902 | } |
| 8903 | |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 8904 | std::unordered_set<std::string> ClassLinker::GetClassDescriptorsForResolvedClasses( |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8905 | const std::set<DexCacheResolvedClasses>& classes) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 8906 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8907 | std::unordered_set<std::string> ret; |
| 8908 | Thread* const self = Thread::Current(); |
| 8909 | std::unordered_map<std::string, const DexFile*> location_to_dex_file; |
| 8910 | ScopedObjectAccess soa(self); |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 8911 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 8912 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8913 | for (const ClassLinker::DexCacheData& data : GetDexCachesData()) { |
| 8914 | if (!self->IsJWeakCleared(data.weak_root)) { |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 8915 | ObjPtr<mirror::DexCache> dex_cache = soa.Decode<mirror::DexCache>(data.weak_root); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8916 | if (dex_cache != nullptr) { |
| 8917 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 8918 | // 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] | 8919 | location_to_dex_file.emplace(dex_file->GetLocation(), dex_file); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8920 | } |
| 8921 | } |
| 8922 | } |
| 8923 | for (const DexCacheResolvedClasses& info : classes) { |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 8924 | const std::string& location = info.GetDexLocation(); |
| 8925 | auto found = location_to_dex_file.find(location); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8926 | if (found != location_to_dex_file.end()) { |
| 8927 | const DexFile* dex_file = found->second; |
| 8928 | VLOG(profiler) << "Found opened dex file for " << dex_file->GetLocation() << " with " |
| 8929 | << info.GetClasses().size() << " classes"; |
| 8930 | DCHECK_EQ(dex_file->GetLocationChecksum(), info.GetLocationChecksum()); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 8931 | for (dex::TypeIndex type_idx : info.GetClasses()) { |
Calin Juravle | 0855688 | 2017-05-26 16:40:45 -0700 | [diff] [blame] | 8932 | if (!dex_file->IsTypeIndexValid(type_idx)) { |
| 8933 | // Something went bad. The profile is probably corrupted. Abort and return an emtpy set. |
| 8934 | LOG(WARNING) << "Corrupted profile: invalid type index " |
| 8935 | << type_idx.index_ << " in dex " << location; |
| 8936 | return std::unordered_set<std::string>(); |
| 8937 | } |
Jeff Hao | 54b5855 | 2016-11-16 15:15:04 -0800 | [diff] [blame] | 8938 | const DexFile::TypeId& type_id = dex_file->GetTypeId(type_idx); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8939 | const char* descriptor = dex_file->GetTypeDescriptor(type_id); |
| 8940 | ret.insert(descriptor); |
| 8941 | } |
| 8942 | } else { |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 8943 | VLOG(class_linker) << "Failed to find opened dex file for location " << location; |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8944 | } |
| 8945 | } |
| 8946 | return ret; |
| 8947 | } |
| 8948 | |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8949 | class ClassLinker::FindVirtualMethodHolderVisitor : public ClassVisitor { |
| 8950 | public: |
| 8951 | FindVirtualMethodHolderVisitor(const ArtMethod* method, PointerSize pointer_size) |
| 8952 | : method_(method), |
| 8953 | pointer_size_(pointer_size) {} |
| 8954 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8955 | bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8956 | if (klass->GetVirtualMethodsSliceUnchecked(pointer_size_).Contains(method_)) { |
| 8957 | holder_ = klass; |
| 8958 | } |
| 8959 | // Return false to stop searching if holder_ is not null. |
| 8960 | return holder_ == nullptr; |
| 8961 | } |
| 8962 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8963 | ObjPtr<mirror::Class> holder_ = nullptr; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8964 | const ArtMethod* const method_; |
| 8965 | const PointerSize pointer_size_; |
| 8966 | }; |
| 8967 | |
| 8968 | mirror::Class* ClassLinker::GetHoldingClassOfCopiedMethod(ArtMethod* method) { |
| 8969 | ScopedTrace trace(__FUNCTION__); // Since this function is slow, have a trace to notify people. |
| 8970 | CHECK(method->IsCopied()); |
| 8971 | FindVirtualMethodHolderVisitor visitor(method, image_pointer_size_); |
| 8972 | VisitClasses(&visitor); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8973 | return visitor.holder_.Ptr(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8974 | } |
| 8975 | |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 8976 | mirror::IfTable* ClassLinker::AllocIfTable(Thread* self, size_t ifcount) { |
| 8977 | return down_cast<mirror::IfTable*>( |
| 8978 | mirror::IfTable::Alloc(self, |
| 8979 | GetClassRoot(kObjectArrayClass), |
| 8980 | ifcount * mirror::IfTable::kMax)); |
| 8981 | } |
| 8982 | |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 8983 | // Instantiate ResolveMethod. |
| 8984 | template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::kForceICCECheck>( |
| 8985 | const DexFile& dex_file, |
| 8986 | uint32_t method_idx, |
| 8987 | Handle<mirror::DexCache> dex_cache, |
| 8988 | Handle<mirror::ClassLoader> class_loader, |
| 8989 | ArtMethod* referrer, |
| 8990 | InvokeType type); |
| 8991 | template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::kNoICCECheckForCache>( |
| 8992 | const DexFile& dex_file, |
| 8993 | uint32_t method_idx, |
| 8994 | Handle<mirror::DexCache> dex_cache, |
| 8995 | Handle<mirror::ClassLoader> class_loader, |
| 8996 | ArtMethod* referrer, |
| 8997 | InvokeType type); |
| 8998 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8999 | } // namespace art |