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> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 24 | #include <utility> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 25 | #include <vector> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 26 | |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 27 | #include "base/casts.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 28 | #include "base/logging.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 29 | #include "base/scoped_flock.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 30 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 31 | #include "base/unix_file/fd_file.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | #include "class_linker-inl.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 33 | #include "compiler_callbacks.h" |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 34 | #include "debugger.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 35 | #include "dex_file-inl.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 36 | #include "entrypoints/runtime_asm_entrypoints.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 37 | #include "gc_root-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 38 | #include "gc/accounting/card_table-inl.h" |
| 39 | #include "gc/accounting/heap_bitmap.h" |
| 40 | #include "gc/heap.h" |
| 41 | #include "gc/space/image_space.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 42 | #include "handle_scope.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 43 | #include "intern_table.h" |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 44 | #include "interpreter/interpreter.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 45 | #include "leb128.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 46 | #include "oat.h" |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 47 | #include "oat_file.h" |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 48 | #include "object_lock.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 49 | #include "mirror/art_field-inl.h" |
| 50 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 51 | #include "mirror/class.h" |
| 52 | #include "mirror/class-inl.h" |
| 53 | #include "mirror/class_loader.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 54 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 55 | #include "mirror/iftable-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 56 | #include "mirror/object-inl.h" |
| 57 | #include "mirror/object_array-inl.h" |
| 58 | #include "mirror/proxy.h" |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 59 | #include "mirror/reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 60 | #include "mirror/stack_trace_element.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 61 | #include "mirror/string-inl.h" |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 62 | #include "os.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 63 | #include "runtime.h" |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 64 | #include "entrypoints/entrypoint_utils.h" |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 65 | #include "ScopedLocalRef.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 66 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 67 | #include "handle_scope-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 68 | #include "thread-inl.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 69 | #include "utils.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 70 | #include "verifier/method_verifier.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 71 | #include "well_known_classes.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 72 | |
| 73 | namespace art { |
| 74 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 75 | static void ThrowNoClassDefFoundError(const char* fmt, ...) |
| 76 | __attribute__((__format__(__printf__, 1, 2))) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 77 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 78 | static void ThrowNoClassDefFoundError(const char* fmt, ...) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 79 | va_list args; |
| 80 | va_start(args, fmt); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 81 | Thread* self = Thread::Current(); |
| 82 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 83 | self->ThrowNewExceptionV(throw_location, "Ljava/lang/NoClassDefFoundError;", fmt, args); |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 84 | va_end(args); |
| 85 | } |
| 86 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 87 | static void ThrowEarlierClassFailure(mirror::Class* c) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 88 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 89 | // The class failed to initialize on a previous attempt, so we want to throw |
| 90 | // a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we |
| 91 | // failed in verification, in which case v2 5.4.1 says we need to re-throw |
| 92 | // the previous error. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 93 | Runtime* runtime = Runtime::Current(); |
| 94 | bool is_compiler = runtime->IsCompiler(); |
| 95 | if (!is_compiler) { // Give info if this occurs at runtime. |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 96 | LOG(INFO) << "Rejecting re-init on previously-failed class " << PrettyClass(c); |
| 97 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 98 | |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 99 | CHECK(c->IsErroneous()) << PrettyClass(c) << " " << c->GetStatus(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 100 | Thread* self = Thread::Current(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 101 | if (is_compiler) { |
| 102 | // At compile time, accurate errors and NCDFE are disabled to speed compilation. |
| 103 | mirror::Throwable* pre_allocated = runtime->GetPreAllocatedNoClassDefFoundError(); |
| 104 | self->SetException(ThrowLocation(), pre_allocated); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 105 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 106 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 107 | if (c->GetVerifyErrorClass() != NULL) { |
| 108 | // TODO: change the verifier to store an _instance_, with a useful detail message? |
| 109 | std::string temp; |
| 110 | self->ThrowNewException(throw_location, c->GetVerifyErrorClass()->GetDescriptor(&temp), |
| 111 | PrettyDescriptor(c).c_str()); |
| 112 | } else { |
| 113 | self->ThrowNewException(throw_location, "Ljava/lang/NoClassDefFoundError;", |
| 114 | PrettyDescriptor(c).c_str()); |
| 115 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 119 | static void VlogClassInitializationFailure(Handle<mirror::Class> klass) |
| 120 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 121 | if (VLOG_IS_ON(class_linker)) { |
| 122 | std::string temp; |
| 123 | LOG(INFO) << "Failed to initialize class " << klass->GetDescriptor(&temp) << " from " |
| 124 | << klass->GetLocation() << "\n" << Thread::Current()->GetException(nullptr)->Dump(); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | static void WrapExceptionInInitializer(Handle<mirror::Class> klass) |
| 129 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 130 | Thread* self = Thread::Current(); |
| 131 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 132 | |
| 133 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 134 | CHECK(cause.get() != nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 135 | |
| 136 | env->ExceptionClear(); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 137 | bool is_error = env->IsInstanceOf(cause.get(), WellKnownClasses::java_lang_Error); |
| 138 | env->Throw(cause.get()); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 139 | |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 140 | // We only wrap non-Error exceptions; an Error can just be used as-is. |
| 141 | if (!is_error) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 142 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 143 | self->ThrowNewWrappedException(throw_location, "Ljava/lang/ExceptionInInitializerError;", |
| 144 | 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 { |
| 159 | // Sort by gap size, largest first. |
| 160 | return lhs.size > rhs.size; |
| 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 | portable_resolution_trampoline_(nullptr), |
| 241 | quick_resolution_trampoline_(nullptr), |
| 242 | portable_imt_conflict_trampoline_(nullptr), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 243 | quick_imt_conflict_trampoline_(nullptr), |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 244 | quick_generic_jni_trampoline_(nullptr), |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 245 | quick_to_interpreter_bridge_trampoline_(nullptr), |
| 246 | image_pointer_size_(sizeof(void*)) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 247 | memset(find_array_class_cache_, 0, kFindArrayCacheSize * sizeof(mirror::Class*)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 248 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 249 | |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 250 | void ClassLinker::InitWithoutImage(const std::vector<const DexFile*>& boot_class_path) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 251 | VLOG(startup) << "ClassLinker::Init"; |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 252 | CHECK(!Runtime::Current()->GetHeap()->HasImageSpace()) << "Runtime has image. We should use it."; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 253 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 254 | CHECK(!init_done_); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 255 | |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 256 | // java_lang_Class comes first, it's needed for AllocClass |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 257 | Thread* self = Thread::Current(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 258 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 259 | // 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] | 260 | heap->IncrementDisableMovingGC(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 261 | StackHandleScope<64> hs(self); // 64 is picked arbitrarily. |
| 262 | Handle<mirror::Class> java_lang_Class(hs.NewHandle(down_cast<mirror::Class*>( |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 263 | heap->AllocNonMovableObject<true>(self, nullptr, |
| 264 | mirror::Class::ClassClassSize(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 265 | VoidFunctor())))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 266 | CHECK(java_lang_Class.Get() != nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 267 | mirror::Class::SetClassClass(java_lang_Class.Get()); |
| 268 | java_lang_Class->SetClass(java_lang_Class.Get()); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 269 | if (kUseBakerOrBrooksReadBarrier) { |
| 270 | java_lang_Class->AssertReadBarrierPointer(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 271 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 272 | java_lang_Class->SetClassSize(mirror::Class::ClassClassSize()); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 273 | java_lang_Class->SetPrimitiveType(Primitive::kPrimNot); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 274 | heap->DecrementDisableMovingGC(self); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 275 | // AllocClass(mirror::Class*) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 276 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 277 | // Class[] is used for reflection support. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 278 | Handle<mirror::Class> class_array_class(hs.NewHandle( |
| 279 | AllocClass(self, java_lang_Class.Get(), mirror::ObjectArray<mirror::Class>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 280 | class_array_class->SetComponentType(java_lang_Class.Get()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 281 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 282 | // 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] | 283 | Handle<mirror::Class> java_lang_Object(hs.NewHandle( |
| 284 | AllocClass(self, java_lang_Class.Get(), mirror::Object::ClassSize()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 285 | CHECK(java_lang_Object.Get() != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 286 | // backfill Object as the super class of Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 287 | java_lang_Class->SetSuperClass(java_lang_Object.Get()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 288 | java_lang_Object->SetStatus(mirror::Class::kStatusLoaded, self); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 289 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 290 | // Object[] next to hold class roots. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 291 | Handle<mirror::Class> object_array_class(hs.NewHandle( |
| 292 | AllocClass(self, java_lang_Class.Get(), mirror::ObjectArray<mirror::Object>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 293 | object_array_class->SetComponentType(java_lang_Object.Get()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 294 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 295 | // Setup the char (primitive) class to be used for char[]. |
| 296 | Handle<mirror::Class> char_class(hs.NewHandle( |
| 297 | AllocClass(self, java_lang_Class.Get(), mirror::Class::PrimitiveClassSize()))); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 298 | // The primitive char class won't be initialized by |
| 299 | // InitializePrimitiveClass until line 459, but strings (and |
| 300 | // internal char arrays) will be allocated before that and the |
| 301 | // component size, which is computed from the primitive type, needs |
| 302 | // to be set here. |
| 303 | char_class->SetPrimitiveType(Primitive::kPrimChar); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 304 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 305 | // Setup the char[] class to be used for String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 306 | Handle<mirror::Class> char_array_class(hs.NewHandle( |
| 307 | AllocClass(self, java_lang_Class.Get(), |
| 308 | mirror::Array::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 309 | char_array_class->SetComponentType(char_class.Get()); |
| 310 | mirror::CharArray::SetArrayClass(char_array_class.Get()); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 311 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 312 | // Setup String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 313 | Handle<mirror::Class> java_lang_String(hs.NewHandle( |
| 314 | AllocClass(self, java_lang_Class.Get(), mirror::String::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 315 | mirror::String::SetClass(java_lang_String.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 316 | java_lang_String->SetObjectSize(mirror::String::InstanceSize()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 317 | java_lang_String->SetStatus(mirror::Class::kStatusResolved, self); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 318 | |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 319 | // Setup Reference. |
| 320 | Handle<mirror::Class> java_lang_ref_Reference(hs.NewHandle( |
| 321 | AllocClass(self, java_lang_Class.Get(), mirror::Reference::ClassSize()))); |
| 322 | mirror::Reference::SetClass(java_lang_ref_Reference.Get()); |
| 323 | java_lang_ref_Reference->SetObjectSize(mirror::Reference::InstanceSize()); |
| 324 | java_lang_ref_Reference->SetStatus(mirror::Class::kStatusResolved, self); |
| 325 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 326 | // Create storage for root classes, save away our work so far (requires descriptors). |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 327 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class> >( |
| 328 | mirror::ObjectArray<mirror::Class>::Alloc(self, object_array_class.Get(), |
| 329 | kClassRootsMax)); |
| 330 | CHECK(!class_roots_.IsNull()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 331 | SetClassRoot(kJavaLangClass, java_lang_Class.Get()); |
| 332 | SetClassRoot(kJavaLangObject, java_lang_Object.Get()); |
| 333 | SetClassRoot(kClassArrayClass, class_array_class.Get()); |
| 334 | SetClassRoot(kObjectArrayClass, object_array_class.Get()); |
| 335 | SetClassRoot(kCharArrayClass, char_array_class.Get()); |
| 336 | SetClassRoot(kJavaLangString, java_lang_String.Get()); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 337 | SetClassRoot(kJavaLangRefReference, java_lang_ref_Reference.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 338 | |
| 339 | // Setup the primitive type classes. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 340 | SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass(self, Primitive::kPrimBoolean)); |
| 341 | SetClassRoot(kPrimitiveByte, CreatePrimitiveClass(self, Primitive::kPrimByte)); |
| 342 | SetClassRoot(kPrimitiveShort, CreatePrimitiveClass(self, Primitive::kPrimShort)); |
| 343 | SetClassRoot(kPrimitiveInt, CreatePrimitiveClass(self, Primitive::kPrimInt)); |
| 344 | SetClassRoot(kPrimitiveLong, CreatePrimitiveClass(self, Primitive::kPrimLong)); |
| 345 | SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass(self, Primitive::kPrimFloat)); |
| 346 | SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass(self, Primitive::kPrimDouble)); |
| 347 | SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass(self, Primitive::kPrimVoid)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 348 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 349 | // Create array interface entries to populate once we can load system classes. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 350 | array_iftable_ = GcRoot<mirror::IfTable>(AllocIfTable(self, 2)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 351 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 352 | // Create int array type for AllocDexCache (done in AppendToBootClassPath). |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 353 | Handle<mirror::Class> int_array_class(hs.NewHandle( |
| 354 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize()))); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 355 | int_array_class->SetComponentType(GetClassRoot(kPrimitiveInt)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 356 | mirror::IntArray::SetArrayClass(int_array_class.Get()); |
| 357 | SetClassRoot(kIntArrayClass, int_array_class.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 358 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 359 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 360 | |
Ian Rogers | 52813c9 | 2012-10-11 11:50:38 -0700 | [diff] [blame] | 361 | // Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 362 | Handle<mirror::Class> java_lang_DexCache(hs.NewHandle( |
| 363 | AllocClass(self, java_lang_Class.Get(), mirror::DexCache::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 364 | SetClassRoot(kJavaLangDexCache, java_lang_DexCache.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 365 | java_lang_DexCache->SetObjectSize(mirror::DexCache::InstanceSize()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 366 | java_lang_DexCache->SetStatus(mirror::Class::kStatusResolved, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 367 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 368 | // Constructor, Field, Method, and AbstractMethod are necessary so |
| 369 | // that FindClass can link members. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 370 | Handle<mirror::Class> java_lang_reflect_ArtField(hs.NewHandle( |
| 371 | AllocClass(self, java_lang_Class.Get(), mirror::ArtField::ClassSize()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 372 | CHECK(java_lang_reflect_ArtField.Get() != nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 373 | java_lang_reflect_ArtField->SetObjectSize(mirror::ArtField::InstanceSize()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 374 | SetClassRoot(kJavaLangReflectArtField, java_lang_reflect_ArtField.Get()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 375 | java_lang_reflect_ArtField->SetStatus(mirror::Class::kStatusResolved, self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 376 | mirror::ArtField::SetClass(java_lang_reflect_ArtField.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 377 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 378 | Handle<mirror::Class> java_lang_reflect_ArtMethod(hs.NewHandle( |
| 379 | AllocClass(self, java_lang_Class.Get(), mirror::ArtMethod::ClassSize()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 380 | CHECK(java_lang_reflect_ArtMethod.Get() != nullptr); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 381 | java_lang_reflect_ArtMethod->SetObjectSize(mirror::ArtMethod::InstanceSize(sizeof(void*))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 382 | SetClassRoot(kJavaLangReflectArtMethod, java_lang_reflect_ArtMethod.Get()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 383 | java_lang_reflect_ArtMethod->SetStatus(mirror::Class::kStatusResolved, self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 384 | mirror::ArtMethod::SetClass(java_lang_reflect_ArtMethod.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 385 | |
| 386 | // Set up array classes for string, field, method |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 387 | Handle<mirror::Class> object_array_string(hs.NewHandle( |
| 388 | AllocClass(self, java_lang_Class.Get(), |
| 389 | mirror::ObjectArray<mirror::String>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 390 | object_array_string->SetComponentType(java_lang_String.Get()); |
| 391 | SetClassRoot(kJavaLangStringArrayClass, object_array_string.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 392 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 393 | Handle<mirror::Class> object_array_art_method(hs.NewHandle( |
| 394 | AllocClass(self, java_lang_Class.Get(), |
| 395 | mirror::ObjectArray<mirror::ArtMethod>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 396 | object_array_art_method->SetComponentType(java_lang_reflect_ArtMethod.Get()); |
| 397 | SetClassRoot(kJavaLangReflectArtMethodArrayClass, object_array_art_method.Get()); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 398 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 399 | Handle<mirror::Class> object_array_art_field(hs.NewHandle( |
| 400 | AllocClass(self, java_lang_Class.Get(), |
| 401 | mirror::ObjectArray<mirror::ArtField>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 402 | object_array_art_field->SetComponentType(java_lang_reflect_ArtField.Get()); |
| 403 | SetClassRoot(kJavaLangReflectArtFieldArrayClass, object_array_art_field.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 404 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 405 | // Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create |
| 406 | // DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses |
| 407 | // these roots. |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 408 | CHECK_NE(0U, boot_class_path.size()); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 409 | for (const DexFile* dex_file : boot_class_path) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 410 | CHECK(dex_file != nullptr); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 411 | AppendToBootClassPath(self, *dex_file); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 412 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 413 | |
| 414 | // now we can use FindSystemClass |
| 415 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 416 | // run char class through InitializePrimitiveClass to finish init |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 417 | InitializePrimitiveClass(char_class.Get(), Primitive::kPrimChar); |
| 418 | SetClassRoot(kPrimitiveChar, char_class.Get()); // needs descriptor |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 419 | |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 420 | // Create runtime resolution and imt conflict methods. Also setup the default imt. |
| 421 | Runtime* runtime = Runtime::Current(); |
| 422 | runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); |
| 423 | runtime->SetImtConflictMethod(runtime->CreateImtConflictMethod()); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 424 | runtime->SetImtUnimplementedMethod(runtime->CreateImtConflictMethod()); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 425 | runtime->SetDefaultImt(runtime->CreateDefaultImt(this)); |
| 426 | |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 427 | // Set up GenericJNI entrypoint. That is mainly a hack for common_compiler_test.h so that |
| 428 | // we do not need friend classes or a publicly exposed setter. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 429 | quick_generic_jni_trampoline_ = GetQuickGenericJniStub(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 430 | if (!runtime->IsCompiler()) { |
| 431 | // 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] | 432 | quick_resolution_trampoline_ = GetQuickResolutionStub(); |
| 433 | quick_imt_conflict_trampoline_ = GetQuickImtConflictStub(); |
| 434 | quick_to_interpreter_bridge_trampoline_ = GetQuickToInterpreterBridge(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 435 | } |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 436 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 437 | // Object, String and DexCache need to be rerun through FindSystemClass to finish init |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 438 | java_lang_Object->SetStatus(mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 439 | mirror::Class* Object_class = FindSystemClass(self, "Ljava/lang/Object;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 440 | CHECK_EQ(java_lang_Object.Get(), Object_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 441 | CHECK_EQ(java_lang_Object->GetObjectSize(), mirror::Object::InstanceSize()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 442 | java_lang_String->SetStatus(mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 443 | mirror::Class* String_class = FindSystemClass(self, "Ljava/lang/String;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 444 | std::ostringstream os1, os2; |
| 445 | java_lang_String->DumpClass(os1, mirror::Class::kDumpClassFullDetail); |
| 446 | String_class->DumpClass(os2, mirror::Class::kDumpClassFullDetail); |
| 447 | CHECK_EQ(java_lang_String.Get(), String_class) << os1.str() << "\n\n" << os2.str(); |
| 448 | CHECK_EQ(java_lang_String->GetObjectSize(), mirror::String::InstanceSize()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 449 | java_lang_DexCache->SetStatus(mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 450 | mirror::Class* DexCache_class = FindSystemClass(self, "Ljava/lang/DexCache;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 451 | CHECK_EQ(java_lang_String.Get(), String_class); |
| 452 | CHECK_EQ(java_lang_DexCache.Get(), DexCache_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 453 | CHECK_EQ(java_lang_DexCache->GetObjectSize(), mirror::DexCache::InstanceSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 454 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 455 | // 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] | 456 | SetClassRoot(kBooleanArrayClass, FindSystemClass(self, "[Z")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 457 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 458 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 459 | SetClassRoot(kByteArrayClass, FindSystemClass(self, "[B")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 460 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
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 | mirror::Class* found_char_array_class = FindSystemClass(self, "[C"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 463 | CHECK_EQ(char_array_class.Get(), found_char_array_class); |
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 | SetClassRoot(kShortArrayClass, FindSystemClass(self, "[S")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 466 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
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 | mirror::Class* found_int_array_class = FindSystemClass(self, "[I"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 469 | CHECK_EQ(int_array_class.Get(), found_int_array_class); |
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 | SetClassRoot(kLongArrayClass, FindSystemClass(self, "[J")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 472 | mirror::LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
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(kFloatArrayClass, FindSystemClass(self, "[F")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 475 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
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(kDoubleArrayClass, FindSystemClass(self, "[D")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 478 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
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 | mirror::Class* found_class_array_class = FindSystemClass(self, "[Ljava/lang/Class;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 481 | CHECK_EQ(class_array_class.Get(), found_class_array_class); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 482 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 483 | mirror::Class* found_object_array_class = FindSystemClass(self, "[Ljava/lang/Object;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 484 | CHECK_EQ(object_array_class.Get(), found_object_array_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 485 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 486 | // Setup the single, global copy of "iftable". |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 487 | mirror::Class* java_lang_Cloneable = FindSystemClass(self, "Ljava/lang/Cloneable;"); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 488 | CHECK(java_lang_Cloneable != nullptr); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 489 | mirror::Class* java_io_Serializable = FindSystemClass(self, "Ljava/io/Serializable;"); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 490 | CHECK(java_io_Serializable != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 491 | // We assume that Cloneable/Serializable don't have superinterfaces -- normally we'd have to |
| 492 | // crawl up and explicitly list all of the supers as well. |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 493 | { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 494 | mirror::IfTable* array_iftable = array_iftable_.Read(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 495 | array_iftable->SetInterface(0, java_lang_Cloneable); |
| 496 | array_iftable->SetInterface(1, java_io_Serializable); |
| 497 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 498 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 499 | // Sanity check Class[] and Object[]'s interfaces. |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 500 | CHECK_EQ(java_lang_Cloneable, mirror::Class::GetDirectInterface(self, class_array_class, 0)); |
| 501 | CHECK_EQ(java_io_Serializable, mirror::Class::GetDirectInterface(self, class_array_class, 1)); |
| 502 | CHECK_EQ(java_lang_Cloneable, mirror::Class::GetDirectInterface(self, object_array_class, 0)); |
| 503 | 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] | 504 | // Run Class, ArtField, and ArtMethod through FindSystemClass. This initializes their |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 505 | // dex_cache_ fields and register them in class_table_. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 506 | mirror::Class* Class_class = FindSystemClass(self, "Ljava/lang/Class;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 507 | CHECK_EQ(java_lang_Class.Get(), Class_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 508 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 509 | java_lang_reflect_ArtMethod->SetStatus(mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 510 | mirror::Class* Art_method_class = FindSystemClass(self, "Ljava/lang/reflect/ArtMethod;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 511 | CHECK_EQ(java_lang_reflect_ArtMethod.Get(), Art_method_class); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 512 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 513 | java_lang_reflect_ArtField->SetStatus(mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 514 | mirror::Class* Art_field_class = FindSystemClass(self, "Ljava/lang/reflect/ArtField;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 515 | CHECK_EQ(java_lang_reflect_ArtField.Get(), Art_field_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 516 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 517 | mirror::Class* String_array_class = |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 518 | FindSystemClass(self, GetClassRootDescriptor(kJavaLangStringArrayClass)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 519 | CHECK_EQ(object_array_string.Get(), String_array_class); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 520 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 521 | mirror::Class* Art_method_array_class = |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 522 | FindSystemClass(self, GetClassRootDescriptor(kJavaLangReflectArtMethodArrayClass)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 523 | CHECK_EQ(object_array_art_method.Get(), Art_method_array_class); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 524 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 525 | mirror::Class* Art_field_array_class = |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 526 | FindSystemClass(self, GetClassRootDescriptor(kJavaLangReflectArtFieldArrayClass)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 527 | CHECK_EQ(object_array_art_field.Get(), Art_field_array_class); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 528 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 529 | // End of special init trickery, subsequent classes may be loaded via FindSystemClass. |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 530 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 531 | // Create java.lang.reflect.Proxy root. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 532 | mirror::Class* java_lang_reflect_Proxy = FindSystemClass(self, "Ljava/lang/reflect/Proxy;"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 533 | SetClassRoot(kJavaLangReflectProxy, java_lang_reflect_Proxy); |
| 534 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 535 | // 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] | 536 | // finish initializing Reference class |
| 537 | java_lang_ref_Reference->SetStatus(mirror::Class::kStatusNotReady, self); |
| 538 | mirror::Class* Reference_class = FindSystemClass(self, "Ljava/lang/ref/Reference;"); |
| 539 | CHECK_EQ(java_lang_ref_Reference.Get(), Reference_class); |
| 540 | CHECK_EQ(java_lang_ref_Reference->GetObjectSize(), mirror::Reference::InstanceSize()); |
| 541 | CHECK_EQ(java_lang_ref_Reference->GetClassSize(), mirror::Reference::ClassSize()); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 542 | mirror::Class* java_lang_ref_FinalizerReference = |
| 543 | FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 544 | java_lang_ref_FinalizerReference->SetAccessFlags( |
| 545 | java_lang_ref_FinalizerReference->GetAccessFlags() | |
| 546 | kAccClassIsReference | kAccClassIsFinalizerReference); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 547 | mirror::Class* java_lang_ref_PhantomReference = |
| 548 | FindSystemClass(self, "Ljava/lang/ref/PhantomReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 549 | java_lang_ref_PhantomReference->SetAccessFlags( |
| 550 | java_lang_ref_PhantomReference->GetAccessFlags() | |
| 551 | kAccClassIsReference | kAccClassIsPhantomReference); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 552 | mirror::Class* java_lang_ref_SoftReference = |
| 553 | FindSystemClass(self, "Ljava/lang/ref/SoftReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 554 | java_lang_ref_SoftReference->SetAccessFlags( |
| 555 | java_lang_ref_SoftReference->GetAccessFlags() | kAccClassIsReference); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 556 | mirror::Class* java_lang_ref_WeakReference = |
| 557 | FindSystemClass(self, "Ljava/lang/ref/WeakReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 558 | java_lang_ref_WeakReference->SetAccessFlags( |
| 559 | java_lang_ref_WeakReference->GetAccessFlags() | |
| 560 | kAccClassIsReference | kAccClassIsWeakReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 561 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 562 | // Setup the ClassLoader, verifying the object_size_. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 563 | mirror::Class* java_lang_ClassLoader = FindSystemClass(self, "Ljava/lang/ClassLoader;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 564 | CHECK_EQ(java_lang_ClassLoader->GetObjectSize(), mirror::ClassLoader::InstanceSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 565 | SetClassRoot(kJavaLangClassLoader, java_lang_ClassLoader); |
| 566 | |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 567 | // Set up java.lang.Throwable, java.lang.ClassNotFoundException, and |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 568 | // java.lang.StackTraceElement as a convenience. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 569 | SetClassRoot(kJavaLangThrowable, FindSystemClass(self, "Ljava/lang/Throwable;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 570 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 571 | SetClassRoot(kJavaLangClassNotFoundException, |
| 572 | FindSystemClass(self, "Ljava/lang/ClassNotFoundException;")); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 573 | SetClassRoot(kJavaLangStackTraceElement, FindSystemClass(self, "Ljava/lang/StackTraceElement;")); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 574 | SetClassRoot(kJavaLangStackTraceElementArrayClass, |
| 575 | FindSystemClass(self, "[Ljava/lang/StackTraceElement;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 576 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 577 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 578 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 579 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 580 | VLOG(startup) << "ClassLinker::InitFromCompiler exiting"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 581 | } |
| 582 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 583 | void ClassLinker::FinishInit(Thread* self) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 584 | VLOG(startup) << "ClassLinker::FinishInit entering"; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 585 | |
| 586 | // Let the heap know some key offsets into java.lang.ref instances |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 587 | // Note: we hard code the field indexes here rather than using FindInstanceField |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 588 | // as the types of the field can't be resolved prior to the runtime being |
| 589 | // fully initialized |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 590 | mirror::Class* java_lang_ref_Reference = GetClassRoot(kJavaLangRefReference); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 591 | mirror::Class* java_lang_ref_FinalizerReference = |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 592 | FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 593 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 594 | mirror::ArtField* pendingNext = java_lang_ref_Reference->GetInstanceField(0); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 595 | CHECK_STREQ(pendingNext->GetName(), "pendingNext"); |
| 596 | CHECK_STREQ(pendingNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 597 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 598 | mirror::ArtField* queue = java_lang_ref_Reference->GetInstanceField(1); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 599 | CHECK_STREQ(queue->GetName(), "queue"); |
| 600 | CHECK_STREQ(queue->GetTypeDescriptor(), "Ljava/lang/ref/ReferenceQueue;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 601 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 602 | mirror::ArtField* queueNext = java_lang_ref_Reference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 603 | CHECK_STREQ(queueNext->GetName(), "queueNext"); |
| 604 | CHECK_STREQ(queueNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 605 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 606 | mirror::ArtField* referent = java_lang_ref_Reference->GetInstanceField(3); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 607 | CHECK_STREQ(referent->GetName(), "referent"); |
| 608 | CHECK_STREQ(referent->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 609 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 610 | mirror::ArtField* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 611 | CHECK_STREQ(zombie->GetName(), "zombie"); |
| 612 | CHECK_STREQ(zombie->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 613 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 614 | // ensure all class_roots_ are initialized |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 615 | for (size_t i = 0; i < kClassRootsMax; i++) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 616 | ClassRoot class_root = static_cast<ClassRoot>(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 617 | mirror::Class* klass = GetClassRoot(class_root); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 618 | CHECK(klass != nullptr); |
| 619 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != nullptr); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 620 | // note SetClassRoot does additional validation. |
| 621 | // if possible add new checks there to catch errors early |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 624 | CHECK(!array_iftable_.IsNull()); |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 625 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 626 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 627 | // that Object, Class, and Object[] are setup |
| 628 | init_done_ = true; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 629 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 630 | VLOG(startup) << "ClassLinker::FinishInit exiting"; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 633 | void ClassLinker::RunRootClinits() { |
| 634 | Thread* self = Thread::Current(); |
| 635 | for (size_t i = 0; i < ClassLinker::kClassRootsMax; ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 636 | mirror::Class* c = GetClassRoot(ClassRoot(i)); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 637 | if (!c->IsArrayClass() && !c->IsPrimitive()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 638 | StackHandleScope<1> hs(self); |
| 639 | Handle<mirror::Class> h_class(hs.NewHandle(GetClassRoot(ClassRoot(i)))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 640 | EnsureInitialized(self, h_class, true, true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 641 | self->AssertNoPendingException(); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 646 | bool ClassLinker::GenerateOatFile(const char* dex_filename, |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 647 | int oat_fd, |
Vladimir Marko | 60836d5 | 2014-01-16 15:53:38 +0000 | [diff] [blame] | 648 | const char* oat_cache_filename, |
| 649 | std::string* error_msg) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 650 | Locks::mutator_lock_->AssertNotHeld(Thread::Current()); // Avoid starving GC. |
Tsu Chiang Chuang | 12e6d74 | 2014-05-22 10:22:25 -0700 | [diff] [blame] | 651 | std::string dex2oat(Runtime::Current()->GetCompilerExecutable()); |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 652 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 653 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 654 | std::string boot_image_option("--boot-image="); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 655 | if (heap->GetImageSpace() == nullptr) { |
| 656 | // TODO If we get a dex2dex compiler working we could maybe use that, OTOH since we are likely |
| 657 | // out of space anyway it might not matter. |
| 658 | *error_msg = StringPrintf("Cannot create oat file for '%s' because we are running " |
| 659 | "without an image.", dex_filename); |
| 660 | return false; |
| 661 | } |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 662 | boot_image_option += heap->GetImageSpace()->GetImageLocation(); |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 663 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 664 | std::string dex_file_option("--dex-file="); |
| 665 | dex_file_option += dex_filename; |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 666 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 667 | std::string oat_fd_option("--oat-fd="); |
| 668 | StringAppendF(&oat_fd_option, "%d", oat_fd); |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 669 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 670 | std::string oat_location_option("--oat-location="); |
| 671 | oat_location_option += oat_cache_filename; |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 672 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 673 | std::vector<std::string> argv; |
| 674 | argv.push_back(dex2oat); |
| 675 | argv.push_back("--runtime-arg"); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 676 | argv.push_back("-classpath"); |
| 677 | argv.push_back("--runtime-arg"); |
Brian Carlstrom | 35d8b8e | 2014-02-25 10:51:11 -0800 | [diff] [blame] | 678 | argv.push_back(Runtime::Current()->GetClassPathString()); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 679 | |
Ian Rogers | 8afeb85 | 2014-04-02 14:55:49 -0700 | [diff] [blame] | 680 | Runtime::Current()->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 681 | |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 682 | if (!Runtime::Current()->IsVerificationEnabled()) { |
| 683 | argv.push_back("--compiler-filter=verify-none"); |
| 684 | } |
| 685 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 686 | if (Runtime::Current()->MustRelocateIfPossible()) { |
| 687 | argv.push_back("--runtime-arg"); |
| 688 | argv.push_back("-Xrelocate"); |
| 689 | } else { |
| 690 | argv.push_back("--runtime-arg"); |
| 691 | argv.push_back("-Xnorelocate"); |
| 692 | } |
| 693 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 694 | if (!kIsTargetBuild) { |
| 695 | argv.push_back("--host"); |
buzbee | a024a06 | 2013-07-31 10:47:37 -0700 | [diff] [blame] | 696 | } |
Ian Rogers | 8afeb85 | 2014-04-02 14:55:49 -0700 | [diff] [blame] | 697 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 698 | argv.push_back(boot_image_option); |
| 699 | argv.push_back(dex_file_option); |
| 700 | argv.push_back(oat_fd_option); |
| 701 | argv.push_back(oat_location_option); |
| 702 | const std::vector<std::string>& compiler_options = Runtime::Current()->GetCompilerOptions(); |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 703 | for (size_t i = 0; i < compiler_options.size(); ++i) { |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 704 | argv.push_back(compiler_options[i].c_str()); |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 705 | } |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 706 | |
| 707 | return Exec(argv, error_msg); |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 710 | const OatFile* ClassLinker::RegisterOatFile(const OatFile* oat_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 711 | WriterMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 712 | if (kIsDebugBuild) { |
| 713 | for (size_t i = 0; i < oat_files_.size(); ++i) { |
| 714 | CHECK_NE(oat_file, oat_files_[i]) << oat_file->GetLocation(); |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 715 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 716 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 717 | VLOG(class_linker) << "Registering " << oat_file->GetLocation(); |
| 718 | oat_files_.push_back(oat_file); |
| 719 | return oat_file; |
Brian Carlstrom | 866c862 | 2012-01-06 16:35:13 -0800 | [diff] [blame] | 720 | } |
| 721 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 722 | OatFile& ClassLinker::GetImageOatFile(gc::space::ImageSpace* space) { |
| 723 | VLOG(startup) << "ClassLinker::GetImageOatFile entering"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 724 | OatFile* oat_file = space->ReleaseOatFile(); |
| 725 | CHECK_EQ(RegisterOatFile(oat_file), oat_file); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 726 | VLOG(startup) << "ClassLinker::GetImageOatFile exiting"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 727 | return *oat_file; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 728 | } |
| 729 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 730 | const OatFile::OatDexFile* ClassLinker::FindOpenedOatDexFileForDexFile(const DexFile& dex_file) { |
Brian Carlstrom | 08cbf66 | 2013-12-10 16:52:57 -0800 | [diff] [blame] | 731 | const char* dex_location = dex_file.GetLocation().c_str(); |
| 732 | uint32_t dex_location_checksum = dex_file.GetLocationChecksum(); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 733 | return FindOpenedOatDexFile(nullptr, dex_location, &dex_location_checksum); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 734 | } |
| 735 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 736 | const OatFile::OatDexFile* ClassLinker::FindOpenedOatDexFile(const char* oat_location, |
| 737 | const char* dex_location, |
| 738 | const uint32_t* dex_location_checksum) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 739 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 740 | for (const OatFile* oat_file : oat_files_) { |
| 741 | DCHECK(oat_file != nullptr); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 742 | |
| 743 | if (oat_location != nullptr) { |
| 744 | if (oat_file->GetLocation() != oat_location) { |
| 745 | continue; |
| 746 | } |
| 747 | } |
| 748 | |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 749 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, |
Brian Carlstrom | 08cbf66 | 2013-12-10 16:52:57 -0800 | [diff] [blame] | 750 | dex_location_checksum, |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 751 | false); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 752 | if (oat_dex_file != nullptr) { |
| 753 | return oat_dex_file; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 754 | } |
| 755 | } |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 756 | return nullptr; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 757 | } |
| 758 | |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 759 | |
| 760 | // Loads all multi dex files from the given oat file returning true on success. |
| 761 | // |
| 762 | // Parameters: |
| 763 | // oat_file - the oat file to load from |
| 764 | // dex_location - the dex location used to generate the oat file |
| 765 | // dex_location_checksum - the checksum of the dex_location (may be null for pre-opted files) |
| 766 | // generated - whether or not the oat_file existed before or was just (re)generated |
| 767 | // error_msgs - any error messages will be appended here |
| 768 | // dex_files - the loaded dex_files will be appended here (only if the loading succeeds) |
| 769 | static bool LoadMultiDexFilesFromOatFile(const OatFile* oat_file, |
| 770 | const char* dex_location, |
| 771 | const uint32_t* dex_location_checksum, |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 772 | bool generated, |
| 773 | std::vector<std::string>* error_msgs, |
| 774 | std::vector<const DexFile*>* dex_files) { |
| 775 | if (oat_file == nullptr) { |
| 776 | return false; |
| 777 | } |
| 778 | |
| 779 | size_t old_size = dex_files->size(); // To rollback on error. |
| 780 | |
| 781 | bool success = true; |
| 782 | for (size_t i = 0; success; ++i) { |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 783 | std::string next_name_str = DexFile::GetMultiDexClassesDexName(i, dex_location); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 784 | const char* next_name = next_name_str.c_str(); |
| 785 | |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 786 | uint32_t next_location_checksum; |
| 787 | uint32_t* next_location_checksum_pointer = &next_location_checksum; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 788 | std::string error_msg; |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 789 | if ((i == 0) && (strcmp(next_name, dex_location) == 0)) { |
| 790 | // When i=0 the multidex name should be the same as the location name. We already have the |
| 791 | // checksum it so we don't need to recompute it. |
| 792 | if (dex_location_checksum == nullptr) { |
| 793 | next_location_checksum_pointer = nullptr; |
| 794 | } else { |
| 795 | next_location_checksum = *dex_location_checksum; |
| 796 | } |
| 797 | } else if (!DexFile::GetChecksum(next_name, next_location_checksum_pointer, &error_msg)) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 798 | DCHECK_EQ(false, i == 0 && generated); |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 799 | next_location_checksum_pointer = nullptr; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(next_name, nullptr, false); |
| 803 | |
| 804 | if (oat_dex_file == nullptr) { |
| 805 | if (i == 0 && generated) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 806 | error_msg = StringPrintf("\nFailed to find dex file '%s' (checksum 0x%x) in generated out " |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 807 | " file'%s'", dex_location, next_location_checksum, |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 808 | oat_file->GetLocation().c_str()); |
| 809 | error_msgs->push_back(error_msg); |
| 810 | } |
| 811 | break; // Not found, done. |
| 812 | } |
| 813 | |
| 814 | // Checksum test. Test must succeed when generated. |
| 815 | success = !generated; |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 816 | if (next_location_checksum_pointer != nullptr) { |
| 817 | success = next_location_checksum == oat_dex_file->GetDexFileLocationChecksum(); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | if (success) { |
| 821 | const DexFile* dex_file = oat_dex_file->OpenDexFile(&error_msg); |
| 822 | if (dex_file == nullptr) { |
| 823 | success = false; |
| 824 | error_msgs->push_back(error_msg); |
| 825 | } else { |
| 826 | dex_files->push_back(dex_file); |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | // When we generated the file, we expect success, or something is terribly wrong. |
| 831 | CHECK_EQ(false, generated && !success) |
| 832 | << "dex_location=" << next_name << " oat_location=" << oat_file->GetLocation().c_str() |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 833 | << std::hex << " dex_location_checksum=" << next_location_checksum |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 834 | << " OatDexFile::GetLocationChecksum()=" << oat_dex_file->GetDexFileLocationChecksum(); |
| 835 | } |
| 836 | |
| 837 | if (dex_files->size() == old_size) { |
| 838 | success = false; // We did not even find classes.dex |
| 839 | } |
| 840 | |
| 841 | if (success) { |
| 842 | return true; |
| 843 | } else { |
| 844 | // Free all the dex files we have loaded. |
| 845 | auto it = dex_files->begin() + old_size; |
| 846 | auto it_end = dex_files->end(); |
| 847 | for (; it != it_end; it++) { |
| 848 | delete *it; |
| 849 | } |
| 850 | dex_files->erase(dex_files->begin() + old_size, it_end); |
| 851 | |
| 852 | return false; |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | // Multidex files make it possible that some, but not all, dex files can be broken/outdated. This |
| 857 | // complicates the loading process, as we should not use an iterative loading process, because that |
| 858 | // would register the oat file and dex files that come before the broken one. Instead, check all |
| 859 | // multidex ahead of time. |
| 860 | bool ClassLinker::OpenDexFilesFromOat(const char* dex_location, const char* oat_location, |
| 861 | std::vector<std::string>* error_msgs, |
| 862 | std::vector<const DexFile*>* dex_files) { |
| 863 | // 1) Check whether we have an open oat file. |
| 864 | // This requires a dex checksum, use the "primary" one. |
| 865 | uint32_t dex_location_checksum; |
| 866 | uint32_t* dex_location_checksum_pointer = &dex_location_checksum; |
| 867 | bool have_checksum = true; |
| 868 | std::string checksum_error_msg; |
| 869 | if (!DexFile::GetChecksum(dex_location, dex_location_checksum_pointer, &checksum_error_msg)) { |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 870 | // This happens for pre-opted files since the corresponding dex files are no longer on disk. |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 871 | dex_location_checksum_pointer = nullptr; |
| 872 | have_checksum = false; |
| 873 | } |
| 874 | |
| 875 | bool needs_registering = false; |
| 876 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 877 | const OatFile::OatDexFile* oat_dex_file = FindOpenedOatDexFile(oat_location, dex_location, |
| 878 | dex_location_checksum_pointer); |
| 879 | std::unique_ptr<const OatFile> open_oat_file( |
| 880 | oat_dex_file != nullptr ? oat_dex_file->GetOatFile() : nullptr); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 881 | |
| 882 | // 2) If we do not have an open one, maybe there's one on disk already. |
| 883 | |
| 884 | // In case the oat file is not open, we play a locking game here so |
| 885 | // that if two different processes race to load and register or generate |
| 886 | // (or worse, one tries to open a partial generated file) we will be okay. |
| 887 | // This is actually common with apps that use DexClassLoader to work |
| 888 | // around the dex method reference limit and that have a background |
| 889 | // service running in a separate process. |
| 890 | ScopedFlock scoped_flock; |
| 891 | |
| 892 | if (open_oat_file.get() == nullptr) { |
| 893 | if (oat_location != nullptr) { |
| 894 | // Can only do this if we have a checksum, else error. |
| 895 | if (!have_checksum) { |
| 896 | error_msgs->push_back(checksum_error_msg); |
| 897 | return false; |
| 898 | } |
| 899 | |
| 900 | std::string error_msg; |
| 901 | |
| 902 | // We are loading or creating one in the future. Time to set up the file lock. |
| 903 | if (!scoped_flock.Init(oat_location, &error_msg)) { |
| 904 | error_msgs->push_back(error_msg); |
| 905 | return false; |
| 906 | } |
| 907 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 908 | // TODO Caller specifically asks for this oat_location. We should honor it. Probably? |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 909 | open_oat_file.reset(FindOatFileInOatLocationForDexFile(dex_location, dex_location_checksum, |
| 910 | oat_location, &error_msg)); |
| 911 | |
| 912 | if (open_oat_file.get() == nullptr) { |
| 913 | std::string compound_msg = StringPrintf("Failed to find dex file '%s' in oat location '%s': %s", |
| 914 | dex_location, oat_location, error_msg.c_str()); |
| 915 | VLOG(class_linker) << compound_msg; |
| 916 | error_msgs->push_back(compound_msg); |
| 917 | } |
| 918 | } else { |
| 919 | // TODO: What to lock here? |
Calin Juravle | c54aea7 | 2014-07-16 14:45:03 +0100 | [diff] [blame] | 920 | bool obsolete_file_cleanup_failed; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 921 | open_oat_file.reset(FindOatFileContainingDexFileFromDexLocation(dex_location, |
| 922 | dex_location_checksum_pointer, |
Calin Juravle | c54aea7 | 2014-07-16 14:45:03 +0100 | [diff] [blame] | 923 | kRuntimeISA, error_msgs, |
| 924 | &obsolete_file_cleanup_failed)); |
| 925 | // There's no point in going forward and eventually try to regenerate the |
| 926 | // file if we couldn't remove the obsolete one. Mostly likely we will fail |
| 927 | // with the same error when trying to write the new file. |
Calin Juravle | c54aea7 | 2014-07-16 14:45:03 +0100 | [diff] [blame] | 928 | // TODO: should we maybe do this only when we get permission issues? (i.e. EACCESS). |
| 929 | if (obsolete_file_cleanup_failed) { |
| 930 | return false; |
| 931 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 932 | } |
| 933 | needs_registering = true; |
| 934 | } |
| 935 | |
| 936 | // 3) If we have an oat file, check all contained multidex files for our dex_location. |
| 937 | // Note: LoadMultiDexFilesFromOatFile will check for nullptr in the first argument. |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 938 | bool success = LoadMultiDexFilesFromOatFile(open_oat_file.get(), dex_location, |
| 939 | dex_location_checksum_pointer, |
| 940 | false, error_msgs, dex_files); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 941 | if (success) { |
| 942 | const OatFile* oat_file = open_oat_file.release(); // Avoid deleting it. |
| 943 | if (needs_registering) { |
| 944 | // We opened the oat file, so we must register it. |
| 945 | RegisterOatFile(oat_file); |
| 946 | } |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 947 | // If the file isn't executable we failed patchoat but did manage to get the dex files. |
| 948 | return oat_file->IsExecutable(); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 949 | } else { |
| 950 | if (needs_registering) { |
| 951 | // We opened it, delete it. |
| 952 | open_oat_file.reset(); |
| 953 | } else { |
| 954 | open_oat_file.release(); // Do not delete open oat files. |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | // 4) If it's not the case (either no oat file or mismatches), regenerate and load. |
| 959 | |
| 960 | // Need a checksum, fail else. |
| 961 | if (!have_checksum) { |
| 962 | error_msgs->push_back(checksum_error_msg); |
| 963 | return false; |
| 964 | } |
| 965 | |
| 966 | // Look in cache location if no oat_location is given. |
| 967 | std::string cache_location; |
| 968 | if (oat_location == nullptr) { |
| 969 | // Use the dalvik cache. |
| 970 | const std::string dalvik_cache(GetDalvikCacheOrDie(GetInstructionSetString(kRuntimeISA))); |
| 971 | cache_location = GetDalvikCacheFilenameOrDie(dex_location, dalvik_cache.c_str()); |
| 972 | oat_location = cache_location.c_str(); |
| 973 | } |
| 974 | |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 975 | bool has_flock = true; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 976 | // Definitely need to lock now. |
| 977 | if (!scoped_flock.HasFile()) { |
| 978 | std::string error_msg; |
| 979 | if (!scoped_flock.Init(oat_location, &error_msg)) { |
| 980 | error_msgs->push_back(error_msg); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 981 | has_flock = false; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 982 | } |
| 983 | } |
| 984 | |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 985 | if (Runtime::Current()->IsDex2OatEnabled() && has_flock && scoped_flock.HasFile()) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 986 | // Create the oat file. |
| 987 | open_oat_file.reset(CreateOatFileForDexLocation(dex_location, scoped_flock.GetFile()->Fd(), |
| 988 | oat_location, error_msgs)); |
| 989 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 990 | |
| 991 | // Failed, bail. |
| 992 | if (open_oat_file.get() == nullptr) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 993 | std::string error_msg; |
| 994 | // dex2oat was disabled or crashed. Add the dex file in the list of dex_files to make progress. |
| 995 | DexFile::Open(dex_location, dex_location, &error_msg, dex_files); |
| 996 | error_msgs->push_back(error_msg); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 997 | return false; |
| 998 | } |
| 999 | |
| 1000 | // Try to load again, but stronger checks. |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1001 | success = LoadMultiDexFilesFromOatFile(open_oat_file.get(), dex_location, |
| 1002 | dex_location_checksum_pointer, |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 1003 | true, error_msgs, dex_files); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1004 | if (success) { |
| 1005 | RegisterOatFile(open_oat_file.release()); |
| 1006 | return true; |
| 1007 | } else { |
| 1008 | return false; |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | const OatFile* ClassLinker::FindOatFileInOatLocationForDexFile(const char* dex_location, |
| 1013 | uint32_t dex_location_checksum, |
| 1014 | const char* oat_location, |
| 1015 | std::string* error_msg) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1016 | std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_location, oat_location, nullptr, nullptr, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1017 | !Runtime::Current()->IsCompiler(), |
| 1018 | error_msg)); |
| 1019 | if (oat_file.get() == nullptr) { |
| 1020 | *error_msg = StringPrintf("Failed to find existing oat file at %s: %s", oat_location, |
| 1021 | error_msg->c_str()); |
| 1022 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1023 | } |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 1024 | Runtime* runtime = Runtime::Current(); |
Alex Light | 7adb7ac | 2014-08-29 10:28:25 -0700 | [diff] [blame] | 1025 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace(); |
| 1026 | if (image_space != nullptr) { |
| 1027 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1028 | uint32_t expected_image_oat_checksum = image_header.GetOatChecksum(); |
| 1029 | uint32_t actual_image_oat_checksum = oat_file->GetOatHeader().GetImageFileLocationOatChecksum(); |
| 1030 | if (expected_image_oat_checksum != actual_image_oat_checksum) { |
| 1031 | *error_msg = StringPrintf("Failed to find oat file at '%s' with expected image oat checksum of " |
| 1032 | "0x%x, found 0x%x", oat_location, expected_image_oat_checksum, |
| 1033 | actual_image_oat_checksum); |
| 1034 | return nullptr; |
| 1035 | } |
| 1036 | |
| 1037 | uintptr_t expected_image_oat_offset = reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()); |
| 1038 | uint32_t actual_image_oat_offset = oat_file->GetOatHeader().GetImageFileLocationOatDataBegin(); |
| 1039 | if (expected_image_oat_offset != actual_image_oat_offset) { |
| 1040 | *error_msg = StringPrintf("Failed to find oat file at '%s' with expected image oat offset %" |
| 1041 | PRIuPTR ", found %ud", oat_location, expected_image_oat_offset, |
| 1042 | actual_image_oat_offset); |
| 1043 | return nullptr; |
| 1044 | } |
| 1045 | int32_t expected_patch_delta = image_header.GetPatchDelta(); |
| 1046 | int32_t actual_patch_delta = oat_file->GetOatHeader().GetImagePatchDelta(); |
| 1047 | if (expected_patch_delta != actual_patch_delta) { |
| 1048 | *error_msg = StringPrintf("Failed to find oat file at '%s' with expected patch delta %d, " |
| 1049 | " found %d", oat_location, expected_patch_delta, actual_patch_delta); |
| 1050 | return nullptr; |
| 1051 | } |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 1052 | } |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1053 | |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 1054 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, |
| 1055 | &dex_location_checksum); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1056 | if (oat_dex_file == nullptr) { |
| 1057 | *error_msg = StringPrintf("Failed to find oat file at '%s' containing '%s'", oat_location, |
| 1058 | dex_location); |
| 1059 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1060 | } |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1061 | uint32_t expected_dex_checksum = dex_location_checksum; |
| 1062 | uint32_t actual_dex_checksum = oat_dex_file->GetDexFileLocationChecksum(); |
| 1063 | if (expected_dex_checksum != actual_dex_checksum) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1064 | *error_msg = StringPrintf("Failed to find oat file at '%s' with expected dex checksum of 0x%x, " |
| 1065 | "found 0x%x", oat_location, expected_dex_checksum, |
| 1066 | actual_dex_checksum); |
| 1067 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1068 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1069 | std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(error_msg)); |
| 1070 | if (dex_file.get() != nullptr) { |
| 1071 | return oat_file.release(); |
| 1072 | } else { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1073 | return nullptr; |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1074 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1075 | } |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1076 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1077 | const OatFile* ClassLinker::CreateOatFileForDexLocation(const char* dex_location, |
| 1078 | int fd, const char* oat_location, |
| 1079 | std::vector<std::string>* error_msgs) { |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1080 | // Generate the output oat file for the dex file |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1081 | VLOG(class_linker) << "Generating oat file " << oat_location << " for " << dex_location; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1082 | std::string error_msg; |
| 1083 | if (!GenerateOatFile(dex_location, fd, oat_location, &error_msg)) { |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 1084 | CHECK(!error_msg.empty()); |
| 1085 | error_msgs->push_back(error_msg); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1086 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1087 | } |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1088 | std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_location, oat_location, nullptr, nullptr, |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 1089 | !Runtime::Current()->IsCompiler(), |
| 1090 | &error_msg)); |
| 1091 | if (oat_file.get() == nullptr) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1092 | std::string compound_msg = StringPrintf("\nFailed to open generated oat file '%s': %s", |
| 1093 | oat_location, error_msg.c_str()); |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 1094 | error_msgs->push_back(compound_msg); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1095 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1096 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1097 | |
| 1098 | return oat_file.release(); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1099 | } |
| 1100 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1101 | bool ClassLinker::VerifyOatImageChecksum(const OatFile* oat_file, |
| 1102 | const InstructionSet instruction_set) { |
| 1103 | Runtime* runtime = Runtime::Current(); |
| 1104 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1105 | if (image_space == nullptr) { |
| 1106 | return false; |
| 1107 | } |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1108 | uint32_t image_oat_checksum = 0; |
| 1109 | if (instruction_set == kRuntimeISA) { |
| 1110 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1111 | image_oat_checksum = image_header.GetOatChecksum(); |
| 1112 | } else { |
| 1113 | std::unique_ptr<ImageHeader> image_header(gc::space::ImageSpace::ReadImageHeaderOrDie( |
| 1114 | image_space->GetImageLocation().c_str(), instruction_set)); |
| 1115 | image_oat_checksum = image_header->GetOatChecksum(); |
| 1116 | } |
| 1117 | return oat_file->GetOatHeader().GetImageFileLocationOatChecksum() == image_oat_checksum; |
| 1118 | } |
| 1119 | |
| 1120 | bool ClassLinker::VerifyOatChecksums(const OatFile* oat_file, |
| 1121 | const InstructionSet instruction_set, |
| 1122 | std::string* error_msg) { |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 1123 | Runtime* runtime = Runtime::Current(); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1124 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1125 | if (image_space == nullptr) { |
| 1126 | *error_msg = "No image space for verification against"; |
| 1127 | return false; |
| 1128 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1129 | |
| 1130 | // If the requested instruction set is the same as the current runtime, |
| 1131 | // we can use the checksums directly. If it isn't, we'll have to read the |
| 1132 | // image header from the image for the right instruction set. |
| 1133 | uint32_t image_oat_checksum = 0; |
| 1134 | uintptr_t image_oat_data_begin = 0; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1135 | int32_t image_patch_delta = 0; |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 1136 | if (instruction_set == runtime->GetInstructionSet()) { |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1137 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1138 | image_oat_checksum = image_header.GetOatChecksum(); |
| 1139 | image_oat_data_begin = reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1140 | image_patch_delta = image_header.GetPatchDelta(); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1141 | } else { |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1142 | std::unique_ptr<ImageHeader> image_header(gc::space::ImageSpace::ReadImageHeaderOrDie( |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1143 | image_space->GetImageLocation().c_str(), instruction_set)); |
| 1144 | image_oat_checksum = image_header->GetOatChecksum(); |
| 1145 | image_oat_data_begin = reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1146 | image_patch_delta = image_header->GetPatchDelta(); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1147 | } |
| 1148 | const OatHeader& oat_header = oat_file->GetOatHeader(); |
Igor Murashkin | 96e8393 | 2014-10-29 19:45:42 -0700 | [diff] [blame] | 1149 | bool ret = (oat_header.GetImageFileLocationOatChecksum() == image_oat_checksum); |
| 1150 | |
| 1151 | // If the oat file is PIC, it doesn't care if/how image was relocated. Ignore these checks. |
| 1152 | if (!oat_file->IsPic()) { |
| 1153 | ret = ret && (oat_header.GetImagePatchDelta() == image_patch_delta) |
| 1154 | && (oat_header.GetImageFileLocationOatDataBegin() == image_oat_data_begin); |
| 1155 | } |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1156 | if (!ret) { |
| 1157 | *error_msg = StringPrintf("oat file '%s' mismatch (0x%x, %d, %d) with (0x%x, %" PRIdPTR ", %d)", |
| 1158 | oat_file->GetLocation().c_str(), |
| 1159 | oat_file->GetOatHeader().GetImageFileLocationOatChecksum(), |
| 1160 | oat_file->GetOatHeader().GetImageFileLocationOatDataBegin(), |
| 1161 | oat_file->GetOatHeader().GetImagePatchDelta(), |
| 1162 | image_oat_checksum, image_oat_data_begin, image_patch_delta); |
| 1163 | } |
| 1164 | return ret; |
| 1165 | } |
| 1166 | |
| 1167 | bool ClassLinker::VerifyOatAndDexFileChecksums(const OatFile* oat_file, |
| 1168 | const char* dex_location, |
| 1169 | uint32_t dex_location_checksum, |
| 1170 | const InstructionSet instruction_set, |
| 1171 | std::string* error_msg) { |
| 1172 | if (!VerifyOatChecksums(oat_file, instruction_set, error_msg)) { |
| 1173 | return false; |
| 1174 | } |
Brian Carlstrom | 46b8a62 | 2012-06-19 23:13:22 -0700 | [diff] [blame] | 1175 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1176 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, |
| 1177 | &dex_location_checksum); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1178 | if (oat_dex_file == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1179 | *error_msg = StringPrintf("oat file '%s' does not contain contents for '%s' with checksum 0x%x", |
| 1180 | oat_file->GetLocation().c_str(), dex_location, dex_location_checksum); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1181 | for (const OatFile::OatDexFile* oat_dex_file_in : oat_file->GetOatDexFiles()) { |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1182 | *error_msg += StringPrintf("\noat file '%s' contains contents for '%s' with checksum 0x%x", |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1183 | oat_file->GetLocation().c_str(), |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1184 | oat_dex_file_in->GetDexFileLocation().c_str(), |
| 1185 | oat_dex_file_in->GetDexFileLocationChecksum()); |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 1186 | } |
| 1187 | return false; |
| 1188 | } |
Brian Carlstrom | 46b8a62 | 2012-06-19 23:13:22 -0700 | [diff] [blame] | 1189 | |
Calin Juravle | a8c55ae | 2014-09-05 16:14:19 +0100 | [diff] [blame] | 1190 | DCHECK_EQ(dex_location_checksum, oat_dex_file->GetDexFileLocationChecksum()); |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1191 | return true; |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1194 | bool ClassLinker::VerifyOatWithDexFile(const OatFile* oat_file, |
| 1195 | const char* dex_location, |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 1196 | const uint32_t* dex_location_checksum, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1197 | std::string* error_msg) { |
| 1198 | CHECK(oat_file != nullptr); |
| 1199 | CHECK(dex_location != nullptr); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1200 | std::unique_ptr<const DexFile> dex_file; |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 1201 | if (dex_location_checksum == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1202 | // If no classes.dex found in dex_location, it has been stripped or is corrupt, assume oat is |
| 1203 | // up-to-date. This is the common case in user builds for jar's and apk's in the /system |
| 1204 | // directory. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1205 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, nullptr); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1206 | if (oat_dex_file == nullptr) { |
| 1207 | *error_msg = StringPrintf("Dex checksum mismatch for location '%s' and failed to find oat " |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1208 | "dex file '%s': %s", oat_file->GetLocation().c_str(), dex_location, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1209 | error_msg->c_str()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1210 | return false; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1211 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1212 | dex_file.reset(oat_dex_file->OpenDexFile(error_msg)); |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 1213 | } else { |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 1214 | bool verified = VerifyOatAndDexFileChecksums(oat_file, dex_location, *dex_location_checksum, |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1215 | kRuntimeISA, error_msg); |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 1216 | if (!verified) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1217 | return false; |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 1218 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1219 | dex_file.reset(oat_file->GetOatDexFile(dex_location, |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 1220 | dex_location_checksum)->OpenDexFile(error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1221 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1222 | return dex_file.get() != nullptr; |
Brian Carlstrom | 46b8a62 | 2012-06-19 23:13:22 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1225 | const OatFile* ClassLinker::FindOatFileContainingDexFileFromDexLocation( |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1226 | const char* dex_location, |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1227 | const uint32_t* dex_location_checksum, |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1228 | InstructionSet isa, |
Calin Juravle | c54aea7 | 2014-07-16 14:45:03 +0100 | [diff] [blame] | 1229 | std::vector<std::string>* error_msgs, |
| 1230 | bool* obsolete_file_cleanup_failed) { |
| 1231 | *obsolete_file_cleanup_failed = false; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1232 | bool already_opened = false; |
| 1233 | std::string dex_location_str(dex_location); |
| 1234 | std::unique_ptr<const OatFile> oat_file(OpenOatFileFromDexLocation(dex_location_str, isa, |
| 1235 | &already_opened, |
| 1236 | obsolete_file_cleanup_failed, |
| 1237 | error_msgs)); |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 1238 | std::string error_msg; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1239 | if (oat_file.get() == nullptr) { |
| 1240 | error_msgs->push_back(StringPrintf("Failed to open oat file from dex location '%s'", |
| 1241 | dex_location)); |
| 1242 | return nullptr; |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1243 | } else if (oat_file->IsExecutable() && |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 1244 | !VerifyOatWithDexFile(oat_file.get(), dex_location, |
| 1245 | dex_location_checksum, &error_msg)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1246 | error_msgs->push_back(StringPrintf("Failed to verify oat file '%s' found for dex location " |
| 1247 | "'%s': %s", oat_file->GetLocation().c_str(), dex_location, |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 1248 | error_msg.c_str())); |
Brian Carlstrom | 08cbf66 | 2013-12-10 16:52:57 -0800 | [diff] [blame] | 1249 | return nullptr; |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1250 | } else if (!oat_file->IsExecutable() && |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 1251 | Runtime::Current()->GetHeap()->HasImageSpace() && |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1252 | !VerifyOatImageChecksum(oat_file.get(), isa)) { |
| 1253 | error_msgs->push_back(StringPrintf("Failed to verify non-executable oat file '%s' found for " |
| 1254 | "dex location '%s'. Image checksum incorrect.", |
| 1255 | oat_file->GetLocation().c_str(), dex_location)); |
| 1256 | return nullptr; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1257 | } else { |
| 1258 | return oat_file.release(); |
Brian Carlstrom | 08cbf66 | 2013-12-10 16:52:57 -0800 | [diff] [blame] | 1259 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1262 | const OatFile* ClassLinker::FindOpenedOatFileFromOatLocation(const std::string& oat_location) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1263 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1264 | for (size_t i = 0; i < oat_files_.size(); i++) { |
| 1265 | const OatFile* oat_file = oat_files_[i]; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1266 | DCHECK(oat_file != nullptr); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1267 | if (oat_file->GetLocation() == oat_location) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1268 | return oat_file; |
| 1269 | } |
| 1270 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1271 | return nullptr; |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 1272 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1273 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1274 | const OatFile* ClassLinker::OpenOatFileFromDexLocation(const std::string& dex_location, |
| 1275 | InstructionSet isa, |
| 1276 | bool *already_opened, |
| 1277 | bool *obsolete_file_cleanup_failed, |
| 1278 | std::vector<std::string>* error_msgs) { |
| 1279 | // Find out if we've already opened the file |
| 1280 | const OatFile* ret = nullptr; |
| 1281 | std::string odex_filename(DexFilenameToOdexFilename(dex_location, isa)); |
| 1282 | ret = FindOpenedOatFileFromOatLocation(odex_filename); |
| 1283 | if (ret != nullptr) { |
| 1284 | *already_opened = true; |
| 1285 | return ret; |
| 1286 | } |
| 1287 | |
| 1288 | std::string dalvik_cache; |
| 1289 | bool have_android_data = false; |
| 1290 | bool have_dalvik_cache = false; |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 1291 | bool is_global_cache = false; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1292 | GetDalvikCache(GetInstructionSetString(kRuntimeISA), false, &dalvik_cache, |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 1293 | &have_android_data, &have_dalvik_cache, &is_global_cache); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1294 | std::string cache_filename; |
| 1295 | if (have_dalvik_cache) { |
| 1296 | cache_filename = GetDalvikCacheFilenameOrDie(dex_location.c_str(), dalvik_cache.c_str()); |
| 1297 | ret = FindOpenedOatFileFromOatLocation(cache_filename); |
| 1298 | if (ret != nullptr) { |
| 1299 | *already_opened = true; |
| 1300 | return ret; |
| 1301 | } |
| 1302 | } else { |
| 1303 | // If we need to relocate we should just place odex back where it started. |
| 1304 | cache_filename = odex_filename; |
| 1305 | } |
| 1306 | |
| 1307 | ret = nullptr; |
| 1308 | |
| 1309 | // We know that neither the odex nor the cache'd version is already in use, if it even exists. |
| 1310 | // |
| 1311 | // Now we do the following: |
| 1312 | // 1) Try and open the odex version |
| 1313 | // 2) If present, checksum-verified & relocated correctly return it |
| 1314 | // 3) Close the odex version to free up its address space. |
| 1315 | // 4) Try and open the cache version |
| 1316 | // 5) If present, checksum-verified & relocated correctly return it |
| 1317 | // 6) Close the cache version to free up its address space. |
| 1318 | // 7) If we should relocate: |
| 1319 | // a) If we have opened and checksum-verified the odex version relocate it to |
| 1320 | // 'cache_filename' and return it |
| 1321 | // b) If we have opened and checksum-verified the cache version relocate it in place and return |
| 1322 | // it. This should not happen often (I think only the run-test's will hit this case). |
| 1323 | // 8) If the cache-version was present we should delete it since it must be obsolete if we get to |
| 1324 | // this point. |
| 1325 | // 9) Return nullptr |
| 1326 | |
| 1327 | *already_opened = false; |
| 1328 | const Runtime* runtime = Runtime::Current(); |
| 1329 | CHECK(runtime != nullptr); |
| 1330 | bool executable = !runtime->IsCompiler(); |
| 1331 | |
| 1332 | std::string odex_error_msg; |
| 1333 | bool should_patch_system = false; |
| 1334 | bool odex_checksum_verified = false; |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 1335 | bool have_system_odex = false; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1336 | { |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1337 | // There is a high probability that both these oat files map similar/the same address |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1338 | // spaces so we must scope them like this so they each gets its turn. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1339 | std::unique_ptr<OatFile> odex_oat_file(OatFile::Open(odex_filename, odex_filename, nullptr, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1340 | nullptr, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1341 | executable, &odex_error_msg)); |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1342 | if (odex_oat_file.get() != nullptr && CheckOatFile(runtime, odex_oat_file.get(), isa, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1343 | &odex_checksum_verified, |
| 1344 | &odex_error_msg)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1345 | return odex_oat_file.release(); |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 1346 | } else { |
| 1347 | if (odex_checksum_verified) { |
| 1348 | // We can just relocate |
| 1349 | should_patch_system = true; |
| 1350 | odex_error_msg = "Image Patches are incorrect"; |
| 1351 | } |
| 1352 | if (odex_oat_file.get() != nullptr) { |
| 1353 | have_system_odex = true; |
| 1354 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1355 | } |
| 1356 | } |
| 1357 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1358 | std::string cache_error_msg; |
| 1359 | bool should_patch_cache = false; |
| 1360 | bool cache_checksum_verified = false; |
| 1361 | if (have_dalvik_cache) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1362 | std::unique_ptr<OatFile> cache_oat_file(OatFile::Open(cache_filename, cache_filename, nullptr, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1363 | nullptr, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1364 | executable, &cache_error_msg)); |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1365 | if (cache_oat_file.get() != nullptr && CheckOatFile(runtime, cache_oat_file.get(), isa, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1366 | &cache_checksum_verified, |
| 1367 | &cache_error_msg)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1368 | return cache_oat_file.release(); |
| 1369 | } else if (cache_checksum_verified) { |
| 1370 | // We can just relocate |
| 1371 | should_patch_cache = true; |
| 1372 | cache_error_msg = "Image Patches are incorrect"; |
| 1373 | } |
| 1374 | } else if (have_android_data) { |
| 1375 | // dalvik_cache does not exist but android data does. This means we should be able to create |
| 1376 | // it, so we should try. |
| 1377 | GetDalvikCacheOrDie(GetInstructionSetString(kRuntimeISA), true); |
| 1378 | } |
| 1379 | |
| 1380 | ret = nullptr; |
| 1381 | std::string error_msg; |
| 1382 | if (runtime->CanRelocate()) { |
| 1383 | // Run relocation |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1384 | gc::space::ImageSpace* space = Runtime::Current()->GetHeap()->GetImageSpace(); |
| 1385 | if (space != nullptr) { |
| 1386 | const std::string& image_location = space->GetImageLocation(); |
| 1387 | if (odex_checksum_verified && should_patch_system) { |
| 1388 | ret = PatchAndRetrieveOat(odex_filename, cache_filename, image_location, isa, &error_msg); |
| 1389 | } else if (cache_checksum_verified && should_patch_cache) { |
| 1390 | CHECK(have_dalvik_cache); |
| 1391 | ret = PatchAndRetrieveOat(cache_filename, cache_filename, image_location, isa, &error_msg); |
| 1392 | } |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 1393 | } else if (have_system_odex) { |
| 1394 | ret = GetInterpretedOnlyOat(odex_filename, isa, &error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1395 | } |
| 1396 | } |
| 1397 | if (ret == nullptr && have_dalvik_cache && OS::FileExists(cache_filename.c_str())) { |
| 1398 | // implicitly: were able to fine where the cached version is but we were unable to use it, |
| 1399 | // either as a destination for relocation or to open a file. We should delete it if it is |
| 1400 | // there. |
| 1401 | if (TEMP_FAILURE_RETRY(unlink(cache_filename.c_str())) != 0) { |
| 1402 | std::string rm_error_msg = StringPrintf("Failed to remove obsolete file from %s when " |
| 1403 | "searching for dex file %s: %s", |
| 1404 | cache_filename.c_str(), dex_location.c_str(), |
| 1405 | strerror(errno)); |
| 1406 | error_msgs->push_back(rm_error_msg); |
| 1407 | VLOG(class_linker) << rm_error_msg; |
| 1408 | // Let the caller know that we couldn't remove the obsolete file. |
| 1409 | // This is a good indication that further writes may fail as well. |
| 1410 | *obsolete_file_cleanup_failed = true; |
| 1411 | } |
| 1412 | } |
| 1413 | if (ret == nullptr) { |
| 1414 | VLOG(class_linker) << error_msg; |
| 1415 | error_msgs->push_back(error_msg); |
| 1416 | std::string relocation_msg; |
| 1417 | if (runtime->CanRelocate()) { |
| 1418 | relocation_msg = StringPrintf(" and relocation failed"); |
| 1419 | } |
| 1420 | if (have_dalvik_cache && cache_checksum_verified) { |
| 1421 | error_msg = StringPrintf("Failed to open oat file from %s (error %s) or %s " |
| 1422 | "(error %s)%s.", odex_filename.c_str(), odex_error_msg.c_str(), |
| 1423 | cache_filename.c_str(), cache_error_msg.c_str(), |
| 1424 | relocation_msg.c_str()); |
| 1425 | } else { |
| 1426 | error_msg = StringPrintf("Failed to open oat file from %s (error %s) (no " |
| 1427 | "dalvik_cache availible)%s.", odex_filename.c_str(), |
| 1428 | odex_error_msg.c_str(), relocation_msg.c_str()); |
| 1429 | } |
| 1430 | VLOG(class_linker) << error_msg; |
| 1431 | error_msgs->push_back(error_msg); |
| 1432 | } |
| 1433 | return ret; |
| 1434 | } |
| 1435 | |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1436 | const OatFile* ClassLinker::GetInterpretedOnlyOat(const std::string& oat_path, |
| 1437 | InstructionSet isa, |
| 1438 | std::string* error_msg) { |
| 1439 | // We open it non-executable |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1440 | std::unique_ptr<OatFile> output(OatFile::Open(oat_path, oat_path, nullptr, nullptr, false, error_msg)); |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1441 | if (output.get() == nullptr) { |
| 1442 | return nullptr; |
| 1443 | } |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 1444 | if (!Runtime::Current()->GetHeap()->HasImageSpace() || |
| 1445 | VerifyOatImageChecksum(output.get(), isa)) { |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1446 | return output.release(); |
| 1447 | } else { |
| 1448 | *error_msg = StringPrintf("Could not use oat file '%s', image checksum failed to verify.", |
| 1449 | oat_path.c_str()); |
| 1450 | return nullptr; |
| 1451 | } |
| 1452 | } |
| 1453 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1454 | const OatFile* ClassLinker::PatchAndRetrieveOat(const std::string& input_oat, |
| 1455 | const std::string& output_oat, |
| 1456 | const std::string& image_location, |
| 1457 | InstructionSet isa, |
| 1458 | std::string* error_msg) { |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1459 | Runtime* runtime = Runtime::Current(); |
| 1460 | DCHECK(runtime != nullptr); |
| 1461 | if (!runtime->GetHeap()->HasImageSpace()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1462 | // We don't have an image space so there is no point in trying to patchoat. |
| 1463 | LOG(WARNING) << "Patching of oat file '" << input_oat << "' not attempted because we are " |
| 1464 | << "running without an image. Attempting to use oat file for interpretation."; |
| 1465 | return GetInterpretedOnlyOat(input_oat, isa, error_msg); |
| 1466 | } |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1467 | if (!runtime->IsDex2OatEnabled()) { |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1468 | // We don't have dex2oat so we can assume we don't have patchoat either. We should just use the |
| 1469 | // input_oat but make sure we only do interpretation on it's dex files. |
| 1470 | LOG(WARNING) << "Patching of oat file '" << input_oat << "' not attempted due to dex2oat being " |
| 1471 | << "disabled. Attempting to use oat file for interpretation"; |
| 1472 | return GetInterpretedOnlyOat(input_oat, isa, error_msg); |
| 1473 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1474 | Locks::mutator_lock_->AssertNotHeld(Thread::Current()); // Avoid starving GC. |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1475 | std::string patchoat(runtime->GetPatchoatExecutable()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1476 | |
| 1477 | std::string isa_arg("--instruction-set="); |
| 1478 | isa_arg += GetInstructionSetString(isa); |
| 1479 | std::string input_oat_filename_arg("--input-oat-file="); |
| 1480 | input_oat_filename_arg += input_oat; |
| 1481 | std::string output_oat_filename_arg("--output-oat-file="); |
| 1482 | output_oat_filename_arg += output_oat; |
| 1483 | std::string patched_image_arg("--patched-image-location="); |
| 1484 | patched_image_arg += image_location; |
| 1485 | |
| 1486 | std::vector<std::string> argv; |
| 1487 | argv.push_back(patchoat); |
| 1488 | argv.push_back(isa_arg); |
| 1489 | argv.push_back(input_oat_filename_arg); |
| 1490 | argv.push_back(output_oat_filename_arg); |
| 1491 | argv.push_back(patched_image_arg); |
| 1492 | |
| 1493 | std::string command_line(Join(argv, ' ')); |
| 1494 | LOG(INFO) << "Relocate Oat File: " << command_line; |
| 1495 | bool success = Exec(argv, error_msg); |
| 1496 | if (success) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1497 | std::unique_ptr<OatFile> output(OatFile::Open(output_oat, output_oat, nullptr, nullptr, |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1498 | !runtime->IsCompiler(), error_msg)); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1499 | bool checksum_verified = false; |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1500 | if (output.get() != nullptr && CheckOatFile(runtime, output.get(), isa, &checksum_verified, |
| 1501 | error_msg)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1502 | return output.release(); |
| 1503 | } else if (output.get() != nullptr) { |
| 1504 | *error_msg = StringPrintf("Patching of oat file '%s' succeeded " |
| 1505 | "but output file '%s' failed verifcation: %s", |
| 1506 | input_oat.c_str(), output_oat.c_str(), error_msg->c_str()); |
| 1507 | } else { |
| 1508 | *error_msg = StringPrintf("Patching of oat file '%s' succeeded " |
| 1509 | "but was unable to open output file '%s': %s", |
| 1510 | input_oat.c_str(), output_oat.c_str(), error_msg->c_str()); |
| 1511 | } |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1512 | } else if (!runtime->IsCompiler()) { |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1513 | // patchoat failed which means we probably don't have enough room to place the output oat file, |
| 1514 | // instead of failing we should just run the interpreter from the dex files in the input oat. |
| 1515 | LOG(WARNING) << "Patching of oat file '" << input_oat << "' failed. Attempting to use oat file " |
| 1516 | << "for interpretation. patchoat failure was: " << *error_msg; |
| 1517 | return GetInterpretedOnlyOat(input_oat, isa, error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1518 | } else { |
| 1519 | *error_msg = StringPrintf("Patching of oat file '%s to '%s' " |
| 1520 | "failed: %s", input_oat.c_str(), output_oat.c_str(), |
| 1521 | error_msg->c_str()); |
| 1522 | } |
| 1523 | return nullptr; |
| 1524 | } |
| 1525 | |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1526 | bool ClassLinker::CheckOatFile(const Runtime* runtime, const OatFile* oat_file, InstructionSet isa, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1527 | bool* checksum_verified, |
| 1528 | std::string* error_msg) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1529 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1530 | if (image_space == nullptr) { |
| 1531 | *error_msg = "No image space present"; |
| 1532 | return false; |
| 1533 | } |
Vladimir Marko | 30c2e1b | 2014-09-08 09:55:29 +0100 | [diff] [blame] | 1534 | uint32_t real_image_checksum; |
| 1535 | void* real_image_oat_offset; |
| 1536 | int32_t real_patch_delta; |
| 1537 | if (isa == runtime->GetInstructionSet()) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1538 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1539 | real_image_checksum = image_header.GetOatChecksum(); |
| 1540 | real_image_oat_offset = image_header.GetOatDataBegin(); |
| 1541 | real_patch_delta = image_header.GetPatchDelta(); |
| 1542 | } else { |
| 1543 | std::unique_ptr<ImageHeader> image_header(gc::space::ImageSpace::ReadImageHeaderOrDie( |
| 1544 | image_space->GetImageLocation().c_str(), isa)); |
| 1545 | real_image_checksum = image_header->GetOatChecksum(); |
| 1546 | real_image_oat_offset = image_header->GetOatDataBegin(); |
| 1547 | real_patch_delta = image_header->GetPatchDelta(); |
| 1548 | } |
| 1549 | |
| 1550 | const OatHeader& oat_header = oat_file->GetOatHeader(); |
Vladimir Marko | 30c2e1b | 2014-09-08 09:55:29 +0100 | [diff] [blame] | 1551 | std::string compound_msg; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1552 | |
| 1553 | uint32_t oat_image_checksum = oat_header.GetImageFileLocationOatChecksum(); |
| 1554 | *checksum_verified = oat_image_checksum == real_image_checksum; |
| 1555 | if (!*checksum_verified) { |
Vladimir Marko | 30c2e1b | 2014-09-08 09:55:29 +0100 | [diff] [blame] | 1556 | StringAppendF(&compound_msg, " Oat Image Checksum Incorrect (expected 0x%x, received 0x%x)", |
| 1557 | real_image_checksum, oat_image_checksum); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1558 | } |
| 1559 | |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1560 | bool offset_verified; |
| 1561 | bool patch_delta_verified; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1562 | |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1563 | if (!oat_file->IsPic()) { |
| 1564 | // If an oat file is not PIC, we need to check that the image is at the expected location and |
| 1565 | // patched in the same way. |
| 1566 | void* oat_image_oat_offset = |
| 1567 | reinterpret_cast<void*>(oat_header.GetImageFileLocationOatDataBegin()); |
| 1568 | offset_verified = oat_image_oat_offset == real_image_oat_offset; |
| 1569 | if (!offset_verified) { |
| 1570 | StringAppendF(&compound_msg, " Oat Image oat offset incorrect (expected 0x%p, received 0x%p)", |
| 1571 | real_image_oat_offset, oat_image_oat_offset); |
| 1572 | } |
| 1573 | |
| 1574 | int32_t oat_patch_delta = oat_header.GetImagePatchDelta(); |
| 1575 | patch_delta_verified = oat_patch_delta == real_patch_delta; |
| 1576 | if (!patch_delta_verified) { |
| 1577 | StringAppendF(&compound_msg, " Oat image patch delta incorrect (expected 0x%x, " |
| 1578 | "received 0x%x)", real_patch_delta, oat_patch_delta); |
| 1579 | } |
| 1580 | } else { |
| 1581 | // If an oat file is PIC, we ignore offset and patching delta. |
| 1582 | offset_verified = true; |
| 1583 | patch_delta_verified = true; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | bool ret = (*checksum_verified && offset_verified && patch_delta_verified); |
Vladimir Marko | 30c2e1b | 2014-09-08 09:55:29 +0100 | [diff] [blame] | 1587 | if (!ret) { |
| 1588 | *error_msg = "Oat file failed to verify:" + compound_msg; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1589 | } |
| 1590 | return ret; |
| 1591 | } |
| 1592 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1593 | const OatFile* ClassLinker::FindOatFileFromOatLocation(const std::string& oat_location, |
| 1594 | std::string* error_msg) { |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 1595 | const OatFile* oat_file = FindOpenedOatFileFromOatLocation(oat_location); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1596 | if (oat_file != nullptr) { |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 1597 | return oat_file; |
| 1598 | } |
| 1599 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1600 | return OatFile::Open(oat_location, oat_location, nullptr, nullptr, !Runtime::Current()->IsCompiler(), |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1601 | error_msg); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1602 | } |
| 1603 | |
Mathieu Chartier | a89d7ed | 2014-12-05 10:57:13 -0800 | [diff] [blame^] | 1604 | void ClassLinker::InitFromImageInterpretOnlyCallback(mirror::Object* obj, void* arg) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1605 | ClassLinker* class_linker = reinterpret_cast<ClassLinker*>(arg); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1606 | DCHECK(obj != nullptr); |
| 1607 | DCHECK(class_linker != nullptr); |
Mathieu Chartier | a89d7ed | 2014-12-05 10:57:13 -0800 | [diff] [blame^] | 1608 | size_t pointer_size = class_linker->image_pointer_size_; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1609 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1610 | if (obj->IsArtMethod()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1611 | mirror::ArtMethod* method = obj->AsArtMethod(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1612 | if (!method->IsNative()) { |
Mathieu Chartier | a89d7ed | 2014-12-05 10:57:13 -0800 | [diff] [blame^] | 1613 | method->SetEntryPointFromInterpreterPtrSize(artInterpreterToInterpreterBridge, pointer_size); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1614 | if (method != Runtime::Current()->GetResolutionMethod()) { |
Mathieu Chartier | a89d7ed | 2014-12-05 10:57:13 -0800 | [diff] [blame^] | 1615 | method->SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(), |
| 1616 | pointer_size); |
| 1617 | method->SetEntryPointFromPortableCompiledCodePtrSize(GetPortableToInterpreterBridge(), |
| 1618 | pointer_size); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1619 | } |
| 1620 | } |
| 1621 | } |
| 1622 | } |
| 1623 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1624 | void ClassLinker::InitFromImage() { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1625 | VLOG(startup) << "ClassLinker::InitFromImage entering"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1626 | CHECK(!init_done_); |
| 1627 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1628 | Thread* self = Thread::Current(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1629 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 1630 | gc::space::ImageSpace* space = heap->GetImageSpace(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1631 | dex_cache_image_class_lookup_required_ = true; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1632 | CHECK(space != nullptr); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1633 | OatFile& oat_file = GetImageOatFile(space); |
| 1634 | CHECK_EQ(oat_file.GetOatHeader().GetImageFileLocationOatChecksum(), 0U); |
| 1635 | CHECK_EQ(oat_file.GetOatHeader().GetImageFileLocationOatDataBegin(), 0U); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1636 | const char* image_file_location = oat_file.GetOatHeader(). |
| 1637 | GetStoreValueByKey(OatHeader::kImageLocationKey); |
| 1638 | CHECK(image_file_location == nullptr || *image_file_location == 0); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1639 | portable_resolution_trampoline_ = oat_file.GetOatHeader().GetPortableResolutionTrampoline(); |
| 1640 | quick_resolution_trampoline_ = oat_file.GetOatHeader().GetQuickResolutionTrampoline(); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1641 | portable_imt_conflict_trampoline_ = oat_file.GetOatHeader().GetPortableImtConflictTrampoline(); |
| 1642 | quick_imt_conflict_trampoline_ = oat_file.GetOatHeader().GetQuickImtConflictTrampoline(); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1643 | quick_generic_jni_trampoline_ = oat_file.GetOatHeader().GetQuickGenericJniTrampoline(); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 1644 | quick_to_interpreter_bridge_trampoline_ = oat_file.GetOatHeader().GetQuickToInterpreterBridge(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1645 | mirror::Object* dex_caches_object = space->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches); |
| 1646 | mirror::ObjectArray<mirror::DexCache>* dex_caches = |
| 1647 | dex_caches_object->AsObjectArray<mirror::DexCache>(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1648 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1649 | StackHandleScope<1> hs(self); |
| 1650 | Handle<mirror::ObjectArray<mirror::Class>> class_roots(hs.NewHandle( |
| 1651 | space->GetImageHeader().GetImageRoot(ImageHeader::kClassRoots)-> |
| 1652 | AsObjectArray<mirror::Class>())); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1653 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>(class_roots.Get()); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1654 | |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1655 | // Special case of setting up the String class early so that we can test arbitrary objects |
| 1656 | // as being Strings or not |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 1657 | mirror::String::SetClass(GetClassRoot(kJavaLangString)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1658 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1659 | CHECK_EQ(oat_file.GetOatHeader().GetDexFileCount(), |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1660 | static_cast<uint32_t>(dex_caches->GetLength())); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1661 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1662 | StackHandleScope<1> hs2(self); |
| 1663 | Handle<mirror::DexCache> dex_cache(hs2.NewHandle(dex_caches->Get(i))); |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1664 | const std::string& dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1665 | const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(dex_file_location.c_str(), |
| 1666 | nullptr); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1667 | CHECK(oat_dex_file != nullptr) << oat_file.GetLocation() << " " << dex_file_location; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1668 | std::string error_msg; |
| 1669 | const DexFile* dex_file = oat_dex_file->OpenDexFile(&error_msg); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1670 | if (dex_file == nullptr) { |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1671 | LOG(FATAL) << "Failed to open dex file " << dex_file_location |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1672 | << " from within oat file " << oat_file.GetLocation() |
| 1673 | << " error '" << error_msg << "'"; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1674 | UNREACHABLE(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1675 | } |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1676 | |
| 1677 | CHECK_EQ(dex_file->GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum()); |
| 1678 | |
| 1679 | AppendToBootClassPath(*dex_file, dex_cache); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1680 | } |
| 1681 | |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 1682 | // Set classes on AbstractMethod early so that IsMethod tests can be performed during the live |
| 1683 | // bitmap walk. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1684 | mirror::ArtMethod::SetClass(GetClassRoot(kJavaLangReflectArtMethod)); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 1685 | size_t art_method_object_size = mirror::ArtMethod::GetJavaLangReflectArtMethod()->GetObjectSize(); |
| 1686 | if (!Runtime::Current()->IsCompiler()) { |
| 1687 | // Compiler supports having an image with a different pointer size than the runtime. This |
| 1688 | // happens on the host for compile 32 bit tests since we use a 64 bit libart compiler. We may |
| 1689 | // also use 32 bit dex2oat on a system with 64 bit apps. |
| 1690 | CHECK_EQ(art_method_object_size, mirror::ArtMethod::InstanceSize(sizeof(void*))) |
| 1691 | << sizeof(void*); |
| 1692 | } |
| 1693 | if (art_method_object_size == mirror::ArtMethod::InstanceSize(4)) { |
| 1694 | image_pointer_size_ = 4; |
| 1695 | } else { |
| 1696 | CHECK_EQ(art_method_object_size, mirror::ArtMethod::InstanceSize(8)); |
| 1697 | image_pointer_size_ = 8; |
| 1698 | } |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 1699 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1700 | // Set entry point to interpreter if in InterpretOnly mode. |
Mathieu Chartier | a89d7ed | 2014-12-05 10:57:13 -0800 | [diff] [blame^] | 1701 | Runtime* runtime = Runtime::Current(); |
| 1702 | if (!runtime->IsCompiler() && runtime->GetInstrumentation()->InterpretOnly()) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1703 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1704 | heap->VisitObjects(InitFromImageInterpretOnlyCallback, this); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1705 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1706 | |
| 1707 | // reinit class_roots_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1708 | mirror::Class::SetClassClass(class_roots->Get(kJavaLangClass)); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1709 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>(class_roots.Get()); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1710 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1711 | // reinit array_iftable_ from any array class instance, they should be == |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1712 | array_iftable_ = GcRoot<mirror::IfTable>(GetClassRoot(kObjectArrayClass)->GetIfTable()); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 1713 | DCHECK_EQ(array_iftable_.Read(), GetClassRoot(kBooleanArrayClass)->GetIfTable()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1714 | // String class root was set above |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 1715 | mirror::Reference::SetClass(GetClassRoot(kJavaLangRefReference)); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1716 | mirror::ArtField::SetClass(GetClassRoot(kJavaLangReflectArtField)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1717 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
| 1718 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
| 1719 | mirror::CharArray::SetArrayClass(GetClassRoot(kCharArrayClass)); |
| 1720 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
| 1721 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
| 1722 | mirror::IntArray::SetArrayClass(GetClassRoot(kIntArrayClass)); |
| 1723 | mirror::LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
| 1724 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
| 1725 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
| 1726 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1727 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1728 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1729 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1730 | VLOG(startup) << "ClassLinker::InitFromImage exiting"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1731 | } |
| 1732 | |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1733 | void ClassLinker::VisitClassRoots(RootCallback* callback, void* arg, VisitRootFlags flags) { |
| 1734 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 1735 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1736 | for (GcRoot<mirror::Class>& root : class_table_) { |
| 1737 | root.VisitRoot(callback, arg, 0, kRootStickyClass); |
| 1738 | } |
| 1739 | for (GcRoot<mirror::Class>& root : pre_zygote_class_table_) { |
| 1740 | root.VisitRoot(callback, arg, 0, kRootStickyClass); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1741 | } |
| 1742 | } else if ((flags & kVisitRootFlagNewRoots) != 0) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1743 | for (auto& root : new_class_roots_) { |
| 1744 | mirror::Class* old_ref = root.Read<kWithoutReadBarrier>(); |
| 1745 | root.VisitRoot(callback, arg, 0, kRootStickyClass); |
| 1746 | mirror::Class* new_ref = root.Read<kWithoutReadBarrier>(); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1747 | if (UNLIKELY(new_ref != old_ref)) { |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1748 | // Uh ohes, GC moved a root in the log. Need to search the class_table and update the |
| 1749 | // corresponding object. This is slow, but luckily for us, this may only happen with a |
| 1750 | // concurrent moving GC. |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1751 | auto it = class_table_.Find(GcRoot<mirror::Class>(old_ref)); |
| 1752 | class_table_.Erase(it); |
| 1753 | class_table_.Insert(GcRoot<mirror::Class>(new_ref)); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1754 | } |
| 1755 | } |
| 1756 | } |
| 1757 | if ((flags & kVisitRootFlagClearRootLog) != 0) { |
| 1758 | new_class_roots_.clear(); |
| 1759 | } |
| 1760 | if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
| 1761 | log_new_class_table_roots_ = true; |
| 1762 | } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
| 1763 | log_new_class_table_roots_ = false; |
| 1764 | } |
| 1765 | // We deliberately ignore the class roots in the image since we |
| 1766 | // handle image roots by using the MS/CMS rescanning of dirty cards. |
| 1767 | } |
| 1768 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1769 | // Keep in sync with InitCallback. Anything we visit, we need to |
| 1770 | // reinit references to when reinitializing a ClassLinker from a |
| 1771 | // mapped image. |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 1772 | void ClassLinker::VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1773 | class_roots_.VisitRoot(callback, arg, 0, kRootVMInternal); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1774 | Thread* self = Thread::Current(); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1775 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 1776 | ReaderMutexLock mu(self, dex_lock_); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 1777 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1778 | for (GcRoot<mirror::DexCache>& dex_cache : dex_caches_) { |
| 1779 | dex_cache.VisitRoot(callback, arg, 0, kRootVMInternal); |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 1780 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 1781 | } else if ((flags & kVisitRootFlagNewRoots) != 0) { |
| 1782 | for (size_t index : new_dex_cache_roots_) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1783 | dex_caches_[index].VisitRoot(callback, arg, 0, kRootVMInternal); |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 1784 | } |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1785 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 1786 | if ((flags & kVisitRootFlagClearRootLog) != 0) { |
| 1787 | new_dex_cache_roots_.clear(); |
| 1788 | } |
| 1789 | if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
| 1790 | log_new_dex_caches_roots_ = true; |
| 1791 | } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
| 1792 | log_new_dex_caches_roots_ = false; |
| 1793 | } |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1794 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1795 | VisitClassRoots(callback, arg, flags); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1796 | array_iftable_.VisitRoot(callback, arg, 0, kRootVMInternal); |
| 1797 | DCHECK(!array_iftable_.IsNull()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1798 | for (size_t i = 0; i < kFindArrayCacheSize; ++i) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1799 | if (!find_array_class_cache_[i].IsNull()) { |
| 1800 | find_array_class_cache_[i].VisitRoot(callback, arg, 0, kRootVMInternal); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1801 | } |
| 1802 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1803 | } |
| 1804 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1805 | void ClassLinker::VisitClasses(ClassVisitor* visitor, void* arg) { |
| 1806 | if (dex_cache_image_class_lookup_required_) { |
| 1807 | MoveImageClassesToClassTable(); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1808 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1809 | // TODO: why isn't this a ReaderMutexLock? |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 1810 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1811 | for (GcRoot<mirror::Class>& root : class_table_) { |
| 1812 | if (!visitor(root.Read(), arg)) { |
| 1813 | return; |
| 1814 | } |
| 1815 | } |
| 1816 | for (GcRoot<mirror::Class>& root : pre_zygote_class_table_) { |
| 1817 | if (!visitor(root.Read(), arg)) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1818 | return; |
| 1819 | } |
| 1820 | } |
| 1821 | } |
| 1822 | |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1823 | static bool GetClassesVisitorSet(mirror::Class* c, void* arg) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1824 | std::set<mirror::Class*>* classes = reinterpret_cast<std::set<mirror::Class*>*>(arg); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1825 | classes->insert(c); |
| 1826 | return true; |
| 1827 | } |
| 1828 | |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1829 | struct GetClassesVisitorArrayArg { |
| 1830 | Handle<mirror::ObjectArray<mirror::Class>>* classes; |
| 1831 | int32_t index; |
| 1832 | bool success; |
| 1833 | }; |
| 1834 | |
| 1835 | static bool GetClassesVisitorArray(mirror::Class* c, void* varg) |
| 1836 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1837 | GetClassesVisitorArrayArg* arg = reinterpret_cast<GetClassesVisitorArrayArg*>(varg); |
| 1838 | if (arg->index < (*arg->classes)->GetLength()) { |
| 1839 | (*arg->classes)->Set(arg->index, c); |
| 1840 | arg->index++; |
| 1841 | return true; |
| 1842 | } else { |
| 1843 | arg->success = false; |
| 1844 | return false; |
| 1845 | } |
| 1846 | } |
| 1847 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1848 | void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor, void* arg) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1849 | // TODO: it may be possible to avoid secondary storage if we iterate over dex caches. The problem |
| 1850 | // is avoiding duplicates. |
| 1851 | if (!kMovingClasses) { |
| 1852 | std::set<mirror::Class*> classes; |
| 1853 | VisitClasses(GetClassesVisitorSet, &classes); |
| 1854 | for (mirror::Class* klass : classes) { |
| 1855 | if (!visitor(klass, arg)) { |
| 1856 | return; |
| 1857 | } |
| 1858 | } |
| 1859 | } else { |
| 1860 | Thread* self = Thread::Current(); |
| 1861 | StackHandleScope<1> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 1862 | MutableHandle<mirror::ObjectArray<mirror::Class>> classes = |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1863 | hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
| 1864 | GetClassesVisitorArrayArg local_arg; |
| 1865 | local_arg.classes = &classes; |
| 1866 | local_arg.success = false; |
| 1867 | // We size the array assuming classes won't be added to the class table during the visit. |
| 1868 | // If this assumption fails we iterate again. |
| 1869 | while (!local_arg.success) { |
| 1870 | size_t class_table_size; |
| 1871 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1872 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1873 | class_table_size = class_table_.Size() + pre_zygote_class_table_.Size(); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1874 | } |
| 1875 | mirror::Class* class_type = mirror::Class::GetJavaLangClass(); |
| 1876 | mirror::Class* array_of_class = FindArrayClass(self, &class_type); |
| 1877 | classes.Assign( |
| 1878 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, class_table_size)); |
| 1879 | CHECK(classes.Get() != nullptr); // OOME. |
| 1880 | local_arg.index = 0; |
| 1881 | local_arg.success = true; |
| 1882 | VisitClasses(GetClassesVisitorArray, &local_arg); |
| 1883 | } |
| 1884 | for (int32_t i = 0; i < classes->GetLength(); ++i) { |
| 1885 | // If the class table shrank during creation of the clases array we expect null elements. If |
| 1886 | // the class table grew then the loop repeats. If classes are created after the loop has |
| 1887 | // finished then we don't visit. |
| 1888 | mirror::Class* klass = classes->Get(i); |
| 1889 | if (klass != nullptr && !visitor(klass, arg)) { |
| 1890 | return; |
| 1891 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1892 | } |
| 1893 | } |
| 1894 | } |
| 1895 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1896 | ClassLinker::~ClassLinker() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1897 | mirror::Class::ResetClass(); |
| 1898 | mirror::String::ResetClass(); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 1899 | mirror::Reference::ResetClass(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1900 | mirror::ArtField::ResetClass(); |
| 1901 | mirror::ArtMethod::ResetClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1902 | mirror::BooleanArray::ResetArrayClass(); |
| 1903 | mirror::ByteArray::ResetArrayClass(); |
| 1904 | mirror::CharArray::ResetArrayClass(); |
| 1905 | mirror::DoubleArray::ResetArrayClass(); |
| 1906 | mirror::FloatArray::ResetArrayClass(); |
| 1907 | mirror::IntArray::ResetArrayClass(); |
| 1908 | mirror::LongArray::ResetArrayClass(); |
| 1909 | mirror::ShortArray::ResetArrayClass(); |
| 1910 | mirror::Throwable::ResetClass(); |
| 1911 | mirror::StackTraceElement::ResetClass(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1912 | STLDeleteElements(&boot_class_path_); |
| 1913 | STLDeleteElements(&oat_files_); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1914 | } |
| 1915 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1916 | mirror::DexCache* ClassLinker::AllocDexCache(Thread* self, const DexFile& dex_file) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1917 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1918 | StackHandleScope<16> hs(self); |
| 1919 | Handle<mirror::Class> dex_cache_class(hs.NewHandle(GetClassRoot(kJavaLangDexCache))); |
| 1920 | Handle<mirror::DexCache> dex_cache( |
| 1921 | hs.NewHandle(down_cast<mirror::DexCache*>( |
| 1922 | heap->AllocObject<true>(self, dex_cache_class.Get(), dex_cache_class->GetObjectSize(), |
| 1923 | VoidFunctor())))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1924 | if (dex_cache.Get() == nullptr) { |
| 1925 | return nullptr; |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1926 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1927 | Handle<mirror::String> |
| 1928 | location(hs.NewHandle(intern_table_->InternStrong(dex_file.GetLocation().c_str()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1929 | if (location.Get() == nullptr) { |
| 1930 | return nullptr; |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1931 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1932 | Handle<mirror::ObjectArray<mirror::String>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1933 | strings(hs.NewHandle(AllocStringArray(self, dex_file.NumStringIds()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1934 | if (strings.Get() == nullptr) { |
| 1935 | return nullptr; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1936 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1937 | Handle<mirror::ObjectArray<mirror::Class>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1938 | types(hs.NewHandle(AllocClassArray(self, dex_file.NumTypeIds()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1939 | if (types.Get() == nullptr) { |
| 1940 | return nullptr; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1941 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1942 | Handle<mirror::ObjectArray<mirror::ArtMethod>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1943 | methods(hs.NewHandle(AllocArtMethodArray(self, dex_file.NumMethodIds()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1944 | if (methods.Get() == nullptr) { |
| 1945 | return nullptr; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1946 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1947 | Handle<mirror::ObjectArray<mirror::ArtField>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1948 | fields(hs.NewHandle(AllocArtFieldArray(self, dex_file.NumFieldIds()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1949 | if (fields.Get() == nullptr) { |
| 1950 | return nullptr; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1951 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1952 | dex_cache->Init(&dex_file, location.Get(), strings.Get(), types.Get(), methods.Get(), |
| 1953 | fields.Get()); |
| 1954 | return dex_cache.Get(); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1955 | } |
| 1956 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1957 | mirror::Class* ClassLinker::AllocClass(Thread* self, mirror::Class* java_lang_Class, |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1958 | uint32_t class_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1959 | DCHECK_GE(class_size, sizeof(mirror::Class)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1960 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1961 | mirror::Class::InitializeClassVisitor visitor(class_size); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1962 | mirror::Object* k = kMovingClasses ? |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1963 | heap->AllocObject<true>(self, java_lang_Class, class_size, visitor) : |
| 1964 | heap->AllocNonMovableObject<true>(self, java_lang_Class, class_size, visitor); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1965 | if (UNLIKELY(k == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1966 | CHECK(self->IsExceptionPending()); // OOME. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1967 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1968 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1969 | return k->AsClass(); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1970 | } |
| 1971 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1972 | mirror::Class* ClassLinker::AllocClass(Thread* self, uint32_t class_size) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1973 | return AllocClass(self, GetClassRoot(kJavaLangClass), class_size); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1974 | } |
| 1975 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1976 | mirror::ArtField* ClassLinker::AllocArtField(Thread* self) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1977 | return down_cast<mirror::ArtField*>( |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1978 | GetClassRoot(kJavaLangReflectArtField)->AllocNonMovableObject(self)); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1979 | } |
| 1980 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1981 | mirror::ArtMethod* ClassLinker::AllocArtMethod(Thread* self) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1982 | return down_cast<mirror::ArtMethod*>( |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1983 | GetClassRoot(kJavaLangReflectArtMethod)->AllocNonMovableObject(self)); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 1984 | } |
| 1985 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1986 | mirror::ObjectArray<mirror::StackTraceElement>* ClassLinker::AllocStackTraceElementArray( |
| 1987 | Thread* self, size_t length) { |
| 1988 | return mirror::ObjectArray<mirror::StackTraceElement>::Alloc( |
| 1989 | self, GetClassRoot(kJavaLangStackTraceElementArrayClass), length); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1990 | } |
| 1991 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1992 | mirror::Class* ClassLinker::EnsureResolved(Thread* self, const char* descriptor, |
| 1993 | mirror::Class* klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1994 | DCHECK(klass != nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1995 | |
| 1996 | // For temporary classes we must wait for them to be retired. |
| 1997 | if (init_done_ && klass->IsTemp()) { |
| 1998 | CHECK(!klass->IsResolved()); |
| 1999 | if (klass->IsErroneous()) { |
| 2000 | ThrowEarlierClassFailure(klass); |
| 2001 | return nullptr; |
| 2002 | } |
| 2003 | StackHandleScope<1> hs(self); |
| 2004 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 2005 | ObjectLock<mirror::Class> lock(self, h_class); |
| 2006 | // Loop and wait for the resolving thread to retire this class. |
| 2007 | while (!h_class->IsRetired() && !h_class->IsErroneous()) { |
| 2008 | lock.WaitIgnoringInterrupts(); |
| 2009 | } |
| 2010 | if (h_class->IsErroneous()) { |
| 2011 | ThrowEarlierClassFailure(h_class.Get()); |
| 2012 | return nullptr; |
| 2013 | } |
| 2014 | CHECK(h_class->IsRetired()); |
| 2015 | // Get the updated class from class table. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2016 | klass = LookupClass(self, descriptor, ComputeModifiedUtf8Hash(descriptor), |
| 2017 | h_class.Get()->GetClassLoader()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2018 | } |
| 2019 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2020 | // Wait for the class if it has not already been linked. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 2021 | if (!klass->IsResolved() && !klass->IsErroneous()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2022 | StackHandleScope<1> hs(self); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 2023 | HandleWrapper<mirror::Class> h_class(hs.NewHandleWrapper(&klass)); |
| 2024 | ObjectLock<mirror::Class> lock(self, h_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2025 | // Check for circular dependencies between classes. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2026 | if (!h_class->IsResolved() && h_class->GetClinitThreadId() == self->GetTid()) { |
| 2027 | ThrowClassCircularityError(h_class.Get()); |
| 2028 | h_class->SetStatus(mirror::Class::kStatusError, self); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2029 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2030 | } |
| 2031 | // Wait for the pending initialization to complete. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2032 | while (!h_class->IsResolved() && !h_class->IsErroneous()) { |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 2033 | lock.WaitIgnoringInterrupts(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2034 | } |
| 2035 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2036 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2037 | if (klass->IsErroneous()) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2038 | ThrowEarlierClassFailure(klass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2039 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2040 | } |
| 2041 | // Return the loaded class. No exceptions should be pending. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2042 | CHECK(klass->IsResolved()) << PrettyClass(klass); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2043 | self->AssertNoPendingException(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2044 | return klass; |
| 2045 | } |
| 2046 | |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2047 | typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry; |
| 2048 | |
| 2049 | // Search a collection of DexFiles for a descriptor |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2050 | ClassPathEntry FindInClassPath(const char* descriptor, |
| 2051 | size_t hash, const std::vector<const DexFile*>& class_path) { |
| 2052 | for (const DexFile* dex_file : class_path) { |
| 2053 | const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2054 | if (dex_class_def != nullptr) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2055 | return ClassPathEntry(dex_file, dex_class_def); |
| 2056 | } |
| 2057 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2058 | return ClassPathEntry(nullptr, nullptr); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2059 | } |
| 2060 | |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2061 | mirror::Class* ClassLinker::FindClassInPathClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
| 2062 | Thread* self, const char* descriptor, |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2063 | size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2064 | Handle<mirror::ClassLoader> class_loader) { |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2065 | // 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] | 2066 | if (class_loader->GetClass() != |
| 2067 | soa.Decode<mirror::Class*>(WellKnownClasses::dalvik_system_PathClassLoader) || |
| 2068 | class_loader->GetParent()->GetClass() != |
| 2069 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_BootClassLoader)) { |
| 2070 | return nullptr; |
| 2071 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2072 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2073 | // Check if this would be found in the parent boot class loader. |
| 2074 | if (pair.second != nullptr) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2075 | mirror::Class* klass = LookupClass(self, descriptor, hash, nullptr); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2076 | if (klass != nullptr) { |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2077 | // May return null if resolution on another thread fails. |
| 2078 | klass = EnsureResolved(self, descriptor, klass); |
| 2079 | } else { |
| 2080 | // May OOME. |
| 2081 | klass = DefineClass(self, descriptor, hash, NullHandle<mirror::ClassLoader>(), *pair.first, |
| 2082 | *pair.second); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2083 | } |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2084 | if (klass == nullptr) { |
| 2085 | CHECK(self->IsExceptionPending()) << descriptor; |
| 2086 | self->ClearException(); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2087 | } |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2088 | return klass; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2089 | } else { |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2090 | // Handle as if this is the child PathClassLoader. |
| 2091 | // Handles as RegisterDexFile may allocate dex caches (and cause thread suspension). |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2092 | StackHandleScope<3> hs(self); |
| 2093 | // The class loader is a PathClassLoader which inherits from BaseDexClassLoader. |
| 2094 | // We need to get the DexPathList and loop through it. |
| 2095 | Handle<mirror::ArtField> cookie_field = |
| 2096 | hs.NewHandle(soa.DecodeField(WellKnownClasses::dalvik_system_DexFile_cookie)); |
| 2097 | Handle<mirror::ArtField> dex_file_field = |
| 2098 | hs.NewHandle( |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 2099 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile)); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2100 | mirror::Object* dex_path_list = |
| 2101 | soa.DecodeField(WellKnownClasses::dalvik_system_PathClassLoader_pathList)-> |
| 2102 | GetObject(class_loader.Get()); |
| 2103 | if (dex_path_list != nullptr && dex_file_field.Get() != nullptr && |
| 2104 | cookie_field.Get() != nullptr) { |
| 2105 | // DexPathList has an array dexElements of Elements[] which each contain a dex file. |
| 2106 | mirror::Object* dex_elements_obj = |
| 2107 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList_dexElements)-> |
| 2108 | GetObject(dex_path_list); |
| 2109 | // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look |
| 2110 | // at the mCookie which is a DexFile vector. |
| 2111 | if (dex_elements_obj != nullptr) { |
| 2112 | Handle<mirror::ObjectArray<mirror::Object>> dex_elements = |
| 2113 | hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>()); |
| 2114 | for (int32_t i = 0; i < dex_elements->GetLength(); ++i) { |
| 2115 | mirror::Object* element = dex_elements->GetWithoutChecks(i); |
| 2116 | if (element == nullptr) { |
| 2117 | // Should never happen, fall back to java code to throw a NPE. |
| 2118 | break; |
| 2119 | } |
| 2120 | mirror::Object* dex_file = dex_file_field->GetObject(element); |
| 2121 | if (dex_file != nullptr) { |
| 2122 | const uint64_t cookie = cookie_field->GetLong(dex_file); |
| 2123 | auto* dex_files = |
| 2124 | reinterpret_cast<std::vector<const DexFile*>*>(static_cast<uintptr_t>(cookie)); |
| 2125 | if (dex_files == nullptr) { |
| 2126 | // This should never happen so log a warning. |
| 2127 | LOG(WARNING) << "Null DexFile::mCookie for " << descriptor; |
| 2128 | break; |
| 2129 | } |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2130 | for (const DexFile* cp_dex_file : *dex_files) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2131 | const DexFile::ClassDef* dex_class_def = cp_dex_file->FindClassDef(descriptor, hash); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2132 | if (dex_class_def != nullptr) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2133 | RegisterDexFile(*cp_dex_file); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2134 | mirror::Class* klass = DefineClass(self, descriptor, hash, class_loader, |
| 2135 | *cp_dex_file, *dex_class_def); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2136 | if (klass == nullptr) { |
| 2137 | CHECK(self->IsExceptionPending()) << descriptor; |
| 2138 | self->ClearException(); |
| 2139 | return nullptr; |
| 2140 | } |
| 2141 | return klass; |
| 2142 | } |
| 2143 | } |
| 2144 | } |
| 2145 | } |
| 2146 | } |
| 2147 | } |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2148 | self->AssertNoPendingException(); |
| 2149 | return nullptr; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2150 | } |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2151 | } |
| 2152 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2153 | mirror::Class* ClassLinker::FindClass(Thread* self, const char* descriptor, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2154 | Handle<mirror::ClassLoader> class_loader) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 2155 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2156 | DCHECK(self != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2157 | self->AssertNoPendingException(); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 2158 | if (descriptor[1] == '\0') { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2159 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 2160 | // for primitive classes that aren't backed by dex files. |
| 2161 | return FindPrimitiveClass(descriptor[0]); |
| 2162 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2163 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2164 | // Find the class in the loaded classes table. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2165 | mirror::Class* klass = LookupClass(self, descriptor, hash, class_loader.Get()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2166 | if (klass != nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2167 | return EnsureResolved(self, descriptor, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2168 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2169 | // Class is not yet loaded. |
| 2170 | if (descriptor[0] == '[') { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2171 | return CreateArrayClass(self, descriptor, hash, class_loader); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2172 | } else if (class_loader.Get() == nullptr) { |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2173 | // The boot class loader, search the boot class path. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2174 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2175 | if (pair.second != nullptr) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2176 | return DefineClass(self, descriptor, hash, NullHandle<mirror::ClassLoader>(), *pair.first, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2177 | *pair.second); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2178 | } else { |
| 2179 | // The boot class loader is searched ahead of the application class loader, failures are |
| 2180 | // expected and will be wrapped in a ClassNotFoundException. Use the pre-allocated error to |
| 2181 | // trigger the chaining with a proper stack trace. |
| 2182 | mirror::Throwable* pre_allocated = Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 2183 | self->SetException(ThrowLocation(), pre_allocated); |
| 2184 | return nullptr; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2185 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 2186 | } else if (Runtime::Current()->UseCompileTimeClassPath()) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2187 | // First try with the bootstrap class loader. |
| 2188 | if (class_loader.Get() != nullptr) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2189 | klass = LookupClass(self, descriptor, hash, nullptr); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2190 | if (klass != nullptr) { |
| 2191 | return EnsureResolved(self, descriptor, klass); |
| 2192 | } |
| 2193 | } |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2194 | // If the lookup failed search the boot class path. We don't perform a recursive call to avoid |
| 2195 | // a NoClassDefFoundError being allocated. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2196 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2197 | if (pair.second != nullptr) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2198 | return DefineClass(self, descriptor, hash, NullHandle<mirror::ClassLoader>(), *pair.first, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2199 | *pair.second); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2200 | } |
Ian Rogers | be7149f | 2013-08-20 09:29:39 -0700 | [diff] [blame] | 2201 | // Next try the compile time class path. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2202 | const std::vector<const DexFile*>* class_path; |
| 2203 | { |
Ian Rogers | be7149f | 2013-08-20 09:29:39 -0700 | [diff] [blame] | 2204 | ScopedObjectAccessUnchecked soa(self); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2205 | ScopedLocalRef<jobject> jclass_loader(soa.Env(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2206 | soa.AddLocalReference<jobject>(class_loader.Get())); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2207 | class_path = &Runtime::Current()->GetCompileTimeClassPath(jclass_loader.get()); |
| 2208 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2209 | pair = FindInClassPath(descriptor, hash, *class_path); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2210 | if (pair.second != nullptr) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2211 | return DefineClass(self, descriptor, hash, class_loader, *pair.first, *pair.second); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2212 | } else { |
| 2213 | // Use the pre-allocated NCDFE at compile time to avoid wasting time constructing exceptions. |
| 2214 | mirror::Throwable* pre_allocated = Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 2215 | self->SetException(ThrowLocation(), pre_allocated); |
| 2216 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2217 | } |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2218 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2219 | ScopedObjectAccessUnchecked soa(self); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2220 | mirror::Class* cp_klass = FindClassInPathClassLoader(soa, self, descriptor, hash, |
| 2221 | class_loader); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2222 | if (cp_klass != nullptr) { |
| 2223 | return cp_klass; |
Mathieu Chartier | 6bcae8f | 2014-09-04 18:33:17 -0700 | [diff] [blame] | 2224 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2225 | ScopedLocalRef<jobject> class_loader_object(soa.Env(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2226 | soa.AddLocalReference<jobject>(class_loader.Get())); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 2227 | std::string class_name_string(DescriptorToDot(descriptor)); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2228 | ScopedLocalRef<jobject> result(soa.Env(), nullptr); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 2229 | { |
| 2230 | ScopedThreadStateChange tsc(self, kNative); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2231 | ScopedLocalRef<jobject> class_name_object(soa.Env(), |
| 2232 | soa.Env()->NewStringUTF(class_name_string.c_str())); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2233 | if (class_name_object.get() == nullptr) { |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2234 | DCHECK(self->IsExceptionPending()); // OOME. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2235 | return nullptr; |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 2236 | } |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2237 | CHECK(class_loader_object.get() != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2238 | result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(), |
| 2239 | WellKnownClasses::java_lang_ClassLoader_loadClass, |
| 2240 | class_name_object.get())); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2241 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2242 | if (self->IsExceptionPending()) { |
Elliott Hughes | 748382f | 2012-01-26 18:07:38 -0800 | [diff] [blame] | 2243 | // If the ClassLoader threw, pass that exception up. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2244 | return nullptr; |
| 2245 | } else if (result.get() == nullptr) { |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 2246 | // broken loader - throw NPE to be compatible with Dalvik |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2247 | ThrowNullPointerException(nullptr, StringPrintf("ClassLoader.loadClass returned null for %s", |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2248 | class_name_string.c_str()).c_str()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2249 | return nullptr; |
Ian Rogers | 761bfa8 | 2012-01-11 10:14:05 -0800 | [diff] [blame] | 2250 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2251 | // success, return mirror::Class* |
| 2252 | return soa.Decode<mirror::Class*>(result.get()); |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 2253 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2254 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 2255 | UNREACHABLE(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2256 | } |
| 2257 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2258 | mirror::Class* ClassLinker::DefineClass(Thread* self, const char* descriptor, size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2259 | Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2260 | const DexFile& dex_file, |
| 2261 | const DexFile::ClassDef& dex_class_def) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2262 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2263 | auto klass = hs.NewHandle<mirror::Class>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2264 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2265 | // Load the class from the dex file. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2266 | if (UNLIKELY(!init_done_)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2267 | // finish up init of hand crafted class_roots_ |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2268 | if (strcmp(descriptor, "Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2269 | klass.Assign(GetClassRoot(kJavaLangObject)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2270 | } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2271 | klass.Assign(GetClassRoot(kJavaLangClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2272 | } else if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2273 | klass.Assign(GetClassRoot(kJavaLangString)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 2274 | } else if (strcmp(descriptor, "Ljava/lang/ref/Reference;") == 0) { |
| 2275 | klass.Assign(GetClassRoot(kJavaLangRefReference)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2276 | } else if (strcmp(descriptor, "Ljava/lang/DexCache;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2277 | klass.Assign(GetClassRoot(kJavaLangDexCache)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2278 | } else if (strcmp(descriptor, "Ljava/lang/reflect/ArtField;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2279 | klass.Assign(GetClassRoot(kJavaLangReflectArtField)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2280 | } else if (strcmp(descriptor, "Ljava/lang/reflect/ArtMethod;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2281 | klass.Assign(GetClassRoot(kJavaLangReflectArtMethod)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2282 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2283 | } |
| 2284 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2285 | if (klass.Get() == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2286 | // Allocate a class with the status of not ready. |
| 2287 | // Interface object should get the right size here. Regular class will |
| 2288 | // figure out the right size later and be replaced with one of the right |
| 2289 | // size when the class becomes resolved. |
| 2290 | klass.Assign(AllocClass(self, SizeOfClassWithoutEmbeddedTables(dex_file, dex_class_def))); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2291 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2292 | if (UNLIKELY(klass.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2293 | CHECK(self->IsExceptionPending()); // Expect an OOME. |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2294 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2295 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2296 | klass->SetDexCache(FindDexCache(dex_file)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2297 | LoadClass(self, dex_file, dex_class_def, klass, class_loader.Get()); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 2298 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2299 | if (self->IsExceptionPending()) { |
| 2300 | // An exception occured during load, set status to erroneous while holding klass' lock in case |
| 2301 | // notification is necessary. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2302 | if (!klass->IsErroneous()) { |
| 2303 | klass->SetStatus(mirror::Class::kStatusError, self); |
| 2304 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2305 | return nullptr; |
| 2306 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2307 | klass->SetClinitThreadId(self->GetTid()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2308 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2309 | // Add the newly loaded class to the loaded classes table. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2310 | mirror::Class* existing = InsertClass(descriptor, klass.Get(), hash); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2311 | if (existing != nullptr) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2312 | // We failed to insert because we raced with another thread. Calling EnsureResolved may cause |
| 2313 | // this thread to block. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2314 | return EnsureResolved(self, descriptor, existing); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2315 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2316 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2317 | // Finish loading (if necessary) by finding parents |
| 2318 | CHECK(!klass->IsLoaded()); |
| 2319 | if (!LoadSuperAndInterfaces(klass, dex_file)) { |
| 2320 | // Loading failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2321 | if (!klass->IsErroneous()) { |
| 2322 | klass->SetStatus(mirror::Class::kStatusError, self); |
| 2323 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2324 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2325 | } |
| 2326 | CHECK(klass->IsLoaded()); |
| 2327 | // Link the class (if necessary) |
| 2328 | CHECK(!klass->IsResolved()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2329 | // TODO: Use fast jobjects? |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2330 | auto interfaces = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2331 | |
| 2332 | mirror::Class* new_class = nullptr; |
| 2333 | if (!LinkClass(self, descriptor, klass, interfaces, &new_class)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2334 | // Linking failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2335 | if (!klass->IsErroneous()) { |
| 2336 | klass->SetStatus(mirror::Class::kStatusError, self); |
| 2337 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2338 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2339 | } |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 2340 | self->AssertNoPendingException(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2341 | CHECK(new_class != nullptr) << descriptor; |
| 2342 | CHECK(new_class->IsResolved()) << descriptor; |
| 2343 | |
| 2344 | Handle<mirror::Class> new_class_h(hs.NewHandle(new_class)); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2345 | |
| 2346 | /* |
| 2347 | * We send CLASS_PREPARE events to the debugger from here. The |
| 2348 | * definition of "preparation" is creating the static fields for a |
| 2349 | * class and initializing them to the standard default values, but not |
| 2350 | * executing any code (that comes later, during "initialization"). |
| 2351 | * |
| 2352 | * We did the static preparation in LinkClass. |
| 2353 | * |
| 2354 | * The class has been prepared and resolved but possibly not yet verified |
| 2355 | * at this point. |
| 2356 | */ |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2357 | Dbg::PostClassPrepare(new_class_h.Get()); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2358 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2359 | return new_class_h.Get(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2360 | } |
| 2361 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2362 | uint32_t ClassLinker::SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, |
| 2363 | const DexFile::ClassDef& dex_class_def) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2364 | const uint8_t* class_data = dex_file.GetClassData(dex_class_def); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2365 | size_t num_ref = 0; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2366 | size_t num_8 = 0; |
| 2367 | size_t num_16 = 0; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2368 | size_t num_32 = 0; |
| 2369 | size_t num_64 = 0; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2370 | if (class_data != nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2371 | for (ClassDataItemIterator it(dex_file, class_data); it.HasNextStaticField(); it.Next()) { |
| 2372 | const DexFile::FieldId& field_id = dex_file.GetFieldId(it.GetMemberIndex()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2373 | const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2374 | char c = descriptor[0]; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2375 | switch (c) { |
| 2376 | case 'L': |
| 2377 | case '[': |
| 2378 | num_ref++; |
| 2379 | break; |
| 2380 | case 'J': |
| 2381 | case 'D': |
| 2382 | num_64++; |
| 2383 | break; |
| 2384 | case 'I': |
| 2385 | case 'F': |
| 2386 | num_32++; |
| 2387 | break; |
| 2388 | case 'S': |
| 2389 | case 'C': |
| 2390 | num_16++; |
| 2391 | break; |
| 2392 | case 'B': |
| 2393 | case 'Z': |
| 2394 | num_8++; |
| 2395 | break; |
| 2396 | default: |
| 2397 | LOG(FATAL) << "Unknown descriptor: " << c; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 2398 | UNREACHABLE(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2399 | } |
| 2400 | } |
| 2401 | } |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2402 | 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] | 2403 | } |
| 2404 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2405 | OatFile::OatClass ClassLinker::FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, |
| 2406 | bool* found) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2407 | DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 2408 | const OatFile::OatDexFile* oat_dex_file = FindOpenedOatDexFileForDexFile(dex_file); |
| 2409 | if (oat_dex_file == nullptr) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2410 | *found = false; |
| 2411 | return OatFile::OatClass::Invalid(); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2412 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2413 | *found = true; |
| 2414 | return oat_dex_file->GetOatClass(class_def_idx); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2415 | } |
| 2416 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2417 | static uint32_t GetOatMethodIndexFromMethodIndex(const DexFile& dex_file, uint16_t class_def_idx, |
| 2418 | uint32_t method_idx) { |
| 2419 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_idx); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2420 | const uint8_t* class_data = dex_file.GetClassData(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2421 | CHECK(class_data != nullptr); |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2422 | ClassDataItemIterator it(dex_file, class_data); |
| 2423 | // Skip fields |
| 2424 | while (it.HasNextStaticField()) { |
| 2425 | it.Next(); |
| 2426 | } |
| 2427 | while (it.HasNextInstanceField()) { |
| 2428 | it.Next(); |
| 2429 | } |
| 2430 | // Process methods |
| 2431 | size_t class_def_method_index = 0; |
| 2432 | while (it.HasNextDirectMethod()) { |
| 2433 | if (it.GetMemberIndex() == method_idx) { |
| 2434 | return class_def_method_index; |
| 2435 | } |
| 2436 | class_def_method_index++; |
| 2437 | it.Next(); |
| 2438 | } |
| 2439 | while (it.HasNextVirtualMethod()) { |
| 2440 | if (it.GetMemberIndex() == method_idx) { |
| 2441 | return class_def_method_index; |
| 2442 | } |
| 2443 | class_def_method_index++; |
| 2444 | it.Next(); |
| 2445 | } |
| 2446 | DCHECK(!it.HasNext()); |
| 2447 | 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] | 2448 | UNREACHABLE(); |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2449 | } |
| 2450 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2451 | const OatFile::OatMethod ClassLinker::FindOatMethodFor(mirror::ArtMethod* method, bool* found) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2452 | // 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] | 2453 | // method for direct methods (or virtual methods made direct). |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2454 | mirror::Class* declaring_class = method->GetDeclaringClass(); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2455 | size_t oat_method_index; |
| 2456 | if (method->IsStatic() || method->IsDirect()) { |
| 2457 | // Simple case where the oat method index was stashed at load time. |
| 2458 | oat_method_index = method->GetMethodIndex(); |
| 2459 | } else { |
| 2460 | // We're invoking a virtual method directly (thanks to sharpening), compute the oat_method_index |
| 2461 | // by search for its position in the declared virtual methods. |
| 2462 | oat_method_index = declaring_class->NumDirectMethods(); |
| 2463 | size_t end = declaring_class->NumVirtualMethods(); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2464 | bool found_virtual = false; |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2465 | for (size_t i = 0; i < end; i++) { |
Jeff Hao | 68caf9e | 2014-09-03 13:48:16 -0700 | [diff] [blame] | 2466 | // Check method index instead of identity in case of duplicate method definitions. |
| 2467 | if (method->GetDexMethodIndex() == |
| 2468 | declaring_class->GetVirtualMethod(i)->GetDexMethodIndex()) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2469 | found_virtual = true; |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2470 | break; |
| 2471 | } |
Ian Rogers | f320b63 | 2012-03-13 18:47:47 -0700 | [diff] [blame] | 2472 | oat_method_index++; |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2473 | } |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2474 | CHECK(found_virtual) << "Didn't find oat method index for virtual method: " |
| 2475 | << PrettyMethod(method); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2476 | } |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2477 | DCHECK_EQ(oat_method_index, |
| 2478 | GetOatMethodIndexFromMethodIndex(*declaring_class->GetDexCache()->GetDexFile(), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2479 | method->GetDeclaringClass()->GetDexClassDefIndex(), |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2480 | method->GetDexMethodIndex())); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2481 | OatFile::OatClass oat_class = FindOatClass(*declaring_class->GetDexCache()->GetDexFile(), |
| 2482 | declaring_class->GetDexClassDefIndex(), |
| 2483 | found); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2484 | if (!(*found)) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2485 | return OatFile::OatMethod::Invalid(); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2486 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2487 | return oat_class.GetOatMethod(oat_method_index); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2488 | } |
| 2489 | |
| 2490 | // Special case to get oat code without overwriting a trampoline. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2491 | const void* ClassLinker::GetQuickOatCodeFor(mirror::ArtMethod* method) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2492 | CHECK(!method->IsAbstract()) << PrettyMethod(method); |
Jeff Hao | 8df6cea | 2013-07-29 13:54:48 -0700 | [diff] [blame] | 2493 | if (method->IsProxyMethod()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2494 | return GetQuickProxyInvokeHandler(); |
Jeff Hao | 8df6cea | 2013-07-29 13:54:48 -0700 | [diff] [blame] | 2495 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2496 | bool found; |
| 2497 | OatFile::OatMethod oat_method = FindOatMethodFor(method, &found); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2498 | const void* result = nullptr; |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2499 | if (found) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2500 | result = oat_method.GetQuickCode(); |
| 2501 | } |
| 2502 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2503 | if (result == nullptr) { |
Ian Rogers | 1a57066 | 2014-03-12 01:02:21 -0700 | [diff] [blame] | 2504 | if (method->IsNative()) { |
| 2505 | // No code and native? Use generic trampoline. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2506 | result = GetQuickGenericJniStub(); |
Ian Rogers | 1a57066 | 2014-03-12 01:02:21 -0700 | [diff] [blame] | 2507 | } else if (method->IsPortableCompiled()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2508 | // No code? Do we expect portable code? |
| 2509 | result = GetQuickToPortableBridge(); |
| 2510 | } else { |
| 2511 | // No code? You must mean to go into the interpreter. |
| 2512 | result = GetQuickToInterpreterBridge(); |
| 2513 | } |
Ian Rogers | f3e9855 | 2013-03-20 15:49:49 -0700 | [diff] [blame] | 2514 | } |
| 2515 | return result; |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2516 | } |
| 2517 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2518 | const void* ClassLinker::GetPortableOatCodeFor(mirror::ArtMethod* method, |
| 2519 | bool* have_portable_code) { |
| 2520 | CHECK(!method->IsAbstract()) << PrettyMethod(method); |
| 2521 | *have_portable_code = false; |
| 2522 | if (method->IsProxyMethod()) { |
| 2523 | return GetPortableProxyInvokeHandler(); |
| 2524 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2525 | bool found; |
| 2526 | OatFile::OatMethod oat_method = FindOatMethodFor(method, &found); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2527 | const void* result = nullptr; |
| 2528 | const void* quick_code = nullptr; |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2529 | if (found) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2530 | result = oat_method.GetPortableCode(); |
| 2531 | quick_code = oat_method.GetQuickCode(); |
| 2532 | } |
| 2533 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2534 | if (result == nullptr) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2535 | if (quick_code == nullptr) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2536 | // No code? You must mean to go into the interpreter. |
| 2537 | result = GetPortableToInterpreterBridge(); |
| 2538 | } else { |
| 2539 | // No code? But there's quick code, so use a bridge. |
| 2540 | result = GetPortableToQuickBridge(); |
| 2541 | } |
| 2542 | } else { |
| 2543 | *have_portable_code = true; |
| 2544 | } |
| 2545 | return result; |
| 2546 | } |
| 2547 | |
Hiroshi Yamauchi | 9bdec88 | 2014-08-15 17:11:12 -0700 | [diff] [blame] | 2548 | const void* ClassLinker::GetOatMethodQuickCodeFor(mirror::ArtMethod* method) { |
| 2549 | if (method->IsNative() || method->IsAbstract() || method->IsProxyMethod()) { |
| 2550 | return nullptr; |
| 2551 | } |
| 2552 | bool found; |
| 2553 | OatFile::OatMethod oat_method = FindOatMethodFor(method, &found); |
| 2554 | return found ? oat_method.GetQuickCode() : nullptr; |
| 2555 | } |
| 2556 | |
| 2557 | const void* ClassLinker::GetOatMethodPortableCodeFor(mirror::ArtMethod* method) { |
| 2558 | if (method->IsNative() || method->IsAbstract() || method->IsProxyMethod()) { |
| 2559 | return nullptr; |
| 2560 | } |
| 2561 | bool found; |
| 2562 | OatFile::OatMethod oat_method = FindOatMethodFor(method, &found); |
| 2563 | return found ? oat_method.GetPortableCode() : nullptr; |
| 2564 | } |
| 2565 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2566 | const void* ClassLinker::GetQuickOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, |
| 2567 | uint32_t method_idx) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2568 | bool found; |
| 2569 | OatFile::OatClass oat_class = FindOatClass(dex_file, class_def_idx, &found); |
| 2570 | if (!found) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2571 | return nullptr; |
| 2572 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2573 | 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] | 2574 | return oat_class.GetOatMethod(oat_method_idx).GetQuickCode(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2575 | } |
| 2576 | |
| 2577 | const void* ClassLinker::GetPortableOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, |
| 2578 | uint32_t method_idx) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2579 | bool found; |
| 2580 | OatFile::OatClass oat_class = FindOatClass(dex_file, class_def_idx, &found); |
| 2581 | if (!found) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2582 | return nullptr; |
| 2583 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2584 | 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] | 2585 | return oat_class.GetOatMethod(oat_method_idx).GetPortableCode(); |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2586 | } |
| 2587 | |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2588 | // Returns true if the method must run with interpreter, false otherwise. |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 2589 | static bool NeedsInterpreter( |
| 2590 | mirror::ArtMethod* method, const void* quick_code, const void* portable_code) |
| 2591 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2592 | if ((quick_code == nullptr) && (portable_code == nullptr)) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2593 | // No code: need interpreter. |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 2594 | // May return true for native code, in the case of generic JNI |
| 2595 | // DCHECK(!method->IsNative()); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2596 | return true; |
| 2597 | } |
Dragos Sbirlea | 90af14d | 2013-08-15 17:50:16 -0700 | [diff] [blame] | 2598 | #ifdef ART_SEA_IR_MODE |
| 2599 | ScopedObjectAccess soa(Thread::Current()); |
| 2600 | if (std::string::npos != PrettyMethod(method).find("fibonacci")) { |
| 2601 | LOG(INFO) << "Found " << PrettyMethod(method); |
| 2602 | return false; |
| 2603 | } |
| 2604 | #endif |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2605 | // If interpreter mode is enabled, every method (except native and proxy) must |
| 2606 | // be run with interpreter. |
| 2607 | return Runtime::Current()->GetInstrumentation()->InterpretOnly() && |
| 2608 | !method->IsNative() && !method->IsProxyMethod(); |
| 2609 | } |
| 2610 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2611 | void ClassLinker::FixupStaticTrampolines(mirror::Class* klass) { |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 2612 | DCHECK(klass->IsInitialized()) << PrettyDescriptor(klass); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2613 | if (klass->NumDirectMethods() == 0) { |
| 2614 | return; // No direct methods => no static methods. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2615 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2616 | Runtime* runtime = Runtime::Current(); |
| 2617 | if (!runtime->IsStarted() || runtime->UseCompileTimeClassPath()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 2618 | if (runtime->IsCompiler() || runtime->GetHeap()->HasImageSpace()) { |
| 2619 | return; // OAT file unavailable. |
| 2620 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2621 | } |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 2622 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 2623 | const DexFile& dex_file = klass->GetDexFile(); |
| 2624 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2625 | CHECK(dex_class_def != nullptr); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2626 | const uint8_t* class_data = dex_file.GetClassData(*dex_class_def); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2627 | // There should always be class data if there were direct methods. |
| 2628 | CHECK(class_data != nullptr) << PrettyDescriptor(klass); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2629 | ClassDataItemIterator it(dex_file, class_data); |
| 2630 | // Skip fields |
| 2631 | while (it.HasNextStaticField()) { |
| 2632 | it.Next(); |
| 2633 | } |
| 2634 | while (it.HasNextInstanceField()) { |
| 2635 | it.Next(); |
| 2636 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2637 | bool has_oat_class; |
| 2638 | OatFile::OatClass oat_class = FindOatClass(dex_file, klass->GetDexClassDefIndex(), |
| 2639 | &has_oat_class); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2640 | // Link the code of methods skipped by LinkCode. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2641 | for (size_t method_index = 0; it.HasNextDirectMethod(); ++method_index, it.Next()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2642 | mirror::ArtMethod* method = klass->GetDirectMethod(method_index); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2643 | if (!method->IsStatic()) { |
| 2644 | // Only update static methods. |
| 2645 | continue; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2646 | } |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2647 | const void* portable_code = nullptr; |
| 2648 | const void* quick_code = nullptr; |
| 2649 | if (has_oat_class) { |
| 2650 | OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index); |
| 2651 | portable_code = oat_method.GetPortableCode(); |
| 2652 | quick_code = oat_method.GetQuickCode(); |
| 2653 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2654 | const bool enter_interpreter = NeedsInterpreter(method, quick_code, portable_code); |
| 2655 | bool have_portable_code = false; |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2656 | if (enter_interpreter) { |
| 2657 | // Use interpreter entry point. |
Ian Rogers | 1a57066 | 2014-03-12 01:02:21 -0700 | [diff] [blame] | 2658 | // Check whether the method is native, in which case it's generic JNI. |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 2659 | if (quick_code == nullptr && portable_code == nullptr && method->IsNative()) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2660 | quick_code = GetQuickGenericJniStub(); |
Ian Rogers | 1a57066 | 2014-03-12 01:02:21 -0700 | [diff] [blame] | 2661 | portable_code = GetPortableToQuickBridge(); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 2662 | } else { |
Ian Rogers | 1a57066 | 2014-03-12 01:02:21 -0700 | [diff] [blame] | 2663 | portable_code = GetPortableToInterpreterBridge(); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 2664 | quick_code = GetQuickToInterpreterBridge(); |
| 2665 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2666 | } else { |
| 2667 | if (portable_code == nullptr) { |
| 2668 | portable_code = GetPortableToQuickBridge(); |
| 2669 | } else { |
| 2670 | have_portable_code = true; |
| 2671 | } |
| 2672 | if (quick_code == nullptr) { |
| 2673 | quick_code = GetQuickToPortableBridge(); |
| 2674 | } |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2675 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2676 | runtime->GetInstrumentation()->UpdateMethodsCode(method, quick_code, portable_code, |
| 2677 | have_portable_code); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2678 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2679 | // Ignore virtual methods on the iterator. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2680 | } |
| 2681 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2682 | void ClassLinker::LinkCode(Handle<mirror::ArtMethod> method, |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 2683 | const OatFile::OatClass* oat_class, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2684 | uint32_t class_def_method_index) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2685 | Runtime* runtime = Runtime::Current(); |
| 2686 | if (runtime->IsCompiler()) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2687 | // The following code only applies to a non-compiler runtime. |
| 2688 | return; |
| 2689 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2690 | // Method shouldn't have already been linked. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2691 | DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr); |
| 2692 | DCHECK(method->GetEntryPointFromPortableCompiledCode() == nullptr); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2693 | if (oat_class != nullptr) { |
| 2694 | // Every kind of method should at least get an invoke stub from the oat_method. |
| 2695 | // non-abstract methods also get their code pointers. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2696 | const OatFile::OatMethod oat_method = oat_class->GetOatMethod(class_def_method_index); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2697 | oat_method.LinkMethod(method.Get()); |
| 2698 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2699 | |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 2700 | // Install entry point from interpreter. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2701 | bool enter_interpreter = NeedsInterpreter(method.Get(), |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2702 | method->GetEntryPointFromQuickCompiledCode(), |
| 2703 | method->GetEntryPointFromPortableCompiledCode()); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2704 | if (enter_interpreter && !method->IsNative()) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2705 | method->SetEntryPointFromInterpreter(artInterpreterToInterpreterBridge); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 2706 | } else { |
Dragos Sbirlea | 08bf196 | 2013-08-12 08:53:04 -0700 | [diff] [blame] | 2707 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 2708 | } |
| 2709 | |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2710 | if (method->IsAbstract()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2711 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 2712 | method->SetEntryPointFromPortableCompiledCode(GetPortableToInterpreterBridge()); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2713 | return; |
| 2714 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2715 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2716 | bool have_portable_code = false; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2717 | if (method->IsStatic() && !method->IsConstructor()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2718 | // For static methods excluding the class initializer, install the trampoline. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2719 | // It will be replaced by the proper entry point by ClassLinker::FixupStaticTrampolines |
| 2720 | // after initializing class (see ClassLinker::InitializeClass method). |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2721 | method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionStub()); |
| 2722 | method->SetEntryPointFromPortableCompiledCode(GetPortableResolutionStub()); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2723 | } else if (enter_interpreter) { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2724 | if (!method->IsNative()) { |
| 2725 | // Set entry point from compiled code if there's no code or in interpreter only mode. |
| 2726 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 2727 | method->SetEntryPointFromPortableCompiledCode(GetPortableToInterpreterBridge()); |
| 2728 | } else { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2729 | method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub()); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2730 | method->SetEntryPointFromPortableCompiledCode(GetPortableToQuickBridge()); |
| 2731 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2732 | } else if (method->GetEntryPointFromPortableCompiledCode() != nullptr) { |
| 2733 | DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr); |
| 2734 | have_portable_code = true; |
| 2735 | method->SetEntryPointFromQuickCompiledCode(GetQuickToPortableBridge()); |
| 2736 | } else { |
| 2737 | DCHECK(method->GetEntryPointFromQuickCompiledCode() != nullptr); |
| 2738 | method->SetEntryPointFromPortableCompiledCode(GetPortableToQuickBridge()); |
Ian Rogers | 0d6de04 | 2012-02-29 08:50:26 -0800 | [diff] [blame] | 2739 | } |
jeffhao | 26c0a1a | 2012-01-17 16:28:33 -0800 | [diff] [blame] | 2740 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2741 | if (method->IsNative()) { |
| 2742 | // Unregistering restores the dlsym lookup stub. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2743 | method->UnregisterNative(); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 2744 | |
| 2745 | if (enter_interpreter) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2746 | // We have a native method here without code. Then it should have either the generic JNI |
| 2747 | // trampoline as entrypoint (non-static), or the resolution trampoline (static). |
| 2748 | // TODO: this doesn't handle all the cases where trampolines may be installed. |
| 2749 | const void* entry_point = method->GetEntryPointFromQuickCompiledCode(); |
| 2750 | DCHECK(IsQuickGenericJniStub(entry_point) || IsQuickResolutionStub(entry_point)); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 2751 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2752 | } |
Ian Rogers | f3e9855 | 2013-03-20 15:49:49 -0700 | [diff] [blame] | 2753 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2754 | // Allow instrumentation its chance to hijack code. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2755 | runtime->GetInstrumentation()->UpdateMethodsCode(method.Get(), |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2756 | method->GetEntryPointFromQuickCompiledCode(), |
| 2757 | method->GetEntryPointFromPortableCompiledCode(), |
| 2758 | have_portable_code); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2759 | } |
| 2760 | |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2761 | |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2762 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2763 | void ClassLinker::LoadClass(Thread* self, const DexFile& dex_file, |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 2764 | const DexFile::ClassDef& dex_class_def, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2765 | Handle<mirror::Class> klass, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2766 | mirror::ClassLoader* class_loader) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2767 | CHECK(klass.Get() != nullptr); |
| 2768 | CHECK(klass->GetDexCache() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2769 | CHECK_EQ(mirror::Class::kStatusNotReady, klass->GetStatus()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 2770 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2771 | CHECK(descriptor != nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2772 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2773 | klass->SetClass(GetClassRoot(kJavaLangClass)); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 2774 | if (kUseBakerOrBrooksReadBarrier) { |
| 2775 | klass->AssertReadBarrierPointer(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 2776 | } |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 2777 | uint32_t access_flags = dex_class_def.GetJavaAccessFlags(); |
Brian Carlstrom | 8e3fb14 | 2013-10-09 21:00:27 -0700 | [diff] [blame] | 2778 | CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2779 | klass->SetAccessFlags(access_flags); |
| 2780 | klass->SetClassLoader(class_loader); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2781 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2782 | klass->SetStatus(mirror::Class::kStatusIdx, nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2783 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2784 | klass->SetDexClassDefIndex(dex_file.GetIndexForClassDef(dex_class_def)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2785 | klass->SetDexTypeIndex(dex_class_def.class_idx_); |
Mathieu Chartier | 91a6dc4 | 2014-12-01 10:31:15 -0800 | [diff] [blame] | 2786 | CHECK(klass->GetDexCacheStrings() != nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2787 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2788 | const uint8_t* class_data = dex_file.GetClassData(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2789 | if (class_data == nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2790 | return; // no fields or methods - for example a marker interface |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2791 | } |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2792 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2793 | |
| 2794 | bool has_oat_class = false; |
| 2795 | if (Runtime::Current()->IsStarted() && !Runtime::Current()->UseCompileTimeClassPath()) { |
| 2796 | OatFile::OatClass oat_class = FindOatClass(dex_file, klass->GetDexClassDefIndex(), |
| 2797 | &has_oat_class); |
| 2798 | if (has_oat_class) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2799 | LoadClassMembers(self, dex_file, class_data, klass, &oat_class); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2800 | } |
| 2801 | } |
| 2802 | if (!has_oat_class) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2803 | LoadClassMembers(self, dex_file, class_data, klass, nullptr); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2804 | } |
| 2805 | } |
| 2806 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2807 | void ClassLinker::LoadClassMembers(Thread* self, const DexFile& dex_file, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2808 | const uint8_t* class_data, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2809 | Handle<mirror::Class> klass, |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2810 | const OatFile::OatClass* oat_class) { |
| 2811 | // Load fields. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2812 | ClassDataItemIterator it(dex_file, class_data); |
| 2813 | if (it.NumStaticFields() != 0) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2814 | mirror::ObjectArray<mirror::ArtField>* statics = AllocArtFieldArray(self, it.NumStaticFields()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2815 | if (UNLIKELY(statics == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2816 | CHECK(self->IsExceptionPending()); // OOME. |
| 2817 | return; |
| 2818 | } |
| 2819 | klass->SetSFields(statics); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2820 | } |
| 2821 | if (it.NumInstanceFields() != 0) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2822 | mirror::ObjectArray<mirror::ArtField>* fields = |
| 2823 | AllocArtFieldArray(self, it.NumInstanceFields()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2824 | if (UNLIKELY(fields == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2825 | CHECK(self->IsExceptionPending()); // OOME. |
| 2826 | return; |
| 2827 | } |
| 2828 | klass->SetIFields(fields); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2829 | } |
| 2830 | for (size_t i = 0; it.HasNextStaticField(); i++, it.Next()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2831 | self->AllowThreadSuspension(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2832 | StackHandleScope<1> hs(self); |
| 2833 | Handle<mirror::ArtField> sfield(hs.NewHandle(AllocArtField(self))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2834 | if (UNLIKELY(sfield.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2835 | CHECK(self->IsExceptionPending()); // OOME. |
| 2836 | return; |
| 2837 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2838 | klass->SetStaticField(i, sfield.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2839 | LoadField(dex_file, it, klass, sfield); |
| 2840 | } |
| 2841 | for (size_t i = 0; it.HasNextInstanceField(); i++, it.Next()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2842 | self->AllowThreadSuspension(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2843 | StackHandleScope<1> hs(self); |
| 2844 | Handle<mirror::ArtField> ifield(hs.NewHandle(AllocArtField(self))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2845 | if (UNLIKELY(ifield.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2846 | CHECK(self->IsExceptionPending()); // OOME. |
| 2847 | return; |
| 2848 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2849 | klass->SetInstanceField(i, ifield.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2850 | LoadField(dex_file, it, klass, ifield); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2851 | } |
| 2852 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2853 | // Load methods. |
| 2854 | if (it.NumDirectMethods() != 0) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2855 | // TODO: append direct methods to class object |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2856 | mirror::ObjectArray<mirror::ArtMethod>* directs = |
| 2857 | AllocArtMethodArray(self, it.NumDirectMethods()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2858 | if (UNLIKELY(directs == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2859 | CHECK(self->IsExceptionPending()); // OOME. |
| 2860 | return; |
| 2861 | } |
| 2862 | klass->SetDirectMethods(directs); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2863 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2864 | if (it.NumVirtualMethods() != 0) { |
| 2865 | // TODO: append direct methods to class object |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2866 | mirror::ObjectArray<mirror::ArtMethod>* virtuals = |
| 2867 | AllocArtMethodArray(self, it.NumVirtualMethods()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2868 | if (UNLIKELY(virtuals == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2869 | CHECK(self->IsExceptionPending()); // OOME. |
| 2870 | return; |
| 2871 | } |
| 2872 | klass->SetVirtualMethods(virtuals); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2873 | } |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2874 | size_t class_def_method_index = 0; |
Jeff Hao | 68caf9e | 2014-09-03 13:48:16 -0700 | [diff] [blame] | 2875 | uint32_t last_dex_method_index = DexFile::kDexNoIndex; |
| 2876 | size_t last_class_def_method_index = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2877 | for (size_t i = 0; it.HasNextDirectMethod(); i++, it.Next()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2878 | self->AllowThreadSuspension(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2879 | StackHandleScope<1> hs(self); |
| 2880 | Handle<mirror::ArtMethod> method(hs.NewHandle(LoadMethod(self, dex_file, it, klass))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2881 | if (UNLIKELY(method.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2882 | CHECK(self->IsExceptionPending()); // OOME. |
| 2883 | return; |
| 2884 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2885 | klass->SetDirectMethod(i, method.Get()); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2886 | LinkCode(method, oat_class, class_def_method_index); |
Jeff Hao | 68caf9e | 2014-09-03 13:48:16 -0700 | [diff] [blame] | 2887 | uint32_t it_method_index = it.GetMemberIndex(); |
| 2888 | if (last_dex_method_index == it_method_index) { |
| 2889 | // duplicate case |
| 2890 | method->SetMethodIndex(last_class_def_method_index); |
| 2891 | } else { |
| 2892 | method->SetMethodIndex(class_def_method_index); |
| 2893 | last_dex_method_index = it_method_index; |
| 2894 | last_class_def_method_index = class_def_method_index; |
| 2895 | } |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2896 | class_def_method_index++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2897 | } |
| 2898 | for (size_t i = 0; it.HasNextVirtualMethod(); i++, it.Next()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2899 | self->AllowThreadSuspension(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2900 | StackHandleScope<1> hs(self); |
| 2901 | Handle<mirror::ArtMethod> method(hs.NewHandle(LoadMethod(self, dex_file, it, klass))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2902 | if (UNLIKELY(method.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2903 | CHECK(self->IsExceptionPending()); // OOME. |
| 2904 | return; |
| 2905 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2906 | klass->SetVirtualMethod(i, method.Get()); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2907 | DCHECK_EQ(class_def_method_index, it.NumDirectMethods() + i); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2908 | LinkCode(method, oat_class, class_def_method_index); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2909 | class_def_method_index++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2910 | } |
| 2911 | DCHECK(!it.HasNext()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2912 | } |
| 2913 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 2914 | void ClassLinker::LoadField(const DexFile& /*dex_file*/, const ClassDataItemIterator& it, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2915 | Handle<mirror::Class> klass, |
| 2916 | Handle<mirror::ArtField> dst) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2917 | uint32_t field_idx = it.GetMemberIndex(); |
| 2918 | dst->SetDexFieldIndex(field_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2919 | dst->SetDeclaringClass(klass.Get()); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 2920 | dst->SetAccessFlags(it.GetFieldAccessFlags()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2921 | } |
| 2922 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2923 | mirror::ArtMethod* ClassLinker::LoadMethod(Thread* self, const DexFile& dex_file, |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2924 | const ClassDataItemIterator& it, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2925 | Handle<mirror::Class> klass) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2926 | uint32_t dex_method_idx = it.GetMemberIndex(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2927 | const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2928 | const char* method_name = dex_file.StringDataByIdx(method_id.name_idx_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2929 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2930 | mirror::ArtMethod* dst = AllocArtMethod(self); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2931 | if (UNLIKELY(dst == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2932 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2933 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2934 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2935 | DCHECK(dst->IsArtMethod()) << PrettyDescriptor(dst->GetClass()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2936 | |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 2937 | ScopedAssertNoThreadSuspension ants(self, "LoadMethod"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2938 | dst->SetDexMethodIndex(dex_method_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2939 | dst->SetDeclaringClass(klass.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2940 | dst->SetCodeItemOffset(it.GetMethodCodeItemOffset()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2941 | |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2942 | dst->SetDexCacheResolvedMethods(klass->GetDexCache()->GetResolvedMethods()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2943 | dst->SetDexCacheResolvedTypes(klass->GetDexCache()->GetResolvedTypes()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2944 | |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 2945 | uint32_t access_flags = it.GetMethodAccessFlags(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2946 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2947 | if (UNLIKELY(strcmp("finalize", method_name) == 0)) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2948 | // Set finalizable flag on declaring class. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2949 | if (strcmp("V", dex_file.GetShorty(method_id.proto_idx_)) == 0) { |
| 2950 | // Void return type. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2951 | if (klass->GetClassLoader() != nullptr) { // All non-boot finalizer methods are flagged. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2952 | klass->SetFinalizable(); |
| 2953 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2954 | std::string temp; |
| 2955 | const char* klass_descriptor = klass->GetDescriptor(&temp); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2956 | // The Enum class declares a "final" finalize() method to prevent subclasses from |
| 2957 | // introducing a finalizer. We don't want to set the finalizable flag for Enum or its |
| 2958 | // subclasses, so we exclude it here. |
| 2959 | // We also want to avoid setting the flag on Object, where we know that finalize() is |
| 2960 | // empty. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2961 | if (strcmp(klass_descriptor, "Ljava/lang/Object;") != 0 && |
| 2962 | strcmp(klass_descriptor, "Ljava/lang/Enum;") != 0) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2963 | klass->SetFinalizable(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2964 | } |
| 2965 | } |
| 2966 | } |
| 2967 | } else if (method_name[0] == '<') { |
| 2968 | // Fix broken access flags for initializers. Bug 11157540. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2969 | bool is_init = (strcmp("<init>", method_name) == 0); |
| 2970 | bool is_clinit = !is_init && (strcmp("<clinit>", method_name) == 0); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2971 | if (UNLIKELY(!is_init && !is_clinit)) { |
| 2972 | LOG(WARNING) << "Unexpected '<' at start of method name " << method_name; |
| 2973 | } else { |
| 2974 | if (UNLIKELY((access_flags & kAccConstructor) == 0)) { |
| 2975 | 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] | 2976 | << PrettyDescriptor(klass.Get()) << " in dex file " << dex_file.GetLocation(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2977 | access_flags |= kAccConstructor; |
| 2978 | } |
| 2979 | } |
| 2980 | } |
| 2981 | dst->SetAccessFlags(access_flags); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2982 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2983 | return dst; |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2984 | } |
| 2985 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2986 | void ClassLinker::AppendToBootClassPath(Thread* self, const DexFile& dex_file) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2987 | StackHandleScope<1> hs(self); |
| 2988 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(AllocDexCache(self, dex_file))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2989 | CHECK(dex_cache.Get() != nullptr) << "Failed to allocate dex cache for " |
| 2990 | << dex_file.GetLocation(); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2991 | AppendToBootClassPath(dex_file, dex_cache); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2992 | } |
| 2993 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2994 | void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2995 | Handle<mirror::DexCache> dex_cache) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2996 | CHECK(dex_cache.Get() != nullptr) << dex_file.GetLocation(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2997 | boot_class_path_.push_back(&dex_file); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2998 | RegisterDexFile(dex_file, dex_cache); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2999 | } |
| 3000 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3001 | bool ClassLinker::IsDexFileRegisteredLocked(const DexFile& dex_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3002 | dex_lock_.AssertSharedHeld(Thread::Current()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3003 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3004 | mirror::DexCache* dex_cache = GetDexCache(i); |
| 3005 | if (dex_cache->GetDexFile() == &dex_file) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3006 | return true; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3007 | } |
| 3008 | } |
| 3009 | return false; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 3010 | } |
| 3011 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3012 | bool ClassLinker::IsDexFileRegistered(const DexFile& dex_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3013 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | 0691851 | 2011-10-16 23:39:12 -0700 | [diff] [blame] | 3014 | return IsDexFileRegisteredLocked(dex_file); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3015 | } |
| 3016 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3017 | void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3018 | Handle<mirror::DexCache> dex_cache) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3019 | dex_lock_.AssertExclusiveHeld(Thread::Current()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3020 | CHECK(dex_cache.Get() != nullptr) << dex_file.GetLocation(); |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 3021 | CHECK(dex_cache->GetLocation()->Equals(dex_file.GetLocation())) |
| 3022 | << dex_cache->GetLocation()->ToModifiedUtf8() << " " << dex_file.GetLocation(); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 3023 | dex_caches_.push_back(GcRoot<mirror::DexCache>(dex_cache.Get())); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3024 | dex_cache->SetDexFile(&dex_file); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3025 | if (log_new_dex_caches_roots_) { |
| 3026 | // TODO: This is not safe if we can remove dex caches. |
| 3027 | new_dex_cache_roots_.push_back(dex_caches_.size() - 1); |
| 3028 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3029 | } |
| 3030 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3031 | void ClassLinker::RegisterDexFile(const DexFile& dex_file) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3032 | Thread* self = Thread::Current(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3033 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3034 | ReaderMutexLock mu(self, dex_lock_); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3035 | if (IsDexFileRegisteredLocked(dex_file)) { |
| 3036 | return; |
| 3037 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3038 | } |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3039 | // Don't alloc while holding the lock, since allocation may need to |
| 3040 | // suspend all threads and another thread may need the dex_lock_ to |
| 3041 | // get to a suspend point. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3042 | StackHandleScope<1> hs(self); |
| 3043 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(AllocDexCache(self, dex_file))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3044 | CHECK(dex_cache.Get() != nullptr) << "Failed to allocate dex cache for " |
| 3045 | << dex_file.GetLocation(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3046 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3047 | WriterMutexLock mu(self, dex_lock_); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3048 | if (IsDexFileRegisteredLocked(dex_file)) { |
| 3049 | return; |
| 3050 | } |
| 3051 | RegisterDexFileLocked(dex_file, dex_cache); |
| 3052 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3053 | } |
| 3054 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3055 | void ClassLinker::RegisterDexFile(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3056 | Handle<mirror::DexCache> dex_cache) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3057 | WriterMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3058 | RegisterDexFileLocked(dex_file, dex_cache); |
| 3059 | } |
| 3060 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3061 | mirror::DexCache* ClassLinker::FindDexCache(const DexFile& dex_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3062 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3063 | // Search assuming unique-ness of dex file. |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3064 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3065 | mirror::DexCache* dex_cache = GetDexCache(i); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3066 | if (dex_cache->GetDexFile() == &dex_file) { |
| 3067 | return dex_cache; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3068 | } |
| 3069 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3070 | // Search matching by location name. |
| 3071 | std::string location(dex_file.GetLocation()); |
| 3072 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3073 | mirror::DexCache* dex_cache = GetDexCache(i); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3074 | if (dex_cache->GetDexFile()->GetLocation() == location) { |
| 3075 | return dex_cache; |
| 3076 | } |
| 3077 | } |
| 3078 | // Failure, dump diagnostic and abort. |
| 3079 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3080 | mirror::DexCache* dex_cache = GetDexCache(i); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3081 | LOG(ERROR) << "Registered dex file " << i << " = " << dex_cache->GetDexFile()->GetLocation(); |
| 3082 | } |
| 3083 | LOG(FATAL) << "Failed to find DexCache for DexFile " << location; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 3084 | UNREACHABLE(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3085 | } |
| 3086 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3087 | void ClassLinker::FixupDexCaches(mirror::ArtMethod* resolution_method) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3088 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3089 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3090 | mirror::DexCache* dex_cache = GetDexCache(i); |
| 3091 | dex_cache->Fixup(resolution_method); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3092 | } |
| 3093 | } |
| 3094 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3095 | mirror::Class* ClassLinker::CreatePrimitiveClass(Thread* self, Primitive::Type type) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3096 | mirror::Class* klass = AllocClass(self, mirror::Class::PrimitiveClassSize()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3097 | if (UNLIKELY(klass == nullptr)) { |
| 3098 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3099 | } |
| 3100 | return InitializePrimitiveClass(klass, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3101 | } |
| 3102 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3103 | mirror::Class* ClassLinker::InitializePrimitiveClass(mirror::Class* primitive_class, |
| 3104 | Primitive::Type type) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3105 | CHECK(primitive_class != nullptr); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3106 | // Must hold lock on object when initializing. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3107 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3108 | StackHandleScope<1> hs(self); |
| 3109 | Handle<mirror::Class> h_class(hs.NewHandle(primitive_class)); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3110 | ObjectLock<mirror::Class> lock(self, h_class); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3111 | primitive_class->SetAccessFlags(kAccPublic | kAccFinal | kAccAbstract); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3112 | primitive_class->SetPrimitiveType(type); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3113 | primitive_class->SetStatus(mirror::Class::kStatusInitialized, self); |
| 3114 | const char* descriptor = Primitive::Descriptor(type); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3115 | mirror::Class* existing = InsertClass(descriptor, primitive_class, |
| 3116 | ComputeModifiedUtf8Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3117 | CHECK(existing == nullptr) << "InitPrimitiveClass(" << type << ") failed"; |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3118 | return primitive_class; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 3119 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3120 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3121 | // Create an array class (i.e. the class object for the array, not the |
| 3122 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 3123 | // "[Ljava/lang/String;". |
| 3124 | // |
| 3125 | // If "descriptor" refers to an array of primitives, look up the |
| 3126 | // primitive type's internally-generated class object. |
| 3127 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3128 | // "class_loader" is the class loader of the class that's referring to |
| 3129 | // us. It's used to ensure that we're looking for the element type in |
| 3130 | // the right context. It does NOT become the class loader for the |
| 3131 | // array class; that always comes from the base element class. |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3132 | // |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3133 | // Returns nullptr with an exception raised on failure. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3134 | mirror::Class* ClassLinker::CreateArrayClass(Thread* self, const char* descriptor, size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3135 | Handle<mirror::ClassLoader> class_loader) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3136 | // Identify the underlying component type |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3137 | CHECK_EQ('[', descriptor[0]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3138 | StackHandleScope<2> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3139 | MutableHandle<mirror::Class> component_type(hs.NewHandle(FindClass(self, descriptor + 1, |
| 3140 | class_loader))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3141 | if (component_type.Get() == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3142 | DCHECK(self->IsExceptionPending()); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 3143 | // We need to accept erroneous classes as component types. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3144 | const size_t component_hash = ComputeModifiedUtf8Hash(descriptor + 1); |
| 3145 | component_type.Assign(LookupClass(self, descriptor + 1, component_hash, class_loader.Get())); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 3146 | if (component_type.Get() == nullptr) { |
| 3147 | DCHECK(self->IsExceptionPending()); |
| 3148 | return nullptr; |
| 3149 | } else { |
| 3150 | self->ClearException(); |
| 3151 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3152 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 3153 | if (UNLIKELY(component_type->IsPrimitiveVoid())) { |
| 3154 | ThrowNoClassDefFoundError("Attempt to create array of void primitive type"); |
| 3155 | return nullptr; |
| 3156 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3157 | // See if the component type is already loaded. Array classes are |
| 3158 | // always associated with the class loader of their underlying |
| 3159 | // element type -- an array of Strings goes with the loader for |
| 3160 | // java/lang/String -- so we need to look for it there. (The |
| 3161 | // caller should have checked for the existence of the class |
| 3162 | // before calling here, but they did so with *their* class loader, |
| 3163 | // not the component type's loader.) |
| 3164 | // |
| 3165 | // If we find it, the caller adds "loader" to the class' initiating |
| 3166 | // loader list, which should prevent us from going through this again. |
| 3167 | // |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3168 | // This call is unnecessary if "loader" and "component_type->GetClassLoader()" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3169 | // are the same, because our caller (FindClass) just did the |
| 3170 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 3171 | // because we effectively do this lookup again when we add the new |
| 3172 | // class to the hash table --- necessary because of possible races with |
| 3173 | // other threads.) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3174 | if (class_loader.Get() != component_type->GetClassLoader()) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3175 | mirror::Class* new_class = LookupClass(self, descriptor, hash, component_type->GetClassLoader()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3176 | if (new_class != nullptr) { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3177 | return new_class; |
| 3178 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3179 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3180 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3181 | // Fill out the fields in the Class. |
| 3182 | // |
| 3183 | // It is possible to execute some methods against arrays, because |
| 3184 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 3185 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 3186 | // |
| 3187 | // Array classes are simple enough that we don't need to do a full |
| 3188 | // link step. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3189 | auto new_class = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3190 | if (UNLIKELY(!init_done_)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3191 | // Classes that were hand created, ie not by FindSystemClass |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3192 | if (strcmp(descriptor, "[Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3193 | new_class.Assign(GetClassRoot(kClassArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3194 | } else if (strcmp(descriptor, "[Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3195 | new_class.Assign(GetClassRoot(kObjectArrayClass)); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3196 | } else if (strcmp(descriptor, GetClassRootDescriptor(kJavaLangStringArrayClass)) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3197 | new_class.Assign(GetClassRoot(kJavaLangStringArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3198 | } else if (strcmp(descriptor, |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3199 | GetClassRootDescriptor(kJavaLangReflectArtMethodArrayClass)) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3200 | new_class.Assign(GetClassRoot(kJavaLangReflectArtMethodArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3201 | } else if (strcmp(descriptor, |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3202 | GetClassRootDescriptor(kJavaLangReflectArtFieldArrayClass)) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3203 | new_class.Assign(GetClassRoot(kJavaLangReflectArtFieldArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3204 | } else if (strcmp(descriptor, "[C") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3205 | new_class.Assign(GetClassRoot(kCharArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3206 | } else if (strcmp(descriptor, "[I") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3207 | new_class.Assign(GetClassRoot(kIntArrayClass)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3208 | } |
| 3209 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3210 | if (new_class.Get() == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3211 | new_class.Assign(AllocClass(self, mirror::Array::ClassSize())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3212 | if (new_class.Get() == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3213 | return nullptr; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3214 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3215 | new_class->SetComponentType(component_type.Get()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3216 | } |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3217 | 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] | 3218 | DCHECK(new_class->GetComponentType() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3219 | mirror::Class* java_lang_Object = GetClassRoot(kJavaLangObject); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3220 | new_class->SetSuperClass(java_lang_Object); |
| 3221 | new_class->SetVTable(java_lang_Object->GetVTable()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3222 | new_class->SetPrimitiveType(Primitive::kPrimNot); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3223 | new_class->SetClassLoader(component_type->GetClassLoader()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3224 | new_class->SetStatus(mirror::Class::kStatusLoaded, self); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 3225 | { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3226 | StackHandleScope<mirror::Class::kImtSize> hs2(self, |
| 3227 | Runtime::Current()->GetImtUnimplementedMethod()); |
| 3228 | new_class->PopulateEmbeddedImtAndVTable(&hs2); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 3229 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3230 | new_class->SetStatus(mirror::Class::kStatusInitialized, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3231 | // don't need to set new_class->SetObjectSize(..) |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 3232 | // because Object::SizeOf delegates to Array::SizeOf |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3233 | |
| 3234 | |
| 3235 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 3236 | // interfaces. We need to set that up here, so that stuff like |
| 3237 | // "instanceof" works right. |
| 3238 | // |
| 3239 | // Note: The GC could run during the call to FindSystemClass, |
| 3240 | // so we need to make sure the class object is GC-valid while we're in |
| 3241 | // there. Do this by clearing the interface list so the GC will just |
| 3242 | // think that the entries are null. |
| 3243 | |
| 3244 | |
| 3245 | // Use the single, global copies of "interfaces" and "iftable" |
| 3246 | // (remember not to free them for arrays). |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3247 | { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 3248 | mirror::IfTable* array_iftable = array_iftable_.Read(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3249 | CHECK(array_iftable != nullptr); |
| 3250 | new_class->SetIfTable(array_iftable); |
| 3251 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3252 | |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 3253 | // Inherit access flags from the component type. |
| 3254 | int access_flags = new_class->GetComponentType()->GetAccessFlags(); |
| 3255 | // Lose any implementation detail flags; in particular, arrays aren't finalizable. |
| 3256 | access_flags &= kAccJavaFlagsMask; |
| 3257 | // 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] | 3258 | // and remove "interface". |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 3259 | access_flags |= kAccAbstract | kAccFinal; |
| 3260 | access_flags &= ~kAccInterface; |
| 3261 | |
| 3262 | new_class->SetAccessFlags(access_flags); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3263 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3264 | mirror::Class* existing = InsertClass(descriptor, new_class.Get(), hash); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3265 | if (existing == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3266 | return new_class.Get(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3267 | } |
| 3268 | // Another thread must have loaded the class after we |
| 3269 | // started but before we finished. Abandon what we've |
| 3270 | // done. |
| 3271 | // |
| 3272 | // (Yes, this happens.) |
| 3273 | |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 3274 | return existing; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3275 | } |
| 3276 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3277 | mirror::Class* ClassLinker::FindPrimitiveClass(char type) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3278 | switch (type) { |
| 3279 | case 'B': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3280 | return GetClassRoot(kPrimitiveByte); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3281 | case 'C': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3282 | return GetClassRoot(kPrimitiveChar); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3283 | case 'D': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3284 | return GetClassRoot(kPrimitiveDouble); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3285 | case 'F': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3286 | return GetClassRoot(kPrimitiveFloat); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3287 | case 'I': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3288 | return GetClassRoot(kPrimitiveInt); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3289 | case 'J': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3290 | return GetClassRoot(kPrimitiveLong); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3291 | case 'S': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3292 | return GetClassRoot(kPrimitiveShort); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3293 | case 'Z': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3294 | return GetClassRoot(kPrimitiveBoolean); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3295 | case 'V': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3296 | return GetClassRoot(kPrimitiveVoid); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3297 | default: |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3298 | break; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 3299 | } |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 3300 | std::string printable_type(PrintableChar(type)); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 3301 | ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3302 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3303 | } |
| 3304 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3305 | mirror::Class* ClassLinker::InsertClass(const char* descriptor, mirror::Class* klass, |
| 3306 | size_t hash) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3307 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3308 | mirror::DexCache* dex_cache = klass->GetDexCache(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3309 | std::string source; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3310 | if (dex_cache != nullptr) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3311 | source += " from "; |
| 3312 | source += dex_cache->GetLocation()->ToModifiedUtf8(); |
| 3313 | } |
| 3314 | LOG(INFO) << "Loaded class " << descriptor << source; |
| 3315 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3316 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3317 | mirror::Class* existing = LookupClassFromTableLocked(descriptor, klass->GetClassLoader(), hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3318 | if (existing != nullptr) { |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 3319 | return existing; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 3320 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3321 | if (kIsDebugBuild && !klass->IsTemp() && klass->GetClassLoader() == nullptr && |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3322 | dex_cache_image_class_lookup_required_) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3323 | // Check a class loaded with the system class loader matches one in the image if the class |
| 3324 | // is in the image. |
| 3325 | existing = LookupClassFromImage(descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3326 | if (existing != nullptr) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3327 | CHECK_EQ(klass, existing); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3328 | } |
| 3329 | } |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 3330 | VerifyObject(klass); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3331 | class_table_.InsertWithHash(GcRoot<mirror::Class>(klass), hash); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3332 | if (log_new_class_table_roots_) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3333 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3334 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3335 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3336 | } |
| 3337 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3338 | mirror::Class* ClassLinker::UpdateClass(const char* descriptor, mirror::Class* klass, |
| 3339 | size_t hash) { |
| 3340 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3341 | auto existing_it = class_table_.FindWithHash(std::make_pair(descriptor, klass->GetClassLoader()), |
| 3342 | hash); |
| 3343 | if (existing_it == class_table_.end()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3344 | CHECK(klass->IsProxyClass()); |
| 3345 | return nullptr; |
| 3346 | } |
| 3347 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3348 | mirror::Class* existing = existing_it->Read(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3349 | CHECK_NE(existing, klass) << descriptor; |
| 3350 | CHECK(!existing->IsResolved()) << descriptor; |
| 3351 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusResolving) << descriptor; |
| 3352 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3353 | CHECK(!klass->IsTemp()) << descriptor; |
| 3354 | if (kIsDebugBuild && klass->GetClassLoader() == nullptr && |
| 3355 | dex_cache_image_class_lookup_required_) { |
| 3356 | // Check a class loaded with the system class loader matches one in the image if the class |
| 3357 | // is in the image. |
| 3358 | existing = LookupClassFromImage(descriptor); |
| 3359 | if (existing != nullptr) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3360 | CHECK_EQ(klass, existing) << descriptor; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3361 | } |
| 3362 | } |
| 3363 | VerifyObject(klass); |
| 3364 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3365 | // Update the element in the hash set. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3366 | *existing_it = GcRoot<mirror::Class>(klass); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3367 | if (log_new_class_table_roots_) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3368 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3369 | } |
| 3370 | |
| 3371 | return existing; |
| 3372 | } |
| 3373 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3374 | bool ClassLinker::RemoveClass(const char* descriptor, mirror::ClassLoader* class_loader) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3375 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3376 | auto pair = std::make_pair(descriptor, class_loader); |
| 3377 | auto it = class_table_.Find(pair); |
| 3378 | if (it != class_table_.end()) { |
| 3379 | class_table_.Erase(it); |
| 3380 | return true; |
| 3381 | } |
| 3382 | it = pre_zygote_class_table_.Find(pair); |
| 3383 | if (it != pre_zygote_class_table_.end()) { |
| 3384 | pre_zygote_class_table_.Erase(it); |
| 3385 | return true; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3386 | } |
| 3387 | return false; |
| 3388 | } |
| 3389 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3390 | mirror::Class* ClassLinker::LookupClass(Thread* self, const char* descriptor, size_t hash, |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3391 | mirror::ClassLoader* class_loader) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3392 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3393 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3394 | mirror::Class* result = LookupClassFromTableLocked(descriptor, class_loader, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3395 | if (result != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3396 | return result; |
| 3397 | } |
Sameer Abu Asal | 2c6de22 | 2013-05-02 17:38:59 -0700 | [diff] [blame] | 3398 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3399 | if (class_loader != nullptr || !dex_cache_image_class_lookup_required_) { |
| 3400 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3401 | } else { |
| 3402 | // Lookup failed but need to search dex_caches_. |
| 3403 | mirror::Class* result = LookupClassFromImage(descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3404 | if (result != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3405 | InsertClass(descriptor, result, hash); |
| 3406 | } else { |
| 3407 | // Searching the image dex files/caches failed, we don't want to get into this situation |
| 3408 | // often as map searches are faster, so after kMaxFailedDexCacheLookups move all image |
| 3409 | // classes into the class table. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 3410 | constexpr uint32_t kMaxFailedDexCacheLookups = 1000; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3411 | if (++failed_dex_cache_class_lookups_ > kMaxFailedDexCacheLookups) { |
| 3412 | MoveImageClassesToClassTable(); |
| 3413 | } |
| 3414 | } |
| 3415 | return result; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3416 | } |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 3417 | } |
| 3418 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3419 | mirror::Class* ClassLinker::LookupClassFromTableLocked(const char* descriptor, |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3420 | mirror::ClassLoader* class_loader, |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3421 | size_t hash) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3422 | auto descriptor_pair = std::make_pair(descriptor, class_loader); |
| 3423 | auto it = pre_zygote_class_table_.FindWithHash(descriptor_pair, hash); |
| 3424 | if (it == pre_zygote_class_table_.end()) { |
| 3425 | it = class_table_.FindWithHash(descriptor_pair, hash); |
| 3426 | if (it == class_table_.end()) { |
| 3427 | return nullptr; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 3428 | } |
| 3429 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3430 | return it->Read(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3431 | } |
| 3432 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3433 | static mirror::ObjectArray<mirror::DexCache>* GetImageDexCaches() |
| 3434 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 3435 | gc::space::ImageSpace* image = Runtime::Current()->GetHeap()->GetImageSpace(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3436 | CHECK(image != nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3437 | mirror::Object* root = image->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches); |
| 3438 | return root->AsObjectArray<mirror::DexCache>(); |
| 3439 | } |
| 3440 | |
| 3441 | void ClassLinker::MoveImageClassesToClassTable() { |
| 3442 | Thread* self = Thread::Current(); |
| 3443 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3444 | if (!dex_cache_image_class_lookup_required_) { |
| 3445 | return; // All dex cache classes are already in the class table. |
| 3446 | } |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 3447 | ScopedAssertNoThreadSuspension ants(self, "Moving image classes to class table"); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3448 | mirror::ObjectArray<mirror::DexCache>* dex_caches = GetImageDexCaches(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3449 | std::string temp; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3450 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 3451 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 3452 | mirror::ObjectArray<mirror::Class>* types = dex_cache->GetResolvedTypes(); |
| 3453 | for (int32_t j = 0; j < types->GetLength(); j++) { |
| 3454 | mirror::Class* klass = types->Get(j); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3455 | if (klass != nullptr) { |
| 3456 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3457 | const char* descriptor = klass->GetDescriptor(&temp); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3458 | size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3459 | mirror::Class* existing = LookupClassFromTableLocked(descriptor, nullptr, hash); |
| 3460 | if (existing != nullptr) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3461 | CHECK_EQ(existing, klass) << PrettyClassAndClassLoader(existing) << " != " |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3462 | << PrettyClassAndClassLoader(klass); |
| 3463 | } else { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3464 | class_table_.Insert(GcRoot<mirror::Class>(klass)); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3465 | if (log_new_class_table_roots_) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3466 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3467 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3468 | } |
| 3469 | } |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 3470 | } |
| 3471 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3472 | dex_cache_image_class_lookup_required_ = false; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3473 | } |
| 3474 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3475 | void ClassLinker::MoveClassTableToPreZygote() { |
| 3476 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3477 | DCHECK(pre_zygote_class_table_.Empty()); |
| 3478 | pre_zygote_class_table_ = std::move(class_table_); |
| 3479 | class_table_.Clear(); |
| 3480 | } |
| 3481 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3482 | mirror::Class* ClassLinker::LookupClassFromImage(const char* descriptor) { |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 3483 | ScopedAssertNoThreadSuspension ants(Thread::Current(), "Image class lookup"); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3484 | mirror::ObjectArray<mirror::DexCache>* dex_caches = GetImageDexCaches(); |
| 3485 | for (int32_t i = 0; i < dex_caches->GetLength(); ++i) { |
| 3486 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 3487 | const DexFile* dex_file = dex_cache->GetDexFile(); |
Vladimir Marko | 801a811 | 2014-01-06 14:28:16 +0000 | [diff] [blame] | 3488 | // Try binary searching the string/type index. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3489 | const DexFile::StringId* string_id = dex_file->FindStringId(descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3490 | if (string_id != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3491 | const DexFile::TypeId* type_id = |
| 3492 | dex_file->FindTypeId(dex_file->GetIndexForStringId(*string_id)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3493 | if (type_id != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3494 | uint16_t type_idx = dex_file->GetIndexForTypeId(*type_id); |
| 3495 | mirror::Class* klass = dex_cache->GetResolvedType(type_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3496 | if (klass != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3497 | return klass; |
| 3498 | } |
| 3499 | } |
| 3500 | } |
| 3501 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3502 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3503 | } |
| 3504 | |
| 3505 | void ClassLinker::LookupClasses(const char* descriptor, std::vector<mirror::Class*>& result) { |
| 3506 | result.clear(); |
| 3507 | if (dex_cache_image_class_lookup_required_) { |
| 3508 | MoveImageClassesToClassTable(); |
| 3509 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3510 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3511 | while (true) { |
| 3512 | auto it = class_table_.Find(descriptor); |
| 3513 | if (it == class_table_.end()) { |
| 3514 | break; |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 3515 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3516 | result.push_back(it->Read()); |
| 3517 | class_table_.Erase(it); |
| 3518 | } |
| 3519 | for (mirror::Class* k : result) { |
| 3520 | class_table_.Insert(GcRoot<mirror::Class>(k)); |
| 3521 | } |
| 3522 | size_t pre_zygote_start = result.size(); |
| 3523 | // Now handle the pre zygote table. |
| 3524 | // Note: This dirties the pre-zygote table but shouldn't be an issue since LookupClasses is only |
| 3525 | // called from the debugger. |
| 3526 | while (true) { |
| 3527 | auto it = pre_zygote_class_table_.Find(descriptor); |
| 3528 | if (it == pre_zygote_class_table_.end()) { |
| 3529 | break; |
| 3530 | } |
| 3531 | result.push_back(it->Read()); |
| 3532 | pre_zygote_class_table_.Erase(it); |
| 3533 | } |
| 3534 | for (size_t i = pre_zygote_start; i < result.size(); ++i) { |
| 3535 | pre_zygote_class_table_.Insert(GcRoot<mirror::Class>(result[i])); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 3536 | } |
| 3537 | } |
| 3538 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3539 | void ClassLinker::VerifyClass(Thread* self, Handle<mirror::Class> klass) { |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 3540 | // TODO: assert that the monitor on the Class is held |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3541 | ObjectLock<mirror::Class> lock(self, klass); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 3542 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3543 | // Don't attempt to re-verify if already sufficiently verified. |
Andreas Gampe | bb0c7f6 | 2014-09-11 10:59:33 -0700 | [diff] [blame] | 3544 | if (klass->IsVerified()) { |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 3545 | EnsurePreverifiedMethods(klass); |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3546 | return; |
| 3547 | } |
Andreas Gampe | bb0c7f6 | 2014-09-11 10:59:33 -0700 | [diff] [blame] | 3548 | if (klass->IsCompileTimeVerified() && Runtime::Current()->IsCompiler()) { |
| 3549 | return; |
| 3550 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3551 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3552 | // The class might already be erroneous, for example at compile time if we attempted to verify |
| 3553 | // this class as a parent to another. |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 3554 | if (klass->IsErroneous()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3555 | ThrowEarlierClassFailure(klass.Get()); |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 3556 | return; |
| 3557 | } |
| 3558 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3559 | if (klass->GetStatus() == mirror::Class::kStatusResolved) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3560 | klass->SetStatus(mirror::Class::kStatusVerifying, self); |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3561 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3562 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3563 | << PrettyClass(klass.Get()); |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3564 | CHECK(!Runtime::Current()->IsCompiler()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3565 | klass->SetStatus(mirror::Class::kStatusVerifyingAtRuntime, self); |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3566 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3567 | |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 3568 | // Skip verification if disabled. |
| 3569 | if (!Runtime::Current()->IsVerificationEnabled()) { |
| 3570 | klass->SetStatus(mirror::Class::kStatusVerified, self); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 3571 | EnsurePreverifiedMethods(klass); |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 3572 | return; |
| 3573 | } |
| 3574 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3575 | // Verify super class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3576 | StackHandleScope<2> hs(self); |
| 3577 | Handle<mirror::Class> super(hs.NewHandle(klass->GetSuperClass())); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3578 | if (super.Get() != nullptr) { |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3579 | // Acquire lock to prevent races on verifying the super class. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3580 | ObjectLock<mirror::Class> super_lock(self, super); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3581 | |
| 3582 | if (!super->IsVerified() && !super->IsErroneous()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3583 | VerifyClass(self, super); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3584 | } |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 3585 | if (!super->IsCompileTimeVerified()) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3586 | std::string error_msg( |
| 3587 | StringPrintf("Rejecting class %s that attempts to sub-class erroneous class %s", |
| 3588 | PrettyDescriptor(klass.Get()).c_str(), |
| 3589 | PrettyDescriptor(super.Get()).c_str())); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3590 | LOG(ERROR) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3591 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException(nullptr))); |
| 3592 | if (cause.Get() != nullptr) { |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3593 | self->ClearException(); |
| 3594 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3595 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
| 3596 | if (cause.Get() != nullptr) { |
| 3597 | self->GetException(nullptr)->SetCause(cause.Get()); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3598 | } |
Jeff Hao | 22cb09b | 2013-12-12 14:29:15 -0800 | [diff] [blame] | 3599 | ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex()); |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 3600 | if (Runtime::Current()->IsCompiler()) { |
| 3601 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
| 3602 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3603 | klass->SetStatus(mirror::Class::kStatusError, self); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3604 | return; |
| 3605 | } |
| 3606 | } |
| 3607 | |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 3608 | // 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] | 3609 | const DexFile& dex_file = *klass->GetDexCache()->GetDexFile(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3610 | mirror::Class::Status oat_file_class_status(mirror::Class::kStatusNotReady); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3611 | bool preverified = VerifyClassUsingOatFile(dex_file, klass.Get(), oat_file_class_status); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3612 | if (oat_file_class_status == mirror::Class::kStatusError) { |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 3613 | VLOG(class_linker) << "Skipping runtime verification of erroneous class " |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3614 | << PrettyDescriptor(klass.Get()) << " in " |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 3615 | << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3616 | ThrowVerifyError(klass.Get(), "Rejecting class %s because it failed compile-time verification", |
| 3617 | PrettyDescriptor(klass.Get()).c_str()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3618 | klass->SetStatus(mirror::Class::kStatusError, self); |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 3619 | return; |
| 3620 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3621 | verifier::MethodVerifier::FailureKind verifier_failure = verifier::MethodVerifier::kNoFailure; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3622 | std::string error_msg; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 3623 | if (!preverified) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3624 | verifier_failure = verifier::MethodVerifier::VerifyClass(self, klass.Get(), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3625 | Runtime::Current()->IsCompiler(), |
| 3626 | &error_msg); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 3627 | } |
| 3628 | if (preverified || verifier_failure != verifier::MethodVerifier::kHardFailure) { |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 3629 | if (!preverified && verifier_failure != verifier::MethodVerifier::kNoFailure) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3630 | VLOG(class_linker) << "Soft verification failure in class " << PrettyDescriptor(klass.Get()) |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 3631 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 3632 | << " because: " << error_msg; |
| 3633 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3634 | self->AssertNoPendingException(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3635 | // Make sure all classes referenced by catch blocks are resolved. |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3636 | ResolveClassExceptionHandlerTypes(dex_file, klass); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3637 | if (verifier_failure == verifier::MethodVerifier::kNoFailure) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3638 | // Even though there were no verifier failures we need to respect whether the super-class |
| 3639 | // was verified or requiring runtime reverification. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3640 | if (super.Get() == nullptr || super->IsVerified()) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3641 | klass->SetStatus(mirror::Class::kStatusVerified, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3642 | } else { |
| 3643 | CHECK_EQ(super->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3644 | klass->SetStatus(mirror::Class::kStatusRetryVerificationAtRuntime, self); |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 3645 | // Pretend a soft failure occured so that we don't consider the class verified below. |
| 3646 | verifier_failure = verifier::MethodVerifier::kSoftFailure; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3647 | } |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3648 | } else { |
| 3649 | CHECK_EQ(verifier_failure, verifier::MethodVerifier::kSoftFailure); |
| 3650 | // Soft failures at compile time should be retried at runtime. Soft |
| 3651 | // failures at runtime will be handled by slow paths in the generated |
| 3652 | // code. Set status accordingly. |
| 3653 | if (Runtime::Current()->IsCompiler()) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3654 | klass->SetStatus(mirror::Class::kStatusRetryVerificationAtRuntime, self); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3655 | } else { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3656 | klass->SetStatus(mirror::Class::kStatusVerified, self); |
Andreas Gampe | bb0c7f6 | 2014-09-11 10:59:33 -0700 | [diff] [blame] | 3657 | // As this is a fake verified status, make sure the methods are _not_ marked preverified |
| 3658 | // later. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3659 | klass->SetPreverified(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3660 | } |
| 3661 | } |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 3662 | } else { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3663 | LOG(ERROR) << "Verification failed on class " << PrettyDescriptor(klass.Get()) |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3664 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 3665 | << " because: " << error_msg; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3666 | self->AssertNoPendingException(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3667 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3668 | klass->SetStatus(mirror::Class::kStatusError, self); |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 3669 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3670 | if (preverified || verifier_failure == verifier::MethodVerifier::kNoFailure) { |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 3671 | // 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] | 3672 | // Let the interpreter know it by setting the kAccPreverified flag onto each |
| 3673 | // method. |
| 3674 | // Note: we're going here during compilation and at runtime. When we set the |
| 3675 | // kAccPreverified flag when compiling image classes, the flag is recorded |
| 3676 | // in the image and is set when loading the image. |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 3677 | EnsurePreverifiedMethods(klass); |
| 3678 | } |
| 3679 | } |
| 3680 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3681 | void ClassLinker::EnsurePreverifiedMethods(Handle<mirror::Class> klass) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3682 | if (!klass->IsPreverified()) { |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3683 | klass->SetPreverifiedFlagOnAllMethods(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3684 | klass->SetPreverified(); |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3685 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3686 | } |
| 3687 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3688 | bool ClassLinker::VerifyClassUsingOatFile(const DexFile& dex_file, mirror::Class* klass, |
| 3689 | mirror::Class::Status& oat_file_class_status) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3690 | // If we're compiling, we can only verify the class using the oat file if |
| 3691 | // we are not compiling the image or if the class we're verifying is not part of |
| 3692 | // the app. In other words, we will only check for preverification of bootclasspath |
| 3693 | // classes. |
| 3694 | if (Runtime::Current()->IsCompiler()) { |
| 3695 | // Are we compiling the bootclasspath? |
| 3696 | if (!Runtime::Current()->UseCompileTimeClassPath()) { |
| 3697 | return false; |
| 3698 | } |
| 3699 | // We are compiling an app (not the image). |
| 3700 | |
| 3701 | // Is this an app class? (I.e. not a bootclasspath class) |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3702 | if (klass->GetClassLoader() != nullptr) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3703 | return false; |
| 3704 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3705 | } |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3706 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 3707 | const OatFile::OatDexFile* oat_dex_file = FindOpenedOatDexFileForDexFile(dex_file); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3708 | // In case we run without an image there won't be a backing oat file. |
| 3709 | if (oat_dex_file == nullptr) { |
Anwar Ghuloum | ad256bb | 2013-07-18 14:58:55 -0700 | [diff] [blame] | 3710 | return false; |
| 3711 | } |
| 3712 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3713 | uint16_t class_def_index = klass->GetDexClassDefIndex(); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 3714 | oat_file_class_status = oat_dex_file->GetOatClass(class_def_index).GetStatus(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3715 | if (oat_file_class_status == mirror::Class::kStatusVerified || |
| 3716 | oat_file_class_status == mirror::Class::kStatusInitialized) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3717 | return true; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3718 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3719 | if (oat_file_class_status == mirror::Class::kStatusRetryVerificationAtRuntime) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 3720 | // Compile time verification failed with a soft error. Compile time verification can fail |
| 3721 | // because we have incomplete type information. Consider the following: |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3722 | // class ... { |
| 3723 | // Foo x; |
| 3724 | // .... () { |
| 3725 | // if (...) { |
| 3726 | // v1 gets assigned a type of resolved class Foo |
| 3727 | // } else { |
| 3728 | // v1 gets assigned a type of unresolved class Bar |
| 3729 | // } |
| 3730 | // iput x = v1 |
| 3731 | // } } |
| 3732 | // when we merge v1 following the if-the-else it results in Conflict |
| 3733 | // (see verifier::RegType::Merge) as we can't know the type of Bar and we could possibly be |
| 3734 | // allowing an unsafe assignment to the field x in the iput (javac may have compiled this as |
| 3735 | // it knew Bar was a sub-class of Foo, but for us this may have been moved into a separate apk |
| 3736 | // at compile time). |
| 3737 | return false; |
| 3738 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3739 | if (oat_file_class_status == mirror::Class::kStatusError) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 3740 | // Compile time verification failed with a hard error. This is caused by invalid instructions |
| 3741 | // in the class. These errors are unrecoverable. |
| 3742 | return false; |
| 3743 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3744 | if (oat_file_class_status == mirror::Class::kStatusNotReady) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3745 | // Status is uninitialized if we couldn't determine the status at compile time, for example, |
| 3746 | // not loading the class. |
| 3747 | // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy |
| 3748 | // isn't a problem and this case shouldn't occur |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3749 | return false; |
| 3750 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3751 | std::string temp; |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 3752 | LOG(FATAL) << "Unexpected class status: " << oat_file_class_status |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3753 | << " " << dex_file.GetLocation() << " " << PrettyClass(klass) << " " |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3754 | << klass->GetDescriptor(&temp); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 3755 | UNREACHABLE(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3756 | } |
| 3757 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3758 | void ClassLinker::ResolveClassExceptionHandlerTypes(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3759 | Handle<mirror::Class> klass) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3760 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
| 3761 | ResolveMethodExceptionHandlerTypes(dex_file, klass->GetDirectMethod(i)); |
| 3762 | } |
| 3763 | for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { |
| 3764 | ResolveMethodExceptionHandlerTypes(dex_file, klass->GetVirtualMethod(i)); |
| 3765 | } |
| 3766 | } |
| 3767 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3768 | void ClassLinker::ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3769 | mirror::ArtMethod* method) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3770 | // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod. |
| 3771 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3772 | if (code_item == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3773 | return; // native or abstract method |
| 3774 | } |
| 3775 | if (code_item->tries_size_ == 0) { |
| 3776 | return; // nothing to process |
| 3777 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 3778 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item, 0); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3779 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 3780 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 3781 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| 3782 | CatchHandlerIterator iterator(handlers_ptr); |
| 3783 | for (; iterator.HasNext(); iterator.Next()) { |
| 3784 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 3785 | // unresolved exception types will be ignored by exception delivery |
| 3786 | if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3787 | mirror::Class* exception_type = linker->ResolveType(iterator.GetHandlerTypeIndex(), method); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3788 | if (exception_type == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3789 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 3790 | Thread::Current()->ClearException(); |
| 3791 | } |
| 3792 | } |
| 3793 | } |
| 3794 | handlers_ptr = iterator.EndDataPointer(); |
| 3795 | } |
| 3796 | } |
| 3797 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3798 | static void CheckProxyConstructor(mirror::ArtMethod* constructor); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3799 | static void CheckProxyMethod(Handle<mirror::ArtMethod> method, |
| 3800 | Handle<mirror::ArtMethod> prototype); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3801 | |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 3802 | mirror::Class* ClassLinker::CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, jstring name, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3803 | jobjectArray interfaces, jobject loader, |
| 3804 | jobjectArray methods, jobjectArray throws) { |
| 3805 | Thread* self = soa.Self(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3806 | StackHandleScope<8> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3807 | MutableHandle<mirror::Class> klass(hs.NewHandle( |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3808 | AllocClass(self, GetClassRoot(kJavaLangClass), sizeof(mirror::Class)))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3809 | if (klass.Get() == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3810 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3811 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3812 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3813 | DCHECK(klass->GetClass() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3814 | klass->SetObjectSize(sizeof(mirror::Proxy)); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 3815 | // Set the class access flags incl. preverified, so we do not try to set the flag on the methods. |
| 3816 | klass->SetAccessFlags(kAccClassIsProxy | kAccPublic | kAccFinal | kAccPreverified); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3817 | klass->SetClassLoader(soa.Decode<mirror::ClassLoader*>(loader)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3818 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3819 | klass->SetName(soa.Decode<mirror::String*>(name)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3820 | mirror::Class* proxy_class = GetClassRoot(kJavaLangReflectProxy); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3821 | klass->SetDexCache(proxy_class->GetDexCache()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3822 | klass->SetStatus(mirror::Class::kStatusIdx, self); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3823 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3824 | // Instance fields are inherited, but we add a couple of static fields... |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3825 | { |
| 3826 | mirror::ObjectArray<mirror::ArtField>* sfields = AllocArtFieldArray(self, 2); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3827 | if (UNLIKELY(sfields == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3828 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3829 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3830 | } |
| 3831 | klass->SetSFields(sfields); |
| 3832 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3833 | // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by |
| 3834 | // our proxy, so Class.getInterfaces doesn't return the flattened set. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3835 | Handle<mirror::ArtField> interfaces_sfield(hs.NewHandle(AllocArtField(self))); |
| 3836 | if (UNLIKELY(interfaces_sfield.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3837 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3838 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3839 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3840 | klass->SetStaticField(0, interfaces_sfield.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3841 | interfaces_sfield->SetDexFieldIndex(0); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3842 | interfaces_sfield->SetDeclaringClass(klass.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3843 | interfaces_sfield->SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
| 3844 | // 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] | 3845 | Handle<mirror::ArtField> throws_sfield(hs.NewHandle(AllocArtField(self))); |
| 3846 | if (UNLIKELY(throws_sfield.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3847 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3848 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3849 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3850 | klass->SetStaticField(1, throws_sfield.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3851 | throws_sfield->SetDexFieldIndex(1); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3852 | throws_sfield->SetDeclaringClass(klass.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3853 | throws_sfield->SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3854 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3855 | // Proxies have 1 direct method, the constructor |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3856 | { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3857 | mirror::ObjectArray<mirror::ArtMethod>* directs = AllocArtMethodArray(self, 1); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3858 | if (UNLIKELY(directs == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3859 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3860 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3861 | } |
| 3862 | klass->SetDirectMethods(directs); |
| 3863 | mirror::ArtMethod* constructor = CreateProxyConstructor(self, klass, proxy_class); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3864 | if (UNLIKELY(constructor == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3865 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3866 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3867 | } |
| 3868 | klass->SetDirectMethod(0, constructor); |
| 3869 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3870 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3871 | // Create virtual method using specified prototypes. |
| 3872 | size_t num_virtual_methods = |
| 3873 | soa.Decode<mirror::ObjectArray<mirror::ArtMethod>*>(methods)->GetLength(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3874 | { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3875 | mirror::ObjectArray<mirror::ArtMethod>* virtuals = AllocArtMethodArray(self, |
| 3876 | num_virtual_methods); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3877 | if (UNLIKELY(virtuals == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3878 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3879 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3880 | } |
| 3881 | klass->SetVirtualMethods(virtuals); |
| 3882 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3883 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3884 | StackHandleScope<1> hs2(self); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3885 | mirror::ObjectArray<mirror::ArtMethod>* decoded_methods = |
| 3886 | soa.Decode<mirror::ObjectArray<mirror::ArtMethod>*>(methods); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3887 | Handle<mirror::ArtMethod> prototype(hs2.NewHandle(decoded_methods->Get(i))); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3888 | mirror::ArtMethod* clone = CreateProxyMethod(self, klass, prototype); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3889 | if (UNLIKELY(clone == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3890 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3891 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3892 | } |
| 3893 | klass->SetVirtualMethod(i, clone); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3894 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3895 | |
| 3896 | klass->SetSuperClass(proxy_class); // The super class is java.lang.reflect.Proxy |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3897 | klass->SetStatus(mirror::Class::kStatusLoaded, self); // Now effectively in the loaded state. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3898 | self->AssertNoPendingException(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3899 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3900 | std::string descriptor(GetDescriptorForProxy(klass.Get())); |
| 3901 | mirror::Class* new_class = nullptr; |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 3902 | { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3903 | // Must hold lock on object when resolved. |
| 3904 | ObjectLock<mirror::Class> resolution_lock(self, klass); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3905 | // Link the fields and virtual methods, creating vtable and iftables |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3906 | Handle<mirror::ObjectArray<mirror::Class> > h_interfaces( |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3907 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces))); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3908 | if (!LinkClass(self, descriptor.c_str(), klass, h_interfaces, &new_class)) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3909 | klass->SetStatus(mirror::Class::kStatusError, self); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3910 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3911 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3912 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3913 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3914 | CHECK(klass->IsRetired()); |
| 3915 | CHECK_NE(klass.Get(), new_class); |
| 3916 | klass.Assign(new_class); |
| 3917 | |
| 3918 | CHECK_EQ(interfaces_sfield->GetDeclaringClass(), new_class); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3919 | interfaces_sfield->SetObject<false>(klass.Get(), |
| 3920 | soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3921 | CHECK_EQ(throws_sfield->GetDeclaringClass(), new_class); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3922 | throws_sfield->SetObject<false>(klass.Get(), |
| 3923 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class> >*>(throws)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3924 | |
| 3925 | { |
| 3926 | // Lock on klass is released. Lock new class object. |
| 3927 | ObjectLock<mirror::Class> initialization_lock(self, klass); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3928 | klass->SetStatus(mirror::Class::kStatusInitialized, self); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 3929 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3930 | |
| 3931 | // sanity checks |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 3932 | if (kIsDebugBuild) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3933 | CHECK(klass->GetIFields() == nullptr); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3934 | CheckProxyConstructor(klass->GetDirectMethod(0)); |
| 3935 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3936 | StackHandleScope<2> hs2(self); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3937 | mirror::ObjectArray<mirror::ArtMethod>* decoded_methods = |
| 3938 | soa.Decode<mirror::ObjectArray<mirror::ArtMethod>*>(methods); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3939 | Handle<mirror::ArtMethod> prototype(hs2.NewHandle(decoded_methods->Get(i))); |
| 3940 | Handle<mirror::ArtMethod> virtual_method(hs2.NewHandle(klass->GetVirtualMethod(i))); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3941 | CheckProxyMethod(virtual_method, prototype); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3942 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3943 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3944 | mirror::String* decoded_name = soa.Decode<mirror::String*>(name); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3945 | std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3946 | decoded_name->ToModifiedUtf8().c_str())); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3947 | CHECK_EQ(PrettyField(klass->GetStaticField(0)), interfaces_field_name); |
| 3948 | |
| 3949 | std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3950 | decoded_name->ToModifiedUtf8().c_str())); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3951 | CHECK_EQ(PrettyField(klass->GetStaticField(1)), throws_field_name); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3952 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3953 | CHECK_EQ(klass.Get()->GetInterfaces(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3954 | soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3955 | CHECK_EQ(klass.Get()->GetThrows(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3956 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>*>(throws)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3957 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3958 | mirror::Class* existing = InsertClass(descriptor.c_str(), klass.Get(), |
| 3959 | ComputeModifiedUtf8Hash(descriptor.c_str())); |
Mathieu Chartier | 31b9d66 | 2013-10-14 11:53:12 -0700 | [diff] [blame] | 3960 | CHECK(existing == nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3961 | return klass.Get(); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3962 | } |
| 3963 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3964 | std::string ClassLinker::GetDescriptorForProxy(mirror::Class* proxy_class) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3965 | DCHECK(proxy_class->IsProxyClass()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3966 | mirror::String* name = proxy_class->GetName(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3967 | DCHECK(name != nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3968 | return DotToDescriptor(name->ToModifiedUtf8().c_str()); |
| 3969 | } |
| 3970 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3971 | mirror::ArtMethod* ClassLinker::FindMethodForProxy(mirror::Class* proxy_class, |
| 3972 | mirror::ArtMethod* proxy_method) { |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3973 | DCHECK(proxy_class->IsProxyClass()); |
| 3974 | DCHECK(proxy_method->IsProxyMethod()); |
| 3975 | // Locate the dex cache of the original interface/Object |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 3976 | mirror::DexCache* dex_cache = nullptr; |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3977 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3978 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3979 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3980 | mirror::DexCache* a_dex_cache = GetDexCache(i); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 3981 | if (proxy_method->HasSameDexCacheResolvedTypes(a_dex_cache->GetResolvedTypes())) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3982 | dex_cache = a_dex_cache; |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3983 | break; |
| 3984 | } |
| 3985 | } |
| 3986 | } |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 3987 | CHECK(dex_cache != nullptr); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3988 | uint32_t method_idx = proxy_method->GetDexMethodIndex(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3989 | mirror::ArtMethod* resolved_method = dex_cache->GetResolvedMethod(method_idx); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 3990 | CHECK(resolved_method != nullptr); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3991 | return resolved_method; |
| 3992 | } |
| 3993 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3994 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3995 | mirror::ArtMethod* ClassLinker::CreateProxyConstructor(Thread* self, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3996 | Handle<mirror::Class> klass, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3997 | mirror::Class* proxy_class) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3998 | // Create constructor for Proxy that must initialize h |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3999 | mirror::ObjectArray<mirror::ArtMethod>* proxy_direct_methods = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4000 | proxy_class->GetDirectMethods(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4001 | CHECK_EQ(proxy_direct_methods->GetLength(), 16); |
| 4002 | mirror::ArtMethod* proxy_constructor = proxy_direct_methods->Get(2); |
Sebastien Hertz | ae94e35 | 2014-08-27 15:32:56 +0200 | [diff] [blame] | 4003 | // Ensure constructor is in dex cache so that we can use the dex cache to look up the overridden |
| 4004 | // constructor method. |
| 4005 | proxy_class->GetDexCache()->SetResolvedMethod(proxy_constructor->GetDexMethodIndex(), |
| 4006 | proxy_constructor); |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 4007 | // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its |
| 4008 | // code_ too) |
Jeff Hao | f0a3f09 | 2014-07-24 16:26:09 -0700 | [diff] [blame] | 4009 | mirror::ArtMethod* constructor = down_cast<mirror::ArtMethod*>(proxy_constructor->Clone(self)); |
| 4010 | if (constructor == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4011 | CHECK(self->IsExceptionPending()); // OOME. |
Jeff Hao | f0a3f09 | 2014-07-24 16:26:09 -0700 | [diff] [blame] | 4012 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4013 | } |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4014 | // Make this constructor public and fix the class to be our Proxy version |
| 4015 | constructor->SetAccessFlags((constructor->GetAccessFlags() & ~kAccProtected) | kAccPublic); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4016 | constructor->SetDeclaringClass(klass.Get()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4017 | return constructor; |
| 4018 | } |
| 4019 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4020 | static void CheckProxyConstructor(mirror::ArtMethod* constructor) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 4021 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4022 | CHECK(constructor->IsConstructor()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4023 | CHECK_STREQ(constructor->GetName(), "<init>"); |
| 4024 | CHECK_STREQ(constructor->GetSignature().ToString().c_str(), |
| 4025 | "(Ljava/lang/reflect/InvocationHandler;)V"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4026 | DCHECK(constructor->IsPublic()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4027 | } |
| 4028 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4029 | mirror::ArtMethod* ClassLinker::CreateProxyMethod(Thread* self, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4030 | Handle<mirror::Class> klass, |
| 4031 | Handle<mirror::ArtMethod> prototype) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4032 | // Ensure prototype is in dex cache so that we can use the dex cache to look up the overridden |
| 4033 | // prototype method |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 4034 | prototype->GetDeclaringClass()->GetDexCache()->SetResolvedMethod(prototype->GetDexMethodIndex(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4035 | prototype.Get()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4036 | // 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] | 4037 | // as necessary |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4038 | mirror::ArtMethod* method = down_cast<mirror::ArtMethod*>(prototype->Clone(self)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4039 | if (UNLIKELY(method == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4040 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4041 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4042 | } |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4043 | |
| 4044 | // Set class to be the concrete proxy class and clear the abstract flag, modify exceptions to |
| 4045 | // the intersection of throw exceptions as defined in Proxy |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4046 | method->SetDeclaringClass(klass.Get()); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4047 | method->SetAccessFlags((method->GetAccessFlags() & ~kAccAbstract) | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4048 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4049 | // At runtime the method looks like a reference and argument saving method, clone the code |
| 4050 | // related parameters from this method. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 4051 | method->SetEntryPointFromQuickCompiledCode(GetQuickProxyInvokeHandler()); |
| 4052 | method->SetEntryPointFromPortableCompiledCode(GetPortableProxyInvokeHandler()); |
Dragos Sbirlea | 08bf196 | 2013-08-12 08:53:04 -0700 | [diff] [blame] | 4053 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 4054 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4055 | return method; |
| 4056 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4057 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4058 | static void CheckProxyMethod(Handle<mirror::ArtMethod> method, |
| 4059 | Handle<mirror::ArtMethod> prototype) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 4060 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4061 | // Basic sanity |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4062 | CHECK(!prototype->IsFinal()); |
| 4063 | CHECK(method->IsFinal()); |
| 4064 | CHECK(!method->IsAbstract()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 4065 | |
| 4066 | // The proxy method doesn't have its own dex cache or dex file and so it steals those of its |
| 4067 | // 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] | 4068 | CHECK(prototype->HasSameDexCacheResolvedMethods(method.Get())); |
| 4069 | CHECK(prototype->HasSameDexCacheResolvedTypes(method.Get())); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 4070 | CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex()); |
| 4071 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4072 | CHECK_STREQ(method->GetName(), prototype->GetName()); |
| 4073 | CHECK_STREQ(method->GetShorty(), prototype->GetShorty()); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4074 | // More complex sanity - via dex cache |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 4075 | CHECK_EQ(method->GetInterfaceMethodIfProxy()->GetReturnType(), prototype->GetReturnType()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4076 | } |
| 4077 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4078 | static bool CanWeInitializeClass(mirror::Class* klass, bool can_init_statics, |
| 4079 | bool can_init_parents) |
| 4080 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 4081 | if (can_init_statics && can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4082 | return true; |
| 4083 | } |
| 4084 | if (!can_init_statics) { |
| 4085 | // Check if there's a class initializer. |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 4086 | mirror::ArtMethod* clinit = klass->FindClassInitializer(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4087 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4088 | return false; |
| 4089 | } |
| 4090 | // Check if there are encoded static values needing initialization. |
| 4091 | if (klass->NumStaticFields() != 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4092 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4093 | DCHECK(dex_class_def != nullptr); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4094 | if (dex_class_def->static_values_off_ != 0) { |
| 4095 | return false; |
| 4096 | } |
| 4097 | } |
| 4098 | } |
| 4099 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
| 4100 | mirror::Class* super_class = klass->GetSuperClass(); |
| 4101 | if (!can_init_parents && !super_class->IsInitialized()) { |
| 4102 | return false; |
| 4103 | } else { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 4104 | if (!CanWeInitializeClass(super_class, can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4105 | return false; |
| 4106 | } |
| 4107 | } |
| 4108 | } |
| 4109 | return true; |
| 4110 | } |
| 4111 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4112 | bool ClassLinker::InitializeClass(Thread* self, Handle<mirror::Class> klass, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4113 | bool can_init_statics, bool can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4114 | // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol |
| 4115 | |
| 4116 | // Are we already initialized and therefore done? |
| 4117 | // Note: we differ from the JLS here as we don't do this under the lock, this is benign as |
| 4118 | // an initialized class will never change its state. |
| 4119 | if (klass->IsInitialized()) { |
| 4120 | return true; |
| 4121 | } |
| 4122 | |
| 4123 | // 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] | 4124 | if (!CanWeInitializeClass(klass.Get(), can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4125 | return false; |
| 4126 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4127 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4128 | self->AllowThreadSuspension(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4129 | uint64_t t0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4130 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4131 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4132 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4133 | // Re-check under the lock in case another thread initialized ahead of us. |
| 4134 | if (klass->IsInitialized()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4135 | return true; |
| 4136 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4137 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4138 | // 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] | 4139 | if (klass->IsErroneous()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4140 | ThrowEarlierClassFailure(klass.Get()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4141 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4142 | return false; |
| 4143 | } |
| 4144 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4145 | CHECK(klass->IsResolved()) << PrettyClass(klass.Get()) << ": state=" << klass->GetStatus(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4146 | |
| 4147 | if (!klass->IsVerified()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4148 | VerifyClass(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4149 | if (!klass->IsVerified()) { |
| 4150 | // We failed to verify, expect either the klass to be erroneous or verification failed at |
| 4151 | // compile time. |
| 4152 | if (klass->IsErroneous()) { |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 4153 | CHECK(self->IsExceptionPending()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4154 | VlogClassInitializationFailure(klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4155 | } else { |
| 4156 | CHECK(Runtime::Current()->IsCompiler()); |
| 4157 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 4158 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4159 | return false; |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4160 | } else { |
| 4161 | self->AssertNoPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4162 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4163 | } |
| 4164 | |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4165 | // If the class is kStatusInitializing, either this thread is |
| 4166 | // initializing higher up the stack or another thread has beat us |
| 4167 | // to initializing and we need to wait. Either way, this |
| 4168 | // invocation of InitializeClass will not be responsible for |
| 4169 | // running <clinit> and will return. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4170 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4171 | // Could have got an exception during verification. |
| 4172 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4173 | VlogClassInitializationFailure(klass); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4174 | return false; |
| 4175 | } |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 4176 | // We caught somebody else in the act; was it us? |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 4177 | if (klass->GetClinitThreadId() == self->GetTid()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4178 | // Yes. That's fine. Return so we can continue initializing. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4179 | return true; |
| 4180 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4181 | // No. That's fine. Wait for another thread to finish initializing. |
| 4182 | return WaitForInitializeClass(klass, self, lock); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4183 | } |
| 4184 | |
| 4185 | if (!ValidateSuperClassDescriptors(klass)) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4186 | klass->SetStatus(mirror::Class::kStatusError, self); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4187 | return false; |
| 4188 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4189 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4190 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4191 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusVerified) << PrettyClass(klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4192 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4193 | // From here out other threads may observe that we're initializing and so changes of state |
| 4194 | // require the a notification. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 4195 | klass->SetClinitThreadId(self->GetTid()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4196 | klass->SetStatus(mirror::Class::kStatusInitializing, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4197 | |
| 4198 | t0 = NanoTime(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4199 | } |
| 4200 | |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 4201 | // Initialize super classes, must be done while initializing for the JLS. |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4202 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
| 4203 | mirror::Class* super_class = klass->GetSuperClass(); |
| 4204 | if (!super_class->IsInitialized()) { |
| 4205 | CHECK(!super_class->IsInterface()); |
| 4206 | CHECK(can_init_parents); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4207 | StackHandleScope<1> hs(self); |
| 4208 | Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4209 | bool super_initialized = InitializeClass(self, handle_scope_super, can_init_statics, true); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4210 | if (!super_initialized) { |
| 4211 | // The super class was verified ahead of entering initializing, we should only be here if |
| 4212 | // the super class became erroneous due to initialization. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4213 | CHECK(handle_scope_super->IsErroneous() && self->IsExceptionPending()) |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4214 | << "Super class initialization failed for " |
| 4215 | << PrettyDescriptor(handle_scope_super.Get()) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4216 | << " that has unexpected status " << handle_scope_super->GetStatus() |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4217 | << "\nPending exception:\n" |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4218 | << (self->GetException(nullptr) != nullptr ? self->GetException(nullptr)->Dump() : ""); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4219 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4220 | // Initialization failed because the super-class is erroneous. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4221 | klass->SetStatus(mirror::Class::kStatusError, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4222 | return false; |
| 4223 | } |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 4224 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4225 | } |
| 4226 | |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4227 | const size_t num_static_fields = klass->NumStaticFields(); |
| 4228 | if (num_static_fields > 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4229 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4230 | CHECK(dex_class_def != nullptr); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4231 | const DexFile& dex_file = klass->GetDexFile(); |
Hiroshi Yamauchi | 67ef46a | 2014-08-21 15:59:43 -0700 | [diff] [blame] | 4232 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4233 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4234 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4235 | |
| 4236 | // Eagerly fill in static fields so that the we don't have to do as many expensive |
| 4237 | // Class::FindStaticField in ResolveField. |
| 4238 | for (size_t i = 0; i < num_static_fields; ++i) { |
| 4239 | mirror::ArtField* field = klass->GetStaticField(i); |
| 4240 | const uint32_t field_idx = field->GetDexFieldIndex(); |
| 4241 | mirror::ArtField* resolved_field = dex_cache->GetResolvedField(field_idx); |
| 4242 | if (resolved_field == nullptr) { |
| 4243 | dex_cache->SetResolvedField(field_idx, field); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4244 | } else { |
| 4245 | DCHECK_EQ(field, resolved_field); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4246 | } |
| 4247 | } |
| 4248 | |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4249 | EncodedStaticFieldValueIterator value_it(dex_file, &dex_cache, &class_loader, |
| 4250 | this, *dex_class_def); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 4251 | const uint8_t* class_data = dex_file.GetClassData(*dex_class_def); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4252 | ClassDataItemIterator field_it(dex_file, class_data); |
| 4253 | if (value_it.HasNext()) { |
| 4254 | DCHECK(field_it.HasNextStaticField()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4255 | CHECK(can_init_statics); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4256 | for ( ; value_it.HasNext(); value_it.Next(), field_it.Next()) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 4257 | StackHandleScope<1> hs2(self); |
| 4258 | Handle<mirror::ArtField> field(hs2.NewHandle( |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4259 | ResolveField(dex_file, field_it.GetMemberIndex(), dex_cache, class_loader, true))); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4260 | if (Runtime::Current()->IsActiveTransaction()) { |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4261 | value_it.ReadValueToField<true>(field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4262 | } else { |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4263 | value_it.ReadValueToField<false>(field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4264 | } |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4265 | DCHECK(!value_it.HasNext() || field_it.HasNextStaticField()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4266 | } |
| 4267 | } |
| 4268 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4269 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 4270 | mirror::ArtMethod* clinit = klass->FindClassInitializer(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4271 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4272 | CHECK(can_init_statics); |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 4273 | JValue result; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4274 | clinit->Invoke(self, nullptr, 0, &result, "V"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4275 | } |
| 4276 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4277 | self->AllowThreadSuspension(); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 4278 | uint64_t t1 = NanoTime(); |
| 4279 | |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4280 | bool success = true; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4281 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4282 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4283 | |
| 4284 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4285 | WrapExceptionInInitializer(klass); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4286 | klass->SetStatus(mirror::Class::kStatusError, self); |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4287 | success = false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4288 | } else { |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 4289 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 4290 | RuntimeStats* thread_stats = self->GetStats(); |
| 4291 | ++global_stats->class_init_count; |
| 4292 | ++thread_stats->class_init_count; |
| 4293 | global_stats->class_init_time_ns += (t1 - t0); |
| 4294 | thread_stats->class_init_time_ns += (t1 - t0); |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 4295 | // Set the class as initialized except if failed to initialize static fields. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4296 | klass->SetStatus(mirror::Class::kStatusInitialized, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4297 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4298 | std::string temp; |
| 4299 | LOG(INFO) << "Initialized class " << klass->GetDescriptor(&temp) << " from " << |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4300 | klass->GetLocation(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 4301 | } |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 4302 | // Opportunistically set static method trampolines to their destination. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4303 | FixupStaticTrampolines(klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4304 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4305 | } |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4306 | return success; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4307 | } |
| 4308 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4309 | bool ClassLinker::WaitForInitializeClass(Handle<mirror::Class> klass, Thread* self, |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4310 | ObjectLock<mirror::Class>& lock) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 4311 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4312 | while (true) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4313 | self->AssertNoPendingException(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4314 | CHECK(!klass->IsInitialized()); |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 4315 | lock.WaitIgnoringInterrupts(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4316 | |
| 4317 | // When we wake up, repeat the test for init-in-progress. If |
| 4318 | // there's an exception pending (only possible if |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4319 | // we were not using WaitIgnoringInterrupts), bail out. |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4320 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4321 | WrapExceptionInInitializer(klass); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4322 | klass->SetStatus(mirror::Class::kStatusError, self); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4323 | return false; |
| 4324 | } |
| 4325 | // Spurious wakeup? Go back to waiting. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4326 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4327 | continue; |
| 4328 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4329 | if (klass->GetStatus() == mirror::Class::kStatusVerified && Runtime::Current()->IsCompiler()) { |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 4330 | // Compile time initialization failed. |
| 4331 | return false; |
| 4332 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4333 | if (klass->IsErroneous()) { |
| 4334 | // The caller wants an exception, but it was thrown in a |
| 4335 | // different thread. Synthesize one here. |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 4336 | ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread", |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4337 | PrettyDescriptor(klass.Get()).c_str()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4338 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4339 | return false; |
| 4340 | } |
| 4341 | if (klass->IsInitialized()) { |
| 4342 | return true; |
| 4343 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4344 | LOG(FATAL) << "Unexpected class status. " << PrettyClass(klass.Get()) << " is " |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4345 | << klass->GetStatus(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4346 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 4347 | UNREACHABLE(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4348 | } |
| 4349 | |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4350 | static bool HasSameSignatureWithDifferentClassLoaders(Thread* self, |
| 4351 | Handle<mirror::ArtMethod> method1, |
| 4352 | Handle<mirror::ArtMethod> method2) |
| 4353 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4354 | { |
| 4355 | StackHandleScope<1> hs(self); |
| 4356 | Handle<mirror::Class> return_type(hs.NewHandle(method1->GetReturnType())); |
| 4357 | if (UNLIKELY(method2->GetReturnType() != return_type.Get())) { |
| 4358 | return false; |
| 4359 | } |
| 4360 | } |
| 4361 | const DexFile::TypeList* types1 = method1->GetParameterTypeList(); |
| 4362 | const DexFile::TypeList* types2 = method2->GetParameterTypeList(); |
| 4363 | if (types1 == nullptr) { |
| 4364 | return (types2 == nullptr) || (types2->Size() == 0); |
| 4365 | } else if (UNLIKELY(types2 == nullptr)) { |
| 4366 | return types1->Size() == 0; |
| 4367 | } |
| 4368 | uint32_t num_types = types1->Size(); |
| 4369 | if (UNLIKELY(num_types != types2->Size())) { |
| 4370 | return false; |
| 4371 | } |
| 4372 | for (uint32_t i = 0; i < num_types; ++i) { |
| 4373 | mirror::Class* param_type = |
| 4374 | method1->GetClassFromTypeIndex(types1->GetTypeItem(i).type_idx_, true); |
| 4375 | mirror::Class* other_param_type = |
| 4376 | method2->GetClassFromTypeIndex(types2->GetTypeItem(i).type_idx_, true); |
| 4377 | if (UNLIKELY(param_type != other_param_type)) { |
| 4378 | return false; |
| 4379 | } |
| 4380 | } |
| 4381 | return true; |
| 4382 | } |
| 4383 | |
| 4384 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4385 | bool ClassLinker::ValidateSuperClassDescriptors(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4386 | if (klass->IsInterface()) { |
| 4387 | return true; |
| 4388 | } |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 4389 | // Begin with the methods local to the superclass. |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 4390 | Thread* self = Thread::Current(); |
| 4391 | StackHandleScope<2> hs(self); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4392 | MutableHandle<mirror::ArtMethod> h_m(hs.NewHandle<mirror::ArtMethod>(nullptr)); |
| 4393 | MutableHandle<mirror::ArtMethod> super_h_m(hs.NewHandle<mirror::ArtMethod>(nullptr)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4394 | if (klass->HasSuperClass() && |
| 4395 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4396 | for (int i = klass->GetSuperClass()->GetVTableLength() - 1; i >= 0; --i) { |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4397 | h_m.Assign(klass->GetVTableEntry(i)); |
| 4398 | super_h_m.Assign(klass->GetSuperClass()->GetVTableEntry(i)); |
| 4399 | if (h_m.Get() != super_h_m.Get() && |
| 4400 | !HasSameSignatureWithDifferentClassLoaders(self, h_m, super_h_m)) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4401 | ThrowLinkageError(klass.Get(), |
| 4402 | "Class %s method %s resolves differently in superclass %s", |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4403 | PrettyDescriptor(klass.Get()).c_str(), |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4404 | PrettyMethod(h_m.Get()).c_str(), |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 4405 | PrettyDescriptor(klass->GetSuperClass()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4406 | return false; |
| 4407 | } |
| 4408 | } |
| 4409 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 4410 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 4411 | if (klass->GetClassLoader() != klass->GetIfTable()->GetInterface(i)->GetClassLoader()) { |
| 4412 | uint32_t num_methods = klass->GetIfTable()->GetInterface(i)->NumVirtualMethods(); |
| 4413 | for (uint32_t j = 0; j < num_methods; ++j) { |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4414 | h_m.Assign(klass->GetIfTable()->GetMethodArray(i)->GetWithoutChecks(j)); |
| 4415 | super_h_m.Assign(klass->GetIfTable()->GetInterface(i)->GetVirtualMethod(j)); |
| 4416 | if (h_m.Get() != super_h_m.Get() && |
| 4417 | !HasSameSignatureWithDifferentClassLoaders(self, h_m, super_h_m)) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4418 | ThrowLinkageError(klass.Get(), |
| 4419 | "Class %s method %s resolves differently in interface %s", |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4420 | PrettyDescriptor(klass.Get()).c_str(), |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4421 | PrettyMethod(h_m.Get()).c_str(), |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 4422 | PrettyDescriptor(klass->GetIfTable()->GetInterface(i)).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4423 | return false; |
| 4424 | } |
| 4425 | } |
| 4426 | } |
| 4427 | } |
| 4428 | return true; |
| 4429 | } |
| 4430 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4431 | bool ClassLinker::EnsureInitialized(Thread* self, Handle<mirror::Class> c, bool can_init_fields, |
| 4432 | bool can_init_parents) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4433 | DCHECK(c.Get() != nullptr); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4434 | if (c->IsInitialized()) { |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4435 | EnsurePreverifiedMethods(c); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4436 | return true; |
| 4437 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4438 | const bool success = InitializeClass(self, c, can_init_fields, can_init_parents); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4439 | if (!success) { |
| 4440 | if (can_init_fields && can_init_parents) { |
| 4441 | CHECK(self->IsExceptionPending()) << PrettyClass(c.Get()); |
| 4442 | } |
| 4443 | } else { |
| 4444 | self->AssertNoPendingException(); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 4445 | } |
| 4446 | return success; |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 4447 | } |
| 4448 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4449 | void ClassLinker::FixupTemporaryDeclaringClass(mirror::Class* temp_class, mirror::Class* new_class) { |
| 4450 | mirror::ObjectArray<mirror::ArtField>* fields = new_class->GetIFields(); |
| 4451 | if (fields != nullptr) { |
| 4452 | for (int index = 0; index < fields->GetLength(); index ++) { |
| 4453 | if (fields->Get(index)->GetDeclaringClass() == temp_class) { |
| 4454 | fields->Get(index)->SetDeclaringClass(new_class); |
| 4455 | } |
| 4456 | } |
| 4457 | } |
| 4458 | |
| 4459 | fields = new_class->GetSFields(); |
| 4460 | if (fields != nullptr) { |
| 4461 | for (int index = 0; index < fields->GetLength(); index ++) { |
| 4462 | if (fields->Get(index)->GetDeclaringClass() == temp_class) { |
| 4463 | fields->Get(index)->SetDeclaringClass(new_class); |
| 4464 | } |
| 4465 | } |
| 4466 | } |
| 4467 | |
| 4468 | mirror::ObjectArray<mirror::ArtMethod>* methods = new_class->GetDirectMethods(); |
| 4469 | if (methods != nullptr) { |
| 4470 | for (int index = 0; index < methods->GetLength(); index ++) { |
| 4471 | if (methods->Get(index)->GetDeclaringClass() == temp_class) { |
| 4472 | methods->Get(index)->SetDeclaringClass(new_class); |
| 4473 | } |
| 4474 | } |
| 4475 | } |
| 4476 | |
| 4477 | methods = new_class->GetVirtualMethods(); |
| 4478 | if (methods != nullptr) { |
| 4479 | for (int index = 0; index < methods->GetLength(); index ++) { |
| 4480 | if (methods->Get(index)->GetDeclaringClass() == temp_class) { |
| 4481 | methods->Get(index)->SetDeclaringClass(new_class); |
| 4482 | } |
| 4483 | } |
| 4484 | } |
| 4485 | } |
| 4486 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4487 | bool ClassLinker::LinkClass(Thread* self, const char* descriptor, Handle<mirror::Class> klass, |
| 4488 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4489 | mirror::Class** new_class) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4490 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4491 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4492 | if (!LinkSuperClass(klass)) { |
| 4493 | return false; |
| 4494 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4495 | StackHandleScope<mirror::Class::kImtSize> imt_handle_scope( |
| 4496 | self, Runtime::Current()->GetImtUnimplementedMethod()); |
| 4497 | if (!LinkMethods(self, klass, interfaces, &imt_handle_scope)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4498 | return false; |
| 4499 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4500 | if (!LinkInstanceFields(self, klass)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4501 | return false; |
| 4502 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4503 | size_t class_size; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4504 | if (!LinkStaticFields(self, klass, &class_size)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 4505 | return false; |
| 4506 | } |
| 4507 | CreateReferenceInstanceOffsets(klass); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4508 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4509 | |
| 4510 | if (!klass->IsTemp() || (!init_done_ && klass->GetClassSize() == class_size)) { |
| 4511 | // We don't need to retire this class as it has no embedded tables or it was created the |
| 4512 | // correct size during class linker initialization. |
| 4513 | CHECK_EQ(klass->GetClassSize(), class_size) << PrettyDescriptor(klass.Get()); |
| 4514 | |
| 4515 | if (klass->ShouldHaveEmbeddedImtAndVTable()) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4516 | klass->PopulateEmbeddedImtAndVTable(&imt_handle_scope); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4517 | } |
| 4518 | |
| 4519 | // This will notify waiters on klass that saw the not yet resolved |
| 4520 | // class in the class_table_ during EnsureResolved. |
| 4521 | klass->SetStatus(mirror::Class::kStatusResolved, self); |
| 4522 | *new_class = klass.Get(); |
| 4523 | } else { |
| 4524 | CHECK(!klass->IsResolved()); |
| 4525 | // Retire the temporary class and create the correctly sized resolved class. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4526 | *new_class = klass->CopyOf(self, class_size, &imt_handle_scope); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4527 | if (UNLIKELY(*new_class == nullptr)) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4528 | CHECK(self->IsExceptionPending()); // Expect an OOME. |
| 4529 | klass->SetStatus(mirror::Class::kStatusError, self); |
| 4530 | return false; |
| 4531 | } |
| 4532 | |
| 4533 | CHECK_EQ((*new_class)->GetClassSize(), class_size); |
| 4534 | StackHandleScope<1> hs(self); |
| 4535 | auto new_class_h = hs.NewHandleWrapper<mirror::Class>(new_class); |
| 4536 | ObjectLock<mirror::Class> lock(self, new_class_h); |
| 4537 | |
| 4538 | FixupTemporaryDeclaringClass(klass.Get(), new_class_h.Get()); |
| 4539 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 4540 | mirror::Class* existing = UpdateClass(descriptor, new_class_h.Get(), |
| 4541 | ComputeModifiedUtf8Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4542 | CHECK(existing == nullptr || existing == klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4543 | |
| 4544 | // This will notify waiters on temp class that saw the not yet resolved class in the |
| 4545 | // class_table_ during EnsureResolved. |
| 4546 | klass->SetStatus(mirror::Class::kStatusRetired, self); |
| 4547 | |
| 4548 | CHECK_EQ(new_class_h->GetStatus(), mirror::Class::kStatusResolving); |
| 4549 | // This will notify waiters on new_class that saw the not yet resolved |
| 4550 | // class in the class_table_ during EnsureResolved. |
| 4551 | new_class_h->SetStatus(mirror::Class::kStatusResolved, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4552 | } |
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::LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4557 | CHECK_EQ(mirror::Class::kStatusIdx, klass->GetStatus()); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4558 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
| 4559 | uint16_t super_class_idx = class_def.superclass_idx_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4560 | if (super_class_idx != DexFile::kDexNoIndex16) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4561 | mirror::Class* super_class = ResolveType(dex_file, super_class_idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4562 | if (super_class == nullptr) { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 4563 | DCHECK(Thread::Current()->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4564 | return false; |
| 4565 | } |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 4566 | // Verify |
| 4567 | if (!klass->CanAccess(super_class)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4568 | ThrowIllegalAccessError(klass.Get(), "Class %s extended by class %s is inaccessible", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4569 | PrettyDescriptor(super_class).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4570 | PrettyDescriptor(klass.Get()).c_str()); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 4571 | return false; |
| 4572 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4573 | CHECK(super_class->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4574 | klass->SetSuperClass(super_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4575 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4576 | const DexFile::TypeList* interfaces = dex_file.GetInterfacesList(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4577 | if (interfaces != nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4578 | for (size_t i = 0; i < interfaces->Size(); i++) { |
| 4579 | uint16_t idx = interfaces->GetTypeItem(i).type_idx_; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4580 | mirror::Class* interface = ResolveType(dex_file, idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4581 | if (interface == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4582 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4583 | return false; |
| 4584 | } |
| 4585 | // Verify |
| 4586 | if (!klass->CanAccess(interface)) { |
| 4587 | // TODO: the RI seemed to ignore this in my testing. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4588 | ThrowIllegalAccessError(klass.Get(), "Interface %s implemented by class %s is inaccessible", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4589 | PrettyDescriptor(interface).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4590 | PrettyDescriptor(klass.Get()).c_str()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4591 | return false; |
| 4592 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4593 | } |
| 4594 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4595 | // Mark the class as loaded. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4596 | klass->SetStatus(mirror::Class::kStatusLoaded, nullptr); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4597 | return true; |
| 4598 | } |
| 4599 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4600 | bool ClassLinker::LinkSuperClass(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4601 | CHECK(!klass->IsPrimitive()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4602 | mirror::Class* super = klass->GetSuperClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4603 | if (klass.Get() == GetClassRoot(kJavaLangObject)) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4604 | if (super != nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4605 | ThrowClassFormatError(klass.Get(), "java.lang.Object must not have a superclass"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4606 | return false; |
| 4607 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4608 | return true; |
| 4609 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4610 | if (super == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4611 | ThrowLinkageError(klass.Get(), "No superclass defined for class %s", |
| 4612 | PrettyDescriptor(klass.Get()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4613 | return false; |
| 4614 | } |
| 4615 | // Verify |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 4616 | if (super->IsFinal() || super->IsInterface()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4617 | ThrowIncompatibleClassChangeError(klass.Get(), "Superclass %s of %s is %s", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4618 | PrettyDescriptor(super).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4619 | PrettyDescriptor(klass.Get()).c_str(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4620 | super->IsFinal() ? "declared final" : "an interface"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4621 | return false; |
| 4622 | } |
| 4623 | if (!klass->CanAccess(super)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4624 | ThrowIllegalAccessError(klass.Get(), "Superclass %s is inaccessible to class %s", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4625 | PrettyDescriptor(super).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4626 | PrettyDescriptor(klass.Get()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4627 | return false; |
| 4628 | } |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 4629 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4630 | // Inherit kAccClassIsFinalizable from the superclass in case this |
| 4631 | // class doesn't override finalize. |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 4632 | if (super->IsFinalizable()) { |
| 4633 | klass->SetFinalizable(); |
| 4634 | } |
| 4635 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 4636 | // Inherit reference flags (if any) from the superclass. |
| 4637 | int reference_flags = (super->GetAccessFlags() & kAccReferenceFlagsMask); |
| 4638 | if (reference_flags != 0) { |
| 4639 | klass->SetAccessFlags(klass->GetAccessFlags() | reference_flags); |
| 4640 | } |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 4641 | // Disallow custom direct subclasses of java.lang.ref.Reference. |
Elliott Hughes | bf61ba3 | 2011-10-11 10:53:09 -0700 | [diff] [blame] | 4642 | if (init_done_ && super == GetClassRoot(kJavaLangRefReference)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4643 | ThrowLinkageError(klass.Get(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4644 | "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] | 4645 | PrettyDescriptor(klass.Get()).c_str()); |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 4646 | return false; |
| 4647 | } |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 4648 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4649 | if (kIsDebugBuild) { |
| 4650 | // Ensure super classes are fully resolved prior to resolving fields.. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4651 | while (super != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4652 | CHECK(super->IsResolved()); |
| 4653 | super = super->GetSuperClass(); |
| 4654 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4655 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4656 | return true; |
| 4657 | } |
| 4658 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4659 | // Populate the class vtable and itable. Compute return type indices. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4660 | bool ClassLinker::LinkMethods(Thread* self, Handle<mirror::Class> klass, |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4661 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
| 4662 | StackHandleScope<mirror::Class::kImtSize>* out_imt) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4663 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4664 | if (klass->IsInterface()) { |
| 4665 | // No vtable. |
| 4666 | size_t count = klass->NumVirtualMethods(); |
| 4667 | if (!IsUint(16, count)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4668 | ThrowClassFormatError(klass.Get(), "Too many methods on interface: %zd", count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4669 | return false; |
| 4670 | } |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 4671 | for (size_t i = 0; i < count; ++i) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4672 | klass->GetVirtualMethodDuringLinking(i)->SetMethodIndex(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4673 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4674 | } else if (!LinkVirtualMethods(self, klass)) { // Link virtual methods first. |
| 4675 | return false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4676 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4677 | return LinkInterfaceMethods(self, klass, interfaces, out_imt); // Link interface method last. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4678 | } |
| 4679 | |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 4680 | // Comparator for name and signature of a method, used in finding overriding methods. Implementation |
| 4681 | // avoids the use of handles, if it didn't then rather than compare dex files we could compare dex |
| 4682 | // caches in the implementation below. |
| 4683 | class MethodNameAndSignatureComparator FINAL : public ValueObject { |
| 4684 | public: |
| 4685 | explicit MethodNameAndSignatureComparator(mirror::ArtMethod* method) |
| 4686 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) : |
| 4687 | dex_file_(method->GetDexFile()), mid_(&dex_file_->GetMethodId(method->GetDexMethodIndex())), |
| 4688 | name_(nullptr), name_len_(0) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4689 | DCHECK(!method->IsProxyMethod()) << PrettyMethod(method); |
| 4690 | } |
| 4691 | |
| 4692 | const char* GetName() { |
| 4693 | if (name_ == nullptr) { |
| 4694 | name_ = dex_file_->StringDataAndUtf16LengthByIdx(mid_->name_idx_, &name_len_); |
| 4695 | } |
| 4696 | return name_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 4697 | } |
| 4698 | |
| 4699 | bool HasSameNameAndSignature(mirror::ArtMethod* other) |
| 4700 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4701 | DCHECK(!other->IsProxyMethod()) << PrettyMethod(other); |
| 4702 | const DexFile* other_dex_file = other->GetDexFile(); |
| 4703 | const DexFile::MethodId& other_mid = other_dex_file->GetMethodId(other->GetDexMethodIndex()); |
| 4704 | if (dex_file_ == other_dex_file) { |
| 4705 | return mid_->name_idx_ == other_mid.name_idx_ && mid_->proto_idx_ == other_mid.proto_idx_; |
| 4706 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4707 | GetName(); // Only used to make sure its calculated. |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 4708 | uint32_t other_name_len; |
| 4709 | const char* other_name = other_dex_file->StringDataAndUtf16LengthByIdx(other_mid.name_idx_, |
| 4710 | &other_name_len); |
| 4711 | if (name_len_ != other_name_len || strcmp(name_, other_name) != 0) { |
| 4712 | return false; |
| 4713 | } |
| 4714 | return dex_file_->GetMethodSignature(*mid_) == other_dex_file->GetMethodSignature(other_mid); |
| 4715 | } |
| 4716 | |
| 4717 | private: |
| 4718 | // Dex file for the method to compare against. |
| 4719 | const DexFile* const dex_file_; |
| 4720 | // MethodId for the method to compare against. |
| 4721 | const DexFile::MethodId* const mid_; |
| 4722 | // Lazily computed name from the dex file's strings. |
| 4723 | const char* name_; |
| 4724 | // Lazily computed name length. |
| 4725 | uint32_t name_len_; |
| 4726 | }; |
| 4727 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4728 | class LinkVirtualHashTable { |
| 4729 | public: |
| 4730 | LinkVirtualHashTable(Handle<mirror::Class> klass, size_t hash_size, uint32_t* hash_table) |
| 4731 | : klass_(klass), hash_size_(hash_size), hash_table_(hash_table) { |
| 4732 | std::fill(hash_table_, hash_table_ + hash_size_, invalid_index_); |
| 4733 | } |
| 4734 | void Add(uint32_t virtual_method_index) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4735 | mirror::ArtMethod* local_method = klass_->GetVirtualMethodDuringLinking(virtual_method_index); |
| 4736 | const char* name = local_method->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 4737 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4738 | uint32_t index = hash % hash_size_; |
| 4739 | // Linear probe until we have an empty slot. |
| 4740 | while (hash_table_[index] != invalid_index_) { |
| 4741 | if (++index == hash_size_) { |
| 4742 | index = 0; |
| 4743 | } |
| 4744 | } |
| 4745 | hash_table_[index] = virtual_method_index; |
| 4746 | } |
| 4747 | uint32_t FindAndRemove(MethodNameAndSignatureComparator* comparator) |
| 4748 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4749 | const char* name = comparator->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 4750 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4751 | size_t index = hash % hash_size_; |
| 4752 | while (true) { |
| 4753 | const uint32_t value = hash_table_[index]; |
| 4754 | // Since linear probe makes continuous blocks, hitting an invalid index means we are done |
| 4755 | // the block and can safely assume not found. |
| 4756 | if (value == invalid_index_) { |
| 4757 | break; |
| 4758 | } |
| 4759 | if (value != removed_index_) { // This signifies not already overriden. |
| 4760 | mirror::ArtMethod* virtual_method = |
| 4761 | klass_->GetVirtualMethodDuringLinking(value); |
| 4762 | if (comparator->HasSameNameAndSignature(virtual_method->GetInterfaceMethodIfProxy())) { |
| 4763 | hash_table_[index] = removed_index_; |
| 4764 | return value; |
| 4765 | } |
| 4766 | } |
| 4767 | if (++index == hash_size_) { |
| 4768 | index = 0; |
| 4769 | } |
| 4770 | } |
| 4771 | return GetNotFoundIndex(); |
| 4772 | } |
| 4773 | static uint32_t GetNotFoundIndex() { |
| 4774 | return invalid_index_; |
| 4775 | } |
| 4776 | |
| 4777 | private: |
| 4778 | static const uint32_t invalid_index_; |
| 4779 | static const uint32_t removed_index_; |
| 4780 | |
| 4781 | Handle<mirror::Class> klass_; |
| 4782 | const size_t hash_size_; |
| 4783 | uint32_t* const hash_table_; |
| 4784 | }; |
| 4785 | |
| 4786 | const uint32_t LinkVirtualHashTable::invalid_index_ = std::numeric_limits<uint32_t>::max(); |
| 4787 | const uint32_t LinkVirtualHashTable::removed_index_ = std::numeric_limits<uint32_t>::max() - 1; |
| 4788 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4789 | bool ClassLinker::LinkVirtualMethods(Thread* self, Handle<mirror::Class> klass) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4790 | const size_t num_virtual_methods = klass->NumVirtualMethods(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4791 | if (klass->HasSuperClass()) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4792 | const size_t super_vtable_length = klass->GetSuperClass()->GetVTableLength(); |
| 4793 | const size_t max_count = num_virtual_methods + super_vtable_length; |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4794 | StackHandleScope<2> hs(self); |
Mingyao Yang | 38eecb0 | 2014-08-13 14:51:03 -0700 | [diff] [blame] | 4795 | Handle<mirror::Class> super_class(hs.NewHandle(klass->GetSuperClass())); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4796 | MutableHandle<mirror::ObjectArray<mirror::ArtMethod>> vtable; |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4797 | if (super_class->ShouldHaveEmbeddedImtAndVTable()) { |
| 4798 | vtable = hs.NewHandle(AllocArtMethodArray(self, max_count)); |
| 4799 | if (UNLIKELY(vtable.Get() == nullptr)) { |
| 4800 | CHECK(self->IsExceptionPending()); // OOME. |
| 4801 | return false; |
| 4802 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4803 | for (size_t i = 0; i < super_vtable_length; i++) { |
| 4804 | vtable->SetWithoutChecks<false>(i, super_class->GetEmbeddedVTableEntry(i)); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4805 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4806 | if (num_virtual_methods == 0) { |
| 4807 | klass->SetVTable(vtable.Get()); |
| 4808 | return true; |
| 4809 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4810 | } else { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4811 | mirror::ObjectArray<mirror::ArtMethod>* super_vtable = super_class->GetVTable(); |
| 4812 | CHECK(super_vtable != nullptr) << PrettyClass(super_class.Get()); |
| 4813 | if (num_virtual_methods == 0) { |
| 4814 | klass->SetVTable(super_vtable); |
| 4815 | return true; |
| 4816 | } |
| 4817 | vtable = hs.NewHandle(super_vtable->CopyOf(self, max_count)); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4818 | if (UNLIKELY(vtable.Get() == nullptr)) { |
| 4819 | CHECK(self->IsExceptionPending()); // OOME. |
| 4820 | return false; |
| 4821 | } |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4822 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4823 | // How the algorithm works: |
| 4824 | // 1. Populate hash table by adding num_virtual_methods from klass. The values in the hash |
| 4825 | // table are: invalid_index for unused slots, index super_vtable_length + i for a virtual |
| 4826 | // method which has not been matched to a vtable method, and j if the virtual method at the |
| 4827 | // index overrode the super virtual method at index j. |
| 4828 | // 2. Loop through super virtual methods, if they overwrite, update hash table to j |
| 4829 | // (j < super_vtable_length) to avoid redundant checks. (TODO maybe use this info for reducing |
| 4830 | // the need for the initial vtable which we later shrink back down). |
| 4831 | // 3. Add non overridden methods to the end of the vtable. |
| 4832 | static constexpr size_t kMaxStackHash = 250; |
| 4833 | const size_t hash_table_size = num_virtual_methods * 3; |
| 4834 | uint32_t* hash_table_ptr; |
| 4835 | std::unique_ptr<uint32_t[]> hash_heap_storage; |
| 4836 | if (hash_table_size <= kMaxStackHash) { |
| 4837 | hash_table_ptr = reinterpret_cast<uint32_t*>( |
| 4838 | alloca(hash_table_size * sizeof(*hash_table_ptr))); |
| 4839 | } else { |
| 4840 | hash_heap_storage.reset(new uint32_t[hash_table_size]); |
| 4841 | hash_table_ptr = hash_heap_storage.get(); |
| 4842 | } |
| 4843 | LinkVirtualHashTable hash_table(klass, hash_table_size, hash_table_ptr); |
| 4844 | // Add virtual methods to the hash table. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4845 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4846 | hash_table.Add(i); |
| 4847 | } |
| 4848 | // Loop through each super vtable method and see if they are overriden by a method we added to |
| 4849 | // the hash table. |
| 4850 | for (size_t j = 0; j < super_vtable_length; ++j) { |
| 4851 | // Search the hash table to see if we are overidden by any method. |
| 4852 | mirror::ArtMethod* super_method = vtable->GetWithoutChecks(j); |
| 4853 | MethodNameAndSignatureComparator super_method_name_comparator( |
| 4854 | super_method->GetInterfaceMethodIfProxy()); |
| 4855 | uint32_t hash_index = hash_table.FindAndRemove(&super_method_name_comparator); |
| 4856 | if (hash_index != hash_table.GetNotFoundIndex()) { |
| 4857 | mirror::ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(hash_index); |
| 4858 | if (klass->CanAccessMember(super_method->GetDeclaringClass(), |
| 4859 | super_method->GetAccessFlags())) { |
| 4860 | if (super_method->IsFinal()) { |
| 4861 | ThrowLinkageError(klass.Get(), "Method %s overrides final method in class %s", |
| 4862 | PrettyMethod(virtual_method).c_str(), |
| 4863 | super_method->GetDeclaringClassDescriptor()); |
| 4864 | return false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4865 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4866 | vtable->SetWithoutChecks<false>(j, virtual_method); |
| 4867 | virtual_method->SetMethodIndex(j); |
| 4868 | } else { |
| 4869 | LOG(WARNING) << "Before Android 4.1, method " << PrettyMethod(virtual_method) |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4870 | << " would have incorrectly overridden the package-private method in " |
| 4871 | << PrettyDescriptor(super_method->GetDeclaringClassDescriptor()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4872 | } |
| 4873 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4874 | } |
| 4875 | // Add the non overridden methods at the end. |
| 4876 | size_t actual_count = super_vtable_length; |
| 4877 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
| 4878 | mirror::ArtMethod* local_method = klass->GetVirtualMethodDuringLinking(i); |
| 4879 | size_t method_idx = local_method->GetMethodIndexDuringLinking(); |
| 4880 | if (method_idx < super_vtable_length && |
| 4881 | local_method == vtable->GetWithoutChecks(method_idx)) { |
| 4882 | continue; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4883 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4884 | vtable->SetWithoutChecks<false>(actual_count, local_method); |
| 4885 | local_method->SetMethodIndex(actual_count); |
| 4886 | ++actual_count; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4887 | } |
| 4888 | if (!IsUint(16, actual_count)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4889 | ThrowClassFormatError(klass.Get(), "Too many methods defined on class: %zd", actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4890 | return false; |
| 4891 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4892 | // Shrink vtable if possible |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4893 | CHECK_LE(actual_count, max_count); |
| 4894 | if (actual_count < max_count) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4895 | vtable.Assign(vtable->CopyOf(self, actual_count)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4896 | if (UNLIKELY(vtable.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4897 | CHECK(self->IsExceptionPending()); // OOME. |
| 4898 | return false; |
| 4899 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4900 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4901 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4902 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4903 | CHECK_EQ(klass.Get(), GetClassRoot(kJavaLangObject)); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 4904 | if (!IsUint(16, num_virtual_methods)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4905 | ThrowClassFormatError(klass.Get(), "Too many methods: %d", |
| 4906 | static_cast<int>(num_virtual_methods)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4907 | return false; |
| 4908 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4909 | mirror::ObjectArray<mirror::ArtMethod>* vtable = AllocArtMethodArray(self, num_virtual_methods); |
| 4910 | if (UNLIKELY(vtable == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4911 | CHECK(self->IsExceptionPending()); // OOME. |
| 4912 | return false; |
| 4913 | } |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 4914 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4915 | mirror::ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4916 | vtable->SetWithoutChecks<false>(i, virtual_method); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4917 | virtual_method->SetMethodIndex(i & 0xFFFF); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4918 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4919 | klass->SetVTable(vtable); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4920 | } |
| 4921 | return true; |
| 4922 | } |
| 4923 | |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4924 | bool ClassLinker::LinkInterfaceMethods(Thread* self, Handle<mirror::Class> klass, |
| 4925 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
| 4926 | StackHandleScope<mirror::Class::kImtSize>* out_imt) { |
Mathieu Chartier | d035c2d | 2014-10-27 17:30:20 -0700 | [diff] [blame] | 4927 | StackHandleScope<3> hs(self); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4928 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4929 | const bool has_superclass = klass->HasSuperClass(); |
| 4930 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
| 4931 | const bool have_interfaces = interfaces.Get() != nullptr; |
| 4932 | const size_t num_interfaces = |
| 4933 | have_interfaces ? interfaces->GetLength() : klass->NumDirectInterfaces(); |
| 4934 | if (num_interfaces == 0) { |
| 4935 | if (super_ifcount == 0) { |
| 4936 | // Class implements no interfaces. |
| 4937 | DCHECK_EQ(klass->GetIfTableCount(), 0); |
| 4938 | DCHECK(klass->GetIfTable() == nullptr); |
| 4939 | return true; |
| 4940 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4941 | // Class implements same interfaces as parent, are any of these not marker interfaces? |
| 4942 | bool has_non_marker_interface = false; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4943 | mirror::IfTable* super_iftable = klass->GetSuperClass()->GetIfTable(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4944 | for (size_t i = 0; i < super_ifcount; ++i) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4945 | if (super_iftable->GetMethodArrayCount(i) > 0) { |
| 4946 | has_non_marker_interface = true; |
| 4947 | break; |
| 4948 | } |
| 4949 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4950 | // Class just inherits marker interfaces from parent so recycle parent's iftable. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4951 | if (!has_non_marker_interface) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4952 | klass->SetIfTable(super_iftable); |
| 4953 | return true; |
| 4954 | } |
| 4955 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4956 | size_t ifcount = super_ifcount + num_interfaces; |
| 4957 | for (size_t i = 0; i < num_interfaces; i++) { |
| 4958 | mirror::Class* interface = have_interfaces ? |
| 4959 | interfaces->GetWithoutChecks(i) : mirror::Class::GetDirectInterface(self, klass, i); |
| 4960 | DCHECK(interface != nullptr); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4961 | if (UNLIKELY(!interface->IsInterface())) { |
| 4962 | std::string temp; |
| 4963 | ThrowIncompatibleClassChangeError(klass.Get(), "Class %s implements non-interface class %s", |
| 4964 | PrettyDescriptor(klass.Get()).c_str(), |
| 4965 | PrettyDescriptor(interface->GetDescriptor(&temp)).c_str()); |
| 4966 | return false; |
| 4967 | } |
| 4968 | ifcount += interface->GetIfTableCount(); |
| 4969 | } |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4970 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(AllocIfTable(self, ifcount))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4971 | if (UNLIKELY(iftable.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4972 | CHECK(self->IsExceptionPending()); // OOME. |
| 4973 | return false; |
| 4974 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4975 | if (super_ifcount != 0) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4976 | mirror::IfTable* super_iftable = klass->GetSuperClass()->GetIfTable(); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 4977 | for (size_t i = 0; i < super_ifcount; i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4978 | mirror::Class* super_interface = super_iftable->GetInterface(i); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4979 | iftable->SetInterface(i, super_interface); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 4980 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4981 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4982 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4983 | // Flatten the interface inheritance hierarchy. |
| 4984 | size_t idx = super_ifcount; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4985 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | d035c2d | 2014-10-27 17:30:20 -0700 | [diff] [blame] | 4986 | mirror::Class* interface = have_interfaces ? interfaces->Get(i) : |
| 4987 | mirror::Class::GetDirectInterface(self, klass, i); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4988 | // Check if interface is already in iftable |
| 4989 | bool duplicate = false; |
| 4990 | for (size_t j = 0; j < idx; j++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4991 | mirror::Class* existing_interface = iftable->GetInterface(j); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4992 | if (existing_interface == interface) { |
| 4993 | duplicate = true; |
| 4994 | break; |
| 4995 | } |
| 4996 | } |
| 4997 | if (!duplicate) { |
| 4998 | // Add this non-duplicate interface. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4999 | iftable->SetInterface(idx++, interface); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5000 | // Add this interface's non-duplicate super-interfaces. |
| 5001 | for (int32_t j = 0; j < interface->GetIfTableCount(); j++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5002 | mirror::Class* super_interface = interface->GetIfTable()->GetInterface(j); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5003 | bool super_duplicate = false; |
| 5004 | for (size_t k = 0; k < idx; k++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5005 | mirror::Class* existing_interface = iftable->GetInterface(k); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5006 | if (existing_interface == super_interface) { |
| 5007 | super_duplicate = true; |
| 5008 | break; |
| 5009 | } |
| 5010 | } |
| 5011 | if (!super_duplicate) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5012 | iftable->SetInterface(idx++, super_interface); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5013 | } |
| 5014 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5015 | } |
| 5016 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5017 | self->AllowThreadSuspension(); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5018 | // Shrink iftable in case duplicates were found |
| 5019 | if (idx < ifcount) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5020 | DCHECK_NE(num_interfaces, 0U); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5021 | 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] | 5022 | if (UNLIKELY(iftable.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5023 | CHECK(self->IsExceptionPending()); // OOME. |
| 5024 | return false; |
| 5025 | } |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5026 | ifcount = idx; |
| 5027 | } else { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5028 | DCHECK_EQ(idx, ifcount); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5029 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5030 | klass->SetIfTable(iftable.Get()); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 5031 | // 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] | 5032 | if (klass->IsInterface()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5033 | return true; |
| 5034 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5035 | size_t miranda_list_size = 0; |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5036 | size_t max_miranda_methods = 0; // The max size of miranda_list. |
| 5037 | for (size_t i = 0; i < ifcount; ++i) { |
| 5038 | max_miranda_methods += iftable->GetInterface(i)->NumVirtualMethods(); |
| 5039 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5040 | MutableHandle<mirror::ObjectArray<mirror::ArtMethod>> |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5041 | miranda_list(hs.NewHandle(AllocArtMethodArray(self, max_miranda_methods))); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5042 | MutableHandle<mirror::ObjectArray<mirror::ArtMethod>> vtable( |
| 5043 | hs.NewHandle(klass->GetVTableDuringLinking())); |
| 5044 | // Copy the IMT from the super class if possible. |
| 5045 | bool extend_super_iftable = false; |
| 5046 | if (has_superclass) { |
| 5047 | mirror::Class* super_class = klass->GetSuperClass(); |
| 5048 | extend_super_iftable = true; |
| 5049 | if (super_class->ShouldHaveEmbeddedImtAndVTable()) { |
| 5050 | for (size_t i = 0; i < mirror::Class::kImtSize; ++i) { |
| 5051 | out_imt->SetReference(i, super_class->GetEmbeddedImTableEntry(i)); |
| 5052 | } |
| 5053 | } else { |
| 5054 | // No imt in the super class, need to reconstruct from the iftable. |
| 5055 | mirror::IfTable* if_table = super_class->GetIfTable(); |
| 5056 | mirror::ArtMethod* conflict_method = runtime->GetImtConflictMethod(); |
| 5057 | const size_t length = super_class->GetIfTableCount(); |
| 5058 | for (size_t i = 0; i < length; ++i) { |
| 5059 | mirror::Class* interface = iftable->GetInterface(i); |
| 5060 | const size_t num_virtuals = interface->NumVirtualMethods(); |
| 5061 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 5062 | DCHECK_EQ(num_virtuals, method_array_count); |
| 5063 | if (method_array_count == 0) { |
| 5064 | continue; |
| 5065 | } |
| 5066 | mirror::ObjectArray<mirror::ArtMethod>* method_array = if_table->GetMethodArray(i); |
| 5067 | for (size_t j = 0; j < num_virtuals; ++j) { |
| 5068 | mirror::ArtMethod* method = method_array->GetWithoutChecks(j); |
| 5069 | if (method->IsMiranda()) { |
| 5070 | continue; |
| 5071 | } |
| 5072 | mirror::ArtMethod* interface_method = interface->GetVirtualMethod(j); |
| 5073 | uint32_t imt_index = interface_method->GetDexMethodIndex() % mirror::Class::kImtSize; |
| 5074 | mirror::ArtMethod* imt_ref = out_imt->GetReference(imt_index)->AsArtMethod(); |
| 5075 | if (imt_ref == runtime->GetImtUnimplementedMethod()) { |
| 5076 | out_imt->SetReference(imt_index, method); |
| 5077 | } else if (imt_ref != conflict_method) { |
| 5078 | out_imt->SetReference(imt_index, conflict_method); |
| 5079 | } |
| 5080 | } |
| 5081 | } |
| 5082 | } |
| 5083 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 5084 | for (size_t i = 0; i < ifcount; ++i) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5085 | self->AllowThreadSuspension(); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 5086 | size_t num_methods = iftable->GetInterface(i)->NumVirtualMethods(); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5087 | if (num_methods > 0) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 5088 | StackHandleScope<2> hs2(self); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5089 | const bool is_super = i < super_ifcount; |
| 5090 | const bool super_interface = is_super && extend_super_iftable; |
| 5091 | Handle<mirror::ObjectArray<mirror::ArtMethod>> method_array; |
| 5092 | Handle<mirror::ObjectArray<mirror::ArtMethod>> input_array; |
| 5093 | if (super_interface) { |
| 5094 | mirror::IfTable* if_table = klass->GetSuperClass()->GetIfTable(); |
| 5095 | DCHECK(if_table != nullptr); |
| 5096 | DCHECK(if_table->GetMethodArray(i) != nullptr); |
| 5097 | // 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] | 5098 | method_array = hs2.NewHandle(if_table->GetMethodArray(i)->Clone(self)-> |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5099 | AsObjectArray<mirror::ArtMethod>()); |
| 5100 | // We are overwriting a super class interface, try to only virtual methods instead of the |
| 5101 | // whole vtable. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 5102 | input_array = hs2.NewHandle(klass->GetVirtualMethods()); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5103 | } else { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 5104 | method_array = hs2.NewHandle(AllocArtMethodArray(self, num_methods)); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5105 | // A new interface, we need the whole vtable incase a new interface method is implemented |
| 5106 | // in the whole superclass. |
| 5107 | input_array = vtable; |
| 5108 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5109 | if (UNLIKELY(method_array.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5110 | CHECK(self->IsExceptionPending()); // OOME. |
| 5111 | return false; |
| 5112 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5113 | iftable->SetMethodArray(i, method_array.Get()); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5114 | if (input_array.Get() == nullptr) { |
| 5115 | // If the added virtual methods is empty, do nothing. |
| 5116 | DCHECK(super_interface); |
| 5117 | continue; |
| 5118 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5119 | for (size_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5120 | mirror::ArtMethod* interface_method = iftable->GetInterface(i)->GetVirtualMethod(j); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5121 | MethodNameAndSignatureComparator interface_name_comparator( |
| 5122 | interface_method->GetInterfaceMethodIfProxy()); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5123 | int32_t k; |
| 5124 | // For each method listed in the interface's method list, find the |
| 5125 | // matching method in our class's method list. We want to favor the |
| 5126 | // subclass over the superclass, which just requires walking |
| 5127 | // back from the end of the vtable. (This only matters if the |
| 5128 | // superclass defines a private method and this class redefines |
| 5129 | // it -- otherwise it would use the same vtable slot. In .dex files |
| 5130 | // those don't end up in the virtual method table, so it shouldn't |
| 5131 | // matter which direction we go. We walk it backward anyway.) |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5132 | for (k = input_array->GetLength() - 1; k >= 0; --k) { |
| 5133 | mirror::ArtMethod* vtable_method = input_array->GetWithoutChecks(k); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5134 | mirror::ArtMethod* vtable_method_for_name_comparison = |
| 5135 | vtable_method->GetInterfaceMethodIfProxy(); |
| 5136 | if (interface_name_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5137 | vtable_method_for_name_comparison)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5138 | if (!vtable_method->IsAbstract() && !vtable_method->IsPublic()) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5139 | ThrowIllegalAccessError( |
| 5140 | klass.Get(), |
| 5141 | "Method '%s' implementing interface method '%s' is not public", |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5142 | PrettyMethod(vtable_method).c_str(), |
| 5143 | PrettyMethod(interface_method).c_str()); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5144 | return false; |
| 5145 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5146 | method_array->SetWithoutChecks<false>(j, vtable_method); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 5147 | // Place method in imt if entry is empty, place conflict otherwise. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5148 | uint32_t imt_index = interface_method->GetDexMethodIndex() % mirror::Class::kImtSize; |
| 5149 | mirror::ArtMethod* imt_ref = out_imt->GetReference(imt_index)->AsArtMethod(); |
| 5150 | mirror::ArtMethod* conflict_method = runtime->GetImtConflictMethod(); |
| 5151 | if (imt_ref == runtime->GetImtUnimplementedMethod()) { |
| 5152 | out_imt->SetReference(imt_index, vtable_method); |
| 5153 | } else if (imt_ref != conflict_method) { |
| 5154 | // If we are not a conflict and we have the same signature and name as the imt entry, |
| 5155 | // it must be that we overwrote a superclass vtable entry. |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5156 | MethodNameAndSignatureComparator imt_ref_name_comparator( |
| 5157 | imt_ref->GetInterfaceMethodIfProxy()); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5158 | if (imt_ref_name_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5159 | vtable_method_for_name_comparison)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5160 | out_imt->SetReference(imt_index, vtable_method); |
| 5161 | } else { |
| 5162 | out_imt->SetReference(imt_index, conflict_method); |
| 5163 | } |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 5164 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5165 | break; |
| 5166 | } |
| 5167 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5168 | if (k < 0 && !super_interface) { |
| 5169 | mirror::ArtMethod* miranda_method = nullptr; |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5170 | for (size_t l = 0; l < miranda_list_size; ++l) { |
| 5171 | mirror::ArtMethod* mir_method = miranda_list->Get(l); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5172 | if (interface_name_comparator.HasSameNameAndSignature(mir_method)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5173 | miranda_method = mir_method; |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5174 | break; |
| 5175 | } |
| 5176 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5177 | if (miranda_method == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5178 | // Point the interface table at a phantom slot. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5179 | miranda_method = interface_method->Clone(self)->AsArtMethod(); |
| 5180 | if (UNLIKELY(miranda_method == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5181 | CHECK(self->IsExceptionPending()); // OOME. |
| 5182 | return false; |
| 5183 | } |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5184 | DCHECK_LT(miranda_list_size, max_miranda_methods); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5185 | miranda_list->Set<false>(miranda_list_size++, miranda_method); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5186 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5187 | method_array->SetWithoutChecks<false>(j, miranda_method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5188 | } |
| 5189 | } |
| 5190 | } |
| 5191 | } |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5192 | if (miranda_list_size > 0) { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 5193 | int old_method_count = klass->NumVirtualMethods(); |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5194 | int new_method_count = old_method_count + miranda_list_size; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5195 | mirror::ObjectArray<mirror::ArtMethod>* virtuals; |
| 5196 | if (old_method_count == 0) { |
| 5197 | virtuals = AllocArtMethodArray(self, new_method_count); |
| 5198 | } else { |
| 5199 | virtuals = klass->GetVirtualMethods()->CopyOf(self, new_method_count); |
| 5200 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5201 | if (UNLIKELY(virtuals == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5202 | CHECK(self->IsExceptionPending()); // OOME. |
| 5203 | return false; |
| 5204 | } |
| 5205 | klass->SetVirtualMethods(virtuals); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5206 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5207 | int old_vtable_count = vtable->GetLength(); |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5208 | int new_vtable_count = old_vtable_count + miranda_list_size; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5209 | vtable.Assign(vtable->CopyOf(self, new_vtable_count)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5210 | if (UNLIKELY(vtable.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5211 | CHECK(self->IsExceptionPending()); // OOME. |
| 5212 | return false; |
| 5213 | } |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5214 | for (size_t i = 0; i < miranda_list_size; ++i) { |
| 5215 | mirror::ArtMethod* method = miranda_list->Get(i); |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 5216 | // Leave the declaring class alone as type indices are relative to it |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 5217 | method->SetAccessFlags(method->GetAccessFlags() | kAccMiranda); |
| 5218 | method->SetMethodIndex(0xFFFF & (old_vtable_count + i)); |
| 5219 | klass->SetVirtualMethod(old_method_count + i, method); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5220 | vtable->SetWithoutChecks<false>(old_vtable_count + i, method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5221 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5222 | // 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] | 5223 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5224 | } |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 5225 | |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5226 | if (kIsDebugBuild) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 5227 | mirror::ObjectArray<mirror::ArtMethod>* check_vtable = klass->GetVTableDuringLinking(); |
| 5228 | for (int i = 0; i < check_vtable->GetLength(); ++i) { |
| 5229 | CHECK(check_vtable->GetWithoutChecks(i) != nullptr); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5230 | } |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 5231 | } |
| 5232 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5233 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5234 | return true; |
| 5235 | } |
| 5236 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5237 | bool ClassLinker::LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5238 | CHECK(klass.Get() != nullptr); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5239 | return LinkFields(self, klass, false, nullptr); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5240 | } |
| 5241 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5242 | 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] | 5243 | CHECK(klass.Get() != nullptr); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5244 | return LinkFields(self, klass, true, class_size); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5245 | } |
| 5246 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5247 | struct LinkFieldsComparator { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5248 | explicit LinkFieldsComparator() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 5249 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 5250 | // 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] | 5251 | bool operator()(mirror::ArtField* field1, mirror::ArtField* field2) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5252 | NO_THREAD_SAFETY_ANALYSIS { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 5253 | // 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] | 5254 | Primitive::Type type1 = field1->GetTypeAsPrimitiveType(); |
| 5255 | Primitive::Type type2 = field2->GetTypeAsPrimitiveType(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 5256 | if (type1 != type2) { |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 5257 | if (type1 == Primitive::kPrimNot) { |
| 5258 | // Reference always goes first. |
| 5259 | return true; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 5260 | } |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 5261 | if (type2 == Primitive::kPrimNot) { |
| 5262 | // Reference always goes first. |
| 5263 | return false; |
| 5264 | } |
| 5265 | size_t size1 = Primitive::ComponentSize(type1); |
| 5266 | size_t size2 = Primitive::ComponentSize(type2); |
| 5267 | if (size1 != size2) { |
| 5268 | // Larger primitive types go first. |
| 5269 | return size1 > size2; |
| 5270 | } |
| 5271 | // Primitive types differ but sizes match. Arbitrarily order by primitive type. |
| 5272 | return type1 < type2; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5273 | } |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 5274 | // Same basic group? Then sort by dex field index. This is guaranteed to be sorted |
| 5275 | // by name and for equal names by type id index. |
| 5276 | // NOTE: This works also for proxies. Their static fields are assigned appropriate indexes. |
| 5277 | return field1->GetDexFieldIndex() < field2->GetDexFieldIndex(); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5278 | } |
| 5279 | }; |
| 5280 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5281 | bool ClassLinker::LinkFields(Thread* self, Handle<mirror::Class> klass, bool is_static, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5282 | size_t* class_size) { |
| 5283 | self->AllowThreadSuspension(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5284 | size_t num_fields = |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5285 | is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5286 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5287 | mirror::ObjectArray<mirror::ArtField>* fields = |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5288 | is_static ? klass->GetSFields() : klass->GetIFields(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5289 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5290 | // Initialize field_offset |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 5291 | MemberOffset field_offset(0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5292 | if (is_static) { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5293 | field_offset = klass->GetFirstReferenceStaticFieldOffsetDuringLinking(); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5294 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5295 | mirror::Class* super_class = klass->GetSuperClass(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5296 | if (super_class != nullptr) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5297 | CHECK(super_class->IsResolved()) |
| 5298 | << PrettyClass(klass.Get()) << " " << PrettyClass(super_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5299 | field_offset = MemberOffset(super_class->GetObjectSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5300 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5301 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5302 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5303 | CHECK_EQ(num_fields == 0, fields == nullptr) << PrettyClass(klass.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5304 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5305 | // we want a relatively stable order so that adding new fields |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 5306 | // minimizes disruption of C++ version such as Class and Method. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5307 | std::deque<mirror::ArtField*> grouped_and_sorted_fields; |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 5308 | const char* old_no_suspend_cause = self->StartAssertNoThreadSuspension( |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 5309 | "Naked ArtField references in deque"); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5310 | for (size_t i = 0; i < num_fields; i++) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5311 | mirror::ArtField* f = fields->Get(i); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5312 | CHECK(f != nullptr) << PrettyClass(klass.Get()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5313 | grouped_and_sorted_fields.push_back(f); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5314 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5315 | std::sort(grouped_and_sorted_fields.begin(), grouped_and_sorted_fields.end(), |
| 5316 | LinkFieldsComparator()); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5317 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 5318 | // References should be at the front. |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5319 | size_t current_field = 0; |
| 5320 | size_t num_reference_fields = 0; |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 5321 | FieldGaps gaps; |
| 5322 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5323 | for (; current_field < num_fields; current_field++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5324 | mirror::ArtField* field = grouped_and_sorted_fields.front(); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 5325 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 5326 | bool isPrimitive = type != Primitive::kPrimNot; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5327 | if (isPrimitive) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 5328 | break; // past last reference, move on to the next phase |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5329 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5330 | if (UNLIKELY(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>( |
| 5331 | field_offset.Uint32Value()))) { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 5332 | MemberOffset old_offset = field_offset; |
| 5333 | field_offset = MemberOffset(RoundUp(field_offset.Uint32Value(), 4)); |
| 5334 | AddFieldGap(old_offset.Uint32Value(), field_offset.Uint32Value(), &gaps); |
| 5335 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5336 | DCHECK(IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(field_offset.Uint32Value())); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5337 | grouped_and_sorted_fields.pop_front(); |
| 5338 | num_reference_fields++; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5339 | field->SetOffset(field_offset); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5340 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 5341 | sizeof(mirror::HeapReference<mirror::Object>)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5342 | } |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 5343 | // Gaps are stored as a max heap which means that we must shuffle from largest to smallest |
| 5344 | // otherwise we could end up with suboptimal gap fills. |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5345 | ShuffleForward<8>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 5346 | ShuffleForward<4>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 5347 | ShuffleForward<2>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 5348 | ShuffleForward<1>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 5349 | CHECK(grouped_and_sorted_fields.empty()) << "Missed " << grouped_and_sorted_fields.size() << |
| 5350 | " fields."; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5351 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5352 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 5353 | // 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] | 5354 | if (!is_static && klass->DescriptorEquals("Ljava/lang/ref/Reference;")) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 5355 | // We know there are no non-reference fields in the Reference classes, and we know |
| 5356 | // that 'referent' is alphabetically last, so this is easy... |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5357 | CHECK_EQ(num_reference_fields, num_fields) << PrettyClass(klass.Get()); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 5358 | CHECK_STREQ(fields->Get(num_fields - 1)->GetName(), "referent") << PrettyClass(klass.Get()); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 5359 | --num_reference_fields; |
| 5360 | } |
| 5361 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5362 | size_t size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5363 | // Update klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5364 | if (is_static) { |
| 5365 | klass->SetNumReferenceStaticFields(num_reference_fields); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5366 | *class_size = size; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5367 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5368 | klass->SetNumReferenceInstanceFields(num_reference_fields); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5369 | if (!klass->IsVariableSize()) { |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 5370 | if (klass->DescriptorEquals("Ljava/lang/reflect/ArtMethod;")) { |
| 5371 | klass->SetObjectSize(mirror::ArtMethod::InstanceSize(sizeof(void*))); |
| 5372 | } else { |
| 5373 | std::string temp; |
| 5374 | DCHECK_GE(size, sizeof(mirror::Object)) << klass->GetDescriptor(&temp); |
| 5375 | size_t previous_size = klass->GetObjectSize(); |
| 5376 | if (previous_size != 0) { |
| 5377 | // Make sure that we didn't originally have an incorrect size. |
| 5378 | CHECK_EQ(previous_size, size) << klass->GetDescriptor(&temp); |
| 5379 | } |
| 5380 | klass->SetObjectSize(size); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 5381 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5382 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5383 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5384 | |
| 5385 | if (kIsDebugBuild) { |
| 5386 | // Make sure that the fields array is ordered by name but all reference |
| 5387 | // offsets are at the beginning as far as alignment allows. |
| 5388 | MemberOffset start_ref_offset = is_static |
| 5389 | ? klass->GetFirstReferenceStaticFieldOffsetDuringLinking() |
| 5390 | : klass->GetFirstReferenceInstanceFieldOffset(); |
| 5391 | MemberOffset end_ref_offset(start_ref_offset.Uint32Value() + |
| 5392 | num_reference_fields * |
| 5393 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 5394 | MemberOffset current_ref_offset = start_ref_offset; |
| 5395 | for (size_t i = 0; i < num_fields; i++) { |
| 5396 | mirror::ArtField* field = fields->Get(i); |
| 5397 | if ((false)) { // enable to debug field layout |
| 5398 | LOG(INFO) << "LinkFields: " << (is_static ? "static" : "instance") |
| 5399 | << " class=" << PrettyClass(klass.Get()) |
| 5400 | << " field=" << PrettyField(field) |
| 5401 | << " offset=" |
| 5402 | << field->GetField32(mirror::ArtField::OffsetOffset()); |
| 5403 | } |
| 5404 | if (i != 0) { |
| 5405 | mirror::ArtField* prev_field = fields->Get(i - 1u); |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 5406 | // NOTE: The field names can be the same. This is not possible in the Java language |
| 5407 | // but it's valid Java/dex bytecode and for example proguard can generate such bytecode. |
| 5408 | CHECK_LE(strcmp(prev_field->GetName(), field->GetName()), 0); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5409 | } |
| 5410 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 5411 | bool is_primitive = type != Primitive::kPrimNot; |
| 5412 | if (klass->DescriptorEquals("Ljava/lang/ref/Reference;") && |
| 5413 | strcmp("referent", field->GetName()) == 0) { |
| 5414 | is_primitive = true; // We lied above, so we have to expect a lie here. |
| 5415 | } |
| 5416 | MemberOffset offset = field->GetOffsetDuringLinking(); |
| 5417 | if (is_primitive) { |
| 5418 | if (offset.Uint32Value() < end_ref_offset.Uint32Value()) { |
| 5419 | // Shuffled before references. |
| 5420 | size_t type_size = Primitive::ComponentSize(type); |
| 5421 | CHECK_LT(type_size, sizeof(mirror::HeapReference<mirror::Object>)); |
| 5422 | CHECK_LT(offset.Uint32Value(), start_ref_offset.Uint32Value()); |
| 5423 | CHECK_LE(offset.Uint32Value() + type_size, start_ref_offset.Uint32Value()); |
| 5424 | CHECK(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(offset.Uint32Value())); |
| 5425 | } |
| 5426 | } else { |
| 5427 | CHECK_EQ(current_ref_offset.Uint32Value(), offset.Uint32Value()); |
| 5428 | current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() + |
| 5429 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 5430 | } |
| 5431 | } |
| 5432 | CHECK_EQ(current_ref_offset.Uint32Value(), end_ref_offset.Uint32Value()); |
| 5433 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5434 | return true; |
| 5435 | } |
| 5436 | |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5437 | // Set the bitmap of reference instance field offsets. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5438 | void ClassLinker::CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5439 | uint32_t reference_offsets = 0; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5440 | mirror::Class* super_class = klass->GetSuperClass(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5441 | // 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] | 5442 | if (super_class != nullptr) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5443 | reference_offsets = super_class->GetReferenceInstanceOffsets(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5444 | // Compute reference offsets unless our superclass overflowed. |
| 5445 | if (reference_offsets != mirror::Class::kClassWalkSuper) { |
| 5446 | size_t num_reference_fields = klass->NumReferenceInstanceFieldsDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5447 | if (num_reference_fields != 0u) { |
| 5448 | // All of the fields that contain object references are guaranteed be grouped in memory |
| 5449 | // starting at an appropriately aligned address after super class object data. |
| 5450 | uint32_t start_offset = RoundUp(super_class->GetObjectSize(), |
| 5451 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 5452 | uint32_t start_bit = (start_offset - mirror::kObjectHeaderSize) / |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5453 | sizeof(mirror::HeapReference<mirror::Object>); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5454 | if (start_bit + num_reference_fields > 32) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5455 | reference_offsets = mirror::Class::kClassWalkSuper; |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5456 | } else { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5457 | reference_offsets |= (0xffffffffu << start_bit) & |
| 5458 | (0xffffffffu >> (32 - (start_bit + num_reference_fields))); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5459 | } |
| 5460 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5461 | } |
| 5462 | } |
Mingyao Yang | faff0f0 | 2014-09-10 12:03:22 -0700 | [diff] [blame] | 5463 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5464 | } |
| 5465 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5466 | mirror::String* ClassLinker::ResolveString(const DexFile& dex_file, uint32_t string_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5467 | Handle<mirror::DexCache> dex_cache) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5468 | DCHECK(dex_cache.Get() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5469 | mirror::String* resolved = dex_cache->GetResolvedString(string_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5470 | if (resolved != nullptr) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5471 | return resolved; |
| 5472 | } |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 5473 | uint32_t utf16_length; |
| 5474 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5475 | mirror::String* string = intern_table_->InternStrong(utf16_length, utf8_data); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5476 | dex_cache->SetResolvedString(string_idx, string); |
| 5477 | return string; |
| 5478 | } |
| 5479 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5480 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, uint16_t type_idx, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 5481 | mirror::Class* referrer) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5482 | StackHandleScope<2> hs(Thread::Current()); |
| 5483 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 5484 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5485 | return ResolveType(dex_file, type_idx, dex_cache, class_loader); |
| 5486 | } |
| 5487 | |
| 5488 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, uint16_t type_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5489 | Handle<mirror::DexCache> dex_cache, |
| 5490 | Handle<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5491 | DCHECK(dex_cache.Get() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5492 | mirror::Class* resolved = dex_cache->GetResolvedType(type_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5493 | if (resolved == nullptr) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 5494 | Thread* self = Thread::Current(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 5495 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 5496 | resolved = FindClass(self, descriptor, class_loader); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5497 | if (resolved != nullptr) { |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 5498 | // TODO: we used to throw here if resolved's class loader was not the |
| 5499 | // boot class loader. This was to permit different classes with the |
| 5500 | // same name to be loaded simultaneously by different loaders |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5501 | dex_cache->SetResolvedType(type_idx, resolved); |
| 5502 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5503 | CHECK(self->IsExceptionPending()) |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 5504 | << "Expected pending exception for failed resolution of: " << descriptor; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5505 | // Convert a ClassNotFoundException to a NoClassDefFoundError. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5506 | StackHandleScope<1> hs(self); |
| 5507 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException(nullptr))); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5508 | if (cause->InstanceOf(GetClassRoot(kJavaLangClassNotFoundException))) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5509 | DCHECK(resolved == nullptr); // No Handle needed to preserve resolved. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 5510 | self->ClearException(); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 5511 | ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5512 | self->GetException(nullptr)->SetCause(cause.Get()); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 5513 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5514 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5515 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5516 | DCHECK((resolved == nullptr) || resolved->IsResolved() || resolved->IsErroneous()) |
Brian Carlstrom | abcf7ae | 2013-09-23 22:19:52 -0700 | [diff] [blame] | 5517 | << PrettyDescriptor(resolved) << " " << resolved->GetStatus(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5518 | return resolved; |
| 5519 | } |
| 5520 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 5521 | mirror::ArtMethod* ClassLinker::ResolveMethod(const DexFile& dex_file, uint32_t method_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5522 | Handle<mirror::DexCache> dex_cache, |
| 5523 | Handle<mirror::ClassLoader> class_loader, |
| 5524 | Handle<mirror::ArtMethod> referrer, |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 5525 | InvokeType type) { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5526 | DCHECK(dex_cache.Get() != nullptr); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5527 | // Check for hit in the dex cache. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5528 | mirror::ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 5529 | if (resolved != nullptr && !resolved->IsRuntimeMethod()) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5530 | return resolved; |
| 5531 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5532 | // Fail, get the declaring class. |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5533 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5534 | 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] | 5535 | if (klass == nullptr) { |
Elliott Hughes | cc5f9a9 | 2011-09-28 19:17:29 -0700 | [diff] [blame] | 5536 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5537 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5538 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5539 | // Scan using method_idx, this saves string compares but will only hit for matching dex |
| 5540 | // caches/files. |
| 5541 | switch (type) { |
| 5542 | case kDirect: // Fall-through. |
| 5543 | case kStatic: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5544 | resolved = klass->FindDirectMethod(dex_cache.Get(), method_idx); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5545 | break; |
| 5546 | case kInterface: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5547 | resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5548 | DCHECK(resolved == nullptr || resolved->GetDeclaringClass()->IsInterface()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5549 | break; |
| 5550 | case kSuper: // Fall-through. |
| 5551 | case kVirtual: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5552 | resolved = klass->FindVirtualMethod(dex_cache.Get(), method_idx); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5553 | break; |
| 5554 | default: |
| 5555 | LOG(FATAL) << "Unreachable - invocation type: " << type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 5556 | UNREACHABLE(); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 5557 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5558 | if (resolved == nullptr) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5559 | // Search by name, which works across dex files. |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5560 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 5561 | const Signature signature = dex_file.GetMethodSignature(method_id); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5562 | switch (type) { |
| 5563 | case kDirect: // Fall-through. |
| 5564 | case kStatic: |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 5565 | resolved = klass->FindDirectMethod(name, signature); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5566 | break; |
| 5567 | case kInterface: |
| 5568 | resolved = klass->FindInterfaceMethod(name, signature); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5569 | DCHECK(resolved == nullptr || resolved->GetDeclaringClass()->IsInterface()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5570 | break; |
| 5571 | case kSuper: // Fall-through. |
| 5572 | case kVirtual: |
| 5573 | resolved = klass->FindVirtualMethod(name, signature); |
| 5574 | break; |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5575 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5576 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5577 | // If we found a method, check for incompatible class changes. |
| 5578 | if (LIKELY(resolved != nullptr && !resolved->CheckIncompatibleClassChange(type))) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5579 | // Be a good citizen and update the dex cache to speed subsequent calls. |
| 5580 | dex_cache->SetResolvedMethod(method_idx, resolved); |
| 5581 | return resolved; |
| 5582 | } else { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5583 | // If we had a method, it's an incompatible-class-change error. |
| 5584 | if (resolved != nullptr) { |
| 5585 | ThrowIncompatibleClassChangeError(type, resolved->GetInvokeType(), resolved, referrer.Get()); |
| 5586 | } else { |
| 5587 | // We failed to find the method which means either an access error, an incompatible class |
| 5588 | // change, or no such method. First try to find the method among direct and virtual methods. |
| 5589 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
| 5590 | const Signature signature = dex_file.GetMethodSignature(method_id); |
| 5591 | switch (type) { |
| 5592 | case kDirect: |
| 5593 | case kStatic: |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5594 | resolved = klass->FindVirtualMethod(name, signature); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5595 | // Note: kDirect and kStatic are also mutually exclusive, but in that case we would |
| 5596 | // have had a resolved method before, which triggers the "true" branch above. |
| 5597 | break; |
| 5598 | case kInterface: |
| 5599 | case kVirtual: |
| 5600 | case kSuper: |
| 5601 | resolved = klass->FindDirectMethod(name, signature); |
| 5602 | break; |
| 5603 | } |
| 5604 | |
| 5605 | // 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] | 5606 | bool exception_generated = false; |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5607 | if (resolved != nullptr && referrer.Get() != nullptr) { |
| 5608 | mirror::Class* methods_class = resolved->GetDeclaringClass(); |
| 5609 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
| 5610 | if (!referring_class->CanAccess(methods_class)) { |
| 5611 | ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, methods_class, |
| 5612 | resolved, type); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5613 | exception_generated = true; |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5614 | } else if (!referring_class->CanAccessMember(methods_class, |
| 5615 | resolved->GetAccessFlags())) { |
| 5616 | ThrowIllegalAccessErrorMethod(referring_class, resolved); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5617 | exception_generated = true; |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5618 | } |
| 5619 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5620 | if (!exception_generated) { |
| 5621 | // Otherwise, throw an IncompatibleClassChangeError if we found something, and check |
| 5622 | // interface methods and throw if we find the method there. If we find nothing, throw a |
| 5623 | // NoSuchMethodError. |
| 5624 | switch (type) { |
| 5625 | case kDirect: |
| 5626 | case kStatic: |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5627 | if (resolved != nullptr) { |
| 5628 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer.Get()); |
| 5629 | } else { |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5630 | resolved = klass->FindInterfaceMethod(name, signature); |
| 5631 | if (resolved != nullptr) { |
| 5632 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer.Get()); |
| 5633 | } else { |
| 5634 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 5635 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5636 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5637 | break; |
| 5638 | case kInterface: |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5639 | if (resolved != nullptr) { |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5640 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer.Get()); |
| 5641 | } else { |
| 5642 | resolved = klass->FindVirtualMethod(name, signature); |
| 5643 | if (resolved != nullptr) { |
| 5644 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer.Get()); |
| 5645 | } else { |
| 5646 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 5647 | } |
| 5648 | } |
| 5649 | break; |
| 5650 | case kSuper: |
| 5651 | if (resolved != nullptr) { |
| 5652 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer.Get()); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5653 | } else { |
| 5654 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 5655 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5656 | break; |
| 5657 | case kVirtual: |
| 5658 | if (resolved != nullptr) { |
| 5659 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer.Get()); |
| 5660 | } else { |
| 5661 | resolved = klass->FindInterfaceMethod(name, signature); |
| 5662 | if (resolved != nullptr) { |
| 5663 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer.Get()); |
| 5664 | } else { |
| 5665 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 5666 | } |
| 5667 | } |
| 5668 | break; |
| 5669 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5670 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5671 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5672 | Thread::Current()->AssertPendingException(); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5673 | return nullptr; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5674 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5675 | } |
| 5676 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5677 | mirror::ArtField* ClassLinker::ResolveField(const DexFile& dex_file, uint32_t field_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5678 | Handle<mirror::DexCache> dex_cache, |
| 5679 | Handle<mirror::ClassLoader> class_loader, |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 5680 | bool is_static) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5681 | DCHECK(dex_cache.Get() != nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5682 | mirror::ArtField* resolved = dex_cache->GetResolvedField(field_idx); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5683 | if (resolved != nullptr) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5684 | return resolved; |
| 5685 | } |
| 5686 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5687 | Thread* const self = Thread::Current(); |
| 5688 | StackHandleScope<1> hs(self); |
| 5689 | Handle<mirror::Class> klass( |
| 5690 | 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] | 5691 | if (klass.Get() == nullptr) { |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 5692 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5693 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5694 | } |
| 5695 | |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 5696 | if (is_static) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5697 | resolved = mirror::Class::FindStaticField(self, klass, dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 5698 | } else { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5699 | resolved = klass->FindInstanceField(dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 5700 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5701 | |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5702 | if (resolved == nullptr) { |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5703 | const char* name = dex_file.GetFieldName(field_id); |
| 5704 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 5705 | if (is_static) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5706 | resolved = mirror::Class::FindStaticField(self, klass, name, type); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5707 | } else { |
| 5708 | resolved = klass->FindInstanceField(name, type); |
| 5709 | } |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5710 | if (resolved == nullptr) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5711 | ThrowNoSuchFieldError(is_static ? "static " : "instance ", klass.Get(), type, name); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5712 | return nullptr; |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5713 | } |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5714 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5715 | dex_cache->SetResolvedField(field_idx, resolved); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5716 | return resolved; |
| 5717 | } |
| 5718 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5719 | mirror::ArtField* ClassLinker::ResolveFieldJLS(const DexFile& dex_file, |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5720 | uint32_t field_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5721 | Handle<mirror::DexCache> dex_cache, |
| 5722 | Handle<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5723 | DCHECK(dex_cache.Get() != nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5724 | mirror::ArtField* resolved = dex_cache->GetResolvedField(field_idx); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5725 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5726 | return resolved; |
| 5727 | } |
| 5728 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5729 | Thread* self = Thread::Current(); |
| 5730 | StackHandleScope<1> hs(self); |
| 5731 | Handle<mirror::Class> klass( |
| 5732 | 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] | 5733 | if (klass.Get() == nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5734 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5735 | return nullptr; |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5736 | } |
| 5737 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 5738 | StringPiece name(dex_file.StringDataByIdx(field_id.name_idx_)); |
| 5739 | StringPiece type(dex_file.StringDataByIdx( |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5740 | dex_file.GetTypeId(field_id.type_idx_).descriptor_idx_)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5741 | resolved = mirror::Class::FindField(self, klass, name, type); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5742 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5743 | dex_cache->SetResolvedField(field_idx, resolved); |
| 5744 | } else { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5745 | ThrowNoSuchFieldError("", klass.Get(), type, name); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5746 | } |
| 5747 | return resolved; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 5748 | } |
| 5749 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5750 | const char* ClassLinker::MethodShorty(uint32_t method_idx, mirror::ArtMethod* referrer, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5751 | uint32_t* length) { |
| 5752 | mirror::Class* declaring_class = referrer->GetDeclaringClass(); |
| 5753 | mirror::DexCache* dex_cache = declaring_class->GetDexCache(); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 5754 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 5755 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 5756 | return dex_file.GetMethodShorty(method_id, length); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 5757 | } |
| 5758 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5759 | void ClassLinker::DumpAllClasses(int flags) { |
| 5760 | if (dex_cache_image_class_lookup_required_) { |
| 5761 | MoveImageClassesToClassTable(); |
| 5762 | } |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 5763 | // TODO: at the time this was written, it wasn't safe to call PrettyField with the ClassLinker |
| 5764 | // 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] | 5765 | std::vector<mirror::Class*> all_classes; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 5766 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 5767 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 5768 | for (GcRoot<mirror::Class>& it : class_table_) { |
| 5769 | all_classes.push_back(it.Read()); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 5770 | } |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 5771 | } |
| 5772 | |
| 5773 | for (size_t i = 0; i < all_classes.size(); ++i) { |
| 5774 | all_classes[i]->DumpClass(std::cerr, flags); |
| 5775 | } |
| 5776 | } |
| 5777 | |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 5778 | static OatFile::OatMethod CreateOatMethod(const void* code, bool is_portable) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5779 | CHECK_EQ(kUsePortableCompiler, is_portable); |
| 5780 | CHECK(code != nullptr); |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 5781 | const uint8_t* base = reinterpret_cast<const uint8_t*>(code); // Base of data points at code. |
| 5782 | base -= sizeof(void*); // Move backward so that code_offset != 0. |
| 5783 | const uint32_t code_offset = sizeof(void*); |
| 5784 | return OatFile::OatMethod(base, code_offset); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5785 | } |
| 5786 | |
| 5787 | bool ClassLinker::IsPortableResolutionStub(const void* entry_point) const { |
| 5788 | return (entry_point == GetPortableResolutionStub()) || |
| 5789 | (portable_resolution_trampoline_ == entry_point); |
| 5790 | } |
| 5791 | |
| 5792 | bool ClassLinker::IsQuickResolutionStub(const void* entry_point) const { |
| 5793 | return (entry_point == GetQuickResolutionStub()) || |
| 5794 | (quick_resolution_trampoline_ == entry_point); |
| 5795 | } |
| 5796 | |
| 5797 | bool ClassLinker::IsPortableToInterpreterBridge(const void* entry_point) const { |
| 5798 | return (entry_point == GetPortableToInterpreterBridge()); |
| 5799 | // TODO: portable_to_interpreter_bridge_trampoline_ == entry_point; |
| 5800 | } |
| 5801 | |
| 5802 | bool ClassLinker::IsQuickToInterpreterBridge(const void* entry_point) const { |
| 5803 | return (entry_point == GetQuickToInterpreterBridge()) || |
| 5804 | (quick_to_interpreter_bridge_trampoline_ == entry_point); |
| 5805 | } |
| 5806 | |
| 5807 | bool ClassLinker::IsQuickGenericJniStub(const void* entry_point) const { |
| 5808 | return (entry_point == GetQuickGenericJniStub()) || |
| 5809 | (quick_generic_jni_trampoline_ == entry_point); |
| 5810 | } |
| 5811 | |
| 5812 | const void* ClassLinker::GetRuntimeQuickGenericJniStub() const { |
| 5813 | return GetQuickGenericJniStub(); |
| 5814 | } |
| 5815 | |
| 5816 | void ClassLinker::SetEntryPointsToCompiledCode(mirror::ArtMethod* method, const void* method_code, |
| 5817 | bool is_portable) const { |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 5818 | OatFile::OatMethod oat_method = CreateOatMethod(method_code, is_portable); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5819 | oat_method.LinkMethod(method); |
| 5820 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
| 5821 | // Create bridges to transition between different kinds of compiled bridge. |
| 5822 | if (method->GetEntryPointFromPortableCompiledCode() == nullptr) { |
| 5823 | method->SetEntryPointFromPortableCompiledCode(GetPortableToQuickBridge()); |
| 5824 | } else { |
| 5825 | CHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr); |
| 5826 | method->SetEntryPointFromQuickCompiledCode(GetQuickToPortableBridge()); |
| 5827 | method->SetIsPortableCompiled(); |
| 5828 | } |
| 5829 | } |
| 5830 | |
| 5831 | void ClassLinker::SetEntryPointsToInterpreter(mirror::ArtMethod* method) const { |
| 5832 | if (!method->IsNative()) { |
| 5833 | method->SetEntryPointFromInterpreter(artInterpreterToInterpreterBridge); |
| 5834 | method->SetEntryPointFromPortableCompiledCode(GetPortableToInterpreterBridge()); |
| 5835 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 5836 | } else { |
| 5837 | const void* quick_method_code = GetQuickGenericJniStub(); |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 5838 | OatFile::OatMethod oat_method = CreateOatMethod(quick_method_code, false); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5839 | oat_method.LinkMethod(method); |
| 5840 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
| 5841 | method->SetEntryPointFromPortableCompiledCode(GetPortableToQuickBridge()); |
| 5842 | } |
| 5843 | } |
| 5844 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5845 | void ClassLinker::DumpForSigQuit(std::ostream& os) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5846 | Thread* self = Thread::Current(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5847 | if (dex_cache_image_class_lookup_required_) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5848 | ScopedObjectAccess soa(self); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5849 | MoveImageClassesToClassTable(); |
| 5850 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5851 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 5852 | os << "Zygote loaded classes=" << pre_zygote_class_table_.Size() << " post zygote classes=" |
| 5853 | << class_table_.Size() << "\n"; |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 5854 | } |
| 5855 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5856 | size_t ClassLinker::NumLoadedClasses() { |
| 5857 | if (dex_cache_image_class_lookup_required_) { |
| 5858 | MoveImageClassesToClassTable(); |
| 5859 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 5860 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 5861 | // Only return non zygote classes since these are the ones which apps which care about. |
| 5862 | return class_table_.Size(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 5863 | } |
| 5864 | |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 5865 | pid_t ClassLinker::GetClassesLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 5866 | return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 5867 | } |
| 5868 | |
| 5869 | pid_t ClassLinker::GetDexLockOwner() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 5870 | return dex_lock_.GetExclusiveOwnerTid(); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 5871 | } |
| 5872 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5873 | void ClassLinker::SetClassRoot(ClassRoot class_root, mirror::Class* klass) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5874 | DCHECK(!init_done_); |
| 5875 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5876 | DCHECK(klass != nullptr); |
| 5877 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5878 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 5879 | mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5880 | DCHECK(class_roots != nullptr); |
| 5881 | DCHECK(class_roots->Get(class_root) == nullptr); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 5882 | class_roots->Set<false>(class_root, klass); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5883 | } |
| 5884 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5885 | const char* ClassLinker::GetClassRootDescriptor(ClassRoot class_root) { |
| 5886 | static const char* class_roots_descriptors[] = { |
| 5887 | "Ljava/lang/Class;", |
| 5888 | "Ljava/lang/Object;", |
| 5889 | "[Ljava/lang/Class;", |
| 5890 | "[Ljava/lang/Object;", |
| 5891 | "Ljava/lang/String;", |
| 5892 | "Ljava/lang/DexCache;", |
| 5893 | "Ljava/lang/ref/Reference;", |
| 5894 | "Ljava/lang/reflect/ArtField;", |
| 5895 | "Ljava/lang/reflect/ArtMethod;", |
| 5896 | "Ljava/lang/reflect/Proxy;", |
| 5897 | "[Ljava/lang/String;", |
| 5898 | "[Ljava/lang/reflect/ArtField;", |
| 5899 | "[Ljava/lang/reflect/ArtMethod;", |
| 5900 | "Ljava/lang/ClassLoader;", |
| 5901 | "Ljava/lang/Throwable;", |
| 5902 | "Ljava/lang/ClassNotFoundException;", |
| 5903 | "Ljava/lang/StackTraceElement;", |
| 5904 | "Z", |
| 5905 | "B", |
| 5906 | "C", |
| 5907 | "D", |
| 5908 | "F", |
| 5909 | "I", |
| 5910 | "J", |
| 5911 | "S", |
| 5912 | "V", |
| 5913 | "[Z", |
| 5914 | "[B", |
| 5915 | "[C", |
| 5916 | "[D", |
| 5917 | "[F", |
| 5918 | "[I", |
| 5919 | "[J", |
| 5920 | "[S", |
| 5921 | "[Ljava/lang/StackTraceElement;", |
| 5922 | }; |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 5923 | static_assert(arraysize(class_roots_descriptors) == size_t(kClassRootsMax), |
| 5924 | "Mismatch between class descriptors and class-root enum"); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5925 | |
| 5926 | const char* descriptor = class_roots_descriptors[class_root]; |
| 5927 | CHECK(descriptor != nullptr); |
| 5928 | return descriptor; |
| 5929 | } |
| 5930 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 5931 | std::size_t ClassLinker::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& root) |
| 5932 | const { |
| 5933 | std::string temp; |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 5934 | return ComputeModifiedUtf8Hash(root.Read()->GetDescriptor(&temp)); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 5935 | } |
| 5936 | |
| 5937 | bool ClassLinker::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a, |
| 5938 | const GcRoot<mirror::Class>& b) { |
| 5939 | if (a.Read()->GetClassLoader() != b.Read()->GetClassLoader()) { |
| 5940 | return false; |
| 5941 | } |
| 5942 | std::string temp; |
| 5943 | return a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp)); |
| 5944 | } |
| 5945 | |
| 5946 | std::size_t ClassLinker::ClassDescriptorHashEquals::operator()( |
| 5947 | const std::pair<const char*, mirror::ClassLoader*>& element) const { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 5948 | return ComputeModifiedUtf8Hash(element.first); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 5949 | } |
| 5950 | |
| 5951 | bool ClassLinker::ClassDescriptorHashEquals::operator()( |
| 5952 | const GcRoot<mirror::Class>& a, const std::pair<const char*, mirror::ClassLoader*>& b) { |
| 5953 | if (a.Read()->GetClassLoader() != b.second) { |
| 5954 | return false; |
| 5955 | } |
| 5956 | return a.Read()->DescriptorEquals(b.first); |
| 5957 | } |
| 5958 | |
| 5959 | bool ClassLinker::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a, |
| 5960 | const char* descriptor) { |
| 5961 | return a.Read()->DescriptorEquals(descriptor); |
| 5962 | } |
| 5963 | |
| 5964 | std::size_t ClassLinker::ClassDescriptorHashEquals::operator()(const char* descriptor) const { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 5965 | return ComputeModifiedUtf8Hash(descriptor); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 5966 | } |
| 5967 | |
Sebastien Hertz | 6963e44 | 2014-11-26 22:11:27 +0100 | [diff] [blame] | 5968 | bool ClassLinker::MayBeCalledWithDirectCodePointer(mirror::ArtMethod* m) { |
| 5969 | // Non-image methods don't use direct code pointer. |
| 5970 | if (!m->GetDeclaringClass()->IsBootStrapClassLoaded()) { |
| 5971 | return false; |
| 5972 | } |
| 5973 | if (m->IsPrivate()) { |
| 5974 | // The method can only be called inside its own oat file. Therefore it won't be called using |
| 5975 | // its direct code if the oat file has been compiled in PIC mode. |
| 5976 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 5977 | const DexFile& dex_file = m->GetDeclaringClass()->GetDexFile(); |
| 5978 | const OatFile::OatDexFile* oat_dex_file = class_linker->FindOpenedOatDexFileForDexFile(dex_file); |
| 5979 | if (oat_dex_file == nullptr) { |
| 5980 | // No oat file: the method has not been compiled. |
| 5981 | return false; |
| 5982 | } |
| 5983 | const OatFile* oat_file = oat_dex_file->GetOatFile(); |
| 5984 | return oat_file != nullptr && !oat_file->IsPic(); |
| 5985 | } else { |
| 5986 | // The method can be called outside its own oat file. Therefore it won't be called using its |
| 5987 | // direct code pointer only if all loaded oat files have been compiled in PIC mode. |
| 5988 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
| 5989 | for (const OatFile* oat_file : oat_files_) { |
| 5990 | if (!oat_file->IsPic()) { |
| 5991 | return true; |
| 5992 | } |
| 5993 | } |
| 5994 | return false; |
| 5995 | } |
| 5996 | } |
| 5997 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5998 | } // namespace art |