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 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 19 | #include <deque> |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 20 | #include <iostream> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 21 | #include <memory> |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 22 | #include <queue> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 23 | #include <string> |
Andreas Gampe | a696c0a | 2014-12-10 20:51:45 -0800 | [diff] [blame] | 24 | #include <unistd.h> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 25 | #include <utility> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 26 | #include <vector> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 27 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 28 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 29 | #include "art_method-inl.h" |
| 30 | #include "base/arena_allocator.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 31 | #include "base/casts.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 32 | #include "base/logging.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 33 | #include "base/scoped_arena_containers.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 34 | #include "base/scoped_flock.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 35 | #include "base/stl_util.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 36 | #include "base/time_utils.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 37 | #include "base/unix_file/fd_file.h" |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 38 | #include "base/value_object.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 39 | #include "class_linker-inl.h" |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 40 | #include "class_table-inl.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 41 | #include "compiler_callbacks.h" |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 42 | #include "debugger.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 43 | #include "dex_file-inl.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 44 | #include "entrypoints/runtime_asm_entrypoints.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 45 | #include "gc_root-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 46 | #include "gc/accounting/card_table-inl.h" |
| 47 | #include "gc/accounting/heap_bitmap.h" |
| 48 | #include "gc/heap.h" |
| 49 | #include "gc/space/image_space.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 50 | #include "handle_scope.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 51 | #include "intern_table.h" |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 52 | #include "interpreter/interpreter.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 53 | #include "jit/jit.h" |
| 54 | #include "jit/jit_code_cache.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 55 | #include "leb128.h" |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 56 | #include "linear_alloc.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 57 | #include "oat.h" |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 58 | #include "oat_file.h" |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 59 | #include "oat_file-inl.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 60 | #include "oat_file_assistant.h" |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 61 | #include "object_lock.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 62 | #include "mirror/class.h" |
| 63 | #include "mirror/class-inl.h" |
| 64 | #include "mirror/class_loader.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 65 | #include "mirror/dex_cache-inl.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 66 | #include "mirror/field.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 67 | #include "mirror/iftable-inl.h" |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 68 | #include "mirror/method.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 69 | #include "mirror/object-inl.h" |
| 70 | #include "mirror/object_array-inl.h" |
| 71 | #include "mirror/proxy.h" |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 72 | #include "mirror/reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 73 | #include "mirror/stack_trace_element.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 74 | #include "mirror/string-inl.h" |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 75 | #include "os.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 76 | #include "runtime.h" |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 77 | #include "entrypoints/entrypoint_utils.h" |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 78 | #include "ScopedLocalRef.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 79 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 80 | #include "handle_scope-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 81 | #include "thread-inl.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 82 | #include "utils.h" |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 83 | #include "utils/dex_cache_arrays_layout-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 84 | #include "verifier/method_verifier.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 85 | #include "well_known_classes.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 86 | |
| 87 | namespace art { |
| 88 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 89 | static constexpr bool kSanityCheckObjects = kIsDebugBuild; |
| 90 | |
Andreas Gampe | 69b5d8f | 2015-06-03 15:43:16 -0700 | [diff] [blame] | 91 | // For b/21333911. |
| 92 | static constexpr bool kDuplicateClassesCheck = false; |
| 93 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 94 | static void ThrowNoClassDefFoundError(const char* fmt, ...) |
| 95 | __attribute__((__format__(__printf__, 1, 2))) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 96 | SHARED_REQUIRES(Locks::mutator_lock_); |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 97 | static void ThrowNoClassDefFoundError(const char* fmt, ...) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 98 | va_list args; |
| 99 | va_start(args, fmt); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 100 | Thread* self = Thread::Current(); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 101 | self->ThrowNewExceptionV("Ljava/lang/NoClassDefFoundError;", fmt, args); |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 102 | va_end(args); |
| 103 | } |
| 104 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 105 | bool ClassLinker::HasInitWithString(Thread* self, const char* descriptor) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 106 | ArtMethod* method = self->GetCurrentMethod(nullptr); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 107 | StackHandleScope<1> hs(self); |
| 108 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(method != nullptr ? |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 109 | method->GetDeclaringClass()->GetClassLoader() : nullptr)); |
| 110 | mirror::Class* exception_class = FindClass(self, descriptor, class_loader); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 111 | |
| 112 | if (exception_class == nullptr) { |
| 113 | // No exc class ~ no <init>-with-string. |
| 114 | CHECK(self->IsExceptionPending()); |
| 115 | self->ClearException(); |
| 116 | return false; |
| 117 | } |
| 118 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 119 | ArtMethod* exception_init_method = exception_class->FindDeclaredDirectMethod( |
| 120 | "<init>", "(Ljava/lang/String;)V", image_pointer_size_); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 121 | return exception_init_method != nullptr; |
| 122 | } |
| 123 | |
| 124 | void ClassLinker::ThrowEarlierClassFailure(mirror::Class* c) { |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 125 | // The class failed to initialize on a previous attempt, so we want to throw |
| 126 | // a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we |
| 127 | // failed in verification, in which case v2 5.4.1 says we need to re-throw |
| 128 | // the previous error. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 129 | Runtime* const runtime = Runtime::Current(); |
| 130 | if (!runtime->IsAotCompiler()) { // Give info if this occurs at runtime. |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 131 | LOG(INFO) << "Rejecting re-init on previously-failed class " << PrettyClass(c); |
| 132 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 133 | |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 134 | CHECK(c->IsErroneous()) << PrettyClass(c) << " " << c->GetStatus(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 135 | Thread* self = Thread::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 136 | if (runtime->IsAotCompiler()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 137 | // At compile time, accurate errors and NCDFE are disabled to speed compilation. |
| 138 | mirror::Throwable* pre_allocated = runtime->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 139 | self->SetException(pre_allocated); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 140 | } else { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 141 | if (c->GetVerifyErrorClass() != nullptr) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 142 | // TODO: change the verifier to store an _instance_, with a useful detail message? |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 143 | // It's possible the exception doesn't have a <init>(String). |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 144 | std::string temp; |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 145 | const char* descriptor = c->GetVerifyErrorClass()->GetDescriptor(&temp); |
| 146 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 147 | if (HasInitWithString(self, descriptor)) { |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 148 | self->ThrowNewException(descriptor, PrettyDescriptor(c).c_str()); |
| 149 | } else { |
| 150 | self->ThrowNewException(descriptor, nullptr); |
| 151 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 152 | } else { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 153 | self->ThrowNewException("Ljava/lang/NoClassDefFoundError;", |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 154 | PrettyDescriptor(c).c_str()); |
| 155 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 159 | static void VlogClassInitializationFailure(Handle<mirror::Class> klass) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 160 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 161 | if (VLOG_IS_ON(class_linker)) { |
| 162 | std::string temp; |
| 163 | LOG(INFO) << "Failed to initialize class " << klass->GetDescriptor(&temp) << " from " |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 164 | << klass->GetLocation() << "\n" << Thread::Current()->GetException()->Dump(); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | static void WrapExceptionInInitializer(Handle<mirror::Class> klass) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 169 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 170 | Thread* self = Thread::Current(); |
| 171 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 172 | |
| 173 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 174 | CHECK(cause.get() != nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 175 | |
| 176 | env->ExceptionClear(); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 177 | bool is_error = env->IsInstanceOf(cause.get(), WellKnownClasses::java_lang_Error); |
| 178 | env->Throw(cause.get()); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 179 | |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 180 | // We only wrap non-Error exceptions; an Error can just be used as-is. |
| 181 | if (!is_error) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 182 | self->ThrowNewWrappedException("Ljava/lang/ExceptionInInitializerError;", nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 183 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 184 | VlogClassInitializationFailure(klass); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 187 | // Gap between two fields in object layout. |
| 188 | struct FieldGap { |
| 189 | uint32_t start_offset; // The offset from the start of the object. |
| 190 | uint32_t size; // The gap size of 1, 2, or 4 bytes. |
| 191 | }; |
| 192 | struct FieldGapsComparator { |
| 193 | explicit FieldGapsComparator() { |
| 194 | } |
| 195 | bool operator() (const FieldGap& lhs, const FieldGap& rhs) |
| 196 | NO_THREAD_SAFETY_ANALYSIS { |
Andreas Gampe | f52857f | 2015-02-18 15:38:57 -0800 | [diff] [blame] | 197 | // Sort by gap size, largest first. Secondary sort by starting offset. |
Richard Uhler | fab6788 | 2015-07-13 17:00:35 -0700 | [diff] [blame] | 198 | // Note that the priority queue returns the largest element, so operator() |
| 199 | // should return true if lhs is less than rhs. |
| 200 | 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] | 201 | } |
| 202 | }; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 203 | typedef std::priority_queue<FieldGap, std::vector<FieldGap>, FieldGapsComparator> FieldGaps; |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 204 | |
| 205 | // Adds largest aligned gaps to queue of gaps. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 206 | 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] | 207 | DCHECK(gaps != nullptr); |
| 208 | |
| 209 | uint32_t current_offset = gap_start; |
| 210 | while (current_offset != gap_end) { |
| 211 | size_t remaining = gap_end - current_offset; |
| 212 | if (remaining >= sizeof(uint32_t) && IsAligned<4>(current_offset)) { |
| 213 | gaps->push(FieldGap {current_offset, sizeof(uint32_t)}); |
| 214 | current_offset += sizeof(uint32_t); |
| 215 | } else if (remaining >= sizeof(uint16_t) && IsAligned<2>(current_offset)) { |
| 216 | gaps->push(FieldGap {current_offset, sizeof(uint16_t)}); |
| 217 | current_offset += sizeof(uint16_t); |
| 218 | } else { |
| 219 | gaps->push(FieldGap {current_offset, sizeof(uint8_t)}); |
| 220 | current_offset += sizeof(uint8_t); |
| 221 | } |
| 222 | DCHECK_LE(current_offset, gap_end) << "Overran gap"; |
| 223 | } |
| 224 | } |
| 225 | // Shuffle fields forward, making use of gaps whenever possible. |
| 226 | template<int n> |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 227 | static void ShuffleForward(size_t* current_field_idx, |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 228 | MemberOffset* field_offset, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 229 | std::deque<ArtField*>* grouped_and_sorted_fields, |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 230 | FieldGaps* gaps) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 231 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 232 | DCHECK(current_field_idx != nullptr); |
| 233 | DCHECK(grouped_and_sorted_fields != nullptr); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 234 | DCHECK(gaps != nullptr); |
| 235 | DCHECK(field_offset != nullptr); |
| 236 | |
| 237 | DCHECK(IsPowerOfTwo(n)); |
| 238 | while (!grouped_and_sorted_fields->empty()) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 239 | ArtField* field = grouped_and_sorted_fields->front(); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 240 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 241 | if (Primitive::ComponentSize(type) < n) { |
| 242 | break; |
| 243 | } |
| 244 | if (!IsAligned<n>(field_offset->Uint32Value())) { |
| 245 | MemberOffset old_offset = *field_offset; |
| 246 | *field_offset = MemberOffset(RoundUp(field_offset->Uint32Value(), n)); |
| 247 | AddFieldGap(old_offset.Uint32Value(), field_offset->Uint32Value(), gaps); |
| 248 | } |
| 249 | CHECK(type != Primitive::kPrimNot) << PrettyField(field); // should be primitive types |
| 250 | grouped_and_sorted_fields->pop_front(); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 251 | if (!gaps->empty() && gaps->top().size >= n) { |
| 252 | FieldGap gap = gaps->top(); |
| 253 | gaps->pop(); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 254 | DCHECK_ALIGNED(gap.start_offset, n); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 255 | field->SetOffset(MemberOffset(gap.start_offset)); |
| 256 | if (gap.size > n) { |
| 257 | AddFieldGap(gap.start_offset + n, gap.start_offset + gap.size, gaps); |
| 258 | } |
| 259 | } else { |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 260 | DCHECK_ALIGNED(field_offset->Uint32Value(), n); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 261 | field->SetOffset(*field_offset); |
| 262 | *field_offset = MemberOffset(field_offset->Uint32Value() + n); |
| 263 | } |
| 264 | ++(*current_field_idx); |
| 265 | } |
| 266 | } |
| 267 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 268 | ClassLinker::ClassLinker(InternTable* intern_table) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 269 | // dex_lock_ is recursive as it may be used in stack dumping. |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 270 | : dex_lock_("ClassLinker dex lock", kDefaultMutexLevel), |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 271 | dex_cache_image_class_lookup_required_(false), |
| 272 | failed_dex_cache_class_lookups_(0), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 273 | class_roots_(nullptr), |
| 274 | array_iftable_(nullptr), |
| 275 | find_array_class_cache_next_victim_(0), |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 276 | init_done_(false), |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 277 | log_new_class_table_roots_(false), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 278 | intern_table_(intern_table), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 279 | quick_resolution_trampoline_(nullptr), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 280 | quick_imt_conflict_trampoline_(nullptr), |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 281 | quick_generic_jni_trampoline_(nullptr), |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 282 | quick_to_interpreter_bridge_trampoline_(nullptr), |
| 283 | image_pointer_size_(sizeof(void*)) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 284 | CHECK(intern_table_ != nullptr); |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 285 | static_assert(kFindArrayCacheSize == arraysize(find_array_class_cache_), |
| 286 | "Array cache size wrong."); |
| 287 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 288 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 289 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 290 | void ClassLinker::InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 291 | VLOG(startup) << "ClassLinker::Init"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 292 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 293 | Thread* const self = Thread::Current(); |
| 294 | Runtime* const runtime = Runtime::Current(); |
| 295 | gc::Heap* const heap = runtime->GetHeap(); |
| 296 | |
| 297 | CHECK(!heap->HasImageSpace()) << "Runtime has image. We should use it."; |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 298 | CHECK(!init_done_); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 299 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 300 | // Use the pointer size from the runtime since we are probably creating the image. |
| 301 | image_pointer_size_ = InstructionSetPointerSize(runtime->GetInstructionSet()); |
| 302 | |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 303 | // java_lang_Class comes first, it's needed for AllocClass |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 304 | // 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] | 305 | heap->IncrementDisableMovingGC(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 306 | StackHandleScope<64> hs(self); // 64 is picked arbitrarily. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 307 | auto class_class_size = mirror::Class::ClassClassSize(image_pointer_size_); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 308 | Handle<mirror::Class> java_lang_Class(hs.NewHandle(down_cast<mirror::Class*>( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 309 | heap->AllocNonMovableObject<true>(self, nullptr, class_class_size, VoidFunctor())))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 310 | CHECK(java_lang_Class.Get() != nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 311 | mirror::Class::SetClassClass(java_lang_Class.Get()); |
| 312 | java_lang_Class->SetClass(java_lang_Class.Get()); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 313 | if (kUseBakerOrBrooksReadBarrier) { |
| 314 | java_lang_Class->AssertReadBarrierPointer(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 315 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 316 | java_lang_Class->SetClassSize(class_class_size); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 317 | java_lang_Class->SetPrimitiveType(Primitive::kPrimNot); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 318 | heap->DecrementDisableMovingGC(self); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 319 | // AllocClass(mirror::Class*) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 320 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 321 | // Class[] is used for reflection support. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 322 | 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] | 323 | Handle<mirror::Class> class_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 324 | AllocClass(self, java_lang_Class.Get(), class_array_class_size))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 325 | class_array_class->SetComponentType(java_lang_Class.Get()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 326 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 327 | // 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] | 328 | Handle<mirror::Class> java_lang_Object(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 329 | AllocClass(self, java_lang_Class.Get(), mirror::Object::ClassSize(image_pointer_size_)))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 330 | CHECK(java_lang_Object.Get() != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 331 | // backfill Object as the super class of Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 332 | java_lang_Class->SetSuperClass(java_lang_Object.Get()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 333 | mirror::Class::SetStatus(java_lang_Object, mirror::Class::kStatusLoaded, self); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 334 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 335 | java_lang_Object->SetObjectSize(sizeof(mirror::Object)); |
| 336 | runtime->SetSentinel(heap->AllocObject<true>(self, |
| 337 | java_lang_Object.Get(), |
| 338 | java_lang_Object->GetObjectSize(), |
| 339 | VoidFunctor())); |
| 340 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 341 | // Object[] next to hold class roots. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 342 | Handle<mirror::Class> object_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 343 | AllocClass(self, java_lang_Class.Get(), |
| 344 | mirror::ObjectArray<mirror::Object>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 345 | object_array_class->SetComponentType(java_lang_Object.Get()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 346 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 347 | // Setup the char (primitive) class to be used for char[]. |
| 348 | Handle<mirror::Class> char_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 349 | AllocClass(self, java_lang_Class.Get(), |
| 350 | mirror::Class::PrimitiveClassSize(image_pointer_size_)))); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 351 | // The primitive char class won't be initialized by |
| 352 | // InitializePrimitiveClass until line 459, but strings (and |
| 353 | // internal char arrays) will be allocated before that and the |
| 354 | // component size, which is computed from the primitive type, needs |
| 355 | // to be set here. |
| 356 | char_class->SetPrimitiveType(Primitive::kPrimChar); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 357 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 358 | // Setup the char[] class to be used for String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 359 | Handle<mirror::Class> char_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 360 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 361 | char_array_class->SetComponentType(char_class.Get()); |
| 362 | mirror::CharArray::SetArrayClass(char_array_class.Get()); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 363 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 364 | // Setup String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 365 | Handle<mirror::Class> java_lang_String(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 366 | AllocClass(self, java_lang_Class.Get(), mirror::String::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 367 | java_lang_String->SetStringClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 368 | mirror::String::SetClass(java_lang_String.Get()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 369 | mirror::Class::SetStatus(java_lang_String, mirror::Class::kStatusResolved, self); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 370 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 371 | // Setup java.lang.ref.Reference. |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 372 | Handle<mirror::Class> java_lang_ref_Reference(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 373 | AllocClass(self, java_lang_Class.Get(), mirror::Reference::ClassSize(image_pointer_size_)))); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 374 | mirror::Reference::SetClass(java_lang_ref_Reference.Get()); |
| 375 | java_lang_ref_Reference->SetObjectSize(mirror::Reference::InstanceSize()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 376 | mirror::Class::SetStatus(java_lang_ref_Reference, mirror::Class::kStatusResolved, self); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 377 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 378 | // 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] | 379 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 380 | mirror::ObjectArray<mirror::Class>::Alloc(self, object_array_class.Get(), |
| 381 | kClassRootsMax)); |
| 382 | CHECK(!class_roots_.IsNull()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 383 | SetClassRoot(kJavaLangClass, java_lang_Class.Get()); |
| 384 | SetClassRoot(kJavaLangObject, java_lang_Object.Get()); |
| 385 | SetClassRoot(kClassArrayClass, class_array_class.Get()); |
| 386 | SetClassRoot(kObjectArrayClass, object_array_class.Get()); |
| 387 | SetClassRoot(kCharArrayClass, char_array_class.Get()); |
| 388 | SetClassRoot(kJavaLangString, java_lang_String.Get()); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 389 | SetClassRoot(kJavaLangRefReference, java_lang_ref_Reference.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 390 | |
| 391 | // Setup the primitive type classes. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 392 | SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass(self, Primitive::kPrimBoolean)); |
| 393 | SetClassRoot(kPrimitiveByte, CreatePrimitiveClass(self, Primitive::kPrimByte)); |
| 394 | SetClassRoot(kPrimitiveShort, CreatePrimitiveClass(self, Primitive::kPrimShort)); |
| 395 | SetClassRoot(kPrimitiveInt, CreatePrimitiveClass(self, Primitive::kPrimInt)); |
| 396 | SetClassRoot(kPrimitiveLong, CreatePrimitiveClass(self, Primitive::kPrimLong)); |
| 397 | SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass(self, Primitive::kPrimFloat)); |
| 398 | SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass(self, Primitive::kPrimDouble)); |
| 399 | SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass(self, Primitive::kPrimVoid)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 400 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 401 | // Create array interface entries to populate once we can load system classes. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 402 | array_iftable_ = GcRoot<mirror::IfTable>(AllocIfTable(self, 2)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 403 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 404 | // Create int array type for AllocDexCache (done in AppendToBootClassPath). |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 405 | Handle<mirror::Class> int_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 406 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize(image_pointer_size_)))); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 407 | int_array_class->SetComponentType(GetClassRoot(kPrimitiveInt)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 408 | mirror::IntArray::SetArrayClass(int_array_class.Get()); |
| 409 | SetClassRoot(kIntArrayClass, int_array_class.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 410 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 411 | // Create long array type for AllocDexCache (done in AppendToBootClassPath). |
| 412 | Handle<mirror::Class> long_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 413 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 414 | long_array_class->SetComponentType(GetClassRoot(kPrimitiveLong)); |
| 415 | mirror::LongArray::SetArrayClass(long_array_class.Get()); |
| 416 | SetClassRoot(kLongArrayClass, long_array_class.Get()); |
| 417 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 418 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 419 | |
Ian Rogers | 52813c9 | 2012-10-11 11:50:38 -0700 | [diff] [blame] | 420 | // Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 421 | Handle<mirror::Class> java_lang_DexCache(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 422 | AllocClass(self, java_lang_Class.Get(), mirror::DexCache::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 423 | SetClassRoot(kJavaLangDexCache, java_lang_DexCache.Get()); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 424 | java_lang_DexCache->SetDexCacheClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 425 | java_lang_DexCache->SetObjectSize(mirror::DexCache::InstanceSize()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 426 | mirror::Class::SetStatus(java_lang_DexCache, mirror::Class::kStatusResolved, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 427 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 428 | // Set up array classes for string, field, method |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 429 | Handle<mirror::Class> object_array_string(hs.NewHandle( |
| 430 | AllocClass(self, java_lang_Class.Get(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 431 | mirror::ObjectArray<mirror::String>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 432 | object_array_string->SetComponentType(java_lang_String.Get()); |
| 433 | SetClassRoot(kJavaLangStringArrayClass, object_array_string.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 434 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 435 | // Create runtime resolution and imt conflict methods. |
| 436 | runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); |
| 437 | runtime->SetImtConflictMethod(runtime->CreateImtConflictMethod()); |
| 438 | runtime->SetImtUnimplementedMethod(runtime->CreateImtConflictMethod()); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 439 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 440 | // Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create |
| 441 | // DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses |
| 442 | // these roots. |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 443 | CHECK_NE(0U, boot_class_path.size()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 444 | for (auto& dex_file : boot_class_path) { |
| 445 | CHECK(dex_file.get() != nullptr); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 446 | AppendToBootClassPath(self, *dex_file); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 447 | opened_dex_files_.push_back(std::move(dex_file)); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 448 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 449 | |
| 450 | // now we can use FindSystemClass |
| 451 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 452 | // run char class through InitializePrimitiveClass to finish init |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 453 | InitializePrimitiveClass(char_class.Get(), Primitive::kPrimChar); |
| 454 | SetClassRoot(kPrimitiveChar, char_class.Get()); // needs descriptor |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 455 | |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 456 | // Set up GenericJNI entrypoint. That is mainly a hack for common_compiler_test.h so that |
| 457 | // we do not need friend classes or a publicly exposed setter. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 458 | quick_generic_jni_trampoline_ = GetQuickGenericJniStub(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 459 | if (!runtime->IsAotCompiler()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 460 | // 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] | 461 | quick_resolution_trampoline_ = GetQuickResolutionStub(); |
| 462 | quick_imt_conflict_trampoline_ = GetQuickImtConflictStub(); |
| 463 | quick_to_interpreter_bridge_trampoline_ = GetQuickToInterpreterBridge(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 464 | } |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 465 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 466 | // Object, String and DexCache need to be rerun through FindSystemClass to finish init |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 467 | mirror::Class::SetStatus(java_lang_Object, mirror::Class::kStatusNotReady, self); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 468 | CHECK_EQ(java_lang_Object.Get(), FindSystemClass(self, "Ljava/lang/Object;")); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 469 | CHECK_EQ(java_lang_Object->GetObjectSize(), mirror::Object::InstanceSize()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 470 | mirror::Class::SetStatus(java_lang_String, mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 471 | mirror::Class* String_class = FindSystemClass(self, "Ljava/lang/String;"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 472 | if (java_lang_String.Get() != String_class) { |
| 473 | std::ostringstream os1, os2; |
| 474 | java_lang_String->DumpClass(os1, mirror::Class::kDumpClassFullDetail); |
| 475 | String_class->DumpClass(os2, mirror::Class::kDumpClassFullDetail); |
| 476 | LOG(FATAL) << os1.str() << "\n\n" << os2.str(); |
| 477 | } |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 478 | mirror::Class::SetStatus(java_lang_DexCache, mirror::Class::kStatusNotReady, self); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 479 | CHECK_EQ(java_lang_DexCache.Get(), FindSystemClass(self, "Ljava/lang/DexCache;")); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 480 | CHECK_EQ(java_lang_DexCache->GetObjectSize(), mirror::DexCache::InstanceSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 481 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 482 | // 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] | 483 | SetClassRoot(kBooleanArrayClass, FindSystemClass(self, "[Z")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 484 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 485 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 486 | SetClassRoot(kByteArrayClass, FindSystemClass(self, "[B")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 487 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 488 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 489 | CHECK_EQ(char_array_class.Get(), FindSystemClass(self, "[C")); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 490 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 491 | SetClassRoot(kShortArrayClass, FindSystemClass(self, "[S")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 492 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 493 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 494 | CHECK_EQ(int_array_class.Get(), FindSystemClass(self, "[I")); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 495 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 496 | CHECK_EQ(long_array_class.Get(), FindSystemClass(self, "[J")); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 497 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 498 | SetClassRoot(kFloatArrayClass, FindSystemClass(self, "[F")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 499 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 500 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 501 | SetClassRoot(kDoubleArrayClass, FindSystemClass(self, "[D")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 502 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 503 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 504 | CHECK_EQ(class_array_class.Get(), FindSystemClass(self, "[Ljava/lang/Class;")); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 505 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 506 | CHECK_EQ(object_array_class.Get(), FindSystemClass(self, "[Ljava/lang/Object;")); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 507 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 508 | // Setup the single, global copy of "iftable". |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 509 | auto java_lang_Cloneable = hs.NewHandle(FindSystemClass(self, "Ljava/lang/Cloneable;")); |
| 510 | CHECK(java_lang_Cloneable.Get() != nullptr); |
| 511 | auto java_io_Serializable = hs.NewHandle(FindSystemClass(self, "Ljava/io/Serializable;")); |
| 512 | CHECK(java_io_Serializable.Get() != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 513 | // We assume that Cloneable/Serializable don't have superinterfaces -- normally we'd have to |
| 514 | // crawl up and explicitly list all of the supers as well. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 515 | array_iftable_.Read()->SetInterface(0, java_lang_Cloneable.Get()); |
| 516 | array_iftable_.Read()->SetInterface(1, java_io_Serializable.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 517 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 518 | // Sanity check Class[] and Object[]'s interfaces. GetDirectInterface may cause thread |
| 519 | // suspension. |
| 520 | CHECK_EQ(java_lang_Cloneable.Get(), |
| 521 | mirror::Class::GetDirectInterface(self, class_array_class, 0)); |
| 522 | CHECK_EQ(java_io_Serializable.Get(), |
| 523 | mirror::Class::GetDirectInterface(self, class_array_class, 1)); |
| 524 | CHECK_EQ(java_lang_Cloneable.Get(), |
| 525 | mirror::Class::GetDirectInterface(self, object_array_class, 0)); |
| 526 | CHECK_EQ(java_io_Serializable.Get(), |
| 527 | mirror::Class::GetDirectInterface(self, object_array_class, 1)); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 528 | // Run Class, ArtField, and ArtMethod through FindSystemClass. This initializes their |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 529 | // dex_cache_ fields and register them in class_table_. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 530 | CHECK_EQ(java_lang_Class.Get(), FindSystemClass(self, "Ljava/lang/Class;")); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 531 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 532 | CHECK_EQ(object_array_string.Get(), |
| 533 | FindSystemClass(self, GetClassRootDescriptor(kJavaLangStringArrayClass))); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 534 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 535 | // End of special init trickery, subsequent classes may be loaded via FindSystemClass. |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 536 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 537 | // Create java.lang.reflect.Proxy root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 538 | SetClassRoot(kJavaLangReflectProxy, FindSystemClass(self, "Ljava/lang/reflect/Proxy;")); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 539 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 540 | // Create java.lang.reflect.Field.class root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 541 | auto* class_root = FindSystemClass(self, "Ljava/lang/reflect/Field;"); |
| 542 | CHECK(class_root != nullptr); |
| 543 | SetClassRoot(kJavaLangReflectField, class_root); |
| 544 | mirror::Field::SetClass(class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 545 | |
| 546 | // Create java.lang.reflect.Field array root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 547 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Field;"); |
| 548 | CHECK(class_root != nullptr); |
| 549 | SetClassRoot(kJavaLangReflectFieldArrayClass, class_root); |
| 550 | mirror::Field::SetArrayClass(class_root); |
| 551 | |
| 552 | // Create java.lang.reflect.Constructor.class root and array root. |
| 553 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Constructor;"); |
| 554 | CHECK(class_root != nullptr); |
| 555 | SetClassRoot(kJavaLangReflectConstructor, class_root); |
| 556 | mirror::Constructor::SetClass(class_root); |
| 557 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Constructor;"); |
| 558 | CHECK(class_root != nullptr); |
| 559 | SetClassRoot(kJavaLangReflectConstructorArrayClass, class_root); |
| 560 | mirror::Constructor::SetArrayClass(class_root); |
| 561 | |
| 562 | // Create java.lang.reflect.Method.class root and array root. |
| 563 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Method;"); |
| 564 | CHECK(class_root != nullptr); |
| 565 | SetClassRoot(kJavaLangReflectMethod, class_root); |
| 566 | mirror::Method::SetClass(class_root); |
| 567 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Method;"); |
| 568 | CHECK(class_root != nullptr); |
| 569 | SetClassRoot(kJavaLangReflectMethodArrayClass, class_root); |
| 570 | mirror::Method::SetArrayClass(class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 571 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 572 | // 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] | 573 | // finish initializing Reference class |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 574 | mirror::Class::SetStatus(java_lang_ref_Reference, mirror::Class::kStatusNotReady, self); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 575 | CHECK_EQ(java_lang_ref_Reference.Get(), FindSystemClass(self, "Ljava/lang/ref/Reference;")); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 576 | CHECK_EQ(java_lang_ref_Reference->GetObjectSize(), mirror::Reference::InstanceSize()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 577 | CHECK_EQ(java_lang_ref_Reference->GetClassSize(), |
| 578 | mirror::Reference::ClassSize(image_pointer_size_)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 579 | class_root = FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 580 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 581 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagFinalizerReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 582 | class_root = FindSystemClass(self, "Ljava/lang/ref/PhantomReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 583 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 584 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagPhantomReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 585 | class_root = FindSystemClass(self, "Ljava/lang/ref/SoftReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 586 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 587 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagSoftReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 588 | class_root = FindSystemClass(self, "Ljava/lang/ref/WeakReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 589 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 590 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagWeakReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 591 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 592 | // Setup the ClassLoader, verifying the object_size_. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 593 | class_root = FindSystemClass(self, "Ljava/lang/ClassLoader;"); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 594 | class_root->SetClassLoaderClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 595 | CHECK_EQ(class_root->GetObjectSize(), mirror::ClassLoader::InstanceSize()); |
| 596 | SetClassRoot(kJavaLangClassLoader, class_root); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 597 | |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 598 | // Set up java.lang.Throwable, java.lang.ClassNotFoundException, and |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 599 | // java.lang.StackTraceElement as a convenience. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 600 | SetClassRoot(kJavaLangThrowable, FindSystemClass(self, "Ljava/lang/Throwable;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 601 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 602 | SetClassRoot(kJavaLangClassNotFoundException, |
| 603 | FindSystemClass(self, "Ljava/lang/ClassNotFoundException;")); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 604 | SetClassRoot(kJavaLangStackTraceElement, FindSystemClass(self, "Ljava/lang/StackTraceElement;")); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 605 | SetClassRoot(kJavaLangStackTraceElementArrayClass, |
| 606 | FindSystemClass(self, "[Ljava/lang/StackTraceElement;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 607 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 608 | |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 609 | // Ensure void type is resolved in the core's dex cache so java.lang.Void is correctly |
| 610 | // initialized. |
| 611 | { |
| 612 | const DexFile& dex_file = java_lang_Object->GetDexFile(); |
| 613 | const DexFile::StringId* void_string_id = dex_file.FindStringId("V"); |
| 614 | CHECK(void_string_id != nullptr); |
| 615 | uint32_t void_string_index = dex_file.GetIndexForStringId(*void_string_id); |
| 616 | const DexFile::TypeId* void_type_id = dex_file.FindTypeId(void_string_index); |
| 617 | CHECK(void_type_id != nullptr); |
| 618 | uint16_t void_type_idx = dex_file.GetIndexForTypeId(*void_type_id); |
| 619 | // Now we resolve void type so the dex cache contains it. We use java.lang.Object class |
| 620 | // as referrer so the used dex cache is core's one. |
| 621 | mirror::Class* resolved_type = ResolveType(dex_file, void_type_idx, java_lang_Object.Get()); |
| 622 | CHECK_EQ(resolved_type, GetClassRoot(kPrimitiveVoid)); |
| 623 | self->AssertNoPendingException(); |
| 624 | } |
| 625 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 626 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 627 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 628 | VLOG(startup) << "ClassLinker::InitFromCompiler exiting"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 631 | void ClassLinker::FinishInit(Thread* self) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 632 | VLOG(startup) << "ClassLinker::FinishInit entering"; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 633 | |
| 634 | // Let the heap know some key offsets into java.lang.ref instances |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 635 | // Note: we hard code the field indexes here rather than using FindInstanceField |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 636 | // as the types of the field can't be resolved prior to the runtime being |
| 637 | // fully initialized |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 638 | mirror::Class* java_lang_ref_Reference = GetClassRoot(kJavaLangRefReference); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 639 | mirror::Class* java_lang_ref_FinalizerReference = |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 640 | FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 641 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 642 | ArtField* pendingNext = java_lang_ref_Reference->GetInstanceField(0); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 643 | CHECK_STREQ(pendingNext->GetName(), "pendingNext"); |
| 644 | CHECK_STREQ(pendingNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 645 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 646 | ArtField* queue = java_lang_ref_Reference->GetInstanceField(1); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 647 | CHECK_STREQ(queue->GetName(), "queue"); |
| 648 | CHECK_STREQ(queue->GetTypeDescriptor(), "Ljava/lang/ref/ReferenceQueue;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 649 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 650 | ArtField* queueNext = java_lang_ref_Reference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 651 | CHECK_STREQ(queueNext->GetName(), "queueNext"); |
| 652 | CHECK_STREQ(queueNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 653 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 654 | ArtField* referent = java_lang_ref_Reference->GetInstanceField(3); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 655 | CHECK_STREQ(referent->GetName(), "referent"); |
| 656 | CHECK_STREQ(referent->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 657 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 658 | ArtField* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 659 | CHECK_STREQ(zombie->GetName(), "zombie"); |
| 660 | CHECK_STREQ(zombie->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 661 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 662 | // ensure all class_roots_ are initialized |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 663 | for (size_t i = 0; i < kClassRootsMax; i++) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 664 | ClassRoot class_root = static_cast<ClassRoot>(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 665 | mirror::Class* klass = GetClassRoot(class_root); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 666 | CHECK(klass != nullptr); |
| 667 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != nullptr); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 668 | // note SetClassRoot does additional validation. |
| 669 | // if possible add new checks there to catch errors early |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 672 | CHECK(!array_iftable_.IsNull()); |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 673 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 674 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 675 | // that Object, Class, and Object[] are setup |
| 676 | init_done_ = true; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 677 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 678 | VLOG(startup) << "ClassLinker::FinishInit exiting"; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 681 | void ClassLinker::RunRootClinits() { |
| 682 | Thread* self = Thread::Current(); |
| 683 | for (size_t i = 0; i < ClassLinker::kClassRootsMax; ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 684 | mirror::Class* c = GetClassRoot(ClassRoot(i)); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 685 | if (!c->IsArrayClass() && !c->IsPrimitive()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 686 | StackHandleScope<1> hs(self); |
| 687 | Handle<mirror::Class> h_class(hs.NewHandle(GetClassRoot(ClassRoot(i)))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 688 | EnsureInitialized(self, h_class, true, true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 689 | self->AssertNoPendingException(); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 694 | const OatFile* ClassLinker::RegisterOatFile(const OatFile* oat_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 695 | WriterMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 696 | if (kIsDebugBuild) { |
| 697 | for (size_t i = 0; i < oat_files_.size(); ++i) { |
| 698 | CHECK_NE(oat_file, oat_files_[i]) << oat_file->GetLocation(); |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 699 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 700 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 701 | VLOG(class_linker) << "Registering " << oat_file->GetLocation(); |
| 702 | oat_files_.push_back(oat_file); |
| 703 | return oat_file; |
Brian Carlstrom | 866c862 | 2012-01-06 16:35:13 -0800 | [diff] [blame] | 704 | } |
| 705 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 706 | OatFile& ClassLinker::GetImageOatFile(gc::space::ImageSpace* space) { |
| 707 | VLOG(startup) << "ClassLinker::GetImageOatFile entering"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 708 | OatFile* oat_file = space->ReleaseOatFile(); |
| 709 | CHECK_EQ(RegisterOatFile(oat_file), oat_file); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 710 | VLOG(startup) << "ClassLinker::GetImageOatFile exiting"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 711 | return *oat_file; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 714 | class DexFileAndClassPair : ValueObject { |
| 715 | public: |
| 716 | DexFileAndClassPair(const DexFile* dex_file, size_t current_class_index, bool from_loaded_oat) |
| 717 | : cached_descriptor_(GetClassDescriptor(dex_file, current_class_index)), |
| 718 | dex_file_(dex_file), |
| 719 | current_class_index_(current_class_index), |
| 720 | from_loaded_oat_(from_loaded_oat) {} |
| 721 | |
| 722 | DexFileAndClassPair(const DexFileAndClassPair&) = default; |
| 723 | |
| 724 | DexFileAndClassPair& operator=(const DexFileAndClassPair& rhs) { |
| 725 | cached_descriptor_ = rhs.cached_descriptor_; |
| 726 | dex_file_ = rhs.dex_file_; |
| 727 | current_class_index_ = rhs.current_class_index_; |
| 728 | from_loaded_oat_ = rhs.from_loaded_oat_; |
| 729 | return *this; |
| 730 | } |
| 731 | |
| 732 | const char* GetCachedDescriptor() const { |
| 733 | return cached_descriptor_; |
| 734 | } |
| 735 | |
| 736 | bool operator<(const DexFileAndClassPair& rhs) const { |
| 737 | const char* lhsDescriptor = cached_descriptor_; |
| 738 | const char* rhsDescriptor = rhs.cached_descriptor_; |
| 739 | int cmp = strcmp(lhsDescriptor, rhsDescriptor); |
| 740 | if (cmp != 0) { |
Andreas Gampe | 0cba004 | 2015-04-29 20:47:16 -0700 | [diff] [blame] | 741 | // Note that the order must be reversed. We want to iterate over the classes in dex files. |
| 742 | // They are sorted lexicographically. Thus, the priority-queue must be a min-queue. |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 743 | return cmp > 0; |
| 744 | } |
| 745 | return dex_file_ < rhs.dex_file_; |
| 746 | } |
| 747 | |
| 748 | bool DexFileHasMoreClasses() const { |
| 749 | return current_class_index_ + 1 < dex_file_->NumClassDefs(); |
| 750 | } |
| 751 | |
| 752 | DexFileAndClassPair GetNext() const { |
| 753 | return DexFileAndClassPair(dex_file_, current_class_index_ + 1, from_loaded_oat_); |
| 754 | } |
| 755 | |
| 756 | size_t GetCurrentClassIndex() const { |
| 757 | return current_class_index_; |
| 758 | } |
| 759 | |
| 760 | bool FromLoadedOat() const { |
| 761 | return from_loaded_oat_; |
| 762 | } |
| 763 | |
| 764 | const DexFile* GetDexFile() const { |
| 765 | return dex_file_; |
| 766 | } |
| 767 | |
Andreas Gampe | 0cba004 | 2015-04-29 20:47:16 -0700 | [diff] [blame] | 768 | void DeleteDexFile() { |
| 769 | delete dex_file_; |
| 770 | dex_file_ = nullptr; |
| 771 | } |
| 772 | |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 773 | private: |
| 774 | static const char* GetClassDescriptor(const DexFile* dex_file, size_t index) { |
| 775 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(static_cast<uint16_t>(index)); |
| 776 | return dex_file->StringByTypeIdx(class_def.class_idx_); |
| 777 | } |
| 778 | |
| 779 | const char* cached_descriptor_; |
| 780 | const DexFile* dex_file_; |
| 781 | size_t current_class_index_; |
| 782 | bool from_loaded_oat_; // We only need to compare mismatches between what we load now |
| 783 | // and what was loaded before. Any old duplicates must have been |
| 784 | // OK, and any new "internal" duplicates are as well (they must |
| 785 | // be from multidex, which resolves correctly). |
| 786 | }; |
| 787 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 788 | static void AddDexFilesFromOat(const OatFile* oat_file, |
| 789 | bool already_loaded, |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 790 | std::priority_queue<DexFileAndClassPair>* heap) { |
| 791 | const std::vector<const OatDexFile*>& oat_dex_files = oat_file->GetOatDexFiles(); |
| 792 | for (const OatDexFile* oat_dex_file : oat_dex_files) { |
| 793 | std::string error; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 794 | std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error); |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 795 | if (dex_file.get() == nullptr) { |
| 796 | LOG(WARNING) << "Could not create dex file from oat file: " << error; |
| 797 | } else { |
| 798 | if (dex_file->NumClassDefs() > 0U) { |
| 799 | heap->emplace(dex_file.release(), 0U, already_loaded); |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | |
Andreas Gampe | 0cba004 | 2015-04-29 20:47:16 -0700 | [diff] [blame] | 805 | static void AddNext(DexFileAndClassPair* original, |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 806 | std::priority_queue<DexFileAndClassPair>* heap) { |
Andreas Gampe | 0cba004 | 2015-04-29 20:47:16 -0700 | [diff] [blame] | 807 | if (original->DexFileHasMoreClasses()) { |
| 808 | heap->push(original->GetNext()); |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 809 | } else { |
| 810 | // Need to delete the dex file. |
Andreas Gampe | 0cba004 | 2015-04-29 20:47:16 -0700 | [diff] [blame] | 811 | original->DeleteDexFile(); |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | |
| 815 | static void FreeDexFilesInHeap(std::priority_queue<DexFileAndClassPair>* heap) { |
| 816 | while (!heap->empty()) { |
| 817 | delete heap->top().GetDexFile(); |
| 818 | heap->pop(); |
| 819 | } |
| 820 | } |
| 821 | |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 822 | const OatFile* ClassLinker::GetBootOatFile() { |
Andreas Gampe | 88da3b0 | 2015-06-12 20:38:49 -0700 | [diff] [blame] | 823 | gc::space::ImageSpace* image_space = Runtime::Current()->GetHeap()->GetImageSpace(); |
| 824 | if (image_space == nullptr) { |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 825 | return nullptr; |
| 826 | } |
Andreas Gampe | 88da3b0 | 2015-06-12 20:38:49 -0700 | [diff] [blame] | 827 | return image_space->GetOatFile(); |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | const OatFile* ClassLinker::GetPrimaryOatFile() { |
| 831 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
| 832 | const OatFile* boot_oat_file = GetBootOatFile(); |
| 833 | if (boot_oat_file != nullptr) { |
| 834 | for (const OatFile* oat_file : oat_files_) { |
| 835 | if (oat_file != boot_oat_file) { |
| 836 | return oat_file; |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | return nullptr; |
| 841 | } |
| 842 | |
Andreas Gampe | 94329d3 | 2015-04-24 20:22:06 -0700 | [diff] [blame] | 843 | // Check for class-def collisions in dex files. |
| 844 | // |
| 845 | // This works by maintaining a heap with one class from each dex file, sorted by the class |
| 846 | // descriptor. Then a dex-file/class pair is continually removed from the heap and compared |
| 847 | // against the following top element. If the descriptor is the same, it is now checked whether |
| 848 | // the two elements agree on whether their dex file was from an already-loaded oat-file or the |
| 849 | // new oat file. Any disagreement indicates a collision. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 850 | bool ClassLinker::HasCollisions(const OatFile* oat_file, std::string* error_msg) { |
Andreas Gampe | 69b5d8f | 2015-06-03 15:43:16 -0700 | [diff] [blame] | 851 | if (!kDuplicateClassesCheck) { |
| 852 | return false; |
| 853 | } |
| 854 | |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 855 | // Dex files are registered late - once a class is actually being loaded. We have to compare |
Andreas Gampe | 0cba004 | 2015-04-29 20:47:16 -0700 | [diff] [blame] | 856 | // against the open oat files. Take the dex_lock_ that protects oat_files_ accesses. |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 857 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
| 858 | |
Andreas Gampe | 0cba004 | 2015-04-29 20:47:16 -0700 | [diff] [blame] | 859 | std::priority_queue<DexFileAndClassPair> queue; |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 860 | |
| 861 | // Add dex files from already loaded oat files, but skip boot. |
| 862 | { |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 863 | const OatFile* boot_oat = GetBootOatFile(); |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 864 | for (const OatFile* loaded_oat_file : oat_files_) { |
| 865 | if (loaded_oat_file == boot_oat) { |
| 866 | continue; |
| 867 | } |
Andreas Gampe | 0cba004 | 2015-04-29 20:47:16 -0700 | [diff] [blame] | 868 | AddDexFilesFromOat(loaded_oat_file, true, &queue); |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | |
Andreas Gampe | 0cba004 | 2015-04-29 20:47:16 -0700 | [diff] [blame] | 872 | if (queue.empty()) { |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 873 | // No other oat files, return early. |
| 874 | return false; |
| 875 | } |
| 876 | |
| 877 | // Add dex files from the oat file to check. |
Andreas Gampe | 0cba004 | 2015-04-29 20:47:16 -0700 | [diff] [blame] | 878 | AddDexFilesFromOat(oat_file, false, &queue); |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 879 | |
Andreas Gampe | 0cba004 | 2015-04-29 20:47:16 -0700 | [diff] [blame] | 880 | // Now drain the queue. |
| 881 | while (!queue.empty()) { |
| 882 | DexFileAndClassPair compare_pop = queue.top(); |
| 883 | queue.pop(); |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 884 | |
| 885 | // Compare against the following elements. |
Andreas Gampe | 0cba004 | 2015-04-29 20:47:16 -0700 | [diff] [blame] | 886 | while (!queue.empty()) { |
| 887 | DexFileAndClassPair top = queue.top(); |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 888 | |
| 889 | if (strcmp(compare_pop.GetCachedDescriptor(), top.GetCachedDescriptor()) == 0) { |
| 890 | // Same descriptor. Check whether it's crossing old-oat-files to new-oat-files. |
| 891 | if (compare_pop.FromLoadedOat() != top.FromLoadedOat()) { |
| 892 | *error_msg = |
| 893 | StringPrintf("Found duplicated class when checking oat files: '%s' in %s and %s", |
| 894 | compare_pop.GetCachedDescriptor(), |
| 895 | compare_pop.GetDexFile()->GetLocation().c_str(), |
| 896 | top.GetDexFile()->GetLocation().c_str()); |
Andreas Gampe | 0cba004 | 2015-04-29 20:47:16 -0700 | [diff] [blame] | 897 | FreeDexFilesInHeap(&queue); |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 898 | return true; |
| 899 | } |
| 900 | // Pop it. |
Andreas Gampe | 0cba004 | 2015-04-29 20:47:16 -0700 | [diff] [blame] | 901 | queue.pop(); |
| 902 | AddNext(&top, &queue); |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 903 | } else { |
| 904 | // Something else. Done here. |
| 905 | break; |
| 906 | } |
| 907 | } |
Andreas Gampe | 0cba004 | 2015-04-29 20:47:16 -0700 | [diff] [blame] | 908 | AddNext(&compare_pop, &queue); |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | return false; |
| 912 | } |
| 913 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 914 | std::vector<std::unique_ptr<const DexFile>> ClassLinker::OpenDexFilesFromOat( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 915 | const char* dex_location, const char* oat_location, |
| 916 | std::vector<std::string>* error_msgs) { |
| 917 | CHECK(error_msgs != nullptr); |
| 918 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 919 | // Verify we aren't holding the mutator lock, which could starve GC if we |
| 920 | // have to generate or relocate an oat file. |
| 921 | Locks::mutator_lock_->AssertNotHeld(Thread::Current()); |
| 922 | |
| 923 | OatFileAssistant oat_file_assistant(dex_location, oat_location, kRuntimeISA, |
| 924 | !Runtime::Current()->IsAotCompiler()); |
| 925 | |
| 926 | // Lock the target oat location to avoid races generating and loading the |
| 927 | // oat file. |
| 928 | std::string error_msg; |
| 929 | if (!oat_file_assistant.Lock(&error_msg)) { |
| 930 | // Don't worry too much if this fails. If it does fail, it's unlikely we |
| 931 | // can generate an oat file anyway. |
| 932 | VLOG(class_linker) << "OatFileAssistant::Lock: " << error_msg; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 933 | } |
| 934 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 935 | // Check if we already have an up-to-date oat file open. |
| 936 | const OatFile* source_oat_file = nullptr; |
| 937 | { |
| 938 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
| 939 | for (const OatFile* oat_file : oat_files_) { |
| 940 | CHECK(oat_file != nullptr); |
| 941 | if (oat_file_assistant.GivenOatFileIsUpToDate(*oat_file)) { |
| 942 | source_oat_file = oat_file; |
| 943 | break; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 944 | } |
| 945 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 946 | } |
| 947 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 948 | // If we didn't have an up-to-date oat file open, try to load one from disk. |
| 949 | if (source_oat_file == nullptr) { |
| 950 | // Update the oat file on disk if we can. This may fail, but that's okay. |
| 951 | // Best effort is all that matters here. |
| 952 | if (!oat_file_assistant.MakeUpToDate(&error_msg)) { |
| 953 | LOG(WARNING) << error_msg; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 954 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 955 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 956 | // Get the oat file on disk. |
| 957 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 958 | if (oat_file.get() != nullptr) { |
Andreas Gampe | 0cba004 | 2015-04-29 20:47:16 -0700 | [diff] [blame] | 959 | // Take the file only if it has no collisions, or we must take it because of preopting. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 960 | bool accept_oat_file = !HasCollisions(oat_file.get(), &error_msg); |
Andreas Gampe | 0cba004 | 2015-04-29 20:47:16 -0700 | [diff] [blame] | 961 | if (!accept_oat_file) { |
| 962 | // Failed the collision check. Print warning. |
Andreas Gampe | 94329d3 | 2015-04-24 20:22:06 -0700 | [diff] [blame] | 963 | if (Runtime::Current()->IsDexFileFallbackEnabled()) { |
| 964 | LOG(WARNING) << "Found duplicate classes, falling back to interpreter mode for " |
| 965 | << dex_location; |
| 966 | } else { |
| 967 | LOG(WARNING) << "Found duplicate classes, dex-file-fallback disabled, will be failing to " |
| 968 | " load classes for " << dex_location; |
| 969 | } |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 970 | LOG(WARNING) << error_msg; |
Andreas Gampe | 0cba004 | 2015-04-29 20:47:16 -0700 | [diff] [blame] | 971 | |
| 972 | // However, if the app was part of /system and preopted, there is no original dex file |
| 973 | // available. In that case grudgingly accept the oat file. |
| 974 | if (!DexFile::MaybeDex(dex_location)) { |
| 975 | accept_oat_file = true; |
| 976 | LOG(WARNING) << "Dex location " << dex_location << " does not seem to include dex file. " |
| 977 | << "Allow oat file use. This is potentially dangerous."; |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | if (accept_oat_file) { |
| 982 | source_oat_file = oat_file.release(); |
| 983 | RegisterOatFile(source_oat_file); |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 984 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 985 | } |
| 986 | } |
| 987 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 988 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 989 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 990 | // Load the dex files from the oat file. |
| 991 | if (source_oat_file != nullptr) { |
| 992 | dex_files = oat_file_assistant.LoadDexFiles(*source_oat_file, dex_location); |
| 993 | if (dex_files.empty()) { |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 994 | error_msgs->push_back("Failed to open dex files from " |
| 995 | + source_oat_file->GetLocation()); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 996 | } |
| 997 | } |
| 998 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 999 | // Fall back to running out of the original dex file if we couldn't load any |
| 1000 | // dex_files from the oat file. |
| 1001 | if (dex_files.empty()) { |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 1002 | if (oat_file_assistant.HasOriginalDexFiles()) { |
| 1003 | if (Runtime::Current()->IsDexFileFallbackEnabled()) { |
| 1004 | if (!DexFile::Open(dex_location, dex_location, &error_msg, &dex_files)) { |
| 1005 | LOG(WARNING) << error_msg; |
| 1006 | error_msgs->push_back("Failed to open dex files from " + std::string(dex_location)); |
| 1007 | } |
| 1008 | } else { |
| 1009 | error_msgs->push_back("Fallback mode disabled, skipping dex files."); |
Jean Christophe Beyler | 24e04aa | 2014-09-12 12:03:25 -0700 | [diff] [blame] | 1010 | } |
| 1011 | } else { |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 1012 | error_msgs->push_back("No original dex files found for dex location " |
| 1013 | + std::string(dex_location)); |
Jean Christophe Beyler | 24e04aa | 2014-09-12 12:03:25 -0700 | [diff] [blame] | 1014 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1015 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1016 | return dex_files; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1019 | const OatFile* ClassLinker::FindOpenedOatFileFromOatLocation(const std::string& oat_location) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1020 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1021 | for (size_t i = 0; i < oat_files_.size(); i++) { |
| 1022 | const OatFile* oat_file = oat_files_[i]; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1023 | DCHECK(oat_file != nullptr); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1024 | if (oat_file->GetLocation() == oat_location) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1025 | return oat_file; |
| 1026 | } |
| 1027 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1028 | return nullptr; |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 1029 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1030 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 1031 | static void SanityCheckArtMethod(ArtMethod* m, |
| 1032 | mirror::Class* expected_class, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1033 | gc::space::ImageSpace* space) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1034 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1035 | if (m->IsRuntimeMethod()) { |
| 1036 | CHECK(m->GetDeclaringClass() == nullptr) << PrettyMethod(m); |
| 1037 | } else if (m->IsMiranda()) { |
| 1038 | CHECK(m->GetDeclaringClass() != nullptr) << PrettyMethod(m); |
| 1039 | } else if (expected_class != nullptr) { |
| 1040 | CHECK_EQ(m->GetDeclaringClassUnchecked(), expected_class) << PrettyMethod(m); |
| 1041 | } |
| 1042 | if (space != nullptr) { |
| 1043 | auto& header = space->GetImageHeader(); |
| 1044 | auto& methods = header.GetMethodsSection(); |
| 1045 | auto offset = reinterpret_cast<uint8_t*>(m) - space->Begin(); |
| 1046 | CHECK(methods.Contains(offset)) << m << " not in " << methods; |
| 1047 | } |
| 1048 | } |
| 1049 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 1050 | static void SanityCheckArtMethodPointerArray(mirror::PointerArray* arr, |
| 1051 | mirror::Class* expected_class, |
| 1052 | size_t pointer_size, |
| 1053 | gc::space::ImageSpace* space) |
| 1054 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1055 | CHECK(arr != nullptr); |
| 1056 | for (int32_t j = 0; j < arr->GetLength(); ++j) { |
| 1057 | auto* method = arr->GetElementPtrSize<ArtMethod*>(j, pointer_size); |
| 1058 | // expected_class == null means we are a dex cache. |
| 1059 | if (expected_class != nullptr) { |
| 1060 | CHECK(method != nullptr); |
| 1061 | } |
| 1062 | if (method != nullptr) { |
| 1063 | SanityCheckArtMethod(method, expected_class, space); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1064 | } |
| 1065 | } |
| 1066 | } |
| 1067 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1068 | static void SanityCheckArtMethodPointerArray( |
| 1069 | ArtMethod** arr, |
| 1070 | size_t size, |
| 1071 | size_t pointer_size, |
| 1072 | gc::space::ImageSpace* space) SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1073 | CHECK_EQ(arr != nullptr, size != 0u); |
| 1074 | if (arr != nullptr) { |
| 1075 | auto offset = reinterpret_cast<uint8_t*>(arr) - space->Begin(); |
| 1076 | CHECK(space->GetImageHeader().GetImageSection( |
| 1077 | ImageHeader::kSectionDexCacheArrays).Contains(offset)); |
| 1078 | } |
| 1079 | for (size_t j = 0; j < size; ++j) { |
| 1080 | ArtMethod* method = mirror::DexCache::GetElementPtrSize(arr, j, pointer_size); |
| 1081 | // expected_class == null means we are a dex cache. |
| 1082 | if (method != nullptr) { |
| 1083 | SanityCheckArtMethod(method, nullptr, space); |
| 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1088 | static void SanityCheckObjectsCallback(mirror::Object* obj, void* arg ATTRIBUTE_UNUSED) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1089 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1090 | DCHECK(obj != nullptr); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1091 | CHECK(obj->GetClass() != nullptr) << "Null class in object " << obj; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1092 | CHECK(obj->GetClass()->GetClass() != nullptr) << "Null class class " << obj; |
| 1093 | if (obj->IsClass()) { |
| 1094 | auto klass = obj->AsClass(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1095 | for (ArtField& field : klass->GetIFields()) { |
| 1096 | CHECK_EQ(field.GetDeclaringClass(), klass); |
| 1097 | } |
| 1098 | for (ArtField& field : klass->GetSFields()) { |
| 1099 | CHECK_EQ(field.GetDeclaringClass(), klass); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1100 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1101 | auto* runtime = Runtime::Current(); |
| 1102 | auto* image_space = runtime->GetHeap()->GetImageSpace(); |
| 1103 | auto pointer_size = runtime->GetClassLinker()->GetImagePointerSize(); |
| 1104 | for (auto& m : klass->GetDirectMethods(pointer_size)) { |
| 1105 | SanityCheckArtMethod(&m, klass, image_space); |
| 1106 | } |
| 1107 | for (auto& m : klass->GetVirtualMethods(pointer_size)) { |
| 1108 | SanityCheckArtMethod(&m, klass, image_space); |
| 1109 | } |
| 1110 | auto* vtable = klass->GetVTable(); |
| 1111 | if (vtable != nullptr) { |
| 1112 | SanityCheckArtMethodPointerArray(vtable, nullptr, pointer_size, image_space); |
| 1113 | } |
| 1114 | if (klass->ShouldHaveEmbeddedImtAndVTable()) { |
| 1115 | for (size_t i = 0; i < mirror::Class::kImtSize; ++i) { |
| 1116 | SanityCheckArtMethod(klass->GetEmbeddedImTableEntry(i, pointer_size), nullptr, image_space); |
| 1117 | } |
| 1118 | for (int32_t i = 0; i < klass->GetEmbeddedVTableLength(); ++i) { |
| 1119 | SanityCheckArtMethod(klass->GetEmbeddedVTableEntry(i, pointer_size), nullptr, image_space); |
| 1120 | } |
| 1121 | } |
| 1122 | auto* iftable = klass->GetIfTable(); |
| 1123 | if (iftable != nullptr) { |
| 1124 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 1125 | if (iftable->GetMethodArrayCount(i) > 0) { |
| 1126 | SanityCheckArtMethodPointerArray(iftable->GetMethodArray(i), nullptr, pointer_size, |
| 1127 | image_space); |
| 1128 | } |
| 1129 | } |
| 1130 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 1134 | // Set image methods' entry point to interpreter. |
| 1135 | class SetInterpreterEntrypointArtMethodVisitor : public ArtMethodVisitor { |
| 1136 | public: |
| 1137 | explicit SetInterpreterEntrypointArtMethodVisitor(size_t image_pointer_size) |
| 1138 | : image_pointer_size_(image_pointer_size) {} |
| 1139 | |
| 1140 | void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1141 | if (kIsDebugBuild && !method->IsRuntimeMethod()) { |
| 1142 | CHECK(method->GetDeclaringClass() != nullptr); |
| 1143 | } |
| 1144 | if (!method->IsNative() && !method->IsRuntimeMethod() && !method->IsResolutionMethod()) { |
| 1145 | method->SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(), |
| 1146 | image_pointer_size_); |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | private: |
| 1151 | const size_t image_pointer_size_; |
| 1152 | |
| 1153 | DISALLOW_COPY_AND_ASSIGN(SetInterpreterEntrypointArtMethodVisitor); |
| 1154 | }; |
| 1155 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1156 | void ClassLinker::InitFromImage() { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1157 | VLOG(startup) << "ClassLinker::InitFromImage entering"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1158 | CHECK(!init_done_); |
| 1159 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 1160 | Runtime* const runtime = Runtime::Current(); |
| 1161 | Thread* const self = Thread::Current(); |
| 1162 | gc::Heap* const heap = runtime->GetHeap(); |
| 1163 | gc::space::ImageSpace* const space = heap->GetImageSpace(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1164 | CHECK(space != nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1165 | image_pointer_size_ = space->GetImageHeader().GetPointerSize(); |
| 1166 | dex_cache_image_class_lookup_required_ = true; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1167 | OatFile& oat_file = GetImageOatFile(space); |
| 1168 | CHECK_EQ(oat_file.GetOatHeader().GetImageFileLocationOatChecksum(), 0U); |
| 1169 | CHECK_EQ(oat_file.GetOatHeader().GetImageFileLocationOatDataBegin(), 0U); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1170 | const char* image_file_location = oat_file.GetOatHeader(). |
| 1171 | GetStoreValueByKey(OatHeader::kImageLocationKey); |
| 1172 | CHECK(image_file_location == nullptr || *image_file_location == 0); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1173 | quick_resolution_trampoline_ = oat_file.GetOatHeader().GetQuickResolutionTrampoline(); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1174 | quick_imt_conflict_trampoline_ = oat_file.GetOatHeader().GetQuickImtConflictTrampoline(); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1175 | quick_generic_jni_trampoline_ = oat_file.GetOatHeader().GetQuickGenericJniTrampoline(); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 1176 | quick_to_interpreter_bridge_trampoline_ = oat_file.GetOatHeader().GetQuickToInterpreterBridge(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1177 | StackHandleScope<2> hs(self); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1178 | mirror::Object* dex_caches_object = space->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1179 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches( |
| 1180 | hs.NewHandle(dex_caches_object->AsObjectArray<mirror::DexCache>())); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1181 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1182 | Handle<mirror::ObjectArray<mirror::Class>> class_roots(hs.NewHandle( |
| 1183 | space->GetImageHeader().GetImageRoot(ImageHeader::kClassRoots)-> |
| 1184 | AsObjectArray<mirror::Class>())); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1185 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>(class_roots.Get()); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1186 | |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1187 | // Special case of setting up the String class early so that we can test arbitrary objects |
| 1188 | // as being Strings or not |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 1189 | mirror::String::SetClass(GetClassRoot(kJavaLangString)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1190 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1191 | mirror::Class* java_lang_Object = GetClassRoot(kJavaLangObject); |
| 1192 | java_lang_Object->SetObjectSize(sizeof(mirror::Object)); |
| 1193 | Runtime::Current()->SetSentinel(Runtime::Current()->GetHeap()->AllocObject<true>(self, |
| 1194 | java_lang_Object, |
| 1195 | java_lang_Object->GetObjectSize(), |
| 1196 | VoidFunctor())); |
| 1197 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1198 | CHECK_EQ(oat_file.GetOatHeader().GetDexFileCount(), |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1199 | static_cast<uint32_t>(dex_caches->GetLength())); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1200 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1201 | StackHandleScope<1> hs2(self); |
| 1202 | Handle<mirror::DexCache> dex_cache(hs2.NewHandle(dex_caches->Get(i))); |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1203 | const std::string& dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1204 | const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(dex_file_location.c_str(), |
| 1205 | nullptr); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1206 | CHECK(oat_dex_file != nullptr) << oat_file.GetLocation() << " " << dex_file_location; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1207 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1208 | std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 1209 | if (dex_file.get() == nullptr) { |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1210 | LOG(FATAL) << "Failed to open dex file " << dex_file_location |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1211 | << " from within oat file " << oat_file.GetLocation() |
| 1212 | << " error '" << error_msg << "'"; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1213 | UNREACHABLE(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1214 | } |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1215 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1216 | if (kSanityCheckObjects) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1217 | SanityCheckArtMethodPointerArray(dex_cache->GetResolvedMethods(), |
| 1218 | dex_cache->NumResolvedMethods(), |
| 1219 | image_pointer_size_, |
| 1220 | space); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1221 | } |
| 1222 | |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1223 | CHECK_EQ(dex_file->GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum()); |
| 1224 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 1225 | AppendToBootClassPath(*dex_file.get(), dex_cache); |
| 1226 | opened_dex_files_.push_back(std::move(dex_file)); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1229 | CHECK(ValidPointerSize(image_pointer_size_)) << image_pointer_size_; |
| 1230 | |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 1231 | // Set classes on AbstractMethod early so that IsMethod tests can be performed during the live |
| 1232 | // bitmap walk. |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 1233 | if (!runtime->IsAotCompiler()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1234 | // Only the Aot compiler supports having an image with a different pointer size than the |
| 1235 | // runtime. This happens on the host for compile 32 bit tests since we use a 64 bit libart |
| 1236 | // compiler. We may also use 32 bit dex2oat on a system with 64 bit apps. |
| 1237 | CHECK_EQ(image_pointer_size_, sizeof(void*)); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 1238 | } |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 1239 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1240 | if (kSanityCheckObjects) { |
| 1241 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 1242 | auto* dex_cache = dex_caches->Get(i); |
| 1243 | for (size_t j = 0; j < dex_cache->NumResolvedFields(); ++j) { |
| 1244 | auto* field = dex_cache->GetResolvedField(j, image_pointer_size_); |
| 1245 | if (field != nullptr) { |
| 1246 | CHECK(field->GetDeclaringClass()->GetClass() != nullptr); |
| 1247 | } |
| 1248 | } |
| 1249 | } |
| 1250 | heap->VisitObjects(SanityCheckObjectsCallback, nullptr); |
| 1251 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1252 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1253 | // Set entry point to interpreter if in InterpretOnly mode. |
| 1254 | if (!runtime->IsAotCompiler() && runtime->GetInstrumentation()->InterpretOnly()) { |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 1255 | const ImageHeader& header = space->GetImageHeader(); |
| 1256 | const ImageSection& methods = header.GetMethodsSection(); |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 1257 | SetInterpreterEntrypointArtMethodVisitor visitor(image_pointer_size_); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1258 | methods.VisitPackedArtMethods(&visitor, space->Begin(), image_pointer_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1261 | // reinit class_roots_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1262 | mirror::Class::SetClassClass(class_roots->Get(kJavaLangClass)); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1263 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>(class_roots.Get()); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1264 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1265 | // reinit array_iftable_ from any array class instance, they should be == |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1266 | array_iftable_ = GcRoot<mirror::IfTable>(GetClassRoot(kObjectArrayClass)->GetIfTable()); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 1267 | DCHECK_EQ(array_iftable_.Read(), GetClassRoot(kBooleanArrayClass)->GetIfTable()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1268 | // String class root was set above |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 1269 | mirror::Field::SetClass(GetClassRoot(kJavaLangReflectField)); |
| 1270 | mirror::Field::SetArrayClass(GetClassRoot(kJavaLangReflectFieldArrayClass)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1271 | mirror::Constructor::SetClass(GetClassRoot(kJavaLangReflectConstructor)); |
| 1272 | mirror::Constructor::SetArrayClass(GetClassRoot(kJavaLangReflectConstructorArrayClass)); |
| 1273 | mirror::Method::SetClass(GetClassRoot(kJavaLangReflectMethod)); |
| 1274 | mirror::Method::SetArrayClass(GetClassRoot(kJavaLangReflectMethodArrayClass)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 1275 | mirror::Reference::SetClass(GetClassRoot(kJavaLangRefReference)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1276 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
| 1277 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
| 1278 | mirror::CharArray::SetArrayClass(GetClassRoot(kCharArrayClass)); |
| 1279 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
| 1280 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
| 1281 | mirror::IntArray::SetArrayClass(GetClassRoot(kIntArrayClass)); |
| 1282 | mirror::LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
| 1283 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
| 1284 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
| 1285 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1286 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1287 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1288 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1289 | VLOG(startup) << "ClassLinker::InitFromImage exiting"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1292 | bool ClassLinker::ClassInClassTable(mirror::Class* klass) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 1293 | ClassTable* const class_table = ClassTableForClassLoader(klass->GetClassLoader()); |
| 1294 | return class_table != nullptr && class_table->Contains(klass); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1297 | void ClassLinker::VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame^] | 1298 | Thread* const self = Thread::Current(); |
| 1299 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1300 | BufferedRootVisitor<kDefaultBufferedRootCount> buffered_visitor( |
| 1301 | visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1302 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1303 | // Argument for how root visiting deals with ArtField and ArtMethod roots. |
| 1304 | // There is 3 GC cases to handle: |
| 1305 | // Non moving concurrent: |
| 1306 | // 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] | 1307 | // live by the class and class roots. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1308 | // |
| 1309 | // Moving non-concurrent: |
| 1310 | // This case needs to call visit VisitNativeRoots in case the classes or dex cache arrays move. |
| 1311 | // To prevent missing roots, this case needs to ensure that there is no |
| 1312 | // suspend points between the point which we allocate ArtMethod arrays and place them in a |
| 1313 | // class which is in the class table. |
| 1314 | // |
| 1315 | // Moving concurrent: |
| 1316 | // Need to make sure to not copy ArtMethods without doing read barriers since the roots are |
| 1317 | // marked concurrently and we don't hold the classlinker_classes_lock_ when we do the copy. |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 1318 | boot_class_table_.VisitRoots(buffered_visitor); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame^] | 1319 | // TODO: Avoid marking these to enable class unloading. |
| 1320 | JavaVMExt* const vm = Runtime::Current()->GetJavaVM(); |
| 1321 | for (jweak weak_root : class_loaders_) { |
| 1322 | mirror::Object* class_loader = |
| 1323 | down_cast<mirror::ClassLoader*>(vm->DecodeWeakGlobal(self, weak_root)); |
| 1324 | // Don't need to update anything since the class loaders will be updated by SweepSystemWeaks. |
| 1325 | visitor->VisitRootIfNonNull(&class_loader, RootInfo(kRootVMInternal)); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1326 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1327 | } else if ((flags & kVisitRootFlagNewRoots) != 0) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1328 | for (auto& root : new_class_roots_) { |
| 1329 | mirror::Class* old_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1330 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1331 | mirror::Class* new_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 1332 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 1333 | CHECK_EQ(new_ref, old_ref); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1334 | } |
| 1335 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1336 | buffered_visitor.Flush(); // Flush before clearing new_class_roots_. |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1337 | if ((flags & kVisitRootFlagClearRootLog) != 0) { |
| 1338 | new_class_roots_.clear(); |
| 1339 | } |
| 1340 | if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
| 1341 | log_new_class_table_roots_ = true; |
| 1342 | } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
| 1343 | log_new_class_table_roots_ = false; |
| 1344 | } |
| 1345 | // We deliberately ignore the class roots in the image since we |
| 1346 | // handle image roots by using the MS/CMS rescanning of dirty cards. |
| 1347 | } |
| 1348 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1349 | // Keep in sync with InitCallback. Anything we visit, we need to |
| 1350 | // reinit references to when reinitializing a ClassLinker from a |
| 1351 | // mapped image. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1352 | void ClassLinker::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 1353 | class_roots_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1354 | VisitClassRoots(visitor, flags); |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 1355 | array_iftable_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); |
| 1356 | for (GcRoot<mirror::Class>& root : find_array_class_cache_) { |
| 1357 | root.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1358 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1359 | } |
| 1360 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame^] | 1361 | class VisitClassLoaderClassesVisitor : public ClassLoaderVisitor { |
| 1362 | public: |
| 1363 | explicit VisitClassLoaderClassesVisitor(ClassVisitor* visitor) |
| 1364 | : visitor_(visitor), |
| 1365 | done_(false) {} |
| 1366 | |
| 1367 | void Visit(mirror::ClassLoader* class_loader) |
| 1368 | SHARED_REQUIRES(Locks::classlinker_classes_lock_, Locks::mutator_lock_) OVERRIDE { |
| 1369 | ClassTable* const class_table = class_loader->GetClassTable(); |
| 1370 | if (!done_ && class_table != nullptr && !class_table->Visit(visitor_)) { |
| 1371 | // If the visitor ClassTable returns false it means that we don't need to continue. |
| 1372 | done_ = true; |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | private: |
| 1377 | ClassVisitor* const visitor_; |
| 1378 | // If done is true then we don't need to do any more visiting. |
| 1379 | bool done_; |
| 1380 | }; |
| 1381 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1382 | void ClassLinker::VisitClassesInternal(ClassVisitor* visitor) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 1383 | if (boot_class_table_.Visit(visitor)) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame^] | 1384 | VisitClassLoaderClassesVisitor loader_visitor(visitor); |
| 1385 | VisitClassLoaders(&loader_visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 1386 | } |
| 1387 | } |
| 1388 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1389 | void ClassLinker::VisitClasses(ClassVisitor* visitor) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1390 | if (dex_cache_image_class_lookup_required_) { |
| 1391 | MoveImageClassesToClassTable(); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1392 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 1393 | Thread* const self = Thread::Current(); |
| 1394 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 1395 | // Not safe to have thread suspension when we are holding a lock. |
| 1396 | if (self != nullptr) { |
| 1397 | ScopedAssertNoThreadSuspension nts(self, __FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1398 | VisitClassesInternal(visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 1399 | } else { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1400 | VisitClassesInternal(visitor); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1401 | } |
| 1402 | } |
| 1403 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1404 | class GetClassesInToVector : public ClassVisitor { |
| 1405 | public: |
| 1406 | bool Visit(mirror::Class* klass) OVERRIDE { |
| 1407 | classes_.push_back(klass); |
| 1408 | return true; |
| 1409 | } |
| 1410 | std::vector<mirror::Class*> classes_; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1411 | }; |
| 1412 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1413 | class GetClassInToObjectArray : public ClassVisitor { |
| 1414 | public: |
| 1415 | explicit GetClassInToObjectArray(mirror::ObjectArray<mirror::Class>* arr) |
| 1416 | : arr_(arr), index_(0) {} |
| 1417 | |
| 1418 | bool Visit(mirror::Class* klass) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1419 | ++index_; |
| 1420 | if (index_ <= arr_->GetLength()) { |
| 1421 | arr_->Set(index_ - 1, klass); |
| 1422 | return true; |
| 1423 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1424 | return false; |
| 1425 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1426 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1427 | bool Succeeded() const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1428 | return index_ <= arr_->GetLength(); |
| 1429 | } |
| 1430 | |
| 1431 | private: |
| 1432 | mirror::ObjectArray<mirror::Class>* const arr_; |
| 1433 | int32_t index_; |
| 1434 | }; |
| 1435 | |
| 1436 | void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1437 | // TODO: it may be possible to avoid secondary storage if we iterate over dex caches. The problem |
| 1438 | // is avoiding duplicates. |
| 1439 | if (!kMovingClasses) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1440 | GetClassesInToVector accumulator; |
| 1441 | VisitClasses(&accumulator); |
| 1442 | for (mirror::Class* klass : accumulator.classes_) { |
| 1443 | if (!visitor->Visit(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1444 | return; |
| 1445 | } |
| 1446 | } |
| 1447 | } else { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1448 | Thread* const self = Thread::Current(); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1449 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1450 | auto classes = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1451 | // We size the array assuming classes won't be added to the class table during the visit. |
| 1452 | // If this assumption fails we iterate again. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1453 | while (true) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1454 | size_t class_table_size; |
| 1455 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1456 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1457 | // Add 100 in case new classes get loaded when we are filling in the object array. |
| 1458 | class_table_size = NumZygoteClasses() + NumNonZygoteClasses() + 100; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1459 | } |
| 1460 | mirror::Class* class_type = mirror::Class::GetJavaLangClass(); |
| 1461 | mirror::Class* array_of_class = FindArrayClass(self, &class_type); |
| 1462 | classes.Assign( |
| 1463 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, class_table_size)); |
| 1464 | CHECK(classes.Get() != nullptr); // OOME. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1465 | GetClassInToObjectArray accumulator(classes.Get()); |
| 1466 | VisitClasses(&accumulator); |
| 1467 | if (accumulator.Succeeded()) { |
| 1468 | break; |
| 1469 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1470 | } |
| 1471 | for (int32_t i = 0; i < classes->GetLength(); ++i) { |
| 1472 | // If the class table shrank during creation of the clases array we expect null elements. If |
| 1473 | // the class table grew then the loop repeats. If classes are created after the loop has |
| 1474 | // finished then we don't visit. |
| 1475 | mirror::Class* klass = classes->Get(i); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1476 | if (klass != nullptr && !visitor->Visit(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1477 | return; |
| 1478 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1479 | } |
| 1480 | } |
| 1481 | } |
| 1482 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1483 | ClassLinker::~ClassLinker() { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1484 | mirror::Class::ResetClass(); |
| 1485 | mirror::Constructor::ResetClass(); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 1486 | mirror::Field::ResetClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1487 | mirror::Method::ResetClass(); |
| 1488 | mirror::Reference::ResetClass(); |
| 1489 | mirror::StackTraceElement::ResetClass(); |
| 1490 | mirror::String::ResetClass(); |
| 1491 | mirror::Throwable::ResetClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1492 | mirror::BooleanArray::ResetArrayClass(); |
| 1493 | mirror::ByteArray::ResetArrayClass(); |
| 1494 | mirror::CharArray::ResetArrayClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1495 | mirror::Constructor::ResetArrayClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1496 | mirror::DoubleArray::ResetArrayClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1497 | mirror::Field::ResetArrayClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1498 | mirror::FloatArray::ResetArrayClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1499 | mirror::Method::ResetArrayClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1500 | mirror::IntArray::ResetArrayClass(); |
| 1501 | mirror::LongArray::ResetArrayClass(); |
| 1502 | mirror::ShortArray::ResetArrayClass(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1503 | STLDeleteElements(&oat_files_); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame^] | 1504 | Thread* const self = Thread::Current(); |
| 1505 | JavaVMExt* const vm = Runtime::Current()->GetJavaVM(); |
| 1506 | for (jweak weak_root : class_loaders_) { |
| 1507 | auto* const class_loader = down_cast<mirror::ClassLoader*>( |
| 1508 | vm->DecodeWeakGlobal(self, weak_root)); |
| 1509 | if (class_loader != nullptr) { |
| 1510 | delete class_loader->GetClassTable(); |
| 1511 | } |
| 1512 | vm->DeleteWeakGlobalRef(self, weak_root); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 1513 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame^] | 1514 | class_loaders_.clear(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1515 | } |
| 1516 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1517 | mirror::PointerArray* ClassLinker::AllocPointerArray(Thread* self, size_t length) { |
| 1518 | return down_cast<mirror::PointerArray*>(image_pointer_size_ == 8u ? |
| 1519 | static_cast<mirror::Array*>(mirror::LongArray::Alloc(self, length)) : |
| 1520 | static_cast<mirror::Array*>(mirror::IntArray::Alloc(self, length))); |
| 1521 | } |
| 1522 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1523 | mirror::DexCache* ClassLinker::AllocDexCache(Thread* self, const DexFile& dex_file) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1524 | StackHandleScope<6> hs(self); |
| 1525 | auto dex_cache(hs.NewHandle(down_cast<mirror::DexCache*>( |
| 1526 | GetClassRoot(kJavaLangDexCache)->AllocObject(self)))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1527 | if (dex_cache.Get() == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1528 | self->AssertPendingOOMException(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1529 | return nullptr; |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1530 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1531 | auto location(hs.NewHandle(intern_table_->InternStrong(dex_file.GetLocation().c_str()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1532 | if (location.Get() == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1533 | self->AssertPendingOOMException(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1534 | return nullptr; |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1535 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1536 | DexCacheArraysLayout layout(image_pointer_size_, &dex_file); |
| 1537 | uint8_t* raw_arrays = nullptr; |
| 1538 | if (dex_file.NumStringIds() != 0u || dex_file.NumTypeIds() != 0u || |
| 1539 | dex_file.NumMethodIds() != 0u || dex_file.NumFieldIds() != 0u) { |
| 1540 | // NOTE: We "leak" the raw_arrays because we never destroy the dex cache. |
| 1541 | DCHECK(image_pointer_size_ == 4u || image_pointer_size_ == 8u); |
| 1542 | if (sizeof(void*) == 8u && image_pointer_size_ == 4u) { |
| 1543 | // When cross-compiling for a 32-bit target on a 64-bit host, we need these arrays |
| 1544 | // in the low 4GiB address space so that we can store pointers in 32-bit fields. |
| 1545 | // This is conveniently provided by the linear allocator. |
| 1546 | raw_arrays = reinterpret_cast<uint8_t*>( |
| 1547 | Runtime::Current()->GetLinearAlloc()->Alloc(self, layout.Size())); // Zero-initialized. |
| 1548 | } else { |
| 1549 | raw_arrays = reinterpret_cast<uint8_t*>(calloc(layout.Size(), 1u)); // Zero-initialized. |
| 1550 | if (raw_arrays == nullptr) { |
| 1551 | return nullptr; |
| 1552 | } |
| 1553 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1554 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1555 | GcRoot<mirror::String>* strings = (dex_file.NumStringIds() == 0u) ? nullptr : |
| 1556 | reinterpret_cast<GcRoot<mirror::String>*>(raw_arrays + layout.StringsOffset()); |
| 1557 | GcRoot<mirror::Class>* types = (dex_file.NumTypeIds() == 0u) ? nullptr : |
| 1558 | reinterpret_cast<GcRoot<mirror::Class>*>(raw_arrays + layout.TypesOffset()); |
| 1559 | ArtMethod** methods = (dex_file.NumMethodIds() == 0u) ? nullptr : |
| 1560 | reinterpret_cast<ArtMethod**>(raw_arrays + layout.MethodsOffset()); |
| 1561 | ArtField** fields = (dex_file.NumFieldIds() == 0u) ? nullptr : |
| 1562 | reinterpret_cast<ArtField**>(raw_arrays + layout.FieldsOffset()); |
| 1563 | dex_cache->Init(&dex_file, |
| 1564 | location.Get(), |
| 1565 | strings, |
| 1566 | dex_file.NumStringIds(), |
| 1567 | types, |
| 1568 | dex_file.NumTypeIds(), |
| 1569 | methods, |
| 1570 | dex_file.NumMethodIds(), |
| 1571 | fields, |
| 1572 | dex_file.NumFieldIds(), |
| 1573 | image_pointer_size_); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1574 | return dex_cache.Get(); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1575 | } |
| 1576 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1577 | mirror::Class* ClassLinker::AllocClass(Thread* self, mirror::Class* java_lang_Class, |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1578 | uint32_t class_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1579 | DCHECK_GE(class_size, sizeof(mirror::Class)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1580 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1581 | mirror::Class::InitializeClassVisitor visitor(class_size); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1582 | mirror::Object* k = kMovingClasses ? |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1583 | heap->AllocObject<true>(self, java_lang_Class, class_size, visitor) : |
| 1584 | heap->AllocNonMovableObject<true>(self, java_lang_Class, class_size, visitor); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1585 | if (UNLIKELY(k == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1586 | self->AssertPendingOOMException(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1587 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1588 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1589 | return k->AsClass(); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1590 | } |
| 1591 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1592 | mirror::Class* ClassLinker::AllocClass(Thread* self, uint32_t class_size) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1593 | return AllocClass(self, GetClassRoot(kJavaLangClass), class_size); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1594 | } |
| 1595 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1596 | mirror::ObjectArray<mirror::StackTraceElement>* ClassLinker::AllocStackTraceElementArray( |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 1597 | Thread* self, |
| 1598 | size_t length) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1599 | return mirror::ObjectArray<mirror::StackTraceElement>::Alloc( |
| 1600 | self, GetClassRoot(kJavaLangStackTraceElementArrayClass), length); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1601 | } |
| 1602 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 1603 | mirror::Class* ClassLinker::EnsureResolved(Thread* self, |
| 1604 | const char* descriptor, |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1605 | mirror::Class* klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1606 | DCHECK(klass != nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1607 | |
| 1608 | // For temporary classes we must wait for them to be retired. |
| 1609 | if (init_done_ && klass->IsTemp()) { |
| 1610 | CHECK(!klass->IsResolved()); |
| 1611 | if (klass->IsErroneous()) { |
| 1612 | ThrowEarlierClassFailure(klass); |
| 1613 | return nullptr; |
| 1614 | } |
| 1615 | StackHandleScope<1> hs(self); |
| 1616 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 1617 | ObjectLock<mirror::Class> lock(self, h_class); |
| 1618 | // Loop and wait for the resolving thread to retire this class. |
| 1619 | while (!h_class->IsRetired() && !h_class->IsErroneous()) { |
| 1620 | lock.WaitIgnoringInterrupts(); |
| 1621 | } |
| 1622 | if (h_class->IsErroneous()) { |
| 1623 | ThrowEarlierClassFailure(h_class.Get()); |
| 1624 | return nullptr; |
| 1625 | } |
| 1626 | CHECK(h_class->IsRetired()); |
| 1627 | // Get the updated class from class table. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1628 | klass = LookupClass(self, descriptor, ComputeModifiedUtf8Hash(descriptor), |
| 1629 | h_class.Get()->GetClassLoader()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1630 | } |
| 1631 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1632 | // Wait for the class if it has not already been linked. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 1633 | if (!klass->IsResolved() && !klass->IsErroneous()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1634 | StackHandleScope<1> hs(self); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 1635 | HandleWrapper<mirror::Class> h_class(hs.NewHandleWrapper(&klass)); |
| 1636 | ObjectLock<mirror::Class> lock(self, h_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1637 | // Check for circular dependencies between classes. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1638 | if (!h_class->IsResolved() && h_class->GetClinitThreadId() == self->GetTid()) { |
| 1639 | ThrowClassCircularityError(h_class.Get()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 1640 | mirror::Class::SetStatus(h_class, mirror::Class::kStatusError, self); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 1641 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1642 | } |
| 1643 | // Wait for the pending initialization to complete. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1644 | while (!h_class->IsResolved() && !h_class->IsErroneous()) { |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 1645 | lock.WaitIgnoringInterrupts(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1646 | } |
| 1647 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1648 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1649 | if (klass->IsErroneous()) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 1650 | ThrowEarlierClassFailure(klass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 1651 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1652 | } |
| 1653 | // Return the loaded class. No exceptions should be pending. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1654 | CHECK(klass->IsResolved()) << PrettyClass(klass); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1655 | self->AssertNoPendingException(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1656 | return klass; |
| 1657 | } |
| 1658 | |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1659 | typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry; |
| 1660 | |
| 1661 | // Search a collection of DexFiles for a descriptor |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1662 | ClassPathEntry FindInClassPath(const char* descriptor, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1663 | size_t hash, const std::vector<const DexFile*>& class_path) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1664 | for (const DexFile* dex_file : class_path) { |
| 1665 | const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1666 | if (dex_class_def != nullptr) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1667 | return ClassPathEntry(dex_file, dex_class_def); |
| 1668 | } |
| 1669 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1670 | return ClassPathEntry(nullptr, nullptr); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1671 | } |
| 1672 | |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 1673 | static bool IsBootClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
| 1674 | mirror::ClassLoader* class_loader) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1675 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 1676 | return class_loader == nullptr || |
| 1677 | class_loader->GetClass() == |
| 1678 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_BootClassLoader); |
| 1679 | } |
| 1680 | |
| 1681 | bool ClassLinker::FindClassInPathClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 1682 | Thread* self, |
| 1683 | const char* descriptor, |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 1684 | size_t hash, |
| 1685 | Handle<mirror::ClassLoader> class_loader, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1686 | mirror::Class** result) { |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 1687 | // Termination case: boot class-loader. |
| 1688 | if (IsBootClassLoader(soa, class_loader.Get())) { |
| 1689 | // The boot class loader, search the boot class path. |
| 1690 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
| 1691 | if (pair.second != nullptr) { |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1692 | mirror::Class* klass = LookupClass(self, descriptor, hash, nullptr); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 1693 | if (klass != nullptr) { |
| 1694 | *result = EnsureResolved(self, descriptor, klass); |
| 1695 | } else { |
| 1696 | *result = DefineClass(self, descriptor, hash, NullHandle<mirror::ClassLoader>(), |
| 1697 | *pair.first, *pair.second); |
| 1698 | } |
| 1699 | if (*result == nullptr) { |
| 1700 | CHECK(self->IsExceptionPending()) << descriptor; |
| 1701 | self->ClearException(); |
| 1702 | } |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 1703 | } else { |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 1704 | *result = nullptr; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 1705 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 1706 | return true; |
| 1707 | } |
| 1708 | |
| 1709 | // Unsupported class-loader? |
| 1710 | if (class_loader->GetClass() != |
| 1711 | soa.Decode<mirror::Class*>(WellKnownClasses::dalvik_system_PathClassLoader)) { |
| 1712 | *result = nullptr; |
| 1713 | return false; |
| 1714 | } |
| 1715 | |
| 1716 | // Handles as RegisterDexFile may allocate dex caches (and cause thread suspension). |
| 1717 | StackHandleScope<4> hs(self); |
| 1718 | Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent())); |
| 1719 | bool recursive_result = FindClassInPathClassLoader(soa, self, descriptor, hash, h_parent, result); |
| 1720 | |
| 1721 | if (!recursive_result) { |
| 1722 | // Something wrong up the chain. |
| 1723 | return false; |
| 1724 | } |
| 1725 | |
| 1726 | if (*result != nullptr) { |
| 1727 | // Found the class up the chain. |
| 1728 | return true; |
| 1729 | } |
| 1730 | |
| 1731 | // Handle this step. |
| 1732 | // Handle as if this is the child PathClassLoader. |
| 1733 | // The class loader is a PathClassLoader which inherits from BaseDexClassLoader. |
| 1734 | // We need to get the DexPathList and loop through it. |
| 1735 | ArtField* const cookie_field = soa.DecodeField(WellKnownClasses::dalvik_system_DexFile_cookie); |
| 1736 | ArtField* const dex_file_field = |
| 1737 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
| 1738 | mirror::Object* dex_path_list = |
| 1739 | soa.DecodeField(WellKnownClasses::dalvik_system_PathClassLoader_pathList)-> |
| 1740 | GetObject(class_loader.Get()); |
| 1741 | if (dex_path_list != nullptr && dex_file_field != nullptr && cookie_field != nullptr) { |
| 1742 | // DexPathList has an array dexElements of Elements[] which each contain a dex file. |
| 1743 | mirror::Object* dex_elements_obj = |
| 1744 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList_dexElements)-> |
| 1745 | GetObject(dex_path_list); |
| 1746 | // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look |
| 1747 | // at the mCookie which is a DexFile vector. |
| 1748 | if (dex_elements_obj != nullptr) { |
| 1749 | Handle<mirror::ObjectArray<mirror::Object>> dex_elements = |
| 1750 | hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>()); |
| 1751 | for (int32_t i = 0; i < dex_elements->GetLength(); ++i) { |
| 1752 | mirror::Object* element = dex_elements->GetWithoutChecks(i); |
| 1753 | if (element == nullptr) { |
| 1754 | // Should never happen, fall back to java code to throw a NPE. |
| 1755 | break; |
| 1756 | } |
| 1757 | mirror::Object* dex_file = dex_file_field->GetObject(element); |
| 1758 | if (dex_file != nullptr) { |
| 1759 | mirror::LongArray* long_array = cookie_field->GetObject(dex_file)->AsLongArray(); |
| 1760 | if (long_array == nullptr) { |
| 1761 | // This should never happen so log a warning. |
| 1762 | LOG(WARNING) << "Null DexFile::mCookie for " << descriptor; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 1763 | break; |
| 1764 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 1765 | int32_t long_array_size = long_array->GetLength(); |
| 1766 | for (int32_t j = 0; j < long_array_size; ++j) { |
| 1767 | const DexFile* cp_dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>( |
| 1768 | long_array->GetWithoutChecks(j))); |
| 1769 | const DexFile::ClassDef* dex_class_def = cp_dex_file->FindClassDef(descriptor, hash); |
| 1770 | if (dex_class_def != nullptr) { |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 1771 | mirror::Class* klass = DefineClass(self, descriptor, hash, class_loader, |
| 1772 | *cp_dex_file, *dex_class_def); |
| 1773 | if (klass == nullptr) { |
| 1774 | CHECK(self->IsExceptionPending()) << descriptor; |
| 1775 | self->ClearException(); |
| 1776 | // TODO: Is it really right to break here, and not check the other dex files? |
| 1777 | return true; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 1778 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 1779 | *result = klass; |
| 1780 | return true; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 1781 | } |
| 1782 | } |
| 1783 | } |
| 1784 | } |
| 1785 | } |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 1786 | self->AssertNoPendingException(); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 1787 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 1788 | |
| 1789 | // Result is still null from the parent call, no need to set it again... |
| 1790 | return true; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 1791 | } |
| 1792 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 1793 | mirror::Class* ClassLinker::FindClass(Thread* self, |
| 1794 | const char* descriptor, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 1795 | Handle<mirror::ClassLoader> class_loader) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 1796 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1797 | DCHECK(self != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1798 | self->AssertNoPendingException(); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 1799 | if (descriptor[1] == '\0') { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1800 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 1801 | // for primitive classes that aren't backed by dex files. |
| 1802 | return FindPrimitiveClass(descriptor[0]); |
| 1803 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1804 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1805 | // Find the class in the loaded classes table. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1806 | mirror::Class* klass = LookupClass(self, descriptor, hash, class_loader.Get()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1807 | if (klass != nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1808 | return EnsureResolved(self, descriptor, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1809 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1810 | // Class is not yet loaded. |
| 1811 | if (descriptor[0] == '[') { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1812 | return CreateArrayClass(self, descriptor, hash, class_loader); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1813 | } else if (class_loader.Get() == nullptr) { |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 1814 | // The boot class loader, search the boot class path. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1815 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1816 | if (pair.second != nullptr) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1817 | return DefineClass(self, descriptor, hash, NullHandle<mirror::ClassLoader>(), *pair.first, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1818 | *pair.second); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 1819 | } else { |
| 1820 | // The boot class loader is searched ahead of the application class loader, failures are |
| 1821 | // expected and will be wrapped in a ClassNotFoundException. Use the pre-allocated error to |
| 1822 | // trigger the chaining with a proper stack trace. |
| 1823 | mirror::Throwable* pre_allocated = Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 1824 | self->SetException(pre_allocated); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 1825 | return nullptr; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1826 | } |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1827 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1828 | ScopedObjectAccessUnchecked soa(self); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 1829 | mirror::Class* cp_klass; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1830 | if (FindClassInPathClassLoader(soa, self, descriptor, hash, class_loader, &cp_klass)) { |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 1831 | // The chain was understood. So the value in cp_klass is either the class we were looking |
| 1832 | // for, or not found. |
| 1833 | if (cp_klass != nullptr) { |
| 1834 | return cp_klass; |
| 1835 | } |
| 1836 | // TODO: We handle the boot classpath loader in FindClassInPathClassLoader. Try to unify this |
| 1837 | // and the branch above. TODO: throw the right exception here. |
| 1838 | |
| 1839 | // We'll let the Java-side rediscover all this and throw the exception with the right stack |
| 1840 | // trace. |
Mathieu Chartier | 6bcae8f | 2014-09-04 18:33:17 -0700 | [diff] [blame] | 1841 | } |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 1842 | |
| 1843 | if (Runtime::Current()->IsAotCompiler()) { |
| 1844 | // Oops, compile-time, can't run actual class-loader code. |
| 1845 | mirror::Throwable* pre_allocated = Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 1846 | self->SetException(pre_allocated); |
| 1847 | return nullptr; |
| 1848 | } |
| 1849 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1850 | ScopedLocalRef<jobject> class_loader_object(soa.Env(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1851 | soa.AddLocalReference<jobject>(class_loader.Get())); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 1852 | std::string class_name_string(DescriptorToDot(descriptor)); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1853 | ScopedLocalRef<jobject> result(soa.Env(), nullptr); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 1854 | { |
| 1855 | ScopedThreadStateChange tsc(self, kNative); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1856 | ScopedLocalRef<jobject> class_name_object(soa.Env(), |
| 1857 | soa.Env()->NewStringUTF(class_name_string.c_str())); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1858 | if (class_name_object.get() == nullptr) { |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 1859 | DCHECK(self->IsExceptionPending()); // OOME. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1860 | return nullptr; |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 1861 | } |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1862 | CHECK(class_loader_object.get() != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1863 | result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(), |
| 1864 | WellKnownClasses::java_lang_ClassLoader_loadClass, |
| 1865 | class_name_object.get())); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1866 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1867 | if (self->IsExceptionPending()) { |
Elliott Hughes | 748382f | 2012-01-26 18:07:38 -0800 | [diff] [blame] | 1868 | // If the ClassLoader threw, pass that exception up. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1869 | return nullptr; |
| 1870 | } else if (result.get() == nullptr) { |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 1871 | // broken loader - throw NPE to be compatible with Dalvik |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 1872 | ThrowNullPointerException(StringPrintf("ClassLoader.loadClass returned null for %s", |
| 1873 | class_name_string.c_str()).c_str()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1874 | return nullptr; |
Ian Rogers | 761bfa8 | 2012-01-11 10:14:05 -0800 | [diff] [blame] | 1875 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1876 | // success, return mirror::Class* |
| 1877 | return soa.Decode<mirror::Class*>(result.get()); |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 1878 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1879 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 1880 | UNREACHABLE(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1881 | } |
| 1882 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 1883 | mirror::Class* ClassLinker::DefineClass(Thread* self, |
| 1884 | const char* descriptor, |
| 1885 | size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 1886 | Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1887 | const DexFile& dex_file, |
| 1888 | const DexFile::ClassDef& dex_class_def) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1889 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1890 | auto klass = hs.NewHandle<mirror::Class>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1891 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1892 | // Load the class from the dex file. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1893 | if (UNLIKELY(!init_done_)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1894 | // finish up init of hand crafted class_roots_ |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1895 | if (strcmp(descriptor, "Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1896 | klass.Assign(GetClassRoot(kJavaLangObject)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1897 | } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1898 | klass.Assign(GetClassRoot(kJavaLangClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1899 | } else if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1900 | klass.Assign(GetClassRoot(kJavaLangString)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 1901 | } else if (strcmp(descriptor, "Ljava/lang/ref/Reference;") == 0) { |
| 1902 | klass.Assign(GetClassRoot(kJavaLangRefReference)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1903 | } else if (strcmp(descriptor, "Ljava/lang/DexCache;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1904 | klass.Assign(GetClassRoot(kJavaLangDexCache)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1905 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1906 | } |
| 1907 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1908 | if (klass.Get() == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1909 | // Allocate a class with the status of not ready. |
| 1910 | // Interface object should get the right size here. Regular class will |
| 1911 | // figure out the right size later and be replaced with one of the right |
| 1912 | // size when the class becomes resolved. |
| 1913 | klass.Assign(AllocClass(self, SizeOfClassWithoutEmbeddedTables(dex_file, dex_class_def))); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1914 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 1915 | if (UNLIKELY(klass.Get() == nullptr)) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1916 | self->AssertPendingOOMException(); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 1917 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1918 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1919 | mirror::DexCache* dex_cache = RegisterDexFile(dex_file); |
| 1920 | if (dex_cache == nullptr) { |
| 1921 | self->AssertPendingOOMException(); |
| 1922 | return nullptr; |
| 1923 | } |
| 1924 | klass->SetDexCache(dex_cache); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1925 | SetupClass(dex_file, dex_class_def, klass, class_loader.Get()); |
| 1926 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1927 | // Mark the string class by setting its access flag. |
| 1928 | if (UNLIKELY(!init_done_)) { |
| 1929 | if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
| 1930 | klass->SetStringClass(); |
| 1931 | } |
| 1932 | } |
| 1933 | |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 1934 | ObjectLock<mirror::Class> lock(self, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1935 | klass->SetClinitThreadId(self->GetTid()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1936 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1937 | // Add the newly loaded class to the loaded classes table. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1938 | mirror::Class* existing = InsertClass(descriptor, klass.Get(), hash); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 1939 | if (existing != nullptr) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1940 | // We failed to insert because we raced with another thread. Calling EnsureResolved may cause |
| 1941 | // this thread to block. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1942 | return EnsureResolved(self, descriptor, existing); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1943 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1944 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1945 | // Load the fields and other things after we are inserted in the table. This is so that we don't |
| 1946 | // end up allocating unfree-able linear alloc resources and then lose the race condition. The |
| 1947 | // other reason is that the field roots are only visited from the class table. So we need to be |
| 1948 | // inserted before we allocate / fill in these fields. |
| 1949 | LoadClass(self, dex_file, dex_class_def, klass); |
| 1950 | if (self->IsExceptionPending()) { |
| 1951 | // An exception occured during load, set status to erroneous while holding klass' lock in case |
| 1952 | // notification is necessary. |
| 1953 | if (!klass->IsErroneous()) { |
| 1954 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
| 1955 | } |
| 1956 | return nullptr; |
| 1957 | } |
| 1958 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1959 | // Finish loading (if necessary) by finding parents |
| 1960 | CHECK(!klass->IsLoaded()); |
| 1961 | if (!LoadSuperAndInterfaces(klass, dex_file)) { |
| 1962 | // Loading failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 1963 | if (!klass->IsErroneous()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 1964 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 1965 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 1966 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1967 | } |
| 1968 | CHECK(klass->IsLoaded()); |
| 1969 | // Link the class (if necessary) |
| 1970 | CHECK(!klass->IsResolved()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1971 | // TODO: Use fast jobjects? |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1972 | auto interfaces = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1973 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 1974 | MutableHandle<mirror::Class> h_new_class = hs.NewHandle<mirror::Class>(nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1975 | if (!LinkClass(self, descriptor, klass, interfaces, &h_new_class)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1976 | // Linking failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 1977 | if (!klass->IsErroneous()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 1978 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 1979 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 1980 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1981 | } |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 1982 | self->AssertNoPendingException(); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 1983 | CHECK(h_new_class.Get() != nullptr) << descriptor; |
| 1984 | CHECK(h_new_class->IsResolved()) << descriptor; |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 1985 | |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 1986 | // Instrumentation may have updated entrypoints for all methods of all |
| 1987 | // classes. However it could not update methods of this class while we |
| 1988 | // were loading it. Now the class is resolved, we can update entrypoints |
| 1989 | // as required by instrumentation. |
| 1990 | if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) { |
| 1991 | // We must be in the kRunnable state to prevent instrumentation from |
| 1992 | // suspending all threads to update entrypoints while we are doing it |
| 1993 | // for this class. |
| 1994 | DCHECK_EQ(self->GetState(), kRunnable); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 1995 | Runtime::Current()->GetInstrumentation()->InstallStubsForClass(h_new_class.Get()); |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 1996 | } |
| 1997 | |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 1998 | /* |
| 1999 | * We send CLASS_PREPARE events to the debugger from here. The |
| 2000 | * definition of "preparation" is creating the static fields for a |
| 2001 | * class and initializing them to the standard default values, but not |
| 2002 | * executing any code (that comes later, during "initialization"). |
| 2003 | * |
| 2004 | * We did the static preparation in LinkClass. |
| 2005 | * |
| 2006 | * The class has been prepared and resolved but possibly not yet verified |
| 2007 | * at this point. |
| 2008 | */ |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 2009 | Dbg::PostClassPrepare(h_new_class.Get()); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2010 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 2011 | return h_new_class.Get(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2012 | } |
| 2013 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2014 | uint32_t ClassLinker::SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, |
| 2015 | const DexFile::ClassDef& dex_class_def) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2016 | const uint8_t* class_data = dex_file.GetClassData(dex_class_def); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2017 | size_t num_ref = 0; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2018 | size_t num_8 = 0; |
| 2019 | size_t num_16 = 0; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2020 | size_t num_32 = 0; |
| 2021 | size_t num_64 = 0; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2022 | if (class_data != nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2023 | for (ClassDataItemIterator it(dex_file, class_data); it.HasNextStaticField(); it.Next()) { |
| 2024 | const DexFile::FieldId& field_id = dex_file.GetFieldId(it.GetMemberIndex()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2025 | const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2026 | char c = descriptor[0]; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2027 | switch (c) { |
| 2028 | case 'L': |
| 2029 | case '[': |
| 2030 | num_ref++; |
| 2031 | break; |
| 2032 | case 'J': |
| 2033 | case 'D': |
| 2034 | num_64++; |
| 2035 | break; |
| 2036 | case 'I': |
| 2037 | case 'F': |
| 2038 | num_32++; |
| 2039 | break; |
| 2040 | case 'S': |
| 2041 | case 'C': |
| 2042 | num_16++; |
| 2043 | break; |
| 2044 | case 'B': |
| 2045 | case 'Z': |
| 2046 | num_8++; |
| 2047 | break; |
| 2048 | default: |
| 2049 | LOG(FATAL) << "Unknown descriptor: " << c; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 2050 | UNREACHABLE(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2051 | } |
| 2052 | } |
| 2053 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2054 | return mirror::Class::ComputeClassSize(false, |
| 2055 | 0, |
| 2056 | num_8, |
| 2057 | num_16, |
| 2058 | num_32, |
| 2059 | num_64, |
| 2060 | num_ref, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2061 | image_pointer_size_); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2062 | } |
| 2063 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2064 | OatFile::OatClass ClassLinker::FindOatClass(const DexFile& dex_file, |
| 2065 | uint16_t class_def_idx, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2066 | bool* found) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2067 | DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16); |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 2068 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 2069 | if (oat_dex_file == nullptr) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2070 | *found = false; |
| 2071 | return OatFile::OatClass::Invalid(); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2072 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2073 | *found = true; |
| 2074 | return oat_dex_file->GetOatClass(class_def_idx); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2075 | } |
| 2076 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2077 | static uint32_t GetOatMethodIndexFromMethodIndex(const DexFile& dex_file, |
| 2078 | uint16_t class_def_idx, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2079 | uint32_t method_idx) { |
| 2080 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_idx); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2081 | const uint8_t* class_data = dex_file.GetClassData(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2082 | CHECK(class_data != nullptr); |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2083 | ClassDataItemIterator it(dex_file, class_data); |
| 2084 | // Skip fields |
| 2085 | while (it.HasNextStaticField()) { |
| 2086 | it.Next(); |
| 2087 | } |
| 2088 | while (it.HasNextInstanceField()) { |
| 2089 | it.Next(); |
| 2090 | } |
| 2091 | // Process methods |
| 2092 | size_t class_def_method_index = 0; |
| 2093 | while (it.HasNextDirectMethod()) { |
| 2094 | if (it.GetMemberIndex() == method_idx) { |
| 2095 | return class_def_method_index; |
| 2096 | } |
| 2097 | class_def_method_index++; |
| 2098 | it.Next(); |
| 2099 | } |
| 2100 | while (it.HasNextVirtualMethod()) { |
| 2101 | if (it.GetMemberIndex() == method_idx) { |
| 2102 | return class_def_method_index; |
| 2103 | } |
| 2104 | class_def_method_index++; |
| 2105 | it.Next(); |
| 2106 | } |
| 2107 | DCHECK(!it.HasNext()); |
| 2108 | LOG(FATAL) << "Failed to find method index " << method_idx << " in " << dex_file.GetLocation(); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 2109 | UNREACHABLE(); |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2110 | } |
| 2111 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2112 | const OatFile::OatMethod ClassLinker::FindOatMethodFor(ArtMethod* method, bool* found) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2113 | // Although we overwrite the trampoline of non-static methods, we may get here via the resolution |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2114 | // method for direct methods (or virtual methods made direct). |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2115 | mirror::Class* declaring_class = method->GetDeclaringClass(); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2116 | size_t oat_method_index; |
| 2117 | if (method->IsStatic() || method->IsDirect()) { |
| 2118 | // Simple case where the oat method index was stashed at load time. |
| 2119 | oat_method_index = method->GetMethodIndex(); |
| 2120 | } else { |
| 2121 | // We're invoking a virtual method directly (thanks to sharpening), compute the oat_method_index |
| 2122 | // by search for its position in the declared virtual methods. |
| 2123 | oat_method_index = declaring_class->NumDirectMethods(); |
| 2124 | size_t end = declaring_class->NumVirtualMethods(); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2125 | bool found_virtual = false; |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2126 | for (size_t i = 0; i < end; i++) { |
Jeff Hao | 68caf9e | 2014-09-03 13:48:16 -0700 | [diff] [blame] | 2127 | // Check method index instead of identity in case of duplicate method definitions. |
| 2128 | if (method->GetDexMethodIndex() == |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2129 | declaring_class->GetVirtualMethod(i, image_pointer_size_)->GetDexMethodIndex()) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2130 | found_virtual = true; |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2131 | break; |
| 2132 | } |
Ian Rogers | f320b63 | 2012-03-13 18:47:47 -0700 | [diff] [blame] | 2133 | oat_method_index++; |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2134 | } |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2135 | CHECK(found_virtual) << "Didn't find oat method index for virtual method: " |
| 2136 | << PrettyMethod(method); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2137 | } |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2138 | DCHECK_EQ(oat_method_index, |
| 2139 | GetOatMethodIndexFromMethodIndex(*declaring_class->GetDexCache()->GetDexFile(), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2140 | method->GetDeclaringClass()->GetDexClassDefIndex(), |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2141 | method->GetDexMethodIndex())); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2142 | OatFile::OatClass oat_class = FindOatClass(*declaring_class->GetDexCache()->GetDexFile(), |
| 2143 | declaring_class->GetDexClassDefIndex(), |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2144 | found); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2145 | if (!(*found)) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2146 | return OatFile::OatMethod::Invalid(); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2147 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2148 | return oat_class.GetOatMethod(oat_method_index); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | // Special case to get oat code without overwriting a trampoline. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2152 | const void* ClassLinker::GetQuickOatCodeFor(ArtMethod* method) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2153 | CHECK(!method->IsAbstract()) << PrettyMethod(method); |
Jeff Hao | 8df6cea | 2013-07-29 13:54:48 -0700 | [diff] [blame] | 2154 | if (method->IsProxyMethod()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2155 | return GetQuickProxyInvokeHandler(); |
Jeff Hao | 8df6cea | 2013-07-29 13:54:48 -0700 | [diff] [blame] | 2156 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2157 | bool found; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2158 | OatFile::OatMethod oat_method = FindOatMethodFor(method, &found); |
Mathieu Chartier | c0d5f89 | 2015-02-25 13:22:57 -0800 | [diff] [blame] | 2159 | if (found) { |
| 2160 | auto* code = oat_method.GetQuickCode(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2161 | if (code != nullptr) { |
| 2162 | return code; |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame] | 2163 | } |
| 2164 | } |
Mathieu Chartier | c0d5f89 | 2015-02-25 13:22:57 -0800 | [diff] [blame] | 2165 | jit::Jit* const jit = Runtime::Current()->GetJit(); |
| 2166 | if (jit != nullptr) { |
| 2167 | auto* code = jit->GetCodeCache()->GetCodeFor(method); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2168 | if (code != nullptr) { |
| 2169 | return code; |
| 2170 | } |
| 2171 | } |
| 2172 | if (method->IsNative()) { |
| 2173 | // No code and native? Use generic trampoline. |
| 2174 | return GetQuickGenericJniStub(); |
| 2175 | } |
| 2176 | return GetQuickToInterpreterBridge(); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2177 | } |
| 2178 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2179 | const void* ClassLinker::GetOatMethodQuickCodeFor(ArtMethod* method) { |
Hiroshi Yamauchi | 9bdec88 | 2014-08-15 17:11:12 -0700 | [diff] [blame] | 2180 | if (method->IsNative() || method->IsAbstract() || method->IsProxyMethod()) { |
| 2181 | return nullptr; |
| 2182 | } |
Mathieu Chartier | c0d5f89 | 2015-02-25 13:22:57 -0800 | [diff] [blame] | 2183 | bool found; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2184 | OatFile::OatMethod oat_method = FindOatMethodFor(method, &found); |
Mathieu Chartier | c0d5f89 | 2015-02-25 13:22:57 -0800 | [diff] [blame] | 2185 | if (found) { |
| 2186 | return oat_method.GetQuickCode(); |
| 2187 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2188 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 2189 | if (jit != nullptr) { |
| 2190 | auto* code = jit->GetCodeCache()->GetCodeFor(method); |
| 2191 | if (code != nullptr) { |
| 2192 | return code; |
| 2193 | } |
| 2194 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2195 | return nullptr; |
Hiroshi Yamauchi | 9bdec88 | 2014-08-15 17:11:12 -0700 | [diff] [blame] | 2196 | } |
| 2197 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2198 | const void* ClassLinker::GetQuickOatCodeFor(const DexFile& dex_file, |
| 2199 | uint16_t class_def_idx, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2200 | uint32_t method_idx) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2201 | bool found; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2202 | OatFile::OatClass oat_class = FindOatClass(dex_file, class_def_idx, &found); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2203 | if (!found) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2204 | return nullptr; |
| 2205 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2206 | uint32_t oat_method_idx = GetOatMethodIndexFromMethodIndex(dex_file, class_def_idx, method_idx); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2207 | return oat_class.GetOatMethod(oat_method_idx).GetQuickCode(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2208 | } |
| 2209 | |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2210 | // Returns true if the method must run with interpreter, false otherwise. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2211 | static bool NeedsInterpreter(ArtMethod* method, const void* quick_code) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 2212 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2213 | if (quick_code == nullptr) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2214 | // No code: need interpreter. |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 2215 | // May return true for native code, in the case of generic JNI |
| 2216 | // DCHECK(!method->IsNative()); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2217 | return true; |
| 2218 | } |
| 2219 | // If interpreter mode is enabled, every method (except native and proxy) must |
| 2220 | // be run with interpreter. |
| 2221 | return Runtime::Current()->GetInstrumentation()->InterpretOnly() && |
| 2222 | !method->IsNative() && !method->IsProxyMethod(); |
| 2223 | } |
| 2224 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2225 | void ClassLinker::FixupStaticTrampolines(mirror::Class* klass) { |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 2226 | DCHECK(klass->IsInitialized()) << PrettyDescriptor(klass); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2227 | if (klass->NumDirectMethods() == 0) { |
| 2228 | return; // No direct methods => no static methods. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2229 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2230 | Runtime* runtime = Runtime::Current(); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 2231 | if (!runtime->IsStarted()) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2232 | if (runtime->IsAotCompiler() || runtime->GetHeap()->HasImageSpace()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 2233 | return; // OAT file unavailable. |
| 2234 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2235 | } |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 2236 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 2237 | const DexFile& dex_file = klass->GetDexFile(); |
| 2238 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2239 | CHECK(dex_class_def != nullptr); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2240 | const uint8_t* class_data = dex_file.GetClassData(*dex_class_def); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2241 | // There should always be class data if there were direct methods. |
| 2242 | CHECK(class_data != nullptr) << PrettyDescriptor(klass); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2243 | ClassDataItemIterator it(dex_file, class_data); |
| 2244 | // Skip fields |
| 2245 | while (it.HasNextStaticField()) { |
| 2246 | it.Next(); |
| 2247 | } |
| 2248 | while (it.HasNextInstanceField()) { |
| 2249 | it.Next(); |
| 2250 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2251 | bool has_oat_class; |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2252 | OatFile::OatClass oat_class = FindOatClass(dex_file, |
| 2253 | klass->GetDexClassDefIndex(), |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2254 | &has_oat_class); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2255 | // Link the code of methods skipped by LinkCode. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2256 | for (size_t method_index = 0; it.HasNextDirectMethod(); ++method_index, it.Next()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2257 | ArtMethod* method = klass->GetDirectMethod(method_index, image_pointer_size_); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2258 | if (!method->IsStatic()) { |
| 2259 | // Only update static methods. |
| 2260 | continue; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2261 | } |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2262 | const void* quick_code = nullptr; |
| 2263 | if (has_oat_class) { |
| 2264 | OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2265 | quick_code = oat_method.GetQuickCode(); |
| 2266 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2267 | const bool enter_interpreter = NeedsInterpreter(method, quick_code); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2268 | if (enter_interpreter) { |
| 2269 | // Use interpreter entry point. |
Ian Rogers | 1a57066 | 2014-03-12 01:02:21 -0700 | [diff] [blame] | 2270 | // Check whether the method is native, in which case it's generic JNI. |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2271 | if (quick_code == nullptr && method->IsNative()) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2272 | quick_code = GetQuickGenericJniStub(); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 2273 | } else { |
| 2274 | quick_code = GetQuickToInterpreterBridge(); |
| 2275 | } |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2276 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2277 | runtime->GetInstrumentation()->UpdateMethodsCode(method, quick_code); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2278 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2279 | // Ignore virtual methods on the iterator. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2280 | } |
| 2281 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2282 | void ClassLinker::LinkCode(ArtMethod* method, const OatFile::OatClass* oat_class, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2283 | uint32_t class_def_method_index) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2284 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2285 | if (runtime->IsAotCompiler()) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2286 | // The following code only applies to a non-compiler runtime. |
| 2287 | return; |
| 2288 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2289 | // Method shouldn't have already been linked. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2290 | DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2291 | if (oat_class != nullptr) { |
| 2292 | // Every kind of method should at least get an invoke stub from the oat_method. |
| 2293 | // non-abstract methods also get their code pointers. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2294 | const OatFile::OatMethod oat_method = oat_class->GetOatMethod(class_def_method_index); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2295 | oat_method.LinkMethod(method); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2296 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2297 | |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 2298 | // Install entry point from interpreter. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2299 | bool enter_interpreter = NeedsInterpreter(method, method->GetEntryPointFromQuickCompiledCode()); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 2300 | |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2301 | if (method->IsAbstract()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2302 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2303 | return; |
| 2304 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2305 | |
| 2306 | if (method->IsStatic() && !method->IsConstructor()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2307 | // For static methods excluding the class initializer, install the trampoline. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2308 | // It will be replaced by the proper entry point by ClassLinker::FixupStaticTrampolines |
| 2309 | // after initializing class (see ClassLinker::InitializeClass method). |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2310 | method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionStub()); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2311 | } else if (enter_interpreter) { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2312 | if (!method->IsNative()) { |
| 2313 | // Set entry point from compiled code if there's no code or in interpreter only mode. |
| 2314 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2315 | } else { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2316 | method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub()); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2317 | } |
Ian Rogers | 0d6de04 | 2012-02-29 08:50:26 -0800 | [diff] [blame] | 2318 | } |
jeffhao | 26c0a1a | 2012-01-17 16:28:33 -0800 | [diff] [blame] | 2319 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2320 | if (method->IsNative()) { |
| 2321 | // Unregistering restores the dlsym lookup stub. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2322 | method->UnregisterNative(); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 2323 | |
| 2324 | if (enter_interpreter) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2325 | // We have a native method here without code. Then it should have either the generic JNI |
| 2326 | // trampoline as entrypoint (non-static), or the resolution trampoline (static). |
| 2327 | // TODO: this doesn't handle all the cases where trampolines may be installed. |
| 2328 | const void* entry_point = method->GetEntryPointFromQuickCompiledCode(); |
| 2329 | DCHECK(IsQuickGenericJniStub(entry_point) || IsQuickResolutionStub(entry_point)); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 2330 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2331 | } |
| 2332 | } |
| 2333 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2334 | void ClassLinker::SetupClass(const DexFile& dex_file, |
| 2335 | const DexFile::ClassDef& dex_class_def, |
| 2336 | Handle<mirror::Class> klass, |
| 2337 | mirror::ClassLoader* class_loader) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2338 | CHECK(klass.Get() != nullptr); |
| 2339 | CHECK(klass->GetDexCache() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2340 | CHECK_EQ(mirror::Class::kStatusNotReady, klass->GetStatus()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 2341 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2342 | CHECK(descriptor != nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2343 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2344 | klass->SetClass(GetClassRoot(kJavaLangClass)); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 2345 | uint32_t access_flags = dex_class_def.GetJavaAccessFlags(); |
Brian Carlstrom | 8e3fb14 | 2013-10-09 21:00:27 -0700 | [diff] [blame] | 2346 | CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2347 | klass->SetAccessFlags(access_flags); |
| 2348 | klass->SetClassLoader(class_loader); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2349 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2350 | mirror::Class::SetStatus(klass, mirror::Class::kStatusIdx, nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2351 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2352 | klass->SetDexClassDefIndex(dex_file.GetIndexForClassDef(dex_class_def)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2353 | klass->SetDexTypeIndex(dex_class_def.class_idx_); |
Mathieu Chartier | 91a6dc4 | 2014-12-01 10:31:15 -0800 | [diff] [blame] | 2354 | CHECK(klass->GetDexCacheStrings() != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2355 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2356 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2357 | void ClassLinker::LoadClass(Thread* self, |
| 2358 | const DexFile& dex_file, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2359 | const DexFile::ClassDef& dex_class_def, |
| 2360 | Handle<mirror::Class> klass) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2361 | const uint8_t* class_data = dex_file.GetClassData(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2362 | if (class_data == nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2363 | return; // no fields or methods - for example a marker interface |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2364 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2365 | bool has_oat_class = false; |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 2366 | if (Runtime::Current()->IsStarted() && !Runtime::Current()->IsAotCompiler()) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2367 | OatFile::OatClass oat_class = FindOatClass(dex_file, klass->GetDexClassDefIndex(), |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2368 | &has_oat_class); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2369 | if (has_oat_class) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2370 | LoadClassMembers(self, dex_file, class_data, klass, &oat_class); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2371 | } |
| 2372 | } |
| 2373 | if (!has_oat_class) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2374 | LoadClassMembers(self, dex_file, class_data, klass, nullptr); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2375 | } |
| 2376 | } |
| 2377 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2378 | LengthPrefixedArray<ArtField>* ClassLinker::AllocArtFieldArray(Thread* self, size_t length) { |
| 2379 | if (length == 0) { |
| 2380 | return nullptr; |
| 2381 | } |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 2382 | // If the ArtField alignment changes, review all uses of LengthPrefixedArray<ArtField>. |
| 2383 | static_assert(alignof(ArtField) == 4, "ArtField alignment is expected to be 4."); |
| 2384 | size_t storage_size = LengthPrefixedArray<ArtField>::ComputeSize(length); |
| 2385 | void* array_storage = Runtime::Current()->GetLinearAlloc()->Alloc(self, storage_size); |
| 2386 | auto* ret = new(array_storage) LengthPrefixedArray<ArtField>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2387 | CHECK(ret != nullptr); |
| 2388 | std::uninitialized_fill_n(&ret->At(0), length, ArtField()); |
| 2389 | return ret; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2390 | } |
| 2391 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2392 | LengthPrefixedArray<ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self, size_t length) { |
| 2393 | if (length == 0) { |
| 2394 | return nullptr; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2395 | } |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 2396 | const size_t method_alignment = ArtMethod::Alignment(image_pointer_size_); |
| 2397 | const size_t method_size = ArtMethod::Size(image_pointer_size_); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 2398 | const size_t storage_size = |
| 2399 | LengthPrefixedArray<ArtMethod>::ComputeSize(length, method_size, method_alignment); |
| 2400 | void* array_storage = Runtime::Current()->GetLinearAlloc()->Alloc(self, storage_size); |
| 2401 | auto* ret = new (array_storage) LengthPrefixedArray<ArtMethod>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2402 | CHECK(ret != nullptr); |
| 2403 | for (size_t i = 0; i < length; ++i) { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 2404 | new(reinterpret_cast<void*>(&ret->At(i, method_size, method_alignment))) ArtMethod; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2405 | } |
| 2406 | return ret; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2407 | } |
| 2408 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2409 | void ClassLinker::LoadClassMembers(Thread* self, |
| 2410 | const DexFile& dex_file, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2411 | const uint8_t* class_data, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2412 | Handle<mirror::Class> klass, |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2413 | const OatFile::OatClass* oat_class) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2414 | { |
| 2415 | // Note: We cannot have thread suspension until the field and method arrays are setup or else |
| 2416 | // Class::VisitFieldRoots may miss some fields or methods. |
| 2417 | ScopedAssertNoThreadSuspension nts(self, __FUNCTION__); |
| 2418 | // Load static fields. |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 2419 | // We allow duplicate definitions of the same field in a class_data_item |
| 2420 | // but ignore the repeated indexes here, b/21868015. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2421 | ClassDataItemIterator it(dex_file, class_data); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2422 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, it.NumStaticFields()); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 2423 | size_t num_sfields = 0; |
| 2424 | uint32_t last_field_idx = 0u; |
| 2425 | for (; it.HasNextStaticField(); it.Next()) { |
| 2426 | uint32_t field_idx = it.GetMemberIndex(); |
| 2427 | DCHECK_GE(field_idx, last_field_idx); // Ordering enforced by DexFileVerifier. |
| 2428 | if (num_sfields == 0 || LIKELY(field_idx > last_field_idx)) { |
| 2429 | DCHECK_LT(num_sfields, it.NumStaticFields()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2430 | LoadField(it, klass, &sfields->At(num_sfields)); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 2431 | ++num_sfields; |
| 2432 | last_field_idx = field_idx; |
| 2433 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2434 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2435 | klass->SetSFieldsPtr(sfields); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2436 | DCHECK_EQ(klass->NumStaticFields(), num_sfields); |
| 2437 | // Load instance fields. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2438 | LengthPrefixedArray<ArtField>* ifields = AllocArtFieldArray(self, it.NumInstanceFields()); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 2439 | size_t num_ifields = 0u; |
| 2440 | last_field_idx = 0u; |
| 2441 | for (; it.HasNextInstanceField(); it.Next()) { |
| 2442 | uint32_t field_idx = it.GetMemberIndex(); |
| 2443 | DCHECK_GE(field_idx, last_field_idx); // Ordering enforced by DexFileVerifier. |
| 2444 | if (num_ifields == 0 || LIKELY(field_idx > last_field_idx)) { |
| 2445 | DCHECK_LT(num_ifields, it.NumInstanceFields()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2446 | LoadField(it, klass, &ifields->At(num_ifields)); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 2447 | ++num_ifields; |
| 2448 | last_field_idx = field_idx; |
| 2449 | } |
| 2450 | } |
| 2451 | if (UNLIKELY(num_sfields != it.NumStaticFields()) || |
| 2452 | UNLIKELY(num_ifields != it.NumInstanceFields())) { |
| 2453 | LOG(WARNING) << "Duplicate fields in class " << PrettyDescriptor(klass.Get()) |
| 2454 | << " (unique static fields: " << num_sfields << "/" << it.NumStaticFields() |
| 2455 | << ", unique instance fields: " << num_ifields << "/" << it.NumInstanceFields() << ")"; |
| 2456 | // NOTE: Not shrinking the over-allocated sfields/ifields. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2457 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2458 | klass->SetIFieldsPtr(ifields); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2459 | DCHECK_EQ(klass->NumInstanceFields(), num_ifields); |
| 2460 | // Load methods. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2461 | klass->SetDirectMethodsPtr(AllocArtMethodArray(self, it.NumDirectMethods())); |
| 2462 | klass->SetVirtualMethodsPtr(AllocArtMethodArray(self, it.NumVirtualMethods())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2463 | size_t class_def_method_index = 0; |
| 2464 | uint32_t last_dex_method_index = DexFile::kDexNoIndex; |
| 2465 | size_t last_class_def_method_index = 0; |
| 2466 | for (size_t i = 0; it.HasNextDirectMethod(); i++, it.Next()) { |
| 2467 | ArtMethod* method = klass->GetDirectMethodUnchecked(i, image_pointer_size_); |
| 2468 | LoadMethod(self, dex_file, it, klass, method); |
| 2469 | LinkCode(method, oat_class, class_def_method_index); |
| 2470 | uint32_t it_method_index = it.GetMemberIndex(); |
| 2471 | if (last_dex_method_index == it_method_index) { |
| 2472 | // duplicate case |
| 2473 | method->SetMethodIndex(last_class_def_method_index); |
| 2474 | } else { |
| 2475 | method->SetMethodIndex(class_def_method_index); |
| 2476 | last_dex_method_index = it_method_index; |
| 2477 | last_class_def_method_index = class_def_method_index; |
| 2478 | } |
| 2479 | class_def_method_index++; |
| 2480 | } |
| 2481 | for (size_t i = 0; it.HasNextVirtualMethod(); i++, it.Next()) { |
| 2482 | ArtMethod* method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
| 2483 | LoadMethod(self, dex_file, it, klass, method); |
| 2484 | DCHECK_EQ(class_def_method_index, it.NumDirectMethods() + i); |
| 2485 | LinkCode(method, oat_class, class_def_method_index); |
| 2486 | class_def_method_index++; |
| 2487 | } |
| 2488 | DCHECK(!it.HasNext()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2489 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 2490 | // Ensure that the card is marked so that remembered sets pick up native roots. |
| 2491 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(klass.Get()); |
Mathieu Chartier | f3f2a7a | 2015-04-14 15:43:10 -0700 | [diff] [blame] | 2492 | self->AllowThreadSuspension(); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2493 | } |
| 2494 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2495 | void ClassLinker::LoadField(const ClassDataItemIterator& it, |
| 2496 | Handle<mirror::Class> klass, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2497 | ArtField* dst) { |
| 2498 | const uint32_t field_idx = it.GetMemberIndex(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2499 | dst->SetDexFieldIndex(field_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2500 | dst->SetDeclaringClass(klass.Get()); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 2501 | dst->SetAccessFlags(it.GetFieldAccessFlags()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2502 | } |
| 2503 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2504 | void ClassLinker::LoadMethod(Thread* self, |
| 2505 | const DexFile& dex_file, |
| 2506 | const ClassDataItemIterator& it, |
| 2507 | Handle<mirror::Class> klass, |
| 2508 | ArtMethod* dst) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2509 | uint32_t dex_method_idx = it.GetMemberIndex(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2510 | const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2511 | const char* method_name = dex_file.StringDataByIdx(method_id.name_idx_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2512 | |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 2513 | ScopedAssertNoThreadSuspension ants(self, "LoadMethod"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2514 | dst->SetDexMethodIndex(dex_method_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2515 | dst->SetDeclaringClass(klass.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2516 | dst->SetCodeItemOffset(it.GetMethodCodeItemOffset()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2517 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2518 | dst->SetDexCacheResolvedMethods(klass->GetDexCache()->GetResolvedMethods(), image_pointer_size_); |
| 2519 | dst->SetDexCacheResolvedTypes(klass->GetDexCache()->GetResolvedTypes(), image_pointer_size_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2520 | |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 2521 | uint32_t access_flags = it.GetMethodAccessFlags(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2522 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2523 | if (UNLIKELY(strcmp("finalize", method_name) == 0)) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2524 | // Set finalizable flag on declaring class. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2525 | if (strcmp("V", dex_file.GetShorty(method_id.proto_idx_)) == 0) { |
| 2526 | // Void return type. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2527 | if (klass->GetClassLoader() != nullptr) { // All non-boot finalizer methods are flagged. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2528 | klass->SetFinalizable(); |
| 2529 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2530 | std::string temp; |
| 2531 | const char* klass_descriptor = klass->GetDescriptor(&temp); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2532 | // The Enum class declares a "final" finalize() method to prevent subclasses from |
| 2533 | // introducing a finalizer. We don't want to set the finalizable flag for Enum or its |
| 2534 | // subclasses, so we exclude it here. |
| 2535 | // We also want to avoid setting the flag on Object, where we know that finalize() is |
| 2536 | // empty. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2537 | if (strcmp(klass_descriptor, "Ljava/lang/Object;") != 0 && |
| 2538 | strcmp(klass_descriptor, "Ljava/lang/Enum;") != 0) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2539 | klass->SetFinalizable(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2540 | } |
| 2541 | } |
| 2542 | } |
| 2543 | } else if (method_name[0] == '<') { |
| 2544 | // Fix broken access flags for initializers. Bug 11157540. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2545 | bool is_init = (strcmp("<init>", method_name) == 0); |
| 2546 | bool is_clinit = !is_init && (strcmp("<clinit>", method_name) == 0); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2547 | if (UNLIKELY(!is_init && !is_clinit)) { |
| 2548 | LOG(WARNING) << "Unexpected '<' at start of method name " << method_name; |
| 2549 | } else { |
| 2550 | if (UNLIKELY((access_flags & kAccConstructor) == 0)) { |
| 2551 | LOG(WARNING) << method_name << " didn't have expected constructor access flag in class " |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2552 | << PrettyDescriptor(klass.Get()) << " in dex file " << dex_file.GetLocation(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2553 | access_flags |= kAccConstructor; |
| 2554 | } |
| 2555 | } |
| 2556 | } |
| 2557 | dst->SetAccessFlags(access_flags); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2558 | } |
| 2559 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2560 | void ClassLinker::AppendToBootClassPath(Thread* self, const DexFile& dex_file) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2561 | StackHandleScope<1> hs(self); |
| 2562 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(AllocDexCache(self, dex_file))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2563 | CHECK(dex_cache.Get() != nullptr) << "Failed to allocate dex cache for " |
| 2564 | << dex_file.GetLocation(); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2565 | AppendToBootClassPath(dex_file, dex_cache); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2566 | } |
| 2567 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2568 | void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2569 | Handle<mirror::DexCache> dex_cache) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2570 | CHECK(dex_cache.Get() != nullptr) << dex_file.GetLocation(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2571 | boot_class_path_.push_back(&dex_file); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2572 | RegisterDexFile(dex_file, dex_cache); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2573 | } |
| 2574 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2575 | void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2576 | Handle<mirror::DexCache> dex_cache) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2577 | Thread* const self = Thread::Current(); |
| 2578 | dex_lock_.AssertExclusiveHeld(self); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2579 | CHECK(dex_cache.Get() != nullptr) << dex_file.GetLocation(); |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 2580 | CHECK(dex_cache->GetLocation()->Equals(dex_file.GetLocation())) |
| 2581 | << dex_cache->GetLocation()->ToModifiedUtf8() << " " << dex_file.GetLocation(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2582 | // Clean up pass to remove null dex caches. |
| 2583 | // Null dex caches can occur due to class unloading and we are lazily removing null entries. |
| 2584 | JavaVMExt* const vm = self->GetJniEnv()->vm; |
| 2585 | for (auto it = dex_caches_.begin(); it != dex_caches_.end();) { |
| 2586 | mirror::Object* dex_cache_root = self->DecodeJObject(*it); |
| 2587 | if (dex_cache_root == nullptr) { |
| 2588 | vm->DeleteWeakGlobalRef(self, *it); |
| 2589 | it = dex_caches_.erase(it); |
| 2590 | } else { |
| 2591 | ++it; |
| 2592 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 2593 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2594 | dex_caches_.push_back(vm->AddWeakGlobalRef(self, dex_cache.Get())); |
| 2595 | dex_cache->SetDexFile(&dex_file); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2596 | } |
| 2597 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2598 | mirror::DexCache* ClassLinker::RegisterDexFile(const DexFile& dex_file) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 2599 | Thread* self = Thread::Current(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 2600 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2601 | ReaderMutexLock mu(self, dex_lock_); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2602 | mirror::DexCache* dex_cache = FindDexCacheLocked(self, dex_file, true); |
| 2603 | if (dex_cache != nullptr) { |
| 2604 | return dex_cache; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 2605 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2606 | } |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 2607 | // Don't alloc while holding the lock, since allocation may need to |
| 2608 | // suspend all threads and another thread may need the dex_lock_ to |
| 2609 | // get to a suspend point. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2610 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2611 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(AllocDexCache(self, dex_file))); |
| 2612 | WriterMutexLock mu(self, dex_lock_); |
| 2613 | mirror::DexCache* dex_cache = FindDexCacheLocked(self, dex_file, true); |
| 2614 | if (dex_cache != nullptr) { |
| 2615 | return dex_cache; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 2616 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2617 | if (h_dex_cache.Get() == nullptr) { |
| 2618 | self->AssertPendingOOMException(); |
| 2619 | return nullptr; |
| 2620 | } |
| 2621 | RegisterDexFileLocked(dex_file, h_dex_cache); |
| 2622 | return h_dex_cache.Get(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2623 | } |
| 2624 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2625 | void ClassLinker::RegisterDexFile(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2626 | Handle<mirror::DexCache> dex_cache) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2627 | WriterMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2628 | RegisterDexFileLocked(dex_file, dex_cache); |
| 2629 | } |
| 2630 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2631 | mirror::DexCache* ClassLinker::FindDexCache(Thread* self, |
| 2632 | const DexFile& dex_file, |
| 2633 | bool allow_failure) { |
| 2634 | ReaderMutexLock mu(self, dex_lock_); |
| 2635 | return FindDexCacheLocked(self, dex_file, allow_failure); |
| 2636 | } |
| 2637 | |
| 2638 | mirror::DexCache* ClassLinker::FindDexCacheLocked(Thread* self, |
| 2639 | const DexFile& dex_file, |
| 2640 | bool allow_failure) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2641 | // Search assuming unique-ness of dex file. |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2642 | JavaVMExt* const vm = self->GetJniEnv()->vm; |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame^] | 2643 | for (jweak weak_root : dex_caches_) { |
Mathieu Chartier | ca33089 | 2015-09-01 20:00:10 -0700 | [diff] [blame] | 2644 | mirror::DexCache* dex_cache = down_cast<mirror::DexCache*>( |
| 2645 | vm->DecodeWeakGlobal(self, weak_root)); |
| 2646 | if (dex_cache != nullptr && dex_cache->GetDexFile() == &dex_file) { |
| 2647 | return dex_cache; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2648 | } |
| 2649 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2650 | if (allow_failure) { |
| 2651 | return nullptr; |
| 2652 | } |
Mathieu Chartier | ac8f439 | 2015-08-27 13:54:20 -0700 | [diff] [blame] | 2653 | std::string location(dex_file.GetLocation()); |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 2654 | // Failure, dump diagnostic and abort. |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2655 | for (jobject weak_root : dex_caches_) { |
| 2656 | mirror::DexCache* dex_cache = down_cast<mirror::DexCache*>(self->DecodeJObject(weak_root)); |
| 2657 | if (dex_cache != nullptr) { |
| 2658 | LOG(ERROR) << "Registered dex file " << dex_cache->GetDexFile()->GetLocation(); |
| 2659 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 2660 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2661 | LOG(FATAL) << "Failed to find DexCache for DexFile " << location; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 2662 | UNREACHABLE(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2663 | } |
| 2664 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2665 | void ClassLinker::FixupDexCaches(ArtMethod* resolution_method) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2666 | Thread* const self = Thread::Current(); |
| 2667 | ReaderMutexLock mu(self, dex_lock_); |
| 2668 | for (jobject weak_root : dex_caches_) { |
| 2669 | mirror::DexCache* dex_cache = down_cast<mirror::DexCache*>(self->DecodeJObject(weak_root)); |
| 2670 | if (dex_cache != nullptr) { |
| 2671 | dex_cache->Fixup(resolution_method, image_pointer_size_); |
| 2672 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2673 | } |
| 2674 | } |
| 2675 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2676 | mirror::Class* ClassLinker::CreatePrimitiveClass(Thread* self, Primitive::Type type) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2677 | mirror::Class* klass = AllocClass(self, mirror::Class::PrimitiveClassSize(image_pointer_size_)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2678 | if (UNLIKELY(klass == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2679 | self->AssertPendingOOMException(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2680 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2681 | } |
| 2682 | return InitializePrimitiveClass(klass, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2683 | } |
| 2684 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2685 | mirror::Class* ClassLinker::InitializePrimitiveClass(mirror::Class* primitive_class, |
| 2686 | Primitive::Type type) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2687 | CHECK(primitive_class != nullptr); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 2688 | // Must hold lock on object when initializing. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2689 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2690 | StackHandleScope<1> hs(self); |
| 2691 | Handle<mirror::Class> h_class(hs.NewHandle(primitive_class)); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 2692 | ObjectLock<mirror::Class> lock(self, h_class); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2693 | h_class->SetAccessFlags(kAccPublic | kAccFinal | kAccAbstract); |
| 2694 | h_class->SetPrimitiveType(type); |
| 2695 | mirror::Class::SetStatus(h_class, mirror::Class::kStatusInitialized, self); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2696 | const char* descriptor = Primitive::Descriptor(type); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2697 | mirror::Class* existing = InsertClass(descriptor, h_class.Get(), |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2698 | ComputeModifiedUtf8Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2699 | CHECK(existing == nullptr) << "InitPrimitiveClass(" << type << ") failed"; |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2700 | return h_class.Get(); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 2701 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2702 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 2703 | // Create an array class (i.e. the class object for the array, not the |
| 2704 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 2705 | // "[Ljava/lang/String;". |
| 2706 | // |
| 2707 | // If "descriptor" refers to an array of primitives, look up the |
| 2708 | // primitive type's internally-generated class object. |
| 2709 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2710 | // "class_loader" is the class loader of the class that's referring to |
| 2711 | // us. It's used to ensure that we're looking for the element type in |
| 2712 | // the right context. It does NOT become the class loader for the |
| 2713 | // array class; that always comes from the base element class. |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 2714 | // |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2715 | // Returns null with an exception raised on failure. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2716 | mirror::Class* ClassLinker::CreateArrayClass(Thread* self, const char* descriptor, size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2717 | Handle<mirror::ClassLoader> class_loader) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2718 | // Identify the underlying component type |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2719 | CHECK_EQ('[', descriptor[0]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2720 | StackHandleScope<2> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2721 | MutableHandle<mirror::Class> component_type(hs.NewHandle(FindClass(self, descriptor + 1, |
| 2722 | class_loader))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2723 | if (component_type.Get() == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 2724 | DCHECK(self->IsExceptionPending()); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 2725 | // We need to accept erroneous classes as component types. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2726 | const size_t component_hash = ComputeModifiedUtf8Hash(descriptor + 1); |
| 2727 | component_type.Assign(LookupClass(self, descriptor + 1, component_hash, class_loader.Get())); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 2728 | if (component_type.Get() == nullptr) { |
| 2729 | DCHECK(self->IsExceptionPending()); |
| 2730 | return nullptr; |
| 2731 | } else { |
| 2732 | self->ClearException(); |
| 2733 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2734 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 2735 | if (UNLIKELY(component_type->IsPrimitiveVoid())) { |
| 2736 | ThrowNoClassDefFoundError("Attempt to create array of void primitive type"); |
| 2737 | return nullptr; |
| 2738 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2739 | // See if the component type is already loaded. Array classes are |
| 2740 | // always associated with the class loader of their underlying |
| 2741 | // element type -- an array of Strings goes with the loader for |
| 2742 | // java/lang/String -- so we need to look for it there. (The |
| 2743 | // caller should have checked for the existence of the class |
| 2744 | // before calling here, but they did so with *their* class loader, |
| 2745 | // not the component type's loader.) |
| 2746 | // |
| 2747 | // If we find it, the caller adds "loader" to the class' initiating |
| 2748 | // loader list, which should prevent us from going through this again. |
| 2749 | // |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2750 | // This call is unnecessary if "loader" and "component_type->GetClassLoader()" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2751 | // are the same, because our caller (FindClass) just did the |
| 2752 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 2753 | // because we effectively do this lookup again when we add the new |
| 2754 | // class to the hash table --- necessary because of possible races with |
| 2755 | // other threads.) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2756 | if (class_loader.Get() != component_type->GetClassLoader()) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2757 | mirror::Class* new_class = LookupClass(self, descriptor, hash, component_type->GetClassLoader()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2758 | if (new_class != nullptr) { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 2759 | return new_class; |
| 2760 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2761 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 2762 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2763 | // Fill out the fields in the Class. |
| 2764 | // |
| 2765 | // It is possible to execute some methods against arrays, because |
| 2766 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 2767 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 2768 | // |
| 2769 | // Array classes are simple enough that we don't need to do a full |
| 2770 | // link step. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2771 | auto new_class = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2772 | if (UNLIKELY(!init_done_)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2773 | // Classes that were hand created, ie not by FindSystemClass |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2774 | if (strcmp(descriptor, "[Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2775 | new_class.Assign(GetClassRoot(kClassArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2776 | } else if (strcmp(descriptor, "[Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2777 | new_class.Assign(GetClassRoot(kObjectArrayClass)); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2778 | } else if (strcmp(descriptor, GetClassRootDescriptor(kJavaLangStringArrayClass)) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2779 | new_class.Assign(GetClassRoot(kJavaLangStringArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2780 | } else if (strcmp(descriptor, "[C") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2781 | new_class.Assign(GetClassRoot(kCharArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2782 | } else if (strcmp(descriptor, "[I") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2783 | new_class.Assign(GetClassRoot(kIntArrayClass)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2784 | } else if (strcmp(descriptor, "[J") == 0) { |
| 2785 | new_class.Assign(GetClassRoot(kLongArrayClass)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2786 | } |
| 2787 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2788 | if (new_class.Get() == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2789 | new_class.Assign(AllocClass(self, mirror::Array::ClassSize(image_pointer_size_))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2790 | if (new_class.Get() == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2791 | self->AssertPendingOOMException(); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 2792 | return nullptr; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2793 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2794 | new_class->SetComponentType(component_type.Get()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2795 | } |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 2796 | 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] | 2797 | DCHECK(new_class->GetComponentType() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2798 | mirror::Class* java_lang_Object = GetClassRoot(kJavaLangObject); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2799 | new_class->SetSuperClass(java_lang_Object); |
| 2800 | new_class->SetVTable(java_lang_Object->GetVTable()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2801 | new_class->SetPrimitiveType(Primitive::kPrimNot); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2802 | new_class->SetClassLoader(component_type->GetClassLoader()); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 2803 | if (component_type->IsPrimitive()) { |
| 2804 | new_class->SetClassFlags(mirror::kClassFlagNoReferenceFields); |
| 2805 | } else { |
| 2806 | new_class->SetClassFlags(mirror::kClassFlagObjectArray); |
| 2807 | } |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2808 | mirror::Class::SetStatus(new_class, mirror::Class::kStatusLoaded, self); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 2809 | { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2810 | ArtMethod* imt[mirror::Class::kImtSize]; |
| 2811 | std::fill_n(imt, arraysize(imt), Runtime::Current()->GetImtUnimplementedMethod()); |
| 2812 | new_class->PopulateEmbeddedImtAndVTable(imt, image_pointer_size_); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 2813 | } |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2814 | mirror::Class::SetStatus(new_class, mirror::Class::kStatusInitialized, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2815 | // don't need to set new_class->SetObjectSize(..) |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 2816 | // because Object::SizeOf delegates to Array::SizeOf |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2817 | |
| 2818 | |
| 2819 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 2820 | // interfaces. We need to set that up here, so that stuff like |
| 2821 | // "instanceof" works right. |
| 2822 | // |
| 2823 | // Note: The GC could run during the call to FindSystemClass, |
| 2824 | // so we need to make sure the class object is GC-valid while we're in |
| 2825 | // there. Do this by clearing the interface list so the GC will just |
| 2826 | // think that the entries are null. |
| 2827 | |
| 2828 | |
| 2829 | // Use the single, global copies of "interfaces" and "iftable" |
| 2830 | // (remember not to free them for arrays). |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2831 | { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 2832 | mirror::IfTable* array_iftable = array_iftable_.Read(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2833 | CHECK(array_iftable != nullptr); |
| 2834 | new_class->SetIfTable(array_iftable); |
| 2835 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2836 | |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 2837 | // Inherit access flags from the component type. |
| 2838 | int access_flags = new_class->GetComponentType()->GetAccessFlags(); |
| 2839 | // Lose any implementation detail flags; in particular, arrays aren't finalizable. |
| 2840 | access_flags &= kAccJavaFlagsMask; |
| 2841 | // 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] | 2842 | // and remove "interface". |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 2843 | access_flags |= kAccAbstract | kAccFinal; |
| 2844 | access_flags &= ~kAccInterface; |
| 2845 | |
| 2846 | new_class->SetAccessFlags(access_flags); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2847 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2848 | mirror::Class* existing = InsertClass(descriptor, new_class.Get(), hash); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 2849 | if (existing == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2850 | return new_class.Get(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2851 | } |
| 2852 | // Another thread must have loaded the class after we |
| 2853 | // started but before we finished. Abandon what we've |
| 2854 | // done. |
| 2855 | // |
| 2856 | // (Yes, this happens.) |
| 2857 | |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 2858 | return existing; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 2859 | } |
| 2860 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2861 | mirror::Class* ClassLinker::FindPrimitiveClass(char type) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2862 | switch (type) { |
| 2863 | case 'B': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2864 | return GetClassRoot(kPrimitiveByte); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2865 | case 'C': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2866 | return GetClassRoot(kPrimitiveChar); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2867 | case 'D': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2868 | return GetClassRoot(kPrimitiveDouble); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2869 | case 'F': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2870 | return GetClassRoot(kPrimitiveFloat); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2871 | case 'I': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2872 | return GetClassRoot(kPrimitiveInt); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2873 | case 'J': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2874 | return GetClassRoot(kPrimitiveLong); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2875 | case 'S': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2876 | return GetClassRoot(kPrimitiveShort); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2877 | case 'Z': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2878 | return GetClassRoot(kPrimitiveBoolean); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2879 | case 'V': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2880 | return GetClassRoot(kPrimitiveVoid); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2881 | default: |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2882 | break; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 2883 | } |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 2884 | std::string printable_type(PrintableChar(type)); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2885 | ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2886 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2887 | } |
| 2888 | |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2889 | mirror::Class* ClassLinker::InsertClass(const char* descriptor, mirror::Class* klass, size_t hash) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2890 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2891 | mirror::DexCache* dex_cache = klass->GetDexCache(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 2892 | std::string source; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2893 | if (dex_cache != nullptr) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 2894 | source += " from "; |
| 2895 | source += dex_cache->GetLocation()->ToModifiedUtf8(); |
| 2896 | } |
| 2897 | LOG(INFO) << "Loaded class " << descriptor << source; |
| 2898 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2899 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2900 | mirror::ClassLoader* const class_loader = klass->GetClassLoader(); |
| 2901 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader); |
| 2902 | mirror::Class* existing = class_table->Lookup(descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2903 | if (existing != nullptr) { |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 2904 | return existing; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 2905 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2906 | if (kIsDebugBuild && !klass->IsTemp() && class_loader == nullptr && |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2907 | dex_cache_image_class_lookup_required_) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2908 | // Check a class loaded with the system class loader matches one in the image if the class |
| 2909 | // is in the image. |
| 2910 | existing = LookupClassFromImage(descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2911 | if (existing != nullptr) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2912 | CHECK_EQ(klass, existing); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2913 | } |
| 2914 | } |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 2915 | VerifyObject(klass); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2916 | class_table->InsertWithHash(klass, hash); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 2917 | if (class_loader != nullptr) { |
| 2918 | // This is necessary because we need to have the card dirtied for remembered sets. |
| 2919 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader); |
| 2920 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 2921 | if (log_new_class_table_roots_) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2922 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 2923 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2924 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2925 | } |
| 2926 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2927 | void ClassLinker::UpdateClassVirtualMethods(mirror::Class* klass, |
| 2928 | LengthPrefixedArray<ArtMethod>* new_methods) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2929 | klass->SetVirtualMethodsPtr(new_methods); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2930 | // 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] | 2931 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(klass); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2932 | } |
| 2933 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2934 | bool ClassLinker::RemoveClass(const char* descriptor, mirror::ClassLoader* class_loader) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2935 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2936 | ClassTable* const class_table = ClassTableForClassLoader(class_loader); |
| 2937 | return class_table != nullptr && class_table->Remove(descriptor); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 2938 | } |
| 2939 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2940 | mirror::Class* ClassLinker::LookupClass(Thread* self, |
| 2941 | const char* descriptor, |
| 2942 | size_t hash, |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2943 | mirror::ClassLoader* class_loader) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2944 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2945 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2946 | ClassTable* const class_table = ClassTableForClassLoader(class_loader); |
| 2947 | if (class_table != nullptr) { |
| 2948 | mirror::Class* result = class_table->Lookup(descriptor, hash); |
| 2949 | if (result != nullptr) { |
| 2950 | return result; |
| 2951 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2952 | } |
Sameer Abu Asal | 2c6de22 | 2013-05-02 17:38:59 -0700 | [diff] [blame] | 2953 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2954 | if (class_loader != nullptr || !dex_cache_image_class_lookup_required_) { |
| 2955 | return nullptr; |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2956 | } |
| 2957 | // Lookup failed but need to search dex_caches_. |
| 2958 | mirror::Class* result = LookupClassFromImage(descriptor); |
| 2959 | if (result != nullptr) { |
| 2960 | result = InsertClass(descriptor, result, hash); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2961 | } else { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2962 | // Searching the image dex files/caches failed, we don't want to get into this situation |
| 2963 | // often as map searches are faster, so after kMaxFailedDexCacheLookups move all image |
| 2964 | // classes into the class table. |
| 2965 | constexpr uint32_t kMaxFailedDexCacheLookups = 1000; |
| 2966 | if (++failed_dex_cache_class_lookups_ > kMaxFailedDexCacheLookups) { |
| 2967 | MoveImageClassesToClassTable(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 2968 | } |
| 2969 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2970 | return result; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2971 | } |
| 2972 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2973 | static mirror::ObjectArray<mirror::DexCache>* GetImageDexCaches() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 2974 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2975 | gc::space::ImageSpace* image = Runtime::Current()->GetHeap()->GetImageSpace(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2976 | CHECK(image != nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2977 | mirror::Object* root = image->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches); |
| 2978 | return root->AsObjectArray<mirror::DexCache>(); |
| 2979 | } |
| 2980 | |
| 2981 | void ClassLinker::MoveImageClassesToClassTable() { |
| 2982 | Thread* self = Thread::Current(); |
| 2983 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2984 | if (!dex_cache_image_class_lookup_required_) { |
| 2985 | return; // All dex cache classes are already in the class table. |
| 2986 | } |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 2987 | ScopedAssertNoThreadSuspension ants(self, "Moving image classes to class table"); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2988 | mirror::ObjectArray<mirror::DexCache>* dex_caches = GetImageDexCaches(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2989 | std::string temp; |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2990 | ClassTable* const class_table = InsertClassTableForClassLoader(nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2991 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 2992 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2993 | GcRoot<mirror::Class>* types = dex_cache->GetResolvedTypes(); |
| 2994 | for (int32_t j = 0, num_types = dex_cache->NumResolvedTypes(); j < num_types; j++) { |
| 2995 | mirror::Class* klass = types[j].Read(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2996 | if (klass != nullptr) { |
| 2997 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2998 | const char* descriptor = klass->GetDescriptor(&temp); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2999 | size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 3000 | mirror::Class* existing = class_table->Lookup(descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3001 | if (existing != nullptr) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3002 | CHECK_EQ(existing, klass) << PrettyClassAndClassLoader(existing) << " != " |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3003 | << PrettyClassAndClassLoader(klass); |
| 3004 | } else { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 3005 | class_table->Insert(klass); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3006 | if (log_new_class_table_roots_) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3007 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3008 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3009 | } |
| 3010 | } |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 3011 | } |
| 3012 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3013 | dex_cache_image_class_lookup_required_ = false; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3014 | } |
| 3015 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame^] | 3016 | class MoveClassTableToPreZygoteVisitor : public ClassLoaderVisitor { |
| 3017 | public: |
| 3018 | explicit MoveClassTableToPreZygoteVisitor() {} |
| 3019 | |
| 3020 | void Visit(mirror::ClassLoader* class_loader) |
| 3021 | REQUIRES(Locks::classlinker_classes_lock_) |
| 3022 | SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE { |
| 3023 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3024 | if (class_table != nullptr) { |
| 3025 | class_table->FreezeSnapshot(); |
| 3026 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 3027 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame^] | 3028 | }; |
| 3029 | |
| 3030 | void ClassLinker::MoveClassTableToPreZygote() { |
| 3031 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3032 | boot_class_table_.FreezeSnapshot(); |
| 3033 | MoveClassTableToPreZygoteVisitor visitor; |
| 3034 | VisitClassLoadersAndRemoveClearedLoaders(&visitor); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3035 | } |
| 3036 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3037 | mirror::Class* ClassLinker::LookupClassFromImage(const char* descriptor) { |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 3038 | ScopedAssertNoThreadSuspension ants(Thread::Current(), "Image class lookup"); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3039 | mirror::ObjectArray<mirror::DexCache>* dex_caches = GetImageDexCaches(); |
| 3040 | for (int32_t i = 0; i < dex_caches->GetLength(); ++i) { |
| 3041 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 3042 | const DexFile* dex_file = dex_cache->GetDexFile(); |
Vladimir Marko | 801a811 | 2014-01-06 14:28:16 +0000 | [diff] [blame] | 3043 | // Try binary searching the string/type index. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3044 | const DexFile::StringId* string_id = dex_file->FindStringId(descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3045 | if (string_id != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3046 | const DexFile::TypeId* type_id = |
| 3047 | dex_file->FindTypeId(dex_file->GetIndexForStringId(*string_id)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3048 | if (type_id != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3049 | uint16_t type_idx = dex_file->GetIndexForTypeId(*type_id); |
| 3050 | mirror::Class* klass = dex_cache->GetResolvedType(type_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3051 | if (klass != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3052 | return klass; |
| 3053 | } |
| 3054 | } |
| 3055 | } |
| 3056 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3057 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3058 | } |
| 3059 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame^] | 3060 | // Look up classes by hash and descriptor and put all matching ones in the result array. |
| 3061 | class LookupClassesVisitor : public ClassLoaderVisitor { |
| 3062 | public: |
| 3063 | LookupClassesVisitor(const char* descriptor, size_t hash, std::vector<mirror::Class*>* result) |
| 3064 | : descriptor_(descriptor), |
| 3065 | hash_(hash), |
| 3066 | result_(result) {} |
| 3067 | |
| 3068 | void Visit(mirror::ClassLoader* class_loader) |
| 3069 | SHARED_REQUIRES(Locks::classlinker_classes_lock_, Locks::mutator_lock_) OVERRIDE { |
| 3070 | ClassTable* const class_table = class_loader->GetClassTable(); |
| 3071 | mirror::Class* klass = class_table->Lookup(descriptor_, hash_); |
| 3072 | if (klass != nullptr) { |
| 3073 | result_->push_back(klass); |
| 3074 | } |
| 3075 | } |
| 3076 | |
| 3077 | private: |
| 3078 | const char* const descriptor_; |
| 3079 | const size_t hash_; |
| 3080 | std::vector<mirror::Class*>* const result_; |
| 3081 | }; |
| 3082 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 3083 | void ClassLinker::LookupClasses(const char* descriptor, std::vector<mirror::Class*>& result) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3084 | result.clear(); |
| 3085 | if (dex_cache_image_class_lookup_required_) { |
| 3086 | MoveImageClassesToClassTable(); |
| 3087 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame^] | 3088 | Thread* const self = Thread::Current(); |
| 3089 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3090 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
| 3091 | mirror::Class* klass = boot_class_table_.Lookup(descriptor, hash); |
| 3092 | if (klass != nullptr) { |
| 3093 | result.push_back(klass); |
| 3094 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame^] | 3095 | LookupClassesVisitor visitor(descriptor, hash, &result); |
| 3096 | VisitClassLoaders(&visitor); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 3097 | } |
| 3098 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3099 | void ClassLinker::VerifyClass(Thread* self, Handle<mirror::Class> klass) { |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 3100 | // TODO: assert that the monitor on the Class is held |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3101 | ObjectLock<mirror::Class> lock(self, klass); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 3102 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3103 | // Don't attempt to re-verify if already sufficiently verified. |
Andreas Gampe | bb0c7f6 | 2014-09-11 10:59:33 -0700 | [diff] [blame] | 3104 | if (klass->IsVerified()) { |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 3105 | EnsurePreverifiedMethods(klass); |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3106 | return; |
| 3107 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3108 | if (klass->IsCompileTimeVerified() && Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | bb0c7f6 | 2014-09-11 10:59:33 -0700 | [diff] [blame] | 3109 | return; |
| 3110 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3111 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3112 | // The class might already be erroneous, for example at compile time if we attempted to verify |
| 3113 | // this class as a parent to another. |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 3114 | if (klass->IsErroneous()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3115 | ThrowEarlierClassFailure(klass.Get()); |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 3116 | return; |
| 3117 | } |
| 3118 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3119 | if (klass->GetStatus() == mirror::Class::kStatusResolved) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3120 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerifying, self); |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3121 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3122 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3123 | << PrettyClass(klass.Get()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3124 | CHECK(!Runtime::Current()->IsAotCompiler()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3125 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerifyingAtRuntime, self); |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3126 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3127 | |
Igor Murashkin | 7617abd | 2015-07-10 18:27:47 -0700 | [diff] [blame] | 3128 | // Skip verification if we are forcing a soft fail. |
| 3129 | // This has to be before the normal verification enabled check, |
| 3130 | // since technically verification is disabled in this mode. |
| 3131 | if (UNLIKELY(Runtime::Current()->IsVerificationSoftFail())) { |
| 3132 | // Force verification to be a 'soft failure'. |
| 3133 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, self); |
| 3134 | // As this is a fake verified status, make sure the methods are _not_ marked preverified |
| 3135 | // later. |
| 3136 | klass->SetPreverified(); |
| 3137 | return; |
| 3138 | } |
| 3139 | |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 3140 | // Skip verification if disabled. |
| 3141 | if (!Runtime::Current()->IsVerificationEnabled()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3142 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, self); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 3143 | EnsurePreverifiedMethods(klass); |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 3144 | return; |
| 3145 | } |
| 3146 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3147 | // Verify super class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3148 | StackHandleScope<2> hs(self); |
| 3149 | Handle<mirror::Class> super(hs.NewHandle(klass->GetSuperClass())); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3150 | if (super.Get() != nullptr) { |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3151 | // Acquire lock to prevent races on verifying the super class. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3152 | ObjectLock<mirror::Class> super_lock(self, super); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3153 | |
| 3154 | if (!super->IsVerified() && !super->IsErroneous()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3155 | VerifyClass(self, super); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3156 | } |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 3157 | if (!super->IsCompileTimeVerified()) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3158 | std::string error_msg( |
| 3159 | StringPrintf("Rejecting class %s that attempts to sub-class erroneous class %s", |
| 3160 | PrettyDescriptor(klass.Get()).c_str(), |
| 3161 | PrettyDescriptor(super.Get()).c_str())); |
Andreas Gampe | e4301ff | 2015-02-17 19:25:29 -0800 | [diff] [blame] | 3162 | LOG(WARNING) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3163 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3164 | if (cause.Get() != nullptr) { |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3165 | self->ClearException(); |
| 3166 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3167 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
| 3168 | if (cause.Get() != nullptr) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3169 | self->GetException()->SetCause(cause.Get()); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3170 | } |
Jeff Hao | 22cb09b | 2013-12-12 14:29:15 -0800 | [diff] [blame] | 3171 | ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3172 | if (Runtime::Current()->IsAotCompiler()) { |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 3173 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
| 3174 | } |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3175 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3176 | return; |
| 3177 | } |
| 3178 | } |
| 3179 | |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 3180 | // 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] | 3181 | const DexFile& dex_file = *klass->GetDexCache()->GetDexFile(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3182 | mirror::Class::Status oat_file_class_status(mirror::Class::kStatusNotReady); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3183 | bool preverified = VerifyClassUsingOatFile(dex_file, klass.Get(), oat_file_class_status); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3184 | if (oat_file_class_status == mirror::Class::kStatusError) { |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 3185 | VLOG(class_linker) << "Skipping runtime verification of erroneous class " |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3186 | << PrettyDescriptor(klass.Get()) << " in " |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 3187 | << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3188 | ThrowVerifyError(klass.Get(), "Rejecting class %s because it failed compile-time verification", |
| 3189 | PrettyDescriptor(klass.Get()).c_str()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3190 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 3191 | return; |
| 3192 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3193 | verifier::MethodVerifier::FailureKind verifier_failure = verifier::MethodVerifier::kNoFailure; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3194 | std::string error_msg; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 3195 | if (!preverified) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3196 | verifier_failure = verifier::MethodVerifier::VerifyClass(self, |
| 3197 | klass.Get(), |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3198 | Runtime::Current()->IsAotCompiler(), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3199 | &error_msg); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 3200 | } |
| 3201 | if (preverified || verifier_failure != verifier::MethodVerifier::kHardFailure) { |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 3202 | if (!preverified && verifier_failure != verifier::MethodVerifier::kNoFailure) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3203 | VLOG(class_linker) << "Soft verification failure in class " << PrettyDescriptor(klass.Get()) |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 3204 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 3205 | << " because: " << error_msg; |
| 3206 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3207 | self->AssertNoPendingException(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3208 | // Make sure all classes referenced by catch blocks are resolved. |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3209 | ResolveClassExceptionHandlerTypes(dex_file, klass); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3210 | if (verifier_failure == verifier::MethodVerifier::kNoFailure) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3211 | // Even though there were no verifier failures we need to respect whether the super-class |
| 3212 | // was verified or requiring runtime reverification. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3213 | if (super.Get() == nullptr || super->IsVerified()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3214 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3215 | } else { |
| 3216 | CHECK_EQ(super->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3217 | mirror::Class::SetStatus(klass, mirror::Class::kStatusRetryVerificationAtRuntime, self); |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 3218 | // Pretend a soft failure occured so that we don't consider the class verified below. |
| 3219 | verifier_failure = verifier::MethodVerifier::kSoftFailure; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3220 | } |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3221 | } else { |
| 3222 | CHECK_EQ(verifier_failure, verifier::MethodVerifier::kSoftFailure); |
| 3223 | // Soft failures at compile time should be retried at runtime. Soft |
| 3224 | // failures at runtime will be handled by slow paths in the generated |
| 3225 | // code. Set status accordingly. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3226 | if (Runtime::Current()->IsAotCompiler()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3227 | mirror::Class::SetStatus(klass, mirror::Class::kStatusRetryVerificationAtRuntime, self); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3228 | } else { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3229 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, self); |
Andreas Gampe | bb0c7f6 | 2014-09-11 10:59:33 -0700 | [diff] [blame] | 3230 | // As this is a fake verified status, make sure the methods are _not_ marked preverified |
| 3231 | // later. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3232 | klass->SetPreverified(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3233 | } |
| 3234 | } |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 3235 | } else { |
Andreas Gampe | e4301ff | 2015-02-17 19:25:29 -0800 | [diff] [blame] | 3236 | LOG(WARNING) << "Verification failed on class " << PrettyDescriptor(klass.Get()) |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3237 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 3238 | << " because: " << error_msg; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3239 | self->AssertNoPendingException(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3240 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3241 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 3242 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3243 | if (preverified || verifier_failure == verifier::MethodVerifier::kNoFailure) { |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 3244 | // Class is verified so we don't need to do any access check on its methods. |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3245 | // Let the interpreter know it by setting the kAccPreverified flag onto each |
| 3246 | // method. |
| 3247 | // Note: we're going here during compilation and at runtime. When we set the |
| 3248 | // kAccPreverified flag when compiling image classes, the flag is recorded |
| 3249 | // in the image and is set when loading the image. |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 3250 | EnsurePreverifiedMethods(klass); |
| 3251 | } |
| 3252 | } |
| 3253 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3254 | void ClassLinker::EnsurePreverifiedMethods(Handle<mirror::Class> klass) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3255 | if (!klass->IsPreverified()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3256 | klass->SetPreverifiedFlagOnAllMethods(image_pointer_size_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3257 | klass->SetPreverified(); |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3258 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3259 | } |
| 3260 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3261 | bool ClassLinker::VerifyClassUsingOatFile(const DexFile& dex_file, |
| 3262 | mirror::Class* klass, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3263 | mirror::Class::Status& oat_file_class_status) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3264 | // If we're compiling, we can only verify the class using the oat file if |
| 3265 | // we are not compiling the image or if the class we're verifying is not part of |
| 3266 | // the app. In other words, we will only check for preverification of bootclasspath |
| 3267 | // classes. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3268 | if (Runtime::Current()->IsAotCompiler()) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3269 | // Are we compiling the bootclasspath? |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 3270 | if (Runtime::Current()->GetCompilerCallbacks()->IsBootImage()) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3271 | return false; |
| 3272 | } |
| 3273 | // We are compiling an app (not the image). |
| 3274 | |
| 3275 | // Is this an app class? (I.e. not a bootclasspath class) |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3276 | if (klass->GetClassLoader() != nullptr) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3277 | return false; |
| 3278 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3279 | } |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3280 | |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 3281 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3282 | // In case we run without an image there won't be a backing oat file. |
| 3283 | if (oat_dex_file == nullptr) { |
Anwar Ghuloum | ad256bb | 2013-07-18 14:58:55 -0700 | [diff] [blame] | 3284 | return false; |
| 3285 | } |
| 3286 | |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 3287 | // We may be running with a preopted oat file but without image. In this case, |
| 3288 | // we don't skip verification of preverified classes to ensure we initialize |
| 3289 | // dex caches with all types resolved during verification. |
| 3290 | // We need to trust image classes, as these might be coming out of a pre-opted, quickened boot |
| 3291 | // image (that we just failed loading), and the verifier can't be run on quickened opcodes when |
| 3292 | // the runtime isn't started. On the other hand, app classes can be re-verified even if they are |
| 3293 | // already pre-opted, as then the runtime is started. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3294 | if (!Runtime::Current()->IsAotCompiler() && |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 3295 | !Runtime::Current()->GetHeap()->HasImageSpace() && |
| 3296 | klass->GetClassLoader() != nullptr) { |
| 3297 | return false; |
| 3298 | } |
| 3299 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3300 | uint16_t class_def_index = klass->GetDexClassDefIndex(); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 3301 | oat_file_class_status = oat_dex_file->GetOatClass(class_def_index).GetStatus(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3302 | if (oat_file_class_status == mirror::Class::kStatusVerified || |
| 3303 | oat_file_class_status == mirror::Class::kStatusInitialized) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3304 | return true; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3305 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3306 | if (oat_file_class_status == mirror::Class::kStatusRetryVerificationAtRuntime) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 3307 | // Compile time verification failed with a soft error. Compile time verification can fail |
| 3308 | // because we have incomplete type information. Consider the following: |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3309 | // class ... { |
| 3310 | // Foo x; |
| 3311 | // .... () { |
| 3312 | // if (...) { |
| 3313 | // v1 gets assigned a type of resolved class Foo |
| 3314 | // } else { |
| 3315 | // v1 gets assigned a type of unresolved class Bar |
| 3316 | // } |
| 3317 | // iput x = v1 |
| 3318 | // } } |
| 3319 | // when we merge v1 following the if-the-else it results in Conflict |
| 3320 | // (see verifier::RegType::Merge) as we can't know the type of Bar and we could possibly be |
| 3321 | // allowing an unsafe assignment to the field x in the iput (javac may have compiled this as |
| 3322 | // it knew Bar was a sub-class of Foo, but for us this may have been moved into a separate apk |
| 3323 | // at compile time). |
| 3324 | return false; |
| 3325 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3326 | if (oat_file_class_status == mirror::Class::kStatusError) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 3327 | // Compile time verification failed with a hard error. This is caused by invalid instructions |
| 3328 | // in the class. These errors are unrecoverable. |
| 3329 | return false; |
| 3330 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3331 | if (oat_file_class_status == mirror::Class::kStatusNotReady) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3332 | // Status is uninitialized if we couldn't determine the status at compile time, for example, |
| 3333 | // not loading the class. |
| 3334 | // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy |
| 3335 | // isn't a problem and this case shouldn't occur |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3336 | return false; |
| 3337 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3338 | std::string temp; |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 3339 | LOG(FATAL) << "Unexpected class status: " << oat_file_class_status |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3340 | << " " << dex_file.GetLocation() << " " << PrettyClass(klass) << " " |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3341 | << klass->GetDescriptor(&temp); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 3342 | UNREACHABLE(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3343 | } |
| 3344 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3345 | void ClassLinker::ResolveClassExceptionHandlerTypes(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3346 | Handle<mirror::Class> klass) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3347 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3348 | ResolveMethodExceptionHandlerTypes(dex_file, klass->GetDirectMethod(i, image_pointer_size_)); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3349 | } |
| 3350 | for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3351 | ResolveMethodExceptionHandlerTypes(dex_file, klass->GetVirtualMethod(i, image_pointer_size_)); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3352 | } |
| 3353 | } |
| 3354 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3355 | void ClassLinker::ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3356 | ArtMethod* method) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3357 | // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod. |
| 3358 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3359 | if (code_item == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3360 | return; // native or abstract method |
| 3361 | } |
| 3362 | if (code_item->tries_size_ == 0) { |
| 3363 | return; // nothing to process |
| 3364 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 3365 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item, 0); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3366 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3367 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| 3368 | CatchHandlerIterator iterator(handlers_ptr); |
| 3369 | for (; iterator.HasNext(); iterator.Next()) { |
| 3370 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 3371 | // unresolved exception types will be ignored by exception delivery |
| 3372 | if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) { |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 3373 | mirror::Class* exception_type = ResolveType(iterator.GetHandlerTypeIndex(), method); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3374 | if (exception_type == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3375 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 3376 | Thread::Current()->ClearException(); |
| 3377 | } |
| 3378 | } |
| 3379 | } |
| 3380 | handlers_ptr = iterator.EndDataPointer(); |
| 3381 | } |
| 3382 | } |
| 3383 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3384 | mirror::Class* ClassLinker::CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, |
| 3385 | jstring name, |
| 3386 | jobjectArray interfaces, |
| 3387 | jobject loader, |
| 3388 | jobjectArray methods, |
| 3389 | jobjectArray throws) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3390 | Thread* self = soa.Self(); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 3391 | StackHandleScope<10> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3392 | MutableHandle<mirror::Class> klass(hs.NewHandle( |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3393 | AllocClass(self, GetClassRoot(kJavaLangClass), sizeof(mirror::Class)))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3394 | if (klass.Get() == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3395 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3396 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3397 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3398 | DCHECK(klass->GetClass() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3399 | klass->SetObjectSize(sizeof(mirror::Proxy)); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 3400 | // Set the class access flags incl. preverified, so we do not try to set the flag on the methods. |
| 3401 | klass->SetAccessFlags(kAccClassIsProxy | kAccPublic | kAccFinal | kAccPreverified); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3402 | klass->SetClassLoader(soa.Decode<mirror::ClassLoader*>(loader)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3403 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3404 | klass->SetName(soa.Decode<mirror::String*>(name)); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 3405 | klass->SetDexCache(GetClassRoot(kJavaLangReflectProxy)->GetDexCache()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3406 | mirror::Class::SetStatus(klass, mirror::Class::kStatusIdx, self); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 3407 | std::string descriptor(GetDescriptorForProxy(klass.Get())); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 3408 | const size_t hash = ComputeModifiedUtf8Hash(descriptor.c_str()); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 3409 | |
| 3410 | // Insert the class before loading the fields as the field roots |
| 3411 | // (ArtField::declaring_class_) are only visited from the class |
| 3412 | // table. There can't be any suspend points between inserting the |
| 3413 | // class and setting the field arrays below. |
| 3414 | mirror::Class* existing = InsertClass(descriptor.c_str(), klass.Get(), hash); |
| 3415 | CHECK(existing == nullptr); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3416 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3417 | // Instance fields are inherited, but we add a couple of static fields... |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3418 | const size_t num_fields = 2; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3419 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, num_fields); |
| 3420 | klass->SetSFieldsPtr(sfields); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3421 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3422 | // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by |
| 3423 | // our proxy, so Class.getInterfaces doesn't return the flattened set. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3424 | ArtField& interfaces_sfield = sfields->At(0); |
| 3425 | interfaces_sfield.SetDexFieldIndex(0); |
| 3426 | interfaces_sfield.SetDeclaringClass(klass.Get()); |
| 3427 | interfaces_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3428 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3429 | // 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] | 3430 | ArtField& throws_sfield = sfields->At(1); |
| 3431 | throws_sfield.SetDexFieldIndex(1); |
| 3432 | throws_sfield.SetDeclaringClass(klass.Get()); |
| 3433 | throws_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3434 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3435 | // Proxies have 1 direct method, the constructor |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3436 | LengthPrefixedArray<ArtMethod>* directs = AllocArtMethodArray(self, 1); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3437 | // Currently AllocArtMethodArray cannot return null, but the OOM logic is left there in case we |
| 3438 | // want to throw OOM in the future. |
| 3439 | if (UNLIKELY(directs == nullptr)) { |
| 3440 | self->AssertPendingOOMException(); |
| 3441 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3442 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3443 | klass->SetDirectMethodsPtr(directs); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3444 | CreateProxyConstructor(klass, klass->GetDirectMethodUnchecked(0, image_pointer_size_)); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3445 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3446 | // Create virtual method using specified prototypes. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 3447 | auto h_methods = hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Method>*>(methods)); |
| 3448 | DCHECK_EQ(h_methods->GetClass(), mirror::Method::ArrayClass()) |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3449 | << PrettyClass(h_methods->GetClass()); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 3450 | const size_t num_virtual_methods = h_methods->GetLength(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3451 | auto* virtuals = AllocArtMethodArray(self, num_virtual_methods); |
| 3452 | // Currently AllocArtMethodArray cannot return null, but the OOM logic is left there in case we |
| 3453 | // want to throw OOM in the future. |
| 3454 | if (UNLIKELY(virtuals == nullptr)) { |
| 3455 | self->AssertPendingOOMException(); |
| 3456 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3457 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3458 | klass->SetVirtualMethodsPtr(virtuals); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3459 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3460 | auto* virtual_method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
| 3461 | auto* prototype = h_methods->Get(i)->GetArtMethod(); |
| 3462 | CreateProxyMethod(klass, prototype, virtual_method); |
| 3463 | DCHECK(virtual_method->GetDeclaringClass() != nullptr); |
| 3464 | DCHECK(prototype->GetDeclaringClass() != nullptr); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3465 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3466 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 3467 | // The super class is java.lang.reflect.Proxy |
| 3468 | klass->SetSuperClass(GetClassRoot(kJavaLangReflectProxy)); |
| 3469 | // Now effectively in the loaded state. |
| 3470 | mirror::Class::SetStatus(klass, mirror::Class::kStatusLoaded, self); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3471 | self->AssertNoPendingException(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3472 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 3473 | MutableHandle<mirror::Class> new_class = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 3474 | { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3475 | // Must hold lock on object when resolved. |
| 3476 | ObjectLock<mirror::Class> resolution_lock(self, klass); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 3477 | // Link the fields and virtual methods, creating vtable and iftables. |
| 3478 | // The new class will replace the old one in the class table. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3479 | Handle<mirror::ObjectArray<mirror::Class>> h_interfaces( |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3480 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces))); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 3481 | if (!LinkClass(self, descriptor.c_str(), klass, h_interfaces, &new_class)) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3482 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3483 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3484 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3485 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3486 | CHECK(klass->IsRetired()); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 3487 | CHECK_NE(klass.Get(), new_class.Get()); |
| 3488 | klass.Assign(new_class.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3489 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3490 | CHECK_EQ(interfaces_sfield.GetDeclaringClass(), klass.Get()); |
| 3491 | interfaces_sfield.SetObject<false>(klass.Get(), |
| 3492 | soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces)); |
| 3493 | CHECK_EQ(throws_sfield.GetDeclaringClass(), klass.Get()); |
| 3494 | throws_sfield.SetObject<false>( |
| 3495 | klass.Get(), soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class> >*>(throws)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3496 | |
| 3497 | { |
| 3498 | // Lock on klass is released. Lock new class object. |
| 3499 | ObjectLock<mirror::Class> initialization_lock(self, klass); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3500 | mirror::Class::SetStatus(klass, mirror::Class::kStatusInitialized, self); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 3501 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3502 | |
| 3503 | // sanity checks |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 3504 | if (kIsDebugBuild) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3505 | CHECK(klass->GetIFieldsPtr() == nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3506 | CheckProxyConstructor(klass->GetDirectMethod(0, image_pointer_size_)); |
| 3507 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3508 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3509 | auto* virtual_method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
| 3510 | auto* prototype = h_methods->Get(i++)->GetArtMethod(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3511 | CheckProxyMethod(virtual_method, prototype); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3512 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3513 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 3514 | StackHandleScope<1> hs2(self); |
| 3515 | Handle<mirror::String> decoded_name = hs2.NewHandle(soa.Decode<mirror::String*>(name)); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3516 | std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3517 | decoded_name->ToModifiedUtf8().c_str())); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3518 | CHECK_EQ(PrettyField(klass->GetStaticField(0)), interfaces_field_name); |
| 3519 | |
| 3520 | std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3521 | decoded_name->ToModifiedUtf8().c_str())); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3522 | CHECK_EQ(PrettyField(klass->GetStaticField(1)), throws_field_name); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3523 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3524 | CHECK_EQ(klass.Get()->GetInterfaces(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3525 | soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3526 | CHECK_EQ(klass.Get()->GetThrows(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3527 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>*>(throws)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3528 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3529 | return klass.Get(); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3530 | } |
| 3531 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3532 | std::string ClassLinker::GetDescriptorForProxy(mirror::Class* proxy_class) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3533 | DCHECK(proxy_class->IsProxyClass()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3534 | mirror::String* name = proxy_class->GetName(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3535 | DCHECK(name != nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3536 | return DotToDescriptor(name->ToModifiedUtf8().c_str()); |
| 3537 | } |
| 3538 | |
Mathieu Chartier | 4e2cb09 | 2015-07-22 16:17:51 -0700 | [diff] [blame] | 3539 | ArtMethod* ClassLinker::FindMethodForProxy(mirror::Class* proxy_class, ArtMethod* proxy_method) { |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3540 | DCHECK(proxy_class->IsProxyClass()); |
| 3541 | DCHECK(proxy_method->IsProxyMethod()); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3542 | { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3543 | Thread* const self = Thread::Current(); |
| 3544 | ReaderMutexLock mu(self, dex_lock_); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 3545 | // Locate the dex cache of the original interface/Object |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3546 | for (jobject weak_root : dex_caches_) { |
| 3547 | mirror::DexCache* dex_cache = down_cast<mirror::DexCache*>(self->DecodeJObject(weak_root)); |
| 3548 | if (dex_cache != nullptr && |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 3549 | proxy_method->HasSameDexCacheResolvedTypes(dex_cache->GetResolvedTypes(), |
| 3550 | image_pointer_size_)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3551 | ArtMethod* resolved_method = dex_cache->GetResolvedMethod( |
| 3552 | proxy_method->GetDexMethodIndex(), image_pointer_size_); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 3553 | CHECK(resolved_method != nullptr); |
| 3554 | return resolved_method; |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3555 | } |
| 3556 | } |
| 3557 | } |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 3558 | LOG(FATAL) << "Didn't find dex cache for " << PrettyClass(proxy_class) << " " |
| 3559 | << PrettyMethod(proxy_method); |
| 3560 | UNREACHABLE(); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3561 | } |
| 3562 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3563 | void ClassLinker::CreateProxyConstructor(Handle<mirror::Class> klass, ArtMethod* out) { |
| 3564 | // Create constructor for Proxy that must initialize the method. |
| 3565 | CHECK_EQ(GetClassRoot(kJavaLangReflectProxy)->NumDirectMethods(), 16u); |
| 3566 | ArtMethod* proxy_constructor = GetClassRoot(kJavaLangReflectProxy)->GetDirectMethodUnchecked( |
| 3567 | 2, image_pointer_size_); |
Sebastien Hertz | ae94e35 | 2014-08-27 15:32:56 +0200 | [diff] [blame] | 3568 | // Ensure constructor is in dex cache so that we can use the dex cache to look up the overridden |
| 3569 | // constructor method. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 3570 | GetClassRoot(kJavaLangReflectProxy)->GetDexCache()->SetResolvedMethod( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3571 | proxy_constructor->GetDexMethodIndex(), proxy_constructor, image_pointer_size_); |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 3572 | // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its |
| 3573 | // code_ too) |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3574 | DCHECK(out != nullptr); |
| 3575 | out->CopyFrom(proxy_constructor, image_pointer_size_); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3576 | // Make this constructor public and fix the class to be our Proxy version |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3577 | out->SetAccessFlags((out->GetAccessFlags() & ~kAccProtected) | kAccPublic); |
| 3578 | out->SetDeclaringClass(klass.Get()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3579 | } |
| 3580 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3581 | void ClassLinker::CheckProxyConstructor(ArtMethod* constructor) const { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3582 | CHECK(constructor->IsConstructor()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3583 | auto* np = constructor->GetInterfaceMethodIfProxy(image_pointer_size_); |
| 3584 | CHECK_STREQ(np->GetName(), "<init>"); |
| 3585 | CHECK_STREQ(np->GetSignature().ToString().c_str(), "(Ljava/lang/reflect/InvocationHandler;)V"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3586 | DCHECK(constructor->IsPublic()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3587 | } |
| 3588 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 3589 | void ClassLinker::CreateProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3590 | ArtMethod* out) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3591 | // Ensure prototype is in dex cache so that we can use the dex cache to look up the overridden |
| 3592 | // prototype method |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 3593 | auto* dex_cache = prototype->GetDeclaringClass()->GetDexCache(); |
| 3594 | // Avoid dirtying the dex cache unless we need to. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3595 | if (dex_cache->GetResolvedMethod(prototype->GetDexMethodIndex(), image_pointer_size_) != |
| 3596 | prototype) { |
| 3597 | dex_cache->SetResolvedMethod( |
| 3598 | prototype->GetDexMethodIndex(), prototype, image_pointer_size_); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 3599 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3600 | // 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] | 3601 | // as necessary |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3602 | DCHECK(out != nullptr); |
| 3603 | out->CopyFrom(prototype, image_pointer_size_); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3604 | |
| 3605 | // Set class to be the concrete proxy class and clear the abstract flag, modify exceptions to |
| 3606 | // the intersection of throw exceptions as defined in Proxy |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3607 | out->SetDeclaringClass(klass.Get()); |
| 3608 | out->SetAccessFlags((out->GetAccessFlags() & ~kAccAbstract) | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3609 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3610 | // At runtime the method looks like a reference and argument saving method, clone the code |
| 3611 | // related parameters from this method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3612 | out->SetEntryPointFromQuickCompiledCode(GetQuickProxyInvokeHandler()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3613 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3614 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3615 | void ClassLinker::CheckProxyMethod(ArtMethod* method, ArtMethod* prototype) const { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3616 | // Basic sanity |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3617 | CHECK(!prototype->IsFinal()); |
| 3618 | CHECK(method->IsFinal()); |
| 3619 | CHECK(!method->IsAbstract()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3620 | |
| 3621 | // The proxy method doesn't have its own dex cache or dex file and so it steals those of its |
| 3622 | // interface prototype. The exception to this are Constructors and the Class of the Proxy itself. |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 3623 | CHECK(prototype->HasSameDexCacheResolvedMethods(method, image_pointer_size_)); |
| 3624 | CHECK(prototype->HasSameDexCacheResolvedTypes(method, image_pointer_size_)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3625 | auto* np = method->GetInterfaceMethodIfProxy(image_pointer_size_); |
| 3626 | CHECK_EQ(prototype->GetDeclaringClass()->GetDexCache(), np->GetDexCache()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3627 | CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex()); |
| 3628 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3629 | CHECK_STREQ(np->GetName(), prototype->GetName()); |
| 3630 | CHECK_STREQ(np->GetShorty(), prototype->GetShorty()); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3631 | // More complex sanity - via dex cache |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 3632 | CHECK_EQ(np->GetReturnType(true /* resolve */, image_pointer_size_), |
| 3633 | prototype->GetReturnType(true /* resolve */, image_pointer_size_)); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3634 | } |
| 3635 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 3636 | bool ClassLinker::CanWeInitializeClass(mirror::Class* klass, bool can_init_statics, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3637 | bool can_init_parents) { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 3638 | if (can_init_statics && can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3639 | return true; |
| 3640 | } |
| 3641 | if (!can_init_statics) { |
| 3642 | // Check if there's a class initializer. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3643 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3644 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3645 | return false; |
| 3646 | } |
| 3647 | // Check if there are encoded static values needing initialization. |
| 3648 | if (klass->NumStaticFields() != 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 3649 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3650 | DCHECK(dex_class_def != nullptr); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3651 | if (dex_class_def->static_values_off_ != 0) { |
| 3652 | return false; |
| 3653 | } |
| 3654 | } |
| 3655 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3656 | if (klass->IsInterface() || !klass->HasSuperClass()) { |
| 3657 | return true; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3658 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3659 | mirror::Class* super_class = klass->GetSuperClass(); |
| 3660 | if (!can_init_parents && !super_class->IsInitialized()) { |
| 3661 | return false; |
| 3662 | } |
| 3663 | return CanWeInitializeClass(super_class, can_init_statics, can_init_parents); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3664 | } |
| 3665 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 3666 | bool ClassLinker::InitializeClass(Thread* self, Handle<mirror::Class> klass, |
| 3667 | bool can_init_statics, bool can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3668 | // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol |
| 3669 | |
| 3670 | // Are we already initialized and therefore done? |
| 3671 | // Note: we differ from the JLS here as we don't do this under the lock, this is benign as |
| 3672 | // an initialized class will never change its state. |
| 3673 | if (klass->IsInitialized()) { |
| 3674 | return true; |
| 3675 | } |
| 3676 | |
| 3677 | // 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] | 3678 | if (!CanWeInitializeClass(klass.Get(), can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3679 | return false; |
| 3680 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3681 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3682 | self->AllowThreadSuspension(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3683 | uint64_t t0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3684 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3685 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3686 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3687 | // Re-check under the lock in case another thread initialized ahead of us. |
| 3688 | if (klass->IsInitialized()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3689 | return true; |
| 3690 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3691 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3692 | // 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] | 3693 | if (klass->IsErroneous()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3694 | ThrowEarlierClassFailure(klass.Get()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 3695 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3696 | return false; |
| 3697 | } |
| 3698 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3699 | CHECK(klass->IsResolved()) << PrettyClass(klass.Get()) << ": state=" << klass->GetStatus(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3700 | |
| 3701 | if (!klass->IsVerified()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3702 | VerifyClass(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3703 | if (!klass->IsVerified()) { |
| 3704 | // We failed to verify, expect either the klass to be erroneous or verification failed at |
| 3705 | // compile time. |
| 3706 | if (klass->IsErroneous()) { |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 3707 | CHECK(self->IsExceptionPending()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 3708 | VlogClassInitializationFailure(klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3709 | } else { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3710 | CHECK(Runtime::Current()->IsAotCompiler()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3711 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 3712 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3713 | return false; |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 3714 | } else { |
| 3715 | self->AssertNoPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3716 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3717 | } |
| 3718 | |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3719 | // If the class is kStatusInitializing, either this thread is |
| 3720 | // initializing higher up the stack or another thread has beat us |
| 3721 | // to initializing and we need to wait. Either way, this |
| 3722 | // invocation of InitializeClass will not be responsible for |
| 3723 | // running <clinit> and will return. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3724 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 3725 | // Could have got an exception during verification. |
| 3726 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 3727 | VlogClassInitializationFailure(klass); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 3728 | return false; |
| 3729 | } |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 3730 | // We caught somebody else in the act; was it us? |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 3731 | if (klass->GetClinitThreadId() == self->GetTid()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3732 | // Yes. That's fine. Return so we can continue initializing. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3733 | return true; |
| 3734 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3735 | // No. That's fine. Wait for another thread to finish initializing. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 3736 | return WaitForInitializeClass(klass, self, lock); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | if (!ValidateSuperClassDescriptors(klass)) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3740 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3741 | return false; |
| 3742 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3743 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3744 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3745 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusVerified) << PrettyClass(klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3746 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3747 | // From here out other threads may observe that we're initializing and so changes of state |
| 3748 | // require the a notification. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 3749 | klass->SetClinitThreadId(self->GetTid()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3750 | mirror::Class::SetStatus(klass, mirror::Class::kStatusInitializing, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3751 | |
| 3752 | t0 = NanoTime(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3753 | } |
| 3754 | |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 3755 | // Initialize super classes, must be done while initializing for the JLS. |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3756 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
| 3757 | mirror::Class* super_class = klass->GetSuperClass(); |
| 3758 | if (!super_class->IsInitialized()) { |
| 3759 | CHECK(!super_class->IsInterface()); |
| 3760 | CHECK(can_init_parents); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3761 | StackHandleScope<1> hs(self); |
| 3762 | Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3763 | bool super_initialized = InitializeClass(self, handle_scope_super, can_init_statics, true); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3764 | if (!super_initialized) { |
| 3765 | // The super class was verified ahead of entering initializing, we should only be here if |
| 3766 | // the super class became erroneous due to initialization. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3767 | CHECK(handle_scope_super->IsErroneous() && self->IsExceptionPending()) |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3768 | << "Super class initialization failed for " |
| 3769 | << PrettyDescriptor(handle_scope_super.Get()) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3770 | << " that has unexpected status " << handle_scope_super->GetStatus() |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3771 | << "\nPending exception:\n" |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3772 | << (self->GetException() != nullptr ? self->GetException()->Dump() : ""); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3773 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3774 | // Initialization failed because the super-class is erroneous. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3775 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3776 | return false; |
| 3777 | } |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 3778 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3779 | } |
| 3780 | |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 3781 | const size_t num_static_fields = klass->NumStaticFields(); |
| 3782 | if (num_static_fields > 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 3783 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3784 | CHECK(dex_class_def != nullptr); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 3785 | const DexFile& dex_file = klass->GetDexFile(); |
Hiroshi Yamauchi | 67ef46a | 2014-08-21 15:59:43 -0700 | [diff] [blame] | 3786 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3787 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 3788 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 3789 | |
| 3790 | // Eagerly fill in static fields so that the we don't have to do as many expensive |
| 3791 | // Class::FindStaticField in ResolveField. |
| 3792 | for (size_t i = 0; i < num_static_fields; ++i) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3793 | ArtField* field = klass->GetStaticField(i); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 3794 | const uint32_t field_idx = field->GetDexFieldIndex(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3795 | ArtField* resolved_field = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 3796 | if (resolved_field == nullptr) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3797 | dex_cache->SetResolvedField(field_idx, field, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 3798 | } else { |
| 3799 | DCHECK_EQ(field, resolved_field); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 3800 | } |
| 3801 | } |
| 3802 | |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 3803 | EncodedStaticFieldValueIterator value_it(dex_file, &dex_cache, &class_loader, |
| 3804 | this, *dex_class_def); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 3805 | const uint8_t* class_data = dex_file.GetClassData(*dex_class_def); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 3806 | ClassDataItemIterator field_it(dex_file, class_data); |
| 3807 | if (value_it.HasNext()) { |
| 3808 | DCHECK(field_it.HasNextStaticField()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3809 | CHECK(can_init_statics); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 3810 | for ( ; value_it.HasNext(); value_it.Next(), field_it.Next()) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3811 | ArtField* field = ResolveField( |
| 3812 | dex_file, field_it.GetMemberIndex(), dex_cache, class_loader, true); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 3813 | if (Runtime::Current()->IsActiveTransaction()) { |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 3814 | value_it.ReadValueToField<true>(field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 3815 | } else { |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 3816 | value_it.ReadValueToField<false>(field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 3817 | } |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 3818 | DCHECK(!value_it.HasNext() || field_it.HasNextStaticField()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3819 | } |
| 3820 | } |
| 3821 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3822 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3823 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3824 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3825 | CHECK(can_init_statics); |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 3826 | JValue result; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3827 | clinit->Invoke(self, nullptr, 0, &result, "V"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3828 | } |
| 3829 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3830 | self->AllowThreadSuspension(); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 3831 | uint64_t t1 = NanoTime(); |
| 3832 | |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 3833 | bool success = true; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3834 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3835 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3836 | |
| 3837 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 3838 | WrapExceptionInInitializer(klass); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3839 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 3840 | success = false; |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 3841 | } else if (Runtime::Current()->IsTransactionAborted()) { |
| 3842 | // The exception thrown when the transaction aborted has been caught and cleared |
| 3843 | // so we need to throw it again now. |
Sebastien Hertz | bd9cf9f | 2015-03-03 12:16:13 +0100 | [diff] [blame] | 3844 | VLOG(compiler) << "Return from class initializer of " << PrettyDescriptor(klass.Get()) |
| 3845 | << " without exception while transaction was aborted: re-throw it now."; |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 3846 | Runtime::Current()->ThrowTransactionAbortError(self); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3847 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 3848 | success = false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3849 | } else { |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 3850 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 3851 | RuntimeStats* thread_stats = self->GetStats(); |
| 3852 | ++global_stats->class_init_count; |
| 3853 | ++thread_stats->class_init_count; |
| 3854 | global_stats->class_init_time_ns += (t1 - t0); |
| 3855 | thread_stats->class_init_time_ns += (t1 - t0); |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 3856 | // Set the class as initialized except if failed to initialize static fields. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3857 | mirror::Class::SetStatus(klass, mirror::Class::kStatusInitialized, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3858 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3859 | std::string temp; |
| 3860 | LOG(INFO) << "Initialized class " << klass->GetDescriptor(&temp) << " from " << |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 3861 | klass->GetLocation(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3862 | } |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 3863 | // Opportunistically set static method trampolines to their destination. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3864 | FixupStaticTrampolines(klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3865 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3866 | } |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 3867 | return success; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3868 | } |
| 3869 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3870 | bool ClassLinker::WaitForInitializeClass(Handle<mirror::Class> klass, |
| 3871 | Thread* self, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 3872 | ObjectLock<mirror::Class>& lock) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 3873 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3874 | while (true) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3875 | self->AssertNoPendingException(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3876 | CHECK(!klass->IsInitialized()); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 3877 | lock.WaitIgnoringInterrupts(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3878 | |
| 3879 | // When we wake up, repeat the test for init-in-progress. If |
| 3880 | // there's an exception pending (only possible if |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 3881 | // we were not using WaitIgnoringInterrupts), bail out. |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3882 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 3883 | WrapExceptionInInitializer(klass); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3884 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3885 | return false; |
| 3886 | } |
| 3887 | // Spurious wakeup? Go back to waiting. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3888 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3889 | continue; |
| 3890 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3891 | if (klass->GetStatus() == mirror::Class::kStatusVerified && |
| 3892 | Runtime::Current()->IsAotCompiler()) { |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 3893 | // Compile time initialization failed. |
| 3894 | return false; |
| 3895 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3896 | if (klass->IsErroneous()) { |
| 3897 | // The caller wants an exception, but it was thrown in a |
| 3898 | // different thread. Synthesize one here. |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 3899 | ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread", |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3900 | PrettyDescriptor(klass.Get()).c_str()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 3901 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3902 | return false; |
| 3903 | } |
| 3904 | if (klass->IsInitialized()) { |
| 3905 | return true; |
| 3906 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3907 | LOG(FATAL) << "Unexpected class status. " << PrettyClass(klass.Get()) << " is " |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3908 | << klass->GetStatus(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3909 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 3910 | UNREACHABLE(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3911 | } |
| 3912 | |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 3913 | static void ThrowSignatureCheckResolveReturnTypeException(Handle<mirror::Class> klass, |
| 3914 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3915 | ArtMethod* method, |
| 3916 | ArtMethod* m) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 3917 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 3918 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 3919 | DCHECK(!m->IsProxyMethod()); |
| 3920 | const DexFile* dex_file = m->GetDexFile(); |
| 3921 | const DexFile::MethodId& method_id = dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 3922 | const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id); |
| 3923 | uint16_t return_type_idx = proto_id.return_type_idx_; |
| 3924 | std::string return_type = PrettyType(return_type_idx, *dex_file); |
| 3925 | std::string class_loader = PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
| 3926 | ThrowWrappedLinkageError(klass.Get(), |
| 3927 | "While checking class %s method %s signature against %s %s: " |
| 3928 | "Failed to resolve return type %s with %s", |
| 3929 | PrettyDescriptor(klass.Get()).c_str(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3930 | PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 3931 | super_klass->IsInterface() ? "interface" : "superclass", |
| 3932 | PrettyDescriptor(super_klass.Get()).c_str(), |
| 3933 | return_type.c_str(), class_loader.c_str()); |
| 3934 | } |
| 3935 | |
| 3936 | static void ThrowSignatureCheckResolveArgException(Handle<mirror::Class> klass, |
| 3937 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3938 | ArtMethod* method, |
| 3939 | ArtMethod* m, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3940 | uint32_t index, |
| 3941 | uint32_t arg_type_idx) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 3942 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 3943 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 3944 | DCHECK(!m->IsProxyMethod()); |
| 3945 | const DexFile* dex_file = m->GetDexFile(); |
| 3946 | std::string arg_type = PrettyType(arg_type_idx, *dex_file); |
| 3947 | std::string class_loader = PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
| 3948 | ThrowWrappedLinkageError(klass.Get(), |
| 3949 | "While checking class %s method %s signature against %s %s: " |
| 3950 | "Failed to resolve arg %u type %s with %s", |
| 3951 | PrettyDescriptor(klass.Get()).c_str(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3952 | PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 3953 | super_klass->IsInterface() ? "interface" : "superclass", |
| 3954 | PrettyDescriptor(super_klass.Get()).c_str(), |
| 3955 | index, arg_type.c_str(), class_loader.c_str()); |
| 3956 | } |
| 3957 | |
| 3958 | static void ThrowSignatureMismatch(Handle<mirror::Class> klass, |
| 3959 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3960 | ArtMethod* method, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 3961 | const std::string& error_msg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 3962 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 3963 | ThrowLinkageError(klass.Get(), |
| 3964 | "Class %s method %s resolves differently in %s %s: %s", |
| 3965 | PrettyDescriptor(klass.Get()).c_str(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3966 | PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 3967 | super_klass->IsInterface() ? "interface" : "superclass", |
| 3968 | PrettyDescriptor(super_klass.Get()).c_str(), |
| 3969 | error_msg.c_str()); |
| 3970 | } |
| 3971 | |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 3972 | static bool HasSameSignatureWithDifferentClassLoaders(Thread* self, |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 3973 | size_t pointer_size, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 3974 | Handle<mirror::Class> klass, |
| 3975 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3976 | ArtMethod* method1, |
| 3977 | ArtMethod* method2) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 3978 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 3979 | { |
| 3980 | StackHandleScope<1> hs(self); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 3981 | Handle<mirror::Class> return_type(hs.NewHandle(method1->GetReturnType(true /* resolve */, |
| 3982 | pointer_size))); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 3983 | if (UNLIKELY(return_type.Get() == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3984 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method1); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 3985 | return false; |
| 3986 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 3987 | mirror::Class* other_return_type = method2->GetReturnType(true /* resolve */, |
| 3988 | pointer_size); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 3989 | if (UNLIKELY(other_return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3990 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method2); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 3991 | return false; |
| 3992 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 3993 | if (UNLIKELY(other_return_type != return_type.Get())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 3994 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 3995 | StringPrintf("Return types mismatch: %s(%p) vs %s(%p)", |
| 3996 | PrettyClassAndClassLoader(return_type.Get()).c_str(), |
| 3997 | return_type.Get(), |
| 3998 | PrettyClassAndClassLoader(other_return_type).c_str(), |
| 3999 | other_return_type)); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4000 | return false; |
| 4001 | } |
| 4002 | } |
| 4003 | const DexFile::TypeList* types1 = method1->GetParameterTypeList(); |
| 4004 | const DexFile::TypeList* types2 = method2->GetParameterTypeList(); |
| 4005 | if (types1 == nullptr) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 4006 | if (types2 != nullptr && types2->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4007 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 4008 | StringPrintf("Type list mismatch with %s", |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4009 | PrettyMethod(method2, true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 4010 | return false; |
| 4011 | } |
| 4012 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4013 | } else if (UNLIKELY(types2 == nullptr)) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 4014 | if (types1->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4015 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 4016 | StringPrintf("Type list mismatch with %s", |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4017 | PrettyMethod(method2, true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 4018 | return false; |
| 4019 | } |
| 4020 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4021 | } |
| 4022 | uint32_t num_types = types1->Size(); |
| 4023 | if (UNLIKELY(num_types != types2->Size())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4024 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 4025 | StringPrintf("Type list mismatch with %s", |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4026 | PrettyMethod(method2, true).c_str())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4027 | return false; |
| 4028 | } |
| 4029 | for (uint32_t i = 0; i < num_types; ++i) { |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 4030 | StackHandleScope<1> hs(self); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4031 | uint32_t param_type_idx = types1->GetTypeItem(i).type_idx_; |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 4032 | Handle<mirror::Class> param_type(hs.NewHandle( |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 4033 | method1->GetClassFromTypeIndex(param_type_idx, true /* resolve */, pointer_size))); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4034 | if (UNLIKELY(param_type.Get() == nullptr)) { |
| 4035 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4036 | method1, i, param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4037 | return false; |
| 4038 | } |
| 4039 | uint32_t other_param_type_idx = types2->GetTypeItem(i).type_idx_; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4040 | mirror::Class* other_param_type = |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 4041 | method2->GetClassFromTypeIndex(other_param_type_idx, true /* resolve */, pointer_size); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4042 | if (UNLIKELY(other_param_type == nullptr)) { |
| 4043 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4044 | method2, i, other_param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4045 | return false; |
| 4046 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 4047 | if (UNLIKELY(param_type.Get() != other_param_type)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4048 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 4049 | StringPrintf("Parameter %u type mismatch: %s(%p) vs %s(%p)", |
| 4050 | i, |
| 4051 | PrettyClassAndClassLoader(param_type.Get()).c_str(), |
| 4052 | param_type.Get(), |
| 4053 | PrettyClassAndClassLoader(other_param_type).c_str(), |
| 4054 | other_param_type)); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4055 | return false; |
| 4056 | } |
| 4057 | } |
| 4058 | return true; |
| 4059 | } |
| 4060 | |
| 4061 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4062 | bool ClassLinker::ValidateSuperClassDescriptors(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4063 | if (klass->IsInterface()) { |
| 4064 | return true; |
| 4065 | } |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 4066 | // Begin with the methods local to the superclass. |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 4067 | Thread* self = Thread::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4068 | StackHandleScope<1> hs(self); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4069 | MutableHandle<mirror::Class> super_klass(hs.NewHandle<mirror::Class>(nullptr)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4070 | if (klass->HasSuperClass() && |
| 4071 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4072 | super_klass.Assign(klass->GetSuperClass()); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4073 | for (int i = klass->GetSuperClass()->GetVTableLength() - 1; i >= 0; --i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4074 | auto* m = klass->GetVTableEntry(i, image_pointer_size_); |
| 4075 | auto* super_m = klass->GetSuperClass()->GetVTableEntry(i, image_pointer_size_); |
| 4076 | if (m != super_m) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 4077 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, image_pointer_size_, |
| 4078 | klass, super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4079 | m, super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4080 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 4081 | return false; |
| 4082 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4083 | } |
| 4084 | } |
| 4085 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 4086 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4087 | super_klass.Assign(klass->GetIfTable()->GetInterface(i)); |
| 4088 | if (klass->GetClassLoader() != super_klass->GetClassLoader()) { |
| 4089 | uint32_t num_methods = super_klass->NumVirtualMethods(); |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 4090 | for (uint32_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4091 | auto* m = klass->GetIfTable()->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>( |
| 4092 | j, image_pointer_size_); |
| 4093 | auto* super_m = super_klass->GetVirtualMethod(j, image_pointer_size_); |
| 4094 | if (m != super_m) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 4095 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, image_pointer_size_, |
| 4096 | klass, super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4097 | m, super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4098 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 4099 | return false; |
| 4100 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4101 | } |
| 4102 | } |
| 4103 | } |
| 4104 | } |
| 4105 | return true; |
| 4106 | } |
| 4107 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4108 | bool ClassLinker::EnsureInitialized(Thread* self, Handle<mirror::Class> c, bool can_init_fields, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4109 | bool can_init_parents) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4110 | DCHECK(c.Get() != nullptr); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4111 | if (c->IsInitialized()) { |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4112 | EnsurePreverifiedMethods(c); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4113 | return true; |
| 4114 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4115 | const bool success = InitializeClass(self, c, can_init_fields, can_init_parents); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4116 | if (!success) { |
| 4117 | if (can_init_fields && can_init_parents) { |
| 4118 | CHECK(self->IsExceptionPending()) << PrettyClass(c.Get()); |
| 4119 | } |
| 4120 | } else { |
| 4121 | self->AssertNoPendingException(); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 4122 | } |
| 4123 | return success; |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 4124 | } |
| 4125 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4126 | void ClassLinker::FixupTemporaryDeclaringClass(mirror::Class* temp_class, |
| 4127 | mirror::Class* new_class) { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 4128 | DCHECK_EQ(temp_class->NumInstanceFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4129 | for (ArtField& field : new_class->GetIFields()) { |
| 4130 | if (field.GetDeclaringClass() == temp_class) { |
| 4131 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4132 | } |
| 4133 | } |
| 4134 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 4135 | DCHECK_EQ(temp_class->NumStaticFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4136 | for (ArtField& field : new_class->GetSFields()) { |
| 4137 | if (field.GetDeclaringClass() == temp_class) { |
| 4138 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4139 | } |
| 4140 | } |
| 4141 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 4142 | DCHECK_EQ(temp_class->NumDirectMethods(), 0u); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4143 | for (auto& method : new_class->GetDirectMethods(image_pointer_size_)) { |
| 4144 | if (method.GetDeclaringClass() == temp_class) { |
| 4145 | method.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4146 | } |
| 4147 | } |
| 4148 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 4149 | DCHECK_EQ(temp_class->NumVirtualMethods(), 0u); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4150 | for (auto& method : new_class->GetVirtualMethods(image_pointer_size_)) { |
| 4151 | if (method.GetDeclaringClass() == temp_class) { |
| 4152 | method.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4153 | } |
| 4154 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 4155 | |
| 4156 | // Make sure the remembered set and mod-union tables know that we updated some of the native |
| 4157 | // roots. |
| 4158 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4159 | } |
| 4160 | |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 4161 | ClassTable* ClassLinker::InsertClassTableForClassLoader(mirror::ClassLoader* class_loader) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4162 | if (class_loader == nullptr) { |
| 4163 | return &boot_class_table_; |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 4164 | } |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4165 | ClassTable* class_table = class_loader->GetClassTable(); |
| 4166 | if (class_table == nullptr) { |
| 4167 | class_table = new ClassTable; |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame^] | 4168 | Thread* const self = Thread::Current(); |
| 4169 | class_loaders_.push_back(self->GetJniEnv()->vm->AddWeakGlobalRef(self, class_loader)); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4170 | // Don't already have a class table, add it to the class loader. |
| 4171 | class_loader->SetClassTable(class_table); |
| 4172 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 4173 | return class_table; |
| 4174 | } |
| 4175 | |
| 4176 | ClassTable* ClassLinker::ClassTableForClassLoader(mirror::ClassLoader* class_loader) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4177 | return class_loader == nullptr ? &boot_class_table_ : class_loader->GetClassTable(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 4178 | } |
| 4179 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4180 | bool ClassLinker::LinkClass(Thread* self, |
| 4181 | const char* descriptor, |
| 4182 | Handle<mirror::Class> klass, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4183 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4184 | MutableHandle<mirror::Class>* h_new_class_out) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4185 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4186 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4187 | if (!LinkSuperClass(klass)) { |
| 4188 | return false; |
| 4189 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4190 | ArtMethod* imt[mirror::Class::kImtSize]; |
| 4191 | std::fill_n(imt, arraysize(imt), Runtime::Current()->GetImtUnimplementedMethod()); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4192 | if (!LinkMethods(self, klass, interfaces, imt)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4193 | return false; |
| 4194 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4195 | if (!LinkInstanceFields(self, klass)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4196 | return false; |
| 4197 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4198 | size_t class_size; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4199 | if (!LinkStaticFields(self, klass, &class_size)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 4200 | return false; |
| 4201 | } |
| 4202 | CreateReferenceInstanceOffsets(klass); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4203 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4204 | |
| 4205 | if (!klass->IsTemp() || (!init_done_ && klass->GetClassSize() == class_size)) { |
| 4206 | // We don't need to retire this class as it has no embedded tables or it was created the |
| 4207 | // correct size during class linker initialization. |
| 4208 | CHECK_EQ(klass->GetClassSize(), class_size) << PrettyDescriptor(klass.Get()); |
| 4209 | |
| 4210 | if (klass->ShouldHaveEmbeddedImtAndVTable()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4211 | klass->PopulateEmbeddedImtAndVTable(imt, image_pointer_size_); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4212 | } |
| 4213 | |
| 4214 | // This will notify waiters on klass that saw the not yet resolved |
| 4215 | // class in the class_table_ during EnsureResolved. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4216 | mirror::Class::SetStatus(klass, mirror::Class::kStatusResolved, self); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4217 | h_new_class_out->Assign(klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4218 | } else { |
| 4219 | CHECK(!klass->IsResolved()); |
| 4220 | // Retire the temporary class and create the correctly sized resolved class. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4221 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4222 | 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] | 4223 | // Set arrays to null since we don't want to have multiple classes with the same ArtField or |
| 4224 | // ArtMethod array pointers. If this occurs, it causes bugs in remembered sets since the GC |
| 4225 | // may not see any references to the target space and clean the card for a class if another |
| 4226 | // class had the same array pointer. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4227 | klass->SetDirectMethodsPtrUnchecked(nullptr); |
| 4228 | klass->SetVirtualMethodsPtr(nullptr); |
| 4229 | klass->SetSFieldsPtrUnchecked(nullptr); |
| 4230 | klass->SetIFieldsPtrUnchecked(nullptr); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4231 | if (UNLIKELY(h_new_class.Get() == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4232 | self->AssertPendingOOMException(); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4233 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4234 | return false; |
| 4235 | } |
| 4236 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4237 | CHECK_EQ(h_new_class->GetClassSize(), class_size); |
| 4238 | ObjectLock<mirror::Class> lock(self, h_new_class); |
| 4239 | FixupTemporaryDeclaringClass(klass.Get(), h_new_class.Get()); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 4240 | |
| 4241 | { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 4242 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 4243 | mirror::ClassLoader* const class_loader = h_new_class.Get()->GetClassLoader(); |
| 4244 | ClassTable* const table = InsertClassTableForClassLoader(class_loader); |
| 4245 | mirror::Class* existing = table->UpdateClass(descriptor, h_new_class.Get(), |
| 4246 | ComputeModifiedUtf8Hash(descriptor)); |
| 4247 | CHECK_EQ(existing, klass.Get()); |
| 4248 | if (kIsDebugBuild && class_loader == nullptr && dex_cache_image_class_lookup_required_) { |
| 4249 | // Check a class loaded with the system class loader matches one in the image if the class |
| 4250 | // is in the image. |
| 4251 | mirror::Class* const image_class = LookupClassFromImage(descriptor); |
| 4252 | if (image_class != nullptr) { |
| 4253 | CHECK_EQ(klass.Get(), existing) << descriptor; |
| 4254 | } |
| 4255 | } |
| 4256 | if (log_new_class_table_roots_) { |
| 4257 | new_class_roots_.push_back(GcRoot<mirror::Class>(h_new_class.Get())); |
| 4258 | } |
| 4259 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4260 | |
| 4261 | // This will notify waiters on temp class that saw the not yet resolved class in the |
| 4262 | // class_table_ during EnsureResolved. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4263 | mirror::Class::SetStatus(klass, mirror::Class::kStatusRetired, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4264 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4265 | CHECK_EQ(h_new_class->GetStatus(), mirror::Class::kStatusResolving); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4266 | // This will notify waiters on new_class that saw the not yet resolved |
| 4267 | // class in the class_table_ during EnsureResolved. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4268 | mirror::Class::SetStatus(h_new_class, mirror::Class::kStatusResolved, self); |
| 4269 | // Return the new class. |
| 4270 | h_new_class_out->Assign(h_new_class.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4271 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4272 | return true; |
| 4273 | } |
| 4274 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4275 | static void CountMethodsAndFields(ClassDataItemIterator& dex_data, |
| 4276 | size_t* virtual_methods, |
| 4277 | size_t* direct_methods, |
| 4278 | size_t* static_fields, |
| 4279 | size_t* instance_fields) { |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4280 | *virtual_methods = *direct_methods = *static_fields = *instance_fields = 0; |
| 4281 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4282 | while (dex_data.HasNextStaticField()) { |
| 4283 | dex_data.Next(); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4284 | (*static_fields)++; |
| 4285 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4286 | while (dex_data.HasNextInstanceField()) { |
| 4287 | dex_data.Next(); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4288 | (*instance_fields)++; |
| 4289 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4290 | while (dex_data.HasNextDirectMethod()) { |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4291 | (*direct_methods)++; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4292 | dex_data.Next(); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4293 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4294 | while (dex_data.HasNextVirtualMethod()) { |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4295 | (*virtual_methods)++; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4296 | dex_data.Next(); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4297 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4298 | DCHECK(!dex_data.HasNext()); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4299 | } |
| 4300 | |
| 4301 | static void DumpClass(std::ostream& os, |
| 4302 | const DexFile& dex_file, const DexFile::ClassDef& dex_class_def, |
| 4303 | const char* suffix) { |
| 4304 | ClassDataItemIterator dex_data(dex_file, dex_file.GetClassData(dex_class_def)); |
| 4305 | os << dex_file.GetClassDescriptor(dex_class_def) << suffix << ":\n"; |
| 4306 | os << " Static fields:\n"; |
| 4307 | while (dex_data.HasNextStaticField()) { |
| 4308 | const DexFile::FieldId& id = dex_file.GetFieldId(dex_data.GetMemberIndex()); |
| 4309 | os << " " << dex_file.GetFieldTypeDescriptor(id) << " " << dex_file.GetFieldName(id) << "\n"; |
| 4310 | dex_data.Next(); |
| 4311 | } |
| 4312 | os << " Instance fields:\n"; |
| 4313 | while (dex_data.HasNextInstanceField()) { |
| 4314 | const DexFile::FieldId& id = dex_file.GetFieldId(dex_data.GetMemberIndex()); |
| 4315 | os << " " << dex_file.GetFieldTypeDescriptor(id) << " " << dex_file.GetFieldName(id) << "\n"; |
| 4316 | dex_data.Next(); |
| 4317 | } |
| 4318 | os << " Direct methods:\n"; |
| 4319 | while (dex_data.HasNextDirectMethod()) { |
| 4320 | const DexFile::MethodId& id = dex_file.GetMethodId(dex_data.GetMemberIndex()); |
| 4321 | os << " " << dex_file.GetMethodName(id) << dex_file.GetMethodSignature(id).ToString() << "\n"; |
| 4322 | dex_data.Next(); |
| 4323 | } |
| 4324 | os << " Virtual methods:\n"; |
| 4325 | while (dex_data.HasNextVirtualMethod()) { |
| 4326 | const DexFile::MethodId& id = dex_file.GetMethodId(dex_data.GetMemberIndex()); |
| 4327 | os << " " << dex_file.GetMethodName(id) << dex_file.GetMethodSignature(id).ToString() << "\n"; |
| 4328 | dex_data.Next(); |
| 4329 | } |
| 4330 | } |
| 4331 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4332 | static std::string DumpClasses(const DexFile& dex_file1, |
| 4333 | const DexFile::ClassDef& dex_class_def1, |
| 4334 | const DexFile& dex_file2, |
| 4335 | const DexFile::ClassDef& dex_class_def2) { |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4336 | std::ostringstream os; |
| 4337 | DumpClass(os, dex_file1, dex_class_def1, " (Compile time)"); |
| 4338 | DumpClass(os, dex_file2, dex_class_def2, " (Runtime)"); |
| 4339 | return os.str(); |
| 4340 | } |
| 4341 | |
| 4342 | |
| 4343 | // Very simple structural check on whether the classes match. Only compares the number of |
| 4344 | // methods and fields. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4345 | static bool SimpleStructuralCheck(const DexFile& dex_file1, |
| 4346 | const DexFile::ClassDef& dex_class_def1, |
| 4347 | const DexFile& dex_file2, |
| 4348 | const DexFile::ClassDef& dex_class_def2, |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4349 | std::string* error_msg) { |
| 4350 | ClassDataItemIterator dex_data1(dex_file1, dex_file1.GetClassData(dex_class_def1)); |
| 4351 | ClassDataItemIterator dex_data2(dex_file2, dex_file2.GetClassData(dex_class_def2)); |
| 4352 | |
| 4353 | // Counters for current dex file. |
| 4354 | size_t dex_virtual_methods1, dex_direct_methods1, dex_static_fields1, dex_instance_fields1; |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4355 | CountMethodsAndFields(dex_data1, |
| 4356 | &dex_virtual_methods1, |
| 4357 | &dex_direct_methods1, |
| 4358 | &dex_static_fields1, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4359 | &dex_instance_fields1); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4360 | // Counters for compile-time dex file. |
| 4361 | size_t dex_virtual_methods2, dex_direct_methods2, dex_static_fields2, dex_instance_fields2; |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4362 | CountMethodsAndFields(dex_data2, |
| 4363 | &dex_virtual_methods2, |
| 4364 | &dex_direct_methods2, |
| 4365 | &dex_static_fields2, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4366 | &dex_instance_fields2); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4367 | |
| 4368 | if (dex_virtual_methods1 != dex_virtual_methods2) { |
| 4369 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4370 | *error_msg = StringPrintf("Virtual method count off: %zu vs %zu\n%s", |
| 4371 | dex_virtual_methods1, |
| 4372 | dex_virtual_methods2, |
| 4373 | class_dump.c_str()); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4374 | return false; |
| 4375 | } |
| 4376 | if (dex_direct_methods1 != dex_direct_methods2) { |
| 4377 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4378 | *error_msg = StringPrintf("Direct method count off: %zu vs %zu\n%s", |
| 4379 | dex_direct_methods1, |
| 4380 | dex_direct_methods2, |
| 4381 | class_dump.c_str()); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4382 | return false; |
| 4383 | } |
| 4384 | if (dex_static_fields1 != dex_static_fields2) { |
| 4385 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4386 | *error_msg = StringPrintf("Static field count off: %zu vs %zu\n%s", |
| 4387 | dex_static_fields1, |
| 4388 | dex_static_fields2, |
| 4389 | class_dump.c_str()); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4390 | return false; |
| 4391 | } |
| 4392 | if (dex_instance_fields1 != dex_instance_fields2) { |
| 4393 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4394 | *error_msg = StringPrintf("Instance field count off: %zu vs %zu\n%s", |
| 4395 | dex_instance_fields1, |
| 4396 | dex_instance_fields2, |
| 4397 | class_dump.c_str()); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4398 | return false; |
| 4399 | } |
| 4400 | |
| 4401 | return true; |
| 4402 | } |
| 4403 | |
| 4404 | // Checks whether a the super-class changed from what we had at compile-time. This would |
| 4405 | // invalidate quickening. |
| 4406 | static bool CheckSuperClassChange(Handle<mirror::Class> klass, |
| 4407 | const DexFile& dex_file, |
| 4408 | const DexFile::ClassDef& class_def, |
| 4409 | mirror::Class* super_class) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 4410 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4411 | // Check for unexpected changes in the superclass. |
| 4412 | // Quick check 1) is the super_class class-loader the boot class loader? This always has |
| 4413 | // precedence. |
| 4414 | if (super_class->GetClassLoader() != nullptr && |
| 4415 | // Quick check 2) different dex cache? Breaks can only occur for different dex files, |
| 4416 | // which is implied by different dex cache. |
| 4417 | klass->GetDexCache() != super_class->GetDexCache()) { |
| 4418 | // Now comes the expensive part: things can be broken if (a) the klass' dex file has a |
| 4419 | // definition for the super-class, and (b) the files are in separate oat files. The oat files |
| 4420 | // are referenced from the dex file, so do (b) first. Only relevant if we have oat files. |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 4421 | const OatDexFile* class_oat_dex_file = dex_file.GetOatDexFile(); |
| 4422 | const OatFile* class_oat_file = nullptr; |
| 4423 | if (class_oat_dex_file != nullptr) { |
| 4424 | class_oat_file = class_oat_dex_file->GetOatFile(); |
| 4425 | } |
| 4426 | |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4427 | if (class_oat_file != nullptr) { |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 4428 | const OatDexFile* loaded_super_oat_dex_file = super_class->GetDexFile().GetOatDexFile(); |
| 4429 | const OatFile* loaded_super_oat_file = nullptr; |
| 4430 | if (loaded_super_oat_dex_file != nullptr) { |
| 4431 | loaded_super_oat_file = loaded_super_oat_dex_file->GetOatFile(); |
| 4432 | } |
| 4433 | |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4434 | if (loaded_super_oat_file != nullptr && class_oat_file != loaded_super_oat_file) { |
| 4435 | // Now check (a). |
| 4436 | const DexFile::ClassDef* super_class_def = dex_file.FindClassDef(class_def.superclass_idx_); |
| 4437 | if (super_class_def != nullptr) { |
| 4438 | // Uh-oh, we found something. Do our check. |
| 4439 | std::string error_msg; |
| 4440 | if (!SimpleStructuralCheck(dex_file, *super_class_def, |
| 4441 | super_class->GetDexFile(), *super_class->GetClassDef(), |
| 4442 | &error_msg)) { |
| 4443 | // Print a warning to the log. This exception might be caught, e.g., as common in test |
| 4444 | // drivers. When the class is later tried to be used, we re-throw a new instance, as we |
| 4445 | // only save the type of the exception. |
| 4446 | LOG(WARNING) << "Incompatible structural change detected: " << |
| 4447 | StringPrintf( |
| 4448 | "Structural change of %s is hazardous (%s at compile time, %s at runtime): %s", |
| 4449 | PrettyType(super_class_def->class_idx_, dex_file).c_str(), |
| 4450 | class_oat_file->GetLocation().c_str(), |
| 4451 | loaded_super_oat_file->GetLocation().c_str(), |
| 4452 | error_msg.c_str()); |
| 4453 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 4454 | "Structural change of %s is hazardous (%s at compile time, %s at runtime): %s", |
| 4455 | PrettyType(super_class_def->class_idx_, dex_file).c_str(), |
| 4456 | class_oat_file->GetLocation().c_str(), |
| 4457 | loaded_super_oat_file->GetLocation().c_str(), |
| 4458 | error_msg.c_str()); |
| 4459 | return false; |
| 4460 | } |
| 4461 | } |
| 4462 | } |
| 4463 | } |
| 4464 | } |
| 4465 | return true; |
| 4466 | } |
| 4467 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4468 | bool ClassLinker::LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4469 | CHECK_EQ(mirror::Class::kStatusIdx, klass->GetStatus()); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4470 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
| 4471 | uint16_t super_class_idx = class_def.superclass_idx_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4472 | if (super_class_idx != DexFile::kDexNoIndex16) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4473 | mirror::Class* super_class = ResolveType(dex_file, super_class_idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4474 | if (super_class == nullptr) { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 4475 | DCHECK(Thread::Current()->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4476 | return false; |
| 4477 | } |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 4478 | // Verify |
| 4479 | if (!klass->CanAccess(super_class)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4480 | ThrowIllegalAccessError(klass.Get(), "Class %s extended by class %s is inaccessible", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4481 | PrettyDescriptor(super_class).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4482 | PrettyDescriptor(klass.Get()).c_str()); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 4483 | return false; |
| 4484 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4485 | CHECK(super_class->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4486 | klass->SetSuperClass(super_class); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4487 | |
| 4488 | if (!CheckSuperClassChange(klass, dex_file, class_def, super_class)) { |
| 4489 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4490 | return false; |
| 4491 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4492 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4493 | const DexFile::TypeList* interfaces = dex_file.GetInterfacesList(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4494 | if (interfaces != nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4495 | for (size_t i = 0; i < interfaces->Size(); i++) { |
| 4496 | uint16_t idx = interfaces->GetTypeItem(i).type_idx_; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4497 | mirror::Class* interface = ResolveType(dex_file, idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4498 | if (interface == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4499 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4500 | return false; |
| 4501 | } |
| 4502 | // Verify |
| 4503 | if (!klass->CanAccess(interface)) { |
| 4504 | // TODO: the RI seemed to ignore this in my testing. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4505 | ThrowIllegalAccessError(klass.Get(), |
| 4506 | "Interface %s implemented by class %s is inaccessible", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4507 | PrettyDescriptor(interface).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4508 | PrettyDescriptor(klass.Get()).c_str()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4509 | return false; |
| 4510 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4511 | } |
| 4512 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4513 | // Mark the class as loaded. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4514 | mirror::Class::SetStatus(klass, mirror::Class::kStatusLoaded, nullptr); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4515 | return true; |
| 4516 | } |
| 4517 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4518 | bool ClassLinker::LinkSuperClass(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4519 | CHECK(!klass->IsPrimitive()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4520 | mirror::Class* super = klass->GetSuperClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4521 | if (klass.Get() == GetClassRoot(kJavaLangObject)) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4522 | if (super != nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4523 | ThrowClassFormatError(klass.Get(), "java.lang.Object must not have a superclass"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4524 | return false; |
| 4525 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4526 | return true; |
| 4527 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4528 | if (super == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4529 | ThrowLinkageError(klass.Get(), "No superclass defined for class %s", |
| 4530 | PrettyDescriptor(klass.Get()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4531 | return false; |
| 4532 | } |
| 4533 | // Verify |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 4534 | if (super->IsFinal() || super->IsInterface()) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4535 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 4536 | "Superclass %s of %s is %s", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4537 | PrettyDescriptor(super).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4538 | PrettyDescriptor(klass.Get()).c_str(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4539 | super->IsFinal() ? "declared final" : "an interface"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4540 | return false; |
| 4541 | } |
| 4542 | if (!klass->CanAccess(super)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4543 | ThrowIllegalAccessError(klass.Get(), "Superclass %s is inaccessible to class %s", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4544 | PrettyDescriptor(super).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4545 | PrettyDescriptor(klass.Get()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4546 | return false; |
| 4547 | } |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 4548 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4549 | // Inherit kAccClassIsFinalizable from the superclass in case this |
| 4550 | // class doesn't override finalize. |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 4551 | if (super->IsFinalizable()) { |
| 4552 | klass->SetFinalizable(); |
| 4553 | } |
| 4554 | |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 4555 | // Inherit class loader flag form super class. |
| 4556 | if (super->IsClassLoaderClass()) { |
| 4557 | klass->SetClassLoaderClass(); |
| 4558 | } |
| 4559 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 4560 | // Inherit reference flags (if any) from the superclass. |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 4561 | uint32_t reference_flags = (super->GetClassFlags() & mirror::kClassFlagReference); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 4562 | if (reference_flags != 0) { |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 4563 | CHECK_EQ(klass->GetClassFlags(), 0u); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 4564 | klass->SetClassFlags(klass->GetClassFlags() | reference_flags); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 4565 | } |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 4566 | // Disallow custom direct subclasses of java.lang.ref.Reference. |
Elliott Hughes | bf61ba3 | 2011-10-11 10:53:09 -0700 | [diff] [blame] | 4567 | if (init_done_ && super == GetClassRoot(kJavaLangRefReference)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4568 | ThrowLinkageError(klass.Get(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4569 | "Class %s attempts to subclass java.lang.ref.Reference, which is not allowed", |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4570 | PrettyDescriptor(klass.Get()).c_str()); |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 4571 | return false; |
| 4572 | } |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 4573 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4574 | if (kIsDebugBuild) { |
| 4575 | // Ensure super classes are fully resolved prior to resolving fields.. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4576 | while (super != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4577 | CHECK(super->IsResolved()); |
| 4578 | super = super->GetSuperClass(); |
| 4579 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4580 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4581 | return true; |
| 4582 | } |
| 4583 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4584 | // Populate the class vtable and itable. Compute return type indices. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4585 | bool ClassLinker::LinkMethods(Thread* self, |
| 4586 | Handle<mirror::Class> klass, |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4587 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4588 | ArtMethod** out_imt) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4589 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4590 | if (klass->IsInterface()) { |
| 4591 | // No vtable. |
| 4592 | size_t count = klass->NumVirtualMethods(); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 4593 | if (!IsUint<16>(count)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4594 | ThrowClassFormatError(klass.Get(), "Too many methods on interface: %zd", count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4595 | return false; |
| 4596 | } |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 4597 | for (size_t i = 0; i < count; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4598 | klass->GetVirtualMethodDuringLinking(i, image_pointer_size_)->SetMethodIndex(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4599 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4600 | } else if (!LinkVirtualMethods(self, klass)) { // Link virtual methods first. |
| 4601 | return false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4602 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4603 | return LinkInterfaceMethods(self, klass, interfaces, out_imt); // Link interface method last. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4604 | } |
| 4605 | |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 4606 | // Comparator for name and signature of a method, used in finding overriding methods. Implementation |
| 4607 | // avoids the use of handles, if it didn't then rather than compare dex files we could compare dex |
| 4608 | // caches in the implementation below. |
| 4609 | class MethodNameAndSignatureComparator FINAL : public ValueObject { |
| 4610 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4611 | explicit MethodNameAndSignatureComparator(ArtMethod* method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 4612 | SHARED_REQUIRES(Locks::mutator_lock_) : |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 4613 | dex_file_(method->GetDexFile()), mid_(&dex_file_->GetMethodId(method->GetDexMethodIndex())), |
| 4614 | name_(nullptr), name_len_(0) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4615 | DCHECK(!method->IsProxyMethod()) << PrettyMethod(method); |
| 4616 | } |
| 4617 | |
| 4618 | const char* GetName() { |
| 4619 | if (name_ == nullptr) { |
| 4620 | name_ = dex_file_->StringDataAndUtf16LengthByIdx(mid_->name_idx_, &name_len_); |
| 4621 | } |
| 4622 | return name_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 4623 | } |
| 4624 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4625 | bool HasSameNameAndSignature(ArtMethod* other) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 4626 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 4627 | DCHECK(!other->IsProxyMethod()) << PrettyMethod(other); |
| 4628 | const DexFile* other_dex_file = other->GetDexFile(); |
| 4629 | const DexFile::MethodId& other_mid = other_dex_file->GetMethodId(other->GetDexMethodIndex()); |
| 4630 | if (dex_file_ == other_dex_file) { |
| 4631 | return mid_->name_idx_ == other_mid.name_idx_ && mid_->proto_idx_ == other_mid.proto_idx_; |
| 4632 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4633 | GetName(); // Only used to make sure its calculated. |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 4634 | uint32_t other_name_len; |
| 4635 | const char* other_name = other_dex_file->StringDataAndUtf16LengthByIdx(other_mid.name_idx_, |
| 4636 | &other_name_len); |
| 4637 | if (name_len_ != other_name_len || strcmp(name_, other_name) != 0) { |
| 4638 | return false; |
| 4639 | } |
| 4640 | return dex_file_->GetMethodSignature(*mid_) == other_dex_file->GetMethodSignature(other_mid); |
| 4641 | } |
| 4642 | |
| 4643 | private: |
| 4644 | // Dex file for the method to compare against. |
| 4645 | const DexFile* const dex_file_; |
| 4646 | // MethodId for the method to compare against. |
| 4647 | const DexFile::MethodId* const mid_; |
| 4648 | // Lazily computed name from the dex file's strings. |
| 4649 | const char* name_; |
| 4650 | // Lazily computed name length. |
| 4651 | uint32_t name_len_; |
| 4652 | }; |
| 4653 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4654 | class LinkVirtualHashTable { |
| 4655 | public: |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4656 | LinkVirtualHashTable(Handle<mirror::Class> klass, |
| 4657 | size_t hash_size, |
| 4658 | uint32_t* hash_table, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4659 | size_t image_pointer_size) |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4660 | : klass_(klass), |
| 4661 | hash_size_(hash_size), |
| 4662 | hash_table_(hash_table), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4663 | image_pointer_size_(image_pointer_size) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4664 | std::fill(hash_table_, hash_table_ + hash_size_, invalid_index_); |
| 4665 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4666 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 4667 | void Add(uint32_t virtual_method_index) SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4668 | ArtMethod* local_method = klass_->GetVirtualMethodDuringLinking( |
| 4669 | virtual_method_index, image_pointer_size_); |
| 4670 | const char* name = local_method->GetInterfaceMethodIfProxy(image_pointer_size_)->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 4671 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4672 | uint32_t index = hash % hash_size_; |
| 4673 | // Linear probe until we have an empty slot. |
| 4674 | while (hash_table_[index] != invalid_index_) { |
| 4675 | if (++index == hash_size_) { |
| 4676 | index = 0; |
| 4677 | } |
| 4678 | } |
| 4679 | hash_table_[index] = virtual_method_index; |
| 4680 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4681 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4682 | uint32_t FindAndRemove(MethodNameAndSignatureComparator* comparator) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 4683 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4684 | const char* name = comparator->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 4685 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4686 | size_t index = hash % hash_size_; |
| 4687 | while (true) { |
| 4688 | const uint32_t value = hash_table_[index]; |
| 4689 | // Since linear probe makes continuous blocks, hitting an invalid index means we are done |
| 4690 | // the block and can safely assume not found. |
| 4691 | if (value == invalid_index_) { |
| 4692 | break; |
| 4693 | } |
| 4694 | if (value != removed_index_) { // This signifies not already overriden. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4695 | ArtMethod* virtual_method = |
| 4696 | klass_->GetVirtualMethodDuringLinking(value, image_pointer_size_); |
| 4697 | if (comparator->HasSameNameAndSignature( |
| 4698 | virtual_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4699 | hash_table_[index] = removed_index_; |
| 4700 | return value; |
| 4701 | } |
| 4702 | } |
| 4703 | if (++index == hash_size_) { |
| 4704 | index = 0; |
| 4705 | } |
| 4706 | } |
| 4707 | return GetNotFoundIndex(); |
| 4708 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4709 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4710 | static uint32_t GetNotFoundIndex() { |
| 4711 | return invalid_index_; |
| 4712 | } |
| 4713 | |
| 4714 | private: |
| 4715 | static const uint32_t invalid_index_; |
| 4716 | static const uint32_t removed_index_; |
| 4717 | |
| 4718 | Handle<mirror::Class> klass_; |
| 4719 | const size_t hash_size_; |
| 4720 | uint32_t* const hash_table_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4721 | const size_t image_pointer_size_; |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4722 | }; |
| 4723 | |
| 4724 | const uint32_t LinkVirtualHashTable::invalid_index_ = std::numeric_limits<uint32_t>::max(); |
| 4725 | const uint32_t LinkVirtualHashTable::removed_index_ = std::numeric_limits<uint32_t>::max() - 1; |
| 4726 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4727 | bool ClassLinker::LinkVirtualMethods(Thread* self, Handle<mirror::Class> klass) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4728 | const size_t num_virtual_methods = klass->NumVirtualMethods(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4729 | if (klass->HasSuperClass()) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4730 | const size_t super_vtable_length = klass->GetSuperClass()->GetVTableLength(); |
| 4731 | const size_t max_count = num_virtual_methods + super_vtable_length; |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4732 | StackHandleScope<2> hs(self); |
Mingyao Yang | 38eecb0 | 2014-08-13 14:51:03 -0700 | [diff] [blame] | 4733 | Handle<mirror::Class> super_class(hs.NewHandle(klass->GetSuperClass())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4734 | MutableHandle<mirror::PointerArray> vtable; |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4735 | if (super_class->ShouldHaveEmbeddedImtAndVTable()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4736 | vtable = hs.NewHandle(AllocPointerArray(self, max_count)); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4737 | if (UNLIKELY(vtable.Get() == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4738 | self->AssertPendingOOMException(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4739 | return false; |
| 4740 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4741 | for (size_t i = 0; i < super_vtable_length; i++) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4742 | vtable->SetElementPtrSize( |
| 4743 | i, super_class->GetEmbeddedVTableEntry(i, image_pointer_size_), image_pointer_size_); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4744 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4745 | if (num_virtual_methods == 0) { |
| 4746 | klass->SetVTable(vtable.Get()); |
| 4747 | return true; |
| 4748 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4749 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4750 | auto* super_vtable = super_class->GetVTable(); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4751 | CHECK(super_vtable != nullptr) << PrettyClass(super_class.Get()); |
| 4752 | if (num_virtual_methods == 0) { |
| 4753 | klass->SetVTable(super_vtable); |
| 4754 | return true; |
| 4755 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4756 | vtable = hs.NewHandle(down_cast<mirror::PointerArray*>( |
| 4757 | super_vtable->CopyOf(self, max_count))); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4758 | if (UNLIKELY(vtable.Get() == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4759 | self->AssertPendingOOMException(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4760 | return false; |
| 4761 | } |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4762 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4763 | // How the algorithm works: |
| 4764 | // 1. Populate hash table by adding num_virtual_methods from klass. The values in the hash |
| 4765 | // table are: invalid_index for unused slots, index super_vtable_length + i for a virtual |
| 4766 | // method which has not been matched to a vtable method, and j if the virtual method at the |
| 4767 | // index overrode the super virtual method at index j. |
| 4768 | // 2. Loop through super virtual methods, if they overwrite, update hash table to j |
| 4769 | // (j < super_vtable_length) to avoid redundant checks. (TODO maybe use this info for reducing |
| 4770 | // the need for the initial vtable which we later shrink back down). |
| 4771 | // 3. Add non overridden methods to the end of the vtable. |
| 4772 | static constexpr size_t kMaxStackHash = 250; |
| 4773 | const size_t hash_table_size = num_virtual_methods * 3; |
| 4774 | uint32_t* hash_table_ptr; |
| 4775 | std::unique_ptr<uint32_t[]> hash_heap_storage; |
| 4776 | if (hash_table_size <= kMaxStackHash) { |
| 4777 | hash_table_ptr = reinterpret_cast<uint32_t*>( |
| 4778 | alloca(hash_table_size * sizeof(*hash_table_ptr))); |
| 4779 | } else { |
| 4780 | hash_heap_storage.reset(new uint32_t[hash_table_size]); |
| 4781 | hash_table_ptr = hash_heap_storage.get(); |
| 4782 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4783 | 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] | 4784 | // Add virtual methods to the hash table. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4785 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4786 | DCHECK(klass->GetVirtualMethodDuringLinking( |
| 4787 | i, image_pointer_size_)->GetDeclaringClass() != nullptr); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4788 | hash_table.Add(i); |
| 4789 | } |
| 4790 | // Loop through each super vtable method and see if they are overriden by a method we added to |
| 4791 | // the hash table. |
| 4792 | for (size_t j = 0; j < super_vtable_length; ++j) { |
| 4793 | // Search the hash table to see if we are overidden by any method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4794 | ArtMethod* super_method = vtable->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4795 | MethodNameAndSignatureComparator super_method_name_comparator( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4796 | super_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4797 | uint32_t hash_index = hash_table.FindAndRemove(&super_method_name_comparator); |
| 4798 | if (hash_index != hash_table.GetNotFoundIndex()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4799 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking( |
| 4800 | hash_index, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4801 | if (klass->CanAccessMember(super_method->GetDeclaringClass(), |
| 4802 | super_method->GetAccessFlags())) { |
| 4803 | if (super_method->IsFinal()) { |
| 4804 | ThrowLinkageError(klass.Get(), "Method %s overrides final method in class %s", |
| 4805 | PrettyMethod(virtual_method).c_str(), |
| 4806 | super_method->GetDeclaringClassDescriptor()); |
| 4807 | return false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4808 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4809 | vtable->SetElementPtrSize(j, virtual_method, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4810 | virtual_method->SetMethodIndex(j); |
| 4811 | } else { |
| 4812 | LOG(WARNING) << "Before Android 4.1, method " << PrettyMethod(virtual_method) |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4813 | << " would have incorrectly overridden the package-private method in " |
| 4814 | << PrettyDescriptor(super_method->GetDeclaringClassDescriptor()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4815 | } |
| 4816 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4817 | } |
| 4818 | // Add the non overridden methods at the end. |
| 4819 | size_t actual_count = super_vtable_length; |
| 4820 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4821 | ArtMethod* local_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4822 | size_t method_idx = local_method->GetMethodIndexDuringLinking(); |
| 4823 | if (method_idx < super_vtable_length && |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4824 | local_method == vtable->GetElementPtrSize<ArtMethod*>(method_idx, image_pointer_size_)) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4825 | continue; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4826 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4827 | vtable->SetElementPtrSize(actual_count, local_method, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4828 | local_method->SetMethodIndex(actual_count); |
| 4829 | ++actual_count; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4830 | } |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 4831 | if (!IsUint<16>(actual_count)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4832 | ThrowClassFormatError(klass.Get(), "Too many methods defined on class: %zd", actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4833 | return false; |
| 4834 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4835 | // Shrink vtable if possible |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4836 | CHECK_LE(actual_count, max_count); |
| 4837 | if (actual_count < max_count) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4838 | vtable.Assign(down_cast<mirror::PointerArray*>(vtable->CopyOf(self, actual_count))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4839 | if (UNLIKELY(vtable.Get() == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4840 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4841 | return false; |
| 4842 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4843 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4844 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4845 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4846 | CHECK_EQ(klass.Get(), GetClassRoot(kJavaLangObject)); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 4847 | if (!IsUint<16>(num_virtual_methods)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4848 | ThrowClassFormatError(klass.Get(), "Too many methods: %d", |
| 4849 | static_cast<int>(num_virtual_methods)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4850 | return false; |
| 4851 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4852 | auto* vtable = AllocPointerArray(self, num_virtual_methods); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4853 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4854 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4855 | return false; |
| 4856 | } |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 4857 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4858 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
| 4859 | vtable->SetElementPtrSize(i, virtual_method, image_pointer_size_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4860 | virtual_method->SetMethodIndex(i & 0xFFFF); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4861 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4862 | klass->SetVTable(vtable); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4863 | } |
| 4864 | return true; |
| 4865 | } |
| 4866 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4867 | bool ClassLinker::LinkInterfaceMethods(Thread* self, |
| 4868 | Handle<mirror::Class> klass, |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4869 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4870 | ArtMethod** out_imt) { |
Mathieu Chartier | d035c2d | 2014-10-27 17:30:20 -0700 | [diff] [blame] | 4871 | StackHandleScope<3> hs(self); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4872 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4873 | const bool has_superclass = klass->HasSuperClass(); |
| 4874 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
| 4875 | const bool have_interfaces = interfaces.Get() != nullptr; |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4876 | const size_t num_interfaces = have_interfaces |
| 4877 | ? interfaces->GetLength() |
| 4878 | : klass->NumDirectInterfaces(); |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 4879 | const size_t method_alignment = ArtMethod::Alignment(image_pointer_size_); |
| 4880 | const size_t method_size = ArtMethod::Size(image_pointer_size_); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4881 | if (num_interfaces == 0) { |
| 4882 | if (super_ifcount == 0) { |
| 4883 | // Class implements no interfaces. |
| 4884 | DCHECK_EQ(klass->GetIfTableCount(), 0); |
| 4885 | DCHECK(klass->GetIfTable() == nullptr); |
| 4886 | return true; |
| 4887 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4888 | // Class implements same interfaces as parent, are any of these not marker interfaces? |
| 4889 | bool has_non_marker_interface = false; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4890 | mirror::IfTable* super_iftable = klass->GetSuperClass()->GetIfTable(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4891 | for (size_t i = 0; i < super_ifcount; ++i) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4892 | if (super_iftable->GetMethodArrayCount(i) > 0) { |
| 4893 | has_non_marker_interface = true; |
| 4894 | break; |
| 4895 | } |
| 4896 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4897 | // Class just inherits marker interfaces from parent so recycle parent's iftable. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4898 | if (!has_non_marker_interface) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4899 | klass->SetIfTable(super_iftable); |
| 4900 | return true; |
| 4901 | } |
| 4902 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4903 | size_t ifcount = super_ifcount + num_interfaces; |
| 4904 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4905 | mirror::Class* interface = have_interfaces |
| 4906 | ? interfaces->GetWithoutChecks(i) |
| 4907 | : mirror::Class::GetDirectInterface(self, klass, i); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4908 | DCHECK(interface != nullptr); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4909 | if (UNLIKELY(!interface->IsInterface())) { |
| 4910 | std::string temp; |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4911 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 4912 | "Class %s implements non-interface class %s", |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4913 | PrettyDescriptor(klass.Get()).c_str(), |
| 4914 | PrettyDescriptor(interface->GetDescriptor(&temp)).c_str()); |
| 4915 | return false; |
| 4916 | } |
| 4917 | ifcount += interface->GetIfTableCount(); |
| 4918 | } |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4919 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(AllocIfTable(self, ifcount))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4920 | if (UNLIKELY(iftable.Get() == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4921 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4922 | return false; |
| 4923 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4924 | if (super_ifcount != 0) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4925 | mirror::IfTable* super_iftable = klass->GetSuperClass()->GetIfTable(); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 4926 | for (size_t i = 0; i < super_ifcount; i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4927 | mirror::Class* super_interface = super_iftable->GetInterface(i); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4928 | iftable->SetInterface(i, super_interface); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 4929 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4930 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4931 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4932 | // Flatten the interface inheritance hierarchy. |
| 4933 | size_t idx = super_ifcount; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4934 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | d035c2d | 2014-10-27 17:30:20 -0700 | [diff] [blame] | 4935 | mirror::Class* interface = have_interfaces ? interfaces->Get(i) : |
| 4936 | mirror::Class::GetDirectInterface(self, klass, i); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4937 | // Check if interface is already in iftable |
| 4938 | bool duplicate = false; |
| 4939 | for (size_t j = 0; j < idx; j++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4940 | mirror::Class* existing_interface = iftable->GetInterface(j); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4941 | if (existing_interface == interface) { |
| 4942 | duplicate = true; |
| 4943 | break; |
| 4944 | } |
| 4945 | } |
| 4946 | if (!duplicate) { |
| 4947 | // Add this non-duplicate interface. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4948 | iftable->SetInterface(idx++, interface); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4949 | // Add this interface's non-duplicate super-interfaces. |
| 4950 | for (int32_t j = 0; j < interface->GetIfTableCount(); j++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4951 | mirror::Class* super_interface = interface->GetIfTable()->GetInterface(j); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4952 | bool super_duplicate = false; |
| 4953 | for (size_t k = 0; k < idx; k++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4954 | mirror::Class* existing_interface = iftable->GetInterface(k); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4955 | if (existing_interface == super_interface) { |
| 4956 | super_duplicate = true; |
| 4957 | break; |
| 4958 | } |
| 4959 | } |
| 4960 | if (!super_duplicate) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4961 | iftable->SetInterface(idx++, super_interface); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4962 | } |
| 4963 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4964 | } |
| 4965 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4966 | self->AllowThreadSuspension(); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4967 | // Shrink iftable in case duplicates were found |
| 4968 | if (idx < ifcount) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4969 | DCHECK_NE(num_interfaces, 0U); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4970 | iftable.Assign(down_cast<mirror::IfTable*>( |
| 4971 | iftable->CopyOf(self, idx * mirror::IfTable::kMax))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4972 | if (UNLIKELY(iftable.Get() == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4973 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4974 | return false; |
| 4975 | } |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4976 | ifcount = idx; |
| 4977 | } else { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4978 | DCHECK_EQ(idx, ifcount); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4979 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4980 | klass->SetIfTable(iftable.Get()); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 4981 | // If we're an interface, we don't need the vtable pointers, so we're done. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4982 | if (klass->IsInterface()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4983 | return true; |
| 4984 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4985 | // These are allocated on the heap to begin, we then transfer to linear alloc when we re-create |
| 4986 | // the virtual methods array. |
| 4987 | // Need to use low 4GB arenas for compiler or else the pointers wont fit in 32 bit method array |
| 4988 | // during cross compilation. |
| 4989 | // Use the linear alloc pool since this one is in the low 4gb for the compiler. |
| 4990 | ArenaStack stack(runtime->GetLinearAlloc()->GetArenaPool()); |
| 4991 | ScopedArenaAllocator allocator(&stack); |
| 4992 | ScopedArenaVector<ArtMethod*> miranda_methods(allocator.Adapter()); |
| 4993 | |
| 4994 | MutableHandle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
| 4995 | ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod(); |
| 4996 | ArtMethod* const conflict_method = runtime->GetImtConflictMethod(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4997 | // Copy the IMT from the super class if possible. |
| 4998 | bool extend_super_iftable = false; |
| 4999 | if (has_superclass) { |
| 5000 | mirror::Class* super_class = klass->GetSuperClass(); |
| 5001 | extend_super_iftable = true; |
| 5002 | if (super_class->ShouldHaveEmbeddedImtAndVTable()) { |
| 5003 | for (size_t i = 0; i < mirror::Class::kImtSize; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5004 | out_imt[i] = super_class->GetEmbeddedImTableEntry(i, image_pointer_size_); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5005 | } |
| 5006 | } else { |
| 5007 | // No imt in the super class, need to reconstruct from the iftable. |
| 5008 | mirror::IfTable* if_table = super_class->GetIfTable(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5009 | const size_t length = super_class->GetIfTableCount(); |
| 5010 | for (size_t i = 0; i < length; ++i) { |
| 5011 | mirror::Class* interface = iftable->GetInterface(i); |
| 5012 | const size_t num_virtuals = interface->NumVirtualMethods(); |
| 5013 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 5014 | DCHECK_EQ(num_virtuals, method_array_count); |
| 5015 | if (method_array_count == 0) { |
| 5016 | continue; |
| 5017 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5018 | auto* method_array = if_table->GetMethodArray(i); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5019 | for (size_t j = 0; j < num_virtuals; ++j) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5020 | auto method = method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 5021 | DCHECK(method != nullptr) << PrettyClass(super_class); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5022 | if (method->IsMiranda()) { |
| 5023 | continue; |
| 5024 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5025 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5026 | uint32_t imt_index = interface_method->GetDexMethodIndex() % mirror::Class::kImtSize; |
Mathieu Chartier | cf3b1a3 | 2015-06-01 14:30:06 -0700 | [diff] [blame] | 5027 | auto** imt_ref = &out_imt[imt_index]; |
| 5028 | if (*imt_ref == unimplemented_method) { |
| 5029 | *imt_ref = method; |
| 5030 | } else if (*imt_ref != conflict_method) { |
| 5031 | *imt_ref = conflict_method; |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5032 | } |
| 5033 | } |
| 5034 | } |
| 5035 | } |
| 5036 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5037 | // Allocate method arrays before since we don't want miss visiting miranda method roots due to |
| 5038 | // thread suspension. |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 5039 | for (size_t i = 0; i < ifcount; ++i) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 5040 | size_t num_methods = iftable->GetInterface(i)->NumVirtualMethods(); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5041 | if (num_methods > 0) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5042 | const bool is_super = i < super_ifcount; |
| 5043 | const bool super_interface = is_super && extend_super_iftable; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5044 | mirror::PointerArray* method_array; |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5045 | if (super_interface) { |
| 5046 | mirror::IfTable* if_table = klass->GetSuperClass()->GetIfTable(); |
| 5047 | DCHECK(if_table != nullptr); |
| 5048 | DCHECK(if_table->GetMethodArray(i) != nullptr); |
| 5049 | // If we are working on a super interface, try extending the existing method array. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5050 | method_array = down_cast<mirror::PointerArray*>(if_table->GetMethodArray(i)->Clone(self)); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5051 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5052 | method_array = AllocPointerArray(self, num_methods); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5053 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5054 | if (UNLIKELY(method_array == nullptr)) { |
| 5055 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5056 | return false; |
| 5057 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5058 | iftable->SetMethodArray(i, method_array); |
| 5059 | } |
| 5060 | } |
| 5061 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5062 | auto* old_cause = self->StartAssertNoThreadSuspension( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5063 | "Copying ArtMethods for LinkInterfaceMethods"); |
| 5064 | for (size_t i = 0; i < ifcount; ++i) { |
| 5065 | size_t num_methods = iftable->GetInterface(i)->NumVirtualMethods(); |
| 5066 | if (num_methods > 0) { |
| 5067 | StackHandleScope<2> hs2(self); |
| 5068 | const bool is_super = i < super_ifcount; |
| 5069 | const bool super_interface = is_super && extend_super_iftable; |
| 5070 | auto method_array(hs2.NewHandle(iftable->GetMethodArray(i))); |
| 5071 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5072 | LengthPrefixedArray<ArtMethod>* input_virtual_methods = nullptr; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5073 | Handle<mirror::PointerArray> input_vtable_array = NullHandle<mirror::PointerArray>(); |
| 5074 | int32_t input_array_length = 0; |
| 5075 | if (super_interface) { |
| 5076 | // We are overwriting a super class interface, try to only virtual methods instead of the |
| 5077 | // whole vtable. |
| 5078 | input_virtual_methods = klass->GetVirtualMethodsPtr(); |
| 5079 | input_array_length = klass->NumVirtualMethods(); |
| 5080 | } else { |
| 5081 | // A new interface, we need the whole vtable in case a new interface method is implemented |
| 5082 | // in the whole superclass. |
| 5083 | input_vtable_array = vtable; |
| 5084 | input_array_length = input_vtable_array->GetLength(); |
| 5085 | } |
| 5086 | if (input_array_length == 0) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5087 | // If the added virtual methods is empty, do nothing. |
| 5088 | DCHECK(super_interface); |
| 5089 | continue; |
| 5090 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5091 | for (size_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5092 | auto* interface_method = iftable->GetInterface(i)->GetVirtualMethod(j, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5093 | MethodNameAndSignatureComparator interface_name_comparator( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5094 | interface_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5095 | int32_t k; |
| 5096 | // For each method listed in the interface's method list, find the |
| 5097 | // matching method in our class's method list. We want to favor the |
| 5098 | // subclass over the superclass, which just requires walking |
| 5099 | // back from the end of the vtable. (This only matters if the |
| 5100 | // superclass defines a private method and this class redefines |
| 5101 | // it -- otherwise it would use the same vtable slot. In .dex files |
| 5102 | // those don't end up in the virtual method table, so it shouldn't |
| 5103 | // matter which direction we go. We walk it backward anyway.) |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5104 | for (k = input_array_length - 1; k >= 0; --k) { |
| 5105 | ArtMethod* vtable_method = input_virtual_methods != nullptr ? |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 5106 | &input_virtual_methods->At(k, method_size, method_alignment) : |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5107 | input_vtable_array->GetElementPtrSize<ArtMethod*>(k, image_pointer_size_); |
| 5108 | ArtMethod* vtable_method_for_name_comparison = |
| 5109 | vtable_method->GetInterfaceMethodIfProxy(image_pointer_size_); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5110 | if (interface_name_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5111 | vtable_method_for_name_comparison)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5112 | if (!vtable_method->IsAbstract() && !vtable_method->IsPublic()) { |
Mathieu Chartier | 4d122c1 | 2015-06-17 14:14:36 -0700 | [diff] [blame] | 5113 | // Must do EndAssertNoThreadSuspension before throw since the throw can cause |
| 5114 | // allocations. |
| 5115 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5116 | ThrowIllegalAccessError(klass.Get(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5117 | "Method '%s' implementing interface method '%s' is not public", |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5118 | PrettyMethod(vtable_method).c_str(), PrettyMethod(interface_method).c_str()); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5119 | return false; |
| 5120 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5121 | method_array->SetElementPtrSize(j, vtable_method, image_pointer_size_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 5122 | // Place method in imt if entry is empty, place conflict otherwise. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5123 | uint32_t imt_index = interface_method->GetDexMethodIndex() % mirror::Class::kImtSize; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5124 | auto** imt_ref = &out_imt[imt_index]; |
| 5125 | if (*imt_ref == unimplemented_method) { |
| 5126 | *imt_ref = vtable_method; |
| 5127 | } else if (*imt_ref != conflict_method) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5128 | // If we are not a conflict and we have the same signature and name as the imt entry, |
| 5129 | // it must be that we overwrote a superclass vtable entry. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5130 | MethodNameAndSignatureComparator imt_comparator( |
| 5131 | (*imt_ref)->GetInterfaceMethodIfProxy(image_pointer_size_)); |
| 5132 | *imt_ref = imt_comparator.HasSameNameAndSignature(vtable_method_for_name_comparison) ? |
| 5133 | vtable_method : conflict_method; |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 5134 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5135 | break; |
| 5136 | } |
| 5137 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5138 | if (k < 0 && !super_interface) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5139 | ArtMethod* miranda_method = nullptr; |
| 5140 | for (auto& mir_method : miranda_methods) { |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5141 | if (interface_name_comparator.HasSameNameAndSignature(mir_method)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5142 | miranda_method = mir_method; |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5143 | break; |
| 5144 | } |
| 5145 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5146 | if (miranda_method == nullptr) { |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 5147 | miranda_method = reinterpret_cast<ArtMethod*>(allocator.Alloc(method_size)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5148 | CHECK(miranda_method != nullptr); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5149 | // Point the interface table at a phantom slot. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5150 | new(miranda_method) ArtMethod(*interface_method, image_pointer_size_); |
| 5151 | miranda_methods.push_back(miranda_method); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5152 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5153 | method_array->SetElementPtrSize(j, miranda_method, image_pointer_size_); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5154 | } |
| 5155 | } |
| 5156 | } |
| 5157 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5158 | if (!miranda_methods.empty()) { |
| 5159 | const size_t old_method_count = klass->NumVirtualMethods(); |
| 5160 | const size_t new_method_count = old_method_count + miranda_methods.size(); |
| 5161 | // Attempt to realloc to save RAM if possible. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5162 | LengthPrefixedArray<ArtMethod>* old_virtuals = klass->GetVirtualMethodsPtr(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5163 | // The Realloced virtual methods aren't visiblef from the class roots, so there is no issue |
| 5164 | // where GCs could attempt to mark stale pointers due to memcpy. And since we overwrite the |
| 5165 | // realloced memory with out->CopyFrom, we are guaranteed to have objects in the to space since |
| 5166 | // CopyFrom has internal read barriers. |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 5167 | const size_t old_size = old_virtuals != nullptr |
| 5168 | ? LengthPrefixedArray<ArtMethod>::ComputeSize(old_method_count, |
| 5169 | method_size, |
| 5170 | method_alignment) |
| 5171 | : 0u; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5172 | const size_t new_size = LengthPrefixedArray<ArtMethod>::ComputeSize(new_method_count, |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 5173 | method_size, |
| 5174 | method_alignment); |
Mathieu Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 5175 | auto* virtuals = reinterpret_cast<LengthPrefixedArray<ArtMethod>*>( |
| 5176 | runtime->GetLinearAlloc()->Realloc(self, old_virtuals, old_size, new_size)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5177 | if (UNLIKELY(virtuals == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5178 | self->AssertPendingOOMException(); |
Mathieu Chartier | 4e2cb09 | 2015-07-22 16:17:51 -0700 | [diff] [blame] | 5179 | self->EndAssertNoThreadSuspension(old_cause); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5180 | return false; |
| 5181 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5182 | ScopedArenaUnorderedMap<ArtMethod*, ArtMethod*> move_table(allocator.Adapter()); |
| 5183 | if (virtuals != old_virtuals) { |
| 5184 | // Maps from heap allocated miranda method to linear alloc miranda method. |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 5185 | StrideIterator<ArtMethod> out = virtuals->Begin(method_size, method_alignment); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5186 | // Copy over the old methods + miranda methods. |
| 5187 | for (auto& m : klass->GetVirtualMethods(image_pointer_size_)) { |
| 5188 | move_table.emplace(&m, &*out); |
| 5189 | // The CopyFrom is only necessary to not miss read barriers since Realloc won't do read |
| 5190 | // barriers when it copies. |
| 5191 | out->CopyFrom(&m, image_pointer_size_); |
| 5192 | ++out; |
| 5193 | } |
| 5194 | } |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 5195 | StrideIterator<ArtMethod> out(virtuals->Begin(method_size, method_alignment) + old_method_count); |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 5196 | // Copy over miranda methods before copying vtable since CopyOf may cause thread suspension and |
| 5197 | // we want the roots of the miranda methods to get visited. |
| 5198 | for (ArtMethod* mir_method : miranda_methods) { |
| 5199 | out->CopyFrom(mir_method, image_pointer_size_); |
| 5200 | out->SetAccessFlags(out->GetAccessFlags() | kAccMiranda); |
| 5201 | move_table.emplace(mir_method, &*out); |
Mathieu Chartier | cf3b1a3 | 2015-06-01 14:30:06 -0700 | [diff] [blame] | 5202 | ++out; |
Mathieu Chartier | cf3b1a3 | 2015-06-01 14:30:06 -0700 | [diff] [blame] | 5203 | } |
Mathieu Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 5204 | virtuals->SetLength(new_method_count); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5205 | UpdateClassVirtualMethods(klass.Get(), virtuals); |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 5206 | // 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] | 5207 | // suspension assert. |
| 5208 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 5209 | |
| 5210 | const size_t old_vtable_count = vtable->GetLength(); |
| 5211 | const size_t new_vtable_count = old_vtable_count + miranda_methods.size(); |
| 5212 | miranda_methods.clear(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5213 | vtable.Assign(down_cast<mirror::PointerArray*>(vtable->CopyOf(self, new_vtable_count))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5214 | if (UNLIKELY(vtable.Get() == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5215 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5216 | return false; |
| 5217 | } |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 5218 | out = virtuals->Begin(method_size, method_alignment) + old_method_count; |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 5219 | size_t vtable_pos = old_vtable_count; |
| 5220 | for (size_t i = old_method_count; i < new_method_count; ++i) { |
| 5221 | // Leave the declaring class alone as type indices are relative to it |
| 5222 | out->SetMethodIndex(0xFFFF & vtable_pos); |
| 5223 | vtable->SetElementPtrSize(vtable_pos, &*out, image_pointer_size_); |
| 5224 | ++out; |
| 5225 | ++vtable_pos; |
| 5226 | } |
| 5227 | CHECK_EQ(vtable_pos, new_vtable_count); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5228 | // Update old vtable methods. |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 5229 | for (size_t i = 0; i < old_vtable_count; ++i) { |
| 5230 | auto* m = vtable->GetElementPtrSize<ArtMethod*>(i, image_pointer_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5231 | DCHECK(m != nullptr) << PrettyClass(klass.Get()); |
| 5232 | auto it = move_table.find(m); |
| 5233 | if (it != move_table.end()) { |
| 5234 | auto* new_m = it->second; |
| 5235 | DCHECK(new_m != nullptr) << PrettyClass(klass.Get()); |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 5236 | vtable->SetElementPtrSize(i, new_m, image_pointer_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5237 | } |
| 5238 | } |
Mathieu Chartier | cf3b1a3 | 2015-06-01 14:30:06 -0700 | [diff] [blame] | 5239 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5240 | klass->SetVTable(vtable.Get()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5241 | // Go fix up all the stale miranda pointers. |
| 5242 | for (size_t i = 0; i < ifcount; ++i) { |
| 5243 | for (size_t j = 0, count = iftable->GetMethodArrayCount(i); j < count; ++j) { |
| 5244 | auto* method_array = iftable->GetMethodArray(i); |
| 5245 | auto* m = method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 5246 | DCHECK(m != nullptr) << PrettyClass(klass.Get()); |
| 5247 | auto it = move_table.find(m); |
| 5248 | if (it != move_table.end()) { |
| 5249 | auto* new_m = it->second; |
| 5250 | DCHECK(new_m != nullptr) << PrettyClass(klass.Get()); |
| 5251 | method_array->SetElementPtrSize(j, new_m, image_pointer_size_); |
| 5252 | } |
| 5253 | } |
| 5254 | } |
Mathieu Chartier | a40c74a | 2015-06-02 09:56:04 -0700 | [diff] [blame] | 5255 | // Fix up IMT in case it has any miranda methods in it. |
| 5256 | for (size_t i = 0; i < mirror::Class::kImtSize; ++i) { |
| 5257 | auto it = move_table.find(out_imt[i]); |
| 5258 | if (it != move_table.end()) { |
| 5259 | out_imt[i] = it->second; |
| 5260 | } |
| 5261 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5262 | // Check that there are no stale methods are in the dex cache array. |
| 5263 | if (kIsDebugBuild) { |
| 5264 | auto* resolved_methods = klass->GetDexCache()->GetResolvedMethods(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 5265 | for (size_t i = 0, count = klass->GetDexCache()->NumResolvedMethods(); i < count; ++i) { |
| 5266 | auto* m = mirror::DexCache::GetElementPtrSize(resolved_methods, i, image_pointer_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5267 | CHECK(move_table.find(m) == move_table.end()) << PrettyMethod(m); |
| 5268 | } |
| 5269 | } |
| 5270 | // Put some random garbage in old virtuals to help find stale pointers. |
| 5271 | if (virtuals != old_virtuals) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5272 | memset(old_virtuals, 0xFEu, old_size); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5273 | } |
| 5274 | } else { |
| 5275 | self->EndAssertNoThreadSuspension(old_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5276 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5277 | if (kIsDebugBuild) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5278 | auto* check_vtable = klass->GetVTableDuringLinking(); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 5279 | for (int i = 0; i < check_vtable->GetLength(); ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5280 | CHECK(check_vtable->GetElementPtrSize<ArtMethod*>(i, image_pointer_size_) != nullptr); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5281 | } |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 5282 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5283 | return true; |
| 5284 | } |
| 5285 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5286 | bool ClassLinker::LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5287 | CHECK(klass.Get() != nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5288 | return LinkFields(self, klass, false, nullptr); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5289 | } |
| 5290 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5291 | bool ClassLinker::LinkStaticFields(Thread* self, Handle<mirror::Class> klass, size_t* class_size) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5292 | CHECK(klass.Get() != nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5293 | return LinkFields(self, klass, true, class_size); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5294 | } |
| 5295 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5296 | struct LinkFieldsComparator { |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 5297 | explicit LinkFieldsComparator() SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5298 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 5299 | // 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] | 5300 | bool operator()(ArtField* field1, ArtField* field2) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5301 | NO_THREAD_SAFETY_ANALYSIS { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 5302 | // 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] | 5303 | Primitive::Type type1 = field1->GetTypeAsPrimitiveType(); |
| 5304 | Primitive::Type type2 = field2->GetTypeAsPrimitiveType(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 5305 | if (type1 != type2) { |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 5306 | if (type1 == Primitive::kPrimNot) { |
| 5307 | // Reference always goes first. |
| 5308 | return true; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 5309 | } |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 5310 | if (type2 == Primitive::kPrimNot) { |
| 5311 | // Reference always goes first. |
| 5312 | return false; |
| 5313 | } |
| 5314 | size_t size1 = Primitive::ComponentSize(type1); |
| 5315 | size_t size2 = Primitive::ComponentSize(type2); |
| 5316 | if (size1 != size2) { |
| 5317 | // Larger primitive types go first. |
| 5318 | return size1 > size2; |
| 5319 | } |
| 5320 | // Primitive types differ but sizes match. Arbitrarily order by primitive type. |
| 5321 | return type1 < type2; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5322 | } |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 5323 | // Same basic group? Then sort by dex field index. This is guaranteed to be sorted |
| 5324 | // by name and for equal names by type id index. |
| 5325 | // NOTE: This works also for proxies. Their static fields are assigned appropriate indexes. |
| 5326 | return field1->GetDexFieldIndex() < field2->GetDexFieldIndex(); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5327 | } |
| 5328 | }; |
| 5329 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5330 | bool ClassLinker::LinkFields(Thread* self, |
| 5331 | Handle<mirror::Class> klass, |
| 5332 | bool is_static, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5333 | size_t* class_size) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5334 | self->AllowThreadSuspension(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5335 | const size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5336 | LengthPrefixedArray<ArtField>* const fields = is_static ? klass->GetSFieldsPtr() : |
| 5337 | klass->GetIFieldsPtr(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5338 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5339 | // Initialize field_offset |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 5340 | MemberOffset field_offset(0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5341 | if (is_static) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5342 | field_offset = klass->GetFirstReferenceStaticFieldOffsetDuringLinking(image_pointer_size_); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5343 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5344 | mirror::Class* super_class = klass->GetSuperClass(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5345 | if (super_class != nullptr) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5346 | CHECK(super_class->IsResolved()) |
| 5347 | << PrettyClass(klass.Get()) << " " << PrettyClass(super_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5348 | field_offset = MemberOffset(super_class->GetObjectSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5349 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5350 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5351 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5352 | CHECK_EQ(num_fields == 0, fields == nullptr) << PrettyClass(klass.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5353 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5354 | // we want a relatively stable order so that adding new fields |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 5355 | // minimizes disruption of C++ version such as Class and Method. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5356 | std::deque<ArtField*> grouped_and_sorted_fields; |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 5357 | const char* old_no_suspend_cause = self->StartAssertNoThreadSuspension( |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 5358 | "Naked ArtField references in deque"); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5359 | for (size_t i = 0; i < num_fields; i++) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5360 | grouped_and_sorted_fields.push_back(&fields->At(i)); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5361 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5362 | std::sort(grouped_and_sorted_fields.begin(), grouped_and_sorted_fields.end(), |
| 5363 | LinkFieldsComparator()); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5364 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 5365 | // References should be at the front. |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5366 | size_t current_field = 0; |
| 5367 | size_t num_reference_fields = 0; |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 5368 | FieldGaps gaps; |
| 5369 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5370 | for (; current_field < num_fields; current_field++) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5371 | ArtField* field = grouped_and_sorted_fields.front(); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 5372 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 5373 | bool isPrimitive = type != Primitive::kPrimNot; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5374 | if (isPrimitive) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 5375 | break; // past last reference, move on to the next phase |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5376 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5377 | if (UNLIKELY(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>( |
| 5378 | field_offset.Uint32Value()))) { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 5379 | MemberOffset old_offset = field_offset; |
| 5380 | field_offset = MemberOffset(RoundUp(field_offset.Uint32Value(), 4)); |
| 5381 | AddFieldGap(old_offset.Uint32Value(), field_offset.Uint32Value(), &gaps); |
| 5382 | } |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 5383 | DCHECK_ALIGNED(field_offset.Uint32Value(), sizeof(mirror::HeapReference<mirror::Object>)); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5384 | grouped_and_sorted_fields.pop_front(); |
| 5385 | num_reference_fields++; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5386 | field->SetOffset(field_offset); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5387 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 5388 | sizeof(mirror::HeapReference<mirror::Object>)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5389 | } |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 5390 | // Gaps are stored as a max heap which means that we must shuffle from largest to smallest |
| 5391 | // otherwise we could end up with suboptimal gap fills. |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5392 | ShuffleForward<8>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 5393 | ShuffleForward<4>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 5394 | ShuffleForward<2>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 5395 | ShuffleForward<1>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 5396 | CHECK(grouped_and_sorted_fields.empty()) << "Missed " << grouped_and_sorted_fields.size() << |
| 5397 | " fields."; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5398 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5399 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 5400 | // 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] | 5401 | if (!is_static && klass->DescriptorEquals("Ljava/lang/ref/Reference;")) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 5402 | // We know there are no non-reference fields in the Reference classes, and we know |
| 5403 | // that 'referent' is alphabetically last, so this is easy... |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5404 | CHECK_EQ(num_reference_fields, num_fields) << PrettyClass(klass.Get()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5405 | CHECK_STREQ(fields->At(num_fields - 1).GetName(), "referent") |
| 5406 | << PrettyClass(klass.Get()); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 5407 | --num_reference_fields; |
| 5408 | } |
| 5409 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5410 | size_t size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5411 | // Update klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5412 | if (is_static) { |
| 5413 | klass->SetNumReferenceStaticFields(num_reference_fields); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5414 | *class_size = size; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5415 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5416 | klass->SetNumReferenceInstanceFields(num_reference_fields); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 5417 | mirror::Class* super_class = klass->GetSuperClass(); |
| 5418 | if (num_reference_fields == 0 || super_class == nullptr) { |
| 5419 | // 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] | 5420 | // super_class is null iff the class is java.lang.Object. |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 5421 | if (super_class == nullptr || |
| 5422 | (super_class->GetClassFlags() & mirror::kClassFlagNoReferenceFields) != 0) { |
| 5423 | klass->SetClassFlags(klass->GetClassFlags() | mirror::kClassFlagNoReferenceFields); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 5424 | } |
| 5425 | } |
| 5426 | if (kIsDebugBuild) { |
| 5427 | DCHECK_EQ(super_class == nullptr, klass->DescriptorEquals("Ljava/lang/Object;")); |
| 5428 | size_t total_reference_instance_fields = 0; |
| 5429 | mirror::Class* cur_super = klass.Get(); |
| 5430 | while (cur_super != nullptr) { |
| 5431 | total_reference_instance_fields += cur_super->NumReferenceInstanceFieldsDuringLinking(); |
| 5432 | cur_super = cur_super->GetSuperClass(); |
| 5433 | } |
| 5434 | if (super_class == nullptr) { |
| 5435 | CHECK_EQ(total_reference_instance_fields, 1u) << PrettyDescriptor(klass.Get()); |
| 5436 | } else { |
| 5437 | // Check that there is at least num_reference_fields other than Object.class. |
| 5438 | CHECK_GE(total_reference_instance_fields, 1u + num_reference_fields) |
| 5439 | << PrettyClass(klass.Get()); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 5440 | } |
| 5441 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5442 | if (!klass->IsVariableSize()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5443 | std::string temp; |
| 5444 | DCHECK_GE(size, sizeof(mirror::Object)) << klass->GetDescriptor(&temp); |
| 5445 | size_t previous_size = klass->GetObjectSize(); |
| 5446 | if (previous_size != 0) { |
| 5447 | // Make sure that we didn't originally have an incorrect size. |
| 5448 | CHECK_EQ(previous_size, size) << klass->GetDescriptor(&temp); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 5449 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5450 | klass->SetObjectSize(size); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5451 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5452 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5453 | |
| 5454 | if (kIsDebugBuild) { |
| 5455 | // Make sure that the fields array is ordered by name but all reference |
| 5456 | // offsets are at the beginning as far as alignment allows. |
| 5457 | MemberOffset start_ref_offset = is_static |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5458 | ? klass->GetFirstReferenceStaticFieldOffsetDuringLinking(image_pointer_size_) |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5459 | : klass->GetFirstReferenceInstanceFieldOffset(); |
| 5460 | MemberOffset end_ref_offset(start_ref_offset.Uint32Value() + |
| 5461 | num_reference_fields * |
| 5462 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 5463 | MemberOffset current_ref_offset = start_ref_offset; |
| 5464 | for (size_t i = 0; i < num_fields; i++) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5465 | ArtField* field = &fields->At(i); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5466 | VLOG(class_linker) << "LinkFields: " << (is_static ? "static" : "instance") |
| 5467 | << " class=" << PrettyClass(klass.Get()) << " field=" << PrettyField(field) << " offset=" |
Nicolas Geoffray | 555b3d0 | 2015-06-23 12:11:24 +0100 | [diff] [blame] | 5468 | << field->GetOffsetDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5469 | if (i != 0) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5470 | ArtField* const prev_field = &fields->At(i - 1); |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 5471 | // NOTE: The field names can be the same. This is not possible in the Java language |
| 5472 | // 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] | 5473 | DCHECK_LE(strcmp(prev_field->GetName(), field->GetName()), 0); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5474 | } |
| 5475 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 5476 | bool is_primitive = type != Primitive::kPrimNot; |
| 5477 | if (klass->DescriptorEquals("Ljava/lang/ref/Reference;") && |
| 5478 | strcmp("referent", field->GetName()) == 0) { |
| 5479 | is_primitive = true; // We lied above, so we have to expect a lie here. |
| 5480 | } |
| 5481 | MemberOffset offset = field->GetOffsetDuringLinking(); |
| 5482 | if (is_primitive) { |
| 5483 | if (offset.Uint32Value() < end_ref_offset.Uint32Value()) { |
| 5484 | // Shuffled before references. |
| 5485 | size_t type_size = Primitive::ComponentSize(type); |
| 5486 | CHECK_LT(type_size, sizeof(mirror::HeapReference<mirror::Object>)); |
| 5487 | CHECK_LT(offset.Uint32Value(), start_ref_offset.Uint32Value()); |
| 5488 | CHECK_LE(offset.Uint32Value() + type_size, start_ref_offset.Uint32Value()); |
| 5489 | CHECK(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(offset.Uint32Value())); |
| 5490 | } |
| 5491 | } else { |
| 5492 | CHECK_EQ(current_ref_offset.Uint32Value(), offset.Uint32Value()); |
| 5493 | current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() + |
| 5494 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 5495 | } |
| 5496 | } |
| 5497 | CHECK_EQ(current_ref_offset.Uint32Value(), end_ref_offset.Uint32Value()); |
| 5498 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5499 | return true; |
| 5500 | } |
| 5501 | |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5502 | // Set the bitmap of reference instance field offsets. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5503 | void ClassLinker::CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5504 | uint32_t reference_offsets = 0; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5505 | mirror::Class* super_class = klass->GetSuperClass(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5506 | // 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] | 5507 | if (super_class != nullptr) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5508 | reference_offsets = super_class->GetReferenceInstanceOffsets(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5509 | // Compute reference offsets unless our superclass overflowed. |
| 5510 | if (reference_offsets != mirror::Class::kClassWalkSuper) { |
| 5511 | size_t num_reference_fields = klass->NumReferenceInstanceFieldsDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5512 | if (num_reference_fields != 0u) { |
| 5513 | // All of the fields that contain object references are guaranteed be grouped in memory |
| 5514 | // starting at an appropriately aligned address after super class object data. |
| 5515 | uint32_t start_offset = RoundUp(super_class->GetObjectSize(), |
| 5516 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 5517 | uint32_t start_bit = (start_offset - mirror::kObjectHeaderSize) / |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5518 | sizeof(mirror::HeapReference<mirror::Object>); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5519 | if (start_bit + num_reference_fields > 32) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5520 | reference_offsets = mirror::Class::kClassWalkSuper; |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5521 | } else { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5522 | reference_offsets |= (0xffffffffu << start_bit) & |
| 5523 | (0xffffffffu >> (32 - (start_bit + num_reference_fields))); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5524 | } |
| 5525 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5526 | } |
| 5527 | } |
Mingyao Yang | faff0f0 | 2014-09-10 12:03:22 -0700 | [diff] [blame] | 5528 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5529 | } |
| 5530 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5531 | mirror::String* ClassLinker::ResolveString(const DexFile& dex_file, |
| 5532 | uint32_t string_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5533 | Handle<mirror::DexCache> dex_cache) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5534 | DCHECK(dex_cache.Get() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5535 | mirror::String* resolved = dex_cache->GetResolvedString(string_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5536 | if (resolved != nullptr) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5537 | return resolved; |
| 5538 | } |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 5539 | uint32_t utf16_length; |
| 5540 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5541 | mirror::String* string = intern_table_->InternStrong(utf16_length, utf8_data); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5542 | dex_cache->SetResolvedString(string_idx, string); |
| 5543 | return string; |
| 5544 | } |
| 5545 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5546 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, |
| 5547 | uint16_t type_idx, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 5548 | mirror::Class* referrer) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5549 | StackHandleScope<2> hs(Thread::Current()); |
| 5550 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 5551 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5552 | return ResolveType(dex_file, type_idx, dex_cache, class_loader); |
| 5553 | } |
| 5554 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5555 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, |
| 5556 | uint16_t type_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5557 | Handle<mirror::DexCache> dex_cache, |
| 5558 | Handle<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5559 | DCHECK(dex_cache.Get() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5560 | mirror::Class* resolved = dex_cache->GetResolvedType(type_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5561 | if (resolved == nullptr) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 5562 | Thread* self = Thread::Current(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 5563 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 5564 | resolved = FindClass(self, descriptor, class_loader); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5565 | if (resolved != nullptr) { |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 5566 | // TODO: we used to throw here if resolved's class loader was not the |
| 5567 | // boot class loader. This was to permit different classes with the |
| 5568 | // same name to be loaded simultaneously by different loaders |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5569 | dex_cache->SetResolvedType(type_idx, resolved); |
| 5570 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5571 | CHECK(self->IsExceptionPending()) |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 5572 | << "Expected pending exception for failed resolution of: " << descriptor; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5573 | // Convert a ClassNotFoundException to a NoClassDefFoundError. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5574 | StackHandleScope<1> hs(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 5575 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5576 | if (cause->InstanceOf(GetClassRoot(kJavaLangClassNotFoundException))) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5577 | DCHECK(resolved == nullptr); // No Handle needed to preserve resolved. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 5578 | self->ClearException(); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 5579 | ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 5580 | self->GetException()->SetCause(cause.Get()); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 5581 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5582 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5583 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5584 | DCHECK((resolved == nullptr) || resolved->IsResolved() || resolved->IsErroneous()) |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5585 | << PrettyDescriptor(resolved) << " " << resolved->GetStatus(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5586 | return resolved; |
| 5587 | } |
| 5588 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5589 | ArtMethod* ClassLinker::ResolveMethod(const DexFile& dex_file, |
| 5590 | uint32_t method_idx, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5591 | Handle<mirror::DexCache> dex_cache, |
| 5592 | Handle<mirror::ClassLoader> class_loader, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5593 | ArtMethod* referrer, |
| 5594 | InvokeType type) { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5595 | DCHECK(dex_cache.Get() != nullptr); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5596 | // Check for hit in the dex cache. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5597 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, image_pointer_size_); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 5598 | if (resolved != nullptr && !resolved->IsRuntimeMethod()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5599 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5600 | return resolved; |
| 5601 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5602 | // Fail, get the declaring class. |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5603 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5604 | mirror::Class* klass = ResolveType(dex_file, method_id.class_idx_, dex_cache, class_loader); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5605 | if (klass == nullptr) { |
Elliott Hughes | cc5f9a9 | 2011-09-28 19:17:29 -0700 | [diff] [blame] | 5606 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5607 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5608 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5609 | // Scan using method_idx, this saves string compares but will only hit for matching dex |
| 5610 | // caches/files. |
| 5611 | switch (type) { |
| 5612 | case kDirect: // Fall-through. |
| 5613 | case kStatic: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5614 | resolved = klass->FindDirectMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 5615 | DCHECK(resolved == nullptr || resolved->GetDeclaringClassUnchecked() != nullptr); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5616 | break; |
| 5617 | case kInterface: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5618 | resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5619 | DCHECK(resolved == nullptr || resolved->GetDeclaringClass()->IsInterface()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5620 | break; |
| 5621 | case kSuper: // Fall-through. |
| 5622 | case kVirtual: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5623 | resolved = klass->FindVirtualMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5624 | break; |
| 5625 | default: |
| 5626 | LOG(FATAL) << "Unreachable - invocation type: " << type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 5627 | UNREACHABLE(); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 5628 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5629 | if (resolved == nullptr) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5630 | // Search by name, which works across dex files. |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5631 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 5632 | const Signature signature = dex_file.GetMethodSignature(method_id); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5633 | switch (type) { |
| 5634 | case kDirect: // Fall-through. |
| 5635 | case kStatic: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5636 | resolved = klass->FindDirectMethod(name, signature, image_pointer_size_); |
| 5637 | DCHECK(resolved == nullptr || resolved->GetDeclaringClassUnchecked() != nullptr); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5638 | break; |
| 5639 | case kInterface: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5640 | resolved = klass->FindInterfaceMethod(name, signature, image_pointer_size_); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5641 | DCHECK(resolved == nullptr || resolved->GetDeclaringClass()->IsInterface()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5642 | break; |
| 5643 | case kSuper: // Fall-through. |
| 5644 | case kVirtual: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5645 | resolved = klass->FindVirtualMethod(name, signature, image_pointer_size_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5646 | break; |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5647 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5648 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5649 | // If we found a method, check for incompatible class changes. |
| 5650 | if (LIKELY(resolved != nullptr && !resolved->CheckIncompatibleClassChange(type))) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5651 | // Be a good citizen and update the dex cache to speed subsequent calls. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5652 | dex_cache->SetResolvedMethod(method_idx, resolved, image_pointer_size_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5653 | return resolved; |
| 5654 | } else { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5655 | // If we had a method, it's an incompatible-class-change error. |
| 5656 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5657 | ThrowIncompatibleClassChangeError(type, resolved->GetInvokeType(), resolved, referrer); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5658 | } else { |
| 5659 | // We failed to find the method which means either an access error, an incompatible class |
| 5660 | // change, or no such method. First try to find the method among direct and virtual methods. |
| 5661 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
| 5662 | const Signature signature = dex_file.GetMethodSignature(method_id); |
| 5663 | switch (type) { |
| 5664 | case kDirect: |
| 5665 | case kStatic: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5666 | resolved = klass->FindVirtualMethod(name, signature, image_pointer_size_); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5667 | // Note: kDirect and kStatic are also mutually exclusive, but in that case we would |
| 5668 | // have had a resolved method before, which triggers the "true" branch above. |
| 5669 | break; |
| 5670 | case kInterface: |
| 5671 | case kVirtual: |
| 5672 | case kSuper: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5673 | resolved = klass->FindDirectMethod(name, signature, image_pointer_size_); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5674 | break; |
| 5675 | } |
| 5676 | |
| 5677 | // If we found something, check that it can be accessed by the referrer. |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5678 | bool exception_generated = false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5679 | if (resolved != nullptr && referrer != nullptr) { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5680 | mirror::Class* methods_class = resolved->GetDeclaringClass(); |
| 5681 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
| 5682 | if (!referring_class->CanAccess(methods_class)) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5683 | ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, |
| 5684 | methods_class, |
| 5685 | resolved, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5686 | type); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5687 | exception_generated = true; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5688 | } else if (!referring_class->CanAccessMember(methods_class, resolved->GetAccessFlags())) { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5689 | ThrowIllegalAccessErrorMethod(referring_class, resolved); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5690 | exception_generated = true; |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5691 | } |
| 5692 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5693 | if (!exception_generated) { |
| 5694 | // Otherwise, throw an IncompatibleClassChangeError if we found something, and check |
| 5695 | // interface methods and throw if we find the method there. If we find nothing, throw a |
| 5696 | // NoSuchMethodError. |
| 5697 | switch (type) { |
| 5698 | case kDirect: |
| 5699 | case kStatic: |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5700 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5701 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5702 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5703 | resolved = klass->FindInterfaceMethod(name, signature, image_pointer_size_); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5704 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5705 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5706 | } else { |
| 5707 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 5708 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5709 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5710 | break; |
| 5711 | case kInterface: |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5712 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5713 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5714 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5715 | resolved = klass->FindVirtualMethod(name, signature, image_pointer_size_); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5716 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5717 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5718 | } else { |
| 5719 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 5720 | } |
| 5721 | } |
| 5722 | break; |
| 5723 | case kSuper: |
| 5724 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5725 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5726 | } else { |
| 5727 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 5728 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5729 | break; |
| 5730 | case kVirtual: |
| 5731 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5732 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5733 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5734 | resolved = klass->FindInterfaceMethod(name, signature, image_pointer_size_); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5735 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5736 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5737 | } else { |
| 5738 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 5739 | } |
| 5740 | } |
| 5741 | break; |
| 5742 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5743 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5744 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5745 | Thread::Current()->AssertPendingException(); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5746 | return nullptr; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5747 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5748 | } |
| 5749 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 5750 | ArtMethod* ClassLinker::ResolveMethodWithoutInvokeType(const DexFile& dex_file, |
| 5751 | uint32_t method_idx, |
| 5752 | Handle<mirror::DexCache> dex_cache, |
| 5753 | Handle<mirror::ClassLoader> class_loader) { |
| 5754 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, image_pointer_size_); |
| 5755 | if (resolved != nullptr && !resolved->IsRuntimeMethod()) { |
| 5756 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
| 5757 | return resolved; |
| 5758 | } |
| 5759 | // Fail, get the declaring class. |
| 5760 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
| 5761 | mirror::Class* klass = ResolveType(dex_file, method_id.class_idx_, dex_cache, class_loader); |
| 5762 | if (klass == nullptr) { |
| 5763 | Thread::Current()->AssertPendingException(); |
| 5764 | return nullptr; |
| 5765 | } |
| 5766 | if (klass->IsInterface()) { |
| 5767 | LOG(FATAL) << "ResolveAmbiguousMethod: unexpected method in interface: " << PrettyClass(klass); |
| 5768 | return nullptr; |
| 5769 | } |
| 5770 | |
| 5771 | // Search both direct and virtual methods |
| 5772 | resolved = klass->FindDirectMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 5773 | if (resolved == nullptr) { |
| 5774 | resolved = klass->FindVirtualMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 5775 | } |
| 5776 | |
| 5777 | return resolved; |
| 5778 | } |
| 5779 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5780 | ArtField* ClassLinker::ResolveField(const DexFile& dex_file, |
| 5781 | uint32_t field_idx, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5782 | Handle<mirror::DexCache> dex_cache, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5783 | Handle<mirror::ClassLoader> class_loader, |
| 5784 | bool is_static) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5785 | DCHECK(dex_cache.Get() != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5786 | ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5787 | if (resolved != nullptr) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5788 | return resolved; |
| 5789 | } |
| 5790 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5791 | Thread* const self = Thread::Current(); |
| 5792 | StackHandleScope<1> hs(self); |
| 5793 | Handle<mirror::Class> klass( |
| 5794 | hs.NewHandle(ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader))); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5795 | if (klass.Get() == nullptr) { |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 5796 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5797 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5798 | } |
| 5799 | |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 5800 | if (is_static) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5801 | resolved = mirror::Class::FindStaticField(self, klass, dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 5802 | } else { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5803 | resolved = klass->FindInstanceField(dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 5804 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5805 | |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5806 | if (resolved == nullptr) { |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5807 | const char* name = dex_file.GetFieldName(field_id); |
| 5808 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 5809 | if (is_static) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5810 | resolved = mirror::Class::FindStaticField(self, klass, name, type); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5811 | } else { |
| 5812 | resolved = klass->FindInstanceField(name, type); |
| 5813 | } |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5814 | if (resolved == nullptr) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5815 | ThrowNoSuchFieldError(is_static ? "static " : "instance ", klass.Get(), type, name); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5816 | return nullptr; |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5817 | } |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5818 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5819 | dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5820 | return resolved; |
| 5821 | } |
| 5822 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5823 | ArtField* ClassLinker::ResolveFieldJLS(const DexFile& dex_file, |
| 5824 | uint32_t field_idx, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5825 | Handle<mirror::DexCache> dex_cache, |
| 5826 | Handle<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5827 | DCHECK(dex_cache.Get() != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5828 | ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5829 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5830 | return resolved; |
| 5831 | } |
| 5832 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5833 | Thread* self = Thread::Current(); |
| 5834 | StackHandleScope<1> hs(self); |
| 5835 | Handle<mirror::Class> klass( |
| 5836 | hs.NewHandle(ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5837 | if (klass.Get() == nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5838 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5839 | return nullptr; |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5840 | } |
| 5841 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 5842 | StringPiece name(dex_file.StringDataByIdx(field_id.name_idx_)); |
| 5843 | StringPiece type(dex_file.StringDataByIdx( |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5844 | dex_file.GetTypeId(field_id.type_idx_).descriptor_idx_)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5845 | resolved = mirror::Class::FindField(self, klass, name, type); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5846 | if (resolved != nullptr) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5847 | dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5848 | } else { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5849 | ThrowNoSuchFieldError("", klass.Get(), type, name); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5850 | } |
| 5851 | return resolved; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 5852 | } |
| 5853 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5854 | const char* ClassLinker::MethodShorty(uint32_t method_idx, |
| 5855 | ArtMethod* referrer, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5856 | uint32_t* length) { |
| 5857 | mirror::Class* declaring_class = referrer->GetDeclaringClass(); |
| 5858 | mirror::DexCache* dex_cache = declaring_class->GetDexCache(); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 5859 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 5860 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 5861 | return dex_file.GetMethodShorty(method_id, length); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 5862 | } |
| 5863 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 5864 | class DumpClassVisitor : public ClassVisitor { |
| 5865 | public: |
| 5866 | explicit DumpClassVisitor(int flags) : flags_(flags) {} |
| 5867 | |
| 5868 | bool Visit(mirror::Class* klass) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
| 5869 | klass->DumpClass(LOG(ERROR), flags_); |
| 5870 | return true; |
| 5871 | } |
| 5872 | |
| 5873 | private: |
| 5874 | const int flags_; |
| 5875 | }; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 5876 | |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5877 | void ClassLinker::DumpAllClasses(int flags) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 5878 | DumpClassVisitor visitor(flags); |
| 5879 | VisitClasses(&visitor); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 5880 | } |
| 5881 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 5882 | static OatFile::OatMethod CreateOatMethod(const void* code) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5883 | CHECK(code != nullptr); |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 5884 | const uint8_t* base = reinterpret_cast<const uint8_t*>(code); // Base of data points at code. |
| 5885 | base -= sizeof(void*); // Move backward so that code_offset != 0. |
| 5886 | const uint32_t code_offset = sizeof(void*); |
| 5887 | return OatFile::OatMethod(base, code_offset); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5888 | } |
| 5889 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5890 | bool ClassLinker::IsQuickResolutionStub(const void* entry_point) const { |
| 5891 | return (entry_point == GetQuickResolutionStub()) || |
| 5892 | (quick_resolution_trampoline_ == entry_point); |
| 5893 | } |
| 5894 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5895 | bool ClassLinker::IsQuickToInterpreterBridge(const void* entry_point) const { |
| 5896 | return (entry_point == GetQuickToInterpreterBridge()) || |
| 5897 | (quick_to_interpreter_bridge_trampoline_ == entry_point); |
| 5898 | } |
| 5899 | |
| 5900 | bool ClassLinker::IsQuickGenericJniStub(const void* entry_point) const { |
| 5901 | return (entry_point == GetQuickGenericJniStub()) || |
| 5902 | (quick_generic_jni_trampoline_ == entry_point); |
| 5903 | } |
| 5904 | |
| 5905 | const void* ClassLinker::GetRuntimeQuickGenericJniStub() const { |
| 5906 | return GetQuickGenericJniStub(); |
| 5907 | } |
| 5908 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5909 | void ClassLinker::SetEntryPointsToCompiledCode(ArtMethod* method, |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 5910 | const void* method_code) const { |
| 5911 | OatFile::OatMethod oat_method = CreateOatMethod(method_code); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5912 | oat_method.LinkMethod(method); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5913 | } |
| 5914 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5915 | void ClassLinker::SetEntryPointsToInterpreter(ArtMethod* method) const { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5916 | if (!method->IsNative()) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5917 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 5918 | } else { |
| 5919 | const void* quick_method_code = GetQuickGenericJniStub(); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 5920 | OatFile::OatMethod oat_method = CreateOatMethod(quick_method_code); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5921 | oat_method.LinkMethod(method); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5922 | } |
| 5923 | } |
| 5924 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5925 | void ClassLinker::DumpForSigQuit(std::ostream& os) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5926 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5927 | if (dex_cache_image_class_lookup_required_) { |
| 5928 | MoveImageClassesToClassTable(); |
| 5929 | } |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5930 | ReaderMutexLock mu(soa.Self(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5931 | os << "Zygote loaded classes=" << NumZygoteClasses() << " post zygote classes=" |
| 5932 | << NumNonZygoteClasses() << "\n"; |
| 5933 | } |
| 5934 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame^] | 5935 | class CountClassesVisitor : public ClassLoaderVisitor { |
| 5936 | public: |
| 5937 | CountClassesVisitor() : num_zygote_classes(0), num_non_zygote_classes(0) {} |
| 5938 | |
| 5939 | void Visit(mirror::ClassLoader* class_loader) |
| 5940 | SHARED_REQUIRES(Locks::classlinker_classes_lock_, Locks::mutator_lock_) OVERRIDE { |
| 5941 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5942 | if (class_table != nullptr) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame^] | 5943 | num_zygote_classes += class_table->NumZygoteClasses(); |
| 5944 | num_non_zygote_classes += class_table->NumNonZygoteClasses(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5945 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5946 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame^] | 5947 | |
| 5948 | size_t num_zygote_classes; |
| 5949 | size_t num_non_zygote_classes; |
| 5950 | }; |
| 5951 | |
| 5952 | size_t ClassLinker::NumZygoteClasses() const { |
| 5953 | CountClassesVisitor visitor; |
| 5954 | VisitClassLoaders(&visitor); |
| 5955 | return visitor.num_zygote_classes + boot_class_table_.NumZygoteClasses(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5956 | } |
| 5957 | |
| 5958 | size_t ClassLinker::NumNonZygoteClasses() const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame^] | 5959 | CountClassesVisitor visitor; |
| 5960 | VisitClassLoaders(&visitor); |
| 5961 | return visitor.num_non_zygote_classes + boot_class_table_.NumNonZygoteClasses(); |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 5962 | } |
| 5963 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5964 | size_t ClassLinker::NumLoadedClasses() { |
| 5965 | if (dex_cache_image_class_lookup_required_) { |
| 5966 | MoveImageClassesToClassTable(); |
| 5967 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 5968 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 5969 | // 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] | 5970 | return NumNonZygoteClasses(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 5971 | } |
| 5972 | |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 5973 | pid_t ClassLinker::GetClassesLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 5974 | return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 5975 | } |
| 5976 | |
| 5977 | pid_t ClassLinker::GetDexLockOwner() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 5978 | return dex_lock_.GetExclusiveOwnerTid(); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 5979 | } |
| 5980 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5981 | void ClassLinker::SetClassRoot(ClassRoot class_root, mirror::Class* klass) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5982 | DCHECK(!init_done_); |
| 5983 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5984 | DCHECK(klass != nullptr); |
| 5985 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5986 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 5987 | mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5988 | DCHECK(class_roots != nullptr); |
| 5989 | DCHECK(class_roots->Get(class_root) == nullptr); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 5990 | class_roots->Set<false>(class_root, klass); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5991 | } |
| 5992 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5993 | const char* ClassLinker::GetClassRootDescriptor(ClassRoot class_root) { |
| 5994 | static const char* class_roots_descriptors[] = { |
| 5995 | "Ljava/lang/Class;", |
| 5996 | "Ljava/lang/Object;", |
| 5997 | "[Ljava/lang/Class;", |
| 5998 | "[Ljava/lang/Object;", |
| 5999 | "Ljava/lang/String;", |
| 6000 | "Ljava/lang/DexCache;", |
| 6001 | "Ljava/lang/ref/Reference;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 6002 | "Ljava/lang/reflect/Constructor;", |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 6003 | "Ljava/lang/reflect/Field;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 6004 | "Ljava/lang/reflect/Method;", |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 6005 | "Ljava/lang/reflect/Proxy;", |
| 6006 | "[Ljava/lang/String;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 6007 | "[Ljava/lang/reflect/Constructor;", |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 6008 | "[Ljava/lang/reflect/Field;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 6009 | "[Ljava/lang/reflect/Method;", |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 6010 | "Ljava/lang/ClassLoader;", |
| 6011 | "Ljava/lang/Throwable;", |
| 6012 | "Ljava/lang/ClassNotFoundException;", |
| 6013 | "Ljava/lang/StackTraceElement;", |
| 6014 | "Z", |
| 6015 | "B", |
| 6016 | "C", |
| 6017 | "D", |
| 6018 | "F", |
| 6019 | "I", |
| 6020 | "J", |
| 6021 | "S", |
| 6022 | "V", |
| 6023 | "[Z", |
| 6024 | "[B", |
| 6025 | "[C", |
| 6026 | "[D", |
| 6027 | "[F", |
| 6028 | "[I", |
| 6029 | "[J", |
| 6030 | "[S", |
| 6031 | "[Ljava/lang/StackTraceElement;", |
| 6032 | }; |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 6033 | static_assert(arraysize(class_roots_descriptors) == size_t(kClassRootsMax), |
| 6034 | "Mismatch between class descriptors and class-root enum"); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 6035 | |
| 6036 | const char* descriptor = class_roots_descriptors[class_root]; |
| 6037 | CHECK(descriptor != nullptr); |
| 6038 | return descriptor; |
| 6039 | } |
| 6040 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6041 | bool ClassLinker::MayBeCalledWithDirectCodePointer(ArtMethod* m) { |
Mathieu Chartier | d856545 | 2015-03-26 09:41:50 -0700 | [diff] [blame] | 6042 | if (Runtime::Current()->UseJit()) { |
| 6043 | // JIT can have direct code pointers from any method to any other method. |
| 6044 | return true; |
| 6045 | } |
Sebastien Hertz | 6963e44 | 2014-11-26 22:11:27 +0100 | [diff] [blame] | 6046 | // Non-image methods don't use direct code pointer. |
| 6047 | if (!m->GetDeclaringClass()->IsBootStrapClassLoaded()) { |
| 6048 | return false; |
| 6049 | } |
| 6050 | if (m->IsPrivate()) { |
| 6051 | // The method can only be called inside its own oat file. Therefore it won't be called using |
| 6052 | // its direct code if the oat file has been compiled in PIC mode. |
Sebastien Hertz | 6963e44 | 2014-11-26 22:11:27 +0100 | [diff] [blame] | 6053 | const DexFile& dex_file = m->GetDeclaringClass()->GetDexFile(); |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 6054 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
Sebastien Hertz | 6963e44 | 2014-11-26 22:11:27 +0100 | [diff] [blame] | 6055 | if (oat_dex_file == nullptr) { |
| 6056 | // No oat file: the method has not been compiled. |
| 6057 | return false; |
| 6058 | } |
| 6059 | const OatFile* oat_file = oat_dex_file->GetOatFile(); |
| 6060 | return oat_file != nullptr && !oat_file->IsPic(); |
| 6061 | } else { |
| 6062 | // The method can be called outside its own oat file. Therefore it won't be called using its |
| 6063 | // direct code pointer only if all loaded oat files have been compiled in PIC mode. |
| 6064 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
| 6065 | for (const OatFile* oat_file : oat_files_) { |
| 6066 | if (!oat_file->IsPic()) { |
| 6067 | return true; |
| 6068 | } |
| 6069 | } |
| 6070 | return false; |
| 6071 | } |
| 6072 | } |
| 6073 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 6074 | jobject ClassLinker::CreatePathClassLoader(Thread* self, std::vector<const DexFile*>& dex_files) { |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 6075 | // SOAAlreadyRunnable is protected, and we need something to add a global reference. |
| 6076 | // We could move the jobject to the callers, but all call-sites do this... |
| 6077 | ScopedObjectAccessUnchecked soa(self); |
| 6078 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 6079 | // For now, create a libcore-level DexFile for each ART DexFile. This "explodes" multidex. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 6080 | StackHandleScope<10> hs(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 6081 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 6082 | ArtField* dex_elements_field = |
| 6083 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList_dexElements); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 6084 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 6085 | mirror::Class* dex_elements_class = dex_elements_field->GetType<true>(); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 6086 | DCHECK(dex_elements_class != nullptr); |
| 6087 | DCHECK(dex_elements_class->IsArrayClass()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 6088 | Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements(hs.NewHandle( |
| 6089 | mirror::ObjectArray<mirror::Object>::Alloc(self, dex_elements_class, dex_files.size()))); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 6090 | Handle<mirror::Class> h_dex_element_class = |
| 6091 | hs.NewHandle(dex_elements_class->GetComponentType()); |
| 6092 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 6093 | ArtField* element_file_field = |
| 6094 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
| 6095 | DCHECK_EQ(h_dex_element_class.Get(), element_file_field->GetDeclaringClass()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 6096 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 6097 | ArtField* cookie_field = soa.DecodeField(WellKnownClasses::dalvik_system_DexFile_cookie); |
| 6098 | DCHECK_EQ(cookie_field->GetDeclaringClass(), element_file_field->GetType<false>()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 6099 | |
| 6100 | // Fill the elements array. |
| 6101 | int32_t index = 0; |
| 6102 | for (const DexFile* dex_file : dex_files) { |
| 6103 | StackHandleScope<3> hs2(self); |
| 6104 | |
| 6105 | Handle<mirror::LongArray> h_long_array = hs2.NewHandle(mirror::LongArray::Alloc(self, 1)); |
| 6106 | DCHECK(h_long_array.Get() != nullptr); |
| 6107 | h_long_array->Set(0, reinterpret_cast<intptr_t>(dex_file)); |
| 6108 | |
| 6109 | Handle<mirror::Object> h_dex_file = hs2.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 6110 | cookie_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 6111 | DCHECK(h_dex_file.Get() != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 6112 | cookie_field->SetObject<false>(h_dex_file.Get(), h_long_array.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 6113 | |
| 6114 | Handle<mirror::Object> h_element = hs2.NewHandle(h_dex_element_class->AllocObject(self)); |
| 6115 | DCHECK(h_element.Get() != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 6116 | element_file_field->SetObject<false>(h_element.Get(), h_dex_file.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 6117 | |
| 6118 | h_dex_elements->Set(index, h_element.Get()); |
| 6119 | index++; |
| 6120 | } |
| 6121 | DCHECK_EQ(index, h_dex_elements->GetLength()); |
| 6122 | |
| 6123 | // Create DexPathList. |
| 6124 | Handle<mirror::Object> h_dex_path_list = hs.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 6125 | dex_elements_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 6126 | DCHECK(h_dex_path_list.Get() != nullptr); |
| 6127 | // Set elements. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 6128 | 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] | 6129 | |
| 6130 | // Create PathClassLoader. |
| 6131 | Handle<mirror::Class> h_path_class_class = hs.NewHandle( |
| 6132 | soa.Decode<mirror::Class*>(WellKnownClasses::dalvik_system_PathClassLoader)); |
| 6133 | Handle<mirror::Object> h_path_class_loader = hs.NewHandle( |
| 6134 | h_path_class_class->AllocObject(self)); |
| 6135 | DCHECK(h_path_class_loader.Get() != nullptr); |
| 6136 | // Set DexPathList. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 6137 | ArtField* path_list_field = |
| 6138 | soa.DecodeField(WellKnownClasses::dalvik_system_PathClassLoader_pathList); |
| 6139 | DCHECK(path_list_field != nullptr); |
| 6140 | path_list_field->SetObject<false>(h_path_class_loader.Get(), h_dex_path_list.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 6141 | |
| 6142 | // Make a pretend boot-classpath. |
| 6143 | // TODO: Should we scan the image? |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 6144 | ArtField* const parent_field = |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 6145 | mirror::Class::FindField(self, hs.NewHandle(h_path_class_loader->GetClass()), "parent", |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 6146 | "Ljava/lang/ClassLoader;"); |
Roland Levillain | f39c9eb | 2015-05-26 15:02:07 +0100 | [diff] [blame] | 6147 | DCHECK(parent_field != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 6148 | mirror::Object* boot_cl = |
| 6149 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_BootClassLoader)->AllocObject(self); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 6150 | parent_field->SetObject<false>(h_path_class_loader.Get(), boot_cl); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 6151 | |
| 6152 | // Make it a global ref and return. |
| 6153 | ScopedLocalRef<jobject> local_ref( |
| 6154 | soa.Env(), soa.Env()->AddLocalReference<jobject>(h_path_class_loader.Get())); |
| 6155 | return soa.Env()->NewGlobalRef(local_ref.get()); |
| 6156 | } |
| 6157 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6158 | ArtMethod* ClassLinker::CreateRuntimeMethod() { |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 6159 | const size_t method_alignment = ArtMethod::Alignment(image_pointer_size_); |
| 6160 | const size_t method_size = ArtMethod::Size(image_pointer_size_); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 6161 | LengthPrefixedArray<ArtMethod>* method_array = AllocArtMethodArray(Thread::Current(), 1); |
| 6162 | ArtMethod* method = &method_array->At(0, method_size, method_alignment); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6163 | CHECK(method != nullptr); |
| 6164 | method->SetDexMethodIndex(DexFile::kDexNoIndex); |
| 6165 | CHECK(method->IsRuntimeMethod()); |
| 6166 | return method; |
| 6167 | } |
| 6168 | |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 6169 | void ClassLinker::DropFindArrayClassCache() { |
| 6170 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
| 6171 | find_array_class_cache_next_victim_ = 0; |
| 6172 | } |
| 6173 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame^] | 6174 | void ClassLinker::VisitClassLoadersAndRemoveClearedLoaders(ClassLoaderVisitor* visitor) { |
| 6175 | Thread* const self = Thread::Current(); |
| 6176 | Locks::classlinker_classes_lock_->AssertExclusiveHeld(self); |
| 6177 | JavaVMExt* const vm = self->GetJniEnv()->vm; |
| 6178 | for (auto it = class_loaders_.begin(); it != class_loaders_.end();) { |
| 6179 | const jweak weak_root = *it; |
| 6180 | mirror::ClassLoader* const class_loader = down_cast<mirror::ClassLoader*>( |
| 6181 | vm->DecodeWeakGlobal(self, weak_root)); |
| 6182 | if (class_loader != nullptr) { |
| 6183 | visitor->Visit(class_loader); |
| 6184 | ++it; |
| 6185 | } else { |
| 6186 | // Remove the cleared weak reference from the array. |
| 6187 | vm->DeleteWeakGlobalRef(self, weak_root); |
| 6188 | it = class_loaders_.erase(it); |
| 6189 | } |
| 6190 | } |
| 6191 | } |
| 6192 | |
| 6193 | void ClassLinker::VisitClassLoaders(ClassLoaderVisitor* visitor) const { |
| 6194 | Thread* const self = Thread::Current(); |
| 6195 | JavaVMExt* const vm = self->GetJniEnv()->vm; |
| 6196 | for (jweak weak_root : class_loaders_) { |
| 6197 | mirror::ClassLoader* const class_loader = down_cast<mirror::ClassLoader*>( |
| 6198 | vm->DecodeWeakGlobal(self, weak_root)); |
| 6199 | if (class_loader != nullptr) { |
| 6200 | visitor->Visit(class_loader); |
| 6201 | } |
| 6202 | } |
| 6203 | } |
| 6204 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6205 | } // namespace art |