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 | |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 28 | #include "base/casts.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 29 | #include "base/logging.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 30 | #include "base/scoped_flock.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 31 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 32 | #include "base/unix_file/fd_file.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 33 | #include "class_linker-inl.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 34 | #include "compiler_callbacks.h" |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 35 | #include "debugger.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 36 | #include "dex_file-inl.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 37 | #include "entrypoints/runtime_asm_entrypoints.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 38 | #include "gc_root-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 39 | #include "gc/accounting/card_table-inl.h" |
| 40 | #include "gc/accounting/heap_bitmap.h" |
| 41 | #include "gc/heap.h" |
| 42 | #include "gc/space/image_space.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 43 | #include "handle_scope.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 44 | #include "intern_table.h" |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 45 | #include "interpreter/interpreter.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 46 | #include "jit/jit.h" |
| 47 | #include "jit/jit_code_cache.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 48 | #include "leb128.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 49 | #include "oat.h" |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 50 | #include "oat_file.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 51 | #include "oat_file_assistant.h" |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 52 | #include "object_lock.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 53 | #include "mirror/art_field-inl.h" |
| 54 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 55 | #include "mirror/class.h" |
| 56 | #include "mirror/class-inl.h" |
| 57 | #include "mirror/class_loader.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 58 | #include "mirror/dex_cache-inl.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 59 | #include "mirror/field.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 60 | #include "mirror/iftable-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 61 | #include "mirror/object-inl.h" |
| 62 | #include "mirror/object_array-inl.h" |
| 63 | #include "mirror/proxy.h" |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 64 | #include "mirror/reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 65 | #include "mirror/stack_trace_element.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 66 | #include "mirror/string-inl.h" |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 67 | #include "os.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 68 | #include "runtime.h" |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 69 | #include "entrypoints/entrypoint_utils.h" |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 70 | #include "ScopedLocalRef.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 71 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 72 | #include "handle_scope-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 73 | #include "thread-inl.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 74 | #include "utils.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 75 | #include "verifier/method_verifier.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 76 | #include "well_known_classes.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 77 | |
| 78 | namespace art { |
| 79 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 80 | static void ThrowNoClassDefFoundError(const char* fmt, ...) |
| 81 | __attribute__((__format__(__printf__, 1, 2))) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 82 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 83 | static void ThrowNoClassDefFoundError(const char* fmt, ...) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 84 | va_list args; |
| 85 | va_start(args, fmt); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 86 | Thread* self = Thread::Current(); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 87 | self->ThrowNewExceptionV("Ljava/lang/NoClassDefFoundError;", fmt, args); |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 88 | va_end(args); |
| 89 | } |
| 90 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 91 | static void ThrowEarlierClassFailure(mirror::Class* c) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 92 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 93 | // The class failed to initialize on a previous attempt, so we want to throw |
| 94 | // a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we |
| 95 | // failed in verification, in which case v2 5.4.1 says we need to re-throw |
| 96 | // the previous error. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 97 | Runtime* const runtime = Runtime::Current(); |
| 98 | if (!runtime->IsAotCompiler()) { // Give info if this occurs at runtime. |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 99 | LOG(INFO) << "Rejecting re-init on previously-failed class " << PrettyClass(c); |
| 100 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 101 | |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 102 | CHECK(c->IsErroneous()) << PrettyClass(c) << " " << c->GetStatus(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 103 | Thread* self = Thread::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 104 | if (runtime->IsAotCompiler()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 105 | // At compile time, accurate errors and NCDFE are disabled to speed compilation. |
| 106 | mirror::Throwable* pre_allocated = runtime->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 107 | self->SetException(pre_allocated); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 108 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 109 | if (c->GetVerifyErrorClass() != NULL) { |
| 110 | // TODO: change the verifier to store an _instance_, with a useful detail message? |
| 111 | std::string temp; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 112 | self->ThrowNewException(c->GetVerifyErrorClass()->GetDescriptor(&temp), |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 113 | PrettyDescriptor(c).c_str()); |
| 114 | } else { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 115 | self->ThrowNewException("Ljava/lang/NoClassDefFoundError;", |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 116 | PrettyDescriptor(c).c_str()); |
| 117 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 121 | static void VlogClassInitializationFailure(Handle<mirror::Class> klass) |
| 122 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 123 | if (VLOG_IS_ON(class_linker)) { |
| 124 | std::string temp; |
| 125 | LOG(INFO) << "Failed to initialize class " << klass->GetDescriptor(&temp) << " from " |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 126 | << klass->GetLocation() << "\n" << Thread::Current()->GetException()->Dump(); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | static void WrapExceptionInInitializer(Handle<mirror::Class> klass) |
| 131 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 132 | Thread* self = Thread::Current(); |
| 133 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 134 | |
| 135 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 136 | CHECK(cause.get() != nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 137 | |
| 138 | env->ExceptionClear(); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 139 | bool is_error = env->IsInstanceOf(cause.get(), WellKnownClasses::java_lang_Error); |
| 140 | env->Throw(cause.get()); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 141 | |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 142 | // We only wrap non-Error exceptions; an Error can just be used as-is. |
| 143 | if (!is_error) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 144 | self->ThrowNewWrappedException("Ljava/lang/ExceptionInInitializerError;", nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 145 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 146 | VlogClassInitializationFailure(klass); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 149 | // Gap between two fields in object layout. |
| 150 | struct FieldGap { |
| 151 | uint32_t start_offset; // The offset from the start of the object. |
| 152 | uint32_t size; // The gap size of 1, 2, or 4 bytes. |
| 153 | }; |
| 154 | struct FieldGapsComparator { |
| 155 | explicit FieldGapsComparator() { |
| 156 | } |
| 157 | bool operator() (const FieldGap& lhs, const FieldGap& rhs) |
| 158 | NO_THREAD_SAFETY_ANALYSIS { |
Andreas Gampe | f52857f | 2015-02-18 15:38:57 -0800 | [diff] [blame] | 159 | // Sort by gap size, largest first. Secondary sort by starting offset. |
| 160 | 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] | 161 | } |
| 162 | }; |
| 163 | typedef std::priority_queue<FieldGap, std::vector<FieldGap>, FieldGapsComparator> FieldGaps; |
| 164 | |
| 165 | // Adds largest aligned gaps to queue of gaps. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 166 | 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] | 167 | DCHECK(gaps != nullptr); |
| 168 | |
| 169 | uint32_t current_offset = gap_start; |
| 170 | while (current_offset != gap_end) { |
| 171 | size_t remaining = gap_end - current_offset; |
| 172 | if (remaining >= sizeof(uint32_t) && IsAligned<4>(current_offset)) { |
| 173 | gaps->push(FieldGap {current_offset, sizeof(uint32_t)}); |
| 174 | current_offset += sizeof(uint32_t); |
| 175 | } else if (remaining >= sizeof(uint16_t) && IsAligned<2>(current_offset)) { |
| 176 | gaps->push(FieldGap {current_offset, sizeof(uint16_t)}); |
| 177 | current_offset += sizeof(uint16_t); |
| 178 | } else { |
| 179 | gaps->push(FieldGap {current_offset, sizeof(uint8_t)}); |
| 180 | current_offset += sizeof(uint8_t); |
| 181 | } |
| 182 | DCHECK_LE(current_offset, gap_end) << "Overran gap"; |
| 183 | } |
| 184 | } |
| 185 | // Shuffle fields forward, making use of gaps whenever possible. |
| 186 | template<int n> |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 187 | static void ShuffleForward(size_t* current_field_idx, |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 188 | MemberOffset* field_offset, |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 189 | std::deque<mirror::ArtField*>* grouped_and_sorted_fields, |
| 190 | FieldGaps* gaps) |
| 191 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 192 | DCHECK(current_field_idx != nullptr); |
| 193 | DCHECK(grouped_and_sorted_fields != nullptr); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 194 | DCHECK(gaps != nullptr); |
| 195 | DCHECK(field_offset != nullptr); |
| 196 | |
| 197 | DCHECK(IsPowerOfTwo(n)); |
| 198 | while (!grouped_and_sorted_fields->empty()) { |
| 199 | mirror::ArtField* field = grouped_and_sorted_fields->front(); |
| 200 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 201 | if (Primitive::ComponentSize(type) < n) { |
| 202 | break; |
| 203 | } |
| 204 | if (!IsAligned<n>(field_offset->Uint32Value())) { |
| 205 | MemberOffset old_offset = *field_offset; |
| 206 | *field_offset = MemberOffset(RoundUp(field_offset->Uint32Value(), n)); |
| 207 | AddFieldGap(old_offset.Uint32Value(), field_offset->Uint32Value(), gaps); |
| 208 | } |
| 209 | CHECK(type != Primitive::kPrimNot) << PrettyField(field); // should be primitive types |
| 210 | grouped_and_sorted_fields->pop_front(); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 211 | if (!gaps->empty() && gaps->top().size >= n) { |
| 212 | FieldGap gap = gaps->top(); |
| 213 | gaps->pop(); |
| 214 | DCHECK(IsAligned<n>(gap.start_offset)); |
| 215 | field->SetOffset(MemberOffset(gap.start_offset)); |
| 216 | if (gap.size > n) { |
| 217 | AddFieldGap(gap.start_offset + n, gap.start_offset + gap.size, gaps); |
| 218 | } |
| 219 | } else { |
| 220 | DCHECK(IsAligned<n>(field_offset->Uint32Value())); |
| 221 | field->SetOffset(*field_offset); |
| 222 | *field_offset = MemberOffset(field_offset->Uint32Value() + n); |
| 223 | } |
| 224 | ++(*current_field_idx); |
| 225 | } |
| 226 | } |
| 227 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 228 | ClassLinker::ClassLinker(InternTable* intern_table) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 229 | // dex_lock_ is recursive as it may be used in stack dumping. |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 230 | : dex_lock_("ClassLinker dex lock", kDefaultMutexLevel), |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 231 | dex_cache_image_class_lookup_required_(false), |
| 232 | failed_dex_cache_class_lookups_(0), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 233 | class_roots_(nullptr), |
| 234 | array_iftable_(nullptr), |
| 235 | find_array_class_cache_next_victim_(0), |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 236 | init_done_(false), |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 237 | log_new_dex_caches_roots_(false), |
| 238 | log_new_class_table_roots_(false), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 239 | intern_table_(intern_table), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 240 | quick_resolution_trampoline_(nullptr), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 241 | quick_imt_conflict_trampoline_(nullptr), |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 242 | quick_generic_jni_trampoline_(nullptr), |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 243 | quick_to_interpreter_bridge_trampoline_(nullptr), |
| 244 | image_pointer_size_(sizeof(void*)) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 245 | CHECK(intern_table_ != nullptr); |
| 246 | for (size_t i = 0; i < kFindArrayCacheSize; ++i) { |
| 247 | find_array_class_cache_[i] = GcRoot<mirror::Class>(nullptr); |
| 248 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 249 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 250 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 251 | 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] | 252 | VLOG(startup) << "ClassLinker::Init"; |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 253 | CHECK(!Runtime::Current()->GetHeap()->HasImageSpace()) << "Runtime has image. We should use it."; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 254 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 255 | CHECK(!init_done_); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 256 | |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 257 | // java_lang_Class comes first, it's needed for AllocClass |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 258 | Thread* self = Thread::Current(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 259 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 260 | // 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] | 261 | heap->IncrementDisableMovingGC(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 262 | StackHandleScope<64> hs(self); // 64 is picked arbitrarily. |
| 263 | Handle<mirror::Class> java_lang_Class(hs.NewHandle(down_cast<mirror::Class*>( |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 264 | heap->AllocNonMovableObject<true>(self, nullptr, |
| 265 | mirror::Class::ClassClassSize(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 266 | VoidFunctor())))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 267 | CHECK(java_lang_Class.Get() != nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 268 | mirror::Class::SetClassClass(java_lang_Class.Get()); |
| 269 | java_lang_Class->SetClass(java_lang_Class.Get()); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 270 | if (kUseBakerOrBrooksReadBarrier) { |
| 271 | java_lang_Class->AssertReadBarrierPointer(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 272 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 273 | java_lang_Class->SetClassSize(mirror::Class::ClassClassSize()); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 274 | java_lang_Class->SetPrimitiveType(Primitive::kPrimNot); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 275 | heap->DecrementDisableMovingGC(self); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 276 | // AllocClass(mirror::Class*) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 277 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 278 | // Class[] is used for reflection support. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 279 | Handle<mirror::Class> class_array_class(hs.NewHandle( |
| 280 | AllocClass(self, java_lang_Class.Get(), mirror::ObjectArray<mirror::Class>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 281 | class_array_class->SetComponentType(java_lang_Class.Get()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 282 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 283 | // 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] | 284 | Handle<mirror::Class> java_lang_Object(hs.NewHandle( |
| 285 | AllocClass(self, java_lang_Class.Get(), mirror::Object::ClassSize()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 286 | CHECK(java_lang_Object.Get() != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 287 | // backfill Object as the super class of Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 288 | java_lang_Class->SetSuperClass(java_lang_Object.Get()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 289 | mirror::Class::SetStatus(java_lang_Object, mirror::Class::kStatusLoaded, self); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 290 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 291 | // Object[] next to hold class roots. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 292 | Handle<mirror::Class> object_array_class(hs.NewHandle( |
| 293 | AllocClass(self, java_lang_Class.Get(), mirror::ObjectArray<mirror::Object>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 294 | object_array_class->SetComponentType(java_lang_Object.Get()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 295 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 296 | // Setup the char (primitive) class to be used for char[]. |
| 297 | Handle<mirror::Class> char_class(hs.NewHandle( |
| 298 | AllocClass(self, java_lang_Class.Get(), mirror::Class::PrimitiveClassSize()))); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 299 | // The primitive char class won't be initialized by |
| 300 | // InitializePrimitiveClass until line 459, but strings (and |
| 301 | // internal char arrays) will be allocated before that and the |
| 302 | // component size, which is computed from the primitive type, needs |
| 303 | // to be set here. |
| 304 | char_class->SetPrimitiveType(Primitive::kPrimChar); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 305 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 306 | // Setup the char[] class to be used for String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 307 | Handle<mirror::Class> char_array_class(hs.NewHandle( |
| 308 | AllocClass(self, java_lang_Class.Get(), |
| 309 | mirror::Array::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 310 | char_array_class->SetComponentType(char_class.Get()); |
| 311 | mirror::CharArray::SetArrayClass(char_array_class.Get()); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 312 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 313 | // Setup String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 314 | Handle<mirror::Class> java_lang_String(hs.NewHandle( |
| 315 | AllocClass(self, java_lang_Class.Get(), mirror::String::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 316 | mirror::String::SetClass(java_lang_String.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 317 | java_lang_String->SetObjectSize(mirror::String::InstanceSize()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 318 | mirror::Class::SetStatus(java_lang_String, mirror::Class::kStatusResolved, self); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 319 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 320 | // Setup java.lang.ref.Reference. |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 321 | Handle<mirror::Class> java_lang_ref_Reference(hs.NewHandle( |
| 322 | AllocClass(self, java_lang_Class.Get(), mirror::Reference::ClassSize()))); |
| 323 | mirror::Reference::SetClass(java_lang_ref_Reference.Get()); |
| 324 | java_lang_ref_Reference->SetObjectSize(mirror::Reference::InstanceSize()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 325 | mirror::Class::SetStatus(java_lang_ref_Reference, mirror::Class::kStatusResolved, self); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 326 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 327 | // 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] | 328 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 329 | mirror::ObjectArray<mirror::Class>::Alloc(self, object_array_class.Get(), |
| 330 | kClassRootsMax)); |
| 331 | CHECK(!class_roots_.IsNull()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 332 | SetClassRoot(kJavaLangClass, java_lang_Class.Get()); |
| 333 | SetClassRoot(kJavaLangObject, java_lang_Object.Get()); |
| 334 | SetClassRoot(kClassArrayClass, class_array_class.Get()); |
| 335 | SetClassRoot(kObjectArrayClass, object_array_class.Get()); |
| 336 | SetClassRoot(kCharArrayClass, char_array_class.Get()); |
| 337 | SetClassRoot(kJavaLangString, java_lang_String.Get()); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 338 | SetClassRoot(kJavaLangRefReference, java_lang_ref_Reference.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 339 | |
| 340 | // Setup the primitive type classes. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 341 | SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass(self, Primitive::kPrimBoolean)); |
| 342 | SetClassRoot(kPrimitiveByte, CreatePrimitiveClass(self, Primitive::kPrimByte)); |
| 343 | SetClassRoot(kPrimitiveShort, CreatePrimitiveClass(self, Primitive::kPrimShort)); |
| 344 | SetClassRoot(kPrimitiveInt, CreatePrimitiveClass(self, Primitive::kPrimInt)); |
| 345 | SetClassRoot(kPrimitiveLong, CreatePrimitiveClass(self, Primitive::kPrimLong)); |
| 346 | SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass(self, Primitive::kPrimFloat)); |
| 347 | SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass(self, Primitive::kPrimDouble)); |
| 348 | SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass(self, Primitive::kPrimVoid)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 349 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 350 | // Create array interface entries to populate once we can load system classes. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 351 | array_iftable_ = GcRoot<mirror::IfTable>(AllocIfTable(self, 2)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 352 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 353 | // Create int array type for AllocDexCache (done in AppendToBootClassPath). |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 354 | Handle<mirror::Class> int_array_class(hs.NewHandle( |
| 355 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize()))); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 356 | int_array_class->SetComponentType(GetClassRoot(kPrimitiveInt)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 357 | mirror::IntArray::SetArrayClass(int_array_class.Get()); |
| 358 | SetClassRoot(kIntArrayClass, int_array_class.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 359 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 360 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 361 | |
Ian Rogers | 52813c9 | 2012-10-11 11:50:38 -0700 | [diff] [blame] | 362 | // Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 363 | Handle<mirror::Class> java_lang_DexCache(hs.NewHandle( |
| 364 | AllocClass(self, java_lang_Class.Get(), mirror::DexCache::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 365 | SetClassRoot(kJavaLangDexCache, java_lang_DexCache.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 366 | java_lang_DexCache->SetObjectSize(mirror::DexCache::InstanceSize()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 367 | mirror::Class::SetStatus(java_lang_DexCache, mirror::Class::kStatusResolved, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 368 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 369 | // Constructor, Field, Method, and AbstractMethod are necessary so |
| 370 | // that FindClass can link members. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 371 | Handle<mirror::Class> java_lang_reflect_ArtField(hs.NewHandle( |
| 372 | AllocClass(self, java_lang_Class.Get(), mirror::ArtField::ClassSize()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 373 | CHECK(java_lang_reflect_ArtField.Get() != nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 374 | java_lang_reflect_ArtField->SetObjectSize(mirror::ArtField::InstanceSize()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 375 | SetClassRoot(kJavaLangReflectArtField, java_lang_reflect_ArtField.Get()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 376 | mirror::Class::SetStatus(java_lang_reflect_ArtField, mirror::Class::kStatusResolved, self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 377 | mirror::ArtField::SetClass(java_lang_reflect_ArtField.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 378 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 379 | Handle<mirror::Class> java_lang_reflect_ArtMethod(hs.NewHandle( |
| 380 | AllocClass(self, java_lang_Class.Get(), mirror::ArtMethod::ClassSize()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 381 | CHECK(java_lang_reflect_ArtMethod.Get() != nullptr); |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 382 | size_t pointer_size = GetInstructionSetPointerSize(Runtime::Current()->GetInstructionSet()); |
| 383 | java_lang_reflect_ArtMethod->SetObjectSize(mirror::ArtMethod::InstanceSize(pointer_size)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 384 | SetClassRoot(kJavaLangReflectArtMethod, java_lang_reflect_ArtMethod.Get()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 385 | mirror::Class::SetStatus(java_lang_reflect_ArtMethod, mirror::Class::kStatusResolved, self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 386 | mirror::ArtMethod::SetClass(java_lang_reflect_ArtMethod.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 387 | |
| 388 | // Set up array classes for string, field, method |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 389 | Handle<mirror::Class> object_array_string(hs.NewHandle( |
| 390 | AllocClass(self, java_lang_Class.Get(), |
| 391 | mirror::ObjectArray<mirror::String>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 392 | object_array_string->SetComponentType(java_lang_String.Get()); |
| 393 | SetClassRoot(kJavaLangStringArrayClass, object_array_string.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 394 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 395 | Handle<mirror::Class> object_array_art_method(hs.NewHandle( |
| 396 | AllocClass(self, java_lang_Class.Get(), |
| 397 | mirror::ObjectArray<mirror::ArtMethod>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 398 | object_array_art_method->SetComponentType(java_lang_reflect_ArtMethod.Get()); |
| 399 | SetClassRoot(kJavaLangReflectArtMethodArrayClass, object_array_art_method.Get()); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 400 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 401 | Handle<mirror::Class> object_array_art_field(hs.NewHandle( |
| 402 | AllocClass(self, java_lang_Class.Get(), |
| 403 | mirror::ObjectArray<mirror::ArtField>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 404 | object_array_art_field->SetComponentType(java_lang_reflect_ArtField.Get()); |
| 405 | SetClassRoot(kJavaLangReflectArtFieldArrayClass, object_array_art_field.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 406 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 407 | // Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create |
| 408 | // DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses |
| 409 | // these roots. |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 410 | CHECK_NE(0U, boot_class_path.size()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 411 | for (auto& dex_file : boot_class_path) { |
| 412 | CHECK(dex_file.get() != nullptr); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 413 | AppendToBootClassPath(self, *dex_file); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 414 | opened_dex_files_.push_back(std::move(dex_file)); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 415 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 416 | |
| 417 | // now we can use FindSystemClass |
| 418 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 419 | // run char class through InitializePrimitiveClass to finish init |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 420 | InitializePrimitiveClass(char_class.Get(), Primitive::kPrimChar); |
| 421 | SetClassRoot(kPrimitiveChar, char_class.Get()); // needs descriptor |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 422 | |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 423 | // Create runtime resolution and imt conflict methods. Also setup the default imt. |
| 424 | Runtime* runtime = Runtime::Current(); |
| 425 | runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); |
| 426 | runtime->SetImtConflictMethod(runtime->CreateImtConflictMethod()); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 427 | runtime->SetImtUnimplementedMethod(runtime->CreateImtConflictMethod()); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 428 | runtime->SetDefaultImt(runtime->CreateDefaultImt(this)); |
| 429 | |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 430 | // Set up GenericJNI entrypoint. That is mainly a hack for common_compiler_test.h so that |
| 431 | // we do not need friend classes or a publicly exposed setter. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 432 | quick_generic_jni_trampoline_ = GetQuickGenericJniStub(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 433 | if (!runtime->IsAotCompiler()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 434 | // 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] | 435 | quick_resolution_trampoline_ = GetQuickResolutionStub(); |
| 436 | quick_imt_conflict_trampoline_ = GetQuickImtConflictStub(); |
| 437 | quick_to_interpreter_bridge_trampoline_ = GetQuickToInterpreterBridge(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 438 | } |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 439 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 440 | // 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] | 441 | mirror::Class::SetStatus(java_lang_Object, mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 442 | mirror::Class* Object_class = FindSystemClass(self, "Ljava/lang/Object;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 443 | CHECK_EQ(java_lang_Object.Get(), Object_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 444 | CHECK_EQ(java_lang_Object->GetObjectSize(), mirror::Object::InstanceSize()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 445 | mirror::Class::SetStatus(java_lang_String, mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 446 | mirror::Class* String_class = FindSystemClass(self, "Ljava/lang/String;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 447 | std::ostringstream os1, os2; |
| 448 | java_lang_String->DumpClass(os1, mirror::Class::kDumpClassFullDetail); |
| 449 | String_class->DumpClass(os2, mirror::Class::kDumpClassFullDetail); |
| 450 | CHECK_EQ(java_lang_String.Get(), String_class) << os1.str() << "\n\n" << os2.str(); |
| 451 | CHECK_EQ(java_lang_String->GetObjectSize(), mirror::String::InstanceSize()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 452 | mirror::Class::SetStatus(java_lang_DexCache, mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 453 | mirror::Class* DexCache_class = FindSystemClass(self, "Ljava/lang/DexCache;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 454 | CHECK_EQ(java_lang_String.Get(), String_class); |
| 455 | CHECK_EQ(java_lang_DexCache.Get(), DexCache_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 456 | CHECK_EQ(java_lang_DexCache->GetObjectSize(), mirror::DexCache::InstanceSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 457 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 458 | // 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] | 459 | SetClassRoot(kBooleanArrayClass, FindSystemClass(self, "[Z")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 460 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 461 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 462 | SetClassRoot(kByteArrayClass, FindSystemClass(self, "[B")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 463 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 464 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 465 | mirror::Class* found_char_array_class = FindSystemClass(self, "[C"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 466 | CHECK_EQ(char_array_class.Get(), found_char_array_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 467 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 468 | SetClassRoot(kShortArrayClass, FindSystemClass(self, "[S")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 469 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 470 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 471 | mirror::Class* found_int_array_class = FindSystemClass(self, "[I"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 472 | CHECK_EQ(int_array_class.Get(), found_int_array_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 473 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 474 | SetClassRoot(kLongArrayClass, FindSystemClass(self, "[J")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 475 | mirror::LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 476 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 477 | SetClassRoot(kFloatArrayClass, FindSystemClass(self, "[F")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 478 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 479 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 480 | SetClassRoot(kDoubleArrayClass, FindSystemClass(self, "[D")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 481 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 482 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 483 | mirror::Class* found_class_array_class = FindSystemClass(self, "[Ljava/lang/Class;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 484 | CHECK_EQ(class_array_class.Get(), found_class_array_class); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 485 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 486 | mirror::Class* found_object_array_class = FindSystemClass(self, "[Ljava/lang/Object;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 487 | CHECK_EQ(object_array_class.Get(), found_object_array_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 488 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 489 | // Setup the single, global copy of "iftable". |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 490 | mirror::Class* java_lang_Cloneable = FindSystemClass(self, "Ljava/lang/Cloneable;"); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 491 | CHECK(java_lang_Cloneable != nullptr); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 492 | mirror::Class* java_io_Serializable = FindSystemClass(self, "Ljava/io/Serializable;"); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 493 | CHECK(java_io_Serializable != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 494 | // We assume that Cloneable/Serializable don't have superinterfaces -- normally we'd have to |
| 495 | // crawl up and explicitly list all of the supers as well. |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 496 | { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 497 | mirror::IfTable* array_iftable = array_iftable_.Read(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 498 | array_iftable->SetInterface(0, java_lang_Cloneable); |
| 499 | array_iftable->SetInterface(1, java_io_Serializable); |
| 500 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 501 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 502 | // Sanity check Class[] and Object[]'s interfaces. |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 503 | CHECK_EQ(java_lang_Cloneable, mirror::Class::GetDirectInterface(self, class_array_class, 0)); |
| 504 | CHECK_EQ(java_io_Serializable, mirror::Class::GetDirectInterface(self, class_array_class, 1)); |
| 505 | CHECK_EQ(java_lang_Cloneable, mirror::Class::GetDirectInterface(self, object_array_class, 0)); |
| 506 | CHECK_EQ(java_io_Serializable, mirror::Class::GetDirectInterface(self, object_array_class, 1)); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 507 | // Run Class, ArtField, and ArtMethod through FindSystemClass. This initializes their |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 508 | // dex_cache_ fields and register them in class_table_. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 509 | mirror::Class* Class_class = FindSystemClass(self, "Ljava/lang/Class;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 510 | CHECK_EQ(java_lang_Class.Get(), Class_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 511 | |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 512 | mirror::Class::SetStatus(java_lang_reflect_ArtMethod, mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 513 | mirror::Class* Art_method_class = FindSystemClass(self, "Ljava/lang/reflect/ArtMethod;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 514 | CHECK_EQ(java_lang_reflect_ArtMethod.Get(), Art_method_class); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 515 | |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 516 | mirror::Class::SetStatus(java_lang_reflect_ArtField, mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 517 | mirror::Class* Art_field_class = FindSystemClass(self, "Ljava/lang/reflect/ArtField;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 518 | CHECK_EQ(java_lang_reflect_ArtField.Get(), Art_field_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 519 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 520 | mirror::Class* String_array_class = |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 521 | FindSystemClass(self, GetClassRootDescriptor(kJavaLangStringArrayClass)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 522 | CHECK_EQ(object_array_string.Get(), String_array_class); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 523 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 524 | mirror::Class* Art_method_array_class = |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 525 | FindSystemClass(self, GetClassRootDescriptor(kJavaLangReflectArtMethodArrayClass)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 526 | CHECK_EQ(object_array_art_method.Get(), Art_method_array_class); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 527 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 528 | mirror::Class* Art_field_array_class = |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 529 | FindSystemClass(self, GetClassRootDescriptor(kJavaLangReflectArtFieldArrayClass)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 530 | CHECK_EQ(object_array_art_field.Get(), Art_field_array_class); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 531 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 532 | // End of special init trickery, subsequent classes may be loaded via FindSystemClass. |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 533 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 534 | // Create java.lang.reflect.Proxy root. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 535 | mirror::Class* java_lang_reflect_Proxy = FindSystemClass(self, "Ljava/lang/reflect/Proxy;"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 536 | SetClassRoot(kJavaLangReflectProxy, java_lang_reflect_Proxy); |
| 537 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 538 | // Create java.lang.reflect.Field.class root. |
| 539 | mirror::Class* java_lang_reflect_Field = FindSystemClass(self, "Ljava/lang/reflect/Field;"); |
| 540 | CHECK(java_lang_reflect_Field != nullptr); |
| 541 | SetClassRoot(kJavaLangReflectField, java_lang_reflect_Field); |
| 542 | mirror::Field::SetClass(java_lang_reflect_Field); |
| 543 | |
| 544 | // Create java.lang.reflect.Field array root. |
| 545 | mirror::Class* java_lang_reflect_Field_array = |
| 546 | FindSystemClass(self, "[Ljava/lang/reflect/Field;"); |
| 547 | CHECK(java_lang_reflect_Field_array != nullptr); |
| 548 | SetClassRoot(kJavaLangReflectFieldArrayClass, java_lang_reflect_Field_array); |
| 549 | mirror::Field::SetArrayClass(java_lang_reflect_Field_array); |
| 550 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 551 | // 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] | 552 | // finish initializing Reference class |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 553 | mirror::Class::SetStatus(java_lang_ref_Reference, mirror::Class::kStatusNotReady, self); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 554 | mirror::Class* Reference_class = FindSystemClass(self, "Ljava/lang/ref/Reference;"); |
| 555 | CHECK_EQ(java_lang_ref_Reference.Get(), Reference_class); |
| 556 | CHECK_EQ(java_lang_ref_Reference->GetObjectSize(), mirror::Reference::InstanceSize()); |
| 557 | CHECK_EQ(java_lang_ref_Reference->GetClassSize(), mirror::Reference::ClassSize()); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 558 | mirror::Class* java_lang_ref_FinalizerReference = |
| 559 | FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 560 | java_lang_ref_FinalizerReference->SetAccessFlags( |
| 561 | java_lang_ref_FinalizerReference->GetAccessFlags() | |
| 562 | kAccClassIsReference | kAccClassIsFinalizerReference); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 563 | mirror::Class* java_lang_ref_PhantomReference = |
| 564 | FindSystemClass(self, "Ljava/lang/ref/PhantomReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 565 | java_lang_ref_PhantomReference->SetAccessFlags( |
| 566 | java_lang_ref_PhantomReference->GetAccessFlags() | |
| 567 | kAccClassIsReference | kAccClassIsPhantomReference); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 568 | mirror::Class* java_lang_ref_SoftReference = |
| 569 | FindSystemClass(self, "Ljava/lang/ref/SoftReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 570 | java_lang_ref_SoftReference->SetAccessFlags( |
| 571 | java_lang_ref_SoftReference->GetAccessFlags() | kAccClassIsReference); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 572 | mirror::Class* java_lang_ref_WeakReference = |
| 573 | FindSystemClass(self, "Ljava/lang/ref/WeakReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 574 | java_lang_ref_WeakReference->SetAccessFlags( |
| 575 | java_lang_ref_WeakReference->GetAccessFlags() | |
| 576 | kAccClassIsReference | kAccClassIsWeakReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 577 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 578 | // Setup the ClassLoader, verifying the object_size_. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 579 | mirror::Class* java_lang_ClassLoader = FindSystemClass(self, "Ljava/lang/ClassLoader;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 580 | CHECK_EQ(java_lang_ClassLoader->GetObjectSize(), mirror::ClassLoader::InstanceSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 581 | SetClassRoot(kJavaLangClassLoader, java_lang_ClassLoader); |
| 582 | |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 583 | // Set up java.lang.Throwable, java.lang.ClassNotFoundException, and |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 584 | // java.lang.StackTraceElement as a convenience. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 585 | SetClassRoot(kJavaLangThrowable, FindSystemClass(self, "Ljava/lang/Throwable;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 586 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 587 | SetClassRoot(kJavaLangClassNotFoundException, |
| 588 | FindSystemClass(self, "Ljava/lang/ClassNotFoundException;")); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 589 | SetClassRoot(kJavaLangStackTraceElement, FindSystemClass(self, "Ljava/lang/StackTraceElement;")); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 590 | SetClassRoot(kJavaLangStackTraceElementArrayClass, |
| 591 | FindSystemClass(self, "[Ljava/lang/StackTraceElement;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 592 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 593 | |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 594 | // Ensure void type is resolved in the core's dex cache so java.lang.Void is correctly |
| 595 | // initialized. |
| 596 | { |
| 597 | const DexFile& dex_file = java_lang_Object->GetDexFile(); |
| 598 | const DexFile::StringId* void_string_id = dex_file.FindStringId("V"); |
| 599 | CHECK(void_string_id != nullptr); |
| 600 | uint32_t void_string_index = dex_file.GetIndexForStringId(*void_string_id); |
| 601 | const DexFile::TypeId* void_type_id = dex_file.FindTypeId(void_string_index); |
| 602 | CHECK(void_type_id != nullptr); |
| 603 | uint16_t void_type_idx = dex_file.GetIndexForTypeId(*void_type_id); |
| 604 | // Now we resolve void type so the dex cache contains it. We use java.lang.Object class |
| 605 | // as referrer so the used dex cache is core's one. |
| 606 | mirror::Class* resolved_type = ResolveType(dex_file, void_type_idx, java_lang_Object.Get()); |
| 607 | CHECK_EQ(resolved_type, GetClassRoot(kPrimitiveVoid)); |
| 608 | self->AssertNoPendingException(); |
| 609 | } |
| 610 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 611 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 612 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 613 | VLOG(startup) << "ClassLinker::InitFromCompiler exiting"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 616 | void ClassLinker::FinishInit(Thread* self) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 617 | VLOG(startup) << "ClassLinker::FinishInit entering"; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 618 | |
| 619 | // Let the heap know some key offsets into java.lang.ref instances |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 620 | // Note: we hard code the field indexes here rather than using FindInstanceField |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 621 | // as the types of the field can't be resolved prior to the runtime being |
| 622 | // fully initialized |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 623 | mirror::Class* java_lang_ref_Reference = GetClassRoot(kJavaLangRefReference); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 624 | mirror::Class* java_lang_ref_FinalizerReference = |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 625 | FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 626 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 627 | mirror::ArtField* pendingNext = java_lang_ref_Reference->GetInstanceField(0); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 628 | CHECK_STREQ(pendingNext->GetName(), "pendingNext"); |
| 629 | CHECK_STREQ(pendingNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 630 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 631 | mirror::ArtField* queue = java_lang_ref_Reference->GetInstanceField(1); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 632 | CHECK_STREQ(queue->GetName(), "queue"); |
| 633 | CHECK_STREQ(queue->GetTypeDescriptor(), "Ljava/lang/ref/ReferenceQueue;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 634 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 635 | mirror::ArtField* queueNext = java_lang_ref_Reference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 636 | CHECK_STREQ(queueNext->GetName(), "queueNext"); |
| 637 | CHECK_STREQ(queueNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 638 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 639 | mirror::ArtField* referent = java_lang_ref_Reference->GetInstanceField(3); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 640 | CHECK_STREQ(referent->GetName(), "referent"); |
| 641 | CHECK_STREQ(referent->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 642 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 643 | mirror::ArtField* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 644 | CHECK_STREQ(zombie->GetName(), "zombie"); |
| 645 | CHECK_STREQ(zombie->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 646 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 647 | // ensure all class_roots_ are initialized |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 648 | for (size_t i = 0; i < kClassRootsMax; i++) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 649 | ClassRoot class_root = static_cast<ClassRoot>(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 650 | mirror::Class* klass = GetClassRoot(class_root); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 651 | CHECK(klass != nullptr); |
| 652 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != nullptr); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 653 | // note SetClassRoot does additional validation. |
| 654 | // if possible add new checks there to catch errors early |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 657 | CHECK(!array_iftable_.IsNull()); |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 658 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 659 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 660 | // that Object, Class, and Object[] are setup |
| 661 | init_done_ = true; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 662 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 663 | VLOG(startup) << "ClassLinker::FinishInit exiting"; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 666 | void ClassLinker::RunRootClinits() { |
| 667 | Thread* self = Thread::Current(); |
| 668 | for (size_t i = 0; i < ClassLinker::kClassRootsMax; ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 669 | mirror::Class* c = GetClassRoot(ClassRoot(i)); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 670 | if (!c->IsArrayClass() && !c->IsPrimitive()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 671 | StackHandleScope<1> hs(self); |
| 672 | Handle<mirror::Class> h_class(hs.NewHandle(GetClassRoot(ClassRoot(i)))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 673 | EnsureInitialized(self, h_class, true, true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 674 | self->AssertNoPendingException(); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | } |
| 678 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 679 | const OatFile* ClassLinker::RegisterOatFile(const OatFile* oat_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 680 | WriterMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 681 | if (kIsDebugBuild) { |
| 682 | for (size_t i = 0; i < oat_files_.size(); ++i) { |
| 683 | CHECK_NE(oat_file, oat_files_[i]) << oat_file->GetLocation(); |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 684 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 685 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 686 | VLOG(class_linker) << "Registering " << oat_file->GetLocation(); |
| 687 | oat_files_.push_back(oat_file); |
| 688 | return oat_file; |
Brian Carlstrom | 866c862 | 2012-01-06 16:35:13 -0800 | [diff] [blame] | 689 | } |
| 690 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 691 | OatFile& ClassLinker::GetImageOatFile(gc::space::ImageSpace* space) { |
| 692 | VLOG(startup) << "ClassLinker::GetImageOatFile entering"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 693 | OatFile* oat_file = space->ReleaseOatFile(); |
| 694 | CHECK_EQ(RegisterOatFile(oat_file), oat_file); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 695 | VLOG(startup) << "ClassLinker::GetImageOatFile exiting"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 696 | return *oat_file; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 697 | } |
| 698 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 699 | std::vector<std::unique_ptr<const DexFile>> ClassLinker::OpenDexFilesFromOat( |
| 700 | const char* dex_location, const char* oat_location, |
| 701 | std::vector<std::string>* error_msgs) { |
| 702 | CHECK(error_msgs != nullptr); |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 703 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 704 | // Verify we aren't holding the mutator lock, which could starve GC if we |
| 705 | // have to generate or relocate an oat file. |
| 706 | Locks::mutator_lock_->AssertNotHeld(Thread::Current()); |
| 707 | |
| 708 | OatFileAssistant oat_file_assistant(dex_location, oat_location, kRuntimeISA, |
| 709 | !Runtime::Current()->IsAotCompiler()); |
| 710 | |
| 711 | // Lock the target oat location to avoid races generating and loading the |
| 712 | // oat file. |
| 713 | std::string error_msg; |
| 714 | if (!oat_file_assistant.Lock(&error_msg)) { |
| 715 | // Don't worry too much if this fails. If it does fail, it's unlikely we |
| 716 | // can generate an oat file anyway. |
| 717 | VLOG(class_linker) << "OatFileAssistant::Lock: " << error_msg; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 718 | } |
| 719 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 720 | // Check if we already have an up-to-date oat file open. |
| 721 | const OatFile* source_oat_file = nullptr; |
| 722 | { |
| 723 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
| 724 | for (const OatFile* oat_file : oat_files_) { |
| 725 | CHECK(oat_file != nullptr); |
| 726 | if (oat_file_assistant.GivenOatFileIsUpToDate(*oat_file)) { |
| 727 | source_oat_file = oat_file; |
| 728 | break; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 729 | } |
| 730 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 733 | // If we didn't have an up-to-date oat file open, try to load one from disk. |
| 734 | if (source_oat_file == nullptr) { |
| 735 | // Update the oat file on disk if we can. This may fail, but that's okay. |
| 736 | // Best effort is all that matters here. |
| 737 | if (!oat_file_assistant.MakeUpToDate(&error_msg)) { |
| 738 | LOG(WARNING) << error_msg; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 739 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 740 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 741 | // Get the oat file on disk. |
| 742 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 743 | if (oat_file.get() != nullptr) { |
| 744 | source_oat_file = oat_file.release(); |
| 745 | RegisterOatFile(source_oat_file); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 749 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 750 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 751 | // Load the dex files from the oat file. |
| 752 | if (source_oat_file != nullptr) { |
| 753 | dex_files = oat_file_assistant.LoadDexFiles(*source_oat_file, dex_location); |
| 754 | if (dex_files.empty()) { |
| 755 | error_msgs->push_back("Failed to open dex files from " |
| 756 | + source_oat_file->GetLocation()); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 760 | // Fall back to running out of the original dex file if we couldn't load any |
| 761 | // dex_files from the oat file. |
| 762 | if (dex_files.empty()) { |
Jean Christophe Beyler | 24e04aa | 2014-09-12 12:03:25 -0700 | [diff] [blame] | 763 | if (Runtime::Current()->IsDexFileFallbackEnabled()) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 764 | if (!DexFile::Open(dex_location, dex_location, &error_msg, &dex_files)) { |
| 765 | LOG(WARNING) << error_msg; |
| 766 | error_msgs->push_back("Failed to open dex files from " |
| 767 | + std::string(dex_location)); |
Jean Christophe Beyler | 24e04aa | 2014-09-12 12:03:25 -0700 | [diff] [blame] | 768 | } |
| 769 | } else { |
| 770 | error_msgs->push_back("Fallback mode disabled, skipping dex files."); |
| 771 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 772 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 773 | return dex_files; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 776 | const OatFile* ClassLinker::FindOpenedOatFileFromOatLocation(const std::string& oat_location) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 777 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 778 | for (size_t i = 0; i < oat_files_.size(); i++) { |
| 779 | const OatFile* oat_file = oat_files_[i]; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 780 | DCHECK(oat_file != nullptr); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 781 | if (oat_file->GetLocation() == oat_location) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 782 | return oat_file; |
| 783 | } |
| 784 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 785 | return nullptr; |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 786 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 787 | |
Mathieu Chartier | a89d7ed | 2014-12-05 10:57:13 -0800 | [diff] [blame] | 788 | void ClassLinker::InitFromImageInterpretOnlyCallback(mirror::Object* obj, void* arg) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 789 | ClassLinker* class_linker = reinterpret_cast<ClassLinker*>(arg); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 790 | DCHECK(obj != nullptr); |
| 791 | DCHECK(class_linker != nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 792 | if (obj->IsArtMethod()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 793 | mirror::ArtMethod* method = obj->AsArtMethod(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 794 | if (!method->IsNative()) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 795 | const size_t pointer_size = class_linker->image_pointer_size_; |
Mathieu Chartier | a89d7ed | 2014-12-05 10:57:13 -0800 | [diff] [blame] | 796 | method->SetEntryPointFromInterpreterPtrSize(artInterpreterToInterpreterBridge, pointer_size); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 797 | if (!method->IsRuntimeMethod() && method != Runtime::Current()->GetResolutionMethod()) { |
Mathieu Chartier | a89d7ed | 2014-12-05 10:57:13 -0800 | [diff] [blame] | 798 | method->SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(), |
| 799 | pointer_size); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 805 | void ClassLinker::InitFromImage() { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 806 | VLOG(startup) << "ClassLinker::InitFromImage entering"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 807 | CHECK(!init_done_); |
| 808 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 809 | Runtime* const runtime = Runtime::Current(); |
| 810 | Thread* const self = Thread::Current(); |
| 811 | gc::Heap* const heap = runtime->GetHeap(); |
| 812 | gc::space::ImageSpace* const space = heap->GetImageSpace(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 813 | dex_cache_image_class_lookup_required_ = true; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 814 | CHECK(space != nullptr); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 815 | OatFile& oat_file = GetImageOatFile(space); |
| 816 | CHECK_EQ(oat_file.GetOatHeader().GetImageFileLocationOatChecksum(), 0U); |
| 817 | CHECK_EQ(oat_file.GetOatHeader().GetImageFileLocationOatDataBegin(), 0U); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 818 | const char* image_file_location = oat_file.GetOatHeader(). |
| 819 | GetStoreValueByKey(OatHeader::kImageLocationKey); |
| 820 | CHECK(image_file_location == nullptr || *image_file_location == 0); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 821 | quick_resolution_trampoline_ = oat_file.GetOatHeader().GetQuickResolutionTrampoline(); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 822 | quick_imt_conflict_trampoline_ = oat_file.GetOatHeader().GetQuickImtConflictTrampoline(); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 823 | quick_generic_jni_trampoline_ = oat_file.GetOatHeader().GetQuickGenericJniTrampoline(); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 824 | quick_to_interpreter_bridge_trampoline_ = oat_file.GetOatHeader().GetQuickToInterpreterBridge(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 825 | mirror::Object* dex_caches_object = space->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches); |
| 826 | mirror::ObjectArray<mirror::DexCache>* dex_caches = |
| 827 | dex_caches_object->AsObjectArray<mirror::DexCache>(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 828 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 829 | StackHandleScope<1> hs(self); |
| 830 | Handle<mirror::ObjectArray<mirror::Class>> class_roots(hs.NewHandle( |
| 831 | space->GetImageHeader().GetImageRoot(ImageHeader::kClassRoots)-> |
| 832 | AsObjectArray<mirror::Class>())); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 833 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>(class_roots.Get()); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 834 | |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 835 | // Special case of setting up the String class early so that we can test arbitrary objects |
| 836 | // as being Strings or not |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 837 | mirror::String::SetClass(GetClassRoot(kJavaLangString)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 838 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 839 | CHECK_EQ(oat_file.GetOatHeader().GetDexFileCount(), |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 840 | static_cast<uint32_t>(dex_caches->GetLength())); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 841 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 842 | StackHandleScope<1> hs2(self); |
| 843 | Handle<mirror::DexCache> dex_cache(hs2.NewHandle(dex_caches->Get(i))); |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 844 | const std::string& dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 845 | const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(dex_file_location.c_str(), |
| 846 | nullptr); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 847 | CHECK(oat_dex_file != nullptr) << oat_file.GetLocation() << " " << dex_file_location; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 848 | std::string error_msg; |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 849 | std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg); |
| 850 | if (dex_file.get() == nullptr) { |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 851 | LOG(FATAL) << "Failed to open dex file " << dex_file_location |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 852 | << " from within oat file " << oat_file.GetLocation() |
| 853 | << " error '" << error_msg << "'"; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 854 | UNREACHABLE(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 855 | } |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 856 | |
| 857 | CHECK_EQ(dex_file->GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum()); |
| 858 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 859 | AppendToBootClassPath(*dex_file.get(), dex_cache); |
| 860 | opened_dex_files_.push_back(std::move(dex_file)); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 861 | } |
| 862 | |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 863 | // Set classes on AbstractMethod early so that IsMethod tests can be performed during the live |
| 864 | // bitmap walk. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 865 | mirror::ArtMethod::SetClass(GetClassRoot(kJavaLangReflectArtMethod)); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 866 | size_t art_method_object_size = mirror::ArtMethod::GetJavaLangReflectArtMethod()->GetObjectSize(); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 867 | if (!runtime->IsAotCompiler()) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 868 | // Aot compiler supports having an image with a different pointer size than the runtime. This |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 869 | // happens on the host for compile 32 bit tests since we use a 64 bit libart compiler. We may |
| 870 | // also use 32 bit dex2oat on a system with 64 bit apps. |
| 871 | CHECK_EQ(art_method_object_size, mirror::ArtMethod::InstanceSize(sizeof(void*))) |
| 872 | << sizeof(void*); |
| 873 | } |
| 874 | if (art_method_object_size == mirror::ArtMethod::InstanceSize(4)) { |
| 875 | image_pointer_size_ = 4; |
| 876 | } else { |
| 877 | CHECK_EQ(art_method_object_size, mirror::ArtMethod::InstanceSize(8)); |
| 878 | image_pointer_size_ = 8; |
| 879 | } |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 880 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 881 | // Set entry point to interpreter if in InterpretOnly mode. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 882 | if (!runtime->IsAotCompiler() && runtime->GetInstrumentation()->InterpretOnly()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 883 | heap->VisitObjects(InitFromImageInterpretOnlyCallback, this); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 884 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 885 | |
| 886 | // reinit class_roots_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 887 | mirror::Class::SetClassClass(class_roots->Get(kJavaLangClass)); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 888 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>(class_roots.Get()); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 889 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 890 | // reinit array_iftable_ from any array class instance, they should be == |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 891 | array_iftable_ = GcRoot<mirror::IfTable>(GetClassRoot(kObjectArrayClass)->GetIfTable()); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 892 | DCHECK_EQ(array_iftable_.Read(), GetClassRoot(kBooleanArrayClass)->GetIfTable()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 893 | // String class root was set above |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 894 | mirror::Field::SetClass(GetClassRoot(kJavaLangReflectField)); |
| 895 | mirror::Field::SetArrayClass(GetClassRoot(kJavaLangReflectFieldArrayClass)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 896 | mirror::Reference::SetClass(GetClassRoot(kJavaLangRefReference)); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 897 | mirror::ArtField::SetClass(GetClassRoot(kJavaLangReflectArtField)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 898 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
| 899 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
| 900 | mirror::CharArray::SetArrayClass(GetClassRoot(kCharArrayClass)); |
| 901 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
| 902 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
| 903 | mirror::IntArray::SetArrayClass(GetClassRoot(kIntArrayClass)); |
| 904 | mirror::LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
| 905 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
| 906 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
| 907 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 908 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 909 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 910 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 911 | VLOG(startup) << "ClassLinker::InitFromImage exiting"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 912 | } |
| 913 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 914 | void ClassLinker::VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 915 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 916 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Mathieu Chartier | 4809d0a | 2015-04-07 10:39:04 -0700 | [diff] [blame] | 917 | BufferedRootVisitor<kDefaultBufferedRootCount> buffered_visitor( |
| 918 | visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 919 | for (GcRoot<mirror::Class>& root : class_table_) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 920 | buffered_visitor.VisitRoot(root); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 921 | } |
| 922 | for (GcRoot<mirror::Class>& root : pre_zygote_class_table_) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 923 | buffered_visitor.VisitRoot(root); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 924 | } |
| 925 | } else if ((flags & kVisitRootFlagNewRoots) != 0) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 926 | for (auto& root : new_class_roots_) { |
| 927 | mirror::Class* old_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 928 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 929 | mirror::Class* new_ref = root.Read<kWithoutReadBarrier>(); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 930 | if (UNLIKELY(new_ref != old_ref)) { |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 931 | // Uh ohes, GC moved a root in the log. Need to search the class_table and update the |
| 932 | // corresponding object. This is slow, but luckily for us, this may only happen with a |
| 933 | // concurrent moving GC. |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 934 | auto it = class_table_.Find(GcRoot<mirror::Class>(old_ref)); |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 935 | DCHECK(it != class_table_.end()); |
| 936 | *it = GcRoot<mirror::Class>(new_ref); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | } |
| 940 | if ((flags & kVisitRootFlagClearRootLog) != 0) { |
| 941 | new_class_roots_.clear(); |
| 942 | } |
| 943 | if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
| 944 | log_new_class_table_roots_ = true; |
| 945 | } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
| 946 | log_new_class_table_roots_ = false; |
| 947 | } |
| 948 | // We deliberately ignore the class roots in the image since we |
| 949 | // handle image roots by using the MS/CMS rescanning of dirty cards. |
| 950 | } |
| 951 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 952 | // Keep in sync with InitCallback. Anything we visit, we need to |
| 953 | // reinit references to when reinitializing a ClassLinker from a |
| 954 | // mapped image. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 955 | void ClassLinker::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) { |
| 956 | class_roots_.VisitRoot(visitor, RootInfo(kRootVMInternal)); |
| 957 | Thread* const self = Thread::Current(); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 958 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 959 | ReaderMutexLock mu(self, dex_lock_); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 960 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 961 | for (GcRoot<mirror::DexCache>& dex_cache : dex_caches_) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 962 | dex_cache.VisitRoot(visitor, RootInfo(kRootVMInternal)); |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 963 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 964 | } else if ((flags & kVisitRootFlagNewRoots) != 0) { |
| 965 | for (size_t index : new_dex_cache_roots_) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 966 | dex_caches_[index].VisitRoot(visitor, RootInfo(kRootVMInternal)); |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 967 | } |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 968 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 969 | if ((flags & kVisitRootFlagClearRootLog) != 0) { |
| 970 | new_dex_cache_roots_.clear(); |
| 971 | } |
| 972 | if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
| 973 | log_new_dex_caches_roots_ = true; |
| 974 | } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
| 975 | log_new_dex_caches_roots_ = false; |
| 976 | } |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 977 | } |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 978 | VisitClassRoots(visitor, flags); |
| 979 | array_iftable_.VisitRoot(visitor, RootInfo(kRootVMInternal)); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 980 | for (size_t i = 0; i < kFindArrayCacheSize; ++i) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 981 | find_array_class_cache_[i].VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 982 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 983 | } |
| 984 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 985 | void ClassLinker::VisitClasses(ClassVisitor* visitor, void* arg) { |
| 986 | if (dex_cache_image_class_lookup_required_) { |
| 987 | MoveImageClassesToClassTable(); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 988 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 989 | // TODO: why isn't this a ReaderMutexLock? |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 990 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 991 | for (GcRoot<mirror::Class>& root : class_table_) { |
| 992 | if (!visitor(root.Read(), arg)) { |
| 993 | return; |
| 994 | } |
| 995 | } |
| 996 | for (GcRoot<mirror::Class>& root : pre_zygote_class_table_) { |
| 997 | if (!visitor(root.Read(), arg)) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 998 | return; |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1003 | static bool GetClassesVisitorSet(mirror::Class* c, void* arg) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1004 | std::set<mirror::Class*>* classes = reinterpret_cast<std::set<mirror::Class*>*>(arg); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1005 | classes->insert(c); |
| 1006 | return true; |
| 1007 | } |
| 1008 | |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1009 | struct GetClassesVisitorArrayArg { |
| 1010 | Handle<mirror::ObjectArray<mirror::Class>>* classes; |
| 1011 | int32_t index; |
| 1012 | bool success; |
| 1013 | }; |
| 1014 | |
| 1015 | static bool GetClassesVisitorArray(mirror::Class* c, void* varg) |
| 1016 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1017 | GetClassesVisitorArrayArg* arg = reinterpret_cast<GetClassesVisitorArrayArg*>(varg); |
| 1018 | if (arg->index < (*arg->classes)->GetLength()) { |
| 1019 | (*arg->classes)->Set(arg->index, c); |
| 1020 | arg->index++; |
| 1021 | return true; |
| 1022 | } else { |
| 1023 | arg->success = false; |
| 1024 | return false; |
| 1025 | } |
| 1026 | } |
| 1027 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1028 | void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor, void* arg) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1029 | // TODO: it may be possible to avoid secondary storage if we iterate over dex caches. The problem |
| 1030 | // is avoiding duplicates. |
| 1031 | if (!kMovingClasses) { |
| 1032 | std::set<mirror::Class*> classes; |
| 1033 | VisitClasses(GetClassesVisitorSet, &classes); |
| 1034 | for (mirror::Class* klass : classes) { |
| 1035 | if (!visitor(klass, arg)) { |
| 1036 | return; |
| 1037 | } |
| 1038 | } |
| 1039 | } else { |
| 1040 | Thread* self = Thread::Current(); |
| 1041 | StackHandleScope<1> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 1042 | MutableHandle<mirror::ObjectArray<mirror::Class>> classes = |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1043 | hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
| 1044 | GetClassesVisitorArrayArg local_arg; |
| 1045 | local_arg.classes = &classes; |
| 1046 | local_arg.success = false; |
| 1047 | // We size the array assuming classes won't be added to the class table during the visit. |
| 1048 | // If this assumption fails we iterate again. |
| 1049 | while (!local_arg.success) { |
| 1050 | size_t class_table_size; |
| 1051 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1052 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1053 | class_table_size = class_table_.Size() + pre_zygote_class_table_.Size(); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1054 | } |
| 1055 | mirror::Class* class_type = mirror::Class::GetJavaLangClass(); |
| 1056 | mirror::Class* array_of_class = FindArrayClass(self, &class_type); |
| 1057 | classes.Assign( |
| 1058 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, class_table_size)); |
| 1059 | CHECK(classes.Get() != nullptr); // OOME. |
| 1060 | local_arg.index = 0; |
| 1061 | local_arg.success = true; |
| 1062 | VisitClasses(GetClassesVisitorArray, &local_arg); |
| 1063 | } |
| 1064 | for (int32_t i = 0; i < classes->GetLength(); ++i) { |
| 1065 | // If the class table shrank during creation of the clases array we expect null elements. If |
| 1066 | // the class table grew then the loop repeats. If classes are created after the loop has |
| 1067 | // finished then we don't visit. |
| 1068 | mirror::Class* klass = classes->Get(i); |
| 1069 | if (klass != nullptr && !visitor(klass, arg)) { |
| 1070 | return; |
| 1071 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1076 | ClassLinker::~ClassLinker() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1077 | mirror::Class::ResetClass(); |
| 1078 | mirror::String::ResetClass(); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 1079 | mirror::Reference::ResetClass(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1080 | mirror::ArtField::ResetClass(); |
| 1081 | mirror::ArtMethod::ResetClass(); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 1082 | mirror::Field::ResetClass(); |
| 1083 | mirror::Field::ResetArrayClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1084 | mirror::BooleanArray::ResetArrayClass(); |
| 1085 | mirror::ByteArray::ResetArrayClass(); |
| 1086 | mirror::CharArray::ResetArrayClass(); |
| 1087 | mirror::DoubleArray::ResetArrayClass(); |
| 1088 | mirror::FloatArray::ResetArrayClass(); |
| 1089 | mirror::IntArray::ResetArrayClass(); |
| 1090 | mirror::LongArray::ResetArrayClass(); |
| 1091 | mirror::ShortArray::ResetArrayClass(); |
| 1092 | mirror::Throwable::ResetClass(); |
| 1093 | mirror::StackTraceElement::ResetClass(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1094 | STLDeleteElements(&oat_files_); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1097 | mirror::DexCache* ClassLinker::AllocDexCache(Thread* self, const DexFile& dex_file) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1098 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1099 | StackHandleScope<16> hs(self); |
| 1100 | Handle<mirror::Class> dex_cache_class(hs.NewHandle(GetClassRoot(kJavaLangDexCache))); |
| 1101 | Handle<mirror::DexCache> dex_cache( |
| 1102 | hs.NewHandle(down_cast<mirror::DexCache*>( |
| 1103 | heap->AllocObject<true>(self, dex_cache_class.Get(), dex_cache_class->GetObjectSize(), |
| 1104 | VoidFunctor())))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1105 | if (dex_cache.Get() == nullptr) { |
| 1106 | return nullptr; |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1107 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1108 | Handle<mirror::String> |
| 1109 | location(hs.NewHandle(intern_table_->InternStrong(dex_file.GetLocation().c_str()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1110 | if (location.Get() == nullptr) { |
| 1111 | return nullptr; |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1112 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1113 | Handle<mirror::ObjectArray<mirror::String>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1114 | strings(hs.NewHandle(AllocStringArray(self, dex_file.NumStringIds()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1115 | if (strings.Get() == nullptr) { |
| 1116 | return nullptr; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1117 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1118 | Handle<mirror::ObjectArray<mirror::Class>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1119 | types(hs.NewHandle(AllocClassArray(self, dex_file.NumTypeIds()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1120 | if (types.Get() == nullptr) { |
| 1121 | return nullptr; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1122 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1123 | Handle<mirror::ObjectArray<mirror::ArtMethod>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1124 | methods(hs.NewHandle(AllocArtMethodArray(self, dex_file.NumMethodIds()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1125 | if (methods.Get() == nullptr) { |
| 1126 | return nullptr; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1127 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1128 | Handle<mirror::ObjectArray<mirror::ArtField>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1129 | fields(hs.NewHandle(AllocArtFieldArray(self, dex_file.NumFieldIds()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1130 | if (fields.Get() == nullptr) { |
| 1131 | return nullptr; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1132 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1133 | dex_cache->Init(&dex_file, location.Get(), strings.Get(), types.Get(), methods.Get(), |
| 1134 | fields.Get()); |
| 1135 | return dex_cache.Get(); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1138 | mirror::Class* ClassLinker::AllocClass(Thread* self, mirror::Class* java_lang_Class, |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1139 | uint32_t class_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1140 | DCHECK_GE(class_size, sizeof(mirror::Class)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1141 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1142 | mirror::Class::InitializeClassVisitor visitor(class_size); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1143 | mirror::Object* k = kMovingClasses ? |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1144 | heap->AllocObject<true>(self, java_lang_Class, class_size, visitor) : |
| 1145 | heap->AllocNonMovableObject<true>(self, java_lang_Class, class_size, visitor); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1146 | if (UNLIKELY(k == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1147 | CHECK(self->IsExceptionPending()); // OOME. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1148 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1149 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1150 | return k->AsClass(); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1153 | mirror::Class* ClassLinker::AllocClass(Thread* self, uint32_t class_size) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1154 | return AllocClass(self, GetClassRoot(kJavaLangClass), class_size); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1157 | mirror::ArtField* ClassLinker::AllocArtField(Thread* self) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1158 | return down_cast<mirror::ArtField*>( |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1159 | GetClassRoot(kJavaLangReflectArtField)->AllocNonMovableObject(self)); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1162 | mirror::ArtMethod* ClassLinker::AllocArtMethod(Thread* self) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1163 | return down_cast<mirror::ArtMethod*>( |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1164 | GetClassRoot(kJavaLangReflectArtMethod)->AllocNonMovableObject(self)); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 1165 | } |
| 1166 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1167 | mirror::ObjectArray<mirror::StackTraceElement>* ClassLinker::AllocStackTraceElementArray( |
| 1168 | Thread* self, size_t length) { |
| 1169 | return mirror::ObjectArray<mirror::StackTraceElement>::Alloc( |
| 1170 | self, GetClassRoot(kJavaLangStackTraceElementArrayClass), length); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1173 | mirror::Class* ClassLinker::EnsureResolved(Thread* self, const char* descriptor, |
| 1174 | mirror::Class* klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1175 | DCHECK(klass != nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1176 | |
| 1177 | // For temporary classes we must wait for them to be retired. |
| 1178 | if (init_done_ && klass->IsTemp()) { |
| 1179 | CHECK(!klass->IsResolved()); |
| 1180 | if (klass->IsErroneous()) { |
| 1181 | ThrowEarlierClassFailure(klass); |
| 1182 | return nullptr; |
| 1183 | } |
| 1184 | StackHandleScope<1> hs(self); |
| 1185 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 1186 | ObjectLock<mirror::Class> lock(self, h_class); |
| 1187 | // Loop and wait for the resolving thread to retire this class. |
| 1188 | while (!h_class->IsRetired() && !h_class->IsErroneous()) { |
| 1189 | lock.WaitIgnoringInterrupts(); |
| 1190 | } |
| 1191 | if (h_class->IsErroneous()) { |
| 1192 | ThrowEarlierClassFailure(h_class.Get()); |
| 1193 | return nullptr; |
| 1194 | } |
| 1195 | CHECK(h_class->IsRetired()); |
| 1196 | // Get the updated class from class table. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1197 | klass = LookupClass(self, descriptor, ComputeModifiedUtf8Hash(descriptor), |
| 1198 | h_class.Get()->GetClassLoader()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1201 | // Wait for the class if it has not already been linked. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 1202 | if (!klass->IsResolved() && !klass->IsErroneous()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1203 | StackHandleScope<1> hs(self); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 1204 | HandleWrapper<mirror::Class> h_class(hs.NewHandleWrapper(&klass)); |
| 1205 | ObjectLock<mirror::Class> lock(self, h_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1206 | // Check for circular dependencies between classes. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1207 | if (!h_class->IsResolved() && h_class->GetClinitThreadId() == self->GetTid()) { |
| 1208 | ThrowClassCircularityError(h_class.Get()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 1209 | mirror::Class::SetStatus(h_class, mirror::Class::kStatusError, self); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 1210 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1211 | } |
| 1212 | // Wait for the pending initialization to complete. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1213 | while (!h_class->IsResolved() && !h_class->IsErroneous()) { |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 1214 | lock.WaitIgnoringInterrupts(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1215 | } |
| 1216 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1217 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1218 | if (klass->IsErroneous()) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 1219 | ThrowEarlierClassFailure(klass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 1220 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1221 | } |
| 1222 | // Return the loaded class. No exceptions should be pending. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1223 | CHECK(klass->IsResolved()) << PrettyClass(klass); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1224 | self->AssertNoPendingException(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1225 | return klass; |
| 1226 | } |
| 1227 | |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1228 | typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry; |
| 1229 | |
| 1230 | // Search a collection of DexFiles for a descriptor |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1231 | ClassPathEntry FindInClassPath(const char* descriptor, |
| 1232 | size_t hash, const std::vector<const DexFile*>& class_path) { |
| 1233 | for (const DexFile* dex_file : class_path) { |
| 1234 | const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1235 | if (dex_class_def != nullptr) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1236 | return ClassPathEntry(dex_file, dex_class_def); |
| 1237 | } |
| 1238 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1239 | return ClassPathEntry(nullptr, nullptr); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 1242 | mirror::Class* ClassLinker::FindClassInPathClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
| 1243 | Thread* self, const char* descriptor, |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1244 | size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 1245 | Handle<mirror::ClassLoader> class_loader) { |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 1246 | // Can we special case for a well understood PathClassLoader with the BootClassLoader as parent? |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 1247 | if (class_loader->GetClass() != |
| 1248 | soa.Decode<mirror::Class*>(WellKnownClasses::dalvik_system_PathClassLoader) || |
| 1249 | class_loader->GetParent()->GetClass() != |
| 1250 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_BootClassLoader)) { |
| 1251 | return nullptr; |
| 1252 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1253 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 1254 | // Check if this would be found in the parent boot class loader. |
| 1255 | if (pair.second != nullptr) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1256 | mirror::Class* klass = LookupClass(self, descriptor, hash, nullptr); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 1257 | if (klass != nullptr) { |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 1258 | // May return null if resolution on another thread fails. |
| 1259 | klass = EnsureResolved(self, descriptor, klass); |
| 1260 | } else { |
| 1261 | // May OOME. |
| 1262 | klass = DefineClass(self, descriptor, hash, NullHandle<mirror::ClassLoader>(), *pair.first, |
| 1263 | *pair.second); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 1264 | } |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 1265 | if (klass == nullptr) { |
| 1266 | CHECK(self->IsExceptionPending()) << descriptor; |
| 1267 | self->ClearException(); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 1268 | } |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 1269 | return klass; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 1270 | } else { |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 1271 | // Handle as if this is the child PathClassLoader. |
| 1272 | // Handles as RegisterDexFile may allocate dex caches (and cause thread suspension). |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 1273 | StackHandleScope<3> hs(self); |
| 1274 | // The class loader is a PathClassLoader which inherits from BaseDexClassLoader. |
| 1275 | // We need to get the DexPathList and loop through it. |
| 1276 | Handle<mirror::ArtField> cookie_field = |
| 1277 | hs.NewHandle(soa.DecodeField(WellKnownClasses::dalvik_system_DexFile_cookie)); |
| 1278 | Handle<mirror::ArtField> dex_file_field = |
| 1279 | hs.NewHandle( |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 1280 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile)); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 1281 | mirror::Object* dex_path_list = |
| 1282 | soa.DecodeField(WellKnownClasses::dalvik_system_PathClassLoader_pathList)-> |
| 1283 | GetObject(class_loader.Get()); |
| 1284 | if (dex_path_list != nullptr && dex_file_field.Get() != nullptr && |
| 1285 | cookie_field.Get() != nullptr) { |
| 1286 | // DexPathList has an array dexElements of Elements[] which each contain a dex file. |
| 1287 | mirror::Object* dex_elements_obj = |
| 1288 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList_dexElements)-> |
| 1289 | GetObject(dex_path_list); |
| 1290 | // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look |
| 1291 | // at the mCookie which is a DexFile vector. |
| 1292 | if (dex_elements_obj != nullptr) { |
| 1293 | Handle<mirror::ObjectArray<mirror::Object>> dex_elements = |
| 1294 | hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>()); |
| 1295 | for (int32_t i = 0; i < dex_elements->GetLength(); ++i) { |
| 1296 | mirror::Object* element = dex_elements->GetWithoutChecks(i); |
| 1297 | if (element == nullptr) { |
| 1298 | // Should never happen, fall back to java code to throw a NPE. |
| 1299 | break; |
| 1300 | } |
| 1301 | mirror::Object* dex_file = dex_file_field->GetObject(element); |
| 1302 | if (dex_file != nullptr) { |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 1303 | mirror::LongArray* long_array = cookie_field->GetObject(dex_file)->AsLongArray(); |
| 1304 | if (long_array == nullptr) { |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 1305 | // This should never happen so log a warning. |
| 1306 | LOG(WARNING) << "Null DexFile::mCookie for " << descriptor; |
| 1307 | break; |
| 1308 | } |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 1309 | int32_t long_array_size = long_array->GetLength(); |
| 1310 | for (int32_t j = 0; j < long_array_size; ++j) { |
| 1311 | const DexFile* cp_dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>( |
| 1312 | long_array->GetWithoutChecks(j))); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1313 | const DexFile::ClassDef* dex_class_def = cp_dex_file->FindClassDef(descriptor, hash); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 1314 | if (dex_class_def != nullptr) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1315 | RegisterDexFile(*cp_dex_file); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1316 | mirror::Class* klass = DefineClass(self, descriptor, hash, class_loader, |
| 1317 | *cp_dex_file, *dex_class_def); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 1318 | if (klass == nullptr) { |
| 1319 | CHECK(self->IsExceptionPending()) << descriptor; |
| 1320 | self->ClearException(); |
| 1321 | return nullptr; |
| 1322 | } |
| 1323 | return klass; |
| 1324 | } |
| 1325 | } |
| 1326 | } |
| 1327 | } |
| 1328 | } |
| 1329 | } |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 1330 | self->AssertNoPendingException(); |
| 1331 | return nullptr; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 1332 | } |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 1333 | } |
| 1334 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1335 | mirror::Class* ClassLinker::FindClass(Thread* self, const char* descriptor, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 1336 | Handle<mirror::ClassLoader> class_loader) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 1337 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1338 | DCHECK(self != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1339 | self->AssertNoPendingException(); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 1340 | if (descriptor[1] == '\0') { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1341 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 1342 | // for primitive classes that aren't backed by dex files. |
| 1343 | return FindPrimitiveClass(descriptor[0]); |
| 1344 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1345 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1346 | // Find the class in the loaded classes table. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1347 | mirror::Class* klass = LookupClass(self, descriptor, hash, class_loader.Get()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1348 | if (klass != nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1349 | return EnsureResolved(self, descriptor, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1350 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1351 | // Class is not yet loaded. |
| 1352 | if (descriptor[0] == '[') { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1353 | return CreateArrayClass(self, descriptor, hash, class_loader); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1354 | } else if (class_loader.Get() == nullptr) { |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 1355 | // The boot class loader, search the boot class path. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1356 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1357 | if (pair.second != nullptr) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1358 | return DefineClass(self, descriptor, hash, NullHandle<mirror::ClassLoader>(), *pair.first, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1359 | *pair.second); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 1360 | } else { |
| 1361 | // The boot class loader is searched ahead of the application class loader, failures are |
| 1362 | // expected and will be wrapped in a ClassNotFoundException. Use the pre-allocated error to |
| 1363 | // trigger the chaining with a proper stack trace. |
| 1364 | mirror::Throwable* pre_allocated = Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 1365 | self->SetException(pre_allocated); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 1366 | return nullptr; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1367 | } |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1368 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1369 | ScopedObjectAccessUnchecked soa(self); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1370 | mirror::Class* cp_klass = FindClassInPathClassLoader(soa, self, descriptor, hash, |
| 1371 | class_loader); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1372 | if (cp_klass != nullptr) { |
| 1373 | return cp_klass; |
Mathieu Chartier | 6bcae8f | 2014-09-04 18:33:17 -0700 | [diff] [blame] | 1374 | } |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 1375 | |
| 1376 | if (Runtime::Current()->IsAotCompiler()) { |
| 1377 | // Oops, compile-time, can't run actual class-loader code. |
| 1378 | mirror::Throwable* pre_allocated = Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 1379 | self->SetException(pre_allocated); |
| 1380 | return nullptr; |
| 1381 | } |
| 1382 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1383 | ScopedLocalRef<jobject> class_loader_object(soa.Env(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1384 | soa.AddLocalReference<jobject>(class_loader.Get())); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 1385 | std::string class_name_string(DescriptorToDot(descriptor)); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1386 | ScopedLocalRef<jobject> result(soa.Env(), nullptr); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 1387 | { |
| 1388 | ScopedThreadStateChange tsc(self, kNative); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1389 | ScopedLocalRef<jobject> class_name_object(soa.Env(), |
| 1390 | soa.Env()->NewStringUTF(class_name_string.c_str())); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1391 | if (class_name_object.get() == nullptr) { |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 1392 | DCHECK(self->IsExceptionPending()); // OOME. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1393 | return nullptr; |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 1394 | } |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1395 | CHECK(class_loader_object.get() != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1396 | result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(), |
| 1397 | WellKnownClasses::java_lang_ClassLoader_loadClass, |
| 1398 | class_name_object.get())); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1399 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1400 | if (self->IsExceptionPending()) { |
Elliott Hughes | 748382f | 2012-01-26 18:07:38 -0800 | [diff] [blame] | 1401 | // If the ClassLoader threw, pass that exception up. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1402 | return nullptr; |
| 1403 | } else if (result.get() == nullptr) { |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 1404 | // broken loader - throw NPE to be compatible with Dalvik |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 1405 | ThrowNullPointerException(StringPrintf("ClassLoader.loadClass returned null for %s", |
| 1406 | class_name_string.c_str()).c_str()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1407 | return nullptr; |
Ian Rogers | 761bfa8 | 2012-01-11 10:14:05 -0800 | [diff] [blame] | 1408 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1409 | // success, return mirror::Class* |
| 1410 | return soa.Decode<mirror::Class*>(result.get()); |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 1411 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1412 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 1413 | UNREACHABLE(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1414 | } |
| 1415 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1416 | mirror::Class* ClassLinker::DefineClass(Thread* self, const char* descriptor, size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 1417 | Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1418 | const DexFile& dex_file, |
| 1419 | const DexFile::ClassDef& dex_class_def) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1420 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1421 | auto klass = hs.NewHandle<mirror::Class>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1422 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1423 | // Load the class from the dex file. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1424 | if (UNLIKELY(!init_done_)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1425 | // finish up init of hand crafted class_roots_ |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1426 | if (strcmp(descriptor, "Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1427 | klass.Assign(GetClassRoot(kJavaLangObject)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1428 | } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1429 | klass.Assign(GetClassRoot(kJavaLangClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1430 | } else if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1431 | klass.Assign(GetClassRoot(kJavaLangString)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 1432 | } else if (strcmp(descriptor, "Ljava/lang/ref/Reference;") == 0) { |
| 1433 | klass.Assign(GetClassRoot(kJavaLangRefReference)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1434 | } else if (strcmp(descriptor, "Ljava/lang/DexCache;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1435 | klass.Assign(GetClassRoot(kJavaLangDexCache)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1436 | } else if (strcmp(descriptor, "Ljava/lang/reflect/ArtField;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1437 | klass.Assign(GetClassRoot(kJavaLangReflectArtField)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1438 | } else if (strcmp(descriptor, "Ljava/lang/reflect/ArtMethod;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1439 | klass.Assign(GetClassRoot(kJavaLangReflectArtMethod)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1440 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1441 | } |
| 1442 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1443 | if (klass.Get() == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1444 | // Allocate a class with the status of not ready. |
| 1445 | // Interface object should get the right size here. Regular class will |
| 1446 | // figure out the right size later and be replaced with one of the right |
| 1447 | // size when the class becomes resolved. |
| 1448 | klass.Assign(AllocClass(self, SizeOfClassWithoutEmbeddedTables(dex_file, dex_class_def))); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1449 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 1450 | if (UNLIKELY(klass.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1451 | CHECK(self->IsExceptionPending()); // Expect an OOME. |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 1452 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1453 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1454 | klass->SetDexCache(FindDexCache(dex_file)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1455 | LoadClass(self, dex_file, dex_class_def, klass, class_loader.Get()); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 1456 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 1457 | if (self->IsExceptionPending()) { |
| 1458 | // An exception occured during load, set status to erroneous while holding klass' lock in case |
| 1459 | // notification is necessary. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 1460 | if (!klass->IsErroneous()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 1461 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 1462 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 1463 | return nullptr; |
| 1464 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1465 | klass->SetClinitThreadId(self->GetTid()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1466 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1467 | // Add the newly loaded class to the loaded classes table. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 1468 | mirror::Class* existing = InsertClass(descriptor, klass.Get(), hash); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 1469 | if (existing != nullptr) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1470 | // We failed to insert because we raced with another thread. Calling EnsureResolved may cause |
| 1471 | // this thread to block. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1472 | return EnsureResolved(self, descriptor, existing); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1473 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1474 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1475 | // Finish loading (if necessary) by finding parents |
| 1476 | CHECK(!klass->IsLoaded()); |
| 1477 | if (!LoadSuperAndInterfaces(klass, dex_file)) { |
| 1478 | // Loading failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 1479 | if (!klass->IsErroneous()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 1480 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 1481 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 1482 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1483 | } |
| 1484 | CHECK(klass->IsLoaded()); |
| 1485 | // Link the class (if necessary) |
| 1486 | CHECK(!klass->IsResolved()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1487 | // TODO: Use fast jobjects? |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1488 | auto interfaces = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1489 | |
| 1490 | mirror::Class* new_class = nullptr; |
| 1491 | if (!LinkClass(self, descriptor, klass, interfaces, &new_class)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1492 | // Linking failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 1493 | if (!klass->IsErroneous()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 1494 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 1495 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 1496 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1497 | } |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 1498 | self->AssertNoPendingException(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1499 | CHECK(new_class != nullptr) << descriptor; |
| 1500 | CHECK(new_class->IsResolved()) << descriptor; |
| 1501 | |
| 1502 | Handle<mirror::Class> new_class_h(hs.NewHandle(new_class)); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 1503 | |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 1504 | // Instrumentation may have updated entrypoints for all methods of all |
| 1505 | // classes. However it could not update methods of this class while we |
| 1506 | // were loading it. Now the class is resolved, we can update entrypoints |
| 1507 | // as required by instrumentation. |
| 1508 | if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) { |
| 1509 | // We must be in the kRunnable state to prevent instrumentation from |
| 1510 | // suspending all threads to update entrypoints while we are doing it |
| 1511 | // for this class. |
| 1512 | DCHECK_EQ(self->GetState(), kRunnable); |
| 1513 | Runtime::Current()->GetInstrumentation()->InstallStubsForClass(new_class_h.Get()); |
| 1514 | } |
| 1515 | |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 1516 | /* |
| 1517 | * We send CLASS_PREPARE events to the debugger from here. The |
| 1518 | * definition of "preparation" is creating the static fields for a |
| 1519 | * class and initializing them to the standard default values, but not |
| 1520 | * executing any code (that comes later, during "initialization"). |
| 1521 | * |
| 1522 | * We did the static preparation in LinkClass. |
| 1523 | * |
| 1524 | * The class has been prepared and resolved but possibly not yet verified |
| 1525 | * at this point. |
| 1526 | */ |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1527 | Dbg::PostClassPrepare(new_class_h.Get()); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 1528 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1529 | return new_class_h.Get(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1530 | } |
| 1531 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1532 | uint32_t ClassLinker::SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, |
| 1533 | const DexFile::ClassDef& dex_class_def) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1534 | const uint8_t* class_data = dex_file.GetClassData(dex_class_def); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1535 | size_t num_ref = 0; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 1536 | size_t num_8 = 0; |
| 1537 | size_t num_16 = 0; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1538 | size_t num_32 = 0; |
| 1539 | size_t num_64 = 0; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1540 | if (class_data != nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1541 | for (ClassDataItemIterator it(dex_file, class_data); it.HasNextStaticField(); it.Next()) { |
| 1542 | const DexFile::FieldId& field_id = dex_file.GetFieldId(it.GetMemberIndex()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1543 | const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1544 | char c = descriptor[0]; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 1545 | switch (c) { |
| 1546 | case 'L': |
| 1547 | case '[': |
| 1548 | num_ref++; |
| 1549 | break; |
| 1550 | case 'J': |
| 1551 | case 'D': |
| 1552 | num_64++; |
| 1553 | break; |
| 1554 | case 'I': |
| 1555 | case 'F': |
| 1556 | num_32++; |
| 1557 | break; |
| 1558 | case 'S': |
| 1559 | case 'C': |
| 1560 | num_16++; |
| 1561 | break; |
| 1562 | case 'B': |
| 1563 | case 'Z': |
| 1564 | num_8++; |
| 1565 | break; |
| 1566 | default: |
| 1567 | LOG(FATAL) << "Unknown descriptor: " << c; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1568 | UNREACHABLE(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1569 | } |
| 1570 | } |
| 1571 | } |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 1572 | return mirror::Class::ComputeClassSize(false, 0, num_8, num_16, num_32, num_64, num_ref); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1573 | } |
| 1574 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1575 | OatFile::OatClass ClassLinker::FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, |
| 1576 | bool* found) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 1577 | DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16); |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 1578 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1579 | if (oat_dex_file == nullptr) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1580 | *found = false; |
| 1581 | return OatFile::OatClass::Invalid(); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 1582 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1583 | *found = true; |
| 1584 | return oat_dex_file->GetOatClass(class_def_idx); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1585 | } |
| 1586 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 1587 | static uint32_t GetOatMethodIndexFromMethodIndex(const DexFile& dex_file, uint16_t class_def_idx, |
| 1588 | uint32_t method_idx) { |
| 1589 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_idx); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1590 | const uint8_t* class_data = dex_file.GetClassData(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1591 | CHECK(class_data != nullptr); |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 1592 | ClassDataItemIterator it(dex_file, class_data); |
| 1593 | // Skip fields |
| 1594 | while (it.HasNextStaticField()) { |
| 1595 | it.Next(); |
| 1596 | } |
| 1597 | while (it.HasNextInstanceField()) { |
| 1598 | it.Next(); |
| 1599 | } |
| 1600 | // Process methods |
| 1601 | size_t class_def_method_index = 0; |
| 1602 | while (it.HasNextDirectMethod()) { |
| 1603 | if (it.GetMemberIndex() == method_idx) { |
| 1604 | return class_def_method_index; |
| 1605 | } |
| 1606 | class_def_method_index++; |
| 1607 | it.Next(); |
| 1608 | } |
| 1609 | while (it.HasNextVirtualMethod()) { |
| 1610 | if (it.GetMemberIndex() == method_idx) { |
| 1611 | return class_def_method_index; |
| 1612 | } |
| 1613 | class_def_method_index++; |
| 1614 | it.Next(); |
| 1615 | } |
| 1616 | DCHECK(!it.HasNext()); |
| 1617 | 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] | 1618 | UNREACHABLE(); |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 1619 | } |
| 1620 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1621 | const OatFile::OatMethod ClassLinker::FindOatMethodFor(mirror::ArtMethod* method, bool* found) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1622 | // 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] | 1623 | // method for direct methods (or virtual methods made direct). |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1624 | mirror::Class* declaring_class = method->GetDeclaringClass(); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 1625 | size_t oat_method_index; |
| 1626 | if (method->IsStatic() || method->IsDirect()) { |
| 1627 | // Simple case where the oat method index was stashed at load time. |
| 1628 | oat_method_index = method->GetMethodIndex(); |
| 1629 | } else { |
| 1630 | // We're invoking a virtual method directly (thanks to sharpening), compute the oat_method_index |
| 1631 | // by search for its position in the declared virtual methods. |
| 1632 | oat_method_index = declaring_class->NumDirectMethods(); |
| 1633 | size_t end = declaring_class->NumVirtualMethods(); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1634 | bool found_virtual = false; |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 1635 | for (size_t i = 0; i < end; i++) { |
Jeff Hao | 68caf9e | 2014-09-03 13:48:16 -0700 | [diff] [blame] | 1636 | // Check method index instead of identity in case of duplicate method definitions. |
| 1637 | if (method->GetDexMethodIndex() == |
| 1638 | declaring_class->GetVirtualMethod(i)->GetDexMethodIndex()) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1639 | found_virtual = true; |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 1640 | break; |
| 1641 | } |
Ian Rogers | f320b63 | 2012-03-13 18:47:47 -0700 | [diff] [blame] | 1642 | oat_method_index++; |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 1643 | } |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1644 | CHECK(found_virtual) << "Didn't find oat method index for virtual method: " |
| 1645 | << PrettyMethod(method); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 1646 | } |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 1647 | DCHECK_EQ(oat_method_index, |
| 1648 | GetOatMethodIndexFromMethodIndex(*declaring_class->GetDexCache()->GetDexFile(), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 1649 | method->GetDeclaringClass()->GetDexClassDefIndex(), |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 1650 | method->GetDexMethodIndex())); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1651 | OatFile::OatClass oat_class = FindOatClass(*declaring_class->GetDexCache()->GetDexFile(), |
| 1652 | declaring_class->GetDexClassDefIndex(), |
| 1653 | found); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1654 | if (!(*found)) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1655 | return OatFile::OatMethod::Invalid(); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 1656 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1657 | return oat_class.GetOatMethod(oat_method_index); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 1658 | } |
| 1659 | |
| 1660 | // Special case to get oat code without overwriting a trampoline. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1661 | const void* ClassLinker::GetQuickOatCodeFor(mirror::ArtMethod* method) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1662 | CHECK(!method->IsAbstract()) << PrettyMethod(method); |
Jeff Hao | 8df6cea | 2013-07-29 13:54:48 -0700 | [diff] [blame] | 1663 | if (method->IsProxyMethod()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1664 | return GetQuickProxyInvokeHandler(); |
Jeff Hao | 8df6cea | 2013-07-29 13:54:48 -0700 | [diff] [blame] | 1665 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1666 | bool found; |
Mathieu Chartier | c0d5f89 | 2015-02-25 13:22:57 -0800 | [diff] [blame] | 1667 | OatFile::OatMethod oat_method = FindOatMethodFor(method, &found); |
| 1668 | if (found) { |
| 1669 | auto* code = oat_method.GetQuickCode(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1670 | if (code != nullptr) { |
| 1671 | return code; |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame] | 1672 | } |
| 1673 | } |
Mathieu Chartier | c0d5f89 | 2015-02-25 13:22:57 -0800 | [diff] [blame] | 1674 | jit::Jit* const jit = Runtime::Current()->GetJit(); |
| 1675 | if (jit != nullptr) { |
| 1676 | auto* code = jit->GetCodeCache()->GetCodeFor(method); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1677 | if (code != nullptr) { |
| 1678 | return code; |
| 1679 | } |
| 1680 | } |
| 1681 | if (method->IsNative()) { |
| 1682 | // No code and native? Use generic trampoline. |
| 1683 | return GetQuickGenericJniStub(); |
| 1684 | } |
| 1685 | return GetQuickToInterpreterBridge(); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 1686 | } |
| 1687 | |
Hiroshi Yamauchi | 9bdec88 | 2014-08-15 17:11:12 -0700 | [diff] [blame] | 1688 | const void* ClassLinker::GetOatMethodQuickCodeFor(mirror::ArtMethod* method) { |
| 1689 | if (method->IsNative() || method->IsAbstract() || method->IsProxyMethod()) { |
| 1690 | return nullptr; |
| 1691 | } |
Mathieu Chartier | c0d5f89 | 2015-02-25 13:22:57 -0800 | [diff] [blame] | 1692 | bool found; |
| 1693 | OatFile::OatMethod oat_method = FindOatMethodFor(method, &found); |
| 1694 | if (found) { |
| 1695 | return oat_method.GetQuickCode(); |
| 1696 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1697 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 1698 | if (jit != nullptr) { |
| 1699 | auto* code = jit->GetCodeCache()->GetCodeFor(method); |
| 1700 | if (code != nullptr) { |
| 1701 | return code; |
| 1702 | } |
| 1703 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1704 | return nullptr; |
Hiroshi Yamauchi | 9bdec88 | 2014-08-15 17:11:12 -0700 | [diff] [blame] | 1705 | } |
| 1706 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1707 | const void* ClassLinker::GetQuickOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, |
| 1708 | uint32_t method_idx) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1709 | bool found; |
| 1710 | OatFile::OatClass oat_class = FindOatClass(dex_file, class_def_idx, &found); |
| 1711 | if (!found) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 1712 | return nullptr; |
| 1713 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 1714 | 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] | 1715 | return oat_class.GetOatMethod(oat_method_idx).GetQuickCode(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1716 | } |
| 1717 | |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 1718 | // Returns true if the method must run with interpreter, false otherwise. |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 1719 | static bool NeedsInterpreter(mirror::ArtMethod* method, const void* quick_code) |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1720 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 1721 | if (quick_code == nullptr) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 1722 | // No code: need interpreter. |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1723 | // May return true for native code, in the case of generic JNI |
| 1724 | // DCHECK(!method->IsNative()); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 1725 | return true; |
| 1726 | } |
| 1727 | // If interpreter mode is enabled, every method (except native and proxy) must |
| 1728 | // be run with interpreter. |
| 1729 | return Runtime::Current()->GetInstrumentation()->InterpretOnly() && |
| 1730 | !method->IsNative() && !method->IsProxyMethod(); |
| 1731 | } |
| 1732 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1733 | void ClassLinker::FixupStaticTrampolines(mirror::Class* klass) { |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 1734 | DCHECK(klass->IsInitialized()) << PrettyDescriptor(klass); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 1735 | if (klass->NumDirectMethods() == 0) { |
| 1736 | return; // No direct methods => no static methods. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1737 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1738 | Runtime* runtime = Runtime::Current(); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 1739 | if (!runtime->IsStarted()) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1740 | if (runtime->IsAotCompiler() || runtime->GetHeap()->HasImageSpace()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1741 | return; // OAT file unavailable. |
| 1742 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1743 | } |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1744 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1745 | const DexFile& dex_file = klass->GetDexFile(); |
| 1746 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 1747 | CHECK(dex_class_def != nullptr); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1748 | const uint8_t* class_data = dex_file.GetClassData(*dex_class_def); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 1749 | // There should always be class data if there were direct methods. |
| 1750 | CHECK(class_data != nullptr) << PrettyDescriptor(klass); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1751 | ClassDataItemIterator it(dex_file, class_data); |
| 1752 | // Skip fields |
| 1753 | while (it.HasNextStaticField()) { |
| 1754 | it.Next(); |
| 1755 | } |
| 1756 | while (it.HasNextInstanceField()) { |
| 1757 | it.Next(); |
| 1758 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1759 | bool has_oat_class; |
| 1760 | OatFile::OatClass oat_class = FindOatClass(dex_file, klass->GetDexClassDefIndex(), |
| 1761 | &has_oat_class); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 1762 | // Link the code of methods skipped by LinkCode. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 1763 | for (size_t method_index = 0; it.HasNextDirectMethod(); ++method_index, it.Next()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1764 | mirror::ArtMethod* method = klass->GetDirectMethod(method_index); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 1765 | if (!method->IsStatic()) { |
| 1766 | // Only update static methods. |
| 1767 | continue; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1768 | } |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 1769 | const void* quick_code = nullptr; |
| 1770 | if (has_oat_class) { |
| 1771 | OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 1772 | quick_code = oat_method.GetQuickCode(); |
| 1773 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 1774 | const bool enter_interpreter = NeedsInterpreter(method, quick_code); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 1775 | if (enter_interpreter) { |
| 1776 | // Use interpreter entry point. |
Ian Rogers | 1a57066 | 2014-03-12 01:02:21 -0700 | [diff] [blame] | 1777 | // 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] | 1778 | if (quick_code == nullptr && method->IsNative()) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 1779 | quick_code = GetQuickGenericJniStub(); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1780 | } else { |
| 1781 | quick_code = GetQuickToInterpreterBridge(); |
| 1782 | } |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 1783 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 1784 | runtime->GetInstrumentation()->UpdateMethodsCode(method, quick_code); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1785 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1786 | // Ignore virtual methods on the iterator. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1787 | } |
| 1788 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 1789 | void ClassLinker::LinkCode(Handle<mirror::ArtMethod> method, |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 1790 | const OatFile::OatClass* oat_class, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1791 | uint32_t class_def_method_index) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 1792 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1793 | if (runtime->IsAotCompiler()) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 1794 | // The following code only applies to a non-compiler runtime. |
| 1795 | return; |
| 1796 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1797 | // Method shouldn't have already been linked. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1798 | DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 1799 | if (oat_class != nullptr) { |
| 1800 | // Every kind of method should at least get an invoke stub from the oat_method. |
| 1801 | // non-abstract methods also get their code pointers. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1802 | const OatFile::OatMethod oat_method = oat_class->GetOatMethod(class_def_method_index); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 1803 | oat_method.LinkMethod(method.Get()); |
| 1804 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 1805 | |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 1806 | // Install entry point from interpreter. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1807 | bool enter_interpreter = NeedsInterpreter(method.Get(), |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 1808 | method->GetEntryPointFromQuickCompiledCode()); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1809 | if (enter_interpreter && !method->IsNative()) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 1810 | method->SetEntryPointFromInterpreter(artInterpreterToInterpreterBridge); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 1811 | } else { |
Dragos Sbirlea | 08bf196 | 2013-08-12 08:53:04 -0700 | [diff] [blame] | 1812 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 1813 | } |
| 1814 | |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 1815 | if (method->IsAbstract()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1816 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 1817 | return; |
| 1818 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1819 | |
| 1820 | if (method->IsStatic() && !method->IsConstructor()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1821 | // For static methods excluding the class initializer, install the trampoline. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 1822 | // It will be replaced by the proper entry point by ClassLinker::FixupStaticTrampolines |
| 1823 | // after initializing class (see ClassLinker::InitializeClass method). |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 1824 | method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionStub()); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 1825 | } else if (enter_interpreter) { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1826 | if (!method->IsNative()) { |
| 1827 | // Set entry point from compiled code if there's no code or in interpreter only mode. |
| 1828 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1829 | } else { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 1830 | method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub()); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1831 | } |
Ian Rogers | 0d6de04 | 2012-02-29 08:50:26 -0800 | [diff] [blame] | 1832 | } |
jeffhao | 26c0a1a | 2012-01-17 16:28:33 -0800 | [diff] [blame] | 1833 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1834 | if (method->IsNative()) { |
| 1835 | // Unregistering restores the dlsym lookup stub. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 1836 | method->UnregisterNative(); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 1837 | |
| 1838 | if (enter_interpreter) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 1839 | // We have a native method here without code. Then it should have either the generic JNI |
| 1840 | // trampoline as entrypoint (non-static), or the resolution trampoline (static). |
| 1841 | // TODO: this doesn't handle all the cases where trampolines may be installed. |
| 1842 | const void* entry_point = method->GetEntryPointFromQuickCompiledCode(); |
| 1843 | DCHECK(IsQuickGenericJniStub(entry_point) || IsQuickResolutionStub(entry_point)); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 1844 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 1845 | } |
| 1846 | } |
| 1847 | |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 1848 | |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 1849 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1850 | void ClassLinker::LoadClass(Thread* self, const DexFile& dex_file, |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1851 | const DexFile::ClassDef& dex_class_def, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 1852 | Handle<mirror::Class> klass, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1853 | mirror::ClassLoader* class_loader) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1854 | CHECK(klass.Get() != nullptr); |
| 1855 | CHECK(klass->GetDexCache() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1856 | CHECK_EQ(mirror::Class::kStatusNotReady, klass->GetStatus()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1857 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1858 | CHECK(descriptor != nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1859 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1860 | klass->SetClass(GetClassRoot(kJavaLangClass)); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 1861 | uint32_t access_flags = dex_class_def.GetJavaAccessFlags(); |
Brian Carlstrom | 8e3fb14 | 2013-10-09 21:00:27 -0700 | [diff] [blame] | 1862 | CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1863 | klass->SetAccessFlags(access_flags); |
| 1864 | klass->SetClassLoader(class_loader); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 1865 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 1866 | mirror::Class::SetStatus(klass, mirror::Class::kStatusIdx, nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1867 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 1868 | klass->SetDexClassDefIndex(dex_file.GetIndexForClassDef(dex_class_def)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1869 | klass->SetDexTypeIndex(dex_class_def.class_idx_); |
Mathieu Chartier | 91a6dc4 | 2014-12-01 10:31:15 -0800 | [diff] [blame] | 1870 | CHECK(klass->GetDexCacheStrings() != nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1871 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1872 | const uint8_t* class_data = dex_file.GetClassData(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1873 | if (class_data == nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1874 | return; // no fields or methods - for example a marker interface |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1875 | } |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 1876 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1877 | |
| 1878 | bool has_oat_class = false; |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 1879 | if (Runtime::Current()->IsStarted() && !Runtime::Current()->IsAotCompiler()) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1880 | OatFile::OatClass oat_class = FindOatClass(dex_file, klass->GetDexClassDefIndex(), |
| 1881 | &has_oat_class); |
| 1882 | if (has_oat_class) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1883 | LoadClassMembers(self, dex_file, class_data, klass, &oat_class); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1884 | } |
| 1885 | } |
| 1886 | if (!has_oat_class) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1887 | LoadClassMembers(self, dex_file, class_data, klass, nullptr); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 1888 | } |
| 1889 | } |
| 1890 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1891 | void ClassLinker::LoadClassMembers(Thread* self, const DexFile& dex_file, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1892 | const uint8_t* class_data, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 1893 | Handle<mirror::Class> klass, |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 1894 | const OatFile::OatClass* oat_class) { |
| 1895 | // Load fields. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1896 | ClassDataItemIterator it(dex_file, class_data); |
| 1897 | if (it.NumStaticFields() != 0) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1898 | mirror::ObjectArray<mirror::ArtField>* statics = AllocArtFieldArray(self, it.NumStaticFields()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1899 | if (UNLIKELY(statics == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1900 | CHECK(self->IsExceptionPending()); // OOME. |
| 1901 | return; |
| 1902 | } |
| 1903 | klass->SetSFields(statics); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1904 | } |
| 1905 | if (it.NumInstanceFields() != 0) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1906 | mirror::ObjectArray<mirror::ArtField>* fields = |
| 1907 | AllocArtFieldArray(self, it.NumInstanceFields()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1908 | if (UNLIKELY(fields == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1909 | CHECK(self->IsExceptionPending()); // OOME. |
| 1910 | return; |
| 1911 | } |
| 1912 | klass->SetIFields(fields); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1913 | } |
| 1914 | for (size_t i = 0; it.HasNextStaticField(); i++, it.Next()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1915 | self->AllowThreadSuspension(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1916 | StackHandleScope<1> hs(self); |
| 1917 | Handle<mirror::ArtField> sfield(hs.NewHandle(AllocArtField(self))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1918 | if (UNLIKELY(sfield.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1919 | CHECK(self->IsExceptionPending()); // OOME. |
| 1920 | return; |
| 1921 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1922 | klass->SetStaticField(i, sfield.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1923 | LoadField(dex_file, it, klass, sfield); |
| 1924 | } |
| 1925 | for (size_t i = 0; it.HasNextInstanceField(); i++, it.Next()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1926 | self->AllowThreadSuspension(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1927 | StackHandleScope<1> hs(self); |
| 1928 | Handle<mirror::ArtField> ifield(hs.NewHandle(AllocArtField(self))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1929 | if (UNLIKELY(ifield.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1930 | CHECK(self->IsExceptionPending()); // OOME. |
| 1931 | return; |
| 1932 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1933 | klass->SetInstanceField(i, ifield.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1934 | LoadField(dex_file, it, klass, ifield); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1935 | } |
| 1936 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1937 | // Load methods. |
| 1938 | if (it.NumDirectMethods() != 0) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1939 | // TODO: append direct methods to class object |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1940 | mirror::ObjectArray<mirror::ArtMethod>* directs = |
| 1941 | AllocArtMethodArray(self, it.NumDirectMethods()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1942 | if (UNLIKELY(directs == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1943 | CHECK(self->IsExceptionPending()); // OOME. |
| 1944 | return; |
| 1945 | } |
| 1946 | klass->SetDirectMethods(directs); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1947 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1948 | if (it.NumVirtualMethods() != 0) { |
| 1949 | // TODO: append direct methods to class object |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1950 | mirror::ObjectArray<mirror::ArtMethod>* virtuals = |
| 1951 | AllocArtMethodArray(self, it.NumVirtualMethods()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1952 | if (UNLIKELY(virtuals == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1953 | CHECK(self->IsExceptionPending()); // OOME. |
| 1954 | return; |
| 1955 | } |
| 1956 | klass->SetVirtualMethods(virtuals); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1957 | } |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 1958 | size_t class_def_method_index = 0; |
Jeff Hao | 68caf9e | 2014-09-03 13:48:16 -0700 | [diff] [blame] | 1959 | uint32_t last_dex_method_index = DexFile::kDexNoIndex; |
| 1960 | size_t last_class_def_method_index = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1961 | for (size_t i = 0; it.HasNextDirectMethod(); i++, it.Next()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1962 | self->AllowThreadSuspension(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1963 | StackHandleScope<1> hs(self); |
| 1964 | Handle<mirror::ArtMethod> method(hs.NewHandle(LoadMethod(self, dex_file, it, klass))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1965 | if (UNLIKELY(method.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1966 | CHECK(self->IsExceptionPending()); // OOME. |
| 1967 | return; |
| 1968 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1969 | klass->SetDirectMethod(i, method.Get()); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1970 | LinkCode(method, oat_class, class_def_method_index); |
Jeff Hao | 68caf9e | 2014-09-03 13:48:16 -0700 | [diff] [blame] | 1971 | uint32_t it_method_index = it.GetMemberIndex(); |
| 1972 | if (last_dex_method_index == it_method_index) { |
| 1973 | // duplicate case |
| 1974 | method->SetMethodIndex(last_class_def_method_index); |
| 1975 | } else { |
| 1976 | method->SetMethodIndex(class_def_method_index); |
| 1977 | last_dex_method_index = it_method_index; |
| 1978 | last_class_def_method_index = class_def_method_index; |
| 1979 | } |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 1980 | class_def_method_index++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1981 | } |
| 1982 | for (size_t i = 0; it.HasNextVirtualMethod(); i++, it.Next()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1983 | self->AllowThreadSuspension(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1984 | StackHandleScope<1> hs(self); |
| 1985 | Handle<mirror::ArtMethod> method(hs.NewHandle(LoadMethod(self, dex_file, it, klass))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1986 | if (UNLIKELY(method.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1987 | CHECK(self->IsExceptionPending()); // OOME. |
| 1988 | return; |
| 1989 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1990 | klass->SetVirtualMethod(i, method.Get()); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 1991 | DCHECK_EQ(class_def_method_index, it.NumDirectMethods() + i); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1992 | LinkCode(method, oat_class, class_def_method_index); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 1993 | class_def_method_index++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1994 | } |
| 1995 | DCHECK(!it.HasNext()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1996 | } |
| 1997 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1998 | void ClassLinker::LoadField(const DexFile& /*dex_file*/, const ClassDataItemIterator& it, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 1999 | Handle<mirror::Class> klass, |
| 2000 | Handle<mirror::ArtField> dst) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2001 | uint32_t field_idx = it.GetMemberIndex(); |
| 2002 | dst->SetDexFieldIndex(field_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2003 | dst->SetDeclaringClass(klass.Get()); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 2004 | dst->SetAccessFlags(it.GetFieldAccessFlags()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2005 | } |
| 2006 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2007 | mirror::ArtMethod* ClassLinker::LoadMethod(Thread* self, const DexFile& dex_file, |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2008 | const ClassDataItemIterator& it, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2009 | Handle<mirror::Class> klass) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2010 | uint32_t dex_method_idx = it.GetMemberIndex(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2011 | const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2012 | const char* method_name = dex_file.StringDataByIdx(method_id.name_idx_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2013 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2014 | mirror::ArtMethod* dst = AllocArtMethod(self); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2015 | if (UNLIKELY(dst == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2016 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2017 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2018 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2019 | DCHECK(dst->IsArtMethod()) << PrettyDescriptor(dst->GetClass()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2020 | |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 2021 | ScopedAssertNoThreadSuspension ants(self, "LoadMethod"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2022 | dst->SetDexMethodIndex(dex_method_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2023 | dst->SetDeclaringClass(klass.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2024 | dst->SetCodeItemOffset(it.GetMethodCodeItemOffset()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2025 | |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2026 | dst->SetDexCacheResolvedMethods(klass->GetDexCache()->GetResolvedMethods()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2027 | dst->SetDexCacheResolvedTypes(klass->GetDexCache()->GetResolvedTypes()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2028 | |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 2029 | uint32_t access_flags = it.GetMethodAccessFlags(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2030 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2031 | if (UNLIKELY(strcmp("finalize", method_name) == 0)) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2032 | // Set finalizable flag on declaring class. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2033 | if (strcmp("V", dex_file.GetShorty(method_id.proto_idx_)) == 0) { |
| 2034 | // Void return type. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2035 | if (klass->GetClassLoader() != nullptr) { // All non-boot finalizer methods are flagged. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2036 | klass->SetFinalizable(); |
| 2037 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2038 | std::string temp; |
| 2039 | const char* klass_descriptor = klass->GetDescriptor(&temp); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2040 | // The Enum class declares a "final" finalize() method to prevent subclasses from |
| 2041 | // introducing a finalizer. We don't want to set the finalizable flag for Enum or its |
| 2042 | // subclasses, so we exclude it here. |
| 2043 | // We also want to avoid setting the flag on Object, where we know that finalize() is |
| 2044 | // empty. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2045 | if (strcmp(klass_descriptor, "Ljava/lang/Object;") != 0 && |
| 2046 | strcmp(klass_descriptor, "Ljava/lang/Enum;") != 0) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2047 | klass->SetFinalizable(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2048 | } |
| 2049 | } |
| 2050 | } |
| 2051 | } else if (method_name[0] == '<') { |
| 2052 | // Fix broken access flags for initializers. Bug 11157540. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2053 | bool is_init = (strcmp("<init>", method_name) == 0); |
| 2054 | bool is_clinit = !is_init && (strcmp("<clinit>", method_name) == 0); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2055 | if (UNLIKELY(!is_init && !is_clinit)) { |
| 2056 | LOG(WARNING) << "Unexpected '<' at start of method name " << method_name; |
| 2057 | } else { |
| 2058 | if (UNLIKELY((access_flags & kAccConstructor) == 0)) { |
| 2059 | 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] | 2060 | << PrettyDescriptor(klass.Get()) << " in dex file " << dex_file.GetLocation(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2061 | access_flags |= kAccConstructor; |
| 2062 | } |
| 2063 | } |
| 2064 | } |
| 2065 | dst->SetAccessFlags(access_flags); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2066 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2067 | return dst; |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2068 | } |
| 2069 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2070 | void ClassLinker::AppendToBootClassPath(Thread* self, const DexFile& dex_file) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2071 | StackHandleScope<1> hs(self); |
| 2072 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(AllocDexCache(self, dex_file))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2073 | CHECK(dex_cache.Get() != nullptr) << "Failed to allocate dex cache for " |
| 2074 | << dex_file.GetLocation(); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2075 | AppendToBootClassPath(dex_file, dex_cache); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2076 | } |
| 2077 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2078 | void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2079 | Handle<mirror::DexCache> dex_cache) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2080 | CHECK(dex_cache.Get() != nullptr) << dex_file.GetLocation(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2081 | boot_class_path_.push_back(&dex_file); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2082 | RegisterDexFile(dex_file, dex_cache); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2083 | } |
| 2084 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2085 | bool ClassLinker::IsDexFileRegisteredLocked(const DexFile& dex_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2086 | dex_lock_.AssertSharedHeld(Thread::Current()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2087 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2088 | mirror::DexCache* dex_cache = GetDexCache(i); |
| 2089 | if (dex_cache->GetDexFile() == &dex_file) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2090 | return true; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2091 | } |
| 2092 | } |
| 2093 | return false; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2094 | } |
| 2095 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2096 | bool ClassLinker::IsDexFileRegistered(const DexFile& dex_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2097 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | 0691851 | 2011-10-16 23:39:12 -0700 | [diff] [blame] | 2098 | return IsDexFileRegisteredLocked(dex_file); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2099 | } |
| 2100 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2101 | void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2102 | Handle<mirror::DexCache> dex_cache) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2103 | dex_lock_.AssertExclusiveHeld(Thread::Current()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2104 | CHECK(dex_cache.Get() != nullptr) << dex_file.GetLocation(); |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 2105 | CHECK(dex_cache->GetLocation()->Equals(dex_file.GetLocation())) |
| 2106 | << dex_cache->GetLocation()->ToModifiedUtf8() << " " << dex_file.GetLocation(); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 2107 | dex_caches_.push_back(GcRoot<mirror::DexCache>(dex_cache.Get())); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2108 | dex_cache->SetDexFile(&dex_file); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 2109 | if (log_new_dex_caches_roots_) { |
| 2110 | // TODO: This is not safe if we can remove dex caches. |
| 2111 | new_dex_cache_roots_.push_back(dex_caches_.size() - 1); |
| 2112 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2113 | } |
| 2114 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2115 | void ClassLinker::RegisterDexFile(const DexFile& dex_file) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 2116 | Thread* self = Thread::Current(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 2117 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2118 | ReaderMutexLock mu(self, dex_lock_); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 2119 | if (IsDexFileRegisteredLocked(dex_file)) { |
| 2120 | return; |
| 2121 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2122 | } |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 2123 | // Don't alloc while holding the lock, since allocation may need to |
| 2124 | // suspend all threads and another thread may need the dex_lock_ to |
| 2125 | // get to a suspend point. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2126 | StackHandleScope<1> hs(self); |
| 2127 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(AllocDexCache(self, dex_file))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2128 | CHECK(dex_cache.Get() != nullptr) << "Failed to allocate dex cache for " |
| 2129 | << dex_file.GetLocation(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 2130 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2131 | WriterMutexLock mu(self, dex_lock_); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 2132 | if (IsDexFileRegisteredLocked(dex_file)) { |
| 2133 | return; |
| 2134 | } |
| 2135 | RegisterDexFileLocked(dex_file, dex_cache); |
| 2136 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2137 | } |
| 2138 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2139 | void ClassLinker::RegisterDexFile(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2140 | Handle<mirror::DexCache> dex_cache) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2141 | WriterMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2142 | RegisterDexFileLocked(dex_file, dex_cache); |
| 2143 | } |
| 2144 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2145 | mirror::DexCache* ClassLinker::FindDexCache(const DexFile& dex_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2146 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2147 | // Search assuming unique-ness of dex file. |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2148 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2149 | mirror::DexCache* dex_cache = GetDexCache(i); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2150 | if (dex_cache->GetDexFile() == &dex_file) { |
| 2151 | return dex_cache; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2152 | } |
| 2153 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2154 | // Search matching by location name. |
| 2155 | std::string location(dex_file.GetLocation()); |
| 2156 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2157 | mirror::DexCache* dex_cache = GetDexCache(i); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2158 | if (dex_cache->GetDexFile()->GetLocation() == location) { |
| 2159 | return dex_cache; |
| 2160 | } |
| 2161 | } |
| 2162 | // Failure, dump diagnostic and abort. |
| 2163 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2164 | mirror::DexCache* dex_cache = GetDexCache(i); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2165 | LOG(ERROR) << "Registered dex file " << i << " = " << dex_cache->GetDexFile()->GetLocation(); |
| 2166 | } |
| 2167 | LOG(FATAL) << "Failed to find DexCache for DexFile " << location; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 2168 | UNREACHABLE(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2169 | } |
| 2170 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2171 | void ClassLinker::FixupDexCaches(mirror::ArtMethod* resolution_method) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2172 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2173 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2174 | mirror::DexCache* dex_cache = GetDexCache(i); |
| 2175 | dex_cache->Fixup(resolution_method); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2176 | } |
| 2177 | } |
| 2178 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2179 | mirror::Class* ClassLinker::CreatePrimitiveClass(Thread* self, Primitive::Type type) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2180 | mirror::Class* klass = AllocClass(self, mirror::Class::PrimitiveClassSize()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2181 | if (UNLIKELY(klass == nullptr)) { |
| 2182 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2183 | } |
| 2184 | return InitializePrimitiveClass(klass, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2185 | } |
| 2186 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2187 | mirror::Class* ClassLinker::InitializePrimitiveClass(mirror::Class* primitive_class, |
| 2188 | Primitive::Type type) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2189 | CHECK(primitive_class != nullptr); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 2190 | // Must hold lock on object when initializing. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2191 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2192 | StackHandleScope<1> hs(self); |
| 2193 | Handle<mirror::Class> h_class(hs.NewHandle(primitive_class)); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 2194 | ObjectLock<mirror::Class> lock(self, h_class); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2195 | h_class->SetAccessFlags(kAccPublic | kAccFinal | kAccAbstract); |
| 2196 | h_class->SetPrimitiveType(type); |
| 2197 | mirror::Class::SetStatus(h_class, mirror::Class::kStatusInitialized, self); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2198 | const char* descriptor = Primitive::Descriptor(type); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2199 | mirror::Class* existing = InsertClass(descriptor, h_class.Get(), |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2200 | ComputeModifiedUtf8Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2201 | CHECK(existing == nullptr) << "InitPrimitiveClass(" << type << ") failed"; |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2202 | return h_class.Get(); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 2203 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2204 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 2205 | // Create an array class (i.e. the class object for the array, not the |
| 2206 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 2207 | // "[Ljava/lang/String;". |
| 2208 | // |
| 2209 | // If "descriptor" refers to an array of primitives, look up the |
| 2210 | // primitive type's internally-generated class object. |
| 2211 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2212 | // "class_loader" is the class loader of the class that's referring to |
| 2213 | // us. It's used to ensure that we're looking for the element type in |
| 2214 | // the right context. It does NOT become the class loader for the |
| 2215 | // array class; that always comes from the base element class. |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 2216 | // |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2217 | // Returns nullptr with an exception raised on failure. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2218 | mirror::Class* ClassLinker::CreateArrayClass(Thread* self, const char* descriptor, size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2219 | Handle<mirror::ClassLoader> class_loader) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2220 | // Identify the underlying component type |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2221 | CHECK_EQ('[', descriptor[0]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2222 | StackHandleScope<2> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2223 | MutableHandle<mirror::Class> component_type(hs.NewHandle(FindClass(self, descriptor + 1, |
| 2224 | class_loader))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2225 | if (component_type.Get() == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 2226 | DCHECK(self->IsExceptionPending()); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 2227 | // We need to accept erroneous classes as component types. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2228 | const size_t component_hash = ComputeModifiedUtf8Hash(descriptor + 1); |
| 2229 | component_type.Assign(LookupClass(self, descriptor + 1, component_hash, class_loader.Get())); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 2230 | if (component_type.Get() == nullptr) { |
| 2231 | DCHECK(self->IsExceptionPending()); |
| 2232 | return nullptr; |
| 2233 | } else { |
| 2234 | self->ClearException(); |
| 2235 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2236 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 2237 | if (UNLIKELY(component_type->IsPrimitiveVoid())) { |
| 2238 | ThrowNoClassDefFoundError("Attempt to create array of void primitive type"); |
| 2239 | return nullptr; |
| 2240 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2241 | // See if the component type is already loaded. Array classes are |
| 2242 | // always associated with the class loader of their underlying |
| 2243 | // element type -- an array of Strings goes with the loader for |
| 2244 | // java/lang/String -- so we need to look for it there. (The |
| 2245 | // caller should have checked for the existence of the class |
| 2246 | // before calling here, but they did so with *their* class loader, |
| 2247 | // not the component type's loader.) |
| 2248 | // |
| 2249 | // If we find it, the caller adds "loader" to the class' initiating |
| 2250 | // loader list, which should prevent us from going through this again. |
| 2251 | // |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2252 | // This call is unnecessary if "loader" and "component_type->GetClassLoader()" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2253 | // are the same, because our caller (FindClass) just did the |
| 2254 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 2255 | // because we effectively do this lookup again when we add the new |
| 2256 | // class to the hash table --- necessary because of possible races with |
| 2257 | // other threads.) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2258 | if (class_loader.Get() != component_type->GetClassLoader()) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2259 | mirror::Class* new_class = LookupClass(self, descriptor, hash, component_type->GetClassLoader()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2260 | if (new_class != nullptr) { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 2261 | return new_class; |
| 2262 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2263 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 2264 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2265 | // Fill out the fields in the Class. |
| 2266 | // |
| 2267 | // It is possible to execute some methods against arrays, because |
| 2268 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 2269 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 2270 | // |
| 2271 | // Array classes are simple enough that we don't need to do a full |
| 2272 | // link step. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2273 | auto new_class = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2274 | if (UNLIKELY(!init_done_)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2275 | // Classes that were hand created, ie not by FindSystemClass |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2276 | if (strcmp(descriptor, "[Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2277 | new_class.Assign(GetClassRoot(kClassArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2278 | } else if (strcmp(descriptor, "[Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2279 | new_class.Assign(GetClassRoot(kObjectArrayClass)); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2280 | } else if (strcmp(descriptor, GetClassRootDescriptor(kJavaLangStringArrayClass)) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2281 | new_class.Assign(GetClassRoot(kJavaLangStringArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2282 | } else if (strcmp(descriptor, |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2283 | GetClassRootDescriptor(kJavaLangReflectArtMethodArrayClass)) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2284 | new_class.Assign(GetClassRoot(kJavaLangReflectArtMethodArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2285 | } else if (strcmp(descriptor, |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2286 | GetClassRootDescriptor(kJavaLangReflectArtFieldArrayClass)) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2287 | new_class.Assign(GetClassRoot(kJavaLangReflectArtFieldArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2288 | } else if (strcmp(descriptor, "[C") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2289 | new_class.Assign(GetClassRoot(kCharArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2290 | } else if (strcmp(descriptor, "[I") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2291 | new_class.Assign(GetClassRoot(kIntArrayClass)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2292 | } |
| 2293 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2294 | if (new_class.Get() == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2295 | new_class.Assign(AllocClass(self, mirror::Array::ClassSize())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2296 | if (new_class.Get() == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 2297 | return nullptr; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2298 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2299 | new_class->SetComponentType(component_type.Get()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2300 | } |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 2301 | 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] | 2302 | DCHECK(new_class->GetComponentType() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2303 | mirror::Class* java_lang_Object = GetClassRoot(kJavaLangObject); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2304 | new_class->SetSuperClass(java_lang_Object); |
| 2305 | new_class->SetVTable(java_lang_Object->GetVTable()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2306 | new_class->SetPrimitiveType(Primitive::kPrimNot); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2307 | new_class->SetClassLoader(component_type->GetClassLoader()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2308 | mirror::Class::SetStatus(new_class, mirror::Class::kStatusLoaded, self); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 2309 | { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2310 | StackHandleScope<mirror::Class::kImtSize> hs2(self, |
| 2311 | Runtime::Current()->GetImtUnimplementedMethod()); |
| 2312 | new_class->PopulateEmbeddedImtAndVTable(&hs2); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 2313 | } |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2314 | mirror::Class::SetStatus(new_class, mirror::Class::kStatusInitialized, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2315 | // don't need to set new_class->SetObjectSize(..) |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 2316 | // because Object::SizeOf delegates to Array::SizeOf |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2317 | |
| 2318 | |
| 2319 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 2320 | // interfaces. We need to set that up here, so that stuff like |
| 2321 | // "instanceof" works right. |
| 2322 | // |
| 2323 | // Note: The GC could run during the call to FindSystemClass, |
| 2324 | // so we need to make sure the class object is GC-valid while we're in |
| 2325 | // there. Do this by clearing the interface list so the GC will just |
| 2326 | // think that the entries are null. |
| 2327 | |
| 2328 | |
| 2329 | // Use the single, global copies of "interfaces" and "iftable" |
| 2330 | // (remember not to free them for arrays). |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2331 | { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 2332 | mirror::IfTable* array_iftable = array_iftable_.Read(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2333 | CHECK(array_iftable != nullptr); |
| 2334 | new_class->SetIfTable(array_iftable); |
| 2335 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2336 | |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 2337 | // Inherit access flags from the component type. |
| 2338 | int access_flags = new_class->GetComponentType()->GetAccessFlags(); |
| 2339 | // Lose any implementation detail flags; in particular, arrays aren't finalizable. |
| 2340 | access_flags &= kAccJavaFlagsMask; |
| 2341 | // 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] | 2342 | // and remove "interface". |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 2343 | access_flags |= kAccAbstract | kAccFinal; |
| 2344 | access_flags &= ~kAccInterface; |
| 2345 | |
| 2346 | new_class->SetAccessFlags(access_flags); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2347 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2348 | mirror::Class* existing = InsertClass(descriptor, new_class.Get(), hash); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 2349 | if (existing == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2350 | return new_class.Get(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2351 | } |
| 2352 | // Another thread must have loaded the class after we |
| 2353 | // started but before we finished. Abandon what we've |
| 2354 | // done. |
| 2355 | // |
| 2356 | // (Yes, this happens.) |
| 2357 | |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 2358 | return existing; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 2359 | } |
| 2360 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2361 | mirror::Class* ClassLinker::FindPrimitiveClass(char type) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2362 | switch (type) { |
| 2363 | case 'B': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2364 | return GetClassRoot(kPrimitiveByte); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2365 | case 'C': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2366 | return GetClassRoot(kPrimitiveChar); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2367 | case 'D': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2368 | return GetClassRoot(kPrimitiveDouble); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2369 | case 'F': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2370 | return GetClassRoot(kPrimitiveFloat); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2371 | case 'I': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2372 | return GetClassRoot(kPrimitiveInt); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2373 | case 'J': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2374 | return GetClassRoot(kPrimitiveLong); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2375 | case 'S': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2376 | return GetClassRoot(kPrimitiveShort); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2377 | case 'Z': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2378 | return GetClassRoot(kPrimitiveBoolean); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2379 | case 'V': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2380 | return GetClassRoot(kPrimitiveVoid); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2381 | default: |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2382 | break; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 2383 | } |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 2384 | std::string printable_type(PrintableChar(type)); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2385 | ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2386 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2387 | } |
| 2388 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2389 | mirror::Class* ClassLinker::InsertClass(const char* descriptor, mirror::Class* klass, |
| 2390 | size_t hash) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2391 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2392 | mirror::DexCache* dex_cache = klass->GetDexCache(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 2393 | std::string source; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2394 | if (dex_cache != nullptr) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 2395 | source += " from "; |
| 2396 | source += dex_cache->GetLocation()->ToModifiedUtf8(); |
| 2397 | } |
| 2398 | LOG(INFO) << "Loaded class " << descriptor << source; |
| 2399 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2400 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2401 | mirror::Class* existing = LookupClassFromTableLocked(descriptor, klass->GetClassLoader(), hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2402 | if (existing != nullptr) { |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 2403 | return existing; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 2404 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2405 | if (kIsDebugBuild && !klass->IsTemp() && klass->GetClassLoader() == nullptr && |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2406 | dex_cache_image_class_lookup_required_) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2407 | // Check a class loaded with the system class loader matches one in the image if the class |
| 2408 | // is in the image. |
| 2409 | existing = LookupClassFromImage(descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2410 | if (existing != nullptr) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2411 | CHECK_EQ(klass, existing); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2412 | } |
| 2413 | } |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 2414 | VerifyObject(klass); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2415 | class_table_.InsertWithHash(GcRoot<mirror::Class>(klass), hash); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 2416 | if (log_new_class_table_roots_) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2417 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 2418 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2419 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2420 | } |
| 2421 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2422 | mirror::Class* ClassLinker::UpdateClass(const char* descriptor, mirror::Class* klass, |
| 2423 | size_t hash) { |
| 2424 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2425 | auto existing_it = class_table_.FindWithHash(std::make_pair(descriptor, klass->GetClassLoader()), |
| 2426 | hash); |
| 2427 | if (existing_it == class_table_.end()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2428 | CHECK(klass->IsProxyClass()); |
| 2429 | return nullptr; |
| 2430 | } |
| 2431 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2432 | mirror::Class* existing = existing_it->Read(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2433 | CHECK_NE(existing, klass) << descriptor; |
| 2434 | CHECK(!existing->IsResolved()) << descriptor; |
| 2435 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusResolving) << descriptor; |
| 2436 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2437 | CHECK(!klass->IsTemp()) << descriptor; |
| 2438 | if (kIsDebugBuild && klass->GetClassLoader() == nullptr && |
| 2439 | dex_cache_image_class_lookup_required_) { |
| 2440 | // Check a class loaded with the system class loader matches one in the image if the class |
| 2441 | // is in the image. |
| 2442 | existing = LookupClassFromImage(descriptor); |
| 2443 | if (existing != nullptr) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2444 | CHECK_EQ(klass, existing) << descriptor; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2445 | } |
| 2446 | } |
| 2447 | VerifyObject(klass); |
| 2448 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2449 | // Update the element in the hash set. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2450 | *existing_it = GcRoot<mirror::Class>(klass); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2451 | if (log_new_class_table_roots_) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2452 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2453 | } |
| 2454 | |
| 2455 | return existing; |
| 2456 | } |
| 2457 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2458 | bool ClassLinker::RemoveClass(const char* descriptor, mirror::ClassLoader* class_loader) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2459 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2460 | auto pair = std::make_pair(descriptor, class_loader); |
| 2461 | auto it = class_table_.Find(pair); |
| 2462 | if (it != class_table_.end()) { |
| 2463 | class_table_.Erase(it); |
| 2464 | return true; |
| 2465 | } |
| 2466 | it = pre_zygote_class_table_.Find(pair); |
| 2467 | if (it != pre_zygote_class_table_.end()) { |
| 2468 | pre_zygote_class_table_.Erase(it); |
| 2469 | return true; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 2470 | } |
| 2471 | return false; |
| 2472 | } |
| 2473 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2474 | mirror::Class* ClassLinker::LookupClass(Thread* self, const char* descriptor, size_t hash, |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2475 | mirror::ClassLoader* class_loader) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2476 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2477 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2478 | mirror::Class* result = LookupClassFromTableLocked(descriptor, class_loader, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2479 | if (result != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2480 | return result; |
| 2481 | } |
Sameer Abu Asal | 2c6de22 | 2013-05-02 17:38:59 -0700 | [diff] [blame] | 2482 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2483 | if (class_loader != nullptr || !dex_cache_image_class_lookup_required_) { |
| 2484 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2485 | } else { |
| 2486 | // Lookup failed but need to search dex_caches_. |
| 2487 | mirror::Class* result = LookupClassFromImage(descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2488 | if (result != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2489 | InsertClass(descriptor, result, hash); |
| 2490 | } else { |
| 2491 | // Searching the image dex files/caches failed, we don't want to get into this situation |
| 2492 | // often as map searches are faster, so after kMaxFailedDexCacheLookups move all image |
| 2493 | // classes into the class table. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2494 | constexpr uint32_t kMaxFailedDexCacheLookups = 1000; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2495 | if (++failed_dex_cache_class_lookups_ > kMaxFailedDexCacheLookups) { |
| 2496 | MoveImageClassesToClassTable(); |
| 2497 | } |
| 2498 | } |
| 2499 | return result; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2500 | } |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 2501 | } |
| 2502 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2503 | mirror::Class* ClassLinker::LookupClassFromTableLocked(const char* descriptor, |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2504 | mirror::ClassLoader* class_loader, |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2505 | size_t hash) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2506 | auto descriptor_pair = std::make_pair(descriptor, class_loader); |
| 2507 | auto it = pre_zygote_class_table_.FindWithHash(descriptor_pair, hash); |
| 2508 | if (it == pre_zygote_class_table_.end()) { |
| 2509 | it = class_table_.FindWithHash(descriptor_pair, hash); |
| 2510 | if (it == class_table_.end()) { |
| 2511 | return nullptr; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 2512 | } |
| 2513 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2514 | return it->Read(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2515 | } |
| 2516 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2517 | static mirror::ObjectArray<mirror::DexCache>* GetImageDexCaches() |
| 2518 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2519 | gc::space::ImageSpace* image = Runtime::Current()->GetHeap()->GetImageSpace(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2520 | CHECK(image != nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2521 | mirror::Object* root = image->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches); |
| 2522 | return root->AsObjectArray<mirror::DexCache>(); |
| 2523 | } |
| 2524 | |
| 2525 | void ClassLinker::MoveImageClassesToClassTable() { |
| 2526 | Thread* self = Thread::Current(); |
| 2527 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2528 | if (!dex_cache_image_class_lookup_required_) { |
| 2529 | return; // All dex cache classes are already in the class table. |
| 2530 | } |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 2531 | ScopedAssertNoThreadSuspension ants(self, "Moving image classes to class table"); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2532 | mirror::ObjectArray<mirror::DexCache>* dex_caches = GetImageDexCaches(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2533 | std::string temp; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2534 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 2535 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 2536 | mirror::ObjectArray<mirror::Class>* types = dex_cache->GetResolvedTypes(); |
| 2537 | for (int32_t j = 0; j < types->GetLength(); j++) { |
| 2538 | mirror::Class* klass = types->Get(j); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2539 | if (klass != nullptr) { |
| 2540 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2541 | const char* descriptor = klass->GetDescriptor(&temp); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2542 | size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2543 | mirror::Class* existing = LookupClassFromTableLocked(descriptor, nullptr, hash); |
| 2544 | if (existing != nullptr) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2545 | CHECK_EQ(existing, klass) << PrettyClassAndClassLoader(existing) << " != " |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2546 | << PrettyClassAndClassLoader(klass); |
| 2547 | } else { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2548 | class_table_.Insert(GcRoot<mirror::Class>(klass)); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 2549 | if (log_new_class_table_roots_) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2550 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 2551 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2552 | } |
| 2553 | } |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 2554 | } |
| 2555 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2556 | dex_cache_image_class_lookup_required_ = false; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2557 | } |
| 2558 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2559 | void ClassLinker::MoveClassTableToPreZygote() { |
| 2560 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 2561 | DCHECK(pre_zygote_class_table_.Empty()); |
| 2562 | pre_zygote_class_table_ = std::move(class_table_); |
| 2563 | class_table_.Clear(); |
| 2564 | } |
| 2565 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2566 | mirror::Class* ClassLinker::LookupClassFromImage(const char* descriptor) { |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 2567 | ScopedAssertNoThreadSuspension ants(Thread::Current(), "Image class lookup"); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2568 | mirror::ObjectArray<mirror::DexCache>* dex_caches = GetImageDexCaches(); |
| 2569 | for (int32_t i = 0; i < dex_caches->GetLength(); ++i) { |
| 2570 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 2571 | const DexFile* dex_file = dex_cache->GetDexFile(); |
Vladimir Marko | 801a811 | 2014-01-06 14:28:16 +0000 | [diff] [blame] | 2572 | // Try binary searching the string/type index. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2573 | const DexFile::StringId* string_id = dex_file->FindStringId(descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2574 | if (string_id != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2575 | const DexFile::TypeId* type_id = |
| 2576 | dex_file->FindTypeId(dex_file->GetIndexForStringId(*string_id)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2577 | if (type_id != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2578 | uint16_t type_idx = dex_file->GetIndexForTypeId(*type_id); |
| 2579 | mirror::Class* klass = dex_cache->GetResolvedType(type_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2580 | if (klass != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2581 | return klass; |
| 2582 | } |
| 2583 | } |
| 2584 | } |
| 2585 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2586 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2587 | } |
| 2588 | |
| 2589 | void ClassLinker::LookupClasses(const char* descriptor, std::vector<mirror::Class*>& result) { |
| 2590 | result.clear(); |
| 2591 | if (dex_cache_image_class_lookup_required_) { |
| 2592 | MoveImageClassesToClassTable(); |
| 2593 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2594 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 2595 | while (true) { |
| 2596 | auto it = class_table_.Find(descriptor); |
| 2597 | if (it == class_table_.end()) { |
| 2598 | break; |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 2599 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2600 | result.push_back(it->Read()); |
| 2601 | class_table_.Erase(it); |
| 2602 | } |
| 2603 | for (mirror::Class* k : result) { |
| 2604 | class_table_.Insert(GcRoot<mirror::Class>(k)); |
| 2605 | } |
| 2606 | size_t pre_zygote_start = result.size(); |
| 2607 | // Now handle the pre zygote table. |
| 2608 | // Note: This dirties the pre-zygote table but shouldn't be an issue since LookupClasses is only |
| 2609 | // called from the debugger. |
| 2610 | while (true) { |
| 2611 | auto it = pre_zygote_class_table_.Find(descriptor); |
| 2612 | if (it == pre_zygote_class_table_.end()) { |
| 2613 | break; |
| 2614 | } |
| 2615 | result.push_back(it->Read()); |
| 2616 | pre_zygote_class_table_.Erase(it); |
| 2617 | } |
| 2618 | for (size_t i = pre_zygote_start; i < result.size(); ++i) { |
| 2619 | pre_zygote_class_table_.Insert(GcRoot<mirror::Class>(result[i])); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 2620 | } |
| 2621 | } |
| 2622 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2623 | void ClassLinker::VerifyClass(Thread* self, Handle<mirror::Class> klass) { |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 2624 | // TODO: assert that the monitor on the Class is held |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 2625 | ObjectLock<mirror::Class> lock(self, klass); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 2626 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 2627 | // Don't attempt to re-verify if already sufficiently verified. |
Andreas Gampe | bb0c7f6 | 2014-09-11 10:59:33 -0700 | [diff] [blame] | 2628 | if (klass->IsVerified()) { |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 2629 | EnsurePreverifiedMethods(klass); |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 2630 | return; |
| 2631 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2632 | if (klass->IsCompileTimeVerified() && Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | bb0c7f6 | 2014-09-11 10:59:33 -0700 | [diff] [blame] | 2633 | return; |
| 2634 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 2635 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 2636 | // The class might already be erroneous, for example at compile time if we attempted to verify |
| 2637 | // this class as a parent to another. |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 2638 | if (klass->IsErroneous()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2639 | ThrowEarlierClassFailure(klass.Get()); |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 2640 | return; |
| 2641 | } |
| 2642 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2643 | if (klass->GetStatus() == mirror::Class::kStatusResolved) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2644 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerifying, self); |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 2645 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2646 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2647 | << PrettyClass(klass.Get()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2648 | CHECK(!Runtime::Current()->IsAotCompiler()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2649 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerifyingAtRuntime, self); |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 2650 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 2651 | |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 2652 | // Skip verification if disabled. |
| 2653 | if (!Runtime::Current()->IsVerificationEnabled()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2654 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, self); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 2655 | EnsurePreverifiedMethods(klass); |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 2656 | return; |
| 2657 | } |
| 2658 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 2659 | // Verify super class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2660 | StackHandleScope<2> hs(self); |
| 2661 | Handle<mirror::Class> super(hs.NewHandle(klass->GetSuperClass())); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2662 | if (super.Get() != nullptr) { |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 2663 | // Acquire lock to prevent races on verifying the super class. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2664 | ObjectLock<mirror::Class> super_lock(self, super); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 2665 | |
| 2666 | if (!super->IsVerified() && !super->IsErroneous()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2667 | VerifyClass(self, super); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 2668 | } |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 2669 | if (!super->IsCompileTimeVerified()) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 2670 | std::string error_msg( |
| 2671 | StringPrintf("Rejecting class %s that attempts to sub-class erroneous class %s", |
| 2672 | PrettyDescriptor(klass.Get()).c_str(), |
| 2673 | PrettyDescriptor(super.Get()).c_str())); |
Andreas Gampe | e4301ff | 2015-02-17 19:25:29 -0800 | [diff] [blame] | 2674 | LOG(WARNING) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2675 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2676 | if (cause.Get() != nullptr) { |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 2677 | self->ClearException(); |
| 2678 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2679 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
| 2680 | if (cause.Get() != nullptr) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2681 | self->GetException()->SetCause(cause.Get()); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 2682 | } |
Jeff Hao | 22cb09b | 2013-12-12 14:29:15 -0800 | [diff] [blame] | 2683 | ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2684 | if (Runtime::Current()->IsAotCompiler()) { |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 2685 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
| 2686 | } |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2687 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 2688 | return; |
| 2689 | } |
| 2690 | } |
| 2691 | |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 2692 | // 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] | 2693 | const DexFile& dex_file = *klass->GetDexCache()->GetDexFile(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2694 | mirror::Class::Status oat_file_class_status(mirror::Class::kStatusNotReady); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2695 | bool preverified = VerifyClassUsingOatFile(dex_file, klass.Get(), oat_file_class_status); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2696 | if (oat_file_class_status == mirror::Class::kStatusError) { |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 2697 | VLOG(class_linker) << "Skipping runtime verification of erroneous class " |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2698 | << PrettyDescriptor(klass.Get()) << " in " |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 2699 | << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2700 | ThrowVerifyError(klass.Get(), "Rejecting class %s because it failed compile-time verification", |
| 2701 | PrettyDescriptor(klass.Get()).c_str()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2702 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 2703 | return; |
| 2704 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 2705 | verifier::MethodVerifier::FailureKind verifier_failure = verifier::MethodVerifier::kNoFailure; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2706 | std::string error_msg; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 2707 | if (!preverified) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2708 | verifier_failure = verifier::MethodVerifier::VerifyClass(self, klass.Get(), |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2709 | Runtime::Current()->IsAotCompiler(), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2710 | &error_msg); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 2711 | } |
| 2712 | if (preverified || verifier_failure != verifier::MethodVerifier::kHardFailure) { |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 2713 | if (!preverified && verifier_failure != verifier::MethodVerifier::kNoFailure) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2714 | VLOG(class_linker) << "Soft verification failure in class " << PrettyDescriptor(klass.Get()) |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 2715 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 2716 | << " because: " << error_msg; |
| 2717 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 2718 | self->AssertNoPendingException(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 2719 | // Make sure all classes referenced by catch blocks are resolved. |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 2720 | ResolveClassExceptionHandlerTypes(dex_file, klass); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 2721 | if (verifier_failure == verifier::MethodVerifier::kNoFailure) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 2722 | // Even though there were no verifier failures we need to respect whether the super-class |
| 2723 | // was verified or requiring runtime reverification. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2724 | if (super.Get() == nullptr || super->IsVerified()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2725 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 2726 | } else { |
| 2727 | CHECK_EQ(super->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2728 | mirror::Class::SetStatus(klass, mirror::Class::kStatusRetryVerificationAtRuntime, self); |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 2729 | // Pretend a soft failure occured so that we don't consider the class verified below. |
| 2730 | verifier_failure = verifier::MethodVerifier::kSoftFailure; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 2731 | } |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 2732 | } else { |
| 2733 | CHECK_EQ(verifier_failure, verifier::MethodVerifier::kSoftFailure); |
| 2734 | // Soft failures at compile time should be retried at runtime. Soft |
| 2735 | // failures at runtime will be handled by slow paths in the generated |
| 2736 | // code. Set status accordingly. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2737 | if (Runtime::Current()->IsAotCompiler()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2738 | mirror::Class::SetStatus(klass, mirror::Class::kStatusRetryVerificationAtRuntime, self); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 2739 | } else { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2740 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, self); |
Andreas Gampe | bb0c7f6 | 2014-09-11 10:59:33 -0700 | [diff] [blame] | 2741 | // As this is a fake verified status, make sure the methods are _not_ marked preverified |
| 2742 | // later. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2743 | klass->SetPreverified(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 2744 | } |
| 2745 | } |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 2746 | } else { |
Andreas Gampe | e4301ff | 2015-02-17 19:25:29 -0800 | [diff] [blame] | 2747 | LOG(WARNING) << "Verification failed on class " << PrettyDescriptor(klass.Get()) |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 2748 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 2749 | << " because: " << error_msg; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2750 | self->AssertNoPendingException(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2751 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2752 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 2753 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 2754 | if (preverified || verifier_failure == verifier::MethodVerifier::kNoFailure) { |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 2755 | // 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] | 2756 | // Let the interpreter know it by setting the kAccPreverified flag onto each |
| 2757 | // method. |
| 2758 | // Note: we're going here during compilation and at runtime. When we set the |
| 2759 | // kAccPreverified flag when compiling image classes, the flag is recorded |
| 2760 | // in the image and is set when loading the image. |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 2761 | EnsurePreverifiedMethods(klass); |
| 2762 | } |
| 2763 | } |
| 2764 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2765 | void ClassLinker::EnsurePreverifiedMethods(Handle<mirror::Class> klass) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2766 | if (!klass->IsPreverified()) { |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 2767 | klass->SetPreverifiedFlagOnAllMethods(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2768 | klass->SetPreverified(); |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 2769 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 2770 | } |
| 2771 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2772 | bool ClassLinker::VerifyClassUsingOatFile(const DexFile& dex_file, mirror::Class* klass, |
| 2773 | mirror::Class::Status& oat_file_class_status) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 2774 | // If we're compiling, we can only verify the class using the oat file if |
| 2775 | // we are not compiling the image or if the class we're verifying is not part of |
| 2776 | // the app. In other words, we will only check for preverification of bootclasspath |
| 2777 | // classes. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2778 | if (Runtime::Current()->IsAotCompiler()) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 2779 | // Are we compiling the bootclasspath? |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 2780 | if (Runtime::Current()->GetCompilerCallbacks()->IsBootImage()) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 2781 | return false; |
| 2782 | } |
| 2783 | // We are compiling an app (not the image). |
| 2784 | |
| 2785 | // Is this an app class? (I.e. not a bootclasspath class) |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2786 | if (klass->GetClassLoader() != nullptr) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 2787 | return false; |
| 2788 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 2789 | } |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 2790 | |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 2791 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2792 | // In case we run without an image there won't be a backing oat file. |
| 2793 | if (oat_dex_file == nullptr) { |
Anwar Ghuloum | ad256bb | 2013-07-18 14:58:55 -0700 | [diff] [blame] | 2794 | return false; |
| 2795 | } |
| 2796 | |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 2797 | // We may be running with a preopted oat file but without image. In this case, |
| 2798 | // we don't skip verification of preverified classes to ensure we initialize |
| 2799 | // dex caches with all types resolved during verification. |
| 2800 | // We need to trust image classes, as these might be coming out of a pre-opted, quickened boot |
| 2801 | // image (that we just failed loading), and the verifier can't be run on quickened opcodes when |
| 2802 | // the runtime isn't started. On the other hand, app classes can be re-verified even if they are |
| 2803 | // already pre-opted, as then the runtime is started. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2804 | if (!Runtime::Current()->IsAotCompiler() && |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 2805 | !Runtime::Current()->GetHeap()->HasImageSpace() && |
| 2806 | klass->GetClassLoader() != nullptr) { |
| 2807 | return false; |
| 2808 | } |
| 2809 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2810 | uint16_t class_def_index = klass->GetDexClassDefIndex(); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2811 | oat_file_class_status = oat_dex_file->GetOatClass(class_def_index).GetStatus(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2812 | if (oat_file_class_status == mirror::Class::kStatusVerified || |
| 2813 | oat_file_class_status == mirror::Class::kStatusInitialized) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 2814 | return true; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 2815 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2816 | if (oat_file_class_status == mirror::Class::kStatusRetryVerificationAtRuntime) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 2817 | // Compile time verification failed with a soft error. Compile time verification can fail |
| 2818 | // because we have incomplete type information. Consider the following: |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 2819 | // class ... { |
| 2820 | // Foo x; |
| 2821 | // .... () { |
| 2822 | // if (...) { |
| 2823 | // v1 gets assigned a type of resolved class Foo |
| 2824 | // } else { |
| 2825 | // v1 gets assigned a type of unresolved class Bar |
| 2826 | // } |
| 2827 | // iput x = v1 |
| 2828 | // } } |
| 2829 | // when we merge v1 following the if-the-else it results in Conflict |
| 2830 | // (see verifier::RegType::Merge) as we can't know the type of Bar and we could possibly be |
| 2831 | // allowing an unsafe assignment to the field x in the iput (javac may have compiled this as |
| 2832 | // it knew Bar was a sub-class of Foo, but for us this may have been moved into a separate apk |
| 2833 | // at compile time). |
| 2834 | return false; |
| 2835 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2836 | if (oat_file_class_status == mirror::Class::kStatusError) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 2837 | // Compile time verification failed with a hard error. This is caused by invalid instructions |
| 2838 | // in the class. These errors are unrecoverable. |
| 2839 | return false; |
| 2840 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2841 | if (oat_file_class_status == mirror::Class::kStatusNotReady) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 2842 | // Status is uninitialized if we couldn't determine the status at compile time, for example, |
| 2843 | // not loading the class. |
| 2844 | // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy |
| 2845 | // isn't a problem and this case shouldn't occur |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 2846 | return false; |
| 2847 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2848 | std::string temp; |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 2849 | LOG(FATAL) << "Unexpected class status: " << oat_file_class_status |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2850 | << " " << dex_file.GetLocation() << " " << PrettyClass(klass) << " " |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2851 | << klass->GetDescriptor(&temp); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 2852 | UNREACHABLE(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 2853 | } |
| 2854 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2855 | void ClassLinker::ResolveClassExceptionHandlerTypes(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2856 | Handle<mirror::Class> klass) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 2857 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
| 2858 | ResolveMethodExceptionHandlerTypes(dex_file, klass->GetDirectMethod(i)); |
| 2859 | } |
| 2860 | for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { |
| 2861 | ResolveMethodExceptionHandlerTypes(dex_file, klass->GetVirtualMethod(i)); |
| 2862 | } |
| 2863 | } |
| 2864 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2865 | void ClassLinker::ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2866 | mirror::ArtMethod* method) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 2867 | // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod. |
| 2868 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2869 | if (code_item == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 2870 | return; // native or abstract method |
| 2871 | } |
| 2872 | if (code_item->tries_size_ == 0) { |
| 2873 | return; // nothing to process |
| 2874 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2875 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item, 0); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 2876 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 2877 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 2878 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| 2879 | CatchHandlerIterator iterator(handlers_ptr); |
| 2880 | for (; iterator.HasNext(); iterator.Next()) { |
| 2881 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 2882 | // unresolved exception types will be ignored by exception delivery |
| 2883 | if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2884 | mirror::Class* exception_type = linker->ResolveType(iterator.GetHandlerTypeIndex(), method); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2885 | if (exception_type == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 2886 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 2887 | Thread::Current()->ClearException(); |
| 2888 | } |
| 2889 | } |
| 2890 | } |
| 2891 | handlers_ptr = iterator.EndDataPointer(); |
| 2892 | } |
| 2893 | } |
| 2894 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2895 | static void CheckProxyConstructor(mirror::ArtMethod* constructor); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2896 | static void CheckProxyMethod(Handle<mirror::ArtMethod> method, |
| 2897 | Handle<mirror::ArtMethod> prototype); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2898 | |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 2899 | mirror::Class* ClassLinker::CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, jstring name, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2900 | jobjectArray interfaces, jobject loader, |
| 2901 | jobjectArray methods, jobjectArray throws) { |
| 2902 | Thread* self = soa.Self(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2903 | StackHandleScope<8> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2904 | MutableHandle<mirror::Class> klass(hs.NewHandle( |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2905 | AllocClass(self, GetClassRoot(kJavaLangClass), sizeof(mirror::Class)))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2906 | if (klass.Get() == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2907 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2908 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2909 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2910 | DCHECK(klass->GetClass() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2911 | klass->SetObjectSize(sizeof(mirror::Proxy)); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 2912 | // Set the class access flags incl. preverified, so we do not try to set the flag on the methods. |
| 2913 | klass->SetAccessFlags(kAccClassIsProxy | kAccPublic | kAccFinal | kAccPreverified); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2914 | klass->SetClassLoader(soa.Decode<mirror::ClassLoader*>(loader)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2915 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2916 | klass->SetName(soa.Decode<mirror::String*>(name)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2917 | mirror::Class* proxy_class = GetClassRoot(kJavaLangReflectProxy); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2918 | klass->SetDexCache(proxy_class->GetDexCache()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2919 | mirror::Class::SetStatus(klass, mirror::Class::kStatusIdx, self); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2920 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 2921 | // Instance fields are inherited, but we add a couple of static fields... |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2922 | { |
| 2923 | mirror::ObjectArray<mirror::ArtField>* sfields = AllocArtFieldArray(self, 2); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2924 | if (UNLIKELY(sfields == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2925 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2926 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2927 | } |
| 2928 | klass->SetSFields(sfields); |
| 2929 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 2930 | // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by |
| 2931 | // our proxy, so Class.getInterfaces doesn't return the flattened set. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2932 | Handle<mirror::ArtField> interfaces_sfield(hs.NewHandle(AllocArtField(self))); |
| 2933 | if (UNLIKELY(interfaces_sfield.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2934 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2935 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2936 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2937 | klass->SetStaticField(0, interfaces_sfield.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 2938 | interfaces_sfield->SetDexFieldIndex(0); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2939 | interfaces_sfield->SetDeclaringClass(klass.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 2940 | interfaces_sfield->SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
| 2941 | // 2. Create a static field 'throws' that holds exceptions thrown by our methods. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2942 | Handle<mirror::ArtField> throws_sfield(hs.NewHandle(AllocArtField(self))); |
| 2943 | if (UNLIKELY(throws_sfield.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2944 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2945 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2946 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2947 | klass->SetStaticField(1, throws_sfield.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 2948 | throws_sfield->SetDexFieldIndex(1); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2949 | throws_sfield->SetDeclaringClass(klass.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 2950 | throws_sfield->SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2951 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2952 | // Proxies have 1 direct method, the constructor |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2953 | { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2954 | mirror::ObjectArray<mirror::ArtMethod>* directs = AllocArtMethodArray(self, 1); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2955 | if (UNLIKELY(directs == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2956 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2957 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2958 | } |
| 2959 | klass->SetDirectMethods(directs); |
| 2960 | mirror::ArtMethod* constructor = CreateProxyConstructor(self, klass, proxy_class); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2961 | if (UNLIKELY(constructor == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2962 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2963 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2964 | } |
| 2965 | klass->SetDirectMethod(0, constructor); |
| 2966 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2967 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2968 | // Create virtual method using specified prototypes. |
| 2969 | size_t num_virtual_methods = |
| 2970 | soa.Decode<mirror::ObjectArray<mirror::ArtMethod>*>(methods)->GetLength(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2971 | { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 2972 | mirror::ObjectArray<mirror::ArtMethod>* virtuals = AllocArtMethodArray(self, |
| 2973 | num_virtual_methods); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2974 | if (UNLIKELY(virtuals == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2975 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2976 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2977 | } |
| 2978 | klass->SetVirtualMethods(virtuals); |
| 2979 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2980 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2981 | StackHandleScope<1> hs2(self); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2982 | mirror::ObjectArray<mirror::ArtMethod>* decoded_methods = |
| 2983 | soa.Decode<mirror::ObjectArray<mirror::ArtMethod>*>(methods); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2984 | Handle<mirror::ArtMethod> prototype(hs2.NewHandle(decoded_methods->Get(i))); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2985 | mirror::ArtMethod* clone = CreateProxyMethod(self, klass, prototype); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2986 | if (UNLIKELY(clone == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2987 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2988 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2989 | } |
| 2990 | klass->SetVirtualMethod(i, clone); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2991 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2992 | |
| 2993 | klass->SetSuperClass(proxy_class); // The super class is java.lang.reflect.Proxy |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2994 | mirror::Class::SetStatus(klass, mirror::Class::kStatusLoaded, self); // Now effectively in the loaded state. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2995 | self->AssertNoPendingException(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2996 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2997 | std::string descriptor(GetDescriptorForProxy(klass.Get())); |
| 2998 | mirror::Class* new_class = nullptr; |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 2999 | { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3000 | // Must hold lock on object when resolved. |
| 3001 | ObjectLock<mirror::Class> resolution_lock(self, klass); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3002 | // Link the fields and virtual methods, creating vtable and iftables |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3003 | Handle<mirror::ObjectArray<mirror::Class> > h_interfaces( |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3004 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces))); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3005 | if (!LinkClass(self, descriptor.c_str(), klass, h_interfaces, &new_class)) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3006 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3007 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3008 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3009 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3010 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3011 | CHECK(klass->IsRetired()); |
| 3012 | CHECK_NE(klass.Get(), new_class); |
| 3013 | klass.Assign(new_class); |
| 3014 | |
| 3015 | CHECK_EQ(interfaces_sfield->GetDeclaringClass(), new_class); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3016 | interfaces_sfield->SetObject<false>(klass.Get(), |
| 3017 | soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3018 | CHECK_EQ(throws_sfield->GetDeclaringClass(), new_class); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3019 | throws_sfield->SetObject<false>(klass.Get(), |
| 3020 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class> >*>(throws)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3021 | |
| 3022 | { |
| 3023 | // Lock on klass is released. Lock new class object. |
| 3024 | ObjectLock<mirror::Class> initialization_lock(self, klass); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3025 | mirror::Class::SetStatus(klass, mirror::Class::kStatusInitialized, self); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 3026 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3027 | |
| 3028 | // sanity checks |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 3029 | if (kIsDebugBuild) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3030 | CHECK(klass->GetIFields() == nullptr); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3031 | CheckProxyConstructor(klass->GetDirectMethod(0)); |
| 3032 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3033 | StackHandleScope<2> hs2(self); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3034 | mirror::ObjectArray<mirror::ArtMethod>* decoded_methods = |
| 3035 | soa.Decode<mirror::ObjectArray<mirror::ArtMethod>*>(methods); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3036 | Handle<mirror::ArtMethod> prototype(hs2.NewHandle(decoded_methods->Get(i))); |
| 3037 | Handle<mirror::ArtMethod> virtual_method(hs2.NewHandle(klass->GetVirtualMethod(i))); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3038 | CheckProxyMethod(virtual_method, prototype); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3039 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3040 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3041 | mirror::String* decoded_name = soa.Decode<mirror::String*>(name); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3042 | std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3043 | decoded_name->ToModifiedUtf8().c_str())); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3044 | CHECK_EQ(PrettyField(klass->GetStaticField(0)), interfaces_field_name); |
| 3045 | |
| 3046 | std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3047 | decoded_name->ToModifiedUtf8().c_str())); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3048 | CHECK_EQ(PrettyField(klass->GetStaticField(1)), throws_field_name); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3049 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3050 | CHECK_EQ(klass.Get()->GetInterfaces(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3051 | soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3052 | CHECK_EQ(klass.Get()->GetThrows(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3053 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>*>(throws)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3054 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3055 | mirror::Class* existing = InsertClass(descriptor.c_str(), klass.Get(), |
| 3056 | ComputeModifiedUtf8Hash(descriptor.c_str())); |
Mathieu Chartier | 31b9d66 | 2013-10-14 11:53:12 -0700 | [diff] [blame] | 3057 | CHECK(existing == nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3058 | return klass.Get(); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3059 | } |
| 3060 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3061 | std::string ClassLinker::GetDescriptorForProxy(mirror::Class* proxy_class) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3062 | DCHECK(proxy_class->IsProxyClass()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3063 | mirror::String* name = proxy_class->GetName(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3064 | DCHECK(name != nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3065 | return DotToDescriptor(name->ToModifiedUtf8().c_str()); |
| 3066 | } |
| 3067 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3068 | mirror::ArtMethod* ClassLinker::FindMethodForProxy(mirror::Class* proxy_class, |
| 3069 | mirror::ArtMethod* proxy_method) { |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3070 | DCHECK(proxy_class->IsProxyClass()); |
| 3071 | DCHECK(proxy_method->IsProxyMethod()); |
| 3072 | // Locate the dex cache of the original interface/Object |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 3073 | mirror::DexCache* dex_cache = nullptr; |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3074 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3075 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3076 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3077 | mirror::DexCache* a_dex_cache = GetDexCache(i); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 3078 | if (proxy_method->HasSameDexCacheResolvedTypes(a_dex_cache->GetResolvedTypes())) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3079 | dex_cache = a_dex_cache; |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3080 | break; |
| 3081 | } |
| 3082 | } |
| 3083 | } |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 3084 | CHECK(dex_cache != nullptr); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3085 | uint32_t method_idx = proxy_method->GetDexMethodIndex(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3086 | mirror::ArtMethod* resolved_method = dex_cache->GetResolvedMethod(method_idx); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 3087 | CHECK(resolved_method != nullptr); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3088 | return resolved_method; |
| 3089 | } |
| 3090 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3091 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3092 | mirror::ArtMethod* ClassLinker::CreateProxyConstructor(Thread* self, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3093 | Handle<mirror::Class> klass, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3094 | mirror::Class* proxy_class) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3095 | // Create constructor for Proxy that must initialize h |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3096 | mirror::ObjectArray<mirror::ArtMethod>* proxy_direct_methods = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3097 | proxy_class->GetDirectMethods(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3098 | CHECK_EQ(proxy_direct_methods->GetLength(), 16); |
| 3099 | mirror::ArtMethod* proxy_constructor = proxy_direct_methods->Get(2); |
Sebastien Hertz | ae94e35 | 2014-08-27 15:32:56 +0200 | [diff] [blame] | 3100 | // Ensure constructor is in dex cache so that we can use the dex cache to look up the overridden |
| 3101 | // constructor method. |
| 3102 | proxy_class->GetDexCache()->SetResolvedMethod(proxy_constructor->GetDexMethodIndex(), |
| 3103 | proxy_constructor); |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 3104 | // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its |
| 3105 | // code_ too) |
Jeff Hao | f0a3f09 | 2014-07-24 16:26:09 -0700 | [diff] [blame] | 3106 | mirror::ArtMethod* constructor = down_cast<mirror::ArtMethod*>(proxy_constructor->Clone(self)); |
| 3107 | if (constructor == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3108 | CHECK(self->IsExceptionPending()); // OOME. |
Jeff Hao | f0a3f09 | 2014-07-24 16:26:09 -0700 | [diff] [blame] | 3109 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3110 | } |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3111 | // Make this constructor public and fix the class to be our Proxy version |
| 3112 | constructor->SetAccessFlags((constructor->GetAccessFlags() & ~kAccProtected) | kAccPublic); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3113 | constructor->SetDeclaringClass(klass.Get()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3114 | return constructor; |
| 3115 | } |
| 3116 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3117 | static void CheckProxyConstructor(mirror::ArtMethod* constructor) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 3118 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3119 | CHECK(constructor->IsConstructor()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3120 | CHECK_STREQ(constructor->GetName(), "<init>"); |
| 3121 | CHECK_STREQ(constructor->GetSignature().ToString().c_str(), |
| 3122 | "(Ljava/lang/reflect/InvocationHandler;)V"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3123 | DCHECK(constructor->IsPublic()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3124 | } |
| 3125 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3126 | mirror::ArtMethod* ClassLinker::CreateProxyMethod(Thread* self, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3127 | Handle<mirror::Class> klass, |
| 3128 | Handle<mirror::ArtMethod> prototype) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3129 | // Ensure prototype is in dex cache so that we can use the dex cache to look up the overridden |
| 3130 | // prototype method |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3131 | prototype->GetDeclaringClass()->GetDexCache()->SetResolvedMethod(prototype->GetDexMethodIndex(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3132 | prototype.Get()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3133 | // 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] | 3134 | // as necessary |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3135 | mirror::ArtMethod* method = down_cast<mirror::ArtMethod*>(prototype->Clone(self)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3136 | if (UNLIKELY(method == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3137 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3138 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3139 | } |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3140 | |
| 3141 | // Set class to be the concrete proxy class and clear the abstract flag, modify exceptions to |
| 3142 | // the intersection of throw exceptions as defined in Proxy |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3143 | method->SetDeclaringClass(klass.Get()); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3144 | method->SetAccessFlags((method->GetAccessFlags() & ~kAccAbstract) | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3145 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3146 | // At runtime the method looks like a reference and argument saving method, clone the code |
| 3147 | // related parameters from this method. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3148 | method->SetEntryPointFromQuickCompiledCode(GetQuickProxyInvokeHandler()); |
Dragos Sbirlea | 08bf196 | 2013-08-12 08:53:04 -0700 | [diff] [blame] | 3149 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3150 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3151 | return method; |
| 3152 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3153 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3154 | static void CheckProxyMethod(Handle<mirror::ArtMethod> method, |
| 3155 | Handle<mirror::ArtMethod> prototype) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 3156 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3157 | // Basic sanity |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3158 | CHECK(!prototype->IsFinal()); |
| 3159 | CHECK(method->IsFinal()); |
| 3160 | CHECK(!method->IsAbstract()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3161 | |
| 3162 | // The proxy method doesn't have its own dex cache or dex file and so it steals those of its |
| 3163 | // interface prototype. The exception to this are Constructors and the Class of the Proxy itself. |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 3164 | CHECK(prototype->HasSameDexCacheResolvedMethods(method.Get())); |
| 3165 | CHECK(prototype->HasSameDexCacheResolvedTypes(method.Get())); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3166 | CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex()); |
| 3167 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3168 | CHECK_STREQ(method->GetName(), prototype->GetName()); |
| 3169 | CHECK_STREQ(method->GetShorty(), prototype->GetShorty()); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3170 | // More complex sanity - via dex cache |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 3171 | CHECK_EQ(method->GetInterfaceMethodIfProxy()->GetReturnType(), prototype->GetReturnType()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3172 | } |
| 3173 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3174 | static bool CanWeInitializeClass(mirror::Class* klass, bool can_init_statics, |
| 3175 | bool can_init_parents) |
| 3176 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 3177 | if (can_init_statics && can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3178 | return true; |
| 3179 | } |
| 3180 | if (!can_init_statics) { |
| 3181 | // Check if there's a class initializer. |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 3182 | mirror::ArtMethod* clinit = klass->FindClassInitializer(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3183 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3184 | return false; |
| 3185 | } |
| 3186 | // Check if there are encoded static values needing initialization. |
| 3187 | if (klass->NumStaticFields() != 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 3188 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3189 | DCHECK(dex_class_def != nullptr); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3190 | if (dex_class_def->static_values_off_ != 0) { |
| 3191 | return false; |
| 3192 | } |
| 3193 | } |
| 3194 | } |
| 3195 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
| 3196 | mirror::Class* super_class = klass->GetSuperClass(); |
| 3197 | if (!can_init_parents && !super_class->IsInitialized()) { |
| 3198 | return false; |
| 3199 | } else { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 3200 | if (!CanWeInitializeClass(super_class, can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3201 | return false; |
| 3202 | } |
| 3203 | } |
| 3204 | } |
| 3205 | return true; |
| 3206 | } |
| 3207 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3208 | bool ClassLinker::InitializeClass(Thread* self, Handle<mirror::Class> klass, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3209 | bool can_init_statics, bool can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3210 | // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol |
| 3211 | |
| 3212 | // Are we already initialized and therefore done? |
| 3213 | // Note: we differ from the JLS here as we don't do this under the lock, this is benign as |
| 3214 | // an initialized class will never change its state. |
| 3215 | if (klass->IsInitialized()) { |
| 3216 | return true; |
| 3217 | } |
| 3218 | |
| 3219 | // 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] | 3220 | if (!CanWeInitializeClass(klass.Get(), can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3221 | return false; |
| 3222 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3223 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3224 | self->AllowThreadSuspension(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3225 | uint64_t t0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3226 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3227 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3228 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3229 | // Re-check under the lock in case another thread initialized ahead of us. |
| 3230 | if (klass->IsInitialized()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3231 | return true; |
| 3232 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3233 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3234 | // 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] | 3235 | if (klass->IsErroneous()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3236 | ThrowEarlierClassFailure(klass.Get()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 3237 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3238 | return false; |
| 3239 | } |
| 3240 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3241 | CHECK(klass->IsResolved()) << PrettyClass(klass.Get()) << ": state=" << klass->GetStatus(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3242 | |
| 3243 | if (!klass->IsVerified()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3244 | VerifyClass(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3245 | if (!klass->IsVerified()) { |
| 3246 | // We failed to verify, expect either the klass to be erroneous or verification failed at |
| 3247 | // compile time. |
| 3248 | if (klass->IsErroneous()) { |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 3249 | CHECK(self->IsExceptionPending()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 3250 | VlogClassInitializationFailure(klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3251 | } else { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3252 | CHECK(Runtime::Current()->IsAotCompiler()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3253 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 3254 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3255 | return false; |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 3256 | } else { |
| 3257 | self->AssertNoPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3258 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3259 | } |
| 3260 | |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3261 | // If the class is kStatusInitializing, either this thread is |
| 3262 | // initializing higher up the stack or another thread has beat us |
| 3263 | // to initializing and we need to wait. Either way, this |
| 3264 | // invocation of InitializeClass will not be responsible for |
| 3265 | // running <clinit> and will return. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3266 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 3267 | // Could have got an exception during verification. |
| 3268 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 3269 | VlogClassInitializationFailure(klass); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 3270 | return false; |
| 3271 | } |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 3272 | // We caught somebody else in the act; was it us? |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 3273 | if (klass->GetClinitThreadId() == self->GetTid()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3274 | // Yes. That's fine. Return so we can continue initializing. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3275 | return true; |
| 3276 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3277 | // No. That's fine. Wait for another thread to finish initializing. |
| 3278 | return WaitForInitializeClass(klass, self, lock); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3279 | } |
| 3280 | |
| 3281 | if (!ValidateSuperClassDescriptors(klass)) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3282 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3283 | return false; |
| 3284 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3285 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3286 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3287 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusVerified) << PrettyClass(klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3288 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3289 | // From here out other threads may observe that we're initializing and so changes of state |
| 3290 | // require the a notification. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 3291 | klass->SetClinitThreadId(self->GetTid()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3292 | mirror::Class::SetStatus(klass, mirror::Class::kStatusInitializing, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3293 | |
| 3294 | t0 = NanoTime(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3295 | } |
| 3296 | |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 3297 | // Initialize super classes, must be done while initializing for the JLS. |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3298 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
| 3299 | mirror::Class* super_class = klass->GetSuperClass(); |
| 3300 | if (!super_class->IsInitialized()) { |
| 3301 | CHECK(!super_class->IsInterface()); |
| 3302 | CHECK(can_init_parents); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3303 | StackHandleScope<1> hs(self); |
| 3304 | Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3305 | bool super_initialized = InitializeClass(self, handle_scope_super, can_init_statics, true); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3306 | if (!super_initialized) { |
| 3307 | // The super class was verified ahead of entering initializing, we should only be here if |
| 3308 | // the super class became erroneous due to initialization. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3309 | CHECK(handle_scope_super->IsErroneous() && self->IsExceptionPending()) |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3310 | << "Super class initialization failed for " |
| 3311 | << PrettyDescriptor(handle_scope_super.Get()) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3312 | << " that has unexpected status " << handle_scope_super->GetStatus() |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3313 | << "\nPending exception:\n" |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3314 | << (self->GetException() != nullptr ? self->GetException()->Dump() : ""); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3315 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3316 | // Initialization failed because the super-class is erroneous. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3317 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3318 | return false; |
| 3319 | } |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 3320 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3321 | } |
| 3322 | |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 3323 | const size_t num_static_fields = klass->NumStaticFields(); |
| 3324 | if (num_static_fields > 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 3325 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3326 | CHECK(dex_class_def != nullptr); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 3327 | const DexFile& dex_file = klass->GetDexFile(); |
Hiroshi Yamauchi | 67ef46a | 2014-08-21 15:59:43 -0700 | [diff] [blame] | 3328 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3329 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 3330 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 3331 | |
| 3332 | // Eagerly fill in static fields so that the we don't have to do as many expensive |
| 3333 | // Class::FindStaticField in ResolveField. |
| 3334 | for (size_t i = 0; i < num_static_fields; ++i) { |
| 3335 | mirror::ArtField* field = klass->GetStaticField(i); |
| 3336 | const uint32_t field_idx = field->GetDexFieldIndex(); |
| 3337 | mirror::ArtField* resolved_field = dex_cache->GetResolvedField(field_idx); |
| 3338 | if (resolved_field == nullptr) { |
| 3339 | dex_cache->SetResolvedField(field_idx, field); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 3340 | } else { |
| 3341 | DCHECK_EQ(field, resolved_field); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 3342 | } |
| 3343 | } |
| 3344 | |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 3345 | EncodedStaticFieldValueIterator value_it(dex_file, &dex_cache, &class_loader, |
| 3346 | this, *dex_class_def); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 3347 | const uint8_t* class_data = dex_file.GetClassData(*dex_class_def); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 3348 | ClassDataItemIterator field_it(dex_file, class_data); |
| 3349 | if (value_it.HasNext()) { |
| 3350 | DCHECK(field_it.HasNextStaticField()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3351 | CHECK(can_init_statics); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 3352 | for ( ; value_it.HasNext(); value_it.Next(), field_it.Next()) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3353 | StackHandleScope<1> hs2(self); |
| 3354 | Handle<mirror::ArtField> field(hs2.NewHandle( |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 3355 | ResolveField(dex_file, field_it.GetMemberIndex(), dex_cache, class_loader, true))); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 3356 | if (Runtime::Current()->IsActiveTransaction()) { |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 3357 | value_it.ReadValueToField<true>(field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 3358 | } else { |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 3359 | value_it.ReadValueToField<false>(field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 3360 | } |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 3361 | DCHECK(!value_it.HasNext() || field_it.HasNextStaticField()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3362 | } |
| 3363 | } |
| 3364 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3365 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 3366 | mirror::ArtMethod* clinit = klass->FindClassInitializer(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3367 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3368 | CHECK(can_init_statics); |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 3369 | JValue result; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3370 | clinit->Invoke(self, nullptr, 0, &result, "V"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3371 | } |
| 3372 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3373 | self->AllowThreadSuspension(); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 3374 | uint64_t t1 = NanoTime(); |
| 3375 | |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 3376 | bool success = true; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3377 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3378 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3379 | |
| 3380 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 3381 | WrapExceptionInInitializer(klass); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3382 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 3383 | success = false; |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 3384 | } else if (Runtime::Current()->IsTransactionAborted()) { |
| 3385 | // The exception thrown when the transaction aborted has been caught and cleared |
| 3386 | // so we need to throw it again now. |
Sebastien Hertz | bd9cf9f | 2015-03-03 12:16:13 +0100 | [diff] [blame] | 3387 | VLOG(compiler) << "Return from class initializer of " << PrettyDescriptor(klass.Get()) |
| 3388 | << " without exception while transaction was aborted: re-throw it now."; |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 3389 | Runtime::Current()->ThrowTransactionAbortError(self); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3390 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 3391 | success = false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3392 | } else { |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 3393 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 3394 | RuntimeStats* thread_stats = self->GetStats(); |
| 3395 | ++global_stats->class_init_count; |
| 3396 | ++thread_stats->class_init_count; |
| 3397 | global_stats->class_init_time_ns += (t1 - t0); |
| 3398 | thread_stats->class_init_time_ns += (t1 - t0); |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 3399 | // Set the class as initialized except if failed to initialize static fields. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3400 | mirror::Class::SetStatus(klass, mirror::Class::kStatusInitialized, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3401 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3402 | std::string temp; |
| 3403 | LOG(INFO) << "Initialized class " << klass->GetDescriptor(&temp) << " from " << |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 3404 | klass->GetLocation(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3405 | } |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 3406 | // Opportunistically set static method trampolines to their destination. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3407 | FixupStaticTrampolines(klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3408 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3409 | } |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 3410 | return success; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3411 | } |
| 3412 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3413 | bool ClassLinker::WaitForInitializeClass(Handle<mirror::Class> klass, Thread* self, |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3414 | ObjectLock<mirror::Class>& lock) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 3415 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3416 | while (true) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3417 | self->AssertNoPendingException(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3418 | CHECK(!klass->IsInitialized()); |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 3419 | lock.WaitIgnoringInterrupts(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3420 | |
| 3421 | // When we wake up, repeat the test for init-in-progress. If |
| 3422 | // there's an exception pending (only possible if |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 3423 | // we were not using WaitIgnoringInterrupts), bail out. |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3424 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 3425 | WrapExceptionInInitializer(klass); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3426 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3427 | return false; |
| 3428 | } |
| 3429 | // Spurious wakeup? Go back to waiting. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3430 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3431 | continue; |
| 3432 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3433 | if (klass->GetStatus() == mirror::Class::kStatusVerified && |
| 3434 | Runtime::Current()->IsAotCompiler()) { |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 3435 | // Compile time initialization failed. |
| 3436 | return false; |
| 3437 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3438 | if (klass->IsErroneous()) { |
| 3439 | // The caller wants an exception, but it was thrown in a |
| 3440 | // different thread. Synthesize one here. |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 3441 | ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread", |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3442 | PrettyDescriptor(klass.Get()).c_str()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 3443 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3444 | return false; |
| 3445 | } |
| 3446 | if (klass->IsInitialized()) { |
| 3447 | return true; |
| 3448 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3449 | LOG(FATAL) << "Unexpected class status. " << PrettyClass(klass.Get()) << " is " |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3450 | << klass->GetStatus(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3451 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 3452 | UNREACHABLE(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3453 | } |
| 3454 | |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 3455 | static bool HasSameSignatureWithDifferentClassLoaders(Thread* self, |
| 3456 | Handle<mirror::ArtMethod> method1, |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 3457 | Handle<mirror::ArtMethod> method2, |
| 3458 | std::string* error_msg) |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 3459 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 3460 | { |
| 3461 | StackHandleScope<1> hs(self); |
| 3462 | Handle<mirror::Class> return_type(hs.NewHandle(method1->GetReturnType())); |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 3463 | mirror::Class* other_return_type = method2->GetReturnType(); |
| 3464 | // NOTE: return_type.Get() must be sequenced after method2->GetReturnType(). |
| 3465 | if (UNLIKELY(other_return_type != return_type.Get())) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 3466 | *error_msg = StringPrintf("Return types mismatch: %s(%p) vs %s(%p)", |
| 3467 | PrettyClassAndClassLoader(return_type.Get()).c_str(), |
| 3468 | return_type.Get(), |
| 3469 | PrettyClassAndClassLoader(other_return_type).c_str(), |
| 3470 | other_return_type); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 3471 | return false; |
| 3472 | } |
| 3473 | } |
| 3474 | const DexFile::TypeList* types1 = method1->GetParameterTypeList(); |
| 3475 | const DexFile::TypeList* types2 = method2->GetParameterTypeList(); |
| 3476 | if (types1 == nullptr) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 3477 | if (types2 != nullptr && types2->Size() != 0) { |
| 3478 | *error_msg = StringPrintf("Type list mismatch with %s", |
| 3479 | PrettyMethod(method2.Get(), true).c_str()); |
| 3480 | return false; |
| 3481 | } |
| 3482 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 3483 | } else if (UNLIKELY(types2 == nullptr)) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 3484 | if (types1->Size() != 0) { |
| 3485 | *error_msg = StringPrintf("Type list mismatch with %s", |
| 3486 | PrettyMethod(method2.Get(), true).c_str()); |
| 3487 | return false; |
| 3488 | } |
| 3489 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 3490 | } |
| 3491 | uint32_t num_types = types1->Size(); |
| 3492 | if (UNLIKELY(num_types != types2->Size())) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 3493 | *error_msg = StringPrintf("Type list mismatch with %s", |
| 3494 | PrettyMethod(method2.Get(), true).c_str()); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 3495 | return false; |
| 3496 | } |
| 3497 | for (uint32_t i = 0; i < num_types; ++i) { |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 3498 | StackHandleScope<1> hs(self); |
| 3499 | Handle<mirror::Class> param_type(hs.NewHandle( |
| 3500 | method1->GetClassFromTypeIndex(types1->GetTypeItem(i).type_idx_, true))); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 3501 | mirror::Class* other_param_type = |
| 3502 | method2->GetClassFromTypeIndex(types2->GetTypeItem(i).type_idx_, true); |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 3503 | // NOTE: param_type.Get() must be sequenced after method2->GetClassFromTypeIndex(...). |
| 3504 | if (UNLIKELY(param_type.Get() != other_param_type)) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 3505 | *error_msg = StringPrintf("Parameter %u type mismatch: %s(%p) vs %s(%p)", |
| 3506 | i, |
| 3507 | PrettyClassAndClassLoader(param_type.Get()).c_str(), |
| 3508 | param_type.Get(), |
| 3509 | PrettyClassAndClassLoader(other_param_type).c_str(), |
| 3510 | other_param_type); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 3511 | return false; |
| 3512 | } |
| 3513 | } |
| 3514 | return true; |
| 3515 | } |
| 3516 | |
| 3517 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3518 | bool ClassLinker::ValidateSuperClassDescriptors(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3519 | if (klass->IsInterface()) { |
| 3520 | return true; |
| 3521 | } |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 3522 | // Begin with the methods local to the superclass. |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 3523 | Thread* self = Thread::Current(); |
| 3524 | StackHandleScope<2> hs(self); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 3525 | MutableHandle<mirror::ArtMethod> h_m(hs.NewHandle<mirror::ArtMethod>(nullptr)); |
| 3526 | MutableHandle<mirror::ArtMethod> super_h_m(hs.NewHandle<mirror::ArtMethod>(nullptr)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3527 | if (klass->HasSuperClass() && |
| 3528 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 3529 | for (int i = klass->GetSuperClass()->GetVTableLength() - 1; i >= 0; --i) { |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 3530 | h_m.Assign(klass->GetVTableEntry(i)); |
| 3531 | super_h_m.Assign(klass->GetSuperClass()->GetVTableEntry(i)); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 3532 | if (h_m.Get() != super_h_m.Get()) { |
| 3533 | std::string error_msg; |
| 3534 | if (!HasSameSignatureWithDifferentClassLoaders(self, h_m, super_h_m, &error_msg)) { |
| 3535 | ThrowLinkageError(klass.Get(), |
| 3536 | "Class %s method %s resolves differently in superclass %s: %s", |
| 3537 | PrettyDescriptor(klass.Get()).c_str(), |
| 3538 | PrettyMethod(h_m.Get()).c_str(), |
| 3539 | PrettyDescriptor(klass->GetSuperClass()).c_str(), |
| 3540 | error_msg.c_str()); |
| 3541 | return false; |
| 3542 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3543 | } |
| 3544 | } |
| 3545 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 3546 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 3547 | if (klass->GetClassLoader() != klass->GetIfTable()->GetInterface(i)->GetClassLoader()) { |
| 3548 | uint32_t num_methods = klass->GetIfTable()->GetInterface(i)->NumVirtualMethods(); |
| 3549 | for (uint32_t j = 0; j < num_methods; ++j) { |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 3550 | h_m.Assign(klass->GetIfTable()->GetMethodArray(i)->GetWithoutChecks(j)); |
| 3551 | super_h_m.Assign(klass->GetIfTable()->GetInterface(i)->GetVirtualMethod(j)); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 3552 | if (h_m.Get() != super_h_m.Get()) { |
| 3553 | std::string error_msg; |
| 3554 | if (!HasSameSignatureWithDifferentClassLoaders(self, h_m, super_h_m, &error_msg)) { |
| 3555 | ThrowLinkageError(klass.Get(), |
| 3556 | "Class %s method %s resolves differently in interface %s: %s", |
| 3557 | PrettyDescriptor(klass.Get()).c_str(), |
| 3558 | PrettyMethod(h_m.Get()).c_str(), |
| 3559 | PrettyDescriptor(klass->GetIfTable()->GetInterface(i)).c_str(), |
| 3560 | error_msg.c_str()); |
| 3561 | return false; |
| 3562 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3563 | } |
| 3564 | } |
| 3565 | } |
| 3566 | } |
| 3567 | return true; |
| 3568 | } |
| 3569 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3570 | bool ClassLinker::EnsureInitialized(Thread* self, Handle<mirror::Class> c, bool can_init_fields, |
| 3571 | bool can_init_parents) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 3572 | DCHECK(c.Get() != nullptr); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 3573 | if (c->IsInitialized()) { |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 3574 | EnsurePreverifiedMethods(c); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 3575 | return true; |
| 3576 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3577 | const bool success = InitializeClass(self, c, can_init_fields, can_init_parents); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 3578 | if (!success) { |
| 3579 | if (can_init_fields && can_init_parents) { |
| 3580 | CHECK(self->IsExceptionPending()) << PrettyClass(c.Get()); |
| 3581 | } |
| 3582 | } else { |
| 3583 | self->AssertNoPendingException(); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 3584 | } |
| 3585 | return success; |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 3586 | } |
| 3587 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3588 | void ClassLinker::FixupTemporaryDeclaringClass(mirror::Class* temp_class, mirror::Class* new_class) { |
| 3589 | mirror::ObjectArray<mirror::ArtField>* fields = new_class->GetIFields(); |
| 3590 | if (fields != nullptr) { |
| 3591 | for (int index = 0; index < fields->GetLength(); index ++) { |
| 3592 | if (fields->Get(index)->GetDeclaringClass() == temp_class) { |
| 3593 | fields->Get(index)->SetDeclaringClass(new_class); |
| 3594 | } |
| 3595 | } |
| 3596 | } |
| 3597 | |
| 3598 | fields = new_class->GetSFields(); |
| 3599 | if (fields != nullptr) { |
| 3600 | for (int index = 0; index < fields->GetLength(); index ++) { |
| 3601 | if (fields->Get(index)->GetDeclaringClass() == temp_class) { |
| 3602 | fields->Get(index)->SetDeclaringClass(new_class); |
| 3603 | } |
| 3604 | } |
| 3605 | } |
| 3606 | |
| 3607 | mirror::ObjectArray<mirror::ArtMethod>* methods = new_class->GetDirectMethods(); |
| 3608 | if (methods != nullptr) { |
| 3609 | for (int index = 0; index < methods->GetLength(); index ++) { |
| 3610 | if (methods->Get(index)->GetDeclaringClass() == temp_class) { |
| 3611 | methods->Get(index)->SetDeclaringClass(new_class); |
| 3612 | } |
| 3613 | } |
| 3614 | } |
| 3615 | |
| 3616 | methods = new_class->GetVirtualMethods(); |
| 3617 | if (methods != nullptr) { |
| 3618 | for (int index = 0; index < methods->GetLength(); index ++) { |
| 3619 | if (methods->Get(index)->GetDeclaringClass() == temp_class) { |
| 3620 | methods->Get(index)->SetDeclaringClass(new_class); |
| 3621 | } |
| 3622 | } |
| 3623 | } |
| 3624 | } |
| 3625 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3626 | bool ClassLinker::LinkClass(Thread* self, const char* descriptor, Handle<mirror::Class> klass, |
| 3627 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3628 | mirror::Class** new_class) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3629 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3630 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3631 | if (!LinkSuperClass(klass)) { |
| 3632 | return false; |
| 3633 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 3634 | StackHandleScope<mirror::Class::kImtSize> imt_handle_scope( |
| 3635 | self, Runtime::Current()->GetImtUnimplementedMethod()); |
| 3636 | if (!LinkMethods(self, klass, interfaces, &imt_handle_scope)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3637 | return false; |
| 3638 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3639 | if (!LinkInstanceFields(self, klass)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3640 | return false; |
| 3641 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3642 | size_t class_size; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3643 | if (!LinkStaticFields(self, klass, &class_size)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3644 | return false; |
| 3645 | } |
| 3646 | CreateReferenceInstanceOffsets(klass); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3647 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3648 | |
| 3649 | if (!klass->IsTemp() || (!init_done_ && klass->GetClassSize() == class_size)) { |
| 3650 | // We don't need to retire this class as it has no embedded tables or it was created the |
| 3651 | // correct size during class linker initialization. |
| 3652 | CHECK_EQ(klass->GetClassSize(), class_size) << PrettyDescriptor(klass.Get()); |
| 3653 | |
| 3654 | if (klass->ShouldHaveEmbeddedImtAndVTable()) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 3655 | klass->PopulateEmbeddedImtAndVTable(&imt_handle_scope); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3656 | } |
| 3657 | |
| 3658 | // This will notify waiters on klass that saw the not yet resolved |
| 3659 | // class in the class_table_ during EnsureResolved. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3660 | mirror::Class::SetStatus(klass, mirror::Class::kStatusResolved, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3661 | *new_class = klass.Get(); |
| 3662 | } else { |
| 3663 | CHECK(!klass->IsResolved()); |
| 3664 | // Retire the temporary class and create the correctly sized resolved class. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 3665 | *new_class = klass->CopyOf(self, class_size, &imt_handle_scope); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3666 | if (UNLIKELY(*new_class == nullptr)) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3667 | CHECK(self->IsExceptionPending()); // Expect an OOME. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3668 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3669 | return false; |
| 3670 | } |
| 3671 | |
| 3672 | CHECK_EQ((*new_class)->GetClassSize(), class_size); |
| 3673 | StackHandleScope<1> hs(self); |
| 3674 | auto new_class_h = hs.NewHandleWrapper<mirror::Class>(new_class); |
| 3675 | ObjectLock<mirror::Class> lock(self, new_class_h); |
| 3676 | |
| 3677 | FixupTemporaryDeclaringClass(klass.Get(), new_class_h.Get()); |
| 3678 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3679 | mirror::Class* existing = UpdateClass(descriptor, new_class_h.Get(), |
| 3680 | ComputeModifiedUtf8Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3681 | CHECK(existing == nullptr || existing == klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3682 | |
| 3683 | // This will notify waiters on temp class that saw the not yet resolved class in the |
| 3684 | // class_table_ during EnsureResolved. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3685 | mirror::Class::SetStatus(klass, mirror::Class::kStatusRetired, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3686 | |
| 3687 | CHECK_EQ(new_class_h->GetStatus(), mirror::Class::kStatusResolving); |
| 3688 | // This will notify waiters on new_class that saw the not yet resolved |
| 3689 | // class in the class_table_ during EnsureResolved. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3690 | mirror::Class::SetStatus(new_class_h, mirror::Class::kStatusResolved, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3691 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3692 | return true; |
| 3693 | } |
| 3694 | |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 3695 | static void CountMethodsAndFields(ClassDataItemIterator& dex_data, |
| 3696 | size_t* virtual_methods, |
| 3697 | size_t* direct_methods, |
| 3698 | size_t* static_fields, |
| 3699 | size_t* instance_fields) { |
| 3700 | *virtual_methods = *direct_methods = *static_fields = *instance_fields = 0; |
| 3701 | |
| 3702 | while (dex_data.HasNextStaticField()) { |
| 3703 | dex_data.Next(); |
| 3704 | (*static_fields)++; |
| 3705 | } |
| 3706 | while (dex_data.HasNextInstanceField()) { |
| 3707 | dex_data.Next(); |
| 3708 | (*instance_fields)++; |
| 3709 | } |
| 3710 | while (dex_data.HasNextDirectMethod()) { |
| 3711 | (*direct_methods)++; |
| 3712 | dex_data.Next(); |
| 3713 | } |
| 3714 | while (dex_data.HasNextVirtualMethod()) { |
| 3715 | (*virtual_methods)++; |
| 3716 | dex_data.Next(); |
| 3717 | } |
| 3718 | DCHECK(!dex_data.HasNext()); |
| 3719 | } |
| 3720 | |
| 3721 | static void DumpClass(std::ostream& os, |
| 3722 | const DexFile& dex_file, const DexFile::ClassDef& dex_class_def, |
| 3723 | const char* suffix) { |
| 3724 | ClassDataItemIterator dex_data(dex_file, dex_file.GetClassData(dex_class_def)); |
| 3725 | os << dex_file.GetClassDescriptor(dex_class_def) << suffix << ":\n"; |
| 3726 | os << " Static fields:\n"; |
| 3727 | while (dex_data.HasNextStaticField()) { |
| 3728 | const DexFile::FieldId& id = dex_file.GetFieldId(dex_data.GetMemberIndex()); |
| 3729 | os << " " << dex_file.GetFieldTypeDescriptor(id) << " " << dex_file.GetFieldName(id) << "\n"; |
| 3730 | dex_data.Next(); |
| 3731 | } |
| 3732 | os << " Instance fields:\n"; |
| 3733 | while (dex_data.HasNextInstanceField()) { |
| 3734 | const DexFile::FieldId& id = dex_file.GetFieldId(dex_data.GetMemberIndex()); |
| 3735 | os << " " << dex_file.GetFieldTypeDescriptor(id) << " " << dex_file.GetFieldName(id) << "\n"; |
| 3736 | dex_data.Next(); |
| 3737 | } |
| 3738 | os << " Direct methods:\n"; |
| 3739 | while (dex_data.HasNextDirectMethod()) { |
| 3740 | const DexFile::MethodId& id = dex_file.GetMethodId(dex_data.GetMemberIndex()); |
| 3741 | os << " " << dex_file.GetMethodName(id) << dex_file.GetMethodSignature(id).ToString() << "\n"; |
| 3742 | dex_data.Next(); |
| 3743 | } |
| 3744 | os << " Virtual methods:\n"; |
| 3745 | while (dex_data.HasNextVirtualMethod()) { |
| 3746 | const DexFile::MethodId& id = dex_file.GetMethodId(dex_data.GetMemberIndex()); |
| 3747 | os << " " << dex_file.GetMethodName(id) << dex_file.GetMethodSignature(id).ToString() << "\n"; |
| 3748 | dex_data.Next(); |
| 3749 | } |
| 3750 | } |
| 3751 | |
| 3752 | static std::string DumpClasses(const DexFile& dex_file1, const DexFile::ClassDef& dex_class_def1, |
| 3753 | const DexFile& dex_file2, const DexFile::ClassDef& dex_class_def2) { |
| 3754 | std::ostringstream os; |
| 3755 | DumpClass(os, dex_file1, dex_class_def1, " (Compile time)"); |
| 3756 | DumpClass(os, dex_file2, dex_class_def2, " (Runtime)"); |
| 3757 | return os.str(); |
| 3758 | } |
| 3759 | |
| 3760 | |
| 3761 | // Very simple structural check on whether the classes match. Only compares the number of |
| 3762 | // methods and fields. |
| 3763 | static bool SimpleStructuralCheck(const DexFile& dex_file1, const DexFile::ClassDef& dex_class_def1, |
| 3764 | const DexFile& dex_file2, const DexFile::ClassDef& dex_class_def2, |
| 3765 | std::string* error_msg) { |
| 3766 | ClassDataItemIterator dex_data1(dex_file1, dex_file1.GetClassData(dex_class_def1)); |
| 3767 | ClassDataItemIterator dex_data2(dex_file2, dex_file2.GetClassData(dex_class_def2)); |
| 3768 | |
| 3769 | // Counters for current dex file. |
| 3770 | size_t dex_virtual_methods1, dex_direct_methods1, dex_static_fields1, dex_instance_fields1; |
| 3771 | CountMethodsAndFields(dex_data1, &dex_virtual_methods1, &dex_direct_methods1, &dex_static_fields1, |
| 3772 | &dex_instance_fields1); |
| 3773 | // Counters for compile-time dex file. |
| 3774 | size_t dex_virtual_methods2, dex_direct_methods2, dex_static_fields2, dex_instance_fields2; |
| 3775 | CountMethodsAndFields(dex_data2, &dex_virtual_methods2, &dex_direct_methods2, &dex_static_fields2, |
| 3776 | &dex_instance_fields2); |
| 3777 | |
| 3778 | if (dex_virtual_methods1 != dex_virtual_methods2) { |
| 3779 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
| 3780 | *error_msg = StringPrintf("Virtual method count off: %zu vs %zu\n%s", dex_virtual_methods1, |
| 3781 | dex_virtual_methods2, class_dump.c_str()); |
| 3782 | return false; |
| 3783 | } |
| 3784 | if (dex_direct_methods1 != dex_direct_methods2) { |
| 3785 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
| 3786 | *error_msg = StringPrintf("Direct method count off: %zu vs %zu\n%s", dex_direct_methods1, |
| 3787 | dex_direct_methods2, class_dump.c_str()); |
| 3788 | return false; |
| 3789 | } |
| 3790 | if (dex_static_fields1 != dex_static_fields2) { |
| 3791 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
| 3792 | *error_msg = StringPrintf("Static field count off: %zu vs %zu\n%s", dex_static_fields1, |
| 3793 | dex_static_fields2, class_dump.c_str()); |
| 3794 | return false; |
| 3795 | } |
| 3796 | if (dex_instance_fields1 != dex_instance_fields2) { |
| 3797 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
| 3798 | *error_msg = StringPrintf("Instance field count off: %zu vs %zu\n%s", dex_instance_fields1, |
| 3799 | dex_instance_fields2, class_dump.c_str()); |
| 3800 | return false; |
| 3801 | } |
| 3802 | |
| 3803 | return true; |
| 3804 | } |
| 3805 | |
| 3806 | // Checks whether a the super-class changed from what we had at compile-time. This would |
| 3807 | // invalidate quickening. |
| 3808 | static bool CheckSuperClassChange(Handle<mirror::Class> klass, |
| 3809 | const DexFile& dex_file, |
| 3810 | const DexFile::ClassDef& class_def, |
| 3811 | mirror::Class* super_class) |
| 3812 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 3813 | // Check for unexpected changes in the superclass. |
| 3814 | // Quick check 1) is the super_class class-loader the boot class loader? This always has |
| 3815 | // precedence. |
| 3816 | if (super_class->GetClassLoader() != nullptr && |
| 3817 | // Quick check 2) different dex cache? Breaks can only occur for different dex files, |
| 3818 | // which is implied by different dex cache. |
| 3819 | klass->GetDexCache() != super_class->GetDexCache()) { |
| 3820 | // Now comes the expensive part: things can be broken if (a) the klass' dex file has a |
| 3821 | // definition for the super-class, and (b) the files are in separate oat files. The oat files |
| 3822 | // 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] | 3823 | const OatDexFile* class_oat_dex_file = dex_file.GetOatDexFile(); |
| 3824 | const OatFile* class_oat_file = nullptr; |
| 3825 | if (class_oat_dex_file != nullptr) { |
| 3826 | class_oat_file = class_oat_dex_file->GetOatFile(); |
| 3827 | } |
| 3828 | |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 3829 | if (class_oat_file != nullptr) { |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 3830 | const OatDexFile* loaded_super_oat_dex_file = super_class->GetDexFile().GetOatDexFile(); |
| 3831 | const OatFile* loaded_super_oat_file = nullptr; |
| 3832 | if (loaded_super_oat_dex_file != nullptr) { |
| 3833 | loaded_super_oat_file = loaded_super_oat_dex_file->GetOatFile(); |
| 3834 | } |
| 3835 | |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 3836 | if (loaded_super_oat_file != nullptr && class_oat_file != loaded_super_oat_file) { |
| 3837 | // Now check (a). |
| 3838 | const DexFile::ClassDef* super_class_def = dex_file.FindClassDef(class_def.superclass_idx_); |
| 3839 | if (super_class_def != nullptr) { |
| 3840 | // Uh-oh, we found something. Do our check. |
| 3841 | std::string error_msg; |
| 3842 | if (!SimpleStructuralCheck(dex_file, *super_class_def, |
| 3843 | super_class->GetDexFile(), *super_class->GetClassDef(), |
| 3844 | &error_msg)) { |
| 3845 | // Print a warning to the log. This exception might be caught, e.g., as common in test |
| 3846 | // drivers. When the class is later tried to be used, we re-throw a new instance, as we |
| 3847 | // only save the type of the exception. |
| 3848 | LOG(WARNING) << "Incompatible structural change detected: " << |
| 3849 | StringPrintf( |
| 3850 | "Structural change of %s is hazardous (%s at compile time, %s at runtime): %s", |
| 3851 | PrettyType(super_class_def->class_idx_, dex_file).c_str(), |
| 3852 | class_oat_file->GetLocation().c_str(), |
| 3853 | loaded_super_oat_file->GetLocation().c_str(), |
| 3854 | error_msg.c_str()); |
| 3855 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 3856 | "Structural change of %s is hazardous (%s at compile time, %s at runtime): %s", |
| 3857 | PrettyType(super_class_def->class_idx_, dex_file).c_str(), |
| 3858 | class_oat_file->GetLocation().c_str(), |
| 3859 | loaded_super_oat_file->GetLocation().c_str(), |
| 3860 | error_msg.c_str()); |
| 3861 | return false; |
| 3862 | } |
| 3863 | } |
| 3864 | } |
| 3865 | } |
| 3866 | } |
| 3867 | return true; |
| 3868 | } |
| 3869 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3870 | bool ClassLinker::LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3871 | CHECK_EQ(mirror::Class::kStatusIdx, klass->GetStatus()); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3872 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
| 3873 | uint16_t super_class_idx = class_def.superclass_idx_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3874 | if (super_class_idx != DexFile::kDexNoIndex16) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3875 | mirror::Class* super_class = ResolveType(dex_file, super_class_idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3876 | if (super_class == nullptr) { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 3877 | DCHECK(Thread::Current()->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3878 | return false; |
| 3879 | } |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 3880 | // Verify |
| 3881 | if (!klass->CanAccess(super_class)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3882 | ThrowIllegalAccessError(klass.Get(), "Class %s extended by class %s is inaccessible", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3883 | PrettyDescriptor(super_class).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3884 | PrettyDescriptor(klass.Get()).c_str()); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 3885 | return false; |
| 3886 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3887 | CHECK(super_class->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3888 | klass->SetSuperClass(super_class); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 3889 | |
| 3890 | if (!CheckSuperClassChange(klass, dex_file, class_def, super_class)) { |
| 3891 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 3892 | return false; |
| 3893 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3894 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3895 | const DexFile::TypeList* interfaces = dex_file.GetInterfacesList(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3896 | if (interfaces != nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3897 | for (size_t i = 0; i < interfaces->Size(); i++) { |
| 3898 | uint16_t idx = interfaces->GetTypeItem(i).type_idx_; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3899 | mirror::Class* interface = ResolveType(dex_file, idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3900 | if (interface == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3901 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 3902 | return false; |
| 3903 | } |
| 3904 | // Verify |
| 3905 | if (!klass->CanAccess(interface)) { |
| 3906 | // TODO: the RI seemed to ignore this in my testing. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3907 | ThrowIllegalAccessError(klass.Get(), "Interface %s implemented by class %s is inaccessible", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3908 | PrettyDescriptor(interface).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3909 | PrettyDescriptor(klass.Get()).c_str()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3910 | return false; |
| 3911 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3912 | } |
| 3913 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3914 | // Mark the class as loaded. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3915 | mirror::Class::SetStatus(klass, mirror::Class::kStatusLoaded, nullptr); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3916 | return true; |
| 3917 | } |
| 3918 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3919 | bool ClassLinker::LinkSuperClass(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3920 | CHECK(!klass->IsPrimitive()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3921 | mirror::Class* super = klass->GetSuperClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3922 | if (klass.Get() == GetClassRoot(kJavaLangObject)) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3923 | if (super != nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3924 | ThrowClassFormatError(klass.Get(), "java.lang.Object must not have a superclass"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3925 | return false; |
| 3926 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3927 | return true; |
| 3928 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3929 | if (super == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3930 | ThrowLinkageError(klass.Get(), "No superclass defined for class %s", |
| 3931 | PrettyDescriptor(klass.Get()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3932 | return false; |
| 3933 | } |
| 3934 | // Verify |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 3935 | if (super->IsFinal() || super->IsInterface()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3936 | ThrowIncompatibleClassChangeError(klass.Get(), "Superclass %s of %s is %s", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3937 | PrettyDescriptor(super).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3938 | PrettyDescriptor(klass.Get()).c_str(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3939 | super->IsFinal() ? "declared final" : "an interface"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3940 | return false; |
| 3941 | } |
| 3942 | if (!klass->CanAccess(super)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3943 | ThrowIllegalAccessError(klass.Get(), "Superclass %s is inaccessible to class %s", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3944 | PrettyDescriptor(super).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3945 | PrettyDescriptor(klass.Get()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3946 | return false; |
| 3947 | } |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 3948 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3949 | // Inherit kAccClassIsFinalizable from the superclass in case this |
| 3950 | // class doesn't override finalize. |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 3951 | if (super->IsFinalizable()) { |
| 3952 | klass->SetFinalizable(); |
| 3953 | } |
| 3954 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 3955 | // Inherit reference flags (if any) from the superclass. |
| 3956 | int reference_flags = (super->GetAccessFlags() & kAccReferenceFlagsMask); |
| 3957 | if (reference_flags != 0) { |
| 3958 | klass->SetAccessFlags(klass->GetAccessFlags() | reference_flags); |
| 3959 | } |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 3960 | // Disallow custom direct subclasses of java.lang.ref.Reference. |
Elliott Hughes | bf61ba3 | 2011-10-11 10:53:09 -0700 | [diff] [blame] | 3961 | if (init_done_ && super == GetClassRoot(kJavaLangRefReference)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3962 | ThrowLinkageError(klass.Get(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3963 | "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] | 3964 | PrettyDescriptor(klass.Get()).c_str()); |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 3965 | return false; |
| 3966 | } |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 3967 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3968 | if (kIsDebugBuild) { |
| 3969 | // Ensure super classes are fully resolved prior to resolving fields.. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3970 | while (super != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3971 | CHECK(super->IsResolved()); |
| 3972 | super = super->GetSuperClass(); |
| 3973 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3974 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3975 | return true; |
| 3976 | } |
| 3977 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3978 | // Populate the class vtable and itable. Compute return type indices. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3979 | bool ClassLinker::LinkMethods(Thread* self, Handle<mirror::Class> klass, |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 3980 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
| 3981 | StackHandleScope<mirror::Class::kImtSize>* out_imt) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3982 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3983 | if (klass->IsInterface()) { |
| 3984 | // No vtable. |
| 3985 | size_t count = klass->NumVirtualMethods(); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 3986 | if (!IsUint<16>(count)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3987 | ThrowClassFormatError(klass.Get(), "Too many methods on interface: %zd", count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3988 | return false; |
| 3989 | } |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 3990 | for (size_t i = 0; i < count; ++i) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3991 | klass->GetVirtualMethodDuringLinking(i)->SetMethodIndex(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3992 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 3993 | } else if (!LinkVirtualMethods(self, klass)) { // Link virtual methods first. |
| 3994 | return false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3995 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 3996 | return LinkInterfaceMethods(self, klass, interfaces, out_imt); // Link interface method last. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3997 | } |
| 3998 | |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 3999 | // Comparator for name and signature of a method, used in finding overriding methods. Implementation |
| 4000 | // avoids the use of handles, if it didn't then rather than compare dex files we could compare dex |
| 4001 | // caches in the implementation below. |
| 4002 | class MethodNameAndSignatureComparator FINAL : public ValueObject { |
| 4003 | public: |
| 4004 | explicit MethodNameAndSignatureComparator(mirror::ArtMethod* method) |
| 4005 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) : |
| 4006 | dex_file_(method->GetDexFile()), mid_(&dex_file_->GetMethodId(method->GetDexMethodIndex())), |
| 4007 | name_(nullptr), name_len_(0) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4008 | DCHECK(!method->IsProxyMethod()) << PrettyMethod(method); |
| 4009 | } |
| 4010 | |
| 4011 | const char* GetName() { |
| 4012 | if (name_ == nullptr) { |
| 4013 | name_ = dex_file_->StringDataAndUtf16LengthByIdx(mid_->name_idx_, &name_len_); |
| 4014 | } |
| 4015 | return name_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 4016 | } |
| 4017 | |
| 4018 | bool HasSameNameAndSignature(mirror::ArtMethod* other) |
| 4019 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4020 | DCHECK(!other->IsProxyMethod()) << PrettyMethod(other); |
| 4021 | const DexFile* other_dex_file = other->GetDexFile(); |
| 4022 | const DexFile::MethodId& other_mid = other_dex_file->GetMethodId(other->GetDexMethodIndex()); |
| 4023 | if (dex_file_ == other_dex_file) { |
| 4024 | return mid_->name_idx_ == other_mid.name_idx_ && mid_->proto_idx_ == other_mid.proto_idx_; |
| 4025 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4026 | GetName(); // Only used to make sure its calculated. |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 4027 | uint32_t other_name_len; |
| 4028 | const char* other_name = other_dex_file->StringDataAndUtf16LengthByIdx(other_mid.name_idx_, |
| 4029 | &other_name_len); |
| 4030 | if (name_len_ != other_name_len || strcmp(name_, other_name) != 0) { |
| 4031 | return false; |
| 4032 | } |
| 4033 | return dex_file_->GetMethodSignature(*mid_) == other_dex_file->GetMethodSignature(other_mid); |
| 4034 | } |
| 4035 | |
| 4036 | private: |
| 4037 | // Dex file for the method to compare against. |
| 4038 | const DexFile* const dex_file_; |
| 4039 | // MethodId for the method to compare against. |
| 4040 | const DexFile::MethodId* const mid_; |
| 4041 | // Lazily computed name from the dex file's strings. |
| 4042 | const char* name_; |
| 4043 | // Lazily computed name length. |
| 4044 | uint32_t name_len_; |
| 4045 | }; |
| 4046 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4047 | class LinkVirtualHashTable { |
| 4048 | public: |
| 4049 | LinkVirtualHashTable(Handle<mirror::Class> klass, size_t hash_size, uint32_t* hash_table) |
| 4050 | : klass_(klass), hash_size_(hash_size), hash_table_(hash_table) { |
| 4051 | std::fill(hash_table_, hash_table_ + hash_size_, invalid_index_); |
| 4052 | } |
| 4053 | void Add(uint32_t virtual_method_index) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4054 | mirror::ArtMethod* local_method = klass_->GetVirtualMethodDuringLinking(virtual_method_index); |
| 4055 | const char* name = local_method->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 4056 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4057 | uint32_t index = hash % hash_size_; |
| 4058 | // Linear probe until we have an empty slot. |
| 4059 | while (hash_table_[index] != invalid_index_) { |
| 4060 | if (++index == hash_size_) { |
| 4061 | index = 0; |
| 4062 | } |
| 4063 | } |
| 4064 | hash_table_[index] = virtual_method_index; |
| 4065 | } |
| 4066 | uint32_t FindAndRemove(MethodNameAndSignatureComparator* comparator) |
| 4067 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4068 | const char* name = comparator->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 4069 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4070 | size_t index = hash % hash_size_; |
| 4071 | while (true) { |
| 4072 | const uint32_t value = hash_table_[index]; |
| 4073 | // Since linear probe makes continuous blocks, hitting an invalid index means we are done |
| 4074 | // the block and can safely assume not found. |
| 4075 | if (value == invalid_index_) { |
| 4076 | break; |
| 4077 | } |
| 4078 | if (value != removed_index_) { // This signifies not already overriden. |
| 4079 | mirror::ArtMethod* virtual_method = |
| 4080 | klass_->GetVirtualMethodDuringLinking(value); |
| 4081 | if (comparator->HasSameNameAndSignature(virtual_method->GetInterfaceMethodIfProxy())) { |
| 4082 | hash_table_[index] = removed_index_; |
| 4083 | return value; |
| 4084 | } |
| 4085 | } |
| 4086 | if (++index == hash_size_) { |
| 4087 | index = 0; |
| 4088 | } |
| 4089 | } |
| 4090 | return GetNotFoundIndex(); |
| 4091 | } |
| 4092 | static uint32_t GetNotFoundIndex() { |
| 4093 | return invalid_index_; |
| 4094 | } |
| 4095 | |
| 4096 | private: |
| 4097 | static const uint32_t invalid_index_; |
| 4098 | static const uint32_t removed_index_; |
| 4099 | |
| 4100 | Handle<mirror::Class> klass_; |
| 4101 | const size_t hash_size_; |
| 4102 | uint32_t* const hash_table_; |
| 4103 | }; |
| 4104 | |
| 4105 | const uint32_t LinkVirtualHashTable::invalid_index_ = std::numeric_limits<uint32_t>::max(); |
| 4106 | const uint32_t LinkVirtualHashTable::removed_index_ = std::numeric_limits<uint32_t>::max() - 1; |
| 4107 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4108 | bool ClassLinker::LinkVirtualMethods(Thread* self, Handle<mirror::Class> klass) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4109 | const size_t num_virtual_methods = klass->NumVirtualMethods(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4110 | if (klass->HasSuperClass()) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4111 | const size_t super_vtable_length = klass->GetSuperClass()->GetVTableLength(); |
| 4112 | const size_t max_count = num_virtual_methods + super_vtable_length; |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4113 | StackHandleScope<2> hs(self); |
Mingyao Yang | 38eecb0 | 2014-08-13 14:51:03 -0700 | [diff] [blame] | 4114 | Handle<mirror::Class> super_class(hs.NewHandle(klass->GetSuperClass())); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4115 | MutableHandle<mirror::ObjectArray<mirror::ArtMethod>> vtable; |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4116 | if (super_class->ShouldHaveEmbeddedImtAndVTable()) { |
| 4117 | vtable = hs.NewHandle(AllocArtMethodArray(self, max_count)); |
| 4118 | if (UNLIKELY(vtable.Get() == nullptr)) { |
| 4119 | CHECK(self->IsExceptionPending()); // OOME. |
| 4120 | return false; |
| 4121 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4122 | for (size_t i = 0; i < super_vtable_length; i++) { |
| 4123 | vtable->SetWithoutChecks<false>(i, super_class->GetEmbeddedVTableEntry(i)); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4124 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4125 | if (num_virtual_methods == 0) { |
| 4126 | klass->SetVTable(vtable.Get()); |
| 4127 | return true; |
| 4128 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4129 | } else { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4130 | mirror::ObjectArray<mirror::ArtMethod>* super_vtable = super_class->GetVTable(); |
| 4131 | CHECK(super_vtable != nullptr) << PrettyClass(super_class.Get()); |
| 4132 | if (num_virtual_methods == 0) { |
| 4133 | klass->SetVTable(super_vtable); |
| 4134 | return true; |
| 4135 | } |
| 4136 | vtable = hs.NewHandle(super_vtable->CopyOf(self, max_count)); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4137 | if (UNLIKELY(vtable.Get() == nullptr)) { |
| 4138 | CHECK(self->IsExceptionPending()); // OOME. |
| 4139 | return false; |
| 4140 | } |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4141 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4142 | // How the algorithm works: |
| 4143 | // 1. Populate hash table by adding num_virtual_methods from klass. The values in the hash |
| 4144 | // table are: invalid_index for unused slots, index super_vtable_length + i for a virtual |
| 4145 | // method which has not been matched to a vtable method, and j if the virtual method at the |
| 4146 | // index overrode the super virtual method at index j. |
| 4147 | // 2. Loop through super virtual methods, if they overwrite, update hash table to j |
| 4148 | // (j < super_vtable_length) to avoid redundant checks. (TODO maybe use this info for reducing |
| 4149 | // the need for the initial vtable which we later shrink back down). |
| 4150 | // 3. Add non overridden methods to the end of the vtable. |
| 4151 | static constexpr size_t kMaxStackHash = 250; |
| 4152 | const size_t hash_table_size = num_virtual_methods * 3; |
| 4153 | uint32_t* hash_table_ptr; |
| 4154 | std::unique_ptr<uint32_t[]> hash_heap_storage; |
| 4155 | if (hash_table_size <= kMaxStackHash) { |
| 4156 | hash_table_ptr = reinterpret_cast<uint32_t*>( |
| 4157 | alloca(hash_table_size * sizeof(*hash_table_ptr))); |
| 4158 | } else { |
| 4159 | hash_heap_storage.reset(new uint32_t[hash_table_size]); |
| 4160 | hash_table_ptr = hash_heap_storage.get(); |
| 4161 | } |
| 4162 | LinkVirtualHashTable hash_table(klass, hash_table_size, hash_table_ptr); |
| 4163 | // Add virtual methods to the hash table. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4164 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4165 | hash_table.Add(i); |
| 4166 | } |
| 4167 | // Loop through each super vtable method and see if they are overriden by a method we added to |
| 4168 | // the hash table. |
| 4169 | for (size_t j = 0; j < super_vtable_length; ++j) { |
| 4170 | // Search the hash table to see if we are overidden by any method. |
| 4171 | mirror::ArtMethod* super_method = vtable->GetWithoutChecks(j); |
| 4172 | MethodNameAndSignatureComparator super_method_name_comparator( |
| 4173 | super_method->GetInterfaceMethodIfProxy()); |
| 4174 | uint32_t hash_index = hash_table.FindAndRemove(&super_method_name_comparator); |
| 4175 | if (hash_index != hash_table.GetNotFoundIndex()) { |
| 4176 | mirror::ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(hash_index); |
| 4177 | if (klass->CanAccessMember(super_method->GetDeclaringClass(), |
| 4178 | super_method->GetAccessFlags())) { |
| 4179 | if (super_method->IsFinal()) { |
| 4180 | ThrowLinkageError(klass.Get(), "Method %s overrides final method in class %s", |
| 4181 | PrettyMethod(virtual_method).c_str(), |
| 4182 | super_method->GetDeclaringClassDescriptor()); |
| 4183 | return false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4184 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4185 | vtable->SetWithoutChecks<false>(j, virtual_method); |
| 4186 | virtual_method->SetMethodIndex(j); |
| 4187 | } else { |
| 4188 | LOG(WARNING) << "Before Android 4.1, method " << PrettyMethod(virtual_method) |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4189 | << " would have incorrectly overridden the package-private method in " |
| 4190 | << PrettyDescriptor(super_method->GetDeclaringClassDescriptor()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4191 | } |
| 4192 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4193 | } |
| 4194 | // Add the non overridden methods at the end. |
| 4195 | size_t actual_count = super_vtable_length; |
| 4196 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
| 4197 | mirror::ArtMethod* local_method = klass->GetVirtualMethodDuringLinking(i); |
| 4198 | size_t method_idx = local_method->GetMethodIndexDuringLinking(); |
| 4199 | if (method_idx < super_vtable_length && |
| 4200 | local_method == vtable->GetWithoutChecks(method_idx)) { |
| 4201 | continue; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4202 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4203 | vtable->SetWithoutChecks<false>(actual_count, local_method); |
| 4204 | local_method->SetMethodIndex(actual_count); |
| 4205 | ++actual_count; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4206 | } |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 4207 | if (!IsUint<16>(actual_count)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4208 | ThrowClassFormatError(klass.Get(), "Too many methods defined on class: %zd", actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4209 | return false; |
| 4210 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4211 | // Shrink vtable if possible |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4212 | CHECK_LE(actual_count, max_count); |
| 4213 | if (actual_count < max_count) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4214 | vtable.Assign(vtable->CopyOf(self, actual_count)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4215 | if (UNLIKELY(vtable.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4216 | CHECK(self->IsExceptionPending()); // OOME. |
| 4217 | return false; |
| 4218 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4219 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4220 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4221 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4222 | CHECK_EQ(klass.Get(), GetClassRoot(kJavaLangObject)); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 4223 | if (!IsUint<16>(num_virtual_methods)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4224 | ThrowClassFormatError(klass.Get(), "Too many methods: %d", |
| 4225 | static_cast<int>(num_virtual_methods)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4226 | return false; |
| 4227 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4228 | mirror::ObjectArray<mirror::ArtMethod>* vtable = AllocArtMethodArray(self, num_virtual_methods); |
| 4229 | if (UNLIKELY(vtable == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4230 | CHECK(self->IsExceptionPending()); // OOME. |
| 4231 | return false; |
| 4232 | } |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 4233 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4234 | mirror::ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4235 | vtable->SetWithoutChecks<false>(i, virtual_method); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4236 | virtual_method->SetMethodIndex(i & 0xFFFF); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4237 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4238 | klass->SetVTable(vtable); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4239 | } |
| 4240 | return true; |
| 4241 | } |
| 4242 | |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4243 | bool ClassLinker::LinkInterfaceMethods(Thread* self, Handle<mirror::Class> klass, |
| 4244 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
| 4245 | StackHandleScope<mirror::Class::kImtSize>* out_imt) { |
Mathieu Chartier | d035c2d | 2014-10-27 17:30:20 -0700 | [diff] [blame] | 4246 | StackHandleScope<3> hs(self); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4247 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4248 | const bool has_superclass = klass->HasSuperClass(); |
| 4249 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
| 4250 | const bool have_interfaces = interfaces.Get() != nullptr; |
| 4251 | const size_t num_interfaces = |
| 4252 | have_interfaces ? interfaces->GetLength() : klass->NumDirectInterfaces(); |
| 4253 | if (num_interfaces == 0) { |
| 4254 | if (super_ifcount == 0) { |
| 4255 | // Class implements no interfaces. |
| 4256 | DCHECK_EQ(klass->GetIfTableCount(), 0); |
| 4257 | DCHECK(klass->GetIfTable() == nullptr); |
| 4258 | return true; |
| 4259 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4260 | // Class implements same interfaces as parent, are any of these not marker interfaces? |
| 4261 | bool has_non_marker_interface = false; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4262 | mirror::IfTable* super_iftable = klass->GetSuperClass()->GetIfTable(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4263 | for (size_t i = 0; i < super_ifcount; ++i) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4264 | if (super_iftable->GetMethodArrayCount(i) > 0) { |
| 4265 | has_non_marker_interface = true; |
| 4266 | break; |
| 4267 | } |
| 4268 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4269 | // Class just inherits marker interfaces from parent so recycle parent's iftable. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4270 | if (!has_non_marker_interface) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4271 | klass->SetIfTable(super_iftable); |
| 4272 | return true; |
| 4273 | } |
| 4274 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4275 | size_t ifcount = super_ifcount + num_interfaces; |
| 4276 | for (size_t i = 0; i < num_interfaces; i++) { |
| 4277 | mirror::Class* interface = have_interfaces ? |
| 4278 | interfaces->GetWithoutChecks(i) : mirror::Class::GetDirectInterface(self, klass, i); |
| 4279 | DCHECK(interface != nullptr); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4280 | if (UNLIKELY(!interface->IsInterface())) { |
| 4281 | std::string temp; |
| 4282 | ThrowIncompatibleClassChangeError(klass.Get(), "Class %s implements non-interface class %s", |
| 4283 | PrettyDescriptor(klass.Get()).c_str(), |
| 4284 | PrettyDescriptor(interface->GetDescriptor(&temp)).c_str()); |
| 4285 | return false; |
| 4286 | } |
| 4287 | ifcount += interface->GetIfTableCount(); |
| 4288 | } |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4289 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(AllocIfTable(self, ifcount))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4290 | if (UNLIKELY(iftable.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4291 | CHECK(self->IsExceptionPending()); // OOME. |
| 4292 | return false; |
| 4293 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4294 | if (super_ifcount != 0) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4295 | mirror::IfTable* super_iftable = klass->GetSuperClass()->GetIfTable(); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 4296 | for (size_t i = 0; i < super_ifcount; i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4297 | mirror::Class* super_interface = super_iftable->GetInterface(i); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4298 | iftable->SetInterface(i, super_interface); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 4299 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4300 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4301 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4302 | // Flatten the interface inheritance hierarchy. |
| 4303 | size_t idx = super_ifcount; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4304 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | d035c2d | 2014-10-27 17:30:20 -0700 | [diff] [blame] | 4305 | mirror::Class* interface = have_interfaces ? interfaces->Get(i) : |
| 4306 | mirror::Class::GetDirectInterface(self, klass, i); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4307 | // Check if interface is already in iftable |
| 4308 | bool duplicate = false; |
| 4309 | for (size_t j = 0; j < idx; j++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4310 | mirror::Class* existing_interface = iftable->GetInterface(j); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4311 | if (existing_interface == interface) { |
| 4312 | duplicate = true; |
| 4313 | break; |
| 4314 | } |
| 4315 | } |
| 4316 | if (!duplicate) { |
| 4317 | // Add this non-duplicate interface. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4318 | iftable->SetInterface(idx++, interface); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4319 | // Add this interface's non-duplicate super-interfaces. |
| 4320 | for (int32_t j = 0; j < interface->GetIfTableCount(); j++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4321 | mirror::Class* super_interface = interface->GetIfTable()->GetInterface(j); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4322 | bool super_duplicate = false; |
| 4323 | for (size_t k = 0; k < idx; k++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4324 | mirror::Class* existing_interface = iftable->GetInterface(k); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4325 | if (existing_interface == super_interface) { |
| 4326 | super_duplicate = true; |
| 4327 | break; |
| 4328 | } |
| 4329 | } |
| 4330 | if (!super_duplicate) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4331 | iftable->SetInterface(idx++, super_interface); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4332 | } |
| 4333 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4334 | } |
| 4335 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4336 | self->AllowThreadSuspension(); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4337 | // Shrink iftable in case duplicates were found |
| 4338 | if (idx < ifcount) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4339 | DCHECK_NE(num_interfaces, 0U); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4340 | iftable.Assign(down_cast<mirror::IfTable*>(iftable->CopyOf(self, idx * mirror::IfTable::kMax))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4341 | if (UNLIKELY(iftable.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4342 | CHECK(self->IsExceptionPending()); // OOME. |
| 4343 | return false; |
| 4344 | } |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4345 | ifcount = idx; |
| 4346 | } else { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4347 | DCHECK_EQ(idx, ifcount); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4348 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4349 | klass->SetIfTable(iftable.Get()); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 4350 | // 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] | 4351 | if (klass->IsInterface()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4352 | return true; |
| 4353 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4354 | size_t miranda_list_size = 0; |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4355 | size_t max_miranda_methods = 0; // The max size of miranda_list. |
| 4356 | for (size_t i = 0; i < ifcount; ++i) { |
| 4357 | max_miranda_methods += iftable->GetInterface(i)->NumVirtualMethods(); |
| 4358 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4359 | MutableHandle<mirror::ObjectArray<mirror::ArtMethod>> |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4360 | miranda_list(hs.NewHandle(AllocArtMethodArray(self, max_miranda_methods))); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4361 | MutableHandle<mirror::ObjectArray<mirror::ArtMethod>> vtable( |
| 4362 | hs.NewHandle(klass->GetVTableDuringLinking())); |
| 4363 | // Copy the IMT from the super class if possible. |
| 4364 | bool extend_super_iftable = false; |
| 4365 | if (has_superclass) { |
| 4366 | mirror::Class* super_class = klass->GetSuperClass(); |
| 4367 | extend_super_iftable = true; |
| 4368 | if (super_class->ShouldHaveEmbeddedImtAndVTable()) { |
| 4369 | for (size_t i = 0; i < mirror::Class::kImtSize; ++i) { |
| 4370 | out_imt->SetReference(i, super_class->GetEmbeddedImTableEntry(i)); |
| 4371 | } |
| 4372 | } else { |
| 4373 | // No imt in the super class, need to reconstruct from the iftable. |
| 4374 | mirror::IfTable* if_table = super_class->GetIfTable(); |
| 4375 | mirror::ArtMethod* conflict_method = runtime->GetImtConflictMethod(); |
| 4376 | const size_t length = super_class->GetIfTableCount(); |
| 4377 | for (size_t i = 0; i < length; ++i) { |
| 4378 | mirror::Class* interface = iftable->GetInterface(i); |
| 4379 | const size_t num_virtuals = interface->NumVirtualMethods(); |
| 4380 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 4381 | DCHECK_EQ(num_virtuals, method_array_count); |
| 4382 | if (method_array_count == 0) { |
| 4383 | continue; |
| 4384 | } |
| 4385 | mirror::ObjectArray<mirror::ArtMethod>* method_array = if_table->GetMethodArray(i); |
| 4386 | for (size_t j = 0; j < num_virtuals; ++j) { |
| 4387 | mirror::ArtMethod* method = method_array->GetWithoutChecks(j); |
| 4388 | if (method->IsMiranda()) { |
| 4389 | continue; |
| 4390 | } |
| 4391 | mirror::ArtMethod* interface_method = interface->GetVirtualMethod(j); |
| 4392 | uint32_t imt_index = interface_method->GetDexMethodIndex() % mirror::Class::kImtSize; |
| 4393 | mirror::ArtMethod* imt_ref = out_imt->GetReference(imt_index)->AsArtMethod(); |
| 4394 | if (imt_ref == runtime->GetImtUnimplementedMethod()) { |
| 4395 | out_imt->SetReference(imt_index, method); |
| 4396 | } else if (imt_ref != conflict_method) { |
| 4397 | out_imt->SetReference(imt_index, conflict_method); |
| 4398 | } |
| 4399 | } |
| 4400 | } |
| 4401 | } |
| 4402 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 4403 | for (size_t i = 0; i < ifcount; ++i) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4404 | self->AllowThreadSuspension(); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4405 | size_t num_methods = iftable->GetInterface(i)->NumVirtualMethods(); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4406 | if (num_methods > 0) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 4407 | StackHandleScope<2> hs2(self); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4408 | const bool is_super = i < super_ifcount; |
| 4409 | const bool super_interface = is_super && extend_super_iftable; |
| 4410 | Handle<mirror::ObjectArray<mirror::ArtMethod>> method_array; |
| 4411 | Handle<mirror::ObjectArray<mirror::ArtMethod>> input_array; |
| 4412 | if (super_interface) { |
| 4413 | mirror::IfTable* if_table = klass->GetSuperClass()->GetIfTable(); |
| 4414 | DCHECK(if_table != nullptr); |
| 4415 | DCHECK(if_table->GetMethodArray(i) != nullptr); |
| 4416 | // If we are working on a super interface, try extending the existing method array. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 4417 | method_array = hs2.NewHandle(if_table->GetMethodArray(i)->Clone(self)-> |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4418 | AsObjectArray<mirror::ArtMethod>()); |
| 4419 | // We are overwriting a super class interface, try to only virtual methods instead of the |
| 4420 | // whole vtable. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 4421 | input_array = hs2.NewHandle(klass->GetVirtualMethods()); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4422 | } else { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 4423 | method_array = hs2.NewHandle(AllocArtMethodArray(self, num_methods)); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4424 | // A new interface, we need the whole vtable incase a new interface method is implemented |
| 4425 | // in the whole superclass. |
| 4426 | input_array = vtable; |
| 4427 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4428 | if (UNLIKELY(method_array.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4429 | CHECK(self->IsExceptionPending()); // OOME. |
| 4430 | return false; |
| 4431 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4432 | iftable->SetMethodArray(i, method_array.Get()); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4433 | if (input_array.Get() == nullptr) { |
| 4434 | // If the added virtual methods is empty, do nothing. |
| 4435 | DCHECK(super_interface); |
| 4436 | continue; |
| 4437 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4438 | for (size_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4439 | mirror::ArtMethod* interface_method = iftable->GetInterface(i)->GetVirtualMethod(j); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4440 | MethodNameAndSignatureComparator interface_name_comparator( |
| 4441 | interface_method->GetInterfaceMethodIfProxy()); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4442 | int32_t k; |
| 4443 | // For each method listed in the interface's method list, find the |
| 4444 | // matching method in our class's method list. We want to favor the |
| 4445 | // subclass over the superclass, which just requires walking |
| 4446 | // back from the end of the vtable. (This only matters if the |
| 4447 | // superclass defines a private method and this class redefines |
| 4448 | // it -- otherwise it would use the same vtable slot. In .dex files |
| 4449 | // those don't end up in the virtual method table, so it shouldn't |
| 4450 | // matter which direction we go. We walk it backward anyway.) |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4451 | for (k = input_array->GetLength() - 1; k >= 0; --k) { |
| 4452 | mirror::ArtMethod* vtable_method = input_array->GetWithoutChecks(k); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 4453 | mirror::ArtMethod* vtable_method_for_name_comparison = |
| 4454 | vtable_method->GetInterfaceMethodIfProxy(); |
| 4455 | if (interface_name_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4456 | vtable_method_for_name_comparison)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4457 | if (!vtable_method->IsAbstract() && !vtable_method->IsPublic()) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4458 | ThrowIllegalAccessError( |
| 4459 | klass.Get(), |
| 4460 | "Method '%s' implementing interface method '%s' is not public", |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4461 | PrettyMethod(vtable_method).c_str(), |
| 4462 | PrettyMethod(interface_method).c_str()); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4463 | return false; |
| 4464 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4465 | method_array->SetWithoutChecks<false>(j, vtable_method); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 4466 | // Place method in imt if entry is empty, place conflict otherwise. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4467 | uint32_t imt_index = interface_method->GetDexMethodIndex() % mirror::Class::kImtSize; |
| 4468 | mirror::ArtMethod* imt_ref = out_imt->GetReference(imt_index)->AsArtMethod(); |
| 4469 | mirror::ArtMethod* conflict_method = runtime->GetImtConflictMethod(); |
| 4470 | if (imt_ref == runtime->GetImtUnimplementedMethod()) { |
| 4471 | out_imt->SetReference(imt_index, vtable_method); |
| 4472 | } else if (imt_ref != conflict_method) { |
| 4473 | // If we are not a conflict and we have the same signature and name as the imt entry, |
| 4474 | // it must be that we overwrote a superclass vtable entry. |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4475 | MethodNameAndSignatureComparator imt_ref_name_comparator( |
| 4476 | imt_ref->GetInterfaceMethodIfProxy()); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 4477 | if (imt_ref_name_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4478 | vtable_method_for_name_comparison)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4479 | out_imt->SetReference(imt_index, vtable_method); |
| 4480 | } else { |
| 4481 | out_imt->SetReference(imt_index, conflict_method); |
| 4482 | } |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 4483 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4484 | break; |
| 4485 | } |
| 4486 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4487 | if (k < 0 && !super_interface) { |
| 4488 | mirror::ArtMethod* miranda_method = nullptr; |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4489 | for (size_t l = 0; l < miranda_list_size; ++l) { |
| 4490 | mirror::ArtMethod* mir_method = miranda_list->Get(l); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 4491 | if (interface_name_comparator.HasSameNameAndSignature(mir_method)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4492 | miranda_method = mir_method; |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4493 | break; |
| 4494 | } |
| 4495 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4496 | if (miranda_method == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4497 | // Point the interface table at a phantom slot. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4498 | miranda_method = interface_method->Clone(self)->AsArtMethod(); |
| 4499 | if (UNLIKELY(miranda_method == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4500 | CHECK(self->IsExceptionPending()); // OOME. |
| 4501 | return false; |
| 4502 | } |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4503 | DCHECK_LT(miranda_list_size, max_miranda_methods); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4504 | miranda_list->Set<false>(miranda_list_size++, miranda_method); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4505 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4506 | method_array->SetWithoutChecks<false>(j, miranda_method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4507 | } |
| 4508 | } |
| 4509 | } |
| 4510 | } |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4511 | if (miranda_list_size > 0) { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 4512 | int old_method_count = klass->NumVirtualMethods(); |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4513 | int new_method_count = old_method_count + miranda_list_size; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4514 | mirror::ObjectArray<mirror::ArtMethod>* virtuals; |
| 4515 | if (old_method_count == 0) { |
| 4516 | virtuals = AllocArtMethodArray(self, new_method_count); |
| 4517 | } else { |
| 4518 | virtuals = klass->GetVirtualMethods()->CopyOf(self, new_method_count); |
| 4519 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4520 | if (UNLIKELY(virtuals == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4521 | CHECK(self->IsExceptionPending()); // OOME. |
| 4522 | return false; |
| 4523 | } |
| 4524 | klass->SetVirtualMethods(virtuals); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4525 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4526 | int old_vtable_count = vtable->GetLength(); |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4527 | int new_vtable_count = old_vtable_count + miranda_list_size; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4528 | vtable.Assign(vtable->CopyOf(self, new_vtable_count)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4529 | if (UNLIKELY(vtable.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4530 | CHECK(self->IsExceptionPending()); // OOME. |
| 4531 | return false; |
| 4532 | } |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4533 | for (size_t i = 0; i < miranda_list_size; ++i) { |
| 4534 | mirror::ArtMethod* method = miranda_list->Get(i); |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 4535 | // Leave the declaring class alone as type indices are relative to it |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 4536 | method->SetAccessFlags(method->GetAccessFlags() | kAccMiranda); |
| 4537 | method->SetMethodIndex(0xFFFF & (old_vtable_count + i)); |
| 4538 | klass->SetVirtualMethod(old_method_count + i, method); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4539 | vtable->SetWithoutChecks<false>(old_vtable_count + i, method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4540 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4541 | // TODO: do not assign to the vtable field until it is fully constructed. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4542 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4543 | } |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 4544 | |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4545 | if (kIsDebugBuild) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 4546 | mirror::ObjectArray<mirror::ArtMethod>* check_vtable = klass->GetVTableDuringLinking(); |
| 4547 | for (int i = 0; i < check_vtable->GetLength(); ++i) { |
| 4548 | CHECK(check_vtable->GetWithoutChecks(i) != nullptr); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4549 | } |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 4550 | } |
| 4551 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4552 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4553 | return true; |
| 4554 | } |
| 4555 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4556 | bool ClassLinker::LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4557 | CHECK(klass.Get() != nullptr); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4558 | return LinkFields(self, klass, false, nullptr); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 4559 | } |
| 4560 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4561 | 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] | 4562 | CHECK(klass.Get() != nullptr); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4563 | return LinkFields(self, klass, true, class_size); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 4564 | } |
| 4565 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4566 | struct LinkFieldsComparator { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4567 | explicit LinkFieldsComparator() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4568 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4569 | // No thread safety analysis as will be called from STL. Checked lock held in constructor. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 4570 | bool operator()(mirror::ArtField* field1, mirror::ArtField* field2) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4571 | NO_THREAD_SAFETY_ANALYSIS { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 4572 | // 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] | 4573 | Primitive::Type type1 = field1->GetTypeAsPrimitiveType(); |
| 4574 | Primitive::Type type2 = field2->GetTypeAsPrimitiveType(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 4575 | if (type1 != type2) { |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 4576 | if (type1 == Primitive::kPrimNot) { |
| 4577 | // Reference always goes first. |
| 4578 | return true; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 4579 | } |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 4580 | if (type2 == Primitive::kPrimNot) { |
| 4581 | // Reference always goes first. |
| 4582 | return false; |
| 4583 | } |
| 4584 | size_t size1 = Primitive::ComponentSize(type1); |
| 4585 | size_t size2 = Primitive::ComponentSize(type2); |
| 4586 | if (size1 != size2) { |
| 4587 | // Larger primitive types go first. |
| 4588 | return size1 > size2; |
| 4589 | } |
| 4590 | // Primitive types differ but sizes match. Arbitrarily order by primitive type. |
| 4591 | return type1 < type2; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4592 | } |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 4593 | // Same basic group? Then sort by dex field index. This is guaranteed to be sorted |
| 4594 | // by name and for equal names by type id index. |
| 4595 | // NOTE: This works also for proxies. Their static fields are assigned appropriate indexes. |
| 4596 | return field1->GetDexFieldIndex() < field2->GetDexFieldIndex(); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4597 | } |
| 4598 | }; |
| 4599 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4600 | bool ClassLinker::LinkFields(Thread* self, Handle<mirror::Class> klass, bool is_static, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4601 | size_t* class_size) { |
| 4602 | self->AllowThreadSuspension(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4603 | size_t num_fields = |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 4604 | is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4605 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4606 | mirror::ObjectArray<mirror::ArtField>* fields = |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 4607 | is_static ? klass->GetSFields() : klass->GetIFields(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4608 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4609 | // Initialize field_offset |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 4610 | MemberOffset field_offset(0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 4611 | if (is_static) { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 4612 | field_offset = klass->GetFirstReferenceStaticFieldOffsetDuringLinking(); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 4613 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4614 | mirror::Class* super_class = klass->GetSuperClass(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4615 | if (super_class != nullptr) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4616 | CHECK(super_class->IsResolved()) |
| 4617 | << PrettyClass(klass.Get()) << " " << PrettyClass(super_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4618 | field_offset = MemberOffset(super_class->GetObjectSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4619 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4620 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4621 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4622 | CHECK_EQ(num_fields == 0, fields == nullptr) << PrettyClass(klass.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4623 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4624 | // we want a relatively stable order so that adding new fields |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 4625 | // minimizes disruption of C++ version such as Class and Method. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4626 | std::deque<mirror::ArtField*> grouped_and_sorted_fields; |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 4627 | const char* old_no_suspend_cause = self->StartAssertNoThreadSuspension( |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 4628 | "Naked ArtField references in deque"); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4629 | for (size_t i = 0; i < num_fields; i++) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4630 | mirror::ArtField* f = fields->Get(i); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4631 | CHECK(f != nullptr) << PrettyClass(klass.Get()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4632 | grouped_and_sorted_fields.push_back(f); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4633 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4634 | std::sort(grouped_and_sorted_fields.begin(), grouped_and_sorted_fields.end(), |
| 4635 | LinkFieldsComparator()); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4636 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 4637 | // References should be at the front. |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4638 | size_t current_field = 0; |
| 4639 | size_t num_reference_fields = 0; |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 4640 | FieldGaps gaps; |
| 4641 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4642 | for (; current_field < num_fields; current_field++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4643 | mirror::ArtField* field = grouped_and_sorted_fields.front(); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 4644 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 4645 | bool isPrimitive = type != Primitive::kPrimNot; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4646 | if (isPrimitive) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4647 | break; // past last reference, move on to the next phase |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4648 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 4649 | if (UNLIKELY(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>( |
| 4650 | field_offset.Uint32Value()))) { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 4651 | MemberOffset old_offset = field_offset; |
| 4652 | field_offset = MemberOffset(RoundUp(field_offset.Uint32Value(), 4)); |
| 4653 | AddFieldGap(old_offset.Uint32Value(), field_offset.Uint32Value(), &gaps); |
| 4654 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 4655 | DCHECK(IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(field_offset.Uint32Value())); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4656 | grouped_and_sorted_fields.pop_front(); |
| 4657 | num_reference_fields++; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4658 | field->SetOffset(field_offset); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 4659 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 4660 | sizeof(mirror::HeapReference<mirror::Object>)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4661 | } |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 4662 | // Gaps are stored as a max heap which means that we must shuffle from largest to smallest |
| 4663 | // otherwise we could end up with suboptimal gap fills. |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 4664 | ShuffleForward<8>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 4665 | ShuffleForward<4>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 4666 | ShuffleForward<2>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 4667 | ShuffleForward<1>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 4668 | CHECK(grouped_and_sorted_fields.empty()) << "Missed " << grouped_and_sorted_fields.size() << |
| 4669 | " fields."; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4670 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4671 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 4672 | // 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] | 4673 | if (!is_static && klass->DescriptorEquals("Ljava/lang/ref/Reference;")) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 4674 | // We know there are no non-reference fields in the Reference classes, and we know |
| 4675 | // that 'referent' is alphabetically last, so this is easy... |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4676 | CHECK_EQ(num_reference_fields, num_fields) << PrettyClass(klass.Get()); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 4677 | CHECK_STREQ(fields->Get(num_fields - 1)->GetName(), "referent") << PrettyClass(klass.Get()); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 4678 | --num_reference_fields; |
| 4679 | } |
| 4680 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4681 | size_t size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4682 | // Update klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 4683 | if (is_static) { |
| 4684 | klass->SetNumReferenceStaticFields(num_reference_fields); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4685 | *class_size = size; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 4686 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4687 | klass->SetNumReferenceInstanceFields(num_reference_fields); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4688 | if (!klass->IsVariableSize()) { |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 4689 | if (klass->DescriptorEquals("Ljava/lang/reflect/ArtMethod;")) { |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 4690 | size_t pointer_size = GetInstructionSetPointerSize(Runtime::Current()->GetInstructionSet()); |
| 4691 | klass->SetObjectSize(mirror::ArtMethod::InstanceSize(pointer_size)); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 4692 | } else { |
| 4693 | std::string temp; |
| 4694 | DCHECK_GE(size, sizeof(mirror::Object)) << klass->GetDescriptor(&temp); |
| 4695 | size_t previous_size = klass->GetObjectSize(); |
| 4696 | if (previous_size != 0) { |
| 4697 | // Make sure that we didn't originally have an incorrect size. |
| 4698 | CHECK_EQ(previous_size, size) << klass->GetDescriptor(&temp); |
| 4699 | } |
| 4700 | klass->SetObjectSize(size); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 4701 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4702 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4703 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 4704 | |
| 4705 | if (kIsDebugBuild) { |
| 4706 | // Make sure that the fields array is ordered by name but all reference |
| 4707 | // offsets are at the beginning as far as alignment allows. |
| 4708 | MemberOffset start_ref_offset = is_static |
| 4709 | ? klass->GetFirstReferenceStaticFieldOffsetDuringLinking() |
| 4710 | : klass->GetFirstReferenceInstanceFieldOffset(); |
| 4711 | MemberOffset end_ref_offset(start_ref_offset.Uint32Value() + |
| 4712 | num_reference_fields * |
| 4713 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 4714 | MemberOffset current_ref_offset = start_ref_offset; |
| 4715 | for (size_t i = 0; i < num_fields; i++) { |
| 4716 | mirror::ArtField* field = fields->Get(i); |
| 4717 | if ((false)) { // enable to debug field layout |
| 4718 | LOG(INFO) << "LinkFields: " << (is_static ? "static" : "instance") |
| 4719 | << " class=" << PrettyClass(klass.Get()) |
| 4720 | << " field=" << PrettyField(field) |
| 4721 | << " offset=" |
| 4722 | << field->GetField32(mirror::ArtField::OffsetOffset()); |
| 4723 | } |
| 4724 | if (i != 0) { |
| 4725 | mirror::ArtField* prev_field = fields->Get(i - 1u); |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 4726 | // NOTE: The field names can be the same. This is not possible in the Java language |
| 4727 | // but it's valid Java/dex bytecode and for example proguard can generate such bytecode. |
| 4728 | CHECK_LE(strcmp(prev_field->GetName(), field->GetName()), 0); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 4729 | } |
| 4730 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 4731 | bool is_primitive = type != Primitive::kPrimNot; |
| 4732 | if (klass->DescriptorEquals("Ljava/lang/ref/Reference;") && |
| 4733 | strcmp("referent", field->GetName()) == 0) { |
| 4734 | is_primitive = true; // We lied above, so we have to expect a lie here. |
| 4735 | } |
| 4736 | MemberOffset offset = field->GetOffsetDuringLinking(); |
| 4737 | if (is_primitive) { |
| 4738 | if (offset.Uint32Value() < end_ref_offset.Uint32Value()) { |
| 4739 | // Shuffled before references. |
| 4740 | size_t type_size = Primitive::ComponentSize(type); |
| 4741 | CHECK_LT(type_size, sizeof(mirror::HeapReference<mirror::Object>)); |
| 4742 | CHECK_LT(offset.Uint32Value(), start_ref_offset.Uint32Value()); |
| 4743 | CHECK_LE(offset.Uint32Value() + type_size, start_ref_offset.Uint32Value()); |
| 4744 | CHECK(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(offset.Uint32Value())); |
| 4745 | } |
| 4746 | } else { |
| 4747 | CHECK_EQ(current_ref_offset.Uint32Value(), offset.Uint32Value()); |
| 4748 | current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() + |
| 4749 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 4750 | } |
| 4751 | } |
| 4752 | CHECK_EQ(current_ref_offset.Uint32Value(), end_ref_offset.Uint32Value()); |
| 4753 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4754 | return true; |
| 4755 | } |
| 4756 | |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 4757 | // Set the bitmap of reference instance field offsets. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4758 | void ClassLinker::CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4759 | uint32_t reference_offsets = 0; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4760 | mirror::Class* super_class = klass->GetSuperClass(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 4761 | // 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] | 4762 | if (super_class != nullptr) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4763 | reference_offsets = super_class->GetReferenceInstanceOffsets(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 4764 | // Compute reference offsets unless our superclass overflowed. |
| 4765 | if (reference_offsets != mirror::Class::kClassWalkSuper) { |
| 4766 | size_t num_reference_fields = klass->NumReferenceInstanceFieldsDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 4767 | if (num_reference_fields != 0u) { |
| 4768 | // All of the fields that contain object references are guaranteed be grouped in memory |
| 4769 | // starting at an appropriately aligned address after super class object data. |
| 4770 | uint32_t start_offset = RoundUp(super_class->GetObjectSize(), |
| 4771 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 4772 | uint32_t start_bit = (start_offset - mirror::kObjectHeaderSize) / |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 4773 | sizeof(mirror::HeapReference<mirror::Object>); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 4774 | if (start_bit + num_reference_fields > 32) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 4775 | reference_offsets = mirror::Class::kClassWalkSuper; |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 4776 | } else { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 4777 | reference_offsets |= (0xffffffffu << start_bit) & |
| 4778 | (0xffffffffu >> (32 - (start_bit + num_reference_fields))); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 4779 | } |
| 4780 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 4781 | } |
| 4782 | } |
Mingyao Yang | faff0f0 | 2014-09-10 12:03:22 -0700 | [diff] [blame] | 4783 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4784 | } |
| 4785 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4786 | mirror::String* ClassLinker::ResolveString(const DexFile& dex_file, uint32_t string_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4787 | Handle<mirror::DexCache> dex_cache) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4788 | DCHECK(dex_cache.Get() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4789 | mirror::String* resolved = dex_cache->GetResolvedString(string_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4790 | if (resolved != nullptr) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4791 | return resolved; |
| 4792 | } |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 4793 | uint32_t utf16_length; |
| 4794 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4795 | mirror::String* string = intern_table_->InternStrong(utf16_length, utf8_data); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 4796 | dex_cache->SetResolvedString(string_idx, string); |
| 4797 | return string; |
| 4798 | } |
| 4799 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4800 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, uint16_t type_idx, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 4801 | mirror::Class* referrer) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4802 | StackHandleScope<2> hs(Thread::Current()); |
| 4803 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 4804 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4805 | return ResolveType(dex_file, type_idx, dex_cache, class_loader); |
| 4806 | } |
| 4807 | |
| 4808 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, uint16_t type_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4809 | Handle<mirror::DexCache> dex_cache, |
| 4810 | Handle<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4811 | DCHECK(dex_cache.Get() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4812 | mirror::Class* resolved = dex_cache->GetResolvedType(type_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4813 | if (resolved == nullptr) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 4814 | Thread* self = Thread::Current(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 4815 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 4816 | resolved = FindClass(self, descriptor, class_loader); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4817 | if (resolved != nullptr) { |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 4818 | // TODO: we used to throw here if resolved's class loader was not the |
| 4819 | // boot class loader. This was to permit different classes with the |
| 4820 | // same name to be loaded simultaneously by different loaders |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4821 | dex_cache->SetResolvedType(type_idx, resolved); |
| 4822 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4823 | CHECK(self->IsExceptionPending()) |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 4824 | << "Expected pending exception for failed resolution of: " << descriptor; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4825 | // Convert a ClassNotFoundException to a NoClassDefFoundError. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4826 | StackHandleScope<1> hs(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 4827 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4828 | if (cause->InstanceOf(GetClassRoot(kJavaLangClassNotFoundException))) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4829 | DCHECK(resolved == nullptr); // No Handle needed to preserve resolved. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 4830 | self->ClearException(); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4831 | ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 4832 | self->GetException()->SetCause(cause.Get()); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4833 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4834 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4835 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4836 | DCHECK((resolved == nullptr) || resolved->IsResolved() || resolved->IsErroneous()) |
Brian Carlstrom | abcf7ae | 2013-09-23 22:19:52 -0700 | [diff] [blame] | 4837 | << PrettyDescriptor(resolved) << " " << resolved->GetStatus(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4838 | return resolved; |
| 4839 | } |
| 4840 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4841 | mirror::ArtMethod* ClassLinker::ResolveMethod(const DexFile& dex_file, uint32_t method_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4842 | Handle<mirror::DexCache> dex_cache, |
| 4843 | Handle<mirror::ClassLoader> class_loader, |
| 4844 | Handle<mirror::ArtMethod> referrer, |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 4845 | InvokeType type) { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 4846 | DCHECK(dex_cache.Get() != nullptr); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4847 | // Check for hit in the dex cache. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4848 | mirror::ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4849 | if (resolved != nullptr && !resolved->IsRuntimeMethod()) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 4850 | return resolved; |
| 4851 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4852 | // Fail, get the declaring class. |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 4853 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4854 | 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] | 4855 | if (klass == nullptr) { |
Elliott Hughes | cc5f9a9 | 2011-09-28 19:17:29 -0700 | [diff] [blame] | 4856 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 4857 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 4858 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4859 | // Scan using method_idx, this saves string compares but will only hit for matching dex |
| 4860 | // caches/files. |
| 4861 | switch (type) { |
| 4862 | case kDirect: // Fall-through. |
| 4863 | case kStatic: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4864 | resolved = klass->FindDirectMethod(dex_cache.Get(), method_idx); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4865 | break; |
| 4866 | case kInterface: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4867 | resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 4868 | DCHECK(resolved == nullptr || resolved->GetDeclaringClass()->IsInterface()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4869 | break; |
| 4870 | case kSuper: // Fall-through. |
| 4871 | case kVirtual: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4872 | resolved = klass->FindVirtualMethod(dex_cache.Get(), method_idx); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4873 | break; |
| 4874 | default: |
| 4875 | LOG(FATAL) << "Unreachable - invocation type: " << type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 4876 | UNREACHABLE(); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 4877 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 4878 | if (resolved == nullptr) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4879 | // Search by name, which works across dex files. |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4880 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 4881 | const Signature signature = dex_file.GetMethodSignature(method_id); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4882 | switch (type) { |
| 4883 | case kDirect: // Fall-through. |
| 4884 | case kStatic: |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4885 | resolved = klass->FindDirectMethod(name, signature); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4886 | break; |
| 4887 | case kInterface: |
| 4888 | resolved = klass->FindInterfaceMethod(name, signature); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 4889 | DCHECK(resolved == nullptr || resolved->GetDeclaringClass()->IsInterface()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4890 | break; |
| 4891 | case kSuper: // Fall-through. |
| 4892 | case kVirtual: |
| 4893 | resolved = klass->FindVirtualMethod(name, signature); |
| 4894 | break; |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4895 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 4896 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 4897 | // If we found a method, check for incompatible class changes. |
| 4898 | if (LIKELY(resolved != nullptr && !resolved->CheckIncompatibleClassChange(type))) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4899 | // Be a good citizen and update the dex cache to speed subsequent calls. |
| 4900 | dex_cache->SetResolvedMethod(method_idx, resolved); |
| 4901 | return resolved; |
| 4902 | } else { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 4903 | // If we had a method, it's an incompatible-class-change error. |
| 4904 | if (resolved != nullptr) { |
| 4905 | ThrowIncompatibleClassChangeError(type, resolved->GetInvokeType(), resolved, referrer.Get()); |
| 4906 | } else { |
| 4907 | // We failed to find the method which means either an access error, an incompatible class |
| 4908 | // change, or no such method. First try to find the method among direct and virtual methods. |
| 4909 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
| 4910 | const Signature signature = dex_file.GetMethodSignature(method_id); |
| 4911 | switch (type) { |
| 4912 | case kDirect: |
| 4913 | case kStatic: |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4914 | resolved = klass->FindVirtualMethod(name, signature); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 4915 | // Note: kDirect and kStatic are also mutually exclusive, but in that case we would |
| 4916 | // have had a resolved method before, which triggers the "true" branch above. |
| 4917 | break; |
| 4918 | case kInterface: |
| 4919 | case kVirtual: |
| 4920 | case kSuper: |
| 4921 | resolved = klass->FindDirectMethod(name, signature); |
| 4922 | break; |
| 4923 | } |
| 4924 | |
| 4925 | // 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] | 4926 | bool exception_generated = false; |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 4927 | if (resolved != nullptr && referrer.Get() != nullptr) { |
| 4928 | mirror::Class* methods_class = resolved->GetDeclaringClass(); |
| 4929 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
| 4930 | if (!referring_class->CanAccess(methods_class)) { |
| 4931 | ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, methods_class, |
| 4932 | resolved, type); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4933 | exception_generated = true; |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 4934 | } else if (!referring_class->CanAccessMember(methods_class, |
| 4935 | resolved->GetAccessFlags())) { |
| 4936 | ThrowIllegalAccessErrorMethod(referring_class, resolved); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4937 | exception_generated = true; |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 4938 | } |
| 4939 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4940 | if (!exception_generated) { |
| 4941 | // Otherwise, throw an IncompatibleClassChangeError if we found something, and check |
| 4942 | // interface methods and throw if we find the method there. If we find nothing, throw a |
| 4943 | // NoSuchMethodError. |
| 4944 | switch (type) { |
| 4945 | case kDirect: |
| 4946 | case kStatic: |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 4947 | if (resolved != nullptr) { |
| 4948 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer.Get()); |
| 4949 | } else { |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4950 | resolved = klass->FindInterfaceMethod(name, signature); |
| 4951 | if (resolved != nullptr) { |
| 4952 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer.Get()); |
| 4953 | } else { |
| 4954 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 4955 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 4956 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4957 | break; |
| 4958 | case kInterface: |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 4959 | if (resolved != nullptr) { |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4960 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer.Get()); |
| 4961 | } else { |
| 4962 | resolved = klass->FindVirtualMethod(name, signature); |
| 4963 | if (resolved != nullptr) { |
| 4964 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer.Get()); |
| 4965 | } else { |
| 4966 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 4967 | } |
| 4968 | } |
| 4969 | break; |
| 4970 | case kSuper: |
| 4971 | if (resolved != nullptr) { |
| 4972 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer.Get()); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 4973 | } else { |
| 4974 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 4975 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4976 | break; |
| 4977 | case kVirtual: |
| 4978 | if (resolved != nullptr) { |
| 4979 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer.Get()); |
| 4980 | } else { |
| 4981 | resolved = klass->FindInterfaceMethod(name, signature); |
| 4982 | if (resolved != nullptr) { |
| 4983 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer.Get()); |
| 4984 | } else { |
| 4985 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 4986 | } |
| 4987 | } |
| 4988 | break; |
| 4989 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 4990 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4991 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4992 | Thread::Current()->AssertPendingException(); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 4993 | return nullptr; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4994 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4995 | } |
| 4996 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4997 | mirror::ArtField* ClassLinker::ResolveField(const DexFile& dex_file, uint32_t field_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4998 | Handle<mirror::DexCache> dex_cache, |
| 4999 | Handle<mirror::ClassLoader> class_loader, |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 5000 | bool is_static) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5001 | DCHECK(dex_cache.Get() != nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5002 | mirror::ArtField* resolved = dex_cache->GetResolvedField(field_idx); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5003 | if (resolved != nullptr) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5004 | return resolved; |
| 5005 | } |
| 5006 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5007 | Thread* const self = Thread::Current(); |
| 5008 | StackHandleScope<1> hs(self); |
| 5009 | Handle<mirror::Class> klass( |
| 5010 | 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] | 5011 | if (klass.Get() == nullptr) { |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 5012 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5013 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5014 | } |
| 5015 | |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 5016 | if (is_static) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5017 | resolved = mirror::Class::FindStaticField(self, klass, dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 5018 | } else { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5019 | resolved = klass->FindInstanceField(dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 5020 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5021 | |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5022 | if (resolved == nullptr) { |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5023 | const char* name = dex_file.GetFieldName(field_id); |
| 5024 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 5025 | if (is_static) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5026 | resolved = mirror::Class::FindStaticField(self, klass, name, type); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5027 | } else { |
| 5028 | resolved = klass->FindInstanceField(name, type); |
| 5029 | } |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5030 | if (resolved == nullptr) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5031 | ThrowNoSuchFieldError(is_static ? "static " : "instance ", klass.Get(), type, name); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5032 | return nullptr; |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5033 | } |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5034 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5035 | dex_cache->SetResolvedField(field_idx, resolved); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5036 | return resolved; |
| 5037 | } |
| 5038 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5039 | mirror::ArtField* ClassLinker::ResolveFieldJLS(const DexFile& dex_file, |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5040 | uint32_t field_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5041 | Handle<mirror::DexCache> dex_cache, |
| 5042 | Handle<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5043 | DCHECK(dex_cache.Get() != nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5044 | mirror::ArtField* resolved = dex_cache->GetResolvedField(field_idx); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5045 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5046 | return resolved; |
| 5047 | } |
| 5048 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5049 | Thread* self = Thread::Current(); |
| 5050 | StackHandleScope<1> hs(self); |
| 5051 | Handle<mirror::Class> klass( |
| 5052 | 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] | 5053 | if (klass.Get() == nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5054 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5055 | return nullptr; |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5056 | } |
| 5057 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 5058 | StringPiece name(dex_file.StringDataByIdx(field_id.name_idx_)); |
| 5059 | StringPiece type(dex_file.StringDataByIdx( |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5060 | dex_file.GetTypeId(field_id.type_idx_).descriptor_idx_)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5061 | resolved = mirror::Class::FindField(self, klass, name, type); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5062 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5063 | dex_cache->SetResolvedField(field_idx, resolved); |
| 5064 | } else { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5065 | ThrowNoSuchFieldError("", klass.Get(), type, name); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5066 | } |
| 5067 | return resolved; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 5068 | } |
| 5069 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5070 | const char* ClassLinker::MethodShorty(uint32_t method_idx, mirror::ArtMethod* referrer, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5071 | uint32_t* length) { |
| 5072 | mirror::Class* declaring_class = referrer->GetDeclaringClass(); |
| 5073 | mirror::DexCache* dex_cache = declaring_class->GetDexCache(); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 5074 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 5075 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 5076 | return dex_file.GetMethodShorty(method_id, length); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 5077 | } |
| 5078 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5079 | void ClassLinker::DumpAllClasses(int flags) { |
| 5080 | if (dex_cache_image_class_lookup_required_) { |
| 5081 | MoveImageClassesToClassTable(); |
| 5082 | } |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 5083 | // TODO: at the time this was written, it wasn't safe to call PrettyField with the ClassLinker |
| 5084 | // lock held, because it might need to resolve a field's type, which would try to take the lock. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5085 | std::vector<mirror::Class*> all_classes; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 5086 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 5087 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 5088 | for (GcRoot<mirror::Class>& it : class_table_) { |
| 5089 | all_classes.push_back(it.Read()); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 5090 | } |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 5091 | } |
| 5092 | |
| 5093 | for (size_t i = 0; i < all_classes.size(); ++i) { |
| 5094 | all_classes[i]->DumpClass(std::cerr, flags); |
| 5095 | } |
| 5096 | } |
| 5097 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 5098 | static OatFile::OatMethod CreateOatMethod(const void* code) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5099 | CHECK(code != nullptr); |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 5100 | const uint8_t* base = reinterpret_cast<const uint8_t*>(code); // Base of data points at code. |
| 5101 | base -= sizeof(void*); // Move backward so that code_offset != 0. |
| 5102 | const uint32_t code_offset = sizeof(void*); |
| 5103 | return OatFile::OatMethod(base, code_offset); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5104 | } |
| 5105 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5106 | bool ClassLinker::IsQuickResolutionStub(const void* entry_point) const { |
| 5107 | return (entry_point == GetQuickResolutionStub()) || |
| 5108 | (quick_resolution_trampoline_ == entry_point); |
| 5109 | } |
| 5110 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5111 | bool ClassLinker::IsQuickToInterpreterBridge(const void* entry_point) const { |
| 5112 | return (entry_point == GetQuickToInterpreterBridge()) || |
| 5113 | (quick_to_interpreter_bridge_trampoline_ == entry_point); |
| 5114 | } |
| 5115 | |
| 5116 | bool ClassLinker::IsQuickGenericJniStub(const void* entry_point) const { |
| 5117 | return (entry_point == GetQuickGenericJniStub()) || |
| 5118 | (quick_generic_jni_trampoline_ == entry_point); |
| 5119 | } |
| 5120 | |
| 5121 | const void* ClassLinker::GetRuntimeQuickGenericJniStub() const { |
| 5122 | return GetQuickGenericJniStub(); |
| 5123 | } |
| 5124 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 5125 | void ClassLinker::SetEntryPointsToCompiledCode(mirror::ArtMethod* method, |
| 5126 | const void* method_code) const { |
| 5127 | OatFile::OatMethod oat_method = CreateOatMethod(method_code); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5128 | oat_method.LinkMethod(method); |
| 5129 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5130 | } |
| 5131 | |
| 5132 | void ClassLinker::SetEntryPointsToInterpreter(mirror::ArtMethod* method) const { |
| 5133 | if (!method->IsNative()) { |
| 5134 | method->SetEntryPointFromInterpreter(artInterpreterToInterpreterBridge); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5135 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 5136 | } else { |
| 5137 | const void* quick_method_code = GetQuickGenericJniStub(); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 5138 | OatFile::OatMethod oat_method = CreateOatMethod(quick_method_code); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5139 | oat_method.LinkMethod(method); |
| 5140 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5141 | } |
| 5142 | } |
| 5143 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5144 | void ClassLinker::DumpForSigQuit(std::ostream& os) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5145 | Thread* self = Thread::Current(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5146 | if (dex_cache_image_class_lookup_required_) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5147 | ScopedObjectAccess soa(self); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5148 | MoveImageClassesToClassTable(); |
| 5149 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5150 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 5151 | os << "Zygote loaded classes=" << pre_zygote_class_table_.Size() << " post zygote classes=" |
| 5152 | << class_table_.Size() << "\n"; |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 5153 | } |
| 5154 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5155 | size_t ClassLinker::NumLoadedClasses() { |
| 5156 | if (dex_cache_image_class_lookup_required_) { |
| 5157 | MoveImageClassesToClassTable(); |
| 5158 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 5159 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 5160 | // Only return non zygote classes since these are the ones which apps which care about. |
| 5161 | return class_table_.Size(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 5162 | } |
| 5163 | |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 5164 | pid_t ClassLinker::GetClassesLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 5165 | return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 5166 | } |
| 5167 | |
| 5168 | pid_t ClassLinker::GetDexLockOwner() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 5169 | return dex_lock_.GetExclusiveOwnerTid(); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 5170 | } |
| 5171 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5172 | void ClassLinker::SetClassRoot(ClassRoot class_root, mirror::Class* klass) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5173 | DCHECK(!init_done_); |
| 5174 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5175 | DCHECK(klass != nullptr); |
| 5176 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5177 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 5178 | mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5179 | DCHECK(class_roots != nullptr); |
| 5180 | DCHECK(class_roots->Get(class_root) == nullptr); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 5181 | class_roots->Set<false>(class_root, klass); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5182 | } |
| 5183 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5184 | const char* ClassLinker::GetClassRootDescriptor(ClassRoot class_root) { |
| 5185 | static const char* class_roots_descriptors[] = { |
| 5186 | "Ljava/lang/Class;", |
| 5187 | "Ljava/lang/Object;", |
| 5188 | "[Ljava/lang/Class;", |
| 5189 | "[Ljava/lang/Object;", |
| 5190 | "Ljava/lang/String;", |
| 5191 | "Ljava/lang/DexCache;", |
| 5192 | "Ljava/lang/ref/Reference;", |
| 5193 | "Ljava/lang/reflect/ArtField;", |
| 5194 | "Ljava/lang/reflect/ArtMethod;", |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 5195 | "Ljava/lang/reflect/Field;", |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5196 | "Ljava/lang/reflect/Proxy;", |
| 5197 | "[Ljava/lang/String;", |
| 5198 | "[Ljava/lang/reflect/ArtField;", |
| 5199 | "[Ljava/lang/reflect/ArtMethod;", |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 5200 | "[Ljava/lang/reflect/Field;", |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5201 | "Ljava/lang/ClassLoader;", |
| 5202 | "Ljava/lang/Throwable;", |
| 5203 | "Ljava/lang/ClassNotFoundException;", |
| 5204 | "Ljava/lang/StackTraceElement;", |
| 5205 | "Z", |
| 5206 | "B", |
| 5207 | "C", |
| 5208 | "D", |
| 5209 | "F", |
| 5210 | "I", |
| 5211 | "J", |
| 5212 | "S", |
| 5213 | "V", |
| 5214 | "[Z", |
| 5215 | "[B", |
| 5216 | "[C", |
| 5217 | "[D", |
| 5218 | "[F", |
| 5219 | "[I", |
| 5220 | "[J", |
| 5221 | "[S", |
| 5222 | "[Ljava/lang/StackTraceElement;", |
| 5223 | }; |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 5224 | static_assert(arraysize(class_roots_descriptors) == size_t(kClassRootsMax), |
| 5225 | "Mismatch between class descriptors and class-root enum"); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5226 | |
| 5227 | const char* descriptor = class_roots_descriptors[class_root]; |
| 5228 | CHECK(descriptor != nullptr); |
| 5229 | return descriptor; |
| 5230 | } |
| 5231 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 5232 | std::size_t ClassLinker::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& root) |
| 5233 | const { |
| 5234 | std::string temp; |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 5235 | return ComputeModifiedUtf8Hash(root.Read()->GetDescriptor(&temp)); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 5236 | } |
| 5237 | |
| 5238 | bool ClassLinker::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a, |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 5239 | const GcRoot<mirror::Class>& b) const { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 5240 | if (a.Read()->GetClassLoader() != b.Read()->GetClassLoader()) { |
| 5241 | return false; |
| 5242 | } |
| 5243 | std::string temp; |
| 5244 | return a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp)); |
| 5245 | } |
| 5246 | |
| 5247 | std::size_t ClassLinker::ClassDescriptorHashEquals::operator()( |
| 5248 | const std::pair<const char*, mirror::ClassLoader*>& element) const { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 5249 | return ComputeModifiedUtf8Hash(element.first); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 5250 | } |
| 5251 | |
| 5252 | bool ClassLinker::ClassDescriptorHashEquals::operator()( |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 5253 | const GcRoot<mirror::Class>& a, const std::pair<const char*, mirror::ClassLoader*>& b) const { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 5254 | if (a.Read()->GetClassLoader() != b.second) { |
| 5255 | return false; |
| 5256 | } |
| 5257 | return a.Read()->DescriptorEquals(b.first); |
| 5258 | } |
| 5259 | |
| 5260 | bool ClassLinker::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a, |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 5261 | const char* descriptor) const { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 5262 | return a.Read()->DescriptorEquals(descriptor); |
| 5263 | } |
| 5264 | |
| 5265 | std::size_t ClassLinker::ClassDescriptorHashEquals::operator()(const char* descriptor) const { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 5266 | return ComputeModifiedUtf8Hash(descriptor); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 5267 | } |
| 5268 | |
Sebastien Hertz | 6963e44 | 2014-11-26 22:11:27 +0100 | [diff] [blame] | 5269 | bool ClassLinker::MayBeCalledWithDirectCodePointer(mirror::ArtMethod* m) { |
Mathieu Chartier | d856545 | 2015-03-26 09:41:50 -0700 | [diff] [blame] | 5270 | if (Runtime::Current()->UseJit()) { |
| 5271 | // JIT can have direct code pointers from any method to any other method. |
| 5272 | return true; |
| 5273 | } |
Sebastien Hertz | 6963e44 | 2014-11-26 22:11:27 +0100 | [diff] [blame] | 5274 | // Non-image methods don't use direct code pointer. |
| 5275 | if (!m->GetDeclaringClass()->IsBootStrapClassLoaded()) { |
| 5276 | return false; |
| 5277 | } |
| 5278 | if (m->IsPrivate()) { |
| 5279 | // The method can only be called inside its own oat file. Therefore it won't be called using |
| 5280 | // 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] | 5281 | const DexFile& dex_file = m->GetDeclaringClass()->GetDexFile(); |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 5282 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
Sebastien Hertz | 6963e44 | 2014-11-26 22:11:27 +0100 | [diff] [blame] | 5283 | if (oat_dex_file == nullptr) { |
| 5284 | // No oat file: the method has not been compiled. |
| 5285 | return false; |
| 5286 | } |
| 5287 | const OatFile* oat_file = oat_dex_file->GetOatFile(); |
| 5288 | return oat_file != nullptr && !oat_file->IsPic(); |
| 5289 | } else { |
| 5290 | // The method can be called outside its own oat file. Therefore it won't be called using its |
| 5291 | // direct code pointer only if all loaded oat files have been compiled in PIC mode. |
| 5292 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
| 5293 | for (const OatFile* oat_file : oat_files_) { |
| 5294 | if (!oat_file->IsPic()) { |
| 5295 | return true; |
| 5296 | } |
| 5297 | } |
| 5298 | return false; |
| 5299 | } |
| 5300 | } |
| 5301 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 5302 | jobject ClassLinker::CreatePathClassLoader(Thread* self, std::vector<const DexFile*>& dex_files) { |
| 5303 | // SOAAlreadyRunnable is protected, and we need something to add a global reference. |
| 5304 | // We could move the jobject to the callers, but all call-sites do this... |
| 5305 | ScopedObjectAccessUnchecked soa(self); |
| 5306 | |
| 5307 | // Register the dex files. |
| 5308 | for (const DexFile* dex_file : dex_files) { |
| 5309 | RegisterDexFile(*dex_file); |
| 5310 | } |
| 5311 | |
| 5312 | // For now, create a libcore-level DexFile for each ART DexFile. This "explodes" multidex. |
| 5313 | StackHandleScope<11> hs(self); |
| 5314 | |
| 5315 | Handle<mirror::ArtField> h_dex_elements_field = |
| 5316 | hs.NewHandle(soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList_dexElements)); |
| 5317 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 5318 | mirror::Class* dex_elements_class = h_dex_elements_field->GetType<true>(); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 5319 | DCHECK(dex_elements_class != nullptr); |
| 5320 | DCHECK(dex_elements_class->IsArrayClass()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 5321 | Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements(hs.NewHandle( |
| 5322 | mirror::ObjectArray<mirror::Object>::Alloc(self, dex_elements_class, dex_files.size()))); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 5323 | Handle<mirror::Class> h_dex_element_class = |
| 5324 | hs.NewHandle(dex_elements_class->GetComponentType()); |
| 5325 | |
| 5326 | Handle<mirror::ArtField> h_element_file_field = |
| 5327 | hs.NewHandle( |
| 5328 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile)); |
| 5329 | DCHECK_EQ(h_dex_element_class.Get(), h_element_file_field->GetDeclaringClass()); |
| 5330 | |
| 5331 | Handle<mirror::ArtField> h_cookie_field = |
| 5332 | hs.NewHandle(soa.DecodeField(WellKnownClasses::dalvik_system_DexFile_cookie)); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 5333 | DCHECK_EQ(h_cookie_field->GetDeclaringClass(), h_element_file_field->GetType<false>()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 5334 | |
| 5335 | // Fill the elements array. |
| 5336 | int32_t index = 0; |
| 5337 | for (const DexFile* dex_file : dex_files) { |
| 5338 | StackHandleScope<3> hs2(self); |
| 5339 | |
| 5340 | Handle<mirror::LongArray> h_long_array = hs2.NewHandle(mirror::LongArray::Alloc(self, 1)); |
| 5341 | DCHECK(h_long_array.Get() != nullptr); |
| 5342 | h_long_array->Set(0, reinterpret_cast<intptr_t>(dex_file)); |
| 5343 | |
| 5344 | Handle<mirror::Object> h_dex_file = hs2.NewHandle( |
| 5345 | h_cookie_field->GetDeclaringClass()->AllocObject(self)); |
| 5346 | DCHECK(h_dex_file.Get() != nullptr); |
| 5347 | h_cookie_field->SetObject<false>(h_dex_file.Get(), h_long_array.Get()); |
| 5348 | |
| 5349 | Handle<mirror::Object> h_element = hs2.NewHandle(h_dex_element_class->AllocObject(self)); |
| 5350 | DCHECK(h_element.Get() != nullptr); |
| 5351 | h_element_file_field->SetObject<false>(h_element.Get(), h_dex_file.Get()); |
| 5352 | |
| 5353 | h_dex_elements->Set(index, h_element.Get()); |
| 5354 | index++; |
| 5355 | } |
| 5356 | DCHECK_EQ(index, h_dex_elements->GetLength()); |
| 5357 | |
| 5358 | // Create DexPathList. |
| 5359 | Handle<mirror::Object> h_dex_path_list = hs.NewHandle( |
| 5360 | h_dex_elements_field->GetDeclaringClass()->AllocObject(self)); |
| 5361 | DCHECK(h_dex_path_list.Get() != nullptr); |
| 5362 | // Set elements. |
| 5363 | h_dex_elements_field->SetObject<false>(h_dex_path_list.Get(), h_dex_elements.Get()); |
| 5364 | |
| 5365 | // Create PathClassLoader. |
| 5366 | Handle<mirror::Class> h_path_class_class = hs.NewHandle( |
| 5367 | soa.Decode<mirror::Class*>(WellKnownClasses::dalvik_system_PathClassLoader)); |
| 5368 | Handle<mirror::Object> h_path_class_loader = hs.NewHandle( |
| 5369 | h_path_class_class->AllocObject(self)); |
| 5370 | DCHECK(h_path_class_loader.Get() != nullptr); |
| 5371 | // Set DexPathList. |
| 5372 | Handle<mirror::ArtField> h_path_list_field = hs.NewHandle( |
| 5373 | soa.DecodeField(WellKnownClasses::dalvik_system_PathClassLoader_pathList)); |
| 5374 | DCHECK(h_path_list_field.Get() != nullptr); |
| 5375 | h_path_list_field->SetObject<false>(h_path_class_loader.Get(), h_dex_path_list.Get()); |
| 5376 | |
| 5377 | // Make a pretend boot-classpath. |
| 5378 | // TODO: Should we scan the image? |
| 5379 | Handle<mirror::ArtField> h_parent_field = hs.NewHandle( |
| 5380 | mirror::Class::FindField(self, hs.NewHandle(h_path_class_loader->GetClass()), "parent", |
| 5381 | "Ljava/lang/ClassLoader;")); |
| 5382 | DCHECK(h_parent_field.Get() != nullptr); |
| 5383 | mirror::Object* boot_cl = |
| 5384 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_BootClassLoader)->AllocObject(self); |
| 5385 | h_parent_field->SetObject<false>(h_path_class_loader.Get(), boot_cl); |
| 5386 | |
| 5387 | // Make it a global ref and return. |
| 5388 | ScopedLocalRef<jobject> local_ref( |
| 5389 | soa.Env(), soa.Env()->AddLocalReference<jobject>(h_path_class_loader.Get())); |
| 5390 | return soa.Env()->NewGlobalRef(local_ref.get()); |
| 5391 | } |
| 5392 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5393 | } // namespace art |