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 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 19 | #include <unistd.h> |
| 20 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 21 | #include <algorithm> |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 22 | #include <deque> |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 23 | #include <iostream> |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 24 | #include <map> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 25 | #include <memory> |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 26 | #include <queue> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 27 | #include <string> |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 28 | #include <tuple> |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 29 | #include <unordered_map> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 30 | #include <utility> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 31 | #include <vector> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 32 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 33 | #include "android-base/stringprintf.h" |
| 34 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 35 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 36 | #include "art_method-inl.h" |
| 37 | #include "base/arena_allocator.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 38 | #include "base/casts.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 39 | #include "base/logging.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 40 | #include "base/scoped_arena_containers.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 41 | #include "base/scoped_flock.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 42 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 43 | #include "base/systrace.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 44 | #include "base/time_utils.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 45 | #include "base/unix_file/fd_file.h" |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 46 | #include "base/value_object.h" |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 47 | #include "cha.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 48 | #include "class_linker-inl.h" |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 49 | #include "class_loader_utils.h" |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 50 | #include "class_table-inl.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 51 | #include "compiler_callbacks.h" |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 52 | #include "debugger.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 53 | #include "dex_file-inl.h" |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame^] | 54 | #include "dex_file_loader.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 55 | #include "entrypoints/entrypoint_utils.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 56 | #include "entrypoints/runtime_asm_entrypoints.h" |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 57 | #include "experimental_flags.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 58 | #include "gc/accounting/card_table-inl.h" |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 59 | #include "gc/accounting/heap_bitmap-inl.h" |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 60 | #include "gc/accounting/space_bitmap-inl.h" |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 61 | #include "gc/heap-visit-objects-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 62 | #include "gc/heap.h" |
Mathieu Chartier | 1b1e31f | 2016-05-19 10:13:04 -0700 | [diff] [blame] | 63 | #include "gc/scoped_gc_critical_section.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 64 | #include "gc/space/image_space.h" |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 65 | #include "gc/space/space-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 66 | #include "gc_root-inl.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 67 | #include "handle_scope-inl.h" |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 68 | #include "image-inl.h" |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 69 | #include "imt_conflict_table.h" |
| 70 | #include "imtable-inl.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 71 | #include "intern_table.h" |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 72 | #include "interpreter/interpreter.h" |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 73 | #include "java_vm_ext.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 74 | #include "jit/jit.h" |
| 75 | #include "jit/jit_code_cache.h" |
Calin Juravle | 33083d6 | 2017-01-18 15:29:12 -0800 | [diff] [blame] | 76 | #include "jit/profile_compilation_info.h" |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 77 | #include "jni_internal.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 78 | #include "leb128.h" |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 79 | #include "linear_alloc.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 80 | #include "mirror/call_site.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 81 | #include "mirror/class-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 82 | #include "mirror/class.h" |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 83 | #include "mirror/class_ext.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 84 | #include "mirror/class_loader.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 85 | #include "mirror/dex_cache-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 86 | #include "mirror/dex_cache.h" |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 87 | #include "mirror/emulated_stack_frame.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 88 | #include "mirror/field.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 89 | #include "mirror/iftable-inl.h" |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 90 | #include "mirror/method.h" |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 91 | #include "mirror/method_handle_impl.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 92 | #include "mirror/method_handles_lookup.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 93 | #include "mirror/method_type.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 94 | #include "mirror/object-inl.h" |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 95 | #include "mirror/object-refvisitor-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 96 | #include "mirror/object_array-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 97 | #include "mirror/proxy.h" |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 98 | #include "mirror/reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 99 | #include "mirror/stack_trace_element.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 100 | #include "mirror/string-inl.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 101 | #include "native/dalvik_system_DexFile.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 102 | #include "nativehelper/ScopedLocalRef.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 103 | #include "oat.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 104 | #include "oat_file-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 105 | #include "oat_file.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 106 | #include "oat_file_assistant.h" |
| 107 | #include "oat_file_manager.h" |
| 108 | #include "object_lock.h" |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 109 | #include "os.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 110 | #include "runtime.h" |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 111 | #include "runtime_callbacks.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 112 | #include "scoped_thread_state_change-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 113 | #include "thread-inl.h" |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 114 | #include "thread_list.h" |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 115 | #include "trace.h" |
Andreas Gampe | 2ff3b97 | 2017-06-05 18:14:53 -0700 | [diff] [blame] | 116 | #include "utf.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 117 | #include "utils.h" |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 118 | #include "utils/dex_cache_arrays_layout-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 119 | #include "verifier/method_verifier.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 120 | #include "well_known_classes.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 121 | |
| 122 | namespace art { |
| 123 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 124 | using android::base::StringPrintf; |
| 125 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 126 | static constexpr bool kSanityCheckObjects = kIsDebugBuild; |
Mathieu Chartier | 8790c7f | 2016-03-31 15:05:45 -0700 | [diff] [blame] | 127 | static constexpr bool kVerifyArtMethodDeclaringClasses = kIsDebugBuild; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 128 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 129 | static void ThrowNoClassDefFoundError(const char* fmt, ...) |
| 130 | __attribute__((__format__(__printf__, 1, 2))) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 131 | REQUIRES_SHARED(Locks::mutator_lock_); |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 132 | static void ThrowNoClassDefFoundError(const char* fmt, ...) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 133 | va_list args; |
| 134 | va_start(args, fmt); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 135 | Thread* self = Thread::Current(); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 136 | self->ThrowNewExceptionV("Ljava/lang/NoClassDefFoundError;", fmt, args); |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 137 | va_end(args); |
| 138 | } |
| 139 | |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 140 | static bool HasInitWithString(Thread* self, ClassLinker* class_linker, const char* descriptor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 141 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 142 | ArtMethod* method = self->GetCurrentMethod(nullptr); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 143 | StackHandleScope<1> hs(self); |
| 144 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(method != nullptr ? |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 145 | method->GetDeclaringClass()->GetClassLoader() : nullptr)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 146 | ObjPtr<mirror::Class> exception_class = class_linker->FindClass(self, descriptor, class_loader); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 147 | |
| 148 | if (exception_class == nullptr) { |
| 149 | // No exc class ~ no <init>-with-string. |
| 150 | CHECK(self->IsExceptionPending()); |
| 151 | self->ClearException(); |
| 152 | return false; |
| 153 | } |
| 154 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 155 | ArtMethod* exception_init_method = exception_class->FindConstructor( |
| 156 | "(Ljava/lang/String;)V", class_linker->GetImagePointerSize()); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 157 | return exception_init_method != nullptr; |
| 158 | } |
| 159 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 160 | static mirror::Object* GetVerifyError(ObjPtr<mirror::Class> c) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 161 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 162 | ObjPtr<mirror::ClassExt> ext(c->GetExtData()); |
| 163 | if (ext == nullptr) { |
| 164 | return nullptr; |
| 165 | } else { |
| 166 | return ext->GetVerifyError(); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | // Helper for ThrowEarlierClassFailure. Throws the stored error. |
| 171 | static void HandleEarlierVerifyError(Thread* self, |
| 172 | ClassLinker* class_linker, |
| 173 | ObjPtr<mirror::Class> c) |
| 174 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 175 | ObjPtr<mirror::Object> obj = GetVerifyError(c); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 176 | DCHECK(obj != nullptr); |
| 177 | self->AssertNoPendingException(); |
| 178 | if (obj->IsClass()) { |
| 179 | // Previous error has been stored as class. Create a new exception of that type. |
| 180 | |
| 181 | // It's possible the exception doesn't have a <init>(String). |
| 182 | std::string temp; |
| 183 | const char* descriptor = obj->AsClass()->GetDescriptor(&temp); |
| 184 | |
| 185 | if (HasInitWithString(self, class_linker, descriptor)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 186 | self->ThrowNewException(descriptor, c->PrettyDescriptor().c_str()); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 187 | } else { |
| 188 | self->ThrowNewException(descriptor, nullptr); |
| 189 | } |
| 190 | } else { |
| 191 | // Previous error has been stored as an instance. Just rethrow. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 192 | ObjPtr<mirror::Class> throwable_class = |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 193 | self->DecodeJObject(WellKnownClasses::java_lang_Throwable)->AsClass(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 194 | ObjPtr<mirror::Class> error_class = obj->GetClass(); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 195 | CHECK(throwable_class->IsAssignableFrom(error_class)); |
| 196 | self->SetException(obj->AsThrowable()); |
| 197 | } |
| 198 | self->AssertPendingException(); |
| 199 | } |
| 200 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 201 | 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] | 202 | // The class failed to initialize on a previous attempt, so we want to throw |
| 203 | // a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we |
| 204 | // failed in verification, in which case v2 5.4.1 says we need to re-throw |
| 205 | // the previous error. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 206 | Runtime* const runtime = Runtime::Current(); |
| 207 | if (!runtime->IsAotCompiler()) { // Give info if this occurs at runtime. |
Andreas Gampe | 3d6b470 | 2015-09-21 08:35:52 -0700 | [diff] [blame] | 208 | std::string extra; |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 209 | if (GetVerifyError(c) != nullptr) { |
| 210 | ObjPtr<mirror::Object> verify_error = GetVerifyError(c); |
Andreas Gampe | 369c851 | 2016-01-28 15:31:39 -0800 | [diff] [blame] | 211 | if (verify_error->IsClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 212 | extra = mirror::Class::PrettyDescriptor(verify_error->AsClass()); |
Andreas Gampe | 369c851 | 2016-01-28 15:31:39 -0800 | [diff] [blame] | 213 | } else { |
| 214 | extra = verify_error->AsThrowable()->Dump(); |
| 215 | } |
Andreas Gampe | 3d6b470 | 2015-09-21 08:35:52 -0700 | [diff] [blame] | 216 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 217 | LOG(INFO) << "Rejecting re-init on previously-failed class " << c->PrettyClass() |
| 218 | << ": " << extra; |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 219 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 220 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 221 | CHECK(c->IsErroneous()) << c->PrettyClass() << " " << c->GetStatus(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 222 | Thread* self = Thread::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 223 | if (runtime->IsAotCompiler()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 224 | // At compile time, accurate errors and NCDFE are disabled to speed compilation. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 225 | ObjPtr<mirror::Throwable> pre_allocated = runtime->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 226 | self->SetException(pre_allocated); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 227 | } else { |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 228 | if (GetVerifyError(c) != nullptr) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 229 | // Rethrow stored error. |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 230 | HandleEarlierVerifyError(self, this, c); |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 231 | } |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 232 | // TODO This might be wrong if we hit an OOME while allocating the ClassExt. In that case we |
| 233 | // might have meant to go down the earlier if statement with the original error but it got |
| 234 | // swallowed by the OOM so we end up here. |
| 235 | if (GetVerifyError(c) == nullptr || wrap_in_no_class_def) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 236 | // If there isn't a recorded earlier error, or this is a repeat throw from initialization, |
| 237 | // the top-level exception must be a NoClassDefFoundError. The potentially already pending |
| 238 | // exception will be a cause. |
| 239 | self->ThrowNewWrappedException("Ljava/lang/NoClassDefFoundError;", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 240 | c->PrettyDescriptor().c_str()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 241 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 245 | static void VlogClassInitializationFailure(Handle<mirror::Class> klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 246 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 247 | if (VLOG_IS_ON(class_linker)) { |
| 248 | std::string temp; |
| 249 | LOG(INFO) << "Failed to initialize class " << klass->GetDescriptor(&temp) << " from " |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 250 | << klass->GetLocation() << "\n" << Thread::Current()->GetException()->Dump(); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
| 254 | static void WrapExceptionInInitializer(Handle<mirror::Class> klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 255 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 256 | Thread* self = Thread::Current(); |
| 257 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 258 | |
| 259 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 260 | CHECK(cause.get() != nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 261 | |
Andreas Gampe | 1e8a395 | 2016-11-30 10:13:19 -0800 | [diff] [blame] | 262 | // Boot classpath classes should not fail initialization. This is a sanity debug check. This |
| 263 | // cannot in general be guaranteed, but in all likelihood leads to breakage down the line. |
| 264 | if (klass->GetClassLoader() == nullptr && !Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | 22f71d2 | 2016-11-21 10:10:08 -0800 | [diff] [blame] | 265 | std::string tmp; |
Andreas Gampe | 1e8a395 | 2016-11-30 10:13:19 -0800 | [diff] [blame] | 266 | LOG(kIsDebugBuild ? FATAL : WARNING) << klass->GetDescriptor(&tmp) << " failed initialization"; |
Andreas Gampe | 22f71d2 | 2016-11-21 10:10:08 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 269 | env->ExceptionClear(); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 270 | bool is_error = env->IsInstanceOf(cause.get(), WellKnownClasses::java_lang_Error); |
| 271 | env->Throw(cause.get()); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 272 | |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 273 | // We only wrap non-Error exceptions; an Error can just be used as-is. |
| 274 | if (!is_error) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 275 | self->ThrowNewWrappedException("Ljava/lang/ExceptionInInitializerError;", nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 276 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 277 | VlogClassInitializationFailure(klass); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 280 | // Gap between two fields in object layout. |
| 281 | struct FieldGap { |
| 282 | uint32_t start_offset; // The offset from the start of the object. |
| 283 | uint32_t size; // The gap size of 1, 2, or 4 bytes. |
| 284 | }; |
| 285 | struct FieldGapsComparator { |
| 286 | explicit FieldGapsComparator() { |
| 287 | } |
| 288 | bool operator() (const FieldGap& lhs, const FieldGap& rhs) |
| 289 | NO_THREAD_SAFETY_ANALYSIS { |
Andreas Gampe | f52857f | 2015-02-18 15:38:57 -0800 | [diff] [blame] | 290 | // Sort by gap size, largest first. Secondary sort by starting offset. |
Richard Uhler | fab6788 | 2015-07-13 17:00:35 -0700 | [diff] [blame] | 291 | // Note that the priority queue returns the largest element, so operator() |
| 292 | // should return true if lhs is less than rhs. |
| 293 | 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] | 294 | } |
| 295 | }; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 296 | typedef std::priority_queue<FieldGap, std::vector<FieldGap>, FieldGapsComparator> FieldGaps; |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 297 | |
| 298 | // Adds largest aligned gaps to queue of gaps. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 299 | 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] | 300 | DCHECK(gaps != nullptr); |
| 301 | |
| 302 | uint32_t current_offset = gap_start; |
| 303 | while (current_offset != gap_end) { |
| 304 | size_t remaining = gap_end - current_offset; |
| 305 | if (remaining >= sizeof(uint32_t) && IsAligned<4>(current_offset)) { |
| 306 | gaps->push(FieldGap {current_offset, sizeof(uint32_t)}); |
| 307 | current_offset += sizeof(uint32_t); |
| 308 | } else if (remaining >= sizeof(uint16_t) && IsAligned<2>(current_offset)) { |
| 309 | gaps->push(FieldGap {current_offset, sizeof(uint16_t)}); |
| 310 | current_offset += sizeof(uint16_t); |
| 311 | } else { |
| 312 | gaps->push(FieldGap {current_offset, sizeof(uint8_t)}); |
| 313 | current_offset += sizeof(uint8_t); |
| 314 | } |
| 315 | DCHECK_LE(current_offset, gap_end) << "Overran gap"; |
| 316 | } |
| 317 | } |
| 318 | // Shuffle fields forward, making use of gaps whenever possible. |
| 319 | template<int n> |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 320 | static void ShuffleForward(size_t* current_field_idx, |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 321 | MemberOffset* field_offset, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 322 | std::deque<ArtField*>* grouped_and_sorted_fields, |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 323 | FieldGaps* gaps) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 324 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 325 | DCHECK(current_field_idx != nullptr); |
| 326 | DCHECK(grouped_and_sorted_fields != nullptr); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 327 | DCHECK(gaps != nullptr); |
| 328 | DCHECK(field_offset != nullptr); |
| 329 | |
| 330 | DCHECK(IsPowerOfTwo(n)); |
| 331 | while (!grouped_and_sorted_fields->empty()) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 332 | ArtField* field = grouped_and_sorted_fields->front(); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 333 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 334 | if (Primitive::ComponentSize(type) < n) { |
| 335 | break; |
| 336 | } |
| 337 | if (!IsAligned<n>(field_offset->Uint32Value())) { |
| 338 | MemberOffset old_offset = *field_offset; |
| 339 | *field_offset = MemberOffset(RoundUp(field_offset->Uint32Value(), n)); |
| 340 | AddFieldGap(old_offset.Uint32Value(), field_offset->Uint32Value(), gaps); |
| 341 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 342 | CHECK(type != Primitive::kPrimNot) << field->PrettyField(); // should be primitive types |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 343 | grouped_and_sorted_fields->pop_front(); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 344 | if (!gaps->empty() && gaps->top().size >= n) { |
| 345 | FieldGap gap = gaps->top(); |
| 346 | gaps->pop(); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 347 | DCHECK_ALIGNED(gap.start_offset, n); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 348 | field->SetOffset(MemberOffset(gap.start_offset)); |
| 349 | if (gap.size > n) { |
| 350 | AddFieldGap(gap.start_offset + n, gap.start_offset + gap.size, gaps); |
| 351 | } |
| 352 | } else { |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 353 | DCHECK_ALIGNED(field_offset->Uint32Value(), n); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 354 | field->SetOffset(*field_offset); |
| 355 | *field_offset = MemberOffset(field_offset->Uint32Value() + n); |
| 356 | } |
| 357 | ++(*current_field_idx); |
| 358 | } |
| 359 | } |
| 360 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 361 | ClassLinker::ClassLinker(InternTable* intern_table) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 362 | : boot_class_table_(new ClassTable()), |
| 363 | failed_dex_cache_class_lookups_(0), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 364 | class_roots_(nullptr), |
| 365 | array_iftable_(nullptr), |
| 366 | find_array_class_cache_next_victim_(0), |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 367 | init_done_(false), |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 368 | log_new_roots_(false), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 369 | intern_table_(intern_table), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 370 | quick_resolution_trampoline_(nullptr), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 371 | quick_imt_conflict_trampoline_(nullptr), |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 372 | quick_generic_jni_trampoline_(nullptr), |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 373 | quick_to_interpreter_bridge_trampoline_(nullptr), |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 374 | image_pointer_size_(kRuntimePointerSize), |
Andreas Gampe | 7dface3 | 2017-07-25 21:32:59 -0700 | [diff] [blame] | 375 | cha_(Runtime::Current()->IsAotCompiler() ? nullptr : new ClassHierarchyAnalysis()) { |
| 376 | // For CHA disabled during Aot, see b/34193647. |
| 377 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 378 | CHECK(intern_table_ != nullptr); |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 379 | static_assert(kFindArrayCacheSize == arraysize(find_array_class_cache_), |
| 380 | "Array cache size wrong."); |
| 381 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 382 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 383 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 384 | void ClassLinker::CheckSystemClass(Thread* self, Handle<mirror::Class> c1, const char* descriptor) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 385 | ObjPtr<mirror::Class> c2 = FindSystemClass(self, descriptor); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 386 | if (c2 == nullptr) { |
| 387 | LOG(FATAL) << "Could not find class " << descriptor; |
| 388 | UNREACHABLE(); |
| 389 | } |
| 390 | if (c1.Get() != c2) { |
| 391 | std::ostringstream os1, os2; |
| 392 | c1->DumpClass(os1, mirror::Class::kDumpClassFullDetail); |
| 393 | c2->DumpClass(os2, mirror::Class::kDumpClassFullDetail); |
| 394 | LOG(FATAL) << "InitWithoutImage: Class mismatch for " << descriptor |
| 395 | << ". This is most likely the result of a broken build. Make sure that " |
| 396 | << "libcore and art projects match.\n\n" |
| 397 | << os1.str() << "\n\n" << os2.str(); |
| 398 | UNREACHABLE(); |
| 399 | } |
| 400 | } |
| 401 | |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 402 | bool ClassLinker::InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path, |
| 403 | std::string* error_msg) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 404 | VLOG(startup) << "ClassLinker::Init"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 405 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 406 | Thread* const self = Thread::Current(); |
| 407 | Runtime* const runtime = Runtime::Current(); |
| 408 | gc::Heap* const heap = runtime->GetHeap(); |
| 409 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 410 | CHECK(!heap->HasBootImageSpace()) << "Runtime has image. We should use it."; |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 411 | CHECK(!init_done_); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 412 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 413 | // Use the pointer size from the runtime since we are probably creating the image. |
| 414 | image_pointer_size_ = InstructionSetPointerSize(runtime->GetInstructionSet()); |
| 415 | |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 416 | // java_lang_Class comes first, it's needed for AllocClass |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 417 | // 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] | 418 | heap->IncrementDisableMovingGC(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 419 | StackHandleScope<64> hs(self); // 64 is picked arbitrarily. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 420 | auto class_class_size = mirror::Class::ClassClassSize(image_pointer_size_); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 421 | Handle<mirror::Class> java_lang_Class(hs.NewHandle(down_cast<mirror::Class*>( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 422 | heap->AllocNonMovableObject<true>(self, nullptr, class_class_size, VoidFunctor())))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 423 | CHECK(java_lang_Class != nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 424 | mirror::Class::SetClassClass(java_lang_Class.Get()); |
| 425 | java_lang_Class->SetClass(java_lang_Class.Get()); |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 426 | if (kUseBakerReadBarrier) { |
| 427 | java_lang_Class->AssertReadBarrierState(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 428 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 429 | java_lang_Class->SetClassSize(class_class_size); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 430 | java_lang_Class->SetPrimitiveType(Primitive::kPrimNot); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 431 | heap->DecrementDisableMovingGC(self); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 432 | // AllocClass(ObjPtr<mirror::Class>) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 433 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 434 | // Class[] is used for reflection support. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 435 | 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] | 436 | Handle<mirror::Class> class_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 437 | AllocClass(self, java_lang_Class.Get(), class_array_class_size))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 438 | class_array_class->SetComponentType(java_lang_Class.Get()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 439 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 440 | // 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] | 441 | Handle<mirror::Class> java_lang_Object(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 442 | AllocClass(self, java_lang_Class.Get(), mirror::Object::ClassSize(image_pointer_size_)))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 443 | CHECK(java_lang_Object != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 444 | // backfill Object as the super class of Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 445 | java_lang_Class->SetSuperClass(java_lang_Object.Get()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 446 | mirror::Class::SetStatus(java_lang_Object, mirror::Class::kStatusLoaded, self); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 447 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 448 | java_lang_Object->SetObjectSize(sizeof(mirror::Object)); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 449 | // Allocate in non-movable so that it's possible to check if a JNI weak global ref has been |
| 450 | // cleared without triggering the read barrier and unintentionally mark the sentinel alive. |
| 451 | runtime->SetSentinel(heap->AllocNonMovableObject<true>(self, |
| 452 | java_lang_Object.Get(), |
| 453 | java_lang_Object->GetObjectSize(), |
| 454 | VoidFunctor())); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 455 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 456 | // Object[] next to hold class roots. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 457 | Handle<mirror::Class> object_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 458 | AllocClass(self, java_lang_Class.Get(), |
| 459 | mirror::ObjectArray<mirror::Object>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 460 | object_array_class->SetComponentType(java_lang_Object.Get()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 461 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 462 | // Setup the char (primitive) class to be used for char[]. |
| 463 | Handle<mirror::Class> char_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 464 | AllocClass(self, java_lang_Class.Get(), |
| 465 | mirror::Class::PrimitiveClassSize(image_pointer_size_)))); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 466 | // The primitive char class won't be initialized by |
| 467 | // InitializePrimitiveClass until line 459, but strings (and |
| 468 | // internal char arrays) will be allocated before that and the |
| 469 | // component size, which is computed from the primitive type, needs |
| 470 | // to be set here. |
| 471 | char_class->SetPrimitiveType(Primitive::kPrimChar); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 472 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 473 | // Setup the char[] class to be used for String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 474 | Handle<mirror::Class> char_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 475 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 476 | char_array_class->SetComponentType(char_class.Get()); |
| 477 | mirror::CharArray::SetArrayClass(char_array_class.Get()); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 478 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 479 | // Setup String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 480 | Handle<mirror::Class> java_lang_String(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 481 | AllocClass(self, java_lang_Class.Get(), mirror::String::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 482 | java_lang_String->SetStringClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 483 | mirror::String::SetClass(java_lang_String.Get()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 484 | mirror::Class::SetStatus(java_lang_String, mirror::Class::kStatusResolved, self); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 485 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 486 | // Setup java.lang.ref.Reference. |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 487 | Handle<mirror::Class> java_lang_ref_Reference(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 488 | AllocClass(self, java_lang_Class.Get(), mirror::Reference::ClassSize(image_pointer_size_)))); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 489 | mirror::Reference::SetClass(java_lang_ref_Reference.Get()); |
| 490 | java_lang_ref_Reference->SetObjectSize(mirror::Reference::InstanceSize()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 491 | mirror::Class::SetStatus(java_lang_ref_Reference, mirror::Class::kStatusResolved, self); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 492 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 493 | // 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] | 494 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 495 | mirror::ObjectArray<mirror::Class>::Alloc(self, object_array_class.Get(), |
| 496 | kClassRootsMax)); |
| 497 | CHECK(!class_roots_.IsNull()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 498 | SetClassRoot(kJavaLangClass, java_lang_Class.Get()); |
| 499 | SetClassRoot(kJavaLangObject, java_lang_Object.Get()); |
| 500 | SetClassRoot(kClassArrayClass, class_array_class.Get()); |
| 501 | SetClassRoot(kObjectArrayClass, object_array_class.Get()); |
| 502 | SetClassRoot(kCharArrayClass, char_array_class.Get()); |
| 503 | SetClassRoot(kJavaLangString, java_lang_String.Get()); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 504 | SetClassRoot(kJavaLangRefReference, java_lang_ref_Reference.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 505 | |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 506 | // Fill in the empty iftable. Needs to be done after the kObjectArrayClass root is set. |
| 507 | java_lang_Object->SetIfTable(AllocIfTable(self, 0)); |
| 508 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 509 | // Setup the primitive type classes. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 510 | SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass(self, Primitive::kPrimBoolean)); |
| 511 | SetClassRoot(kPrimitiveByte, CreatePrimitiveClass(self, Primitive::kPrimByte)); |
| 512 | SetClassRoot(kPrimitiveShort, CreatePrimitiveClass(self, Primitive::kPrimShort)); |
| 513 | SetClassRoot(kPrimitiveInt, CreatePrimitiveClass(self, Primitive::kPrimInt)); |
| 514 | SetClassRoot(kPrimitiveLong, CreatePrimitiveClass(self, Primitive::kPrimLong)); |
| 515 | SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass(self, Primitive::kPrimFloat)); |
| 516 | SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass(self, Primitive::kPrimDouble)); |
| 517 | SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass(self, Primitive::kPrimVoid)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 518 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 519 | // Create array interface entries to populate once we can load system classes. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 520 | array_iftable_ = GcRoot<mirror::IfTable>(AllocIfTable(self, 2)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 521 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 522 | // Create int array type for AllocDexCache (done in AppendToBootClassPath). |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 523 | Handle<mirror::Class> int_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 524 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize(image_pointer_size_)))); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 525 | int_array_class->SetComponentType(GetClassRoot(kPrimitiveInt)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 526 | mirror::IntArray::SetArrayClass(int_array_class.Get()); |
| 527 | SetClassRoot(kIntArrayClass, int_array_class.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 528 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 529 | // Create long array type for AllocDexCache (done in AppendToBootClassPath). |
| 530 | Handle<mirror::Class> long_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 531 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 532 | long_array_class->SetComponentType(GetClassRoot(kPrimitiveLong)); |
| 533 | mirror::LongArray::SetArrayClass(long_array_class.Get()); |
| 534 | SetClassRoot(kLongArrayClass, long_array_class.Get()); |
| 535 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 536 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 537 | |
Ian Rogers | 52813c9 | 2012-10-11 11:50:38 -0700 | [diff] [blame] | 538 | // Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 539 | Handle<mirror::Class> java_lang_DexCache(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 540 | AllocClass(self, java_lang_Class.Get(), mirror::DexCache::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 541 | SetClassRoot(kJavaLangDexCache, java_lang_DexCache.Get()); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 542 | java_lang_DexCache->SetDexCacheClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 543 | java_lang_DexCache->SetObjectSize(mirror::DexCache::InstanceSize()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 544 | mirror::Class::SetStatus(java_lang_DexCache, mirror::Class::kStatusResolved, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 545 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 546 | |
| 547 | // Setup dalvik.system.ClassExt |
| 548 | Handle<mirror::Class> dalvik_system_ClassExt(hs.NewHandle( |
| 549 | AllocClass(self, java_lang_Class.Get(), mirror::ClassExt::ClassSize(image_pointer_size_)))); |
| 550 | SetClassRoot(kDalvikSystemClassExt, dalvik_system_ClassExt.Get()); |
| 551 | mirror::ClassExt::SetClass(dalvik_system_ClassExt.Get()); |
| 552 | mirror::Class::SetStatus(dalvik_system_ClassExt, mirror::Class::kStatusResolved, self); |
| 553 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 554 | // Set up array classes for string, field, method |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 555 | Handle<mirror::Class> object_array_string(hs.NewHandle( |
| 556 | AllocClass(self, java_lang_Class.Get(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 557 | mirror::ObjectArray<mirror::String>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 558 | object_array_string->SetComponentType(java_lang_String.Get()); |
| 559 | SetClassRoot(kJavaLangStringArrayClass, object_array_string.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 560 | |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 561 | LinearAlloc* linear_alloc = runtime->GetLinearAlloc(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 562 | // Create runtime resolution and imt conflict methods. |
| 563 | runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 564 | runtime->SetImtConflictMethod(runtime->CreateImtConflictMethod(linear_alloc)); |
| 565 | runtime->SetImtUnimplementedMethod(runtime->CreateImtConflictMethod(linear_alloc)); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 566 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 567 | // Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create |
| 568 | // DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses |
| 569 | // these roots. |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 570 | if (boot_class_path.empty()) { |
| 571 | *error_msg = "Boot classpath is empty."; |
| 572 | return false; |
| 573 | } |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 574 | for (auto& dex_file : boot_class_path) { |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 575 | if (dex_file.get() == nullptr) { |
| 576 | *error_msg = "Null dex file."; |
| 577 | return false; |
| 578 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 579 | AppendToBootClassPath(self, *dex_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 580 | boot_dex_files_.push_back(std::move(dex_file)); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 581 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 582 | |
| 583 | // now we can use FindSystemClass |
| 584 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 585 | // run char class through InitializePrimitiveClass to finish init |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 586 | InitializePrimitiveClass(char_class.Get(), Primitive::kPrimChar); |
| 587 | SetClassRoot(kPrimitiveChar, char_class.Get()); // needs descriptor |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 588 | |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 589 | // Set up GenericJNI entrypoint. That is mainly a hack for common_compiler_test.h so that |
| 590 | // we do not need friend classes or a publicly exposed setter. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 591 | quick_generic_jni_trampoline_ = GetQuickGenericJniStub(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 592 | if (!runtime->IsAotCompiler()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 593 | // 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] | 594 | quick_resolution_trampoline_ = GetQuickResolutionStub(); |
| 595 | quick_imt_conflict_trampoline_ = GetQuickImtConflictStub(); |
| 596 | quick_to_interpreter_bridge_trampoline_ = GetQuickToInterpreterBridge(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 597 | } |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 598 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 599 | // 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] | 600 | mirror::Class::SetStatus(java_lang_Object, mirror::Class::kStatusNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 601 | CheckSystemClass(self, java_lang_Object, "Ljava/lang/Object;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 602 | CHECK_EQ(java_lang_Object->GetObjectSize(), mirror::Object::InstanceSize()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 603 | mirror::Class::SetStatus(java_lang_String, mirror::Class::kStatusNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 604 | CheckSystemClass(self, java_lang_String, "Ljava/lang/String;"); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 605 | mirror::Class::SetStatus(java_lang_DexCache, mirror::Class::kStatusNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 606 | CheckSystemClass(self, java_lang_DexCache, "Ljava/lang/DexCache;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 607 | CHECK_EQ(java_lang_DexCache->GetObjectSize(), mirror::DexCache::InstanceSize()); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 608 | mirror::Class::SetStatus(dalvik_system_ClassExt, mirror::Class::kStatusNotReady, self); |
| 609 | CheckSystemClass(self, dalvik_system_ClassExt, "Ldalvik/system/ClassExt;"); |
| 610 | CHECK_EQ(dalvik_system_ClassExt->GetObjectSize(), mirror::ClassExt::InstanceSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 611 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 612 | // 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] | 613 | SetClassRoot(kBooleanArrayClass, FindSystemClass(self, "[Z")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 614 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 615 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 616 | SetClassRoot(kByteArrayClass, FindSystemClass(self, "[B")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 617 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 618 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 619 | CheckSystemClass(self, char_array_class, "[C"); |
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(kShortArrayClass, FindSystemClass(self, "[S")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 622 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
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 | CheckSystemClass(self, int_array_class, "[I"); |
| 625 | CheckSystemClass(self, long_array_class, "[J"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 626 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 627 | SetClassRoot(kFloatArrayClass, FindSystemClass(self, "[F")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 628 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 629 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 630 | SetClassRoot(kDoubleArrayClass, FindSystemClass(self, "[D")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 631 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 632 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 633 | // Run Class through FindSystemClass. This initializes the dex_cache_ fields and register it |
| 634 | // in class_table_. |
| 635 | CheckSystemClass(self, java_lang_Class, "Ljava/lang/Class;"); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 636 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 637 | CheckSystemClass(self, class_array_class, "[Ljava/lang/Class;"); |
| 638 | CheckSystemClass(self, object_array_class, "[Ljava/lang/Object;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 639 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 640 | // Setup the single, global copy of "iftable". |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 641 | auto java_lang_Cloneable = hs.NewHandle(FindSystemClass(self, "Ljava/lang/Cloneable;")); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 642 | CHECK(java_lang_Cloneable != nullptr); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 643 | auto java_io_Serializable = hs.NewHandle(FindSystemClass(self, "Ljava/io/Serializable;")); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 644 | CHECK(java_io_Serializable != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 645 | // We assume that Cloneable/Serializable don't have superinterfaces -- normally we'd have to |
| 646 | // crawl up and explicitly list all of the supers as well. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 647 | array_iftable_.Read()->SetInterface(0, java_lang_Cloneable.Get()); |
| 648 | array_iftable_.Read()->SetInterface(1, java_io_Serializable.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 649 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 650 | // Sanity check Class[] and Object[]'s interfaces. GetDirectInterface may cause thread |
| 651 | // suspension. |
| 652 | CHECK_EQ(java_lang_Cloneable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 653 | mirror::Class::GetDirectInterface(self, class_array_class.Get(), 0)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 654 | CHECK_EQ(java_io_Serializable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 655 | mirror::Class::GetDirectInterface(self, class_array_class.Get(), 1)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 656 | CHECK_EQ(java_lang_Cloneable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 657 | mirror::Class::GetDirectInterface(self, object_array_class.Get(), 0)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 658 | CHECK_EQ(java_io_Serializable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 659 | mirror::Class::GetDirectInterface(self, object_array_class.Get(), 1)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 660 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 661 | CHECK_EQ(object_array_string.Get(), |
| 662 | FindSystemClass(self, GetClassRootDescriptor(kJavaLangStringArrayClass))); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 663 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 664 | // 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] | 665 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 666 | // Create java.lang.reflect.Proxy root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 667 | SetClassRoot(kJavaLangReflectProxy, FindSystemClass(self, "Ljava/lang/reflect/Proxy;")); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 668 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 669 | // Create java.lang.reflect.Field.class root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 670 | auto* class_root = FindSystemClass(self, "Ljava/lang/reflect/Field;"); |
| 671 | CHECK(class_root != nullptr); |
| 672 | SetClassRoot(kJavaLangReflectField, class_root); |
| 673 | mirror::Field::SetClass(class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 674 | |
| 675 | // Create java.lang.reflect.Field array root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 676 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Field;"); |
| 677 | CHECK(class_root != nullptr); |
| 678 | SetClassRoot(kJavaLangReflectFieldArrayClass, class_root); |
| 679 | mirror::Field::SetArrayClass(class_root); |
| 680 | |
| 681 | // Create java.lang.reflect.Constructor.class root and array root. |
| 682 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Constructor;"); |
| 683 | CHECK(class_root != nullptr); |
| 684 | SetClassRoot(kJavaLangReflectConstructor, class_root); |
| 685 | mirror::Constructor::SetClass(class_root); |
| 686 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Constructor;"); |
| 687 | CHECK(class_root != nullptr); |
| 688 | SetClassRoot(kJavaLangReflectConstructorArrayClass, class_root); |
| 689 | mirror::Constructor::SetArrayClass(class_root); |
| 690 | |
| 691 | // Create java.lang.reflect.Method.class root and array root. |
| 692 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Method;"); |
| 693 | CHECK(class_root != nullptr); |
| 694 | SetClassRoot(kJavaLangReflectMethod, class_root); |
| 695 | mirror::Method::SetClass(class_root); |
| 696 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Method;"); |
| 697 | CHECK(class_root != nullptr); |
| 698 | SetClassRoot(kJavaLangReflectMethodArrayClass, class_root); |
| 699 | mirror::Method::SetArrayClass(class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 700 | |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 701 | // Create java.lang.invoke.MethodType.class root |
| 702 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodType;"); |
| 703 | CHECK(class_root != nullptr); |
| 704 | SetClassRoot(kJavaLangInvokeMethodType, class_root); |
| 705 | mirror::MethodType::SetClass(class_root); |
| 706 | |
| 707 | // Create java.lang.invoke.MethodHandleImpl.class root |
| 708 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandleImpl;"); |
| 709 | CHECK(class_root != nullptr); |
| 710 | SetClassRoot(kJavaLangInvokeMethodHandleImpl, class_root); |
| 711 | mirror::MethodHandleImpl::SetClass(class_root); |
| 712 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 713 | // Create java.lang.invoke.MethodHandles.Lookup.class root |
| 714 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandles$Lookup;"); |
| 715 | CHECK(class_root != nullptr); |
| 716 | SetClassRoot(kJavaLangInvokeMethodHandlesLookup, class_root); |
| 717 | mirror::MethodHandlesLookup::SetClass(class_root); |
| 718 | |
| 719 | // Create java.lang.invoke.CallSite.class root |
| 720 | class_root = FindSystemClass(self, "Ljava/lang/invoke/CallSite;"); |
| 721 | CHECK(class_root != nullptr); |
| 722 | SetClassRoot(kJavaLangInvokeCallSite, class_root); |
| 723 | mirror::CallSite::SetClass(class_root); |
| 724 | |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 725 | class_root = FindSystemClass(self, "Ldalvik/system/EmulatedStackFrame;"); |
| 726 | CHECK(class_root != nullptr); |
| 727 | SetClassRoot(kDalvikSystemEmulatedStackFrame, class_root); |
| 728 | mirror::EmulatedStackFrame::SetClass(class_root); |
| 729 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 730 | // 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] | 731 | // finish initializing Reference class |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 732 | mirror::Class::SetStatus(java_lang_ref_Reference, mirror::Class::kStatusNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 733 | CheckSystemClass(self, java_lang_ref_Reference, "Ljava/lang/ref/Reference;"); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 734 | CHECK_EQ(java_lang_ref_Reference->GetObjectSize(), mirror::Reference::InstanceSize()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 735 | CHECK_EQ(java_lang_ref_Reference->GetClassSize(), |
| 736 | mirror::Reference::ClassSize(image_pointer_size_)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 737 | class_root = FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
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::kClassFlagFinalizerReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 740 | class_root = FindSystemClass(self, "Ljava/lang/ref/PhantomReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 741 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 742 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagPhantomReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 743 | class_root = FindSystemClass(self, "Ljava/lang/ref/SoftReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 744 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 745 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagSoftReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 746 | class_root = FindSystemClass(self, "Ljava/lang/ref/WeakReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 747 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 748 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagWeakReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 749 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 750 | // Setup the ClassLoader, verifying the object_size_. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 751 | class_root = FindSystemClass(self, "Ljava/lang/ClassLoader;"); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 752 | class_root->SetClassLoaderClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 753 | CHECK_EQ(class_root->GetObjectSize(), mirror::ClassLoader::InstanceSize()); |
| 754 | SetClassRoot(kJavaLangClassLoader, class_root); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 755 | |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 756 | // Set up java.lang.Throwable, java.lang.ClassNotFoundException, and |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 757 | // java.lang.StackTraceElement as a convenience. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 758 | SetClassRoot(kJavaLangThrowable, FindSystemClass(self, "Ljava/lang/Throwable;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 759 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 760 | SetClassRoot(kJavaLangClassNotFoundException, |
| 761 | FindSystemClass(self, "Ljava/lang/ClassNotFoundException;")); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 762 | SetClassRoot(kJavaLangStackTraceElement, FindSystemClass(self, "Ljava/lang/StackTraceElement;")); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 763 | SetClassRoot(kJavaLangStackTraceElementArrayClass, |
| 764 | FindSystemClass(self, "[Ljava/lang/StackTraceElement;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 765 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 766 | |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 767 | // Create conflict tables that depend on the class linker. |
| 768 | runtime->FixupConflictTables(); |
| 769 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 770 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 771 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 772 | VLOG(startup) << "ClassLinker::InitFromCompiler exiting"; |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 773 | |
| 774 | return true; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 777 | void ClassLinker::FinishInit(Thread* self) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 778 | VLOG(startup) << "ClassLinker::FinishInit entering"; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 779 | |
| 780 | // Let the heap know some key offsets into java.lang.ref instances |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 781 | // Note: we hard code the field indexes here rather than using FindInstanceField |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 782 | // as the types of the field can't be resolved prior to the runtime being |
| 783 | // fully initialized |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 784 | StackHandleScope<2> hs(self); |
| 785 | Handle<mirror::Class> java_lang_ref_Reference = hs.NewHandle(GetClassRoot(kJavaLangRefReference)); |
| 786 | Handle<mirror::Class> java_lang_ref_FinalizerReference = |
| 787 | hs.NewHandle(FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;")); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 788 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 789 | ArtField* pendingNext = java_lang_ref_Reference->GetInstanceField(0); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 790 | CHECK_STREQ(pendingNext->GetName(), "pendingNext"); |
| 791 | CHECK_STREQ(pendingNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 792 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 793 | ArtField* queue = java_lang_ref_Reference->GetInstanceField(1); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 794 | CHECK_STREQ(queue->GetName(), "queue"); |
| 795 | CHECK_STREQ(queue->GetTypeDescriptor(), "Ljava/lang/ref/ReferenceQueue;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 796 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 797 | ArtField* queueNext = java_lang_ref_Reference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 798 | CHECK_STREQ(queueNext->GetName(), "queueNext"); |
| 799 | CHECK_STREQ(queueNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 800 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 801 | ArtField* referent = java_lang_ref_Reference->GetInstanceField(3); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 802 | CHECK_STREQ(referent->GetName(), "referent"); |
| 803 | CHECK_STREQ(referent->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 804 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 805 | ArtField* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 806 | CHECK_STREQ(zombie->GetName(), "zombie"); |
| 807 | CHECK_STREQ(zombie->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 808 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 809 | // ensure all class_roots_ are initialized |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 810 | for (size_t i = 0; i < kClassRootsMax; i++) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 811 | ClassRoot class_root = static_cast<ClassRoot>(i); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 812 | ObjPtr<mirror::Class> klass = GetClassRoot(class_root); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 813 | CHECK(klass != nullptr); |
| 814 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != nullptr); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 815 | // note SetClassRoot does additional validation. |
| 816 | // if possible add new checks there to catch errors early |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 819 | CHECK(!array_iftable_.IsNull()); |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 820 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 821 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 822 | // that Object, Class, and Object[] are setup |
| 823 | init_done_ = true; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 824 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 825 | VLOG(startup) << "ClassLinker::FinishInit exiting"; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 826 | } |
| 827 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 828 | void ClassLinker::RunRootClinits() { |
| 829 | Thread* self = Thread::Current(); |
| 830 | for (size_t i = 0; i < ClassLinker::kClassRootsMax; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 831 | ObjPtr<mirror::Class> c = GetClassRoot(ClassRoot(i)); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 832 | if (!c->IsArrayClass() && !c->IsPrimitive()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 833 | StackHandleScope<1> hs(self); |
| 834 | Handle<mirror::Class> h_class(hs.NewHandle(GetClassRoot(ClassRoot(i)))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 835 | EnsureInitialized(self, h_class, true, true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 836 | self->AssertNoPendingException(); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | } |
| 840 | |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 841 | // Set image methods' entry point to interpreter. |
| 842 | class SetInterpreterEntrypointArtMethodVisitor : public ArtMethodVisitor { |
| 843 | public: |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 844 | explicit SetInterpreterEntrypointArtMethodVisitor(PointerSize image_pointer_size) |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 845 | : image_pointer_size_(image_pointer_size) {} |
| 846 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 847 | void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 848 | if (kIsDebugBuild && !method->IsRuntimeMethod()) { |
| 849 | CHECK(method->GetDeclaringClass() != nullptr); |
| 850 | } |
| 851 | if (!method->IsNative() && !method->IsRuntimeMethod() && !method->IsResolutionMethod()) { |
| 852 | method->SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(), |
| 853 | image_pointer_size_); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | private: |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 858 | const PointerSize image_pointer_size_; |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 859 | |
| 860 | DISALLOW_COPY_AND_ASSIGN(SetInterpreterEntrypointArtMethodVisitor); |
| 861 | }; |
| 862 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 863 | struct TrampolineCheckData { |
| 864 | const void* quick_resolution_trampoline; |
| 865 | const void* quick_imt_conflict_trampoline; |
| 866 | const void* quick_generic_jni_trampoline; |
| 867 | const void* quick_to_interpreter_bridge_trampoline; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 868 | PointerSize pointer_size; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 869 | ArtMethod* m; |
| 870 | bool error; |
| 871 | }; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 872 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 873 | bool ClassLinker::InitFromBootImage(std::string* error_msg) { |
| 874 | VLOG(startup) << __FUNCTION__ << " entering"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 875 | CHECK(!init_done_); |
| 876 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 877 | Runtime* const runtime = Runtime::Current(); |
| 878 | Thread* const self = Thread::Current(); |
| 879 | gc::Heap* const heap = runtime->GetHeap(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 880 | std::vector<gc::space::ImageSpace*> spaces = heap->GetBootImageSpaces(); |
| 881 | CHECK(!spaces.empty()); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 882 | uint32_t pointer_size_unchecked = spaces[0]->GetImageHeader().GetPointerSizeUnchecked(); |
| 883 | if (!ValidPointerSize(pointer_size_unchecked)) { |
| 884 | *error_msg = StringPrintf("Invalid image pointer size: %u", pointer_size_unchecked); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 885 | return false; |
| 886 | } |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 887 | image_pointer_size_ = spaces[0]->GetImageHeader().GetPointerSize(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 888 | if (!runtime->IsAotCompiler()) { |
| 889 | // Only the Aot compiler supports having an image with a different pointer size than the |
| 890 | // runtime. This happens on the host for compiling 32 bit tests since we use a 64 bit libart |
| 891 | // 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] | 892 | if (image_pointer_size_ != kRuntimePointerSize) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 893 | *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] | 894 | static_cast<size_t>(image_pointer_size_), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 895 | sizeof(void*)); |
| 896 | return false; |
| 897 | } |
| 898 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 899 | std::vector<const OatFile*> oat_files = |
| 900 | runtime->GetOatFileManager().RegisterImageOatFiles(spaces); |
| 901 | DCHECK(!oat_files.empty()); |
| 902 | const OatHeader& default_oat_header = oat_files[0]->GetOatHeader(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 903 | CHECK_EQ(default_oat_header.GetImageFileLocationOatDataBegin(), 0U); |
| 904 | const char* image_file_location = oat_files[0]->GetOatHeader(). |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 905 | GetStoreValueByKey(OatHeader::kImageLocationKey); |
| 906 | CHECK(image_file_location == nullptr || *image_file_location == 0); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 907 | quick_resolution_trampoline_ = default_oat_header.GetQuickResolutionTrampoline(); |
| 908 | quick_imt_conflict_trampoline_ = default_oat_header.GetQuickImtConflictTrampoline(); |
| 909 | quick_generic_jni_trampoline_ = default_oat_header.GetQuickGenericJniTrampoline(); |
| 910 | quick_to_interpreter_bridge_trampoline_ = default_oat_header.GetQuickToInterpreterBridge(); |
| 911 | if (kIsDebugBuild) { |
| 912 | // Check that the other images use the same trampoline. |
| 913 | for (size_t i = 1; i < oat_files.size(); ++i) { |
| 914 | const OatHeader& ith_oat_header = oat_files[i]->GetOatHeader(); |
| 915 | const void* ith_quick_resolution_trampoline = |
| 916 | ith_oat_header.GetQuickResolutionTrampoline(); |
| 917 | const void* ith_quick_imt_conflict_trampoline = |
| 918 | ith_oat_header.GetQuickImtConflictTrampoline(); |
| 919 | const void* ith_quick_generic_jni_trampoline = |
| 920 | ith_oat_header.GetQuickGenericJniTrampoline(); |
| 921 | const void* ith_quick_to_interpreter_bridge_trampoline = |
| 922 | ith_oat_header.GetQuickToInterpreterBridge(); |
| 923 | if (ith_quick_resolution_trampoline != quick_resolution_trampoline_ || |
| 924 | ith_quick_imt_conflict_trampoline != quick_imt_conflict_trampoline_ || |
| 925 | ith_quick_generic_jni_trampoline != quick_generic_jni_trampoline_ || |
| 926 | ith_quick_to_interpreter_bridge_trampoline != quick_to_interpreter_bridge_trampoline_) { |
| 927 | // Make sure that all methods in this image do not contain those trampolines as |
| 928 | // entrypoints. Otherwise the class-linker won't be able to work with a single set. |
| 929 | TrampolineCheckData data; |
| 930 | data.error = false; |
| 931 | data.pointer_size = GetImagePointerSize(); |
| 932 | data.quick_resolution_trampoline = ith_quick_resolution_trampoline; |
| 933 | data.quick_imt_conflict_trampoline = ith_quick_imt_conflict_trampoline; |
| 934 | data.quick_generic_jni_trampoline = ith_quick_generic_jni_trampoline; |
| 935 | data.quick_to_interpreter_bridge_trampoline = ith_quick_to_interpreter_bridge_trampoline; |
| 936 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 937 | auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 938 | if (obj->IsClass()) { |
| 939 | ObjPtr<mirror::Class> klass = obj->AsClass(); |
| 940 | for (ArtMethod& m : klass->GetMethods(data.pointer_size)) { |
| 941 | const void* entrypoint = |
| 942 | m.GetEntryPointFromQuickCompiledCodePtrSize(data.pointer_size); |
| 943 | if (entrypoint == data.quick_resolution_trampoline || |
| 944 | entrypoint == data.quick_imt_conflict_trampoline || |
| 945 | entrypoint == data.quick_generic_jni_trampoline || |
| 946 | entrypoint == data.quick_to_interpreter_bridge_trampoline) { |
| 947 | data.m = &m; |
| 948 | data.error = true; |
| 949 | return; |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | }; |
| 954 | spaces[i]->GetLiveBitmap()->Walk(visitor); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 955 | if (data.error) { |
| 956 | ArtMethod* m = data.m; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 957 | LOG(ERROR) << "Found a broken ArtMethod: " << ArtMethod::PrettyMethod(m); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 958 | *error_msg = "Found an ArtMethod with a bad entrypoint"; |
| 959 | return false; |
| 960 | } |
| 961 | } |
| 962 | } |
| 963 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 964 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 965 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
| 966 | down_cast<mirror::ObjectArray<mirror::Class>*>( |
| 967 | spaces[0]->GetImageHeader().GetImageRoot(ImageHeader::kClassRoots))); |
| 968 | mirror::Class::SetClassClass(class_roots_.Read()->Get(kJavaLangClass)); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 969 | |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 970 | // Special case of setting up the String class early so that we can test arbitrary objects |
| 971 | // as being Strings or not |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 972 | mirror::String::SetClass(GetClassRoot(kJavaLangString)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 973 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 974 | ObjPtr<mirror::Class> java_lang_Object = GetClassRoot(kJavaLangObject); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 975 | java_lang_Object->SetObjectSize(sizeof(mirror::Object)); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 976 | // Allocate in non-movable so that it's possible to check if a JNI weak global ref has been |
| 977 | // cleared without triggering the read barrier and unintentionally mark the sentinel alive. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 978 | runtime->SetSentinel(heap->AllocNonMovableObject<true>( |
| 979 | self, java_lang_Object, java_lang_Object->GetObjectSize(), VoidFunctor())); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 980 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 981 | // reinit array_iftable_ from any array class instance, they should be == |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 982 | array_iftable_ = GcRoot<mirror::IfTable>(GetClassRoot(kObjectArrayClass)->GetIfTable()); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 983 | DCHECK_EQ(array_iftable_.Read(), GetClassRoot(kBooleanArrayClass)->GetIfTable()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 984 | // String class root was set above |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 985 | mirror::Field::SetClass(GetClassRoot(kJavaLangReflectField)); |
| 986 | mirror::Field::SetArrayClass(GetClassRoot(kJavaLangReflectFieldArrayClass)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 987 | mirror::Constructor::SetClass(GetClassRoot(kJavaLangReflectConstructor)); |
| 988 | mirror::Constructor::SetArrayClass(GetClassRoot(kJavaLangReflectConstructorArrayClass)); |
| 989 | mirror::Method::SetClass(GetClassRoot(kJavaLangReflectMethod)); |
| 990 | mirror::Method::SetArrayClass(GetClassRoot(kJavaLangReflectMethodArrayClass)); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 991 | mirror::MethodType::SetClass(GetClassRoot(kJavaLangInvokeMethodType)); |
| 992 | mirror::MethodHandleImpl::SetClass(GetClassRoot(kJavaLangInvokeMethodHandleImpl)); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 993 | mirror::MethodHandlesLookup::SetClass(GetClassRoot(kJavaLangInvokeMethodHandlesLookup)); |
| 994 | mirror::CallSite::SetClass(GetClassRoot(kJavaLangInvokeCallSite)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 995 | mirror::Reference::SetClass(GetClassRoot(kJavaLangRefReference)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 996 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
| 997 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
| 998 | mirror::CharArray::SetArrayClass(GetClassRoot(kCharArrayClass)); |
| 999 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
| 1000 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
| 1001 | mirror::IntArray::SetArrayClass(GetClassRoot(kIntArrayClass)); |
| 1002 | mirror::LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
| 1003 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
| 1004 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
| 1005 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 1006 | mirror::EmulatedStackFrame::SetClass(GetClassRoot(kDalvikSystemEmulatedStackFrame)); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 1007 | mirror::ClassExt::SetClass(GetClassRoot(kDalvikSystemClassExt)); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1008 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1009 | for (gc::space::ImageSpace* image_space : spaces) { |
| 1010 | // Boot class loader, use a null handle. |
| 1011 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1012 | if (!AddImageSpace(image_space, |
| 1013 | ScopedNullHandle<mirror::ClassLoader>(), |
| 1014 | /*dex_elements*/nullptr, |
| 1015 | /*dex_location*/nullptr, |
| 1016 | /*out*/&dex_files, |
| 1017 | error_msg)) { |
| 1018 | return false; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1019 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1020 | // Append opened dex files at the end. |
| 1021 | boot_dex_files_.insert(boot_dex_files_.end(), |
| 1022 | std::make_move_iterator(dex_files.begin()), |
| 1023 | std::make_move_iterator(dex_files.end())); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 1024 | } |
Mathieu Chartier | be8303d | 2017-08-17 17:39:39 -0700 | [diff] [blame] | 1025 | for (const std::unique_ptr<const DexFile>& dex_file : boot_dex_files_) { |
| 1026 | OatDexFile::MadviseDexFile(*dex_file, MadviseState::kMadviseStateAtLoad); |
| 1027 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1028 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1029 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1030 | VLOG(startup) << __FUNCTION__ << " exiting"; |
| 1031 | return true; |
| 1032 | } |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 1033 | |
Jeff Hao | 5872d7c | 2016-04-27 11:07:41 -0700 | [diff] [blame] | 1034 | bool ClassLinker::IsBootClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1035 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1036 | return class_loader == nullptr || |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1037 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader) == |
| 1038 | class_loader->GetClass(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1039 | } |
| 1040 | |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1041 | static bool GetDexPathListElementName(ObjPtr<mirror::Object> element, |
| 1042 | ObjPtr<mirror::String>* out_name) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1043 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1044 | ArtField* const dex_file_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 1045 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1046 | ArtField* const dex_file_name_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 1047 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_fileName); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1048 | DCHECK(dex_file_field != nullptr); |
| 1049 | DCHECK(dex_file_name_field != nullptr); |
| 1050 | DCHECK(element != nullptr); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1051 | CHECK_EQ(dex_file_field->GetDeclaringClass(), element->GetClass()) << element->PrettyTypeOf(); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1052 | ObjPtr<mirror::Object> dex_file = dex_file_field->GetObject(element); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1053 | if (dex_file == nullptr) { |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1054 | // Null dex file means it was probably a jar with no dex files, return a null string. |
| 1055 | *out_name = nullptr; |
| 1056 | return true; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1057 | } |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1058 | ObjPtr<mirror::Object> name_object = dex_file_name_field->GetObject(dex_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1059 | if (name_object != nullptr) { |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1060 | *out_name = name_object->AsString(); |
| 1061 | return true; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1062 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1063 | return false; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1064 | } |
| 1065 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1066 | static bool FlattenPathClassLoader(ObjPtr<mirror::ClassLoader> class_loader, |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1067 | std::list<ObjPtr<mirror::String>>* out_dex_file_names, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1068 | std::string* error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1069 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1070 | DCHECK(out_dex_file_names != nullptr); |
| 1071 | DCHECK(error_msg != nullptr); |
| 1072 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1073 | ArtField* const dex_path_list_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 1074 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1075 | ArtField* const dex_elements_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 1076 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1077 | CHECK(dex_path_list_field != nullptr); |
| 1078 | CHECK(dex_elements_field != nullptr); |
Jeff Hao | 5872d7c | 2016-04-27 11:07:41 -0700 | [diff] [blame] | 1079 | while (!ClassLinker::IsBootClassLoader(soa, class_loader)) { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1080 | if (soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader) != |
| 1081 | class_loader->GetClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1082 | *error_msg = StringPrintf("Unknown class loader type %s", |
| 1083 | class_loader->PrettyTypeOf().c_str()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1084 | // Unsupported class loader. |
| 1085 | return false; |
| 1086 | } |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1087 | 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] | 1088 | if (dex_path_list != nullptr) { |
| 1089 | // 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] | 1090 | 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] | 1091 | // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look |
| 1092 | // at the mCookie which is a DexFile vector. |
| 1093 | if (dex_elements_obj != nullptr) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1094 | ObjPtr<mirror::ObjectArray<mirror::Object>> dex_elements = |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1095 | dex_elements_obj->AsObjectArray<mirror::Object>(); |
| 1096 | // Reverse order since we insert the parent at the front. |
| 1097 | for (int32_t i = dex_elements->GetLength() - 1; i >= 0; --i) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1098 | ObjPtr<mirror::Object> element = dex_elements->GetWithoutChecks(i); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1099 | if (element == nullptr) { |
| 1100 | *error_msg = StringPrintf("Null dex element at index %d", i); |
| 1101 | return false; |
| 1102 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1103 | ObjPtr<mirror::String> name; |
| 1104 | if (!GetDexPathListElementName(element, &name)) { |
| 1105 | *error_msg = StringPrintf("Invalid dex path list element at index %d", i); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1106 | return false; |
| 1107 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1108 | if (name != nullptr) { |
| 1109 | out_dex_file_names->push_front(name.Ptr()); |
| 1110 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1111 | } |
| 1112 | } |
| 1113 | } |
| 1114 | class_loader = class_loader->GetParent(); |
| 1115 | } |
| 1116 | return true; |
| 1117 | } |
| 1118 | |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1119 | class VerifyDeclaringClassVisitor : public ArtMethodVisitor { |
| 1120 | public: |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1121 | VerifyDeclaringClassVisitor() REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1122 | : live_bitmap_(Runtime::Current()->GetHeap()->GetLiveBitmap()) {} |
| 1123 | |
| 1124 | virtual void Visit(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1125 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1126 | ObjPtr<mirror::Class> klass = method->GetDeclaringClassUnchecked(); |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1127 | if (klass != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1128 | CHECK(live_bitmap_->Test(klass.Ptr())) << "Image method has unmarked declaring class"; |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | private: |
| 1133 | gc::accounting::HeapBitmap* const live_bitmap_; |
| 1134 | }; |
| 1135 | |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1136 | class FixupInternVisitor { |
| 1137 | public: |
| 1138 | ALWAYS_INLINE ObjPtr<mirror::Object> TryInsertIntern(mirror::Object* obj) const |
| 1139 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1140 | if (obj != nullptr && obj->IsString()) { |
| 1141 | const auto intern = Runtime::Current()->GetInternTable()->InternStrong(obj->AsString()); |
| 1142 | return intern; |
| 1143 | } |
| 1144 | return obj; |
| 1145 | } |
| 1146 | |
| 1147 | ALWAYS_INLINE void VisitRootIfNonNull( |
| 1148 | mirror::CompressedReference<mirror::Object>* root) const |
| 1149 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1150 | if (!root->IsNull()) { |
| 1151 | VisitRoot(root); |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | ALWAYS_INLINE void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
| 1156 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1157 | root->Assign(TryInsertIntern(root->AsMirrorPtr())); |
| 1158 | } |
| 1159 | |
| 1160 | // Visit Class Fields |
| 1161 | ALWAYS_INLINE void operator()(ObjPtr<mirror::Object> obj, |
| 1162 | MemberOffset offset, |
| 1163 | bool is_static ATTRIBUTE_UNUSED) const |
| 1164 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1165 | // There could be overlap between ranges, we must avoid visiting the same reference twice. |
| 1166 | // Avoid the class field since we already fixed it up in FixupClassVisitor. |
| 1167 | if (offset.Uint32Value() != mirror::Object::ClassOffset().Uint32Value()) { |
| 1168 | // Updating images, don't do a read barrier. |
| 1169 | // Only string fields are fixed, don't do a verify. |
| 1170 | mirror::Object* ref = obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>( |
| 1171 | offset); |
| 1172 | obj->SetFieldObject<false, false>(offset, TryInsertIntern(ref)); |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | void operator()(ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED, |
| 1177 | ObjPtr<mirror::Reference> ref) const |
| 1178 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
| 1179 | this->operator()(ref, mirror::Reference::ReferentOffset(), false); |
| 1180 | } |
| 1181 | |
| 1182 | void operator()(mirror::Object* obj) const |
| 1183 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1184 | if (obj->IsDexCache()) { |
| 1185 | obj->VisitReferences<true, kVerifyNone, kWithoutReadBarrier>(*this, *this); |
| 1186 | } else { |
| 1187 | // Don't visit native roots for non-dex-cache |
| 1188 | obj->VisitReferences<false, kVerifyNone, kWithoutReadBarrier>(*this, *this); |
| 1189 | } |
| 1190 | } |
| 1191 | }; |
| 1192 | |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1193 | // new_class_set is the set of classes that were read from the class table section in the image. |
| 1194 | // If there was no class table section, it is null. |
| 1195 | // Note: using a class here to avoid having to make ClassLinker internals public. |
| 1196 | class AppImageClassLoadersAndDexCachesHelper { |
| 1197 | public: |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1198 | static void Update( |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1199 | ClassLinker* class_linker, |
| 1200 | gc::space::ImageSpace* space, |
| 1201 | Handle<mirror::ClassLoader> class_loader, |
| 1202 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches, |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1203 | ClassTable::ClassSet* new_class_set) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1204 | REQUIRES(!Locks::dex_lock_) |
| 1205 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 1206 | }; |
| 1207 | |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1208 | void AppImageClassLoadersAndDexCachesHelper::Update( |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1209 | ClassLinker* class_linker, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1210 | gc::space::ImageSpace* space, |
| 1211 | Handle<mirror::ClassLoader> class_loader, |
| 1212 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches, |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1213 | ClassTable::ClassSet* new_class_set) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1214 | REQUIRES(!Locks::dex_lock_) |
| 1215 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1216 | Thread* const self = Thread::Current(); |
| 1217 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
| 1218 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1219 | { |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1220 | // Register dex caches with the class loader. |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1221 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1222 | const size_t num_dex_caches = dex_caches->GetLength(); |
| 1223 | for (size_t i = 0; i < num_dex_caches; i++) { |
Vladimir Marko | 1bc4b17 | 2016-10-24 16:53:39 +0000 | [diff] [blame] | 1224 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1225 | const DexFile* const dex_file = dex_cache->GetDexFile(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1226 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 1227 | WriterMutexLock mu2(self, *Locks::dex_lock_); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1228 | CHECK(!class_linker->FindDexCacheDataLocked(*dex_file).IsValid()); |
| 1229 | class_linker->RegisterDexFileLocked(*dex_file, dex_cache, class_loader.Get()); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1230 | } |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1231 | if (kIsDebugBuild) { |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1232 | CHECK(new_class_set != nullptr); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1233 | mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes(); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1234 | const size_t num_types = dex_cache->NumResolvedTypes(); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1235 | for (size_t j = 0; j != num_types; ++j) { |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1236 | // 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] | 1237 | 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] | 1238 | if (space->HasAddress(klass.Ptr())) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 1239 | DCHECK(!klass->IsErroneous()) << klass->GetStatus(); |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 1240 | auto it = new_class_set->Find(ClassTable::TableSlot(klass)); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1241 | DCHECK(it != new_class_set->end()); |
| 1242 | DCHECK_EQ(it->Read(), klass); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1243 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1244 | if (super_class != nullptr && !heap->ObjectIsInBootImageSpace(super_class)) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 1245 | auto it2 = new_class_set->Find(ClassTable::TableSlot(super_class)); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1246 | DCHECK(it2 != new_class_set->end()); |
| 1247 | DCHECK_EQ(it2->Read(), super_class); |
| 1248 | } |
| 1249 | for (ArtMethod& m : klass->GetDirectMethods(kRuntimePointerSize)) { |
| 1250 | const void* code = m.GetEntryPointFromQuickCompiledCode(); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1251 | const void* oat_code = m.IsInvokable() ? class_linker->GetQuickOatCodeFor(&m) : code; |
| 1252 | if (!class_linker->IsQuickResolutionStub(code) && |
| 1253 | !class_linker->IsQuickGenericJniStub(code) && |
| 1254 | !class_linker->IsQuickToInterpreterBridge(code) && |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1255 | !m.IsNative()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1256 | DCHECK_EQ(code, oat_code) << m.PrettyMethod(); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1257 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1258 | } |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1259 | for (ArtMethod& m : klass->GetVirtualMethods(kRuntimePointerSize)) { |
| 1260 | const void* code = m.GetEntryPointFromQuickCompiledCode(); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1261 | const void* oat_code = m.IsInvokable() ? class_linker->GetQuickOatCodeFor(&m) : code; |
| 1262 | if (!class_linker->IsQuickResolutionStub(code) && |
| 1263 | !class_linker->IsQuickGenericJniStub(code) && |
| 1264 | !class_linker->IsQuickToInterpreterBridge(code) && |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1265 | !m.IsNative()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1266 | DCHECK_EQ(code, oat_code) << m.PrettyMethod(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1267 | } |
| 1268 | } |
| 1269 | } |
| 1270 | } |
| 1271 | } |
| 1272 | } |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1273 | } |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1274 | { |
| 1275 | // Fixup all the literal strings happens at app images which are supposed to be interned. |
| 1276 | ScopedTrace timing("Fixup String Intern in image and dex_cache"); |
| 1277 | const auto& image_header = space->GetImageHeader(); |
| 1278 | const auto bitmap = space->GetMarkBitmap(); // bitmap of objects |
| 1279 | const uint8_t* target_base = space->GetMemMap()->Begin(); |
Vladimir Marko | cd87c3e | 2017-09-05 13:11:57 +0100 | [diff] [blame] | 1280 | const ImageSection& objects_section = image_header.GetObjectsSection(); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1281 | |
| 1282 | uintptr_t objects_begin = reinterpret_cast<uintptr_t>(target_base + objects_section.Offset()); |
| 1283 | uintptr_t objects_end = reinterpret_cast<uintptr_t>(target_base + objects_section.End()); |
| 1284 | |
| 1285 | FixupInternVisitor fixup_intern_visitor; |
| 1286 | bitmap->VisitMarkedRange(objects_begin, objects_end, fixup_intern_visitor); |
| 1287 | } |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1288 | if (kVerifyArtMethodDeclaringClasses) { |
| 1289 | ScopedTrace timing("Verify declaring classes"); |
| 1290 | ReaderMutexLock rmu(self, *Locks::heap_bitmap_lock_); |
| 1291 | VerifyDeclaringClassVisitor visitor; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1292 | header.VisitPackedArtMethods(&visitor, space->Begin(), kRuntimePointerSize); |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1293 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1294 | } |
| 1295 | |
Nicolas Geoffray | f9bf250 | 2016-12-14 14:59:04 +0000 | [diff] [blame] | 1296 | // Update the class loader. Should only be used on classes in the image space. |
| 1297 | class UpdateClassLoaderVisitor { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1298 | public: |
Nicolas Geoffray | f9bf250 | 2016-12-14 14:59:04 +0000 | [diff] [blame] | 1299 | UpdateClassLoaderVisitor(gc::space::ImageSpace* space, ObjPtr<mirror::ClassLoader> class_loader) |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1300 | : space_(space), |
Nicolas Geoffray | f9bf250 | 2016-12-14 14:59:04 +0000 | [diff] [blame] | 1301 | class_loader_(class_loader) {} |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1302 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1303 | bool operator()(ObjPtr<mirror::Class> klass) const REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 1304 | // Do not update class loader for boot image classes where the app image |
| 1305 | // class loader is only the initiating loader but not the defining loader. |
| 1306 | if (klass->GetClassLoader() != nullptr) { |
| 1307 | klass->SetClassLoader(class_loader_); |
| 1308 | } |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1309 | return true; |
| 1310 | } |
| 1311 | |
| 1312 | gc::space::ImageSpace* const space_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1313 | ObjPtr<mirror::ClassLoader> const class_loader_; |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1314 | }; |
| 1315 | |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1316 | static std::unique_ptr<const DexFile> OpenOatDexFile(const OatFile* oat_file, |
| 1317 | const char* location, |
| 1318 | std::string* error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1319 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1320 | DCHECK(error_msg != nullptr); |
| 1321 | std::unique_ptr<const DexFile> dex_file; |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 1322 | 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] | 1323 | if (oat_dex_file == nullptr) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1324 | return std::unique_ptr<const DexFile>(); |
| 1325 | } |
| 1326 | std::string inner_error_msg; |
| 1327 | dex_file = oat_dex_file->OpenDexFile(&inner_error_msg); |
| 1328 | if (dex_file == nullptr) { |
| 1329 | *error_msg = StringPrintf("Failed to open dex file %s from within oat file %s error '%s'", |
| 1330 | location, |
| 1331 | oat_file->GetLocation().c_str(), |
| 1332 | inner_error_msg.c_str()); |
| 1333 | return std::unique_ptr<const DexFile>(); |
| 1334 | } |
| 1335 | |
| 1336 | if (dex_file->GetLocationChecksum() != oat_dex_file->GetDexFileLocationChecksum()) { |
| 1337 | *error_msg = StringPrintf("Checksums do not match for %s: %x vs %x", |
| 1338 | location, |
| 1339 | dex_file->GetLocationChecksum(), |
| 1340 | oat_dex_file->GetDexFileLocationChecksum()); |
| 1341 | return std::unique_ptr<const DexFile>(); |
| 1342 | } |
| 1343 | return dex_file; |
| 1344 | } |
| 1345 | |
| 1346 | bool ClassLinker::OpenImageDexFiles(gc::space::ImageSpace* space, |
| 1347 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 1348 | std::string* error_msg) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 1349 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1350 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1351 | ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1352 | DCHECK(dex_caches_object != nullptr); |
| 1353 | mirror::ObjectArray<mirror::DexCache>* dex_caches = |
| 1354 | dex_caches_object->AsObjectArray<mirror::DexCache>(); |
| 1355 | const OatFile* oat_file = space->GetOatFile(); |
| 1356 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1357 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1358 | std::string dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
| 1359 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 1360 | dex_file_location.c_str(), |
| 1361 | error_msg); |
| 1362 | if (dex_file == nullptr) { |
| 1363 | return false; |
| 1364 | } |
| 1365 | dex_cache->SetDexFile(dex_file.get()); |
| 1366 | out_dex_files->push_back(std::move(dex_file)); |
| 1367 | } |
| 1368 | return true; |
| 1369 | } |
| 1370 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1371 | // Helper class for ArtMethod checks when adding an image. Keeps all required functionality |
| 1372 | // together and caches some intermediate results. |
| 1373 | class ImageSanityChecks FINAL { |
| 1374 | public: |
| 1375 | static void CheckObjects(gc::Heap* heap, ClassLinker* class_linker) |
| 1376 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1377 | ImageSanityChecks isc(heap, class_linker); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1378 | auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1379 | DCHECK(obj != nullptr); |
| 1380 | CHECK(obj->GetClass() != nullptr) << "Null class in object " << obj; |
| 1381 | CHECK(obj->GetClass()->GetClass() != nullptr) << "Null class class " << obj; |
| 1382 | if (obj->IsClass()) { |
| 1383 | auto klass = obj->AsClass(); |
| 1384 | for (ArtField& field : klass->GetIFields()) { |
| 1385 | CHECK_EQ(field.GetDeclaringClass(), klass); |
| 1386 | } |
| 1387 | for (ArtField& field : klass->GetSFields()) { |
| 1388 | CHECK_EQ(field.GetDeclaringClass(), klass); |
| 1389 | } |
| 1390 | const auto pointer_size = isc.pointer_size_; |
| 1391 | for (auto& m : klass->GetMethods(pointer_size)) { |
| 1392 | isc.SanityCheckArtMethod(&m, klass); |
| 1393 | } |
| 1394 | auto* vtable = klass->GetVTable(); |
| 1395 | if (vtable != nullptr) { |
| 1396 | isc.SanityCheckArtMethodPointerArray(vtable, nullptr); |
| 1397 | } |
| 1398 | if (klass->ShouldHaveImt()) { |
| 1399 | ImTable* imt = klass->GetImt(pointer_size); |
| 1400 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 1401 | isc.SanityCheckArtMethod(imt->Get(i, pointer_size), nullptr); |
| 1402 | } |
| 1403 | } |
| 1404 | if (klass->ShouldHaveEmbeddedVTable()) { |
| 1405 | for (int32_t i = 0; i < klass->GetEmbeddedVTableLength(); ++i) { |
| 1406 | isc.SanityCheckArtMethod(klass->GetEmbeddedVTableEntry(i, pointer_size), nullptr); |
| 1407 | } |
| 1408 | } |
| 1409 | mirror::IfTable* iftable = klass->GetIfTable(); |
| 1410 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 1411 | if (iftable->GetMethodArrayCount(i) > 0) { |
| 1412 | isc.SanityCheckArtMethodPointerArray(iftable->GetMethodArray(i), nullptr); |
| 1413 | } |
| 1414 | } |
| 1415 | } |
| 1416 | }; |
| 1417 | heap->VisitObjects(visitor); |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1418 | } |
| 1419 | |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 1420 | static void CheckArtMethodDexCacheArray(gc::Heap* heap, |
| 1421 | ClassLinker* class_linker, |
| 1422 | mirror::MethodDexCacheType* arr, |
| 1423 | size_t size) |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1424 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1425 | ImageSanityChecks isc(heap, class_linker); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 1426 | isc.SanityCheckArtMethodDexCacheArray(arr, size); |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1427 | } |
| 1428 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1429 | private: |
| 1430 | ImageSanityChecks(gc::Heap* heap, ClassLinker* class_linker) |
| 1431 | : spaces_(heap->GetBootImageSpaces()), |
| 1432 | pointer_size_(class_linker->GetImagePointerSize()) { |
| 1433 | space_begin_.reserve(spaces_.size()); |
| 1434 | method_sections_.reserve(spaces_.size()); |
| 1435 | runtime_method_sections_.reserve(spaces_.size()); |
| 1436 | for (gc::space::ImageSpace* space : spaces_) { |
| 1437 | space_begin_.push_back(space->Begin()); |
| 1438 | auto& header = space->GetImageHeader(); |
| 1439 | method_sections_.push_back(&header.GetMethodsSection()); |
| 1440 | runtime_method_sections_.push_back(&header.GetRuntimeMethodsSection()); |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | void SanityCheckArtMethod(ArtMethod* m, ObjPtr<mirror::Class> expected_class) |
| 1445 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1446 | if (m->IsRuntimeMethod()) { |
| 1447 | ObjPtr<mirror::Class> declaring_class = m->GetDeclaringClassUnchecked(); |
| 1448 | CHECK(declaring_class == nullptr) << declaring_class << " " << m->PrettyMethod(); |
| 1449 | } else if (m->IsCopied()) { |
| 1450 | CHECK(m->GetDeclaringClass() != nullptr) << m->PrettyMethod(); |
| 1451 | } else if (expected_class != nullptr) { |
| 1452 | CHECK_EQ(m->GetDeclaringClassUnchecked(), expected_class) << m->PrettyMethod(); |
| 1453 | } |
| 1454 | if (!spaces_.empty()) { |
| 1455 | bool contains = false; |
| 1456 | for (size_t i = 0; !contains && i != space_begin_.size(); ++i) { |
| 1457 | const size_t offset = reinterpret_cast<uint8_t*>(m) - space_begin_[i]; |
| 1458 | contains = method_sections_[i]->Contains(offset) || |
| 1459 | runtime_method_sections_[i]->Contains(offset); |
| 1460 | } |
| 1461 | CHECK(contains) << m << " not found"; |
| 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | void SanityCheckArtMethodPointerArray(ObjPtr<mirror::PointerArray> arr, |
| 1466 | ObjPtr<mirror::Class> expected_class) |
| 1467 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1468 | CHECK(arr != nullptr); |
| 1469 | for (int32_t j = 0; j < arr->GetLength(); ++j) { |
| 1470 | auto* method = arr->GetElementPtrSize<ArtMethod*>(j, pointer_size_); |
| 1471 | // expected_class == null means we are a dex cache. |
| 1472 | if (expected_class != nullptr) { |
| 1473 | CHECK(method != nullptr); |
| 1474 | } |
| 1475 | if (method != nullptr) { |
| 1476 | SanityCheckArtMethod(method, expected_class); |
| 1477 | } |
| 1478 | } |
| 1479 | } |
| 1480 | |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 1481 | void SanityCheckArtMethodDexCacheArray(mirror::MethodDexCacheType* arr, size_t size) |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1482 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1483 | CHECK_EQ(arr != nullptr, size != 0u); |
| 1484 | if (arr != nullptr) { |
| 1485 | bool contains = false; |
| 1486 | for (auto space : spaces_) { |
| 1487 | auto offset = reinterpret_cast<uint8_t*>(arr) - space->Begin(); |
Vladimir Marko | cd87c3e | 2017-09-05 13:11:57 +0100 | [diff] [blame] | 1488 | if (space->GetImageHeader().GetDexCacheArraysSection().Contains(offset)) { |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1489 | contains = true; |
| 1490 | break; |
| 1491 | } |
| 1492 | } |
| 1493 | CHECK(contains); |
| 1494 | } |
| 1495 | for (size_t j = 0; j < size; ++j) { |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 1496 | auto pair = mirror::DexCache::GetNativePairPtrSize(arr, j, pointer_size_); |
| 1497 | ArtMethod* method = pair.object; |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1498 | // expected_class == null means we are a dex cache. |
| 1499 | if (method != nullptr) { |
| 1500 | SanityCheckArtMethod(method, nullptr); |
| 1501 | } |
| 1502 | } |
| 1503 | } |
| 1504 | |
| 1505 | const std::vector<gc::space::ImageSpace*>& spaces_; |
| 1506 | const PointerSize pointer_size_; |
| 1507 | |
| 1508 | // Cached sections from the spaces. |
| 1509 | std::vector<const uint8_t*> space_begin_; |
| 1510 | std::vector<const ImageSection*> method_sections_; |
| 1511 | std::vector<const ImageSection*> runtime_method_sections_; |
| 1512 | }; |
| 1513 | |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1514 | static void VerifyAppImage(const ImageHeader& header, |
| 1515 | const Handle<mirror::ClassLoader>& class_loader, |
| 1516 | const Handle<mirror::ObjectArray<mirror::DexCache> >& dex_caches, |
| 1517 | ClassTable* class_table, gc::space::ImageSpace* space) |
| 1518 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1519 | { |
| 1520 | class VerifyClassInTableArtMethodVisitor : public ArtMethodVisitor { |
| 1521 | public: |
| 1522 | explicit VerifyClassInTableArtMethodVisitor(ClassTable* table) : table_(table) {} |
| 1523 | |
| 1524 | virtual void Visit(ArtMethod* method) |
| 1525 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::classlinker_classes_lock_) { |
| 1526 | ObjPtr<mirror::Class> klass = method->GetDeclaringClass(); |
| 1527 | if (klass != nullptr && !Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) { |
| 1528 | CHECK_EQ(table_->LookupByDescriptor(klass), klass) << mirror::Class::PrettyClass(klass); |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | private: |
| 1533 | ClassTable* const table_; |
| 1534 | }; |
| 1535 | VerifyClassInTableArtMethodVisitor visitor(class_table); |
| 1536 | header.VisitPackedArtMethods(&visitor, space->Begin(), kRuntimePointerSize); |
| 1537 | } |
| 1538 | { |
| 1539 | // Verify that all direct interfaces of classes in the class table are also resolved. |
| 1540 | std::vector<ObjPtr<mirror::Class>> classes; |
| 1541 | auto verify_direct_interfaces_in_table = [&](ObjPtr<mirror::Class> klass) |
| 1542 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1543 | if (!klass->IsPrimitive() && klass->GetClassLoader() == class_loader.Get()) { |
| 1544 | classes.push_back(klass); |
| 1545 | } |
| 1546 | return true; |
| 1547 | }; |
| 1548 | class_table->Visit(verify_direct_interfaces_in_table); |
| 1549 | Thread* self = Thread::Current(); |
| 1550 | for (ObjPtr<mirror::Class> klass : classes) { |
| 1551 | for (uint32_t i = 0, num = klass->NumDirectInterfaces(); i != num; ++i) { |
| 1552 | CHECK(klass->GetDirectInterface(self, klass, i) != nullptr) |
| 1553 | << klass->PrettyDescriptor() << " iface #" << i; |
| 1554 | } |
| 1555 | } |
| 1556 | } |
| 1557 | // Check that all non-primitive classes in dex caches are also in the class table. |
| 1558 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 1559 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
| 1560 | mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes(); |
| 1561 | for (int32_t j = 0, num_types = dex_cache->NumResolvedTypes(); j < num_types; j++) { |
| 1562 | ObjPtr<mirror::Class> klass = types[j].load(std::memory_order_relaxed).object.Read(); |
| 1563 | if (klass != nullptr && !klass->IsPrimitive()) { |
| 1564 | CHECK(class_table->Contains(klass)) |
| 1565 | << klass->PrettyDescriptor() << " " << dex_cache->GetDexFile()->GetLocation(); |
| 1566 | } |
| 1567 | } |
| 1568 | } |
| 1569 | } |
| 1570 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1571 | bool ClassLinker::AddImageSpace( |
| 1572 | gc::space::ImageSpace* space, |
| 1573 | Handle<mirror::ClassLoader> class_loader, |
| 1574 | jobjectArray dex_elements, |
| 1575 | const char* dex_location, |
| 1576 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 1577 | std::string* error_msg) { |
| 1578 | DCHECK(out_dex_files != nullptr); |
| 1579 | DCHECK(error_msg != nullptr); |
| 1580 | const uint64_t start_time = NanoTime(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1581 | const bool app_image = class_loader != nullptr; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1582 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1583 | ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1584 | DCHECK(dex_caches_object != nullptr); |
| 1585 | Runtime* const runtime = Runtime::Current(); |
| 1586 | gc::Heap* const heap = runtime->GetHeap(); |
| 1587 | Thread* const self = Thread::Current(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1588 | // Check that the image is what we are expecting. |
| 1589 | if (image_pointer_size_ != space->GetImageHeader().GetPointerSize()) { |
| 1590 | *error_msg = StringPrintf("Application image pointer size does not match runtime: %zu vs %zu", |
| 1591 | static_cast<size_t>(space->GetImageHeader().GetPointerSize()), |
| 1592 | image_pointer_size_); |
| 1593 | return false; |
| 1594 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1595 | size_t expected_image_roots = ImageHeader::NumberOfImageRoots(app_image); |
| 1596 | if (static_cast<size_t>(header.GetImageRoots()->GetLength()) != expected_image_roots) { |
| 1597 | *error_msg = StringPrintf("Expected %zu image roots but got %d", |
| 1598 | expected_image_roots, |
| 1599 | header.GetImageRoots()->GetLength()); |
| 1600 | return false; |
| 1601 | } |
| 1602 | StackHandleScope<3> hs(self); |
| 1603 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches( |
| 1604 | hs.NewHandle(dex_caches_object->AsObjectArray<mirror::DexCache>())); |
| 1605 | Handle<mirror::ObjectArray<mirror::Class>> class_roots(hs.NewHandle( |
| 1606 | header.GetImageRoot(ImageHeader::kClassRoots)->AsObjectArray<mirror::Class>())); |
| 1607 | static_assert(ImageHeader::kClassLoader + 1u == ImageHeader::kImageRootsMax, |
| 1608 | "Class loader should be the last image root."); |
| 1609 | MutableHandle<mirror::ClassLoader> image_class_loader(hs.NewHandle( |
| 1610 | app_image ? header.GetImageRoot(ImageHeader::kClassLoader)->AsClassLoader() : nullptr)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1611 | DCHECK(class_roots != nullptr); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1612 | if (class_roots->GetLength() != static_cast<int32_t>(kClassRootsMax)) { |
| 1613 | *error_msg = StringPrintf("Expected %d class roots but got %d", |
| 1614 | class_roots->GetLength(), |
| 1615 | static_cast<int32_t>(kClassRootsMax)); |
| 1616 | return false; |
| 1617 | } |
| 1618 | // Check against existing class roots to make sure they match the ones in the boot image. |
| 1619 | for (size_t i = 0; i < kClassRootsMax; i++) { |
| 1620 | if (class_roots->Get(i) != GetClassRoot(static_cast<ClassRoot>(i))) { |
| 1621 | *error_msg = "App image class roots must have pointer equality with runtime ones."; |
| 1622 | return false; |
| 1623 | } |
| 1624 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1625 | const OatFile* oat_file = space->GetOatFile(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1626 | if (oat_file->GetOatHeader().GetDexFileCount() != |
| 1627 | static_cast<uint32_t>(dex_caches->GetLength())) { |
| 1628 | *error_msg = "Dex cache count and dex file count mismatch while trying to initialize from " |
| 1629 | "image"; |
| 1630 | return false; |
| 1631 | } |
| 1632 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1633 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1634 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
| 1635 | std::string dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1636 | // TODO: Only store qualified paths. |
| 1637 | // If non qualified, qualify it. |
| 1638 | if (dex_file_location.find('/') == std::string::npos) { |
| 1639 | std::string dex_location_path = dex_location; |
| 1640 | const size_t pos = dex_location_path.find_last_of('/'); |
| 1641 | CHECK_NE(pos, std::string::npos); |
| 1642 | dex_location_path = dex_location_path.substr(0, pos + 1); // Keep trailing '/' |
| 1643 | dex_file_location = dex_location_path + dex_file_location; |
| 1644 | } |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1645 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 1646 | dex_file_location.c_str(), |
| 1647 | error_msg); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1648 | if (dex_file == nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1649 | return false; |
| 1650 | } |
| 1651 | |
| 1652 | if (app_image) { |
| 1653 | // The current dex file field is bogus, overwrite it so that we can get the dex file in the |
| 1654 | // loop below. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1655 | dex_cache->SetDexFile(dex_file.get()); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1656 | mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes(); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1657 | 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] | 1658 | 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] | 1659 | if (klass != nullptr) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 1660 | DCHECK(!klass->IsErroneous()) << klass->GetStatus(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1661 | } |
| 1662 | } |
| 1663 | } else { |
| 1664 | if (kSanityCheckObjects) { |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 1665 | ImageSanityChecks::CheckArtMethodDexCacheArray(heap, |
| 1666 | this, |
| 1667 | dex_cache->GetResolvedMethods(), |
| 1668 | dex_cache->NumResolvedMethods()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1669 | } |
| 1670 | // Register dex files, keep track of existing ones that are conflicts. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1671 | AppendToBootClassPath(*dex_file.get(), dex_cache); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1672 | } |
| 1673 | out_dex_files->push_back(std::move(dex_file)); |
| 1674 | } |
| 1675 | |
| 1676 | if (app_image) { |
| 1677 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1678 | // Check that the class loader resolves the same way as the ones in the image. |
| 1679 | // Image class loader [A][B][C][image dex files] |
| 1680 | // Class loader = [???][dex_elements][image dex files] |
| 1681 | // Need to ensure that [???][dex_elements] == [A][B][C]. |
| 1682 | // For each class loader, PathClassLoader, the laoder checks the parent first. Also the logic |
| 1683 | // for PathClassLoader does this by looping through the array of dex files. To ensure they |
| 1684 | // resolve the same way, simply flatten the hierarchy in the way the resolution order would be, |
| 1685 | // and check that the dex file names are the same. |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1686 | if (IsBootClassLoader(soa, image_class_loader.Get())) { |
| 1687 | *error_msg = "Unexpected BootClassLoader in app image"; |
| 1688 | return false; |
| 1689 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1690 | std::list<ObjPtr<mirror::String>> image_dex_file_names; |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1691 | std::string temp_error_msg; |
| 1692 | if (!FlattenPathClassLoader(image_class_loader.Get(), &image_dex_file_names, &temp_error_msg)) { |
| 1693 | *error_msg = StringPrintf("Failed to flatten image class loader hierarchy '%s'", |
| 1694 | temp_error_msg.c_str()); |
| 1695 | return false; |
| 1696 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1697 | std::list<ObjPtr<mirror::String>> loader_dex_file_names; |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1698 | if (!FlattenPathClassLoader(class_loader.Get(), &loader_dex_file_names, &temp_error_msg)) { |
| 1699 | *error_msg = StringPrintf("Failed to flatten class loader hierarchy '%s'", |
| 1700 | temp_error_msg.c_str()); |
| 1701 | return false; |
| 1702 | } |
| 1703 | // Add the temporary dex path list elements at the end. |
| 1704 | auto elements = soa.Decode<mirror::ObjectArray<mirror::Object>>(dex_elements); |
| 1705 | for (size_t i = 0, num_elems = elements->GetLength(); i < num_elems; ++i) { |
| 1706 | ObjPtr<mirror::Object> element = elements->GetWithoutChecks(i); |
| 1707 | if (element != nullptr) { |
| 1708 | // 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] | 1709 | ObjPtr<mirror::String> name; |
| 1710 | if (GetDexPathListElementName(element, &name) && name != nullptr) { |
| 1711 | loader_dex_file_names.push_back(name); |
| 1712 | } |
Nicolas Geoffray | 1df3b55 | 2016-04-26 18:30:31 +0100 | [diff] [blame] | 1713 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1714 | } |
| 1715 | // Ignore the number of image dex files since we are adding those to the class loader anyways. |
| 1716 | CHECK_GE(static_cast<size_t>(image_dex_file_names.size()), |
| 1717 | static_cast<size_t>(dex_caches->GetLength())); |
| 1718 | size_t image_count = image_dex_file_names.size() - dex_caches->GetLength(); |
| 1719 | // Check that the dex file names match. |
| 1720 | bool equal = image_count == loader_dex_file_names.size(); |
| 1721 | if (equal) { |
| 1722 | auto it1 = image_dex_file_names.begin(); |
| 1723 | auto it2 = loader_dex_file_names.begin(); |
| 1724 | for (size_t i = 0; equal && i < image_count; ++i, ++it1, ++it2) { |
| 1725 | equal = equal && (*it1)->Equals(*it2); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1726 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1727 | } |
| 1728 | if (!equal) { |
| 1729 | VLOG(image) << "Image dex files " << image_dex_file_names.size(); |
| 1730 | for (ObjPtr<mirror::String> name : image_dex_file_names) { |
| 1731 | VLOG(image) << name->ToModifiedUtf8(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1732 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1733 | VLOG(image) << "Loader dex files " << loader_dex_file_names.size(); |
| 1734 | for (ObjPtr<mirror::String> name : loader_dex_file_names) { |
| 1735 | VLOG(image) << name->ToModifiedUtf8(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1736 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1737 | *error_msg = "Rejecting application image due to class loader mismatch"; |
| 1738 | // Ignore class loader mismatch for now since these would just use possibly incorrect |
| 1739 | // 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] | 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | if (kSanityCheckObjects) { |
| 1744 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 1745 | auto* dex_cache = dex_caches->Get(i); |
| 1746 | for (size_t j = 0; j < dex_cache->NumResolvedFields(); ++j) { |
| 1747 | auto* field = dex_cache->GetResolvedField(j, image_pointer_size_); |
| 1748 | if (field != nullptr) { |
| 1749 | CHECK(field->GetDeclaringClass()->GetClass() != nullptr); |
| 1750 | } |
| 1751 | } |
| 1752 | } |
| 1753 | if (!app_image) { |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1754 | ImageSanityChecks::CheckObjects(heap, this); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1755 | } |
| 1756 | } |
| 1757 | |
| 1758 | // Set entry point to interpreter if in InterpretOnly mode. |
| 1759 | if (!runtime->IsAotCompiler() && runtime->GetInstrumentation()->InterpretOnly()) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1760 | SetInterpreterEntrypointArtMethodVisitor visitor(image_pointer_size_); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 1761 | header.VisitPackedArtMethods(&visitor, space->Begin(), image_pointer_size_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1762 | } |
| 1763 | |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 1764 | if (!app_image) { |
| 1765 | // Make the string intern table and class table immutable for boot image. |
| 1766 | // PIC app oat files may mmap a read-only copy into their own .bss section, |
| 1767 | // so enforce that the data in the boot image tables remains unchanged. |
| 1768 | // |
| 1769 | // We cannot do that for app image even after the fixup as some interned |
| 1770 | // String references may actually end up pointing to moveable Strings. |
| 1771 | uint8_t* const_section_begin = space->Begin() + header.GetBootImageConstantTablesOffset(); |
Mathieu Chartier | 8d8de0c | 2017-10-04 09:35:30 -0700 | [diff] [blame] | 1772 | CheckedCall(mprotect, |
| 1773 | "protect constant tables", |
| 1774 | const_section_begin, |
| 1775 | header.GetBootImageConstantTablesSize(), |
| 1776 | PROT_READ); |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 1777 | } |
| 1778 | |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1779 | ClassTable* class_table = nullptr; |
| 1780 | { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1781 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1782 | class_table = InsertClassTableForClassLoader(class_loader.Get()); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1783 | } |
| 1784 | // If we have a class table section, read it and use it for verification in |
| 1785 | // UpdateAppImageClassLoadersAndDexCaches. |
| 1786 | ClassTable::ClassSet temp_set; |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1787 | const ImageSection& class_table_section = header.GetClassTableSection(); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1788 | const bool added_class_table = class_table_section.Size() > 0u; |
| 1789 | if (added_class_table) { |
| 1790 | const uint64_t start_time2 = NanoTime(); |
| 1791 | size_t read_count = 0; |
| 1792 | temp_set = ClassTable::ClassSet(space->Begin() + class_table_section.Offset(), |
| 1793 | /*make copy*/false, |
| 1794 | &read_count); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1795 | VLOG(image) << "Adding class table classes took " << PrettyDuration(NanoTime() - start_time2); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1796 | } |
| 1797 | if (app_image) { |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1798 | AppImageClassLoadersAndDexCachesHelper::Update(this, |
| 1799 | space, |
| 1800 | class_loader, |
| 1801 | dex_caches, |
| 1802 | &temp_set); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1803 | // Update class loader and resolved strings. If added_class_table is false, the resolved |
| 1804 | // strings were forwarded UpdateAppImageClassLoadersAndDexCaches. |
Nicolas Geoffray | f9bf250 | 2016-12-14 14:59:04 +0000 | [diff] [blame] | 1805 | UpdateClassLoaderVisitor visitor(space, class_loader.Get()); |
| 1806 | for (const ClassTable::TableSlot& root : temp_set) { |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1807 | visitor(root.Read()); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1808 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 1809 | } |
| 1810 | if (!oat_file->GetBssGcRoots().empty()) { |
| 1811 | // Insert oat file to class table for visiting .bss GC roots. |
| 1812 | class_table->InsertOatFile(oat_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1813 | } |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1814 | if (added_class_table) { |
| 1815 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 1816 | class_table->AddClassSet(std::move(temp_set)); |
| 1817 | } |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1818 | |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1819 | if (kIsDebugBuild && app_image) { |
| 1820 | // This verification needs to happen after the classes have been added to the class loader. |
| 1821 | // Since it ensures classes are in the class table. |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1822 | VerifyAppImage(header, class_loader, dex_caches, class_table, space); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1823 | } |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1824 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1825 | VLOG(class_linker) << "Adding image space took " << PrettyDuration(NanoTime() - start_time); |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 1826 | return true; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1827 | } |
| 1828 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1829 | bool ClassLinker::ClassInClassTable(ObjPtr<mirror::Class> klass) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 1830 | ClassTable* const class_table = ClassTableForClassLoader(klass->GetClassLoader()); |
| 1831 | return class_table != nullptr && class_table->Contains(klass); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1832 | } |
| 1833 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1834 | void ClassLinker::VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 1835 | // Acquire tracing_enabled before locking class linker lock to prevent lock order violation. Since |
| 1836 | // enabling tracing requires the mutator lock, there are no race conditions here. |
| 1837 | const bool tracing_enabled = Trace::IsTracingEnabled(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 1838 | Thread* const self = Thread::Current(); |
| 1839 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 1840 | if (kUseReadBarrier) { |
| 1841 | // We do not track new roots for CC. |
| 1842 | DCHECK_EQ(0, flags & (kVisitRootFlagNewRoots | |
| 1843 | kVisitRootFlagClearRootLog | |
| 1844 | kVisitRootFlagStartLoggingNewRoots | |
| 1845 | kVisitRootFlagStopLoggingNewRoots)); |
| 1846 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1847 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1848 | // Argument for how root visiting deals with ArtField and ArtMethod roots. |
| 1849 | // There is 3 GC cases to handle: |
| 1850 | // Non moving concurrent: |
| 1851 | // 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] | 1852 | // live by the class and class roots. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1853 | // |
| 1854 | // Moving non-concurrent: |
| 1855 | // This case needs to call visit VisitNativeRoots in case the classes or dex cache arrays move. |
| 1856 | // To prevent missing roots, this case needs to ensure that there is no |
| 1857 | // suspend points between the point which we allocate ArtMethod arrays and place them in a |
| 1858 | // class which is in the class table. |
| 1859 | // |
| 1860 | // Moving concurrent: |
| 1861 | // Need to make sure to not copy ArtMethods without doing read barriers since the roots are |
| 1862 | // 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] | 1863 | // |
| 1864 | // Use an unbuffered visitor since the class table uses a temporary GcRoot for holding decoded |
| 1865 | // ClassTable::TableSlot. The buffered root visiting would access a stale stack location for |
| 1866 | // these objects. |
| 1867 | UnbufferedRootVisitor root_visitor(visitor, RootInfo(kRootStickyClass)); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1868 | boot_class_table_->VisitRoots(root_visitor); |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 1869 | // 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] | 1870 | if ((flags & kVisitRootFlagClassLoader) != 0 || tracing_enabled) { |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 1871 | for (const ClassLoaderData& data : class_loaders_) { |
| 1872 | GcRoot<mirror::Object> root(GcRoot<mirror::Object>(self->DecodeJObject(data.weak_root))); |
| 1873 | root.VisitRoot(visitor, RootInfo(kRootVMInternal)); |
| 1874 | } |
| 1875 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 1876 | } else if (!kUseReadBarrier && (flags & kVisitRootFlagNewRoots) != 0) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1877 | for (auto& root : new_class_roots_) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1878 | ObjPtr<mirror::Class> old_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1879 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1880 | ObjPtr<mirror::Class> new_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 1881 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 1882 | CHECK_EQ(new_ref, old_ref); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1883 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 1884 | for (const OatFile* oat_file : new_bss_roots_boot_oat_files_) { |
| 1885 | for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) { |
| 1886 | ObjPtr<mirror::Object> old_ref = root.Read<kWithoutReadBarrier>(); |
| 1887 | if (old_ref != nullptr) { |
| 1888 | DCHECK(old_ref->IsClass()); |
| 1889 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
| 1890 | ObjPtr<mirror::Object> new_ref = root.Read<kWithoutReadBarrier>(); |
| 1891 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 1892 | CHECK_EQ(new_ref, old_ref); |
| 1893 | } |
| 1894 | } |
| 1895 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1896 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 1897 | if (!kUseReadBarrier && (flags & kVisitRootFlagClearRootLog) != 0) { |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1898 | new_class_roots_.clear(); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 1899 | new_bss_roots_boot_oat_files_.clear(); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1900 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 1901 | if (!kUseReadBarrier && (flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 1902 | log_new_roots_ = true; |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 1903 | } else if (!kUseReadBarrier && (flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 1904 | log_new_roots_ = false; |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1905 | } |
| 1906 | // We deliberately ignore the class roots in the image since we |
| 1907 | // handle image roots by using the MS/CMS rescanning of dirty cards. |
| 1908 | } |
| 1909 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1910 | // Keep in sync with InitCallback. Anything we visit, we need to |
| 1911 | // reinit references to when reinitializing a ClassLinker from a |
| 1912 | // mapped image. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1913 | void ClassLinker::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 1914 | class_roots_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1915 | VisitClassRoots(visitor, flags); |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 1916 | array_iftable_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); |
Mathieu Chartier | 6cfc2c0 | 2015-10-12 15:06:16 -0700 | [diff] [blame] | 1917 | // Instead of visiting the find_array_class_cache_ drop it so that it doesn't prevent class |
| 1918 | // unloading if we are marking roots. |
| 1919 | DropFindArrayClassCache(); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1920 | } |
| 1921 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 1922 | class VisitClassLoaderClassesVisitor : public ClassLoaderVisitor { |
| 1923 | public: |
| 1924 | explicit VisitClassLoaderClassesVisitor(ClassVisitor* visitor) |
| 1925 | : visitor_(visitor), |
| 1926 | done_(false) {} |
| 1927 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1928 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1929 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) OVERRIDE { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 1930 | ClassTable* const class_table = class_loader->GetClassTable(); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 1931 | if (!done_ && class_table != nullptr) { |
| 1932 | DefiningClassLoaderFilterVisitor visitor(class_loader, visitor_); |
| 1933 | if (!class_table->Visit(visitor)) { |
| 1934 | // If the visitor ClassTable returns false it means that we don't need to continue. |
| 1935 | done_ = true; |
| 1936 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 1937 | } |
| 1938 | } |
| 1939 | |
| 1940 | private: |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 1941 | // Class visitor that limits the class visits from a ClassTable to the classes with |
| 1942 | // the provided defining class loader. This filter is used to avoid multiple visits |
| 1943 | // of the same class which can be recorded for multiple initiating class loaders. |
| 1944 | class DefiningClassLoaderFilterVisitor : public ClassVisitor { |
| 1945 | public: |
| 1946 | DefiningClassLoaderFilterVisitor(ObjPtr<mirror::ClassLoader> defining_class_loader, |
| 1947 | ClassVisitor* visitor) |
| 1948 | : defining_class_loader_(defining_class_loader), visitor_(visitor) { } |
| 1949 | |
| 1950 | bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1951 | if (klass->GetClassLoader() != defining_class_loader_) { |
| 1952 | return true; |
| 1953 | } |
| 1954 | return (*visitor_)(klass); |
| 1955 | } |
| 1956 | |
| 1957 | ObjPtr<mirror::ClassLoader> const defining_class_loader_; |
| 1958 | ClassVisitor* const visitor_; |
| 1959 | }; |
| 1960 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 1961 | ClassVisitor* const visitor_; |
| 1962 | // If done is true then we don't need to do any more visiting. |
| 1963 | bool done_; |
| 1964 | }; |
| 1965 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1966 | void ClassLinker::VisitClassesInternal(ClassVisitor* visitor) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1967 | if (boot_class_table_->Visit(*visitor)) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 1968 | VisitClassLoaderClassesVisitor loader_visitor(visitor); |
| 1969 | VisitClassLoaders(&loader_visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 1970 | } |
| 1971 | } |
| 1972 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1973 | void ClassLinker::VisitClasses(ClassVisitor* visitor) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 1974 | Thread* const self = Thread::Current(); |
| 1975 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 1976 | // Not safe to have thread suspension when we are holding a lock. |
| 1977 | if (self != nullptr) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 1978 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1979 | VisitClassesInternal(visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 1980 | } else { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1981 | VisitClassesInternal(visitor); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1982 | } |
| 1983 | } |
| 1984 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1985 | class GetClassesInToVector : public ClassVisitor { |
| 1986 | public: |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1987 | bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1988 | classes_.push_back(klass); |
| 1989 | return true; |
| 1990 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1991 | std::vector<ObjPtr<mirror::Class>> classes_; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1992 | }; |
| 1993 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1994 | class GetClassInToObjectArray : public ClassVisitor { |
| 1995 | public: |
| 1996 | explicit GetClassInToObjectArray(mirror::ObjectArray<mirror::Class>* arr) |
| 1997 | : arr_(arr), index_(0) {} |
| 1998 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1999 | bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2000 | ++index_; |
| 2001 | if (index_ <= arr_->GetLength()) { |
| 2002 | arr_->Set(index_ - 1, klass); |
| 2003 | return true; |
| 2004 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2005 | return false; |
| 2006 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2007 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2008 | bool Succeeded() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2009 | return index_ <= arr_->GetLength(); |
| 2010 | } |
| 2011 | |
| 2012 | private: |
| 2013 | mirror::ObjectArray<mirror::Class>* const arr_; |
| 2014 | int32_t index_; |
| 2015 | }; |
| 2016 | |
| 2017 | void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2018 | // TODO: it may be possible to avoid secondary storage if we iterate over dex caches. The problem |
| 2019 | // is avoiding duplicates. |
| 2020 | if (!kMovingClasses) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2021 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2022 | GetClassesInToVector accumulator; |
| 2023 | VisitClasses(&accumulator); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2024 | for (ObjPtr<mirror::Class> klass : accumulator.classes_) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2025 | if (!visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2026 | return; |
| 2027 | } |
| 2028 | } |
| 2029 | } else { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2030 | Thread* const self = Thread::Current(); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2031 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2032 | auto classes = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2033 | // We size the array assuming classes won't be added to the class table during the visit. |
| 2034 | // If this assumption fails we iterate again. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2035 | while (true) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2036 | size_t class_table_size; |
| 2037 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2038 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2039 | // Add 100 in case new classes get loaded when we are filling in the object array. |
| 2040 | class_table_size = NumZygoteClasses() + NumNonZygoteClasses() + 100; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2041 | } |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 2042 | ObjPtr<mirror::Class> class_type = mirror::Class::GetJavaLangClass(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2043 | ObjPtr<mirror::Class> array_of_class = FindArrayClass(self, &class_type); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2044 | classes.Assign( |
| 2045 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, class_table_size)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2046 | CHECK(classes != nullptr); // OOME. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2047 | GetClassInToObjectArray accumulator(classes.Get()); |
| 2048 | VisitClasses(&accumulator); |
| 2049 | if (accumulator.Succeeded()) { |
| 2050 | break; |
| 2051 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2052 | } |
| 2053 | for (int32_t i = 0; i < classes->GetLength(); ++i) { |
| 2054 | // If the class table shrank during creation of the clases array we expect null elements. If |
| 2055 | // the class table grew then the loop repeats. If classes are created after the loop has |
| 2056 | // finished then we don't visit. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2057 | ObjPtr<mirror::Class> klass = classes->Get(i); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2058 | if (klass != nullptr && !visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2059 | return; |
| 2060 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2061 | } |
| 2062 | } |
| 2063 | } |
| 2064 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2065 | ClassLinker::~ClassLinker() { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2066 | mirror::Class::ResetClass(); |
| 2067 | mirror::Constructor::ResetClass(); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 2068 | mirror::Field::ResetClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2069 | mirror::Method::ResetClass(); |
| 2070 | mirror::Reference::ResetClass(); |
| 2071 | mirror::StackTraceElement::ResetClass(); |
| 2072 | mirror::String::ResetClass(); |
| 2073 | mirror::Throwable::ResetClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2074 | mirror::BooleanArray::ResetArrayClass(); |
| 2075 | mirror::ByteArray::ResetArrayClass(); |
| 2076 | mirror::CharArray::ResetArrayClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2077 | mirror::Constructor::ResetArrayClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2078 | mirror::DoubleArray::ResetArrayClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2079 | mirror::Field::ResetArrayClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2080 | mirror::FloatArray::ResetArrayClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2081 | mirror::Method::ResetArrayClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2082 | mirror::IntArray::ResetArrayClass(); |
| 2083 | mirror::LongArray::ResetArrayClass(); |
| 2084 | mirror::ShortArray::ResetArrayClass(); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 2085 | mirror::MethodType::ResetClass(); |
| 2086 | mirror::MethodHandleImpl::ResetClass(); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 2087 | mirror::MethodHandlesLookup::ResetClass(); |
| 2088 | mirror::CallSite::ResetClass(); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 2089 | mirror::EmulatedStackFrame::ResetClass(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2090 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 2091 | for (const ClassLoaderData& data : class_loaders_) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2092 | DeleteClassLoader(self, data); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 2093 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2094 | class_loaders_.clear(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2095 | } |
| 2096 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2097 | void ClassLinker::DeleteClassLoader(Thread* self, const ClassLoaderData& data) { |
| 2098 | Runtime* const runtime = Runtime::Current(); |
| 2099 | JavaVMExt* const vm = runtime->GetJavaVM(); |
| 2100 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 2101 | // 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] | 2102 | if (runtime->GetJit() != nullptr) { |
| 2103 | jit::JitCodeCache* code_cache = runtime->GetJit()->GetCodeCache(); |
| 2104 | if (code_cache != nullptr) { |
Mathieu Chartier | cf79cf5 | 2017-07-21 11:17:57 -0700 | [diff] [blame] | 2105 | // For the JIT case, RemoveMethodsIn removes the CHA dependencies. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2106 | code_cache->RemoveMethodsIn(self, *data.allocator); |
| 2107 | } |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 2108 | } else if (cha_ != nullptr) { |
Mathieu Chartier | cf79cf5 | 2017-07-21 11:17:57 -0700 | [diff] [blame] | 2109 | // If we don't have a JIT, we need to manually remove the CHA dependencies manually. |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 2110 | cha_->RemoveDependenciesForLinearAlloc(data.allocator); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2111 | } |
| 2112 | delete data.allocator; |
| 2113 | delete data.class_table; |
| 2114 | } |
| 2115 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2116 | mirror::PointerArray* ClassLinker::AllocPointerArray(Thread* self, size_t length) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2117 | return down_cast<mirror::PointerArray*>( |
| 2118 | image_pointer_size_ == PointerSize::k64 |
| 2119 | ? static_cast<mirror::Array*>(mirror::LongArray::Alloc(self, length)) |
| 2120 | : static_cast<mirror::Array*>(mirror::IntArray::Alloc(self, length))); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2121 | } |
| 2122 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2123 | mirror::DexCache* ClassLinker::AllocDexCache(ObjPtr<mirror::String>* out_location, |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2124 | Thread* self, |
| 2125 | const DexFile& dex_file) { |
| 2126 | StackHandleScope<1> hs(self); |
| 2127 | DCHECK(out_location != nullptr); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 2128 | auto dex_cache(hs.NewHandle(ObjPtr<mirror::DexCache>::DownCast( |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2129 | GetClassRoot(kJavaLangDexCache)->AllocObject(self)))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2130 | if (dex_cache == nullptr) { |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2131 | self->AssertPendingOOMException(); |
| 2132 | return nullptr; |
| 2133 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2134 | ObjPtr<mirror::String> location = intern_table_->InternStrong(dex_file.GetLocation().c_str()); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2135 | if (location == nullptr) { |
| 2136 | self->AssertPendingOOMException(); |
| 2137 | return nullptr; |
| 2138 | } |
| 2139 | *out_location = location; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2140 | return dex_cache.Get(); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2141 | } |
| 2142 | |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2143 | mirror::DexCache* ClassLinker::AllocAndInitializeDexCache(Thread* self, |
| 2144 | const DexFile& dex_file, |
| 2145 | LinearAlloc* linear_alloc) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2146 | ObjPtr<mirror::String> location = nullptr; |
| 2147 | ObjPtr<mirror::DexCache> dex_cache = AllocDexCache(&location, self, dex_file); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2148 | if (dex_cache != nullptr) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 2149 | WriterMutexLock mu(self, *Locks::dex_lock_); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2150 | DCHECK(location != nullptr); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 2151 | mirror::DexCache::InitializeDexCache(self, |
| 2152 | dex_cache, |
| 2153 | location, |
| 2154 | &dex_file, |
| 2155 | linear_alloc, |
| 2156 | image_pointer_size_); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2157 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2158 | return dex_cache.Ptr(); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2159 | } |
| 2160 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2161 | mirror::Class* ClassLinker::AllocClass(Thread* self, |
| 2162 | ObjPtr<mirror::Class> java_lang_Class, |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2163 | uint32_t class_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2164 | DCHECK_GE(class_size, sizeof(mirror::Class)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2165 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2166 | mirror::Class::InitializeClassVisitor visitor(class_size); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2167 | ObjPtr<mirror::Object> k = kMovingClasses ? |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 2168 | heap->AllocObject<true>(self, java_lang_Class, class_size, visitor) : |
| 2169 | heap->AllocNonMovableObject<true>(self, java_lang_Class, class_size, visitor); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2170 | if (UNLIKELY(k == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2171 | self->AssertPendingOOMException(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2172 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2173 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2174 | return k->AsClass(); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 2175 | } |
| 2176 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2177 | mirror::Class* ClassLinker::AllocClass(Thread* self, uint32_t class_size) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2178 | return AllocClass(self, GetClassRoot(kJavaLangClass), class_size); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2179 | } |
| 2180 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2181 | mirror::ObjectArray<mirror::StackTraceElement>* ClassLinker::AllocStackTraceElementArray( |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2182 | Thread* self, |
| 2183 | size_t length) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2184 | return mirror::ObjectArray<mirror::StackTraceElement>::Alloc( |
| 2185 | self, GetClassRoot(kJavaLangStackTraceElementArrayClass), length); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2186 | } |
| 2187 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2188 | mirror::Class* ClassLinker::EnsureResolved(Thread* self, |
| 2189 | const char* descriptor, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2190 | ObjPtr<mirror::Class> klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2191 | DCHECK(klass != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2192 | if (kIsDebugBuild) { |
| 2193 | StackHandleScope<1> hs(self); |
| 2194 | HandleWrapperObjPtr<mirror::Class> h = hs.NewHandleWrapper(&klass); |
| 2195 | Thread::PoisonObjectPointersIfDebug(); |
| 2196 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2197 | |
| 2198 | // For temporary classes we must wait for them to be retired. |
| 2199 | if (init_done_ && klass->IsTemp()) { |
| 2200 | CHECK(!klass->IsResolved()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2201 | if (klass->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2202 | ThrowEarlierClassFailure(klass); |
| 2203 | return nullptr; |
| 2204 | } |
| 2205 | StackHandleScope<1> hs(self); |
| 2206 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 2207 | ObjectLock<mirror::Class> lock(self, h_class); |
| 2208 | // Loop and wait for the resolving thread to retire this class. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2209 | while (!h_class->IsRetired() && !h_class->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2210 | lock.WaitIgnoringInterrupts(); |
| 2211 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2212 | if (h_class->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2213 | ThrowEarlierClassFailure(h_class.Get()); |
| 2214 | return nullptr; |
| 2215 | } |
| 2216 | CHECK(h_class->IsRetired()); |
| 2217 | // Get the updated class from class table. |
Andreas Gampe | 34ee684 | 2014-12-02 15:43:52 -0800 | [diff] [blame] | 2218 | klass = LookupClass(self, descriptor, h_class.Get()->GetClassLoader()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2219 | } |
| 2220 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2221 | // Wait for the class if it has not already been linked. |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2222 | size_t index = 0; |
| 2223 | // Maximum number of yield iterations until we start sleeping. |
| 2224 | static const size_t kNumYieldIterations = 1000; |
| 2225 | // How long each sleep is in us. |
| 2226 | static const size_t kSleepDurationUS = 1000; // 1 ms. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2227 | while (!klass->IsResolved() && !klass->IsErroneousUnresolved()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2228 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2229 | HandleWrapperObjPtr<mirror::Class> h_class(hs.NewHandleWrapper(&klass)); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2230 | { |
| 2231 | ObjectTryLock<mirror::Class> lock(self, h_class); |
| 2232 | // Can not use a monitor wait here since it may block when returning and deadlock if another |
| 2233 | // thread has locked klass. |
| 2234 | if (lock.Acquired()) { |
| 2235 | // Check for circular dependencies between classes, the lock is required for SetStatus. |
| 2236 | if (!h_class->IsResolved() && h_class->GetClinitThreadId() == self->GetTid()) { |
| 2237 | ThrowClassCircularityError(h_class.Get()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2238 | mirror::Class::SetStatus(h_class, mirror::Class::kStatusErrorUnresolved, self); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2239 | return nullptr; |
| 2240 | } |
| 2241 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2242 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2243 | { |
| 2244 | // Handle wrapper deals with klass moving. |
| 2245 | ScopedThreadSuspension sts(self, kSuspended); |
| 2246 | if (index < kNumYieldIterations) { |
| 2247 | sched_yield(); |
| 2248 | } else { |
| 2249 | usleep(kSleepDurationUS); |
| 2250 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2251 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2252 | ++index; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2253 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2254 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2255 | if (klass->IsErroneousUnresolved()) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2256 | ThrowEarlierClassFailure(klass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2257 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2258 | } |
| 2259 | // Return the loaded class. No exceptions should be pending. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2260 | CHECK(klass->IsResolved()) << klass->PrettyClass(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2261 | self->AssertNoPendingException(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2262 | return klass.Ptr(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2263 | } |
| 2264 | |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2265 | typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry; |
| 2266 | |
| 2267 | // Search a collection of DexFiles for a descriptor |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2268 | ClassPathEntry FindInClassPath(const char* descriptor, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2269 | size_t hash, const std::vector<const DexFile*>& class_path) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2270 | for (const DexFile* dex_file : class_path) { |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 2271 | const DexFile::ClassDef* dex_class_def = OatDexFile::FindClassDef(*dex_file, descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2272 | if (dex_class_def != nullptr) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2273 | return ClassPathEntry(dex_file, dex_class_def); |
| 2274 | } |
| 2275 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2276 | return ClassPathEntry(nullptr, nullptr); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2277 | } |
| 2278 | |
Nicolas Geoffray | 7d8d8ff | 2016-11-02 12:38:05 +0000 | [diff] [blame] | 2279 | bool ClassLinker::FindClassInBaseDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
| 2280 | Thread* self, |
| 2281 | const char* descriptor, |
| 2282 | size_t hash, |
| 2283 | Handle<mirror::ClassLoader> class_loader, |
| 2284 | ObjPtr<mirror::Class>* result) { |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2285 | // Termination case: boot class loader. |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2286 | if (IsBootClassLoader(soa, class_loader.Get())) { |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2287 | *result = FindClassInBootClassLoaderClassPath(self, descriptor, hash); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2288 | return true; |
| 2289 | } |
| 2290 | |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2291 | if (IsPathOrDexClassLoader(soa, class_loader)) { |
| 2292 | // For regular path or dex class loader the search order is: |
| 2293 | // - parent |
| 2294 | // - class loader dex files |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2295 | |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2296 | // Handles as RegisterDexFile may allocate dex caches (and cause thread suspension). |
| 2297 | StackHandleScope<1> hs(self); |
| 2298 | Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent())); |
| 2299 | if (!FindClassInBaseDexClassLoader(soa, self, descriptor, hash, h_parent, result)) { |
| 2300 | return false; // One of the parents is not supported. |
| 2301 | } |
| 2302 | if (*result != nullptr) { |
| 2303 | return true; // Found the class up the chain. |
| 2304 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2305 | |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2306 | // Search the current class loader classpath. |
| 2307 | *result = FindClassInBaseDexClassLoaderClassPath(soa, descriptor, hash, class_loader); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2308 | return true; |
| 2309 | } |
| 2310 | |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2311 | if (IsDelegateLastClassLoader(soa, class_loader)) { |
| 2312 | // For delegate last, the search order is: |
| 2313 | // - boot class path |
| 2314 | // - class loader dex files |
| 2315 | // - parent |
| 2316 | *result = FindClassInBootClassLoaderClassPath(self, descriptor, hash); |
| 2317 | if (*result != nullptr) { |
| 2318 | return true; // The class is part of the boot class path. |
| 2319 | } |
| 2320 | |
| 2321 | *result = FindClassInBaseDexClassLoaderClassPath(soa, descriptor, hash, class_loader); |
| 2322 | if (*result != nullptr) { |
| 2323 | return true; // Found the class in the current class loader |
| 2324 | } |
| 2325 | |
| 2326 | // Handles as RegisterDexFile may allocate dex caches (and cause thread suspension). |
| 2327 | StackHandleScope<1> hs(self); |
| 2328 | Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent())); |
| 2329 | return FindClassInBaseDexClassLoader(soa, self, descriptor, hash, h_parent, result); |
| 2330 | } |
| 2331 | |
| 2332 | // Unsupported class loader. |
| 2333 | *result = nullptr; |
| 2334 | return false; |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | // Finds the class in the boot class loader. |
| 2338 | // If the class is found the method returns the resolved class. Otherwise it returns null. |
| 2339 | ObjPtr<mirror::Class> ClassLinker::FindClassInBootClassLoaderClassPath(Thread* self, |
| 2340 | const char* descriptor, |
| 2341 | size_t hash) { |
| 2342 | ObjPtr<mirror::Class> result = nullptr; |
| 2343 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
| 2344 | if (pair.second != nullptr) { |
| 2345 | ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, nullptr); |
| 2346 | if (klass != nullptr) { |
| 2347 | result = EnsureResolved(self, descriptor, klass); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2348 | } else { |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2349 | result = DefineClass(self, |
| 2350 | descriptor, |
| 2351 | hash, |
| 2352 | ScopedNullHandle<mirror::ClassLoader>(), |
| 2353 | *pair.first, |
| 2354 | *pair.second); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2355 | } |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2356 | if (result == nullptr) { |
| 2357 | CHECK(self->IsExceptionPending()) << descriptor; |
| 2358 | self->ClearException(); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2359 | } |
| 2360 | } |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2361 | return result; |
| 2362 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2363 | |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2364 | ObjPtr<mirror::Class> ClassLinker::FindClassInBaseDexClassLoaderClassPath( |
| 2365 | ScopedObjectAccessAlreadyRunnable& soa, |
| 2366 | const char* descriptor, |
| 2367 | size_t hash, |
| 2368 | Handle<mirror::ClassLoader> class_loader) { |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2369 | CHECK(IsPathOrDexClassLoader(soa, class_loader) || IsDelegateLastClassLoader(soa, class_loader)) |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2370 | << "Unexpected class loader for descriptor " << descriptor; |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2371 | |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2372 | Thread* self = soa.Self(); |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2373 | ArtField* const cookie_field = |
| 2374 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2375 | ArtField* const dex_file_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2376 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2377 | ObjPtr<mirror::Object> dex_path_list = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2378 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList)-> |
| 2379 | GetObject(class_loader.Get()); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2380 | if (dex_path_list != nullptr && dex_file_field != nullptr && cookie_field != nullptr) { |
| 2381 | // 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] | 2382 | ObjPtr<mirror::Object> dex_elements_obj = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2383 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements)-> |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2384 | GetObject(dex_path_list); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2385 | // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look |
| 2386 | // at the mCookie which is a DexFile vector. |
| 2387 | if (dex_elements_obj != nullptr) { |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2388 | StackHandleScope<1> hs(self); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2389 | Handle<mirror::ObjectArray<mirror::Object>> dex_elements = |
| 2390 | hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>()); |
| 2391 | for (int32_t i = 0; i < dex_elements->GetLength(); ++i) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2392 | ObjPtr<mirror::Object> element = dex_elements->GetWithoutChecks(i); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2393 | if (element == nullptr) { |
| 2394 | // Should never happen, fall back to java code to throw a NPE. |
| 2395 | break; |
| 2396 | } |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2397 | ObjPtr<mirror::Object> dex_file = dex_file_field->GetObject(element); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2398 | if (dex_file != nullptr) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2399 | ObjPtr<mirror::LongArray> long_array = cookie_field->GetObject(dex_file)->AsLongArray(); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2400 | if (long_array == nullptr) { |
| 2401 | // This should never happen so log a warning. |
| 2402 | LOG(WARNING) << "Null DexFile::mCookie for " << descriptor; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2403 | break; |
| 2404 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2405 | int32_t long_array_size = long_array->GetLength(); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 2406 | // First element is the oat file. |
| 2407 | for (int32_t j = kDexFileIndexStart; j < long_array_size; ++j) { |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2408 | const DexFile* cp_dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>( |
| 2409 | long_array->GetWithoutChecks(j))); |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 2410 | const DexFile::ClassDef* dex_class_def = |
| 2411 | OatDexFile::FindClassDef(*cp_dex_file, descriptor, hash); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2412 | if (dex_class_def != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2413 | ObjPtr<mirror::Class> klass = DefineClass(self, |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2414 | descriptor, |
| 2415 | hash, |
| 2416 | class_loader, |
| 2417 | *cp_dex_file, |
| 2418 | *dex_class_def); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2419 | if (klass == nullptr) { |
| 2420 | CHECK(self->IsExceptionPending()) << descriptor; |
| 2421 | self->ClearException(); |
| 2422 | // TODO: Is it really right to break here, and not check the other dex files? |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2423 | return nullptr; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2424 | } |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2425 | return klass; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2426 | } |
| 2427 | } |
| 2428 | } |
| 2429 | } |
| 2430 | } |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2431 | self->AssertNoPendingException(); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2432 | } |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2433 | return nullptr; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2434 | } |
| 2435 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2436 | mirror::Class* ClassLinker::FindClass(Thread* self, |
| 2437 | const char* descriptor, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2438 | Handle<mirror::ClassLoader> class_loader) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 2439 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2440 | DCHECK(self != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2441 | self->AssertNoPendingException(); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 2442 | self->PoisonObjectPointers(); // For DefineClass, CreateArrayClass, etc... |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 2443 | if (descriptor[1] == '\0') { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2444 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 2445 | // for primitive classes that aren't backed by dex files. |
| 2446 | return FindPrimitiveClass(descriptor[0]); |
| 2447 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2448 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2449 | // Find the class in the loaded classes table. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2450 | ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, class_loader.Get()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2451 | if (klass != nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2452 | return EnsureResolved(self, descriptor, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2453 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2454 | // Class is not yet loaded. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2455 | if (descriptor[0] != '[' && class_loader == nullptr) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2456 | // 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] | 2457 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2458 | if (pair.second != nullptr) { |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 2459 | return DefineClass(self, |
| 2460 | descriptor, |
| 2461 | hash, |
| 2462 | ScopedNullHandle<mirror::ClassLoader>(), |
| 2463 | *pair.first, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2464 | *pair.second); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2465 | } else { |
| 2466 | // The boot class loader is searched ahead of the application class loader, failures are |
| 2467 | // expected and will be wrapped in a ClassNotFoundException. Use the pre-allocated error to |
| 2468 | // trigger the chaining with a proper stack trace. |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2469 | ObjPtr<mirror::Throwable> pre_allocated = |
| 2470 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2471 | self->SetException(pre_allocated); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2472 | return nullptr; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2473 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2474 | } |
| 2475 | ObjPtr<mirror::Class> result_ptr; |
| 2476 | bool descriptor_equals; |
| 2477 | if (descriptor[0] == '[') { |
| 2478 | result_ptr = CreateArrayClass(self, descriptor, hash, class_loader); |
| 2479 | DCHECK_EQ(result_ptr == nullptr, self->IsExceptionPending()); |
| 2480 | DCHECK(result_ptr == nullptr || result_ptr->DescriptorEquals(descriptor)); |
| 2481 | descriptor_equals = true; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2482 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2483 | ScopedObjectAccessUnchecked soa(self); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2484 | bool known_hierarchy = |
| 2485 | FindClassInBaseDexClassLoader(soa, self, descriptor, hash, class_loader, &result_ptr); |
| 2486 | if (result_ptr != nullptr) { |
| 2487 | // The chain was understood and we found the class. We still need to add the class to |
| 2488 | // the class table to protect from racy programs that can try and redefine the path list |
| 2489 | // which would change the Class<?> returned for subsequent evaluation of const-class. |
| 2490 | DCHECK(known_hierarchy); |
| 2491 | DCHECK(result_ptr->DescriptorEquals(descriptor)); |
| 2492 | descriptor_equals = true; |
| 2493 | } else { |
| 2494 | // Either the chain wasn't understood or the class wasn't found. |
| 2495 | // |
| 2496 | // If the chain was understood but we did not find the class, let the Java-side |
| 2497 | // rediscover all this and throw the exception with the right stack trace. Note that |
| 2498 | // the Java-side could still succeed for racy programs if another thread is actively |
| 2499 | // modifying the class loader's path list. |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2500 | |
Calin Juravle | ccd5695 | 2016-12-15 17:57:38 +0000 | [diff] [blame] | 2501 | if (!self->CanCallIntoJava()) { |
| 2502 | // Oops, we can't call into java so we can't run actual class-loader code. |
| 2503 | // This is true for e.g. for the compiler (jit or aot). |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2504 | ObjPtr<mirror::Throwable> pre_allocated = |
| 2505 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 2506 | self->SetException(pre_allocated); |
Vladimir Marko | 2c8c6b6 | 2016-12-01 17:42:00 +0000 | [diff] [blame] | 2507 | return nullptr; |
| 2508 | } |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2509 | |
Vladimir Marko | 5fdd778 | 2017-04-20 11:26:03 +0100 | [diff] [blame] | 2510 | // Inlined DescriptorToDot(descriptor) with extra validation. |
| 2511 | // |
| 2512 | // Throw NoClassDefFoundError early rather than potentially load a class only to fail |
| 2513 | // the DescriptorEquals() check below and give a confusing error message. For example, |
| 2514 | // when native code erroneously calls JNI GetFieldId() with signature "java/lang/String" |
| 2515 | // instead of "Ljava/lang/String;", the message below using the "dot" names would be |
| 2516 | // "class loader [...] returned class java.lang.String instead of java.lang.String". |
| 2517 | size_t descriptor_length = strlen(descriptor); |
| 2518 | if (UNLIKELY(descriptor[0] != 'L') || |
| 2519 | UNLIKELY(descriptor[descriptor_length - 1] != ';') || |
| 2520 | UNLIKELY(memchr(descriptor + 1, '.', descriptor_length - 2) != nullptr)) { |
| 2521 | ThrowNoClassDefFoundError("Invalid descriptor: %s.", descriptor); |
| 2522 | return nullptr; |
| 2523 | } |
| 2524 | std::string class_name_string(descriptor + 1, descriptor_length - 2); |
| 2525 | std::replace(class_name_string.begin(), class_name_string.end(), '/', '.'); |
| 2526 | |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2527 | ScopedLocalRef<jobject> class_loader_object( |
| 2528 | soa.Env(), soa.AddLocalReference<jobject>(class_loader.Get())); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2529 | ScopedLocalRef<jobject> result(soa.Env(), nullptr); |
| 2530 | { |
| 2531 | ScopedThreadStateChange tsc(self, kNative); |
| 2532 | ScopedLocalRef<jobject> class_name_object( |
| 2533 | soa.Env(), soa.Env()->NewStringUTF(class_name_string.c_str())); |
| 2534 | if (class_name_object.get() == nullptr) { |
| 2535 | DCHECK(self->IsExceptionPending()); // OOME. |
| 2536 | return nullptr; |
| 2537 | } |
| 2538 | CHECK(class_loader_object.get() != nullptr); |
| 2539 | result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(), |
| 2540 | WellKnownClasses::java_lang_ClassLoader_loadClass, |
| 2541 | class_name_object.get())); |
| 2542 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2543 | if (result.get() == nullptr && !self->IsExceptionPending()) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2544 | // broken loader - throw NPE to be compatible with Dalvik |
| 2545 | ThrowNullPointerException(StringPrintf("ClassLoader.loadClass returned null for %s", |
| 2546 | class_name_string.c_str()).c_str()); |
| 2547 | return nullptr; |
| 2548 | } |
| 2549 | result_ptr = soa.Decode<mirror::Class>(result.get()); |
| 2550 | // Check the name of the returned class. |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2551 | descriptor_equals = (result_ptr != nullptr) && result_ptr->DescriptorEquals(descriptor); |
Vladimir Marko | 2c8c6b6 | 2016-12-01 17:42:00 +0000 | [diff] [blame] | 2552 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2553 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2554 | |
| 2555 | if (self->IsExceptionPending()) { |
| 2556 | // If the ClassLoader threw or array class allocation failed, pass that exception up. |
| 2557 | // However, to comply with the RI behavior, first check if another thread succeeded. |
| 2558 | result_ptr = LookupClass(self, descriptor, hash, class_loader.Get()); |
| 2559 | if (result_ptr != nullptr && !result_ptr->IsErroneous()) { |
| 2560 | self->ClearException(); |
| 2561 | return EnsureResolved(self, descriptor, result_ptr); |
| 2562 | } |
| 2563 | return nullptr; |
| 2564 | } |
| 2565 | |
| 2566 | // Try to insert the class to the class table, checking for mismatch. |
| 2567 | ObjPtr<mirror::Class> old; |
| 2568 | { |
| 2569 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2570 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader.Get()); |
| 2571 | old = class_table->Lookup(descriptor, hash); |
| 2572 | if (old == nullptr) { |
| 2573 | old = result_ptr; // For the comparison below, after releasing the lock. |
| 2574 | if (descriptor_equals) { |
| 2575 | class_table->InsertWithHash(result_ptr.Ptr(), hash); |
| 2576 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader.Get()); |
| 2577 | } // else throw below, after releasing the lock. |
| 2578 | } |
| 2579 | } |
| 2580 | if (UNLIKELY(old != result_ptr)) { |
| 2581 | // Return `old` (even if `!descriptor_equals`) to mimic the RI behavior for parallel |
| 2582 | // capable class loaders. (All class loaders are considered parallel capable on Android.) |
| 2583 | mirror::Class* loader_class = class_loader->GetClass(); |
| 2584 | const char* loader_class_name = |
| 2585 | loader_class->GetDexFile().StringByTypeIdx(loader_class->GetDexTypeIndex()); |
| 2586 | LOG(WARNING) << "Initiating class loader of type " << DescriptorToDot(loader_class_name) |
| 2587 | << " is not well-behaved; it returned a different Class for racing loadClass(\"" |
| 2588 | << DescriptorToDot(descriptor) << "\")."; |
| 2589 | return EnsureResolved(self, descriptor, old); |
| 2590 | } |
| 2591 | if (UNLIKELY(!descriptor_equals)) { |
| 2592 | std::string result_storage; |
| 2593 | const char* result_name = result_ptr->GetDescriptor(&result_storage); |
| 2594 | std::string loader_storage; |
| 2595 | const char* loader_class_name = class_loader->GetClass()->GetDescriptor(&loader_storage); |
| 2596 | ThrowNoClassDefFoundError( |
| 2597 | "Initiating class loader of type %s returned class %s instead of %s.", |
| 2598 | DescriptorToDot(loader_class_name).c_str(), |
| 2599 | DescriptorToDot(result_name).c_str(), |
| 2600 | DescriptorToDot(descriptor).c_str()); |
| 2601 | return nullptr; |
| 2602 | } |
| 2603 | // success, return mirror::Class* |
| 2604 | return result_ptr.Ptr(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2605 | } |
| 2606 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2607 | mirror::Class* ClassLinker::DefineClass(Thread* self, |
| 2608 | const char* descriptor, |
| 2609 | size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2610 | Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2611 | const DexFile& dex_file, |
| 2612 | const DexFile::ClassDef& dex_class_def) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2613 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2614 | auto klass = hs.NewHandle<mirror::Class>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2615 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2616 | // Load the class from the dex file. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2617 | if (UNLIKELY(!init_done_)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2618 | // finish up init of hand crafted class_roots_ |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2619 | if (strcmp(descriptor, "Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2620 | klass.Assign(GetClassRoot(kJavaLangObject)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2621 | } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2622 | klass.Assign(GetClassRoot(kJavaLangClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2623 | } else if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2624 | klass.Assign(GetClassRoot(kJavaLangString)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 2625 | } else if (strcmp(descriptor, "Ljava/lang/ref/Reference;") == 0) { |
| 2626 | klass.Assign(GetClassRoot(kJavaLangRefReference)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2627 | } else if (strcmp(descriptor, "Ljava/lang/DexCache;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2628 | klass.Assign(GetClassRoot(kJavaLangDexCache)); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 2629 | } else if (strcmp(descriptor, "Ldalvik/system/ClassExt;") == 0) { |
| 2630 | klass.Assign(GetClassRoot(kDalvikSystemClassExt)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2631 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2632 | } |
| 2633 | |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2634 | if (klass == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2635 | // Allocate a class with the status of not ready. |
| 2636 | // Interface object should get the right size here. Regular class will |
| 2637 | // figure out the right size later and be replaced with one of the right |
| 2638 | // size when the class becomes resolved. |
Nicolas Geoffray | abadf02 | 2017-08-03 08:25:41 +0000 | [diff] [blame] | 2639 | klass.Assign(AllocClass(self, SizeOfClassWithoutEmbeddedTables(dex_file, dex_class_def))); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2640 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2641 | if (UNLIKELY(klass == nullptr)) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2642 | self->AssertPendingOOMException(); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2643 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2644 | } |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2645 | // Get the real dex file. This will return the input if there aren't any callbacks or they do |
| 2646 | // nothing. |
| 2647 | DexFile const* new_dex_file = nullptr; |
| 2648 | DexFile::ClassDef const* new_class_def = nullptr; |
| 2649 | // TODO We should ideally figure out some way to move this after we get a lock on the klass so it |
| 2650 | // will only be called once. |
| 2651 | Runtime::Current()->GetRuntimeCallbacks()->ClassPreDefine(descriptor, |
| 2652 | klass, |
| 2653 | class_loader, |
| 2654 | dex_file, |
| 2655 | dex_class_def, |
| 2656 | &new_dex_file, |
| 2657 | &new_class_def); |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 2658 | // Check to see if an exception happened during runtime callbacks. Return if so. |
| 2659 | if (self->IsExceptionPending()) { |
| 2660 | return nullptr; |
| 2661 | } |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2662 | ObjPtr<mirror::DexCache> dex_cache = RegisterDexFile(*new_dex_file, class_loader.Get()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2663 | if (dex_cache == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 2664 | self->AssertPendingException(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2665 | return nullptr; |
| 2666 | } |
| 2667 | klass->SetDexCache(dex_cache); |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2668 | SetupClass(*new_dex_file, *new_class_def, klass, class_loader.Get()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2669 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 2670 | // Mark the string class by setting its access flag. |
| 2671 | if (UNLIKELY(!init_done_)) { |
| 2672 | if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
| 2673 | klass->SetStringClass(); |
| 2674 | } |
| 2675 | } |
| 2676 | |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 2677 | ObjectLock<mirror::Class> lock(self, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2678 | klass->SetClinitThreadId(self->GetTid()); |
Mathieu Chartier | 1e4841e | 2016-12-15 14:21:04 -0800 | [diff] [blame] | 2679 | // Make sure we have a valid empty iftable even if there are errors. |
| 2680 | klass->SetIfTable(GetClassRoot(kJavaLangObject)->GetIfTable()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2681 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2682 | // Add the newly loaded class to the loaded classes table. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2683 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, klass.Get(), hash); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2684 | if (existing != nullptr) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2685 | // We failed to insert because we raced with another thread. Calling EnsureResolved may cause |
| 2686 | // this thread to block. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2687 | return EnsureResolved(self, descriptor, existing); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2688 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2689 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2690 | // Load the fields and other things after we are inserted in the table. This is so that we don't |
| 2691 | // end up allocating unfree-able linear alloc resources and then lose the race condition. The |
| 2692 | // other reason is that the field roots are only visited from the class table. So we need to be |
| 2693 | // inserted before we allocate / fill in these fields. |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2694 | LoadClass(self, *new_dex_file, *new_class_def, klass); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2695 | if (self->IsExceptionPending()) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2696 | VLOG(class_linker) << self->GetException()->Dump(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2697 | // An exception occured during load, set status to erroneous while holding klass' lock in case |
| 2698 | // notification is necessary. |
| 2699 | if (!klass->IsErroneous()) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2700 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorUnresolved, self); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2701 | } |
| 2702 | return nullptr; |
| 2703 | } |
| 2704 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2705 | // Finish loading (if necessary) by finding parents |
| 2706 | CHECK(!klass->IsLoaded()); |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2707 | if (!LoadSuperAndInterfaces(klass, *new_dex_file)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2708 | // Loading failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2709 | if (!klass->IsErroneous()) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2710 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorUnresolved, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2711 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2712 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2713 | } |
| 2714 | CHECK(klass->IsLoaded()); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 2715 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 2716 | // At this point the class is loaded. Publish a ClassLoad event. |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 2717 | // 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] | 2718 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(klass); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 2719 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2720 | // Link the class (if necessary) |
| 2721 | CHECK(!klass->IsResolved()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2722 | // TODO: Use fast jobjects? |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2723 | auto interfaces = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2724 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 2725 | MutableHandle<mirror::Class> h_new_class = hs.NewHandle<mirror::Class>(nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2726 | if (!LinkClass(self, descriptor, klass, interfaces, &h_new_class)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2727 | // Linking failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2728 | if (!klass->IsErroneous()) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2729 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorUnresolved, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2730 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2731 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2732 | } |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 2733 | self->AssertNoPendingException(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2734 | CHECK(h_new_class != nullptr) << descriptor; |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2735 | CHECK(h_new_class->IsResolved() && !h_new_class->IsErroneousResolved()) << descriptor; |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2736 | |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 2737 | // Instrumentation may have updated entrypoints for all methods of all |
| 2738 | // classes. However it could not update methods of this class while we |
| 2739 | // were loading it. Now the class is resolved, we can update entrypoints |
| 2740 | // as required by instrumentation. |
| 2741 | if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) { |
| 2742 | // We must be in the kRunnable state to prevent instrumentation from |
| 2743 | // suspending all threads to update entrypoints while we are doing it |
| 2744 | // for this class. |
| 2745 | DCHECK_EQ(self->GetState(), kRunnable); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 2746 | Runtime::Current()->GetInstrumentation()->InstallStubsForClass(h_new_class.Get()); |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 2747 | } |
| 2748 | |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2749 | /* |
| 2750 | * We send CLASS_PREPARE events to the debugger from here. The |
| 2751 | * definition of "preparation" is creating the static fields for a |
| 2752 | * class and initializing them to the standard default values, but not |
| 2753 | * executing any code (that comes later, during "initialization"). |
| 2754 | * |
| 2755 | * We did the static preparation in LinkClass. |
| 2756 | * |
| 2757 | * The class has been prepared and resolved but possibly not yet verified |
| 2758 | * at this point. |
| 2759 | */ |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 2760 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(klass, h_new_class); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2761 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 2762 | // Notify native debugger of the new class and its layout. |
| 2763 | jit::Jit::NewTypeLoadedIfUsingJit(h_new_class.Get()); |
| 2764 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 2765 | return h_new_class.Get(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2766 | } |
| 2767 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2768 | uint32_t ClassLinker::SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, |
| 2769 | const DexFile::ClassDef& dex_class_def) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2770 | const uint8_t* class_data = dex_file.GetClassData(dex_class_def); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2771 | size_t num_ref = 0; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2772 | size_t num_8 = 0; |
| 2773 | size_t num_16 = 0; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2774 | size_t num_32 = 0; |
| 2775 | size_t num_64 = 0; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2776 | if (class_data != nullptr) { |
Vladimir Marko | 879d27b | 2016-03-15 20:31:50 +0000 | [diff] [blame] | 2777 | // We allow duplicate definitions of the same field in a class_data_item |
| 2778 | // but ignore the repeated indexes here, b/21868015. |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 2779 | uint32_t last_field_idx = dex::kDexNoIndex; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2780 | for (ClassDataItemIterator it(dex_file, class_data); it.HasNextStaticField(); it.Next()) { |
Vladimir Marko | 879d27b | 2016-03-15 20:31:50 +0000 | [diff] [blame] | 2781 | uint32_t field_idx = it.GetMemberIndex(); |
| 2782 | // Ordering enforced by DexFileVerifier. |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 2783 | DCHECK(last_field_idx == dex::kDexNoIndex || last_field_idx <= field_idx); |
Vladimir Marko | 879d27b | 2016-03-15 20:31:50 +0000 | [diff] [blame] | 2784 | if (UNLIKELY(field_idx == last_field_idx)) { |
| 2785 | continue; |
| 2786 | } |
| 2787 | last_field_idx = field_idx; |
| 2788 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2789 | const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2790 | char c = descriptor[0]; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2791 | switch (c) { |
| 2792 | case 'L': |
| 2793 | case '[': |
| 2794 | num_ref++; |
| 2795 | break; |
| 2796 | case 'J': |
| 2797 | case 'D': |
| 2798 | num_64++; |
| 2799 | break; |
| 2800 | case 'I': |
| 2801 | case 'F': |
| 2802 | num_32++; |
| 2803 | break; |
| 2804 | case 'S': |
| 2805 | case 'C': |
| 2806 | num_16++; |
| 2807 | break; |
| 2808 | case 'B': |
| 2809 | case 'Z': |
| 2810 | num_8++; |
| 2811 | break; |
| 2812 | default: |
| 2813 | LOG(FATAL) << "Unknown descriptor: " << c; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 2814 | UNREACHABLE(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2815 | } |
| 2816 | } |
| 2817 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2818 | return mirror::Class::ComputeClassSize(false, |
| 2819 | 0, |
| 2820 | num_8, |
| 2821 | num_16, |
| 2822 | num_32, |
| 2823 | num_64, |
| 2824 | num_ref, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2825 | image_pointer_size_); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2826 | } |
| 2827 | |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2828 | // Special case to get oat code without overwriting a trampoline. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2829 | const void* ClassLinker::GetQuickOatCodeFor(ArtMethod* method) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2830 | CHECK(method->IsInvokable()) << method->PrettyMethod(); |
Nicolas Geoffray | a7a4759 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 2831 | if (method->IsProxyMethod()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2832 | return GetQuickProxyInvokeHandler(); |
Jeff Hao | 8df6cea | 2013-07-29 13:54:48 -0700 | [diff] [blame] | 2833 | } |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 2834 | auto* code = method->GetOatMethodQuickCode(GetImagePointerSize()); |
| 2835 | if (code != nullptr) { |
| 2836 | return code; |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame] | 2837 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2838 | if (method->IsNative()) { |
| 2839 | // No code and native? Use generic trampoline. |
| 2840 | return GetQuickGenericJniStub(); |
| 2841 | } |
| 2842 | return GetQuickToInterpreterBridge(); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2843 | } |
| 2844 | |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2845 | bool ClassLinker::ShouldUseInterpreterEntrypoint(ArtMethod* method, const void* quick_code) { |
| 2846 | if (UNLIKELY(method->IsNative() || method->IsProxyMethod())) { |
| 2847 | return false; |
| 2848 | } |
| 2849 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2850 | if (quick_code == nullptr) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2851 | return true; |
| 2852 | } |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2853 | |
| 2854 | Runtime* runtime = Runtime::Current(); |
| 2855 | instrumentation::Instrumentation* instr = runtime->GetInstrumentation(); |
| 2856 | if (instr->InterpretOnly()) { |
| 2857 | return true; |
| 2858 | } |
| 2859 | |
| 2860 | if (runtime->GetClassLinker()->IsQuickToInterpreterBridge(quick_code)) { |
| 2861 | // Doing this check avoids doing compiled/interpreter transitions. |
| 2862 | return true; |
| 2863 | } |
| 2864 | |
| 2865 | if (Dbg::IsForcedInterpreterNeededForCalling(Thread::Current(), method)) { |
| 2866 | // Force the use of interpreter when it is required by the debugger. |
| 2867 | return true; |
| 2868 | } |
| 2869 | |
Alex Light | 8f34aba | 2017-10-09 13:46:32 -0700 | [diff] [blame] | 2870 | if (Thread::Current()->IsAsyncExceptionPending()) { |
| 2871 | // Force use of interpreter to handle async-exceptions |
| 2872 | return true; |
| 2873 | } |
| 2874 | |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 2875 | if (runtime->IsJavaDebuggable()) { |
| 2876 | // For simplicity, we ignore precompiled code and go to the interpreter |
| 2877 | // assuming we don't already have jitted code. |
| 2878 | // We could look at the oat file where `quick_code` is being defined, |
| 2879 | // and check whether it's been compiled debuggable, but we decided to |
| 2880 | // only rely on the JIT for debuggable apps. |
Alex Light | 6b16d89 | 2016-11-11 11:21:04 -0800 | [diff] [blame] | 2881 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 2882 | return (jit == nullptr) || !jit->GetCodeCache()->ContainsPc(quick_code); |
| 2883 | } |
| 2884 | |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 2885 | if (runtime->IsNativeDebuggable()) { |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 2886 | DCHECK(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse()); |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 2887 | // If we are doing native debugging, ignore application's AOT code, |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 2888 | // since we want to JIT it (at first use) with extra stackmaps for native |
| 2889 | // debugging. We keep however all AOT code from the boot image, |
| 2890 | // since the JIT-at-first-use is blocking and would result in non-negligible |
| 2891 | // startup performance impact. |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 2892 | return !runtime->GetHeap()->IsInBootImageOatFile(quick_code); |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2893 | } |
| 2894 | |
| 2895 | return false; |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2896 | } |
| 2897 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2898 | void ClassLinker::FixupStaticTrampolines(ObjPtr<mirror::Class> klass) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2899 | DCHECK(klass->IsInitialized()) << klass->PrettyDescriptor(); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2900 | if (klass->NumDirectMethods() == 0) { |
| 2901 | return; // No direct methods => no static methods. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2902 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2903 | Runtime* runtime = Runtime::Current(); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 2904 | if (!runtime->IsStarted()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2905 | if (runtime->IsAotCompiler() || runtime->GetHeap()->HasBootImageSpace()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 2906 | return; // OAT file unavailable. |
| 2907 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2908 | } |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 2909 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 2910 | const DexFile& dex_file = klass->GetDexFile(); |
| 2911 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2912 | CHECK(dex_class_def != nullptr); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2913 | const uint8_t* class_data = dex_file.GetClassData(*dex_class_def); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2914 | // There should always be class data if there were direct methods. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2915 | CHECK(class_data != nullptr) << klass->PrettyDescriptor(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2916 | ClassDataItemIterator it(dex_file, class_data); |
Mathieu Chartier | e17cf24 | 2017-06-19 11:05:51 -0700 | [diff] [blame] | 2917 | it.SkipAllFields(); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2918 | bool has_oat_class; |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 2919 | OatFile::OatClass oat_class = OatFile::FindOatClass(dex_file, |
| 2920 | klass->GetDexClassDefIndex(), |
| 2921 | &has_oat_class); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2922 | // Link the code of methods skipped by LinkCode. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2923 | for (size_t method_index = 0; it.HasNextDirectMethod(); ++method_index, it.Next()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2924 | ArtMethod* method = klass->GetDirectMethod(method_index, image_pointer_size_); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2925 | if (!method->IsStatic()) { |
| 2926 | // Only update static methods. |
| 2927 | continue; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2928 | } |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2929 | const void* quick_code = nullptr; |
| 2930 | if (has_oat_class) { |
| 2931 | OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2932 | quick_code = oat_method.GetQuickCode(); |
| 2933 | } |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2934 | // Check whether the method is native, in which case it's generic JNI. |
| 2935 | if (quick_code == nullptr && method->IsNative()) { |
| 2936 | quick_code = GetQuickGenericJniStub(); |
| 2937 | } else if (ShouldUseInterpreterEntrypoint(method, quick_code)) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2938 | // Use interpreter entry point. |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2939 | quick_code = GetQuickToInterpreterBridge(); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2940 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2941 | runtime->GetInstrumentation()->UpdateMethodsCode(method, quick_code); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2942 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2943 | // Ignore virtual methods on the iterator. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2944 | } |
| 2945 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 2946 | // Does anything needed to make sure that the compiler will not generate a direct invoke to this |
| 2947 | // method. Should only be called on non-invokable methods. |
| 2948 | inline void EnsureThrowsInvocationError(ClassLinker* class_linker, ArtMethod* method) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 2949 | DCHECK(method != nullptr); |
| 2950 | DCHECK(!method->IsInvokable()); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 2951 | method->SetEntryPointFromQuickCompiledCodePtrSize( |
| 2952 | class_linker->GetQuickToInterpreterBridgeTrampoline(), |
| 2953 | class_linker->GetImagePointerSize()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 2954 | } |
| 2955 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 2956 | static void LinkCode(ClassLinker* class_linker, |
| 2957 | ArtMethod* method, |
| 2958 | const OatFile::OatClass* oat_class, |
| 2959 | uint32_t class_def_method_index) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2960 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2961 | if (runtime->IsAotCompiler()) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2962 | // The following code only applies to a non-compiler runtime. |
| 2963 | return; |
| 2964 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2965 | // Method shouldn't have already been linked. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2966 | DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2967 | if (oat_class != nullptr) { |
| 2968 | // Every kind of method should at least get an invoke stub from the oat_method. |
| 2969 | // non-abstract methods also get their code pointers. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2970 | const OatFile::OatMethod oat_method = oat_class->GetOatMethod(class_def_method_index); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2971 | oat_method.LinkMethod(method); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2972 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2973 | |
Tamas Berghammer | 3a98aae | 2016-02-08 20:21:54 +0000 | [diff] [blame] | 2974 | // Install entry point from interpreter. |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2975 | const void* quick_code = method->GetEntryPointFromQuickCompiledCode(); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 2976 | bool enter_interpreter = class_linker->ShouldUseInterpreterEntrypoint(method, quick_code); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 2977 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 2978 | if (!method->IsInvokable()) { |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 2979 | EnsureThrowsInvocationError(class_linker, method); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2980 | return; |
| 2981 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2982 | |
| 2983 | if (method->IsStatic() && !method->IsConstructor()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2984 | // For static methods excluding the class initializer, install the trampoline. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2985 | // It will be replaced by the proper entry point by ClassLinker::FixupStaticTrampolines |
| 2986 | // after initializing class (see ClassLinker::InitializeClass method). |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2987 | method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionStub()); |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2988 | } else if (quick_code == nullptr && method->IsNative()) { |
| 2989 | method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub()); |
Tamas Berghammer | 3a98aae | 2016-02-08 20:21:54 +0000 | [diff] [blame] | 2990 | } else if (enter_interpreter) { |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2991 | // Set entry point from compiled code if there's no code or in interpreter only mode. |
| 2992 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
Ian Rogers | 0d6de04 | 2012-02-29 08:50:26 -0800 | [diff] [blame] | 2993 | } |
jeffhao | 26c0a1a | 2012-01-17 16:28:33 -0800 | [diff] [blame] | 2994 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2995 | if (method->IsNative()) { |
| 2996 | // Unregistering restores the dlsym lookup stub. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2997 | method->UnregisterNative(); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 2998 | |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2999 | if (enter_interpreter || quick_code == nullptr) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3000 | // We have a native method here without code. Then it should have either the generic JNI |
| 3001 | // trampoline as entrypoint (non-static), or the resolution trampoline (static). |
| 3002 | // TODO: this doesn't handle all the cases where trampolines may be installed. |
| 3003 | const void* entry_point = method->GetEntryPointFromQuickCompiledCode(); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3004 | DCHECK(class_linker->IsQuickGenericJniStub(entry_point) || |
| 3005 | class_linker->IsQuickResolutionStub(entry_point)); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 3006 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3007 | } |
| 3008 | } |
| 3009 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3010 | void ClassLinker::SetupClass(const DexFile& dex_file, |
| 3011 | const DexFile::ClassDef& dex_class_def, |
| 3012 | Handle<mirror::Class> klass, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3013 | ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3014 | CHECK(klass != nullptr); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3015 | CHECK(klass->GetDexCache() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3016 | CHECK_EQ(mirror::Class::kStatusNotReady, klass->GetStatus()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 3017 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3018 | CHECK(descriptor != nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3019 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3020 | klass->SetClass(GetClassRoot(kJavaLangClass)); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 3021 | uint32_t access_flags = dex_class_def.GetJavaAccessFlags(); |
Brian Carlstrom | 8e3fb14 | 2013-10-09 21:00:27 -0700 | [diff] [blame] | 3022 | CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3023 | klass->SetAccessFlags(access_flags); |
| 3024 | klass->SetClassLoader(class_loader); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3025 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3026 | mirror::Class::SetStatus(klass, mirror::Class::kStatusIdx, nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3027 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3028 | klass->SetDexClassDefIndex(dex_file.GetIndexForClassDef(dex_class_def)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3029 | klass->SetDexTypeIndex(dex_class_def.class_idx_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3030 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3031 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3032 | void ClassLinker::LoadClass(Thread* self, |
| 3033 | const DexFile& dex_file, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3034 | const DexFile::ClassDef& dex_class_def, |
| 3035 | Handle<mirror::Class> klass) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 3036 | const uint8_t* class_data = dex_file.GetClassData(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3037 | if (class_data == nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3038 | return; // no fields or methods - for example a marker interface |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3039 | } |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3040 | LoadClassMembers(self, dex_file, class_data, klass); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 3041 | } |
| 3042 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3043 | LengthPrefixedArray<ArtField>* ClassLinker::AllocArtFieldArray(Thread* self, |
| 3044 | LinearAlloc* allocator, |
| 3045 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3046 | if (length == 0) { |
| 3047 | return nullptr; |
| 3048 | } |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3049 | // If the ArtField alignment changes, review all uses of LengthPrefixedArray<ArtField>. |
| 3050 | static_assert(alignof(ArtField) == 4, "ArtField alignment is expected to be 4."); |
| 3051 | size_t storage_size = LengthPrefixedArray<ArtField>::ComputeSize(length); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3052 | void* array_storage = allocator->Alloc(self, storage_size); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3053 | auto* ret = new(array_storage) LengthPrefixedArray<ArtField>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3054 | CHECK(ret != nullptr); |
| 3055 | std::uninitialized_fill_n(&ret->At(0), length, ArtField()); |
| 3056 | return ret; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3057 | } |
| 3058 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3059 | LengthPrefixedArray<ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self, |
| 3060 | LinearAlloc* allocator, |
| 3061 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3062 | if (length == 0) { |
| 3063 | return nullptr; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3064 | } |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 3065 | const size_t method_alignment = ArtMethod::Alignment(image_pointer_size_); |
| 3066 | const size_t method_size = ArtMethod::Size(image_pointer_size_); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3067 | const size_t storage_size = |
| 3068 | LengthPrefixedArray<ArtMethod>::ComputeSize(length, method_size, method_alignment); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3069 | void* array_storage = allocator->Alloc(self, storage_size); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3070 | auto* ret = new (array_storage) LengthPrefixedArray<ArtMethod>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3071 | CHECK(ret != nullptr); |
| 3072 | for (size_t i = 0; i < length; ++i) { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3073 | new(reinterpret_cast<void*>(&ret->At(i, method_size, method_alignment))) ArtMethod; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3074 | } |
| 3075 | return ret; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3076 | } |
| 3077 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3078 | LinearAlloc* ClassLinker::GetAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3079 | if (class_loader == nullptr) { |
| 3080 | return Runtime::Current()->GetLinearAlloc(); |
| 3081 | } |
| 3082 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3083 | DCHECK(allocator != nullptr); |
| 3084 | return allocator; |
| 3085 | } |
| 3086 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3087 | LinearAlloc* ClassLinker::GetOrCreateAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3088 | if (class_loader == nullptr) { |
| 3089 | return Runtime::Current()->GetLinearAlloc(); |
| 3090 | } |
| 3091 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3092 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3093 | if (allocator == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 3094 | RegisterClassLoader(class_loader); |
| 3095 | allocator = class_loader->GetAllocator(); |
| 3096 | CHECK(allocator != nullptr); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3097 | } |
| 3098 | return allocator; |
| 3099 | } |
| 3100 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3101 | void ClassLinker::LoadClassMembers(Thread* self, |
| 3102 | const DexFile& dex_file, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 3103 | const uint8_t* class_data, |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3104 | Handle<mirror::Class> klass) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3105 | { |
| 3106 | // Note: We cannot have thread suspension until the field and method arrays are setup or else |
| 3107 | // Class::VisitFieldRoots may miss some fields or methods. |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3108 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3109 | // Load static fields. |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3110 | // We allow duplicate definitions of the same field in a class_data_item |
| 3111 | // but ignore the repeated indexes here, b/21868015. |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3112 | LinearAlloc* const allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3113 | ClassDataItemIterator it(dex_file, class_data); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3114 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, |
| 3115 | allocator, |
| 3116 | it.NumStaticFields()); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3117 | size_t num_sfields = 0; |
| 3118 | uint32_t last_field_idx = 0u; |
| 3119 | for (; it.HasNextStaticField(); it.Next()) { |
| 3120 | uint32_t field_idx = it.GetMemberIndex(); |
| 3121 | DCHECK_GE(field_idx, last_field_idx); // Ordering enforced by DexFileVerifier. |
| 3122 | if (num_sfields == 0 || LIKELY(field_idx > last_field_idx)) { |
| 3123 | DCHECK_LT(num_sfields, it.NumStaticFields()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3124 | LoadField(it, klass, &sfields->At(num_sfields)); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3125 | ++num_sfields; |
| 3126 | last_field_idx = field_idx; |
| 3127 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3128 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3129 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3130 | // Load instance fields. |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3131 | LengthPrefixedArray<ArtField>* ifields = AllocArtFieldArray(self, |
| 3132 | allocator, |
| 3133 | it.NumInstanceFields()); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3134 | size_t num_ifields = 0u; |
| 3135 | last_field_idx = 0u; |
| 3136 | for (; it.HasNextInstanceField(); it.Next()) { |
| 3137 | uint32_t field_idx = it.GetMemberIndex(); |
| 3138 | DCHECK_GE(field_idx, last_field_idx); // Ordering enforced by DexFileVerifier. |
| 3139 | if (num_ifields == 0 || LIKELY(field_idx > last_field_idx)) { |
| 3140 | DCHECK_LT(num_ifields, it.NumInstanceFields()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3141 | LoadField(it, klass, &ifields->At(num_ifields)); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3142 | ++num_ifields; |
| 3143 | last_field_idx = field_idx; |
| 3144 | } |
| 3145 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3146 | |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3147 | if (UNLIKELY(num_sfields != it.NumStaticFields()) || |
| 3148 | UNLIKELY(num_ifields != it.NumInstanceFields())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3149 | LOG(WARNING) << "Duplicate fields in class " << klass->PrettyDescriptor() |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3150 | << " (unique static fields: " << num_sfields << "/" << it.NumStaticFields() |
| 3151 | << ", unique instance fields: " << num_ifields << "/" << it.NumInstanceFields() << ")"; |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 3152 | // NOTE: Not shrinking the over-allocated sfields/ifields, just setting size. |
| 3153 | if (sfields != nullptr) { |
| 3154 | sfields->SetSize(num_sfields); |
| 3155 | } |
| 3156 | if (ifields != nullptr) { |
| 3157 | ifields->SetSize(num_ifields); |
| 3158 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3159 | } |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 3160 | // Set the field arrays. |
| 3161 | klass->SetSFieldsPtr(sfields); |
| 3162 | DCHECK_EQ(klass->NumStaticFields(), num_sfields); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3163 | klass->SetIFieldsPtr(ifields); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3164 | DCHECK_EQ(klass->NumInstanceFields(), num_ifields); |
| 3165 | // Load methods. |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3166 | bool has_oat_class = false; |
| 3167 | const OatFile::OatClass oat_class = |
| 3168 | (Runtime::Current()->IsStarted() && !Runtime::Current()->IsAotCompiler()) |
| 3169 | ? OatFile::FindOatClass(dex_file, klass->GetDexClassDefIndex(), &has_oat_class) |
| 3170 | : OatFile::OatClass::Invalid(); |
| 3171 | const OatFile::OatClass* oat_class_ptr = has_oat_class ? &oat_class : nullptr; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 3172 | klass->SetMethodsPtr( |
| 3173 | AllocArtMethodArray(self, allocator, it.NumDirectMethods() + it.NumVirtualMethods()), |
| 3174 | it.NumDirectMethods(), |
| 3175 | it.NumVirtualMethods()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3176 | size_t class_def_method_index = 0; |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 3177 | uint32_t last_dex_method_index = dex::kDexNoIndex; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3178 | size_t last_class_def_method_index = 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 3179 | // TODO These should really use the iterators. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3180 | for (size_t i = 0; it.HasNextDirectMethod(); i++, it.Next()) { |
| 3181 | ArtMethod* method = klass->GetDirectMethodUnchecked(i, image_pointer_size_); |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3182 | LoadMethod(dex_file, it, klass, method); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3183 | LinkCode(this, method, oat_class_ptr, class_def_method_index); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3184 | uint32_t it_method_index = it.GetMemberIndex(); |
| 3185 | if (last_dex_method_index == it_method_index) { |
| 3186 | // duplicate case |
| 3187 | method->SetMethodIndex(last_class_def_method_index); |
| 3188 | } else { |
| 3189 | method->SetMethodIndex(class_def_method_index); |
| 3190 | last_dex_method_index = it_method_index; |
| 3191 | last_class_def_method_index = class_def_method_index; |
| 3192 | } |
| 3193 | class_def_method_index++; |
| 3194 | } |
| 3195 | for (size_t i = 0; it.HasNextVirtualMethod(); i++, it.Next()) { |
| 3196 | ArtMethod* method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3197 | LoadMethod(dex_file, it, klass, method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3198 | DCHECK_EQ(class_def_method_index, it.NumDirectMethods() + i); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3199 | LinkCode(this, method, oat_class_ptr, class_def_method_index); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3200 | class_def_method_index++; |
| 3201 | } |
| 3202 | DCHECK(!it.HasNext()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3203 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 3204 | // Ensure that the card is marked so that remembered sets pick up native roots. |
| 3205 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(klass.Get()); |
Mathieu Chartier | f3f2a7a | 2015-04-14 15:43:10 -0700 | [diff] [blame] | 3206 | self->AllowThreadSuspension(); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3207 | } |
| 3208 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3209 | void ClassLinker::LoadField(const ClassDataItemIterator& it, |
| 3210 | Handle<mirror::Class> klass, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3211 | ArtField* dst) { |
| 3212 | const uint32_t field_idx = it.GetMemberIndex(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3213 | dst->SetDexFieldIndex(field_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3214 | dst->SetDeclaringClass(klass.Get()); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 3215 | dst->SetAccessFlags(it.GetFieldAccessFlags()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3216 | } |
| 3217 | |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3218 | void ClassLinker::LoadMethod(const DexFile& dex_file, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3219 | const ClassDataItemIterator& it, |
| 3220 | Handle<mirror::Class> klass, |
| 3221 | ArtMethod* dst) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3222 | uint32_t dex_method_idx = it.GetMemberIndex(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3223 | const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3224 | const char* method_name = dex_file.StringDataByIdx(method_id.name_idx_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3225 | |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3226 | ScopedAssertNoThreadSuspension ants("LoadMethod"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3227 | dst->SetDexMethodIndex(dex_method_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3228 | dst->SetDeclaringClass(klass.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3229 | dst->SetCodeItemOffset(it.GetMethodCodeItemOffset()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3230 | |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 3231 | uint32_t access_flags = it.GetMethodAccessFlags(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3232 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3233 | if (UNLIKELY(strcmp("finalize", method_name) == 0)) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3234 | // Set finalizable flag on declaring class. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3235 | if (strcmp("V", dex_file.GetShorty(method_id.proto_idx_)) == 0) { |
| 3236 | // Void return type. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3237 | if (klass->GetClassLoader() != nullptr) { // All non-boot finalizer methods are flagged. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3238 | klass->SetFinalizable(); |
| 3239 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3240 | std::string temp; |
| 3241 | const char* klass_descriptor = klass->GetDescriptor(&temp); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3242 | // The Enum class declares a "final" finalize() method to prevent subclasses from |
| 3243 | // introducing a finalizer. We don't want to set the finalizable flag for Enum or its |
| 3244 | // subclasses, so we exclude it here. |
| 3245 | // We also want to avoid setting the flag on Object, where we know that finalize() is |
| 3246 | // empty. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3247 | if (strcmp(klass_descriptor, "Ljava/lang/Object;") != 0 && |
| 3248 | strcmp(klass_descriptor, "Ljava/lang/Enum;") != 0) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3249 | klass->SetFinalizable(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3250 | } |
| 3251 | } |
| 3252 | } |
| 3253 | } else if (method_name[0] == '<') { |
| 3254 | // Fix broken access flags for initializers. Bug 11157540. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3255 | bool is_init = (strcmp("<init>", method_name) == 0); |
| 3256 | bool is_clinit = !is_init && (strcmp("<clinit>", method_name) == 0); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3257 | if (UNLIKELY(!is_init && !is_clinit)) { |
| 3258 | LOG(WARNING) << "Unexpected '<' at start of method name " << method_name; |
| 3259 | } else { |
| 3260 | if (UNLIKELY((access_flags & kAccConstructor) == 0)) { |
| 3261 | 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] | 3262 | << klass->PrettyDescriptor() << " in dex file " << dex_file.GetLocation(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3263 | access_flags |= kAccConstructor; |
| 3264 | } |
| 3265 | } |
| 3266 | } |
| 3267 | dst->SetAccessFlags(access_flags); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3268 | } |
| 3269 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3270 | void ClassLinker::AppendToBootClassPath(Thread* self, const DexFile& dex_file) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3271 | ObjPtr<mirror::DexCache> dex_cache = AllocAndInitializeDexCache( |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3272 | self, |
| 3273 | dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3274 | Runtime::Current()->GetLinearAlloc()); |
| 3275 | 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] | 3276 | AppendToBootClassPath(dex_file, dex_cache); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 3277 | } |
| 3278 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3279 | void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3280 | ObjPtr<mirror::DexCache> dex_cache) { |
| 3281 | CHECK(dex_cache != nullptr) << dex_file.GetLocation(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 3282 | boot_class_path_.push_back(&dex_file); |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 3283 | WriterMutexLock mu(Thread::Current(), *Locks::dex_lock_); |
| 3284 | RegisterDexFileLocked(dex_file, dex_cache, /* class_loader */ nullptr); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3285 | } |
| 3286 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3287 | void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3288 | ObjPtr<mirror::DexCache> dex_cache, |
| 3289 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3290 | Thread* const self = Thread::Current(); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3291 | Locks::dex_lock_->AssertExclusiveHeld(self); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3292 | CHECK(dex_cache != nullptr) << dex_file.GetLocation(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3293 | // For app images, the dex cache location may be a suffix of the dex file location since the |
| 3294 | // dex file location is an absolute path. |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 3295 | const std::string dex_cache_location = dex_cache->GetLocation()->ToModifiedUtf8(); |
| 3296 | const size_t dex_cache_length = dex_cache_location.length(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3297 | CHECK_GT(dex_cache_length, 0u) << dex_file.GetLocation(); |
| 3298 | std::string dex_file_location = dex_file.GetLocation(); |
| 3299 | CHECK_GE(dex_file_location.length(), dex_cache_length) |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 3300 | << dex_cache_location << " " << dex_file.GetLocation(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3301 | // Take suffix. |
| 3302 | const std::string dex_file_suffix = dex_file_location.substr( |
| 3303 | dex_file_location.length() - dex_cache_length, |
| 3304 | dex_cache_length); |
| 3305 | // Example dex_cache location is SettingsProvider.apk and |
| 3306 | // dex file location is /system/priv-app/SettingsProvider/SettingsProvider.apk |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 3307 | CHECK_EQ(dex_cache_location, dex_file_suffix); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3308 | const OatFile* oat_file = |
| 3309 | (dex_file.GetOatDexFile() != nullptr) ? dex_file.GetOatDexFile()->GetOatFile() : nullptr; |
| 3310 | // 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] | 3311 | // 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] | 3312 | bool initialize_oat_file_bss = (oat_file != nullptr); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3313 | JavaVMExt* const vm = self->GetJniEnv()->vm; |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3314 | for (auto it = dex_caches_.begin(); it != dex_caches_.end(); ) { |
| 3315 | DexCacheData data = *it; |
| 3316 | if (self->IsJWeakCleared(data.weak_root)) { |
| 3317 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3318 | it = dex_caches_.erase(it); |
| 3319 | } else { |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3320 | if (initialize_oat_file_bss && |
| 3321 | it->dex_file->GetOatDexFile() != nullptr && |
| 3322 | it->dex_file->GetOatDexFile()->GetOatFile() == oat_file) { |
| 3323 | initialize_oat_file_bss = false; // Already initialized. |
| 3324 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3325 | ++it; |
| 3326 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3327 | } |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3328 | if (initialize_oat_file_bss) { |
| 3329 | // TODO: Pre-initialize from boot/app image? |
| 3330 | ArtMethod* resolution_method = Runtime::Current()->GetResolutionMethod(); |
| 3331 | for (ArtMethod*& entry : oat_file->GetBssMethods()) { |
| 3332 | entry = resolution_method; |
| 3333 | } |
| 3334 | } |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3335 | jweak dex_cache_jweak = vm->AddWeakGlobalRef(self, dex_cache); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3336 | dex_cache->SetDexFile(&dex_file); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3337 | DexCacheData data; |
| 3338 | data.weak_root = dex_cache_jweak; |
| 3339 | data.dex_file = dex_cache->GetDexFile(); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3340 | data.class_table = ClassTableForClassLoader(class_loader); |
| 3341 | DCHECK(data.class_table != nullptr); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 3342 | // Make sure to hold the dex cache live in the class table. This case happens for the boot class |
| 3343 | // path dex caches without an image. |
| 3344 | data.class_table->InsertStrongRoot(dex_cache); |
| 3345 | if (class_loader != nullptr) { |
| 3346 | // Since we added a strong root to the class table, do the write barrier as required for |
| 3347 | // remembered sets and generational GCs. |
| 3348 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader); |
| 3349 | } |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3350 | dex_caches_.push_back(data); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3351 | } |
| 3352 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3353 | ObjPtr<mirror::DexCache> ClassLinker::DecodeDexCache(Thread* self, const DexCacheData& data) { |
| 3354 | return data.IsValid() |
| 3355 | ? ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root)) |
| 3356 | : nullptr; |
| 3357 | } |
| 3358 | |
| 3359 | ObjPtr<mirror::DexCache> ClassLinker::EnsureSameClassLoader( |
| 3360 | Thread* self, |
| 3361 | ObjPtr<mirror::DexCache> dex_cache, |
| 3362 | const DexCacheData& data, |
| 3363 | ObjPtr<mirror::ClassLoader> class_loader) { |
| 3364 | DCHECK_EQ(dex_cache->GetDexFile(), data.dex_file); |
| 3365 | if (data.class_table != ClassTableForClassLoader(class_loader)) { |
| 3366 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
| 3367 | "Attempt to register dex file %s with multiple class loaders", |
| 3368 | data.dex_file->GetLocation().c_str()); |
| 3369 | return nullptr; |
| 3370 | } |
| 3371 | return dex_cache; |
| 3372 | } |
| 3373 | |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3374 | void ClassLinker::RegisterExistingDexCache(ObjPtr<mirror::DexCache> dex_cache, |
| 3375 | ObjPtr<mirror::ClassLoader> class_loader) { |
| 3376 | Thread* self = Thread::Current(); |
| 3377 | StackHandleScope<2> hs(self); |
| 3378 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(dex_cache)); |
| 3379 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 3380 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 3381 | DCHECK(dex_file != nullptr) << "Attempt to register uninitialized dex_cache object!"; |
| 3382 | if (kIsDebugBuild) { |
| 3383 | DexCacheData old_data; |
| 3384 | { |
| 3385 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
| 3386 | old_data = FindDexCacheDataLocked(*dex_file); |
| 3387 | } |
| 3388 | ObjPtr<mirror::DexCache> old_dex_cache = DecodeDexCache(self, old_data); |
| 3389 | DCHECK(old_dex_cache.IsNull()) << "Attempt to manually register a dex cache thats already " |
| 3390 | << "been registered on dex file " << dex_file->GetLocation(); |
| 3391 | } |
| 3392 | ClassTable* table; |
| 3393 | { |
| 3394 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3395 | table = InsertClassTableForClassLoader(h_class_loader.Get()); |
| 3396 | } |
| 3397 | WriterMutexLock mu(self, *Locks::dex_lock_); |
| 3398 | RegisterDexFileLocked(*dex_file, h_dex_cache.Get(), h_class_loader.Get()); |
| 3399 | table->InsertStrongRoot(h_dex_cache.Get()); |
| 3400 | if (h_class_loader.Get() != nullptr) { |
| 3401 | // Since we added a strong root to the class table, do the write barrier as required for |
| 3402 | // remembered sets and generational GCs. |
| 3403 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(h_class_loader.Get()); |
| 3404 | } |
| 3405 | } |
| 3406 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3407 | ObjPtr<mirror::DexCache> ClassLinker::RegisterDexFile(const DexFile& dex_file, |
| 3408 | ObjPtr<mirror::ClassLoader> class_loader) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3409 | Thread* self = Thread::Current(); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3410 | DexCacheData old_data; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3411 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3412 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3413 | old_data = FindDexCacheDataLocked(dex_file); |
| 3414 | } |
| 3415 | ObjPtr<mirror::DexCache> old_dex_cache = DecodeDexCache(self, old_data); |
| 3416 | if (old_dex_cache != nullptr) { |
| 3417 | return EnsureSameClassLoader(self, old_dex_cache, old_data, class_loader); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3418 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3419 | LinearAlloc* const linear_alloc = GetOrCreateAllocatorForClassLoader(class_loader); |
| 3420 | DCHECK(linear_alloc != nullptr); |
| 3421 | ClassTable* table; |
| 3422 | { |
| 3423 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3424 | table = InsertClassTableForClassLoader(class_loader); |
| 3425 | } |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3426 | // Don't alloc while holding the lock, since allocation may need to |
| 3427 | // suspend all threads and another thread may need the dex_lock_ to |
| 3428 | // get to a suspend point. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3429 | StackHandleScope<3> hs(self); |
| 3430 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3431 | ObjPtr<mirror::String> location; |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 3432 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(AllocDexCache(/*out*/&location, |
| 3433 | self, |
| 3434 | dex_file))); |
| 3435 | Handle<mirror::String> h_location(hs.NewHandle(location)); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3436 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3437 | WriterMutexLock mu(self, *Locks::dex_lock_); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3438 | old_data = FindDexCacheDataLocked(dex_file); |
| 3439 | old_dex_cache = DecodeDexCache(self, old_data); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3440 | if (old_dex_cache == nullptr && h_dex_cache != nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3441 | // Do InitializeDexCache while holding dex lock to make sure two threads don't call it at the |
| 3442 | // same time with the same dex cache. Since the .bss is shared this can cause failing DCHECK |
| 3443 | // that the arrays are null. |
| 3444 | mirror::DexCache::InitializeDexCache(self, |
| 3445 | h_dex_cache.Get(), |
| 3446 | h_location.Get(), |
| 3447 | &dex_file, |
| 3448 | linear_alloc, |
| 3449 | image_pointer_size_); |
| 3450 | RegisterDexFileLocked(dex_file, h_dex_cache.Get(), h_class_loader.Get()); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3451 | } |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3452 | } |
| 3453 | if (old_dex_cache != nullptr) { |
| 3454 | // Another thread managed to initialize the dex cache faster, so use that DexCache. |
| 3455 | // If this thread encountered OOME, ignore it. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3456 | DCHECK_EQ(h_dex_cache == nullptr, self->IsExceptionPending()); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3457 | self->ClearException(); |
| 3458 | // We cannot call EnsureSameClassLoader() while holding the dex_lock_. |
| 3459 | return EnsureSameClassLoader(self, old_dex_cache, old_data, h_class_loader.Get()); |
| 3460 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3461 | if (h_dex_cache == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3462 | self->AssertPendingOOMException(); |
| 3463 | return nullptr; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3464 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3465 | table->InsertStrongRoot(h_dex_cache.Get()); |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 3466 | if (h_class_loader.Get() != nullptr) { |
| 3467 | // Since we added a strong root to the class table, do the write barrier as required for |
| 3468 | // remembered sets and generational GCs. |
| 3469 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(h_class_loader.Get()); |
| 3470 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3471 | return h_dex_cache.Get(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3472 | } |
| 3473 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3474 | bool ClassLinker::IsDexFileRegistered(Thread* self, const DexFile& dex_file) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3475 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3476 | return DecodeDexCache(self, FindDexCacheDataLocked(dex_file)) != nullptr; |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3477 | } |
| 3478 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3479 | ObjPtr<mirror::DexCache> ClassLinker::FindDexCache(Thread* self, const DexFile& dex_file) { |
| 3480 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 3481 | DexCacheData dex_cache_data = FindDexCacheDataLocked(dex_file); |
| 3482 | ObjPtr<mirror::DexCache> dex_cache = DecodeDexCache(self, dex_cache_data); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3483 | if (dex_cache != nullptr) { |
| 3484 | return dex_cache; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3485 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3486 | // Failure, dump diagnostic and abort. |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3487 | for (const DexCacheData& data : dex_caches_) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3488 | if (DecodeDexCache(self, data) != nullptr) { |
Andreas Gampe | 37c5846 | 2017-03-27 15:14:27 -0700 | [diff] [blame] | 3489 | LOG(FATAL_WITHOUT_ABORT) << "Registered dex file " << data.dex_file->GetLocation(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3490 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3491 | } |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 3492 | LOG(FATAL) << "Failed to find DexCache for DexFile " << dex_file.GetLocation() |
| 3493 | << " " << &dex_file << " " << dex_cache_data.dex_file; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 3494 | UNREACHABLE(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3495 | } |
| 3496 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3497 | ClassTable* ClassLinker::FindClassTable(Thread* self, ObjPtr<mirror::DexCache> dex_cache) { |
| 3498 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 3499 | DCHECK(dex_file != nullptr); |
| 3500 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
| 3501 | // Search assuming unique-ness of dex file. |
| 3502 | for (const DexCacheData& data : dex_caches_) { |
| 3503 | // Avoid decoding (and read barriers) other unrelated dex caches. |
| 3504 | if (data.dex_file == dex_file) { |
| 3505 | ObjPtr<mirror::DexCache> registered_dex_cache = DecodeDexCache(self, data); |
| 3506 | if (registered_dex_cache != nullptr) { |
| 3507 | CHECK_EQ(registered_dex_cache, dex_cache) << dex_file->GetLocation(); |
| 3508 | return data.class_table; |
| 3509 | } |
| 3510 | } |
| 3511 | } |
| 3512 | return nullptr; |
| 3513 | } |
| 3514 | |
| 3515 | ClassLinker::DexCacheData ClassLinker::FindDexCacheDataLocked(const DexFile& dex_file) { |
| 3516 | // Search assuming unique-ness of dex file. |
| 3517 | for (const DexCacheData& data : dex_caches_) { |
| 3518 | // Avoid decoding (and read barriers) other unrelated dex caches. |
| 3519 | if (data.dex_file == &dex_file) { |
| 3520 | return data; |
| 3521 | } |
| 3522 | } |
| 3523 | return DexCacheData(); |
| 3524 | } |
| 3525 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3526 | mirror::Class* ClassLinker::CreatePrimitiveClass(Thread* self, Primitive::Type type) { |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 3527 | ObjPtr<mirror::Class> klass = |
| 3528 | AllocClass(self, mirror::Class::PrimitiveClassSize(image_pointer_size_)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3529 | if (UNLIKELY(klass == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3530 | self->AssertPendingOOMException(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3531 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3532 | } |
| 3533 | return InitializePrimitiveClass(klass, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3534 | } |
| 3535 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3536 | mirror::Class* ClassLinker::InitializePrimitiveClass(ObjPtr<mirror::Class> primitive_class, |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3537 | Primitive::Type type) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3538 | CHECK(primitive_class != nullptr); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3539 | // Must hold lock on object when initializing. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3540 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3541 | StackHandleScope<1> hs(self); |
| 3542 | Handle<mirror::Class> h_class(hs.NewHandle(primitive_class)); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3543 | ObjectLock<mirror::Class> lock(self, h_class); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3544 | h_class->SetAccessFlags(kAccPublic | kAccFinal | kAccAbstract); |
| 3545 | h_class->SetPrimitiveType(type); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 3546 | h_class->SetIfTable(GetClassRoot(kJavaLangObject)->GetIfTable()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3547 | mirror::Class::SetStatus(h_class, mirror::Class::kStatusInitialized, self); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3548 | const char* descriptor = Primitive::Descriptor(type); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 3549 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, |
| 3550 | h_class.Get(), |
| 3551 | ComputeModifiedUtf8Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3552 | CHECK(existing == nullptr) << "InitPrimitiveClass(" << type << ") failed"; |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3553 | return h_class.Get(); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 3554 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3555 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3556 | // Create an array class (i.e. the class object for the array, not the |
| 3557 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 3558 | // "[Ljava/lang/String;". |
| 3559 | // |
| 3560 | // If "descriptor" refers to an array of primitives, look up the |
| 3561 | // primitive type's internally-generated class object. |
| 3562 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3563 | // "class_loader" is the class loader of the class that's referring to |
| 3564 | // us. It's used to ensure that we're looking for the element type in |
| 3565 | // the right context. It does NOT become the class loader for the |
| 3566 | // array class; that always comes from the base element class. |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3567 | // |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 3568 | // Returns null with an exception raised on failure. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 3569 | mirror::Class* ClassLinker::CreateArrayClass(Thread* self, const char* descriptor, size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3570 | Handle<mirror::ClassLoader> class_loader) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3571 | // Identify the underlying component type |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3572 | CHECK_EQ('[', descriptor[0]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3573 | StackHandleScope<2> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3574 | MutableHandle<mirror::Class> component_type(hs.NewHandle(FindClass(self, descriptor + 1, |
| 3575 | class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3576 | if (component_type == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3577 | DCHECK(self->IsExceptionPending()); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 3578 | // We need to accept erroneous classes as component types. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3579 | const size_t component_hash = ComputeModifiedUtf8Hash(descriptor + 1); |
| 3580 | component_type.Assign(LookupClass(self, descriptor + 1, component_hash, class_loader.Get())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3581 | if (component_type == nullptr) { |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 3582 | DCHECK(self->IsExceptionPending()); |
| 3583 | return nullptr; |
| 3584 | } else { |
| 3585 | self->ClearException(); |
| 3586 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3587 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 3588 | if (UNLIKELY(component_type->IsPrimitiveVoid())) { |
| 3589 | ThrowNoClassDefFoundError("Attempt to create array of void primitive type"); |
| 3590 | return nullptr; |
| 3591 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3592 | // See if the component type is already loaded. Array classes are |
| 3593 | // always associated with the class loader of their underlying |
| 3594 | // element type -- an array of Strings goes with the loader for |
| 3595 | // java/lang/String -- so we need to look for it there. (The |
| 3596 | // caller should have checked for the existence of the class |
| 3597 | // before calling here, but they did so with *their* class loader, |
| 3598 | // not the component type's loader.) |
| 3599 | // |
| 3600 | // If we find it, the caller adds "loader" to the class' initiating |
| 3601 | // loader list, which should prevent us from going through this again. |
| 3602 | // |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3603 | // This call is unnecessary if "loader" and "component_type->GetClassLoader()" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3604 | // are the same, because our caller (FindClass) just did the |
| 3605 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 3606 | // because we effectively do this lookup again when we add the new |
| 3607 | // class to the hash table --- necessary because of possible races with |
| 3608 | // other threads.) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3609 | if (class_loader.Get() != component_type->GetClassLoader()) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3610 | ObjPtr<mirror::Class> new_class = |
| 3611 | LookupClass(self, descriptor, hash, component_type->GetClassLoader()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3612 | if (new_class != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3613 | return new_class.Ptr(); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3614 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3615 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3616 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3617 | // Fill out the fields in the Class. |
| 3618 | // |
| 3619 | // It is possible to execute some methods against arrays, because |
| 3620 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 3621 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 3622 | // |
| 3623 | // Array classes are simple enough that we don't need to do a full |
| 3624 | // link step. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3625 | auto new_class = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3626 | if (UNLIKELY(!init_done_)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3627 | // Classes that were hand created, ie not by FindSystemClass |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3628 | if (strcmp(descriptor, "[Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3629 | new_class.Assign(GetClassRoot(kClassArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3630 | } else if (strcmp(descriptor, "[Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3631 | new_class.Assign(GetClassRoot(kObjectArrayClass)); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3632 | } else if (strcmp(descriptor, GetClassRootDescriptor(kJavaLangStringArrayClass)) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3633 | new_class.Assign(GetClassRoot(kJavaLangStringArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3634 | } else if (strcmp(descriptor, "[C") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3635 | new_class.Assign(GetClassRoot(kCharArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3636 | } else if (strcmp(descriptor, "[I") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3637 | new_class.Assign(GetClassRoot(kIntArrayClass)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3638 | } else if (strcmp(descriptor, "[J") == 0) { |
| 3639 | new_class.Assign(GetClassRoot(kLongArrayClass)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3640 | } |
| 3641 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3642 | if (new_class == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3643 | new_class.Assign(AllocClass(self, mirror::Array::ClassSize(image_pointer_size_))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3644 | if (new_class == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3645 | self->AssertPendingOOMException(); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3646 | return nullptr; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3647 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3648 | new_class->SetComponentType(component_type.Get()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3649 | } |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3650 | 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] | 3651 | DCHECK(new_class->GetComponentType() != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3652 | ObjPtr<mirror::Class> java_lang_Object = GetClassRoot(kJavaLangObject); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3653 | new_class->SetSuperClass(java_lang_Object); |
| 3654 | new_class->SetVTable(java_lang_Object->GetVTable()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3655 | new_class->SetPrimitiveType(Primitive::kPrimNot); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3656 | new_class->SetClassLoader(component_type->GetClassLoader()); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 3657 | if (component_type->IsPrimitive()) { |
| 3658 | new_class->SetClassFlags(mirror::kClassFlagNoReferenceFields); |
| 3659 | } else { |
| 3660 | new_class->SetClassFlags(mirror::kClassFlagObjectArray); |
| 3661 | } |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3662 | mirror::Class::SetStatus(new_class, mirror::Class::kStatusLoaded, self); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 3663 | new_class->PopulateEmbeddedVTable(image_pointer_size_); |
| 3664 | ImTable* object_imt = java_lang_Object->GetImt(image_pointer_size_); |
| 3665 | new_class->SetImt(object_imt, image_pointer_size_); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3666 | mirror::Class::SetStatus(new_class, mirror::Class::kStatusInitialized, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3667 | // don't need to set new_class->SetObjectSize(..) |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 3668 | // because Object::SizeOf delegates to Array::SizeOf |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3669 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3670 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 3671 | // interfaces. We need to set that up here, so that stuff like |
| 3672 | // "instanceof" works right. |
| 3673 | // |
| 3674 | // Note: The GC could run during the call to FindSystemClass, |
| 3675 | // so we need to make sure the class object is GC-valid while we're in |
| 3676 | // there. Do this by clearing the interface list so the GC will just |
| 3677 | // think that the entries are null. |
| 3678 | |
| 3679 | |
| 3680 | // Use the single, global copies of "interfaces" and "iftable" |
| 3681 | // (remember not to free them for arrays). |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3682 | { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3683 | ObjPtr<mirror::IfTable> array_iftable = array_iftable_.Read(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3684 | CHECK(array_iftable != nullptr); |
| 3685 | new_class->SetIfTable(array_iftable); |
| 3686 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3687 | |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 3688 | // Inherit access flags from the component type. |
| 3689 | int access_flags = new_class->GetComponentType()->GetAccessFlags(); |
| 3690 | // Lose any implementation detail flags; in particular, arrays aren't finalizable. |
| 3691 | access_flags &= kAccJavaFlagsMask; |
| 3692 | // 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] | 3693 | // and remove "interface". |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 3694 | access_flags |= kAccAbstract | kAccFinal; |
| 3695 | access_flags &= ~kAccInterface; |
| 3696 | |
| 3697 | new_class->SetAccessFlags(access_flags); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3698 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3699 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, new_class.Get(), hash); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3700 | if (existing == nullptr) { |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 3701 | // We postpone ClassLoad and ClassPrepare events to this point in time to avoid |
| 3702 | // duplicate events in case of races. Array classes don't really follow dedicated |
| 3703 | // load and prepare, anyways. |
| 3704 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(new_class); |
| 3705 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(new_class, new_class); |
| 3706 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 3707 | jit::Jit::NewTypeLoadedIfUsingJit(new_class.Get()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3708 | return new_class.Get(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3709 | } |
| 3710 | // Another thread must have loaded the class after we |
| 3711 | // started but before we finished. Abandon what we've |
| 3712 | // done. |
| 3713 | // |
| 3714 | // (Yes, this happens.) |
| 3715 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3716 | return existing.Ptr(); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3717 | } |
| 3718 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3719 | mirror::Class* ClassLinker::FindPrimitiveClass(char type) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3720 | switch (type) { |
| 3721 | case 'B': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3722 | return GetClassRoot(kPrimitiveByte); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3723 | case 'C': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3724 | return GetClassRoot(kPrimitiveChar); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3725 | case 'D': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3726 | return GetClassRoot(kPrimitiveDouble); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3727 | case 'F': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3728 | return GetClassRoot(kPrimitiveFloat); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3729 | case 'I': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3730 | return GetClassRoot(kPrimitiveInt); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3731 | case 'J': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3732 | return GetClassRoot(kPrimitiveLong); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3733 | case 'S': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3734 | return GetClassRoot(kPrimitiveShort); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3735 | case 'Z': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3736 | return GetClassRoot(kPrimitiveBoolean); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3737 | case 'V': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3738 | return GetClassRoot(kPrimitiveVoid); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3739 | default: |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3740 | break; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 3741 | } |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 3742 | std::string printable_type(PrintableChar(type)); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 3743 | ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3744 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3745 | } |
| 3746 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3747 | 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] | 3748 | if (VLOG_IS_ON(class_linker)) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3749 | ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3750 | std::string source; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3751 | if (dex_cache != nullptr) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3752 | source += " from "; |
| 3753 | source += dex_cache->GetLocation()->ToModifiedUtf8(); |
| 3754 | } |
| 3755 | LOG(INFO) << "Loaded class " << descriptor << source; |
| 3756 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3757 | { |
| 3758 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3759 | ObjPtr<mirror::ClassLoader> const class_loader = klass->GetClassLoader(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3760 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3761 | ObjPtr<mirror::Class> existing = class_table->Lookup(descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3762 | if (existing != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3763 | return existing.Ptr(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3764 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3765 | VerifyObject(klass); |
| 3766 | class_table->InsertWithHash(klass, hash); |
| 3767 | if (class_loader != nullptr) { |
| 3768 | // This is necessary because we need to have the card dirtied for remembered sets. |
| 3769 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader); |
| 3770 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 3771 | if (log_new_roots_) { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3772 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3773 | } |
| 3774 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3775 | if (kIsDebugBuild) { |
| 3776 | // Test that copied methods correctly can find their holder. |
| 3777 | for (ArtMethod& method : klass->GetCopiedMethods(image_pointer_size_)) { |
| 3778 | CHECK_EQ(GetHoldingClassOfCopiedMethod(&method), klass); |
| 3779 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3780 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3781 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3782 | } |
| 3783 | |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 3784 | void ClassLinker::WriteBarrierForBootOatFileBssRoots(const OatFile* oat_file) { |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 3785 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3786 | DCHECK(!oat_file->GetBssGcRoots().empty()) << oat_file->GetLocation(); |
| 3787 | if (log_new_roots_ && !ContainsElement(new_bss_roots_boot_oat_files_, oat_file)) { |
| 3788 | new_bss_roots_boot_oat_files_.push_back(oat_file); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 3789 | } |
| 3790 | } |
| 3791 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 3792 | // TODO This should really be in mirror::Class. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3793 | void ClassLinker::UpdateClassMethods(ObjPtr<mirror::Class> klass, |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 3794 | LengthPrefixedArray<ArtMethod>* new_methods) { |
| 3795 | klass->SetMethodsPtrUnchecked(new_methods, |
| 3796 | klass->NumDirectMethods(), |
| 3797 | klass->NumDeclaredVirtualMethods()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3798 | // 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] | 3799 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(klass); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3800 | } |
| 3801 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3802 | mirror::Class* ClassLinker::LookupClass(Thread* self, |
Andreas Gampe | 2ff3b97 | 2017-06-05 18:14:53 -0700 | [diff] [blame] | 3803 | const char* descriptor, |
| 3804 | ObjPtr<mirror::ClassLoader> class_loader) { |
| 3805 | return LookupClass(self, descriptor, ComputeModifiedUtf8Hash(descriptor), class_loader); |
| 3806 | } |
| 3807 | |
| 3808 | mirror::Class* ClassLinker::LookupClass(Thread* self, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3809 | const char* descriptor, |
| 3810 | size_t hash, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3811 | ObjPtr<mirror::ClassLoader> class_loader) { |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 3812 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3813 | ClassTable* const class_table = ClassTableForClassLoader(class_loader); |
| 3814 | if (class_table != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3815 | ObjPtr<mirror::Class> result = class_table->Lookup(descriptor, hash); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 3816 | if (result != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3817 | return result.Ptr(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3818 | } |
Sameer Abu Asal | 2c6de22 | 2013-05-02 17:38:59 -0700 | [diff] [blame] | 3819 | } |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 3820 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3821 | } |
| 3822 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3823 | class MoveClassTableToPreZygoteVisitor : public ClassLoaderVisitor { |
| 3824 | public: |
| 3825 | explicit MoveClassTableToPreZygoteVisitor() {} |
| 3826 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3827 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3828 | REQUIRES(Locks::classlinker_classes_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3829 | REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3830 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3831 | if (class_table != nullptr) { |
| 3832 | class_table->FreezeSnapshot(); |
| 3833 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 3834 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3835 | }; |
| 3836 | |
| 3837 | void ClassLinker::MoveClassTableToPreZygote() { |
| 3838 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 3839 | boot_class_table_->FreezeSnapshot(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3840 | MoveClassTableToPreZygoteVisitor visitor; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3841 | VisitClassLoaders(&visitor); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3842 | } |
| 3843 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3844 | // Look up classes by hash and descriptor and put all matching ones in the result array. |
| 3845 | class LookupClassesVisitor : public ClassLoaderVisitor { |
| 3846 | public: |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3847 | LookupClassesVisitor(const char* descriptor, |
| 3848 | size_t hash, |
| 3849 | std::vector<ObjPtr<mirror::Class>>* result) |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3850 | : descriptor_(descriptor), |
| 3851 | hash_(hash), |
| 3852 | result_(result) {} |
| 3853 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3854 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3855 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) OVERRIDE { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3856 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3857 | ObjPtr<mirror::Class> klass = class_table->Lookup(descriptor_, hash_); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 3858 | // Add `klass` only if `class_loader` is its defining (not just initiating) class loader. |
| 3859 | if (klass != nullptr && klass->GetClassLoader() == class_loader) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3860 | result_->push_back(klass); |
| 3861 | } |
| 3862 | } |
| 3863 | |
| 3864 | private: |
| 3865 | const char* const descriptor_; |
| 3866 | const size_t hash_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3867 | std::vector<ObjPtr<mirror::Class>>* const result_; |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3868 | }; |
| 3869 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3870 | void ClassLinker::LookupClasses(const char* descriptor, |
| 3871 | std::vector<ObjPtr<mirror::Class>>& result) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3872 | result.clear(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3873 | Thread* const self = Thread::Current(); |
| 3874 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3875 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 3876 | ObjPtr<mirror::Class> klass = boot_class_table_->Lookup(descriptor, hash); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3877 | if (klass != nullptr) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 3878 | DCHECK(klass->GetClassLoader() == nullptr); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3879 | result.push_back(klass); |
| 3880 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3881 | LookupClassesVisitor visitor(descriptor, hash, &result); |
| 3882 | VisitClassLoaders(&visitor); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 3883 | } |
| 3884 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3885 | bool ClassLinker::AttemptSupertypeVerification(Thread* self, |
| 3886 | Handle<mirror::Class> klass, |
| 3887 | Handle<mirror::Class> supertype) { |
| 3888 | DCHECK(self != nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3889 | DCHECK(klass != nullptr); |
| 3890 | DCHECK(supertype != nullptr); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3891 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3892 | if (!supertype->IsVerified() && !supertype->IsErroneous()) { |
| 3893 | VerifyClass(self, supertype); |
| 3894 | } |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 3895 | |
| 3896 | if (supertype->IsVerified() || supertype->ShouldVerifyAtRuntime()) { |
| 3897 | // The supertype is either verified, or we soft failed at AOT time. |
| 3898 | DCHECK(supertype->IsVerified() || Runtime::Current()->IsAotCompiler()); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3899 | return true; |
| 3900 | } |
| 3901 | // If we got this far then we have a hard failure. |
| 3902 | std::string error_msg = |
| 3903 | StringPrintf("Rejecting class %s that attempts to sub-type erroneous class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3904 | klass->PrettyDescriptor().c_str(), |
| 3905 | supertype->PrettyDescriptor().c_str()); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3906 | LOG(WARNING) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3907 | StackHandleScope<1> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3908 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3909 | if (cause != nullptr) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3910 | // Set during VerifyClass call (if at all). |
| 3911 | self->ClearException(); |
| 3912 | } |
| 3913 | // Change into a verify error. |
| 3914 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3915 | if (cause != nullptr) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3916 | self->GetException()->SetCause(cause.Get()); |
| 3917 | } |
| 3918 | ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex()); |
| 3919 | if (Runtime::Current()->IsAotCompiler()) { |
| 3920 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
| 3921 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3922 | // Need to grab the lock to change status. |
| 3923 | ObjectLock<mirror::Class> super_lock(self, klass); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 3924 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3925 | return false; |
| 3926 | } |
| 3927 | |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3928 | // Ensures that methods have the kAccSkipAccessChecks bit set. We use the |
| 3929 | // kAccVerificationAttempted bit on the class access flags to determine whether this has been done |
| 3930 | // before. |
| 3931 | static void EnsureSkipAccessChecksMethods(Handle<mirror::Class> klass, PointerSize pointer_size) |
| 3932 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3933 | if (!klass->WasVerificationAttempted()) { |
| 3934 | klass->SetSkipAccessChecksFlagOnAllMethods(pointer_size); |
| 3935 | klass->SetVerificationAttempted(); |
| 3936 | } |
| 3937 | } |
| 3938 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 3939 | verifier::FailureKind ClassLinker::VerifyClass( |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 3940 | Thread* self, Handle<mirror::Class> klass, verifier::HardFailLogMode log_level) { |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3941 | { |
| 3942 | // TODO: assert that the monitor on the Class is held |
| 3943 | ObjectLock<mirror::Class> lock(self, klass); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 3944 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3945 | // Is somebody verifying this now? |
| 3946 | mirror::Class::Status old_status = klass->GetStatus(); |
| 3947 | while (old_status == mirror::Class::kStatusVerifying || |
| 3948 | old_status == mirror::Class::kStatusVerifyingAtRuntime) { |
| 3949 | lock.WaitIgnoringInterrupts(); |
Mathieu Chartier | 5ef7020 | 2017-06-29 10:45:10 -0700 | [diff] [blame] | 3950 | // WaitIgnoringInterrupts can still receive an interrupt and return early, in this |
| 3951 | // case we may see the same status again. b/62912904. This is why the check is |
| 3952 | // greater or equal. |
| 3953 | CHECK(klass->IsErroneous() || (klass->GetStatus() >= old_status)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3954 | << "Class '" << klass->PrettyClass() |
| 3955 | << "' performed an illegal verification state transition from " << old_status |
| 3956 | << " to " << klass->GetStatus(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3957 | old_status = klass->GetStatus(); |
| 3958 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3959 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3960 | // The class might already be erroneous, for example at compile time if we attempted to verify |
| 3961 | // this class as a parent to another. |
| 3962 | if (klass->IsErroneous()) { |
| 3963 | ThrowEarlierClassFailure(klass.Get()); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 3964 | return verifier::FailureKind::kHardFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3965 | } |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 3966 | |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 3967 | // Don't attempt to re-verify if already verified. |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3968 | if (klass->IsVerified()) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3969 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 3970 | return verifier::FailureKind::kNoFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3971 | } |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 3972 | |
| 3973 | // For AOT, don't attempt to re-verify if we have already found we should |
| 3974 | // verify at runtime. |
| 3975 | if (Runtime::Current()->IsAotCompiler() && klass->ShouldVerifyAtRuntime()) { |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 3976 | return verifier::FailureKind::kSoftFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3977 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3978 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3979 | if (klass->GetStatus() == mirror::Class::kStatusResolved) { |
| 3980 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerifying, self); |
| 3981 | } else { |
| 3982 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3983 | << klass->PrettyClass(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3984 | CHECK(!Runtime::Current()->IsAotCompiler()); |
| 3985 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerifyingAtRuntime, self); |
| 3986 | } |
| 3987 | |
| 3988 | // Skip verification if disabled. |
| 3989 | if (!Runtime::Current()->IsVerificationEnabled()) { |
| 3990 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, self); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3991 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 3992 | return verifier::FailureKind::kNoFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3993 | } |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 3994 | } |
| 3995 | |
Bharadwaj Kalandhabhatta | 271c1e1 | 2017-06-27 11:14:49 -0700 | [diff] [blame] | 3996 | VLOG(class_linker) << "Beginning verification for class: " |
| 3997 | << klass->PrettyDescriptor() |
| 3998 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
| 3999 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 4000 | // Verify super class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4001 | StackHandleScope<2> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4002 | MutableHandle<mirror::Class> supertype(hs.NewHandle(klass->GetSuperClass())); |
| 4003 | // 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] | 4004 | if (supertype != nullptr && !AttemptSupertypeVerification(self, klass, supertype)) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4005 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4006 | return verifier::FailureKind::kHardFailure; |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4007 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4008 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4009 | // Verify all default super-interfaces. |
| 4010 | // |
| 4011 | // (1) Don't bother if the superclass has already had a soft verification failure. |
| 4012 | // |
| 4013 | // (2) Interfaces shouldn't bother to do this recursive verification because they cannot cause |
| 4014 | // recursive initialization by themselves. This is because when an interface is initialized |
| 4015 | // directly it must not initialize its superinterfaces. We are allowed to verify regardless |
| 4016 | // but choose not to for an optimization. If the interfaces is being verified due to a class |
| 4017 | // initialization (which would need all the default interfaces to be verified) the class code |
| 4018 | // will trigger the recursive verification anyway. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4019 | if ((supertype == nullptr || supertype->IsVerified()) // See (1) |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4020 | && !klass->IsInterface()) { // See (2) |
| 4021 | int32_t iftable_count = klass->GetIfTableCount(); |
| 4022 | MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 4023 | // Loop through all interfaces this class has defined. It doesn't matter the order. |
| 4024 | for (int32_t i = 0; i < iftable_count; i++) { |
| 4025 | iface.Assign(klass->GetIfTable()->GetInterface(i)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4026 | DCHECK(iface != nullptr); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4027 | // We only care if we have default interfaces and can skip if we are already verified... |
| 4028 | if (LIKELY(!iface->HasDefaultMethods() || iface->IsVerified())) { |
| 4029 | continue; |
| 4030 | } else if (UNLIKELY(!AttemptSupertypeVerification(self, klass, iface))) { |
| 4031 | // We had a hard failure while verifying this interface. Just return immediately. |
| 4032 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4033 | return verifier::FailureKind::kHardFailure; |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4034 | } else if (UNLIKELY(!iface->IsVerified())) { |
| 4035 | // We softly failed to verify the iface. Stop checking and clean up. |
| 4036 | // Put the iface into the supertype handle so we know what caused us to fail. |
| 4037 | supertype.Assign(iface.Get()); |
| 4038 | break; |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4039 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4040 | } |
| 4041 | } |
| 4042 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4043 | // At this point if verification failed, then supertype is the "first" supertype that failed |
| 4044 | // verification (without a specific order). If verification succeeded, then supertype is either |
| 4045 | // null or the original superclass of klass and is verified. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4046 | DCHECK(supertype == nullptr || |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4047 | supertype.Get() == klass->GetSuperClass() || |
| 4048 | !supertype->IsVerified()); |
| 4049 | |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 4050 | // 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] | 4051 | const DexFile& dex_file = *klass->GetDexCache()->GetDexFile(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4052 | mirror::Class::Status oat_file_class_status(mirror::Class::kStatusNotReady); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4053 | bool preverified = VerifyClassUsingOatFile(dex_file, klass.Get(), oat_file_class_status); |
Bharadwaj Kalandhabhatta | 271c1e1 | 2017-06-27 11:14:49 -0700 | [diff] [blame] | 4054 | |
| 4055 | VLOG(class_linker) << "Class preverified status for class " |
| 4056 | << klass->PrettyDescriptor() |
| 4057 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4058 | << ": " |
| 4059 | << preverified; |
| 4060 | |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4061 | // If the oat file says the class had an error, re-run the verifier. That way we will get a |
| 4062 | // precise error message. To ensure a rerun, test: |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4063 | // mirror::Class::IsErroneous(oat_file_class_status) => !preverified |
| 4064 | DCHECK(!mirror::Class::IsErroneous(oat_file_class_status) || !preverified); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4065 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4066 | std::string error_msg; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4067 | verifier::FailureKind verifier_failure = verifier::FailureKind::kNoFailure; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 4068 | if (!preverified) { |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4069 | verifier_failure = PerformClassVerification(self, klass, log_level, &error_msg); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 4070 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4071 | |
| 4072 | // Verification is done, grab the lock again. |
| 4073 | ObjectLock<mirror::Class> lock(self, klass); |
| 4074 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4075 | if (preverified || verifier_failure != verifier::FailureKind::kHardFailure) { |
| 4076 | if (!preverified && verifier_failure != verifier::FailureKind::kNoFailure) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4077 | VLOG(class_linker) << "Soft verification failure in class " |
| 4078 | << klass->PrettyDescriptor() |
| 4079 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4080 | << " because: " << error_msg; |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 4081 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 4082 | self->AssertNoPendingException(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4083 | // Make sure all classes referenced by catch blocks are resolved. |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4084 | ResolveClassExceptionHandlerTypes(klass); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4085 | if (verifier_failure == verifier::FailureKind::kNoFailure) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4086 | // Even though there were no verifier failures we need to respect whether the super-class and |
| 4087 | // super-default-interfaces were verified or requiring runtime reverification. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4088 | if (supertype == nullptr || supertype->IsVerified()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4089 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4090 | } else { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4091 | CHECK_EQ(supertype->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4092 | mirror::Class::SetStatus(klass, mirror::Class::kStatusRetryVerificationAtRuntime, self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4093 | // 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] | 4094 | verifier_failure = verifier::FailureKind::kSoftFailure; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4095 | } |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4096 | } else { |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4097 | CHECK_EQ(verifier_failure, verifier::FailureKind::kSoftFailure); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4098 | // Soft failures at compile time should be retried at runtime. Soft |
| 4099 | // failures at runtime will be handled by slow paths in the generated |
| 4100 | // code. Set status accordingly. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4101 | if (Runtime::Current()->IsAotCompiler()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4102 | mirror::Class::SetStatus(klass, mirror::Class::kStatusRetryVerificationAtRuntime, self); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4103 | } else { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4104 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, self); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4105 | // As this is a fake verified status, make sure the methods are _not_ marked |
| 4106 | // kAccSkipAccessChecks later. |
| 4107 | klass->SetVerificationAttempted(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4108 | } |
| 4109 | } |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 4110 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4111 | VLOG(verifier) << "Verification failed on class " << klass->PrettyDescriptor() |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4112 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4113 | << " because: " << error_msg; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4114 | self->AssertNoPendingException(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4115 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4116 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 4117 | } |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4118 | if (preverified || verifier_failure == verifier::FailureKind::kNoFailure) { |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 4119 | // 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] | 4120 | // Let the interpreter know it by setting the kAccSkipAccessChecks flag onto each |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 4121 | // method. |
| 4122 | // 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] | 4123 | // kAccSkipAccessChecks flag when compiling image classes, the flag is recorded |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 4124 | // in the image and is set when loading the image. |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4125 | |
| 4126 | if (UNLIKELY(Runtime::Current()->IsVerificationSoftFail())) { |
| 4127 | // Never skip access checks if the verification soft fail is forced. |
| 4128 | // Mark the class as having a verification attempt to avoid re-running the verifier. |
| 4129 | klass->SetVerificationAttempted(); |
| 4130 | } else { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4131 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4132 | } |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4133 | } |
Nicolas Geoffray | 486dda0 | 2017-09-11 14:15:52 +0100 | [diff] [blame] | 4134 | // Done verifying. Notify the compiler about the verification status, in case the class |
| 4135 | // was verified implicitly (eg super class of a compiled class). |
| 4136 | if (Runtime::Current()->IsAotCompiler()) { |
| 4137 | Runtime::Current()->GetCompilerCallbacks()->UpdateClassState( |
| 4138 | ClassReference(&klass->GetDexFile(), klass->GetDexClassDefIndex()), klass->GetStatus()); |
| 4139 | } |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 4140 | return verifier_failure; |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4141 | } |
| 4142 | |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4143 | verifier::FailureKind ClassLinker::PerformClassVerification(Thread* self, |
| 4144 | Handle<mirror::Class> klass, |
| 4145 | verifier::HardFailLogMode log_level, |
| 4146 | std::string* error_msg) { |
| 4147 | Runtime* const runtime = Runtime::Current(); |
| 4148 | return verifier::MethodVerifier::VerifyClass(self, |
| 4149 | klass.Get(), |
| 4150 | runtime->GetCompilerCallbacks(), |
| 4151 | runtime->IsAotCompiler(), |
| 4152 | log_level, |
| 4153 | error_msg); |
| 4154 | } |
| 4155 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4156 | bool ClassLinker::VerifyClassUsingOatFile(const DexFile& dex_file, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4157 | ObjPtr<mirror::Class> klass, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4158 | mirror::Class::Status& oat_file_class_status) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4159 | // If we're compiling, we can only verify the class using the oat file if |
| 4160 | // we are not compiling the image or if the class we're verifying is not part of |
| 4161 | // the app. In other words, we will only check for preverification of bootclasspath |
| 4162 | // classes. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4163 | if (Runtime::Current()->IsAotCompiler()) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4164 | // Are we compiling the bootclasspath? |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 4165 | if (Runtime::Current()->GetCompilerCallbacks()->IsBootImage()) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4166 | return false; |
| 4167 | } |
| 4168 | // We are compiling an app (not the image). |
| 4169 | |
| 4170 | // Is this an app class? (I.e. not a bootclasspath class) |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4171 | if (klass->GetClassLoader() != nullptr) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4172 | return false; |
| 4173 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4174 | } |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4175 | |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 4176 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4177 | // 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] | 4178 | if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) { |
Anwar Ghuloum | ad256bb | 2013-07-18 14:58:55 -0700 | [diff] [blame] | 4179 | return false; |
| 4180 | } |
| 4181 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4182 | uint16_t class_def_index = klass->GetDexClassDefIndex(); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 4183 | oat_file_class_status = oat_dex_file->GetOatClass(class_def_index).GetStatus(); |
Jeff Hao | 0cb1728 | 2017-07-12 14:51:49 -0700 | [diff] [blame] | 4184 | if (oat_file_class_status >= mirror::Class::kStatusVerified) { |
Mathieu Chartier | a079e3a | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 4185 | return true; |
| 4186 | } |
| 4187 | // If we only verified a subset of the classes at compile time, we can end up with classes that |
| 4188 | // were resolved by the verifier. |
| 4189 | if (oat_file_class_status == mirror::Class::kStatusResolved) { |
| 4190 | return false; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4191 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4192 | if (oat_file_class_status == mirror::Class::kStatusRetryVerificationAtRuntime) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 4193 | // Compile time verification failed with a soft error. Compile time verification can fail |
| 4194 | // because we have incomplete type information. Consider the following: |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 4195 | // class ... { |
| 4196 | // Foo x; |
| 4197 | // .... () { |
| 4198 | // if (...) { |
| 4199 | // v1 gets assigned a type of resolved class Foo |
| 4200 | // } else { |
| 4201 | // v1 gets assigned a type of unresolved class Bar |
| 4202 | // } |
| 4203 | // iput x = v1 |
| 4204 | // } } |
| 4205 | // when we merge v1 following the if-the-else it results in Conflict |
| 4206 | // (see verifier::RegType::Merge) as we can't know the type of Bar and we could possibly be |
| 4207 | // allowing an unsafe assignment to the field x in the iput (javac may have compiled this as |
| 4208 | // it knew Bar was a sub-class of Foo, but for us this may have been moved into a separate apk |
| 4209 | // at compile time). |
| 4210 | return false; |
| 4211 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4212 | if (mirror::Class::IsErroneous(oat_file_class_status)) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 4213 | // Compile time verification failed with a hard error. This is caused by invalid instructions |
| 4214 | // in the class. These errors are unrecoverable. |
| 4215 | return false; |
| 4216 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4217 | if (oat_file_class_status == mirror::Class::kStatusNotReady) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 4218 | // Status is uninitialized if we couldn't determine the status at compile time, for example, |
| 4219 | // not loading the class. |
| 4220 | // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy |
| 4221 | // isn't a problem and this case shouldn't occur |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4222 | return false; |
| 4223 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4224 | std::string temp; |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 4225 | LOG(FATAL) << "Unexpected class status: " << oat_file_class_status |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4226 | << " " << dex_file.GetLocation() << " " << klass->PrettyClass() << " " |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4227 | << klass->GetDescriptor(&temp); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4228 | UNREACHABLE(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4229 | } |
| 4230 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4231 | void ClassLinker::ResolveClassExceptionHandlerTypes(Handle<mirror::Class> klass) { |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 4232 | for (ArtMethod& method : klass->GetMethods(image_pointer_size_)) { |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4233 | ResolveMethodExceptionHandlerTypes(&method); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4234 | } |
| 4235 | } |
| 4236 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4237 | void ClassLinker::ResolveMethodExceptionHandlerTypes(ArtMethod* method) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4238 | // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod. |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4239 | const DexFile::CodeItem* code_item = |
| 4240 | method->GetDexFile()->GetCodeItem(method->GetCodeItemOffset()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4241 | if (code_item == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4242 | return; // native or abstract method |
| 4243 | } |
| 4244 | if (code_item->tries_size_ == 0) { |
| 4245 | return; // nothing to process |
| 4246 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 4247 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item, 0); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4248 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4249 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| 4250 | CatchHandlerIterator iterator(handlers_ptr); |
| 4251 | for (; iterator.HasNext(); iterator.Next()) { |
| 4252 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 4253 | // unresolved exception types will be ignored by exception delivery |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4254 | if (iterator.GetHandlerTypeIndex().IsValid()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4255 | ObjPtr<mirror::Class> exception_type = ResolveType(iterator.GetHandlerTypeIndex(), method); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4256 | if (exception_type == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4257 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4258 | Thread::Current()->ClearException(); |
| 4259 | } |
| 4260 | } |
| 4261 | } |
| 4262 | handlers_ptr = iterator.EndDataPointer(); |
| 4263 | } |
| 4264 | } |
| 4265 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4266 | mirror::Class* ClassLinker::CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, |
| 4267 | jstring name, |
| 4268 | jobjectArray interfaces, |
| 4269 | jobject loader, |
| 4270 | jobjectArray methods, |
| 4271 | jobjectArray throws) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4272 | Thread* self = soa.Self(); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4273 | StackHandleScope<10> hs(self); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4274 | MutableHandle<mirror::Class> temp_klass(hs.NewHandle( |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4275 | AllocClass(self, GetClassRoot(kJavaLangClass), sizeof(mirror::Class)))); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4276 | if (temp_klass == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4277 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4278 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4279 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4280 | DCHECK(temp_klass->GetClass() != nullptr); |
| 4281 | temp_klass->SetObjectSize(sizeof(mirror::Proxy)); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4282 | // Set the class access flags incl. VerificationAttempted, so we do not try to set the flag on |
| 4283 | // the methods. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4284 | temp_klass->SetAccessFlags(kAccClassIsProxy | kAccPublic | kAccFinal | kAccVerificationAttempted); |
| 4285 | temp_klass->SetClassLoader(soa.Decode<mirror::ClassLoader>(loader)); |
| 4286 | DCHECK_EQ(temp_klass->GetPrimitiveType(), Primitive::kPrimNot); |
| 4287 | temp_klass->SetName(soa.Decode<mirror::String>(name)); |
| 4288 | temp_klass->SetDexCache(GetClassRoot(kJavaLangReflectProxy)->GetDexCache()); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4289 | // Object has an empty iftable, copy it for that reason. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4290 | temp_klass->SetIfTable(GetClassRoot(kJavaLangObject)->GetIfTable()); |
| 4291 | mirror::Class::SetStatus(temp_klass, mirror::Class::kStatusIdx, self); |
| 4292 | std::string descriptor(GetDescriptorForProxy(temp_klass.Get())); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 4293 | const size_t hash = ComputeModifiedUtf8Hash(descriptor.c_str()); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4294 | |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 4295 | // 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] | 4296 | LinearAlloc* const allocator = GetOrCreateAllocatorForClassLoader(temp_klass->GetClassLoader()); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 4297 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4298 | // Insert the class before loading the fields as the field roots |
| 4299 | // (ArtField::declaring_class_) are only visited from the class |
| 4300 | // table. There can't be any suspend points between inserting the |
| 4301 | // class and setting the field arrays below. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4302 | ObjPtr<mirror::Class> existing = InsertClass(descriptor.c_str(), temp_klass.Get(), hash); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4303 | CHECK(existing == nullptr); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4304 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4305 | // Instance fields are inherited, but we add a couple of static fields... |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4306 | const size_t num_fields = 2; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 4307 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, allocator, num_fields); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4308 | temp_klass->SetSFieldsPtr(sfields); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4309 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4310 | // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by |
| 4311 | // our proxy, so Class.getInterfaces doesn't return the flattened set. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4312 | ArtField& interfaces_sfield = sfields->At(0); |
| 4313 | interfaces_sfield.SetDexFieldIndex(0); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4314 | interfaces_sfield.SetDeclaringClass(temp_klass.Get()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4315 | interfaces_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4316 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4317 | // 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] | 4318 | ArtField& throws_sfield = sfields->At(1); |
| 4319 | throws_sfield.SetDexFieldIndex(1); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4320 | throws_sfield.SetDeclaringClass(temp_klass.Get()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4321 | throws_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4322 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4323 | // Proxies have 1 direct method, the constructor |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4324 | const size_t num_direct_methods = 1; |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4325 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4326 | // They have as many virtual methods as the array |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4327 | auto h_methods = hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Method>>(methods)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 4328 | DCHECK_EQ(h_methods->GetClass(), mirror::Method::ArrayClass()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4329 | << mirror::Class::PrettyClass(h_methods->GetClass()); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 4330 | const size_t num_virtual_methods = h_methods->GetLength(); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4331 | |
| 4332 | // Create the methods array. |
| 4333 | LengthPrefixedArray<ArtMethod>* proxy_class_methods = AllocArtMethodArray( |
| 4334 | self, allocator, num_direct_methods + num_virtual_methods); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4335 | // Currently AllocArtMethodArray cannot return null, but the OOM logic is left there in case we |
| 4336 | // want to throw OOM in the future. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4337 | if (UNLIKELY(proxy_class_methods == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4338 | self->AssertPendingOOMException(); |
| 4339 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4340 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4341 | temp_klass->SetMethodsPtr(proxy_class_methods, num_direct_methods, num_virtual_methods); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4342 | |
| 4343 | // Create the single direct method. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4344 | CreateProxyConstructor(temp_klass, temp_klass->GetDirectMethodUnchecked(0, image_pointer_size_)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4345 | |
| 4346 | // Create virtual method using specified prototypes. |
| 4347 | // TODO These should really use the iterators. |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4348 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4349 | auto* virtual_method = temp_klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4350 | auto* prototype = h_methods->Get(i)->GetArtMethod(); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4351 | CreateProxyMethod(temp_klass, prototype, virtual_method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4352 | DCHECK(virtual_method->GetDeclaringClass() != nullptr); |
| 4353 | DCHECK(prototype->GetDeclaringClass() != nullptr); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4354 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4355 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4356 | // The super class is java.lang.reflect.Proxy |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4357 | temp_klass->SetSuperClass(GetClassRoot(kJavaLangReflectProxy)); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4358 | // Now effectively in the loaded state. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4359 | mirror::Class::SetStatus(temp_klass, mirror::Class::kStatusLoaded, self); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4360 | self->AssertNoPendingException(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4361 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4362 | // At this point the class is loaded. Publish a ClassLoad event. |
| 4363 | // Note: this may be a temporary class. It is a listener's responsibility to handle this. |
| 4364 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(temp_klass); |
| 4365 | |
| 4366 | MutableHandle<mirror::Class> klass = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 4367 | { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4368 | // Must hold lock on object when resolved. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4369 | ObjectLock<mirror::Class> resolution_lock(self, temp_klass); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4370 | // Link the fields and virtual methods, creating vtable and iftables. |
| 4371 | // The new class will replace the old one in the class table. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4372 | Handle<mirror::ObjectArray<mirror::Class>> h_interfaces( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4373 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces))); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4374 | if (!LinkClass(self, descriptor.c_str(), temp_klass, h_interfaces, &klass)) { |
| 4375 | mirror::Class::SetStatus(temp_klass, mirror::Class::kStatusErrorUnresolved, self); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4376 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4377 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4378 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4379 | CHECK(temp_klass->IsRetired()); |
| 4380 | CHECK_NE(temp_klass.Get(), klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4381 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4382 | CHECK_EQ(interfaces_sfield.GetDeclaringClass(), klass.Get()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4383 | interfaces_sfield.SetObject<false>( |
| 4384 | klass.Get(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4385 | soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4386 | CHECK_EQ(throws_sfield.GetDeclaringClass(), klass.Get()); |
| 4387 | throws_sfield.SetObject<false>( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4388 | klass.Get(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4389 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>(throws)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4390 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4391 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(temp_klass, klass); |
| 4392 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4393 | { |
| 4394 | // Lock on klass is released. Lock new class object. |
| 4395 | ObjectLock<mirror::Class> initialization_lock(self, klass); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4396 | mirror::Class::SetStatus(klass, mirror::Class::kStatusInitialized, self); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 4397 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4398 | |
| 4399 | // sanity checks |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 4400 | if (kIsDebugBuild) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4401 | CHECK(klass->GetIFieldsPtr() == nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4402 | CheckProxyConstructor(klass->GetDirectMethod(0, image_pointer_size_)); |
| 4403 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4404 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4405 | auto* virtual_method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
| 4406 | auto* prototype = h_methods->Get(i++)->GetArtMethod(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4407 | CheckProxyMethod(virtual_method, prototype); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4408 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4409 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4410 | StackHandleScope<1> hs2(self); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4411 | Handle<mirror::String> decoded_name = hs2.NewHandle(soa.Decode<mirror::String>(name)); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4412 | std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4413 | decoded_name->ToModifiedUtf8().c_str())); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4414 | CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(0)), interfaces_field_name); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4415 | |
| 4416 | std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4417 | decoded_name->ToModifiedUtf8().c_str())); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4418 | CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(1)), throws_field_name); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4419 | |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 4420 | CHECK_EQ(klass.Get()->GetProxyInterfaces(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4421 | soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)); |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 4422 | CHECK_EQ(klass.Get()->GetProxyThrows(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4423 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>(throws)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4424 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4425 | return klass.Get(); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4426 | } |
| 4427 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4428 | std::string ClassLinker::GetDescriptorForProxy(ObjPtr<mirror::Class> proxy_class) { |
Nicolas Geoffray | a7a4759 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 4429 | DCHECK(proxy_class->IsProxyClass()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4430 | ObjPtr<mirror::String> name = proxy_class->GetName(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4431 | DCHECK(name != nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4432 | return DotToDescriptor(name->ToModifiedUtf8().c_str()); |
| 4433 | } |
| 4434 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4435 | void ClassLinker::CreateProxyConstructor(Handle<mirror::Class> klass, ArtMethod* out) { |
| 4436 | // Create constructor for Proxy that must initialize the method. |
Przemyslaw Szczepaniak | f11cd29 | 2016-08-17 17:46:38 +0100 | [diff] [blame] | 4437 | CHECK_EQ(GetClassRoot(kJavaLangReflectProxy)->NumDirectMethods(), 23u); |
| 4438 | |
Igor Murashkin | 9d3d752 | 2017-02-27 10:39:49 -0800 | [diff] [blame] | 4439 | // Find the <init>(InvocationHandler)V method. The exact method offset varies depending |
| 4440 | // on which front-end compiler was used to build the libcore DEX files. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 4441 | ArtMethod* proxy_constructor = GetClassRoot(kJavaLangReflectProxy)->FindConstructor( |
| 4442 | "(Ljava/lang/reflect/InvocationHandler;)V", image_pointer_size_); |
Igor Murashkin | 9d3d752 | 2017-02-27 10:39:49 -0800 | [diff] [blame] | 4443 | DCHECK(proxy_constructor != nullptr) |
| 4444 | << "Could not find <init> method in java.lang.reflect.Proxy"; |
| 4445 | |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 4446 | // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its |
| 4447 | // code_ too) |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4448 | DCHECK(out != nullptr); |
| 4449 | out->CopyFrom(proxy_constructor, image_pointer_size_); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 4450 | // 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] | 4451 | // Mark kAccCompileDontBother so that we don't take JIT samples for the method. b/62349349 |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 4452 | // Note that the compiler calls a ResolveMethod() overload that does not handle a Proxy referrer. |
Mathieu Chartier | 201e297 | 2017-06-05 18:34:53 -0700 | [diff] [blame] | 4453 | out->SetAccessFlags((out->GetAccessFlags() & ~kAccProtected) | |
| 4454 | kAccPublic | |
| 4455 | kAccCompileDontBother); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4456 | out->SetDeclaringClass(klass.Get()); |
Vladimir Marko | d1ee20f | 2017-08-17 09:21:16 +0000 | [diff] [blame] | 4457 | |
| 4458 | // Set the original constructor method. |
| 4459 | out->SetDataPtrSize(proxy_constructor, image_pointer_size_); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4460 | } |
| 4461 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4462 | void ClassLinker::CheckProxyConstructor(ArtMethod* constructor) const { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4463 | CHECK(constructor->IsConstructor()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4464 | auto* np = constructor->GetInterfaceMethodIfProxy(image_pointer_size_); |
| 4465 | CHECK_STREQ(np->GetName(), "<init>"); |
| 4466 | CHECK_STREQ(np->GetSignature().ToString().c_str(), "(Ljava/lang/reflect/InvocationHandler;)V"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4467 | DCHECK(constructor->IsPublic()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4468 | } |
| 4469 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4470 | void ClassLinker::CreateProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4471 | ArtMethod* out) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4472 | // 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] | 4473 | // as necessary |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4474 | DCHECK(out != nullptr); |
| 4475 | out->CopyFrom(prototype, image_pointer_size_); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4476 | |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 4477 | // Set class to be the concrete proxy class. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4478 | out->SetDeclaringClass(klass.Get()); |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 4479 | // Clear the abstract, default and conflict flags to ensure that defaults aren't picked in |
| 4480 | // preference to the invocation handler. |
| 4481 | const uint32_t kRemoveFlags = kAccAbstract | kAccDefault | kAccDefaultConflict; |
| 4482 | // Make the method final. |
Mathieu Chartier | 201e297 | 2017-06-05 18:34:53 -0700 | [diff] [blame] | 4483 | // Mark kAccCompileDontBother so that we don't take JIT samples for the method. b/62349349 |
| 4484 | const uint32_t kAddFlags = kAccFinal | kAccCompileDontBother; |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 4485 | out->SetAccessFlags((out->GetAccessFlags() & ~kRemoveFlags) | kAddFlags); |
| 4486 | |
| 4487 | // Clear the dex_code_item_offset_. It needs to be 0 since proxy methods have no CodeItems but the |
| 4488 | // method they copy might (if it's a default method). |
| 4489 | out->SetCodeItemOffset(0); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4490 | |
Vladimir Marko | d1ee20f | 2017-08-17 09:21:16 +0000 | [diff] [blame] | 4491 | // Set the original interface method. |
| 4492 | out->SetDataPtrSize(prototype, image_pointer_size_); |
| 4493 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4494 | // At runtime the method looks like a reference and argument saving method, clone the code |
| 4495 | // related parameters from this method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4496 | out->SetEntryPointFromQuickCompiledCode(GetQuickProxyInvokeHandler()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4497 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4498 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4499 | void ClassLinker::CheckProxyMethod(ArtMethod* method, ArtMethod* prototype) const { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4500 | // Basic sanity |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4501 | CHECK(!prototype->IsFinal()); |
| 4502 | CHECK(method->IsFinal()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 4503 | CHECK(method->IsInvokable()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 4504 | |
| 4505 | // The proxy method doesn't have its own dex cache or dex file and so it steals those of its |
| 4506 | // interface prototype. The exception to this are Constructors and the Class of the Proxy itself. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 4507 | CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex()); |
Vladimir Marko | 5c3e9d1 | 2017-08-30 16:43:54 +0100 | [diff] [blame] | 4508 | CHECK_EQ(prototype, method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4509 | } |
| 4510 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4511 | bool ClassLinker::CanWeInitializeClass(ObjPtr<mirror::Class> klass, bool can_init_statics, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4512 | bool can_init_parents) { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 4513 | if (can_init_statics && can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4514 | return true; |
| 4515 | } |
| 4516 | if (!can_init_statics) { |
| 4517 | // Check if there's a class initializer. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4518 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4519 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4520 | return false; |
| 4521 | } |
| 4522 | // Check if there are encoded static values needing initialization. |
| 4523 | if (klass->NumStaticFields() != 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4524 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4525 | DCHECK(dex_class_def != nullptr); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4526 | if (dex_class_def->static_values_off_ != 0) { |
| 4527 | return false; |
| 4528 | } |
| 4529 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4530 | // If we are a class we need to initialize all interfaces with default methods when we are |
| 4531 | // initialized. Check all of them. |
| 4532 | if (!klass->IsInterface()) { |
| 4533 | size_t num_interfaces = klass->GetIfTableCount(); |
| 4534 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4535 | ObjPtr<mirror::Class> iface = klass->GetIfTable()->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4536 | if (iface->HasDefaultMethods() && |
| 4537 | !CanWeInitializeClass(iface, can_init_statics, can_init_parents)) { |
| 4538 | return false; |
| 4539 | } |
| 4540 | } |
| 4541 | } |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4542 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4543 | if (klass->IsInterface() || !klass->HasSuperClass()) { |
| 4544 | return true; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4545 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4546 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4547 | if (!can_init_parents && !super_class->IsInitialized()) { |
| 4548 | return false; |
| 4549 | } |
| 4550 | return CanWeInitializeClass(super_class, can_init_statics, can_init_parents); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4551 | } |
| 4552 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4553 | bool ClassLinker::InitializeClass(Thread* self, Handle<mirror::Class> klass, |
| 4554 | bool can_init_statics, bool can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4555 | // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol |
| 4556 | |
| 4557 | // Are we already initialized and therefore done? |
| 4558 | // Note: we differ from the JLS here as we don't do this under the lock, this is benign as |
| 4559 | // an initialized class will never change its state. |
| 4560 | if (klass->IsInitialized()) { |
| 4561 | return true; |
| 4562 | } |
| 4563 | |
| 4564 | // 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] | 4565 | if (!CanWeInitializeClass(klass.Get(), can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4566 | return false; |
| 4567 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4568 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4569 | self->AllowThreadSuspension(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4570 | uint64_t t0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4571 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4572 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4573 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4574 | // Re-check under the lock in case another thread initialized ahead of us. |
| 4575 | if (klass->IsInitialized()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4576 | return true; |
| 4577 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4578 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4579 | // 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] | 4580 | if (klass->IsErroneous()) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 4581 | ThrowEarlierClassFailure(klass.Get(), true); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4582 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4583 | return false; |
| 4584 | } |
| 4585 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4586 | CHECK(klass->IsResolved() && !klass->IsErroneousResolved()) |
| 4587 | << klass->PrettyClass() << ": state=" << klass->GetStatus(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4588 | |
| 4589 | if (!klass->IsVerified()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4590 | VerifyClass(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4591 | if (!klass->IsVerified()) { |
| 4592 | // We failed to verify, expect either the klass to be erroneous or verification failed at |
| 4593 | // compile time. |
| 4594 | if (klass->IsErroneous()) { |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 4595 | // The class is erroneous. This may be a verifier error, or another thread attempted |
| 4596 | // verification and/or initialization and failed. We can distinguish those cases by |
| 4597 | // whether an exception is already pending. |
| 4598 | if (self->IsExceptionPending()) { |
| 4599 | // Check that it's a VerifyError. |
| 4600 | DCHECK_EQ("java.lang.Class<java.lang.VerifyError>", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4601 | mirror::Class::PrettyClass(self->GetException()->GetClass())); |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 4602 | } else { |
| 4603 | // Check that another thread attempted initialization. |
| 4604 | DCHECK_NE(0, klass->GetClinitThreadId()); |
| 4605 | DCHECK_NE(self->GetTid(), klass->GetClinitThreadId()); |
| 4606 | // Need to rethrow the previous failure now. |
| 4607 | ThrowEarlierClassFailure(klass.Get(), true); |
| 4608 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4609 | VlogClassInitializationFailure(klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4610 | } else { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4611 | CHECK(Runtime::Current()->IsAotCompiler()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4612 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 4613 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4614 | return false; |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4615 | } else { |
| 4616 | self->AssertNoPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4617 | } |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 4618 | |
| 4619 | // A separate thread could have moved us all the way to initialized. A "simple" example |
| 4620 | // involves a subclass of the current class being initialized at the same time (which |
| 4621 | // will implicitly initialize the superclass, if scheduled that way). b/28254258 |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4622 | DCHECK(!klass->IsErroneous()) << klass->GetStatus(); |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 4623 | if (klass->IsInitialized()) { |
| 4624 | return true; |
| 4625 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4626 | } |
| 4627 | |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4628 | // If the class is kStatusInitializing, either this thread is |
| 4629 | // initializing higher up the stack or another thread has beat us |
| 4630 | // to initializing and we need to wait. Either way, this |
| 4631 | // invocation of InitializeClass will not be responsible for |
| 4632 | // running <clinit> and will return. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4633 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4634 | // Could have got an exception during verification. |
| 4635 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4636 | VlogClassInitializationFailure(klass); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4637 | return false; |
| 4638 | } |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 4639 | // We caught somebody else in the act; was it us? |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 4640 | if (klass->GetClinitThreadId() == self->GetTid()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4641 | // Yes. That's fine. Return so we can continue initializing. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4642 | return true; |
| 4643 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4644 | // No. That's fine. Wait for another thread to finish initializing. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4645 | return WaitForInitializeClass(klass, self, lock); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4646 | } |
| 4647 | |
Jeff Hao | e2e4034 | 2017-07-19 10:45:18 -0700 | [diff] [blame] | 4648 | // Try to get the oat class's status for this class if the oat file is present. The compiler |
| 4649 | // tries to validate superclass descriptors, and writes the result into the oat file. |
| 4650 | // Runtime correctness is guaranteed by classpath checks done on loading. If the classpath |
| 4651 | // is different at runtime than it was at compile time, the oat file is rejected. So if the |
| 4652 | // oat file is present, the classpaths must match, and the runtime time check can be skipped. |
Jeff Hao | 0cb1728 | 2017-07-12 14:51:49 -0700 | [diff] [blame] | 4653 | bool has_oat_class = false; |
Jeff Hao | e2e4034 | 2017-07-19 10:45:18 -0700 | [diff] [blame] | 4654 | const Runtime* runtime = Runtime::Current(); |
| 4655 | const OatFile::OatClass oat_class = (runtime->IsStarted() && !runtime->IsAotCompiler()) |
| 4656 | ? OatFile::FindOatClass(klass->GetDexFile(), klass->GetDexClassDefIndex(), &has_oat_class) |
| 4657 | : OatFile::OatClass::Invalid(); |
Jeff Hao | 0cb1728 | 2017-07-12 14:51:49 -0700 | [diff] [blame] | 4658 | if (oat_class.GetStatus() < mirror::Class::kStatusSuperclassValidated && |
| 4659 | !ValidateSuperClassDescriptors(klass)) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4660 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4661 | return false; |
| 4662 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4663 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4664 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4665 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusVerified) << klass->PrettyClass() |
Andreas Gampe | 9510ccd | 2016-04-20 09:55:25 -0700 | [diff] [blame] | 4666 | << " self.tid=" << self->GetTid() << " clinit.tid=" << klass->GetClinitThreadId(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4667 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4668 | // From here out other threads may observe that we're initializing and so changes of state |
| 4669 | // require the a notification. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 4670 | klass->SetClinitThreadId(self->GetTid()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4671 | mirror::Class::SetStatus(klass, mirror::Class::kStatusInitializing, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4672 | |
| 4673 | t0 = NanoTime(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4674 | } |
| 4675 | |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 4676 | // Initialize super classes, must be done while initializing for the JLS. |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4677 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4678 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4679 | if (!super_class->IsInitialized()) { |
| 4680 | CHECK(!super_class->IsInterface()); |
| 4681 | CHECK(can_init_parents); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4682 | StackHandleScope<1> hs(self); |
| 4683 | Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4684 | bool super_initialized = InitializeClass(self, handle_scope_super, can_init_statics, true); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4685 | if (!super_initialized) { |
| 4686 | // The super class was verified ahead of entering initializing, we should only be here if |
| 4687 | // the super class became erroneous due to initialization. |
Chang Xing | adbb91c | 2017-07-17 11:23:55 -0700 | [diff] [blame] | 4688 | // For the case of aot compiler, the super class might also be initializing but we don't |
| 4689 | // want to process circular dependencies in pre-compile. |
| 4690 | CHECK(self->IsExceptionPending()) |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4691 | << "Super class initialization failed for " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4692 | << handle_scope_super->PrettyDescriptor() |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4693 | << " that has unexpected status " << handle_scope_super->GetStatus() |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4694 | << "\nPending exception:\n" |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 4695 | << (self->GetException() != nullptr ? self->GetException()->Dump() : ""); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4696 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4697 | // Initialization failed because the super-class is erroneous. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4698 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4699 | return false; |
| 4700 | } |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 4701 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4702 | } |
| 4703 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4704 | if (!klass->IsInterface()) { |
| 4705 | // Initialize interfaces with default methods for the JLS. |
| 4706 | size_t num_direct_interfaces = klass->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4707 | // Only setup the (expensive) handle scope if we actually need to. |
| 4708 | if (UNLIKELY(num_direct_interfaces > 0)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4709 | StackHandleScope<1> hs_iface(self); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4710 | MutableHandle<mirror::Class> handle_scope_iface(hs_iface.NewHandle<mirror::Class>(nullptr)); |
| 4711 | for (size_t i = 0; i < num_direct_interfaces; i++) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 4712 | handle_scope_iface.Assign(mirror::Class::GetDirectInterface(self, klass.Get(), i)); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 4713 | CHECK(handle_scope_iface != nullptr) << klass->PrettyDescriptor() << " iface #" << i; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4714 | CHECK(handle_scope_iface->IsInterface()); |
| 4715 | if (handle_scope_iface->HasBeenRecursivelyInitialized()) { |
| 4716 | // We have already done this for this interface. Skip it. |
| 4717 | continue; |
| 4718 | } |
| 4719 | // We cannot just call initialize class directly because we need to ensure that ALL |
| 4720 | // interfaces with default methods are initialized. Non-default interface initialization |
| 4721 | // will not affect other non-default super-interfaces. |
| 4722 | bool iface_initialized = InitializeDefaultInterfaceRecursive(self, |
| 4723 | handle_scope_iface, |
| 4724 | can_init_statics, |
| 4725 | can_init_parents); |
| 4726 | if (!iface_initialized) { |
| 4727 | ObjectLock<mirror::Class> lock(self, klass); |
| 4728 | // Initialization failed because one of our interfaces with default methods is erroneous. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4729 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4730 | return false; |
| 4731 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4732 | } |
| 4733 | } |
| 4734 | } |
| 4735 | |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4736 | const size_t num_static_fields = klass->NumStaticFields(); |
| 4737 | if (num_static_fields > 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4738 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4739 | CHECK(dex_class_def != nullptr); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4740 | const DexFile& dex_file = klass->GetDexFile(); |
Hiroshi Yamauchi | 67ef46a | 2014-08-21 15:59:43 -0700 | [diff] [blame] | 4741 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4742 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4743 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4744 | |
| 4745 | // Eagerly fill in static fields so that the we don't have to do as many expensive |
| 4746 | // Class::FindStaticField in ResolveField. |
| 4747 | for (size_t i = 0; i < num_static_fields; ++i) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4748 | ArtField* field = klass->GetStaticField(i); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4749 | const uint32_t field_idx = field->GetDexFieldIndex(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4750 | ArtField* resolved_field = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4751 | if (resolved_field == nullptr) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4752 | dex_cache->SetResolvedField(field_idx, field, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4753 | } else { |
| 4754 | DCHECK_EQ(field, resolved_field); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4755 | } |
| 4756 | } |
| 4757 | |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 4758 | annotations::RuntimeEncodedStaticFieldValueIterator value_it(dex_file, |
| 4759 | &dex_cache, |
| 4760 | &class_loader, |
| 4761 | this, |
| 4762 | *dex_class_def); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 4763 | const uint8_t* class_data = dex_file.GetClassData(*dex_class_def); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4764 | ClassDataItemIterator field_it(dex_file, class_data); |
| 4765 | if (value_it.HasNext()) { |
| 4766 | DCHECK(field_it.HasNextStaticField()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4767 | CHECK(can_init_statics); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4768 | for ( ; value_it.HasNext(); value_it.Next(), field_it.Next()) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4769 | ArtField* field = ResolveField( |
| 4770 | dex_file, field_it.GetMemberIndex(), dex_cache, class_loader, true); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4771 | if (Runtime::Current()->IsActiveTransaction()) { |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4772 | value_it.ReadValueToField<true>(field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4773 | } else { |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4774 | value_it.ReadValueToField<false>(field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4775 | } |
Mathieu Chartier | da595be | 2016-08-10 13:57:39 -0700 | [diff] [blame] | 4776 | if (self->IsExceptionPending()) { |
| 4777 | break; |
| 4778 | } |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4779 | DCHECK(!value_it.HasNext() || field_it.HasNextStaticField()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4780 | } |
| 4781 | } |
| 4782 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4783 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4784 | |
Mathieu Chartier | da595be | 2016-08-10 13:57:39 -0700 | [diff] [blame] | 4785 | if (!self->IsExceptionPending()) { |
| 4786 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
| 4787 | if (clinit != nullptr) { |
| 4788 | CHECK(can_init_statics); |
| 4789 | JValue result; |
| 4790 | clinit->Invoke(self, nullptr, 0, &result, "V"); |
| 4791 | } |
| 4792 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4793 | self->AllowThreadSuspension(); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 4794 | uint64_t t1 = NanoTime(); |
| 4795 | |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4796 | bool success = true; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4797 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4798 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4799 | |
| 4800 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4801 | WrapExceptionInInitializer(klass); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4802 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4803 | success = false; |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 4804 | } else if (Runtime::Current()->IsTransactionAborted()) { |
| 4805 | // The exception thrown when the transaction aborted has been caught and cleared |
| 4806 | // so we need to throw it again now. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4807 | VLOG(compiler) << "Return from class initializer of " |
| 4808 | << mirror::Class::PrettyDescriptor(klass.Get()) |
Sebastien Hertz | bd9cf9f | 2015-03-03 12:16:13 +0100 | [diff] [blame] | 4809 | << " without exception while transaction was aborted: re-throw it now."; |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 4810 | Runtime::Current()->ThrowTransactionAbortError(self); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4811 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 4812 | success = false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4813 | } else { |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 4814 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 4815 | RuntimeStats* thread_stats = self->GetStats(); |
| 4816 | ++global_stats->class_init_count; |
| 4817 | ++thread_stats->class_init_count; |
| 4818 | global_stats->class_init_time_ns += (t1 - t0); |
| 4819 | thread_stats->class_init_time_ns += (t1 - t0); |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 4820 | // Set the class as initialized except if failed to initialize static fields. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4821 | mirror::Class::SetStatus(klass, mirror::Class::kStatusInitialized, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4822 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4823 | std::string temp; |
| 4824 | LOG(INFO) << "Initialized class " << klass->GetDescriptor(&temp) << " from " << |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4825 | klass->GetLocation(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 4826 | } |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 4827 | // Opportunistically set static method trampolines to their destination. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4828 | FixupStaticTrampolines(klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4829 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4830 | } |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4831 | return success; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4832 | } |
| 4833 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4834 | // We recursively run down the tree of interfaces. We need to do this in the order they are declared |
| 4835 | // and perform the initialization only on those interfaces that contain default methods. |
| 4836 | bool ClassLinker::InitializeDefaultInterfaceRecursive(Thread* self, |
| 4837 | Handle<mirror::Class> iface, |
| 4838 | bool can_init_statics, |
| 4839 | bool can_init_parents) { |
| 4840 | CHECK(iface->IsInterface()); |
| 4841 | size_t num_direct_ifaces = iface->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4842 | // Only create the (expensive) handle scope if we need it. |
| 4843 | if (UNLIKELY(num_direct_ifaces > 0)) { |
| 4844 | StackHandleScope<1> hs(self); |
| 4845 | MutableHandle<mirror::Class> handle_super_iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 4846 | // First we initialize all of iface's super-interfaces recursively. |
| 4847 | for (size_t i = 0; i < num_direct_ifaces; i++) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 4848 | ObjPtr<mirror::Class> super_iface = mirror::Class::GetDirectInterface(self, iface.Get(), i); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 4849 | CHECK(super_iface != nullptr) << iface->PrettyDescriptor() << " iface #" << i; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4850 | if (!super_iface->HasBeenRecursivelyInitialized()) { |
| 4851 | // Recursive step |
| 4852 | handle_super_iface.Assign(super_iface); |
| 4853 | if (!InitializeDefaultInterfaceRecursive(self, |
| 4854 | handle_super_iface, |
| 4855 | can_init_statics, |
| 4856 | can_init_parents)) { |
| 4857 | return false; |
| 4858 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4859 | } |
| 4860 | } |
| 4861 | } |
| 4862 | |
| 4863 | bool result = true; |
| 4864 | // Then we initialize 'iface' if it has default methods. We do not need to (and in fact must not) |
| 4865 | // initialize if we don't have default methods. |
| 4866 | if (iface->HasDefaultMethods()) { |
| 4867 | result = EnsureInitialized(self, iface, can_init_statics, can_init_parents); |
| 4868 | } |
| 4869 | |
| 4870 | // Mark that this interface has undergone recursive default interface initialization so we know we |
| 4871 | // can skip it on any later class initializations. We do this even if we are not a default |
| 4872 | // interface since we can still avoid the traversal. This is purely a performance optimization. |
| 4873 | if (result) { |
| 4874 | // TODO This should be done in a better way |
| 4875 | ObjectLock<mirror::Class> lock(self, iface); |
| 4876 | iface->SetRecursivelyInitialized(); |
| 4877 | } |
| 4878 | return result; |
| 4879 | } |
| 4880 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4881 | bool ClassLinker::WaitForInitializeClass(Handle<mirror::Class> klass, |
| 4882 | Thread* self, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4883 | ObjectLock<mirror::Class>& lock) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4884 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4885 | while (true) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4886 | self->AssertNoPendingException(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4887 | CHECK(!klass->IsInitialized()); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4888 | lock.WaitIgnoringInterrupts(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4889 | |
| 4890 | // When we wake up, repeat the test for init-in-progress. If |
| 4891 | // there's an exception pending (only possible if |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4892 | // we were not using WaitIgnoringInterrupts), bail out. |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4893 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4894 | WrapExceptionInInitializer(klass); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4895 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4896 | return false; |
| 4897 | } |
| 4898 | // Spurious wakeup? Go back to waiting. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4899 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4900 | continue; |
| 4901 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4902 | if (klass->GetStatus() == mirror::Class::kStatusVerified && |
| 4903 | Runtime::Current()->IsAotCompiler()) { |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 4904 | // Compile time initialization failed. |
| 4905 | return false; |
| 4906 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4907 | if (klass->IsErroneous()) { |
| 4908 | // The caller wants an exception, but it was thrown in a |
| 4909 | // different thread. Synthesize one here. |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 4910 | ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4911 | klass->PrettyDescriptor().c_str()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4912 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4913 | return false; |
| 4914 | } |
| 4915 | if (klass->IsInitialized()) { |
| 4916 | return true; |
| 4917 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4918 | LOG(FATAL) << "Unexpected class status. " << klass->PrettyClass() << " is " |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4919 | << klass->GetStatus(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4920 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 4921 | UNREACHABLE(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4922 | } |
| 4923 | |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4924 | static void ThrowSignatureCheckResolveReturnTypeException(Handle<mirror::Class> klass, |
| 4925 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4926 | ArtMethod* method, |
| 4927 | ArtMethod* m) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4928 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4929 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4930 | DCHECK(!m->IsProxyMethod()); |
| 4931 | const DexFile* dex_file = m->GetDexFile(); |
| 4932 | const DexFile::MethodId& method_id = dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 4933 | const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4934 | dex::TypeIndex return_type_idx = proto_id.return_type_idx_; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4935 | std::string return_type = dex_file->PrettyType(return_type_idx); |
| 4936 | std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4937 | ThrowWrappedLinkageError(klass.Get(), |
| 4938 | "While checking class %s method %s signature against %s %s: " |
| 4939 | "Failed to resolve return type %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4940 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 4941 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4942 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4943 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4944 | return_type.c_str(), class_loader.c_str()); |
| 4945 | } |
| 4946 | |
| 4947 | static void ThrowSignatureCheckResolveArgException(Handle<mirror::Class> klass, |
| 4948 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4949 | ArtMethod* method, |
| 4950 | ArtMethod* m, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4951 | uint32_t index, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4952 | dex::TypeIndex arg_type_idx) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4953 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4954 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4955 | DCHECK(!m->IsProxyMethod()); |
| 4956 | const DexFile* dex_file = m->GetDexFile(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4957 | std::string arg_type = dex_file->PrettyType(arg_type_idx); |
| 4958 | std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4959 | ThrowWrappedLinkageError(klass.Get(), |
| 4960 | "While checking class %s method %s signature against %s %s: " |
| 4961 | "Failed to resolve arg %u type %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4962 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 4963 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4964 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4965 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4966 | index, arg_type.c_str(), class_loader.c_str()); |
| 4967 | } |
| 4968 | |
| 4969 | static void ThrowSignatureMismatch(Handle<mirror::Class> klass, |
| 4970 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4971 | ArtMethod* method, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4972 | const std::string& error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4973 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4974 | ThrowLinkageError(klass.Get(), |
| 4975 | "Class %s method %s resolves differently in %s %s: %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4976 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 4977 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4978 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4979 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4980 | error_msg.c_str()); |
| 4981 | } |
| 4982 | |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4983 | static bool HasSameSignatureWithDifferentClassLoaders(Thread* self, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4984 | Handle<mirror::Class> klass, |
| 4985 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4986 | ArtMethod* method1, |
| 4987 | ArtMethod* method2) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4988 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4989 | { |
| 4990 | StackHandleScope<1> hs(self); |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 4991 | Handle<mirror::Class> return_type(hs.NewHandle(method1->ResolveReturnType())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4992 | if (UNLIKELY(return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4993 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method1); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4994 | return false; |
| 4995 | } |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 4996 | ObjPtr<mirror::Class> other_return_type = method2->ResolveReturnType(); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4997 | if (UNLIKELY(other_return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4998 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method2); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4999 | return false; |
| 5000 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5001 | if (UNLIKELY(other_return_type != return_type.Get())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5002 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5003 | StringPrintf("Return types mismatch: %s(%p) vs %s(%p)", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5004 | return_type->PrettyClassAndClassLoader().c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5005 | return_type.Get(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5006 | other_return_type->PrettyClassAndClassLoader().c_str(), |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5007 | other_return_type.Ptr())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5008 | return false; |
| 5009 | } |
| 5010 | } |
| 5011 | const DexFile::TypeList* types1 = method1->GetParameterTypeList(); |
| 5012 | const DexFile::TypeList* types2 = method2->GetParameterTypeList(); |
| 5013 | if (types1 == nullptr) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5014 | if (types2 != nullptr && types2->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5015 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5016 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5017 | method2->PrettyMethod(true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5018 | return false; |
| 5019 | } |
| 5020 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5021 | } else if (UNLIKELY(types2 == nullptr)) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5022 | if (types1->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5023 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5024 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5025 | method2->PrettyMethod(true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5026 | return false; |
| 5027 | } |
| 5028 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5029 | } |
| 5030 | uint32_t num_types = types1->Size(); |
| 5031 | if (UNLIKELY(num_types != types2->Size())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5032 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5033 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5034 | method2->PrettyMethod(true).c_str())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5035 | return false; |
| 5036 | } |
| 5037 | for (uint32_t i = 0; i < num_types; ++i) { |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5038 | StackHandleScope<1> hs(self); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5039 | dex::TypeIndex param_type_idx = types1->GetTypeItem(i).type_idx_; |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5040 | Handle<mirror::Class> param_type(hs.NewHandle( |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5041 | method1->ResolveClassFromTypeIndex(param_type_idx))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5042 | if (UNLIKELY(param_type == nullptr)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5043 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5044 | method1, i, param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5045 | return false; |
| 5046 | } |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5047 | dex::TypeIndex other_param_type_idx = types2->GetTypeItem(i).type_idx_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5048 | ObjPtr<mirror::Class> other_param_type = |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5049 | method2->ResolveClassFromTypeIndex(other_param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5050 | if (UNLIKELY(other_param_type == nullptr)) { |
| 5051 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5052 | method2, i, other_param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5053 | return false; |
| 5054 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5055 | if (UNLIKELY(param_type.Get() != other_param_type)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5056 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5057 | StringPrintf("Parameter %u type mismatch: %s(%p) vs %s(%p)", |
| 5058 | i, |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5059 | param_type->PrettyClassAndClassLoader().c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5060 | param_type.Get(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5061 | other_param_type->PrettyClassAndClassLoader().c_str(), |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5062 | other_param_type.Ptr())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5063 | return false; |
| 5064 | } |
| 5065 | } |
| 5066 | return true; |
| 5067 | } |
| 5068 | |
| 5069 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5070 | bool ClassLinker::ValidateSuperClassDescriptors(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5071 | if (klass->IsInterface()) { |
| 5072 | return true; |
| 5073 | } |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 5074 | // Begin with the methods local to the superclass. |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 5075 | Thread* self = Thread::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5076 | StackHandleScope<1> hs(self); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5077 | MutableHandle<mirror::Class> super_klass(hs.NewHandle<mirror::Class>(nullptr)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5078 | if (klass->HasSuperClass() && |
| 5079 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5080 | super_klass.Assign(klass->GetSuperClass()); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5081 | for (int i = klass->GetSuperClass()->GetVTableLength() - 1; i >= 0; --i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5082 | auto* m = klass->GetVTableEntry(i, image_pointer_size_); |
| 5083 | auto* super_m = klass->GetSuperClass()->GetVTableEntry(i, image_pointer_size_); |
| 5084 | if (m != super_m) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5085 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, |
| 5086 | klass, |
| 5087 | super_klass, |
| 5088 | m, |
| 5089 | super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5090 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5091 | return false; |
| 5092 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5093 | } |
| 5094 | } |
| 5095 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 5096 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5097 | super_klass.Assign(klass->GetIfTable()->GetInterface(i)); |
| 5098 | if (klass->GetClassLoader() != super_klass->GetClassLoader()) { |
| 5099 | uint32_t num_methods = super_klass->NumVirtualMethods(); |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 5100 | for (uint32_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5101 | auto* m = klass->GetIfTable()->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>( |
| 5102 | j, image_pointer_size_); |
| 5103 | auto* super_m = super_klass->GetVirtualMethod(j, image_pointer_size_); |
| 5104 | if (m != super_m) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5105 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, |
| 5106 | klass, |
| 5107 | super_klass, |
| 5108 | m, |
| 5109 | super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5110 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5111 | return false; |
| 5112 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5113 | } |
| 5114 | } |
| 5115 | } |
| 5116 | } |
| 5117 | return true; |
| 5118 | } |
| 5119 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5120 | bool ClassLinker::EnsureInitialized(Thread* self, |
| 5121 | Handle<mirror::Class> c, |
| 5122 | bool can_init_fields, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5123 | bool can_init_parents) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5124 | DCHECK(c != nullptr); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5125 | if (c->IsInitialized()) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 5126 | EnsureSkipAccessChecksMethods(c, image_pointer_size_); |
Mathieu Chartier | 8502f72 | 2016-06-08 15:09:08 -0700 | [diff] [blame] | 5127 | self->AssertNoPendingException(); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5128 | return true; |
| 5129 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5130 | const bool success = InitializeClass(self, c, can_init_fields, can_init_parents); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5131 | if (!success) { |
| 5132 | if (can_init_fields && can_init_parents) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5133 | CHECK(self->IsExceptionPending()) << c->PrettyClass(); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5134 | } |
| 5135 | } else { |
| 5136 | self->AssertNoPendingException(); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 5137 | } |
| 5138 | return success; |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 5139 | } |
| 5140 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5141 | void ClassLinker::FixupTemporaryDeclaringClass(ObjPtr<mirror::Class> temp_class, |
| 5142 | ObjPtr<mirror::Class> new_class) { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5143 | DCHECK_EQ(temp_class->NumInstanceFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5144 | for (ArtField& field : new_class->GetIFields()) { |
| 5145 | if (field.GetDeclaringClass() == temp_class) { |
| 5146 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5147 | } |
| 5148 | } |
| 5149 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5150 | DCHECK_EQ(temp_class->NumStaticFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5151 | for (ArtField& field : new_class->GetSFields()) { |
| 5152 | if (field.GetDeclaringClass() == temp_class) { |
| 5153 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5154 | } |
| 5155 | } |
| 5156 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5157 | DCHECK_EQ(temp_class->NumDirectMethods(), 0u); |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5158 | DCHECK_EQ(temp_class->NumVirtualMethods(), 0u); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5159 | for (auto& method : new_class->GetMethods(image_pointer_size_)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5160 | if (method.GetDeclaringClass() == temp_class) { |
| 5161 | method.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5162 | } |
| 5163 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5164 | |
| 5165 | // Make sure the remembered set and mod-union tables know that we updated some of the native |
| 5166 | // roots. |
| 5167 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5168 | } |
| 5169 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5170 | void ClassLinker::RegisterClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5171 | CHECK(class_loader->GetAllocator() == nullptr); |
| 5172 | CHECK(class_loader->GetClassTable() == nullptr); |
| 5173 | Thread* const self = Thread::Current(); |
| 5174 | ClassLoaderData data; |
| 5175 | data.weak_root = self->GetJniEnv()->vm->AddWeakGlobalRef(self, class_loader); |
| 5176 | // Create and set the class table. |
| 5177 | data.class_table = new ClassTable; |
| 5178 | class_loader->SetClassTable(data.class_table); |
| 5179 | // Create and set the linear allocator. |
| 5180 | data.allocator = Runtime::Current()->CreateLinearAlloc(); |
| 5181 | class_loader->SetAllocator(data.allocator); |
| 5182 | // Add to the list so that we know to free the data later. |
| 5183 | class_loaders_.push_back(data); |
| 5184 | } |
| 5185 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5186 | ClassTable* ClassLinker::InsertClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5187 | if (class_loader == nullptr) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 5188 | return boot_class_table_.get(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5189 | } |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5190 | ClassTable* class_table = class_loader->GetClassTable(); |
| 5191 | if (class_table == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5192 | RegisterClassLoader(class_loader); |
| 5193 | class_table = class_loader->GetClassTable(); |
| 5194 | DCHECK(class_table != nullptr); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5195 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5196 | return class_table; |
| 5197 | } |
| 5198 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5199 | ClassTable* ClassLinker::ClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 5200 | return class_loader == nullptr ? boot_class_table_.get() : class_loader->GetClassTable(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5201 | } |
| 5202 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5203 | static ImTable* FindSuperImt(ObjPtr<mirror::Class> klass, PointerSize pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5204 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5205 | while (klass->HasSuperClass()) { |
| 5206 | klass = klass->GetSuperClass(); |
| 5207 | if (klass->ShouldHaveImt()) { |
| 5208 | return klass->GetImt(pointer_size); |
| 5209 | } |
| 5210 | } |
| 5211 | return nullptr; |
| 5212 | } |
| 5213 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5214 | bool ClassLinker::LinkClass(Thread* self, |
| 5215 | const char* descriptor, |
| 5216 | Handle<mirror::Class> klass, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5217 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5218 | MutableHandle<mirror::Class>* h_new_class_out) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5219 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5220 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5221 | if (!LinkSuperClass(klass)) { |
| 5222 | return false; |
| 5223 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5224 | ArtMethod* imt_data[ImTable::kSize]; |
| 5225 | // If there are any new conflicts compared to super class. |
| 5226 | bool new_conflict = false; |
Nicolas Geoffray | 918dcea | 2017-07-21 07:58:14 +0000 | [diff] [blame] | 5227 | std::fill_n(imt_data, arraysize(imt_data), Runtime::Current()->GetImtUnimplementedMethod()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5228 | if (!LinkMethods(self, klass, interfaces, &new_conflict, imt_data)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5229 | return false; |
| 5230 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5231 | if (!LinkInstanceFields(self, klass)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5232 | return false; |
| 5233 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5234 | size_t class_size; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5235 | if (!LinkStaticFields(self, klass, &class_size)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5236 | return false; |
| 5237 | } |
| 5238 | CreateReferenceInstanceOffsets(klass); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5239 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5240 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5241 | ImTable* imt = nullptr; |
| 5242 | if (klass->ShouldHaveImt()) { |
| 5243 | // If there are any new conflicts compared to the super class we can not make a copy. There |
| 5244 | // can be cases where both will have a conflict method at the same slot without having the same |
| 5245 | // set of conflicts. In this case, we can not share the IMT since the conflict table slow path |
| 5246 | // will possibly create a table that is incorrect for either of the classes. |
| 5247 | // Same IMT with new_conflict does not happen very often. |
| 5248 | if (!new_conflict) { |
| 5249 | ImTable* super_imt = FindSuperImt(klass.Get(), image_pointer_size_); |
| 5250 | if (super_imt != nullptr) { |
| 5251 | bool imt_equals = true; |
| 5252 | for (size_t i = 0; i < ImTable::kSize && imt_equals; ++i) { |
| 5253 | imt_equals = imt_equals && (super_imt->Get(i, image_pointer_size_) == imt_data[i]); |
| 5254 | } |
| 5255 | if (imt_equals) { |
| 5256 | imt = super_imt; |
| 5257 | } |
| 5258 | } |
| 5259 | } |
| 5260 | if (imt == nullptr) { |
| 5261 | LinearAlloc* allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
| 5262 | imt = reinterpret_cast<ImTable*>( |
| 5263 | allocator->Alloc(self, ImTable::SizeInBytes(image_pointer_size_))); |
| 5264 | if (imt == nullptr) { |
| 5265 | return false; |
| 5266 | } |
| 5267 | imt->Populate(imt_data, image_pointer_size_); |
| 5268 | } |
| 5269 | } |
| 5270 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5271 | if (!klass->IsTemp() || (!init_done_ && klass->GetClassSize() == class_size)) { |
| 5272 | // We don't need to retire this class as it has no embedded tables or it was created the |
| 5273 | // correct size during class linker initialization. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5274 | CHECK_EQ(klass->GetClassSize(), class_size) << klass->PrettyDescriptor(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5275 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5276 | if (klass->ShouldHaveEmbeddedVTable()) { |
| 5277 | klass->PopulateEmbeddedVTable(image_pointer_size_); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5278 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5279 | if (klass->ShouldHaveImt()) { |
| 5280 | klass->SetImt(imt, image_pointer_size_); |
| 5281 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 5282 | |
| 5283 | // Update CHA info based on whether we override methods. |
| 5284 | // Have to do this before setting the class as resolved which allows |
| 5285 | // instantiation of klass. |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 5286 | if (cha_ != nullptr) { |
| 5287 | cha_->UpdateAfterLoadingOf(klass); |
| 5288 | } |
Nicolas Geoffray | 918dcea | 2017-07-21 07:58:14 +0000 | [diff] [blame] | 5289 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5290 | // This will notify waiters on klass that saw the not yet resolved |
| 5291 | // class in the class_table_ during EnsureResolved. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 5292 | mirror::Class::SetStatus(klass, mirror::Class::kStatusResolved, self); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5293 | h_new_class_out->Assign(klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5294 | } else { |
| 5295 | CHECK(!klass->IsResolved()); |
| 5296 | // Retire the temporary class and create the correctly sized resolved class. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5297 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5298 | 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] | 5299 | // Set arrays to null since we don't want to have multiple classes with the same ArtField or |
| 5300 | // ArtMethod array pointers. If this occurs, it causes bugs in remembered sets since the GC |
| 5301 | // may not see any references to the target space and clean the card for a class if another |
| 5302 | // class had the same array pointer. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5303 | klass->SetMethodsPtrUnchecked(nullptr, 0, 0); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5304 | klass->SetSFieldsPtrUnchecked(nullptr); |
| 5305 | klass->SetIFieldsPtrUnchecked(nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5306 | if (UNLIKELY(h_new_class == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5307 | self->AssertPendingOOMException(); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 5308 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorUnresolved, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5309 | return false; |
| 5310 | } |
| 5311 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5312 | CHECK_EQ(h_new_class->GetClassSize(), class_size); |
| 5313 | ObjectLock<mirror::Class> lock(self, h_new_class); |
| 5314 | FixupTemporaryDeclaringClass(klass.Get(), h_new_class.Get()); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5315 | |
| 5316 | { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5317 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5318 | ObjPtr<mirror::ClassLoader> const class_loader = h_new_class.Get()->GetClassLoader(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5319 | ClassTable* const table = InsertClassTableForClassLoader(class_loader); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5320 | ObjPtr<mirror::Class> existing = table->UpdateClass(descriptor, h_new_class.Get(), |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5321 | ComputeModifiedUtf8Hash(descriptor)); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 5322 | if (class_loader != nullptr) { |
| 5323 | // We updated the class in the class table, perform the write barrier so that the GC knows |
| 5324 | // about the change. |
| 5325 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader); |
| 5326 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5327 | CHECK_EQ(existing, klass.Get()); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 5328 | if (log_new_roots_) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5329 | new_class_roots_.push_back(GcRoot<mirror::Class>(h_new_class.Get())); |
| 5330 | } |
| 5331 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5332 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 5333 | // Update CHA info based on whether we override methods. |
| 5334 | // Have to do this before setting the class as resolved which allows |
| 5335 | // instantiation of klass. |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 5336 | if (cha_ != nullptr) { |
| 5337 | cha_->UpdateAfterLoadingOf(h_new_class); |
| 5338 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 5339 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5340 | // This will notify waiters on temp class that saw the not yet resolved class in the |
| 5341 | // class_table_ during EnsureResolved. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 5342 | mirror::Class::SetStatus(klass, mirror::Class::kStatusRetired, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5343 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5344 | CHECK_EQ(h_new_class->GetStatus(), mirror::Class::kStatusResolving); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5345 | // This will notify waiters on new_class that saw the not yet resolved |
| 5346 | // class in the class_table_ during EnsureResolved. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5347 | mirror::Class::SetStatus(h_new_class, mirror::Class::kStatusResolved, self); |
| 5348 | // Return the new class. |
| 5349 | h_new_class_out->Assign(h_new_class.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5350 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5351 | return true; |
| 5352 | } |
| 5353 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5354 | bool ClassLinker::LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5355 | CHECK_EQ(mirror::Class::kStatusIdx, klass->GetStatus()); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 5356 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5357 | dex::TypeIndex super_class_idx = class_def.superclass_idx_; |
| 5358 | if (super_class_idx.IsValid()) { |
Roland Levillain | 90328ac | 2016-05-18 12:25:38 +0100 | [diff] [blame] | 5359 | // Check that a class does not inherit from itself directly. |
| 5360 | // |
| 5361 | // TODO: This is a cheap check to detect the straightforward case |
| 5362 | // of a class extending itself (b/28685551), but we should do a |
| 5363 | // proper cycle detection on loaded classes, to detect all cases |
| 5364 | // of class circularity errors (b/28830038). |
| 5365 | if (super_class_idx == class_def.class_idx_) { |
| 5366 | ThrowClassCircularityError(klass.Get(), |
| 5367 | "Class %s extends itself", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5368 | klass->PrettyDescriptor().c_str()); |
Roland Levillain | 90328ac | 2016-05-18 12:25:38 +0100 | [diff] [blame] | 5369 | return false; |
| 5370 | } |
| 5371 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5372 | 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] | 5373 | if (super_class == nullptr) { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 5374 | DCHECK(Thread::Current()->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5375 | return false; |
| 5376 | } |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 5377 | // Verify |
| 5378 | if (!klass->CanAccess(super_class)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5379 | ThrowIllegalAccessError(klass.Get(), "Class %s extended by class %s is inaccessible", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5380 | super_class->PrettyDescriptor().c_str(), |
| 5381 | klass->PrettyDescriptor().c_str()); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 5382 | return false; |
| 5383 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5384 | CHECK(super_class->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5385 | klass->SetSuperClass(super_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5386 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 5387 | const DexFile::TypeList* interfaces = dex_file.GetInterfacesList(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5388 | if (interfaces != nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5389 | for (size_t i = 0; i < interfaces->Size(); i++) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5390 | dex::TypeIndex idx = interfaces->GetTypeItem(i).type_idx_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5391 | ObjPtr<mirror::Class> interface = ResolveType(dex_file, idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5392 | if (interface == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5393 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5394 | return false; |
| 5395 | } |
| 5396 | // Verify |
| 5397 | if (!klass->CanAccess(interface)) { |
| 5398 | // TODO: the RI seemed to ignore this in my testing. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5399 | ThrowIllegalAccessError(klass.Get(), |
| 5400 | "Interface %s implemented by class %s is inaccessible", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5401 | interface->PrettyDescriptor().c_str(), |
| 5402 | klass->PrettyDescriptor().c_str()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5403 | return false; |
| 5404 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5405 | } |
| 5406 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 5407 | // Mark the class as loaded. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 5408 | mirror::Class::SetStatus(klass, mirror::Class::kStatusLoaded, nullptr); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5409 | return true; |
| 5410 | } |
| 5411 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5412 | bool ClassLinker::LinkSuperClass(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5413 | CHECK(!klass->IsPrimitive()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5414 | ObjPtr<mirror::Class> super = klass->GetSuperClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5415 | if (klass.Get() == GetClassRoot(kJavaLangObject)) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5416 | if (super != nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5417 | ThrowClassFormatError(klass.Get(), "java.lang.Object must not have a superclass"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5418 | return false; |
| 5419 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5420 | return true; |
| 5421 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5422 | if (super == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5423 | ThrowLinkageError(klass.Get(), "No superclass defined for class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5424 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5425 | return false; |
| 5426 | } |
| 5427 | // Verify |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 5428 | if (super->IsFinal()) { |
| 5429 | ThrowVerifyError(klass.Get(), |
| 5430 | "Superclass %s of %s is declared final", |
| 5431 | super->PrettyDescriptor().c_str(), |
| 5432 | klass->PrettyDescriptor().c_str()); |
| 5433 | return false; |
| 5434 | } |
| 5435 | if (super->IsInterface()) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5436 | ThrowIncompatibleClassChangeError(klass.Get(), |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 5437 | "Superclass %s of %s is an interface", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5438 | super->PrettyDescriptor().c_str(), |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 5439 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5440 | return false; |
| 5441 | } |
| 5442 | if (!klass->CanAccess(super)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5443 | ThrowIllegalAccessError(klass.Get(), "Superclass %s is inaccessible to class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5444 | super->PrettyDescriptor().c_str(), |
| 5445 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5446 | return false; |
| 5447 | } |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 5448 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5449 | // Inherit kAccClassIsFinalizable from the superclass in case this |
| 5450 | // class doesn't override finalize. |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 5451 | if (super->IsFinalizable()) { |
| 5452 | klass->SetFinalizable(); |
| 5453 | } |
| 5454 | |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 5455 | // Inherit class loader flag form super class. |
| 5456 | if (super->IsClassLoaderClass()) { |
| 5457 | klass->SetClassLoaderClass(); |
| 5458 | } |
| 5459 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5460 | // Inherit reference flags (if any) from the superclass. |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 5461 | uint32_t reference_flags = (super->GetClassFlags() & mirror::kClassFlagReference); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5462 | if (reference_flags != 0) { |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 5463 | CHECK_EQ(klass->GetClassFlags(), 0u); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 5464 | klass->SetClassFlags(klass->GetClassFlags() | reference_flags); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5465 | } |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 5466 | // Disallow custom direct subclasses of java.lang.ref.Reference. |
Elliott Hughes | bf61ba3 | 2011-10-11 10:53:09 -0700 | [diff] [blame] | 5467 | if (init_done_ && super == GetClassRoot(kJavaLangRefReference)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5468 | ThrowLinkageError(klass.Get(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5469 | "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] | 5470 | klass->PrettyDescriptor().c_str()); |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 5471 | return false; |
| 5472 | } |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5473 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5474 | if (kIsDebugBuild) { |
| 5475 | // Ensure super classes are fully resolved prior to resolving fields.. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5476 | while (super != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5477 | CHECK(super->IsResolved()); |
| 5478 | super = super->GetSuperClass(); |
| 5479 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5480 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5481 | return true; |
| 5482 | } |
| 5483 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5484 | // Populate the class vtable and itable. Compute return type indices. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5485 | bool ClassLinker::LinkMethods(Thread* self, |
| 5486 | Handle<mirror::Class> klass, |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5487 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5488 | bool* out_new_conflict, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5489 | ArtMethod** out_imt) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5490 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5491 | // A map from vtable indexes to the method they need to be updated to point to. Used because we |
| 5492 | // need to have default methods be in the virtuals array of each class but we don't set that up |
| 5493 | // until LinkInterfaceMethods. |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5494 | std::unordered_map<size_t, ClassLinker::MethodTranslation> default_translations; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5495 | // Link virtual methods then interface methods. |
| 5496 | // We set up the interface lookup table first because we need it to determine if we need to update |
| 5497 | // any vtable entries with new default method implementations. |
| 5498 | return SetupInterfaceLookupTable(self, klass, interfaces) |
| 5499 | && LinkVirtualMethods(self, klass, /*out*/ &default_translations) |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5500 | && LinkInterfaceMethods(self, klass, default_translations, out_new_conflict, out_imt); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5501 | } |
| 5502 | |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5503 | // Comparator for name and signature of a method, used in finding overriding methods. Implementation |
| 5504 | // avoids the use of handles, if it didn't then rather than compare dex files we could compare dex |
| 5505 | // caches in the implementation below. |
| 5506 | class MethodNameAndSignatureComparator FINAL : public ValueObject { |
| 5507 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5508 | explicit MethodNameAndSignatureComparator(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5509 | REQUIRES_SHARED(Locks::mutator_lock_) : |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5510 | dex_file_(method->GetDexFile()), mid_(&dex_file_->GetMethodId(method->GetDexMethodIndex())), |
| 5511 | name_(nullptr), name_len_(0) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5512 | DCHECK(!method->IsProxyMethod()) << method->PrettyMethod(); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5513 | } |
| 5514 | |
| 5515 | const char* GetName() { |
| 5516 | if (name_ == nullptr) { |
| 5517 | name_ = dex_file_->StringDataAndUtf16LengthByIdx(mid_->name_idx_, &name_len_); |
| 5518 | } |
| 5519 | return name_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5520 | } |
| 5521 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5522 | bool HasSameNameAndSignature(ArtMethod* other) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5523 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5524 | DCHECK(!other->IsProxyMethod()) << other->PrettyMethod(); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5525 | const DexFile* other_dex_file = other->GetDexFile(); |
| 5526 | const DexFile::MethodId& other_mid = other_dex_file->GetMethodId(other->GetDexMethodIndex()); |
| 5527 | if (dex_file_ == other_dex_file) { |
| 5528 | return mid_->name_idx_ == other_mid.name_idx_ && mid_->proto_idx_ == other_mid.proto_idx_; |
| 5529 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5530 | GetName(); // Only used to make sure its calculated. |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5531 | uint32_t other_name_len; |
| 5532 | const char* other_name = other_dex_file->StringDataAndUtf16LengthByIdx(other_mid.name_idx_, |
| 5533 | &other_name_len); |
| 5534 | if (name_len_ != other_name_len || strcmp(name_, other_name) != 0) { |
| 5535 | return false; |
| 5536 | } |
| 5537 | return dex_file_->GetMethodSignature(*mid_) == other_dex_file->GetMethodSignature(other_mid); |
| 5538 | } |
| 5539 | |
| 5540 | private: |
| 5541 | // Dex file for the method to compare against. |
| 5542 | const DexFile* const dex_file_; |
| 5543 | // MethodId for the method to compare against. |
| 5544 | const DexFile::MethodId* const mid_; |
| 5545 | // Lazily computed name from the dex file's strings. |
| 5546 | const char* name_; |
| 5547 | // Lazily computed name length. |
| 5548 | uint32_t name_len_; |
| 5549 | }; |
| 5550 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5551 | class LinkVirtualHashTable { |
| 5552 | public: |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5553 | LinkVirtualHashTable(Handle<mirror::Class> klass, |
| 5554 | size_t hash_size, |
| 5555 | uint32_t* hash_table, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 5556 | PointerSize image_pointer_size) |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5557 | : klass_(klass), |
| 5558 | hash_size_(hash_size), |
| 5559 | hash_table_(hash_table), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5560 | image_pointer_size_(image_pointer_size) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5561 | std::fill(hash_table_, hash_table_ + hash_size_, invalid_index_); |
| 5562 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5563 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5564 | void Add(uint32_t virtual_method_index) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5565 | ArtMethod* local_method = klass_->GetVirtualMethodDuringLinking( |
| 5566 | virtual_method_index, image_pointer_size_); |
| 5567 | const char* name = local_method->GetInterfaceMethodIfProxy(image_pointer_size_)->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 5568 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5569 | uint32_t index = hash % hash_size_; |
| 5570 | // Linear probe until we have an empty slot. |
| 5571 | while (hash_table_[index] != invalid_index_) { |
| 5572 | if (++index == hash_size_) { |
| 5573 | index = 0; |
| 5574 | } |
| 5575 | } |
| 5576 | hash_table_[index] = virtual_method_index; |
| 5577 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5578 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5579 | uint32_t FindAndRemove(MethodNameAndSignatureComparator* comparator) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5580 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5581 | const char* name = comparator->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 5582 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5583 | size_t index = hash % hash_size_; |
| 5584 | while (true) { |
| 5585 | const uint32_t value = hash_table_[index]; |
| 5586 | // Since linear probe makes continuous blocks, hitting an invalid index means we are done |
| 5587 | // the block and can safely assume not found. |
| 5588 | if (value == invalid_index_) { |
| 5589 | break; |
| 5590 | } |
| 5591 | if (value != removed_index_) { // This signifies not already overriden. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5592 | ArtMethod* virtual_method = |
| 5593 | klass_->GetVirtualMethodDuringLinking(value, image_pointer_size_); |
| 5594 | if (comparator->HasSameNameAndSignature( |
| 5595 | virtual_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5596 | hash_table_[index] = removed_index_; |
| 5597 | return value; |
| 5598 | } |
| 5599 | } |
| 5600 | if (++index == hash_size_) { |
| 5601 | index = 0; |
| 5602 | } |
| 5603 | } |
| 5604 | return GetNotFoundIndex(); |
| 5605 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5606 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5607 | static uint32_t GetNotFoundIndex() { |
| 5608 | return invalid_index_; |
| 5609 | } |
| 5610 | |
| 5611 | private: |
| 5612 | static const uint32_t invalid_index_; |
| 5613 | static const uint32_t removed_index_; |
| 5614 | |
| 5615 | Handle<mirror::Class> klass_; |
| 5616 | const size_t hash_size_; |
| 5617 | uint32_t* const hash_table_; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 5618 | const PointerSize image_pointer_size_; |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5619 | }; |
| 5620 | |
| 5621 | const uint32_t LinkVirtualHashTable::invalid_index_ = std::numeric_limits<uint32_t>::max(); |
| 5622 | const uint32_t LinkVirtualHashTable::removed_index_ = std::numeric_limits<uint32_t>::max() - 1; |
| 5623 | |
Stephen Hines | 1ddd913 | 2017-02-08 01:51:18 -0800 | [diff] [blame] | 5624 | bool ClassLinker::LinkVirtualMethods( |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5625 | Thread* self, |
| 5626 | Handle<mirror::Class> klass, |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5627 | /*out*/std::unordered_map<size_t, ClassLinker::MethodTranslation>* default_translations) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5628 | const size_t num_virtual_methods = klass->NumVirtualMethods(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5629 | if (klass->IsInterface()) { |
| 5630 | // No vtable. |
| 5631 | if (!IsUint<16>(num_virtual_methods)) { |
| 5632 | ThrowClassFormatError(klass.Get(), "Too many methods on interface: %zu", num_virtual_methods); |
| 5633 | return false; |
| 5634 | } |
| 5635 | bool has_defaults = false; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5636 | // Assign each method an IMT index and set the default flag. |
| 5637 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
| 5638 | ArtMethod* m = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
| 5639 | m->SetMethodIndex(i); |
| 5640 | if (!m->IsAbstract()) { |
| 5641 | m->SetAccessFlags(m->GetAccessFlags() | kAccDefault); |
| 5642 | has_defaults = true; |
| 5643 | } |
| 5644 | } |
| 5645 | // Mark that we have default methods so that we won't need to scan the virtual_methods_ array |
| 5646 | // during initialization. This is a performance optimization. We could simply traverse the |
| 5647 | // virtual_methods_ array again during initialization. |
| 5648 | if (has_defaults) { |
| 5649 | klass->SetHasDefaultMethods(); |
| 5650 | } |
| 5651 | return true; |
| 5652 | } else if (klass->HasSuperClass()) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5653 | const size_t super_vtable_length = klass->GetSuperClass()->GetVTableLength(); |
| 5654 | const size_t max_count = num_virtual_methods + super_vtable_length; |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5655 | StackHandleScope<2> hs(self); |
Mingyao Yang | 38eecb0 | 2014-08-13 14:51:03 -0700 | [diff] [blame] | 5656 | Handle<mirror::Class> super_class(hs.NewHandle(klass->GetSuperClass())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5657 | MutableHandle<mirror::PointerArray> vtable; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5658 | if (super_class->ShouldHaveEmbeddedVTable()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5659 | vtable = hs.NewHandle(AllocPointerArray(self, max_count)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5660 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5661 | self->AssertPendingOOMException(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5662 | return false; |
| 5663 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5664 | for (size_t i = 0; i < super_vtable_length; i++) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5665 | vtable->SetElementPtrSize( |
| 5666 | i, super_class->GetEmbeddedVTableEntry(i, image_pointer_size_), image_pointer_size_); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5667 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5668 | // We might need to change vtable if we have new virtual methods or new interfaces (since that |
| 5669 | // might give us new default methods). If no new interfaces then we can skip the rest since |
| 5670 | // the class cannot override any of the super-class's methods. This is required for |
| 5671 | // correctness since without it we might not update overridden default method vtable entries |
| 5672 | // correctly. |
| 5673 | if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5674 | klass->SetVTable(vtable.Get()); |
| 5675 | return true; |
| 5676 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5677 | } else { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5678 | DCHECK(super_class->IsAbstract() && !super_class->IsArrayClass()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5679 | auto* super_vtable = super_class->GetVTable(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5680 | CHECK(super_vtable != nullptr) << super_class->PrettyClass(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5681 | // We might need to change vtable if we have new virtual methods or new interfaces (since that |
| 5682 | // might give us new default methods). See comment above. |
| 5683 | if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5684 | klass->SetVTable(super_vtable); |
| 5685 | return true; |
| 5686 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5687 | vtable = hs.NewHandle(down_cast<mirror::PointerArray*>( |
| 5688 | super_vtable->CopyOf(self, max_count))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5689 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5690 | self->AssertPendingOOMException(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5691 | return false; |
| 5692 | } |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5693 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5694 | // How the algorithm works: |
| 5695 | // 1. Populate hash table by adding num_virtual_methods from klass. The values in the hash |
| 5696 | // table are: invalid_index for unused slots, index super_vtable_length + i for a virtual |
| 5697 | // method which has not been matched to a vtable method, and j if the virtual method at the |
| 5698 | // index overrode the super virtual method at index j. |
| 5699 | // 2. Loop through super virtual methods, if they overwrite, update hash table to j |
| 5700 | // (j < super_vtable_length) to avoid redundant checks. (TODO maybe use this info for reducing |
| 5701 | // the need for the initial vtable which we later shrink back down). |
| 5702 | // 3. Add non overridden methods to the end of the vtable. |
| 5703 | static constexpr size_t kMaxStackHash = 250; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5704 | // + 1 so that even if we only have new default methods we will still be able to use this hash |
| 5705 | // table (i.e. it will never have 0 size). |
| 5706 | const size_t hash_table_size = num_virtual_methods * 3 + 1; |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5707 | uint32_t* hash_table_ptr; |
| 5708 | std::unique_ptr<uint32_t[]> hash_heap_storage; |
| 5709 | if (hash_table_size <= kMaxStackHash) { |
| 5710 | hash_table_ptr = reinterpret_cast<uint32_t*>( |
| 5711 | alloca(hash_table_size * sizeof(*hash_table_ptr))); |
| 5712 | } else { |
| 5713 | hash_heap_storage.reset(new uint32_t[hash_table_size]); |
| 5714 | hash_table_ptr = hash_heap_storage.get(); |
| 5715 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5716 | 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] | 5717 | // Add virtual methods to the hash table. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5718 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5719 | DCHECK(klass->GetVirtualMethodDuringLinking( |
| 5720 | i, image_pointer_size_)->GetDeclaringClass() != nullptr); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5721 | hash_table.Add(i); |
| 5722 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5723 | // 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] | 5724 | // the hash table. |
| 5725 | for (size_t j = 0; j < super_vtable_length; ++j) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5726 | // 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] | 5727 | ArtMethod* super_method = vtable->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 5728 | if (!klass->CanAccessMember(super_method->GetDeclaringClass(), |
| 5729 | super_method->GetAccessFlags())) { |
| 5730 | // Continue on to the next method since this one is package private and canot be overridden. |
| 5731 | // Before Android 4.1, the package-private method super_method might have been incorrectly |
| 5732 | // overridden. |
| 5733 | continue; |
| 5734 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5735 | MethodNameAndSignatureComparator super_method_name_comparator( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5736 | super_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 5737 | // We remove the method so that subsequent lookups will be faster by making the hash-map |
| 5738 | // smaller as we go on. |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5739 | uint32_t hash_index = hash_table.FindAndRemove(&super_method_name_comparator); |
| 5740 | if (hash_index != hash_table.GetNotFoundIndex()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5741 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking( |
| 5742 | hash_index, image_pointer_size_); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 5743 | if (super_method->IsFinal()) { |
| 5744 | ThrowLinkageError(klass.Get(), "Method %s overrides final method in class %s", |
| 5745 | virtual_method->PrettyMethod().c_str(), |
| 5746 | super_method->GetDeclaringClassDescriptor()); |
| 5747 | return false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5748 | } |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 5749 | vtable->SetElementPtrSize(j, virtual_method, image_pointer_size_); |
| 5750 | virtual_method->SetMethodIndex(j); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5751 | } else if (super_method->IsOverridableByDefaultMethod()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5752 | // We didn't directly override this method but we might through default methods... |
| 5753 | // Check for default method update. |
| 5754 | ArtMethod* default_method = nullptr; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5755 | switch (FindDefaultMethodImplementation(self, |
| 5756 | super_method, |
| 5757 | klass, |
| 5758 | /*out*/&default_method)) { |
| 5759 | case DefaultMethodSearchResult::kDefaultConflict: { |
| 5760 | // A conflict was found looking for default methods. Note this (assuming it wasn't |
| 5761 | // pre-existing) in the translations map. |
| 5762 | if (UNLIKELY(!super_method->IsDefaultConflicting())) { |
| 5763 | // Don't generate another conflict method to reduce memory use as an optimization. |
| 5764 | default_translations->insert( |
| 5765 | {j, ClassLinker::MethodTranslation::CreateConflictingMethod()}); |
| 5766 | } |
| 5767 | break; |
| 5768 | } |
| 5769 | case DefaultMethodSearchResult::kAbstractFound: { |
| 5770 | // No conflict but method is abstract. |
| 5771 | // We note that this vtable entry must be made abstract. |
| 5772 | if (UNLIKELY(!super_method->IsAbstract())) { |
| 5773 | default_translations->insert( |
| 5774 | {j, ClassLinker::MethodTranslation::CreateAbstractMethod()}); |
| 5775 | } |
| 5776 | break; |
| 5777 | } |
| 5778 | case DefaultMethodSearchResult::kDefaultFound: { |
| 5779 | if (UNLIKELY(super_method->IsDefaultConflicting() || |
| 5780 | default_method->GetDeclaringClass() != super_method->GetDeclaringClass())) { |
| 5781 | // Found a default method implementation that is new. |
| 5782 | // TODO Refactor this add default methods to virtuals here and not in |
| 5783 | // LinkInterfaceMethods maybe. |
| 5784 | // The problem is default methods might override previously present |
| 5785 | // default-method or miranda-method vtable entries from the superclass. |
| 5786 | // Unfortunately we need these to be entries in this class's virtuals. We do not |
| 5787 | // give these entries there until LinkInterfaceMethods so we pass this map around |
| 5788 | // to let it know which vtable entries need to be updated. |
| 5789 | // Make a note that vtable entry j must be updated, store what it needs to be updated |
| 5790 | // to. We will allocate a virtual method slot in LinkInterfaceMethods and fix it up |
| 5791 | // then. |
| 5792 | default_translations->insert( |
| 5793 | {j, ClassLinker::MethodTranslation::CreateTranslatedMethod(default_method)}); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5794 | VLOG(class_linker) << "Method " << super_method->PrettyMethod() |
| 5795 | << " overridden by default " |
| 5796 | << default_method->PrettyMethod() |
| 5797 | << " in " << mirror::Class::PrettyClass(klass.Get()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5798 | } |
| 5799 | break; |
| 5800 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5801 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5802 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5803 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5804 | size_t actual_count = super_vtable_length; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5805 | // Add the non-overridden methods at the end. |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5806 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5807 | ArtMethod* local_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5808 | size_t method_idx = local_method->GetMethodIndexDuringLinking(); |
| 5809 | if (method_idx < super_vtable_length && |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5810 | local_method == vtable->GetElementPtrSize<ArtMethod*>(method_idx, image_pointer_size_)) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5811 | continue; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5812 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5813 | vtable->SetElementPtrSize(actual_count, local_method, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5814 | local_method->SetMethodIndex(actual_count); |
| 5815 | ++actual_count; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5816 | } |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 5817 | if (!IsUint<16>(actual_count)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5818 | ThrowClassFormatError(klass.Get(), "Too many methods defined on class: %zd", actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5819 | return false; |
| 5820 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5821 | // Shrink vtable if possible |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5822 | CHECK_LE(actual_count, max_count); |
| 5823 | if (actual_count < max_count) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5824 | vtable.Assign(down_cast<mirror::PointerArray*>(vtable->CopyOf(self, actual_count))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5825 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5826 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5827 | return false; |
| 5828 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5829 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5830 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5831 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 5832 | CHECK_EQ(klass.Get(), GetClassRoot(kJavaLangObject)); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 5833 | if (!IsUint<16>(num_virtual_methods)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5834 | ThrowClassFormatError(klass.Get(), "Too many methods: %d", |
| 5835 | static_cast<int>(num_virtual_methods)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5836 | return false; |
| 5837 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5838 | auto* vtable = AllocPointerArray(self, num_virtual_methods); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5839 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5840 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5841 | return false; |
| 5842 | } |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 5843 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5844 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
| 5845 | vtable->SetElementPtrSize(i, virtual_method, image_pointer_size_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5846 | virtual_method->SetMethodIndex(i & 0xFFFF); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5847 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5848 | klass->SetVTable(vtable); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5849 | } |
| 5850 | return true; |
| 5851 | } |
| 5852 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5853 | // Determine if the given iface has any subinterface in the given list that declares the method |
| 5854 | // specified by 'target'. |
| 5855 | // |
| 5856 | // Arguments |
| 5857 | // - self: The thread we are running on |
| 5858 | // - target: A comparator that will match any method that overrides the method we are checking for |
| 5859 | // - iftable: The iftable we are searching for an overriding method on. |
| 5860 | // - ifstart: The index of the interface we are checking to see if anything overrides |
| 5861 | // - iface: The interface we are checking to see if anything overrides. |
| 5862 | // - image_pointer_size: |
| 5863 | // The image pointer size. |
| 5864 | // |
| 5865 | // Returns |
| 5866 | // - True: There is some method that matches the target comparator defined in an interface that |
| 5867 | // is a subtype of iface. |
| 5868 | // - False: There is no method that matches the target comparator in any interface that is a subtype |
| 5869 | // of iface. |
| 5870 | static bool ContainsOverridingMethodOf(Thread* self, |
| 5871 | MethodNameAndSignatureComparator& target, |
| 5872 | Handle<mirror::IfTable> iftable, |
| 5873 | size_t ifstart, |
| 5874 | Handle<mirror::Class> iface, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 5875 | PointerSize image_pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5876 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5877 | DCHECK(self != nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5878 | DCHECK(iface != nullptr); |
| 5879 | DCHECK(iftable != nullptr); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5880 | DCHECK_GE(ifstart, 0u); |
| 5881 | DCHECK_LT(ifstart, iftable->Count()); |
| 5882 | DCHECK_EQ(iface.Get(), iftable->GetInterface(ifstart)); |
| 5883 | DCHECK(iface->IsInterface()); |
| 5884 | |
| 5885 | size_t iftable_count = iftable->Count(); |
| 5886 | StackHandleScope<1> hs(self); |
| 5887 | MutableHandle<mirror::Class> current_iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 5888 | for (size_t k = ifstart + 1; k < iftable_count; k++) { |
| 5889 | // Skip ifstart since our current interface obviously cannot override itself. |
| 5890 | current_iface.Assign(iftable->GetInterface(k)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5891 | // Iterate through every method on this interface. The order does not matter. |
| 5892 | for (ArtMethod& current_method : current_iface->GetDeclaredVirtualMethods(image_pointer_size)) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5893 | if (UNLIKELY(target.HasSameNameAndSignature( |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5894 | current_method.GetInterfaceMethodIfProxy(image_pointer_size)))) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5895 | // Check if the i'th interface is a subtype of this one. |
| 5896 | if (iface->IsAssignableFrom(current_iface.Get())) { |
| 5897 | return true; |
| 5898 | } |
| 5899 | break; |
| 5900 | } |
| 5901 | } |
| 5902 | } |
| 5903 | return false; |
| 5904 | } |
| 5905 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5906 | // 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] | 5907 | // out_default_method and returns kDefaultFound on success. If no default method was found return |
| 5908 | // kAbstractFound and store nullptr into out_default_method. If an error occurs (such as a |
| 5909 | // default_method conflict) it will return kDefaultConflict. |
| 5910 | ClassLinker::DefaultMethodSearchResult ClassLinker::FindDefaultMethodImplementation( |
| 5911 | Thread* self, |
| 5912 | ArtMethod* target_method, |
| 5913 | Handle<mirror::Class> klass, |
| 5914 | /*out*/ArtMethod** out_default_method) const { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5915 | DCHECK(self != nullptr); |
| 5916 | DCHECK(target_method != nullptr); |
| 5917 | DCHECK(out_default_method != nullptr); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5918 | |
| 5919 | *out_default_method = nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5920 | |
| 5921 | // We organize the interface table so that, for interface I any subinterfaces J follow it in the |
| 5922 | // table. This lets us walk the table backwards when searching for default methods. The first one |
| 5923 | // we encounter is the best candidate since it is the most specific. Once we have found it we keep |
| 5924 | // track of it and then continue checking all other interfaces, since we need to throw an error if |
| 5925 | // we encounter conflicting default method implementations (one is not a subtype of the other). |
| 5926 | // |
| 5927 | // The order of unrelated interfaces does not matter and is not defined. |
| 5928 | size_t iftable_count = klass->GetIfTableCount(); |
| 5929 | if (iftable_count == 0) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5930 | // No interfaces. We have already reset out to null so just return kAbstractFound. |
| 5931 | return DefaultMethodSearchResult::kAbstractFound; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5932 | } |
| 5933 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5934 | StackHandleScope<3> hs(self); |
| 5935 | MutableHandle<mirror::Class> chosen_iface(hs.NewHandle<mirror::Class>(nullptr)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5936 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable())); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5937 | MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5938 | MethodNameAndSignatureComparator target_name_comparator( |
| 5939 | target_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
| 5940 | // Iterates over the klass's iftable in reverse |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5941 | for (size_t k = iftable_count; k != 0; ) { |
| 5942 | --k; |
| 5943 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5944 | DCHECK_LT(k, iftable->Count()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5945 | |
| 5946 | iface.Assign(iftable->GetInterface(k)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5947 | // Iterate through every declared method on this interface. The order does not matter. |
| 5948 | for (auto& method_iter : iface->GetDeclaredVirtualMethods(image_pointer_size_)) { |
| 5949 | ArtMethod* current_method = &method_iter; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5950 | // Skip abstract methods and methods with different names. |
| 5951 | if (current_method->IsAbstract() || |
| 5952 | !target_name_comparator.HasSameNameAndSignature( |
| 5953 | current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
| 5954 | continue; |
Alex Light | d7c10c2 | 2016-03-31 10:03:07 -0700 | [diff] [blame] | 5955 | } else if (!current_method->IsPublic()) { |
| 5956 | // The verifier should have caught the non-public method for dex version 37. Just warn and |
| 5957 | // skip it since this is from before default-methods so we don't really need to care that it |
| 5958 | // has code. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5959 | LOG(WARNING) << "Interface method " << current_method->PrettyMethod() |
| 5960 | << " is not public! " |
Alex Light | d7c10c2 | 2016-03-31 10:03:07 -0700 | [diff] [blame] | 5961 | << "This will be a fatal error in subsequent versions of android. " |
| 5962 | << "Continuing anyway."; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5963 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5964 | if (UNLIKELY(chosen_iface != nullptr)) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5965 | // We have multiple default impls of the same method. This is a potential default conflict. |
| 5966 | // We need to check if this possibly conflicting method is either a superclass of the chosen |
| 5967 | // default implementation or is overridden by a non-default interface method. In either case |
| 5968 | // there is no conflict. |
| 5969 | if (!iface->IsAssignableFrom(chosen_iface.Get()) && |
| 5970 | !ContainsOverridingMethodOf(self, |
| 5971 | target_name_comparator, |
| 5972 | iftable, |
| 5973 | k, |
| 5974 | iface, |
| 5975 | image_pointer_size_)) { |
Nicolas Geoffray | 7f3e0db | 2016-01-28 09:29:31 +0000 | [diff] [blame] | 5976 | VLOG(class_linker) << "Conflicting default method implementations found: " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5977 | << current_method->PrettyMethod() << " and " |
| 5978 | << ArtMethod::PrettyMethod(*out_default_method) << " in class " |
| 5979 | << klass->PrettyClass() << " conflict."; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5980 | *out_default_method = nullptr; |
| 5981 | return DefaultMethodSearchResult::kDefaultConflict; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5982 | } else { |
| 5983 | break; // Continue checking at the next interface. |
| 5984 | } |
| 5985 | } else { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5986 | // chosen_iface == null |
| 5987 | if (!ContainsOverridingMethodOf(self, |
| 5988 | target_name_comparator, |
| 5989 | iftable, |
| 5990 | k, |
| 5991 | iface, |
| 5992 | image_pointer_size_)) { |
| 5993 | // Don't set this as the chosen interface if something else is overriding it (because that |
| 5994 | // other interface would be potentially chosen instead if it was default). If the other |
| 5995 | // interface was abstract then we wouldn't select this interface as chosen anyway since |
| 5996 | // the abstract method masks it. |
| 5997 | *out_default_method = current_method; |
| 5998 | chosen_iface.Assign(iface.Get()); |
| 5999 | // We should now finish traversing the graph to find if we have default methods that |
| 6000 | // conflict. |
| 6001 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6002 | VLOG(class_linker) << "A default method '" << current_method->PrettyMethod() |
| 6003 | << "' was " |
| 6004 | << "skipped because it was overridden by an abstract method in a " |
| 6005 | << "subinterface on class '" << klass->PrettyClass() << "'"; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6006 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6007 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6008 | break; |
| 6009 | } |
| 6010 | } |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6011 | if (*out_default_method != nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6012 | VLOG(class_linker) << "Default method '" << (*out_default_method)->PrettyMethod() |
| 6013 | << "' selected " |
| 6014 | << "as the implementation for '" << target_method->PrettyMethod() |
| 6015 | << "' in '" << klass->PrettyClass() << "'"; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6016 | return DefaultMethodSearchResult::kDefaultFound; |
| 6017 | } else { |
| 6018 | return DefaultMethodSearchResult::kAbstractFound; |
| 6019 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6020 | } |
| 6021 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6022 | ArtMethod* ClassLinker::AddMethodToConflictTable(ObjPtr<mirror::Class> klass, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6023 | ArtMethod* conflict_method, |
| 6024 | ArtMethod* interface_method, |
| 6025 | ArtMethod* method, |
| 6026 | bool force_new_conflict_method) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6027 | ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6028 | Runtime* const runtime = Runtime::Current(); |
| 6029 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
| 6030 | bool new_entry = conflict_method == runtime->GetImtConflictMethod() || force_new_conflict_method; |
| 6031 | |
| 6032 | // Create a new entry if the existing one is the shared conflict method. |
| 6033 | ArtMethod* new_conflict_method = new_entry |
| 6034 | ? runtime->CreateImtConflictMethod(linear_alloc) |
| 6035 | : conflict_method; |
| 6036 | |
| 6037 | // Allocate a new table. Note that we will leak this table at the next conflict, |
| 6038 | // but that's a tradeoff compared to making the table fixed size. |
| 6039 | void* data = linear_alloc->Alloc( |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6040 | Thread::Current(), ImtConflictTable::ComputeSizeWithOneMoreEntry(current_table, |
| 6041 | image_pointer_size_)); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6042 | if (data == nullptr) { |
| 6043 | LOG(ERROR) << "Failed to allocate conflict table"; |
| 6044 | return conflict_method; |
| 6045 | } |
| 6046 | ImtConflictTable* new_table = new (data) ImtConflictTable(current_table, |
| 6047 | interface_method, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6048 | method, |
| 6049 | image_pointer_size_); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6050 | |
| 6051 | // Do a fence to ensure threads see the data in the table before it is assigned |
| 6052 | // to the conflict method. |
| 6053 | // Note that there is a race in the presence of multiple threads and we may leak |
| 6054 | // memory from the LinearAlloc, but that's a tradeoff compared to using |
| 6055 | // atomic operations. |
| 6056 | QuasiAtomic::ThreadFenceRelease(); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6057 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6058 | return new_conflict_method; |
| 6059 | } |
| 6060 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6061 | bool ClassLinker::AllocateIfTableMethodArrays(Thread* self, |
| 6062 | Handle<mirror::Class> klass, |
| 6063 | Handle<mirror::IfTable> iftable) { |
| 6064 | DCHECK(!klass->IsInterface()); |
| 6065 | const bool has_superclass = klass->HasSuperClass(); |
| 6066 | const bool extend_super_iftable = has_superclass; |
| 6067 | const size_t ifcount = klass->GetIfTableCount(); |
| 6068 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
| 6069 | for (size_t i = 0; i < ifcount; ++i) { |
| 6070 | size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods(); |
| 6071 | if (num_methods > 0) { |
| 6072 | const bool is_super = i < super_ifcount; |
| 6073 | // This is an interface implemented by a super-class. Therefore we can just copy the method |
| 6074 | // array from the superclass. |
| 6075 | const bool super_interface = is_super && extend_super_iftable; |
| 6076 | ObjPtr<mirror::PointerArray> method_array; |
| 6077 | if (super_interface) { |
| 6078 | ObjPtr<mirror::IfTable> if_table = klass->GetSuperClass()->GetIfTable(); |
| 6079 | DCHECK(if_table != nullptr); |
| 6080 | DCHECK(if_table->GetMethodArray(i) != nullptr); |
| 6081 | // If we are working on a super interface, try extending the existing method array. |
| 6082 | method_array = down_cast<mirror::PointerArray*>(if_table->GetMethodArray(i)->Clone(self)); |
| 6083 | } else { |
| 6084 | method_array = AllocPointerArray(self, num_methods); |
| 6085 | } |
| 6086 | if (UNLIKELY(method_array == nullptr)) { |
| 6087 | self->AssertPendingOOMException(); |
| 6088 | return false; |
| 6089 | } |
| 6090 | iftable->SetMethodArray(i, method_array); |
| 6091 | } |
| 6092 | } |
| 6093 | return true; |
| 6094 | } |
| 6095 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6096 | void ClassLinker::SetIMTRef(ArtMethod* unimplemented_method, |
| 6097 | ArtMethod* imt_conflict_method, |
| 6098 | ArtMethod* current_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6099 | /*out*/bool* new_conflict, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6100 | /*out*/ArtMethod** imt_ref) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6101 | // Place method in imt if entry is empty, place conflict otherwise. |
| 6102 | if (*imt_ref == unimplemented_method) { |
| 6103 | *imt_ref = current_method; |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6104 | } else if (!(*imt_ref)->IsRuntimeMethod()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6105 | // If we are not a conflict and we have the same signature and name as the imt |
| 6106 | // entry, it must be that we overwrote a superclass vtable entry. |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6107 | // Note that we have checked IsRuntimeMethod, as there may be multiple different |
| 6108 | // conflict methods. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6109 | MethodNameAndSignatureComparator imt_comparator( |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6110 | (*imt_ref)->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6111 | if (imt_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6112 | current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6113 | *imt_ref = current_method; |
| 6114 | } else { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6115 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6116 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6117 | } |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6118 | } else { |
| 6119 | // Place the default conflict method. Note that there may be an existing conflict |
| 6120 | // method in the IMT, but it could be one tailored to the super class, with a |
| 6121 | // specific ImtConflictTable. |
| 6122 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6123 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6124 | } |
| 6125 | } |
| 6126 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6127 | void ClassLinker::FillIMTAndConflictTables(ObjPtr<mirror::Class> klass) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6128 | DCHECK(klass->ShouldHaveImt()) << klass->PrettyClass(); |
| 6129 | DCHECK(!klass->IsTemp()) << klass->PrettyClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6130 | ArtMethod* imt_data[ImTable::kSize]; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6131 | Runtime* const runtime = Runtime::Current(); |
| 6132 | ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod(); |
| 6133 | ArtMethod* const conflict_method = runtime->GetImtConflictMethod(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6134 | std::fill_n(imt_data, arraysize(imt_data), unimplemented_method); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6135 | if (klass->GetIfTable() != nullptr) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6136 | bool new_conflict = false; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6137 | FillIMTFromIfTable(klass->GetIfTable(), |
| 6138 | unimplemented_method, |
| 6139 | conflict_method, |
| 6140 | klass, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6141 | /*create_conflict_tables*/true, |
| 6142 | /*ignore_copied_methods*/false, |
| 6143 | &new_conflict, |
| 6144 | &imt_data[0]); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6145 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6146 | if (!klass->ShouldHaveImt()) { |
| 6147 | return; |
| 6148 | } |
| 6149 | // Compare the IMT with the super class including the conflict methods. If they are equivalent, |
| 6150 | // we can just use the same pointer. |
| 6151 | ImTable* imt = nullptr; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6152 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6153 | if (super_class != nullptr && super_class->ShouldHaveImt()) { |
| 6154 | ImTable* super_imt = super_class->GetImt(image_pointer_size_); |
| 6155 | bool same = true; |
| 6156 | for (size_t i = 0; same && i < ImTable::kSize; ++i) { |
| 6157 | ArtMethod* method = imt_data[i]; |
| 6158 | ArtMethod* super_method = super_imt->Get(i, image_pointer_size_); |
| 6159 | if (method != super_method) { |
| 6160 | bool is_conflict_table = method->IsRuntimeMethod() && |
| 6161 | method != unimplemented_method && |
| 6162 | method != conflict_method; |
| 6163 | // Verify conflict contents. |
| 6164 | bool super_conflict_table = super_method->IsRuntimeMethod() && |
| 6165 | super_method != unimplemented_method && |
| 6166 | super_method != conflict_method; |
| 6167 | if (!is_conflict_table || !super_conflict_table) { |
| 6168 | same = false; |
| 6169 | } else { |
| 6170 | ImtConflictTable* table1 = method->GetImtConflictTable(image_pointer_size_); |
| 6171 | ImtConflictTable* table2 = super_method->GetImtConflictTable(image_pointer_size_); |
| 6172 | same = same && table1->Equals(table2, image_pointer_size_); |
| 6173 | } |
| 6174 | } |
| 6175 | } |
| 6176 | if (same) { |
| 6177 | imt = super_imt; |
| 6178 | } |
| 6179 | } |
| 6180 | if (imt == nullptr) { |
| 6181 | imt = klass->GetImt(image_pointer_size_); |
| 6182 | DCHECK(imt != nullptr); |
| 6183 | imt->Populate(imt_data, image_pointer_size_); |
| 6184 | } else { |
| 6185 | klass->SetImt(imt, image_pointer_size_); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6186 | } |
| 6187 | } |
| 6188 | |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6189 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, |
| 6190 | LinearAlloc* linear_alloc, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6191 | PointerSize image_pointer_size) { |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6192 | void* data = linear_alloc->Alloc(Thread::Current(), |
| 6193 | ImtConflictTable::ComputeSize(count, |
| 6194 | image_pointer_size)); |
| 6195 | return (data != nullptr) ? new (data) ImtConflictTable(count, image_pointer_size) : nullptr; |
| 6196 | } |
| 6197 | |
| 6198 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, LinearAlloc* linear_alloc) { |
| 6199 | return CreateImtConflictTable(count, linear_alloc, image_pointer_size_); |
| 6200 | } |
| 6201 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6202 | void ClassLinker::FillIMTFromIfTable(ObjPtr<mirror::IfTable> if_table, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6203 | ArtMethod* unimplemented_method, |
| 6204 | ArtMethod* imt_conflict_method, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6205 | ObjPtr<mirror::Class> klass, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6206 | bool create_conflict_tables, |
| 6207 | bool ignore_copied_methods, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6208 | /*out*/bool* new_conflict, |
| 6209 | /*out*/ArtMethod** imt) { |
| 6210 | uint32_t conflict_counts[ImTable::kSize] = {}; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6211 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6212 | ObjPtr<mirror::Class> interface = if_table->GetInterface(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6213 | const size_t num_virtuals = interface->NumVirtualMethods(); |
| 6214 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 6215 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 6216 | DCHECK_GE(num_virtuals, method_array_count); |
| 6217 | if (kIsDebugBuild) { |
| 6218 | if (klass->IsInterface()) { |
| 6219 | DCHECK_EQ(method_array_count, 0u); |
| 6220 | } else { |
| 6221 | DCHECK_EQ(interface->NumDeclaredVirtualMethods(), method_array_count); |
| 6222 | } |
| 6223 | } |
| 6224 | if (method_array_count == 0) { |
| 6225 | continue; |
| 6226 | } |
| 6227 | auto* method_array = if_table->GetMethodArray(i); |
| 6228 | for (size_t j = 0; j < method_array_count; ++j) { |
| 6229 | ArtMethod* implementation_method = |
| 6230 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 6231 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 6232 | continue; |
| 6233 | } |
| 6234 | DCHECK(implementation_method != nullptr); |
| 6235 | // Miranda methods cannot be used to implement an interface method, but they are safe to put |
| 6236 | // in the IMT since their entrypoint is the interface trampoline. If we put any copied methods |
| 6237 | // or interface methods in the IMT here they will not create extra conflicts since we compare |
| 6238 | // names and signatures in SetIMTRef. |
| 6239 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 6240 | const uint32_t imt_index = ImTable::GetImtIndex(interface_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6241 | |
| 6242 | // There is only any conflicts if all of the interface methods for an IMT slot don't have |
| 6243 | // the same implementation method, keep track of this to avoid creating a conflict table in |
| 6244 | // this case. |
| 6245 | |
| 6246 | // Conflict table size for each IMT slot. |
| 6247 | ++conflict_counts[imt_index]; |
| 6248 | |
| 6249 | SetIMTRef(unimplemented_method, |
| 6250 | imt_conflict_method, |
| 6251 | implementation_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6252 | /*out*/new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6253 | /*out*/&imt[imt_index]); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6254 | } |
| 6255 | } |
| 6256 | |
| 6257 | if (create_conflict_tables) { |
| 6258 | // Create the conflict tables. |
| 6259 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6260 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6261 | size_t conflicts = conflict_counts[i]; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6262 | if (imt[i] == imt_conflict_method) { |
| 6263 | ImtConflictTable* new_table = CreateImtConflictTable(conflicts, linear_alloc); |
| 6264 | if (new_table != nullptr) { |
| 6265 | ArtMethod* new_conflict_method = |
| 6266 | Runtime::Current()->CreateImtConflictMethod(linear_alloc); |
| 6267 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
| 6268 | imt[i] = new_conflict_method; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6269 | } else { |
| 6270 | LOG(ERROR) << "Failed to allocate conflict table"; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6271 | imt[i] = imt_conflict_method; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6272 | } |
| 6273 | } else { |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6274 | DCHECK_NE(imt[i], imt_conflict_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6275 | } |
| 6276 | } |
| 6277 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6278 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6279 | ObjPtr<mirror::Class> interface = if_table->GetInterface(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6280 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 6281 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 6282 | if (method_array_count == 0) { |
| 6283 | continue; |
| 6284 | } |
| 6285 | auto* method_array = if_table->GetMethodArray(i); |
| 6286 | for (size_t j = 0; j < method_array_count; ++j) { |
| 6287 | ArtMethod* implementation_method = |
| 6288 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 6289 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 6290 | continue; |
| 6291 | } |
| 6292 | DCHECK(implementation_method != nullptr); |
| 6293 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 6294 | const uint32_t imt_index = ImTable::GetImtIndex(interface_method); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6295 | if (!imt[imt_index]->IsRuntimeMethod() || |
| 6296 | imt[imt_index] == unimplemented_method || |
| 6297 | imt[imt_index] == imt_conflict_method) { |
| 6298 | continue; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6299 | } |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6300 | ImtConflictTable* table = imt[imt_index]->GetImtConflictTable(image_pointer_size_); |
| 6301 | const size_t num_entries = table->NumEntries(image_pointer_size_); |
| 6302 | table->SetInterfaceMethod(num_entries, image_pointer_size_, interface_method); |
| 6303 | table->SetImplementationMethod(num_entries, image_pointer_size_, implementation_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6304 | } |
| 6305 | } |
| 6306 | } |
| 6307 | } |
| 6308 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6309 | // Simple helper function that checks that no subtypes of 'val' are contained within the 'classes' |
| 6310 | // set. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6311 | static bool NotSubinterfaceOfAny( |
| 6312 | const std::unordered_set<ObjPtr<mirror::Class>, HashObjPtr>& classes, |
| 6313 | ObjPtr<mirror::Class> val) |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6314 | REQUIRES(Roles::uninterruptible_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6315 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6316 | DCHECK(val != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6317 | for (ObjPtr<mirror::Class> c : classes) { |
| 6318 | if (val->IsAssignableFrom(c)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6319 | return false; |
| 6320 | } |
| 6321 | } |
| 6322 | return true; |
| 6323 | } |
| 6324 | |
| 6325 | // Fills in and flattens the interface inheritance hierarchy. |
| 6326 | // |
| 6327 | // By the end of this function all interfaces in the transitive closure of to_process are added to |
| 6328 | // the iftable and every interface precedes all of its sub-interfaces in this list. |
| 6329 | // |
| 6330 | // all I, J: Interface | I <: J implies J precedes I |
| 6331 | // |
| 6332 | // (note A <: B means that A is a subtype of B) |
| 6333 | // |
| 6334 | // This returns the total number of items in the iftable. The iftable might be resized down after |
| 6335 | // this call. |
| 6336 | // |
| 6337 | // We order this backwards so that we do not need to reorder superclass interfaces when new |
| 6338 | // interfaces are added in subclass's interface tables. |
| 6339 | // |
| 6340 | // Upon entry into this function iftable is a copy of the superclass's iftable with the first |
| 6341 | // super_ifcount entries filled in with the transitive closure of the interfaces of the superclass. |
| 6342 | // The other entries are uninitialized. We will fill in the remaining entries in this function. The |
| 6343 | // 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] | 6344 | static size_t FillIfTable(ObjPtr<mirror::IfTable> iftable, |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6345 | size_t super_ifcount, |
| 6346 | std::vector<mirror::Class*> to_process) |
| 6347 | REQUIRES(Roles::uninterruptible_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6348 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6349 | // This is the set of all class's already in the iftable. Used to make checking if a class has |
| 6350 | // already been added quicker. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6351 | std::unordered_set<ObjPtr<mirror::Class>, HashObjPtr> classes_in_iftable; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6352 | // The first super_ifcount elements are from the superclass. We note that they are already added. |
| 6353 | for (size_t i = 0; i < super_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6354 | ObjPtr<mirror::Class> iface = iftable->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6355 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, iface)) << "Bad ordering."; |
| 6356 | classes_in_iftable.insert(iface); |
| 6357 | } |
| 6358 | size_t filled_ifcount = super_ifcount; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6359 | for (ObjPtr<mirror::Class> interface : to_process) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6360 | // Let us call the first filled_ifcount elements of iftable the current-iface-list. |
| 6361 | // At this point in the loop current-iface-list has the invariant that: |
| 6362 | // for every pair of interfaces I,J within it: |
| 6363 | // if index_of(I) < index_of(J) then I is not a subtype of J |
| 6364 | |
| 6365 | // If we have already seen this element then all of its super-interfaces must already be in the |
| 6366 | // current-iface-list so we can skip adding it. |
| 6367 | if (!ContainsElement(classes_in_iftable, interface)) { |
| 6368 | // We haven't seen this interface so add all of its super-interfaces onto the |
| 6369 | // current-iface-list, skipping those already on it. |
| 6370 | int32_t ifcount = interface->GetIfTableCount(); |
| 6371 | for (int32_t j = 0; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6372 | ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6373 | if (!ContainsElement(classes_in_iftable, super_interface)) { |
| 6374 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, super_interface)) << "Bad ordering."; |
| 6375 | classes_in_iftable.insert(super_interface); |
| 6376 | iftable->SetInterface(filled_ifcount, super_interface); |
| 6377 | filled_ifcount++; |
| 6378 | } |
| 6379 | } |
| 6380 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, interface)) << "Bad ordering"; |
| 6381 | // Place this interface onto the current-iface-list after all of its super-interfaces. |
| 6382 | classes_in_iftable.insert(interface); |
| 6383 | iftable->SetInterface(filled_ifcount, interface); |
| 6384 | filled_ifcount++; |
| 6385 | } else if (kIsDebugBuild) { |
| 6386 | // Check all super-interfaces are already in the list. |
| 6387 | int32_t ifcount = interface->GetIfTableCount(); |
| 6388 | for (int32_t j = 0; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6389 | ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6390 | DCHECK(ContainsElement(classes_in_iftable, super_interface)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6391 | << "Iftable does not contain " << mirror::Class::PrettyClass(super_interface) |
| 6392 | << ", a superinterface of " << interface->PrettyClass(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6393 | } |
| 6394 | } |
| 6395 | } |
| 6396 | if (kIsDebugBuild) { |
| 6397 | // Check that the iftable is ordered correctly. |
| 6398 | for (size_t i = 0; i < filled_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6399 | ObjPtr<mirror::Class> if_a = iftable->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6400 | for (size_t j = i + 1; j < filled_ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6401 | ObjPtr<mirror::Class> if_b = iftable->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6402 | // !(if_a <: if_b) |
| 6403 | CHECK(!if_b->IsAssignableFrom(if_a)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6404 | << "Bad interface order: " << mirror::Class::PrettyClass(if_a) << " (index " << i |
| 6405 | << ") extends " |
| 6406 | << 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] | 6407 | << "interface list."; |
| 6408 | } |
| 6409 | } |
| 6410 | } |
| 6411 | return filled_ifcount; |
| 6412 | } |
| 6413 | |
| 6414 | bool ClassLinker::SetupInterfaceLookupTable(Thread* self, Handle<mirror::Class> klass, |
| 6415 | Handle<mirror::ObjectArray<mirror::Class>> interfaces) { |
| 6416 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 6417 | const bool has_superclass = klass->HasSuperClass(); |
| 6418 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6419 | const bool have_interfaces = interfaces != nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6420 | const size_t num_interfaces = |
| 6421 | have_interfaces ? interfaces->GetLength() : klass->NumDirectInterfaces(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6422 | if (num_interfaces == 0) { |
| 6423 | if (super_ifcount == 0) { |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 6424 | if (LIKELY(has_superclass)) { |
| 6425 | klass->SetIfTable(klass->GetSuperClass()->GetIfTable()); |
| 6426 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6427 | // Class implements no interfaces. |
| 6428 | DCHECK_EQ(klass->GetIfTableCount(), 0); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6429 | return true; |
| 6430 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6431 | // Class implements same interfaces as parent, are any of these not marker interfaces? |
| 6432 | bool has_non_marker_interface = false; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6433 | ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6434 | for (size_t i = 0; i < super_ifcount; ++i) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6435 | if (super_iftable->GetMethodArrayCount(i) > 0) { |
| 6436 | has_non_marker_interface = true; |
| 6437 | break; |
| 6438 | } |
| 6439 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6440 | // Class just inherits marker interfaces from parent so recycle parent's iftable. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6441 | if (!has_non_marker_interface) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6442 | klass->SetIfTable(super_iftable); |
| 6443 | return true; |
| 6444 | } |
| 6445 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6446 | size_t ifcount = super_ifcount + num_interfaces; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6447 | // Check that every class being implemented is an interface. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6448 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 6449 | ObjPtr<mirror::Class> interface = have_interfaces |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6450 | ? interfaces->GetWithoutChecks(i) |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 6451 | : mirror::Class::GetDirectInterface(self, klass.Get(), i); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6452 | DCHECK(interface != nullptr); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6453 | if (UNLIKELY(!interface->IsInterface())) { |
| 6454 | std::string temp; |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6455 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 6456 | "Class %s implements non-interface class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6457 | klass->PrettyDescriptor().c_str(), |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6458 | PrettyDescriptor(interface->GetDescriptor(&temp)).c_str()); |
| 6459 | return false; |
| 6460 | } |
| 6461 | ifcount += interface->GetIfTableCount(); |
| 6462 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6463 | // Create the interface function table. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 6464 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(AllocIfTable(self, ifcount))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6465 | if (UNLIKELY(iftable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6466 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6467 | return false; |
| 6468 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6469 | // Fill in table with superclass's iftable. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6470 | if (super_ifcount != 0) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6471 | ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable(); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 6472 | for (size_t i = 0; i < super_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6473 | ObjPtr<mirror::Class> super_interface = super_iftable->GetInterface(i); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6474 | iftable->SetInterface(i, super_interface); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 6475 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6476 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6477 | |
| 6478 | // Note that AllowThreadSuspension is to thread suspension as pthread_testcancel is to pthread |
| 6479 | // cancellation. That is it will suspend if one has a pending suspend request but otherwise |
| 6480 | // doesn't really do anything. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 6481 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6482 | |
| 6483 | size_t new_ifcount; |
| 6484 | { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 6485 | ScopedAssertNoThreadSuspension nts("Copying mirror::Class*'s for FillIfTable"); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6486 | std::vector<mirror::Class*> to_add; |
| 6487 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 6488 | ObjPtr<mirror::Class> interface = have_interfaces ? interfaces->Get(i) : |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 6489 | mirror::Class::GetDirectInterface(self, klass.Get(), i); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 6490 | to_add.push_back(interface.Ptr()); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6491 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6492 | |
| 6493 | new_ifcount = FillIfTable(iftable.Get(), super_ifcount, std::move(to_add)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6494 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6495 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 6496 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6497 | |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6498 | // Shrink iftable in case duplicates were found |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6499 | if (new_ifcount < ifcount) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6500 | DCHECK_NE(num_interfaces, 0U); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6501 | iftable.Assign(down_cast<mirror::IfTable*>( |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6502 | iftable->CopyOf(self, new_ifcount * mirror::IfTable::kMax))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6503 | if (UNLIKELY(iftable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6504 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6505 | return false; |
| 6506 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6507 | ifcount = new_ifcount; |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6508 | } else { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6509 | DCHECK_EQ(new_ifcount, ifcount); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6510 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6511 | klass->SetIfTable(iftable.Get()); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6512 | return true; |
| 6513 | } |
| 6514 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6515 | // Finds the method with a name/signature that matches cmp in the given lists of methods. The list |
| 6516 | // of methods must be unique. |
| 6517 | static ArtMethod* FindSameNameAndSignature(MethodNameAndSignatureComparator& cmp ATTRIBUTE_UNUSED) { |
| 6518 | return nullptr; |
| 6519 | } |
| 6520 | |
| 6521 | template <typename ... Types> |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6522 | static ArtMethod* FindSameNameAndSignature(MethodNameAndSignatureComparator& cmp, |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6523 | const ScopedArenaVector<ArtMethod*>& list, |
| 6524 | const Types& ... rest) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6525 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6526 | for (ArtMethod* method : list) { |
| 6527 | if (cmp.HasSameNameAndSignature(method)) { |
| 6528 | return method; |
| 6529 | } |
| 6530 | } |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6531 | return FindSameNameAndSignature(cmp, rest...); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6532 | } |
| 6533 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6534 | // Check that all vtable entries are present in this class's virtuals or are the same as a |
| 6535 | // superclasses vtable entry. |
| 6536 | static void CheckClassOwnsVTableEntries(Thread* self, |
| 6537 | Handle<mirror::Class> klass, |
| 6538 | PointerSize pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6539 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6540 | StackHandleScope<2> hs(self); |
| 6541 | Handle<mirror::PointerArray> check_vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6542 | ObjPtr<mirror::Class> super_temp = (klass->HasSuperClass()) ? klass->GetSuperClass() : nullptr; |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6543 | Handle<mirror::Class> superclass(hs.NewHandle(super_temp)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6544 | int32_t super_vtable_length = (superclass != nullptr) ? superclass->GetVTableLength() : 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6545 | for (int32_t i = 0; i < check_vtable->GetLength(); ++i) { |
| 6546 | ArtMethod* m = check_vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 6547 | CHECK(m != nullptr); |
| 6548 | |
Alex Light | a41a3078 | 2017-03-29 11:33:19 -0700 | [diff] [blame] | 6549 | if (m->GetMethodIndexDuringLinking() != i) { |
| 6550 | LOG(WARNING) << m->PrettyMethod() |
| 6551 | << " has an unexpected method index for its spot in the vtable for class" |
| 6552 | << klass->PrettyClass(); |
| 6553 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6554 | ArraySlice<ArtMethod> virtuals = klass->GetVirtualMethodsSliceUnchecked(pointer_size); |
| 6555 | auto is_same_method = [m] (const ArtMethod& meth) { |
| 6556 | return &meth == m; |
| 6557 | }; |
Alex Light | 3f98053 | 2017-03-17 15:10:32 -0700 | [diff] [blame] | 6558 | if (!((super_vtable_length > i && superclass->GetVTableEntry(i, pointer_size) == m) || |
| 6559 | std::find_if(virtuals.begin(), virtuals.end(), is_same_method) != virtuals.end())) { |
| 6560 | LOG(WARNING) << m->PrettyMethod() << " does not seem to be owned by current class " |
| 6561 | << klass->PrettyClass() << " or any of its superclasses!"; |
| 6562 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6563 | } |
| 6564 | } |
| 6565 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6566 | // Check to make sure the vtable does not have duplicates. Duplicates could cause problems when a |
| 6567 | // method is overridden in a subclass. |
| 6568 | static void CheckVTableHasNoDuplicates(Thread* self, |
| 6569 | Handle<mirror::Class> klass, |
| 6570 | PointerSize pointer_size) |
| 6571 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6572 | StackHandleScope<1> hs(self); |
| 6573 | Handle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
| 6574 | int32_t num_entries = vtable->GetLength(); |
| 6575 | for (int32_t i = 0; i < num_entries; i++) { |
| 6576 | ArtMethod* vtable_entry = vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 6577 | // Don't bother if we cannot 'see' the vtable entry (i.e. it is a package-private member maybe). |
| 6578 | if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(), |
| 6579 | vtable_entry->GetAccessFlags())) { |
| 6580 | continue; |
| 6581 | } |
| 6582 | MethodNameAndSignatureComparator name_comparator( |
| 6583 | vtable_entry->GetInterfaceMethodIfProxy(pointer_size)); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 6584 | for (int32_t j = i + 1; j < num_entries; j++) { |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6585 | ArtMethod* other_entry = vtable->GetElementPtrSize<ArtMethod*>(j, pointer_size); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 6586 | if (!klass->CanAccessMember(other_entry->GetDeclaringClass(), |
| 6587 | other_entry->GetAccessFlags())) { |
| 6588 | continue; |
| 6589 | } |
Alex Light | 3f98053 | 2017-03-17 15:10:32 -0700 | [diff] [blame] | 6590 | if (vtable_entry == other_entry || |
| 6591 | name_comparator.HasSameNameAndSignature( |
| 6592 | other_entry->GetInterfaceMethodIfProxy(pointer_size))) { |
| 6593 | LOG(WARNING) << "vtable entries " << i << " and " << j << " are identical for " |
| 6594 | << klass->PrettyClass() << " in method " << vtable_entry->PrettyMethod() |
| 6595 | << " (0x" << std::hex << reinterpret_cast<uintptr_t>(vtable_entry) << ") and " |
| 6596 | << other_entry->PrettyMethod() << " (0x" << std::hex |
| 6597 | << reinterpret_cast<uintptr_t>(other_entry) << ")"; |
| 6598 | } |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6599 | } |
| 6600 | } |
| 6601 | } |
| 6602 | |
| 6603 | static void SanityCheckVTable(Thread* self, Handle<mirror::Class> klass, PointerSize pointer_size) |
| 6604 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6605 | CheckClassOwnsVTableEntries(self, klass, pointer_size); |
| 6606 | CheckVTableHasNoDuplicates(self, klass, pointer_size); |
| 6607 | } |
| 6608 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6609 | void ClassLinker::FillImtFromSuperClass(Handle<mirror::Class> klass, |
| 6610 | ArtMethod* unimplemented_method, |
| 6611 | ArtMethod* imt_conflict_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6612 | bool* new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6613 | ArtMethod** imt) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6614 | DCHECK(klass->HasSuperClass()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6615 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6616 | if (super_class->ShouldHaveImt()) { |
| 6617 | ImTable* super_imt = super_class->GetImt(image_pointer_size_); |
| 6618 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 6619 | imt[i] = super_imt->Get(i, image_pointer_size_); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6620 | } |
| 6621 | } else { |
| 6622 | // No imt in the super class, need to reconstruct from the iftable. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6623 | ObjPtr<mirror::IfTable> if_table = super_class->GetIfTable(); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 6624 | if (if_table->Count() != 0) { |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6625 | // Ignore copied methods since we will handle these in LinkInterfaceMethods. |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6626 | FillIMTFromIfTable(if_table, |
| 6627 | unimplemented_method, |
| 6628 | imt_conflict_method, |
| 6629 | klass.Get(), |
| 6630 | /*create_conflict_table*/false, |
| 6631 | /*ignore_copied_methods*/true, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6632 | /*out*/new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6633 | /*out*/imt); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6634 | } |
| 6635 | } |
| 6636 | } |
| 6637 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6638 | class ClassLinker::LinkInterfaceMethodsHelper { |
| 6639 | public: |
| 6640 | LinkInterfaceMethodsHelper(ClassLinker* class_linker, |
| 6641 | Handle<mirror::Class> klass, |
| 6642 | Thread* self, |
| 6643 | Runtime* runtime) |
| 6644 | : class_linker_(class_linker), |
| 6645 | klass_(klass), |
| 6646 | method_alignment_(ArtMethod::Alignment(class_linker->GetImagePointerSize())), |
| 6647 | method_size_(ArtMethod::Size(class_linker->GetImagePointerSize())), |
| 6648 | self_(self), |
| 6649 | stack_(runtime->GetLinearAlloc()->GetArenaPool()), |
| 6650 | allocator_(&stack_), |
| 6651 | default_conflict_methods_(allocator_.Adapter()), |
| 6652 | overriding_default_conflict_methods_(allocator_.Adapter()), |
| 6653 | miranda_methods_(allocator_.Adapter()), |
| 6654 | default_methods_(allocator_.Adapter()), |
| 6655 | overriding_default_methods_(allocator_.Adapter()), |
| 6656 | move_table_(allocator_.Adapter()) { |
| 6657 | } |
| 6658 | |
| 6659 | ArtMethod* FindMethod(ArtMethod* interface_method, |
| 6660 | MethodNameAndSignatureComparator& interface_name_comparator, |
| 6661 | ArtMethod* vtable_impl) |
| 6662 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 6663 | |
| 6664 | ArtMethod* GetOrCreateMirandaMethod(ArtMethod* interface_method, |
| 6665 | MethodNameAndSignatureComparator& interface_name_comparator) |
| 6666 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 6667 | |
| 6668 | bool HasNewVirtuals() const { |
| 6669 | return !(miranda_methods_.empty() && |
| 6670 | default_methods_.empty() && |
| 6671 | overriding_default_methods_.empty() && |
| 6672 | overriding_default_conflict_methods_.empty() && |
| 6673 | default_conflict_methods_.empty()); |
| 6674 | } |
| 6675 | |
| 6676 | void ReallocMethods() REQUIRES_SHARED(Locks::mutator_lock_); |
| 6677 | |
| 6678 | ObjPtr<mirror::PointerArray> UpdateVtable( |
| 6679 | const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations, |
| 6680 | ObjPtr<mirror::PointerArray> old_vtable) REQUIRES_SHARED(Locks::mutator_lock_); |
| 6681 | |
| 6682 | void UpdateIfTable(Handle<mirror::IfTable> iftable) REQUIRES_SHARED(Locks::mutator_lock_); |
| 6683 | |
| 6684 | void UpdateIMT(ArtMethod** out_imt); |
| 6685 | |
| 6686 | void CheckNoStaleMethodsInDexCache() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6687 | if (kIsDebugBuild) { |
| 6688 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 6689 | // Check that there are no stale methods are in the dex cache array. |
| 6690 | auto* resolved_methods = klass_->GetDexCache()->GetResolvedMethods(); |
| 6691 | for (size_t i = 0, count = klass_->GetDexCache()->NumResolvedMethods(); i < count; ++i) { |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 6692 | auto pair = mirror::DexCache::GetNativePairPtrSize(resolved_methods, i, pointer_size); |
| 6693 | ArtMethod* m = pair.object; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6694 | CHECK(move_table_.find(m) == move_table_.end() || |
| 6695 | // The original versions of copied methods will still be present so allow those too. |
| 6696 | // Note that if the first check passes this might fail to GetDeclaringClass(). |
| 6697 | std::find_if(m->GetDeclaringClass()->GetMethods(pointer_size).begin(), |
| 6698 | m->GetDeclaringClass()->GetMethods(pointer_size).end(), |
| 6699 | [m] (ArtMethod& meth) { |
| 6700 | return &meth == m; |
| 6701 | }) != m->GetDeclaringClass()->GetMethods(pointer_size).end()) |
| 6702 | << "Obsolete method " << m->PrettyMethod() << " is in dex cache!"; |
| 6703 | } |
| 6704 | } |
| 6705 | } |
| 6706 | |
| 6707 | void ClobberOldMethods(LengthPrefixedArray<ArtMethod>* old_methods, |
| 6708 | LengthPrefixedArray<ArtMethod>* methods) { |
| 6709 | if (kIsDebugBuild) { |
| 6710 | CHECK(methods != nullptr); |
| 6711 | // Put some random garbage in old methods to help find stale pointers. |
| 6712 | if (methods != old_methods && old_methods != nullptr) { |
| 6713 | // Need to make sure the GC is not running since it could be scanning the methods we are |
| 6714 | // about to overwrite. |
| 6715 | ScopedThreadStateChange tsc(self_, kSuspended); |
| 6716 | gc::ScopedGCCriticalSection gcs(self_, |
| 6717 | gc::kGcCauseClassLinker, |
| 6718 | gc::kCollectorTypeClassLinker); |
| 6719 | const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_methods->size(), |
| 6720 | method_size_, |
| 6721 | method_alignment_); |
| 6722 | memset(old_methods, 0xFEu, old_size); |
| 6723 | } |
| 6724 | } |
| 6725 | } |
| 6726 | |
| 6727 | private: |
| 6728 | size_t NumberOfNewVirtuals() const { |
| 6729 | return miranda_methods_.size() + |
| 6730 | default_methods_.size() + |
| 6731 | overriding_default_conflict_methods_.size() + |
| 6732 | overriding_default_methods_.size() + |
| 6733 | default_conflict_methods_.size(); |
| 6734 | } |
| 6735 | |
| 6736 | bool FillTables() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6737 | return !klass_->IsInterface(); |
| 6738 | } |
| 6739 | |
| 6740 | void LogNewVirtuals() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6741 | DCHECK(!klass_->IsInterface() || (default_methods_.empty() && miranda_methods_.empty())) |
| 6742 | << "Interfaces should only have default-conflict methods appended to them."; |
| 6743 | VLOG(class_linker) << mirror::Class::PrettyClass(klass_.Get()) << ": miranda_methods=" |
| 6744 | << miranda_methods_.size() |
| 6745 | << " default_methods=" << default_methods_.size() |
| 6746 | << " overriding_default_methods=" << overriding_default_methods_.size() |
| 6747 | << " default_conflict_methods=" << default_conflict_methods_.size() |
| 6748 | << " overriding_default_conflict_methods=" |
| 6749 | << overriding_default_conflict_methods_.size(); |
| 6750 | } |
| 6751 | |
| 6752 | ClassLinker* class_linker_; |
| 6753 | Handle<mirror::Class> klass_; |
| 6754 | size_t method_alignment_; |
| 6755 | size_t method_size_; |
| 6756 | Thread* const self_; |
| 6757 | |
| 6758 | // These are allocated on the heap to begin, we then transfer to linear alloc when we re-create |
| 6759 | // the virtual methods array. |
| 6760 | // Need to use low 4GB arenas for compiler or else the pointers wont fit in 32 bit method array |
| 6761 | // during cross compilation. |
| 6762 | // Use the linear alloc pool since this one is in the low 4gb for the compiler. |
| 6763 | ArenaStack stack_; |
| 6764 | ScopedArenaAllocator allocator_; |
| 6765 | |
| 6766 | ScopedArenaVector<ArtMethod*> default_conflict_methods_; |
| 6767 | ScopedArenaVector<ArtMethod*> overriding_default_conflict_methods_; |
| 6768 | ScopedArenaVector<ArtMethod*> miranda_methods_; |
| 6769 | ScopedArenaVector<ArtMethod*> default_methods_; |
| 6770 | ScopedArenaVector<ArtMethod*> overriding_default_methods_; |
| 6771 | |
| 6772 | ScopedArenaUnorderedMap<ArtMethod*, ArtMethod*> move_table_; |
| 6773 | }; |
| 6774 | |
| 6775 | ArtMethod* ClassLinker::LinkInterfaceMethodsHelper::FindMethod( |
| 6776 | ArtMethod* interface_method, |
| 6777 | MethodNameAndSignatureComparator& interface_name_comparator, |
| 6778 | ArtMethod* vtable_impl) { |
| 6779 | ArtMethod* current_method = nullptr; |
| 6780 | switch (class_linker_->FindDefaultMethodImplementation(self_, |
| 6781 | interface_method, |
| 6782 | klass_, |
| 6783 | /*out*/¤t_method)) { |
| 6784 | case DefaultMethodSearchResult::kDefaultConflict: { |
| 6785 | // Default method conflict. |
| 6786 | DCHECK(current_method == nullptr); |
| 6787 | ArtMethod* default_conflict_method = nullptr; |
| 6788 | if (vtable_impl != nullptr && vtable_impl->IsDefaultConflicting()) { |
| 6789 | // We can reuse the method from the superclass, don't bother adding it to virtuals. |
| 6790 | default_conflict_method = vtable_impl; |
| 6791 | } else { |
| 6792 | // See if we already have a conflict method for this method. |
| 6793 | ArtMethod* preexisting_conflict = FindSameNameAndSignature( |
| 6794 | interface_name_comparator, |
| 6795 | default_conflict_methods_, |
| 6796 | overriding_default_conflict_methods_); |
| 6797 | if (LIKELY(preexisting_conflict != nullptr)) { |
| 6798 | // We already have another conflict we can reuse. |
| 6799 | default_conflict_method = preexisting_conflict; |
| 6800 | } else { |
| 6801 | // Note that we do this even if we are an interface since we need to create this and |
| 6802 | // cannot reuse another classes. |
| 6803 | // Create a new conflict method for this to use. |
| 6804 | default_conflict_method = reinterpret_cast<ArtMethod*>(allocator_.Alloc(method_size_)); |
| 6805 | new(default_conflict_method) ArtMethod(interface_method, |
| 6806 | class_linker_->GetImagePointerSize()); |
| 6807 | if (vtable_impl == nullptr) { |
| 6808 | // Save the conflict method. We need to add it to the vtable. |
| 6809 | default_conflict_methods_.push_back(default_conflict_method); |
| 6810 | } else { |
| 6811 | // Save the conflict method but it is already in the vtable. |
| 6812 | overriding_default_conflict_methods_.push_back(default_conflict_method); |
| 6813 | } |
| 6814 | } |
| 6815 | } |
| 6816 | current_method = default_conflict_method; |
| 6817 | break; |
| 6818 | } // case kDefaultConflict |
| 6819 | case DefaultMethodSearchResult::kDefaultFound: { |
| 6820 | DCHECK(current_method != nullptr); |
| 6821 | // Found a default method. |
| 6822 | if (vtable_impl != nullptr && |
| 6823 | current_method->GetDeclaringClass() == vtable_impl->GetDeclaringClass()) { |
| 6824 | // We found a default method but it was the same one we already have from our |
| 6825 | // superclass. Don't bother adding it to our vtable again. |
| 6826 | current_method = vtable_impl; |
| 6827 | } else if (LIKELY(FillTables())) { |
| 6828 | // Interfaces don't need to copy default methods since they don't have vtables. |
| 6829 | // Only record this default method if it is new to save space. |
| 6830 | // TODO It might be worthwhile to copy default methods on interfaces anyway since it |
| 6831 | // would make lookup for interface super much faster. (We would only need to scan |
| 6832 | // the iftable to find if there is a NSME or AME.) |
| 6833 | ArtMethod* old = FindSameNameAndSignature(interface_name_comparator, |
| 6834 | default_methods_, |
| 6835 | overriding_default_methods_); |
| 6836 | if (old == nullptr) { |
| 6837 | // We found a default method implementation and there were no conflicts. |
| 6838 | if (vtable_impl == nullptr) { |
| 6839 | // Save the default method. We need to add it to the vtable. |
| 6840 | default_methods_.push_back(current_method); |
| 6841 | } else { |
| 6842 | // Save the default method but it is already in the vtable. |
| 6843 | overriding_default_methods_.push_back(current_method); |
| 6844 | } |
| 6845 | } else { |
| 6846 | CHECK(old == current_method) << "Multiple default implementations selected!"; |
| 6847 | } |
| 6848 | } |
| 6849 | break; |
| 6850 | } // case kDefaultFound |
| 6851 | case DefaultMethodSearchResult::kAbstractFound: { |
| 6852 | DCHECK(current_method == nullptr); |
| 6853 | // Abstract method masks all defaults. |
| 6854 | if (vtable_impl != nullptr && |
| 6855 | vtable_impl->IsAbstract() && |
| 6856 | !vtable_impl->IsDefaultConflicting()) { |
| 6857 | // We need to make this an abstract method but the version in the vtable already is so |
| 6858 | // don't do anything. |
| 6859 | current_method = vtable_impl; |
| 6860 | } |
| 6861 | break; |
| 6862 | } // case kAbstractFound |
| 6863 | } |
| 6864 | return current_method; |
| 6865 | } |
| 6866 | |
| 6867 | ArtMethod* ClassLinker::LinkInterfaceMethodsHelper::GetOrCreateMirandaMethod( |
| 6868 | ArtMethod* interface_method, |
| 6869 | MethodNameAndSignatureComparator& interface_name_comparator) { |
| 6870 | // Find out if there is already a miranda method we can use. |
| 6871 | ArtMethod* miranda_method = FindSameNameAndSignature(interface_name_comparator, |
| 6872 | miranda_methods_); |
| 6873 | if (miranda_method == nullptr) { |
| 6874 | DCHECK(interface_method->IsAbstract()) << interface_method->PrettyMethod(); |
| 6875 | miranda_method = reinterpret_cast<ArtMethod*>(allocator_.Alloc(method_size_)); |
| 6876 | CHECK(miranda_method != nullptr); |
| 6877 | // Point the interface table at a phantom slot. |
| 6878 | new(miranda_method) ArtMethod(interface_method, class_linker_->GetImagePointerSize()); |
| 6879 | miranda_methods_.push_back(miranda_method); |
| 6880 | } |
| 6881 | return miranda_method; |
| 6882 | } |
| 6883 | |
| 6884 | void ClassLinker::LinkInterfaceMethodsHelper::ReallocMethods() { |
| 6885 | LogNewVirtuals(); |
| 6886 | |
| 6887 | const size_t old_method_count = klass_->NumMethods(); |
| 6888 | const size_t new_method_count = old_method_count + NumberOfNewVirtuals(); |
| 6889 | DCHECK_NE(old_method_count, new_method_count); |
| 6890 | |
| 6891 | // Attempt to realloc to save RAM if possible. |
| 6892 | LengthPrefixedArray<ArtMethod>* old_methods = klass_->GetMethodsPtr(); |
| 6893 | // The Realloced virtual methods aren't visible from the class roots, so there is no issue |
| 6894 | // where GCs could attempt to mark stale pointers due to memcpy. And since we overwrite the |
| 6895 | // realloced memory with out->CopyFrom, we are guaranteed to have objects in the to space since |
| 6896 | // CopyFrom has internal read barriers. |
| 6897 | // |
| 6898 | // TODO We should maybe move some of this into mirror::Class or at least into another method. |
| 6899 | const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_method_count, |
| 6900 | method_size_, |
| 6901 | method_alignment_); |
| 6902 | const size_t new_size = LengthPrefixedArray<ArtMethod>::ComputeSize(new_method_count, |
| 6903 | method_size_, |
| 6904 | method_alignment_); |
| 6905 | const size_t old_methods_ptr_size = (old_methods != nullptr) ? old_size : 0; |
| 6906 | auto* methods = reinterpret_cast<LengthPrefixedArray<ArtMethod>*>( |
Nicolas Geoffray | 48b40cc | 2017-08-07 16:52:40 +0100 | [diff] [blame] | 6907 | class_linker_->GetAllocatorForClassLoader(klass_->GetClassLoader())->Realloc( |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6908 | self_, old_methods, old_methods_ptr_size, new_size)); |
| 6909 | CHECK(methods != nullptr); // Native allocation failure aborts. |
| 6910 | |
| 6911 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 6912 | if (methods != old_methods) { |
| 6913 | // Maps from heap allocated miranda method to linear alloc miranda method. |
| 6914 | StrideIterator<ArtMethod> out = methods->begin(method_size_, method_alignment_); |
| 6915 | // Copy over the old methods. |
| 6916 | for (auto& m : klass_->GetMethods(pointer_size)) { |
| 6917 | move_table_.emplace(&m, &*out); |
| 6918 | // The CopyFrom is only necessary to not miss read barriers since Realloc won't do read |
| 6919 | // barriers when it copies. |
| 6920 | out->CopyFrom(&m, pointer_size); |
| 6921 | ++out; |
| 6922 | } |
| 6923 | } |
| 6924 | StrideIterator<ArtMethod> out(methods->begin(method_size_, method_alignment_) + old_method_count); |
| 6925 | // Copy over miranda methods before copying vtable since CopyOf may cause thread suspension and |
| 6926 | // we want the roots of the miranda methods to get visited. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 6927 | for (size_t i = 0; i < miranda_methods_.size(); ++i) { |
| 6928 | ArtMethod* mir_method = miranda_methods_[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6929 | ArtMethod& new_method = *out; |
| 6930 | new_method.CopyFrom(mir_method, pointer_size); |
| 6931 | new_method.SetAccessFlags(new_method.GetAccessFlags() | kAccMiranda | kAccCopied); |
| 6932 | DCHECK_NE(new_method.GetAccessFlags() & kAccAbstract, 0u) |
| 6933 | << "Miranda method should be abstract!"; |
| 6934 | move_table_.emplace(mir_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 6935 | // Update the entry in the method array, as the array will be used for future lookups, |
| 6936 | // where thread suspension is allowed. |
| 6937 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 6938 | // would not see them. |
| 6939 | miranda_methods_[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6940 | ++out; |
| 6941 | } |
| 6942 | // We need to copy the default methods into our own method table since the runtime requires that |
| 6943 | // every method on a class's vtable be in that respective class's virtual method table. |
| 6944 | // NOTE This means that two classes might have the same implementation of a method from the same |
| 6945 | // interface but will have different ArtMethod*s for them. This also means we cannot compare a |
| 6946 | // default method found on a class with one found on the declaring interface directly and must |
| 6947 | // look at the declaring class to determine if they are the same. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 6948 | for (ScopedArenaVector<ArtMethod*>* methods_vec : {&default_methods_, |
| 6949 | &overriding_default_methods_}) { |
| 6950 | for (size_t i = 0; i < methods_vec->size(); ++i) { |
| 6951 | ArtMethod* def_method = (*methods_vec)[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6952 | ArtMethod& new_method = *out; |
| 6953 | new_method.CopyFrom(def_method, pointer_size); |
| 6954 | // Clear the kAccSkipAccessChecks flag if it is present. Since this class hasn't been |
| 6955 | // verified yet it shouldn't have methods that are skipping access checks. |
| 6956 | // TODO This is rather arbitrary. We should maybe support classes where only some of its |
| 6957 | // methods are skip_access_checks. |
| 6958 | constexpr uint32_t kSetFlags = kAccDefault | kAccCopied; |
| 6959 | constexpr uint32_t kMaskFlags = ~kAccSkipAccessChecks; |
| 6960 | new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags); |
| 6961 | move_table_.emplace(def_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 6962 | // Update the entry in the method array, as the array will be used for future lookups, |
| 6963 | // where thread suspension is allowed. |
| 6964 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 6965 | // would not see them. |
| 6966 | (*methods_vec)[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6967 | ++out; |
| 6968 | } |
| 6969 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 6970 | for (ScopedArenaVector<ArtMethod*>* methods_vec : {&default_conflict_methods_, |
| 6971 | &overriding_default_conflict_methods_}) { |
| 6972 | for (size_t i = 0; i < methods_vec->size(); ++i) { |
| 6973 | ArtMethod* conf_method = (*methods_vec)[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6974 | ArtMethod& new_method = *out; |
| 6975 | new_method.CopyFrom(conf_method, pointer_size); |
| 6976 | // This is a type of default method (there are default method impls, just a conflict) so |
| 6977 | // mark this as a default, non-abstract method, since thats what it is. Also clear the |
| 6978 | // kAccSkipAccessChecks bit since this class hasn't been verified yet it shouldn't have |
| 6979 | // methods that are skipping access checks. |
| 6980 | constexpr uint32_t kSetFlags = kAccDefault | kAccDefaultConflict | kAccCopied; |
| 6981 | constexpr uint32_t kMaskFlags = ~(kAccAbstract | kAccSkipAccessChecks); |
| 6982 | new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags); |
| 6983 | DCHECK(new_method.IsDefaultConflicting()); |
| 6984 | // The actual method might or might not be marked abstract since we just copied it from a |
| 6985 | // (possibly default) interface method. We need to set it entry point to be the bridge so |
| 6986 | // that the compiler will not invoke the implementation of whatever method we copied from. |
| 6987 | EnsureThrowsInvocationError(class_linker_, &new_method); |
| 6988 | move_table_.emplace(conf_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 6989 | // Update the entry in the method array, as the array will be used for future lookups, |
| 6990 | // where thread suspension is allowed. |
| 6991 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 6992 | // would not see them. |
| 6993 | (*methods_vec)[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6994 | ++out; |
| 6995 | } |
| 6996 | } |
| 6997 | methods->SetSize(new_method_count); |
| 6998 | class_linker_->UpdateClassMethods(klass_.Get(), methods); |
| 6999 | } |
| 7000 | |
| 7001 | ObjPtr<mirror::PointerArray> ClassLinker::LinkInterfaceMethodsHelper::UpdateVtable( |
| 7002 | const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations, |
| 7003 | ObjPtr<mirror::PointerArray> old_vtable) { |
| 7004 | // Update the vtable to the new method structures. We can skip this for interfaces since they |
| 7005 | // do not have vtables. |
| 7006 | const size_t old_vtable_count = old_vtable->GetLength(); |
| 7007 | const size_t new_vtable_count = old_vtable_count + |
| 7008 | miranda_methods_.size() + |
| 7009 | default_methods_.size() + |
| 7010 | default_conflict_methods_.size(); |
| 7011 | |
| 7012 | ObjPtr<mirror::PointerArray> vtable = |
| 7013 | down_cast<mirror::PointerArray*>(old_vtable->CopyOf(self_, new_vtable_count)); |
| 7014 | if (UNLIKELY(vtable == nullptr)) { |
| 7015 | self_->AssertPendingOOMException(); |
| 7016 | return nullptr; |
| 7017 | } |
| 7018 | |
| 7019 | size_t vtable_pos = old_vtable_count; |
| 7020 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 7021 | // Update all the newly copied method's indexes so they denote their placement in the vtable. |
| 7022 | for (const ScopedArenaVector<ArtMethod*>& methods_vec : {default_methods_, |
| 7023 | default_conflict_methods_, |
| 7024 | miranda_methods_}) { |
| 7025 | // These are the functions that are not already in the vtable! |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7026 | for (ArtMethod* new_vtable_method : methods_vec) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7027 | // Leave the declaring class alone the method's dex_code_item_offset_ and dex_method_index_ |
| 7028 | // fields are references into the dex file the method was defined in. Since the ArtMethod |
| 7029 | // does not store that information it uses declaring_class_->dex_cache_. |
| 7030 | new_vtable_method->SetMethodIndex(0xFFFF & vtable_pos); |
| 7031 | vtable->SetElementPtrSize(vtable_pos, new_vtable_method, pointer_size); |
| 7032 | ++vtable_pos; |
| 7033 | } |
| 7034 | } |
| 7035 | DCHECK_EQ(vtable_pos, new_vtable_count); |
| 7036 | |
| 7037 | // Update old vtable methods. We use the default_translations map to figure out what each |
| 7038 | // vtable entry should be updated to, if they need to be at all. |
| 7039 | for (size_t i = 0; i < old_vtable_count; ++i) { |
| 7040 | ArtMethod* translated_method = vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 7041 | // Try and find what we need to change this method to. |
| 7042 | auto translation_it = default_translations.find(i); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7043 | if (translation_it != default_translations.end()) { |
| 7044 | if (translation_it->second.IsInConflict()) { |
| 7045 | // Find which conflict method we are to use for this method. |
| 7046 | MethodNameAndSignatureComparator old_method_comparator( |
| 7047 | translated_method->GetInterfaceMethodIfProxy(pointer_size)); |
| 7048 | // We only need to look through overriding_default_conflict_methods since this is an |
| 7049 | // overridden method we are fixing up here. |
| 7050 | ArtMethod* new_conflict_method = FindSameNameAndSignature( |
| 7051 | old_method_comparator, overriding_default_conflict_methods_); |
| 7052 | CHECK(new_conflict_method != nullptr) << "Expected a conflict method!"; |
| 7053 | translated_method = new_conflict_method; |
| 7054 | } else if (translation_it->second.IsAbstract()) { |
| 7055 | // Find which miranda method we are to use for this method. |
| 7056 | MethodNameAndSignatureComparator old_method_comparator( |
| 7057 | translated_method->GetInterfaceMethodIfProxy(pointer_size)); |
| 7058 | ArtMethod* miranda_method = FindSameNameAndSignature(old_method_comparator, |
| 7059 | miranda_methods_); |
| 7060 | DCHECK(miranda_method != nullptr); |
| 7061 | translated_method = miranda_method; |
| 7062 | } else { |
| 7063 | // Normal default method (changed from an older default or abstract interface method). |
| 7064 | DCHECK(translation_it->second.IsTranslation()); |
| 7065 | translated_method = translation_it->second.GetTranslation(); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7066 | auto it = move_table_.find(translated_method); |
| 7067 | DCHECK(it != move_table_.end()); |
| 7068 | translated_method = it->second; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7069 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7070 | } else { |
| 7071 | auto it = move_table_.find(translated_method); |
| 7072 | translated_method = (it != move_table_.end()) ? it->second : nullptr; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7073 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7074 | |
| 7075 | if (translated_method != nullptr) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7076 | // Make sure the new_methods index is set. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7077 | if (translated_method->GetMethodIndexDuringLinking() != i) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7078 | if (kIsDebugBuild) { |
| 7079 | auto* methods = klass_->GetMethodsPtr(); |
| 7080 | CHECK_LE(reinterpret_cast<uintptr_t>(&*methods->begin(method_size_, method_alignment_)), |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7081 | reinterpret_cast<uintptr_t>(translated_method)); |
| 7082 | CHECK_LT(reinterpret_cast<uintptr_t>(translated_method), |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7083 | reinterpret_cast<uintptr_t>(&*methods->end(method_size_, method_alignment_))); |
| 7084 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7085 | translated_method->SetMethodIndex(0xFFFF & i); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7086 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7087 | vtable->SetElementPtrSize(i, translated_method, pointer_size); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7088 | } |
| 7089 | } |
| 7090 | klass_->SetVTable(vtable.Ptr()); |
| 7091 | return vtable; |
| 7092 | } |
| 7093 | |
| 7094 | void ClassLinker::LinkInterfaceMethodsHelper::UpdateIfTable(Handle<mirror::IfTable> iftable) { |
| 7095 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 7096 | const size_t ifcount = klass_->GetIfTableCount(); |
| 7097 | // Go fix up all the stale iftable pointers. |
| 7098 | for (size_t i = 0; i < ifcount; ++i) { |
| 7099 | for (size_t j = 0, count = iftable->GetMethodArrayCount(i); j < count; ++j) { |
| 7100 | auto* method_array = iftable->GetMethodArray(i); |
| 7101 | auto* m = method_array->GetElementPtrSize<ArtMethod*>(j, pointer_size); |
| 7102 | DCHECK(m != nullptr) << klass_->PrettyClass(); |
| 7103 | auto it = move_table_.find(m); |
| 7104 | if (it != move_table_.end()) { |
| 7105 | auto* new_m = it->second; |
| 7106 | DCHECK(new_m != nullptr) << klass_->PrettyClass(); |
| 7107 | method_array->SetElementPtrSize(j, new_m, pointer_size); |
| 7108 | } |
| 7109 | } |
| 7110 | } |
| 7111 | } |
| 7112 | |
| 7113 | void ClassLinker::LinkInterfaceMethodsHelper::UpdateIMT(ArtMethod** out_imt) { |
| 7114 | // Fix up IMT next. |
| 7115 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 7116 | auto it = move_table_.find(out_imt[i]); |
| 7117 | if (it != move_table_.end()) { |
| 7118 | out_imt[i] = it->second; |
| 7119 | } |
| 7120 | } |
| 7121 | } |
| 7122 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7123 | // TODO This method needs to be split up into several smaller methods. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7124 | bool ClassLinker::LinkInterfaceMethods( |
| 7125 | Thread* self, |
| 7126 | Handle<mirror::Class> klass, |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7127 | const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7128 | bool* out_new_conflict, |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7129 | ArtMethod** out_imt) { |
| 7130 | StackHandleScope<3> hs(self); |
| 7131 | Runtime* const runtime = Runtime::Current(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7132 | |
| 7133 | const bool is_interface = klass->IsInterface(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7134 | const bool has_superclass = klass->HasSuperClass(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7135 | const bool fill_tables = !is_interface; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7136 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7137 | const size_t ifcount = klass->GetIfTableCount(); |
| 7138 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7139 | Handle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7140 | |
| 7141 | MutableHandle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
| 7142 | ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod(); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7143 | ArtMethod* const imt_conflict_method = runtime->GetImtConflictMethod(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7144 | // Copy the IMT from the super class if possible. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7145 | const bool extend_super_iftable = has_superclass; |
| 7146 | if (has_superclass && fill_tables) { |
| 7147 | FillImtFromSuperClass(klass, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7148 | unimplemented_method, |
| 7149 | imt_conflict_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7150 | out_new_conflict, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7151 | out_imt); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7152 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7153 | // Allocate method arrays before since we don't want miss visiting miranda method roots due to |
| 7154 | // thread suspension. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7155 | if (fill_tables) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7156 | if (!AllocateIfTableMethodArrays(self, klass, iftable)) { |
| 7157 | return false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7158 | } |
| 7159 | } |
| 7160 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7161 | LinkInterfaceMethodsHelper helper(this, klass, self, runtime); |
| 7162 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7163 | auto* old_cause = self->StartAssertNoThreadSuspension( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7164 | "Copying ArtMethods for LinkInterfaceMethods"); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7165 | // Going in reverse to ensure that we will hit abstract methods that override defaults before the |
| 7166 | // defaults. This means we don't need to do any trickery when creating the Miranda methods, since |
| 7167 | // they will already be null. This has the additional benefit that the declarer of a miranda |
| 7168 | // method will actually declare an abstract method. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7169 | for (size_t i = ifcount; i != 0u; ) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7170 | --i; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7171 | DCHECK_LT(i, ifcount); |
| 7172 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7173 | size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7174 | if (num_methods > 0) { |
| 7175 | StackHandleScope<2> hs2(self); |
| 7176 | const bool is_super = i < super_ifcount; |
| 7177 | const bool super_interface = is_super && extend_super_iftable; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7178 | // We don't actually create or fill these tables for interfaces, we just copy some methods for |
| 7179 | // conflict methods. Just set this as nullptr in those cases. |
| 7180 | Handle<mirror::PointerArray> method_array(fill_tables |
| 7181 | ? hs2.NewHandle(iftable->GetMethodArray(i)) |
| 7182 | : hs2.NewHandle<mirror::PointerArray>(nullptr)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7183 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7184 | ArraySlice<ArtMethod> input_virtual_methods; |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 7185 | ScopedNullHandle<mirror::PointerArray> null_handle; |
| 7186 | Handle<mirror::PointerArray> input_vtable_array(null_handle); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7187 | int32_t input_array_length = 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7188 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7189 | // TODO Cleanup Needed: In the presence of default methods this optimization is rather dirty |
| 7190 | // and confusing. Default methods should always look through all the superclasses |
| 7191 | // because they are the last choice of an implementation. We get around this by looking |
| 7192 | // at the super-classes iftable methods (copied into method_array previously) when we are |
| 7193 | // 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] | 7194 | bool using_virtuals; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7195 | if (super_interface || is_interface) { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7196 | // 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] | 7197 | // whole vtable. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7198 | using_virtuals = true; |
| 7199 | input_virtual_methods = klass->GetDeclaredMethodsSlice(image_pointer_size_); |
| 7200 | input_array_length = input_virtual_methods.size(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7201 | } else { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7202 | // For a new interface, however, we need the whole vtable in case a new |
| 7203 | // interface method is implemented in the whole superclass. |
| 7204 | using_virtuals = false; |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7205 | DCHECK(vtable != nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7206 | input_vtable_array = vtable; |
| 7207 | input_array_length = input_vtable_array->GetLength(); |
| 7208 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7209 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7210 | // For each method in interface |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 7211 | for (size_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7212 | auto* interface_method = iftable->GetInterface(i)->GetVirtualMethod(j, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 7213 | MethodNameAndSignatureComparator interface_name_comparator( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7214 | interface_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 7215 | uint32_t imt_index = ImTable::GetImtIndex(interface_method); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7216 | ArtMethod** imt_ptr = &out_imt[imt_index]; |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7217 | // For each method listed in the interface's method list, find the |
| 7218 | // matching method in our class's method list. We want to favor the |
| 7219 | // subclass over the superclass, which just requires walking |
| 7220 | // back from the end of the vtable. (This only matters if the |
| 7221 | // superclass defines a private method and this class redefines |
| 7222 | // it -- otherwise it would use the same vtable slot. In .dex files |
| 7223 | // those don't end up in the virtual method table, so it shouldn't |
| 7224 | // matter which direction we go. We walk it backward anyway.) |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7225 | // |
| 7226 | // To find defaults we need to do the same but also go over interfaces. |
| 7227 | bool found_impl = false; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7228 | ArtMethod* vtable_impl = nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7229 | for (int32_t k = input_array_length - 1; k >= 0; --k) { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7230 | ArtMethod* vtable_method = using_virtuals ? |
| 7231 | &input_virtual_methods[k] : |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7232 | input_vtable_array->GetElementPtrSize<ArtMethod*>(k, image_pointer_size_); |
| 7233 | ArtMethod* vtable_method_for_name_comparison = |
| 7234 | vtable_method->GetInterfaceMethodIfProxy(image_pointer_size_); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 7235 | if (interface_name_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 7236 | vtable_method_for_name_comparison)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7237 | if (!vtable_method->IsAbstract() && !vtable_method->IsPublic()) { |
Mathieu Chartier | 4d122c1 | 2015-06-17 14:14:36 -0700 | [diff] [blame] | 7238 | // Must do EndAssertNoThreadSuspension before throw since the throw can cause |
| 7239 | // allocations. |
| 7240 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7241 | ThrowIllegalAccessError(klass.Get(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 7242 | "Method '%s' implementing interface method '%s' is not public", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7243 | vtable_method->PrettyMethod().c_str(), |
| 7244 | interface_method->PrettyMethod().c_str()); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7245 | return false; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7246 | } else if (UNLIKELY(vtable_method->IsOverridableByDefaultMethod())) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7247 | // We might have a newer, better, default method for this, so we just skip it. If we |
| 7248 | // are still using this we will select it again when scanning for default methods. To |
| 7249 | // obviate the need to copy the method again we will make a note that we already found |
| 7250 | // a default here. |
| 7251 | // TODO This should be much cleaner. |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7252 | vtable_impl = vtable_method; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7253 | break; |
| 7254 | } else { |
| 7255 | found_impl = true; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7256 | if (LIKELY(fill_tables)) { |
| 7257 | method_array->SetElementPtrSize(j, vtable_method, image_pointer_size_); |
| 7258 | // Place method in imt if entry is empty, place conflict otherwise. |
| 7259 | SetIMTRef(unimplemented_method, |
| 7260 | imt_conflict_method, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7261 | vtable_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7262 | /*out*/out_new_conflict, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7263 | /*out*/imt_ptr); |
| 7264 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7265 | break; |
| 7266 | } |
| 7267 | } |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7268 | } |
| 7269 | // Continue on to the next method if we are done. |
| 7270 | if (LIKELY(found_impl)) { |
| 7271 | continue; |
| 7272 | } else if (LIKELY(super_interface)) { |
| 7273 | // Don't look for a default implementation when the super-method is implemented directly |
| 7274 | // by the class. |
| 7275 | // |
| 7276 | // See if we can use the superclasses method and skip searching everything else. |
| 7277 | // Note: !found_impl && super_interface |
| 7278 | CHECK(extend_super_iftable); |
| 7279 | // If this is a super_interface method it is possible we shouldn't override it because a |
| 7280 | // superclass could have implemented it directly. We get the method the superclass used |
| 7281 | // to implement this to know if we can override it with a default method. Doing this is |
| 7282 | // safe since we know that the super_iftable is filled in so we can simply pull it from |
| 7283 | // there. We don't bother if this is not a super-classes interface since in that case we |
| 7284 | // have scanned the entire vtable anyway and would have found it. |
| 7285 | // TODO This is rather dirty but it is faster than searching through the entire vtable |
| 7286 | // every time. |
| 7287 | ArtMethod* supers_method = |
| 7288 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 7289 | DCHECK(supers_method != nullptr); |
| 7290 | DCHECK(interface_name_comparator.HasSameNameAndSignature(supers_method)); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7291 | if (LIKELY(!supers_method->IsOverridableByDefaultMethod())) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7292 | // The method is not overridable by a default method (i.e. it is directly implemented |
| 7293 | // in some class). Therefore move onto the next interface method. |
| 7294 | continue; |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 7295 | } else { |
| 7296 | // If the super-classes method is override-able by a default method we need to keep |
| 7297 | // track of it since though it is override-able it is not guaranteed to be 'overridden'. |
| 7298 | // 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] | 7299 | // to the vtable twice, causing corruption (vtable entries having inconsistent and |
| 7300 | // illegal states, incorrect vtable size, and incorrect or inconsistent iftable entries) |
| 7301 | // in this class and any subclasses. |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 7302 | DCHECK(vtable_impl == nullptr || vtable_impl == supers_method) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7303 | << "vtable_impl was " << ArtMethod::PrettyMethod(vtable_impl) |
| 7304 | << " and not 'nullptr' or " |
| 7305 | << supers_method->PrettyMethod() |
| 7306 | << " as expected. IFTable appears to be corrupt!"; |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 7307 | vtable_impl = supers_method; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7308 | } |
| 7309 | } |
| 7310 | // 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] | 7311 | ArtMethod* current_method = helper.FindMethod(interface_method, |
| 7312 | interface_name_comparator, |
| 7313 | vtable_impl); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7314 | if (LIKELY(fill_tables)) { |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 7315 | if (current_method == nullptr && !super_interface) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7316 | // We could not find an implementation for this method and since it is a brand new |
| 7317 | // interface we searched the entire vtable (and all default methods) for an |
| 7318 | // 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] | 7319 | current_method = helper.GetOrCreateMirandaMethod(interface_method, |
| 7320 | interface_name_comparator); |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 7321 | } |
| 7322 | |
| 7323 | if (current_method != nullptr) { |
| 7324 | // We found a default method implementation. Record it in the iftable and IMT. |
| 7325 | method_array->SetElementPtrSize(j, current_method, image_pointer_size_); |
| 7326 | SetIMTRef(unimplemented_method, |
| 7327 | imt_conflict_method, |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 7328 | current_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7329 | /*out*/out_new_conflict, |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 7330 | /*out*/imt_ptr); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7331 | } |
| 7332 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7333 | } // For each method in interface end. |
| 7334 | } // if (num_methods > 0) |
| 7335 | } // For each interface. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7336 | // TODO don't extend virtuals of interface unless necessary (when is it?). |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7337 | if (helper.HasNewVirtuals()) { |
| 7338 | LengthPrefixedArray<ArtMethod>* old_methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr; |
| 7339 | helper.ReallocMethods(); // No return value to check. Native allocation failure aborts. |
| 7340 | LengthPrefixedArray<ArtMethod>* methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr; |
| 7341 | |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 7342 | // 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] | 7343 | // suspension assert. |
| 7344 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 7345 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7346 | if (fill_tables) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7347 | vtable.Assign(helper.UpdateVtable(default_translations, vtable.Get())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7348 | if (UNLIKELY(vtable == nullptr)) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7349 | // The helper has already called self->AssertPendingOOMException(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7350 | return false; |
| 7351 | } |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7352 | helper.UpdateIfTable(iftable); |
| 7353 | helper.UpdateIMT(out_imt); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7354 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7355 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7356 | helper.CheckNoStaleMethodsInDexCache(); |
| 7357 | helper.ClobberOldMethods(old_methods, methods); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7358 | } else { |
| 7359 | self->EndAssertNoThreadSuspension(old_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7360 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7361 | if (kIsDebugBuild && !is_interface) { |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7362 | SanityCheckVTable(self, klass, image_pointer_size_); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 7363 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7364 | return true; |
| 7365 | } |
| 7366 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7367 | bool ClassLinker::LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7368 | CHECK(klass != nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7369 | return LinkFields(self, klass, false, nullptr); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 7370 | } |
| 7371 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7372 | 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] | 7373 | CHECK(klass != nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7374 | return LinkFields(self, klass, true, class_size); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 7375 | } |
| 7376 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7377 | struct LinkFieldsComparator { |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 7378 | explicit LinkFieldsComparator() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 7379 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 7380 | // 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] | 7381 | bool operator()(ArtField* field1, ArtField* field2) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 7382 | NO_THREAD_SAFETY_ANALYSIS { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 7383 | // 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] | 7384 | Primitive::Type type1 = field1->GetTypeAsPrimitiveType(); |
| 7385 | Primitive::Type type2 = field2->GetTypeAsPrimitiveType(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 7386 | if (type1 != type2) { |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 7387 | if (type1 == Primitive::kPrimNot) { |
| 7388 | // Reference always goes first. |
| 7389 | return true; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 7390 | } |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 7391 | if (type2 == Primitive::kPrimNot) { |
| 7392 | // Reference always goes first. |
| 7393 | return false; |
| 7394 | } |
| 7395 | size_t size1 = Primitive::ComponentSize(type1); |
| 7396 | size_t size2 = Primitive::ComponentSize(type2); |
| 7397 | if (size1 != size2) { |
| 7398 | // Larger primitive types go first. |
| 7399 | return size1 > size2; |
| 7400 | } |
| 7401 | // Primitive types differ but sizes match. Arbitrarily order by primitive type. |
| 7402 | return type1 < type2; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7403 | } |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 7404 | // Same basic group? Then sort by dex field index. This is guaranteed to be sorted |
| 7405 | // by name and for equal names by type id index. |
| 7406 | // NOTE: This works also for proxies. Their static fields are assigned appropriate indexes. |
| 7407 | return field1->GetDexFieldIndex() < field2->GetDexFieldIndex(); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7408 | } |
| 7409 | }; |
| 7410 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7411 | bool ClassLinker::LinkFields(Thread* self, |
| 7412 | Handle<mirror::Class> klass, |
| 7413 | bool is_static, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7414 | size_t* class_size) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 7415 | self->AllowThreadSuspension(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7416 | const size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7417 | LengthPrefixedArray<ArtField>* const fields = is_static ? klass->GetSFieldsPtr() : |
| 7418 | klass->GetIFieldsPtr(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7419 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 7420 | // Initialize field_offset |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 7421 | MemberOffset field_offset(0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7422 | if (is_static) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7423 | field_offset = klass->GetFirstReferenceStaticFieldOffsetDuringLinking(image_pointer_size_); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7424 | } else { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7425 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7426 | if (super_class != nullptr) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 7427 | CHECK(super_class->IsResolved()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7428 | << klass->PrettyClass() << " " << super_class->PrettyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7429 | field_offset = MemberOffset(super_class->GetObjectSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7430 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7431 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7432 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7433 | CHECK_EQ(num_fields == 0, fields == nullptr) << klass->PrettyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7434 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7435 | // we want a relatively stable order so that adding new fields |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7436 | // minimizes disruption of C++ version such as Class and Method. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7437 | // |
| 7438 | // The overall sort order order is: |
| 7439 | // 1) All object reference fields, sorted alphabetically. |
| 7440 | // 2) All java long (64-bit) integer fields, sorted alphabetically. |
| 7441 | // 3) All java double (64-bit) floating point fields, sorted alphabetically. |
| 7442 | // 4) All java int (32-bit) integer fields, sorted alphabetically. |
| 7443 | // 5) All java float (32-bit) floating point fields, sorted alphabetically. |
| 7444 | // 6) All java char (16-bit) integer fields, sorted alphabetically. |
| 7445 | // 7) All java short (16-bit) integer fields, sorted alphabetically. |
| 7446 | // 8) All java boolean (8-bit) integer fields, sorted alphabetically. |
| 7447 | // 9) All java byte (8-bit) integer fields, sorted alphabetically. |
| 7448 | // |
| 7449 | // Once the fields are sorted in this order we will attempt to fill any gaps that might be present |
| 7450 | // 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] | 7451 | std::deque<ArtField*> grouped_and_sorted_fields; |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 7452 | const char* old_no_suspend_cause = self->StartAssertNoThreadSuspension( |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 7453 | "Naked ArtField references in deque"); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7454 | for (size_t i = 0; i < num_fields; i++) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7455 | grouped_and_sorted_fields.push_back(&fields->At(i)); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7456 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 7457 | std::sort(grouped_and_sorted_fields.begin(), grouped_and_sorted_fields.end(), |
| 7458 | LinkFieldsComparator()); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7459 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7460 | // References should be at the front. |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7461 | size_t current_field = 0; |
| 7462 | size_t num_reference_fields = 0; |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7463 | FieldGaps gaps; |
| 7464 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7465 | for (; current_field < num_fields; current_field++) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7466 | ArtField* field = grouped_and_sorted_fields.front(); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 7467 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 7468 | bool isPrimitive = type != Primitive::kPrimNot; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7469 | if (isPrimitive) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 7470 | break; // past last reference, move on to the next phase |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7471 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7472 | if (UNLIKELY(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>( |
| 7473 | field_offset.Uint32Value()))) { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7474 | MemberOffset old_offset = field_offset; |
| 7475 | field_offset = MemberOffset(RoundUp(field_offset.Uint32Value(), 4)); |
| 7476 | AddFieldGap(old_offset.Uint32Value(), field_offset.Uint32Value(), &gaps); |
| 7477 | } |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 7478 | DCHECK_ALIGNED(field_offset.Uint32Value(), sizeof(mirror::HeapReference<mirror::Object>)); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7479 | grouped_and_sorted_fields.pop_front(); |
| 7480 | num_reference_fields++; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7481 | field->SetOffset(field_offset); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7482 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 7483 | sizeof(mirror::HeapReference<mirror::Object>)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7484 | } |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7485 | // Gaps are stored as a max heap which means that we must shuffle from largest to smallest |
| 7486 | // otherwise we could end up with suboptimal gap fills. |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7487 | ShuffleForward<8>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 7488 | ShuffleForward<4>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 7489 | ShuffleForward<2>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 7490 | ShuffleForward<1>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 7491 | CHECK(grouped_and_sorted_fields.empty()) << "Missed " << grouped_and_sorted_fields.size() << |
| 7492 | " fields."; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 7493 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7494 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7495 | // 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] | 7496 | if (!is_static && klass->DescriptorEquals("Ljava/lang/ref/Reference;")) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7497 | // We know there are no non-reference fields in the Reference classes, and we know |
| 7498 | // that 'referent' is alphabetically last, so this is easy... |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7499 | CHECK_EQ(num_reference_fields, num_fields) << klass->PrettyClass(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7500 | CHECK_STREQ(fields->At(num_fields - 1).GetName(), "referent") |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7501 | << klass->PrettyClass(); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7502 | --num_reference_fields; |
| 7503 | } |
| 7504 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 7505 | size_t size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7506 | // Update klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7507 | if (is_static) { |
| 7508 | klass->SetNumReferenceStaticFields(num_reference_fields); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 7509 | *class_size = size; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7510 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7511 | klass->SetNumReferenceInstanceFields(num_reference_fields); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7512 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 7513 | if (num_reference_fields == 0 || super_class == nullptr) { |
| 7514 | // 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] | 7515 | // super_class is null iff the class is java.lang.Object. |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 7516 | if (super_class == nullptr || |
| 7517 | (super_class->GetClassFlags() & mirror::kClassFlagNoReferenceFields) != 0) { |
| 7518 | klass->SetClassFlags(klass->GetClassFlags() | mirror::kClassFlagNoReferenceFields); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 7519 | } |
| 7520 | } |
| 7521 | if (kIsDebugBuild) { |
| 7522 | DCHECK_EQ(super_class == nullptr, klass->DescriptorEquals("Ljava/lang/Object;")); |
| 7523 | size_t total_reference_instance_fields = 0; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7524 | ObjPtr<mirror::Class> cur_super = klass.Get(); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 7525 | while (cur_super != nullptr) { |
| 7526 | total_reference_instance_fields += cur_super->NumReferenceInstanceFieldsDuringLinking(); |
| 7527 | cur_super = cur_super->GetSuperClass(); |
| 7528 | } |
| 7529 | if (super_class == nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7530 | CHECK_EQ(total_reference_instance_fields, 1u) << klass->PrettyDescriptor(); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 7531 | } else { |
| 7532 | // Check that there is at least num_reference_fields other than Object.class. |
| 7533 | CHECK_GE(total_reference_instance_fields, 1u + num_reference_fields) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7534 | << klass->PrettyClass(); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 7535 | } |
| 7536 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7537 | if (!klass->IsVariableSize()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7538 | std::string temp; |
| 7539 | DCHECK_GE(size, sizeof(mirror::Object)) << klass->GetDescriptor(&temp); |
| 7540 | size_t previous_size = klass->GetObjectSize(); |
| 7541 | if (previous_size != 0) { |
| 7542 | // Make sure that we didn't originally have an incorrect size. |
| 7543 | CHECK_EQ(previous_size, size) << klass->GetDescriptor(&temp); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 7544 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7545 | klass->SetObjectSize(size); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7546 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7547 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7548 | |
| 7549 | if (kIsDebugBuild) { |
| 7550 | // Make sure that the fields array is ordered by name but all reference |
| 7551 | // offsets are at the beginning as far as alignment allows. |
| 7552 | MemberOffset start_ref_offset = is_static |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7553 | ? klass->GetFirstReferenceStaticFieldOffsetDuringLinking(image_pointer_size_) |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7554 | : klass->GetFirstReferenceInstanceFieldOffset(); |
| 7555 | MemberOffset end_ref_offset(start_ref_offset.Uint32Value() + |
| 7556 | num_reference_fields * |
| 7557 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 7558 | MemberOffset current_ref_offset = start_ref_offset; |
| 7559 | for (size_t i = 0; i < num_fields; i++) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7560 | ArtField* field = &fields->At(i); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7561 | VLOG(class_linker) << "LinkFields: " << (is_static ? "static" : "instance") |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7562 | << " class=" << klass->PrettyClass() << " field=" << field->PrettyField() |
| 7563 | << " offset=" << field->GetOffsetDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7564 | if (i != 0) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7565 | ArtField* const prev_field = &fields->At(i - 1); |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 7566 | // NOTE: The field names can be the same. This is not possible in the Java language |
| 7567 | // 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] | 7568 | DCHECK_LE(strcmp(prev_field->GetName(), field->GetName()), 0); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7569 | } |
| 7570 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 7571 | bool is_primitive = type != Primitive::kPrimNot; |
| 7572 | if (klass->DescriptorEquals("Ljava/lang/ref/Reference;") && |
| 7573 | strcmp("referent", field->GetName()) == 0) { |
| 7574 | is_primitive = true; // We lied above, so we have to expect a lie here. |
| 7575 | } |
| 7576 | MemberOffset offset = field->GetOffsetDuringLinking(); |
| 7577 | if (is_primitive) { |
| 7578 | if (offset.Uint32Value() < end_ref_offset.Uint32Value()) { |
| 7579 | // Shuffled before references. |
| 7580 | size_t type_size = Primitive::ComponentSize(type); |
| 7581 | CHECK_LT(type_size, sizeof(mirror::HeapReference<mirror::Object>)); |
| 7582 | CHECK_LT(offset.Uint32Value(), start_ref_offset.Uint32Value()); |
| 7583 | CHECK_LE(offset.Uint32Value() + type_size, start_ref_offset.Uint32Value()); |
| 7584 | CHECK(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(offset.Uint32Value())); |
| 7585 | } |
| 7586 | } else { |
| 7587 | CHECK_EQ(current_ref_offset.Uint32Value(), offset.Uint32Value()); |
| 7588 | current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() + |
| 7589 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 7590 | } |
| 7591 | } |
| 7592 | CHECK_EQ(current_ref_offset.Uint32Value(), end_ref_offset.Uint32Value()); |
| 7593 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7594 | return true; |
| 7595 | } |
| 7596 | |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7597 | // Set the bitmap of reference instance field offsets. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7598 | void ClassLinker::CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7599 | uint32_t reference_offsets = 0; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7600 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7601 | // 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] | 7602 | if (super_class != nullptr) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7603 | reference_offsets = super_class->GetReferenceInstanceOffsets(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7604 | // Compute reference offsets unless our superclass overflowed. |
| 7605 | if (reference_offsets != mirror::Class::kClassWalkSuper) { |
| 7606 | size_t num_reference_fields = klass->NumReferenceInstanceFieldsDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7607 | if (num_reference_fields != 0u) { |
| 7608 | // All of the fields that contain object references are guaranteed be grouped in memory |
| 7609 | // starting at an appropriately aligned address after super class object data. |
| 7610 | uint32_t start_offset = RoundUp(super_class->GetObjectSize(), |
| 7611 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 7612 | uint32_t start_bit = (start_offset - mirror::kObjectHeaderSize) / |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7613 | sizeof(mirror::HeapReference<mirror::Object>); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7614 | if (start_bit + num_reference_fields > 32) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7615 | reference_offsets = mirror::Class::kClassWalkSuper; |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7616 | } else { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7617 | reference_offsets |= (0xffffffffu << start_bit) & |
| 7618 | (0xffffffffu >> (32 - (start_bit + num_reference_fields))); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7619 | } |
| 7620 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 7621 | } |
| 7622 | } |
Mingyao Yang | faff0f0 | 2014-09-10 12:03:22 -0700 | [diff] [blame] | 7623 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7624 | } |
| 7625 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7626 | mirror::String* ClassLinker::ResolveString(const DexFile& dex_file, |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 7627 | dex::StringIndex string_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7628 | Handle<mirror::DexCache> dex_cache) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7629 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 7630 | Thread::PoisonObjectPointersIfDebug(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7631 | ObjPtr<mirror::String> resolved = dex_cache->GetResolvedString(string_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7632 | if (resolved != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7633 | return resolved.Ptr(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7634 | } |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 7635 | uint32_t utf16_length; |
| 7636 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7637 | ObjPtr<mirror::String> string = intern_table_->InternStrong(utf16_length, utf8_data); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 7638 | if (string != nullptr) { |
| 7639 | dex_cache->SetResolvedString(string_idx, string); |
| 7640 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7641 | return string.Ptr(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7642 | } |
| 7643 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7644 | mirror::String* ClassLinker::LookupString(const DexFile& dex_file, |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 7645 | dex::StringIndex string_idx, |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 7646 | ObjPtr<mirror::DexCache> dex_cache) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7647 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7648 | ObjPtr<mirror::String> resolved = dex_cache->GetResolvedString(string_idx); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7649 | if (resolved != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7650 | return resolved.Ptr(); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7651 | } |
| 7652 | uint32_t utf16_length; |
| 7653 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 7654 | ObjPtr<mirror::String> string = |
| 7655 | intern_table_->LookupStrong(Thread::Current(), utf16_length, utf8_data); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7656 | if (string != nullptr) { |
| 7657 | dex_cache->SetResolvedString(string_idx, string); |
| 7658 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7659 | return string.Ptr(); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7660 | } |
| 7661 | |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 7662 | ObjPtr<mirror::Class> ClassLinker::LookupResolvedType(const DexFile& dex_file, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 7663 | dex::TypeIndex type_idx, |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 7664 | ObjPtr<mirror::DexCache> dex_cache, |
| 7665 | ObjPtr<mirror::ClassLoader> class_loader) { |
| 7666 | ObjPtr<mirror::Class> type = dex_cache->GetResolvedType(type_idx); |
| 7667 | if (type == nullptr) { |
| 7668 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
| 7669 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
| 7670 | if (descriptor[1] == '\0') { |
| 7671 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 7672 | // for primitive classes that aren't backed by dex files. |
| 7673 | type = FindPrimitiveClass(descriptor[0]); |
| 7674 | } else { |
| 7675 | Thread* const self = Thread::Current(); |
| 7676 | DCHECK(self != nullptr); |
| 7677 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
| 7678 | // Find the class in the loaded classes table. |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 7679 | type = LookupClass(self, descriptor, hash, class_loader.Ptr()); |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 7680 | } |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 7681 | if (type != nullptr) { |
| 7682 | if (type->IsResolved()) { |
| 7683 | dex_cache->SetResolvedType(type_idx, type); |
| 7684 | } else { |
| 7685 | type = nullptr; |
| 7686 | } |
| 7687 | } |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 7688 | } |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 7689 | DCHECK(type == nullptr || type->IsResolved()); |
| 7690 | return type; |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 7691 | } |
| 7692 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7693 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 7694 | dex::TypeIndex type_idx, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7695 | ObjPtr<mirror::Class> referrer) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 7696 | StackHandleScope<2> hs(Thread::Current()); |
| 7697 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 7698 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 7699 | return ResolveType(dex_file, type_idx, dex_cache, class_loader); |
| 7700 | } |
| 7701 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7702 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 7703 | dex::TypeIndex type_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7704 | Handle<mirror::DexCache> dex_cache, |
| 7705 | Handle<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7706 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 7707 | Thread::PoisonObjectPointersIfDebug(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7708 | ObjPtr<mirror::Class> resolved = dex_cache->GetResolvedType(type_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7709 | if (resolved == nullptr) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 7710 | // TODO: Avoid this lookup as it duplicates work done in FindClass(). It is here |
| 7711 | // as a workaround for FastNative JNI to avoid AssertNoPendingException() when |
| 7712 | // trying to resolve annotations while an exception may be pending. Bug: 34659969 |
| 7713 | resolved = LookupResolvedType(dex_file, type_idx, dex_cache.Get(), class_loader.Get()); |
| 7714 | } |
| 7715 | if (resolved == nullptr) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 7716 | Thread* self = Thread::Current(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 7717 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 7718 | resolved = FindClass(self, descriptor, class_loader); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7719 | if (resolved != nullptr) { |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 7720 | // TODO: we used to throw here if resolved's class loader was not the |
| 7721 | // boot class loader. This was to permit different classes with the |
| 7722 | // same name to be loaded simultaneously by different loaders |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7723 | dex_cache->SetResolvedType(type_idx, resolved); |
| 7724 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 7725 | CHECK(self->IsExceptionPending()) |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 7726 | << "Expected pending exception for failed resolution of: " << descriptor; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 7727 | // Convert a ClassNotFoundException to a NoClassDefFoundError. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 7728 | StackHandleScope<1> hs(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 7729 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 7730 | if (cause->InstanceOf(GetClassRoot(kJavaLangClassNotFoundException))) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7731 | DCHECK(resolved == nullptr); // No Handle needed to preserve resolved. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 7732 | self->ClearException(); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 7733 | ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 7734 | self->GetException()->SetCause(cause.Get()); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 7735 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7736 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7737 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 7738 | DCHECK((resolved == nullptr) || resolved->IsResolved()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7739 | << resolved->PrettyDescriptor() << " " << resolved->GetStatus(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7740 | return resolved.Ptr(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7741 | } |
| 7742 | |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 7743 | template <ClassLinker::ResolveMode kResolveMode> |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7744 | ArtMethod* ClassLinker::ResolveMethod(const DexFile& dex_file, |
| 7745 | uint32_t method_idx, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7746 | Handle<mirror::DexCache> dex_cache, |
| 7747 | Handle<mirror::ClassLoader> class_loader, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7748 | ArtMethod* referrer, |
| 7749 | InvokeType type) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7750 | DCHECK(dex_cache != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7751 | DCHECK(referrer == nullptr || !referrer->IsProxyMethod()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7752 | // Check for hit in the dex cache. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7753 | PointerSize pointer_size = image_pointer_size_; |
| 7754 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, pointer_size); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 7755 | Thread::PoisonObjectPointersIfDebug(); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 7756 | DCHECK(resolved == nullptr || !resolved->IsRuntimeMethod()); |
| 7757 | bool valid_dex_cache_method = resolved != nullptr; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7758 | if (kResolveMode == ResolveMode::kNoChecks && valid_dex_cache_method) { |
| 7759 | // We have a valid method from the DexCache and no checks to perform. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7760 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7761 | return resolved; |
| 7762 | } |
| 7763 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7764 | ObjPtr<mirror::Class> klass = nullptr; |
| 7765 | if (valid_dex_cache_method) { |
| 7766 | // We have a valid method from the DexCache but we need to perform ICCE and IAE checks. |
| 7767 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
| 7768 | klass = LookupResolvedType(dex_file, method_id.class_idx_, dex_cache.Get(), class_loader.Get()); |
Mathieu Chartier | f598131 | 2017-09-06 14:17:34 -0700 | [diff] [blame] | 7769 | CHECK(klass != nullptr) << resolved->PrettyMethod() << " " << resolved << " " |
| 7770 | << resolved->GetAccessFlags(); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7771 | } else { |
| 7772 | // The method was not in the DexCache, resolve the declaring class. |
| 7773 | klass = ResolveType(dex_file, method_id.class_idx_, dex_cache, class_loader); |
| 7774 | if (klass == nullptr) { |
| 7775 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 7776 | return nullptr; |
| 7777 | } |
| 7778 | } |
| 7779 | |
| 7780 | // Check if the invoke type matches the class type. |
| 7781 | if (kResolveMode == ResolveMode::kCheckICCEAndIAE && |
| 7782 | CheckInvokeClassMismatch</* kThrow */ true>( |
| 7783 | dex_cache.Get(), type, [klass]() { return klass; })) { |
Elliott Hughes | cc5f9a9 | 2011-09-28 19:17:29 -0700 | [diff] [blame] | 7784 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7785 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7786 | } |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7787 | |
| 7788 | if (!valid_dex_cache_method) { |
| 7789 | // Search for the method using dex_cache and method_idx. The Class::Find*Method() |
| 7790 | // functions can optimize the search if the dex_cache is the same as the DexCache |
| 7791 | // of the class, with fall-back to name and signature search otherwise. |
| 7792 | if (klass->IsInterface()) { |
| 7793 | resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx, pointer_size); |
| 7794 | } else { |
| 7795 | resolved = klass->FindClassMethod(dex_cache.Get(), method_idx, pointer_size); |
| 7796 | } |
| 7797 | DCHECK(resolved == nullptr || resolved->GetDeclaringClassUnchecked() != nullptr); |
| 7798 | if (resolved != nullptr) { |
| 7799 | // Be a good citizen and update the dex cache to speed subsequent calls. |
| 7800 | dex_cache->SetResolvedMethod(method_idx, resolved, pointer_size); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 7801 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7802 | } |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7803 | |
| 7804 | // Note: We can check for IllegalAccessError only if we have a referrer. |
| 7805 | if (kResolveMode == ResolveMode::kCheckICCEAndIAE && resolved != nullptr && referrer != nullptr) { |
| 7806 | ObjPtr<mirror::Class> methods_class = resolved->GetDeclaringClass(); |
| 7807 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 7808 | if (!referring_class->CheckResolvedMethodAccess(methods_class, |
| 7809 | resolved, |
| 7810 | dex_cache.Get(), |
| 7811 | method_idx, |
| 7812 | type)) { |
| 7813 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 7814 | return nullptr; |
| 7815 | } |
| 7816 | } |
| 7817 | |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7818 | // If we found a method, check for incompatible class changes. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7819 | if (LIKELY(resolved != nullptr) && |
| 7820 | LIKELY(kResolveMode == ResolveMode::kNoChecks || |
| 7821 | !resolved->CheckIncompatibleClassChange(type))) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7822 | return resolved; |
| 7823 | } else { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7824 | // If we had a method, or if we can find one with another lookup type, |
| 7825 | // it's an incompatible-class-change error. |
| 7826 | if (resolved == nullptr) { |
| 7827 | if (klass->IsInterface()) { |
| 7828 | resolved = klass->FindClassMethod(dex_cache.Get(), method_idx, pointer_size); |
| 7829 | } else { |
| 7830 | // If there was an interface method with the same signature, |
| 7831 | // we would have found it also in the "copied" methods. |
| 7832 | DCHECK(klass->FindInterfaceMethod(dex_cache.Get(), method_idx, pointer_size) == nullptr); |
| 7833 | } |
| 7834 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7835 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7836 | ThrowIncompatibleClassChangeError(type, resolved->GetInvokeType(), resolved, referrer); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7837 | } else { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7838 | // We failed to find the method (using all lookup types), so throw a NoSuchMethodError. |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7839 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
| 7840 | const Signature signature = dex_file.GetMethodSignature(method_id); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7841 | ThrowNoSuchMethodError(type, klass, name, signature); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7842 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 7843 | Thread::Current()->AssertPendingException(); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7844 | return nullptr; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7845 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7846 | } |
| 7847 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 7848 | ArtMethod* ClassLinker::ResolveMethodWithoutInvokeType(const DexFile& dex_file, |
| 7849 | uint32_t method_idx, |
| 7850 | Handle<mirror::DexCache> dex_cache, |
| 7851 | Handle<mirror::ClassLoader> class_loader) { |
| 7852 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 7853 | Thread::PoisonObjectPointersIfDebug(); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 7854 | if (resolved != nullptr) { |
| 7855 | DCHECK(!resolved->IsRuntimeMethod()); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 7856 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
| 7857 | return resolved; |
| 7858 | } |
| 7859 | // Fail, get the declaring class. |
| 7860 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7861 | ObjPtr<mirror::Class> klass = |
| 7862 | ResolveType(dex_file, method_id.class_idx_, dex_cache, class_loader); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 7863 | if (klass == nullptr) { |
| 7864 | Thread::Current()->AssertPendingException(); |
| 7865 | return nullptr; |
| 7866 | } |
| 7867 | if (klass->IsInterface()) { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7868 | resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 7869 | } else { |
| 7870 | resolved = klass->FindClassMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 7871 | } |
| 7872 | |
| 7873 | return resolved; |
| 7874 | } |
| 7875 | |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 7876 | ArtField* ClassLinker::LookupResolvedField(uint32_t field_idx, |
| 7877 | ObjPtr<mirror::DexCache> dex_cache, |
| 7878 | ObjPtr<mirror::ClassLoader> class_loader, |
| 7879 | bool is_static) { |
| 7880 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 7881 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
| 7882 | ObjPtr<mirror::Class> klass = dex_cache->GetResolvedType(field_id.class_idx_); |
| 7883 | if (klass == nullptr) { |
| 7884 | klass = LookupResolvedType(dex_file, field_id.class_idx_, dex_cache, class_loader); |
| 7885 | } |
| 7886 | if (klass == nullptr) { |
| 7887 | // The class has not been resolved yet, so the field is also unresolved. |
| 7888 | return nullptr; |
| 7889 | } |
| 7890 | DCHECK(klass->IsResolved()); |
| 7891 | Thread* self = is_static ? Thread::Current() : nullptr; |
| 7892 | |
| 7893 | // First try to find a field declared directly by `klass` by the field index. |
| 7894 | ArtField* resolved_field = is_static |
| 7895 | ? mirror::Class::FindStaticField(self, klass, dex_cache, field_idx) |
| 7896 | : klass->FindInstanceField(dex_cache, field_idx); |
| 7897 | |
| 7898 | if (resolved_field == nullptr) { |
| 7899 | // If not found in `klass` by field index, search the class hierarchy using the name and type. |
| 7900 | const char* name = dex_file.GetFieldName(field_id); |
| 7901 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 7902 | resolved_field = is_static |
| 7903 | ? mirror::Class::FindStaticField(self, klass, name, type) |
| 7904 | : klass->FindInstanceField(name, type); |
| 7905 | } |
| 7906 | |
| 7907 | if (resolved_field != nullptr) { |
| 7908 | dex_cache->SetResolvedField(field_idx, resolved_field, image_pointer_size_); |
| 7909 | } |
| 7910 | return resolved_field; |
| 7911 | } |
| 7912 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7913 | ArtField* ClassLinker::ResolveField(const DexFile& dex_file, |
| 7914 | uint32_t field_idx, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7915 | Handle<mirror::DexCache> dex_cache, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7916 | Handle<mirror::ClassLoader> class_loader, |
| 7917 | bool is_static) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7918 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7919 | ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 7920 | Thread::PoisonObjectPointersIfDebug(); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 7921 | if (resolved != nullptr) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7922 | return resolved; |
| 7923 | } |
| 7924 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 7925 | Thread* const self = Thread::Current(); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 7926 | ObjPtr<mirror::Class> klass = ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader); |
| 7927 | if (klass == nullptr) { |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 7928 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 7929 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7930 | } |
| 7931 | |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 7932 | if (is_static) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 7933 | resolved = mirror::Class::FindStaticField(self, klass, dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 7934 | } else { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 7935 | resolved = klass->FindInstanceField(dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 7936 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 7937 | |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 7938 | if (resolved == nullptr) { |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 7939 | const char* name = dex_file.GetFieldName(field_id); |
| 7940 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 7941 | if (is_static) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 7942 | resolved = mirror::Class::FindStaticField(self, klass, name, type); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 7943 | } else { |
| 7944 | resolved = klass->FindInstanceField(name, type); |
| 7945 | } |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 7946 | if (resolved == nullptr) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 7947 | ThrowNoSuchFieldError(is_static ? "static " : "instance ", klass, type, name); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7948 | return nullptr; |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 7949 | } |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 7950 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7951 | dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 7952 | return resolved; |
| 7953 | } |
| 7954 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7955 | ArtField* ClassLinker::ResolveFieldJLS(const DexFile& dex_file, |
| 7956 | uint32_t field_idx, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7957 | Handle<mirror::DexCache> dex_cache, |
| 7958 | Handle<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7959 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7960 | ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 7961 | Thread::PoisonObjectPointersIfDebug(); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 7962 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 7963 | return resolved; |
| 7964 | } |
| 7965 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 7966 | Thread* self = Thread::Current(); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 7967 | ObjPtr<mirror::Class> klass(ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader)); |
| 7968 | if (klass == nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 7969 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7970 | return nullptr; |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 7971 | } |
| 7972 | |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 7973 | StringPiece name(dex_file.GetFieldName(field_id)); |
| 7974 | StringPiece type(dex_file.GetFieldTypeDescriptor(field_id)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 7975 | resolved = mirror::Class::FindField(self, klass, name, type); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7976 | if (resolved != nullptr) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7977 | dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 7978 | } else { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 7979 | ThrowNoSuchFieldError("", klass, type, name); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7980 | } |
| 7981 | return resolved; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 7982 | } |
| 7983 | |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 7984 | mirror::MethodType* ClassLinker::ResolveMethodType(const DexFile& dex_file, |
| 7985 | uint32_t proto_idx, |
| 7986 | Handle<mirror::DexCache> dex_cache, |
| 7987 | Handle<mirror::ClassLoader> class_loader) { |
| 7988 | DCHECK(Runtime::Current()->IsMethodHandlesEnabled()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7989 | DCHECK(dex_cache != nullptr); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 7990 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7991 | ObjPtr<mirror::MethodType> resolved = dex_cache->GetResolvedMethodType(proto_idx); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 7992 | if (resolved != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7993 | return resolved.Ptr(); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 7994 | } |
| 7995 | |
| 7996 | Thread* const self = Thread::Current(); |
| 7997 | StackHandleScope<4> hs(self); |
| 7998 | |
| 7999 | // First resolve the return type. |
| 8000 | const DexFile::ProtoId& proto_id = dex_file.GetProtoId(proto_idx); |
| 8001 | Handle<mirror::Class> return_type(hs.NewHandle( |
| 8002 | ResolveType(dex_file, proto_id.return_type_idx_, dex_cache, class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8003 | if (return_type == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8004 | DCHECK(self->IsExceptionPending()); |
| 8005 | return nullptr; |
| 8006 | } |
| 8007 | |
| 8008 | // Then resolve the argument types. |
| 8009 | // |
| 8010 | // TODO: Is there a better way to figure out the number of method arguments |
| 8011 | // other than by looking at the shorty ? |
| 8012 | const size_t num_method_args = strlen(dex_file.StringDataByIdx(proto_id.shorty_idx_)) - 1; |
| 8013 | |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 8014 | ObjPtr<mirror::Class> class_type = mirror::Class::GetJavaLangClass(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8015 | ObjPtr<mirror::Class> array_of_class = FindArrayClass(self, &class_type); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8016 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 8017 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_method_args))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8018 | if (method_params == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8019 | DCHECK(self->IsExceptionPending()); |
| 8020 | return nullptr; |
| 8021 | } |
| 8022 | |
| 8023 | DexFileParameterIterator it(dex_file, proto_id); |
| 8024 | int32_t i = 0; |
| 8025 | MutableHandle<mirror::Class> param_class = hs.NewHandle<mirror::Class>(nullptr); |
| 8026 | for (; it.HasNext(); it.Next()) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 8027 | const dex::TypeIndex type_idx = it.GetTypeIdx(); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8028 | param_class.Assign(ResolveType(dex_file, type_idx, dex_cache, class_loader)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8029 | if (param_class == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8030 | DCHECK(self->IsExceptionPending()); |
| 8031 | return nullptr; |
| 8032 | } |
| 8033 | |
| 8034 | method_params->Set(i++, param_class.Get()); |
| 8035 | } |
| 8036 | |
| 8037 | DCHECK(!it.HasNext()); |
| 8038 | |
| 8039 | Handle<mirror::MethodType> type = hs.NewHandle( |
| 8040 | mirror::MethodType::Create(self, return_type, method_params)); |
| 8041 | dex_cache->SetResolvedMethodType(proto_idx, type.Get()); |
| 8042 | |
| 8043 | return type.Get(); |
| 8044 | } |
| 8045 | |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 8046 | mirror::MethodType* ClassLinker::ResolveMethodType(uint32_t proto_idx, ArtMethod* referrer) { |
| 8047 | Thread* const self = Thread::Current(); |
| 8048 | StackHandleScope<2> hs(self); |
| 8049 | const DexFile* dex_file = referrer->GetDexFile(); |
| 8050 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 8051 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
| 8052 | return ResolveMethodType(*dex_file, proto_idx, dex_cache, class_loader); |
| 8053 | } |
| 8054 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8055 | mirror::MethodHandle* ClassLinker::ResolveMethodHandleForField( |
| 8056 | Thread* self, |
| 8057 | const DexFile::MethodHandleItem& method_handle, |
| 8058 | ArtMethod* referrer) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8059 | DexFile::MethodHandleType handle_type = |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8060 | static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_); |
| 8061 | mirror::MethodHandle::Kind kind; |
| 8062 | bool is_static; |
| 8063 | int32_t num_params; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8064 | switch (handle_type) { |
| 8065 | case DexFile::MethodHandleType::kStaticPut: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 8066 | kind = mirror::MethodHandle::Kind::kStaticPut; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8067 | is_static = true; |
| 8068 | num_params = 1; |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 8069 | break; |
| 8070 | } |
| 8071 | case DexFile::MethodHandleType::kStaticGet: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 8072 | kind = mirror::MethodHandle::Kind::kStaticGet; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8073 | is_static = true; |
| 8074 | num_params = 0; |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 8075 | break; |
| 8076 | } |
| 8077 | case DexFile::MethodHandleType::kInstancePut: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 8078 | kind = mirror::MethodHandle::Kind::kInstancePut; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8079 | is_static = false; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8080 | num_params = 2; |
| 8081 | break; |
| 8082 | } |
| 8083 | case DexFile::MethodHandleType::kInstanceGet: { |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8084 | kind = mirror::MethodHandle::Kind::kInstanceGet; |
| 8085 | is_static = false; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8086 | num_params = 1; |
| 8087 | break; |
| 8088 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8089 | case DexFile::MethodHandleType::kInvokeStatic: |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 8090 | case DexFile::MethodHandleType::kInvokeInstance: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8091 | case DexFile::MethodHandleType::kInvokeConstructor: |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 8092 | case DexFile::MethodHandleType::kInvokeDirect: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8093 | case DexFile::MethodHandleType::kInvokeInterface: |
| 8094 | UNREACHABLE(); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8095 | } |
| 8096 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8097 | ArtField* target_field = |
| 8098 | ResolveField(method_handle.field_or_method_idx_, referrer, is_static); |
| 8099 | if (LIKELY(target_field != nullptr)) { |
| 8100 | ObjPtr<mirror::Class> target_class = target_field->GetDeclaringClass(); |
| 8101 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 8102 | if (UNLIKELY(!referring_class->CanAccessMember(target_class, target_field->GetAccessFlags()))) { |
| 8103 | ThrowIllegalAccessErrorField(referring_class, target_field); |
| 8104 | return nullptr; |
| 8105 | } |
| 8106 | } else { |
| 8107 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 8108 | return nullptr; |
| 8109 | } |
| 8110 | |
| 8111 | StackHandleScope<4> hs(self); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8112 | ObjPtr<mirror::Class> class_type = mirror::Class::GetJavaLangClass(); |
| 8113 | ObjPtr<mirror::Class> array_of_class = FindArrayClass(self, &class_type); |
| 8114 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 8115 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params))); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8116 | if (UNLIKELY(method_params.Get() == nullptr)) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8117 | DCHECK(self->IsExceptionPending()); |
| 8118 | return nullptr; |
| 8119 | } |
| 8120 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8121 | Handle<mirror::Class> constructor_class; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8122 | Handle<mirror::Class> return_type; |
| 8123 | switch (handle_type) { |
| 8124 | case DexFile::MethodHandleType::kStaticPut: { |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 8125 | method_params->Set(0, target_field->GetType<true>()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8126 | return_type = hs.NewHandle(FindPrimitiveClass('V')); |
| 8127 | break; |
| 8128 | } |
| 8129 | case DexFile::MethodHandleType::kStaticGet: { |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 8130 | return_type = hs.NewHandle(target_field->GetType<true>()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8131 | break; |
| 8132 | } |
| 8133 | case DexFile::MethodHandleType::kInstancePut: { |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 8134 | method_params->Set(0, target_field->GetDeclaringClass()); |
| 8135 | method_params->Set(1, target_field->GetType<true>()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8136 | return_type = hs.NewHandle(FindPrimitiveClass('V')); |
| 8137 | break; |
| 8138 | } |
| 8139 | case DexFile::MethodHandleType::kInstanceGet: { |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 8140 | method_params->Set(0, target_field->GetDeclaringClass()); |
| 8141 | return_type = hs.NewHandle(target_field->GetType<true>()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8142 | break; |
| 8143 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8144 | case DexFile::MethodHandleType::kInvokeStatic: |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 8145 | case DexFile::MethodHandleType::kInvokeInstance: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8146 | case DexFile::MethodHandleType::kInvokeConstructor: |
| 8147 | case DexFile::MethodHandleType::kInvokeDirect: |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 8148 | case DexFile::MethodHandleType::kInvokeInterface: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8149 | UNREACHABLE(); |
| 8150 | } |
| 8151 | |
| 8152 | for (int32_t i = 0; i < num_params; ++i) { |
| 8153 | if (UNLIKELY(method_params->Get(i) == nullptr)) { |
| 8154 | DCHECK(self->IsExceptionPending()); |
| 8155 | return nullptr; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8156 | } |
| 8157 | } |
| 8158 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8159 | if (UNLIKELY(return_type.IsNull())) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8160 | DCHECK(self->IsExceptionPending()); |
| 8161 | return nullptr; |
| 8162 | } |
| 8163 | |
| 8164 | Handle<mirror::MethodType> |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8165 | method_type(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params))); |
| 8166 | if (UNLIKELY(method_type.IsNull())) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8167 | DCHECK(self->IsExceptionPending()); |
| 8168 | return nullptr; |
| 8169 | } |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 8170 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8171 | uintptr_t target = reinterpret_cast<uintptr_t>(target_field); |
| 8172 | return mirror::MethodHandleImpl::Create(self, target, kind, method_type); |
| 8173 | } |
| 8174 | |
| 8175 | mirror::MethodHandle* ClassLinker::ResolveMethodHandleForMethod( |
| 8176 | Thread* self, |
| 8177 | const DexFile* const dex_file, |
| 8178 | const DexFile::MethodHandleItem& method_handle, |
| 8179 | ArtMethod* referrer) { |
| 8180 | DexFile::MethodHandleType handle_type = |
| 8181 | static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_); |
| 8182 | mirror::MethodHandle::Kind kind; |
| 8183 | uint32_t receiver_count = 0; |
| 8184 | ArtMethod* target_method = nullptr; |
| 8185 | switch (handle_type) { |
| 8186 | case DexFile::MethodHandleType::kStaticPut: |
| 8187 | case DexFile::MethodHandleType::kStaticGet: |
| 8188 | case DexFile::MethodHandleType::kInstancePut: |
| 8189 | case DexFile::MethodHandleType::kInstanceGet: |
| 8190 | UNREACHABLE(); |
| 8191 | case DexFile::MethodHandleType::kInvokeStatic: { |
| 8192 | kind = mirror::MethodHandle::Kind::kInvokeStatic; |
| 8193 | receiver_count = 0; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8194 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 8195 | method_handle.field_or_method_idx_, |
| 8196 | referrer, |
| 8197 | InvokeType::kStatic); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8198 | break; |
| 8199 | } |
| 8200 | case DexFile::MethodHandleType::kInvokeInstance: { |
| 8201 | kind = mirror::MethodHandle::Kind::kInvokeVirtual; |
| 8202 | receiver_count = 1; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8203 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 8204 | method_handle.field_or_method_idx_, |
| 8205 | referrer, |
| 8206 | InvokeType::kVirtual); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8207 | break; |
| 8208 | } |
| 8209 | case DexFile::MethodHandleType::kInvokeConstructor: { |
| 8210 | // Constructors are currently implemented as a transform. They |
| 8211 | // are special cased later in this method. |
| 8212 | kind = mirror::MethodHandle::Kind::kInvokeTransform; |
| 8213 | receiver_count = 0; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8214 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 8215 | method_handle.field_or_method_idx_, |
| 8216 | referrer, |
| 8217 | InvokeType::kDirect); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8218 | break; |
| 8219 | } |
| 8220 | case DexFile::MethodHandleType::kInvokeDirect: { |
| 8221 | kind = mirror::MethodHandle::Kind::kInvokeDirect; |
| 8222 | receiver_count = 1; |
| 8223 | StackHandleScope<2> hs(self); |
| 8224 | // A constant method handle with type kInvokeDirect can refer to |
| 8225 | // a method that is private or to a method in a super class. To |
| 8226 | // disambiguate the two options, we resolve the method ignoring |
| 8227 | // the invocation type to determine if the method is private. We |
| 8228 | // then resolve again specifying the intended invocation type to |
| 8229 | // force the appropriate checks. |
| 8230 | target_method = ResolveMethodWithoutInvokeType(*dex_file, |
| 8231 | method_handle.field_or_method_idx_, |
| 8232 | hs.NewHandle(referrer->GetDexCache()), |
| 8233 | hs.NewHandle(referrer->GetClassLoader())); |
| 8234 | if (UNLIKELY(target_method == nullptr)) { |
| 8235 | break; |
| 8236 | } |
| 8237 | |
| 8238 | if (target_method->IsPrivate()) { |
| 8239 | kind = mirror::MethodHandle::Kind::kInvokeDirect; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8240 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 8241 | method_handle.field_or_method_idx_, |
| 8242 | referrer, |
| 8243 | InvokeType::kDirect); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8244 | } else { |
| 8245 | kind = mirror::MethodHandle::Kind::kInvokeSuper; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8246 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 8247 | method_handle.field_or_method_idx_, |
| 8248 | referrer, |
| 8249 | InvokeType::kSuper); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8250 | if (UNLIKELY(target_method == nullptr)) { |
| 8251 | break; |
| 8252 | } |
| 8253 | // Find the method specified in the parent in referring class |
| 8254 | // so invoke-super invokes the method in the parent of the |
| 8255 | // referrer. |
| 8256 | target_method = |
| 8257 | referrer->GetDeclaringClass()->FindVirtualMethodForVirtual(target_method, |
| 8258 | kRuntimePointerSize); |
| 8259 | } |
| 8260 | break; |
| 8261 | } |
| 8262 | case DexFile::MethodHandleType::kInvokeInterface: { |
| 8263 | kind = mirror::MethodHandle::Kind::kInvokeInterface; |
| 8264 | receiver_count = 1; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8265 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 8266 | method_handle.field_or_method_idx_, |
| 8267 | referrer, |
| 8268 | InvokeType::kInterface); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8269 | break; |
| 8270 | } |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 8271 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8272 | |
| 8273 | if (UNLIKELY(target_method == nullptr)) { |
| 8274 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 8275 | return nullptr; |
| 8276 | } |
| 8277 | |
| 8278 | ObjPtr<mirror::Class> target_class = target_method->GetDeclaringClass(); |
| 8279 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 8280 | uint32_t access_flags = target_method->GetAccessFlags(); |
| 8281 | if (UNLIKELY(!referring_class->CanAccessMember(target_class, access_flags))) { |
| 8282 | ThrowIllegalAccessErrorMethod(referring_class, target_method); |
| 8283 | return nullptr; |
| 8284 | } |
| 8285 | |
| 8286 | // Calculate the number of parameters from the method shorty. We add the |
| 8287 | // receiver count (0 or 1) and deduct one for the return value. |
| 8288 | uint32_t shorty_length; |
| 8289 | target_method->GetShorty(&shorty_length); |
| 8290 | int32_t num_params = static_cast<int32_t>(shorty_length + receiver_count - 1); |
| 8291 | |
| 8292 | StackHandleScope<7> hs(self); |
| 8293 | ObjPtr<mirror::Class> class_type = mirror::Class::GetJavaLangClass(); |
| 8294 | ObjPtr<mirror::Class> array_of_class = FindArrayClass(self, &class_type); |
| 8295 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 8296 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params))); |
| 8297 | if (method_params.Get() == nullptr) { |
| 8298 | DCHECK(self->IsExceptionPending()); |
| 8299 | return nullptr; |
| 8300 | } |
| 8301 | |
| 8302 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 8303 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
| 8304 | int32_t index = 0; |
| 8305 | |
| 8306 | if (receiver_count != 0) { |
| 8307 | // Insert receiver |
| 8308 | method_params->Set(index++, target_method->GetDeclaringClass()); |
| 8309 | } |
| 8310 | |
| 8311 | DexFileParameterIterator it(*dex_file, target_method->GetPrototype()); |
| 8312 | while (it.HasNext()) { |
| 8313 | const dex::TypeIndex type_idx = it.GetTypeIdx(); |
| 8314 | mirror::Class* klass = ResolveType(*dex_file, type_idx, dex_cache, class_loader); |
| 8315 | if (nullptr == klass) { |
| 8316 | DCHECK(self->IsExceptionPending()); |
| 8317 | return nullptr; |
| 8318 | } |
| 8319 | method_params->Set(index++, klass); |
| 8320 | it.Next(); |
| 8321 | } |
| 8322 | |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 8323 | Handle<mirror::Class> return_type = hs.NewHandle(target_method->ResolveReturnType()); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8324 | if (UNLIKELY(return_type.IsNull())) { |
| 8325 | DCHECK(self->IsExceptionPending()); |
| 8326 | return nullptr; |
| 8327 | } |
| 8328 | |
| 8329 | Handle<mirror::MethodType> |
| 8330 | method_type(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params))); |
| 8331 | if (UNLIKELY(method_type.IsNull())) { |
| 8332 | DCHECK(self->IsExceptionPending()); |
| 8333 | return nullptr; |
| 8334 | } |
| 8335 | |
| 8336 | if (UNLIKELY(handle_type == DexFile::MethodHandleType::kInvokeConstructor)) { |
| 8337 | Handle<mirror::Class> constructor_class = hs.NewHandle(target_method->GetDeclaringClass()); |
| 8338 | Handle<mirror::MethodHandlesLookup> lookup = |
| 8339 | hs.NewHandle(mirror::MethodHandlesLookup::GetDefault(self)); |
| 8340 | return lookup->FindConstructor(self, constructor_class, method_type); |
| 8341 | } |
| 8342 | |
| 8343 | uintptr_t target = reinterpret_cast<uintptr_t>(target_method); |
| 8344 | return mirror::MethodHandleImpl::Create(self, target, kind, method_type); |
| 8345 | } |
| 8346 | |
| 8347 | mirror::MethodHandle* ClassLinker::ResolveMethodHandle(uint32_t method_handle_idx, |
| 8348 | ArtMethod* referrer) |
| 8349 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8350 | Thread* const self = Thread::Current(); |
| 8351 | const DexFile* const dex_file = referrer->GetDexFile(); |
| 8352 | const DexFile::MethodHandleItem& method_handle = dex_file->GetMethodHandle(method_handle_idx); |
| 8353 | switch (static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_)) { |
| 8354 | case DexFile::MethodHandleType::kStaticPut: |
| 8355 | case DexFile::MethodHandleType::kStaticGet: |
| 8356 | case DexFile::MethodHandleType::kInstancePut: |
| 8357 | case DexFile::MethodHandleType::kInstanceGet: |
| 8358 | return ResolveMethodHandleForField(self, method_handle, referrer); |
| 8359 | case DexFile::MethodHandleType::kInvokeStatic: |
| 8360 | case DexFile::MethodHandleType::kInvokeInstance: |
| 8361 | case DexFile::MethodHandleType::kInvokeConstructor: |
| 8362 | case DexFile::MethodHandleType::kInvokeDirect: |
| 8363 | case DexFile::MethodHandleType::kInvokeInterface: |
| 8364 | return ResolveMethodHandleForMethod(self, dex_file, method_handle, referrer); |
| 8365 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8366 | } |
| 8367 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8368 | bool ClassLinker::IsQuickResolutionStub(const void* entry_point) const { |
| 8369 | return (entry_point == GetQuickResolutionStub()) || |
| 8370 | (quick_resolution_trampoline_ == entry_point); |
| 8371 | } |
| 8372 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8373 | bool ClassLinker::IsQuickToInterpreterBridge(const void* entry_point) const { |
| 8374 | return (entry_point == GetQuickToInterpreterBridge()) || |
| 8375 | (quick_to_interpreter_bridge_trampoline_ == entry_point); |
| 8376 | } |
| 8377 | |
| 8378 | bool ClassLinker::IsQuickGenericJniStub(const void* entry_point) const { |
| 8379 | return (entry_point == GetQuickGenericJniStub()) || |
| 8380 | (quick_generic_jni_trampoline_ == entry_point); |
| 8381 | } |
| 8382 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 8383 | bool ClassLinker::IsJniDlsymLookupStub(const void* entry_point) const { |
| 8384 | return entry_point == GetJniDlsymLookupStub(); |
| 8385 | } |
| 8386 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8387 | const void* ClassLinker::GetRuntimeQuickGenericJniStub() const { |
| 8388 | return GetQuickGenericJniStub(); |
| 8389 | } |
| 8390 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8391 | void ClassLinker::SetEntryPointsToInterpreter(ArtMethod* method) const { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8392 | if (!method->IsNative()) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8393 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 8394 | } else { |
Goran Jakovljevic | c16268f | 2017-07-27 10:03:32 +0200 | [diff] [blame] | 8395 | method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub()); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8396 | } |
| 8397 | } |
| 8398 | |
Alex Light | db01a09 | 2017-04-03 15:39:55 -0700 | [diff] [blame] | 8399 | void ClassLinker::SetEntryPointsForObsoleteMethod(ArtMethod* method) const { |
| 8400 | DCHECK(method->IsObsolete()); |
| 8401 | // We cannot mess with the entrypoints of native methods because they are used to determine how |
| 8402 | // large the method's quick stack frame is. Without this information we cannot walk the stacks. |
| 8403 | if (!method->IsNative()) { |
| 8404 | method->SetEntryPointFromQuickCompiledCode(GetInvokeObsoleteMethodStub()); |
| 8405 | } |
| 8406 | } |
| 8407 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 8408 | void ClassLinker::DumpForSigQuit(std::ostream& os) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 8409 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 8410 | ReaderMutexLock mu(soa.Self(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 8411 | os << "Zygote loaded classes=" << NumZygoteClasses() << " post zygote classes=" |
| 8412 | << NumNonZygoteClasses() << "\n"; |
| 8413 | } |
| 8414 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8415 | class CountClassesVisitor : public ClassLoaderVisitor { |
| 8416 | public: |
| 8417 | CountClassesVisitor() : num_zygote_classes(0), num_non_zygote_classes(0) {} |
| 8418 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8419 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 8420 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) OVERRIDE { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8421 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 8422 | if (class_table != nullptr) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 8423 | num_zygote_classes += class_table->NumZygoteClasses(class_loader); |
| 8424 | num_non_zygote_classes += class_table->NumNonZygoteClasses(class_loader); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 8425 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 8426 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8427 | |
| 8428 | size_t num_zygote_classes; |
| 8429 | size_t num_non_zygote_classes; |
| 8430 | }; |
| 8431 | |
| 8432 | size_t ClassLinker::NumZygoteClasses() const { |
| 8433 | CountClassesVisitor visitor; |
| 8434 | VisitClassLoaders(&visitor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 8435 | return visitor.num_zygote_classes + boot_class_table_->NumZygoteClasses(nullptr); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 8436 | } |
| 8437 | |
| 8438 | size_t ClassLinker::NumNonZygoteClasses() const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8439 | CountClassesVisitor visitor; |
| 8440 | VisitClassLoaders(&visitor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 8441 | return visitor.num_non_zygote_classes + boot_class_table_->NumNonZygoteClasses(nullptr); |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 8442 | } |
| 8443 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 8444 | size_t ClassLinker::NumLoadedClasses() { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 8445 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 8446 | // 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] | 8447 | return NumNonZygoteClasses(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 8448 | } |
| 8449 | |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 8450 | pid_t ClassLinker::GetClassesLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 8451 | return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 8452 | } |
| 8453 | |
| 8454 | pid_t ClassLinker::GetDexLockOwner() { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 8455 | return Locks::dex_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 8456 | } |
| 8457 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8458 | void ClassLinker::SetClassRoot(ClassRoot class_root, ObjPtr<mirror::Class> klass) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 8459 | DCHECK(!init_done_); |
| 8460 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8461 | DCHECK(klass != nullptr); |
| 8462 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 8463 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 8464 | mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8465 | DCHECK(class_roots != nullptr); |
| 8466 | DCHECK(class_roots->Get(class_root) == nullptr); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 8467 | class_roots->Set<false>(class_root, klass); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 8468 | } |
| 8469 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8470 | const char* ClassLinker::GetClassRootDescriptor(ClassRoot class_root) { |
| 8471 | static const char* class_roots_descriptors[] = { |
| 8472 | "Ljava/lang/Class;", |
| 8473 | "Ljava/lang/Object;", |
| 8474 | "[Ljava/lang/Class;", |
| 8475 | "[Ljava/lang/Object;", |
| 8476 | "Ljava/lang/String;", |
| 8477 | "Ljava/lang/DexCache;", |
| 8478 | "Ljava/lang/ref/Reference;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 8479 | "Ljava/lang/reflect/Constructor;", |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 8480 | "Ljava/lang/reflect/Field;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 8481 | "Ljava/lang/reflect/Method;", |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8482 | "Ljava/lang/reflect/Proxy;", |
| 8483 | "[Ljava/lang/String;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 8484 | "[Ljava/lang/reflect/Constructor;", |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 8485 | "[Ljava/lang/reflect/Field;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 8486 | "[Ljava/lang/reflect/Method;", |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8487 | "Ljava/lang/invoke/CallSite;", |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8488 | "Ljava/lang/invoke/MethodHandleImpl;", |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8489 | "Ljava/lang/invoke/MethodHandles$Lookup;", |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8490 | "Ljava/lang/invoke/MethodType;", |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8491 | "Ljava/lang/ClassLoader;", |
| 8492 | "Ljava/lang/Throwable;", |
| 8493 | "Ljava/lang/ClassNotFoundException;", |
| 8494 | "Ljava/lang/StackTraceElement;", |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 8495 | "Ldalvik/system/EmulatedStackFrame;", |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8496 | "Z", |
| 8497 | "B", |
| 8498 | "C", |
| 8499 | "D", |
| 8500 | "F", |
| 8501 | "I", |
| 8502 | "J", |
| 8503 | "S", |
| 8504 | "V", |
| 8505 | "[Z", |
| 8506 | "[B", |
| 8507 | "[C", |
| 8508 | "[D", |
| 8509 | "[F", |
| 8510 | "[I", |
| 8511 | "[J", |
| 8512 | "[S", |
| 8513 | "[Ljava/lang/StackTraceElement;", |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 8514 | "Ldalvik/system/ClassExt;", |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8515 | }; |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 8516 | static_assert(arraysize(class_roots_descriptors) == size_t(kClassRootsMax), |
| 8517 | "Mismatch between class descriptors and class-root enum"); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8518 | |
| 8519 | const char* descriptor = class_roots_descriptors[class_root]; |
| 8520 | CHECK(descriptor != nullptr); |
| 8521 | return descriptor; |
| 8522 | } |
| 8523 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 8524 | jobject ClassLinker::CreateWellKnownClassLoader(Thread* self, |
| 8525 | const std::vector<const DexFile*>& dex_files, |
| 8526 | jclass loader_class, |
| 8527 | jobject parent_loader) { |
| 8528 | CHECK(self->GetJniEnv()->IsSameObject(loader_class, |
| 8529 | WellKnownClasses::dalvik_system_PathClassLoader) || |
| 8530 | self->GetJniEnv()->IsSameObject(loader_class, |
| 8531 | WellKnownClasses::dalvik_system_DelegateLastClassLoader)); |
| 8532 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8533 | // SOAAlreadyRunnable is protected, and we need something to add a global reference. |
| 8534 | // We could move the jobject to the callers, but all call-sites do this... |
| 8535 | ScopedObjectAccessUnchecked soa(self); |
| 8536 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8537 | // 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] | 8538 | StackHandleScope<6> hs(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8539 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8540 | ArtField* dex_elements_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8541 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8542 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 8543 | 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] | 8544 | DCHECK(dex_elements_class != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8545 | DCHECK(dex_elements_class->IsArrayClass()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 8546 | Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements(hs.NewHandle( |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 8547 | mirror::ObjectArray<mirror::Object>::Alloc(self, |
| 8548 | dex_elements_class.Get(), |
| 8549 | dex_files.size()))); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8550 | Handle<mirror::Class> h_dex_element_class = |
| 8551 | hs.NewHandle(dex_elements_class->GetComponentType()); |
| 8552 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8553 | ArtField* element_file_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8554 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8555 | DCHECK_EQ(h_dex_element_class.Get(), element_file_field->GetDeclaringClass()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8556 | |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8557 | ArtField* cookie_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8558 | DCHECK_EQ(cookie_field->GetDeclaringClass(), element_file_field->GetType<false>()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8559 | |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8560 | ArtField* file_name_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_fileName); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 8561 | DCHECK_EQ(file_name_field->GetDeclaringClass(), element_file_field->GetType<false>()); |
| 8562 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8563 | // Fill the elements array. |
| 8564 | int32_t index = 0; |
| 8565 | for (const DexFile* dex_file : dex_files) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 8566 | StackHandleScope<4> hs2(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8567 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 8568 | // CreateWellKnownClassLoader is only used by gtests and compiler. |
| 8569 | // Index 0 of h_long_array is supposed to be the oat file but we can leave it null. |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 8570 | Handle<mirror::LongArray> h_long_array = hs2.NewHandle(mirror::LongArray::Alloc( |
| 8571 | self, |
| 8572 | kDexFileIndexStart + 1)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8573 | DCHECK(h_long_array != nullptr); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 8574 | h_long_array->Set(kDexFileIndexStart, reinterpret_cast<intptr_t>(dex_file)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8575 | |
Mathieu Chartier | 3738e98 | 2017-05-12 16:07:28 -0700 | [diff] [blame] | 8576 | // Note that this creates a finalizable dalvik.system.DexFile object and a corresponding |
| 8577 | // FinalizerReference which will never get cleaned up without a started runtime. |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8578 | Handle<mirror::Object> h_dex_file = hs2.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8579 | cookie_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8580 | DCHECK(h_dex_file != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8581 | cookie_field->SetObject<false>(h_dex_file.Get(), h_long_array.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8582 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 8583 | Handle<mirror::String> h_file_name = hs2.NewHandle( |
| 8584 | mirror::String::AllocFromModifiedUtf8(self, dex_file->GetLocation().c_str())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8585 | DCHECK(h_file_name != nullptr); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 8586 | file_name_field->SetObject<false>(h_dex_file.Get(), h_file_name.Get()); |
| 8587 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8588 | 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] | 8589 | DCHECK(h_element != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8590 | element_file_field->SetObject<false>(h_element.Get(), h_dex_file.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8591 | |
| 8592 | h_dex_elements->Set(index, h_element.Get()); |
| 8593 | index++; |
| 8594 | } |
| 8595 | DCHECK_EQ(index, h_dex_elements->GetLength()); |
| 8596 | |
| 8597 | // Create DexPathList. |
| 8598 | Handle<mirror::Object> h_dex_path_list = hs.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8599 | dex_elements_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8600 | DCHECK(h_dex_path_list != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8601 | // Set elements. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8602 | 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] | 8603 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 8604 | // Create the class loader.. |
| 8605 | Handle<mirror::Class> h_loader_class = hs.NewHandle(soa.Decode<mirror::Class>(loader_class)); |
| 8606 | Handle<mirror::Object> h_class_loader = hs.NewHandle(h_loader_class->AllocObject(self)); |
| 8607 | DCHECK(h_class_loader != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8608 | // Set DexPathList. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8609 | ArtField* path_list_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8610 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8611 | DCHECK(path_list_field != nullptr); |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 8612 | path_list_field->SetObject<false>(h_class_loader.Get(), h_dex_path_list.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8613 | |
| 8614 | // Make a pretend boot-classpath. |
| 8615 | // TODO: Should we scan the image? |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8616 | ArtField* const parent_field = |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8617 | mirror::Class::FindField(self, |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 8618 | h_class_loader->GetClass(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8619 | "parent", |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8620 | "Ljava/lang/ClassLoader;"); |
Roland Levillain | f39c9eb | 2015-05-26 15:02:07 +0100 | [diff] [blame] | 8621 | DCHECK(parent_field != nullptr); |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 8622 | |
| 8623 | ObjPtr<mirror::Object> parent = (parent_loader != nullptr) |
| 8624 | ? soa.Decode<mirror::ClassLoader>(parent_loader) |
| 8625 | : soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader)->AllocObject(self); |
| 8626 | parent_field->SetObject<false>(h_class_loader.Get(), parent); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8627 | |
| 8628 | // Make it a global ref and return. |
| 8629 | ScopedLocalRef<jobject> local_ref( |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 8630 | soa.Env(), soa.Env()->AddLocalReference<jobject>(h_class_loader.Get())); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8631 | return soa.Env()->NewGlobalRef(local_ref.get()); |
| 8632 | } |
| 8633 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 8634 | jobject ClassLinker::CreatePathClassLoader(Thread* self, |
| 8635 | const std::vector<const DexFile*>& dex_files) { |
| 8636 | return CreateWellKnownClassLoader(self, |
| 8637 | dex_files, |
| 8638 | WellKnownClasses::dalvik_system_PathClassLoader, |
| 8639 | nullptr); |
| 8640 | } |
| 8641 | |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 8642 | void ClassLinker::DropFindArrayClassCache() { |
| 8643 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
| 8644 | find_array_class_cache_next_victim_ = 0; |
| 8645 | } |
| 8646 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 8647 | void ClassLinker::VisitClassLoaders(ClassLoaderVisitor* visitor) const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8648 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 8649 | for (const ClassLoaderData& data : class_loaders_) { |
Mathieu Chartier | 4843bd5 | 2015-10-01 17:08:44 -0700 | [diff] [blame] | 8650 | // 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] | 8651 | ObjPtr<mirror::ClassLoader> class_loader = ObjPtr<mirror::ClassLoader>::DownCast( |
| 8652 | self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8653 | if (class_loader != nullptr) { |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 8654 | visitor->Visit(class_loader.Ptr()); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8655 | } |
| 8656 | } |
| 8657 | } |
| 8658 | |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 8659 | void ClassLinker::InsertDexFileInToClassLoader(ObjPtr<mirror::Object> dex_file, |
| 8660 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8661 | DCHECK(dex_file != nullptr); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8662 | Thread* const self = Thread::Current(); |
| 8663 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 8664 | ClassTable* const table = ClassTableForClassLoader(class_loader.Ptr()); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8665 | DCHECK(table != nullptr); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 8666 | if (table->InsertStrongRoot(dex_file) && class_loader != nullptr) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8667 | // It was not already inserted, perform the write barrier to let the GC know the class loader's |
| 8668 | // class table was modified. |
| 8669 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader); |
| 8670 | } |
| 8671 | } |
| 8672 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 8673 | void ClassLinker::CleanupClassLoaders() { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8674 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8675 | std::vector<ClassLoaderData> to_delete; |
| 8676 | // Do the delete outside the lock to avoid lock violation in jit code cache. |
| 8677 | { |
| 8678 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 8679 | for (auto it = class_loaders_.begin(); it != class_loaders_.end(); ) { |
| 8680 | const ClassLoaderData& data = *it; |
| 8681 | // 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] | 8682 | ObjPtr<mirror::ClassLoader> class_loader = |
| 8683 | ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8684 | if (class_loader != nullptr) { |
| 8685 | ++it; |
| 8686 | } else { |
| 8687 | VLOG(class_linker) << "Freeing class loader"; |
| 8688 | to_delete.push_back(data); |
| 8689 | it = class_loaders_.erase(it); |
| 8690 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8691 | } |
| 8692 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8693 | for (ClassLoaderData& data : to_delete) { |
| 8694 | DeleteClassLoader(self, data); |
| 8695 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8696 | } |
| 8697 | |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 8698 | class GetResolvedClassesVisitor : public ClassVisitor { |
| 8699 | public: |
| 8700 | GetResolvedClassesVisitor(std::set<DexCacheResolvedClasses>* result, bool ignore_boot_classes) |
| 8701 | : result_(result), |
| 8702 | ignore_boot_classes_(ignore_boot_classes), |
| 8703 | last_resolved_classes_(result->end()), |
| 8704 | last_dex_file_(nullptr), |
| 8705 | vlog_is_on_(VLOG_IS_ON(class_linker)), |
| 8706 | extra_stats_(), |
| 8707 | last_extra_stats_(extra_stats_.end()) { } |
| 8708 | |
| 8709 | bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8710 | if (!klass->IsProxyClass() && |
| 8711 | !klass->IsArrayClass() && |
| 8712 | klass->IsResolved() && |
| 8713 | !klass->IsErroneousResolved() && |
| 8714 | (!ignore_boot_classes_ || klass->GetClassLoader() != nullptr)) { |
| 8715 | const DexFile& dex_file = klass->GetDexFile(); |
| 8716 | if (&dex_file != last_dex_file_) { |
| 8717 | last_dex_file_ = &dex_file; |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame^] | 8718 | DexCacheResolvedClasses resolved_classes( |
| 8719 | dex_file.GetLocation(), |
| 8720 | DexFileLoader::GetBaseLocation(dex_file.GetLocation()), |
| 8721 | dex_file.GetLocationChecksum(), |
| 8722 | dex_file.NumMethodIds()); |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 8723 | last_resolved_classes_ = result_->find(resolved_classes); |
| 8724 | if (last_resolved_classes_ == result_->end()) { |
| 8725 | last_resolved_classes_ = result_->insert(resolved_classes).first; |
| 8726 | } |
| 8727 | } |
| 8728 | bool added = last_resolved_classes_->AddClass(klass->GetDexTypeIndex()); |
| 8729 | if (UNLIKELY(vlog_is_on_) && added) { |
| 8730 | const DexCacheResolvedClasses* resolved_classes = std::addressof(*last_resolved_classes_); |
| 8731 | if (last_extra_stats_ == extra_stats_.end() || |
| 8732 | last_extra_stats_->first != resolved_classes) { |
| 8733 | last_extra_stats_ = extra_stats_.find(resolved_classes); |
| 8734 | if (last_extra_stats_ == extra_stats_.end()) { |
| 8735 | last_extra_stats_ = |
| 8736 | extra_stats_.emplace(resolved_classes, ExtraStats(dex_file.NumClassDefs())).first; |
| 8737 | } |
| 8738 | } |
| 8739 | } |
| 8740 | } |
| 8741 | return true; |
| 8742 | } |
| 8743 | |
| 8744 | void PrintStatistics() const { |
| 8745 | if (vlog_is_on_) { |
| 8746 | for (const DexCacheResolvedClasses& resolved_classes : *result_) { |
| 8747 | auto it = extra_stats_.find(std::addressof(resolved_classes)); |
| 8748 | DCHECK(it != extra_stats_.end()); |
| 8749 | const ExtraStats& extra_stats = it->second; |
| 8750 | LOG(INFO) << "Dex location " << resolved_classes.GetDexLocation() |
| 8751 | << " has " << resolved_classes.GetClasses().size() << " / " |
| 8752 | << extra_stats.number_of_class_defs_ << " resolved classes"; |
| 8753 | } |
| 8754 | } |
| 8755 | } |
| 8756 | |
| 8757 | private: |
| 8758 | struct ExtraStats { |
| 8759 | explicit ExtraStats(uint32_t number_of_class_defs) |
| 8760 | : number_of_class_defs_(number_of_class_defs) {} |
| 8761 | uint32_t number_of_class_defs_; |
| 8762 | }; |
| 8763 | |
| 8764 | std::set<DexCacheResolvedClasses>* result_; |
| 8765 | bool ignore_boot_classes_; |
| 8766 | std::set<DexCacheResolvedClasses>::iterator last_resolved_classes_; |
| 8767 | const DexFile* last_dex_file_; |
| 8768 | |
| 8769 | // Statistics. |
| 8770 | bool vlog_is_on_; |
| 8771 | std::map<const DexCacheResolvedClasses*, ExtraStats> extra_stats_; |
| 8772 | std::map<const DexCacheResolvedClasses*, ExtraStats>::iterator last_extra_stats_; |
| 8773 | }; |
| 8774 | |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8775 | std::set<DexCacheResolvedClasses> ClassLinker::GetResolvedClasses(bool ignore_boot_classes) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 8776 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8777 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 8778 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8779 | std::set<DexCacheResolvedClasses> ret; |
| 8780 | VLOG(class_linker) << "Collecting resolved classes"; |
| 8781 | const uint64_t start_time = NanoTime(); |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 8782 | GetResolvedClassesVisitor visitor(&ret, ignore_boot_classes); |
| 8783 | VisitClasses(&visitor); |
| 8784 | if (VLOG_IS_ON(class_linker)) { |
| 8785 | visitor.PrintStatistics(); |
| 8786 | LOG(INFO) << "Collecting class profile took " << PrettyDuration(NanoTime() - start_time); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8787 | } |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8788 | return ret; |
| 8789 | } |
| 8790 | |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8791 | class ClassLinker::FindVirtualMethodHolderVisitor : public ClassVisitor { |
| 8792 | public: |
| 8793 | FindVirtualMethodHolderVisitor(const ArtMethod* method, PointerSize pointer_size) |
| 8794 | : method_(method), |
| 8795 | pointer_size_(pointer_size) {} |
| 8796 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8797 | bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8798 | if (klass->GetVirtualMethodsSliceUnchecked(pointer_size_).Contains(method_)) { |
| 8799 | holder_ = klass; |
| 8800 | } |
| 8801 | // Return false to stop searching if holder_ is not null. |
| 8802 | return holder_ == nullptr; |
| 8803 | } |
| 8804 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8805 | ObjPtr<mirror::Class> holder_ = nullptr; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8806 | const ArtMethod* const method_; |
| 8807 | const PointerSize pointer_size_; |
| 8808 | }; |
| 8809 | |
| 8810 | mirror::Class* ClassLinker::GetHoldingClassOfCopiedMethod(ArtMethod* method) { |
| 8811 | ScopedTrace trace(__FUNCTION__); // Since this function is slow, have a trace to notify people. |
| 8812 | CHECK(method->IsCopied()); |
| 8813 | FindVirtualMethodHolderVisitor visitor(method, image_pointer_size_); |
| 8814 | VisitClasses(&visitor); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8815 | return visitor.holder_.Ptr(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8816 | } |
| 8817 | |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 8818 | mirror::IfTable* ClassLinker::AllocIfTable(Thread* self, size_t ifcount) { |
| 8819 | return down_cast<mirror::IfTable*>( |
| 8820 | mirror::IfTable::Alloc(self, |
| 8821 | GetClassRoot(kObjectArrayClass), |
| 8822 | ifcount * mirror::IfTable::kMax)); |
| 8823 | } |
| 8824 | |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 8825 | // Instantiate ResolveMethod. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8826 | template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>( |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 8827 | const DexFile& dex_file, |
| 8828 | uint32_t method_idx, |
| 8829 | Handle<mirror::DexCache> dex_cache, |
| 8830 | Handle<mirror::ClassLoader> class_loader, |
| 8831 | ArtMethod* referrer, |
| 8832 | InvokeType type); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8833 | template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::ResolveMode::kNoChecks>( |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 8834 | const DexFile& dex_file, |
| 8835 | uint32_t method_idx, |
| 8836 | Handle<mirror::DexCache> dex_cache, |
| 8837 | Handle<mirror::ClassLoader> class_loader, |
| 8838 | ArtMethod* referrer, |
| 8839 | InvokeType type); |
| 8840 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8841 | } // namespace art |