Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "class_linker.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 18 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 19 | #include <deque> |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 20 | #include <iostream> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 21 | #include <memory> |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 22 | #include <queue> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 23 | #include <string> |
Andreas Gampe | a696c0a | 2014-12-10 20:51:45 -0800 | [diff] [blame] | 24 | #include <unistd.h> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 25 | #include <utility> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 26 | #include <vector> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 27 | |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 28 | #include "base/casts.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 29 | #include "base/logging.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 30 | #include "base/scoped_flock.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 31 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 32 | #include "base/unix_file/fd_file.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 33 | #include "class_linker-inl.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 34 | #include "compiler_callbacks.h" |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 35 | #include "debugger.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 36 | #include "dex_file-inl.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 37 | #include "entrypoints/runtime_asm_entrypoints.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 38 | #include "gc_root-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 39 | #include "gc/accounting/card_table-inl.h" |
| 40 | #include "gc/accounting/heap_bitmap.h" |
| 41 | #include "gc/heap.h" |
| 42 | #include "gc/space/image_space.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 43 | #include "handle_scope.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 44 | #include "intern_table.h" |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 45 | #include "interpreter/interpreter.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 46 | #include "leb128.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 47 | #include "oat.h" |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 48 | #include "oat_file.h" |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 49 | #include "object_lock.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 50 | #include "mirror/art_field-inl.h" |
| 51 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 52 | #include "mirror/class.h" |
| 53 | #include "mirror/class-inl.h" |
| 54 | #include "mirror/class_loader.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 55 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 56 | #include "mirror/iftable-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 57 | #include "mirror/object-inl.h" |
| 58 | #include "mirror/object_array-inl.h" |
| 59 | #include "mirror/proxy.h" |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 60 | #include "mirror/reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 61 | #include "mirror/stack_trace_element.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 62 | #include "mirror/string-inl.h" |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 63 | #include "os.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 64 | #include "runtime.h" |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 65 | #include "entrypoints/entrypoint_utils.h" |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 66 | #include "ScopedLocalRef.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 67 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 68 | #include "handle_scope-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 69 | #include "thread-inl.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 70 | #include "utils.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 71 | #include "verifier/method_verifier.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 72 | #include "well_known_classes.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 73 | |
| 74 | namespace art { |
| 75 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 76 | static void ThrowNoClassDefFoundError(const char* fmt, ...) |
| 77 | __attribute__((__format__(__printf__, 1, 2))) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 78 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 79 | static void ThrowNoClassDefFoundError(const char* fmt, ...) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 80 | va_list args; |
| 81 | va_start(args, fmt); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 82 | Thread* self = Thread::Current(); |
| 83 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 84 | self->ThrowNewExceptionV(throw_location, "Ljava/lang/NoClassDefFoundError;", fmt, args); |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 85 | va_end(args); |
| 86 | } |
| 87 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 88 | static void ThrowEarlierClassFailure(mirror::Class* c) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 89 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 90 | // The class failed to initialize on a previous attempt, so we want to throw |
| 91 | // a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we |
| 92 | // failed in verification, in which case v2 5.4.1 says we need to re-throw |
| 93 | // the previous error. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 94 | Runtime* runtime = Runtime::Current(); |
| 95 | bool is_compiler = runtime->IsCompiler(); |
| 96 | if (!is_compiler) { // Give info if this occurs at runtime. |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 97 | LOG(INFO) << "Rejecting re-init on previously-failed class " << PrettyClass(c); |
| 98 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 99 | |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 100 | CHECK(c->IsErroneous()) << PrettyClass(c) << " " << c->GetStatus(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 101 | Thread* self = Thread::Current(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 102 | if (is_compiler) { |
| 103 | // At compile time, accurate errors and NCDFE are disabled to speed compilation. |
| 104 | mirror::Throwable* pre_allocated = runtime->GetPreAllocatedNoClassDefFoundError(); |
| 105 | self->SetException(ThrowLocation(), pre_allocated); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 106 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 107 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 108 | if (c->GetVerifyErrorClass() != NULL) { |
| 109 | // TODO: change the verifier to store an _instance_, with a useful detail message? |
| 110 | std::string temp; |
| 111 | self->ThrowNewException(throw_location, c->GetVerifyErrorClass()->GetDescriptor(&temp), |
| 112 | PrettyDescriptor(c).c_str()); |
| 113 | } else { |
| 114 | self->ThrowNewException(throw_location, "Ljava/lang/NoClassDefFoundError;", |
| 115 | PrettyDescriptor(c).c_str()); |
| 116 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 120 | static void VlogClassInitializationFailure(Handle<mirror::Class> klass) |
| 121 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 122 | if (VLOG_IS_ON(class_linker)) { |
| 123 | std::string temp; |
| 124 | LOG(INFO) << "Failed to initialize class " << klass->GetDescriptor(&temp) << " from " |
| 125 | << klass->GetLocation() << "\n" << Thread::Current()->GetException(nullptr)->Dump(); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | static void WrapExceptionInInitializer(Handle<mirror::Class> klass) |
| 130 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 131 | Thread* self = Thread::Current(); |
| 132 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 133 | |
| 134 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 135 | CHECK(cause.get() != nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 136 | |
| 137 | env->ExceptionClear(); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 138 | bool is_error = env->IsInstanceOf(cause.get(), WellKnownClasses::java_lang_Error); |
| 139 | env->Throw(cause.get()); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 140 | |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 141 | // We only wrap non-Error exceptions; an Error can just be used as-is. |
| 142 | if (!is_error) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 143 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 144 | self->ThrowNewWrappedException(throw_location, "Ljava/lang/ExceptionInInitializerError;", |
| 145 | nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 146 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 147 | VlogClassInitializationFailure(klass); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 150 | // Gap between two fields in object layout. |
| 151 | struct FieldGap { |
| 152 | uint32_t start_offset; // The offset from the start of the object. |
| 153 | uint32_t size; // The gap size of 1, 2, or 4 bytes. |
| 154 | }; |
| 155 | struct FieldGapsComparator { |
| 156 | explicit FieldGapsComparator() { |
| 157 | } |
| 158 | bool operator() (const FieldGap& lhs, const FieldGap& rhs) |
| 159 | NO_THREAD_SAFETY_ANALYSIS { |
| 160 | // Sort by gap size, largest first. |
| 161 | return lhs.size > rhs.size; |
| 162 | } |
| 163 | }; |
| 164 | typedef std::priority_queue<FieldGap, std::vector<FieldGap>, FieldGapsComparator> FieldGaps; |
| 165 | |
| 166 | // Adds largest aligned gaps to queue of gaps. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 167 | 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] | 168 | DCHECK(gaps != nullptr); |
| 169 | |
| 170 | uint32_t current_offset = gap_start; |
| 171 | while (current_offset != gap_end) { |
| 172 | size_t remaining = gap_end - current_offset; |
| 173 | if (remaining >= sizeof(uint32_t) && IsAligned<4>(current_offset)) { |
| 174 | gaps->push(FieldGap {current_offset, sizeof(uint32_t)}); |
| 175 | current_offset += sizeof(uint32_t); |
| 176 | } else if (remaining >= sizeof(uint16_t) && IsAligned<2>(current_offset)) { |
| 177 | gaps->push(FieldGap {current_offset, sizeof(uint16_t)}); |
| 178 | current_offset += sizeof(uint16_t); |
| 179 | } else { |
| 180 | gaps->push(FieldGap {current_offset, sizeof(uint8_t)}); |
| 181 | current_offset += sizeof(uint8_t); |
| 182 | } |
| 183 | DCHECK_LE(current_offset, gap_end) << "Overran gap"; |
| 184 | } |
| 185 | } |
| 186 | // Shuffle fields forward, making use of gaps whenever possible. |
| 187 | template<int n> |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 188 | static void ShuffleForward(size_t* current_field_idx, |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 189 | MemberOffset* field_offset, |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 190 | std::deque<mirror::ArtField*>* grouped_and_sorted_fields, |
| 191 | FieldGaps* gaps) |
| 192 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 193 | DCHECK(current_field_idx != nullptr); |
| 194 | DCHECK(grouped_and_sorted_fields != nullptr); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 195 | DCHECK(gaps != nullptr); |
| 196 | DCHECK(field_offset != nullptr); |
| 197 | |
| 198 | DCHECK(IsPowerOfTwo(n)); |
| 199 | while (!grouped_and_sorted_fields->empty()) { |
| 200 | mirror::ArtField* field = grouped_and_sorted_fields->front(); |
| 201 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 202 | if (Primitive::ComponentSize(type) < n) { |
| 203 | break; |
| 204 | } |
| 205 | if (!IsAligned<n>(field_offset->Uint32Value())) { |
| 206 | MemberOffset old_offset = *field_offset; |
| 207 | *field_offset = MemberOffset(RoundUp(field_offset->Uint32Value(), n)); |
| 208 | AddFieldGap(old_offset.Uint32Value(), field_offset->Uint32Value(), gaps); |
| 209 | } |
| 210 | CHECK(type != Primitive::kPrimNot) << PrettyField(field); // should be primitive types |
| 211 | grouped_and_sorted_fields->pop_front(); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 212 | if (!gaps->empty() && gaps->top().size >= n) { |
| 213 | FieldGap gap = gaps->top(); |
| 214 | gaps->pop(); |
| 215 | DCHECK(IsAligned<n>(gap.start_offset)); |
| 216 | field->SetOffset(MemberOffset(gap.start_offset)); |
| 217 | if (gap.size > n) { |
| 218 | AddFieldGap(gap.start_offset + n, gap.start_offset + gap.size, gaps); |
| 219 | } |
| 220 | } else { |
| 221 | DCHECK(IsAligned<n>(field_offset->Uint32Value())); |
| 222 | field->SetOffset(*field_offset); |
| 223 | *field_offset = MemberOffset(field_offset->Uint32Value() + n); |
| 224 | } |
| 225 | ++(*current_field_idx); |
| 226 | } |
| 227 | } |
| 228 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 229 | ClassLinker::ClassLinker(InternTable* intern_table) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 230 | // dex_lock_ is recursive as it may be used in stack dumping. |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 231 | : dex_lock_("ClassLinker dex lock", kDefaultMutexLevel), |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 232 | dex_cache_image_class_lookup_required_(false), |
| 233 | failed_dex_cache_class_lookups_(0), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 234 | class_roots_(nullptr), |
| 235 | array_iftable_(nullptr), |
| 236 | find_array_class_cache_next_victim_(0), |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 237 | init_done_(false), |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 238 | log_new_dex_caches_roots_(false), |
| 239 | log_new_class_table_roots_(false), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 240 | intern_table_(intern_table), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 241 | quick_resolution_trampoline_(nullptr), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 242 | quick_imt_conflict_trampoline_(nullptr), |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 243 | quick_generic_jni_trampoline_(nullptr), |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 244 | quick_to_interpreter_bridge_trampoline_(nullptr), |
| 245 | image_pointer_size_(sizeof(void*)) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 246 | memset(find_array_class_cache_, 0, kFindArrayCacheSize * sizeof(mirror::Class*)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 247 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 248 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 249 | void ClassLinker::InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 250 | VLOG(startup) << "ClassLinker::Init"; |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 251 | CHECK(!Runtime::Current()->GetHeap()->HasImageSpace()) << "Runtime has image. We should use it."; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 252 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 253 | CHECK(!init_done_); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 254 | |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 255 | // java_lang_Class comes first, it's needed for AllocClass |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 256 | Thread* self = Thread::Current(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 257 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 258 | // 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] | 259 | heap->IncrementDisableMovingGC(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 260 | StackHandleScope<64> hs(self); // 64 is picked arbitrarily. |
| 261 | Handle<mirror::Class> java_lang_Class(hs.NewHandle(down_cast<mirror::Class*>( |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 262 | heap->AllocNonMovableObject<true>(self, nullptr, |
| 263 | mirror::Class::ClassClassSize(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 264 | VoidFunctor())))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 265 | CHECK(java_lang_Class.Get() != nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 266 | mirror::Class::SetClassClass(java_lang_Class.Get()); |
| 267 | java_lang_Class->SetClass(java_lang_Class.Get()); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 268 | if (kUseBakerOrBrooksReadBarrier) { |
| 269 | java_lang_Class->AssertReadBarrierPointer(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 270 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 271 | java_lang_Class->SetClassSize(mirror::Class::ClassClassSize()); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 272 | java_lang_Class->SetPrimitiveType(Primitive::kPrimNot); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 273 | heap->DecrementDisableMovingGC(self); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 274 | // AllocClass(mirror::Class*) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 275 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 276 | // Class[] is used for reflection support. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 277 | Handle<mirror::Class> class_array_class(hs.NewHandle( |
| 278 | AllocClass(self, java_lang_Class.Get(), mirror::ObjectArray<mirror::Class>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 279 | class_array_class->SetComponentType(java_lang_Class.Get()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 280 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 281 | // 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] | 282 | Handle<mirror::Class> java_lang_Object(hs.NewHandle( |
| 283 | AllocClass(self, java_lang_Class.Get(), mirror::Object::ClassSize()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 284 | CHECK(java_lang_Object.Get() != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 285 | // backfill Object as the super class of Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 286 | java_lang_Class->SetSuperClass(java_lang_Object.Get()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 287 | java_lang_Object->SetStatus(mirror::Class::kStatusLoaded, self); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 288 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 289 | // Object[] next to hold class roots. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 290 | Handle<mirror::Class> object_array_class(hs.NewHandle( |
| 291 | AllocClass(self, java_lang_Class.Get(), mirror::ObjectArray<mirror::Object>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 292 | object_array_class->SetComponentType(java_lang_Object.Get()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 293 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 294 | // Setup the char (primitive) class to be used for char[]. |
| 295 | Handle<mirror::Class> char_class(hs.NewHandle( |
| 296 | AllocClass(self, java_lang_Class.Get(), mirror::Class::PrimitiveClassSize()))); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 297 | // The primitive char class won't be initialized by |
| 298 | // InitializePrimitiveClass until line 459, but strings (and |
| 299 | // internal char arrays) will be allocated before that and the |
| 300 | // component size, which is computed from the primitive type, needs |
| 301 | // to be set here. |
| 302 | char_class->SetPrimitiveType(Primitive::kPrimChar); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 303 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 304 | // Setup the char[] class to be used for String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 305 | Handle<mirror::Class> char_array_class(hs.NewHandle( |
| 306 | AllocClass(self, java_lang_Class.Get(), |
| 307 | mirror::Array::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 308 | char_array_class->SetComponentType(char_class.Get()); |
| 309 | mirror::CharArray::SetArrayClass(char_array_class.Get()); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 310 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 311 | // Setup String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 312 | Handle<mirror::Class> java_lang_String(hs.NewHandle( |
| 313 | AllocClass(self, java_lang_Class.Get(), mirror::String::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 314 | mirror::String::SetClass(java_lang_String.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 315 | java_lang_String->SetObjectSize(mirror::String::InstanceSize()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 316 | java_lang_String->SetStatus(mirror::Class::kStatusResolved, self); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 317 | |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 318 | // Setup Reference. |
| 319 | Handle<mirror::Class> java_lang_ref_Reference(hs.NewHandle( |
| 320 | AllocClass(self, java_lang_Class.Get(), mirror::Reference::ClassSize()))); |
| 321 | mirror::Reference::SetClass(java_lang_ref_Reference.Get()); |
| 322 | java_lang_ref_Reference->SetObjectSize(mirror::Reference::InstanceSize()); |
| 323 | java_lang_ref_Reference->SetStatus(mirror::Class::kStatusResolved, self); |
| 324 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 325 | // 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] | 326 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class> >( |
| 327 | mirror::ObjectArray<mirror::Class>::Alloc(self, object_array_class.Get(), |
| 328 | kClassRootsMax)); |
| 329 | CHECK(!class_roots_.IsNull()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 330 | SetClassRoot(kJavaLangClass, java_lang_Class.Get()); |
| 331 | SetClassRoot(kJavaLangObject, java_lang_Object.Get()); |
| 332 | SetClassRoot(kClassArrayClass, class_array_class.Get()); |
| 333 | SetClassRoot(kObjectArrayClass, object_array_class.Get()); |
| 334 | SetClassRoot(kCharArrayClass, char_array_class.Get()); |
| 335 | SetClassRoot(kJavaLangString, java_lang_String.Get()); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 336 | SetClassRoot(kJavaLangRefReference, java_lang_ref_Reference.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 337 | |
| 338 | // Setup the primitive type classes. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 339 | SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass(self, Primitive::kPrimBoolean)); |
| 340 | SetClassRoot(kPrimitiveByte, CreatePrimitiveClass(self, Primitive::kPrimByte)); |
| 341 | SetClassRoot(kPrimitiveShort, CreatePrimitiveClass(self, Primitive::kPrimShort)); |
| 342 | SetClassRoot(kPrimitiveInt, CreatePrimitiveClass(self, Primitive::kPrimInt)); |
| 343 | SetClassRoot(kPrimitiveLong, CreatePrimitiveClass(self, Primitive::kPrimLong)); |
| 344 | SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass(self, Primitive::kPrimFloat)); |
| 345 | SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass(self, Primitive::kPrimDouble)); |
| 346 | SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass(self, Primitive::kPrimVoid)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 347 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 348 | // Create array interface entries to populate once we can load system classes. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 349 | array_iftable_ = GcRoot<mirror::IfTable>(AllocIfTable(self, 2)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 350 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 351 | // Create int array type for AllocDexCache (done in AppendToBootClassPath). |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 352 | Handle<mirror::Class> int_array_class(hs.NewHandle( |
| 353 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize()))); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 354 | int_array_class->SetComponentType(GetClassRoot(kPrimitiveInt)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 355 | mirror::IntArray::SetArrayClass(int_array_class.Get()); |
| 356 | SetClassRoot(kIntArrayClass, int_array_class.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 357 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 358 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 359 | |
Ian Rogers | 52813c9 | 2012-10-11 11:50:38 -0700 | [diff] [blame] | 360 | // Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 361 | Handle<mirror::Class> java_lang_DexCache(hs.NewHandle( |
| 362 | AllocClass(self, java_lang_Class.Get(), mirror::DexCache::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 363 | SetClassRoot(kJavaLangDexCache, java_lang_DexCache.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 364 | java_lang_DexCache->SetObjectSize(mirror::DexCache::InstanceSize()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 365 | java_lang_DexCache->SetStatus(mirror::Class::kStatusResolved, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 366 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 367 | // Constructor, Field, Method, and AbstractMethod are necessary so |
| 368 | // that FindClass can link members. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 369 | Handle<mirror::Class> java_lang_reflect_ArtField(hs.NewHandle( |
| 370 | AllocClass(self, java_lang_Class.Get(), mirror::ArtField::ClassSize()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 371 | CHECK(java_lang_reflect_ArtField.Get() != nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 372 | java_lang_reflect_ArtField->SetObjectSize(mirror::ArtField::InstanceSize()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 373 | SetClassRoot(kJavaLangReflectArtField, java_lang_reflect_ArtField.Get()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 374 | java_lang_reflect_ArtField->SetStatus(mirror::Class::kStatusResolved, self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 375 | mirror::ArtField::SetClass(java_lang_reflect_ArtField.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 376 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 377 | Handle<mirror::Class> java_lang_reflect_ArtMethod(hs.NewHandle( |
| 378 | AllocClass(self, java_lang_Class.Get(), mirror::ArtMethod::ClassSize()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 379 | CHECK(java_lang_reflect_ArtMethod.Get() != nullptr); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 380 | java_lang_reflect_ArtMethod->SetObjectSize(mirror::ArtMethod::InstanceSize(sizeof(void*))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 381 | SetClassRoot(kJavaLangReflectArtMethod, java_lang_reflect_ArtMethod.Get()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 382 | java_lang_reflect_ArtMethod->SetStatus(mirror::Class::kStatusResolved, self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 383 | mirror::ArtMethod::SetClass(java_lang_reflect_ArtMethod.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 384 | |
| 385 | // Set up array classes for string, field, method |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 386 | Handle<mirror::Class> object_array_string(hs.NewHandle( |
| 387 | AllocClass(self, java_lang_Class.Get(), |
| 388 | mirror::ObjectArray<mirror::String>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 389 | object_array_string->SetComponentType(java_lang_String.Get()); |
| 390 | SetClassRoot(kJavaLangStringArrayClass, object_array_string.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 391 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 392 | Handle<mirror::Class> object_array_art_method(hs.NewHandle( |
| 393 | AllocClass(self, java_lang_Class.Get(), |
| 394 | mirror::ObjectArray<mirror::ArtMethod>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 395 | object_array_art_method->SetComponentType(java_lang_reflect_ArtMethod.Get()); |
| 396 | SetClassRoot(kJavaLangReflectArtMethodArrayClass, object_array_art_method.Get()); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 397 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 398 | Handle<mirror::Class> object_array_art_field(hs.NewHandle( |
| 399 | AllocClass(self, java_lang_Class.Get(), |
| 400 | mirror::ObjectArray<mirror::ArtField>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 401 | object_array_art_field->SetComponentType(java_lang_reflect_ArtField.Get()); |
| 402 | SetClassRoot(kJavaLangReflectArtFieldArrayClass, object_array_art_field.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 403 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 404 | // Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create |
| 405 | // DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses |
| 406 | // these roots. |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 407 | CHECK_NE(0U, boot_class_path.size()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 408 | for (auto& dex_file : boot_class_path) { |
| 409 | CHECK(dex_file.get() != nullptr); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 410 | AppendToBootClassPath(self, *dex_file); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 411 | opened_dex_files_.push_back(std::move(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 | |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 578 | // Ensure void type is resolved in the core's dex cache so java.lang.Void is correctly |
| 579 | // initialized. |
| 580 | { |
| 581 | const DexFile& dex_file = java_lang_Object->GetDexFile(); |
| 582 | const DexFile::StringId* void_string_id = dex_file.FindStringId("V"); |
| 583 | CHECK(void_string_id != nullptr); |
| 584 | uint32_t void_string_index = dex_file.GetIndexForStringId(*void_string_id); |
| 585 | const DexFile::TypeId* void_type_id = dex_file.FindTypeId(void_string_index); |
| 586 | CHECK(void_type_id != nullptr); |
| 587 | uint16_t void_type_idx = dex_file.GetIndexForTypeId(*void_type_id); |
| 588 | // Now we resolve void type so the dex cache contains it. We use java.lang.Object class |
| 589 | // as referrer so the used dex cache is core's one. |
| 590 | mirror::Class* resolved_type = ResolveType(dex_file, void_type_idx, java_lang_Object.Get()); |
| 591 | CHECK_EQ(resolved_type, GetClassRoot(kPrimitiveVoid)); |
| 592 | self->AssertNoPendingException(); |
| 593 | } |
| 594 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 595 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 596 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 597 | VLOG(startup) << "ClassLinker::InitFromCompiler exiting"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 600 | void ClassLinker::FinishInit(Thread* self) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 601 | VLOG(startup) << "ClassLinker::FinishInit entering"; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 602 | |
| 603 | // Let the heap know some key offsets into java.lang.ref instances |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 604 | // Note: we hard code the field indexes here rather than using FindInstanceField |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 605 | // as the types of the field can't be resolved prior to the runtime being |
| 606 | // fully initialized |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 607 | mirror::Class* java_lang_ref_Reference = GetClassRoot(kJavaLangRefReference); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 608 | mirror::Class* java_lang_ref_FinalizerReference = |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 609 | FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 610 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 611 | mirror::ArtField* pendingNext = java_lang_ref_Reference->GetInstanceField(0); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 612 | CHECK_STREQ(pendingNext->GetName(), "pendingNext"); |
| 613 | CHECK_STREQ(pendingNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 614 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 615 | mirror::ArtField* queue = java_lang_ref_Reference->GetInstanceField(1); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 616 | CHECK_STREQ(queue->GetName(), "queue"); |
| 617 | CHECK_STREQ(queue->GetTypeDescriptor(), "Ljava/lang/ref/ReferenceQueue;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 618 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 619 | mirror::ArtField* queueNext = java_lang_ref_Reference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 620 | CHECK_STREQ(queueNext->GetName(), "queueNext"); |
| 621 | CHECK_STREQ(queueNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 622 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 623 | mirror::ArtField* referent = java_lang_ref_Reference->GetInstanceField(3); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 624 | CHECK_STREQ(referent->GetName(), "referent"); |
| 625 | CHECK_STREQ(referent->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 626 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 627 | mirror::ArtField* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 628 | CHECK_STREQ(zombie->GetName(), "zombie"); |
| 629 | CHECK_STREQ(zombie->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 630 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 631 | // ensure all class_roots_ are initialized |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 632 | for (size_t i = 0; i < kClassRootsMax; i++) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 633 | ClassRoot class_root = static_cast<ClassRoot>(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 634 | mirror::Class* klass = GetClassRoot(class_root); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 635 | CHECK(klass != nullptr); |
| 636 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != nullptr); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 637 | // note SetClassRoot does additional validation. |
| 638 | // if possible add new checks there to catch errors early |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 641 | CHECK(!array_iftable_.IsNull()); |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 642 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 643 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 644 | // that Object, Class, and Object[] are setup |
| 645 | init_done_ = true; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 646 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 647 | VLOG(startup) << "ClassLinker::FinishInit exiting"; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 650 | void ClassLinker::RunRootClinits() { |
| 651 | Thread* self = Thread::Current(); |
| 652 | for (size_t i = 0; i < ClassLinker::kClassRootsMax; ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 653 | mirror::Class* c = GetClassRoot(ClassRoot(i)); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 654 | if (!c->IsArrayClass() && !c->IsPrimitive()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 655 | StackHandleScope<1> hs(self); |
| 656 | Handle<mirror::Class> h_class(hs.NewHandle(GetClassRoot(ClassRoot(i)))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 657 | EnsureInitialized(self, h_class, true, true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 658 | self->AssertNoPendingException(); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | } |
| 662 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 663 | bool ClassLinker::GenerateOatFile(const char* dex_filename, |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 664 | int oat_fd, |
Vladimir Marko | 60836d5 | 2014-01-16 15:53:38 +0000 | [diff] [blame] | 665 | const char* oat_cache_filename, |
| 666 | std::string* error_msg) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 667 | Locks::mutator_lock_->AssertNotHeld(Thread::Current()); // Avoid starving GC. |
Tsu Chiang Chuang | 12e6d74 | 2014-05-22 10:22:25 -0700 | [diff] [blame] | 668 | std::string dex2oat(Runtime::Current()->GetCompilerExecutable()); |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 669 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 670 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 671 | std::string boot_image_option("--boot-image="); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 672 | if (heap->GetImageSpace() == nullptr) { |
| 673 | // TODO If we get a dex2dex compiler working we could maybe use that, OTOH since we are likely |
| 674 | // out of space anyway it might not matter. |
| 675 | *error_msg = StringPrintf("Cannot create oat file for '%s' because we are running " |
| 676 | "without an image.", dex_filename); |
| 677 | return false; |
| 678 | } |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 679 | boot_image_option += heap->GetImageSpace()->GetImageLocation(); |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 680 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 681 | std::string dex_file_option("--dex-file="); |
| 682 | dex_file_option += dex_filename; |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 683 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 684 | std::string oat_fd_option("--oat-fd="); |
| 685 | StringAppendF(&oat_fd_option, "%d", oat_fd); |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 686 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 687 | std::string oat_location_option("--oat-location="); |
| 688 | oat_location_option += oat_cache_filename; |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 689 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 690 | std::vector<std::string> argv; |
| 691 | argv.push_back(dex2oat); |
| 692 | argv.push_back("--runtime-arg"); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 693 | argv.push_back("-classpath"); |
| 694 | argv.push_back("--runtime-arg"); |
Brian Carlstrom | 35d8b8e | 2014-02-25 10:51:11 -0800 | [diff] [blame] | 695 | argv.push_back(Runtime::Current()->GetClassPathString()); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 696 | |
Ian Rogers | 8afeb85 | 2014-04-02 14:55:49 -0700 | [diff] [blame] | 697 | Runtime::Current()->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 698 | |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 699 | if (!Runtime::Current()->IsVerificationEnabled()) { |
| 700 | argv.push_back("--compiler-filter=verify-none"); |
| 701 | } |
| 702 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 703 | if (Runtime::Current()->MustRelocateIfPossible()) { |
| 704 | argv.push_back("--runtime-arg"); |
| 705 | argv.push_back("-Xrelocate"); |
| 706 | } else { |
| 707 | argv.push_back("--runtime-arg"); |
| 708 | argv.push_back("-Xnorelocate"); |
| 709 | } |
| 710 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 711 | if (!kIsTargetBuild) { |
| 712 | argv.push_back("--host"); |
buzbee | a024a06 | 2013-07-31 10:47:37 -0700 | [diff] [blame] | 713 | } |
Ian Rogers | 8afeb85 | 2014-04-02 14:55:49 -0700 | [diff] [blame] | 714 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 715 | argv.push_back(boot_image_option); |
| 716 | argv.push_back(dex_file_option); |
| 717 | argv.push_back(oat_fd_option); |
| 718 | argv.push_back(oat_location_option); |
| 719 | const std::vector<std::string>& compiler_options = Runtime::Current()->GetCompilerOptions(); |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 720 | for (size_t i = 0; i < compiler_options.size(); ++i) { |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 721 | argv.push_back(compiler_options[i].c_str()); |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 722 | } |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 723 | |
Andreas Gampe | a696c0a | 2014-12-10 20:51:45 -0800 | [diff] [blame] | 724 | if (!Exec(argv, error_msg)) { |
| 725 | // Manually delete the file. Ensures there is no garbage left over if the process unexpectedly |
| 726 | // died. Ignore unlink failure, propagate the original error. |
| 727 | TEMP_FAILURE_RETRY(unlink(oat_cache_filename)); |
| 728 | return false; |
| 729 | } |
| 730 | |
| 731 | return true; |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 734 | const OatFile* ClassLinker::RegisterOatFile(const OatFile* oat_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 735 | WriterMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 736 | if (kIsDebugBuild) { |
| 737 | for (size_t i = 0; i < oat_files_.size(); ++i) { |
| 738 | CHECK_NE(oat_file, oat_files_[i]) << oat_file->GetLocation(); |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 739 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 740 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 741 | VLOG(class_linker) << "Registering " << oat_file->GetLocation(); |
| 742 | oat_files_.push_back(oat_file); |
| 743 | return oat_file; |
Brian Carlstrom | 866c862 | 2012-01-06 16:35:13 -0800 | [diff] [blame] | 744 | } |
| 745 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 746 | OatFile& ClassLinker::GetImageOatFile(gc::space::ImageSpace* space) { |
| 747 | VLOG(startup) << "ClassLinker::GetImageOatFile entering"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 748 | OatFile* oat_file = space->ReleaseOatFile(); |
| 749 | CHECK_EQ(RegisterOatFile(oat_file), oat_file); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 750 | VLOG(startup) << "ClassLinker::GetImageOatFile exiting"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 751 | return *oat_file; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 754 | const OatFile::OatDexFile* ClassLinker::FindOpenedOatDexFileForDexFile(const DexFile& dex_file) { |
Brian Carlstrom | 08cbf66 | 2013-12-10 16:52:57 -0800 | [diff] [blame] | 755 | const char* dex_location = dex_file.GetLocation().c_str(); |
| 756 | uint32_t dex_location_checksum = dex_file.GetLocationChecksum(); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 757 | return FindOpenedOatDexFile(nullptr, dex_location, &dex_location_checksum); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 758 | } |
| 759 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 760 | const OatFile::OatDexFile* ClassLinker::FindOpenedOatDexFile(const char* oat_location, |
| 761 | const char* dex_location, |
| 762 | const uint32_t* dex_location_checksum) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 763 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 764 | for (const OatFile* oat_file : oat_files_) { |
| 765 | DCHECK(oat_file != nullptr); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 766 | |
| 767 | if (oat_location != nullptr) { |
| 768 | if (oat_file->GetLocation() != oat_location) { |
| 769 | continue; |
| 770 | } |
| 771 | } |
| 772 | |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 773 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, |
Brian Carlstrom | 08cbf66 | 2013-12-10 16:52:57 -0800 | [diff] [blame] | 774 | dex_location_checksum, |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 775 | false); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 776 | if (oat_dex_file != nullptr) { |
| 777 | return oat_dex_file; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 778 | } |
| 779 | } |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 780 | return nullptr; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 781 | } |
| 782 | |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 783 | |
| 784 | // Loads all multi dex files from the given oat file returning true on success. |
| 785 | // |
| 786 | // Parameters: |
| 787 | // oat_file - the oat file to load from |
| 788 | // dex_location - the dex location used to generate the oat file |
| 789 | // dex_location_checksum - the checksum of the dex_location (may be null for pre-opted files) |
| 790 | // generated - whether or not the oat_file existed before or was just (re)generated |
| 791 | // error_msgs - any error messages will be appended here |
| 792 | // dex_files - the loaded dex_files will be appended here (only if the loading succeeds) |
| 793 | static bool LoadMultiDexFilesFromOatFile(const OatFile* oat_file, |
| 794 | const char* dex_location, |
| 795 | const uint32_t* dex_location_checksum, |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 796 | bool generated, |
| 797 | std::vector<std::string>* error_msgs, |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 798 | std::vector<std::unique_ptr<const DexFile>>* dex_files) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 799 | if (oat_file == nullptr) { |
| 800 | return false; |
| 801 | } |
| 802 | |
| 803 | size_t old_size = dex_files->size(); // To rollback on error. |
| 804 | |
| 805 | bool success = true; |
| 806 | for (size_t i = 0; success; ++i) { |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 807 | std::string next_name_str = DexFile::GetMultiDexClassesDexName(i, dex_location); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 808 | const char* next_name = next_name_str.c_str(); |
| 809 | |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 810 | uint32_t next_location_checksum; |
| 811 | uint32_t* next_location_checksum_pointer = &next_location_checksum; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 812 | std::string error_msg; |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 813 | if ((i == 0) && (strcmp(next_name, dex_location) == 0)) { |
| 814 | // When i=0 the multidex name should be the same as the location name. We already have the |
| 815 | // checksum it so we don't need to recompute it. |
| 816 | if (dex_location_checksum == nullptr) { |
| 817 | next_location_checksum_pointer = nullptr; |
| 818 | } else { |
| 819 | next_location_checksum = *dex_location_checksum; |
| 820 | } |
| 821 | } else if (!DexFile::GetChecksum(next_name, next_location_checksum_pointer, &error_msg)) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 822 | DCHECK_EQ(false, i == 0 && generated); |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 823 | next_location_checksum_pointer = nullptr; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(next_name, nullptr, false); |
| 827 | |
| 828 | if (oat_dex_file == nullptr) { |
| 829 | if (i == 0 && generated) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 830 | 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] | 831 | " file'%s'", dex_location, next_location_checksum, |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 832 | oat_file->GetLocation().c_str()); |
| 833 | error_msgs->push_back(error_msg); |
| 834 | } |
| 835 | break; // Not found, done. |
| 836 | } |
| 837 | |
| 838 | // Checksum test. Test must succeed when generated. |
| 839 | success = !generated; |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 840 | if (next_location_checksum_pointer != nullptr) { |
| 841 | success = next_location_checksum == oat_dex_file->GetDexFileLocationChecksum(); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | if (success) { |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 845 | std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg); |
| 846 | if (dex_file.get() == nullptr) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 847 | success = false; |
| 848 | error_msgs->push_back(error_msg); |
| 849 | } else { |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 850 | dex_files->push_back(std::move(dex_file)); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | |
| 854 | // When we generated the file, we expect success, or something is terribly wrong. |
| 855 | CHECK_EQ(false, generated && !success) |
| 856 | << "dex_location=" << next_name << " oat_location=" << oat_file->GetLocation().c_str() |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 857 | << std::hex << " dex_location_checksum=" << next_location_checksum |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 858 | << " OatDexFile::GetLocationChecksum()=" << oat_dex_file->GetDexFileLocationChecksum(); |
| 859 | } |
| 860 | |
| 861 | if (dex_files->size() == old_size) { |
| 862 | success = false; // We did not even find classes.dex |
| 863 | } |
| 864 | |
| 865 | if (success) { |
| 866 | return true; |
| 867 | } else { |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 868 | dex_files->erase(dex_files->begin() + old_size, dex_files->end()); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 869 | return false; |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | // Multidex files make it possible that some, but not all, dex files can be broken/outdated. This |
| 874 | // complicates the loading process, as we should not use an iterative loading process, because that |
| 875 | // would register the oat file and dex files that come before the broken one. Instead, check all |
| 876 | // multidex ahead of time. |
| 877 | bool ClassLinker::OpenDexFilesFromOat(const char* dex_location, const char* oat_location, |
| 878 | std::vector<std::string>* error_msgs, |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 879 | std::vector<std::unique_ptr<const DexFile>>* dex_files) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 880 | // 1) Check whether we have an open oat file. |
| 881 | // This requires a dex checksum, use the "primary" one. |
| 882 | uint32_t dex_location_checksum; |
| 883 | uint32_t* dex_location_checksum_pointer = &dex_location_checksum; |
| 884 | bool have_checksum = true; |
| 885 | std::string checksum_error_msg; |
| 886 | if (!DexFile::GetChecksum(dex_location, dex_location_checksum_pointer, &checksum_error_msg)) { |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 887 | // 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] | 888 | dex_location_checksum_pointer = nullptr; |
| 889 | have_checksum = false; |
| 890 | } |
| 891 | |
| 892 | bool needs_registering = false; |
| 893 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 894 | const OatFile::OatDexFile* oat_dex_file = FindOpenedOatDexFile(oat_location, dex_location, |
| 895 | dex_location_checksum_pointer); |
| 896 | std::unique_ptr<const OatFile> open_oat_file( |
| 897 | oat_dex_file != nullptr ? oat_dex_file->GetOatFile() : nullptr); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 898 | |
| 899 | // 2) If we do not have an open one, maybe there's one on disk already. |
| 900 | |
| 901 | // In case the oat file is not open, we play a locking game here so |
| 902 | // that if two different processes race to load and register or generate |
| 903 | // (or worse, one tries to open a partial generated file) we will be okay. |
| 904 | // This is actually common with apps that use DexClassLoader to work |
| 905 | // around the dex method reference limit and that have a background |
| 906 | // service running in a separate process. |
| 907 | ScopedFlock scoped_flock; |
| 908 | |
| 909 | if (open_oat_file.get() == nullptr) { |
| 910 | if (oat_location != nullptr) { |
| 911 | // Can only do this if we have a checksum, else error. |
| 912 | if (!have_checksum) { |
| 913 | error_msgs->push_back(checksum_error_msg); |
| 914 | return false; |
| 915 | } |
| 916 | |
| 917 | std::string error_msg; |
| 918 | |
| 919 | // We are loading or creating one in the future. Time to set up the file lock. |
| 920 | if (!scoped_flock.Init(oat_location, &error_msg)) { |
| 921 | error_msgs->push_back(error_msg); |
| 922 | return false; |
| 923 | } |
| 924 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 925 | // 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] | 926 | open_oat_file.reset(FindOatFileInOatLocationForDexFile(dex_location, dex_location_checksum, |
| 927 | oat_location, &error_msg)); |
| 928 | |
| 929 | if (open_oat_file.get() == nullptr) { |
| 930 | std::string compound_msg = StringPrintf("Failed to find dex file '%s' in oat location '%s': %s", |
| 931 | dex_location, oat_location, error_msg.c_str()); |
| 932 | VLOG(class_linker) << compound_msg; |
| 933 | error_msgs->push_back(compound_msg); |
| 934 | } |
| 935 | } else { |
| 936 | // TODO: What to lock here? |
Calin Juravle | c54aea7 | 2014-07-16 14:45:03 +0100 | [diff] [blame] | 937 | bool obsolete_file_cleanup_failed; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 938 | open_oat_file.reset(FindOatFileContainingDexFileFromDexLocation(dex_location, |
| 939 | dex_location_checksum_pointer, |
Calin Juravle | c54aea7 | 2014-07-16 14:45:03 +0100 | [diff] [blame] | 940 | kRuntimeISA, error_msgs, |
| 941 | &obsolete_file_cleanup_failed)); |
| 942 | // There's no point in going forward and eventually try to regenerate the |
| 943 | // file if we couldn't remove the obsolete one. Mostly likely we will fail |
| 944 | // with the same error when trying to write the new file. |
Calin Juravle | c54aea7 | 2014-07-16 14:45:03 +0100 | [diff] [blame] | 945 | // TODO: should we maybe do this only when we get permission issues? (i.e. EACCESS). |
| 946 | if (obsolete_file_cleanup_failed) { |
| 947 | return false; |
| 948 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 949 | } |
| 950 | needs_registering = true; |
| 951 | } |
| 952 | |
| 953 | // 3) If we have an oat file, check all contained multidex files for our dex_location. |
| 954 | // Note: LoadMultiDexFilesFromOatFile will check for nullptr in the first argument. |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 955 | bool success = LoadMultiDexFilesFromOatFile(open_oat_file.get(), dex_location, |
| 956 | dex_location_checksum_pointer, |
| 957 | false, error_msgs, dex_files); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 958 | if (success) { |
| 959 | const OatFile* oat_file = open_oat_file.release(); // Avoid deleting it. |
| 960 | if (needs_registering) { |
| 961 | // We opened the oat file, so we must register it. |
| 962 | RegisterOatFile(oat_file); |
| 963 | } |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 964 | // If the file isn't executable we failed patchoat but did manage to get the dex files. |
| 965 | return oat_file->IsExecutable(); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 966 | } else { |
| 967 | if (needs_registering) { |
| 968 | // We opened it, delete it. |
| 969 | open_oat_file.reset(); |
| 970 | } else { |
| 971 | open_oat_file.release(); // Do not delete open oat files. |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | // 4) If it's not the case (either no oat file or mismatches), regenerate and load. |
| 976 | |
| 977 | // Need a checksum, fail else. |
| 978 | if (!have_checksum) { |
| 979 | error_msgs->push_back(checksum_error_msg); |
| 980 | return false; |
| 981 | } |
| 982 | |
| 983 | // Look in cache location if no oat_location is given. |
| 984 | std::string cache_location; |
| 985 | if (oat_location == nullptr) { |
| 986 | // Use the dalvik cache. |
| 987 | const std::string dalvik_cache(GetDalvikCacheOrDie(GetInstructionSetString(kRuntimeISA))); |
| 988 | cache_location = GetDalvikCacheFilenameOrDie(dex_location, dalvik_cache.c_str()); |
| 989 | oat_location = cache_location.c_str(); |
| 990 | } |
| 991 | |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 992 | bool has_flock = true; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 993 | // Definitely need to lock now. |
| 994 | if (!scoped_flock.HasFile()) { |
| 995 | std::string error_msg; |
| 996 | if (!scoped_flock.Init(oat_location, &error_msg)) { |
| 997 | error_msgs->push_back(error_msg); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 998 | has_flock = false; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1002 | if (Runtime::Current()->IsDex2OatEnabled() && has_flock && scoped_flock.HasFile()) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 1003 | // Create the oat file. |
| 1004 | open_oat_file.reset(CreateOatFileForDexLocation(dex_location, scoped_flock.GetFile()->Fd(), |
| 1005 | oat_location, error_msgs)); |
| 1006 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1007 | |
| 1008 | // Failed, bail. |
| 1009 | if (open_oat_file.get() == nullptr) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 1010 | std::string error_msg; |
| 1011 | // dex2oat was disabled or crashed. Add the dex file in the list of dex_files to make progress. |
| 1012 | DexFile::Open(dex_location, dex_location, &error_msg, dex_files); |
| 1013 | error_msgs->push_back(error_msg); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1014 | return false; |
| 1015 | } |
| 1016 | |
| 1017 | // Try to load again, but stronger checks. |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1018 | success = LoadMultiDexFilesFromOatFile(open_oat_file.get(), dex_location, |
| 1019 | dex_location_checksum_pointer, |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 1020 | true, error_msgs, dex_files); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1021 | if (success) { |
| 1022 | RegisterOatFile(open_oat_file.release()); |
| 1023 | return true; |
| 1024 | } else { |
| 1025 | return false; |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | const OatFile* ClassLinker::FindOatFileInOatLocationForDexFile(const char* dex_location, |
| 1030 | uint32_t dex_location_checksum, |
| 1031 | const char* oat_location, |
| 1032 | std::string* error_msg) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1033 | 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] | 1034 | !Runtime::Current()->IsCompiler(), |
| 1035 | error_msg)); |
| 1036 | if (oat_file.get() == nullptr) { |
| 1037 | *error_msg = StringPrintf("Failed to find existing oat file at %s: %s", oat_location, |
| 1038 | error_msg->c_str()); |
| 1039 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1040 | } |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 1041 | Runtime* runtime = Runtime::Current(); |
Alex Light | 7adb7ac | 2014-08-29 10:28:25 -0700 | [diff] [blame] | 1042 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace(); |
| 1043 | if (image_space != nullptr) { |
| 1044 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1045 | uint32_t expected_image_oat_checksum = image_header.GetOatChecksum(); |
| 1046 | uint32_t actual_image_oat_checksum = oat_file->GetOatHeader().GetImageFileLocationOatChecksum(); |
| 1047 | if (expected_image_oat_checksum != actual_image_oat_checksum) { |
| 1048 | *error_msg = StringPrintf("Failed to find oat file at '%s' with expected image oat checksum of " |
| 1049 | "0x%x, found 0x%x", oat_location, expected_image_oat_checksum, |
| 1050 | actual_image_oat_checksum); |
| 1051 | return nullptr; |
| 1052 | } |
| 1053 | |
| 1054 | uintptr_t expected_image_oat_offset = reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()); |
| 1055 | uint32_t actual_image_oat_offset = oat_file->GetOatHeader().GetImageFileLocationOatDataBegin(); |
| 1056 | if (expected_image_oat_offset != actual_image_oat_offset) { |
| 1057 | *error_msg = StringPrintf("Failed to find oat file at '%s' with expected image oat offset %" |
| 1058 | PRIuPTR ", found %ud", oat_location, expected_image_oat_offset, |
| 1059 | actual_image_oat_offset); |
| 1060 | return nullptr; |
| 1061 | } |
| 1062 | int32_t expected_patch_delta = image_header.GetPatchDelta(); |
| 1063 | int32_t actual_patch_delta = oat_file->GetOatHeader().GetImagePatchDelta(); |
| 1064 | if (expected_patch_delta != actual_patch_delta) { |
| 1065 | *error_msg = StringPrintf("Failed to find oat file at '%s' with expected patch delta %d, " |
| 1066 | " found %d", oat_location, expected_patch_delta, actual_patch_delta); |
| 1067 | return nullptr; |
| 1068 | } |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 1069 | } |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1070 | |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 1071 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, |
| 1072 | &dex_location_checksum); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1073 | if (oat_dex_file == nullptr) { |
| 1074 | *error_msg = StringPrintf("Failed to find oat file at '%s' containing '%s'", oat_location, |
| 1075 | dex_location); |
| 1076 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1077 | } |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1078 | uint32_t expected_dex_checksum = dex_location_checksum; |
| 1079 | uint32_t actual_dex_checksum = oat_dex_file->GetDexFileLocationChecksum(); |
| 1080 | if (expected_dex_checksum != actual_dex_checksum) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1081 | *error_msg = StringPrintf("Failed to find oat file at '%s' with expected dex checksum of 0x%x, " |
| 1082 | "found 0x%x", oat_location, expected_dex_checksum, |
| 1083 | actual_dex_checksum); |
| 1084 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1085 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1086 | std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(error_msg)); |
| 1087 | if (dex_file.get() != nullptr) { |
| 1088 | return oat_file.release(); |
| 1089 | } else { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1090 | return nullptr; |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1091 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1092 | } |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1093 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1094 | const OatFile* ClassLinker::CreateOatFileForDexLocation(const char* dex_location, |
| 1095 | int fd, const char* oat_location, |
| 1096 | std::vector<std::string>* error_msgs) { |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1097 | // Generate the output oat file for the dex file |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1098 | VLOG(class_linker) << "Generating oat file " << oat_location << " for " << dex_location; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1099 | std::string error_msg; |
| 1100 | if (!GenerateOatFile(dex_location, fd, oat_location, &error_msg)) { |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 1101 | CHECK(!error_msg.empty()); |
| 1102 | error_msgs->push_back(error_msg); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1103 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1104 | } |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1105 | 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] | 1106 | !Runtime::Current()->IsCompiler(), |
| 1107 | &error_msg)); |
| 1108 | if (oat_file.get() == nullptr) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1109 | std::string compound_msg = StringPrintf("\nFailed to open generated oat file '%s': %s", |
| 1110 | oat_location, error_msg.c_str()); |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 1111 | error_msgs->push_back(compound_msg); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1112 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1113 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1114 | |
| 1115 | return oat_file.release(); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1116 | } |
| 1117 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1118 | bool ClassLinker::VerifyOatImageChecksum(const OatFile* oat_file, |
| 1119 | const InstructionSet instruction_set) { |
| 1120 | Runtime* runtime = Runtime::Current(); |
| 1121 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1122 | if (image_space == nullptr) { |
| 1123 | return false; |
| 1124 | } |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1125 | uint32_t image_oat_checksum = 0; |
| 1126 | if (instruction_set == kRuntimeISA) { |
| 1127 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1128 | image_oat_checksum = image_header.GetOatChecksum(); |
| 1129 | } else { |
| 1130 | std::unique_ptr<ImageHeader> image_header(gc::space::ImageSpace::ReadImageHeaderOrDie( |
| 1131 | image_space->GetImageLocation().c_str(), instruction_set)); |
| 1132 | image_oat_checksum = image_header->GetOatChecksum(); |
| 1133 | } |
| 1134 | return oat_file->GetOatHeader().GetImageFileLocationOatChecksum() == image_oat_checksum; |
| 1135 | } |
| 1136 | |
| 1137 | bool ClassLinker::VerifyOatChecksums(const OatFile* oat_file, |
| 1138 | const InstructionSet instruction_set, |
| 1139 | std::string* error_msg) { |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 1140 | Runtime* runtime = Runtime::Current(); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1141 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1142 | if (image_space == nullptr) { |
| 1143 | *error_msg = "No image space for verification against"; |
| 1144 | return false; |
| 1145 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1146 | |
| 1147 | // If the requested instruction set is the same as the current runtime, |
| 1148 | // we can use the checksums directly. If it isn't, we'll have to read the |
| 1149 | // image header from the image for the right instruction set. |
| 1150 | uint32_t image_oat_checksum = 0; |
| 1151 | uintptr_t image_oat_data_begin = 0; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1152 | int32_t image_patch_delta = 0; |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 1153 | if (instruction_set == runtime->GetInstructionSet()) { |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1154 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1155 | image_oat_checksum = image_header.GetOatChecksum(); |
| 1156 | image_oat_data_begin = reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1157 | image_patch_delta = image_header.GetPatchDelta(); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1158 | } else { |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1159 | std::unique_ptr<ImageHeader> image_header(gc::space::ImageSpace::ReadImageHeaderOrDie( |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1160 | image_space->GetImageLocation().c_str(), instruction_set)); |
| 1161 | image_oat_checksum = image_header->GetOatChecksum(); |
| 1162 | image_oat_data_begin = reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1163 | image_patch_delta = image_header->GetPatchDelta(); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1164 | } |
| 1165 | const OatHeader& oat_header = oat_file->GetOatHeader(); |
Igor Murashkin | 96e8393 | 2014-10-29 19:45:42 -0700 | [diff] [blame] | 1166 | bool ret = (oat_header.GetImageFileLocationOatChecksum() == image_oat_checksum); |
| 1167 | |
| 1168 | // If the oat file is PIC, it doesn't care if/how image was relocated. Ignore these checks. |
| 1169 | if (!oat_file->IsPic()) { |
| 1170 | ret = ret && (oat_header.GetImagePatchDelta() == image_patch_delta) |
| 1171 | && (oat_header.GetImageFileLocationOatDataBegin() == image_oat_data_begin); |
| 1172 | } |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1173 | if (!ret) { |
| 1174 | *error_msg = StringPrintf("oat file '%s' mismatch (0x%x, %d, %d) with (0x%x, %" PRIdPTR ", %d)", |
| 1175 | oat_file->GetLocation().c_str(), |
| 1176 | oat_file->GetOatHeader().GetImageFileLocationOatChecksum(), |
| 1177 | oat_file->GetOatHeader().GetImageFileLocationOatDataBegin(), |
| 1178 | oat_file->GetOatHeader().GetImagePatchDelta(), |
| 1179 | image_oat_checksum, image_oat_data_begin, image_patch_delta); |
| 1180 | } |
| 1181 | return ret; |
| 1182 | } |
| 1183 | |
| 1184 | bool ClassLinker::VerifyOatAndDexFileChecksums(const OatFile* oat_file, |
| 1185 | const char* dex_location, |
| 1186 | uint32_t dex_location_checksum, |
| 1187 | const InstructionSet instruction_set, |
| 1188 | std::string* error_msg) { |
| 1189 | if (!VerifyOatChecksums(oat_file, instruction_set, error_msg)) { |
| 1190 | return false; |
| 1191 | } |
Brian Carlstrom | 46b8a62 | 2012-06-19 23:13:22 -0700 | [diff] [blame] | 1192 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1193 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, |
| 1194 | &dex_location_checksum); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1195 | if (oat_dex_file == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1196 | *error_msg = StringPrintf("oat file '%s' does not contain contents for '%s' with checksum 0x%x", |
| 1197 | oat_file->GetLocation().c_str(), dex_location, dex_location_checksum); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1198 | for (const OatFile::OatDexFile* oat_dex_file_in : oat_file->GetOatDexFiles()) { |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1199 | *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] | 1200 | oat_file->GetLocation().c_str(), |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1201 | oat_dex_file_in->GetDexFileLocation().c_str(), |
| 1202 | oat_dex_file_in->GetDexFileLocationChecksum()); |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 1203 | } |
| 1204 | return false; |
| 1205 | } |
Brian Carlstrom | 46b8a62 | 2012-06-19 23:13:22 -0700 | [diff] [blame] | 1206 | |
Calin Juravle | a8c55ae | 2014-09-05 16:14:19 +0100 | [diff] [blame] | 1207 | DCHECK_EQ(dex_location_checksum, oat_dex_file->GetDexFileLocationChecksum()); |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1208 | return true; |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 1209 | } |
| 1210 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1211 | bool ClassLinker::VerifyOatWithDexFile(const OatFile* oat_file, |
| 1212 | const char* dex_location, |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 1213 | const uint32_t* dex_location_checksum, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1214 | std::string* error_msg) { |
| 1215 | CHECK(oat_file != nullptr); |
| 1216 | CHECK(dex_location != nullptr); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1217 | std::unique_ptr<const DexFile> dex_file; |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 1218 | if (dex_location_checksum == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1219 | // If no classes.dex found in dex_location, it has been stripped or is corrupt, assume oat is |
| 1220 | // up-to-date. This is the common case in user builds for jar's and apk's in the /system |
| 1221 | // directory. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1222 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, nullptr); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1223 | if (oat_dex_file == nullptr) { |
| 1224 | *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] | 1225 | "dex file '%s': %s", oat_file->GetLocation().c_str(), dex_location, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1226 | error_msg->c_str()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1227 | return false; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1228 | } |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 1229 | dex_file = oat_dex_file->OpenDexFile(error_msg); |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 1230 | } else { |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 1231 | bool verified = VerifyOatAndDexFileChecksums(oat_file, dex_location, *dex_location_checksum, |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1232 | kRuntimeISA, error_msg); |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 1233 | if (!verified) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1234 | return false; |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 1235 | } |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 1236 | dex_file = oat_file->GetOatDexFile(dex_location, |
| 1237 | dex_location_checksum)->OpenDexFile(error_msg); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1238 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1239 | return dex_file.get() != nullptr; |
Brian Carlstrom | 46b8a62 | 2012-06-19 23:13:22 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1242 | const OatFile* ClassLinker::FindOatFileContainingDexFileFromDexLocation( |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1243 | const char* dex_location, |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1244 | const uint32_t* dex_location_checksum, |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1245 | InstructionSet isa, |
Calin Juravle | c54aea7 | 2014-07-16 14:45:03 +0100 | [diff] [blame] | 1246 | std::vector<std::string>* error_msgs, |
| 1247 | bool* obsolete_file_cleanup_failed) { |
| 1248 | *obsolete_file_cleanup_failed = false; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1249 | bool already_opened = false; |
| 1250 | std::string dex_location_str(dex_location); |
| 1251 | std::unique_ptr<const OatFile> oat_file(OpenOatFileFromDexLocation(dex_location_str, isa, |
| 1252 | &already_opened, |
| 1253 | obsolete_file_cleanup_failed, |
| 1254 | error_msgs)); |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 1255 | std::string error_msg; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1256 | if (oat_file.get() == nullptr) { |
| 1257 | error_msgs->push_back(StringPrintf("Failed to open oat file from dex location '%s'", |
| 1258 | dex_location)); |
| 1259 | return nullptr; |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1260 | } else if (oat_file->IsExecutable() && |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 1261 | !VerifyOatWithDexFile(oat_file.get(), dex_location, |
| 1262 | dex_location_checksum, &error_msg)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1263 | error_msgs->push_back(StringPrintf("Failed to verify oat file '%s' found for dex location " |
| 1264 | "'%s': %s", oat_file->GetLocation().c_str(), dex_location, |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 1265 | error_msg.c_str())); |
Brian Carlstrom | 08cbf66 | 2013-12-10 16:52:57 -0800 | [diff] [blame] | 1266 | return nullptr; |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1267 | } else if (!oat_file->IsExecutable() && |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 1268 | Runtime::Current()->GetHeap()->HasImageSpace() && |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1269 | !VerifyOatImageChecksum(oat_file.get(), isa)) { |
| 1270 | error_msgs->push_back(StringPrintf("Failed to verify non-executable oat file '%s' found for " |
| 1271 | "dex location '%s'. Image checksum incorrect.", |
| 1272 | oat_file->GetLocation().c_str(), dex_location)); |
| 1273 | return nullptr; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1274 | } else { |
| 1275 | return oat_file.release(); |
Brian Carlstrom | 08cbf66 | 2013-12-10 16:52:57 -0800 | [diff] [blame] | 1276 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1279 | const OatFile* ClassLinker::FindOpenedOatFileFromOatLocation(const std::string& oat_location) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1280 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1281 | for (size_t i = 0; i < oat_files_.size(); i++) { |
| 1282 | const OatFile* oat_file = oat_files_[i]; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1283 | DCHECK(oat_file != nullptr); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1284 | if (oat_file->GetLocation() == oat_location) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1285 | return oat_file; |
| 1286 | } |
| 1287 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1288 | return nullptr; |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 1289 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1290 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1291 | const OatFile* ClassLinker::OpenOatFileFromDexLocation(const std::string& dex_location, |
| 1292 | InstructionSet isa, |
| 1293 | bool *already_opened, |
| 1294 | bool *obsolete_file_cleanup_failed, |
| 1295 | std::vector<std::string>* error_msgs) { |
| 1296 | // Find out if we've already opened the file |
| 1297 | const OatFile* ret = nullptr; |
| 1298 | std::string odex_filename(DexFilenameToOdexFilename(dex_location, isa)); |
| 1299 | ret = FindOpenedOatFileFromOatLocation(odex_filename); |
| 1300 | if (ret != nullptr) { |
| 1301 | *already_opened = true; |
| 1302 | return ret; |
| 1303 | } |
| 1304 | |
| 1305 | std::string dalvik_cache; |
| 1306 | bool have_android_data = false; |
| 1307 | bool have_dalvik_cache = false; |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 1308 | bool is_global_cache = false; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1309 | GetDalvikCache(GetInstructionSetString(kRuntimeISA), false, &dalvik_cache, |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 1310 | &have_android_data, &have_dalvik_cache, &is_global_cache); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1311 | std::string cache_filename; |
| 1312 | if (have_dalvik_cache) { |
| 1313 | cache_filename = GetDalvikCacheFilenameOrDie(dex_location.c_str(), dalvik_cache.c_str()); |
| 1314 | ret = FindOpenedOatFileFromOatLocation(cache_filename); |
| 1315 | if (ret != nullptr) { |
| 1316 | *already_opened = true; |
| 1317 | return ret; |
| 1318 | } |
| 1319 | } else { |
| 1320 | // If we need to relocate we should just place odex back where it started. |
| 1321 | cache_filename = odex_filename; |
| 1322 | } |
| 1323 | |
| 1324 | ret = nullptr; |
| 1325 | |
| 1326 | // We know that neither the odex nor the cache'd version is already in use, if it even exists. |
| 1327 | // |
| 1328 | // Now we do the following: |
| 1329 | // 1) Try and open the odex version |
| 1330 | // 2) If present, checksum-verified & relocated correctly return it |
| 1331 | // 3) Close the odex version to free up its address space. |
| 1332 | // 4) Try and open the cache version |
| 1333 | // 5) If present, checksum-verified & relocated correctly return it |
| 1334 | // 6) Close the cache version to free up its address space. |
| 1335 | // 7) If we should relocate: |
| 1336 | // a) If we have opened and checksum-verified the odex version relocate it to |
| 1337 | // 'cache_filename' and return it |
| 1338 | // b) If we have opened and checksum-verified the cache version relocate it in place and return |
| 1339 | // it. This should not happen often (I think only the run-test's will hit this case). |
| 1340 | // 8) If the cache-version was present we should delete it since it must be obsolete if we get to |
| 1341 | // this point. |
| 1342 | // 9) Return nullptr |
| 1343 | |
| 1344 | *already_opened = false; |
| 1345 | const Runtime* runtime = Runtime::Current(); |
| 1346 | CHECK(runtime != nullptr); |
| 1347 | bool executable = !runtime->IsCompiler(); |
| 1348 | |
| 1349 | std::string odex_error_msg; |
| 1350 | bool should_patch_system = false; |
| 1351 | bool odex_checksum_verified = false; |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 1352 | bool have_system_odex = false; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1353 | { |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1354 | // 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] | 1355 | // 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] | 1356 | 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] | 1357 | nullptr, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1358 | executable, &odex_error_msg)); |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1359 | 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] | 1360 | &odex_checksum_verified, |
| 1361 | &odex_error_msg)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1362 | return odex_oat_file.release(); |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 1363 | } else { |
| 1364 | if (odex_checksum_verified) { |
| 1365 | // We can just relocate |
| 1366 | should_patch_system = true; |
| 1367 | odex_error_msg = "Image Patches are incorrect"; |
| 1368 | } |
| 1369 | if (odex_oat_file.get() != nullptr) { |
| 1370 | have_system_odex = true; |
| 1371 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1372 | } |
| 1373 | } |
| 1374 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1375 | std::string cache_error_msg; |
| 1376 | bool should_patch_cache = false; |
| 1377 | bool cache_checksum_verified = false; |
| 1378 | if (have_dalvik_cache) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1379 | 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] | 1380 | nullptr, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1381 | executable, &cache_error_msg)); |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1382 | 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] | 1383 | &cache_checksum_verified, |
| 1384 | &cache_error_msg)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1385 | return cache_oat_file.release(); |
| 1386 | } else if (cache_checksum_verified) { |
| 1387 | // We can just relocate |
| 1388 | should_patch_cache = true; |
| 1389 | cache_error_msg = "Image Patches are incorrect"; |
| 1390 | } |
| 1391 | } else if (have_android_data) { |
| 1392 | // dalvik_cache does not exist but android data does. This means we should be able to create |
| 1393 | // it, so we should try. |
| 1394 | GetDalvikCacheOrDie(GetInstructionSetString(kRuntimeISA), true); |
| 1395 | } |
| 1396 | |
| 1397 | ret = nullptr; |
| 1398 | std::string error_msg; |
| 1399 | if (runtime->CanRelocate()) { |
| 1400 | // Run relocation |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1401 | gc::space::ImageSpace* space = Runtime::Current()->GetHeap()->GetImageSpace(); |
| 1402 | if (space != nullptr) { |
| 1403 | const std::string& image_location = space->GetImageLocation(); |
| 1404 | if (odex_checksum_verified && should_patch_system) { |
| 1405 | ret = PatchAndRetrieveOat(odex_filename, cache_filename, image_location, isa, &error_msg); |
| 1406 | } else if (cache_checksum_verified && should_patch_cache) { |
| 1407 | CHECK(have_dalvik_cache); |
| 1408 | ret = PatchAndRetrieveOat(cache_filename, cache_filename, image_location, isa, &error_msg); |
| 1409 | } |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 1410 | } else if (have_system_odex) { |
| 1411 | ret = GetInterpretedOnlyOat(odex_filename, isa, &error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1412 | } |
| 1413 | } |
| 1414 | if (ret == nullptr && have_dalvik_cache && OS::FileExists(cache_filename.c_str())) { |
| 1415 | // implicitly: were able to fine where the cached version is but we were unable to use it, |
| 1416 | // either as a destination for relocation or to open a file. We should delete it if it is |
| 1417 | // there. |
| 1418 | if (TEMP_FAILURE_RETRY(unlink(cache_filename.c_str())) != 0) { |
| 1419 | std::string rm_error_msg = StringPrintf("Failed to remove obsolete file from %s when " |
| 1420 | "searching for dex file %s: %s", |
| 1421 | cache_filename.c_str(), dex_location.c_str(), |
| 1422 | strerror(errno)); |
| 1423 | error_msgs->push_back(rm_error_msg); |
| 1424 | VLOG(class_linker) << rm_error_msg; |
| 1425 | // Let the caller know that we couldn't remove the obsolete file. |
| 1426 | // This is a good indication that further writes may fail as well. |
| 1427 | *obsolete_file_cleanup_failed = true; |
| 1428 | } |
| 1429 | } |
| 1430 | if (ret == nullptr) { |
| 1431 | VLOG(class_linker) << error_msg; |
| 1432 | error_msgs->push_back(error_msg); |
| 1433 | std::string relocation_msg; |
| 1434 | if (runtime->CanRelocate()) { |
| 1435 | relocation_msg = StringPrintf(" and relocation failed"); |
| 1436 | } |
| 1437 | if (have_dalvik_cache && cache_checksum_verified) { |
| 1438 | error_msg = StringPrintf("Failed to open oat file from %s (error %s) or %s " |
| 1439 | "(error %s)%s.", odex_filename.c_str(), odex_error_msg.c_str(), |
| 1440 | cache_filename.c_str(), cache_error_msg.c_str(), |
| 1441 | relocation_msg.c_str()); |
| 1442 | } else { |
| 1443 | error_msg = StringPrintf("Failed to open oat file from %s (error %s) (no " |
| 1444 | "dalvik_cache availible)%s.", odex_filename.c_str(), |
| 1445 | odex_error_msg.c_str(), relocation_msg.c_str()); |
| 1446 | } |
| 1447 | VLOG(class_linker) << error_msg; |
| 1448 | error_msgs->push_back(error_msg); |
| 1449 | } |
| 1450 | return ret; |
| 1451 | } |
| 1452 | |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1453 | const OatFile* ClassLinker::GetInterpretedOnlyOat(const std::string& oat_path, |
| 1454 | InstructionSet isa, |
| 1455 | std::string* error_msg) { |
| 1456 | // We open it non-executable |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1457 | 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] | 1458 | if (output.get() == nullptr) { |
| 1459 | return nullptr; |
| 1460 | } |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 1461 | if (!Runtime::Current()->GetHeap()->HasImageSpace() || |
| 1462 | VerifyOatImageChecksum(output.get(), isa)) { |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1463 | return output.release(); |
| 1464 | } else { |
| 1465 | *error_msg = StringPrintf("Could not use oat file '%s', image checksum failed to verify.", |
| 1466 | oat_path.c_str()); |
| 1467 | return nullptr; |
| 1468 | } |
| 1469 | } |
| 1470 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1471 | const OatFile* ClassLinker::PatchAndRetrieveOat(const std::string& input_oat, |
| 1472 | const std::string& output_oat, |
| 1473 | const std::string& image_location, |
| 1474 | InstructionSet isa, |
| 1475 | std::string* error_msg) { |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1476 | Runtime* runtime = Runtime::Current(); |
| 1477 | DCHECK(runtime != nullptr); |
| 1478 | if (!runtime->GetHeap()->HasImageSpace()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1479 | // We don't have an image space so there is no point in trying to patchoat. |
| 1480 | LOG(WARNING) << "Patching of oat file '" << input_oat << "' not attempted because we are " |
| 1481 | << "running without an image. Attempting to use oat file for interpretation."; |
| 1482 | return GetInterpretedOnlyOat(input_oat, isa, error_msg); |
| 1483 | } |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1484 | if (!runtime->IsDex2OatEnabled()) { |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1485 | // We don't have dex2oat so we can assume we don't have patchoat either. We should just use the |
| 1486 | // input_oat but make sure we only do interpretation on it's dex files. |
| 1487 | LOG(WARNING) << "Patching of oat file '" << input_oat << "' not attempted due to dex2oat being " |
| 1488 | << "disabled. Attempting to use oat file for interpretation"; |
| 1489 | return GetInterpretedOnlyOat(input_oat, isa, error_msg); |
| 1490 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1491 | Locks::mutator_lock_->AssertNotHeld(Thread::Current()); // Avoid starving GC. |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1492 | std::string patchoat(runtime->GetPatchoatExecutable()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1493 | |
| 1494 | std::string isa_arg("--instruction-set="); |
| 1495 | isa_arg += GetInstructionSetString(isa); |
| 1496 | std::string input_oat_filename_arg("--input-oat-file="); |
| 1497 | input_oat_filename_arg += input_oat; |
| 1498 | std::string output_oat_filename_arg("--output-oat-file="); |
| 1499 | output_oat_filename_arg += output_oat; |
| 1500 | std::string patched_image_arg("--patched-image-location="); |
| 1501 | patched_image_arg += image_location; |
| 1502 | |
| 1503 | std::vector<std::string> argv; |
| 1504 | argv.push_back(patchoat); |
| 1505 | argv.push_back(isa_arg); |
| 1506 | argv.push_back(input_oat_filename_arg); |
| 1507 | argv.push_back(output_oat_filename_arg); |
| 1508 | argv.push_back(patched_image_arg); |
| 1509 | |
| 1510 | std::string command_line(Join(argv, ' ')); |
| 1511 | LOG(INFO) << "Relocate Oat File: " << command_line; |
| 1512 | bool success = Exec(argv, error_msg); |
| 1513 | if (success) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1514 | 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] | 1515 | !runtime->IsCompiler(), error_msg)); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1516 | bool checksum_verified = false; |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1517 | if (output.get() != nullptr && CheckOatFile(runtime, output.get(), isa, &checksum_verified, |
| 1518 | error_msg)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1519 | return output.release(); |
| 1520 | } else if (output.get() != nullptr) { |
| 1521 | *error_msg = StringPrintf("Patching of oat file '%s' succeeded " |
| 1522 | "but output file '%s' failed verifcation: %s", |
| 1523 | input_oat.c_str(), output_oat.c_str(), error_msg->c_str()); |
| 1524 | } else { |
| 1525 | *error_msg = StringPrintf("Patching of oat file '%s' succeeded " |
| 1526 | "but was unable to open output file '%s': %s", |
| 1527 | input_oat.c_str(), output_oat.c_str(), error_msg->c_str()); |
| 1528 | } |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1529 | } else if (!runtime->IsCompiler()) { |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1530 | // patchoat failed which means we probably don't have enough room to place the output oat file, |
| 1531 | // instead of failing we should just run the interpreter from the dex files in the input oat. |
| 1532 | LOG(WARNING) << "Patching of oat file '" << input_oat << "' failed. Attempting to use oat file " |
| 1533 | << "for interpretation. patchoat failure was: " << *error_msg; |
| 1534 | return GetInterpretedOnlyOat(input_oat, isa, error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1535 | } else { |
| 1536 | *error_msg = StringPrintf("Patching of oat file '%s to '%s' " |
| 1537 | "failed: %s", input_oat.c_str(), output_oat.c_str(), |
| 1538 | error_msg->c_str()); |
| 1539 | } |
| 1540 | return nullptr; |
| 1541 | } |
| 1542 | |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1543 | bool ClassLinker::CheckOatFile(const Runtime* runtime, const OatFile* oat_file, InstructionSet isa, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1544 | bool* checksum_verified, |
| 1545 | std::string* error_msg) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1546 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1547 | if (image_space == nullptr) { |
| 1548 | *error_msg = "No image space present"; |
| 1549 | return false; |
| 1550 | } |
Vladimir Marko | 30c2e1b | 2014-09-08 09:55:29 +0100 | [diff] [blame] | 1551 | uint32_t real_image_checksum; |
| 1552 | void* real_image_oat_offset; |
| 1553 | int32_t real_patch_delta; |
| 1554 | if (isa == runtime->GetInstructionSet()) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1555 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1556 | real_image_checksum = image_header.GetOatChecksum(); |
| 1557 | real_image_oat_offset = image_header.GetOatDataBegin(); |
| 1558 | real_patch_delta = image_header.GetPatchDelta(); |
| 1559 | } else { |
| 1560 | std::unique_ptr<ImageHeader> image_header(gc::space::ImageSpace::ReadImageHeaderOrDie( |
| 1561 | image_space->GetImageLocation().c_str(), isa)); |
| 1562 | real_image_checksum = image_header->GetOatChecksum(); |
| 1563 | real_image_oat_offset = image_header->GetOatDataBegin(); |
| 1564 | real_patch_delta = image_header->GetPatchDelta(); |
| 1565 | } |
| 1566 | |
| 1567 | const OatHeader& oat_header = oat_file->GetOatHeader(); |
Vladimir Marko | 30c2e1b | 2014-09-08 09:55:29 +0100 | [diff] [blame] | 1568 | std::string compound_msg; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1569 | |
| 1570 | uint32_t oat_image_checksum = oat_header.GetImageFileLocationOatChecksum(); |
| 1571 | *checksum_verified = oat_image_checksum == real_image_checksum; |
| 1572 | if (!*checksum_verified) { |
Vladimir Marko | 30c2e1b | 2014-09-08 09:55:29 +0100 | [diff] [blame] | 1573 | StringAppendF(&compound_msg, " Oat Image Checksum Incorrect (expected 0x%x, received 0x%x)", |
| 1574 | real_image_checksum, oat_image_checksum); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1575 | } |
| 1576 | |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1577 | bool offset_verified; |
| 1578 | bool patch_delta_verified; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1579 | |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1580 | if (!oat_file->IsPic()) { |
| 1581 | // If an oat file is not PIC, we need to check that the image is at the expected location and |
| 1582 | // patched in the same way. |
| 1583 | void* oat_image_oat_offset = |
| 1584 | reinterpret_cast<void*>(oat_header.GetImageFileLocationOatDataBegin()); |
| 1585 | offset_verified = oat_image_oat_offset == real_image_oat_offset; |
| 1586 | if (!offset_verified) { |
| 1587 | StringAppendF(&compound_msg, " Oat Image oat offset incorrect (expected 0x%p, received 0x%p)", |
| 1588 | real_image_oat_offset, oat_image_oat_offset); |
| 1589 | } |
| 1590 | |
| 1591 | int32_t oat_patch_delta = oat_header.GetImagePatchDelta(); |
| 1592 | patch_delta_verified = oat_patch_delta == real_patch_delta; |
| 1593 | if (!patch_delta_verified) { |
| 1594 | StringAppendF(&compound_msg, " Oat image patch delta incorrect (expected 0x%x, " |
| 1595 | "received 0x%x)", real_patch_delta, oat_patch_delta); |
| 1596 | } |
| 1597 | } else { |
| 1598 | // If an oat file is PIC, we ignore offset and patching delta. |
| 1599 | offset_verified = true; |
| 1600 | patch_delta_verified = true; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | bool ret = (*checksum_verified && offset_verified && patch_delta_verified); |
Vladimir Marko | 30c2e1b | 2014-09-08 09:55:29 +0100 | [diff] [blame] | 1604 | if (!ret) { |
| 1605 | *error_msg = "Oat file failed to verify:" + compound_msg; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1606 | } |
| 1607 | return ret; |
| 1608 | } |
| 1609 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1610 | const OatFile* ClassLinker::FindOatFileFromOatLocation(const std::string& oat_location, |
| 1611 | std::string* error_msg) { |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 1612 | const OatFile* oat_file = FindOpenedOatFileFromOatLocation(oat_location); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1613 | if (oat_file != nullptr) { |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 1614 | return oat_file; |
| 1615 | } |
| 1616 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1617 | return OatFile::Open(oat_location, oat_location, nullptr, nullptr, !Runtime::Current()->IsCompiler(), |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1618 | error_msg); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1619 | } |
| 1620 | |
Mathieu Chartier | a89d7ed | 2014-12-05 10:57:13 -0800 | [diff] [blame] | 1621 | void ClassLinker::InitFromImageInterpretOnlyCallback(mirror::Object* obj, void* arg) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1622 | ClassLinker* class_linker = reinterpret_cast<ClassLinker*>(arg); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1623 | DCHECK(obj != nullptr); |
| 1624 | DCHECK(class_linker != nullptr); |
Mathieu Chartier | a89d7ed | 2014-12-05 10:57:13 -0800 | [diff] [blame] | 1625 | size_t pointer_size = class_linker->image_pointer_size_; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1626 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1627 | if (obj->IsArtMethod()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1628 | mirror::ArtMethod* method = obj->AsArtMethod(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1629 | if (!method->IsNative()) { |
Mathieu Chartier | a89d7ed | 2014-12-05 10:57:13 -0800 | [diff] [blame] | 1630 | method->SetEntryPointFromInterpreterPtrSize(artInterpreterToInterpreterBridge, pointer_size); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1631 | if (method != Runtime::Current()->GetResolutionMethod()) { |
Mathieu Chartier | a89d7ed | 2014-12-05 10:57:13 -0800 | [diff] [blame] | 1632 | method->SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(), |
| 1633 | pointer_size); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1634 | } |
| 1635 | } |
| 1636 | } |
| 1637 | } |
| 1638 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1639 | void ClassLinker::InitFromImage() { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1640 | VLOG(startup) << "ClassLinker::InitFromImage entering"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1641 | CHECK(!init_done_); |
| 1642 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1643 | Thread* self = Thread::Current(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1644 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 1645 | gc::space::ImageSpace* space = heap->GetImageSpace(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1646 | dex_cache_image_class_lookup_required_ = true; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1647 | CHECK(space != nullptr); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1648 | OatFile& oat_file = GetImageOatFile(space); |
| 1649 | CHECK_EQ(oat_file.GetOatHeader().GetImageFileLocationOatChecksum(), 0U); |
| 1650 | CHECK_EQ(oat_file.GetOatHeader().GetImageFileLocationOatDataBegin(), 0U); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1651 | const char* image_file_location = oat_file.GetOatHeader(). |
| 1652 | GetStoreValueByKey(OatHeader::kImageLocationKey); |
| 1653 | CHECK(image_file_location == nullptr || *image_file_location == 0); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1654 | quick_resolution_trampoline_ = oat_file.GetOatHeader().GetQuickResolutionTrampoline(); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1655 | quick_imt_conflict_trampoline_ = oat_file.GetOatHeader().GetQuickImtConflictTrampoline(); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1656 | quick_generic_jni_trampoline_ = oat_file.GetOatHeader().GetQuickGenericJniTrampoline(); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 1657 | quick_to_interpreter_bridge_trampoline_ = oat_file.GetOatHeader().GetQuickToInterpreterBridge(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1658 | mirror::Object* dex_caches_object = space->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches); |
| 1659 | mirror::ObjectArray<mirror::DexCache>* dex_caches = |
| 1660 | dex_caches_object->AsObjectArray<mirror::DexCache>(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1661 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1662 | StackHandleScope<1> hs(self); |
| 1663 | Handle<mirror::ObjectArray<mirror::Class>> class_roots(hs.NewHandle( |
| 1664 | space->GetImageHeader().GetImageRoot(ImageHeader::kClassRoots)-> |
| 1665 | AsObjectArray<mirror::Class>())); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1666 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>(class_roots.Get()); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1667 | |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1668 | // Special case of setting up the String class early so that we can test arbitrary objects |
| 1669 | // as being Strings or not |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 1670 | mirror::String::SetClass(GetClassRoot(kJavaLangString)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1671 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1672 | CHECK_EQ(oat_file.GetOatHeader().GetDexFileCount(), |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1673 | static_cast<uint32_t>(dex_caches->GetLength())); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1674 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1675 | StackHandleScope<1> hs2(self); |
| 1676 | Handle<mirror::DexCache> dex_cache(hs2.NewHandle(dex_caches->Get(i))); |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1677 | const std::string& dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1678 | const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(dex_file_location.c_str(), |
| 1679 | nullptr); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1680 | CHECK(oat_dex_file != nullptr) << oat_file.GetLocation() << " " << dex_file_location; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1681 | std::string error_msg; |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 1682 | std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg); |
| 1683 | if (dex_file.get() == nullptr) { |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1684 | LOG(FATAL) << "Failed to open dex file " << dex_file_location |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1685 | << " from within oat file " << oat_file.GetLocation() |
| 1686 | << " error '" << error_msg << "'"; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1687 | UNREACHABLE(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1688 | } |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1689 | |
| 1690 | CHECK_EQ(dex_file->GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum()); |
| 1691 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 1692 | AppendToBootClassPath(*dex_file.get(), dex_cache); |
| 1693 | opened_dex_files_.push_back(std::move(dex_file)); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1694 | } |
| 1695 | |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 1696 | // Set classes on AbstractMethod early so that IsMethod tests can be performed during the live |
| 1697 | // bitmap walk. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1698 | mirror::ArtMethod::SetClass(GetClassRoot(kJavaLangReflectArtMethod)); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 1699 | size_t art_method_object_size = mirror::ArtMethod::GetJavaLangReflectArtMethod()->GetObjectSize(); |
| 1700 | if (!Runtime::Current()->IsCompiler()) { |
| 1701 | // Compiler supports having an image with a different pointer size than the runtime. This |
| 1702 | // happens on the host for compile 32 bit tests since we use a 64 bit libart compiler. We may |
| 1703 | // also use 32 bit dex2oat on a system with 64 bit apps. |
| 1704 | CHECK_EQ(art_method_object_size, mirror::ArtMethod::InstanceSize(sizeof(void*))) |
| 1705 | << sizeof(void*); |
| 1706 | } |
| 1707 | if (art_method_object_size == mirror::ArtMethod::InstanceSize(4)) { |
| 1708 | image_pointer_size_ = 4; |
| 1709 | } else { |
| 1710 | CHECK_EQ(art_method_object_size, mirror::ArtMethod::InstanceSize(8)); |
| 1711 | image_pointer_size_ = 8; |
| 1712 | } |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 1713 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1714 | // Set entry point to interpreter if in InterpretOnly mode. |
Mathieu Chartier | a89d7ed | 2014-12-05 10:57:13 -0800 | [diff] [blame] | 1715 | Runtime* runtime = Runtime::Current(); |
| 1716 | if (!runtime->IsCompiler() && runtime->GetInstrumentation()->InterpretOnly()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1717 | heap->VisitObjects(InitFromImageInterpretOnlyCallback, this); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1718 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1719 | |
| 1720 | // reinit class_roots_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1721 | mirror::Class::SetClassClass(class_roots->Get(kJavaLangClass)); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1722 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>(class_roots.Get()); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1723 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1724 | // reinit array_iftable_ from any array class instance, they should be == |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1725 | array_iftable_ = GcRoot<mirror::IfTable>(GetClassRoot(kObjectArrayClass)->GetIfTable()); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 1726 | DCHECK_EQ(array_iftable_.Read(), GetClassRoot(kBooleanArrayClass)->GetIfTable()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1727 | // String class root was set above |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 1728 | mirror::Reference::SetClass(GetClassRoot(kJavaLangRefReference)); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1729 | mirror::ArtField::SetClass(GetClassRoot(kJavaLangReflectArtField)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1730 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
| 1731 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
| 1732 | mirror::CharArray::SetArrayClass(GetClassRoot(kCharArrayClass)); |
| 1733 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
| 1734 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
| 1735 | mirror::IntArray::SetArrayClass(GetClassRoot(kIntArrayClass)); |
| 1736 | mirror::LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
| 1737 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
| 1738 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
| 1739 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1740 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1741 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1742 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1743 | VLOG(startup) << "ClassLinker::InitFromImage exiting"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1744 | } |
| 1745 | |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1746 | void ClassLinker::VisitClassRoots(RootCallback* callback, void* arg, VisitRootFlags flags) { |
| 1747 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 1748 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1749 | for (GcRoot<mirror::Class>& root : class_table_) { |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 1750 | root.VisitRoot(callback, arg, RootInfo(kRootStickyClass)); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1751 | } |
| 1752 | for (GcRoot<mirror::Class>& root : pre_zygote_class_table_) { |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 1753 | root.VisitRoot(callback, arg, RootInfo(kRootStickyClass)); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1754 | } |
| 1755 | } else if ((flags & kVisitRootFlagNewRoots) != 0) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1756 | for (auto& root : new_class_roots_) { |
| 1757 | mirror::Class* old_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 1758 | root.VisitRoot(callback, arg, RootInfo(kRootStickyClass)); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1759 | mirror::Class* new_ref = root.Read<kWithoutReadBarrier>(); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1760 | if (UNLIKELY(new_ref != old_ref)) { |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1761 | // Uh ohes, GC moved a root in the log. Need to search the class_table and update the |
| 1762 | // corresponding object. This is slow, but luckily for us, this may only happen with a |
| 1763 | // concurrent moving GC. |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1764 | auto it = class_table_.Find(GcRoot<mirror::Class>(old_ref)); |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 1765 | DCHECK(it != class_table_.end()); |
| 1766 | *it = GcRoot<mirror::Class>(new_ref); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1767 | } |
| 1768 | } |
| 1769 | } |
| 1770 | if ((flags & kVisitRootFlagClearRootLog) != 0) { |
| 1771 | new_class_roots_.clear(); |
| 1772 | } |
| 1773 | if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
| 1774 | log_new_class_table_roots_ = true; |
| 1775 | } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
| 1776 | log_new_class_table_roots_ = false; |
| 1777 | } |
| 1778 | // We deliberately ignore the class roots in the image since we |
| 1779 | // handle image roots by using the MS/CMS rescanning of dirty cards. |
| 1780 | } |
| 1781 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1782 | // Keep in sync with InitCallback. Anything we visit, we need to |
| 1783 | // reinit references to when reinitializing a ClassLinker from a |
| 1784 | // mapped image. |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 1785 | void ClassLinker::VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags) { |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 1786 | class_roots_.VisitRoot(callback, arg, RootInfo(kRootVMInternal)); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1787 | Thread* self = Thread::Current(); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1788 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 1789 | ReaderMutexLock mu(self, dex_lock_); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 1790 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1791 | for (GcRoot<mirror::DexCache>& dex_cache : dex_caches_) { |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 1792 | dex_cache.VisitRoot(callback, arg, RootInfo(kRootVMInternal)); |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 1793 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 1794 | } else if ((flags & kVisitRootFlagNewRoots) != 0) { |
| 1795 | for (size_t index : new_dex_cache_roots_) { |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 1796 | dex_caches_[index].VisitRoot(callback, arg, RootInfo(kRootVMInternal)); |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 1797 | } |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1798 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 1799 | if ((flags & kVisitRootFlagClearRootLog) != 0) { |
| 1800 | new_dex_cache_roots_.clear(); |
| 1801 | } |
| 1802 | if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
| 1803 | log_new_dex_caches_roots_ = true; |
| 1804 | } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
| 1805 | log_new_dex_caches_roots_ = false; |
| 1806 | } |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1807 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1808 | VisitClassRoots(callback, arg, flags); |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 1809 | array_iftable_.VisitRoot(callback, arg, RootInfo(kRootVMInternal)); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1810 | DCHECK(!array_iftable_.IsNull()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1811 | for (size_t i = 0; i < kFindArrayCacheSize; ++i) { |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 1812 | find_array_class_cache_[i].VisitRootIfNonNull(callback, arg, RootInfo(kRootVMInternal)); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1813 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1814 | } |
| 1815 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1816 | void ClassLinker::VisitClasses(ClassVisitor* visitor, void* arg) { |
| 1817 | if (dex_cache_image_class_lookup_required_) { |
| 1818 | MoveImageClassesToClassTable(); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1819 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1820 | // TODO: why isn't this a ReaderMutexLock? |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 1821 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1822 | for (GcRoot<mirror::Class>& root : class_table_) { |
| 1823 | if (!visitor(root.Read(), arg)) { |
| 1824 | return; |
| 1825 | } |
| 1826 | } |
| 1827 | for (GcRoot<mirror::Class>& root : pre_zygote_class_table_) { |
| 1828 | if (!visitor(root.Read(), arg)) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1829 | return; |
| 1830 | } |
| 1831 | } |
| 1832 | } |
| 1833 | |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1834 | static bool GetClassesVisitorSet(mirror::Class* c, void* arg) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1835 | std::set<mirror::Class*>* classes = reinterpret_cast<std::set<mirror::Class*>*>(arg); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1836 | classes->insert(c); |
| 1837 | return true; |
| 1838 | } |
| 1839 | |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1840 | struct GetClassesVisitorArrayArg { |
| 1841 | Handle<mirror::ObjectArray<mirror::Class>>* classes; |
| 1842 | int32_t index; |
| 1843 | bool success; |
| 1844 | }; |
| 1845 | |
| 1846 | static bool GetClassesVisitorArray(mirror::Class* c, void* varg) |
| 1847 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1848 | GetClassesVisitorArrayArg* arg = reinterpret_cast<GetClassesVisitorArrayArg*>(varg); |
| 1849 | if (arg->index < (*arg->classes)->GetLength()) { |
| 1850 | (*arg->classes)->Set(arg->index, c); |
| 1851 | arg->index++; |
| 1852 | return true; |
| 1853 | } else { |
| 1854 | arg->success = false; |
| 1855 | return false; |
| 1856 | } |
| 1857 | } |
| 1858 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1859 | void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor, void* arg) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1860 | // TODO: it may be possible to avoid secondary storage if we iterate over dex caches. The problem |
| 1861 | // is avoiding duplicates. |
| 1862 | if (!kMovingClasses) { |
| 1863 | std::set<mirror::Class*> classes; |
| 1864 | VisitClasses(GetClassesVisitorSet, &classes); |
| 1865 | for (mirror::Class* klass : classes) { |
| 1866 | if (!visitor(klass, arg)) { |
| 1867 | return; |
| 1868 | } |
| 1869 | } |
| 1870 | } else { |
| 1871 | Thread* self = Thread::Current(); |
| 1872 | StackHandleScope<1> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 1873 | MutableHandle<mirror::ObjectArray<mirror::Class>> classes = |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1874 | hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
| 1875 | GetClassesVisitorArrayArg local_arg; |
| 1876 | local_arg.classes = &classes; |
| 1877 | local_arg.success = false; |
| 1878 | // We size the array assuming classes won't be added to the class table during the visit. |
| 1879 | // If this assumption fails we iterate again. |
| 1880 | while (!local_arg.success) { |
| 1881 | size_t class_table_size; |
| 1882 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1883 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1884 | class_table_size = class_table_.Size() + pre_zygote_class_table_.Size(); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1885 | } |
| 1886 | mirror::Class* class_type = mirror::Class::GetJavaLangClass(); |
| 1887 | mirror::Class* array_of_class = FindArrayClass(self, &class_type); |
| 1888 | classes.Assign( |
| 1889 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, class_table_size)); |
| 1890 | CHECK(classes.Get() != nullptr); // OOME. |
| 1891 | local_arg.index = 0; |
| 1892 | local_arg.success = true; |
| 1893 | VisitClasses(GetClassesVisitorArray, &local_arg); |
| 1894 | } |
| 1895 | for (int32_t i = 0; i < classes->GetLength(); ++i) { |
| 1896 | // If the class table shrank during creation of the clases array we expect null elements. If |
| 1897 | // the class table grew then the loop repeats. If classes are created after the loop has |
| 1898 | // finished then we don't visit. |
| 1899 | mirror::Class* klass = classes->Get(i); |
| 1900 | if (klass != nullptr && !visitor(klass, arg)) { |
| 1901 | return; |
| 1902 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1903 | } |
| 1904 | } |
| 1905 | } |
| 1906 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1907 | ClassLinker::~ClassLinker() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1908 | mirror::Class::ResetClass(); |
| 1909 | mirror::String::ResetClass(); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 1910 | mirror::Reference::ResetClass(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1911 | mirror::ArtField::ResetClass(); |
| 1912 | mirror::ArtMethod::ResetClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1913 | mirror::BooleanArray::ResetArrayClass(); |
| 1914 | mirror::ByteArray::ResetArrayClass(); |
| 1915 | mirror::CharArray::ResetArrayClass(); |
| 1916 | mirror::DoubleArray::ResetArrayClass(); |
| 1917 | mirror::FloatArray::ResetArrayClass(); |
| 1918 | mirror::IntArray::ResetArrayClass(); |
| 1919 | mirror::LongArray::ResetArrayClass(); |
| 1920 | mirror::ShortArray::ResetArrayClass(); |
| 1921 | mirror::Throwable::ResetClass(); |
| 1922 | mirror::StackTraceElement::ResetClass(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1923 | STLDeleteElements(&oat_files_); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1924 | } |
| 1925 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1926 | mirror::DexCache* ClassLinker::AllocDexCache(Thread* self, const DexFile& dex_file) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1927 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1928 | StackHandleScope<16> hs(self); |
| 1929 | Handle<mirror::Class> dex_cache_class(hs.NewHandle(GetClassRoot(kJavaLangDexCache))); |
| 1930 | Handle<mirror::DexCache> dex_cache( |
| 1931 | hs.NewHandle(down_cast<mirror::DexCache*>( |
| 1932 | heap->AllocObject<true>(self, dex_cache_class.Get(), dex_cache_class->GetObjectSize(), |
| 1933 | VoidFunctor())))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1934 | if (dex_cache.Get() == nullptr) { |
| 1935 | return nullptr; |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1936 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1937 | Handle<mirror::String> |
| 1938 | location(hs.NewHandle(intern_table_->InternStrong(dex_file.GetLocation().c_str()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1939 | if (location.Get() == nullptr) { |
| 1940 | return nullptr; |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1941 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1942 | Handle<mirror::ObjectArray<mirror::String>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1943 | strings(hs.NewHandle(AllocStringArray(self, dex_file.NumStringIds()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1944 | if (strings.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::Class>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1948 | types(hs.NewHandle(AllocClassArray(self, dex_file.NumTypeIds()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1949 | if (types.Get() == nullptr) { |
| 1950 | return nullptr; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1951 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1952 | Handle<mirror::ObjectArray<mirror::ArtMethod>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1953 | methods(hs.NewHandle(AllocArtMethodArray(self, dex_file.NumMethodIds()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1954 | if (methods.Get() == nullptr) { |
| 1955 | return nullptr; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1956 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1957 | Handle<mirror::ObjectArray<mirror::ArtField>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1958 | fields(hs.NewHandle(AllocArtFieldArray(self, dex_file.NumFieldIds()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1959 | if (fields.Get() == nullptr) { |
| 1960 | return nullptr; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1961 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1962 | dex_cache->Init(&dex_file, location.Get(), strings.Get(), types.Get(), methods.Get(), |
| 1963 | fields.Get()); |
| 1964 | return dex_cache.Get(); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1965 | } |
| 1966 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1967 | mirror::Class* ClassLinker::AllocClass(Thread* self, mirror::Class* java_lang_Class, |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1968 | uint32_t class_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1969 | DCHECK_GE(class_size, sizeof(mirror::Class)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1970 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1971 | mirror::Class::InitializeClassVisitor visitor(class_size); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1972 | mirror::Object* k = kMovingClasses ? |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1973 | heap->AllocObject<true>(self, java_lang_Class, class_size, visitor) : |
| 1974 | heap->AllocNonMovableObject<true>(self, java_lang_Class, class_size, visitor); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1975 | if (UNLIKELY(k == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1976 | CHECK(self->IsExceptionPending()); // OOME. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1977 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1978 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1979 | return k->AsClass(); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1980 | } |
| 1981 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1982 | mirror::Class* ClassLinker::AllocClass(Thread* self, uint32_t class_size) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1983 | return AllocClass(self, GetClassRoot(kJavaLangClass), class_size); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1984 | } |
| 1985 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1986 | mirror::ArtField* ClassLinker::AllocArtField(Thread* self) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1987 | return down_cast<mirror::ArtField*>( |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1988 | GetClassRoot(kJavaLangReflectArtField)->AllocNonMovableObject(self)); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1989 | } |
| 1990 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1991 | mirror::ArtMethod* ClassLinker::AllocArtMethod(Thread* self) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1992 | return down_cast<mirror::ArtMethod*>( |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1993 | GetClassRoot(kJavaLangReflectArtMethod)->AllocNonMovableObject(self)); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 1994 | } |
| 1995 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1996 | mirror::ObjectArray<mirror::StackTraceElement>* ClassLinker::AllocStackTraceElementArray( |
| 1997 | Thread* self, size_t length) { |
| 1998 | return mirror::ObjectArray<mirror::StackTraceElement>::Alloc( |
| 1999 | self, GetClassRoot(kJavaLangStackTraceElementArrayClass), length); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2000 | } |
| 2001 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2002 | mirror::Class* ClassLinker::EnsureResolved(Thread* self, const char* descriptor, |
| 2003 | mirror::Class* klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2004 | DCHECK(klass != nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2005 | |
| 2006 | // For temporary classes we must wait for them to be retired. |
| 2007 | if (init_done_ && klass->IsTemp()) { |
| 2008 | CHECK(!klass->IsResolved()); |
| 2009 | if (klass->IsErroneous()) { |
| 2010 | ThrowEarlierClassFailure(klass); |
| 2011 | return nullptr; |
| 2012 | } |
| 2013 | StackHandleScope<1> hs(self); |
| 2014 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 2015 | ObjectLock<mirror::Class> lock(self, h_class); |
| 2016 | // Loop and wait for the resolving thread to retire this class. |
| 2017 | while (!h_class->IsRetired() && !h_class->IsErroneous()) { |
| 2018 | lock.WaitIgnoringInterrupts(); |
| 2019 | } |
| 2020 | if (h_class->IsErroneous()) { |
| 2021 | ThrowEarlierClassFailure(h_class.Get()); |
| 2022 | return nullptr; |
| 2023 | } |
| 2024 | CHECK(h_class->IsRetired()); |
| 2025 | // Get the updated class from class table. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2026 | klass = LookupClass(self, descriptor, ComputeModifiedUtf8Hash(descriptor), |
| 2027 | h_class.Get()->GetClassLoader()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2028 | } |
| 2029 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2030 | // Wait for the class if it has not already been linked. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 2031 | if (!klass->IsResolved() && !klass->IsErroneous()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2032 | StackHandleScope<1> hs(self); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 2033 | HandleWrapper<mirror::Class> h_class(hs.NewHandleWrapper(&klass)); |
| 2034 | ObjectLock<mirror::Class> lock(self, h_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2035 | // Check for circular dependencies between classes. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2036 | if (!h_class->IsResolved() && h_class->GetClinitThreadId() == self->GetTid()) { |
| 2037 | ThrowClassCircularityError(h_class.Get()); |
| 2038 | h_class->SetStatus(mirror::Class::kStatusError, self); |
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 | // Wait for the pending initialization to complete. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2042 | while (!h_class->IsResolved() && !h_class->IsErroneous()) { |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 2043 | lock.WaitIgnoringInterrupts(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2044 | } |
| 2045 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2046 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2047 | if (klass->IsErroneous()) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2048 | ThrowEarlierClassFailure(klass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2049 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2050 | } |
| 2051 | // Return the loaded class. No exceptions should be pending. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2052 | CHECK(klass->IsResolved()) << PrettyClass(klass); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2053 | self->AssertNoPendingException(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2054 | return klass; |
| 2055 | } |
| 2056 | |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2057 | typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry; |
| 2058 | |
| 2059 | // Search a collection of DexFiles for a descriptor |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2060 | ClassPathEntry FindInClassPath(const char* descriptor, |
| 2061 | size_t hash, const std::vector<const DexFile*>& class_path) { |
| 2062 | for (const DexFile* dex_file : class_path) { |
| 2063 | const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2064 | if (dex_class_def != nullptr) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2065 | return ClassPathEntry(dex_file, dex_class_def); |
| 2066 | } |
| 2067 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2068 | return ClassPathEntry(nullptr, nullptr); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2069 | } |
| 2070 | |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2071 | mirror::Class* ClassLinker::FindClassInPathClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
| 2072 | Thread* self, const char* descriptor, |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2073 | size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2074 | Handle<mirror::ClassLoader> class_loader) { |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2075 | // 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] | 2076 | if (class_loader->GetClass() != |
| 2077 | soa.Decode<mirror::Class*>(WellKnownClasses::dalvik_system_PathClassLoader) || |
| 2078 | class_loader->GetParent()->GetClass() != |
| 2079 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_BootClassLoader)) { |
| 2080 | return nullptr; |
| 2081 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2082 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2083 | // Check if this would be found in the parent boot class loader. |
| 2084 | if (pair.second != nullptr) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2085 | mirror::Class* klass = LookupClass(self, descriptor, hash, nullptr); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2086 | if (klass != nullptr) { |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2087 | // May return null if resolution on another thread fails. |
| 2088 | klass = EnsureResolved(self, descriptor, klass); |
| 2089 | } else { |
| 2090 | // May OOME. |
| 2091 | klass = DefineClass(self, descriptor, hash, NullHandle<mirror::ClassLoader>(), *pair.first, |
| 2092 | *pair.second); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2093 | } |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2094 | if (klass == nullptr) { |
| 2095 | CHECK(self->IsExceptionPending()) << descriptor; |
| 2096 | self->ClearException(); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2097 | } |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2098 | return klass; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2099 | } else { |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2100 | // Handle as if this is the child PathClassLoader. |
| 2101 | // Handles as RegisterDexFile may allocate dex caches (and cause thread suspension). |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2102 | StackHandleScope<3> hs(self); |
| 2103 | // The class loader is a PathClassLoader which inherits from BaseDexClassLoader. |
| 2104 | // We need to get the DexPathList and loop through it. |
| 2105 | Handle<mirror::ArtField> cookie_field = |
| 2106 | hs.NewHandle(soa.DecodeField(WellKnownClasses::dalvik_system_DexFile_cookie)); |
| 2107 | Handle<mirror::ArtField> dex_file_field = |
| 2108 | hs.NewHandle( |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 2109 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile)); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2110 | mirror::Object* dex_path_list = |
| 2111 | soa.DecodeField(WellKnownClasses::dalvik_system_PathClassLoader_pathList)-> |
| 2112 | GetObject(class_loader.Get()); |
| 2113 | if (dex_path_list != nullptr && dex_file_field.Get() != nullptr && |
| 2114 | cookie_field.Get() != nullptr) { |
| 2115 | // DexPathList has an array dexElements of Elements[] which each contain a dex file. |
| 2116 | mirror::Object* dex_elements_obj = |
| 2117 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList_dexElements)-> |
| 2118 | GetObject(dex_path_list); |
| 2119 | // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look |
| 2120 | // at the mCookie which is a DexFile vector. |
| 2121 | if (dex_elements_obj != nullptr) { |
| 2122 | Handle<mirror::ObjectArray<mirror::Object>> dex_elements = |
| 2123 | hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>()); |
| 2124 | for (int32_t i = 0; i < dex_elements->GetLength(); ++i) { |
| 2125 | mirror::Object* element = dex_elements->GetWithoutChecks(i); |
| 2126 | if (element == nullptr) { |
| 2127 | // Should never happen, fall back to java code to throw a NPE. |
| 2128 | break; |
| 2129 | } |
| 2130 | mirror::Object* dex_file = dex_file_field->GetObject(element); |
| 2131 | if (dex_file != nullptr) { |
| 2132 | const uint64_t cookie = cookie_field->GetLong(dex_file); |
| 2133 | auto* dex_files = |
| 2134 | reinterpret_cast<std::vector<const DexFile*>*>(static_cast<uintptr_t>(cookie)); |
| 2135 | if (dex_files == nullptr) { |
| 2136 | // This should never happen so log a warning. |
| 2137 | LOG(WARNING) << "Null DexFile::mCookie for " << descriptor; |
| 2138 | break; |
| 2139 | } |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2140 | for (const DexFile* cp_dex_file : *dex_files) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2141 | const DexFile::ClassDef* dex_class_def = cp_dex_file->FindClassDef(descriptor, hash); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2142 | if (dex_class_def != nullptr) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2143 | RegisterDexFile(*cp_dex_file); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2144 | mirror::Class* klass = DefineClass(self, descriptor, hash, class_loader, |
| 2145 | *cp_dex_file, *dex_class_def); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2146 | if (klass == nullptr) { |
| 2147 | CHECK(self->IsExceptionPending()) << descriptor; |
| 2148 | self->ClearException(); |
| 2149 | return nullptr; |
| 2150 | } |
| 2151 | return klass; |
| 2152 | } |
| 2153 | } |
| 2154 | } |
| 2155 | } |
| 2156 | } |
| 2157 | } |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2158 | self->AssertNoPendingException(); |
| 2159 | return nullptr; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2160 | } |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2161 | } |
| 2162 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2163 | mirror::Class* ClassLinker::FindClass(Thread* self, const char* descriptor, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2164 | Handle<mirror::ClassLoader> class_loader) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 2165 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2166 | DCHECK(self != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2167 | self->AssertNoPendingException(); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 2168 | if (descriptor[1] == '\0') { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2169 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 2170 | // for primitive classes that aren't backed by dex files. |
| 2171 | return FindPrimitiveClass(descriptor[0]); |
| 2172 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2173 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2174 | // Find the class in the loaded classes table. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2175 | mirror::Class* klass = LookupClass(self, descriptor, hash, class_loader.Get()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2176 | if (klass != nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2177 | return EnsureResolved(self, descriptor, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2178 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2179 | // Class is not yet loaded. |
| 2180 | if (descriptor[0] == '[') { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2181 | return CreateArrayClass(self, descriptor, hash, class_loader); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2182 | } else if (class_loader.Get() == nullptr) { |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2183 | // The boot class loader, search the boot class path. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2184 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2185 | if (pair.second != nullptr) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2186 | return DefineClass(self, descriptor, hash, NullHandle<mirror::ClassLoader>(), *pair.first, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2187 | *pair.second); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2188 | } else { |
| 2189 | // The boot class loader is searched ahead of the application class loader, failures are |
| 2190 | // expected and will be wrapped in a ClassNotFoundException. Use the pre-allocated error to |
| 2191 | // trigger the chaining with a proper stack trace. |
| 2192 | mirror::Throwable* pre_allocated = Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 2193 | self->SetException(ThrowLocation(), pre_allocated); |
| 2194 | return nullptr; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2195 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 2196 | } else if (Runtime::Current()->UseCompileTimeClassPath()) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2197 | // First try with the bootstrap class loader. |
| 2198 | if (class_loader.Get() != nullptr) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2199 | klass = LookupClass(self, descriptor, hash, nullptr); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2200 | if (klass != nullptr) { |
| 2201 | return EnsureResolved(self, descriptor, klass); |
| 2202 | } |
| 2203 | } |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2204 | // If the lookup failed search the boot class path. We don't perform a recursive call to avoid |
| 2205 | // a NoClassDefFoundError being allocated. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2206 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2207 | if (pair.second != nullptr) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2208 | return DefineClass(self, descriptor, hash, NullHandle<mirror::ClassLoader>(), *pair.first, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2209 | *pair.second); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2210 | } |
Ian Rogers | be7149f | 2013-08-20 09:29:39 -0700 | [diff] [blame] | 2211 | // Next try the compile time class path. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2212 | const std::vector<const DexFile*>* class_path; |
| 2213 | { |
Ian Rogers | be7149f | 2013-08-20 09:29:39 -0700 | [diff] [blame] | 2214 | ScopedObjectAccessUnchecked soa(self); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2215 | ScopedLocalRef<jobject> jclass_loader(soa.Env(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2216 | soa.AddLocalReference<jobject>(class_loader.Get())); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2217 | class_path = &Runtime::Current()->GetCompileTimeClassPath(jclass_loader.get()); |
| 2218 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2219 | pair = FindInClassPath(descriptor, hash, *class_path); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2220 | if (pair.second != nullptr) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2221 | return DefineClass(self, descriptor, hash, class_loader, *pair.first, *pair.second); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2222 | } else { |
| 2223 | // Use the pre-allocated NCDFE at compile time to avoid wasting time constructing exceptions. |
| 2224 | mirror::Throwable* pre_allocated = Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 2225 | self->SetException(ThrowLocation(), pre_allocated); |
| 2226 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2227 | } |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2228 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2229 | ScopedObjectAccessUnchecked soa(self); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2230 | mirror::Class* cp_klass = FindClassInPathClassLoader(soa, self, descriptor, hash, |
| 2231 | class_loader); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2232 | if (cp_klass != nullptr) { |
| 2233 | return cp_klass; |
Mathieu Chartier | 6bcae8f | 2014-09-04 18:33:17 -0700 | [diff] [blame] | 2234 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2235 | ScopedLocalRef<jobject> class_loader_object(soa.Env(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2236 | soa.AddLocalReference<jobject>(class_loader.Get())); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 2237 | std::string class_name_string(DescriptorToDot(descriptor)); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2238 | ScopedLocalRef<jobject> result(soa.Env(), nullptr); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 2239 | { |
| 2240 | ScopedThreadStateChange tsc(self, kNative); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2241 | ScopedLocalRef<jobject> class_name_object(soa.Env(), |
| 2242 | soa.Env()->NewStringUTF(class_name_string.c_str())); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2243 | if (class_name_object.get() == nullptr) { |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2244 | DCHECK(self->IsExceptionPending()); // OOME. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2245 | return nullptr; |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 2246 | } |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2247 | CHECK(class_loader_object.get() != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2248 | result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(), |
| 2249 | WellKnownClasses::java_lang_ClassLoader_loadClass, |
| 2250 | class_name_object.get())); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2251 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2252 | if (self->IsExceptionPending()) { |
Elliott Hughes | 748382f | 2012-01-26 18:07:38 -0800 | [diff] [blame] | 2253 | // If the ClassLoader threw, pass that exception up. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2254 | return nullptr; |
| 2255 | } else if (result.get() == nullptr) { |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 2256 | // broken loader - throw NPE to be compatible with Dalvik |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2257 | ThrowNullPointerException(nullptr, StringPrintf("ClassLoader.loadClass returned null for %s", |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2258 | class_name_string.c_str()).c_str()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2259 | return nullptr; |
Ian Rogers | 761bfa8 | 2012-01-11 10:14:05 -0800 | [diff] [blame] | 2260 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2261 | // success, return mirror::Class* |
| 2262 | return soa.Decode<mirror::Class*>(result.get()); |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 2263 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2264 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 2265 | UNREACHABLE(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2266 | } |
| 2267 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2268 | mirror::Class* ClassLinker::DefineClass(Thread* self, const char* descriptor, size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2269 | Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2270 | const DexFile& dex_file, |
| 2271 | const DexFile::ClassDef& dex_class_def) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2272 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2273 | auto klass = hs.NewHandle<mirror::Class>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2274 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2275 | // Load the class from the dex file. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2276 | if (UNLIKELY(!init_done_)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2277 | // finish up init of hand crafted class_roots_ |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2278 | if (strcmp(descriptor, "Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2279 | klass.Assign(GetClassRoot(kJavaLangObject)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2280 | } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2281 | klass.Assign(GetClassRoot(kJavaLangClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2282 | } else if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2283 | klass.Assign(GetClassRoot(kJavaLangString)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 2284 | } else if (strcmp(descriptor, "Ljava/lang/ref/Reference;") == 0) { |
| 2285 | klass.Assign(GetClassRoot(kJavaLangRefReference)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2286 | } else if (strcmp(descriptor, "Ljava/lang/DexCache;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2287 | klass.Assign(GetClassRoot(kJavaLangDexCache)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2288 | } else if (strcmp(descriptor, "Ljava/lang/reflect/ArtField;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2289 | klass.Assign(GetClassRoot(kJavaLangReflectArtField)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2290 | } else if (strcmp(descriptor, "Ljava/lang/reflect/ArtMethod;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2291 | klass.Assign(GetClassRoot(kJavaLangReflectArtMethod)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2292 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2293 | } |
| 2294 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2295 | if (klass.Get() == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2296 | // Allocate a class with the status of not ready. |
| 2297 | // Interface object should get the right size here. Regular class will |
| 2298 | // figure out the right size later and be replaced with one of the right |
| 2299 | // size when the class becomes resolved. |
| 2300 | klass.Assign(AllocClass(self, SizeOfClassWithoutEmbeddedTables(dex_file, dex_class_def))); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2301 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2302 | if (UNLIKELY(klass.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2303 | CHECK(self->IsExceptionPending()); // Expect an OOME. |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2304 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2305 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2306 | klass->SetDexCache(FindDexCache(dex_file)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2307 | LoadClass(self, dex_file, dex_class_def, klass, class_loader.Get()); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 2308 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2309 | if (self->IsExceptionPending()) { |
| 2310 | // An exception occured during load, set status to erroneous while holding klass' lock in case |
| 2311 | // notification is necessary. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2312 | if (!klass->IsErroneous()) { |
| 2313 | klass->SetStatus(mirror::Class::kStatusError, self); |
| 2314 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2315 | return nullptr; |
| 2316 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2317 | klass->SetClinitThreadId(self->GetTid()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2318 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2319 | // Add the newly loaded class to the loaded classes table. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2320 | mirror::Class* existing = InsertClass(descriptor, klass.Get(), hash); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2321 | if (existing != nullptr) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2322 | // We failed to insert because we raced with another thread. Calling EnsureResolved may cause |
| 2323 | // this thread to block. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2324 | return EnsureResolved(self, descriptor, existing); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2325 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2326 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2327 | // Finish loading (if necessary) by finding parents |
| 2328 | CHECK(!klass->IsLoaded()); |
| 2329 | if (!LoadSuperAndInterfaces(klass, dex_file)) { |
| 2330 | // Loading failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2331 | if (!klass->IsErroneous()) { |
| 2332 | klass->SetStatus(mirror::Class::kStatusError, self); |
| 2333 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2334 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2335 | } |
| 2336 | CHECK(klass->IsLoaded()); |
| 2337 | // Link the class (if necessary) |
| 2338 | CHECK(!klass->IsResolved()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2339 | // TODO: Use fast jobjects? |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2340 | auto interfaces = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2341 | |
| 2342 | mirror::Class* new_class = nullptr; |
| 2343 | if (!LinkClass(self, descriptor, klass, interfaces, &new_class)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2344 | // Linking failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2345 | if (!klass->IsErroneous()) { |
| 2346 | klass->SetStatus(mirror::Class::kStatusError, self); |
| 2347 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2348 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2349 | } |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 2350 | self->AssertNoPendingException(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2351 | CHECK(new_class != nullptr) << descriptor; |
| 2352 | CHECK(new_class->IsResolved()) << descriptor; |
| 2353 | |
| 2354 | Handle<mirror::Class> new_class_h(hs.NewHandle(new_class)); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2355 | |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 2356 | // Instrumentation may have updated entrypoints for all methods of all |
| 2357 | // classes. However it could not update methods of this class while we |
| 2358 | // were loading it. Now the class is resolved, we can update entrypoints |
| 2359 | // as required by instrumentation. |
| 2360 | if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) { |
| 2361 | // We must be in the kRunnable state to prevent instrumentation from |
| 2362 | // suspending all threads to update entrypoints while we are doing it |
| 2363 | // for this class. |
| 2364 | DCHECK_EQ(self->GetState(), kRunnable); |
| 2365 | Runtime::Current()->GetInstrumentation()->InstallStubsForClass(new_class_h.Get()); |
| 2366 | } |
| 2367 | |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2368 | /* |
| 2369 | * We send CLASS_PREPARE events to the debugger from here. The |
| 2370 | * definition of "preparation" is creating the static fields for a |
| 2371 | * class and initializing them to the standard default values, but not |
| 2372 | * executing any code (that comes later, during "initialization"). |
| 2373 | * |
| 2374 | * We did the static preparation in LinkClass. |
| 2375 | * |
| 2376 | * The class has been prepared and resolved but possibly not yet verified |
| 2377 | * at this point. |
| 2378 | */ |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2379 | Dbg::PostClassPrepare(new_class_h.Get()); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2380 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2381 | return new_class_h.Get(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2382 | } |
| 2383 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2384 | uint32_t ClassLinker::SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, |
| 2385 | const DexFile::ClassDef& dex_class_def) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2386 | const uint8_t* class_data = dex_file.GetClassData(dex_class_def); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2387 | size_t num_ref = 0; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2388 | size_t num_8 = 0; |
| 2389 | size_t num_16 = 0; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2390 | size_t num_32 = 0; |
| 2391 | size_t num_64 = 0; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2392 | if (class_data != nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2393 | for (ClassDataItemIterator it(dex_file, class_data); it.HasNextStaticField(); it.Next()) { |
| 2394 | const DexFile::FieldId& field_id = dex_file.GetFieldId(it.GetMemberIndex()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2395 | const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2396 | char c = descriptor[0]; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2397 | switch (c) { |
| 2398 | case 'L': |
| 2399 | case '[': |
| 2400 | num_ref++; |
| 2401 | break; |
| 2402 | case 'J': |
| 2403 | case 'D': |
| 2404 | num_64++; |
| 2405 | break; |
| 2406 | case 'I': |
| 2407 | case 'F': |
| 2408 | num_32++; |
| 2409 | break; |
| 2410 | case 'S': |
| 2411 | case 'C': |
| 2412 | num_16++; |
| 2413 | break; |
| 2414 | case 'B': |
| 2415 | case 'Z': |
| 2416 | num_8++; |
| 2417 | break; |
| 2418 | default: |
| 2419 | LOG(FATAL) << "Unknown descriptor: " << c; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 2420 | UNREACHABLE(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2421 | } |
| 2422 | } |
| 2423 | } |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2424 | 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] | 2425 | } |
| 2426 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2427 | OatFile::OatClass ClassLinker::FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, |
| 2428 | bool* found) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2429 | DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 2430 | const OatFile::OatDexFile* oat_dex_file = FindOpenedOatDexFileForDexFile(dex_file); |
| 2431 | if (oat_dex_file == nullptr) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2432 | *found = false; |
| 2433 | return OatFile::OatClass::Invalid(); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2434 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2435 | *found = true; |
| 2436 | return oat_dex_file->GetOatClass(class_def_idx); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2437 | } |
| 2438 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2439 | static uint32_t GetOatMethodIndexFromMethodIndex(const DexFile& dex_file, uint16_t class_def_idx, |
| 2440 | uint32_t method_idx) { |
| 2441 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_idx); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2442 | const uint8_t* class_data = dex_file.GetClassData(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2443 | CHECK(class_data != nullptr); |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2444 | ClassDataItemIterator it(dex_file, class_data); |
| 2445 | // Skip fields |
| 2446 | while (it.HasNextStaticField()) { |
| 2447 | it.Next(); |
| 2448 | } |
| 2449 | while (it.HasNextInstanceField()) { |
| 2450 | it.Next(); |
| 2451 | } |
| 2452 | // Process methods |
| 2453 | size_t class_def_method_index = 0; |
| 2454 | while (it.HasNextDirectMethod()) { |
| 2455 | if (it.GetMemberIndex() == method_idx) { |
| 2456 | return class_def_method_index; |
| 2457 | } |
| 2458 | class_def_method_index++; |
| 2459 | it.Next(); |
| 2460 | } |
| 2461 | while (it.HasNextVirtualMethod()) { |
| 2462 | if (it.GetMemberIndex() == method_idx) { |
| 2463 | return class_def_method_index; |
| 2464 | } |
| 2465 | class_def_method_index++; |
| 2466 | it.Next(); |
| 2467 | } |
| 2468 | DCHECK(!it.HasNext()); |
| 2469 | 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] | 2470 | UNREACHABLE(); |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2471 | } |
| 2472 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2473 | const OatFile::OatMethod ClassLinker::FindOatMethodFor(mirror::ArtMethod* method, bool* found) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2474 | // 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] | 2475 | // method for direct methods (or virtual methods made direct). |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2476 | mirror::Class* declaring_class = method->GetDeclaringClass(); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2477 | size_t oat_method_index; |
| 2478 | if (method->IsStatic() || method->IsDirect()) { |
| 2479 | // Simple case where the oat method index was stashed at load time. |
| 2480 | oat_method_index = method->GetMethodIndex(); |
| 2481 | } else { |
| 2482 | // We're invoking a virtual method directly (thanks to sharpening), compute the oat_method_index |
| 2483 | // by search for its position in the declared virtual methods. |
| 2484 | oat_method_index = declaring_class->NumDirectMethods(); |
| 2485 | size_t end = declaring_class->NumVirtualMethods(); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2486 | bool found_virtual = false; |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2487 | for (size_t i = 0; i < end; i++) { |
Jeff Hao | 68caf9e | 2014-09-03 13:48:16 -0700 | [diff] [blame] | 2488 | // Check method index instead of identity in case of duplicate method definitions. |
| 2489 | if (method->GetDexMethodIndex() == |
| 2490 | declaring_class->GetVirtualMethod(i)->GetDexMethodIndex()) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2491 | found_virtual = true; |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2492 | break; |
| 2493 | } |
Ian Rogers | f320b63 | 2012-03-13 18:47:47 -0700 | [diff] [blame] | 2494 | oat_method_index++; |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2495 | } |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2496 | CHECK(found_virtual) << "Didn't find oat method index for virtual method: " |
| 2497 | << PrettyMethod(method); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2498 | } |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2499 | DCHECK_EQ(oat_method_index, |
| 2500 | GetOatMethodIndexFromMethodIndex(*declaring_class->GetDexCache()->GetDexFile(), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2501 | method->GetDeclaringClass()->GetDexClassDefIndex(), |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2502 | method->GetDexMethodIndex())); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2503 | OatFile::OatClass oat_class = FindOatClass(*declaring_class->GetDexCache()->GetDexFile(), |
| 2504 | declaring_class->GetDexClassDefIndex(), |
| 2505 | found); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2506 | if (!(*found)) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2507 | return OatFile::OatMethod::Invalid(); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2508 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2509 | return oat_class.GetOatMethod(oat_method_index); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2510 | } |
| 2511 | |
| 2512 | // Special case to get oat code without overwriting a trampoline. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2513 | const void* ClassLinker::GetQuickOatCodeFor(mirror::ArtMethod* method) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2514 | CHECK(!method->IsAbstract()) << PrettyMethod(method); |
Jeff Hao | 8df6cea | 2013-07-29 13:54:48 -0700 | [diff] [blame] | 2515 | if (method->IsProxyMethod()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2516 | return GetQuickProxyInvokeHandler(); |
Jeff Hao | 8df6cea | 2013-07-29 13:54:48 -0700 | [diff] [blame] | 2517 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2518 | bool found; |
| 2519 | OatFile::OatMethod oat_method = FindOatMethodFor(method, &found); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2520 | const void* result = nullptr; |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2521 | if (found) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2522 | result = oat_method.GetQuickCode(); |
| 2523 | } |
| 2524 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2525 | if (result == nullptr) { |
Ian Rogers | 1a57066 | 2014-03-12 01:02:21 -0700 | [diff] [blame] | 2526 | if (method->IsNative()) { |
| 2527 | // No code and native? Use generic trampoline. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2528 | result = GetQuickGenericJniStub(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2529 | } else { |
| 2530 | // No code? You must mean to go into the interpreter. |
| 2531 | result = GetQuickToInterpreterBridge(); |
| 2532 | } |
Ian Rogers | f3e9855 | 2013-03-20 15:49:49 -0700 | [diff] [blame] | 2533 | } |
| 2534 | return result; |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2535 | } |
| 2536 | |
Hiroshi Yamauchi | 9bdec88 | 2014-08-15 17:11:12 -0700 | [diff] [blame] | 2537 | const void* ClassLinker::GetOatMethodQuickCodeFor(mirror::ArtMethod* method) { |
| 2538 | if (method->IsNative() || method->IsAbstract() || method->IsProxyMethod()) { |
| 2539 | return nullptr; |
| 2540 | } |
| 2541 | bool found; |
| 2542 | OatFile::OatMethod oat_method = FindOatMethodFor(method, &found); |
| 2543 | return found ? oat_method.GetQuickCode() : nullptr; |
| 2544 | } |
| 2545 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2546 | const void* ClassLinker::GetQuickOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, |
| 2547 | uint32_t method_idx) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2548 | bool found; |
| 2549 | OatFile::OatClass oat_class = FindOatClass(dex_file, class_def_idx, &found); |
| 2550 | if (!found) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2551 | return nullptr; |
| 2552 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2553 | 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] | 2554 | return oat_class.GetOatMethod(oat_method_idx).GetQuickCode(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2555 | } |
| 2556 | |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2557 | // Returns true if the method must run with interpreter, false otherwise. |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2558 | static bool NeedsInterpreter(mirror::ArtMethod* method, const void* quick_code) |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 2559 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2560 | if (quick_code == nullptr) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2561 | // No code: need interpreter. |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 2562 | // May return true for native code, in the case of generic JNI |
| 2563 | // DCHECK(!method->IsNative()); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2564 | return true; |
| 2565 | } |
| 2566 | // If interpreter mode is enabled, every method (except native and proxy) must |
| 2567 | // be run with interpreter. |
| 2568 | return Runtime::Current()->GetInstrumentation()->InterpretOnly() && |
| 2569 | !method->IsNative() && !method->IsProxyMethod(); |
| 2570 | } |
| 2571 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2572 | void ClassLinker::FixupStaticTrampolines(mirror::Class* klass) { |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 2573 | DCHECK(klass->IsInitialized()) << PrettyDescriptor(klass); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2574 | if (klass->NumDirectMethods() == 0) { |
| 2575 | return; // No direct methods => no static methods. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2576 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2577 | Runtime* runtime = Runtime::Current(); |
| 2578 | if (!runtime->IsStarted() || runtime->UseCompileTimeClassPath()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 2579 | if (runtime->IsCompiler() || runtime->GetHeap()->HasImageSpace()) { |
| 2580 | return; // OAT file unavailable. |
| 2581 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2582 | } |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 2583 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 2584 | const DexFile& dex_file = klass->GetDexFile(); |
| 2585 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2586 | CHECK(dex_class_def != nullptr); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2587 | const uint8_t* class_data = dex_file.GetClassData(*dex_class_def); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2588 | // There should always be class data if there were direct methods. |
| 2589 | CHECK(class_data != nullptr) << PrettyDescriptor(klass); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2590 | ClassDataItemIterator it(dex_file, class_data); |
| 2591 | // Skip fields |
| 2592 | while (it.HasNextStaticField()) { |
| 2593 | it.Next(); |
| 2594 | } |
| 2595 | while (it.HasNextInstanceField()) { |
| 2596 | it.Next(); |
| 2597 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2598 | bool has_oat_class; |
| 2599 | OatFile::OatClass oat_class = FindOatClass(dex_file, klass->GetDexClassDefIndex(), |
| 2600 | &has_oat_class); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2601 | // Link the code of methods skipped by LinkCode. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2602 | for (size_t method_index = 0; it.HasNextDirectMethod(); ++method_index, it.Next()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2603 | mirror::ArtMethod* method = klass->GetDirectMethod(method_index); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2604 | if (!method->IsStatic()) { |
| 2605 | // Only update static methods. |
| 2606 | continue; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2607 | } |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2608 | const void* quick_code = nullptr; |
| 2609 | if (has_oat_class) { |
| 2610 | OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2611 | quick_code = oat_method.GetQuickCode(); |
| 2612 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2613 | const bool enter_interpreter = NeedsInterpreter(method, quick_code); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2614 | if (enter_interpreter) { |
| 2615 | // Use interpreter entry point. |
Ian Rogers | 1a57066 | 2014-03-12 01:02:21 -0700 | [diff] [blame] | 2616 | // Check whether the method is native, in which case it's generic JNI. |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2617 | if (quick_code == nullptr && method->IsNative()) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2618 | quick_code = GetQuickGenericJniStub(); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 2619 | } else { |
| 2620 | quick_code = GetQuickToInterpreterBridge(); |
| 2621 | } |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2622 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2623 | runtime->GetInstrumentation()->UpdateMethodsCode(method, quick_code); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2624 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2625 | // Ignore virtual methods on the iterator. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2626 | } |
| 2627 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2628 | void ClassLinker::LinkCode(Handle<mirror::ArtMethod> method, |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 2629 | const OatFile::OatClass* oat_class, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2630 | uint32_t class_def_method_index) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2631 | Runtime* runtime = Runtime::Current(); |
| 2632 | if (runtime->IsCompiler()) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2633 | // The following code only applies to a non-compiler runtime. |
| 2634 | return; |
| 2635 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2636 | // Method shouldn't have already been linked. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2637 | DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2638 | if (oat_class != nullptr) { |
| 2639 | // Every kind of method should at least get an invoke stub from the oat_method. |
| 2640 | // non-abstract methods also get their code pointers. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2641 | const OatFile::OatMethod oat_method = oat_class->GetOatMethod(class_def_method_index); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2642 | oat_method.LinkMethod(method.Get()); |
| 2643 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2644 | |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 2645 | // Install entry point from interpreter. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2646 | bool enter_interpreter = NeedsInterpreter(method.Get(), |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2647 | method->GetEntryPointFromQuickCompiledCode()); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2648 | if (enter_interpreter && !method->IsNative()) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2649 | method->SetEntryPointFromInterpreter(artInterpreterToInterpreterBridge); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 2650 | } else { |
Dragos Sbirlea | 08bf196 | 2013-08-12 08:53:04 -0700 | [diff] [blame] | 2651 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 2652 | } |
| 2653 | |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2654 | if (method->IsAbstract()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2655 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2656 | return; |
| 2657 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2658 | |
| 2659 | if (method->IsStatic() && !method->IsConstructor()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2660 | // For static methods excluding the class initializer, install the trampoline. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2661 | // It will be replaced by the proper entry point by ClassLinker::FixupStaticTrampolines |
| 2662 | // after initializing class (see ClassLinker::InitializeClass method). |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2663 | method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionStub()); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2664 | } else if (enter_interpreter) { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2665 | if (!method->IsNative()) { |
| 2666 | // Set entry point from compiled code if there's no code or in interpreter only mode. |
| 2667 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2668 | } else { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2669 | method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub()); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2670 | } |
Ian Rogers | 0d6de04 | 2012-02-29 08:50:26 -0800 | [diff] [blame] | 2671 | } |
jeffhao | 26c0a1a | 2012-01-17 16:28:33 -0800 | [diff] [blame] | 2672 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2673 | if (method->IsNative()) { |
| 2674 | // Unregistering restores the dlsym lookup stub. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2675 | method->UnregisterNative(); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 2676 | |
| 2677 | if (enter_interpreter) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2678 | // We have a native method here without code. Then it should have either the generic JNI |
| 2679 | // trampoline as entrypoint (non-static), or the resolution trampoline (static). |
| 2680 | // TODO: this doesn't handle all the cases where trampolines may be installed. |
| 2681 | const void* entry_point = method->GetEntryPointFromQuickCompiledCode(); |
| 2682 | DCHECK(IsQuickGenericJniStub(entry_point) || IsQuickResolutionStub(entry_point)); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 2683 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2684 | } |
| 2685 | } |
| 2686 | |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2687 | |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2688 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2689 | void ClassLinker::LoadClass(Thread* self, const DexFile& dex_file, |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 2690 | const DexFile::ClassDef& dex_class_def, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2691 | Handle<mirror::Class> klass, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2692 | mirror::ClassLoader* class_loader) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2693 | CHECK(klass.Get() != nullptr); |
| 2694 | CHECK(klass->GetDexCache() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2695 | CHECK_EQ(mirror::Class::kStatusNotReady, klass->GetStatus()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 2696 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2697 | CHECK(descriptor != nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2698 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2699 | klass->SetClass(GetClassRoot(kJavaLangClass)); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 2700 | uint32_t access_flags = dex_class_def.GetJavaAccessFlags(); |
Brian Carlstrom | 8e3fb14 | 2013-10-09 21:00:27 -0700 | [diff] [blame] | 2701 | CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2702 | klass->SetAccessFlags(access_flags); |
| 2703 | klass->SetClassLoader(class_loader); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2704 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2705 | klass->SetStatus(mirror::Class::kStatusIdx, nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2706 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2707 | klass->SetDexClassDefIndex(dex_file.GetIndexForClassDef(dex_class_def)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2708 | klass->SetDexTypeIndex(dex_class_def.class_idx_); |
Mathieu Chartier | 91a6dc4 | 2014-12-01 10:31:15 -0800 | [diff] [blame] | 2709 | CHECK(klass->GetDexCacheStrings() != nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2710 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2711 | const uint8_t* class_data = dex_file.GetClassData(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2712 | if (class_data == nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2713 | return; // no fields or methods - for example a marker interface |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2714 | } |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2715 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2716 | |
| 2717 | bool has_oat_class = false; |
| 2718 | if (Runtime::Current()->IsStarted() && !Runtime::Current()->UseCompileTimeClassPath()) { |
| 2719 | OatFile::OatClass oat_class = FindOatClass(dex_file, klass->GetDexClassDefIndex(), |
| 2720 | &has_oat_class); |
| 2721 | if (has_oat_class) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2722 | LoadClassMembers(self, dex_file, class_data, klass, &oat_class); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2723 | } |
| 2724 | } |
| 2725 | if (!has_oat_class) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2726 | LoadClassMembers(self, dex_file, class_data, klass, nullptr); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2727 | } |
| 2728 | } |
| 2729 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2730 | void ClassLinker::LoadClassMembers(Thread* self, const DexFile& dex_file, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2731 | const uint8_t* class_data, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2732 | Handle<mirror::Class> klass, |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2733 | const OatFile::OatClass* oat_class) { |
| 2734 | // Load fields. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2735 | ClassDataItemIterator it(dex_file, class_data); |
| 2736 | if (it.NumStaticFields() != 0) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2737 | mirror::ObjectArray<mirror::ArtField>* statics = AllocArtFieldArray(self, it.NumStaticFields()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2738 | if (UNLIKELY(statics == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2739 | CHECK(self->IsExceptionPending()); // OOME. |
| 2740 | return; |
| 2741 | } |
| 2742 | klass->SetSFields(statics); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2743 | } |
| 2744 | if (it.NumInstanceFields() != 0) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2745 | mirror::ObjectArray<mirror::ArtField>* fields = |
| 2746 | AllocArtFieldArray(self, it.NumInstanceFields()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2747 | if (UNLIKELY(fields == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2748 | CHECK(self->IsExceptionPending()); // OOME. |
| 2749 | return; |
| 2750 | } |
| 2751 | klass->SetIFields(fields); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2752 | } |
| 2753 | for (size_t i = 0; it.HasNextStaticField(); i++, it.Next()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2754 | self->AllowThreadSuspension(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2755 | StackHandleScope<1> hs(self); |
| 2756 | Handle<mirror::ArtField> sfield(hs.NewHandle(AllocArtField(self))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2757 | if (UNLIKELY(sfield.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2758 | CHECK(self->IsExceptionPending()); // OOME. |
| 2759 | return; |
| 2760 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2761 | klass->SetStaticField(i, sfield.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2762 | LoadField(dex_file, it, klass, sfield); |
| 2763 | } |
| 2764 | for (size_t i = 0; it.HasNextInstanceField(); i++, it.Next()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2765 | self->AllowThreadSuspension(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2766 | StackHandleScope<1> hs(self); |
| 2767 | Handle<mirror::ArtField> ifield(hs.NewHandle(AllocArtField(self))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2768 | if (UNLIKELY(ifield.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2769 | CHECK(self->IsExceptionPending()); // OOME. |
| 2770 | return; |
| 2771 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2772 | klass->SetInstanceField(i, ifield.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2773 | LoadField(dex_file, it, klass, ifield); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2774 | } |
| 2775 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2776 | // Load methods. |
| 2777 | if (it.NumDirectMethods() != 0) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2778 | // TODO: append direct methods to class object |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2779 | mirror::ObjectArray<mirror::ArtMethod>* directs = |
| 2780 | AllocArtMethodArray(self, it.NumDirectMethods()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2781 | if (UNLIKELY(directs == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2782 | CHECK(self->IsExceptionPending()); // OOME. |
| 2783 | return; |
| 2784 | } |
| 2785 | klass->SetDirectMethods(directs); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2786 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2787 | if (it.NumVirtualMethods() != 0) { |
| 2788 | // TODO: append direct methods to class object |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2789 | mirror::ObjectArray<mirror::ArtMethod>* virtuals = |
| 2790 | AllocArtMethodArray(self, it.NumVirtualMethods()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2791 | if (UNLIKELY(virtuals == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2792 | CHECK(self->IsExceptionPending()); // OOME. |
| 2793 | return; |
| 2794 | } |
| 2795 | klass->SetVirtualMethods(virtuals); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2796 | } |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2797 | size_t class_def_method_index = 0; |
Jeff Hao | 68caf9e | 2014-09-03 13:48:16 -0700 | [diff] [blame] | 2798 | uint32_t last_dex_method_index = DexFile::kDexNoIndex; |
| 2799 | size_t last_class_def_method_index = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2800 | for (size_t i = 0; it.HasNextDirectMethod(); i++, it.Next()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2801 | self->AllowThreadSuspension(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2802 | StackHandleScope<1> hs(self); |
| 2803 | Handle<mirror::ArtMethod> method(hs.NewHandle(LoadMethod(self, dex_file, it, klass))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2804 | if (UNLIKELY(method.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2805 | CHECK(self->IsExceptionPending()); // OOME. |
| 2806 | return; |
| 2807 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2808 | klass->SetDirectMethod(i, method.Get()); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2809 | LinkCode(method, oat_class, class_def_method_index); |
Jeff Hao | 68caf9e | 2014-09-03 13:48:16 -0700 | [diff] [blame] | 2810 | uint32_t it_method_index = it.GetMemberIndex(); |
| 2811 | if (last_dex_method_index == it_method_index) { |
| 2812 | // duplicate case |
| 2813 | method->SetMethodIndex(last_class_def_method_index); |
| 2814 | } else { |
| 2815 | method->SetMethodIndex(class_def_method_index); |
| 2816 | last_dex_method_index = it_method_index; |
| 2817 | last_class_def_method_index = class_def_method_index; |
| 2818 | } |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2819 | class_def_method_index++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2820 | } |
| 2821 | for (size_t i = 0; it.HasNextVirtualMethod(); i++, it.Next()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2822 | self->AllowThreadSuspension(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2823 | StackHandleScope<1> hs(self); |
| 2824 | Handle<mirror::ArtMethod> method(hs.NewHandle(LoadMethod(self, dex_file, it, klass))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2825 | if (UNLIKELY(method.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2826 | CHECK(self->IsExceptionPending()); // OOME. |
| 2827 | return; |
| 2828 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2829 | klass->SetVirtualMethod(i, method.Get()); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2830 | DCHECK_EQ(class_def_method_index, it.NumDirectMethods() + i); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2831 | LinkCode(method, oat_class, class_def_method_index); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2832 | class_def_method_index++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2833 | } |
| 2834 | DCHECK(!it.HasNext()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2835 | } |
| 2836 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 2837 | void ClassLinker::LoadField(const DexFile& /*dex_file*/, const ClassDataItemIterator& it, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2838 | Handle<mirror::Class> klass, |
| 2839 | Handle<mirror::ArtField> dst) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2840 | uint32_t field_idx = it.GetMemberIndex(); |
| 2841 | dst->SetDexFieldIndex(field_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2842 | dst->SetDeclaringClass(klass.Get()); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 2843 | dst->SetAccessFlags(it.GetFieldAccessFlags()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2844 | } |
| 2845 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2846 | mirror::ArtMethod* ClassLinker::LoadMethod(Thread* self, const DexFile& dex_file, |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2847 | const ClassDataItemIterator& it, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2848 | Handle<mirror::Class> klass) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2849 | uint32_t dex_method_idx = it.GetMemberIndex(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2850 | const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2851 | const char* method_name = dex_file.StringDataByIdx(method_id.name_idx_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2852 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2853 | mirror::ArtMethod* dst = AllocArtMethod(self); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2854 | if (UNLIKELY(dst == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2855 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2856 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2857 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2858 | DCHECK(dst->IsArtMethod()) << PrettyDescriptor(dst->GetClass()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2859 | |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 2860 | ScopedAssertNoThreadSuspension ants(self, "LoadMethod"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2861 | dst->SetDexMethodIndex(dex_method_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2862 | dst->SetDeclaringClass(klass.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2863 | dst->SetCodeItemOffset(it.GetMethodCodeItemOffset()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2864 | |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2865 | dst->SetDexCacheResolvedMethods(klass->GetDexCache()->GetResolvedMethods()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2866 | dst->SetDexCacheResolvedTypes(klass->GetDexCache()->GetResolvedTypes()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2867 | |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 2868 | uint32_t access_flags = it.GetMethodAccessFlags(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2869 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2870 | if (UNLIKELY(strcmp("finalize", method_name) == 0)) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2871 | // Set finalizable flag on declaring class. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2872 | if (strcmp("V", dex_file.GetShorty(method_id.proto_idx_)) == 0) { |
| 2873 | // Void return type. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2874 | if (klass->GetClassLoader() != nullptr) { // All non-boot finalizer methods are flagged. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2875 | klass->SetFinalizable(); |
| 2876 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2877 | std::string temp; |
| 2878 | const char* klass_descriptor = klass->GetDescriptor(&temp); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2879 | // The Enum class declares a "final" finalize() method to prevent subclasses from |
| 2880 | // introducing a finalizer. We don't want to set the finalizable flag for Enum or its |
| 2881 | // subclasses, so we exclude it here. |
| 2882 | // We also want to avoid setting the flag on Object, where we know that finalize() is |
| 2883 | // empty. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2884 | if (strcmp(klass_descriptor, "Ljava/lang/Object;") != 0 && |
| 2885 | strcmp(klass_descriptor, "Ljava/lang/Enum;") != 0) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2886 | klass->SetFinalizable(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2887 | } |
| 2888 | } |
| 2889 | } |
| 2890 | } else if (method_name[0] == '<') { |
| 2891 | // Fix broken access flags for initializers. Bug 11157540. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2892 | bool is_init = (strcmp("<init>", method_name) == 0); |
| 2893 | bool is_clinit = !is_init && (strcmp("<clinit>", method_name) == 0); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2894 | if (UNLIKELY(!is_init && !is_clinit)) { |
| 2895 | LOG(WARNING) << "Unexpected '<' at start of method name " << method_name; |
| 2896 | } else { |
| 2897 | if (UNLIKELY((access_flags & kAccConstructor) == 0)) { |
| 2898 | 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] | 2899 | << PrettyDescriptor(klass.Get()) << " in dex file " << dex_file.GetLocation(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2900 | access_flags |= kAccConstructor; |
| 2901 | } |
| 2902 | } |
| 2903 | } |
| 2904 | dst->SetAccessFlags(access_flags); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2905 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2906 | return dst; |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2907 | } |
| 2908 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2909 | void ClassLinker::AppendToBootClassPath(Thread* self, const DexFile& dex_file) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2910 | StackHandleScope<1> hs(self); |
| 2911 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(AllocDexCache(self, dex_file))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2912 | CHECK(dex_cache.Get() != nullptr) << "Failed to allocate dex cache for " |
| 2913 | << dex_file.GetLocation(); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2914 | AppendToBootClassPath(dex_file, dex_cache); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2915 | } |
| 2916 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2917 | void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2918 | Handle<mirror::DexCache> dex_cache) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2919 | CHECK(dex_cache.Get() != nullptr) << dex_file.GetLocation(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2920 | boot_class_path_.push_back(&dex_file); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2921 | RegisterDexFile(dex_file, dex_cache); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2922 | } |
| 2923 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2924 | bool ClassLinker::IsDexFileRegisteredLocked(const DexFile& dex_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2925 | dex_lock_.AssertSharedHeld(Thread::Current()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2926 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2927 | mirror::DexCache* dex_cache = GetDexCache(i); |
| 2928 | if (dex_cache->GetDexFile() == &dex_file) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2929 | return true; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2930 | } |
| 2931 | } |
| 2932 | return false; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2933 | } |
| 2934 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2935 | bool ClassLinker::IsDexFileRegistered(const DexFile& dex_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2936 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | 0691851 | 2011-10-16 23:39:12 -0700 | [diff] [blame] | 2937 | return IsDexFileRegisteredLocked(dex_file); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2938 | } |
| 2939 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2940 | void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2941 | Handle<mirror::DexCache> dex_cache) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2942 | dex_lock_.AssertExclusiveHeld(Thread::Current()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2943 | CHECK(dex_cache.Get() != nullptr) << dex_file.GetLocation(); |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 2944 | CHECK(dex_cache->GetLocation()->Equals(dex_file.GetLocation())) |
| 2945 | << dex_cache->GetLocation()->ToModifiedUtf8() << " " << dex_file.GetLocation(); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 2946 | dex_caches_.push_back(GcRoot<mirror::DexCache>(dex_cache.Get())); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2947 | dex_cache->SetDexFile(&dex_file); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 2948 | if (log_new_dex_caches_roots_) { |
| 2949 | // TODO: This is not safe if we can remove dex caches. |
| 2950 | new_dex_cache_roots_.push_back(dex_caches_.size() - 1); |
| 2951 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2952 | } |
| 2953 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2954 | void ClassLinker::RegisterDexFile(const DexFile& dex_file) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 2955 | Thread* self = Thread::Current(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 2956 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2957 | ReaderMutexLock mu(self, dex_lock_); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 2958 | if (IsDexFileRegisteredLocked(dex_file)) { |
| 2959 | return; |
| 2960 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2961 | } |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 2962 | // Don't alloc while holding the lock, since allocation may need to |
| 2963 | // suspend all threads and another thread may need the dex_lock_ to |
| 2964 | // get to a suspend point. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2965 | StackHandleScope<1> hs(self); |
| 2966 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(AllocDexCache(self, dex_file))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2967 | CHECK(dex_cache.Get() != nullptr) << "Failed to allocate dex cache for " |
| 2968 | << dex_file.GetLocation(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 2969 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2970 | WriterMutexLock mu(self, dex_lock_); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 2971 | if (IsDexFileRegisteredLocked(dex_file)) { |
| 2972 | return; |
| 2973 | } |
| 2974 | RegisterDexFileLocked(dex_file, dex_cache); |
| 2975 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2976 | } |
| 2977 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2978 | void ClassLinker::RegisterDexFile(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2979 | Handle<mirror::DexCache> dex_cache) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2980 | WriterMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2981 | RegisterDexFileLocked(dex_file, dex_cache); |
| 2982 | } |
| 2983 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2984 | mirror::DexCache* ClassLinker::FindDexCache(const DexFile& dex_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2985 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2986 | // Search assuming unique-ness of dex file. |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2987 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2988 | mirror::DexCache* dex_cache = GetDexCache(i); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2989 | if (dex_cache->GetDexFile() == &dex_file) { |
| 2990 | return dex_cache; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2991 | } |
| 2992 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2993 | // Search matching by location name. |
| 2994 | std::string location(dex_file.GetLocation()); |
| 2995 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2996 | mirror::DexCache* dex_cache = GetDexCache(i); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2997 | if (dex_cache->GetDexFile()->GetLocation() == location) { |
| 2998 | return dex_cache; |
| 2999 | } |
| 3000 | } |
| 3001 | // Failure, dump diagnostic and abort. |
| 3002 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3003 | mirror::DexCache* dex_cache = GetDexCache(i); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3004 | LOG(ERROR) << "Registered dex file " << i << " = " << dex_cache->GetDexFile()->GetLocation(); |
| 3005 | } |
| 3006 | LOG(FATAL) << "Failed to find DexCache for DexFile " << location; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 3007 | UNREACHABLE(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3008 | } |
| 3009 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3010 | void ClassLinker::FixupDexCaches(mirror::ArtMethod* resolution_method) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3011 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3012 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3013 | mirror::DexCache* dex_cache = GetDexCache(i); |
| 3014 | dex_cache->Fixup(resolution_method); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3015 | } |
| 3016 | } |
| 3017 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3018 | mirror::Class* ClassLinker::CreatePrimitiveClass(Thread* self, Primitive::Type type) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3019 | mirror::Class* klass = AllocClass(self, mirror::Class::PrimitiveClassSize()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3020 | if (UNLIKELY(klass == nullptr)) { |
| 3021 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3022 | } |
| 3023 | return InitializePrimitiveClass(klass, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3024 | } |
| 3025 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3026 | mirror::Class* ClassLinker::InitializePrimitiveClass(mirror::Class* primitive_class, |
| 3027 | Primitive::Type type) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3028 | CHECK(primitive_class != nullptr); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3029 | // Must hold lock on object when initializing. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3030 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3031 | StackHandleScope<1> hs(self); |
| 3032 | Handle<mirror::Class> h_class(hs.NewHandle(primitive_class)); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3033 | ObjectLock<mirror::Class> lock(self, h_class); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3034 | primitive_class->SetAccessFlags(kAccPublic | kAccFinal | kAccAbstract); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3035 | primitive_class->SetPrimitiveType(type); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3036 | primitive_class->SetStatus(mirror::Class::kStatusInitialized, self); |
| 3037 | const char* descriptor = Primitive::Descriptor(type); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3038 | mirror::Class* existing = InsertClass(descriptor, primitive_class, |
| 3039 | ComputeModifiedUtf8Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3040 | CHECK(existing == nullptr) << "InitPrimitiveClass(" << type << ") failed"; |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3041 | return primitive_class; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 3042 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3043 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3044 | // Create an array class (i.e. the class object for the array, not the |
| 3045 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 3046 | // "[Ljava/lang/String;". |
| 3047 | // |
| 3048 | // If "descriptor" refers to an array of primitives, look up the |
| 3049 | // primitive type's internally-generated class object. |
| 3050 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3051 | // "class_loader" is the class loader of the class that's referring to |
| 3052 | // us. It's used to ensure that we're looking for the element type in |
| 3053 | // the right context. It does NOT become the class loader for the |
| 3054 | // array class; that always comes from the base element class. |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3055 | // |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3056 | // Returns nullptr with an exception raised on failure. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3057 | mirror::Class* ClassLinker::CreateArrayClass(Thread* self, const char* descriptor, size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3058 | Handle<mirror::ClassLoader> class_loader) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3059 | // Identify the underlying component type |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3060 | CHECK_EQ('[', descriptor[0]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3061 | StackHandleScope<2> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3062 | MutableHandle<mirror::Class> component_type(hs.NewHandle(FindClass(self, descriptor + 1, |
| 3063 | class_loader))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3064 | if (component_type.Get() == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3065 | DCHECK(self->IsExceptionPending()); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 3066 | // We need to accept erroneous classes as component types. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3067 | const size_t component_hash = ComputeModifiedUtf8Hash(descriptor + 1); |
| 3068 | component_type.Assign(LookupClass(self, descriptor + 1, component_hash, class_loader.Get())); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 3069 | if (component_type.Get() == nullptr) { |
| 3070 | DCHECK(self->IsExceptionPending()); |
| 3071 | return nullptr; |
| 3072 | } else { |
| 3073 | self->ClearException(); |
| 3074 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3075 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 3076 | if (UNLIKELY(component_type->IsPrimitiveVoid())) { |
| 3077 | ThrowNoClassDefFoundError("Attempt to create array of void primitive type"); |
| 3078 | return nullptr; |
| 3079 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3080 | // See if the component type is already loaded. Array classes are |
| 3081 | // always associated with the class loader of their underlying |
| 3082 | // element type -- an array of Strings goes with the loader for |
| 3083 | // java/lang/String -- so we need to look for it there. (The |
| 3084 | // caller should have checked for the existence of the class |
| 3085 | // before calling here, but they did so with *their* class loader, |
| 3086 | // not the component type's loader.) |
| 3087 | // |
| 3088 | // If we find it, the caller adds "loader" to the class' initiating |
| 3089 | // loader list, which should prevent us from going through this again. |
| 3090 | // |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3091 | // This call is unnecessary if "loader" and "component_type->GetClassLoader()" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3092 | // are the same, because our caller (FindClass) just did the |
| 3093 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 3094 | // because we effectively do this lookup again when we add the new |
| 3095 | // class to the hash table --- necessary because of possible races with |
| 3096 | // other threads.) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3097 | if (class_loader.Get() != component_type->GetClassLoader()) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3098 | mirror::Class* new_class = LookupClass(self, descriptor, hash, component_type->GetClassLoader()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3099 | if (new_class != nullptr) { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3100 | return new_class; |
| 3101 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3102 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3103 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3104 | // Fill out the fields in the Class. |
| 3105 | // |
| 3106 | // It is possible to execute some methods against arrays, because |
| 3107 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 3108 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 3109 | // |
| 3110 | // Array classes are simple enough that we don't need to do a full |
| 3111 | // link step. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3112 | auto new_class = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3113 | if (UNLIKELY(!init_done_)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3114 | // Classes that were hand created, ie not by FindSystemClass |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3115 | if (strcmp(descriptor, "[Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3116 | new_class.Assign(GetClassRoot(kClassArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3117 | } else if (strcmp(descriptor, "[Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3118 | new_class.Assign(GetClassRoot(kObjectArrayClass)); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3119 | } else if (strcmp(descriptor, GetClassRootDescriptor(kJavaLangStringArrayClass)) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3120 | new_class.Assign(GetClassRoot(kJavaLangStringArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3121 | } else if (strcmp(descriptor, |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3122 | GetClassRootDescriptor(kJavaLangReflectArtMethodArrayClass)) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3123 | new_class.Assign(GetClassRoot(kJavaLangReflectArtMethodArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3124 | } else if (strcmp(descriptor, |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3125 | GetClassRootDescriptor(kJavaLangReflectArtFieldArrayClass)) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3126 | new_class.Assign(GetClassRoot(kJavaLangReflectArtFieldArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3127 | } else if (strcmp(descriptor, "[C") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3128 | new_class.Assign(GetClassRoot(kCharArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3129 | } else if (strcmp(descriptor, "[I") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3130 | new_class.Assign(GetClassRoot(kIntArrayClass)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3131 | } |
| 3132 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3133 | if (new_class.Get() == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3134 | new_class.Assign(AllocClass(self, mirror::Array::ClassSize())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3135 | if (new_class.Get() == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3136 | return nullptr; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3137 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3138 | new_class->SetComponentType(component_type.Get()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3139 | } |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3140 | 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] | 3141 | DCHECK(new_class->GetComponentType() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3142 | mirror::Class* java_lang_Object = GetClassRoot(kJavaLangObject); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3143 | new_class->SetSuperClass(java_lang_Object); |
| 3144 | new_class->SetVTable(java_lang_Object->GetVTable()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3145 | new_class->SetPrimitiveType(Primitive::kPrimNot); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3146 | new_class->SetClassLoader(component_type->GetClassLoader()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3147 | new_class->SetStatus(mirror::Class::kStatusLoaded, self); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 3148 | { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3149 | StackHandleScope<mirror::Class::kImtSize> hs2(self, |
| 3150 | Runtime::Current()->GetImtUnimplementedMethod()); |
| 3151 | new_class->PopulateEmbeddedImtAndVTable(&hs2); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 3152 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3153 | new_class->SetStatus(mirror::Class::kStatusInitialized, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3154 | // don't need to set new_class->SetObjectSize(..) |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 3155 | // because Object::SizeOf delegates to Array::SizeOf |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3156 | |
| 3157 | |
| 3158 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 3159 | // interfaces. We need to set that up here, so that stuff like |
| 3160 | // "instanceof" works right. |
| 3161 | // |
| 3162 | // Note: The GC could run during the call to FindSystemClass, |
| 3163 | // so we need to make sure the class object is GC-valid while we're in |
| 3164 | // there. Do this by clearing the interface list so the GC will just |
| 3165 | // think that the entries are null. |
| 3166 | |
| 3167 | |
| 3168 | // Use the single, global copies of "interfaces" and "iftable" |
| 3169 | // (remember not to free them for arrays). |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3170 | { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 3171 | mirror::IfTable* array_iftable = array_iftable_.Read(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3172 | CHECK(array_iftable != nullptr); |
| 3173 | new_class->SetIfTable(array_iftable); |
| 3174 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3175 | |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 3176 | // Inherit access flags from the component type. |
| 3177 | int access_flags = new_class->GetComponentType()->GetAccessFlags(); |
| 3178 | // Lose any implementation detail flags; in particular, arrays aren't finalizable. |
| 3179 | access_flags &= kAccJavaFlagsMask; |
| 3180 | // 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] | 3181 | // and remove "interface". |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 3182 | access_flags |= kAccAbstract | kAccFinal; |
| 3183 | access_flags &= ~kAccInterface; |
| 3184 | |
| 3185 | new_class->SetAccessFlags(access_flags); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3186 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3187 | mirror::Class* existing = InsertClass(descriptor, new_class.Get(), hash); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3188 | if (existing == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3189 | return new_class.Get(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3190 | } |
| 3191 | // Another thread must have loaded the class after we |
| 3192 | // started but before we finished. Abandon what we've |
| 3193 | // done. |
| 3194 | // |
| 3195 | // (Yes, this happens.) |
| 3196 | |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 3197 | return existing; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3198 | } |
| 3199 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3200 | mirror::Class* ClassLinker::FindPrimitiveClass(char type) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3201 | switch (type) { |
| 3202 | case 'B': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3203 | return GetClassRoot(kPrimitiveByte); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3204 | case 'C': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3205 | return GetClassRoot(kPrimitiveChar); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3206 | case 'D': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3207 | return GetClassRoot(kPrimitiveDouble); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3208 | case 'F': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3209 | return GetClassRoot(kPrimitiveFloat); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3210 | case 'I': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3211 | return GetClassRoot(kPrimitiveInt); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3212 | case 'J': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3213 | return GetClassRoot(kPrimitiveLong); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3214 | case 'S': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3215 | return GetClassRoot(kPrimitiveShort); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3216 | case 'Z': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3217 | return GetClassRoot(kPrimitiveBoolean); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3218 | case 'V': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3219 | return GetClassRoot(kPrimitiveVoid); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3220 | default: |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3221 | break; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 3222 | } |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 3223 | std::string printable_type(PrintableChar(type)); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 3224 | ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3225 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3226 | } |
| 3227 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3228 | mirror::Class* ClassLinker::InsertClass(const char* descriptor, mirror::Class* klass, |
| 3229 | size_t hash) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3230 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3231 | mirror::DexCache* dex_cache = klass->GetDexCache(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3232 | std::string source; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3233 | if (dex_cache != nullptr) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3234 | source += " from "; |
| 3235 | source += dex_cache->GetLocation()->ToModifiedUtf8(); |
| 3236 | } |
| 3237 | LOG(INFO) << "Loaded class " << descriptor << source; |
| 3238 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3239 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3240 | mirror::Class* existing = LookupClassFromTableLocked(descriptor, klass->GetClassLoader(), hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3241 | if (existing != nullptr) { |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 3242 | return existing; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 3243 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3244 | if (kIsDebugBuild && !klass->IsTemp() && klass->GetClassLoader() == nullptr && |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3245 | dex_cache_image_class_lookup_required_) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3246 | // Check a class loaded with the system class loader matches one in the image if the class |
| 3247 | // is in the image. |
| 3248 | existing = LookupClassFromImage(descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3249 | if (existing != nullptr) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3250 | CHECK_EQ(klass, existing); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3251 | } |
| 3252 | } |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 3253 | VerifyObject(klass); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3254 | class_table_.InsertWithHash(GcRoot<mirror::Class>(klass), hash); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3255 | if (log_new_class_table_roots_) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3256 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3257 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3258 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3259 | } |
| 3260 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3261 | mirror::Class* ClassLinker::UpdateClass(const char* descriptor, mirror::Class* klass, |
| 3262 | size_t hash) { |
| 3263 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3264 | auto existing_it = class_table_.FindWithHash(std::make_pair(descriptor, klass->GetClassLoader()), |
| 3265 | hash); |
| 3266 | if (existing_it == class_table_.end()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3267 | CHECK(klass->IsProxyClass()); |
| 3268 | return nullptr; |
| 3269 | } |
| 3270 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3271 | mirror::Class* existing = existing_it->Read(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3272 | CHECK_NE(existing, klass) << descriptor; |
| 3273 | CHECK(!existing->IsResolved()) << descriptor; |
| 3274 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusResolving) << descriptor; |
| 3275 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3276 | CHECK(!klass->IsTemp()) << descriptor; |
| 3277 | if (kIsDebugBuild && klass->GetClassLoader() == nullptr && |
| 3278 | dex_cache_image_class_lookup_required_) { |
| 3279 | // Check a class loaded with the system class loader matches one in the image if the class |
| 3280 | // is in the image. |
| 3281 | existing = LookupClassFromImage(descriptor); |
| 3282 | if (existing != nullptr) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3283 | CHECK_EQ(klass, existing) << descriptor; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3284 | } |
| 3285 | } |
| 3286 | VerifyObject(klass); |
| 3287 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3288 | // Update the element in the hash set. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3289 | *existing_it = GcRoot<mirror::Class>(klass); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3290 | if (log_new_class_table_roots_) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3291 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3292 | } |
| 3293 | |
| 3294 | return existing; |
| 3295 | } |
| 3296 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3297 | bool ClassLinker::RemoveClass(const char* descriptor, mirror::ClassLoader* class_loader) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3298 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3299 | auto pair = std::make_pair(descriptor, class_loader); |
| 3300 | auto it = class_table_.Find(pair); |
| 3301 | if (it != class_table_.end()) { |
| 3302 | class_table_.Erase(it); |
| 3303 | return true; |
| 3304 | } |
| 3305 | it = pre_zygote_class_table_.Find(pair); |
| 3306 | if (it != pre_zygote_class_table_.end()) { |
| 3307 | pre_zygote_class_table_.Erase(it); |
| 3308 | return true; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3309 | } |
| 3310 | return false; |
| 3311 | } |
| 3312 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3313 | mirror::Class* ClassLinker::LookupClass(Thread* self, const char* descriptor, size_t hash, |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3314 | mirror::ClassLoader* class_loader) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3315 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3316 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3317 | mirror::Class* result = LookupClassFromTableLocked(descriptor, class_loader, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3318 | if (result != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3319 | return result; |
| 3320 | } |
Sameer Abu Asal | 2c6de22 | 2013-05-02 17:38:59 -0700 | [diff] [blame] | 3321 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3322 | if (class_loader != nullptr || !dex_cache_image_class_lookup_required_) { |
| 3323 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3324 | } else { |
| 3325 | // Lookup failed but need to search dex_caches_. |
| 3326 | mirror::Class* result = LookupClassFromImage(descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3327 | if (result != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3328 | InsertClass(descriptor, result, hash); |
| 3329 | } else { |
| 3330 | // Searching the image dex files/caches failed, we don't want to get into this situation |
| 3331 | // often as map searches are faster, so after kMaxFailedDexCacheLookups move all image |
| 3332 | // classes into the class table. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 3333 | constexpr uint32_t kMaxFailedDexCacheLookups = 1000; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3334 | if (++failed_dex_cache_class_lookups_ > kMaxFailedDexCacheLookups) { |
| 3335 | MoveImageClassesToClassTable(); |
| 3336 | } |
| 3337 | } |
| 3338 | return result; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3339 | } |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 3340 | } |
| 3341 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3342 | mirror::Class* ClassLinker::LookupClassFromTableLocked(const char* descriptor, |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3343 | mirror::ClassLoader* class_loader, |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3344 | size_t hash) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3345 | auto descriptor_pair = std::make_pair(descriptor, class_loader); |
| 3346 | auto it = pre_zygote_class_table_.FindWithHash(descriptor_pair, hash); |
| 3347 | if (it == pre_zygote_class_table_.end()) { |
| 3348 | it = class_table_.FindWithHash(descriptor_pair, hash); |
| 3349 | if (it == class_table_.end()) { |
| 3350 | return nullptr; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 3351 | } |
| 3352 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3353 | return it->Read(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3354 | } |
| 3355 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3356 | static mirror::ObjectArray<mirror::DexCache>* GetImageDexCaches() |
| 3357 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 3358 | gc::space::ImageSpace* image = Runtime::Current()->GetHeap()->GetImageSpace(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3359 | CHECK(image != nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3360 | mirror::Object* root = image->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches); |
| 3361 | return root->AsObjectArray<mirror::DexCache>(); |
| 3362 | } |
| 3363 | |
| 3364 | void ClassLinker::MoveImageClassesToClassTable() { |
| 3365 | Thread* self = Thread::Current(); |
| 3366 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3367 | if (!dex_cache_image_class_lookup_required_) { |
| 3368 | return; // All dex cache classes are already in the class table. |
| 3369 | } |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 3370 | ScopedAssertNoThreadSuspension ants(self, "Moving image classes to class table"); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3371 | mirror::ObjectArray<mirror::DexCache>* dex_caches = GetImageDexCaches(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3372 | std::string temp; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3373 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 3374 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 3375 | mirror::ObjectArray<mirror::Class>* types = dex_cache->GetResolvedTypes(); |
| 3376 | for (int32_t j = 0; j < types->GetLength(); j++) { |
| 3377 | mirror::Class* klass = types->Get(j); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3378 | if (klass != nullptr) { |
| 3379 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3380 | const char* descriptor = klass->GetDescriptor(&temp); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3381 | size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3382 | mirror::Class* existing = LookupClassFromTableLocked(descriptor, nullptr, hash); |
| 3383 | if (existing != nullptr) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3384 | CHECK_EQ(existing, klass) << PrettyClassAndClassLoader(existing) << " != " |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3385 | << PrettyClassAndClassLoader(klass); |
| 3386 | } else { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3387 | class_table_.Insert(GcRoot<mirror::Class>(klass)); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3388 | if (log_new_class_table_roots_) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3389 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3390 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3391 | } |
| 3392 | } |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 3393 | } |
| 3394 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3395 | dex_cache_image_class_lookup_required_ = false; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3396 | } |
| 3397 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3398 | void ClassLinker::MoveClassTableToPreZygote() { |
| 3399 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3400 | DCHECK(pre_zygote_class_table_.Empty()); |
| 3401 | pre_zygote_class_table_ = std::move(class_table_); |
| 3402 | class_table_.Clear(); |
| 3403 | } |
| 3404 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3405 | mirror::Class* ClassLinker::LookupClassFromImage(const char* descriptor) { |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 3406 | ScopedAssertNoThreadSuspension ants(Thread::Current(), "Image class lookup"); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3407 | mirror::ObjectArray<mirror::DexCache>* dex_caches = GetImageDexCaches(); |
| 3408 | for (int32_t i = 0; i < dex_caches->GetLength(); ++i) { |
| 3409 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 3410 | const DexFile* dex_file = dex_cache->GetDexFile(); |
Vladimir Marko | 801a811 | 2014-01-06 14:28:16 +0000 | [diff] [blame] | 3411 | // Try binary searching the string/type index. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3412 | const DexFile::StringId* string_id = dex_file->FindStringId(descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3413 | if (string_id != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3414 | const DexFile::TypeId* type_id = |
| 3415 | dex_file->FindTypeId(dex_file->GetIndexForStringId(*string_id)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3416 | if (type_id != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3417 | uint16_t type_idx = dex_file->GetIndexForTypeId(*type_id); |
| 3418 | mirror::Class* klass = dex_cache->GetResolvedType(type_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3419 | if (klass != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3420 | return klass; |
| 3421 | } |
| 3422 | } |
| 3423 | } |
| 3424 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3425 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3426 | } |
| 3427 | |
| 3428 | void ClassLinker::LookupClasses(const char* descriptor, std::vector<mirror::Class*>& result) { |
| 3429 | result.clear(); |
| 3430 | if (dex_cache_image_class_lookup_required_) { |
| 3431 | MoveImageClassesToClassTable(); |
| 3432 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3433 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3434 | while (true) { |
| 3435 | auto it = class_table_.Find(descriptor); |
| 3436 | if (it == class_table_.end()) { |
| 3437 | break; |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 3438 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3439 | result.push_back(it->Read()); |
| 3440 | class_table_.Erase(it); |
| 3441 | } |
| 3442 | for (mirror::Class* k : result) { |
| 3443 | class_table_.Insert(GcRoot<mirror::Class>(k)); |
| 3444 | } |
| 3445 | size_t pre_zygote_start = result.size(); |
| 3446 | // Now handle the pre zygote table. |
| 3447 | // Note: This dirties the pre-zygote table but shouldn't be an issue since LookupClasses is only |
| 3448 | // called from the debugger. |
| 3449 | while (true) { |
| 3450 | auto it = pre_zygote_class_table_.Find(descriptor); |
| 3451 | if (it == pre_zygote_class_table_.end()) { |
| 3452 | break; |
| 3453 | } |
| 3454 | result.push_back(it->Read()); |
| 3455 | pre_zygote_class_table_.Erase(it); |
| 3456 | } |
| 3457 | for (size_t i = pre_zygote_start; i < result.size(); ++i) { |
| 3458 | pre_zygote_class_table_.Insert(GcRoot<mirror::Class>(result[i])); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 3459 | } |
| 3460 | } |
| 3461 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3462 | void ClassLinker::VerifyClass(Thread* self, Handle<mirror::Class> klass) { |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 3463 | // TODO: assert that the monitor on the Class is held |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3464 | ObjectLock<mirror::Class> lock(self, klass); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 3465 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3466 | // Don't attempt to re-verify if already sufficiently verified. |
Andreas Gampe | bb0c7f6 | 2014-09-11 10:59:33 -0700 | [diff] [blame] | 3467 | if (klass->IsVerified()) { |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 3468 | EnsurePreverifiedMethods(klass); |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3469 | return; |
| 3470 | } |
Andreas Gampe | bb0c7f6 | 2014-09-11 10:59:33 -0700 | [diff] [blame] | 3471 | if (klass->IsCompileTimeVerified() && Runtime::Current()->IsCompiler()) { |
| 3472 | return; |
| 3473 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3474 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3475 | // The class might already be erroneous, for example at compile time if we attempted to verify |
| 3476 | // this class as a parent to another. |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 3477 | if (klass->IsErroneous()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3478 | ThrowEarlierClassFailure(klass.Get()); |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 3479 | return; |
| 3480 | } |
| 3481 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3482 | if (klass->GetStatus() == mirror::Class::kStatusResolved) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3483 | klass->SetStatus(mirror::Class::kStatusVerifying, self); |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3484 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3485 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3486 | << PrettyClass(klass.Get()); |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3487 | CHECK(!Runtime::Current()->IsCompiler()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3488 | klass->SetStatus(mirror::Class::kStatusVerifyingAtRuntime, self); |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3489 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3490 | |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 3491 | // Skip verification if disabled. |
| 3492 | if (!Runtime::Current()->IsVerificationEnabled()) { |
| 3493 | klass->SetStatus(mirror::Class::kStatusVerified, self); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 3494 | EnsurePreverifiedMethods(klass); |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 3495 | return; |
| 3496 | } |
| 3497 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3498 | // Verify super class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3499 | StackHandleScope<2> hs(self); |
| 3500 | Handle<mirror::Class> super(hs.NewHandle(klass->GetSuperClass())); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3501 | if (super.Get() != nullptr) { |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3502 | // Acquire lock to prevent races on verifying the super class. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3503 | ObjectLock<mirror::Class> super_lock(self, super); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3504 | |
| 3505 | if (!super->IsVerified() && !super->IsErroneous()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3506 | VerifyClass(self, super); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3507 | } |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 3508 | if (!super->IsCompileTimeVerified()) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3509 | std::string error_msg( |
| 3510 | StringPrintf("Rejecting class %s that attempts to sub-class erroneous class %s", |
| 3511 | PrettyDescriptor(klass.Get()).c_str(), |
| 3512 | PrettyDescriptor(super.Get()).c_str())); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3513 | LOG(ERROR) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3514 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException(nullptr))); |
| 3515 | if (cause.Get() != nullptr) { |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3516 | self->ClearException(); |
| 3517 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3518 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
| 3519 | if (cause.Get() != nullptr) { |
| 3520 | self->GetException(nullptr)->SetCause(cause.Get()); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3521 | } |
Jeff Hao | 22cb09b | 2013-12-12 14:29:15 -0800 | [diff] [blame] | 3522 | ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex()); |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 3523 | if (Runtime::Current()->IsCompiler()) { |
| 3524 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
| 3525 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3526 | klass->SetStatus(mirror::Class::kStatusError, self); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3527 | return; |
| 3528 | } |
| 3529 | } |
| 3530 | |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 3531 | // 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] | 3532 | const DexFile& dex_file = *klass->GetDexCache()->GetDexFile(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3533 | mirror::Class::Status oat_file_class_status(mirror::Class::kStatusNotReady); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3534 | bool preverified = VerifyClassUsingOatFile(dex_file, klass.Get(), oat_file_class_status); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3535 | if (oat_file_class_status == mirror::Class::kStatusError) { |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 3536 | VLOG(class_linker) << "Skipping runtime verification of erroneous class " |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3537 | << PrettyDescriptor(klass.Get()) << " in " |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 3538 | << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3539 | ThrowVerifyError(klass.Get(), "Rejecting class %s because it failed compile-time verification", |
| 3540 | PrettyDescriptor(klass.Get()).c_str()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3541 | klass->SetStatus(mirror::Class::kStatusError, self); |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 3542 | return; |
| 3543 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3544 | verifier::MethodVerifier::FailureKind verifier_failure = verifier::MethodVerifier::kNoFailure; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3545 | std::string error_msg; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 3546 | if (!preverified) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3547 | verifier_failure = verifier::MethodVerifier::VerifyClass(self, klass.Get(), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3548 | Runtime::Current()->IsCompiler(), |
| 3549 | &error_msg); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 3550 | } |
| 3551 | if (preverified || verifier_failure != verifier::MethodVerifier::kHardFailure) { |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 3552 | if (!preverified && verifier_failure != verifier::MethodVerifier::kNoFailure) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3553 | VLOG(class_linker) << "Soft verification failure in class " << PrettyDescriptor(klass.Get()) |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 3554 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 3555 | << " because: " << error_msg; |
| 3556 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3557 | self->AssertNoPendingException(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3558 | // Make sure all classes referenced by catch blocks are resolved. |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3559 | ResolveClassExceptionHandlerTypes(dex_file, klass); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3560 | if (verifier_failure == verifier::MethodVerifier::kNoFailure) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3561 | // Even though there were no verifier failures we need to respect whether the super-class |
| 3562 | // was verified or requiring runtime reverification. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3563 | if (super.Get() == nullptr || super->IsVerified()) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3564 | klass->SetStatus(mirror::Class::kStatusVerified, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3565 | } else { |
| 3566 | CHECK_EQ(super->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3567 | klass->SetStatus(mirror::Class::kStatusRetryVerificationAtRuntime, self); |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 3568 | // Pretend a soft failure occured so that we don't consider the class verified below. |
| 3569 | verifier_failure = verifier::MethodVerifier::kSoftFailure; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3570 | } |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3571 | } else { |
| 3572 | CHECK_EQ(verifier_failure, verifier::MethodVerifier::kSoftFailure); |
| 3573 | // Soft failures at compile time should be retried at runtime. Soft |
| 3574 | // failures at runtime will be handled by slow paths in the generated |
| 3575 | // code. Set status accordingly. |
| 3576 | if (Runtime::Current()->IsCompiler()) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3577 | klass->SetStatus(mirror::Class::kStatusRetryVerificationAtRuntime, self); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3578 | } else { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3579 | klass->SetStatus(mirror::Class::kStatusVerified, self); |
Andreas Gampe | bb0c7f6 | 2014-09-11 10:59:33 -0700 | [diff] [blame] | 3580 | // As this is a fake verified status, make sure the methods are _not_ marked preverified |
| 3581 | // later. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3582 | klass->SetPreverified(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3583 | } |
| 3584 | } |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 3585 | } else { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3586 | LOG(ERROR) << "Verification failed on class " << PrettyDescriptor(klass.Get()) |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3587 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 3588 | << " because: " << error_msg; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3589 | self->AssertNoPendingException(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3590 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3591 | klass->SetStatus(mirror::Class::kStatusError, self); |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 3592 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3593 | if (preverified || verifier_failure == verifier::MethodVerifier::kNoFailure) { |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 3594 | // 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] | 3595 | // Let the interpreter know it by setting the kAccPreverified flag onto each |
| 3596 | // method. |
| 3597 | // Note: we're going here during compilation and at runtime. When we set the |
| 3598 | // kAccPreverified flag when compiling image classes, the flag is recorded |
| 3599 | // in the image and is set when loading the image. |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 3600 | EnsurePreverifiedMethods(klass); |
| 3601 | } |
| 3602 | } |
| 3603 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3604 | void ClassLinker::EnsurePreverifiedMethods(Handle<mirror::Class> klass) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3605 | if (!klass->IsPreverified()) { |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3606 | klass->SetPreverifiedFlagOnAllMethods(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3607 | klass->SetPreverified(); |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3608 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3609 | } |
| 3610 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3611 | bool ClassLinker::VerifyClassUsingOatFile(const DexFile& dex_file, mirror::Class* klass, |
| 3612 | mirror::Class::Status& oat_file_class_status) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3613 | // If we're compiling, we can only verify the class using the oat file if |
| 3614 | // we are not compiling the image or if the class we're verifying is not part of |
| 3615 | // the app. In other words, we will only check for preverification of bootclasspath |
| 3616 | // classes. |
| 3617 | if (Runtime::Current()->IsCompiler()) { |
| 3618 | // Are we compiling the bootclasspath? |
| 3619 | if (!Runtime::Current()->UseCompileTimeClassPath()) { |
| 3620 | return false; |
| 3621 | } |
| 3622 | // We are compiling an app (not the image). |
| 3623 | |
| 3624 | // Is this an app class? (I.e. not a bootclasspath class) |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3625 | if (klass->GetClassLoader() != nullptr) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3626 | return false; |
| 3627 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3628 | } |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3629 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 3630 | const OatFile::OatDexFile* oat_dex_file = FindOpenedOatDexFileForDexFile(dex_file); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3631 | // In case we run without an image there won't be a backing oat file. |
| 3632 | if (oat_dex_file == nullptr) { |
Anwar Ghuloum | ad256bb | 2013-07-18 14:58:55 -0700 | [diff] [blame] | 3633 | return false; |
| 3634 | } |
| 3635 | |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 3636 | // We may be running with a preopted oat file but without image. In this case, |
| 3637 | // we don't skip verification of preverified classes to ensure we initialize |
| 3638 | // dex caches with all types resolved during verification. |
| 3639 | // We need to trust image classes, as these might be coming out of a pre-opted, quickened boot |
| 3640 | // image (that we just failed loading), and the verifier can't be run on quickened opcodes when |
| 3641 | // the runtime isn't started. On the other hand, app classes can be re-verified even if they are |
| 3642 | // already pre-opted, as then the runtime is started. |
| 3643 | if (!Runtime::Current()->IsCompiler() && |
| 3644 | !Runtime::Current()->GetHeap()->HasImageSpace() && |
| 3645 | klass->GetClassLoader() != nullptr) { |
| 3646 | return false; |
| 3647 | } |
| 3648 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3649 | uint16_t class_def_index = klass->GetDexClassDefIndex(); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 3650 | oat_file_class_status = oat_dex_file->GetOatClass(class_def_index).GetStatus(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3651 | if (oat_file_class_status == mirror::Class::kStatusVerified || |
| 3652 | oat_file_class_status == mirror::Class::kStatusInitialized) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3653 | return true; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3654 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3655 | if (oat_file_class_status == mirror::Class::kStatusRetryVerificationAtRuntime) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 3656 | // Compile time verification failed with a soft error. Compile time verification can fail |
| 3657 | // because we have incomplete type information. Consider the following: |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3658 | // class ... { |
| 3659 | // Foo x; |
| 3660 | // .... () { |
| 3661 | // if (...) { |
| 3662 | // v1 gets assigned a type of resolved class Foo |
| 3663 | // } else { |
| 3664 | // v1 gets assigned a type of unresolved class Bar |
| 3665 | // } |
| 3666 | // iput x = v1 |
| 3667 | // } } |
| 3668 | // when we merge v1 following the if-the-else it results in Conflict |
| 3669 | // (see verifier::RegType::Merge) as we can't know the type of Bar and we could possibly be |
| 3670 | // allowing an unsafe assignment to the field x in the iput (javac may have compiled this as |
| 3671 | // it knew Bar was a sub-class of Foo, but for us this may have been moved into a separate apk |
| 3672 | // at compile time). |
| 3673 | return false; |
| 3674 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3675 | if (oat_file_class_status == mirror::Class::kStatusError) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 3676 | // Compile time verification failed with a hard error. This is caused by invalid instructions |
| 3677 | // in the class. These errors are unrecoverable. |
| 3678 | return false; |
| 3679 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3680 | if (oat_file_class_status == mirror::Class::kStatusNotReady) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3681 | // Status is uninitialized if we couldn't determine the status at compile time, for example, |
| 3682 | // not loading the class. |
| 3683 | // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy |
| 3684 | // isn't a problem and this case shouldn't occur |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3685 | return false; |
| 3686 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3687 | std::string temp; |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 3688 | LOG(FATAL) << "Unexpected class status: " << oat_file_class_status |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3689 | << " " << dex_file.GetLocation() << " " << PrettyClass(klass) << " " |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3690 | << klass->GetDescriptor(&temp); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 3691 | UNREACHABLE(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3692 | } |
| 3693 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3694 | void ClassLinker::ResolveClassExceptionHandlerTypes(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3695 | Handle<mirror::Class> klass) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3696 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
| 3697 | ResolveMethodExceptionHandlerTypes(dex_file, klass->GetDirectMethod(i)); |
| 3698 | } |
| 3699 | for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { |
| 3700 | ResolveMethodExceptionHandlerTypes(dex_file, klass->GetVirtualMethod(i)); |
| 3701 | } |
| 3702 | } |
| 3703 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3704 | void ClassLinker::ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3705 | mirror::ArtMethod* method) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3706 | // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod. |
| 3707 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3708 | if (code_item == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3709 | return; // native or abstract method |
| 3710 | } |
| 3711 | if (code_item->tries_size_ == 0) { |
| 3712 | return; // nothing to process |
| 3713 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 3714 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item, 0); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3715 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 3716 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 3717 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| 3718 | CatchHandlerIterator iterator(handlers_ptr); |
| 3719 | for (; iterator.HasNext(); iterator.Next()) { |
| 3720 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 3721 | // unresolved exception types will be ignored by exception delivery |
| 3722 | if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3723 | mirror::Class* exception_type = linker->ResolveType(iterator.GetHandlerTypeIndex(), method); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3724 | if (exception_type == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3725 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 3726 | Thread::Current()->ClearException(); |
| 3727 | } |
| 3728 | } |
| 3729 | } |
| 3730 | handlers_ptr = iterator.EndDataPointer(); |
| 3731 | } |
| 3732 | } |
| 3733 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3734 | static void CheckProxyConstructor(mirror::ArtMethod* constructor); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3735 | static void CheckProxyMethod(Handle<mirror::ArtMethod> method, |
| 3736 | Handle<mirror::ArtMethod> prototype); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3737 | |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 3738 | mirror::Class* ClassLinker::CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, jstring name, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3739 | jobjectArray interfaces, jobject loader, |
| 3740 | jobjectArray methods, jobjectArray throws) { |
| 3741 | Thread* self = soa.Self(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3742 | StackHandleScope<8> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3743 | MutableHandle<mirror::Class> klass(hs.NewHandle( |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3744 | AllocClass(self, GetClassRoot(kJavaLangClass), sizeof(mirror::Class)))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3745 | if (klass.Get() == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3746 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3747 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3748 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3749 | DCHECK(klass->GetClass() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3750 | klass->SetObjectSize(sizeof(mirror::Proxy)); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 3751 | // Set the class access flags incl. preverified, so we do not try to set the flag on the methods. |
| 3752 | klass->SetAccessFlags(kAccClassIsProxy | kAccPublic | kAccFinal | kAccPreverified); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3753 | klass->SetClassLoader(soa.Decode<mirror::ClassLoader*>(loader)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3754 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3755 | klass->SetName(soa.Decode<mirror::String*>(name)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3756 | mirror::Class* proxy_class = GetClassRoot(kJavaLangReflectProxy); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3757 | klass->SetDexCache(proxy_class->GetDexCache()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3758 | klass->SetStatus(mirror::Class::kStatusIdx, self); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3759 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3760 | // Instance fields are inherited, but we add a couple of static fields... |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3761 | { |
| 3762 | mirror::ObjectArray<mirror::ArtField>* sfields = AllocArtFieldArray(self, 2); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3763 | if (UNLIKELY(sfields == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3764 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3765 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3766 | } |
| 3767 | klass->SetSFields(sfields); |
| 3768 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3769 | // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by |
| 3770 | // our proxy, so Class.getInterfaces doesn't return the flattened set. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3771 | Handle<mirror::ArtField> interfaces_sfield(hs.NewHandle(AllocArtField(self))); |
| 3772 | if (UNLIKELY(interfaces_sfield.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3773 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3774 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3775 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3776 | klass->SetStaticField(0, interfaces_sfield.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3777 | interfaces_sfield->SetDexFieldIndex(0); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3778 | interfaces_sfield->SetDeclaringClass(klass.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3779 | interfaces_sfield->SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
| 3780 | // 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] | 3781 | Handle<mirror::ArtField> throws_sfield(hs.NewHandle(AllocArtField(self))); |
| 3782 | if (UNLIKELY(throws_sfield.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3783 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3784 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3785 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3786 | klass->SetStaticField(1, throws_sfield.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3787 | throws_sfield->SetDexFieldIndex(1); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3788 | throws_sfield->SetDeclaringClass(klass.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3789 | throws_sfield->SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3790 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3791 | // Proxies have 1 direct method, the constructor |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3792 | { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3793 | mirror::ObjectArray<mirror::ArtMethod>* directs = AllocArtMethodArray(self, 1); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3794 | if (UNLIKELY(directs == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3795 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3796 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3797 | } |
| 3798 | klass->SetDirectMethods(directs); |
| 3799 | mirror::ArtMethod* constructor = CreateProxyConstructor(self, klass, proxy_class); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3800 | if (UNLIKELY(constructor == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3801 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3802 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3803 | } |
| 3804 | klass->SetDirectMethod(0, constructor); |
| 3805 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3806 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3807 | // Create virtual method using specified prototypes. |
| 3808 | size_t num_virtual_methods = |
| 3809 | soa.Decode<mirror::ObjectArray<mirror::ArtMethod>*>(methods)->GetLength(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3810 | { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3811 | mirror::ObjectArray<mirror::ArtMethod>* virtuals = AllocArtMethodArray(self, |
| 3812 | num_virtual_methods); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3813 | if (UNLIKELY(virtuals == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3814 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3815 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3816 | } |
| 3817 | klass->SetVirtualMethods(virtuals); |
| 3818 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3819 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3820 | StackHandleScope<1> hs2(self); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3821 | mirror::ObjectArray<mirror::ArtMethod>* decoded_methods = |
| 3822 | soa.Decode<mirror::ObjectArray<mirror::ArtMethod>*>(methods); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3823 | Handle<mirror::ArtMethod> prototype(hs2.NewHandle(decoded_methods->Get(i))); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3824 | mirror::ArtMethod* clone = CreateProxyMethod(self, klass, prototype); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3825 | if (UNLIKELY(clone == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3826 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3827 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3828 | } |
| 3829 | klass->SetVirtualMethod(i, clone); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3830 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3831 | |
| 3832 | klass->SetSuperClass(proxy_class); // The super class is java.lang.reflect.Proxy |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3833 | klass->SetStatus(mirror::Class::kStatusLoaded, self); // Now effectively in the loaded state. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3834 | self->AssertNoPendingException(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3835 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3836 | std::string descriptor(GetDescriptorForProxy(klass.Get())); |
| 3837 | mirror::Class* new_class = nullptr; |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 3838 | { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3839 | // Must hold lock on object when resolved. |
| 3840 | ObjectLock<mirror::Class> resolution_lock(self, klass); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3841 | // Link the fields and virtual methods, creating vtable and iftables |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3842 | Handle<mirror::ObjectArray<mirror::Class> > h_interfaces( |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3843 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces))); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3844 | if (!LinkClass(self, descriptor.c_str(), klass, h_interfaces, &new_class)) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3845 | klass->SetStatus(mirror::Class::kStatusError, self); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3846 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3847 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3848 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3849 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3850 | CHECK(klass->IsRetired()); |
| 3851 | CHECK_NE(klass.Get(), new_class); |
| 3852 | klass.Assign(new_class); |
| 3853 | |
| 3854 | CHECK_EQ(interfaces_sfield->GetDeclaringClass(), new_class); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3855 | interfaces_sfield->SetObject<false>(klass.Get(), |
| 3856 | soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3857 | CHECK_EQ(throws_sfield->GetDeclaringClass(), new_class); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3858 | throws_sfield->SetObject<false>(klass.Get(), |
| 3859 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class> >*>(throws)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3860 | |
| 3861 | { |
| 3862 | // Lock on klass is released. Lock new class object. |
| 3863 | ObjectLock<mirror::Class> initialization_lock(self, klass); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3864 | klass->SetStatus(mirror::Class::kStatusInitialized, self); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 3865 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3866 | |
| 3867 | // sanity checks |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 3868 | if (kIsDebugBuild) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3869 | CHECK(klass->GetIFields() == nullptr); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3870 | CheckProxyConstructor(klass->GetDirectMethod(0)); |
| 3871 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3872 | StackHandleScope<2> hs2(self); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3873 | mirror::ObjectArray<mirror::ArtMethod>* decoded_methods = |
| 3874 | soa.Decode<mirror::ObjectArray<mirror::ArtMethod>*>(methods); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3875 | Handle<mirror::ArtMethod> prototype(hs2.NewHandle(decoded_methods->Get(i))); |
| 3876 | Handle<mirror::ArtMethod> virtual_method(hs2.NewHandle(klass->GetVirtualMethod(i))); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3877 | CheckProxyMethod(virtual_method, prototype); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3878 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3879 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3880 | mirror::String* decoded_name = soa.Decode<mirror::String*>(name); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3881 | std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3882 | decoded_name->ToModifiedUtf8().c_str())); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3883 | CHECK_EQ(PrettyField(klass->GetStaticField(0)), interfaces_field_name); |
| 3884 | |
| 3885 | std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3886 | decoded_name->ToModifiedUtf8().c_str())); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3887 | CHECK_EQ(PrettyField(klass->GetStaticField(1)), throws_field_name); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3888 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3889 | CHECK_EQ(klass.Get()->GetInterfaces(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3890 | soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3891 | CHECK_EQ(klass.Get()->GetThrows(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3892 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>*>(throws)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3893 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3894 | mirror::Class* existing = InsertClass(descriptor.c_str(), klass.Get(), |
| 3895 | ComputeModifiedUtf8Hash(descriptor.c_str())); |
Mathieu Chartier | 31b9d66 | 2013-10-14 11:53:12 -0700 | [diff] [blame] | 3896 | CHECK(existing == nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3897 | return klass.Get(); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3898 | } |
| 3899 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3900 | std::string ClassLinker::GetDescriptorForProxy(mirror::Class* proxy_class) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3901 | DCHECK(proxy_class->IsProxyClass()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3902 | mirror::String* name = proxy_class->GetName(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3903 | DCHECK(name != nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3904 | return DotToDescriptor(name->ToModifiedUtf8().c_str()); |
| 3905 | } |
| 3906 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3907 | mirror::ArtMethod* ClassLinker::FindMethodForProxy(mirror::Class* proxy_class, |
| 3908 | mirror::ArtMethod* proxy_method) { |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3909 | DCHECK(proxy_class->IsProxyClass()); |
| 3910 | DCHECK(proxy_method->IsProxyMethod()); |
| 3911 | // Locate the dex cache of the original interface/Object |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 3912 | mirror::DexCache* dex_cache = nullptr; |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3913 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3914 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3915 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3916 | mirror::DexCache* a_dex_cache = GetDexCache(i); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 3917 | if (proxy_method->HasSameDexCacheResolvedTypes(a_dex_cache->GetResolvedTypes())) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3918 | dex_cache = a_dex_cache; |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3919 | break; |
| 3920 | } |
| 3921 | } |
| 3922 | } |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 3923 | CHECK(dex_cache != nullptr); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3924 | uint32_t method_idx = proxy_method->GetDexMethodIndex(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3925 | mirror::ArtMethod* resolved_method = dex_cache->GetResolvedMethod(method_idx); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 3926 | CHECK(resolved_method != nullptr); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3927 | return resolved_method; |
| 3928 | } |
| 3929 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3930 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3931 | mirror::ArtMethod* ClassLinker::CreateProxyConstructor(Thread* self, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3932 | Handle<mirror::Class> klass, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3933 | mirror::Class* proxy_class) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3934 | // Create constructor for Proxy that must initialize h |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3935 | mirror::ObjectArray<mirror::ArtMethod>* proxy_direct_methods = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3936 | proxy_class->GetDirectMethods(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3937 | CHECK_EQ(proxy_direct_methods->GetLength(), 16); |
| 3938 | mirror::ArtMethod* proxy_constructor = proxy_direct_methods->Get(2); |
Sebastien Hertz | ae94e35 | 2014-08-27 15:32:56 +0200 | [diff] [blame] | 3939 | // Ensure constructor is in dex cache so that we can use the dex cache to look up the overridden |
| 3940 | // constructor method. |
| 3941 | proxy_class->GetDexCache()->SetResolvedMethod(proxy_constructor->GetDexMethodIndex(), |
| 3942 | proxy_constructor); |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 3943 | // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its |
| 3944 | // code_ too) |
Jeff Hao | f0a3f09 | 2014-07-24 16:26:09 -0700 | [diff] [blame] | 3945 | mirror::ArtMethod* constructor = down_cast<mirror::ArtMethod*>(proxy_constructor->Clone(self)); |
| 3946 | if (constructor == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3947 | CHECK(self->IsExceptionPending()); // OOME. |
Jeff Hao | f0a3f09 | 2014-07-24 16:26:09 -0700 | [diff] [blame] | 3948 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3949 | } |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3950 | // Make this constructor public and fix the class to be our Proxy version |
| 3951 | constructor->SetAccessFlags((constructor->GetAccessFlags() & ~kAccProtected) | kAccPublic); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3952 | constructor->SetDeclaringClass(klass.Get()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3953 | return constructor; |
| 3954 | } |
| 3955 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3956 | static void CheckProxyConstructor(mirror::ArtMethod* constructor) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 3957 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3958 | CHECK(constructor->IsConstructor()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3959 | CHECK_STREQ(constructor->GetName(), "<init>"); |
| 3960 | CHECK_STREQ(constructor->GetSignature().ToString().c_str(), |
| 3961 | "(Ljava/lang/reflect/InvocationHandler;)V"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3962 | DCHECK(constructor->IsPublic()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3963 | } |
| 3964 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3965 | mirror::ArtMethod* ClassLinker::CreateProxyMethod(Thread* self, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3966 | Handle<mirror::Class> klass, |
| 3967 | Handle<mirror::ArtMethod> prototype) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3968 | // Ensure prototype is in dex cache so that we can use the dex cache to look up the overridden |
| 3969 | // prototype method |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3970 | prototype->GetDeclaringClass()->GetDexCache()->SetResolvedMethod(prototype->GetDexMethodIndex(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3971 | prototype.Get()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3972 | // 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] | 3973 | // as necessary |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3974 | mirror::ArtMethod* method = down_cast<mirror::ArtMethod*>(prototype->Clone(self)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3975 | if (UNLIKELY(method == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3976 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3977 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3978 | } |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3979 | |
| 3980 | // Set class to be the concrete proxy class and clear the abstract flag, modify exceptions to |
| 3981 | // the intersection of throw exceptions as defined in Proxy |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3982 | method->SetDeclaringClass(klass.Get()); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3983 | method->SetAccessFlags((method->GetAccessFlags() & ~kAccAbstract) | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3984 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3985 | // At runtime the method looks like a reference and argument saving method, clone the code |
| 3986 | // related parameters from this method. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3987 | method->SetEntryPointFromQuickCompiledCode(GetQuickProxyInvokeHandler()); |
Dragos Sbirlea | 08bf196 | 2013-08-12 08:53:04 -0700 | [diff] [blame] | 3988 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3989 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3990 | return method; |
| 3991 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3992 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3993 | static void CheckProxyMethod(Handle<mirror::ArtMethod> method, |
| 3994 | Handle<mirror::ArtMethod> prototype) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 3995 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3996 | // Basic sanity |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3997 | CHECK(!prototype->IsFinal()); |
| 3998 | CHECK(method->IsFinal()); |
| 3999 | CHECK(!method->IsAbstract()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 4000 | |
| 4001 | // The proxy method doesn't have its own dex cache or dex file and so it steals those of its |
| 4002 | // 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] | 4003 | CHECK(prototype->HasSameDexCacheResolvedMethods(method.Get())); |
| 4004 | CHECK(prototype->HasSameDexCacheResolvedTypes(method.Get())); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 4005 | CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex()); |
| 4006 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4007 | CHECK_STREQ(method->GetName(), prototype->GetName()); |
| 4008 | CHECK_STREQ(method->GetShorty(), prototype->GetShorty()); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4009 | // More complex sanity - via dex cache |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 4010 | CHECK_EQ(method->GetInterfaceMethodIfProxy()->GetReturnType(), prototype->GetReturnType()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4011 | } |
| 4012 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4013 | static bool CanWeInitializeClass(mirror::Class* klass, bool can_init_statics, |
| 4014 | bool can_init_parents) |
| 4015 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 4016 | if (can_init_statics && can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4017 | return true; |
| 4018 | } |
| 4019 | if (!can_init_statics) { |
| 4020 | // Check if there's a class initializer. |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 4021 | mirror::ArtMethod* clinit = klass->FindClassInitializer(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4022 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4023 | return false; |
| 4024 | } |
| 4025 | // Check if there are encoded static values needing initialization. |
| 4026 | if (klass->NumStaticFields() != 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4027 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4028 | DCHECK(dex_class_def != nullptr); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4029 | if (dex_class_def->static_values_off_ != 0) { |
| 4030 | return false; |
| 4031 | } |
| 4032 | } |
| 4033 | } |
| 4034 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
| 4035 | mirror::Class* super_class = klass->GetSuperClass(); |
| 4036 | if (!can_init_parents && !super_class->IsInitialized()) { |
| 4037 | return false; |
| 4038 | } else { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 4039 | if (!CanWeInitializeClass(super_class, can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4040 | return false; |
| 4041 | } |
| 4042 | } |
| 4043 | } |
| 4044 | return true; |
| 4045 | } |
| 4046 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4047 | bool ClassLinker::InitializeClass(Thread* self, Handle<mirror::Class> klass, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4048 | bool can_init_statics, bool can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4049 | // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol |
| 4050 | |
| 4051 | // Are we already initialized and therefore done? |
| 4052 | // Note: we differ from the JLS here as we don't do this under the lock, this is benign as |
| 4053 | // an initialized class will never change its state. |
| 4054 | if (klass->IsInitialized()) { |
| 4055 | return true; |
| 4056 | } |
| 4057 | |
| 4058 | // 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] | 4059 | if (!CanWeInitializeClass(klass.Get(), can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4060 | return false; |
| 4061 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4062 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4063 | self->AllowThreadSuspension(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4064 | uint64_t t0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4065 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4066 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4067 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4068 | // Re-check under the lock in case another thread initialized ahead of us. |
| 4069 | if (klass->IsInitialized()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4070 | return true; |
| 4071 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4072 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4073 | // 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] | 4074 | if (klass->IsErroneous()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4075 | ThrowEarlierClassFailure(klass.Get()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4076 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4077 | return false; |
| 4078 | } |
| 4079 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4080 | CHECK(klass->IsResolved()) << PrettyClass(klass.Get()) << ": state=" << klass->GetStatus(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4081 | |
| 4082 | if (!klass->IsVerified()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4083 | VerifyClass(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4084 | if (!klass->IsVerified()) { |
| 4085 | // We failed to verify, expect either the klass to be erroneous or verification failed at |
| 4086 | // compile time. |
| 4087 | if (klass->IsErroneous()) { |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 4088 | CHECK(self->IsExceptionPending()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4089 | VlogClassInitializationFailure(klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4090 | } else { |
| 4091 | CHECK(Runtime::Current()->IsCompiler()); |
| 4092 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 4093 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4094 | return false; |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4095 | } else { |
| 4096 | self->AssertNoPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4097 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4098 | } |
| 4099 | |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4100 | // If the class is kStatusInitializing, either this thread is |
| 4101 | // initializing higher up the stack or another thread has beat us |
| 4102 | // to initializing and we need to wait. Either way, this |
| 4103 | // invocation of InitializeClass will not be responsible for |
| 4104 | // running <clinit> and will return. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4105 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4106 | // Could have got an exception during verification. |
| 4107 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4108 | VlogClassInitializationFailure(klass); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4109 | return false; |
| 4110 | } |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 4111 | // We caught somebody else in the act; was it us? |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 4112 | if (klass->GetClinitThreadId() == self->GetTid()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4113 | // Yes. That's fine. Return so we can continue initializing. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4114 | return true; |
| 4115 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4116 | // No. That's fine. Wait for another thread to finish initializing. |
| 4117 | return WaitForInitializeClass(klass, self, lock); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4118 | } |
| 4119 | |
| 4120 | if (!ValidateSuperClassDescriptors(klass)) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4121 | klass->SetStatus(mirror::Class::kStatusError, self); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4122 | return false; |
| 4123 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4124 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4125 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4126 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusVerified) << PrettyClass(klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4127 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4128 | // From here out other threads may observe that we're initializing and so changes of state |
| 4129 | // require the a notification. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 4130 | klass->SetClinitThreadId(self->GetTid()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4131 | klass->SetStatus(mirror::Class::kStatusInitializing, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4132 | |
| 4133 | t0 = NanoTime(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4134 | } |
| 4135 | |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 4136 | // Initialize super classes, must be done while initializing for the JLS. |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4137 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
| 4138 | mirror::Class* super_class = klass->GetSuperClass(); |
| 4139 | if (!super_class->IsInitialized()) { |
| 4140 | CHECK(!super_class->IsInterface()); |
| 4141 | CHECK(can_init_parents); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4142 | StackHandleScope<1> hs(self); |
| 4143 | Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4144 | bool super_initialized = InitializeClass(self, handle_scope_super, can_init_statics, true); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4145 | if (!super_initialized) { |
| 4146 | // The super class was verified ahead of entering initializing, we should only be here if |
| 4147 | // the super class became erroneous due to initialization. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4148 | CHECK(handle_scope_super->IsErroneous() && self->IsExceptionPending()) |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4149 | << "Super class initialization failed for " |
| 4150 | << PrettyDescriptor(handle_scope_super.Get()) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4151 | << " that has unexpected status " << handle_scope_super->GetStatus() |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4152 | << "\nPending exception:\n" |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4153 | << (self->GetException(nullptr) != nullptr ? self->GetException(nullptr)->Dump() : ""); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4154 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4155 | // Initialization failed because the super-class is erroneous. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4156 | klass->SetStatus(mirror::Class::kStatusError, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4157 | return false; |
| 4158 | } |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 4159 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4160 | } |
| 4161 | |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4162 | const size_t num_static_fields = klass->NumStaticFields(); |
| 4163 | if (num_static_fields > 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4164 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4165 | CHECK(dex_class_def != nullptr); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4166 | const DexFile& dex_file = klass->GetDexFile(); |
Hiroshi Yamauchi | 67ef46a | 2014-08-21 15:59:43 -0700 | [diff] [blame] | 4167 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4168 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4169 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4170 | |
| 4171 | // Eagerly fill in static fields so that the we don't have to do as many expensive |
| 4172 | // Class::FindStaticField in ResolveField. |
| 4173 | for (size_t i = 0; i < num_static_fields; ++i) { |
| 4174 | mirror::ArtField* field = klass->GetStaticField(i); |
| 4175 | const uint32_t field_idx = field->GetDexFieldIndex(); |
| 4176 | mirror::ArtField* resolved_field = dex_cache->GetResolvedField(field_idx); |
| 4177 | if (resolved_field == nullptr) { |
| 4178 | dex_cache->SetResolvedField(field_idx, field); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4179 | } else { |
| 4180 | DCHECK_EQ(field, resolved_field); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4181 | } |
| 4182 | } |
| 4183 | |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4184 | EncodedStaticFieldValueIterator value_it(dex_file, &dex_cache, &class_loader, |
| 4185 | this, *dex_class_def); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 4186 | const uint8_t* class_data = dex_file.GetClassData(*dex_class_def); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4187 | ClassDataItemIterator field_it(dex_file, class_data); |
| 4188 | if (value_it.HasNext()) { |
| 4189 | DCHECK(field_it.HasNextStaticField()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4190 | CHECK(can_init_statics); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4191 | for ( ; value_it.HasNext(); value_it.Next(), field_it.Next()) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 4192 | StackHandleScope<1> hs2(self); |
| 4193 | Handle<mirror::ArtField> field(hs2.NewHandle( |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4194 | ResolveField(dex_file, field_it.GetMemberIndex(), dex_cache, class_loader, true))); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4195 | if (Runtime::Current()->IsActiveTransaction()) { |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4196 | value_it.ReadValueToField<true>(field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4197 | } else { |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4198 | value_it.ReadValueToField<false>(field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4199 | } |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4200 | DCHECK(!value_it.HasNext() || field_it.HasNextStaticField()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4201 | } |
| 4202 | } |
| 4203 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4204 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 4205 | mirror::ArtMethod* clinit = klass->FindClassInitializer(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4206 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4207 | CHECK(can_init_statics); |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 4208 | JValue result; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4209 | clinit->Invoke(self, nullptr, 0, &result, "V"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4210 | } |
| 4211 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4212 | self->AllowThreadSuspension(); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 4213 | uint64_t t1 = NanoTime(); |
| 4214 | |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4215 | bool success = true; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4216 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4217 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4218 | |
| 4219 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4220 | WrapExceptionInInitializer(klass); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4221 | klass->SetStatus(mirror::Class::kStatusError, self); |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4222 | success = false; |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 4223 | } else if (Runtime::Current()->IsTransactionAborted()) { |
| 4224 | // The exception thrown when the transaction aborted has been caught and cleared |
| 4225 | // so we need to throw it again now. |
| 4226 | LOG(WARNING) << "Return from class initializer of " << PrettyDescriptor(klass.Get()) |
| 4227 | << " without exception while transaction was aborted: re-throw it now."; |
| 4228 | Runtime::Current()->ThrowInternalErrorForAbortedTransaction(self); |
| 4229 | klass->SetStatus(mirror::Class::kStatusError, self); |
| 4230 | success = false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4231 | } else { |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 4232 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 4233 | RuntimeStats* thread_stats = self->GetStats(); |
| 4234 | ++global_stats->class_init_count; |
| 4235 | ++thread_stats->class_init_count; |
| 4236 | global_stats->class_init_time_ns += (t1 - t0); |
| 4237 | thread_stats->class_init_time_ns += (t1 - t0); |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 4238 | // Set the class as initialized except if failed to initialize static fields. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4239 | klass->SetStatus(mirror::Class::kStatusInitialized, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4240 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4241 | std::string temp; |
| 4242 | LOG(INFO) << "Initialized class " << klass->GetDescriptor(&temp) << " from " << |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4243 | klass->GetLocation(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 4244 | } |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 4245 | // Opportunistically set static method trampolines to their destination. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4246 | FixupStaticTrampolines(klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4247 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4248 | } |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4249 | return success; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4250 | } |
| 4251 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4252 | bool ClassLinker::WaitForInitializeClass(Handle<mirror::Class> klass, Thread* self, |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4253 | ObjectLock<mirror::Class>& lock) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 4254 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4255 | while (true) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4256 | self->AssertNoPendingException(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4257 | CHECK(!klass->IsInitialized()); |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 4258 | lock.WaitIgnoringInterrupts(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4259 | |
| 4260 | // When we wake up, repeat the test for init-in-progress. If |
| 4261 | // there's an exception pending (only possible if |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4262 | // we were not using WaitIgnoringInterrupts), bail out. |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4263 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4264 | WrapExceptionInInitializer(klass); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4265 | klass->SetStatus(mirror::Class::kStatusError, self); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4266 | return false; |
| 4267 | } |
| 4268 | // Spurious wakeup? Go back to waiting. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4269 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4270 | continue; |
| 4271 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4272 | if (klass->GetStatus() == mirror::Class::kStatusVerified && Runtime::Current()->IsCompiler()) { |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 4273 | // Compile time initialization failed. |
| 4274 | return false; |
| 4275 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4276 | if (klass->IsErroneous()) { |
| 4277 | // The caller wants an exception, but it was thrown in a |
| 4278 | // different thread. Synthesize one here. |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 4279 | ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread", |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4280 | PrettyDescriptor(klass.Get()).c_str()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4281 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4282 | return false; |
| 4283 | } |
| 4284 | if (klass->IsInitialized()) { |
| 4285 | return true; |
| 4286 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4287 | LOG(FATAL) << "Unexpected class status. " << PrettyClass(klass.Get()) << " is " |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4288 | << klass->GetStatus(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4289 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 4290 | UNREACHABLE(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4291 | } |
| 4292 | |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4293 | static bool HasSameSignatureWithDifferentClassLoaders(Thread* self, |
| 4294 | Handle<mirror::ArtMethod> method1, |
| 4295 | Handle<mirror::ArtMethod> method2) |
| 4296 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4297 | { |
| 4298 | StackHandleScope<1> hs(self); |
| 4299 | Handle<mirror::Class> return_type(hs.NewHandle(method1->GetReturnType())); |
| 4300 | if (UNLIKELY(method2->GetReturnType() != return_type.Get())) { |
| 4301 | return false; |
| 4302 | } |
| 4303 | } |
| 4304 | const DexFile::TypeList* types1 = method1->GetParameterTypeList(); |
| 4305 | const DexFile::TypeList* types2 = method2->GetParameterTypeList(); |
| 4306 | if (types1 == nullptr) { |
| 4307 | return (types2 == nullptr) || (types2->Size() == 0); |
| 4308 | } else if (UNLIKELY(types2 == nullptr)) { |
| 4309 | return types1->Size() == 0; |
| 4310 | } |
| 4311 | uint32_t num_types = types1->Size(); |
| 4312 | if (UNLIKELY(num_types != types2->Size())) { |
| 4313 | return false; |
| 4314 | } |
| 4315 | for (uint32_t i = 0; i < num_types; ++i) { |
| 4316 | mirror::Class* param_type = |
| 4317 | method1->GetClassFromTypeIndex(types1->GetTypeItem(i).type_idx_, true); |
| 4318 | mirror::Class* other_param_type = |
| 4319 | method2->GetClassFromTypeIndex(types2->GetTypeItem(i).type_idx_, true); |
| 4320 | if (UNLIKELY(param_type != other_param_type)) { |
| 4321 | return false; |
| 4322 | } |
| 4323 | } |
| 4324 | return true; |
| 4325 | } |
| 4326 | |
| 4327 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4328 | bool ClassLinker::ValidateSuperClassDescriptors(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4329 | if (klass->IsInterface()) { |
| 4330 | return true; |
| 4331 | } |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 4332 | // Begin with the methods local to the superclass. |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 4333 | Thread* self = Thread::Current(); |
| 4334 | StackHandleScope<2> hs(self); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4335 | MutableHandle<mirror::ArtMethod> h_m(hs.NewHandle<mirror::ArtMethod>(nullptr)); |
| 4336 | MutableHandle<mirror::ArtMethod> super_h_m(hs.NewHandle<mirror::ArtMethod>(nullptr)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4337 | if (klass->HasSuperClass() && |
| 4338 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4339 | for (int i = klass->GetSuperClass()->GetVTableLength() - 1; i >= 0; --i) { |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4340 | h_m.Assign(klass->GetVTableEntry(i)); |
| 4341 | super_h_m.Assign(klass->GetSuperClass()->GetVTableEntry(i)); |
| 4342 | if (h_m.Get() != super_h_m.Get() && |
| 4343 | !HasSameSignatureWithDifferentClassLoaders(self, h_m, super_h_m)) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4344 | ThrowLinkageError(klass.Get(), |
| 4345 | "Class %s method %s resolves differently in superclass %s", |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4346 | PrettyDescriptor(klass.Get()).c_str(), |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4347 | PrettyMethod(h_m.Get()).c_str(), |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 4348 | PrettyDescriptor(klass->GetSuperClass()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4349 | return false; |
| 4350 | } |
| 4351 | } |
| 4352 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 4353 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 4354 | if (klass->GetClassLoader() != klass->GetIfTable()->GetInterface(i)->GetClassLoader()) { |
| 4355 | uint32_t num_methods = klass->GetIfTable()->GetInterface(i)->NumVirtualMethods(); |
| 4356 | for (uint32_t j = 0; j < num_methods; ++j) { |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4357 | h_m.Assign(klass->GetIfTable()->GetMethodArray(i)->GetWithoutChecks(j)); |
| 4358 | super_h_m.Assign(klass->GetIfTable()->GetInterface(i)->GetVirtualMethod(j)); |
| 4359 | if (h_m.Get() != super_h_m.Get() && |
| 4360 | !HasSameSignatureWithDifferentClassLoaders(self, h_m, super_h_m)) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4361 | ThrowLinkageError(klass.Get(), |
| 4362 | "Class %s method %s resolves differently in interface %s", |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4363 | PrettyDescriptor(klass.Get()).c_str(), |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4364 | PrettyMethod(h_m.Get()).c_str(), |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 4365 | PrettyDescriptor(klass->GetIfTable()->GetInterface(i)).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4366 | return false; |
| 4367 | } |
| 4368 | } |
| 4369 | } |
| 4370 | } |
| 4371 | return true; |
| 4372 | } |
| 4373 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4374 | bool ClassLinker::EnsureInitialized(Thread* self, Handle<mirror::Class> c, bool can_init_fields, |
| 4375 | bool can_init_parents) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4376 | DCHECK(c.Get() != nullptr); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4377 | if (c->IsInitialized()) { |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4378 | EnsurePreverifiedMethods(c); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4379 | return true; |
| 4380 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4381 | const bool success = InitializeClass(self, c, can_init_fields, can_init_parents); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4382 | if (!success) { |
| 4383 | if (can_init_fields && can_init_parents) { |
| 4384 | CHECK(self->IsExceptionPending()) << PrettyClass(c.Get()); |
| 4385 | } |
| 4386 | } else { |
| 4387 | self->AssertNoPendingException(); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 4388 | } |
| 4389 | return success; |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 4390 | } |
| 4391 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4392 | void ClassLinker::FixupTemporaryDeclaringClass(mirror::Class* temp_class, mirror::Class* new_class) { |
| 4393 | mirror::ObjectArray<mirror::ArtField>* fields = new_class->GetIFields(); |
| 4394 | if (fields != nullptr) { |
| 4395 | for (int index = 0; index < fields->GetLength(); index ++) { |
| 4396 | if (fields->Get(index)->GetDeclaringClass() == temp_class) { |
| 4397 | fields->Get(index)->SetDeclaringClass(new_class); |
| 4398 | } |
| 4399 | } |
| 4400 | } |
| 4401 | |
| 4402 | fields = new_class->GetSFields(); |
| 4403 | if (fields != nullptr) { |
| 4404 | for (int index = 0; index < fields->GetLength(); index ++) { |
| 4405 | if (fields->Get(index)->GetDeclaringClass() == temp_class) { |
| 4406 | fields->Get(index)->SetDeclaringClass(new_class); |
| 4407 | } |
| 4408 | } |
| 4409 | } |
| 4410 | |
| 4411 | mirror::ObjectArray<mirror::ArtMethod>* methods = new_class->GetDirectMethods(); |
| 4412 | if (methods != nullptr) { |
| 4413 | for (int index = 0; index < methods->GetLength(); index ++) { |
| 4414 | if (methods->Get(index)->GetDeclaringClass() == temp_class) { |
| 4415 | methods->Get(index)->SetDeclaringClass(new_class); |
| 4416 | } |
| 4417 | } |
| 4418 | } |
| 4419 | |
| 4420 | methods = new_class->GetVirtualMethods(); |
| 4421 | if (methods != nullptr) { |
| 4422 | for (int index = 0; index < methods->GetLength(); index ++) { |
| 4423 | if (methods->Get(index)->GetDeclaringClass() == temp_class) { |
| 4424 | methods->Get(index)->SetDeclaringClass(new_class); |
| 4425 | } |
| 4426 | } |
| 4427 | } |
| 4428 | } |
| 4429 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4430 | bool ClassLinker::LinkClass(Thread* self, const char* descriptor, Handle<mirror::Class> klass, |
| 4431 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4432 | mirror::Class** new_class) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4433 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4434 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4435 | if (!LinkSuperClass(klass)) { |
| 4436 | return false; |
| 4437 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4438 | StackHandleScope<mirror::Class::kImtSize> imt_handle_scope( |
| 4439 | self, Runtime::Current()->GetImtUnimplementedMethod()); |
| 4440 | if (!LinkMethods(self, klass, interfaces, &imt_handle_scope)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4441 | return false; |
| 4442 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4443 | if (!LinkInstanceFields(self, klass)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4444 | return false; |
| 4445 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4446 | size_t class_size; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4447 | if (!LinkStaticFields(self, klass, &class_size)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 4448 | return false; |
| 4449 | } |
| 4450 | CreateReferenceInstanceOffsets(klass); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4451 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4452 | |
| 4453 | if (!klass->IsTemp() || (!init_done_ && klass->GetClassSize() == class_size)) { |
| 4454 | // We don't need to retire this class as it has no embedded tables or it was created the |
| 4455 | // correct size during class linker initialization. |
| 4456 | CHECK_EQ(klass->GetClassSize(), class_size) << PrettyDescriptor(klass.Get()); |
| 4457 | |
| 4458 | if (klass->ShouldHaveEmbeddedImtAndVTable()) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4459 | klass->PopulateEmbeddedImtAndVTable(&imt_handle_scope); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4460 | } |
| 4461 | |
| 4462 | // This will notify waiters on klass that saw the not yet resolved |
| 4463 | // class in the class_table_ during EnsureResolved. |
| 4464 | klass->SetStatus(mirror::Class::kStatusResolved, self); |
| 4465 | *new_class = klass.Get(); |
| 4466 | } else { |
| 4467 | CHECK(!klass->IsResolved()); |
| 4468 | // Retire the temporary class and create the correctly sized resolved class. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4469 | *new_class = klass->CopyOf(self, class_size, &imt_handle_scope); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4470 | if (UNLIKELY(*new_class == nullptr)) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4471 | CHECK(self->IsExceptionPending()); // Expect an OOME. |
| 4472 | klass->SetStatus(mirror::Class::kStatusError, self); |
| 4473 | return false; |
| 4474 | } |
| 4475 | |
| 4476 | CHECK_EQ((*new_class)->GetClassSize(), class_size); |
| 4477 | StackHandleScope<1> hs(self); |
| 4478 | auto new_class_h = hs.NewHandleWrapper<mirror::Class>(new_class); |
| 4479 | ObjectLock<mirror::Class> lock(self, new_class_h); |
| 4480 | |
| 4481 | FixupTemporaryDeclaringClass(klass.Get(), new_class_h.Get()); |
| 4482 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 4483 | mirror::Class* existing = UpdateClass(descriptor, new_class_h.Get(), |
| 4484 | ComputeModifiedUtf8Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4485 | CHECK(existing == nullptr || existing == klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4486 | |
| 4487 | // This will notify waiters on temp class that saw the not yet resolved class in the |
| 4488 | // class_table_ during EnsureResolved. |
| 4489 | klass->SetStatus(mirror::Class::kStatusRetired, self); |
| 4490 | |
| 4491 | CHECK_EQ(new_class_h->GetStatus(), mirror::Class::kStatusResolving); |
| 4492 | // This will notify waiters on new_class that saw the not yet resolved |
| 4493 | // class in the class_table_ during EnsureResolved. |
| 4494 | new_class_h->SetStatus(mirror::Class::kStatusResolved, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4495 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4496 | return true; |
| 4497 | } |
| 4498 | |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4499 | static void CountMethodsAndFields(ClassDataItemIterator& dex_data, |
| 4500 | size_t* virtual_methods, |
| 4501 | size_t* direct_methods, |
| 4502 | size_t* static_fields, |
| 4503 | size_t* instance_fields) { |
| 4504 | *virtual_methods = *direct_methods = *static_fields = *instance_fields = 0; |
| 4505 | |
| 4506 | while (dex_data.HasNextStaticField()) { |
| 4507 | dex_data.Next(); |
| 4508 | (*static_fields)++; |
| 4509 | } |
| 4510 | while (dex_data.HasNextInstanceField()) { |
| 4511 | dex_data.Next(); |
| 4512 | (*instance_fields)++; |
| 4513 | } |
| 4514 | while (dex_data.HasNextDirectMethod()) { |
| 4515 | (*direct_methods)++; |
| 4516 | dex_data.Next(); |
| 4517 | } |
| 4518 | while (dex_data.HasNextVirtualMethod()) { |
| 4519 | (*virtual_methods)++; |
| 4520 | dex_data.Next(); |
| 4521 | } |
| 4522 | DCHECK(!dex_data.HasNext()); |
| 4523 | } |
| 4524 | |
| 4525 | static void DumpClass(std::ostream& os, |
| 4526 | const DexFile& dex_file, const DexFile::ClassDef& dex_class_def, |
| 4527 | const char* suffix) { |
| 4528 | ClassDataItemIterator dex_data(dex_file, dex_file.GetClassData(dex_class_def)); |
| 4529 | os << dex_file.GetClassDescriptor(dex_class_def) << suffix << ":\n"; |
| 4530 | os << " Static fields:\n"; |
| 4531 | while (dex_data.HasNextStaticField()) { |
| 4532 | const DexFile::FieldId& id = dex_file.GetFieldId(dex_data.GetMemberIndex()); |
| 4533 | os << " " << dex_file.GetFieldTypeDescriptor(id) << " " << dex_file.GetFieldName(id) << "\n"; |
| 4534 | dex_data.Next(); |
| 4535 | } |
| 4536 | os << " Instance fields:\n"; |
| 4537 | while (dex_data.HasNextInstanceField()) { |
| 4538 | const DexFile::FieldId& id = dex_file.GetFieldId(dex_data.GetMemberIndex()); |
| 4539 | os << " " << dex_file.GetFieldTypeDescriptor(id) << " " << dex_file.GetFieldName(id) << "\n"; |
| 4540 | dex_data.Next(); |
| 4541 | } |
| 4542 | os << " Direct methods:\n"; |
| 4543 | while (dex_data.HasNextDirectMethod()) { |
| 4544 | const DexFile::MethodId& id = dex_file.GetMethodId(dex_data.GetMemberIndex()); |
| 4545 | os << " " << dex_file.GetMethodName(id) << dex_file.GetMethodSignature(id).ToString() << "\n"; |
| 4546 | dex_data.Next(); |
| 4547 | } |
| 4548 | os << " Virtual methods:\n"; |
| 4549 | while (dex_data.HasNextVirtualMethod()) { |
| 4550 | const DexFile::MethodId& id = dex_file.GetMethodId(dex_data.GetMemberIndex()); |
| 4551 | os << " " << dex_file.GetMethodName(id) << dex_file.GetMethodSignature(id).ToString() << "\n"; |
| 4552 | dex_data.Next(); |
| 4553 | } |
| 4554 | } |
| 4555 | |
| 4556 | static std::string DumpClasses(const DexFile& dex_file1, const DexFile::ClassDef& dex_class_def1, |
| 4557 | const DexFile& dex_file2, const DexFile::ClassDef& dex_class_def2) { |
| 4558 | std::ostringstream os; |
| 4559 | DumpClass(os, dex_file1, dex_class_def1, " (Compile time)"); |
| 4560 | DumpClass(os, dex_file2, dex_class_def2, " (Runtime)"); |
| 4561 | return os.str(); |
| 4562 | } |
| 4563 | |
| 4564 | |
| 4565 | // Very simple structural check on whether the classes match. Only compares the number of |
| 4566 | // methods and fields. |
| 4567 | static bool SimpleStructuralCheck(const DexFile& dex_file1, const DexFile::ClassDef& dex_class_def1, |
| 4568 | const DexFile& dex_file2, const DexFile::ClassDef& dex_class_def2, |
| 4569 | std::string* error_msg) { |
| 4570 | ClassDataItemIterator dex_data1(dex_file1, dex_file1.GetClassData(dex_class_def1)); |
| 4571 | ClassDataItemIterator dex_data2(dex_file2, dex_file2.GetClassData(dex_class_def2)); |
| 4572 | |
| 4573 | // Counters for current dex file. |
| 4574 | size_t dex_virtual_methods1, dex_direct_methods1, dex_static_fields1, dex_instance_fields1; |
| 4575 | CountMethodsAndFields(dex_data1, &dex_virtual_methods1, &dex_direct_methods1, &dex_static_fields1, |
| 4576 | &dex_instance_fields1); |
| 4577 | // Counters for compile-time dex file. |
| 4578 | size_t dex_virtual_methods2, dex_direct_methods2, dex_static_fields2, dex_instance_fields2; |
| 4579 | CountMethodsAndFields(dex_data2, &dex_virtual_methods2, &dex_direct_methods2, &dex_static_fields2, |
| 4580 | &dex_instance_fields2); |
| 4581 | |
| 4582 | if (dex_virtual_methods1 != dex_virtual_methods2) { |
| 4583 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
| 4584 | *error_msg = StringPrintf("Virtual method count off: %zu vs %zu\n%s", dex_virtual_methods1, |
| 4585 | dex_virtual_methods2, class_dump.c_str()); |
| 4586 | return false; |
| 4587 | } |
| 4588 | if (dex_direct_methods1 != dex_direct_methods2) { |
| 4589 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
| 4590 | *error_msg = StringPrintf("Direct method count off: %zu vs %zu\n%s", dex_direct_methods1, |
| 4591 | dex_direct_methods2, class_dump.c_str()); |
| 4592 | return false; |
| 4593 | } |
| 4594 | if (dex_static_fields1 != dex_static_fields2) { |
| 4595 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
| 4596 | *error_msg = StringPrintf("Static field count off: %zu vs %zu\n%s", dex_static_fields1, |
| 4597 | dex_static_fields2, class_dump.c_str()); |
| 4598 | return false; |
| 4599 | } |
| 4600 | if (dex_instance_fields1 != dex_instance_fields2) { |
| 4601 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
| 4602 | *error_msg = StringPrintf("Instance field count off: %zu vs %zu\n%s", dex_instance_fields1, |
| 4603 | dex_instance_fields2, class_dump.c_str()); |
| 4604 | return false; |
| 4605 | } |
| 4606 | |
| 4607 | return true; |
| 4608 | } |
| 4609 | |
| 4610 | // Checks whether a the super-class changed from what we had at compile-time. This would |
| 4611 | // invalidate quickening. |
| 4612 | static bool CheckSuperClassChange(Handle<mirror::Class> klass, |
| 4613 | const DexFile& dex_file, |
| 4614 | const DexFile::ClassDef& class_def, |
| 4615 | mirror::Class* super_class) |
| 4616 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4617 | // Check for unexpected changes in the superclass. |
| 4618 | // Quick check 1) is the super_class class-loader the boot class loader? This always has |
| 4619 | // precedence. |
| 4620 | if (super_class->GetClassLoader() != nullptr && |
| 4621 | // Quick check 2) different dex cache? Breaks can only occur for different dex files, |
| 4622 | // which is implied by different dex cache. |
| 4623 | klass->GetDexCache() != super_class->GetDexCache()) { |
| 4624 | // Now comes the expensive part: things can be broken if (a) the klass' dex file has a |
| 4625 | // definition for the super-class, and (b) the files are in separate oat files. The oat files |
| 4626 | // are referenced from the dex file, so do (b) first. Only relevant if we have oat files. |
| 4627 | const OatFile* class_oat_file = dex_file.GetOatFile(); |
| 4628 | if (class_oat_file != nullptr) { |
| 4629 | const OatFile* loaded_super_oat_file = super_class->GetDexFile().GetOatFile(); |
| 4630 | if (loaded_super_oat_file != nullptr && class_oat_file != loaded_super_oat_file) { |
| 4631 | // Now check (a). |
| 4632 | const DexFile::ClassDef* super_class_def = dex_file.FindClassDef(class_def.superclass_idx_); |
| 4633 | if (super_class_def != nullptr) { |
| 4634 | // Uh-oh, we found something. Do our check. |
| 4635 | std::string error_msg; |
| 4636 | if (!SimpleStructuralCheck(dex_file, *super_class_def, |
| 4637 | super_class->GetDexFile(), *super_class->GetClassDef(), |
| 4638 | &error_msg)) { |
| 4639 | // Print a warning to the log. This exception might be caught, e.g., as common in test |
| 4640 | // drivers. When the class is later tried to be used, we re-throw a new instance, as we |
| 4641 | // only save the type of the exception. |
| 4642 | LOG(WARNING) << "Incompatible structural change detected: " << |
| 4643 | StringPrintf( |
| 4644 | "Structural change of %s is hazardous (%s at compile time, %s at runtime): %s", |
| 4645 | PrettyType(super_class_def->class_idx_, dex_file).c_str(), |
| 4646 | class_oat_file->GetLocation().c_str(), |
| 4647 | loaded_super_oat_file->GetLocation().c_str(), |
| 4648 | error_msg.c_str()); |
| 4649 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 4650 | "Structural change of %s is hazardous (%s at compile time, %s at runtime): %s", |
| 4651 | PrettyType(super_class_def->class_idx_, dex_file).c_str(), |
| 4652 | class_oat_file->GetLocation().c_str(), |
| 4653 | loaded_super_oat_file->GetLocation().c_str(), |
| 4654 | error_msg.c_str()); |
| 4655 | return false; |
| 4656 | } |
| 4657 | } |
| 4658 | } |
| 4659 | } |
| 4660 | } |
| 4661 | return true; |
| 4662 | } |
| 4663 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4664 | bool ClassLinker::LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4665 | CHECK_EQ(mirror::Class::kStatusIdx, klass->GetStatus()); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4666 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
| 4667 | uint16_t super_class_idx = class_def.superclass_idx_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4668 | if (super_class_idx != DexFile::kDexNoIndex16) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4669 | mirror::Class* super_class = ResolveType(dex_file, super_class_idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4670 | if (super_class == nullptr) { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 4671 | DCHECK(Thread::Current()->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4672 | return false; |
| 4673 | } |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 4674 | // Verify |
| 4675 | if (!klass->CanAccess(super_class)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4676 | ThrowIllegalAccessError(klass.Get(), "Class %s extended by class %s is inaccessible", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4677 | PrettyDescriptor(super_class).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4678 | PrettyDescriptor(klass.Get()).c_str()); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 4679 | return false; |
| 4680 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4681 | CHECK(super_class->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4682 | klass->SetSuperClass(super_class); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4683 | |
| 4684 | if (!CheckSuperClassChange(klass, dex_file, class_def, super_class)) { |
| 4685 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4686 | return false; |
| 4687 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4688 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4689 | const DexFile::TypeList* interfaces = dex_file.GetInterfacesList(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4690 | if (interfaces != nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4691 | for (size_t i = 0; i < interfaces->Size(); i++) { |
| 4692 | uint16_t idx = interfaces->GetTypeItem(i).type_idx_; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4693 | mirror::Class* interface = ResolveType(dex_file, idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4694 | if (interface == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4695 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4696 | return false; |
| 4697 | } |
| 4698 | // Verify |
| 4699 | if (!klass->CanAccess(interface)) { |
| 4700 | // TODO: the RI seemed to ignore this in my testing. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4701 | ThrowIllegalAccessError(klass.Get(), "Interface %s implemented by class %s is inaccessible", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4702 | PrettyDescriptor(interface).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4703 | PrettyDescriptor(klass.Get()).c_str()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4704 | return false; |
| 4705 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4706 | } |
| 4707 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4708 | // Mark the class as loaded. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4709 | klass->SetStatus(mirror::Class::kStatusLoaded, nullptr); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4710 | return true; |
| 4711 | } |
| 4712 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4713 | bool ClassLinker::LinkSuperClass(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4714 | CHECK(!klass->IsPrimitive()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4715 | mirror::Class* super = klass->GetSuperClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4716 | if (klass.Get() == GetClassRoot(kJavaLangObject)) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4717 | if (super != nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4718 | ThrowClassFormatError(klass.Get(), "java.lang.Object must not have a superclass"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4719 | return false; |
| 4720 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4721 | return true; |
| 4722 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4723 | if (super == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4724 | ThrowLinkageError(klass.Get(), "No superclass defined for class %s", |
| 4725 | PrettyDescriptor(klass.Get()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4726 | return false; |
| 4727 | } |
| 4728 | // Verify |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 4729 | if (super->IsFinal() || super->IsInterface()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4730 | ThrowIncompatibleClassChangeError(klass.Get(), "Superclass %s of %s is %s", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4731 | PrettyDescriptor(super).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4732 | PrettyDescriptor(klass.Get()).c_str(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4733 | super->IsFinal() ? "declared final" : "an interface"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4734 | return false; |
| 4735 | } |
| 4736 | if (!klass->CanAccess(super)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4737 | ThrowIllegalAccessError(klass.Get(), "Superclass %s is inaccessible to class %s", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4738 | PrettyDescriptor(super).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4739 | PrettyDescriptor(klass.Get()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4740 | return false; |
| 4741 | } |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 4742 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4743 | // Inherit kAccClassIsFinalizable from the superclass in case this |
| 4744 | // class doesn't override finalize. |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 4745 | if (super->IsFinalizable()) { |
| 4746 | klass->SetFinalizable(); |
| 4747 | } |
| 4748 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 4749 | // Inherit reference flags (if any) from the superclass. |
| 4750 | int reference_flags = (super->GetAccessFlags() & kAccReferenceFlagsMask); |
| 4751 | if (reference_flags != 0) { |
| 4752 | klass->SetAccessFlags(klass->GetAccessFlags() | reference_flags); |
| 4753 | } |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 4754 | // Disallow custom direct subclasses of java.lang.ref.Reference. |
Elliott Hughes | bf61ba3 | 2011-10-11 10:53:09 -0700 | [diff] [blame] | 4755 | if (init_done_ && super == GetClassRoot(kJavaLangRefReference)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4756 | ThrowLinkageError(klass.Get(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4757 | "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] | 4758 | PrettyDescriptor(klass.Get()).c_str()); |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 4759 | return false; |
| 4760 | } |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 4761 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4762 | if (kIsDebugBuild) { |
| 4763 | // Ensure super classes are fully resolved prior to resolving fields.. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4764 | while (super != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4765 | CHECK(super->IsResolved()); |
| 4766 | super = super->GetSuperClass(); |
| 4767 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4768 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4769 | return true; |
| 4770 | } |
| 4771 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4772 | // Populate the class vtable and itable. Compute return type indices. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4773 | bool ClassLinker::LinkMethods(Thread* self, Handle<mirror::Class> klass, |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4774 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
| 4775 | StackHandleScope<mirror::Class::kImtSize>* out_imt) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4776 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4777 | if (klass->IsInterface()) { |
| 4778 | // No vtable. |
| 4779 | size_t count = klass->NumVirtualMethods(); |
| 4780 | if (!IsUint(16, count)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4781 | ThrowClassFormatError(klass.Get(), "Too many methods on interface: %zd", count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4782 | return false; |
| 4783 | } |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 4784 | for (size_t i = 0; i < count; ++i) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4785 | klass->GetVirtualMethodDuringLinking(i)->SetMethodIndex(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4786 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4787 | } else if (!LinkVirtualMethods(self, klass)) { // Link virtual methods first. |
| 4788 | return false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4789 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4790 | return LinkInterfaceMethods(self, klass, interfaces, out_imt); // Link interface method last. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4791 | } |
| 4792 | |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 4793 | // Comparator for name and signature of a method, used in finding overriding methods. Implementation |
| 4794 | // avoids the use of handles, if it didn't then rather than compare dex files we could compare dex |
| 4795 | // caches in the implementation below. |
| 4796 | class MethodNameAndSignatureComparator FINAL : public ValueObject { |
| 4797 | public: |
| 4798 | explicit MethodNameAndSignatureComparator(mirror::ArtMethod* method) |
| 4799 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) : |
| 4800 | dex_file_(method->GetDexFile()), mid_(&dex_file_->GetMethodId(method->GetDexMethodIndex())), |
| 4801 | name_(nullptr), name_len_(0) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4802 | DCHECK(!method->IsProxyMethod()) << PrettyMethod(method); |
| 4803 | } |
| 4804 | |
| 4805 | const char* GetName() { |
| 4806 | if (name_ == nullptr) { |
| 4807 | name_ = dex_file_->StringDataAndUtf16LengthByIdx(mid_->name_idx_, &name_len_); |
| 4808 | } |
| 4809 | return name_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 4810 | } |
| 4811 | |
| 4812 | bool HasSameNameAndSignature(mirror::ArtMethod* other) |
| 4813 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4814 | DCHECK(!other->IsProxyMethod()) << PrettyMethod(other); |
| 4815 | const DexFile* other_dex_file = other->GetDexFile(); |
| 4816 | const DexFile::MethodId& other_mid = other_dex_file->GetMethodId(other->GetDexMethodIndex()); |
| 4817 | if (dex_file_ == other_dex_file) { |
| 4818 | return mid_->name_idx_ == other_mid.name_idx_ && mid_->proto_idx_ == other_mid.proto_idx_; |
| 4819 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4820 | GetName(); // Only used to make sure its calculated. |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 4821 | uint32_t other_name_len; |
| 4822 | const char* other_name = other_dex_file->StringDataAndUtf16LengthByIdx(other_mid.name_idx_, |
| 4823 | &other_name_len); |
| 4824 | if (name_len_ != other_name_len || strcmp(name_, other_name) != 0) { |
| 4825 | return false; |
| 4826 | } |
| 4827 | return dex_file_->GetMethodSignature(*mid_) == other_dex_file->GetMethodSignature(other_mid); |
| 4828 | } |
| 4829 | |
| 4830 | private: |
| 4831 | // Dex file for the method to compare against. |
| 4832 | const DexFile* const dex_file_; |
| 4833 | // MethodId for the method to compare against. |
| 4834 | const DexFile::MethodId* const mid_; |
| 4835 | // Lazily computed name from the dex file's strings. |
| 4836 | const char* name_; |
| 4837 | // Lazily computed name length. |
| 4838 | uint32_t name_len_; |
| 4839 | }; |
| 4840 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4841 | class LinkVirtualHashTable { |
| 4842 | public: |
| 4843 | LinkVirtualHashTable(Handle<mirror::Class> klass, size_t hash_size, uint32_t* hash_table) |
| 4844 | : klass_(klass), hash_size_(hash_size), hash_table_(hash_table) { |
| 4845 | std::fill(hash_table_, hash_table_ + hash_size_, invalid_index_); |
| 4846 | } |
| 4847 | void Add(uint32_t virtual_method_index) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4848 | mirror::ArtMethod* local_method = klass_->GetVirtualMethodDuringLinking(virtual_method_index); |
| 4849 | const char* name = local_method->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 4850 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4851 | uint32_t index = hash % hash_size_; |
| 4852 | // Linear probe until we have an empty slot. |
| 4853 | while (hash_table_[index] != invalid_index_) { |
| 4854 | if (++index == hash_size_) { |
| 4855 | index = 0; |
| 4856 | } |
| 4857 | } |
| 4858 | hash_table_[index] = virtual_method_index; |
| 4859 | } |
| 4860 | uint32_t FindAndRemove(MethodNameAndSignatureComparator* comparator) |
| 4861 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4862 | const char* name = comparator->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 4863 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4864 | size_t index = hash % hash_size_; |
| 4865 | while (true) { |
| 4866 | const uint32_t value = hash_table_[index]; |
| 4867 | // Since linear probe makes continuous blocks, hitting an invalid index means we are done |
| 4868 | // the block and can safely assume not found. |
| 4869 | if (value == invalid_index_) { |
| 4870 | break; |
| 4871 | } |
| 4872 | if (value != removed_index_) { // This signifies not already overriden. |
| 4873 | mirror::ArtMethod* virtual_method = |
| 4874 | klass_->GetVirtualMethodDuringLinking(value); |
| 4875 | if (comparator->HasSameNameAndSignature(virtual_method->GetInterfaceMethodIfProxy())) { |
| 4876 | hash_table_[index] = removed_index_; |
| 4877 | return value; |
| 4878 | } |
| 4879 | } |
| 4880 | if (++index == hash_size_) { |
| 4881 | index = 0; |
| 4882 | } |
| 4883 | } |
| 4884 | return GetNotFoundIndex(); |
| 4885 | } |
| 4886 | static uint32_t GetNotFoundIndex() { |
| 4887 | return invalid_index_; |
| 4888 | } |
| 4889 | |
| 4890 | private: |
| 4891 | static const uint32_t invalid_index_; |
| 4892 | static const uint32_t removed_index_; |
| 4893 | |
| 4894 | Handle<mirror::Class> klass_; |
| 4895 | const size_t hash_size_; |
| 4896 | uint32_t* const hash_table_; |
| 4897 | }; |
| 4898 | |
| 4899 | const uint32_t LinkVirtualHashTable::invalid_index_ = std::numeric_limits<uint32_t>::max(); |
| 4900 | const uint32_t LinkVirtualHashTable::removed_index_ = std::numeric_limits<uint32_t>::max() - 1; |
| 4901 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4902 | bool ClassLinker::LinkVirtualMethods(Thread* self, Handle<mirror::Class> klass) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4903 | const size_t num_virtual_methods = klass->NumVirtualMethods(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4904 | if (klass->HasSuperClass()) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4905 | const size_t super_vtable_length = klass->GetSuperClass()->GetVTableLength(); |
| 4906 | const size_t max_count = num_virtual_methods + super_vtable_length; |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4907 | StackHandleScope<2> hs(self); |
Mingyao Yang | 38eecb0 | 2014-08-13 14:51:03 -0700 | [diff] [blame] | 4908 | Handle<mirror::Class> super_class(hs.NewHandle(klass->GetSuperClass())); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4909 | MutableHandle<mirror::ObjectArray<mirror::ArtMethod>> vtable; |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4910 | if (super_class->ShouldHaveEmbeddedImtAndVTable()) { |
| 4911 | vtable = hs.NewHandle(AllocArtMethodArray(self, max_count)); |
| 4912 | if (UNLIKELY(vtable.Get() == nullptr)) { |
| 4913 | CHECK(self->IsExceptionPending()); // OOME. |
| 4914 | return false; |
| 4915 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4916 | for (size_t i = 0; i < super_vtable_length; i++) { |
| 4917 | vtable->SetWithoutChecks<false>(i, super_class->GetEmbeddedVTableEntry(i)); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4918 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4919 | if (num_virtual_methods == 0) { |
| 4920 | klass->SetVTable(vtable.Get()); |
| 4921 | return true; |
| 4922 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4923 | } else { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4924 | mirror::ObjectArray<mirror::ArtMethod>* super_vtable = super_class->GetVTable(); |
| 4925 | CHECK(super_vtable != nullptr) << PrettyClass(super_class.Get()); |
| 4926 | if (num_virtual_methods == 0) { |
| 4927 | klass->SetVTable(super_vtable); |
| 4928 | return true; |
| 4929 | } |
| 4930 | vtable = hs.NewHandle(super_vtable->CopyOf(self, max_count)); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4931 | if (UNLIKELY(vtable.Get() == nullptr)) { |
| 4932 | CHECK(self->IsExceptionPending()); // OOME. |
| 4933 | return false; |
| 4934 | } |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4935 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4936 | // How the algorithm works: |
| 4937 | // 1. Populate hash table by adding num_virtual_methods from klass. The values in the hash |
| 4938 | // table are: invalid_index for unused slots, index super_vtable_length + i for a virtual |
| 4939 | // method which has not been matched to a vtable method, and j if the virtual method at the |
| 4940 | // index overrode the super virtual method at index j. |
| 4941 | // 2. Loop through super virtual methods, if they overwrite, update hash table to j |
| 4942 | // (j < super_vtable_length) to avoid redundant checks. (TODO maybe use this info for reducing |
| 4943 | // the need for the initial vtable which we later shrink back down). |
| 4944 | // 3. Add non overridden methods to the end of the vtable. |
| 4945 | static constexpr size_t kMaxStackHash = 250; |
| 4946 | const size_t hash_table_size = num_virtual_methods * 3; |
| 4947 | uint32_t* hash_table_ptr; |
| 4948 | std::unique_ptr<uint32_t[]> hash_heap_storage; |
| 4949 | if (hash_table_size <= kMaxStackHash) { |
| 4950 | hash_table_ptr = reinterpret_cast<uint32_t*>( |
| 4951 | alloca(hash_table_size * sizeof(*hash_table_ptr))); |
| 4952 | } else { |
| 4953 | hash_heap_storage.reset(new uint32_t[hash_table_size]); |
| 4954 | hash_table_ptr = hash_heap_storage.get(); |
| 4955 | } |
| 4956 | LinkVirtualHashTable hash_table(klass, hash_table_size, hash_table_ptr); |
| 4957 | // Add virtual methods to the hash table. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4958 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4959 | hash_table.Add(i); |
| 4960 | } |
| 4961 | // Loop through each super vtable method and see if they are overriden by a method we added to |
| 4962 | // the hash table. |
| 4963 | for (size_t j = 0; j < super_vtable_length; ++j) { |
| 4964 | // Search the hash table to see if we are overidden by any method. |
| 4965 | mirror::ArtMethod* super_method = vtable->GetWithoutChecks(j); |
| 4966 | MethodNameAndSignatureComparator super_method_name_comparator( |
| 4967 | super_method->GetInterfaceMethodIfProxy()); |
| 4968 | uint32_t hash_index = hash_table.FindAndRemove(&super_method_name_comparator); |
| 4969 | if (hash_index != hash_table.GetNotFoundIndex()) { |
| 4970 | mirror::ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(hash_index); |
| 4971 | if (klass->CanAccessMember(super_method->GetDeclaringClass(), |
| 4972 | super_method->GetAccessFlags())) { |
| 4973 | if (super_method->IsFinal()) { |
| 4974 | ThrowLinkageError(klass.Get(), "Method %s overrides final method in class %s", |
| 4975 | PrettyMethod(virtual_method).c_str(), |
| 4976 | super_method->GetDeclaringClassDescriptor()); |
| 4977 | return false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4978 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4979 | vtable->SetWithoutChecks<false>(j, virtual_method); |
| 4980 | virtual_method->SetMethodIndex(j); |
| 4981 | } else { |
| 4982 | LOG(WARNING) << "Before Android 4.1, method " << PrettyMethod(virtual_method) |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4983 | << " would have incorrectly overridden the package-private method in " |
| 4984 | << PrettyDescriptor(super_method->GetDeclaringClassDescriptor()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4985 | } |
| 4986 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4987 | } |
| 4988 | // Add the non overridden methods at the end. |
| 4989 | size_t actual_count = super_vtable_length; |
| 4990 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
| 4991 | mirror::ArtMethod* local_method = klass->GetVirtualMethodDuringLinking(i); |
| 4992 | size_t method_idx = local_method->GetMethodIndexDuringLinking(); |
| 4993 | if (method_idx < super_vtable_length && |
| 4994 | local_method == vtable->GetWithoutChecks(method_idx)) { |
| 4995 | continue; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4996 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4997 | vtable->SetWithoutChecks<false>(actual_count, local_method); |
| 4998 | local_method->SetMethodIndex(actual_count); |
| 4999 | ++actual_count; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5000 | } |
| 5001 | if (!IsUint(16, actual_count)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5002 | ThrowClassFormatError(klass.Get(), "Too many methods defined on class: %zd", actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5003 | return false; |
| 5004 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5005 | // Shrink vtable if possible |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5006 | CHECK_LE(actual_count, max_count); |
| 5007 | if (actual_count < max_count) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5008 | vtable.Assign(vtable->CopyOf(self, actual_count)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5009 | if (UNLIKELY(vtable.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5010 | CHECK(self->IsExceptionPending()); // OOME. |
| 5011 | return false; |
| 5012 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5013 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5014 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5015 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 5016 | CHECK_EQ(klass.Get(), GetClassRoot(kJavaLangObject)); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 5017 | if (!IsUint(16, num_virtual_methods)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5018 | ThrowClassFormatError(klass.Get(), "Too many methods: %d", |
| 5019 | static_cast<int>(num_virtual_methods)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5020 | return false; |
| 5021 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5022 | mirror::ObjectArray<mirror::ArtMethod>* vtable = AllocArtMethodArray(self, num_virtual_methods); |
| 5023 | if (UNLIKELY(vtable == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5024 | CHECK(self->IsExceptionPending()); // OOME. |
| 5025 | return false; |
| 5026 | } |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 5027 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5028 | mirror::ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5029 | vtable->SetWithoutChecks<false>(i, virtual_method); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5030 | virtual_method->SetMethodIndex(i & 0xFFFF); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5031 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5032 | klass->SetVTable(vtable); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5033 | } |
| 5034 | return true; |
| 5035 | } |
| 5036 | |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5037 | bool ClassLinker::LinkInterfaceMethods(Thread* self, Handle<mirror::Class> klass, |
| 5038 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
| 5039 | StackHandleScope<mirror::Class::kImtSize>* out_imt) { |
Mathieu Chartier | d035c2d | 2014-10-27 17:30:20 -0700 | [diff] [blame] | 5040 | StackHandleScope<3> hs(self); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 5041 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5042 | const bool has_superclass = klass->HasSuperClass(); |
| 5043 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
| 5044 | const bool have_interfaces = interfaces.Get() != nullptr; |
| 5045 | const size_t num_interfaces = |
| 5046 | have_interfaces ? interfaces->GetLength() : klass->NumDirectInterfaces(); |
| 5047 | if (num_interfaces == 0) { |
| 5048 | if (super_ifcount == 0) { |
| 5049 | // Class implements no interfaces. |
| 5050 | DCHECK_EQ(klass->GetIfTableCount(), 0); |
| 5051 | DCHECK(klass->GetIfTable() == nullptr); |
| 5052 | return true; |
| 5053 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5054 | // Class implements same interfaces as parent, are any of these not marker interfaces? |
| 5055 | bool has_non_marker_interface = false; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5056 | mirror::IfTable* super_iftable = klass->GetSuperClass()->GetIfTable(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5057 | for (size_t i = 0; i < super_ifcount; ++i) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5058 | if (super_iftable->GetMethodArrayCount(i) > 0) { |
| 5059 | has_non_marker_interface = true; |
| 5060 | break; |
| 5061 | } |
| 5062 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5063 | // Class just inherits marker interfaces from parent so recycle parent's iftable. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5064 | if (!has_non_marker_interface) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5065 | klass->SetIfTable(super_iftable); |
| 5066 | return true; |
| 5067 | } |
| 5068 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5069 | size_t ifcount = super_ifcount + num_interfaces; |
| 5070 | for (size_t i = 0; i < num_interfaces; i++) { |
| 5071 | mirror::Class* interface = have_interfaces ? |
| 5072 | interfaces->GetWithoutChecks(i) : mirror::Class::GetDirectInterface(self, klass, i); |
| 5073 | DCHECK(interface != nullptr); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5074 | if (UNLIKELY(!interface->IsInterface())) { |
| 5075 | std::string temp; |
| 5076 | ThrowIncompatibleClassChangeError(klass.Get(), "Class %s implements non-interface class %s", |
| 5077 | PrettyDescriptor(klass.Get()).c_str(), |
| 5078 | PrettyDescriptor(interface->GetDescriptor(&temp)).c_str()); |
| 5079 | return false; |
| 5080 | } |
| 5081 | ifcount += interface->GetIfTableCount(); |
| 5082 | } |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5083 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(AllocIfTable(self, ifcount))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5084 | if (UNLIKELY(iftable.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5085 | CHECK(self->IsExceptionPending()); // OOME. |
| 5086 | return false; |
| 5087 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5088 | if (super_ifcount != 0) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5089 | mirror::IfTable* super_iftable = klass->GetSuperClass()->GetIfTable(); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 5090 | for (size_t i = 0; i < super_ifcount; i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5091 | mirror::Class* super_interface = super_iftable->GetInterface(i); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5092 | iftable->SetInterface(i, super_interface); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 5093 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5094 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5095 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5096 | // Flatten the interface inheritance hierarchy. |
| 5097 | size_t idx = super_ifcount; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5098 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | d035c2d | 2014-10-27 17:30:20 -0700 | [diff] [blame] | 5099 | mirror::Class* interface = have_interfaces ? interfaces->Get(i) : |
| 5100 | mirror::Class::GetDirectInterface(self, klass, i); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5101 | // Check if interface is already in iftable |
| 5102 | bool duplicate = false; |
| 5103 | for (size_t j = 0; j < idx; j++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5104 | mirror::Class* existing_interface = iftable->GetInterface(j); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5105 | if (existing_interface == interface) { |
| 5106 | duplicate = true; |
| 5107 | break; |
| 5108 | } |
| 5109 | } |
| 5110 | if (!duplicate) { |
| 5111 | // Add this non-duplicate interface. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5112 | iftable->SetInterface(idx++, interface); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5113 | // Add this interface's non-duplicate super-interfaces. |
| 5114 | for (int32_t j = 0; j < interface->GetIfTableCount(); j++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5115 | mirror::Class* super_interface = interface->GetIfTable()->GetInterface(j); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5116 | bool super_duplicate = false; |
| 5117 | for (size_t k = 0; k < idx; k++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5118 | mirror::Class* existing_interface = iftable->GetInterface(k); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5119 | if (existing_interface == super_interface) { |
| 5120 | super_duplicate = true; |
| 5121 | break; |
| 5122 | } |
| 5123 | } |
| 5124 | if (!super_duplicate) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5125 | iftable->SetInterface(idx++, super_interface); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5126 | } |
| 5127 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5128 | } |
| 5129 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5130 | self->AllowThreadSuspension(); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5131 | // Shrink iftable in case duplicates were found |
| 5132 | if (idx < ifcount) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5133 | DCHECK_NE(num_interfaces, 0U); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5134 | 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] | 5135 | if (UNLIKELY(iftable.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5136 | CHECK(self->IsExceptionPending()); // OOME. |
| 5137 | return false; |
| 5138 | } |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5139 | ifcount = idx; |
| 5140 | } else { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5141 | DCHECK_EQ(idx, ifcount); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5142 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5143 | klass->SetIfTable(iftable.Get()); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 5144 | // 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] | 5145 | if (klass->IsInterface()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5146 | return true; |
| 5147 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5148 | size_t miranda_list_size = 0; |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5149 | size_t max_miranda_methods = 0; // The max size of miranda_list. |
| 5150 | for (size_t i = 0; i < ifcount; ++i) { |
| 5151 | max_miranda_methods += iftable->GetInterface(i)->NumVirtualMethods(); |
| 5152 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5153 | MutableHandle<mirror::ObjectArray<mirror::ArtMethod>> |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5154 | miranda_list(hs.NewHandle(AllocArtMethodArray(self, max_miranda_methods))); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5155 | MutableHandle<mirror::ObjectArray<mirror::ArtMethod>> vtable( |
| 5156 | hs.NewHandle(klass->GetVTableDuringLinking())); |
| 5157 | // Copy the IMT from the super class if possible. |
| 5158 | bool extend_super_iftable = false; |
| 5159 | if (has_superclass) { |
| 5160 | mirror::Class* super_class = klass->GetSuperClass(); |
| 5161 | extend_super_iftable = true; |
| 5162 | if (super_class->ShouldHaveEmbeddedImtAndVTable()) { |
| 5163 | for (size_t i = 0; i < mirror::Class::kImtSize; ++i) { |
| 5164 | out_imt->SetReference(i, super_class->GetEmbeddedImTableEntry(i)); |
| 5165 | } |
| 5166 | } else { |
| 5167 | // No imt in the super class, need to reconstruct from the iftable. |
| 5168 | mirror::IfTable* if_table = super_class->GetIfTable(); |
| 5169 | mirror::ArtMethod* conflict_method = runtime->GetImtConflictMethod(); |
| 5170 | const size_t length = super_class->GetIfTableCount(); |
| 5171 | for (size_t i = 0; i < length; ++i) { |
| 5172 | mirror::Class* interface = iftable->GetInterface(i); |
| 5173 | const size_t num_virtuals = interface->NumVirtualMethods(); |
| 5174 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 5175 | DCHECK_EQ(num_virtuals, method_array_count); |
| 5176 | if (method_array_count == 0) { |
| 5177 | continue; |
| 5178 | } |
| 5179 | mirror::ObjectArray<mirror::ArtMethod>* method_array = if_table->GetMethodArray(i); |
| 5180 | for (size_t j = 0; j < num_virtuals; ++j) { |
| 5181 | mirror::ArtMethod* method = method_array->GetWithoutChecks(j); |
| 5182 | if (method->IsMiranda()) { |
| 5183 | continue; |
| 5184 | } |
| 5185 | mirror::ArtMethod* interface_method = interface->GetVirtualMethod(j); |
| 5186 | uint32_t imt_index = interface_method->GetDexMethodIndex() % mirror::Class::kImtSize; |
| 5187 | mirror::ArtMethod* imt_ref = out_imt->GetReference(imt_index)->AsArtMethod(); |
| 5188 | if (imt_ref == runtime->GetImtUnimplementedMethod()) { |
| 5189 | out_imt->SetReference(imt_index, method); |
| 5190 | } else if (imt_ref != conflict_method) { |
| 5191 | out_imt->SetReference(imt_index, conflict_method); |
| 5192 | } |
| 5193 | } |
| 5194 | } |
| 5195 | } |
| 5196 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 5197 | for (size_t i = 0; i < ifcount; ++i) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5198 | self->AllowThreadSuspension(); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 5199 | size_t num_methods = iftable->GetInterface(i)->NumVirtualMethods(); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5200 | if (num_methods > 0) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 5201 | StackHandleScope<2> hs2(self); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5202 | const bool is_super = i < super_ifcount; |
| 5203 | const bool super_interface = is_super && extend_super_iftable; |
| 5204 | Handle<mirror::ObjectArray<mirror::ArtMethod>> method_array; |
| 5205 | Handle<mirror::ObjectArray<mirror::ArtMethod>> input_array; |
| 5206 | if (super_interface) { |
| 5207 | mirror::IfTable* if_table = klass->GetSuperClass()->GetIfTable(); |
| 5208 | DCHECK(if_table != nullptr); |
| 5209 | DCHECK(if_table->GetMethodArray(i) != nullptr); |
| 5210 | // 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] | 5211 | method_array = hs2.NewHandle(if_table->GetMethodArray(i)->Clone(self)-> |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5212 | AsObjectArray<mirror::ArtMethod>()); |
| 5213 | // We are overwriting a super class interface, try to only virtual methods instead of the |
| 5214 | // whole vtable. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 5215 | input_array = hs2.NewHandle(klass->GetVirtualMethods()); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5216 | } else { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 5217 | method_array = hs2.NewHandle(AllocArtMethodArray(self, num_methods)); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5218 | // A new interface, we need the whole vtable incase a new interface method is implemented |
| 5219 | // in the whole superclass. |
| 5220 | input_array = vtable; |
| 5221 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5222 | if (UNLIKELY(method_array.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5223 | CHECK(self->IsExceptionPending()); // OOME. |
| 5224 | return false; |
| 5225 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5226 | iftable->SetMethodArray(i, method_array.Get()); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5227 | if (input_array.Get() == nullptr) { |
| 5228 | // If the added virtual methods is empty, do nothing. |
| 5229 | DCHECK(super_interface); |
| 5230 | continue; |
| 5231 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5232 | for (size_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5233 | mirror::ArtMethod* interface_method = iftable->GetInterface(i)->GetVirtualMethod(j); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5234 | MethodNameAndSignatureComparator interface_name_comparator( |
| 5235 | interface_method->GetInterfaceMethodIfProxy()); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5236 | int32_t k; |
| 5237 | // For each method listed in the interface's method list, find the |
| 5238 | // matching method in our class's method list. We want to favor the |
| 5239 | // subclass over the superclass, which just requires walking |
| 5240 | // back from the end of the vtable. (This only matters if the |
| 5241 | // superclass defines a private method and this class redefines |
| 5242 | // it -- otherwise it would use the same vtable slot. In .dex files |
| 5243 | // those don't end up in the virtual method table, so it shouldn't |
| 5244 | // matter which direction we go. We walk it backward anyway.) |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5245 | for (k = input_array->GetLength() - 1; k >= 0; --k) { |
| 5246 | mirror::ArtMethod* vtable_method = input_array->GetWithoutChecks(k); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5247 | mirror::ArtMethod* vtable_method_for_name_comparison = |
| 5248 | vtable_method->GetInterfaceMethodIfProxy(); |
| 5249 | if (interface_name_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5250 | vtable_method_for_name_comparison)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5251 | if (!vtable_method->IsAbstract() && !vtable_method->IsPublic()) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5252 | ThrowIllegalAccessError( |
| 5253 | klass.Get(), |
| 5254 | "Method '%s' implementing interface method '%s' is not public", |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5255 | PrettyMethod(vtable_method).c_str(), |
| 5256 | PrettyMethod(interface_method).c_str()); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5257 | return false; |
| 5258 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5259 | method_array->SetWithoutChecks<false>(j, vtable_method); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 5260 | // Place method in imt if entry is empty, place conflict otherwise. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5261 | uint32_t imt_index = interface_method->GetDexMethodIndex() % mirror::Class::kImtSize; |
| 5262 | mirror::ArtMethod* imt_ref = out_imt->GetReference(imt_index)->AsArtMethod(); |
| 5263 | mirror::ArtMethod* conflict_method = runtime->GetImtConflictMethod(); |
| 5264 | if (imt_ref == runtime->GetImtUnimplementedMethod()) { |
| 5265 | out_imt->SetReference(imt_index, vtable_method); |
| 5266 | } else if (imt_ref != conflict_method) { |
| 5267 | // If we are not a conflict and we have the same signature and name as the imt entry, |
| 5268 | // it must be that we overwrote a superclass vtable entry. |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5269 | MethodNameAndSignatureComparator imt_ref_name_comparator( |
| 5270 | imt_ref->GetInterfaceMethodIfProxy()); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5271 | if (imt_ref_name_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5272 | vtable_method_for_name_comparison)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5273 | out_imt->SetReference(imt_index, vtable_method); |
| 5274 | } else { |
| 5275 | out_imt->SetReference(imt_index, conflict_method); |
| 5276 | } |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 5277 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5278 | break; |
| 5279 | } |
| 5280 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5281 | if (k < 0 && !super_interface) { |
| 5282 | mirror::ArtMethod* miranda_method = nullptr; |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5283 | for (size_t l = 0; l < miranda_list_size; ++l) { |
| 5284 | mirror::ArtMethod* mir_method = miranda_list->Get(l); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5285 | if (interface_name_comparator.HasSameNameAndSignature(mir_method)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5286 | miranda_method = mir_method; |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5287 | break; |
| 5288 | } |
| 5289 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5290 | if (miranda_method == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5291 | // Point the interface table at a phantom slot. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5292 | miranda_method = interface_method->Clone(self)->AsArtMethod(); |
| 5293 | if (UNLIKELY(miranda_method == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5294 | CHECK(self->IsExceptionPending()); // OOME. |
| 5295 | return false; |
| 5296 | } |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5297 | DCHECK_LT(miranda_list_size, max_miranda_methods); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5298 | miranda_list->Set<false>(miranda_list_size++, miranda_method); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5299 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5300 | method_array->SetWithoutChecks<false>(j, miranda_method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5301 | } |
| 5302 | } |
| 5303 | } |
| 5304 | } |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5305 | if (miranda_list_size > 0) { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 5306 | int old_method_count = klass->NumVirtualMethods(); |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5307 | int new_method_count = old_method_count + miranda_list_size; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5308 | mirror::ObjectArray<mirror::ArtMethod>* virtuals; |
| 5309 | if (old_method_count == 0) { |
| 5310 | virtuals = AllocArtMethodArray(self, new_method_count); |
| 5311 | } else { |
| 5312 | virtuals = klass->GetVirtualMethods()->CopyOf(self, new_method_count); |
| 5313 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5314 | if (UNLIKELY(virtuals == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5315 | CHECK(self->IsExceptionPending()); // OOME. |
| 5316 | return false; |
| 5317 | } |
| 5318 | klass->SetVirtualMethods(virtuals); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5319 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5320 | int old_vtable_count = vtable->GetLength(); |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5321 | int new_vtable_count = old_vtable_count + miranda_list_size; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5322 | vtable.Assign(vtable->CopyOf(self, new_vtable_count)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5323 | if (UNLIKELY(vtable.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5324 | CHECK(self->IsExceptionPending()); // OOME. |
| 5325 | return false; |
| 5326 | } |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5327 | for (size_t i = 0; i < miranda_list_size; ++i) { |
| 5328 | mirror::ArtMethod* method = miranda_list->Get(i); |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 5329 | // Leave the declaring class alone as type indices are relative to it |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 5330 | method->SetAccessFlags(method->GetAccessFlags() | kAccMiranda); |
| 5331 | method->SetMethodIndex(0xFFFF & (old_vtable_count + i)); |
| 5332 | klass->SetVirtualMethod(old_method_count + i, method); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5333 | vtable->SetWithoutChecks<false>(old_vtable_count + i, method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5334 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5335 | // 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] | 5336 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5337 | } |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 5338 | |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5339 | if (kIsDebugBuild) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 5340 | mirror::ObjectArray<mirror::ArtMethod>* check_vtable = klass->GetVTableDuringLinking(); |
| 5341 | for (int i = 0; i < check_vtable->GetLength(); ++i) { |
| 5342 | CHECK(check_vtable->GetWithoutChecks(i) != nullptr); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5343 | } |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 5344 | } |
| 5345 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5346 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5347 | return true; |
| 5348 | } |
| 5349 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5350 | bool ClassLinker::LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5351 | CHECK(klass.Get() != nullptr); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5352 | return LinkFields(self, klass, false, nullptr); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5353 | } |
| 5354 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5355 | 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] | 5356 | CHECK(klass.Get() != nullptr); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5357 | return LinkFields(self, klass, true, class_size); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5358 | } |
| 5359 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5360 | struct LinkFieldsComparator { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5361 | explicit LinkFieldsComparator() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 5362 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 5363 | // 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] | 5364 | bool operator()(mirror::ArtField* field1, mirror::ArtField* field2) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5365 | NO_THREAD_SAFETY_ANALYSIS { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 5366 | // 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] | 5367 | Primitive::Type type1 = field1->GetTypeAsPrimitiveType(); |
| 5368 | Primitive::Type type2 = field2->GetTypeAsPrimitiveType(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 5369 | if (type1 != type2) { |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 5370 | if (type1 == Primitive::kPrimNot) { |
| 5371 | // Reference always goes first. |
| 5372 | return true; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 5373 | } |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 5374 | if (type2 == Primitive::kPrimNot) { |
| 5375 | // Reference always goes first. |
| 5376 | return false; |
| 5377 | } |
| 5378 | size_t size1 = Primitive::ComponentSize(type1); |
| 5379 | size_t size2 = Primitive::ComponentSize(type2); |
| 5380 | if (size1 != size2) { |
| 5381 | // Larger primitive types go first. |
| 5382 | return size1 > size2; |
| 5383 | } |
| 5384 | // Primitive types differ but sizes match. Arbitrarily order by primitive type. |
| 5385 | return type1 < type2; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5386 | } |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 5387 | // Same basic group? Then sort by dex field index. This is guaranteed to be sorted |
| 5388 | // by name and for equal names by type id index. |
| 5389 | // NOTE: This works also for proxies. Their static fields are assigned appropriate indexes. |
| 5390 | return field1->GetDexFieldIndex() < field2->GetDexFieldIndex(); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5391 | } |
| 5392 | }; |
| 5393 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5394 | bool ClassLinker::LinkFields(Thread* self, Handle<mirror::Class> klass, bool is_static, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5395 | size_t* class_size) { |
| 5396 | self->AllowThreadSuspension(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5397 | size_t num_fields = |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5398 | is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5399 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5400 | mirror::ObjectArray<mirror::ArtField>* fields = |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5401 | is_static ? klass->GetSFields() : klass->GetIFields(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5402 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5403 | // Initialize field_offset |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 5404 | MemberOffset field_offset(0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5405 | if (is_static) { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5406 | field_offset = klass->GetFirstReferenceStaticFieldOffsetDuringLinking(); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5407 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5408 | mirror::Class* super_class = klass->GetSuperClass(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5409 | if (super_class != nullptr) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5410 | CHECK(super_class->IsResolved()) |
| 5411 | << PrettyClass(klass.Get()) << " " << PrettyClass(super_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5412 | field_offset = MemberOffset(super_class->GetObjectSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5413 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5414 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5415 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5416 | CHECK_EQ(num_fields == 0, fields == nullptr) << PrettyClass(klass.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5417 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5418 | // we want a relatively stable order so that adding new fields |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 5419 | // minimizes disruption of C++ version such as Class and Method. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5420 | std::deque<mirror::ArtField*> grouped_and_sorted_fields; |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 5421 | const char* old_no_suspend_cause = self->StartAssertNoThreadSuspension( |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 5422 | "Naked ArtField references in deque"); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5423 | for (size_t i = 0; i < num_fields; i++) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5424 | mirror::ArtField* f = fields->Get(i); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5425 | CHECK(f != nullptr) << PrettyClass(klass.Get()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5426 | grouped_and_sorted_fields.push_back(f); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5427 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5428 | std::sort(grouped_and_sorted_fields.begin(), grouped_and_sorted_fields.end(), |
| 5429 | LinkFieldsComparator()); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5430 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 5431 | // References should be at the front. |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5432 | size_t current_field = 0; |
| 5433 | size_t num_reference_fields = 0; |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 5434 | FieldGaps gaps; |
| 5435 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5436 | for (; current_field < num_fields; current_field++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5437 | mirror::ArtField* field = grouped_and_sorted_fields.front(); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 5438 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 5439 | bool isPrimitive = type != Primitive::kPrimNot; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5440 | if (isPrimitive) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 5441 | break; // past last reference, move on to the next phase |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5442 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5443 | if (UNLIKELY(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>( |
| 5444 | field_offset.Uint32Value()))) { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 5445 | MemberOffset old_offset = field_offset; |
| 5446 | field_offset = MemberOffset(RoundUp(field_offset.Uint32Value(), 4)); |
| 5447 | AddFieldGap(old_offset.Uint32Value(), field_offset.Uint32Value(), &gaps); |
| 5448 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5449 | DCHECK(IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(field_offset.Uint32Value())); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5450 | grouped_and_sorted_fields.pop_front(); |
| 5451 | num_reference_fields++; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5452 | field->SetOffset(field_offset); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5453 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 5454 | sizeof(mirror::HeapReference<mirror::Object>)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5455 | } |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 5456 | // Gaps are stored as a max heap which means that we must shuffle from largest to smallest |
| 5457 | // otherwise we could end up with suboptimal gap fills. |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5458 | ShuffleForward<8>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 5459 | ShuffleForward<4>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 5460 | ShuffleForward<2>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 5461 | ShuffleForward<1>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 5462 | CHECK(grouped_and_sorted_fields.empty()) << "Missed " << grouped_and_sorted_fields.size() << |
| 5463 | " fields."; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5464 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5465 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 5466 | // 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] | 5467 | if (!is_static && klass->DescriptorEquals("Ljava/lang/ref/Reference;")) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 5468 | // We know there are no non-reference fields in the Reference classes, and we know |
| 5469 | // that 'referent' is alphabetically last, so this is easy... |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5470 | CHECK_EQ(num_reference_fields, num_fields) << PrettyClass(klass.Get()); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 5471 | CHECK_STREQ(fields->Get(num_fields - 1)->GetName(), "referent") << PrettyClass(klass.Get()); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 5472 | --num_reference_fields; |
| 5473 | } |
| 5474 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5475 | size_t size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5476 | // Update klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5477 | if (is_static) { |
| 5478 | klass->SetNumReferenceStaticFields(num_reference_fields); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5479 | *class_size = size; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5480 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5481 | klass->SetNumReferenceInstanceFields(num_reference_fields); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5482 | if (!klass->IsVariableSize()) { |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 5483 | if (klass->DescriptorEquals("Ljava/lang/reflect/ArtMethod;")) { |
| 5484 | klass->SetObjectSize(mirror::ArtMethod::InstanceSize(sizeof(void*))); |
| 5485 | } else { |
| 5486 | std::string temp; |
| 5487 | DCHECK_GE(size, sizeof(mirror::Object)) << klass->GetDescriptor(&temp); |
| 5488 | size_t previous_size = klass->GetObjectSize(); |
| 5489 | if (previous_size != 0) { |
| 5490 | // Make sure that we didn't originally have an incorrect size. |
| 5491 | CHECK_EQ(previous_size, size) << klass->GetDescriptor(&temp); |
| 5492 | } |
| 5493 | klass->SetObjectSize(size); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 5494 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5495 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5496 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5497 | |
| 5498 | if (kIsDebugBuild) { |
| 5499 | // Make sure that the fields array is ordered by name but all reference |
| 5500 | // offsets are at the beginning as far as alignment allows. |
| 5501 | MemberOffset start_ref_offset = is_static |
| 5502 | ? klass->GetFirstReferenceStaticFieldOffsetDuringLinking() |
| 5503 | : klass->GetFirstReferenceInstanceFieldOffset(); |
| 5504 | MemberOffset end_ref_offset(start_ref_offset.Uint32Value() + |
| 5505 | num_reference_fields * |
| 5506 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 5507 | MemberOffset current_ref_offset = start_ref_offset; |
| 5508 | for (size_t i = 0; i < num_fields; i++) { |
| 5509 | mirror::ArtField* field = fields->Get(i); |
| 5510 | if ((false)) { // enable to debug field layout |
| 5511 | LOG(INFO) << "LinkFields: " << (is_static ? "static" : "instance") |
| 5512 | << " class=" << PrettyClass(klass.Get()) |
| 5513 | << " field=" << PrettyField(field) |
| 5514 | << " offset=" |
| 5515 | << field->GetField32(mirror::ArtField::OffsetOffset()); |
| 5516 | } |
| 5517 | if (i != 0) { |
| 5518 | mirror::ArtField* prev_field = fields->Get(i - 1u); |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 5519 | // NOTE: The field names can be the same. This is not possible in the Java language |
| 5520 | // but it's valid Java/dex bytecode and for example proguard can generate such bytecode. |
| 5521 | CHECK_LE(strcmp(prev_field->GetName(), field->GetName()), 0); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5522 | } |
| 5523 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 5524 | bool is_primitive = type != Primitive::kPrimNot; |
| 5525 | if (klass->DescriptorEquals("Ljava/lang/ref/Reference;") && |
| 5526 | strcmp("referent", field->GetName()) == 0) { |
| 5527 | is_primitive = true; // We lied above, so we have to expect a lie here. |
| 5528 | } |
| 5529 | MemberOffset offset = field->GetOffsetDuringLinking(); |
| 5530 | if (is_primitive) { |
| 5531 | if (offset.Uint32Value() < end_ref_offset.Uint32Value()) { |
| 5532 | // Shuffled before references. |
| 5533 | size_t type_size = Primitive::ComponentSize(type); |
| 5534 | CHECK_LT(type_size, sizeof(mirror::HeapReference<mirror::Object>)); |
| 5535 | CHECK_LT(offset.Uint32Value(), start_ref_offset.Uint32Value()); |
| 5536 | CHECK_LE(offset.Uint32Value() + type_size, start_ref_offset.Uint32Value()); |
| 5537 | CHECK(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(offset.Uint32Value())); |
| 5538 | } |
| 5539 | } else { |
| 5540 | CHECK_EQ(current_ref_offset.Uint32Value(), offset.Uint32Value()); |
| 5541 | current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() + |
| 5542 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 5543 | } |
| 5544 | } |
| 5545 | CHECK_EQ(current_ref_offset.Uint32Value(), end_ref_offset.Uint32Value()); |
| 5546 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5547 | return true; |
| 5548 | } |
| 5549 | |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5550 | // Set the bitmap of reference instance field offsets. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5551 | void ClassLinker::CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5552 | uint32_t reference_offsets = 0; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5553 | mirror::Class* super_class = klass->GetSuperClass(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5554 | // 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] | 5555 | if (super_class != nullptr) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5556 | reference_offsets = super_class->GetReferenceInstanceOffsets(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5557 | // Compute reference offsets unless our superclass overflowed. |
| 5558 | if (reference_offsets != mirror::Class::kClassWalkSuper) { |
| 5559 | size_t num_reference_fields = klass->NumReferenceInstanceFieldsDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5560 | if (num_reference_fields != 0u) { |
| 5561 | // All of the fields that contain object references are guaranteed be grouped in memory |
| 5562 | // starting at an appropriately aligned address after super class object data. |
| 5563 | uint32_t start_offset = RoundUp(super_class->GetObjectSize(), |
| 5564 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 5565 | uint32_t start_bit = (start_offset - mirror::kObjectHeaderSize) / |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5566 | sizeof(mirror::HeapReference<mirror::Object>); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5567 | if (start_bit + num_reference_fields > 32) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5568 | reference_offsets = mirror::Class::kClassWalkSuper; |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5569 | } else { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5570 | reference_offsets |= (0xffffffffu << start_bit) & |
| 5571 | (0xffffffffu >> (32 - (start_bit + num_reference_fields))); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5572 | } |
| 5573 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5574 | } |
| 5575 | } |
Mingyao Yang | faff0f0 | 2014-09-10 12:03:22 -0700 | [diff] [blame] | 5576 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5577 | } |
| 5578 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5579 | mirror::String* ClassLinker::ResolveString(const DexFile& dex_file, uint32_t string_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5580 | Handle<mirror::DexCache> dex_cache) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5581 | DCHECK(dex_cache.Get() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5582 | mirror::String* resolved = dex_cache->GetResolvedString(string_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5583 | if (resolved != nullptr) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5584 | return resolved; |
| 5585 | } |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 5586 | uint32_t utf16_length; |
| 5587 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5588 | mirror::String* string = intern_table_->InternStrong(utf16_length, utf8_data); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5589 | dex_cache->SetResolvedString(string_idx, string); |
| 5590 | return string; |
| 5591 | } |
| 5592 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5593 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, uint16_t type_idx, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 5594 | mirror::Class* referrer) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5595 | StackHandleScope<2> hs(Thread::Current()); |
| 5596 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 5597 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5598 | return ResolveType(dex_file, type_idx, dex_cache, class_loader); |
| 5599 | } |
| 5600 | |
| 5601 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, uint16_t type_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5602 | Handle<mirror::DexCache> dex_cache, |
| 5603 | Handle<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5604 | DCHECK(dex_cache.Get() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5605 | mirror::Class* resolved = dex_cache->GetResolvedType(type_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5606 | if (resolved == nullptr) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 5607 | Thread* self = Thread::Current(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 5608 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 5609 | resolved = FindClass(self, descriptor, class_loader); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5610 | if (resolved != nullptr) { |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 5611 | // TODO: we used to throw here if resolved's class loader was not the |
| 5612 | // boot class loader. This was to permit different classes with the |
| 5613 | // same name to be loaded simultaneously by different loaders |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5614 | dex_cache->SetResolvedType(type_idx, resolved); |
| 5615 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5616 | CHECK(self->IsExceptionPending()) |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 5617 | << "Expected pending exception for failed resolution of: " << descriptor; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5618 | // Convert a ClassNotFoundException to a NoClassDefFoundError. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5619 | StackHandleScope<1> hs(self); |
| 5620 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException(nullptr))); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5621 | if (cause->InstanceOf(GetClassRoot(kJavaLangClassNotFoundException))) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5622 | DCHECK(resolved == nullptr); // No Handle needed to preserve resolved. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 5623 | self->ClearException(); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 5624 | ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5625 | self->GetException(nullptr)->SetCause(cause.Get()); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 5626 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5627 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5628 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5629 | DCHECK((resolved == nullptr) || resolved->IsResolved() || resolved->IsErroneous()) |
Brian Carlstrom | abcf7ae | 2013-09-23 22:19:52 -0700 | [diff] [blame] | 5630 | << PrettyDescriptor(resolved) << " " << resolved->GetStatus(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5631 | return resolved; |
| 5632 | } |
| 5633 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 5634 | mirror::ArtMethod* ClassLinker::ResolveMethod(const DexFile& dex_file, uint32_t method_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5635 | Handle<mirror::DexCache> dex_cache, |
| 5636 | Handle<mirror::ClassLoader> class_loader, |
| 5637 | Handle<mirror::ArtMethod> referrer, |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 5638 | InvokeType type) { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5639 | DCHECK(dex_cache.Get() != nullptr); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5640 | // Check for hit in the dex cache. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5641 | mirror::ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 5642 | if (resolved != nullptr && !resolved->IsRuntimeMethod()) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5643 | return resolved; |
| 5644 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5645 | // Fail, get the declaring class. |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5646 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5647 | 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] | 5648 | if (klass == nullptr) { |
Elliott Hughes | cc5f9a9 | 2011-09-28 19:17:29 -0700 | [diff] [blame] | 5649 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5650 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5651 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5652 | // Scan using method_idx, this saves string compares but will only hit for matching dex |
| 5653 | // caches/files. |
| 5654 | switch (type) { |
| 5655 | case kDirect: // Fall-through. |
| 5656 | case kStatic: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5657 | resolved = klass->FindDirectMethod(dex_cache.Get(), method_idx); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5658 | break; |
| 5659 | case kInterface: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5660 | resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5661 | DCHECK(resolved == nullptr || resolved->GetDeclaringClass()->IsInterface()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5662 | break; |
| 5663 | case kSuper: // Fall-through. |
| 5664 | case kVirtual: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5665 | resolved = klass->FindVirtualMethod(dex_cache.Get(), method_idx); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5666 | break; |
| 5667 | default: |
| 5668 | LOG(FATAL) << "Unreachable - invocation type: " << type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 5669 | UNREACHABLE(); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 5670 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5671 | if (resolved == nullptr) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5672 | // Search by name, which works across dex files. |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5673 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 5674 | const Signature signature = dex_file.GetMethodSignature(method_id); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5675 | switch (type) { |
| 5676 | case kDirect: // Fall-through. |
| 5677 | case kStatic: |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 5678 | resolved = klass->FindDirectMethod(name, signature); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5679 | break; |
| 5680 | case kInterface: |
| 5681 | resolved = klass->FindInterfaceMethod(name, signature); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5682 | DCHECK(resolved == nullptr || resolved->GetDeclaringClass()->IsInterface()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5683 | break; |
| 5684 | case kSuper: // Fall-through. |
| 5685 | case kVirtual: |
| 5686 | resolved = klass->FindVirtualMethod(name, signature); |
| 5687 | break; |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5688 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5689 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5690 | // If we found a method, check for incompatible class changes. |
| 5691 | if (LIKELY(resolved != nullptr && !resolved->CheckIncompatibleClassChange(type))) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5692 | // Be a good citizen and update the dex cache to speed subsequent calls. |
| 5693 | dex_cache->SetResolvedMethod(method_idx, resolved); |
| 5694 | return resolved; |
| 5695 | } else { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5696 | // If we had a method, it's an incompatible-class-change error. |
| 5697 | if (resolved != nullptr) { |
| 5698 | ThrowIncompatibleClassChangeError(type, resolved->GetInvokeType(), resolved, referrer.Get()); |
| 5699 | } else { |
| 5700 | // We failed to find the method which means either an access error, an incompatible class |
| 5701 | // change, or no such method. First try to find the method among direct and virtual methods. |
| 5702 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
| 5703 | const Signature signature = dex_file.GetMethodSignature(method_id); |
| 5704 | switch (type) { |
| 5705 | case kDirect: |
| 5706 | case kStatic: |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5707 | resolved = klass->FindVirtualMethod(name, signature); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5708 | // Note: kDirect and kStatic are also mutually exclusive, but in that case we would |
| 5709 | // have had a resolved method before, which triggers the "true" branch above. |
| 5710 | break; |
| 5711 | case kInterface: |
| 5712 | case kVirtual: |
| 5713 | case kSuper: |
| 5714 | resolved = klass->FindDirectMethod(name, signature); |
| 5715 | break; |
| 5716 | } |
| 5717 | |
| 5718 | // 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] | 5719 | bool exception_generated = false; |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5720 | if (resolved != nullptr && referrer.Get() != nullptr) { |
| 5721 | mirror::Class* methods_class = resolved->GetDeclaringClass(); |
| 5722 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
| 5723 | if (!referring_class->CanAccess(methods_class)) { |
| 5724 | ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, methods_class, |
| 5725 | resolved, type); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5726 | exception_generated = true; |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5727 | } else if (!referring_class->CanAccessMember(methods_class, |
| 5728 | resolved->GetAccessFlags())) { |
| 5729 | ThrowIllegalAccessErrorMethod(referring_class, resolved); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5730 | exception_generated = true; |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5731 | } |
| 5732 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5733 | if (!exception_generated) { |
| 5734 | // Otherwise, throw an IncompatibleClassChangeError if we found something, and check |
| 5735 | // interface methods and throw if we find the method there. If we find nothing, throw a |
| 5736 | // NoSuchMethodError. |
| 5737 | switch (type) { |
| 5738 | case kDirect: |
| 5739 | case kStatic: |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5740 | if (resolved != nullptr) { |
| 5741 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer.Get()); |
| 5742 | } else { |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5743 | resolved = klass->FindInterfaceMethod(name, signature); |
| 5744 | if (resolved != nullptr) { |
| 5745 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer.Get()); |
| 5746 | } else { |
| 5747 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 5748 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5749 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5750 | break; |
| 5751 | case kInterface: |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5752 | if (resolved != nullptr) { |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5753 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer.Get()); |
| 5754 | } else { |
| 5755 | resolved = klass->FindVirtualMethod(name, signature); |
| 5756 | if (resolved != nullptr) { |
| 5757 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer.Get()); |
| 5758 | } else { |
| 5759 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 5760 | } |
| 5761 | } |
| 5762 | break; |
| 5763 | case kSuper: |
| 5764 | if (resolved != nullptr) { |
| 5765 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer.Get()); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5766 | } else { |
| 5767 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 5768 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5769 | break; |
| 5770 | case kVirtual: |
| 5771 | if (resolved != nullptr) { |
| 5772 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer.Get()); |
| 5773 | } else { |
| 5774 | resolved = klass->FindInterfaceMethod(name, signature); |
| 5775 | if (resolved != nullptr) { |
| 5776 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer.Get()); |
| 5777 | } else { |
| 5778 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 5779 | } |
| 5780 | } |
| 5781 | break; |
| 5782 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5783 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5784 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5785 | Thread::Current()->AssertPendingException(); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5786 | return nullptr; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5787 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5788 | } |
| 5789 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5790 | mirror::ArtField* ClassLinker::ResolveField(const DexFile& dex_file, uint32_t field_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5791 | Handle<mirror::DexCache> dex_cache, |
| 5792 | Handle<mirror::ClassLoader> class_loader, |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 5793 | bool is_static) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5794 | DCHECK(dex_cache.Get() != nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5795 | mirror::ArtField* resolved = dex_cache->GetResolvedField(field_idx); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5796 | if (resolved != nullptr) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5797 | return resolved; |
| 5798 | } |
| 5799 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5800 | Thread* const self = Thread::Current(); |
| 5801 | StackHandleScope<1> hs(self); |
| 5802 | Handle<mirror::Class> klass( |
| 5803 | 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] | 5804 | if (klass.Get() == nullptr) { |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 5805 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5806 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5807 | } |
| 5808 | |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 5809 | if (is_static) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5810 | resolved = mirror::Class::FindStaticField(self, klass, dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 5811 | } else { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5812 | resolved = klass->FindInstanceField(dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 5813 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5814 | |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5815 | if (resolved == nullptr) { |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5816 | const char* name = dex_file.GetFieldName(field_id); |
| 5817 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 5818 | if (is_static) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5819 | resolved = mirror::Class::FindStaticField(self, klass, name, type); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5820 | } else { |
| 5821 | resolved = klass->FindInstanceField(name, type); |
| 5822 | } |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5823 | if (resolved == nullptr) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5824 | ThrowNoSuchFieldError(is_static ? "static " : "instance ", klass.Get(), type, name); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5825 | return nullptr; |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5826 | } |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5827 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5828 | dex_cache->SetResolvedField(field_idx, resolved); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5829 | return resolved; |
| 5830 | } |
| 5831 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5832 | mirror::ArtField* ClassLinker::ResolveFieldJLS(const DexFile& dex_file, |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5833 | uint32_t field_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5834 | Handle<mirror::DexCache> dex_cache, |
| 5835 | Handle<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5836 | DCHECK(dex_cache.Get() != nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5837 | mirror::ArtField* resolved = dex_cache->GetResolvedField(field_idx); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5838 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5839 | return resolved; |
| 5840 | } |
| 5841 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5842 | Thread* self = Thread::Current(); |
| 5843 | StackHandleScope<1> hs(self); |
| 5844 | Handle<mirror::Class> klass( |
| 5845 | 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] | 5846 | if (klass.Get() == nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5847 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5848 | return nullptr; |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5849 | } |
| 5850 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 5851 | StringPiece name(dex_file.StringDataByIdx(field_id.name_idx_)); |
| 5852 | StringPiece type(dex_file.StringDataByIdx( |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5853 | dex_file.GetTypeId(field_id.type_idx_).descriptor_idx_)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5854 | resolved = mirror::Class::FindField(self, klass, name, type); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5855 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5856 | dex_cache->SetResolvedField(field_idx, resolved); |
| 5857 | } else { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5858 | ThrowNoSuchFieldError("", klass.Get(), type, name); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5859 | } |
| 5860 | return resolved; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 5861 | } |
| 5862 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5863 | const char* ClassLinker::MethodShorty(uint32_t method_idx, mirror::ArtMethod* referrer, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5864 | uint32_t* length) { |
| 5865 | mirror::Class* declaring_class = referrer->GetDeclaringClass(); |
| 5866 | mirror::DexCache* dex_cache = declaring_class->GetDexCache(); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 5867 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 5868 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 5869 | return dex_file.GetMethodShorty(method_id, length); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 5870 | } |
| 5871 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5872 | void ClassLinker::DumpAllClasses(int flags) { |
| 5873 | if (dex_cache_image_class_lookup_required_) { |
| 5874 | MoveImageClassesToClassTable(); |
| 5875 | } |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 5876 | // TODO: at the time this was written, it wasn't safe to call PrettyField with the ClassLinker |
| 5877 | // 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] | 5878 | std::vector<mirror::Class*> all_classes; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 5879 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 5880 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 5881 | for (GcRoot<mirror::Class>& it : class_table_) { |
| 5882 | all_classes.push_back(it.Read()); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 5883 | } |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 5884 | } |
| 5885 | |
| 5886 | for (size_t i = 0; i < all_classes.size(); ++i) { |
| 5887 | all_classes[i]->DumpClass(std::cerr, flags); |
| 5888 | } |
| 5889 | } |
| 5890 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 5891 | static OatFile::OatMethod CreateOatMethod(const void* code) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5892 | CHECK(code != nullptr); |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 5893 | const uint8_t* base = reinterpret_cast<const uint8_t*>(code); // Base of data points at code. |
| 5894 | base -= sizeof(void*); // Move backward so that code_offset != 0. |
| 5895 | const uint32_t code_offset = sizeof(void*); |
| 5896 | return OatFile::OatMethod(base, code_offset); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5897 | } |
| 5898 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5899 | bool ClassLinker::IsQuickResolutionStub(const void* entry_point) const { |
| 5900 | return (entry_point == GetQuickResolutionStub()) || |
| 5901 | (quick_resolution_trampoline_ == entry_point); |
| 5902 | } |
| 5903 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5904 | bool ClassLinker::IsQuickToInterpreterBridge(const void* entry_point) const { |
| 5905 | return (entry_point == GetQuickToInterpreterBridge()) || |
| 5906 | (quick_to_interpreter_bridge_trampoline_ == entry_point); |
| 5907 | } |
| 5908 | |
| 5909 | bool ClassLinker::IsQuickGenericJniStub(const void* entry_point) const { |
| 5910 | return (entry_point == GetQuickGenericJniStub()) || |
| 5911 | (quick_generic_jni_trampoline_ == entry_point); |
| 5912 | } |
| 5913 | |
| 5914 | const void* ClassLinker::GetRuntimeQuickGenericJniStub() const { |
| 5915 | return GetQuickGenericJniStub(); |
| 5916 | } |
| 5917 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 5918 | void ClassLinker::SetEntryPointsToCompiledCode(mirror::ArtMethod* method, |
| 5919 | const void* method_code) const { |
| 5920 | OatFile::OatMethod oat_method = CreateOatMethod(method_code); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5921 | oat_method.LinkMethod(method); |
| 5922 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5923 | } |
| 5924 | |
| 5925 | void ClassLinker::SetEntryPointsToInterpreter(mirror::ArtMethod* method) const { |
| 5926 | if (!method->IsNative()) { |
| 5927 | method->SetEntryPointFromInterpreter(artInterpreterToInterpreterBridge); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5928 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 5929 | } else { |
| 5930 | const void* quick_method_code = GetQuickGenericJniStub(); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 5931 | OatFile::OatMethod oat_method = CreateOatMethod(quick_method_code); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5932 | oat_method.LinkMethod(method); |
| 5933 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5934 | } |
| 5935 | } |
| 5936 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5937 | void ClassLinker::DumpForSigQuit(std::ostream& os) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5938 | Thread* self = Thread::Current(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5939 | if (dex_cache_image_class_lookup_required_) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5940 | ScopedObjectAccess soa(self); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5941 | MoveImageClassesToClassTable(); |
| 5942 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5943 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 5944 | os << "Zygote loaded classes=" << pre_zygote_class_table_.Size() << " post zygote classes=" |
| 5945 | << class_table_.Size() << "\n"; |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 5946 | } |
| 5947 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5948 | size_t ClassLinker::NumLoadedClasses() { |
| 5949 | if (dex_cache_image_class_lookup_required_) { |
| 5950 | MoveImageClassesToClassTable(); |
| 5951 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 5952 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 5953 | // Only return non zygote classes since these are the ones which apps which care about. |
| 5954 | return class_table_.Size(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 5955 | } |
| 5956 | |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 5957 | pid_t ClassLinker::GetClassesLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 5958 | return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 5959 | } |
| 5960 | |
| 5961 | pid_t ClassLinker::GetDexLockOwner() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 5962 | return dex_lock_.GetExclusiveOwnerTid(); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 5963 | } |
| 5964 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5965 | void ClassLinker::SetClassRoot(ClassRoot class_root, mirror::Class* klass) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5966 | DCHECK(!init_done_); |
| 5967 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5968 | DCHECK(klass != nullptr); |
| 5969 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5970 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 5971 | mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5972 | DCHECK(class_roots != nullptr); |
| 5973 | DCHECK(class_roots->Get(class_root) == nullptr); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 5974 | class_roots->Set<false>(class_root, klass); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5975 | } |
| 5976 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5977 | const char* ClassLinker::GetClassRootDescriptor(ClassRoot class_root) { |
| 5978 | static const char* class_roots_descriptors[] = { |
| 5979 | "Ljava/lang/Class;", |
| 5980 | "Ljava/lang/Object;", |
| 5981 | "[Ljava/lang/Class;", |
| 5982 | "[Ljava/lang/Object;", |
| 5983 | "Ljava/lang/String;", |
| 5984 | "Ljava/lang/DexCache;", |
| 5985 | "Ljava/lang/ref/Reference;", |
| 5986 | "Ljava/lang/reflect/ArtField;", |
| 5987 | "Ljava/lang/reflect/ArtMethod;", |
| 5988 | "Ljava/lang/reflect/Proxy;", |
| 5989 | "[Ljava/lang/String;", |
| 5990 | "[Ljava/lang/reflect/ArtField;", |
| 5991 | "[Ljava/lang/reflect/ArtMethod;", |
| 5992 | "Ljava/lang/ClassLoader;", |
| 5993 | "Ljava/lang/Throwable;", |
| 5994 | "Ljava/lang/ClassNotFoundException;", |
| 5995 | "Ljava/lang/StackTraceElement;", |
| 5996 | "Z", |
| 5997 | "B", |
| 5998 | "C", |
| 5999 | "D", |
| 6000 | "F", |
| 6001 | "I", |
| 6002 | "J", |
| 6003 | "S", |
| 6004 | "V", |
| 6005 | "[Z", |
| 6006 | "[B", |
| 6007 | "[C", |
| 6008 | "[D", |
| 6009 | "[F", |
| 6010 | "[I", |
| 6011 | "[J", |
| 6012 | "[S", |
| 6013 | "[Ljava/lang/StackTraceElement;", |
| 6014 | }; |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 6015 | static_assert(arraysize(class_roots_descriptors) == size_t(kClassRootsMax), |
| 6016 | "Mismatch between class descriptors and class-root enum"); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 6017 | |
| 6018 | const char* descriptor = class_roots_descriptors[class_root]; |
| 6019 | CHECK(descriptor != nullptr); |
| 6020 | return descriptor; |
| 6021 | } |
| 6022 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 6023 | std::size_t ClassLinker::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& root) |
| 6024 | const { |
| 6025 | std::string temp; |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 6026 | return ComputeModifiedUtf8Hash(root.Read()->GetDescriptor(&temp)); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 6027 | } |
| 6028 | |
| 6029 | bool ClassLinker::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a, |
| 6030 | const GcRoot<mirror::Class>& b) { |
| 6031 | if (a.Read()->GetClassLoader() != b.Read()->GetClassLoader()) { |
| 6032 | return false; |
| 6033 | } |
| 6034 | std::string temp; |
| 6035 | return a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp)); |
| 6036 | } |
| 6037 | |
| 6038 | std::size_t ClassLinker::ClassDescriptorHashEquals::operator()( |
| 6039 | const std::pair<const char*, mirror::ClassLoader*>& element) const { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 6040 | return ComputeModifiedUtf8Hash(element.first); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 6041 | } |
| 6042 | |
| 6043 | bool ClassLinker::ClassDescriptorHashEquals::operator()( |
| 6044 | const GcRoot<mirror::Class>& a, const std::pair<const char*, mirror::ClassLoader*>& b) { |
| 6045 | if (a.Read()->GetClassLoader() != b.second) { |
| 6046 | return false; |
| 6047 | } |
| 6048 | return a.Read()->DescriptorEquals(b.first); |
| 6049 | } |
| 6050 | |
| 6051 | bool ClassLinker::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a, |
| 6052 | const char* descriptor) { |
| 6053 | return a.Read()->DescriptorEquals(descriptor); |
| 6054 | } |
| 6055 | |
| 6056 | std::size_t ClassLinker::ClassDescriptorHashEquals::operator()(const char* descriptor) const { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 6057 | return ComputeModifiedUtf8Hash(descriptor); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 6058 | } |
| 6059 | |
Sebastien Hertz | 6963e44 | 2014-11-26 22:11:27 +0100 | [diff] [blame] | 6060 | bool ClassLinker::MayBeCalledWithDirectCodePointer(mirror::ArtMethod* m) { |
| 6061 | // Non-image methods don't use direct code pointer. |
| 6062 | if (!m->GetDeclaringClass()->IsBootStrapClassLoaded()) { |
| 6063 | return false; |
| 6064 | } |
| 6065 | if (m->IsPrivate()) { |
| 6066 | // The method can only be called inside its own oat file. Therefore it won't be called using |
| 6067 | // its direct code if the oat file has been compiled in PIC mode. |
| 6068 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 6069 | const DexFile& dex_file = m->GetDeclaringClass()->GetDexFile(); |
| 6070 | const OatFile::OatDexFile* oat_dex_file = class_linker->FindOpenedOatDexFileForDexFile(dex_file); |
| 6071 | if (oat_dex_file == nullptr) { |
| 6072 | // No oat file: the method has not been compiled. |
| 6073 | return false; |
| 6074 | } |
| 6075 | const OatFile* oat_file = oat_dex_file->GetOatFile(); |
| 6076 | return oat_file != nullptr && !oat_file->IsPic(); |
| 6077 | } else { |
| 6078 | // The method can be called outside its own oat file. Therefore it won't be called using its |
| 6079 | // direct code pointer only if all loaded oat files have been compiled in PIC mode. |
| 6080 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
| 6081 | for (const OatFile* oat_file : oat_files_) { |
| 6082 | if (!oat_file->IsPic()) { |
| 6083 | return true; |
| 6084 | } |
| 6085 | } |
| 6086 | return false; |
| 6087 | } |
| 6088 | } |
| 6089 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6090 | } // namespace art |