Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "class_linker.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 18 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 19 | #include <deque> |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 20 | #include <iostream> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 21 | #include <memory> |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 22 | #include <queue> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 23 | #include <string> |
Andreas Gampe | a696c0a | 2014-12-10 20:51:45 -0800 | [diff] [blame] | 24 | #include <unistd.h> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 25 | #include <utility> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 26 | #include <vector> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 27 | |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 28 | #include "base/casts.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 29 | #include "base/logging.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 30 | #include "base/scoped_flock.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 31 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 32 | #include "base/unix_file/fd_file.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 33 | #include "class_linker-inl.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 34 | #include "compiler_callbacks.h" |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 35 | #include "debugger.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 36 | #include "dex_file-inl.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 37 | #include "entrypoints/runtime_asm_entrypoints.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 38 | #include "gc_root-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 39 | #include "gc/accounting/card_table-inl.h" |
| 40 | #include "gc/accounting/heap_bitmap.h" |
| 41 | #include "gc/heap.h" |
| 42 | #include "gc/space/image_space.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 43 | #include "handle_scope.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 44 | #include "intern_table.h" |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 45 | #include "interpreter/interpreter.h" |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 46 | #include "jit/jit.h" |
| 47 | #include "jit/jit_code_cache.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 48 | #include "leb128.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 49 | #include "oat.h" |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 50 | #include "oat_file.h" |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 51 | #include "object_lock.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 52 | #include "mirror/art_field-inl.h" |
| 53 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 54 | #include "mirror/class.h" |
| 55 | #include "mirror/class-inl.h" |
| 56 | #include "mirror/class_loader.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 57 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 58 | #include "mirror/iftable-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 59 | #include "mirror/object-inl.h" |
| 60 | #include "mirror/object_array-inl.h" |
| 61 | #include "mirror/proxy.h" |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 62 | #include "mirror/reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 63 | #include "mirror/stack_trace_element.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 64 | #include "mirror/string-inl.h" |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 65 | #include "os.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 66 | #include "runtime.h" |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 67 | #include "entrypoints/entrypoint_utils.h" |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 68 | #include "ScopedLocalRef.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 69 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 70 | #include "handle_scope-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 71 | #include "thread-inl.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 72 | #include "utils.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 73 | #include "verifier/method_verifier.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 74 | #include "well_known_classes.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 75 | |
| 76 | namespace art { |
| 77 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 78 | static void ThrowNoClassDefFoundError(const char* fmt, ...) |
| 79 | __attribute__((__format__(__printf__, 1, 2))) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 80 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 81 | static void ThrowNoClassDefFoundError(const char* fmt, ...) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 82 | va_list args; |
| 83 | va_start(args, fmt); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 84 | Thread* self = Thread::Current(); |
| 85 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 86 | self->ThrowNewExceptionV(throw_location, "Ljava/lang/NoClassDefFoundError;", fmt, args); |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 87 | va_end(args); |
| 88 | } |
| 89 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 90 | static void ThrowEarlierClassFailure(mirror::Class* c) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 91 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 92 | // The class failed to initialize on a previous attempt, so we want to throw |
| 93 | // a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we |
| 94 | // failed in verification, in which case v2 5.4.1 says we need to re-throw |
| 95 | // the previous error. |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 96 | Runtime* const runtime = Runtime::Current(); |
| 97 | if (!runtime->IsAotCompiler()) { // Give info if this occurs at runtime. |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 98 | LOG(INFO) << "Rejecting re-init on previously-failed class " << PrettyClass(c); |
| 99 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 100 | |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 101 | CHECK(c->IsErroneous()) << PrettyClass(c) << " " << c->GetStatus(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 102 | Thread* self = Thread::Current(); |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 103 | if (runtime->IsAotCompiler()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 104 | // At compile time, accurate errors and NCDFE are disabled to speed compilation. |
| 105 | mirror::Throwable* pre_allocated = runtime->GetPreAllocatedNoClassDefFoundError(); |
| 106 | self->SetException(ThrowLocation(), pre_allocated); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 107 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 108 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 109 | if (c->GetVerifyErrorClass() != NULL) { |
| 110 | // TODO: change the verifier to store an _instance_, with a useful detail message? |
| 111 | std::string temp; |
| 112 | self->ThrowNewException(throw_location, c->GetVerifyErrorClass()->GetDescriptor(&temp), |
| 113 | PrettyDescriptor(c).c_str()); |
| 114 | } else { |
| 115 | self->ThrowNewException(throw_location, "Ljava/lang/NoClassDefFoundError;", |
| 116 | PrettyDescriptor(c).c_str()); |
| 117 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 121 | static void VlogClassInitializationFailure(Handle<mirror::Class> klass) |
| 122 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 123 | if (VLOG_IS_ON(class_linker)) { |
| 124 | std::string temp; |
| 125 | LOG(INFO) << "Failed to initialize class " << klass->GetDescriptor(&temp) << " from " |
| 126 | << klass->GetLocation() << "\n" << Thread::Current()->GetException(nullptr)->Dump(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | static void WrapExceptionInInitializer(Handle<mirror::Class> klass) |
| 131 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 132 | Thread* self = Thread::Current(); |
| 133 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 134 | |
| 135 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 136 | CHECK(cause.get() != nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 137 | |
| 138 | env->ExceptionClear(); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 139 | bool is_error = env->IsInstanceOf(cause.get(), WellKnownClasses::java_lang_Error); |
| 140 | env->Throw(cause.get()); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 141 | |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 142 | // We only wrap non-Error exceptions; an Error can just be used as-is. |
| 143 | if (!is_error) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 144 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 145 | self->ThrowNewWrappedException(throw_location, "Ljava/lang/ExceptionInInitializerError;", |
| 146 | nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 147 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 148 | VlogClassInitializationFailure(klass); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 151 | // Gap between two fields in object layout. |
| 152 | struct FieldGap { |
| 153 | uint32_t start_offset; // The offset from the start of the object. |
| 154 | uint32_t size; // The gap size of 1, 2, or 4 bytes. |
| 155 | }; |
| 156 | struct FieldGapsComparator { |
| 157 | explicit FieldGapsComparator() { |
| 158 | } |
| 159 | bool operator() (const FieldGap& lhs, const FieldGap& rhs) |
| 160 | NO_THREAD_SAFETY_ANALYSIS { |
Andreas Gampe | f52857f | 2015-02-18 15:38:57 -0800 | [diff] [blame] | 161 | // Sort by gap size, largest first. Secondary sort by starting offset. |
| 162 | return lhs.size > rhs.size || (lhs.size == rhs.size && lhs.start_offset < rhs.start_offset); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 163 | } |
| 164 | }; |
| 165 | typedef std::priority_queue<FieldGap, std::vector<FieldGap>, FieldGapsComparator> FieldGaps; |
| 166 | |
| 167 | // Adds largest aligned gaps to queue of gaps. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 168 | 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] | 169 | DCHECK(gaps != nullptr); |
| 170 | |
| 171 | uint32_t current_offset = gap_start; |
| 172 | while (current_offset != gap_end) { |
| 173 | size_t remaining = gap_end - current_offset; |
| 174 | if (remaining >= sizeof(uint32_t) && IsAligned<4>(current_offset)) { |
| 175 | gaps->push(FieldGap {current_offset, sizeof(uint32_t)}); |
| 176 | current_offset += sizeof(uint32_t); |
| 177 | } else if (remaining >= sizeof(uint16_t) && IsAligned<2>(current_offset)) { |
| 178 | gaps->push(FieldGap {current_offset, sizeof(uint16_t)}); |
| 179 | current_offset += sizeof(uint16_t); |
| 180 | } else { |
| 181 | gaps->push(FieldGap {current_offset, sizeof(uint8_t)}); |
| 182 | current_offset += sizeof(uint8_t); |
| 183 | } |
| 184 | DCHECK_LE(current_offset, gap_end) << "Overran gap"; |
| 185 | } |
| 186 | } |
| 187 | // Shuffle fields forward, making use of gaps whenever possible. |
| 188 | template<int n> |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 189 | static void ShuffleForward(size_t* current_field_idx, |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 190 | MemberOffset* field_offset, |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 191 | std::deque<mirror::ArtField*>* grouped_and_sorted_fields, |
| 192 | FieldGaps* gaps) |
| 193 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 194 | DCHECK(current_field_idx != nullptr); |
| 195 | DCHECK(grouped_and_sorted_fields != nullptr); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 196 | DCHECK(gaps != nullptr); |
| 197 | DCHECK(field_offset != nullptr); |
| 198 | |
| 199 | DCHECK(IsPowerOfTwo(n)); |
| 200 | while (!grouped_and_sorted_fields->empty()) { |
| 201 | mirror::ArtField* field = grouped_and_sorted_fields->front(); |
| 202 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 203 | if (Primitive::ComponentSize(type) < n) { |
| 204 | break; |
| 205 | } |
| 206 | if (!IsAligned<n>(field_offset->Uint32Value())) { |
| 207 | MemberOffset old_offset = *field_offset; |
| 208 | *field_offset = MemberOffset(RoundUp(field_offset->Uint32Value(), n)); |
| 209 | AddFieldGap(old_offset.Uint32Value(), field_offset->Uint32Value(), gaps); |
| 210 | } |
| 211 | CHECK(type != Primitive::kPrimNot) << PrettyField(field); // should be primitive types |
| 212 | grouped_and_sorted_fields->pop_front(); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 213 | if (!gaps->empty() && gaps->top().size >= n) { |
| 214 | FieldGap gap = gaps->top(); |
| 215 | gaps->pop(); |
| 216 | DCHECK(IsAligned<n>(gap.start_offset)); |
| 217 | field->SetOffset(MemberOffset(gap.start_offset)); |
| 218 | if (gap.size > n) { |
| 219 | AddFieldGap(gap.start_offset + n, gap.start_offset + gap.size, gaps); |
| 220 | } |
| 221 | } else { |
| 222 | DCHECK(IsAligned<n>(field_offset->Uint32Value())); |
| 223 | field->SetOffset(*field_offset); |
| 224 | *field_offset = MemberOffset(field_offset->Uint32Value() + n); |
| 225 | } |
| 226 | ++(*current_field_idx); |
| 227 | } |
| 228 | } |
| 229 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 230 | ClassLinker::ClassLinker(InternTable* intern_table) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 231 | // dex_lock_ is recursive as it may be used in stack dumping. |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 232 | : dex_lock_("ClassLinker dex lock", kDefaultMutexLevel), |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 233 | dex_cache_image_class_lookup_required_(false), |
| 234 | failed_dex_cache_class_lookups_(0), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 235 | class_roots_(nullptr), |
| 236 | array_iftable_(nullptr), |
| 237 | find_array_class_cache_next_victim_(0), |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 238 | init_done_(false), |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 239 | log_new_dex_caches_roots_(false), |
| 240 | log_new_class_table_roots_(false), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 241 | intern_table_(intern_table), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 242 | quick_resolution_trampoline_(nullptr), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 243 | quick_imt_conflict_trampoline_(nullptr), |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 244 | quick_generic_jni_trampoline_(nullptr), |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 245 | quick_to_interpreter_bridge_trampoline_(nullptr), |
| 246 | image_pointer_size_(sizeof(void*)) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 247 | memset(find_array_class_cache_, 0, kFindArrayCacheSize * sizeof(mirror::Class*)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 248 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 249 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 250 | 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] | 251 | VLOG(startup) << "ClassLinker::Init"; |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 252 | CHECK(!Runtime::Current()->GetHeap()->HasImageSpace()) << "Runtime has image. We should use it."; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 253 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 254 | CHECK(!init_done_); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 255 | |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 256 | // java_lang_Class comes first, it's needed for AllocClass |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 257 | Thread* self = Thread::Current(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 258 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 259 | // The GC can't handle an object with a null class since we can't get the size of this object. |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 260 | heap->IncrementDisableMovingGC(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 261 | StackHandleScope<64> hs(self); // 64 is picked arbitrarily. |
| 262 | Handle<mirror::Class> java_lang_Class(hs.NewHandle(down_cast<mirror::Class*>( |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 263 | heap->AllocNonMovableObject<true>(self, nullptr, |
| 264 | mirror::Class::ClassClassSize(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 265 | VoidFunctor())))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 266 | CHECK(java_lang_Class.Get() != nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 267 | mirror::Class::SetClassClass(java_lang_Class.Get()); |
| 268 | java_lang_Class->SetClass(java_lang_Class.Get()); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 269 | if (kUseBakerOrBrooksReadBarrier) { |
| 270 | java_lang_Class->AssertReadBarrierPointer(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 271 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 272 | java_lang_Class->SetClassSize(mirror::Class::ClassClassSize()); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 273 | java_lang_Class->SetPrimitiveType(Primitive::kPrimNot); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 274 | heap->DecrementDisableMovingGC(self); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 275 | // AllocClass(mirror::Class*) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 276 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 277 | // Class[] is used for reflection support. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 278 | Handle<mirror::Class> class_array_class(hs.NewHandle( |
| 279 | AllocClass(self, java_lang_Class.Get(), mirror::ObjectArray<mirror::Class>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 280 | class_array_class->SetComponentType(java_lang_Class.Get()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 281 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 282 | // java_lang_Object comes next so that object_array_class can be created. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 283 | Handle<mirror::Class> java_lang_Object(hs.NewHandle( |
| 284 | AllocClass(self, java_lang_Class.Get(), mirror::Object::ClassSize()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 285 | CHECK(java_lang_Object.Get() != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 286 | // backfill Object as the super class of Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 287 | java_lang_Class->SetSuperClass(java_lang_Object.Get()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 288 | java_lang_Object->SetStatus(mirror::Class::kStatusLoaded, self); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 289 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 290 | // Object[] next to hold class roots. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 291 | Handle<mirror::Class> object_array_class(hs.NewHandle( |
| 292 | AllocClass(self, java_lang_Class.Get(), mirror::ObjectArray<mirror::Object>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 293 | object_array_class->SetComponentType(java_lang_Object.Get()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 294 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 295 | // Setup the char (primitive) class to be used for char[]. |
| 296 | Handle<mirror::Class> char_class(hs.NewHandle( |
| 297 | AllocClass(self, java_lang_Class.Get(), mirror::Class::PrimitiveClassSize()))); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 298 | // The primitive char class won't be initialized by |
| 299 | // InitializePrimitiveClass until line 459, but strings (and |
| 300 | // internal char arrays) will be allocated before that and the |
| 301 | // component size, which is computed from the primitive type, needs |
| 302 | // to be set here. |
| 303 | char_class->SetPrimitiveType(Primitive::kPrimChar); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 304 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 305 | // Setup the char[] class to be used for String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 306 | Handle<mirror::Class> char_array_class(hs.NewHandle( |
| 307 | AllocClass(self, java_lang_Class.Get(), |
| 308 | mirror::Array::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 309 | char_array_class->SetComponentType(char_class.Get()); |
| 310 | mirror::CharArray::SetArrayClass(char_array_class.Get()); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 311 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 312 | // Setup String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 313 | Handle<mirror::Class> java_lang_String(hs.NewHandle( |
| 314 | AllocClass(self, java_lang_Class.Get(), mirror::String::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 315 | mirror::String::SetClass(java_lang_String.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 316 | java_lang_String->SetObjectSize(mirror::String::InstanceSize()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 317 | java_lang_String->SetStatus(mirror::Class::kStatusResolved, self); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 318 | |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 319 | // Setup Reference. |
| 320 | Handle<mirror::Class> java_lang_ref_Reference(hs.NewHandle( |
| 321 | AllocClass(self, java_lang_Class.Get(), mirror::Reference::ClassSize()))); |
| 322 | mirror::Reference::SetClass(java_lang_ref_Reference.Get()); |
| 323 | java_lang_ref_Reference->SetObjectSize(mirror::Reference::InstanceSize()); |
| 324 | java_lang_ref_Reference->SetStatus(mirror::Class::kStatusResolved, self); |
| 325 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 326 | // Create storage for root classes, save away our work so far (requires descriptors). |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 327 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class> >( |
| 328 | mirror::ObjectArray<mirror::Class>::Alloc(self, object_array_class.Get(), |
| 329 | kClassRootsMax)); |
| 330 | CHECK(!class_roots_.IsNull()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 331 | SetClassRoot(kJavaLangClass, java_lang_Class.Get()); |
| 332 | SetClassRoot(kJavaLangObject, java_lang_Object.Get()); |
| 333 | SetClassRoot(kClassArrayClass, class_array_class.Get()); |
| 334 | SetClassRoot(kObjectArrayClass, object_array_class.Get()); |
| 335 | SetClassRoot(kCharArrayClass, char_array_class.Get()); |
| 336 | SetClassRoot(kJavaLangString, java_lang_String.Get()); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 337 | SetClassRoot(kJavaLangRefReference, java_lang_ref_Reference.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 338 | |
| 339 | // Setup the primitive type classes. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 340 | SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass(self, Primitive::kPrimBoolean)); |
| 341 | SetClassRoot(kPrimitiveByte, CreatePrimitiveClass(self, Primitive::kPrimByte)); |
| 342 | SetClassRoot(kPrimitiveShort, CreatePrimitiveClass(self, Primitive::kPrimShort)); |
| 343 | SetClassRoot(kPrimitiveInt, CreatePrimitiveClass(self, Primitive::kPrimInt)); |
| 344 | SetClassRoot(kPrimitiveLong, CreatePrimitiveClass(self, Primitive::kPrimLong)); |
| 345 | SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass(self, Primitive::kPrimFloat)); |
| 346 | SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass(self, Primitive::kPrimDouble)); |
| 347 | SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass(self, Primitive::kPrimVoid)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 348 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 349 | // Create array interface entries to populate once we can load system classes. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 350 | array_iftable_ = GcRoot<mirror::IfTable>(AllocIfTable(self, 2)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 351 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 352 | // Create int array type for AllocDexCache (done in AppendToBootClassPath). |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 353 | Handle<mirror::Class> int_array_class(hs.NewHandle( |
| 354 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize()))); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 355 | int_array_class->SetComponentType(GetClassRoot(kPrimitiveInt)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 356 | mirror::IntArray::SetArrayClass(int_array_class.Get()); |
| 357 | SetClassRoot(kIntArrayClass, int_array_class.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 358 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 359 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 360 | |
Ian Rogers | 52813c9 | 2012-10-11 11:50:38 -0700 | [diff] [blame] | 361 | // Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 362 | Handle<mirror::Class> java_lang_DexCache(hs.NewHandle( |
| 363 | AllocClass(self, java_lang_Class.Get(), mirror::DexCache::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 364 | SetClassRoot(kJavaLangDexCache, java_lang_DexCache.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 365 | java_lang_DexCache->SetObjectSize(mirror::DexCache::InstanceSize()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 366 | java_lang_DexCache->SetStatus(mirror::Class::kStatusResolved, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 367 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 368 | // Constructor, Field, Method, and AbstractMethod are necessary so |
| 369 | // that FindClass can link members. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 370 | Handle<mirror::Class> java_lang_reflect_ArtField(hs.NewHandle( |
| 371 | AllocClass(self, java_lang_Class.Get(), mirror::ArtField::ClassSize()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 372 | CHECK(java_lang_reflect_ArtField.Get() != nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 373 | java_lang_reflect_ArtField->SetObjectSize(mirror::ArtField::InstanceSize()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 374 | SetClassRoot(kJavaLangReflectArtField, java_lang_reflect_ArtField.Get()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 375 | java_lang_reflect_ArtField->SetStatus(mirror::Class::kStatusResolved, self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 376 | mirror::ArtField::SetClass(java_lang_reflect_ArtField.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 377 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 378 | Handle<mirror::Class> java_lang_reflect_ArtMethod(hs.NewHandle( |
| 379 | AllocClass(self, java_lang_Class.Get(), mirror::ArtMethod::ClassSize()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 380 | CHECK(java_lang_reflect_ArtMethod.Get() != nullptr); |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 381 | size_t pointer_size = GetInstructionSetPointerSize(Runtime::Current()->GetInstructionSet()); |
| 382 | java_lang_reflect_ArtMethod->SetObjectSize(mirror::ArtMethod::InstanceSize(pointer_size)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 383 | SetClassRoot(kJavaLangReflectArtMethod, java_lang_reflect_ArtMethod.Get()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 384 | java_lang_reflect_ArtMethod->SetStatus(mirror::Class::kStatusResolved, self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 385 | mirror::ArtMethod::SetClass(java_lang_reflect_ArtMethod.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 386 | |
| 387 | // Set up array classes for string, field, method |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 388 | Handle<mirror::Class> object_array_string(hs.NewHandle( |
| 389 | AllocClass(self, java_lang_Class.Get(), |
| 390 | mirror::ObjectArray<mirror::String>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 391 | object_array_string->SetComponentType(java_lang_String.Get()); |
| 392 | SetClassRoot(kJavaLangStringArrayClass, object_array_string.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 393 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 394 | Handle<mirror::Class> object_array_art_method(hs.NewHandle( |
| 395 | AllocClass(self, java_lang_Class.Get(), |
| 396 | mirror::ObjectArray<mirror::ArtMethod>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 397 | object_array_art_method->SetComponentType(java_lang_reflect_ArtMethod.Get()); |
| 398 | SetClassRoot(kJavaLangReflectArtMethodArrayClass, object_array_art_method.Get()); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 399 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 400 | Handle<mirror::Class> object_array_art_field(hs.NewHandle( |
| 401 | AllocClass(self, java_lang_Class.Get(), |
| 402 | mirror::ObjectArray<mirror::ArtField>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 403 | object_array_art_field->SetComponentType(java_lang_reflect_ArtField.Get()); |
| 404 | SetClassRoot(kJavaLangReflectArtFieldArrayClass, object_array_art_field.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 405 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 406 | // Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create |
| 407 | // DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses |
| 408 | // these roots. |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 409 | CHECK_NE(0U, boot_class_path.size()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 410 | for (auto& dex_file : boot_class_path) { |
| 411 | CHECK(dex_file.get() != nullptr); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 412 | AppendToBootClassPath(self, *dex_file); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 413 | opened_dex_files_.push_back(std::move(dex_file)); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 414 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 415 | |
| 416 | // now we can use FindSystemClass |
| 417 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 418 | // run char class through InitializePrimitiveClass to finish init |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 419 | InitializePrimitiveClass(char_class.Get(), Primitive::kPrimChar); |
| 420 | SetClassRoot(kPrimitiveChar, char_class.Get()); // needs descriptor |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 421 | |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 422 | // Create runtime resolution and imt conflict methods. Also setup the default imt. |
| 423 | Runtime* runtime = Runtime::Current(); |
| 424 | runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); |
| 425 | runtime->SetImtConflictMethod(runtime->CreateImtConflictMethod()); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 426 | runtime->SetImtUnimplementedMethod(runtime->CreateImtConflictMethod()); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 427 | runtime->SetDefaultImt(runtime->CreateDefaultImt(this)); |
| 428 | |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 429 | // Set up GenericJNI entrypoint. That is mainly a hack for common_compiler_test.h so that |
| 430 | // we do not need friend classes or a publicly exposed setter. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 431 | quick_generic_jni_trampoline_ = GetQuickGenericJniStub(); |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 432 | if (!runtime->IsAotCompiler()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 433 | // 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] | 434 | quick_resolution_trampoline_ = GetQuickResolutionStub(); |
| 435 | quick_imt_conflict_trampoline_ = GetQuickImtConflictStub(); |
| 436 | quick_to_interpreter_bridge_trampoline_ = GetQuickToInterpreterBridge(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 437 | } |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 438 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 439 | // 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] | 440 | java_lang_Object->SetStatus(mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 441 | mirror::Class* Object_class = FindSystemClass(self, "Ljava/lang/Object;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 442 | CHECK_EQ(java_lang_Object.Get(), Object_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 443 | CHECK_EQ(java_lang_Object->GetObjectSize(), mirror::Object::InstanceSize()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 444 | java_lang_String->SetStatus(mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 445 | mirror::Class* String_class = FindSystemClass(self, "Ljava/lang/String;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 446 | std::ostringstream os1, os2; |
| 447 | java_lang_String->DumpClass(os1, mirror::Class::kDumpClassFullDetail); |
| 448 | String_class->DumpClass(os2, mirror::Class::kDumpClassFullDetail); |
| 449 | CHECK_EQ(java_lang_String.Get(), String_class) << os1.str() << "\n\n" << os2.str(); |
| 450 | CHECK_EQ(java_lang_String->GetObjectSize(), mirror::String::InstanceSize()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 451 | java_lang_DexCache->SetStatus(mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 452 | mirror::Class* DexCache_class = FindSystemClass(self, "Ljava/lang/DexCache;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 453 | CHECK_EQ(java_lang_String.Get(), String_class); |
| 454 | CHECK_EQ(java_lang_DexCache.Get(), DexCache_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 455 | CHECK_EQ(java_lang_DexCache->GetObjectSize(), mirror::DexCache::InstanceSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 456 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 457 | // 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] | 458 | SetClassRoot(kBooleanArrayClass, FindSystemClass(self, "[Z")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 459 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 460 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 461 | SetClassRoot(kByteArrayClass, FindSystemClass(self, "[B")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 462 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 463 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 464 | mirror::Class* found_char_array_class = FindSystemClass(self, "[C"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 465 | CHECK_EQ(char_array_class.Get(), found_char_array_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 466 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 467 | SetClassRoot(kShortArrayClass, FindSystemClass(self, "[S")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 468 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 469 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 470 | mirror::Class* found_int_array_class = FindSystemClass(self, "[I"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 471 | CHECK_EQ(int_array_class.Get(), found_int_array_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 472 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 473 | SetClassRoot(kLongArrayClass, FindSystemClass(self, "[J")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 474 | mirror::LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 475 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 476 | SetClassRoot(kFloatArrayClass, FindSystemClass(self, "[F")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 477 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 478 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 479 | SetClassRoot(kDoubleArrayClass, FindSystemClass(self, "[D")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 480 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 481 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 482 | mirror::Class* found_class_array_class = FindSystemClass(self, "[Ljava/lang/Class;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 483 | CHECK_EQ(class_array_class.Get(), found_class_array_class); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 484 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 485 | mirror::Class* found_object_array_class = FindSystemClass(self, "[Ljava/lang/Object;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 486 | CHECK_EQ(object_array_class.Get(), found_object_array_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 487 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 488 | // Setup the single, global copy of "iftable". |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 489 | mirror::Class* java_lang_Cloneable = FindSystemClass(self, "Ljava/lang/Cloneable;"); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 490 | CHECK(java_lang_Cloneable != nullptr); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 491 | mirror::Class* java_io_Serializable = FindSystemClass(self, "Ljava/io/Serializable;"); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 492 | CHECK(java_io_Serializable != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 493 | // We assume that Cloneable/Serializable don't have superinterfaces -- normally we'd have to |
| 494 | // crawl up and explicitly list all of the supers as well. |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 495 | { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 496 | mirror::IfTable* array_iftable = array_iftable_.Read(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 497 | array_iftable->SetInterface(0, java_lang_Cloneable); |
| 498 | array_iftable->SetInterface(1, java_io_Serializable); |
| 499 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 500 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 501 | // Sanity check Class[] and Object[]'s interfaces. |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 502 | CHECK_EQ(java_lang_Cloneable, mirror::Class::GetDirectInterface(self, class_array_class, 0)); |
| 503 | CHECK_EQ(java_io_Serializable, mirror::Class::GetDirectInterface(self, class_array_class, 1)); |
| 504 | CHECK_EQ(java_lang_Cloneable, mirror::Class::GetDirectInterface(self, object_array_class, 0)); |
| 505 | 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] | 506 | // Run Class, ArtField, and ArtMethod through FindSystemClass. This initializes their |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 507 | // dex_cache_ fields and register them in class_table_. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 508 | mirror::Class* Class_class = FindSystemClass(self, "Ljava/lang/Class;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 509 | CHECK_EQ(java_lang_Class.Get(), Class_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 510 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 511 | java_lang_reflect_ArtMethod->SetStatus(mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 512 | mirror::Class* Art_method_class = FindSystemClass(self, "Ljava/lang/reflect/ArtMethod;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 513 | CHECK_EQ(java_lang_reflect_ArtMethod.Get(), Art_method_class); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 514 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 515 | java_lang_reflect_ArtField->SetStatus(mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 516 | mirror::Class* Art_field_class = FindSystemClass(self, "Ljava/lang/reflect/ArtField;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 517 | CHECK_EQ(java_lang_reflect_ArtField.Get(), Art_field_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 518 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 519 | mirror::Class* String_array_class = |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 520 | FindSystemClass(self, GetClassRootDescriptor(kJavaLangStringArrayClass)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 521 | CHECK_EQ(object_array_string.Get(), String_array_class); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 522 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 523 | mirror::Class* Art_method_array_class = |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 524 | FindSystemClass(self, GetClassRootDescriptor(kJavaLangReflectArtMethodArrayClass)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 525 | CHECK_EQ(object_array_art_method.Get(), Art_method_array_class); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 526 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 527 | mirror::Class* Art_field_array_class = |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 528 | FindSystemClass(self, GetClassRootDescriptor(kJavaLangReflectArtFieldArrayClass)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 529 | CHECK_EQ(object_array_art_field.Get(), Art_field_array_class); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 530 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 531 | // End of special init trickery, subsequent classes may be loaded via FindSystemClass. |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 532 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 533 | // Create java.lang.reflect.Proxy root. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 534 | mirror::Class* java_lang_reflect_Proxy = FindSystemClass(self, "Ljava/lang/reflect/Proxy;"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 535 | SetClassRoot(kJavaLangReflectProxy, java_lang_reflect_Proxy); |
| 536 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 537 | // 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] | 538 | // finish initializing Reference class |
| 539 | java_lang_ref_Reference->SetStatus(mirror::Class::kStatusNotReady, self); |
| 540 | mirror::Class* Reference_class = FindSystemClass(self, "Ljava/lang/ref/Reference;"); |
| 541 | CHECK_EQ(java_lang_ref_Reference.Get(), Reference_class); |
| 542 | CHECK_EQ(java_lang_ref_Reference->GetObjectSize(), mirror::Reference::InstanceSize()); |
| 543 | CHECK_EQ(java_lang_ref_Reference->GetClassSize(), mirror::Reference::ClassSize()); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 544 | mirror::Class* java_lang_ref_FinalizerReference = |
| 545 | FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 546 | java_lang_ref_FinalizerReference->SetAccessFlags( |
| 547 | java_lang_ref_FinalizerReference->GetAccessFlags() | |
| 548 | kAccClassIsReference | kAccClassIsFinalizerReference); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 549 | mirror::Class* java_lang_ref_PhantomReference = |
| 550 | FindSystemClass(self, "Ljava/lang/ref/PhantomReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 551 | java_lang_ref_PhantomReference->SetAccessFlags( |
| 552 | java_lang_ref_PhantomReference->GetAccessFlags() | |
| 553 | kAccClassIsReference | kAccClassIsPhantomReference); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 554 | mirror::Class* java_lang_ref_SoftReference = |
| 555 | FindSystemClass(self, "Ljava/lang/ref/SoftReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 556 | java_lang_ref_SoftReference->SetAccessFlags( |
| 557 | java_lang_ref_SoftReference->GetAccessFlags() | kAccClassIsReference); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 558 | mirror::Class* java_lang_ref_WeakReference = |
| 559 | FindSystemClass(self, "Ljava/lang/ref/WeakReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 560 | java_lang_ref_WeakReference->SetAccessFlags( |
| 561 | java_lang_ref_WeakReference->GetAccessFlags() | |
| 562 | kAccClassIsReference | kAccClassIsWeakReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 563 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 564 | // Setup the ClassLoader, verifying the object_size_. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 565 | mirror::Class* java_lang_ClassLoader = FindSystemClass(self, "Ljava/lang/ClassLoader;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 566 | CHECK_EQ(java_lang_ClassLoader->GetObjectSize(), mirror::ClassLoader::InstanceSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 567 | SetClassRoot(kJavaLangClassLoader, java_lang_ClassLoader); |
| 568 | |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 569 | // Set up java.lang.Throwable, java.lang.ClassNotFoundException, and |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 570 | // java.lang.StackTraceElement as a convenience. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 571 | SetClassRoot(kJavaLangThrowable, FindSystemClass(self, "Ljava/lang/Throwable;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 572 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 573 | SetClassRoot(kJavaLangClassNotFoundException, |
| 574 | FindSystemClass(self, "Ljava/lang/ClassNotFoundException;")); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 575 | SetClassRoot(kJavaLangStackTraceElement, FindSystemClass(self, "Ljava/lang/StackTraceElement;")); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 576 | SetClassRoot(kJavaLangStackTraceElementArrayClass, |
| 577 | FindSystemClass(self, "[Ljava/lang/StackTraceElement;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 578 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 579 | |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 580 | // Ensure void type is resolved in the core's dex cache so java.lang.Void is correctly |
| 581 | // initialized. |
| 582 | { |
| 583 | const DexFile& dex_file = java_lang_Object->GetDexFile(); |
| 584 | const DexFile::StringId* void_string_id = dex_file.FindStringId("V"); |
| 585 | CHECK(void_string_id != nullptr); |
| 586 | uint32_t void_string_index = dex_file.GetIndexForStringId(*void_string_id); |
| 587 | const DexFile::TypeId* void_type_id = dex_file.FindTypeId(void_string_index); |
| 588 | CHECK(void_type_id != nullptr); |
| 589 | uint16_t void_type_idx = dex_file.GetIndexForTypeId(*void_type_id); |
| 590 | // Now we resolve void type so the dex cache contains it. We use java.lang.Object class |
| 591 | // as referrer so the used dex cache is core's one. |
| 592 | mirror::Class* resolved_type = ResolveType(dex_file, void_type_idx, java_lang_Object.Get()); |
| 593 | CHECK_EQ(resolved_type, GetClassRoot(kPrimitiveVoid)); |
| 594 | self->AssertNoPendingException(); |
| 595 | } |
| 596 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 597 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 598 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 599 | VLOG(startup) << "ClassLinker::InitFromCompiler exiting"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 602 | void ClassLinker::FinishInit(Thread* self) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 603 | VLOG(startup) << "ClassLinker::FinishInit entering"; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 604 | |
| 605 | // Let the heap know some key offsets into java.lang.ref instances |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 606 | // Note: we hard code the field indexes here rather than using FindInstanceField |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 607 | // as the types of the field can't be resolved prior to the runtime being |
| 608 | // fully initialized |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 609 | mirror::Class* java_lang_ref_Reference = GetClassRoot(kJavaLangRefReference); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 610 | mirror::Class* java_lang_ref_FinalizerReference = |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 611 | FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 612 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 613 | mirror::ArtField* pendingNext = java_lang_ref_Reference->GetInstanceField(0); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 614 | CHECK_STREQ(pendingNext->GetName(), "pendingNext"); |
| 615 | CHECK_STREQ(pendingNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 616 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 617 | mirror::ArtField* queue = java_lang_ref_Reference->GetInstanceField(1); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 618 | CHECK_STREQ(queue->GetName(), "queue"); |
| 619 | CHECK_STREQ(queue->GetTypeDescriptor(), "Ljava/lang/ref/ReferenceQueue;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 620 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 621 | mirror::ArtField* queueNext = java_lang_ref_Reference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 622 | CHECK_STREQ(queueNext->GetName(), "queueNext"); |
| 623 | CHECK_STREQ(queueNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 624 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 625 | mirror::ArtField* referent = java_lang_ref_Reference->GetInstanceField(3); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 626 | CHECK_STREQ(referent->GetName(), "referent"); |
| 627 | CHECK_STREQ(referent->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 628 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 629 | mirror::ArtField* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 630 | CHECK_STREQ(zombie->GetName(), "zombie"); |
| 631 | CHECK_STREQ(zombie->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 632 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 633 | // ensure all class_roots_ are initialized |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 634 | for (size_t i = 0; i < kClassRootsMax; i++) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 635 | ClassRoot class_root = static_cast<ClassRoot>(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 636 | mirror::Class* klass = GetClassRoot(class_root); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 637 | CHECK(klass != nullptr); |
| 638 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != nullptr); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 639 | // note SetClassRoot does additional validation. |
| 640 | // if possible add new checks there to catch errors early |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 643 | CHECK(!array_iftable_.IsNull()); |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 644 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 645 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 646 | // that Object, Class, and Object[] are setup |
| 647 | init_done_ = true; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 648 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 649 | VLOG(startup) << "ClassLinker::FinishInit exiting"; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 652 | void ClassLinker::RunRootClinits() { |
| 653 | Thread* self = Thread::Current(); |
| 654 | for (size_t i = 0; i < ClassLinker::kClassRootsMax; ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 655 | mirror::Class* c = GetClassRoot(ClassRoot(i)); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 656 | if (!c->IsArrayClass() && !c->IsPrimitive()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 657 | StackHandleScope<1> hs(self); |
| 658 | Handle<mirror::Class> h_class(hs.NewHandle(GetClassRoot(ClassRoot(i)))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 659 | EnsureInitialized(self, h_class, true, true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 660 | self->AssertNoPendingException(); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | } |
| 664 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 665 | bool ClassLinker::GenerateOatFile(const char* dex_filename, |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 666 | int oat_fd, |
Vladimir Marko | 60836d5 | 2014-01-16 15:53:38 +0000 | [diff] [blame] | 667 | const char* oat_cache_filename, |
| 668 | std::string* error_msg) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 669 | Locks::mutator_lock_->AssertNotHeld(Thread::Current()); // Avoid starving GC. |
Tsu Chiang Chuang | 12e6d74 | 2014-05-22 10:22:25 -0700 | [diff] [blame] | 670 | std::string dex2oat(Runtime::Current()->GetCompilerExecutable()); |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 671 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 672 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 673 | std::string boot_image_option("--boot-image="); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 674 | if (heap->GetImageSpace() == nullptr) { |
| 675 | // TODO If we get a dex2dex compiler working we could maybe use that, OTOH since we are likely |
| 676 | // out of space anyway it might not matter. |
| 677 | *error_msg = StringPrintf("Cannot create oat file for '%s' because we are running " |
| 678 | "without an image.", dex_filename); |
| 679 | return false; |
| 680 | } |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 681 | boot_image_option += heap->GetImageSpace()->GetImageLocation(); |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 682 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 683 | std::string dex_file_option("--dex-file="); |
| 684 | dex_file_option += dex_filename; |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 685 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 686 | std::string oat_fd_option("--oat-fd="); |
| 687 | StringAppendF(&oat_fd_option, "%d", oat_fd); |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 688 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 689 | std::string oat_location_option("--oat-location="); |
| 690 | oat_location_option += oat_cache_filename; |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 691 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 692 | std::vector<std::string> argv; |
| 693 | argv.push_back(dex2oat); |
| 694 | argv.push_back("--runtime-arg"); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 695 | argv.push_back("-classpath"); |
| 696 | argv.push_back("--runtime-arg"); |
Brian Carlstrom | 35d8b8e | 2014-02-25 10:51:11 -0800 | [diff] [blame] | 697 | argv.push_back(Runtime::Current()->GetClassPathString()); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 698 | |
Ian Rogers | 8afeb85 | 2014-04-02 14:55:49 -0700 | [diff] [blame] | 699 | Runtime::Current()->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 700 | |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 701 | if (!Runtime::Current()->IsVerificationEnabled()) { |
| 702 | argv.push_back("--compiler-filter=verify-none"); |
| 703 | } |
| 704 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 705 | if (Runtime::Current()->MustRelocateIfPossible()) { |
| 706 | argv.push_back("--runtime-arg"); |
| 707 | argv.push_back("-Xrelocate"); |
| 708 | } else { |
| 709 | argv.push_back("--runtime-arg"); |
| 710 | argv.push_back("-Xnorelocate"); |
| 711 | } |
| 712 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 713 | if (!kIsTargetBuild) { |
| 714 | argv.push_back("--host"); |
buzbee | a024a06 | 2013-07-31 10:47:37 -0700 | [diff] [blame] | 715 | } |
Ian Rogers | 8afeb85 | 2014-04-02 14:55:49 -0700 | [diff] [blame] | 716 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 717 | argv.push_back(boot_image_option); |
| 718 | argv.push_back(dex_file_option); |
| 719 | argv.push_back(oat_fd_option); |
| 720 | argv.push_back(oat_location_option); |
| 721 | const std::vector<std::string>& compiler_options = Runtime::Current()->GetCompilerOptions(); |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 722 | for (size_t i = 0; i < compiler_options.size(); ++i) { |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 723 | argv.push_back(compiler_options[i].c_str()); |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 724 | } |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 725 | |
Andreas Gampe | a696c0a | 2014-12-10 20:51:45 -0800 | [diff] [blame] | 726 | if (!Exec(argv, error_msg)) { |
| 727 | // Manually delete the file. Ensures there is no garbage left over if the process unexpectedly |
| 728 | // died. Ignore unlink failure, propagate the original error. |
| 729 | TEMP_FAILURE_RETRY(unlink(oat_cache_filename)); |
| 730 | return false; |
| 731 | } |
| 732 | |
| 733 | return true; |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 734 | } |
| 735 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 736 | const OatFile* ClassLinker::RegisterOatFile(const OatFile* oat_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 737 | WriterMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 738 | if (kIsDebugBuild) { |
| 739 | for (size_t i = 0; i < oat_files_.size(); ++i) { |
| 740 | CHECK_NE(oat_file, oat_files_[i]) << oat_file->GetLocation(); |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 741 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 742 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 743 | VLOG(class_linker) << "Registering " << oat_file->GetLocation(); |
| 744 | oat_files_.push_back(oat_file); |
| 745 | return oat_file; |
Brian Carlstrom | 866c862 | 2012-01-06 16:35:13 -0800 | [diff] [blame] | 746 | } |
| 747 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 748 | OatFile& ClassLinker::GetImageOatFile(gc::space::ImageSpace* space) { |
| 749 | VLOG(startup) << "ClassLinker::GetImageOatFile entering"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 750 | OatFile* oat_file = space->ReleaseOatFile(); |
| 751 | CHECK_EQ(RegisterOatFile(oat_file), oat_file); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 752 | VLOG(startup) << "ClassLinker::GetImageOatFile exiting"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 753 | return *oat_file; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 756 | const OatFile::OatDexFile* ClassLinker::FindOpenedOatDexFileForDexFile(const DexFile& dex_file) { |
Brian Carlstrom | 08cbf66 | 2013-12-10 16:52:57 -0800 | [diff] [blame] | 757 | const char* dex_location = dex_file.GetLocation().c_str(); |
| 758 | uint32_t dex_location_checksum = dex_file.GetLocationChecksum(); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 759 | return FindOpenedOatDexFile(nullptr, dex_location, &dex_location_checksum); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 760 | } |
| 761 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 762 | const OatFile::OatDexFile* ClassLinker::FindOpenedOatDexFile(const char* oat_location, |
| 763 | const char* dex_location, |
| 764 | const uint32_t* dex_location_checksum) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 765 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 766 | for (const OatFile* oat_file : oat_files_) { |
| 767 | DCHECK(oat_file != nullptr); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 768 | |
| 769 | if (oat_location != nullptr) { |
| 770 | if (oat_file->GetLocation() != oat_location) { |
| 771 | continue; |
| 772 | } |
| 773 | } |
| 774 | |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 775 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, |
Brian Carlstrom | 08cbf66 | 2013-12-10 16:52:57 -0800 | [diff] [blame] | 776 | dex_location_checksum, |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 777 | false); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 778 | if (oat_dex_file != nullptr) { |
| 779 | return oat_dex_file; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 780 | } |
| 781 | } |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 782 | return nullptr; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 783 | } |
| 784 | |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 785 | |
| 786 | // Loads all multi dex files from the given oat file returning true on success. |
| 787 | // |
| 788 | // Parameters: |
| 789 | // oat_file - the oat file to load from |
| 790 | // dex_location - the dex location used to generate the oat file |
| 791 | // dex_location_checksum - the checksum of the dex_location (may be null for pre-opted files) |
| 792 | // generated - whether or not the oat_file existed before or was just (re)generated |
| 793 | // error_msgs - any error messages will be appended here |
| 794 | // dex_files - the loaded dex_files will be appended here (only if the loading succeeds) |
| 795 | static bool LoadMultiDexFilesFromOatFile(const OatFile* oat_file, |
| 796 | const char* dex_location, |
| 797 | const uint32_t* dex_location_checksum, |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 798 | bool generated, |
| 799 | std::vector<std::string>* error_msgs, |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 800 | std::vector<std::unique_ptr<const DexFile>>* dex_files) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 801 | if (oat_file == nullptr) { |
| 802 | return false; |
| 803 | } |
| 804 | |
| 805 | size_t old_size = dex_files->size(); // To rollback on error. |
| 806 | |
| 807 | bool success = true; |
| 808 | for (size_t i = 0; success; ++i) { |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 809 | std::string next_name_str = DexFile::GetMultiDexClassesDexName(i, dex_location); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 810 | const char* next_name = next_name_str.c_str(); |
| 811 | |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 812 | uint32_t next_location_checksum; |
| 813 | uint32_t* next_location_checksum_pointer = &next_location_checksum; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 814 | std::string error_msg; |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 815 | if ((i == 0) && (strcmp(next_name, dex_location) == 0)) { |
| 816 | // When i=0 the multidex name should be the same as the location name. We already have the |
| 817 | // checksum it so we don't need to recompute it. |
| 818 | if (dex_location_checksum == nullptr) { |
| 819 | next_location_checksum_pointer = nullptr; |
| 820 | } else { |
| 821 | next_location_checksum = *dex_location_checksum; |
| 822 | } |
| 823 | } else if (!DexFile::GetChecksum(next_name, next_location_checksum_pointer, &error_msg)) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 824 | DCHECK_EQ(false, i == 0 && generated); |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 825 | next_location_checksum_pointer = nullptr; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(next_name, nullptr, false); |
| 829 | |
| 830 | if (oat_dex_file == nullptr) { |
| 831 | if (i == 0 && generated) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 832 | 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] | 833 | " file'%s'", dex_location, next_location_checksum, |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 834 | oat_file->GetLocation().c_str()); |
| 835 | error_msgs->push_back(error_msg); |
| 836 | } |
| 837 | break; // Not found, done. |
| 838 | } |
| 839 | |
| 840 | // Checksum test. Test must succeed when generated. |
| 841 | success = !generated; |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 842 | if (next_location_checksum_pointer != nullptr) { |
| 843 | success = next_location_checksum == oat_dex_file->GetDexFileLocationChecksum(); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | if (success) { |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 847 | std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg); |
| 848 | if (dex_file.get() == nullptr) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 849 | success = false; |
| 850 | error_msgs->push_back(error_msg); |
| 851 | } else { |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 852 | dex_files->push_back(std::move(dex_file)); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | |
| 856 | // When we generated the file, we expect success, or something is terribly wrong. |
| 857 | CHECK_EQ(false, generated && !success) |
| 858 | << "dex_location=" << next_name << " oat_location=" << oat_file->GetLocation().c_str() |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 859 | << std::hex << " dex_location_checksum=" << next_location_checksum |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 860 | << " OatDexFile::GetLocationChecksum()=" << oat_dex_file->GetDexFileLocationChecksum(); |
| 861 | } |
| 862 | |
| 863 | if (dex_files->size() == old_size) { |
| 864 | success = false; // We did not even find classes.dex |
| 865 | } |
| 866 | |
| 867 | if (success) { |
| 868 | return true; |
| 869 | } else { |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 870 | dex_files->erase(dex_files->begin() + old_size, dex_files->end()); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 871 | return false; |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | // Multidex files make it possible that some, but not all, dex files can be broken/outdated. This |
| 876 | // complicates the loading process, as we should not use an iterative loading process, because that |
| 877 | // would register the oat file and dex files that come before the broken one. Instead, check all |
| 878 | // multidex ahead of time. |
| 879 | bool ClassLinker::OpenDexFilesFromOat(const char* dex_location, const char* oat_location, |
| 880 | std::vector<std::string>* error_msgs, |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 881 | std::vector<std::unique_ptr<const DexFile>>* dex_files) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 882 | // 1) Check whether we have an open oat file. |
| 883 | // This requires a dex checksum, use the "primary" one. |
| 884 | uint32_t dex_location_checksum; |
| 885 | uint32_t* dex_location_checksum_pointer = &dex_location_checksum; |
| 886 | bool have_checksum = true; |
| 887 | std::string checksum_error_msg; |
| 888 | if (!DexFile::GetChecksum(dex_location, dex_location_checksum_pointer, &checksum_error_msg)) { |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 889 | // 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] | 890 | dex_location_checksum_pointer = nullptr; |
| 891 | have_checksum = false; |
| 892 | } |
| 893 | |
| 894 | bool needs_registering = false; |
| 895 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 896 | const OatFile::OatDexFile* oat_dex_file = FindOpenedOatDexFile(oat_location, dex_location, |
| 897 | dex_location_checksum_pointer); |
| 898 | std::unique_ptr<const OatFile> open_oat_file( |
| 899 | oat_dex_file != nullptr ? oat_dex_file->GetOatFile() : nullptr); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 900 | |
| 901 | // 2) If we do not have an open one, maybe there's one on disk already. |
| 902 | |
| 903 | // In case the oat file is not open, we play a locking game here so |
| 904 | // that if two different processes race to load and register or generate |
| 905 | // (or worse, one tries to open a partial generated file) we will be okay. |
| 906 | // This is actually common with apps that use DexClassLoader to work |
| 907 | // around the dex method reference limit and that have a background |
| 908 | // service running in a separate process. |
| 909 | ScopedFlock scoped_flock; |
| 910 | |
| 911 | if (open_oat_file.get() == nullptr) { |
| 912 | if (oat_location != nullptr) { |
| 913 | // Can only do this if we have a checksum, else error. |
| 914 | if (!have_checksum) { |
| 915 | error_msgs->push_back(checksum_error_msg); |
| 916 | return false; |
| 917 | } |
| 918 | |
| 919 | std::string error_msg; |
| 920 | |
| 921 | // We are loading or creating one in the future. Time to set up the file lock. |
| 922 | if (!scoped_flock.Init(oat_location, &error_msg)) { |
| 923 | error_msgs->push_back(error_msg); |
| 924 | return false; |
| 925 | } |
| 926 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 927 | // 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] | 928 | open_oat_file.reset(FindOatFileInOatLocationForDexFile(dex_location, dex_location_checksum, |
| 929 | oat_location, &error_msg)); |
| 930 | |
| 931 | if (open_oat_file.get() == nullptr) { |
| 932 | std::string compound_msg = StringPrintf("Failed to find dex file '%s' in oat location '%s': %s", |
| 933 | dex_location, oat_location, error_msg.c_str()); |
| 934 | VLOG(class_linker) << compound_msg; |
| 935 | error_msgs->push_back(compound_msg); |
| 936 | } |
| 937 | } else { |
| 938 | // TODO: What to lock here? |
Calin Juravle | c54aea7 | 2014-07-16 14:45:03 +0100 | [diff] [blame] | 939 | bool obsolete_file_cleanup_failed; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 940 | open_oat_file.reset(FindOatFileContainingDexFileFromDexLocation(dex_location, |
| 941 | dex_location_checksum_pointer, |
Calin Juravle | c54aea7 | 2014-07-16 14:45:03 +0100 | [diff] [blame] | 942 | kRuntimeISA, error_msgs, |
| 943 | &obsolete_file_cleanup_failed)); |
| 944 | // There's no point in going forward and eventually try to regenerate the |
| 945 | // file if we couldn't remove the obsolete one. Mostly likely we will fail |
| 946 | // with the same error when trying to write the new file. |
Calin Juravle | c54aea7 | 2014-07-16 14:45:03 +0100 | [diff] [blame] | 947 | // TODO: should we maybe do this only when we get permission issues? (i.e. EACCESS). |
| 948 | if (obsolete_file_cleanup_failed) { |
| 949 | return false; |
| 950 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 951 | } |
| 952 | needs_registering = true; |
| 953 | } |
| 954 | |
| 955 | // 3) If we have an oat file, check all contained multidex files for our dex_location. |
| 956 | // Note: LoadMultiDexFilesFromOatFile will check for nullptr in the first argument. |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 957 | bool success = LoadMultiDexFilesFromOatFile(open_oat_file.get(), dex_location, |
| 958 | dex_location_checksum_pointer, |
| 959 | false, error_msgs, dex_files); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 960 | if (success) { |
| 961 | const OatFile* oat_file = open_oat_file.release(); // Avoid deleting it. |
| 962 | if (needs_registering) { |
| 963 | // We opened the oat file, so we must register it. |
| 964 | RegisterOatFile(oat_file); |
| 965 | } |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 966 | // If the file isn't executable we failed patchoat but did manage to get the dex files. |
| 967 | return oat_file->IsExecutable(); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 968 | } else { |
| 969 | if (needs_registering) { |
| 970 | // We opened it, delete it. |
| 971 | open_oat_file.reset(); |
| 972 | } else { |
| 973 | open_oat_file.release(); // Do not delete open oat files. |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | // 4) If it's not the case (either no oat file or mismatches), regenerate and load. |
| 978 | |
| 979 | // Need a checksum, fail else. |
| 980 | if (!have_checksum) { |
| 981 | error_msgs->push_back(checksum_error_msg); |
| 982 | return false; |
| 983 | } |
| 984 | |
| 985 | // Look in cache location if no oat_location is given. |
| 986 | std::string cache_location; |
| 987 | if (oat_location == nullptr) { |
| 988 | // Use the dalvik cache. |
| 989 | const std::string dalvik_cache(GetDalvikCacheOrDie(GetInstructionSetString(kRuntimeISA))); |
| 990 | cache_location = GetDalvikCacheFilenameOrDie(dex_location, dalvik_cache.c_str()); |
| 991 | oat_location = cache_location.c_str(); |
| 992 | } |
| 993 | |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 994 | bool has_flock = true; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 995 | // Definitely need to lock now. |
| 996 | if (!scoped_flock.HasFile()) { |
| 997 | std::string error_msg; |
| 998 | if (!scoped_flock.Init(oat_location, &error_msg)) { |
| 999 | error_msgs->push_back(error_msg); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1000 | has_flock = false; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1001 | } |
| 1002 | } |
| 1003 | |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1004 | if (Runtime::Current()->IsDex2OatEnabled() && has_flock && scoped_flock.HasFile()) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 1005 | // Create the oat file. |
| 1006 | open_oat_file.reset(CreateOatFileForDexLocation(dex_location, scoped_flock.GetFile()->Fd(), |
| 1007 | oat_location, error_msgs)); |
| 1008 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1009 | |
| 1010 | // Failed, bail. |
| 1011 | if (open_oat_file.get() == nullptr) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 1012 | std::string error_msg; |
| 1013 | // dex2oat was disabled or crashed. Add the dex file in the list of dex_files to make progress. |
| 1014 | DexFile::Open(dex_location, dex_location, &error_msg, dex_files); |
| 1015 | error_msgs->push_back(error_msg); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1016 | return false; |
| 1017 | } |
| 1018 | |
| 1019 | // Try to load again, but stronger checks. |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1020 | success = LoadMultiDexFilesFromOatFile(open_oat_file.get(), dex_location, |
| 1021 | dex_location_checksum_pointer, |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 1022 | true, error_msgs, dex_files); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1023 | if (success) { |
| 1024 | RegisterOatFile(open_oat_file.release()); |
| 1025 | return true; |
| 1026 | } else { |
| 1027 | return false; |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | const OatFile* ClassLinker::FindOatFileInOatLocationForDexFile(const char* dex_location, |
| 1032 | uint32_t dex_location_checksum, |
| 1033 | const char* oat_location, |
| 1034 | std::string* error_msg) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1035 | std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_location, oat_location, nullptr, nullptr, |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 1036 | !Runtime::Current()->IsAotCompiler(), error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1037 | if (oat_file.get() == nullptr) { |
| 1038 | *error_msg = StringPrintf("Failed to find existing oat file at %s: %s", oat_location, |
| 1039 | error_msg->c_str()); |
| 1040 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1041 | } |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 1042 | Runtime* runtime = Runtime::Current(); |
Alex Light | 7adb7ac | 2014-08-29 10:28:25 -0700 | [diff] [blame] | 1043 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace(); |
| 1044 | if (image_space != nullptr) { |
| 1045 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1046 | uint32_t expected_image_oat_checksum = image_header.GetOatChecksum(); |
| 1047 | uint32_t actual_image_oat_checksum = oat_file->GetOatHeader().GetImageFileLocationOatChecksum(); |
| 1048 | if (expected_image_oat_checksum != actual_image_oat_checksum) { |
| 1049 | *error_msg = StringPrintf("Failed to find oat file at '%s' with expected image oat checksum of " |
| 1050 | "0x%x, found 0x%x", oat_location, expected_image_oat_checksum, |
| 1051 | actual_image_oat_checksum); |
| 1052 | return nullptr; |
| 1053 | } |
| 1054 | |
| 1055 | uintptr_t expected_image_oat_offset = reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()); |
| 1056 | uint32_t actual_image_oat_offset = oat_file->GetOatHeader().GetImageFileLocationOatDataBegin(); |
| 1057 | if (expected_image_oat_offset != actual_image_oat_offset) { |
| 1058 | *error_msg = StringPrintf("Failed to find oat file at '%s' with expected image oat offset %" |
| 1059 | PRIuPTR ", found %ud", oat_location, expected_image_oat_offset, |
| 1060 | actual_image_oat_offset); |
| 1061 | return nullptr; |
| 1062 | } |
| 1063 | int32_t expected_patch_delta = image_header.GetPatchDelta(); |
| 1064 | int32_t actual_patch_delta = oat_file->GetOatHeader().GetImagePatchDelta(); |
| 1065 | if (expected_patch_delta != actual_patch_delta) { |
| 1066 | *error_msg = StringPrintf("Failed to find oat file at '%s' with expected patch delta %d, " |
| 1067 | " found %d", oat_location, expected_patch_delta, actual_patch_delta); |
| 1068 | return nullptr; |
| 1069 | } |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 1070 | } |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1071 | |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 1072 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, |
| 1073 | &dex_location_checksum); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1074 | if (oat_dex_file == nullptr) { |
| 1075 | *error_msg = StringPrintf("Failed to find oat file at '%s' containing '%s'", oat_location, |
| 1076 | dex_location); |
| 1077 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1078 | } |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1079 | uint32_t expected_dex_checksum = dex_location_checksum; |
| 1080 | uint32_t actual_dex_checksum = oat_dex_file->GetDexFileLocationChecksum(); |
| 1081 | if (expected_dex_checksum != actual_dex_checksum) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1082 | *error_msg = StringPrintf("Failed to find oat file at '%s' with expected dex checksum of 0x%x, " |
| 1083 | "found 0x%x", oat_location, expected_dex_checksum, |
| 1084 | actual_dex_checksum); |
| 1085 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1086 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1087 | std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(error_msg)); |
| 1088 | if (dex_file.get() != nullptr) { |
| 1089 | return oat_file.release(); |
| 1090 | } else { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1091 | return nullptr; |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1092 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1093 | } |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1094 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1095 | const OatFile* ClassLinker::CreateOatFileForDexLocation(const char* dex_location, |
| 1096 | int fd, const char* oat_location, |
| 1097 | std::vector<std::string>* error_msgs) { |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1098 | // Generate the output oat file for the dex file |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1099 | VLOG(class_linker) << "Generating oat file " << oat_location << " for " << dex_location; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1100 | std::string error_msg; |
| 1101 | if (!GenerateOatFile(dex_location, fd, oat_location, &error_msg)) { |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 1102 | CHECK(!error_msg.empty()); |
| 1103 | error_msgs->push_back(error_msg); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1104 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1105 | } |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1106 | std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_location, oat_location, nullptr, nullptr, |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 1107 | !Runtime::Current()->IsAotCompiler(), |
| 1108 | &error_msg)); |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 1109 | if (oat_file.get() == nullptr) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1110 | std::string compound_msg = StringPrintf("\nFailed to open generated oat file '%s': %s", |
| 1111 | oat_location, error_msg.c_str()); |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 1112 | error_msgs->push_back(compound_msg); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1113 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1114 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1115 | |
| 1116 | return oat_file.release(); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1117 | } |
| 1118 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1119 | bool ClassLinker::VerifyOatImageChecksum(const OatFile* oat_file, |
| 1120 | const InstructionSet instruction_set) { |
| 1121 | Runtime* runtime = Runtime::Current(); |
| 1122 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1123 | if (image_space == nullptr) { |
| 1124 | return false; |
| 1125 | } |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1126 | uint32_t image_oat_checksum = 0; |
| 1127 | if (instruction_set == kRuntimeISA) { |
| 1128 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1129 | image_oat_checksum = image_header.GetOatChecksum(); |
| 1130 | } else { |
| 1131 | std::unique_ptr<ImageHeader> image_header(gc::space::ImageSpace::ReadImageHeaderOrDie( |
| 1132 | image_space->GetImageLocation().c_str(), instruction_set)); |
| 1133 | image_oat_checksum = image_header->GetOatChecksum(); |
| 1134 | } |
| 1135 | return oat_file->GetOatHeader().GetImageFileLocationOatChecksum() == image_oat_checksum; |
| 1136 | } |
| 1137 | |
| 1138 | bool ClassLinker::VerifyOatChecksums(const OatFile* oat_file, |
| 1139 | const InstructionSet instruction_set, |
| 1140 | std::string* error_msg) { |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 1141 | Runtime* runtime = Runtime::Current(); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1142 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1143 | if (image_space == nullptr) { |
| 1144 | *error_msg = "No image space for verification against"; |
| 1145 | return false; |
| 1146 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1147 | |
| 1148 | // If the requested instruction set is the same as the current runtime, |
| 1149 | // we can use the checksums directly. If it isn't, we'll have to read the |
| 1150 | // image header from the image for the right instruction set. |
| 1151 | uint32_t image_oat_checksum = 0; |
| 1152 | uintptr_t image_oat_data_begin = 0; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1153 | int32_t image_patch_delta = 0; |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 1154 | if (instruction_set == runtime->GetInstructionSet()) { |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1155 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1156 | image_oat_checksum = image_header.GetOatChecksum(); |
| 1157 | image_oat_data_begin = reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1158 | image_patch_delta = image_header.GetPatchDelta(); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1159 | } else { |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1160 | std::unique_ptr<ImageHeader> image_header(gc::space::ImageSpace::ReadImageHeaderOrDie( |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1161 | image_space->GetImageLocation().c_str(), instruction_set)); |
| 1162 | image_oat_checksum = image_header->GetOatChecksum(); |
| 1163 | image_oat_data_begin = reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1164 | image_patch_delta = image_header->GetPatchDelta(); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1165 | } |
| 1166 | const OatHeader& oat_header = oat_file->GetOatHeader(); |
Igor Murashkin | 96e8393 | 2014-10-29 19:45:42 -0700 | [diff] [blame] | 1167 | bool ret = (oat_header.GetImageFileLocationOatChecksum() == image_oat_checksum); |
| 1168 | |
| 1169 | // If the oat file is PIC, it doesn't care if/how image was relocated. Ignore these checks. |
| 1170 | if (!oat_file->IsPic()) { |
| 1171 | ret = ret && (oat_header.GetImagePatchDelta() == image_patch_delta) |
| 1172 | && (oat_header.GetImageFileLocationOatDataBegin() == image_oat_data_begin); |
| 1173 | } |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1174 | if (!ret) { |
| 1175 | *error_msg = StringPrintf("oat file '%s' mismatch (0x%x, %d, %d) with (0x%x, %" PRIdPTR ", %d)", |
| 1176 | oat_file->GetLocation().c_str(), |
| 1177 | oat_file->GetOatHeader().GetImageFileLocationOatChecksum(), |
| 1178 | oat_file->GetOatHeader().GetImageFileLocationOatDataBegin(), |
| 1179 | oat_file->GetOatHeader().GetImagePatchDelta(), |
| 1180 | image_oat_checksum, image_oat_data_begin, image_patch_delta); |
| 1181 | } |
| 1182 | return ret; |
| 1183 | } |
| 1184 | |
| 1185 | bool ClassLinker::VerifyOatAndDexFileChecksums(const OatFile* oat_file, |
| 1186 | const char* dex_location, |
| 1187 | uint32_t dex_location_checksum, |
| 1188 | const InstructionSet instruction_set, |
| 1189 | std::string* error_msg) { |
| 1190 | if (!VerifyOatChecksums(oat_file, instruction_set, error_msg)) { |
| 1191 | return false; |
| 1192 | } |
Brian Carlstrom | 46b8a62 | 2012-06-19 23:13:22 -0700 | [diff] [blame] | 1193 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1194 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, |
| 1195 | &dex_location_checksum); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1196 | if (oat_dex_file == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1197 | *error_msg = StringPrintf("oat file '%s' does not contain contents for '%s' with checksum 0x%x", |
| 1198 | oat_file->GetLocation().c_str(), dex_location, dex_location_checksum); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1199 | for (const OatFile::OatDexFile* oat_dex_file_in : oat_file->GetOatDexFiles()) { |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1200 | *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] | 1201 | oat_file->GetLocation().c_str(), |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1202 | oat_dex_file_in->GetDexFileLocation().c_str(), |
| 1203 | oat_dex_file_in->GetDexFileLocationChecksum()); |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 1204 | } |
| 1205 | return false; |
| 1206 | } |
Brian Carlstrom | 46b8a62 | 2012-06-19 23:13:22 -0700 | [diff] [blame] | 1207 | |
Calin Juravle | a8c55ae | 2014-09-05 16:14:19 +0100 | [diff] [blame] | 1208 | DCHECK_EQ(dex_location_checksum, oat_dex_file->GetDexFileLocationChecksum()); |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1209 | return true; |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1212 | bool ClassLinker::VerifyOatWithDexFile(const OatFile* oat_file, |
| 1213 | const char* dex_location, |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 1214 | const uint32_t* dex_location_checksum, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1215 | std::string* error_msg) { |
| 1216 | CHECK(oat_file != nullptr); |
| 1217 | CHECK(dex_location != nullptr); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1218 | std::unique_ptr<const DexFile> dex_file; |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 1219 | if (dex_location_checksum == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1220 | // If no classes.dex found in dex_location, it has been stripped or is corrupt, assume oat is |
| 1221 | // up-to-date. This is the common case in user builds for jar's and apk's in the /system |
| 1222 | // directory. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1223 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, nullptr); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1224 | if (oat_dex_file == nullptr) { |
| 1225 | *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] | 1226 | "dex file '%s': %s", oat_file->GetLocation().c_str(), dex_location, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1227 | error_msg->c_str()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1228 | return false; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1229 | } |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 1230 | dex_file = oat_dex_file->OpenDexFile(error_msg); |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 1231 | } else { |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 1232 | bool verified = VerifyOatAndDexFileChecksums(oat_file, dex_location, *dex_location_checksum, |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1233 | kRuntimeISA, error_msg); |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 1234 | if (!verified) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1235 | return false; |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 1236 | } |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 1237 | dex_file = oat_file->GetOatDexFile(dex_location, |
| 1238 | dex_location_checksum)->OpenDexFile(error_msg); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1239 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1240 | return dex_file.get() != nullptr; |
Brian Carlstrom | 46b8a62 | 2012-06-19 23:13:22 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1243 | const OatFile* ClassLinker::FindOatFileContainingDexFileFromDexLocation( |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1244 | const char* dex_location, |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1245 | const uint32_t* dex_location_checksum, |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1246 | InstructionSet isa, |
Calin Juravle | c54aea7 | 2014-07-16 14:45:03 +0100 | [diff] [blame] | 1247 | std::vector<std::string>* error_msgs, |
| 1248 | bool* obsolete_file_cleanup_failed) { |
| 1249 | *obsolete_file_cleanup_failed = false; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1250 | bool already_opened = false; |
| 1251 | std::string dex_location_str(dex_location); |
| 1252 | std::unique_ptr<const OatFile> oat_file(OpenOatFileFromDexLocation(dex_location_str, isa, |
| 1253 | &already_opened, |
| 1254 | obsolete_file_cleanup_failed, |
| 1255 | error_msgs)); |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 1256 | std::string error_msg; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1257 | if (oat_file.get() == nullptr) { |
| 1258 | error_msgs->push_back(StringPrintf("Failed to open oat file from dex location '%s'", |
| 1259 | dex_location)); |
| 1260 | return nullptr; |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1261 | } else if (oat_file->IsExecutable() && |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 1262 | !VerifyOatWithDexFile(oat_file.get(), dex_location, |
| 1263 | dex_location_checksum, &error_msg)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1264 | error_msgs->push_back(StringPrintf("Failed to verify oat file '%s' found for dex location " |
| 1265 | "'%s': %s", oat_file->GetLocation().c_str(), dex_location, |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 1266 | error_msg.c_str())); |
Brian Carlstrom | 08cbf66 | 2013-12-10 16:52:57 -0800 | [diff] [blame] | 1267 | return nullptr; |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1268 | } else if (!oat_file->IsExecutable() && |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 1269 | Runtime::Current()->GetHeap()->HasImageSpace() && |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1270 | !VerifyOatImageChecksum(oat_file.get(), isa)) { |
| 1271 | error_msgs->push_back(StringPrintf("Failed to verify non-executable oat file '%s' found for " |
| 1272 | "dex location '%s'. Image checksum incorrect.", |
| 1273 | oat_file->GetLocation().c_str(), dex_location)); |
| 1274 | return nullptr; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1275 | } else { |
| 1276 | return oat_file.release(); |
Brian Carlstrom | 08cbf66 | 2013-12-10 16:52:57 -0800 | [diff] [blame] | 1277 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1278 | } |
| 1279 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1280 | const OatFile* ClassLinker::FindOpenedOatFileFromOatLocation(const std::string& oat_location) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1281 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1282 | for (size_t i = 0; i < oat_files_.size(); i++) { |
| 1283 | const OatFile* oat_file = oat_files_[i]; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1284 | DCHECK(oat_file != nullptr); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1285 | if (oat_file->GetLocation() == oat_location) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1286 | return oat_file; |
| 1287 | } |
| 1288 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1289 | return nullptr; |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 1290 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1291 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1292 | const OatFile* ClassLinker::OpenOatFileFromDexLocation(const std::string& dex_location, |
| 1293 | InstructionSet isa, |
| 1294 | bool *already_opened, |
| 1295 | bool *obsolete_file_cleanup_failed, |
| 1296 | std::vector<std::string>* error_msgs) { |
| 1297 | // Find out if we've already opened the file |
| 1298 | const OatFile* ret = nullptr; |
| 1299 | std::string odex_filename(DexFilenameToOdexFilename(dex_location, isa)); |
| 1300 | ret = FindOpenedOatFileFromOatLocation(odex_filename); |
| 1301 | if (ret != nullptr) { |
| 1302 | *already_opened = true; |
| 1303 | return ret; |
| 1304 | } |
| 1305 | |
| 1306 | std::string dalvik_cache; |
| 1307 | bool have_android_data = false; |
| 1308 | bool have_dalvik_cache = false; |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 1309 | bool is_global_cache = false; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1310 | GetDalvikCache(GetInstructionSetString(kRuntimeISA), false, &dalvik_cache, |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 1311 | &have_android_data, &have_dalvik_cache, &is_global_cache); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1312 | std::string cache_filename; |
| 1313 | if (have_dalvik_cache) { |
| 1314 | cache_filename = GetDalvikCacheFilenameOrDie(dex_location.c_str(), dalvik_cache.c_str()); |
| 1315 | ret = FindOpenedOatFileFromOatLocation(cache_filename); |
| 1316 | if (ret != nullptr) { |
| 1317 | *already_opened = true; |
| 1318 | return ret; |
| 1319 | } |
| 1320 | } else { |
| 1321 | // If we need to relocate we should just place odex back where it started. |
| 1322 | cache_filename = odex_filename; |
| 1323 | } |
| 1324 | |
| 1325 | ret = nullptr; |
| 1326 | |
| 1327 | // We know that neither the odex nor the cache'd version is already in use, if it even exists. |
| 1328 | // |
| 1329 | // Now we do the following: |
| 1330 | // 1) Try and open the odex version |
| 1331 | // 2) If present, checksum-verified & relocated correctly return it |
| 1332 | // 3) Close the odex version to free up its address space. |
| 1333 | // 4) Try and open the cache version |
| 1334 | // 5) If present, checksum-verified & relocated correctly return it |
| 1335 | // 6) Close the cache version to free up its address space. |
| 1336 | // 7) If we should relocate: |
| 1337 | // a) If we have opened and checksum-verified the odex version relocate it to |
| 1338 | // 'cache_filename' and return it |
| 1339 | // b) If we have opened and checksum-verified the cache version relocate it in place and return |
| 1340 | // it. This should not happen often (I think only the run-test's will hit this case). |
| 1341 | // 8) If the cache-version was present we should delete it since it must be obsolete if we get to |
| 1342 | // this point. |
| 1343 | // 9) Return nullptr |
| 1344 | |
| 1345 | *already_opened = false; |
| 1346 | const Runtime* runtime = Runtime::Current(); |
| 1347 | CHECK(runtime != nullptr); |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 1348 | bool executable = !runtime->IsAotCompiler(); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1349 | |
| 1350 | std::string odex_error_msg; |
| 1351 | bool should_patch_system = false; |
| 1352 | bool odex_checksum_verified = false; |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 1353 | bool have_system_odex = false; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1354 | { |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1355 | // 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] | 1356 | // 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] | 1357 | 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] | 1358 | nullptr, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1359 | executable, &odex_error_msg)); |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1360 | 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] | 1361 | &odex_checksum_verified, |
| 1362 | &odex_error_msg)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1363 | return odex_oat_file.release(); |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 1364 | } else { |
| 1365 | if (odex_checksum_verified) { |
| 1366 | // We can just relocate |
| 1367 | should_patch_system = true; |
| 1368 | odex_error_msg = "Image Patches are incorrect"; |
| 1369 | } |
| 1370 | if (odex_oat_file.get() != nullptr) { |
| 1371 | have_system_odex = true; |
| 1372 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1373 | } |
| 1374 | } |
| 1375 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1376 | std::string cache_error_msg; |
| 1377 | bool should_patch_cache = false; |
| 1378 | bool cache_checksum_verified = false; |
| 1379 | if (have_dalvik_cache) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1380 | 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] | 1381 | nullptr, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1382 | executable, &cache_error_msg)); |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1383 | 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] | 1384 | &cache_checksum_verified, |
| 1385 | &cache_error_msg)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1386 | return cache_oat_file.release(); |
| 1387 | } else if (cache_checksum_verified) { |
| 1388 | // We can just relocate |
| 1389 | should_patch_cache = true; |
| 1390 | cache_error_msg = "Image Patches are incorrect"; |
| 1391 | } |
| 1392 | } else if (have_android_data) { |
| 1393 | // dalvik_cache does not exist but android data does. This means we should be able to create |
| 1394 | // it, so we should try. |
| 1395 | GetDalvikCacheOrDie(GetInstructionSetString(kRuntimeISA), true); |
| 1396 | } |
| 1397 | |
| 1398 | ret = nullptr; |
| 1399 | std::string error_msg; |
| 1400 | if (runtime->CanRelocate()) { |
| 1401 | // Run relocation |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1402 | gc::space::ImageSpace* space = Runtime::Current()->GetHeap()->GetImageSpace(); |
| 1403 | if (space != nullptr) { |
| 1404 | const std::string& image_location = space->GetImageLocation(); |
| 1405 | if (odex_checksum_verified && should_patch_system) { |
| 1406 | ret = PatchAndRetrieveOat(odex_filename, cache_filename, image_location, isa, &error_msg); |
| 1407 | } else if (cache_checksum_verified && should_patch_cache) { |
| 1408 | CHECK(have_dalvik_cache); |
| 1409 | ret = PatchAndRetrieveOat(cache_filename, cache_filename, image_location, isa, &error_msg); |
| 1410 | } |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 1411 | } else if (have_system_odex) { |
| 1412 | ret = GetInterpretedOnlyOat(odex_filename, isa, &error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1413 | } |
| 1414 | } |
| 1415 | if (ret == nullptr && have_dalvik_cache && OS::FileExists(cache_filename.c_str())) { |
| 1416 | // implicitly: were able to fine where the cached version is but we were unable to use it, |
| 1417 | // either as a destination for relocation or to open a file. We should delete it if it is |
| 1418 | // there. |
| 1419 | if (TEMP_FAILURE_RETRY(unlink(cache_filename.c_str())) != 0) { |
| 1420 | std::string rm_error_msg = StringPrintf("Failed to remove obsolete file from %s when " |
| 1421 | "searching for dex file %s: %s", |
| 1422 | cache_filename.c_str(), dex_location.c_str(), |
| 1423 | strerror(errno)); |
| 1424 | error_msgs->push_back(rm_error_msg); |
| 1425 | VLOG(class_linker) << rm_error_msg; |
| 1426 | // Let the caller know that we couldn't remove the obsolete file. |
| 1427 | // This is a good indication that further writes may fail as well. |
| 1428 | *obsolete_file_cleanup_failed = true; |
| 1429 | } |
| 1430 | } |
| 1431 | if (ret == nullptr) { |
| 1432 | VLOG(class_linker) << error_msg; |
| 1433 | error_msgs->push_back(error_msg); |
| 1434 | std::string relocation_msg; |
| 1435 | if (runtime->CanRelocate()) { |
| 1436 | relocation_msg = StringPrintf(" and relocation failed"); |
| 1437 | } |
| 1438 | if (have_dalvik_cache && cache_checksum_verified) { |
| 1439 | error_msg = StringPrintf("Failed to open oat file from %s (error %s) or %s " |
| 1440 | "(error %s)%s.", odex_filename.c_str(), odex_error_msg.c_str(), |
| 1441 | cache_filename.c_str(), cache_error_msg.c_str(), |
| 1442 | relocation_msg.c_str()); |
| 1443 | } else { |
| 1444 | error_msg = StringPrintf("Failed to open oat file from %s (error %s) (no " |
| 1445 | "dalvik_cache availible)%s.", odex_filename.c_str(), |
| 1446 | odex_error_msg.c_str(), relocation_msg.c_str()); |
| 1447 | } |
| 1448 | VLOG(class_linker) << error_msg; |
| 1449 | error_msgs->push_back(error_msg); |
| 1450 | } |
| 1451 | return ret; |
| 1452 | } |
| 1453 | |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1454 | const OatFile* ClassLinker::GetInterpretedOnlyOat(const std::string& oat_path, |
| 1455 | InstructionSet isa, |
| 1456 | std::string* error_msg) { |
| 1457 | // We open it non-executable |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1458 | 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] | 1459 | if (output.get() == nullptr) { |
| 1460 | return nullptr; |
| 1461 | } |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 1462 | if (!Runtime::Current()->GetHeap()->HasImageSpace() || |
| 1463 | VerifyOatImageChecksum(output.get(), isa)) { |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1464 | return output.release(); |
| 1465 | } else { |
| 1466 | *error_msg = StringPrintf("Could not use oat file '%s', image checksum failed to verify.", |
| 1467 | oat_path.c_str()); |
| 1468 | return nullptr; |
| 1469 | } |
| 1470 | } |
| 1471 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1472 | const OatFile* ClassLinker::PatchAndRetrieveOat(const std::string& input_oat, |
| 1473 | const std::string& output_oat, |
| 1474 | const std::string& image_location, |
| 1475 | InstructionSet isa, |
| 1476 | std::string* error_msg) { |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1477 | Runtime* runtime = Runtime::Current(); |
| 1478 | DCHECK(runtime != nullptr); |
| 1479 | if (!runtime->GetHeap()->HasImageSpace()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1480 | // We don't have an image space so there is no point in trying to patchoat. |
| 1481 | LOG(WARNING) << "Patching of oat file '" << input_oat << "' not attempted because we are " |
| 1482 | << "running without an image. Attempting to use oat file for interpretation."; |
| 1483 | return GetInterpretedOnlyOat(input_oat, isa, error_msg); |
| 1484 | } |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1485 | if (!runtime->IsDex2OatEnabled()) { |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1486 | // We don't have dex2oat so we can assume we don't have patchoat either. We should just use the |
| 1487 | // input_oat but make sure we only do interpretation on it's dex files. |
| 1488 | LOG(WARNING) << "Patching of oat file '" << input_oat << "' not attempted due to dex2oat being " |
| 1489 | << "disabled. Attempting to use oat file for interpretation"; |
| 1490 | return GetInterpretedOnlyOat(input_oat, isa, error_msg); |
| 1491 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1492 | Locks::mutator_lock_->AssertNotHeld(Thread::Current()); // Avoid starving GC. |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1493 | std::string patchoat(runtime->GetPatchoatExecutable()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1494 | |
| 1495 | std::string isa_arg("--instruction-set="); |
| 1496 | isa_arg += GetInstructionSetString(isa); |
| 1497 | std::string input_oat_filename_arg("--input-oat-file="); |
| 1498 | input_oat_filename_arg += input_oat; |
| 1499 | std::string output_oat_filename_arg("--output-oat-file="); |
| 1500 | output_oat_filename_arg += output_oat; |
| 1501 | std::string patched_image_arg("--patched-image-location="); |
| 1502 | patched_image_arg += image_location; |
| 1503 | |
| 1504 | std::vector<std::string> argv; |
| 1505 | argv.push_back(patchoat); |
| 1506 | argv.push_back(isa_arg); |
| 1507 | argv.push_back(input_oat_filename_arg); |
| 1508 | argv.push_back(output_oat_filename_arg); |
| 1509 | argv.push_back(patched_image_arg); |
| 1510 | |
| 1511 | std::string command_line(Join(argv, ' ')); |
| 1512 | LOG(INFO) << "Relocate Oat File: " << command_line; |
| 1513 | bool success = Exec(argv, error_msg); |
| 1514 | if (success) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1515 | std::unique_ptr<OatFile> output(OatFile::Open(output_oat, output_oat, nullptr, nullptr, |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 1516 | !runtime->IsAotCompiler(), error_msg)); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1517 | bool checksum_verified = false; |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1518 | if (output.get() != nullptr && CheckOatFile(runtime, output.get(), isa, &checksum_verified, |
| 1519 | error_msg)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1520 | return output.release(); |
| 1521 | } else if (output.get() != nullptr) { |
| 1522 | *error_msg = StringPrintf("Patching of oat file '%s' succeeded " |
| 1523 | "but output file '%s' failed verifcation: %s", |
| 1524 | input_oat.c_str(), output_oat.c_str(), error_msg->c_str()); |
| 1525 | } else { |
| 1526 | *error_msg = StringPrintf("Patching of oat file '%s' succeeded " |
| 1527 | "but was unable to open output file '%s': %s", |
| 1528 | input_oat.c_str(), output_oat.c_str(), error_msg->c_str()); |
| 1529 | } |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 1530 | } else if (!runtime->IsAotCompiler()) { |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1531 | // patchoat failed which means we probably don't have enough room to place the output oat file, |
| 1532 | // instead of failing we should just run the interpreter from the dex files in the input oat. |
| 1533 | LOG(WARNING) << "Patching of oat file '" << input_oat << "' failed. Attempting to use oat file " |
| 1534 | << "for interpretation. patchoat failure was: " << *error_msg; |
| 1535 | return GetInterpretedOnlyOat(input_oat, isa, error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1536 | } else { |
| 1537 | *error_msg = StringPrintf("Patching of oat file '%s to '%s' " |
| 1538 | "failed: %s", input_oat.c_str(), output_oat.c_str(), |
| 1539 | error_msg->c_str()); |
| 1540 | } |
| 1541 | return nullptr; |
| 1542 | } |
| 1543 | |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1544 | bool ClassLinker::CheckOatFile(const Runtime* runtime, const OatFile* oat_file, InstructionSet isa, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1545 | bool* checksum_verified, |
| 1546 | std::string* error_msg) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1547 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1548 | if (image_space == nullptr) { |
| 1549 | *error_msg = "No image space present"; |
| 1550 | return false; |
| 1551 | } |
Vladimir Marko | 30c2e1b | 2014-09-08 09:55:29 +0100 | [diff] [blame] | 1552 | uint32_t real_image_checksum; |
| 1553 | void* real_image_oat_offset; |
| 1554 | int32_t real_patch_delta; |
| 1555 | if (isa == runtime->GetInstructionSet()) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1556 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1557 | real_image_checksum = image_header.GetOatChecksum(); |
| 1558 | real_image_oat_offset = image_header.GetOatDataBegin(); |
| 1559 | real_patch_delta = image_header.GetPatchDelta(); |
| 1560 | } else { |
| 1561 | std::unique_ptr<ImageHeader> image_header(gc::space::ImageSpace::ReadImageHeaderOrDie( |
| 1562 | image_space->GetImageLocation().c_str(), isa)); |
| 1563 | real_image_checksum = image_header->GetOatChecksum(); |
| 1564 | real_image_oat_offset = image_header->GetOatDataBegin(); |
| 1565 | real_patch_delta = image_header->GetPatchDelta(); |
| 1566 | } |
| 1567 | |
| 1568 | const OatHeader& oat_header = oat_file->GetOatHeader(); |
Vladimir Marko | 30c2e1b | 2014-09-08 09:55:29 +0100 | [diff] [blame] | 1569 | std::string compound_msg; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1570 | |
| 1571 | uint32_t oat_image_checksum = oat_header.GetImageFileLocationOatChecksum(); |
| 1572 | *checksum_verified = oat_image_checksum == real_image_checksum; |
| 1573 | if (!*checksum_verified) { |
Vladimir Marko | 30c2e1b | 2014-09-08 09:55:29 +0100 | [diff] [blame] | 1574 | StringAppendF(&compound_msg, " Oat Image Checksum Incorrect (expected 0x%x, received 0x%x)", |
| 1575 | real_image_checksum, oat_image_checksum); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1576 | } |
| 1577 | |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1578 | bool offset_verified; |
| 1579 | bool patch_delta_verified; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1580 | |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1581 | if (!oat_file->IsPic()) { |
| 1582 | // If an oat file is not PIC, we need to check that the image is at the expected location and |
| 1583 | // patched in the same way. |
| 1584 | void* oat_image_oat_offset = |
| 1585 | reinterpret_cast<void*>(oat_header.GetImageFileLocationOatDataBegin()); |
| 1586 | offset_verified = oat_image_oat_offset == real_image_oat_offset; |
| 1587 | if (!offset_verified) { |
| 1588 | StringAppendF(&compound_msg, " Oat Image oat offset incorrect (expected 0x%p, received 0x%p)", |
| 1589 | real_image_oat_offset, oat_image_oat_offset); |
| 1590 | } |
| 1591 | |
| 1592 | int32_t oat_patch_delta = oat_header.GetImagePatchDelta(); |
| 1593 | patch_delta_verified = oat_patch_delta == real_patch_delta; |
| 1594 | if (!patch_delta_verified) { |
| 1595 | StringAppendF(&compound_msg, " Oat image patch delta incorrect (expected 0x%x, " |
| 1596 | "received 0x%x)", real_patch_delta, oat_patch_delta); |
| 1597 | } |
| 1598 | } else { |
| 1599 | // If an oat file is PIC, we ignore offset and patching delta. |
| 1600 | offset_verified = true; |
| 1601 | patch_delta_verified = true; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | bool ret = (*checksum_verified && offset_verified && patch_delta_verified); |
Vladimir Marko | 30c2e1b | 2014-09-08 09:55:29 +0100 | [diff] [blame] | 1605 | if (!ret) { |
| 1606 | *error_msg = "Oat file failed to verify:" + compound_msg; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1607 | } |
| 1608 | return ret; |
| 1609 | } |
| 1610 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1611 | const OatFile* ClassLinker::FindOatFileFromOatLocation(const std::string& oat_location, |
| 1612 | std::string* error_msg) { |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 1613 | const OatFile* oat_file = FindOpenedOatFileFromOatLocation(oat_location); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1614 | if (oat_file != nullptr) { |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 1615 | return oat_file; |
| 1616 | } |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 1617 | return OatFile::Open(oat_location, oat_location, nullptr, nullptr, |
| 1618 | !Runtime::Current()->IsAotCompiler(), 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); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1625 | if (obj->IsArtMethod()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1626 | mirror::ArtMethod* method = obj->AsArtMethod(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1627 | if (!method->IsNative()) { |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 1628 | const size_t pointer_size = class_linker->image_pointer_size_; |
Mathieu Chartier | a89d7ed | 2014-12-05 10:57:13 -0800 | [diff] [blame] | 1629 | method->SetEntryPointFromInterpreterPtrSize(artInterpreterToInterpreterBridge, pointer_size); |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 1630 | if (!method->IsRuntimeMethod() && method != Runtime::Current()->GetResolutionMethod()) { |
Mathieu Chartier | a89d7ed | 2014-12-05 10:57:13 -0800 | [diff] [blame] | 1631 | method->SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(), |
| 1632 | pointer_size); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1633 | } |
| 1634 | } |
| 1635 | } |
| 1636 | } |
| 1637 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1638 | void ClassLinker::InitFromImage() { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1639 | VLOG(startup) << "ClassLinker::InitFromImage entering"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1640 | CHECK(!init_done_); |
| 1641 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1642 | Thread* self = Thread::Current(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1643 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 1644 | gc::space::ImageSpace* space = heap->GetImageSpace(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1645 | dex_cache_image_class_lookup_required_ = true; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1646 | CHECK(space != nullptr); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1647 | OatFile& oat_file = GetImageOatFile(space); |
| 1648 | CHECK_EQ(oat_file.GetOatHeader().GetImageFileLocationOatChecksum(), 0U); |
| 1649 | CHECK_EQ(oat_file.GetOatHeader().GetImageFileLocationOatDataBegin(), 0U); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1650 | const char* image_file_location = oat_file.GetOatHeader(). |
| 1651 | GetStoreValueByKey(OatHeader::kImageLocationKey); |
| 1652 | CHECK(image_file_location == nullptr || *image_file_location == 0); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1653 | quick_resolution_trampoline_ = oat_file.GetOatHeader().GetQuickResolutionTrampoline(); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1654 | quick_imt_conflict_trampoline_ = oat_file.GetOatHeader().GetQuickImtConflictTrampoline(); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1655 | quick_generic_jni_trampoline_ = oat_file.GetOatHeader().GetQuickGenericJniTrampoline(); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 1656 | quick_to_interpreter_bridge_trampoline_ = oat_file.GetOatHeader().GetQuickToInterpreterBridge(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1657 | mirror::Object* dex_caches_object = space->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches); |
| 1658 | mirror::ObjectArray<mirror::DexCache>* dex_caches = |
| 1659 | dex_caches_object->AsObjectArray<mirror::DexCache>(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1660 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1661 | StackHandleScope<1> hs(self); |
| 1662 | Handle<mirror::ObjectArray<mirror::Class>> class_roots(hs.NewHandle( |
| 1663 | space->GetImageHeader().GetImageRoot(ImageHeader::kClassRoots)-> |
| 1664 | AsObjectArray<mirror::Class>())); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1665 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>(class_roots.Get()); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1666 | |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1667 | // Special case of setting up the String class early so that we can test arbitrary objects |
| 1668 | // as being Strings or not |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 1669 | mirror::String::SetClass(GetClassRoot(kJavaLangString)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1670 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1671 | CHECK_EQ(oat_file.GetOatHeader().GetDexFileCount(), |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1672 | static_cast<uint32_t>(dex_caches->GetLength())); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1673 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1674 | StackHandleScope<1> hs2(self); |
| 1675 | Handle<mirror::DexCache> dex_cache(hs2.NewHandle(dex_caches->Get(i))); |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1676 | const std::string& dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1677 | const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(dex_file_location.c_str(), |
| 1678 | nullptr); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1679 | CHECK(oat_dex_file != nullptr) << oat_file.GetLocation() << " " << dex_file_location; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1680 | std::string error_msg; |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 1681 | std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg); |
| 1682 | if (dex_file.get() == nullptr) { |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1683 | LOG(FATAL) << "Failed to open dex file " << dex_file_location |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1684 | << " from within oat file " << oat_file.GetLocation() |
| 1685 | << " error '" << error_msg << "'"; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1686 | UNREACHABLE(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1687 | } |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1688 | |
| 1689 | CHECK_EQ(dex_file->GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum()); |
| 1690 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 1691 | AppendToBootClassPath(*dex_file.get(), dex_cache); |
| 1692 | opened_dex_files_.push_back(std::move(dex_file)); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1693 | } |
| 1694 | |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 1695 | // Set classes on AbstractMethod early so that IsMethod tests can be performed during the live |
| 1696 | // bitmap walk. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1697 | mirror::ArtMethod::SetClass(GetClassRoot(kJavaLangReflectArtMethod)); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 1698 | size_t art_method_object_size = mirror::ArtMethod::GetJavaLangReflectArtMethod()->GetObjectSize(); |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 1699 | if (!Runtime::Current()->IsAotCompiler()) { |
| 1700 | // Aot compiler supports having an image with a different pointer size than the runtime. This |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 1701 | // happens on the host for compile 32 bit tests since we use a 64 bit libart compiler. We may |
| 1702 | // also use 32 bit dex2oat on a system with 64 bit apps. |
| 1703 | CHECK_EQ(art_method_object_size, mirror::ArtMethod::InstanceSize(sizeof(void*))) |
| 1704 | << sizeof(void*); |
| 1705 | } |
| 1706 | if (art_method_object_size == mirror::ArtMethod::InstanceSize(4)) { |
| 1707 | image_pointer_size_ = 4; |
| 1708 | } else { |
| 1709 | CHECK_EQ(art_method_object_size, mirror::ArtMethod::InstanceSize(8)); |
| 1710 | image_pointer_size_ = 8; |
| 1711 | } |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 1712 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1713 | // Set entry point to interpreter if in InterpretOnly mode. |
Mathieu Chartier | a89d7ed | 2014-12-05 10:57:13 -0800 | [diff] [blame] | 1714 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 1715 | if (!runtime->IsAotCompiler() && runtime->GetInstrumentation()->InterpretOnly()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1716 | heap->VisitObjects(InitFromImageInterpretOnlyCallback, this); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1717 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1718 | |
| 1719 | // reinit class_roots_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1720 | mirror::Class::SetClassClass(class_roots->Get(kJavaLangClass)); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1721 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>(class_roots.Get()); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1722 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1723 | // reinit array_iftable_ from any array class instance, they should be == |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1724 | array_iftable_ = GcRoot<mirror::IfTable>(GetClassRoot(kObjectArrayClass)->GetIfTable()); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 1725 | DCHECK_EQ(array_iftable_.Read(), GetClassRoot(kBooleanArrayClass)->GetIfTable()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1726 | // String class root was set above |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 1727 | mirror::Reference::SetClass(GetClassRoot(kJavaLangRefReference)); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1728 | mirror::ArtField::SetClass(GetClassRoot(kJavaLangReflectArtField)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1729 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
| 1730 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
| 1731 | mirror::CharArray::SetArrayClass(GetClassRoot(kCharArrayClass)); |
| 1732 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
| 1733 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
| 1734 | mirror::IntArray::SetArrayClass(GetClassRoot(kIntArrayClass)); |
| 1735 | mirror::LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
| 1736 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
| 1737 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
| 1738 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1739 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1740 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1741 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1742 | VLOG(startup) << "ClassLinker::InitFromImage exiting"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1743 | } |
| 1744 | |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1745 | void ClassLinker::VisitClassRoots(RootCallback* callback, void* arg, VisitRootFlags flags) { |
| 1746 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 1747 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1748 | for (GcRoot<mirror::Class>& root : class_table_) { |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 1749 | root.VisitRoot(callback, arg, RootInfo(kRootStickyClass)); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1750 | } |
| 1751 | for (GcRoot<mirror::Class>& root : pre_zygote_class_table_) { |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 1752 | root.VisitRoot(callback, arg, RootInfo(kRootStickyClass)); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1753 | } |
| 1754 | } else if ((flags & kVisitRootFlagNewRoots) != 0) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1755 | for (auto& root : new_class_roots_) { |
| 1756 | mirror::Class* old_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 1757 | root.VisitRoot(callback, arg, RootInfo(kRootStickyClass)); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1758 | mirror::Class* new_ref = root.Read<kWithoutReadBarrier>(); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1759 | if (UNLIKELY(new_ref != old_ref)) { |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1760 | // Uh ohes, GC moved a root in the log. Need to search the class_table and update the |
| 1761 | // corresponding object. This is slow, but luckily for us, this may only happen with a |
| 1762 | // concurrent moving GC. |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1763 | auto it = class_table_.Find(GcRoot<mirror::Class>(old_ref)); |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 1764 | DCHECK(it != class_table_.end()); |
| 1765 | *it = GcRoot<mirror::Class>(new_ref); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1766 | } |
| 1767 | } |
| 1768 | } |
| 1769 | if ((flags & kVisitRootFlagClearRootLog) != 0) { |
| 1770 | new_class_roots_.clear(); |
| 1771 | } |
| 1772 | if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
| 1773 | log_new_class_table_roots_ = true; |
| 1774 | } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
| 1775 | log_new_class_table_roots_ = false; |
| 1776 | } |
| 1777 | // We deliberately ignore the class roots in the image since we |
| 1778 | // handle image roots by using the MS/CMS rescanning of dirty cards. |
| 1779 | } |
| 1780 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1781 | // Keep in sync with InitCallback. Anything we visit, we need to |
| 1782 | // reinit references to when reinitializing a ClassLinker from a |
| 1783 | // mapped image. |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 1784 | void ClassLinker::VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags) { |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 1785 | class_roots_.VisitRoot(callback, arg, RootInfo(kRootVMInternal)); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1786 | Thread* self = Thread::Current(); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1787 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 1788 | ReaderMutexLock mu(self, dex_lock_); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 1789 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1790 | for (GcRoot<mirror::DexCache>& dex_cache : dex_caches_) { |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 1791 | dex_cache.VisitRoot(callback, arg, RootInfo(kRootVMInternal)); |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 1792 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 1793 | } else if ((flags & kVisitRootFlagNewRoots) != 0) { |
| 1794 | for (size_t index : new_dex_cache_roots_) { |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 1795 | dex_caches_[index].VisitRoot(callback, arg, RootInfo(kRootVMInternal)); |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 1796 | } |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1797 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 1798 | if ((flags & kVisitRootFlagClearRootLog) != 0) { |
| 1799 | new_dex_cache_roots_.clear(); |
| 1800 | } |
| 1801 | if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
| 1802 | log_new_dex_caches_roots_ = true; |
| 1803 | } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
| 1804 | log_new_dex_caches_roots_ = false; |
| 1805 | } |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1806 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1807 | VisitClassRoots(callback, arg, flags); |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 1808 | array_iftable_.VisitRoot(callback, arg, RootInfo(kRootVMInternal)); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1809 | DCHECK(!array_iftable_.IsNull()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1810 | for (size_t i = 0; i < kFindArrayCacheSize; ++i) { |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 1811 | find_array_class_cache_[i].VisitRootIfNonNull(callback, arg, RootInfo(kRootVMInternal)); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1812 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1813 | } |
| 1814 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1815 | void ClassLinker::VisitClasses(ClassVisitor* visitor, void* arg) { |
| 1816 | if (dex_cache_image_class_lookup_required_) { |
| 1817 | MoveImageClassesToClassTable(); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1818 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1819 | // TODO: why isn't this a ReaderMutexLock? |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 1820 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1821 | for (GcRoot<mirror::Class>& root : class_table_) { |
| 1822 | if (!visitor(root.Read(), arg)) { |
| 1823 | return; |
| 1824 | } |
| 1825 | } |
| 1826 | for (GcRoot<mirror::Class>& root : pre_zygote_class_table_) { |
| 1827 | if (!visitor(root.Read(), arg)) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1828 | return; |
| 1829 | } |
| 1830 | } |
| 1831 | } |
| 1832 | |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1833 | static bool GetClassesVisitorSet(mirror::Class* c, void* arg) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1834 | std::set<mirror::Class*>* classes = reinterpret_cast<std::set<mirror::Class*>*>(arg); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1835 | classes->insert(c); |
| 1836 | return true; |
| 1837 | } |
| 1838 | |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1839 | struct GetClassesVisitorArrayArg { |
| 1840 | Handle<mirror::ObjectArray<mirror::Class>>* classes; |
| 1841 | int32_t index; |
| 1842 | bool success; |
| 1843 | }; |
| 1844 | |
| 1845 | static bool GetClassesVisitorArray(mirror::Class* c, void* varg) |
| 1846 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1847 | GetClassesVisitorArrayArg* arg = reinterpret_cast<GetClassesVisitorArrayArg*>(varg); |
| 1848 | if (arg->index < (*arg->classes)->GetLength()) { |
| 1849 | (*arg->classes)->Set(arg->index, c); |
| 1850 | arg->index++; |
| 1851 | return true; |
| 1852 | } else { |
| 1853 | arg->success = false; |
| 1854 | return false; |
| 1855 | } |
| 1856 | } |
| 1857 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1858 | void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor, void* arg) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1859 | // TODO: it may be possible to avoid secondary storage if we iterate over dex caches. The problem |
| 1860 | // is avoiding duplicates. |
| 1861 | if (!kMovingClasses) { |
| 1862 | std::set<mirror::Class*> classes; |
| 1863 | VisitClasses(GetClassesVisitorSet, &classes); |
| 1864 | for (mirror::Class* klass : classes) { |
| 1865 | if (!visitor(klass, arg)) { |
| 1866 | return; |
| 1867 | } |
| 1868 | } |
| 1869 | } else { |
| 1870 | Thread* self = Thread::Current(); |
| 1871 | StackHandleScope<1> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 1872 | MutableHandle<mirror::ObjectArray<mirror::Class>> classes = |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1873 | hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
| 1874 | GetClassesVisitorArrayArg local_arg; |
| 1875 | local_arg.classes = &classes; |
| 1876 | local_arg.success = false; |
| 1877 | // We size the array assuming classes won't be added to the class table during the visit. |
| 1878 | // If this assumption fails we iterate again. |
| 1879 | while (!local_arg.success) { |
| 1880 | size_t class_table_size; |
| 1881 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1882 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1883 | class_table_size = class_table_.Size() + pre_zygote_class_table_.Size(); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1884 | } |
| 1885 | mirror::Class* class_type = mirror::Class::GetJavaLangClass(); |
| 1886 | mirror::Class* array_of_class = FindArrayClass(self, &class_type); |
| 1887 | classes.Assign( |
| 1888 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, class_table_size)); |
| 1889 | CHECK(classes.Get() != nullptr); // OOME. |
| 1890 | local_arg.index = 0; |
| 1891 | local_arg.success = true; |
| 1892 | VisitClasses(GetClassesVisitorArray, &local_arg); |
| 1893 | } |
| 1894 | for (int32_t i = 0; i < classes->GetLength(); ++i) { |
| 1895 | // If the class table shrank during creation of the clases array we expect null elements. If |
| 1896 | // the class table grew then the loop repeats. If classes are created after the loop has |
| 1897 | // finished then we don't visit. |
| 1898 | mirror::Class* klass = classes->Get(i); |
| 1899 | if (klass != nullptr && !visitor(klass, arg)) { |
| 1900 | return; |
| 1901 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1902 | } |
| 1903 | } |
| 1904 | } |
| 1905 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1906 | ClassLinker::~ClassLinker() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1907 | mirror::Class::ResetClass(); |
| 1908 | mirror::String::ResetClass(); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 1909 | mirror::Reference::ResetClass(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1910 | mirror::ArtField::ResetClass(); |
| 1911 | mirror::ArtMethod::ResetClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1912 | mirror::BooleanArray::ResetArrayClass(); |
| 1913 | mirror::ByteArray::ResetArrayClass(); |
| 1914 | mirror::CharArray::ResetArrayClass(); |
| 1915 | mirror::DoubleArray::ResetArrayClass(); |
| 1916 | mirror::FloatArray::ResetArrayClass(); |
| 1917 | mirror::IntArray::ResetArrayClass(); |
| 1918 | mirror::LongArray::ResetArrayClass(); |
| 1919 | mirror::ShortArray::ResetArrayClass(); |
| 1920 | mirror::Throwable::ResetClass(); |
| 1921 | mirror::StackTraceElement::ResetClass(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1922 | STLDeleteElements(&oat_files_); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1923 | } |
| 1924 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1925 | mirror::DexCache* ClassLinker::AllocDexCache(Thread* self, const DexFile& dex_file) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1926 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1927 | StackHandleScope<16> hs(self); |
| 1928 | Handle<mirror::Class> dex_cache_class(hs.NewHandle(GetClassRoot(kJavaLangDexCache))); |
| 1929 | Handle<mirror::DexCache> dex_cache( |
| 1930 | hs.NewHandle(down_cast<mirror::DexCache*>( |
| 1931 | heap->AllocObject<true>(self, dex_cache_class.Get(), dex_cache_class->GetObjectSize(), |
| 1932 | VoidFunctor())))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1933 | if (dex_cache.Get() == nullptr) { |
| 1934 | return nullptr; |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1935 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1936 | Handle<mirror::String> |
| 1937 | location(hs.NewHandle(intern_table_->InternStrong(dex_file.GetLocation().c_str()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1938 | if (location.Get() == nullptr) { |
| 1939 | return nullptr; |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1940 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1941 | Handle<mirror::ObjectArray<mirror::String>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1942 | strings(hs.NewHandle(AllocStringArray(self, dex_file.NumStringIds()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1943 | if (strings.Get() == nullptr) { |
| 1944 | return nullptr; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1945 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1946 | Handle<mirror::ObjectArray<mirror::Class>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1947 | types(hs.NewHandle(AllocClassArray(self, dex_file.NumTypeIds()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1948 | if (types.Get() == nullptr) { |
| 1949 | return nullptr; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1950 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1951 | Handle<mirror::ObjectArray<mirror::ArtMethod>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1952 | methods(hs.NewHandle(AllocArtMethodArray(self, dex_file.NumMethodIds()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1953 | if (methods.Get() == nullptr) { |
| 1954 | return nullptr; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1955 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1956 | Handle<mirror::ObjectArray<mirror::ArtField>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1957 | fields(hs.NewHandle(AllocArtFieldArray(self, dex_file.NumFieldIds()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1958 | if (fields.Get() == nullptr) { |
| 1959 | return nullptr; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1960 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1961 | dex_cache->Init(&dex_file, location.Get(), strings.Get(), types.Get(), methods.Get(), |
| 1962 | fields.Get()); |
| 1963 | return dex_cache.Get(); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1964 | } |
| 1965 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1966 | mirror::Class* ClassLinker::AllocClass(Thread* self, mirror::Class* java_lang_Class, |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1967 | uint32_t class_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1968 | DCHECK_GE(class_size, sizeof(mirror::Class)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1969 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1970 | mirror::Class::InitializeClassVisitor visitor(class_size); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1971 | mirror::Object* k = kMovingClasses ? |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1972 | heap->AllocObject<true>(self, java_lang_Class, class_size, visitor) : |
| 1973 | heap->AllocNonMovableObject<true>(self, java_lang_Class, class_size, visitor); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1974 | if (UNLIKELY(k == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1975 | CHECK(self->IsExceptionPending()); // OOME. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1976 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1977 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1978 | return k->AsClass(); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1979 | } |
| 1980 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1981 | mirror::Class* ClassLinker::AllocClass(Thread* self, uint32_t class_size) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1982 | return AllocClass(self, GetClassRoot(kJavaLangClass), class_size); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1983 | } |
| 1984 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1985 | mirror::ArtField* ClassLinker::AllocArtField(Thread* self) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1986 | return down_cast<mirror::ArtField*>( |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1987 | GetClassRoot(kJavaLangReflectArtField)->AllocNonMovableObject(self)); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1988 | } |
| 1989 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1990 | mirror::ArtMethod* ClassLinker::AllocArtMethod(Thread* self) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1991 | return down_cast<mirror::ArtMethod*>( |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1992 | GetClassRoot(kJavaLangReflectArtMethod)->AllocNonMovableObject(self)); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 1993 | } |
| 1994 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1995 | mirror::ObjectArray<mirror::StackTraceElement>* ClassLinker::AllocStackTraceElementArray( |
| 1996 | Thread* self, size_t length) { |
| 1997 | return mirror::ObjectArray<mirror::StackTraceElement>::Alloc( |
| 1998 | self, GetClassRoot(kJavaLangStackTraceElementArrayClass), length); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1999 | } |
| 2000 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2001 | mirror::Class* ClassLinker::EnsureResolved(Thread* self, const char* descriptor, |
| 2002 | mirror::Class* klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2003 | DCHECK(klass != nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2004 | |
| 2005 | // For temporary classes we must wait for them to be retired. |
| 2006 | if (init_done_ && klass->IsTemp()) { |
| 2007 | CHECK(!klass->IsResolved()); |
| 2008 | if (klass->IsErroneous()) { |
| 2009 | ThrowEarlierClassFailure(klass); |
| 2010 | return nullptr; |
| 2011 | } |
| 2012 | StackHandleScope<1> hs(self); |
| 2013 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 2014 | ObjectLock<mirror::Class> lock(self, h_class); |
| 2015 | // Loop and wait for the resolving thread to retire this class. |
| 2016 | while (!h_class->IsRetired() && !h_class->IsErroneous()) { |
| 2017 | lock.WaitIgnoringInterrupts(); |
| 2018 | } |
| 2019 | if (h_class->IsErroneous()) { |
| 2020 | ThrowEarlierClassFailure(h_class.Get()); |
| 2021 | return nullptr; |
| 2022 | } |
| 2023 | CHECK(h_class->IsRetired()); |
| 2024 | // Get the updated class from class table. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2025 | klass = LookupClass(self, descriptor, ComputeModifiedUtf8Hash(descriptor), |
| 2026 | h_class.Get()->GetClassLoader()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2027 | } |
| 2028 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2029 | // Wait for the class if it has not already been linked. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 2030 | if (!klass->IsResolved() && !klass->IsErroneous()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2031 | StackHandleScope<1> hs(self); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 2032 | HandleWrapper<mirror::Class> h_class(hs.NewHandleWrapper(&klass)); |
| 2033 | ObjectLock<mirror::Class> lock(self, h_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2034 | // Check for circular dependencies between classes. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2035 | if (!h_class->IsResolved() && h_class->GetClinitThreadId() == self->GetTid()) { |
| 2036 | ThrowClassCircularityError(h_class.Get()); |
| 2037 | h_class->SetStatus(mirror::Class::kStatusError, self); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2038 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2039 | } |
| 2040 | // Wait for the pending initialization to complete. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2041 | while (!h_class->IsResolved() && !h_class->IsErroneous()) { |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 2042 | lock.WaitIgnoringInterrupts(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2043 | } |
| 2044 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2045 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2046 | if (klass->IsErroneous()) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2047 | ThrowEarlierClassFailure(klass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2048 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2049 | } |
| 2050 | // Return the loaded class. No exceptions should be pending. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2051 | CHECK(klass->IsResolved()) << PrettyClass(klass); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2052 | self->AssertNoPendingException(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2053 | return klass; |
| 2054 | } |
| 2055 | |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2056 | typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry; |
| 2057 | |
| 2058 | // Search a collection of DexFiles for a descriptor |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2059 | ClassPathEntry FindInClassPath(const char* descriptor, |
| 2060 | size_t hash, const std::vector<const DexFile*>& class_path) { |
| 2061 | for (const DexFile* dex_file : class_path) { |
| 2062 | const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2063 | if (dex_class_def != nullptr) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2064 | return ClassPathEntry(dex_file, dex_class_def); |
| 2065 | } |
| 2066 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2067 | return ClassPathEntry(nullptr, nullptr); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2068 | } |
| 2069 | |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2070 | mirror::Class* ClassLinker::FindClassInPathClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
| 2071 | Thread* self, const char* descriptor, |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2072 | size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2073 | Handle<mirror::ClassLoader> class_loader) { |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2074 | // 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] | 2075 | if (class_loader->GetClass() != |
| 2076 | soa.Decode<mirror::Class*>(WellKnownClasses::dalvik_system_PathClassLoader) || |
| 2077 | class_loader->GetParent()->GetClass() != |
| 2078 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_BootClassLoader)) { |
| 2079 | return nullptr; |
| 2080 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2081 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2082 | // Check if this would be found in the parent boot class loader. |
| 2083 | if (pair.second != nullptr) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2084 | mirror::Class* klass = LookupClass(self, descriptor, hash, nullptr); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2085 | if (klass != nullptr) { |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2086 | // May return null if resolution on another thread fails. |
| 2087 | klass = EnsureResolved(self, descriptor, klass); |
| 2088 | } else { |
| 2089 | // May OOME. |
| 2090 | klass = DefineClass(self, descriptor, hash, NullHandle<mirror::ClassLoader>(), *pair.first, |
| 2091 | *pair.second); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2092 | } |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2093 | if (klass == nullptr) { |
| 2094 | CHECK(self->IsExceptionPending()) << descriptor; |
| 2095 | self->ClearException(); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2096 | } |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2097 | return klass; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2098 | } else { |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2099 | // Handle as if this is the child PathClassLoader. |
| 2100 | // Handles as RegisterDexFile may allocate dex caches (and cause thread suspension). |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2101 | StackHandleScope<3> hs(self); |
| 2102 | // The class loader is a PathClassLoader which inherits from BaseDexClassLoader. |
| 2103 | // We need to get the DexPathList and loop through it. |
| 2104 | Handle<mirror::ArtField> cookie_field = |
| 2105 | hs.NewHandle(soa.DecodeField(WellKnownClasses::dalvik_system_DexFile_cookie)); |
| 2106 | Handle<mirror::ArtField> dex_file_field = |
| 2107 | hs.NewHandle( |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 2108 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile)); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2109 | mirror::Object* dex_path_list = |
| 2110 | soa.DecodeField(WellKnownClasses::dalvik_system_PathClassLoader_pathList)-> |
| 2111 | GetObject(class_loader.Get()); |
| 2112 | if (dex_path_list != nullptr && dex_file_field.Get() != nullptr && |
| 2113 | cookie_field.Get() != nullptr) { |
| 2114 | // DexPathList has an array dexElements of Elements[] which each contain a dex file. |
| 2115 | mirror::Object* dex_elements_obj = |
| 2116 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList_dexElements)-> |
| 2117 | GetObject(dex_path_list); |
| 2118 | // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look |
| 2119 | // at the mCookie which is a DexFile vector. |
| 2120 | if (dex_elements_obj != nullptr) { |
| 2121 | Handle<mirror::ObjectArray<mirror::Object>> dex_elements = |
| 2122 | hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>()); |
| 2123 | for (int32_t i = 0; i < dex_elements->GetLength(); ++i) { |
| 2124 | mirror::Object* element = dex_elements->GetWithoutChecks(i); |
| 2125 | if (element == nullptr) { |
| 2126 | // Should never happen, fall back to java code to throw a NPE. |
| 2127 | break; |
| 2128 | } |
| 2129 | mirror::Object* dex_file = dex_file_field->GetObject(element); |
| 2130 | if (dex_file != nullptr) { |
| 2131 | const uint64_t cookie = cookie_field->GetLong(dex_file); |
| 2132 | auto* dex_files = |
| 2133 | reinterpret_cast<std::vector<const DexFile*>*>(static_cast<uintptr_t>(cookie)); |
| 2134 | if (dex_files == nullptr) { |
| 2135 | // This should never happen so log a warning. |
| 2136 | LOG(WARNING) << "Null DexFile::mCookie for " << descriptor; |
| 2137 | break; |
| 2138 | } |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2139 | for (const DexFile* cp_dex_file : *dex_files) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2140 | const DexFile::ClassDef* dex_class_def = cp_dex_file->FindClassDef(descriptor, hash); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2141 | if (dex_class_def != nullptr) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2142 | RegisterDexFile(*cp_dex_file); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2143 | mirror::Class* klass = DefineClass(self, descriptor, hash, class_loader, |
| 2144 | *cp_dex_file, *dex_class_def); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2145 | if (klass == nullptr) { |
| 2146 | CHECK(self->IsExceptionPending()) << descriptor; |
| 2147 | self->ClearException(); |
| 2148 | return nullptr; |
| 2149 | } |
| 2150 | return klass; |
| 2151 | } |
| 2152 | } |
| 2153 | } |
| 2154 | } |
| 2155 | } |
| 2156 | } |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2157 | self->AssertNoPendingException(); |
| 2158 | return nullptr; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2159 | } |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2160 | } |
| 2161 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2162 | mirror::Class* ClassLinker::FindClass(Thread* self, const char* descriptor, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2163 | Handle<mirror::ClassLoader> class_loader) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 2164 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2165 | DCHECK(self != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2166 | self->AssertNoPendingException(); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 2167 | if (descriptor[1] == '\0') { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2168 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 2169 | // for primitive classes that aren't backed by dex files. |
| 2170 | return FindPrimitiveClass(descriptor[0]); |
| 2171 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2172 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2173 | // Find the class in the loaded classes table. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2174 | mirror::Class* klass = LookupClass(self, descriptor, hash, class_loader.Get()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2175 | if (klass != nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2176 | return EnsureResolved(self, descriptor, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2177 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2178 | // Class is not yet loaded. |
| 2179 | if (descriptor[0] == '[') { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2180 | return CreateArrayClass(self, descriptor, hash, class_loader); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2181 | } else if (class_loader.Get() == nullptr) { |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2182 | // The boot class loader, search the boot class path. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2183 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2184 | if (pair.second != nullptr) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2185 | return DefineClass(self, descriptor, hash, NullHandle<mirror::ClassLoader>(), *pair.first, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2186 | *pair.second); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2187 | } else { |
| 2188 | // The boot class loader is searched ahead of the application class loader, failures are |
| 2189 | // expected and will be wrapped in a ClassNotFoundException. Use the pre-allocated error to |
| 2190 | // trigger the chaining with a proper stack trace. |
| 2191 | mirror::Throwable* pre_allocated = Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 2192 | self->SetException(ThrowLocation(), pre_allocated); |
| 2193 | return nullptr; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2194 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 2195 | } else if (Runtime::Current()->UseCompileTimeClassPath()) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2196 | // First try with the bootstrap class loader. |
| 2197 | if (class_loader.Get() != nullptr) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2198 | klass = LookupClass(self, descriptor, hash, nullptr); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2199 | if (klass != nullptr) { |
| 2200 | return EnsureResolved(self, descriptor, klass); |
| 2201 | } |
| 2202 | } |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2203 | // If the lookup failed search the boot class path. We don't perform a recursive call to avoid |
| 2204 | // a NoClassDefFoundError being allocated. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2205 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2206 | if (pair.second != nullptr) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2207 | return DefineClass(self, descriptor, hash, NullHandle<mirror::ClassLoader>(), *pair.first, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2208 | *pair.second); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2209 | } |
Ian Rogers | be7149f | 2013-08-20 09:29:39 -0700 | [diff] [blame] | 2210 | // Next try the compile time class path. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2211 | const std::vector<const DexFile*>* class_path; |
| 2212 | { |
Ian Rogers | be7149f | 2013-08-20 09:29:39 -0700 | [diff] [blame] | 2213 | ScopedObjectAccessUnchecked soa(self); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2214 | ScopedLocalRef<jobject> jclass_loader(soa.Env(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2215 | soa.AddLocalReference<jobject>(class_loader.Get())); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2216 | class_path = &Runtime::Current()->GetCompileTimeClassPath(jclass_loader.get()); |
| 2217 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2218 | pair = FindInClassPath(descriptor, hash, *class_path); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2219 | if (pair.second != nullptr) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2220 | return DefineClass(self, descriptor, hash, class_loader, *pair.first, *pair.second); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2221 | } else { |
| 2222 | // Use the pre-allocated NCDFE at compile time to avoid wasting time constructing exceptions. |
| 2223 | mirror::Throwable* pre_allocated = Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 2224 | self->SetException(ThrowLocation(), pre_allocated); |
| 2225 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2226 | } |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2227 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2228 | ScopedObjectAccessUnchecked soa(self); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2229 | mirror::Class* cp_klass = FindClassInPathClassLoader(soa, self, descriptor, hash, |
| 2230 | class_loader); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2231 | if (cp_klass != nullptr) { |
| 2232 | return cp_klass; |
Mathieu Chartier | 6bcae8f | 2014-09-04 18:33:17 -0700 | [diff] [blame] | 2233 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2234 | ScopedLocalRef<jobject> class_loader_object(soa.Env(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2235 | soa.AddLocalReference<jobject>(class_loader.Get())); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 2236 | std::string class_name_string(DescriptorToDot(descriptor)); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2237 | ScopedLocalRef<jobject> result(soa.Env(), nullptr); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 2238 | { |
| 2239 | ScopedThreadStateChange tsc(self, kNative); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2240 | ScopedLocalRef<jobject> class_name_object(soa.Env(), |
| 2241 | soa.Env()->NewStringUTF(class_name_string.c_str())); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2242 | if (class_name_object.get() == nullptr) { |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2243 | DCHECK(self->IsExceptionPending()); // OOME. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2244 | return nullptr; |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 2245 | } |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2246 | CHECK(class_loader_object.get() != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2247 | result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(), |
| 2248 | WellKnownClasses::java_lang_ClassLoader_loadClass, |
| 2249 | class_name_object.get())); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2250 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2251 | if (self->IsExceptionPending()) { |
Elliott Hughes | 748382f | 2012-01-26 18:07:38 -0800 | [diff] [blame] | 2252 | // If the ClassLoader threw, pass that exception up. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2253 | return nullptr; |
| 2254 | } else if (result.get() == nullptr) { |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 2255 | // broken loader - throw NPE to be compatible with Dalvik |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2256 | ThrowNullPointerException(nullptr, StringPrintf("ClassLoader.loadClass returned null for %s", |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2257 | class_name_string.c_str()).c_str()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2258 | return nullptr; |
Ian Rogers | 761bfa8 | 2012-01-11 10:14:05 -0800 | [diff] [blame] | 2259 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2260 | // success, return mirror::Class* |
| 2261 | return soa.Decode<mirror::Class*>(result.get()); |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 2262 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2263 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 2264 | UNREACHABLE(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2265 | } |
| 2266 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2267 | mirror::Class* ClassLinker::DefineClass(Thread* self, const char* descriptor, size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2268 | Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2269 | const DexFile& dex_file, |
| 2270 | const DexFile::ClassDef& dex_class_def) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2271 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2272 | auto klass = hs.NewHandle<mirror::Class>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2273 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2274 | // Load the class from the dex file. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2275 | if (UNLIKELY(!init_done_)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2276 | // finish up init of hand crafted class_roots_ |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2277 | if (strcmp(descriptor, "Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2278 | klass.Assign(GetClassRoot(kJavaLangObject)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2279 | } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2280 | klass.Assign(GetClassRoot(kJavaLangClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2281 | } else if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2282 | klass.Assign(GetClassRoot(kJavaLangString)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 2283 | } else if (strcmp(descriptor, "Ljava/lang/ref/Reference;") == 0) { |
| 2284 | klass.Assign(GetClassRoot(kJavaLangRefReference)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2285 | } else if (strcmp(descriptor, "Ljava/lang/DexCache;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2286 | klass.Assign(GetClassRoot(kJavaLangDexCache)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2287 | } else if (strcmp(descriptor, "Ljava/lang/reflect/ArtField;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2288 | klass.Assign(GetClassRoot(kJavaLangReflectArtField)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2289 | } else if (strcmp(descriptor, "Ljava/lang/reflect/ArtMethod;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2290 | klass.Assign(GetClassRoot(kJavaLangReflectArtMethod)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2291 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2292 | } |
| 2293 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2294 | if (klass.Get() == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2295 | // Allocate a class with the status of not ready. |
| 2296 | // Interface object should get the right size here. Regular class will |
| 2297 | // figure out the right size later and be replaced with one of the right |
| 2298 | // size when the class becomes resolved. |
| 2299 | klass.Assign(AllocClass(self, SizeOfClassWithoutEmbeddedTables(dex_file, dex_class_def))); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2300 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2301 | if (UNLIKELY(klass.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2302 | CHECK(self->IsExceptionPending()); // Expect an OOME. |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2303 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2304 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2305 | klass->SetDexCache(FindDexCache(dex_file)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2306 | LoadClass(self, dex_file, dex_class_def, klass, class_loader.Get()); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 2307 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2308 | if (self->IsExceptionPending()) { |
| 2309 | // An exception occured during load, set status to erroneous while holding klass' lock in case |
| 2310 | // notification is necessary. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2311 | if (!klass->IsErroneous()) { |
| 2312 | klass->SetStatus(mirror::Class::kStatusError, self); |
| 2313 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2314 | return nullptr; |
| 2315 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2316 | klass->SetClinitThreadId(self->GetTid()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2317 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2318 | // Add the newly loaded class to the loaded classes table. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2319 | mirror::Class* existing = InsertClass(descriptor, klass.Get(), hash); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2320 | if (existing != nullptr) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2321 | // We failed to insert because we raced with another thread. Calling EnsureResolved may cause |
| 2322 | // this thread to block. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2323 | return EnsureResolved(self, descriptor, existing); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2324 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2325 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2326 | // Finish loading (if necessary) by finding parents |
| 2327 | CHECK(!klass->IsLoaded()); |
| 2328 | if (!LoadSuperAndInterfaces(klass, dex_file)) { |
| 2329 | // Loading failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2330 | if (!klass->IsErroneous()) { |
| 2331 | klass->SetStatus(mirror::Class::kStatusError, self); |
| 2332 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2333 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2334 | } |
| 2335 | CHECK(klass->IsLoaded()); |
| 2336 | // Link the class (if necessary) |
| 2337 | CHECK(!klass->IsResolved()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2338 | // TODO: Use fast jobjects? |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2339 | auto interfaces = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2340 | |
| 2341 | mirror::Class* new_class = nullptr; |
| 2342 | if (!LinkClass(self, descriptor, klass, interfaces, &new_class)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2343 | // Linking failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2344 | if (!klass->IsErroneous()) { |
| 2345 | klass->SetStatus(mirror::Class::kStatusError, self); |
| 2346 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2347 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2348 | } |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 2349 | self->AssertNoPendingException(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2350 | CHECK(new_class != nullptr) << descriptor; |
| 2351 | CHECK(new_class->IsResolved()) << descriptor; |
| 2352 | |
| 2353 | Handle<mirror::Class> new_class_h(hs.NewHandle(new_class)); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2354 | |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 2355 | // Instrumentation may have updated entrypoints for all methods of all |
| 2356 | // classes. However it could not update methods of this class while we |
| 2357 | // were loading it. Now the class is resolved, we can update entrypoints |
| 2358 | // as required by instrumentation. |
| 2359 | if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) { |
| 2360 | // We must be in the kRunnable state to prevent instrumentation from |
| 2361 | // suspending all threads to update entrypoints while we are doing it |
| 2362 | // for this class. |
| 2363 | DCHECK_EQ(self->GetState(), kRunnable); |
| 2364 | Runtime::Current()->GetInstrumentation()->InstallStubsForClass(new_class_h.Get()); |
| 2365 | } |
| 2366 | |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2367 | /* |
| 2368 | * We send CLASS_PREPARE events to the debugger from here. The |
| 2369 | * definition of "preparation" is creating the static fields for a |
| 2370 | * class and initializing them to the standard default values, but not |
| 2371 | * executing any code (that comes later, during "initialization"). |
| 2372 | * |
| 2373 | * We did the static preparation in LinkClass. |
| 2374 | * |
| 2375 | * The class has been prepared and resolved but possibly not yet verified |
| 2376 | * at this point. |
| 2377 | */ |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2378 | Dbg::PostClassPrepare(new_class_h.Get()); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2379 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2380 | return new_class_h.Get(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2381 | } |
| 2382 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2383 | uint32_t ClassLinker::SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, |
| 2384 | const DexFile::ClassDef& dex_class_def) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2385 | const uint8_t* class_data = dex_file.GetClassData(dex_class_def); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2386 | size_t num_ref = 0; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2387 | size_t num_8 = 0; |
| 2388 | size_t num_16 = 0; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2389 | size_t num_32 = 0; |
| 2390 | size_t num_64 = 0; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2391 | if (class_data != nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2392 | for (ClassDataItemIterator it(dex_file, class_data); it.HasNextStaticField(); it.Next()) { |
| 2393 | const DexFile::FieldId& field_id = dex_file.GetFieldId(it.GetMemberIndex()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2394 | const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2395 | char c = descriptor[0]; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2396 | switch (c) { |
| 2397 | case 'L': |
| 2398 | case '[': |
| 2399 | num_ref++; |
| 2400 | break; |
| 2401 | case 'J': |
| 2402 | case 'D': |
| 2403 | num_64++; |
| 2404 | break; |
| 2405 | case 'I': |
| 2406 | case 'F': |
| 2407 | num_32++; |
| 2408 | break; |
| 2409 | case 'S': |
| 2410 | case 'C': |
| 2411 | num_16++; |
| 2412 | break; |
| 2413 | case 'B': |
| 2414 | case 'Z': |
| 2415 | num_8++; |
| 2416 | break; |
| 2417 | default: |
| 2418 | LOG(FATAL) << "Unknown descriptor: " << c; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 2419 | UNREACHABLE(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2420 | } |
| 2421 | } |
| 2422 | } |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2423 | 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] | 2424 | } |
| 2425 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2426 | OatFile::OatClass ClassLinker::FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, |
| 2427 | bool* found) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2428 | DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 2429 | const OatFile::OatDexFile* oat_dex_file = FindOpenedOatDexFileForDexFile(dex_file); |
| 2430 | if (oat_dex_file == nullptr) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2431 | *found = false; |
| 2432 | return OatFile::OatClass::Invalid(); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2433 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2434 | *found = true; |
| 2435 | return oat_dex_file->GetOatClass(class_def_idx); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2436 | } |
| 2437 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2438 | static uint32_t GetOatMethodIndexFromMethodIndex(const DexFile& dex_file, uint16_t class_def_idx, |
| 2439 | uint32_t method_idx) { |
| 2440 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_idx); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2441 | const uint8_t* class_data = dex_file.GetClassData(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2442 | CHECK(class_data != nullptr); |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2443 | ClassDataItemIterator it(dex_file, class_data); |
| 2444 | // Skip fields |
| 2445 | while (it.HasNextStaticField()) { |
| 2446 | it.Next(); |
| 2447 | } |
| 2448 | while (it.HasNextInstanceField()) { |
| 2449 | it.Next(); |
| 2450 | } |
| 2451 | // Process methods |
| 2452 | size_t class_def_method_index = 0; |
| 2453 | while (it.HasNextDirectMethod()) { |
| 2454 | if (it.GetMemberIndex() == method_idx) { |
| 2455 | return class_def_method_index; |
| 2456 | } |
| 2457 | class_def_method_index++; |
| 2458 | it.Next(); |
| 2459 | } |
| 2460 | while (it.HasNextVirtualMethod()) { |
| 2461 | if (it.GetMemberIndex() == method_idx) { |
| 2462 | return class_def_method_index; |
| 2463 | } |
| 2464 | class_def_method_index++; |
| 2465 | it.Next(); |
| 2466 | } |
| 2467 | DCHECK(!it.HasNext()); |
| 2468 | 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] | 2469 | UNREACHABLE(); |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2470 | } |
| 2471 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2472 | const OatFile::OatMethod ClassLinker::FindOatMethodFor(mirror::ArtMethod* method, bool* found) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2473 | // 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] | 2474 | // method for direct methods (or virtual methods made direct). |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2475 | mirror::Class* declaring_class = method->GetDeclaringClass(); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2476 | size_t oat_method_index; |
| 2477 | if (method->IsStatic() || method->IsDirect()) { |
| 2478 | // Simple case where the oat method index was stashed at load time. |
| 2479 | oat_method_index = method->GetMethodIndex(); |
| 2480 | } else { |
| 2481 | // We're invoking a virtual method directly (thanks to sharpening), compute the oat_method_index |
| 2482 | // by search for its position in the declared virtual methods. |
| 2483 | oat_method_index = declaring_class->NumDirectMethods(); |
| 2484 | size_t end = declaring_class->NumVirtualMethods(); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2485 | bool found_virtual = false; |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2486 | for (size_t i = 0; i < end; i++) { |
Jeff Hao | 68caf9e | 2014-09-03 13:48:16 -0700 | [diff] [blame] | 2487 | // Check method index instead of identity in case of duplicate method definitions. |
| 2488 | if (method->GetDexMethodIndex() == |
| 2489 | declaring_class->GetVirtualMethod(i)->GetDexMethodIndex()) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2490 | found_virtual = true; |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2491 | break; |
| 2492 | } |
Ian Rogers | f320b63 | 2012-03-13 18:47:47 -0700 | [diff] [blame] | 2493 | oat_method_index++; |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2494 | } |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2495 | CHECK(found_virtual) << "Didn't find oat method index for virtual method: " |
| 2496 | << PrettyMethod(method); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2497 | } |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2498 | DCHECK_EQ(oat_method_index, |
| 2499 | GetOatMethodIndexFromMethodIndex(*declaring_class->GetDexCache()->GetDexFile(), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2500 | method->GetDeclaringClass()->GetDexClassDefIndex(), |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2501 | method->GetDexMethodIndex())); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2502 | OatFile::OatClass oat_class = FindOatClass(*declaring_class->GetDexCache()->GetDexFile(), |
| 2503 | declaring_class->GetDexClassDefIndex(), |
| 2504 | found); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2505 | if (!(*found)) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2506 | return OatFile::OatMethod::Invalid(); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2507 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2508 | return oat_class.GetOatMethod(oat_method_index); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2509 | } |
| 2510 | |
| 2511 | // Special case to get oat code without overwriting a trampoline. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2512 | const void* ClassLinker::GetQuickOatCodeFor(mirror::ArtMethod* method) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2513 | CHECK(!method->IsAbstract()) << PrettyMethod(method); |
Jeff Hao | 8df6cea | 2013-07-29 13:54:48 -0700 | [diff] [blame] | 2514 | if (method->IsProxyMethod()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2515 | return GetQuickProxyInvokeHandler(); |
Jeff Hao | 8df6cea | 2013-07-29 13:54:48 -0700 | [diff] [blame] | 2516 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2517 | bool found; |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 2518 | jit::Jit* const jit = Runtime::Current()->GetJit(); |
| 2519 | if (jit != nullptr) { |
| 2520 | auto* code = jit->GetCodeCache()->GetCodeFor(method); |
| 2521 | if (code != nullptr) { |
| 2522 | return code; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2523 | } |
Ian Rogers | f3e9855 | 2013-03-20 15:49:49 -0700 | [diff] [blame] | 2524 | } |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 2525 | OatFile::OatMethod oat_method = FindOatMethodFor(method, &found); |
| 2526 | if (found) { |
| 2527 | auto* code = oat_method.GetQuickCode(); |
| 2528 | if (code != nullptr) { |
| 2529 | return code; |
| 2530 | } |
| 2531 | } |
| 2532 | if (method->IsNative()) { |
| 2533 | // No code and native? Use generic trampoline. |
| 2534 | return GetQuickGenericJniStub(); |
| 2535 | } |
| 2536 | return GetQuickToInterpreterBridge(); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2537 | } |
| 2538 | |
Hiroshi Yamauchi | 9bdec88 | 2014-08-15 17:11:12 -0700 | [diff] [blame] | 2539 | const void* ClassLinker::GetOatMethodQuickCodeFor(mirror::ArtMethod* method) { |
| 2540 | if (method->IsNative() || method->IsAbstract() || method->IsProxyMethod()) { |
| 2541 | return nullptr; |
| 2542 | } |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 2543 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 2544 | if (jit != nullptr) { |
| 2545 | auto* code = jit->GetCodeCache()->GetCodeFor(method); |
| 2546 | if (code != nullptr) { |
| 2547 | return code; |
| 2548 | } |
| 2549 | } |
Hiroshi Yamauchi | 9bdec88 | 2014-08-15 17:11:12 -0700 | [diff] [blame] | 2550 | bool found; |
| 2551 | OatFile::OatMethod oat_method = FindOatMethodFor(method, &found); |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 2552 | if (found) { |
| 2553 | return oat_method.GetQuickCode(); |
| 2554 | } |
| 2555 | return nullptr; |
Hiroshi Yamauchi | 9bdec88 | 2014-08-15 17:11:12 -0700 | [diff] [blame] | 2556 | } |
| 2557 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2558 | const void* ClassLinker::GetQuickOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, |
| 2559 | uint32_t method_idx) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2560 | bool found; |
| 2561 | OatFile::OatClass oat_class = FindOatClass(dex_file, class_def_idx, &found); |
| 2562 | if (!found) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2563 | return nullptr; |
| 2564 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2565 | 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] | 2566 | return oat_class.GetOatMethod(oat_method_idx).GetQuickCode(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2567 | } |
| 2568 | |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2569 | // Returns true if the method must run with interpreter, false otherwise. |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2570 | static bool NeedsInterpreter(mirror::ArtMethod* method, const void* quick_code) |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 2571 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2572 | if (quick_code == nullptr) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2573 | // No code: need interpreter. |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 2574 | // May return true for native code, in the case of generic JNI |
| 2575 | // DCHECK(!method->IsNative()); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2576 | return true; |
| 2577 | } |
| 2578 | // If interpreter mode is enabled, every method (except native and proxy) must |
| 2579 | // be run with interpreter. |
| 2580 | return Runtime::Current()->GetInstrumentation()->InterpretOnly() && |
| 2581 | !method->IsNative() && !method->IsProxyMethod(); |
| 2582 | } |
| 2583 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2584 | void ClassLinker::FixupStaticTrampolines(mirror::Class* klass) { |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 2585 | DCHECK(klass->IsInitialized()) << PrettyDescriptor(klass); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2586 | if (klass->NumDirectMethods() == 0) { |
| 2587 | return; // No direct methods => no static methods. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2588 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2589 | Runtime* runtime = Runtime::Current(); |
| 2590 | if (!runtime->IsStarted() || runtime->UseCompileTimeClassPath()) { |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 2591 | if (runtime->IsAotCompiler() || runtime->GetHeap()->HasImageSpace()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 2592 | return; // OAT file unavailable. |
| 2593 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2594 | } |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 2595 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 2596 | const DexFile& dex_file = klass->GetDexFile(); |
| 2597 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2598 | CHECK(dex_class_def != nullptr); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2599 | const uint8_t* class_data = dex_file.GetClassData(*dex_class_def); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2600 | // There should always be class data if there were direct methods. |
| 2601 | CHECK(class_data != nullptr) << PrettyDescriptor(klass); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2602 | ClassDataItemIterator it(dex_file, class_data); |
| 2603 | // Skip fields |
| 2604 | while (it.HasNextStaticField()) { |
| 2605 | it.Next(); |
| 2606 | } |
| 2607 | while (it.HasNextInstanceField()) { |
| 2608 | it.Next(); |
| 2609 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2610 | bool has_oat_class; |
| 2611 | OatFile::OatClass oat_class = FindOatClass(dex_file, klass->GetDexClassDefIndex(), |
| 2612 | &has_oat_class); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2613 | // Link the code of methods skipped by LinkCode. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2614 | for (size_t method_index = 0; it.HasNextDirectMethod(); ++method_index, it.Next()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2615 | mirror::ArtMethod* method = klass->GetDirectMethod(method_index); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2616 | if (!method->IsStatic()) { |
| 2617 | // Only update static methods. |
| 2618 | continue; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2619 | } |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2620 | const void* quick_code = nullptr; |
| 2621 | if (has_oat_class) { |
| 2622 | OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2623 | quick_code = oat_method.GetQuickCode(); |
| 2624 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2625 | const bool enter_interpreter = NeedsInterpreter(method, quick_code); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2626 | if (enter_interpreter) { |
| 2627 | // Use interpreter entry point. |
Ian Rogers | 1a57066 | 2014-03-12 01:02:21 -0700 | [diff] [blame] | 2628 | // 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] | 2629 | if (quick_code == nullptr && method->IsNative()) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2630 | quick_code = GetQuickGenericJniStub(); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 2631 | } else { |
| 2632 | quick_code = GetQuickToInterpreterBridge(); |
| 2633 | } |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2634 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2635 | runtime->GetInstrumentation()->UpdateMethodsCode(method, quick_code); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2636 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2637 | // Ignore virtual methods on the iterator. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2638 | } |
| 2639 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2640 | void ClassLinker::LinkCode(Handle<mirror::ArtMethod> method, |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 2641 | const OatFile::OatClass* oat_class, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2642 | uint32_t class_def_method_index) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2643 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 2644 | if (runtime->IsAotCompiler()) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2645 | // The following code only applies to a non-compiler runtime. |
| 2646 | return; |
| 2647 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2648 | // Method shouldn't have already been linked. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2649 | DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2650 | if (oat_class != nullptr) { |
| 2651 | // Every kind of method should at least get an invoke stub from the oat_method. |
| 2652 | // non-abstract methods also get their code pointers. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2653 | const OatFile::OatMethod oat_method = oat_class->GetOatMethod(class_def_method_index); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2654 | oat_method.LinkMethod(method.Get()); |
| 2655 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2656 | |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 2657 | // Install entry point from interpreter. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2658 | bool enter_interpreter = NeedsInterpreter(method.Get(), |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2659 | method->GetEntryPointFromQuickCompiledCode()); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2660 | if (enter_interpreter && !method->IsNative()) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2661 | method->SetEntryPointFromInterpreter(artInterpreterToInterpreterBridge); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 2662 | } else { |
Dragos Sbirlea | 08bf196 | 2013-08-12 08:53:04 -0700 | [diff] [blame] | 2663 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 2664 | } |
| 2665 | |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2666 | if (method->IsAbstract()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2667 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2668 | return; |
| 2669 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2670 | |
| 2671 | if (method->IsStatic() && !method->IsConstructor()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2672 | // For static methods excluding the class initializer, install the trampoline. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2673 | // It will be replaced by the proper entry point by ClassLinker::FixupStaticTrampolines |
| 2674 | // after initializing class (see ClassLinker::InitializeClass method). |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2675 | method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionStub()); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2676 | } else if (enter_interpreter) { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2677 | if (!method->IsNative()) { |
| 2678 | // Set entry point from compiled code if there's no code or in interpreter only mode. |
| 2679 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2680 | } else { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2681 | method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub()); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2682 | } |
Ian Rogers | 0d6de04 | 2012-02-29 08:50:26 -0800 | [diff] [blame] | 2683 | } |
jeffhao | 26c0a1a | 2012-01-17 16:28:33 -0800 | [diff] [blame] | 2684 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2685 | if (method->IsNative()) { |
| 2686 | // Unregistering restores the dlsym lookup stub. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2687 | method->UnregisterNative(); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 2688 | |
| 2689 | if (enter_interpreter) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2690 | // We have a native method here without code. Then it should have either the generic JNI |
| 2691 | // trampoline as entrypoint (non-static), or the resolution trampoline (static). |
| 2692 | // TODO: this doesn't handle all the cases where trampolines may be installed. |
| 2693 | const void* entry_point = method->GetEntryPointFromQuickCompiledCode(); |
| 2694 | DCHECK(IsQuickGenericJniStub(entry_point) || IsQuickResolutionStub(entry_point)); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 2695 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2696 | } |
| 2697 | } |
| 2698 | |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2699 | |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2700 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2701 | void ClassLinker::LoadClass(Thread* self, const DexFile& dex_file, |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 2702 | const DexFile::ClassDef& dex_class_def, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2703 | Handle<mirror::Class> klass, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2704 | mirror::ClassLoader* class_loader) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2705 | CHECK(klass.Get() != nullptr); |
| 2706 | CHECK(klass->GetDexCache() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2707 | CHECK_EQ(mirror::Class::kStatusNotReady, klass->GetStatus()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 2708 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2709 | CHECK(descriptor != nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2710 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2711 | klass->SetClass(GetClassRoot(kJavaLangClass)); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 2712 | uint32_t access_flags = dex_class_def.GetJavaAccessFlags(); |
Brian Carlstrom | 8e3fb14 | 2013-10-09 21:00:27 -0700 | [diff] [blame] | 2713 | CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2714 | klass->SetAccessFlags(access_flags); |
| 2715 | klass->SetClassLoader(class_loader); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2716 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2717 | klass->SetStatus(mirror::Class::kStatusIdx, nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2718 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2719 | klass->SetDexClassDefIndex(dex_file.GetIndexForClassDef(dex_class_def)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2720 | klass->SetDexTypeIndex(dex_class_def.class_idx_); |
Mathieu Chartier | 91a6dc4 | 2014-12-01 10:31:15 -0800 | [diff] [blame] | 2721 | CHECK(klass->GetDexCacheStrings() != nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2722 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2723 | const uint8_t* class_data = dex_file.GetClassData(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2724 | if (class_data == nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2725 | return; // no fields or methods - for example a marker interface |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2726 | } |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2727 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2728 | |
| 2729 | bool has_oat_class = false; |
| 2730 | if (Runtime::Current()->IsStarted() && !Runtime::Current()->UseCompileTimeClassPath()) { |
| 2731 | OatFile::OatClass oat_class = FindOatClass(dex_file, klass->GetDexClassDefIndex(), |
| 2732 | &has_oat_class); |
| 2733 | if (has_oat_class) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2734 | LoadClassMembers(self, dex_file, class_data, klass, &oat_class); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2735 | } |
| 2736 | } |
| 2737 | if (!has_oat_class) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2738 | LoadClassMembers(self, dex_file, class_data, klass, nullptr); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2739 | } |
| 2740 | } |
| 2741 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2742 | void ClassLinker::LoadClassMembers(Thread* self, const DexFile& dex_file, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2743 | const uint8_t* class_data, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2744 | Handle<mirror::Class> klass, |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2745 | const OatFile::OatClass* oat_class) { |
| 2746 | // Load fields. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2747 | ClassDataItemIterator it(dex_file, class_data); |
| 2748 | if (it.NumStaticFields() != 0) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2749 | mirror::ObjectArray<mirror::ArtField>* statics = AllocArtFieldArray(self, it.NumStaticFields()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2750 | if (UNLIKELY(statics == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2751 | CHECK(self->IsExceptionPending()); // OOME. |
| 2752 | return; |
| 2753 | } |
| 2754 | klass->SetSFields(statics); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2755 | } |
| 2756 | if (it.NumInstanceFields() != 0) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2757 | mirror::ObjectArray<mirror::ArtField>* fields = |
| 2758 | AllocArtFieldArray(self, it.NumInstanceFields()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2759 | if (UNLIKELY(fields == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2760 | CHECK(self->IsExceptionPending()); // OOME. |
| 2761 | return; |
| 2762 | } |
| 2763 | klass->SetIFields(fields); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2764 | } |
| 2765 | for (size_t i = 0; it.HasNextStaticField(); i++, it.Next()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2766 | self->AllowThreadSuspension(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2767 | StackHandleScope<1> hs(self); |
| 2768 | Handle<mirror::ArtField> sfield(hs.NewHandle(AllocArtField(self))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2769 | if (UNLIKELY(sfield.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2770 | CHECK(self->IsExceptionPending()); // OOME. |
| 2771 | return; |
| 2772 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2773 | klass->SetStaticField(i, sfield.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2774 | LoadField(dex_file, it, klass, sfield); |
| 2775 | } |
| 2776 | for (size_t i = 0; it.HasNextInstanceField(); i++, it.Next()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2777 | self->AllowThreadSuspension(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2778 | StackHandleScope<1> hs(self); |
| 2779 | Handle<mirror::ArtField> ifield(hs.NewHandle(AllocArtField(self))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2780 | if (UNLIKELY(ifield.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2781 | CHECK(self->IsExceptionPending()); // OOME. |
| 2782 | return; |
| 2783 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2784 | klass->SetInstanceField(i, ifield.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2785 | LoadField(dex_file, it, klass, ifield); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2786 | } |
| 2787 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2788 | // Load methods. |
| 2789 | if (it.NumDirectMethods() != 0) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2790 | // TODO: append direct methods to class object |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2791 | mirror::ObjectArray<mirror::ArtMethod>* directs = |
| 2792 | AllocArtMethodArray(self, it.NumDirectMethods()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2793 | if (UNLIKELY(directs == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2794 | CHECK(self->IsExceptionPending()); // OOME. |
| 2795 | return; |
| 2796 | } |
| 2797 | klass->SetDirectMethods(directs); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2798 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2799 | if (it.NumVirtualMethods() != 0) { |
| 2800 | // TODO: append direct methods to class object |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2801 | mirror::ObjectArray<mirror::ArtMethod>* virtuals = |
| 2802 | AllocArtMethodArray(self, it.NumVirtualMethods()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2803 | if (UNLIKELY(virtuals == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2804 | CHECK(self->IsExceptionPending()); // OOME. |
| 2805 | return; |
| 2806 | } |
| 2807 | klass->SetVirtualMethods(virtuals); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2808 | } |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2809 | size_t class_def_method_index = 0; |
Jeff Hao | 68caf9e | 2014-09-03 13:48:16 -0700 | [diff] [blame] | 2810 | uint32_t last_dex_method_index = DexFile::kDexNoIndex; |
| 2811 | size_t last_class_def_method_index = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2812 | for (size_t i = 0; it.HasNextDirectMethod(); i++, it.Next()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2813 | self->AllowThreadSuspension(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2814 | StackHandleScope<1> hs(self); |
| 2815 | Handle<mirror::ArtMethod> method(hs.NewHandle(LoadMethod(self, dex_file, it, klass))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2816 | if (UNLIKELY(method.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2817 | CHECK(self->IsExceptionPending()); // OOME. |
| 2818 | return; |
| 2819 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2820 | klass->SetDirectMethod(i, method.Get()); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2821 | LinkCode(method, oat_class, class_def_method_index); |
Jeff Hao | 68caf9e | 2014-09-03 13:48:16 -0700 | [diff] [blame] | 2822 | uint32_t it_method_index = it.GetMemberIndex(); |
| 2823 | if (last_dex_method_index == it_method_index) { |
| 2824 | // duplicate case |
| 2825 | method->SetMethodIndex(last_class_def_method_index); |
| 2826 | } else { |
| 2827 | method->SetMethodIndex(class_def_method_index); |
| 2828 | last_dex_method_index = it_method_index; |
| 2829 | last_class_def_method_index = class_def_method_index; |
| 2830 | } |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2831 | class_def_method_index++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2832 | } |
| 2833 | for (size_t i = 0; it.HasNextVirtualMethod(); i++, it.Next()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2834 | self->AllowThreadSuspension(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2835 | StackHandleScope<1> hs(self); |
| 2836 | Handle<mirror::ArtMethod> method(hs.NewHandle(LoadMethod(self, dex_file, it, klass))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2837 | if (UNLIKELY(method.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2838 | CHECK(self->IsExceptionPending()); // OOME. |
| 2839 | return; |
| 2840 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2841 | klass->SetVirtualMethod(i, method.Get()); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2842 | DCHECK_EQ(class_def_method_index, it.NumDirectMethods() + i); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2843 | LinkCode(method, oat_class, class_def_method_index); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2844 | class_def_method_index++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2845 | } |
| 2846 | DCHECK(!it.HasNext()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2847 | } |
| 2848 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 2849 | void ClassLinker::LoadField(const DexFile& /*dex_file*/, const ClassDataItemIterator& it, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2850 | Handle<mirror::Class> klass, |
| 2851 | Handle<mirror::ArtField> dst) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2852 | uint32_t field_idx = it.GetMemberIndex(); |
| 2853 | dst->SetDexFieldIndex(field_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2854 | dst->SetDeclaringClass(klass.Get()); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 2855 | dst->SetAccessFlags(it.GetFieldAccessFlags()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2856 | } |
| 2857 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2858 | mirror::ArtMethod* ClassLinker::LoadMethod(Thread* self, const DexFile& dex_file, |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2859 | const ClassDataItemIterator& it, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2860 | Handle<mirror::Class> klass) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2861 | uint32_t dex_method_idx = it.GetMemberIndex(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2862 | const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2863 | const char* method_name = dex_file.StringDataByIdx(method_id.name_idx_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2864 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2865 | mirror::ArtMethod* dst = AllocArtMethod(self); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2866 | if (UNLIKELY(dst == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2867 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2868 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2869 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2870 | DCHECK(dst->IsArtMethod()) << PrettyDescriptor(dst->GetClass()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2871 | |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 2872 | ScopedAssertNoThreadSuspension ants(self, "LoadMethod"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2873 | dst->SetDexMethodIndex(dex_method_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2874 | dst->SetDeclaringClass(klass.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2875 | dst->SetCodeItemOffset(it.GetMethodCodeItemOffset()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2876 | |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2877 | dst->SetDexCacheResolvedMethods(klass->GetDexCache()->GetResolvedMethods()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2878 | dst->SetDexCacheResolvedTypes(klass->GetDexCache()->GetResolvedTypes()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2879 | |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 2880 | uint32_t access_flags = it.GetMethodAccessFlags(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2881 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2882 | if (UNLIKELY(strcmp("finalize", method_name) == 0)) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2883 | // Set finalizable flag on declaring class. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2884 | if (strcmp("V", dex_file.GetShorty(method_id.proto_idx_)) == 0) { |
| 2885 | // Void return type. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2886 | if (klass->GetClassLoader() != nullptr) { // All non-boot finalizer methods are flagged. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2887 | klass->SetFinalizable(); |
| 2888 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2889 | std::string temp; |
| 2890 | const char* klass_descriptor = klass->GetDescriptor(&temp); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2891 | // The Enum class declares a "final" finalize() method to prevent subclasses from |
| 2892 | // introducing a finalizer. We don't want to set the finalizable flag for Enum or its |
| 2893 | // subclasses, so we exclude it here. |
| 2894 | // We also want to avoid setting the flag on Object, where we know that finalize() is |
| 2895 | // empty. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2896 | if (strcmp(klass_descriptor, "Ljava/lang/Object;") != 0 && |
| 2897 | strcmp(klass_descriptor, "Ljava/lang/Enum;") != 0) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2898 | klass->SetFinalizable(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2899 | } |
| 2900 | } |
| 2901 | } |
| 2902 | } else if (method_name[0] == '<') { |
| 2903 | // Fix broken access flags for initializers. Bug 11157540. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2904 | bool is_init = (strcmp("<init>", method_name) == 0); |
| 2905 | bool is_clinit = !is_init && (strcmp("<clinit>", method_name) == 0); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2906 | if (UNLIKELY(!is_init && !is_clinit)) { |
| 2907 | LOG(WARNING) << "Unexpected '<' at start of method name " << method_name; |
| 2908 | } else { |
| 2909 | if (UNLIKELY((access_flags & kAccConstructor) == 0)) { |
| 2910 | 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] | 2911 | << PrettyDescriptor(klass.Get()) << " in dex file " << dex_file.GetLocation(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2912 | access_flags |= kAccConstructor; |
| 2913 | } |
| 2914 | } |
| 2915 | } |
| 2916 | dst->SetAccessFlags(access_flags); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2917 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2918 | return dst; |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2919 | } |
| 2920 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2921 | void ClassLinker::AppendToBootClassPath(Thread* self, const DexFile& dex_file) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2922 | StackHandleScope<1> hs(self); |
| 2923 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(AllocDexCache(self, dex_file))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2924 | CHECK(dex_cache.Get() != nullptr) << "Failed to allocate dex cache for " |
| 2925 | << dex_file.GetLocation(); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2926 | AppendToBootClassPath(dex_file, dex_cache); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2927 | } |
| 2928 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2929 | void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2930 | Handle<mirror::DexCache> dex_cache) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2931 | CHECK(dex_cache.Get() != nullptr) << dex_file.GetLocation(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2932 | boot_class_path_.push_back(&dex_file); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2933 | RegisterDexFile(dex_file, dex_cache); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2934 | } |
| 2935 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2936 | bool ClassLinker::IsDexFileRegisteredLocked(const DexFile& dex_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2937 | dex_lock_.AssertSharedHeld(Thread::Current()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2938 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2939 | mirror::DexCache* dex_cache = GetDexCache(i); |
| 2940 | if (dex_cache->GetDexFile() == &dex_file) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2941 | return true; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2942 | } |
| 2943 | } |
| 2944 | return false; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2945 | } |
| 2946 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2947 | bool ClassLinker::IsDexFileRegistered(const DexFile& dex_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2948 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | 0691851 | 2011-10-16 23:39:12 -0700 | [diff] [blame] | 2949 | return IsDexFileRegisteredLocked(dex_file); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2950 | } |
| 2951 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2952 | void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2953 | Handle<mirror::DexCache> dex_cache) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2954 | dex_lock_.AssertExclusiveHeld(Thread::Current()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2955 | CHECK(dex_cache.Get() != nullptr) << dex_file.GetLocation(); |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 2956 | CHECK(dex_cache->GetLocation()->Equals(dex_file.GetLocation())) |
| 2957 | << dex_cache->GetLocation()->ToModifiedUtf8() << " " << dex_file.GetLocation(); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 2958 | dex_caches_.push_back(GcRoot<mirror::DexCache>(dex_cache.Get())); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2959 | dex_cache->SetDexFile(&dex_file); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 2960 | if (log_new_dex_caches_roots_) { |
| 2961 | // TODO: This is not safe if we can remove dex caches. |
| 2962 | new_dex_cache_roots_.push_back(dex_caches_.size() - 1); |
| 2963 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2964 | } |
| 2965 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2966 | void ClassLinker::RegisterDexFile(const DexFile& dex_file) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 2967 | Thread* self = Thread::Current(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 2968 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2969 | ReaderMutexLock mu(self, dex_lock_); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 2970 | if (IsDexFileRegisteredLocked(dex_file)) { |
| 2971 | return; |
| 2972 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2973 | } |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 2974 | // Don't alloc while holding the lock, since allocation may need to |
| 2975 | // suspend all threads and another thread may need the dex_lock_ to |
| 2976 | // get to a suspend point. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2977 | StackHandleScope<1> hs(self); |
| 2978 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(AllocDexCache(self, dex_file))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2979 | CHECK(dex_cache.Get() != nullptr) << "Failed to allocate dex cache for " |
| 2980 | << dex_file.GetLocation(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 2981 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2982 | WriterMutexLock mu(self, dex_lock_); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 2983 | if (IsDexFileRegisteredLocked(dex_file)) { |
| 2984 | return; |
| 2985 | } |
| 2986 | RegisterDexFileLocked(dex_file, dex_cache); |
| 2987 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2988 | } |
| 2989 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2990 | void ClassLinker::RegisterDexFile(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2991 | Handle<mirror::DexCache> dex_cache) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2992 | WriterMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2993 | RegisterDexFileLocked(dex_file, dex_cache); |
| 2994 | } |
| 2995 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2996 | mirror::DexCache* ClassLinker::FindDexCache(const DexFile& dex_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2997 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2998 | // Search assuming unique-ness of dex file. |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2999 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3000 | mirror::DexCache* dex_cache = GetDexCache(i); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3001 | if (dex_cache->GetDexFile() == &dex_file) { |
| 3002 | return dex_cache; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3003 | } |
| 3004 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3005 | // Search matching by location name. |
| 3006 | std::string location(dex_file.GetLocation()); |
| 3007 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3008 | mirror::DexCache* dex_cache = GetDexCache(i); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3009 | if (dex_cache->GetDexFile()->GetLocation() == location) { |
| 3010 | return dex_cache; |
| 3011 | } |
| 3012 | } |
| 3013 | // Failure, dump diagnostic and abort. |
| 3014 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3015 | mirror::DexCache* dex_cache = GetDexCache(i); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3016 | LOG(ERROR) << "Registered dex file " << i << " = " << dex_cache->GetDexFile()->GetLocation(); |
| 3017 | } |
| 3018 | LOG(FATAL) << "Failed to find DexCache for DexFile " << location; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 3019 | UNREACHABLE(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3020 | } |
| 3021 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3022 | void ClassLinker::FixupDexCaches(mirror::ArtMethod* resolution_method) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3023 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3024 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3025 | mirror::DexCache* dex_cache = GetDexCache(i); |
| 3026 | dex_cache->Fixup(resolution_method); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3027 | } |
| 3028 | } |
| 3029 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3030 | mirror::Class* ClassLinker::CreatePrimitiveClass(Thread* self, Primitive::Type type) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3031 | mirror::Class* klass = AllocClass(self, mirror::Class::PrimitiveClassSize()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3032 | if (UNLIKELY(klass == nullptr)) { |
| 3033 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3034 | } |
| 3035 | return InitializePrimitiveClass(klass, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3036 | } |
| 3037 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3038 | mirror::Class* ClassLinker::InitializePrimitiveClass(mirror::Class* primitive_class, |
| 3039 | Primitive::Type type) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3040 | CHECK(primitive_class != nullptr); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3041 | // Must hold lock on object when initializing. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3042 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3043 | StackHandleScope<1> hs(self); |
| 3044 | Handle<mirror::Class> h_class(hs.NewHandle(primitive_class)); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3045 | ObjectLock<mirror::Class> lock(self, h_class); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3046 | primitive_class->SetAccessFlags(kAccPublic | kAccFinal | kAccAbstract); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3047 | primitive_class->SetPrimitiveType(type); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3048 | primitive_class->SetStatus(mirror::Class::kStatusInitialized, self); |
| 3049 | const char* descriptor = Primitive::Descriptor(type); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3050 | mirror::Class* existing = InsertClass(descriptor, primitive_class, |
| 3051 | ComputeModifiedUtf8Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3052 | CHECK(existing == nullptr) << "InitPrimitiveClass(" << type << ") failed"; |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3053 | return primitive_class; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 3054 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3055 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3056 | // Create an array class (i.e. the class object for the array, not the |
| 3057 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 3058 | // "[Ljava/lang/String;". |
| 3059 | // |
| 3060 | // If "descriptor" refers to an array of primitives, look up the |
| 3061 | // primitive type's internally-generated class object. |
| 3062 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3063 | // "class_loader" is the class loader of the class that's referring to |
| 3064 | // us. It's used to ensure that we're looking for the element type in |
| 3065 | // the right context. It does NOT become the class loader for the |
| 3066 | // array class; that always comes from the base element class. |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3067 | // |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3068 | // Returns nullptr with an exception raised on failure. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3069 | mirror::Class* ClassLinker::CreateArrayClass(Thread* self, const char* descriptor, size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3070 | Handle<mirror::ClassLoader> class_loader) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3071 | // Identify the underlying component type |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3072 | CHECK_EQ('[', descriptor[0]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3073 | StackHandleScope<2> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3074 | MutableHandle<mirror::Class> component_type(hs.NewHandle(FindClass(self, descriptor + 1, |
| 3075 | class_loader))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3076 | if (component_type.Get() == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3077 | DCHECK(self->IsExceptionPending()); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 3078 | // We need to accept erroneous classes as component types. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3079 | const size_t component_hash = ComputeModifiedUtf8Hash(descriptor + 1); |
| 3080 | component_type.Assign(LookupClass(self, descriptor + 1, component_hash, class_loader.Get())); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 3081 | if (component_type.Get() == nullptr) { |
| 3082 | DCHECK(self->IsExceptionPending()); |
| 3083 | return nullptr; |
| 3084 | } else { |
| 3085 | self->ClearException(); |
| 3086 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3087 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 3088 | if (UNLIKELY(component_type->IsPrimitiveVoid())) { |
| 3089 | ThrowNoClassDefFoundError("Attempt to create array of void primitive type"); |
| 3090 | return nullptr; |
| 3091 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3092 | // See if the component type is already loaded. Array classes are |
| 3093 | // always associated with the class loader of their underlying |
| 3094 | // element type -- an array of Strings goes with the loader for |
| 3095 | // java/lang/String -- so we need to look for it there. (The |
| 3096 | // caller should have checked for the existence of the class |
| 3097 | // before calling here, but they did so with *their* class loader, |
| 3098 | // not the component type's loader.) |
| 3099 | // |
| 3100 | // If we find it, the caller adds "loader" to the class' initiating |
| 3101 | // loader list, which should prevent us from going through this again. |
| 3102 | // |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3103 | // This call is unnecessary if "loader" and "component_type->GetClassLoader()" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3104 | // are the same, because our caller (FindClass) just did the |
| 3105 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 3106 | // because we effectively do this lookup again when we add the new |
| 3107 | // class to the hash table --- necessary because of possible races with |
| 3108 | // other threads.) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3109 | if (class_loader.Get() != component_type->GetClassLoader()) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3110 | mirror::Class* new_class = LookupClass(self, descriptor, hash, component_type->GetClassLoader()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3111 | if (new_class != nullptr) { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3112 | return new_class; |
| 3113 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3114 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3115 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3116 | // Fill out the fields in the Class. |
| 3117 | // |
| 3118 | // It is possible to execute some methods against arrays, because |
| 3119 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 3120 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 3121 | // |
| 3122 | // Array classes are simple enough that we don't need to do a full |
| 3123 | // link step. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3124 | auto new_class = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3125 | if (UNLIKELY(!init_done_)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3126 | // Classes that were hand created, ie not by FindSystemClass |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3127 | if (strcmp(descriptor, "[Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3128 | new_class.Assign(GetClassRoot(kClassArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3129 | } else if (strcmp(descriptor, "[Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3130 | new_class.Assign(GetClassRoot(kObjectArrayClass)); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3131 | } else if (strcmp(descriptor, GetClassRootDescriptor(kJavaLangStringArrayClass)) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3132 | new_class.Assign(GetClassRoot(kJavaLangStringArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3133 | } else if (strcmp(descriptor, |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3134 | GetClassRootDescriptor(kJavaLangReflectArtMethodArrayClass)) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3135 | new_class.Assign(GetClassRoot(kJavaLangReflectArtMethodArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3136 | } else if (strcmp(descriptor, |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3137 | GetClassRootDescriptor(kJavaLangReflectArtFieldArrayClass)) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3138 | new_class.Assign(GetClassRoot(kJavaLangReflectArtFieldArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3139 | } else if (strcmp(descriptor, "[C") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3140 | new_class.Assign(GetClassRoot(kCharArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3141 | } else if (strcmp(descriptor, "[I") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3142 | new_class.Assign(GetClassRoot(kIntArrayClass)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3143 | } |
| 3144 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3145 | if (new_class.Get() == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3146 | new_class.Assign(AllocClass(self, mirror::Array::ClassSize())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3147 | if (new_class.Get() == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3148 | return nullptr; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3149 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3150 | new_class->SetComponentType(component_type.Get()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3151 | } |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3152 | 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] | 3153 | DCHECK(new_class->GetComponentType() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3154 | mirror::Class* java_lang_Object = GetClassRoot(kJavaLangObject); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3155 | new_class->SetSuperClass(java_lang_Object); |
| 3156 | new_class->SetVTable(java_lang_Object->GetVTable()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3157 | new_class->SetPrimitiveType(Primitive::kPrimNot); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3158 | new_class->SetClassLoader(component_type->GetClassLoader()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3159 | new_class->SetStatus(mirror::Class::kStatusLoaded, self); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 3160 | { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3161 | StackHandleScope<mirror::Class::kImtSize> hs2(self, |
| 3162 | Runtime::Current()->GetImtUnimplementedMethod()); |
| 3163 | new_class->PopulateEmbeddedImtAndVTable(&hs2); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 3164 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3165 | new_class->SetStatus(mirror::Class::kStatusInitialized, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3166 | // don't need to set new_class->SetObjectSize(..) |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 3167 | // because Object::SizeOf delegates to Array::SizeOf |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3168 | |
| 3169 | |
| 3170 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 3171 | // interfaces. We need to set that up here, so that stuff like |
| 3172 | // "instanceof" works right. |
| 3173 | // |
| 3174 | // Note: The GC could run during the call to FindSystemClass, |
| 3175 | // so we need to make sure the class object is GC-valid while we're in |
| 3176 | // there. Do this by clearing the interface list so the GC will just |
| 3177 | // think that the entries are null. |
| 3178 | |
| 3179 | |
| 3180 | // Use the single, global copies of "interfaces" and "iftable" |
| 3181 | // (remember not to free them for arrays). |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3182 | { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 3183 | mirror::IfTable* array_iftable = array_iftable_.Read(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3184 | CHECK(array_iftable != nullptr); |
| 3185 | new_class->SetIfTable(array_iftable); |
| 3186 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3187 | |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 3188 | // Inherit access flags from the component type. |
| 3189 | int access_flags = new_class->GetComponentType()->GetAccessFlags(); |
| 3190 | // Lose any implementation detail flags; in particular, arrays aren't finalizable. |
| 3191 | access_flags &= kAccJavaFlagsMask; |
| 3192 | // 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] | 3193 | // and remove "interface". |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 3194 | access_flags |= kAccAbstract | kAccFinal; |
| 3195 | access_flags &= ~kAccInterface; |
| 3196 | |
| 3197 | new_class->SetAccessFlags(access_flags); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3198 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3199 | mirror::Class* existing = InsertClass(descriptor, new_class.Get(), hash); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3200 | if (existing == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3201 | return new_class.Get(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3202 | } |
| 3203 | // Another thread must have loaded the class after we |
| 3204 | // started but before we finished. Abandon what we've |
| 3205 | // done. |
| 3206 | // |
| 3207 | // (Yes, this happens.) |
| 3208 | |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 3209 | return existing; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3210 | } |
| 3211 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3212 | mirror::Class* ClassLinker::FindPrimitiveClass(char type) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3213 | switch (type) { |
| 3214 | case 'B': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3215 | return GetClassRoot(kPrimitiveByte); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3216 | case 'C': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3217 | return GetClassRoot(kPrimitiveChar); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3218 | case 'D': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3219 | return GetClassRoot(kPrimitiveDouble); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3220 | case 'F': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3221 | return GetClassRoot(kPrimitiveFloat); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3222 | case 'I': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3223 | return GetClassRoot(kPrimitiveInt); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3224 | case 'J': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3225 | return GetClassRoot(kPrimitiveLong); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3226 | case 'S': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3227 | return GetClassRoot(kPrimitiveShort); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3228 | case 'Z': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3229 | return GetClassRoot(kPrimitiveBoolean); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3230 | case 'V': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3231 | return GetClassRoot(kPrimitiveVoid); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3232 | default: |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3233 | break; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 3234 | } |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 3235 | std::string printable_type(PrintableChar(type)); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 3236 | ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3237 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3238 | } |
| 3239 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3240 | mirror::Class* ClassLinker::InsertClass(const char* descriptor, mirror::Class* klass, |
| 3241 | size_t hash) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3242 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3243 | mirror::DexCache* dex_cache = klass->GetDexCache(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3244 | std::string source; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3245 | if (dex_cache != nullptr) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3246 | source += " from "; |
| 3247 | source += dex_cache->GetLocation()->ToModifiedUtf8(); |
| 3248 | } |
| 3249 | LOG(INFO) << "Loaded class " << descriptor << source; |
| 3250 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3251 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3252 | mirror::Class* existing = LookupClassFromTableLocked(descriptor, klass->GetClassLoader(), hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3253 | if (existing != nullptr) { |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 3254 | return existing; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 3255 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3256 | if (kIsDebugBuild && !klass->IsTemp() && klass->GetClassLoader() == nullptr && |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3257 | dex_cache_image_class_lookup_required_) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3258 | // Check a class loaded with the system class loader matches one in the image if the class |
| 3259 | // is in the image. |
| 3260 | existing = LookupClassFromImage(descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3261 | if (existing != nullptr) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3262 | CHECK_EQ(klass, existing); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3263 | } |
| 3264 | } |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 3265 | VerifyObject(klass); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3266 | class_table_.InsertWithHash(GcRoot<mirror::Class>(klass), hash); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3267 | if (log_new_class_table_roots_) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3268 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3269 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3270 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3271 | } |
| 3272 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3273 | mirror::Class* ClassLinker::UpdateClass(const char* descriptor, mirror::Class* klass, |
| 3274 | size_t hash) { |
| 3275 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3276 | auto existing_it = class_table_.FindWithHash(std::make_pair(descriptor, klass->GetClassLoader()), |
| 3277 | hash); |
| 3278 | if (existing_it == class_table_.end()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3279 | CHECK(klass->IsProxyClass()); |
| 3280 | return nullptr; |
| 3281 | } |
| 3282 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3283 | mirror::Class* existing = existing_it->Read(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3284 | CHECK_NE(existing, klass) << descriptor; |
| 3285 | CHECK(!existing->IsResolved()) << descriptor; |
| 3286 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusResolving) << descriptor; |
| 3287 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3288 | CHECK(!klass->IsTemp()) << descriptor; |
| 3289 | if (kIsDebugBuild && klass->GetClassLoader() == nullptr && |
| 3290 | dex_cache_image_class_lookup_required_) { |
| 3291 | // Check a class loaded with the system class loader matches one in the image if the class |
| 3292 | // is in the image. |
| 3293 | existing = LookupClassFromImage(descriptor); |
| 3294 | if (existing != nullptr) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3295 | CHECK_EQ(klass, existing) << descriptor; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3296 | } |
| 3297 | } |
| 3298 | VerifyObject(klass); |
| 3299 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3300 | // Update the element in the hash set. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3301 | *existing_it = GcRoot<mirror::Class>(klass); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3302 | if (log_new_class_table_roots_) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3303 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3304 | } |
| 3305 | |
| 3306 | return existing; |
| 3307 | } |
| 3308 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3309 | bool ClassLinker::RemoveClass(const char* descriptor, mirror::ClassLoader* class_loader) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3310 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3311 | auto pair = std::make_pair(descriptor, class_loader); |
| 3312 | auto it = class_table_.Find(pair); |
| 3313 | if (it != class_table_.end()) { |
| 3314 | class_table_.Erase(it); |
| 3315 | return true; |
| 3316 | } |
| 3317 | it = pre_zygote_class_table_.Find(pair); |
| 3318 | if (it != pre_zygote_class_table_.end()) { |
| 3319 | pre_zygote_class_table_.Erase(it); |
| 3320 | return true; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3321 | } |
| 3322 | return false; |
| 3323 | } |
| 3324 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3325 | mirror::Class* ClassLinker::LookupClass(Thread* self, const char* descriptor, size_t hash, |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3326 | mirror::ClassLoader* class_loader) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3327 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3328 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3329 | mirror::Class* result = LookupClassFromTableLocked(descriptor, class_loader, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3330 | if (result != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3331 | return result; |
| 3332 | } |
Sameer Abu Asal | 2c6de22 | 2013-05-02 17:38:59 -0700 | [diff] [blame] | 3333 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3334 | if (class_loader != nullptr || !dex_cache_image_class_lookup_required_) { |
| 3335 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3336 | } else { |
| 3337 | // Lookup failed but need to search dex_caches_. |
| 3338 | mirror::Class* result = LookupClassFromImage(descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3339 | if (result != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3340 | InsertClass(descriptor, result, hash); |
| 3341 | } else { |
| 3342 | // Searching the image dex files/caches failed, we don't want to get into this situation |
| 3343 | // often as map searches are faster, so after kMaxFailedDexCacheLookups move all image |
| 3344 | // classes into the class table. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 3345 | constexpr uint32_t kMaxFailedDexCacheLookups = 1000; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3346 | if (++failed_dex_cache_class_lookups_ > kMaxFailedDexCacheLookups) { |
| 3347 | MoveImageClassesToClassTable(); |
| 3348 | } |
| 3349 | } |
| 3350 | return result; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3351 | } |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 3352 | } |
| 3353 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3354 | mirror::Class* ClassLinker::LookupClassFromTableLocked(const char* descriptor, |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3355 | mirror::ClassLoader* class_loader, |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3356 | size_t hash) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3357 | auto descriptor_pair = std::make_pair(descriptor, class_loader); |
| 3358 | auto it = pre_zygote_class_table_.FindWithHash(descriptor_pair, hash); |
| 3359 | if (it == pre_zygote_class_table_.end()) { |
| 3360 | it = class_table_.FindWithHash(descriptor_pair, hash); |
| 3361 | if (it == class_table_.end()) { |
| 3362 | return nullptr; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 3363 | } |
| 3364 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3365 | return it->Read(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3366 | } |
| 3367 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3368 | static mirror::ObjectArray<mirror::DexCache>* GetImageDexCaches() |
| 3369 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 3370 | gc::space::ImageSpace* image = Runtime::Current()->GetHeap()->GetImageSpace(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3371 | CHECK(image != nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3372 | mirror::Object* root = image->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches); |
| 3373 | return root->AsObjectArray<mirror::DexCache>(); |
| 3374 | } |
| 3375 | |
| 3376 | void ClassLinker::MoveImageClassesToClassTable() { |
| 3377 | Thread* self = Thread::Current(); |
| 3378 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3379 | if (!dex_cache_image_class_lookup_required_) { |
| 3380 | return; // All dex cache classes are already in the class table. |
| 3381 | } |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 3382 | ScopedAssertNoThreadSuspension ants(self, "Moving image classes to class table"); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3383 | mirror::ObjectArray<mirror::DexCache>* dex_caches = GetImageDexCaches(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3384 | std::string temp; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3385 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 3386 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 3387 | mirror::ObjectArray<mirror::Class>* types = dex_cache->GetResolvedTypes(); |
| 3388 | for (int32_t j = 0; j < types->GetLength(); j++) { |
| 3389 | mirror::Class* klass = types->Get(j); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3390 | if (klass != nullptr) { |
| 3391 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3392 | const char* descriptor = klass->GetDescriptor(&temp); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3393 | size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3394 | mirror::Class* existing = LookupClassFromTableLocked(descriptor, nullptr, hash); |
| 3395 | if (existing != nullptr) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3396 | CHECK_EQ(existing, klass) << PrettyClassAndClassLoader(existing) << " != " |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3397 | << PrettyClassAndClassLoader(klass); |
| 3398 | } else { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3399 | class_table_.Insert(GcRoot<mirror::Class>(klass)); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3400 | if (log_new_class_table_roots_) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3401 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3402 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3403 | } |
| 3404 | } |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 3405 | } |
| 3406 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3407 | dex_cache_image_class_lookup_required_ = false; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3408 | } |
| 3409 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3410 | void ClassLinker::MoveClassTableToPreZygote() { |
| 3411 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3412 | DCHECK(pre_zygote_class_table_.Empty()); |
| 3413 | pre_zygote_class_table_ = std::move(class_table_); |
| 3414 | class_table_.Clear(); |
| 3415 | } |
| 3416 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3417 | mirror::Class* ClassLinker::LookupClassFromImage(const char* descriptor) { |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 3418 | ScopedAssertNoThreadSuspension ants(Thread::Current(), "Image class lookup"); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3419 | mirror::ObjectArray<mirror::DexCache>* dex_caches = GetImageDexCaches(); |
| 3420 | for (int32_t i = 0; i < dex_caches->GetLength(); ++i) { |
| 3421 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 3422 | const DexFile* dex_file = dex_cache->GetDexFile(); |
Vladimir Marko | 801a811 | 2014-01-06 14:28:16 +0000 | [diff] [blame] | 3423 | // Try binary searching the string/type index. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3424 | const DexFile::StringId* string_id = dex_file->FindStringId(descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3425 | if (string_id != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3426 | const DexFile::TypeId* type_id = |
| 3427 | dex_file->FindTypeId(dex_file->GetIndexForStringId(*string_id)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3428 | if (type_id != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3429 | uint16_t type_idx = dex_file->GetIndexForTypeId(*type_id); |
| 3430 | mirror::Class* klass = dex_cache->GetResolvedType(type_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3431 | if (klass != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3432 | return klass; |
| 3433 | } |
| 3434 | } |
| 3435 | } |
| 3436 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3437 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3438 | } |
| 3439 | |
| 3440 | void ClassLinker::LookupClasses(const char* descriptor, std::vector<mirror::Class*>& result) { |
| 3441 | result.clear(); |
| 3442 | if (dex_cache_image_class_lookup_required_) { |
| 3443 | MoveImageClassesToClassTable(); |
| 3444 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3445 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3446 | while (true) { |
| 3447 | auto it = class_table_.Find(descriptor); |
| 3448 | if (it == class_table_.end()) { |
| 3449 | break; |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 3450 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3451 | result.push_back(it->Read()); |
| 3452 | class_table_.Erase(it); |
| 3453 | } |
| 3454 | for (mirror::Class* k : result) { |
| 3455 | class_table_.Insert(GcRoot<mirror::Class>(k)); |
| 3456 | } |
| 3457 | size_t pre_zygote_start = result.size(); |
| 3458 | // Now handle the pre zygote table. |
| 3459 | // Note: This dirties the pre-zygote table but shouldn't be an issue since LookupClasses is only |
| 3460 | // called from the debugger. |
| 3461 | while (true) { |
| 3462 | auto it = pre_zygote_class_table_.Find(descriptor); |
| 3463 | if (it == pre_zygote_class_table_.end()) { |
| 3464 | break; |
| 3465 | } |
| 3466 | result.push_back(it->Read()); |
| 3467 | pre_zygote_class_table_.Erase(it); |
| 3468 | } |
| 3469 | for (size_t i = pre_zygote_start; i < result.size(); ++i) { |
| 3470 | pre_zygote_class_table_.Insert(GcRoot<mirror::Class>(result[i])); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 3471 | } |
| 3472 | } |
| 3473 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3474 | void ClassLinker::VerifyClass(Thread* self, Handle<mirror::Class> klass) { |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 3475 | // TODO: assert that the monitor on the Class is held |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3476 | ObjectLock<mirror::Class> lock(self, klass); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 3477 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3478 | // Don't attempt to re-verify if already sufficiently verified. |
Andreas Gampe | bb0c7f6 | 2014-09-11 10:59:33 -0700 | [diff] [blame] | 3479 | if (klass->IsVerified()) { |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 3480 | EnsurePreverifiedMethods(klass); |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3481 | return; |
| 3482 | } |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 3483 | if (klass->IsCompileTimeVerified() && Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | bb0c7f6 | 2014-09-11 10:59:33 -0700 | [diff] [blame] | 3484 | return; |
| 3485 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3486 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3487 | // The class might already be erroneous, for example at compile time if we attempted to verify |
| 3488 | // this class as a parent to another. |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 3489 | if (klass->IsErroneous()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3490 | ThrowEarlierClassFailure(klass.Get()); |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 3491 | return; |
| 3492 | } |
| 3493 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3494 | if (klass->GetStatus() == mirror::Class::kStatusResolved) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3495 | klass->SetStatus(mirror::Class::kStatusVerifying, self); |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3496 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3497 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3498 | << PrettyClass(klass.Get()); |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 3499 | CHECK(!Runtime::Current()->IsAotCompiler()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3500 | klass->SetStatus(mirror::Class::kStatusVerifyingAtRuntime, self); |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3501 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3502 | |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 3503 | // Skip verification if disabled. |
| 3504 | if (!Runtime::Current()->IsVerificationEnabled()) { |
| 3505 | klass->SetStatus(mirror::Class::kStatusVerified, self); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 3506 | EnsurePreverifiedMethods(klass); |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 3507 | return; |
| 3508 | } |
| 3509 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3510 | // Verify super class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3511 | StackHandleScope<2> hs(self); |
| 3512 | Handle<mirror::Class> super(hs.NewHandle(klass->GetSuperClass())); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3513 | if (super.Get() != nullptr) { |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3514 | // Acquire lock to prevent races on verifying the super class. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3515 | ObjectLock<mirror::Class> super_lock(self, super); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3516 | |
| 3517 | if (!super->IsVerified() && !super->IsErroneous()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3518 | VerifyClass(self, super); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3519 | } |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 3520 | if (!super->IsCompileTimeVerified()) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3521 | std::string error_msg( |
| 3522 | StringPrintf("Rejecting class %s that attempts to sub-class erroneous class %s", |
| 3523 | PrettyDescriptor(klass.Get()).c_str(), |
| 3524 | PrettyDescriptor(super.Get()).c_str())); |
Andreas Gampe | e4301ff | 2015-02-17 19:25:29 -0800 | [diff] [blame] | 3525 | LOG(WARNING) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3526 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException(nullptr))); |
| 3527 | if (cause.Get() != nullptr) { |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3528 | self->ClearException(); |
| 3529 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3530 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
| 3531 | if (cause.Get() != nullptr) { |
| 3532 | self->GetException(nullptr)->SetCause(cause.Get()); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3533 | } |
Jeff Hao | 22cb09b | 2013-12-12 14:29:15 -0800 | [diff] [blame] | 3534 | ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex()); |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 3535 | if (Runtime::Current()->IsAotCompiler()) { |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 3536 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
| 3537 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3538 | klass->SetStatus(mirror::Class::kStatusError, self); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3539 | return; |
| 3540 | } |
| 3541 | } |
| 3542 | |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 3543 | // 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] | 3544 | const DexFile& dex_file = *klass->GetDexCache()->GetDexFile(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3545 | mirror::Class::Status oat_file_class_status(mirror::Class::kStatusNotReady); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3546 | bool preverified = VerifyClassUsingOatFile(dex_file, klass.Get(), oat_file_class_status); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3547 | if (oat_file_class_status == mirror::Class::kStatusError) { |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 3548 | VLOG(class_linker) << "Skipping runtime verification of erroneous class " |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3549 | << PrettyDescriptor(klass.Get()) << " in " |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 3550 | << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3551 | ThrowVerifyError(klass.Get(), "Rejecting class %s because it failed compile-time verification", |
| 3552 | PrettyDescriptor(klass.Get()).c_str()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3553 | klass->SetStatus(mirror::Class::kStatusError, self); |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 3554 | return; |
| 3555 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3556 | verifier::MethodVerifier::FailureKind verifier_failure = verifier::MethodVerifier::kNoFailure; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3557 | std::string error_msg; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 3558 | if (!preverified) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3559 | verifier_failure = verifier::MethodVerifier::VerifyClass(self, klass.Get(), |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 3560 | Runtime::Current()->IsAotCompiler(), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3561 | &error_msg); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 3562 | } |
| 3563 | if (preverified || verifier_failure != verifier::MethodVerifier::kHardFailure) { |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 3564 | if (!preverified && verifier_failure != verifier::MethodVerifier::kNoFailure) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3565 | VLOG(class_linker) << "Soft verification failure in class " << PrettyDescriptor(klass.Get()) |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 3566 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 3567 | << " because: " << error_msg; |
| 3568 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3569 | self->AssertNoPendingException(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3570 | // Make sure all classes referenced by catch blocks are resolved. |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3571 | ResolveClassExceptionHandlerTypes(dex_file, klass); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3572 | if (verifier_failure == verifier::MethodVerifier::kNoFailure) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3573 | // Even though there were no verifier failures we need to respect whether the super-class |
| 3574 | // was verified or requiring runtime reverification. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3575 | if (super.Get() == nullptr || super->IsVerified()) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3576 | klass->SetStatus(mirror::Class::kStatusVerified, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3577 | } else { |
| 3578 | CHECK_EQ(super->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3579 | klass->SetStatus(mirror::Class::kStatusRetryVerificationAtRuntime, self); |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 3580 | // Pretend a soft failure occured so that we don't consider the class verified below. |
| 3581 | verifier_failure = verifier::MethodVerifier::kSoftFailure; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3582 | } |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3583 | } else { |
| 3584 | CHECK_EQ(verifier_failure, verifier::MethodVerifier::kSoftFailure); |
| 3585 | // Soft failures at compile time should be retried at runtime. Soft |
| 3586 | // failures at runtime will be handled by slow paths in the generated |
| 3587 | // code. Set status accordingly. |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 3588 | if (Runtime::Current()->IsAotCompiler()) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3589 | klass->SetStatus(mirror::Class::kStatusRetryVerificationAtRuntime, self); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3590 | } else { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3591 | klass->SetStatus(mirror::Class::kStatusVerified, self); |
Andreas Gampe | bb0c7f6 | 2014-09-11 10:59:33 -0700 | [diff] [blame] | 3592 | // As this is a fake verified status, make sure the methods are _not_ marked preverified |
| 3593 | // later. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3594 | klass->SetPreverified(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3595 | } |
| 3596 | } |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 3597 | } else { |
Andreas Gampe | e4301ff | 2015-02-17 19:25:29 -0800 | [diff] [blame] | 3598 | LOG(WARNING) << "Verification failed on class " << PrettyDescriptor(klass.Get()) |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3599 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 3600 | << " because: " << error_msg; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3601 | self->AssertNoPendingException(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3602 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3603 | klass->SetStatus(mirror::Class::kStatusError, self); |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 3604 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3605 | if (preverified || verifier_failure == verifier::MethodVerifier::kNoFailure) { |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 3606 | // 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] | 3607 | // Let the interpreter know it by setting the kAccPreverified flag onto each |
| 3608 | // method. |
| 3609 | // Note: we're going here during compilation and at runtime. When we set the |
| 3610 | // kAccPreverified flag when compiling image classes, the flag is recorded |
| 3611 | // in the image and is set when loading the image. |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 3612 | EnsurePreverifiedMethods(klass); |
| 3613 | } |
| 3614 | } |
| 3615 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3616 | void ClassLinker::EnsurePreverifiedMethods(Handle<mirror::Class> klass) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3617 | if (!klass->IsPreverified()) { |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3618 | klass->SetPreverifiedFlagOnAllMethods(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3619 | klass->SetPreverified(); |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3620 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3621 | } |
| 3622 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3623 | bool ClassLinker::VerifyClassUsingOatFile(const DexFile& dex_file, mirror::Class* klass, |
| 3624 | mirror::Class::Status& oat_file_class_status) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3625 | // If we're compiling, we can only verify the class using the oat file if |
| 3626 | // we are not compiling the image or if the class we're verifying is not part of |
| 3627 | // the app. In other words, we will only check for preverification of bootclasspath |
| 3628 | // classes. |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 3629 | if (Runtime::Current()->IsAotCompiler()) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3630 | // Are we compiling the bootclasspath? |
| 3631 | if (!Runtime::Current()->UseCompileTimeClassPath()) { |
| 3632 | return false; |
| 3633 | } |
| 3634 | // We are compiling an app (not the image). |
| 3635 | |
| 3636 | // Is this an app class? (I.e. not a bootclasspath class) |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3637 | if (klass->GetClassLoader() != nullptr) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3638 | return false; |
| 3639 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3640 | } |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3641 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 3642 | const OatFile::OatDexFile* oat_dex_file = FindOpenedOatDexFileForDexFile(dex_file); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3643 | // In case we run without an image there won't be a backing oat file. |
| 3644 | if (oat_dex_file == nullptr) { |
Anwar Ghuloum | ad256bb | 2013-07-18 14:58:55 -0700 | [diff] [blame] | 3645 | return false; |
| 3646 | } |
| 3647 | |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 3648 | // We may be running with a preopted oat file but without image. In this case, |
| 3649 | // we don't skip verification of preverified classes to ensure we initialize |
| 3650 | // dex caches with all types resolved during verification. |
| 3651 | // We need to trust image classes, as these might be coming out of a pre-opted, quickened boot |
| 3652 | // image (that we just failed loading), and the verifier can't be run on quickened opcodes when |
| 3653 | // the runtime isn't started. On the other hand, app classes can be re-verified even if they are |
| 3654 | // already pre-opted, as then the runtime is started. |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 3655 | if (!Runtime::Current()->IsAotCompiler() && |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 3656 | !Runtime::Current()->GetHeap()->HasImageSpace() && |
| 3657 | klass->GetClassLoader() != nullptr) { |
| 3658 | return false; |
| 3659 | } |
| 3660 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3661 | uint16_t class_def_index = klass->GetDexClassDefIndex(); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 3662 | oat_file_class_status = oat_dex_file->GetOatClass(class_def_index).GetStatus(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3663 | if (oat_file_class_status == mirror::Class::kStatusVerified || |
| 3664 | oat_file_class_status == mirror::Class::kStatusInitialized) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3665 | return true; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3666 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3667 | if (oat_file_class_status == mirror::Class::kStatusRetryVerificationAtRuntime) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 3668 | // Compile time verification failed with a soft error. Compile time verification can fail |
| 3669 | // because we have incomplete type information. Consider the following: |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3670 | // class ... { |
| 3671 | // Foo x; |
| 3672 | // .... () { |
| 3673 | // if (...) { |
| 3674 | // v1 gets assigned a type of resolved class Foo |
| 3675 | // } else { |
| 3676 | // v1 gets assigned a type of unresolved class Bar |
| 3677 | // } |
| 3678 | // iput x = v1 |
| 3679 | // } } |
| 3680 | // when we merge v1 following the if-the-else it results in Conflict |
| 3681 | // (see verifier::RegType::Merge) as we can't know the type of Bar and we could possibly be |
| 3682 | // allowing an unsafe assignment to the field x in the iput (javac may have compiled this as |
| 3683 | // it knew Bar was a sub-class of Foo, but for us this may have been moved into a separate apk |
| 3684 | // at compile time). |
| 3685 | return false; |
| 3686 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3687 | if (oat_file_class_status == mirror::Class::kStatusError) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 3688 | // Compile time verification failed with a hard error. This is caused by invalid instructions |
| 3689 | // in the class. These errors are unrecoverable. |
| 3690 | return false; |
| 3691 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3692 | if (oat_file_class_status == mirror::Class::kStatusNotReady) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3693 | // Status is uninitialized if we couldn't determine the status at compile time, for example, |
| 3694 | // not loading the class. |
| 3695 | // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy |
| 3696 | // isn't a problem and this case shouldn't occur |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3697 | return false; |
| 3698 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3699 | std::string temp; |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 3700 | LOG(FATAL) << "Unexpected class status: " << oat_file_class_status |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3701 | << " " << dex_file.GetLocation() << " " << PrettyClass(klass) << " " |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3702 | << klass->GetDescriptor(&temp); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 3703 | UNREACHABLE(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3704 | } |
| 3705 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3706 | void ClassLinker::ResolveClassExceptionHandlerTypes(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3707 | Handle<mirror::Class> klass) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3708 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
| 3709 | ResolveMethodExceptionHandlerTypes(dex_file, klass->GetDirectMethod(i)); |
| 3710 | } |
| 3711 | for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { |
| 3712 | ResolveMethodExceptionHandlerTypes(dex_file, klass->GetVirtualMethod(i)); |
| 3713 | } |
| 3714 | } |
| 3715 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3716 | void ClassLinker::ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3717 | mirror::ArtMethod* method) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3718 | // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod. |
| 3719 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3720 | if (code_item == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3721 | return; // native or abstract method |
| 3722 | } |
| 3723 | if (code_item->tries_size_ == 0) { |
| 3724 | return; // nothing to process |
| 3725 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 3726 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item, 0); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3727 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 3728 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 3729 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| 3730 | CatchHandlerIterator iterator(handlers_ptr); |
| 3731 | for (; iterator.HasNext(); iterator.Next()) { |
| 3732 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 3733 | // unresolved exception types will be ignored by exception delivery |
| 3734 | if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3735 | mirror::Class* exception_type = linker->ResolveType(iterator.GetHandlerTypeIndex(), method); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3736 | if (exception_type == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3737 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 3738 | Thread::Current()->ClearException(); |
| 3739 | } |
| 3740 | } |
| 3741 | } |
| 3742 | handlers_ptr = iterator.EndDataPointer(); |
| 3743 | } |
| 3744 | } |
| 3745 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3746 | static void CheckProxyConstructor(mirror::ArtMethod* constructor); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3747 | static void CheckProxyMethod(Handle<mirror::ArtMethod> method, |
| 3748 | Handle<mirror::ArtMethod> prototype); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3749 | |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 3750 | mirror::Class* ClassLinker::CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, jstring name, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3751 | jobjectArray interfaces, jobject loader, |
| 3752 | jobjectArray methods, jobjectArray throws) { |
| 3753 | Thread* self = soa.Self(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3754 | StackHandleScope<8> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3755 | MutableHandle<mirror::Class> klass(hs.NewHandle( |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3756 | AllocClass(self, GetClassRoot(kJavaLangClass), sizeof(mirror::Class)))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3757 | if (klass.Get() == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3758 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3759 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3760 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3761 | DCHECK(klass->GetClass() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3762 | klass->SetObjectSize(sizeof(mirror::Proxy)); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 3763 | // Set the class access flags incl. preverified, so we do not try to set the flag on the methods. |
| 3764 | klass->SetAccessFlags(kAccClassIsProxy | kAccPublic | kAccFinal | kAccPreverified); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3765 | klass->SetClassLoader(soa.Decode<mirror::ClassLoader*>(loader)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3766 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3767 | klass->SetName(soa.Decode<mirror::String*>(name)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3768 | mirror::Class* proxy_class = GetClassRoot(kJavaLangReflectProxy); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3769 | klass->SetDexCache(proxy_class->GetDexCache()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3770 | klass->SetStatus(mirror::Class::kStatusIdx, self); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3771 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3772 | // Instance fields are inherited, but we add a couple of static fields... |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3773 | { |
| 3774 | mirror::ObjectArray<mirror::ArtField>* sfields = AllocArtFieldArray(self, 2); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3775 | if (UNLIKELY(sfields == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3776 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3777 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3778 | } |
| 3779 | klass->SetSFields(sfields); |
| 3780 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3781 | // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by |
| 3782 | // our proxy, so Class.getInterfaces doesn't return the flattened set. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3783 | Handle<mirror::ArtField> interfaces_sfield(hs.NewHandle(AllocArtField(self))); |
| 3784 | if (UNLIKELY(interfaces_sfield.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3785 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3786 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3787 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3788 | klass->SetStaticField(0, interfaces_sfield.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3789 | interfaces_sfield->SetDexFieldIndex(0); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3790 | interfaces_sfield->SetDeclaringClass(klass.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3791 | interfaces_sfield->SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
| 3792 | // 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] | 3793 | Handle<mirror::ArtField> throws_sfield(hs.NewHandle(AllocArtField(self))); |
| 3794 | if (UNLIKELY(throws_sfield.Get() == 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 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3798 | klass->SetStaticField(1, throws_sfield.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3799 | throws_sfield->SetDexFieldIndex(1); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3800 | throws_sfield->SetDeclaringClass(klass.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3801 | throws_sfield->SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3802 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3803 | // Proxies have 1 direct method, the constructor |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3804 | { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3805 | mirror::ObjectArray<mirror::ArtMethod>* directs = AllocArtMethodArray(self, 1); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3806 | if (UNLIKELY(directs == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3807 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3808 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3809 | } |
| 3810 | klass->SetDirectMethods(directs); |
| 3811 | mirror::ArtMethod* constructor = CreateProxyConstructor(self, klass, proxy_class); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3812 | if (UNLIKELY(constructor == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3813 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3814 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3815 | } |
| 3816 | klass->SetDirectMethod(0, constructor); |
| 3817 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3818 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3819 | // Create virtual method using specified prototypes. |
| 3820 | size_t num_virtual_methods = |
| 3821 | soa.Decode<mirror::ObjectArray<mirror::ArtMethod>*>(methods)->GetLength(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3822 | { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3823 | mirror::ObjectArray<mirror::ArtMethod>* virtuals = AllocArtMethodArray(self, |
| 3824 | num_virtual_methods); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3825 | if (UNLIKELY(virtuals == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3826 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3827 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3828 | } |
| 3829 | klass->SetVirtualMethods(virtuals); |
| 3830 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3831 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3832 | StackHandleScope<1> hs2(self); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3833 | mirror::ObjectArray<mirror::ArtMethod>* decoded_methods = |
| 3834 | soa.Decode<mirror::ObjectArray<mirror::ArtMethod>*>(methods); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3835 | Handle<mirror::ArtMethod> prototype(hs2.NewHandle(decoded_methods->Get(i))); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3836 | mirror::ArtMethod* clone = CreateProxyMethod(self, klass, prototype); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3837 | if (UNLIKELY(clone == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3838 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3839 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3840 | } |
| 3841 | klass->SetVirtualMethod(i, clone); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3842 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3843 | |
| 3844 | klass->SetSuperClass(proxy_class); // The super class is java.lang.reflect.Proxy |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3845 | klass->SetStatus(mirror::Class::kStatusLoaded, self); // Now effectively in the loaded state. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3846 | self->AssertNoPendingException(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3847 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3848 | std::string descriptor(GetDescriptorForProxy(klass.Get())); |
| 3849 | mirror::Class* new_class = nullptr; |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 3850 | { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3851 | // Must hold lock on object when resolved. |
| 3852 | ObjectLock<mirror::Class> resolution_lock(self, klass); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3853 | // Link the fields and virtual methods, creating vtable and iftables |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3854 | Handle<mirror::ObjectArray<mirror::Class> > h_interfaces( |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3855 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces))); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3856 | if (!LinkClass(self, descriptor.c_str(), klass, h_interfaces, &new_class)) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3857 | klass->SetStatus(mirror::Class::kStatusError, self); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3858 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3859 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3860 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3861 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3862 | CHECK(klass->IsRetired()); |
| 3863 | CHECK_NE(klass.Get(), new_class); |
| 3864 | klass.Assign(new_class); |
| 3865 | |
| 3866 | CHECK_EQ(interfaces_sfield->GetDeclaringClass(), new_class); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3867 | interfaces_sfield->SetObject<false>(klass.Get(), |
| 3868 | soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3869 | CHECK_EQ(throws_sfield->GetDeclaringClass(), new_class); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3870 | throws_sfield->SetObject<false>(klass.Get(), |
| 3871 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class> >*>(throws)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3872 | |
| 3873 | { |
| 3874 | // Lock on klass is released. Lock new class object. |
| 3875 | ObjectLock<mirror::Class> initialization_lock(self, klass); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3876 | klass->SetStatus(mirror::Class::kStatusInitialized, self); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 3877 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3878 | |
| 3879 | // sanity checks |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 3880 | if (kIsDebugBuild) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3881 | CHECK(klass->GetIFields() == nullptr); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3882 | CheckProxyConstructor(klass->GetDirectMethod(0)); |
| 3883 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3884 | StackHandleScope<2> hs2(self); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3885 | mirror::ObjectArray<mirror::ArtMethod>* decoded_methods = |
| 3886 | soa.Decode<mirror::ObjectArray<mirror::ArtMethod>*>(methods); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3887 | Handle<mirror::ArtMethod> prototype(hs2.NewHandle(decoded_methods->Get(i))); |
| 3888 | Handle<mirror::ArtMethod> virtual_method(hs2.NewHandle(klass->GetVirtualMethod(i))); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3889 | CheckProxyMethod(virtual_method, prototype); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3890 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3891 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3892 | mirror::String* decoded_name = soa.Decode<mirror::String*>(name); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3893 | std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3894 | decoded_name->ToModifiedUtf8().c_str())); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3895 | CHECK_EQ(PrettyField(klass->GetStaticField(0)), interfaces_field_name); |
| 3896 | |
| 3897 | std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3898 | decoded_name->ToModifiedUtf8().c_str())); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3899 | CHECK_EQ(PrettyField(klass->GetStaticField(1)), throws_field_name); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3900 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3901 | CHECK_EQ(klass.Get()->GetInterfaces(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3902 | soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3903 | CHECK_EQ(klass.Get()->GetThrows(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3904 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>*>(throws)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3905 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3906 | mirror::Class* existing = InsertClass(descriptor.c_str(), klass.Get(), |
| 3907 | ComputeModifiedUtf8Hash(descriptor.c_str())); |
Mathieu Chartier | 31b9d66 | 2013-10-14 11:53:12 -0700 | [diff] [blame] | 3908 | CHECK(existing == nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3909 | return klass.Get(); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3910 | } |
| 3911 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3912 | std::string ClassLinker::GetDescriptorForProxy(mirror::Class* proxy_class) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3913 | DCHECK(proxy_class->IsProxyClass()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3914 | mirror::String* name = proxy_class->GetName(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3915 | DCHECK(name != nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3916 | return DotToDescriptor(name->ToModifiedUtf8().c_str()); |
| 3917 | } |
| 3918 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3919 | mirror::ArtMethod* ClassLinker::FindMethodForProxy(mirror::Class* proxy_class, |
| 3920 | mirror::ArtMethod* proxy_method) { |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3921 | DCHECK(proxy_class->IsProxyClass()); |
| 3922 | DCHECK(proxy_method->IsProxyMethod()); |
| 3923 | // Locate the dex cache of the original interface/Object |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 3924 | mirror::DexCache* dex_cache = nullptr; |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3925 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3926 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3927 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3928 | mirror::DexCache* a_dex_cache = GetDexCache(i); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 3929 | if (proxy_method->HasSameDexCacheResolvedTypes(a_dex_cache->GetResolvedTypes())) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3930 | dex_cache = a_dex_cache; |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3931 | break; |
| 3932 | } |
| 3933 | } |
| 3934 | } |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 3935 | CHECK(dex_cache != nullptr); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3936 | uint32_t method_idx = proxy_method->GetDexMethodIndex(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3937 | mirror::ArtMethod* resolved_method = dex_cache->GetResolvedMethod(method_idx); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 3938 | CHECK(resolved_method != nullptr); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3939 | return resolved_method; |
| 3940 | } |
| 3941 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3942 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3943 | mirror::ArtMethod* ClassLinker::CreateProxyConstructor(Thread* self, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3944 | Handle<mirror::Class> klass, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3945 | mirror::Class* proxy_class) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3946 | // Create constructor for Proxy that must initialize h |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3947 | mirror::ObjectArray<mirror::ArtMethod>* proxy_direct_methods = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3948 | proxy_class->GetDirectMethods(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3949 | CHECK_EQ(proxy_direct_methods->GetLength(), 16); |
| 3950 | mirror::ArtMethod* proxy_constructor = proxy_direct_methods->Get(2); |
Sebastien Hertz | ae94e35 | 2014-08-27 15:32:56 +0200 | [diff] [blame] | 3951 | // Ensure constructor is in dex cache so that we can use the dex cache to look up the overridden |
| 3952 | // constructor method. |
| 3953 | proxy_class->GetDexCache()->SetResolvedMethod(proxy_constructor->GetDexMethodIndex(), |
| 3954 | proxy_constructor); |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 3955 | // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its |
| 3956 | // code_ too) |
Jeff Hao | f0a3f09 | 2014-07-24 16:26:09 -0700 | [diff] [blame] | 3957 | mirror::ArtMethod* constructor = down_cast<mirror::ArtMethod*>(proxy_constructor->Clone(self)); |
| 3958 | if (constructor == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3959 | CHECK(self->IsExceptionPending()); // OOME. |
Jeff Hao | f0a3f09 | 2014-07-24 16:26:09 -0700 | [diff] [blame] | 3960 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3961 | } |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3962 | // Make this constructor public and fix the class to be our Proxy version |
| 3963 | constructor->SetAccessFlags((constructor->GetAccessFlags() & ~kAccProtected) | kAccPublic); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3964 | constructor->SetDeclaringClass(klass.Get()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3965 | return constructor; |
| 3966 | } |
| 3967 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3968 | static void CheckProxyConstructor(mirror::ArtMethod* constructor) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 3969 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3970 | CHECK(constructor->IsConstructor()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3971 | CHECK_STREQ(constructor->GetName(), "<init>"); |
| 3972 | CHECK_STREQ(constructor->GetSignature().ToString().c_str(), |
| 3973 | "(Ljava/lang/reflect/InvocationHandler;)V"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3974 | DCHECK(constructor->IsPublic()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3975 | } |
| 3976 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3977 | mirror::ArtMethod* ClassLinker::CreateProxyMethod(Thread* self, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3978 | Handle<mirror::Class> klass, |
| 3979 | Handle<mirror::ArtMethod> prototype) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3980 | // Ensure prototype is in dex cache so that we can use the dex cache to look up the overridden |
| 3981 | // prototype method |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3982 | prototype->GetDeclaringClass()->GetDexCache()->SetResolvedMethod(prototype->GetDexMethodIndex(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3983 | prototype.Get()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3984 | // 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] | 3985 | // as necessary |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3986 | mirror::ArtMethod* method = down_cast<mirror::ArtMethod*>(prototype->Clone(self)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3987 | if (UNLIKELY(method == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3988 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3989 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3990 | } |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3991 | |
| 3992 | // Set class to be the concrete proxy class and clear the abstract flag, modify exceptions to |
| 3993 | // the intersection of throw exceptions as defined in Proxy |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3994 | method->SetDeclaringClass(klass.Get()); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3995 | method->SetAccessFlags((method->GetAccessFlags() & ~kAccAbstract) | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3996 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3997 | // At runtime the method looks like a reference and argument saving method, clone the code |
| 3998 | // related parameters from this method. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3999 | method->SetEntryPointFromQuickCompiledCode(GetQuickProxyInvokeHandler()); |
Dragos Sbirlea | 08bf196 | 2013-08-12 08:53:04 -0700 | [diff] [blame] | 4000 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 4001 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4002 | return method; |
| 4003 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4004 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4005 | static void CheckProxyMethod(Handle<mirror::ArtMethod> method, |
| 4006 | Handle<mirror::ArtMethod> prototype) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 4007 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4008 | // Basic sanity |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4009 | CHECK(!prototype->IsFinal()); |
| 4010 | CHECK(method->IsFinal()); |
| 4011 | CHECK(!method->IsAbstract()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 4012 | |
| 4013 | // The proxy method doesn't have its own dex cache or dex file and so it steals those of its |
| 4014 | // 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] | 4015 | CHECK(prototype->HasSameDexCacheResolvedMethods(method.Get())); |
| 4016 | CHECK(prototype->HasSameDexCacheResolvedTypes(method.Get())); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 4017 | CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex()); |
| 4018 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4019 | CHECK_STREQ(method->GetName(), prototype->GetName()); |
| 4020 | CHECK_STREQ(method->GetShorty(), prototype->GetShorty()); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4021 | // More complex sanity - via dex cache |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 4022 | CHECK_EQ(method->GetInterfaceMethodIfProxy()->GetReturnType(), prototype->GetReturnType()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4023 | } |
| 4024 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4025 | static bool CanWeInitializeClass(mirror::Class* klass, bool can_init_statics, |
| 4026 | bool can_init_parents) |
| 4027 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 4028 | if (can_init_statics && can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4029 | return true; |
| 4030 | } |
| 4031 | if (!can_init_statics) { |
| 4032 | // Check if there's a class initializer. |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 4033 | mirror::ArtMethod* clinit = klass->FindClassInitializer(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4034 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4035 | return false; |
| 4036 | } |
| 4037 | // Check if there are encoded static values needing initialization. |
| 4038 | if (klass->NumStaticFields() != 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4039 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4040 | DCHECK(dex_class_def != nullptr); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4041 | if (dex_class_def->static_values_off_ != 0) { |
| 4042 | return false; |
| 4043 | } |
| 4044 | } |
| 4045 | } |
| 4046 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
| 4047 | mirror::Class* super_class = klass->GetSuperClass(); |
| 4048 | if (!can_init_parents && !super_class->IsInitialized()) { |
| 4049 | return false; |
| 4050 | } else { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 4051 | if (!CanWeInitializeClass(super_class, can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4052 | return false; |
| 4053 | } |
| 4054 | } |
| 4055 | } |
| 4056 | return true; |
| 4057 | } |
| 4058 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4059 | bool ClassLinker::InitializeClass(Thread* self, Handle<mirror::Class> klass, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4060 | bool can_init_statics, bool can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4061 | // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol |
| 4062 | |
| 4063 | // Are we already initialized and therefore done? |
| 4064 | // Note: we differ from the JLS here as we don't do this under the lock, this is benign as |
| 4065 | // an initialized class will never change its state. |
| 4066 | if (klass->IsInitialized()) { |
| 4067 | return true; |
| 4068 | } |
| 4069 | |
| 4070 | // 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] | 4071 | if (!CanWeInitializeClass(klass.Get(), can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4072 | return false; |
| 4073 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4074 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4075 | self->AllowThreadSuspension(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4076 | uint64_t t0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4077 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4078 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4079 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4080 | // Re-check under the lock in case another thread initialized ahead of us. |
| 4081 | if (klass->IsInitialized()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4082 | return true; |
| 4083 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4084 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4085 | // 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] | 4086 | if (klass->IsErroneous()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4087 | ThrowEarlierClassFailure(klass.Get()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4088 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4089 | return false; |
| 4090 | } |
| 4091 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4092 | CHECK(klass->IsResolved()) << PrettyClass(klass.Get()) << ": state=" << klass->GetStatus(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4093 | |
| 4094 | if (!klass->IsVerified()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4095 | VerifyClass(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4096 | if (!klass->IsVerified()) { |
| 4097 | // We failed to verify, expect either the klass to be erroneous or verification failed at |
| 4098 | // compile time. |
| 4099 | if (klass->IsErroneous()) { |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 4100 | CHECK(self->IsExceptionPending()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4101 | VlogClassInitializationFailure(klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4102 | } else { |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 4103 | CHECK(Runtime::Current()->IsAotCompiler()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4104 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 4105 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4106 | return false; |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4107 | } else { |
| 4108 | self->AssertNoPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4109 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4110 | } |
| 4111 | |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4112 | // If the class is kStatusInitializing, either this thread is |
| 4113 | // initializing higher up the stack or another thread has beat us |
| 4114 | // to initializing and we need to wait. Either way, this |
| 4115 | // invocation of InitializeClass will not be responsible for |
| 4116 | // running <clinit> and will return. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4117 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4118 | // Could have got an exception during verification. |
| 4119 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4120 | VlogClassInitializationFailure(klass); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4121 | return false; |
| 4122 | } |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 4123 | // We caught somebody else in the act; was it us? |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 4124 | if (klass->GetClinitThreadId() == self->GetTid()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4125 | // Yes. That's fine. Return so we can continue initializing. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4126 | return true; |
| 4127 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4128 | // No. That's fine. Wait for another thread to finish initializing. |
| 4129 | return WaitForInitializeClass(klass, self, lock); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4130 | } |
| 4131 | |
| 4132 | if (!ValidateSuperClassDescriptors(klass)) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4133 | klass->SetStatus(mirror::Class::kStatusError, self); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4134 | return false; |
| 4135 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4136 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4137 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4138 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusVerified) << PrettyClass(klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4139 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4140 | // From here out other threads may observe that we're initializing and so changes of state |
| 4141 | // require the a notification. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 4142 | klass->SetClinitThreadId(self->GetTid()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4143 | klass->SetStatus(mirror::Class::kStatusInitializing, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4144 | |
| 4145 | t0 = NanoTime(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4146 | } |
| 4147 | |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 4148 | // Initialize super classes, must be done while initializing for the JLS. |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4149 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
| 4150 | mirror::Class* super_class = klass->GetSuperClass(); |
| 4151 | if (!super_class->IsInitialized()) { |
| 4152 | CHECK(!super_class->IsInterface()); |
| 4153 | CHECK(can_init_parents); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4154 | StackHandleScope<1> hs(self); |
| 4155 | Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4156 | bool super_initialized = InitializeClass(self, handle_scope_super, can_init_statics, true); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4157 | if (!super_initialized) { |
| 4158 | // The super class was verified ahead of entering initializing, we should only be here if |
| 4159 | // the super class became erroneous due to initialization. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4160 | CHECK(handle_scope_super->IsErroneous() && self->IsExceptionPending()) |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4161 | << "Super class initialization failed for " |
| 4162 | << PrettyDescriptor(handle_scope_super.Get()) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4163 | << " that has unexpected status " << handle_scope_super->GetStatus() |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4164 | << "\nPending exception:\n" |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4165 | << (self->GetException(nullptr) != nullptr ? self->GetException(nullptr)->Dump() : ""); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4166 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4167 | // Initialization failed because the super-class is erroneous. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4168 | klass->SetStatus(mirror::Class::kStatusError, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4169 | return false; |
| 4170 | } |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 4171 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4172 | } |
| 4173 | |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4174 | const size_t num_static_fields = klass->NumStaticFields(); |
| 4175 | if (num_static_fields > 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4176 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4177 | CHECK(dex_class_def != nullptr); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4178 | const DexFile& dex_file = klass->GetDexFile(); |
Hiroshi Yamauchi | 67ef46a | 2014-08-21 15:59:43 -0700 | [diff] [blame] | 4179 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4180 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4181 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4182 | |
| 4183 | // Eagerly fill in static fields so that the we don't have to do as many expensive |
| 4184 | // Class::FindStaticField in ResolveField. |
| 4185 | for (size_t i = 0; i < num_static_fields; ++i) { |
| 4186 | mirror::ArtField* field = klass->GetStaticField(i); |
| 4187 | const uint32_t field_idx = field->GetDexFieldIndex(); |
| 4188 | mirror::ArtField* resolved_field = dex_cache->GetResolvedField(field_idx); |
| 4189 | if (resolved_field == nullptr) { |
| 4190 | dex_cache->SetResolvedField(field_idx, field); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4191 | } else { |
| 4192 | DCHECK_EQ(field, resolved_field); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4193 | } |
| 4194 | } |
| 4195 | |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4196 | EncodedStaticFieldValueIterator value_it(dex_file, &dex_cache, &class_loader, |
| 4197 | this, *dex_class_def); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 4198 | const uint8_t* class_data = dex_file.GetClassData(*dex_class_def); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4199 | ClassDataItemIterator field_it(dex_file, class_data); |
| 4200 | if (value_it.HasNext()) { |
| 4201 | DCHECK(field_it.HasNextStaticField()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4202 | CHECK(can_init_statics); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4203 | for ( ; value_it.HasNext(); value_it.Next(), field_it.Next()) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 4204 | StackHandleScope<1> hs2(self); |
| 4205 | Handle<mirror::ArtField> field(hs2.NewHandle( |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4206 | ResolveField(dex_file, field_it.GetMemberIndex(), dex_cache, class_loader, true))); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4207 | if (Runtime::Current()->IsActiveTransaction()) { |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4208 | value_it.ReadValueToField<true>(field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4209 | } else { |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4210 | value_it.ReadValueToField<false>(field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4211 | } |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4212 | DCHECK(!value_it.HasNext() || field_it.HasNextStaticField()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4213 | } |
| 4214 | } |
| 4215 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4216 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 4217 | mirror::ArtMethod* clinit = klass->FindClassInitializer(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4218 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4219 | CHECK(can_init_statics); |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 4220 | JValue result; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4221 | clinit->Invoke(self, nullptr, 0, &result, "V"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4222 | } |
| 4223 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4224 | self->AllowThreadSuspension(); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 4225 | uint64_t t1 = NanoTime(); |
| 4226 | |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4227 | bool success = true; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4228 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4229 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4230 | |
| 4231 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4232 | WrapExceptionInInitializer(klass); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4233 | klass->SetStatus(mirror::Class::kStatusError, self); |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4234 | success = false; |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 4235 | } else if (Runtime::Current()->IsTransactionAborted()) { |
| 4236 | // The exception thrown when the transaction aborted has been caught and cleared |
| 4237 | // so we need to throw it again now. |
| 4238 | LOG(WARNING) << "Return from class initializer of " << PrettyDescriptor(klass.Get()) |
| 4239 | << " without exception while transaction was aborted: re-throw it now."; |
| 4240 | Runtime::Current()->ThrowInternalErrorForAbortedTransaction(self); |
| 4241 | klass->SetStatus(mirror::Class::kStatusError, self); |
| 4242 | success = false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4243 | } else { |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 4244 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 4245 | RuntimeStats* thread_stats = self->GetStats(); |
| 4246 | ++global_stats->class_init_count; |
| 4247 | ++thread_stats->class_init_count; |
| 4248 | global_stats->class_init_time_ns += (t1 - t0); |
| 4249 | thread_stats->class_init_time_ns += (t1 - t0); |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 4250 | // Set the class as initialized except if failed to initialize static fields. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4251 | klass->SetStatus(mirror::Class::kStatusInitialized, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4252 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4253 | std::string temp; |
| 4254 | LOG(INFO) << "Initialized class " << klass->GetDescriptor(&temp) << " from " << |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4255 | klass->GetLocation(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 4256 | } |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 4257 | // Opportunistically set static method trampolines to their destination. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4258 | FixupStaticTrampolines(klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4259 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4260 | } |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4261 | return success; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4262 | } |
| 4263 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4264 | bool ClassLinker::WaitForInitializeClass(Handle<mirror::Class> klass, Thread* self, |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4265 | ObjectLock<mirror::Class>& lock) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 4266 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4267 | while (true) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4268 | self->AssertNoPendingException(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4269 | CHECK(!klass->IsInitialized()); |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 4270 | lock.WaitIgnoringInterrupts(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4271 | |
| 4272 | // When we wake up, repeat the test for init-in-progress. If |
| 4273 | // there's an exception pending (only possible if |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4274 | // we were not using WaitIgnoringInterrupts), bail out. |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4275 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4276 | WrapExceptionInInitializer(klass); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4277 | klass->SetStatus(mirror::Class::kStatusError, self); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4278 | return false; |
| 4279 | } |
| 4280 | // Spurious wakeup? Go back to waiting. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4281 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4282 | continue; |
| 4283 | } |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame^] | 4284 | if (klass->GetStatus() == mirror::Class::kStatusVerified && |
| 4285 | Runtime::Current()->IsAotCompiler()) { |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 4286 | // Compile time initialization failed. |
| 4287 | return false; |
| 4288 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4289 | if (klass->IsErroneous()) { |
| 4290 | // The caller wants an exception, but it was thrown in a |
| 4291 | // different thread. Synthesize one here. |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 4292 | ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread", |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4293 | PrettyDescriptor(klass.Get()).c_str()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4294 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4295 | return false; |
| 4296 | } |
| 4297 | if (klass->IsInitialized()) { |
| 4298 | return true; |
| 4299 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4300 | LOG(FATAL) << "Unexpected class status. " << PrettyClass(klass.Get()) << " is " |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4301 | << klass->GetStatus(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4302 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 4303 | UNREACHABLE(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4304 | } |
| 4305 | |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4306 | static bool HasSameSignatureWithDifferentClassLoaders(Thread* self, |
| 4307 | Handle<mirror::ArtMethod> method1, |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 4308 | Handle<mirror::ArtMethod> method2, |
| 4309 | std::string* error_msg) |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4310 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4311 | { |
| 4312 | StackHandleScope<1> hs(self); |
| 4313 | Handle<mirror::Class> return_type(hs.NewHandle(method1->GetReturnType())); |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 4314 | mirror::Class* other_return_type = method2->GetReturnType(); |
| 4315 | // NOTE: return_type.Get() must be sequenced after method2->GetReturnType(). |
| 4316 | if (UNLIKELY(other_return_type != return_type.Get())) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 4317 | *error_msg = StringPrintf("Return types mismatch: %s(%p) vs %s(%p)", |
| 4318 | PrettyClassAndClassLoader(return_type.Get()).c_str(), |
| 4319 | return_type.Get(), |
| 4320 | PrettyClassAndClassLoader(other_return_type).c_str(), |
| 4321 | other_return_type); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4322 | return false; |
| 4323 | } |
| 4324 | } |
| 4325 | const DexFile::TypeList* types1 = method1->GetParameterTypeList(); |
| 4326 | const DexFile::TypeList* types2 = method2->GetParameterTypeList(); |
| 4327 | if (types1 == nullptr) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 4328 | if (types2 != nullptr && types2->Size() != 0) { |
| 4329 | *error_msg = StringPrintf("Type list mismatch with %s", |
| 4330 | PrettyMethod(method2.Get(), true).c_str()); |
| 4331 | return false; |
| 4332 | } |
| 4333 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4334 | } else if (UNLIKELY(types2 == nullptr)) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 4335 | if (types1->Size() != 0) { |
| 4336 | *error_msg = StringPrintf("Type list mismatch with %s", |
| 4337 | PrettyMethod(method2.Get(), true).c_str()); |
| 4338 | return false; |
| 4339 | } |
| 4340 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4341 | } |
| 4342 | uint32_t num_types = types1->Size(); |
| 4343 | if (UNLIKELY(num_types != types2->Size())) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 4344 | *error_msg = StringPrintf("Type list mismatch with %s", |
| 4345 | PrettyMethod(method2.Get(), true).c_str()); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4346 | return false; |
| 4347 | } |
| 4348 | for (uint32_t i = 0; i < num_types; ++i) { |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 4349 | StackHandleScope<1> hs(self); |
| 4350 | Handle<mirror::Class> param_type(hs.NewHandle( |
| 4351 | method1->GetClassFromTypeIndex(types1->GetTypeItem(i).type_idx_, true))); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4352 | mirror::Class* other_param_type = |
| 4353 | method2->GetClassFromTypeIndex(types2->GetTypeItem(i).type_idx_, true); |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 4354 | // NOTE: param_type.Get() must be sequenced after method2->GetClassFromTypeIndex(...). |
| 4355 | if (UNLIKELY(param_type.Get() != other_param_type)) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 4356 | *error_msg = StringPrintf("Parameter %u type mismatch: %s(%p) vs %s(%p)", |
| 4357 | i, |
| 4358 | PrettyClassAndClassLoader(param_type.Get()).c_str(), |
| 4359 | param_type.Get(), |
| 4360 | PrettyClassAndClassLoader(other_param_type).c_str(), |
| 4361 | other_param_type); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4362 | return false; |
| 4363 | } |
| 4364 | } |
| 4365 | return true; |
| 4366 | } |
| 4367 | |
| 4368 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4369 | bool ClassLinker::ValidateSuperClassDescriptors(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4370 | if (klass->IsInterface()) { |
| 4371 | return true; |
| 4372 | } |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 4373 | // Begin with the methods local to the superclass. |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 4374 | Thread* self = Thread::Current(); |
| 4375 | StackHandleScope<2> hs(self); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4376 | MutableHandle<mirror::ArtMethod> h_m(hs.NewHandle<mirror::ArtMethod>(nullptr)); |
| 4377 | MutableHandle<mirror::ArtMethod> super_h_m(hs.NewHandle<mirror::ArtMethod>(nullptr)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4378 | if (klass->HasSuperClass() && |
| 4379 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4380 | for (int i = klass->GetSuperClass()->GetVTableLength() - 1; i >= 0; --i) { |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4381 | h_m.Assign(klass->GetVTableEntry(i)); |
| 4382 | super_h_m.Assign(klass->GetSuperClass()->GetVTableEntry(i)); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 4383 | if (h_m.Get() != super_h_m.Get()) { |
| 4384 | std::string error_msg; |
| 4385 | if (!HasSameSignatureWithDifferentClassLoaders(self, h_m, super_h_m, &error_msg)) { |
| 4386 | ThrowLinkageError(klass.Get(), |
| 4387 | "Class %s method %s resolves differently in superclass %s: %s", |
| 4388 | PrettyDescriptor(klass.Get()).c_str(), |
| 4389 | PrettyMethod(h_m.Get()).c_str(), |
| 4390 | PrettyDescriptor(klass->GetSuperClass()).c_str(), |
| 4391 | error_msg.c_str()); |
| 4392 | return false; |
| 4393 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4394 | } |
| 4395 | } |
| 4396 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 4397 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 4398 | if (klass->GetClassLoader() != klass->GetIfTable()->GetInterface(i)->GetClassLoader()) { |
| 4399 | uint32_t num_methods = klass->GetIfTable()->GetInterface(i)->NumVirtualMethods(); |
| 4400 | for (uint32_t j = 0; j < num_methods; ++j) { |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4401 | h_m.Assign(klass->GetIfTable()->GetMethodArray(i)->GetWithoutChecks(j)); |
| 4402 | super_h_m.Assign(klass->GetIfTable()->GetInterface(i)->GetVirtualMethod(j)); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 4403 | if (h_m.Get() != super_h_m.Get()) { |
| 4404 | std::string error_msg; |
| 4405 | if (!HasSameSignatureWithDifferentClassLoaders(self, h_m, super_h_m, &error_msg)) { |
| 4406 | ThrowLinkageError(klass.Get(), |
| 4407 | "Class %s method %s resolves differently in interface %s: %s", |
| 4408 | PrettyDescriptor(klass.Get()).c_str(), |
| 4409 | PrettyMethod(h_m.Get()).c_str(), |
| 4410 | PrettyDescriptor(klass->GetIfTable()->GetInterface(i)).c_str(), |
| 4411 | error_msg.c_str()); |
| 4412 | return false; |
| 4413 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4414 | } |
| 4415 | } |
| 4416 | } |
| 4417 | } |
| 4418 | return true; |
| 4419 | } |
| 4420 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4421 | bool ClassLinker::EnsureInitialized(Thread* self, Handle<mirror::Class> c, bool can_init_fields, |
| 4422 | bool can_init_parents) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4423 | DCHECK(c.Get() != nullptr); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4424 | if (c->IsInitialized()) { |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4425 | EnsurePreverifiedMethods(c); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4426 | return true; |
| 4427 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4428 | const bool success = InitializeClass(self, c, can_init_fields, can_init_parents); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4429 | if (!success) { |
| 4430 | if (can_init_fields && can_init_parents) { |
| 4431 | CHECK(self->IsExceptionPending()) << PrettyClass(c.Get()); |
| 4432 | } |
| 4433 | } else { |
| 4434 | self->AssertNoPendingException(); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 4435 | } |
| 4436 | return success; |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 4437 | } |
| 4438 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4439 | void ClassLinker::FixupTemporaryDeclaringClass(mirror::Class* temp_class, mirror::Class* new_class) { |
| 4440 | mirror::ObjectArray<mirror::ArtField>* fields = new_class->GetIFields(); |
| 4441 | if (fields != nullptr) { |
| 4442 | for (int index = 0; index < fields->GetLength(); index ++) { |
| 4443 | if (fields->Get(index)->GetDeclaringClass() == temp_class) { |
| 4444 | fields->Get(index)->SetDeclaringClass(new_class); |
| 4445 | } |
| 4446 | } |
| 4447 | } |
| 4448 | |
| 4449 | fields = new_class->GetSFields(); |
| 4450 | if (fields != nullptr) { |
| 4451 | for (int index = 0; index < fields->GetLength(); index ++) { |
| 4452 | if (fields->Get(index)->GetDeclaringClass() == temp_class) { |
| 4453 | fields->Get(index)->SetDeclaringClass(new_class); |
| 4454 | } |
| 4455 | } |
| 4456 | } |
| 4457 | |
| 4458 | mirror::ObjectArray<mirror::ArtMethod>* methods = new_class->GetDirectMethods(); |
| 4459 | if (methods != nullptr) { |
| 4460 | for (int index = 0; index < methods->GetLength(); index ++) { |
| 4461 | if (methods->Get(index)->GetDeclaringClass() == temp_class) { |
| 4462 | methods->Get(index)->SetDeclaringClass(new_class); |
| 4463 | } |
| 4464 | } |
| 4465 | } |
| 4466 | |
| 4467 | methods = new_class->GetVirtualMethods(); |
| 4468 | if (methods != nullptr) { |
| 4469 | for (int index = 0; index < methods->GetLength(); index ++) { |
| 4470 | if (methods->Get(index)->GetDeclaringClass() == temp_class) { |
| 4471 | methods->Get(index)->SetDeclaringClass(new_class); |
| 4472 | } |
| 4473 | } |
| 4474 | } |
| 4475 | } |
| 4476 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4477 | bool ClassLinker::LinkClass(Thread* self, const char* descriptor, Handle<mirror::Class> klass, |
| 4478 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4479 | mirror::Class** new_class) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4480 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4481 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4482 | if (!LinkSuperClass(klass)) { |
| 4483 | return false; |
| 4484 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4485 | StackHandleScope<mirror::Class::kImtSize> imt_handle_scope( |
| 4486 | self, Runtime::Current()->GetImtUnimplementedMethod()); |
| 4487 | if (!LinkMethods(self, klass, interfaces, &imt_handle_scope)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4488 | return false; |
| 4489 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4490 | if (!LinkInstanceFields(self, klass)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4491 | return false; |
| 4492 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4493 | size_t class_size; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4494 | if (!LinkStaticFields(self, klass, &class_size)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 4495 | return false; |
| 4496 | } |
| 4497 | CreateReferenceInstanceOffsets(klass); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4498 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4499 | |
| 4500 | if (!klass->IsTemp() || (!init_done_ && klass->GetClassSize() == class_size)) { |
| 4501 | // We don't need to retire this class as it has no embedded tables or it was created the |
| 4502 | // correct size during class linker initialization. |
| 4503 | CHECK_EQ(klass->GetClassSize(), class_size) << PrettyDescriptor(klass.Get()); |
| 4504 | |
| 4505 | if (klass->ShouldHaveEmbeddedImtAndVTable()) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4506 | klass->PopulateEmbeddedImtAndVTable(&imt_handle_scope); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4507 | } |
| 4508 | |
| 4509 | // This will notify waiters on klass that saw the not yet resolved |
| 4510 | // class in the class_table_ during EnsureResolved. |
| 4511 | klass->SetStatus(mirror::Class::kStatusResolved, self); |
| 4512 | *new_class = klass.Get(); |
| 4513 | } else { |
| 4514 | CHECK(!klass->IsResolved()); |
| 4515 | // Retire the temporary class and create the correctly sized resolved class. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4516 | *new_class = klass->CopyOf(self, class_size, &imt_handle_scope); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4517 | if (UNLIKELY(*new_class == nullptr)) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4518 | CHECK(self->IsExceptionPending()); // Expect an OOME. |
| 4519 | klass->SetStatus(mirror::Class::kStatusError, self); |
| 4520 | return false; |
| 4521 | } |
| 4522 | |
| 4523 | CHECK_EQ((*new_class)->GetClassSize(), class_size); |
| 4524 | StackHandleScope<1> hs(self); |
| 4525 | auto new_class_h = hs.NewHandleWrapper<mirror::Class>(new_class); |
| 4526 | ObjectLock<mirror::Class> lock(self, new_class_h); |
| 4527 | |
| 4528 | FixupTemporaryDeclaringClass(klass.Get(), new_class_h.Get()); |
| 4529 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 4530 | mirror::Class* existing = UpdateClass(descriptor, new_class_h.Get(), |
| 4531 | ComputeModifiedUtf8Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4532 | CHECK(existing == nullptr || existing == klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4533 | |
| 4534 | // This will notify waiters on temp class that saw the not yet resolved class in the |
| 4535 | // class_table_ during EnsureResolved. |
| 4536 | klass->SetStatus(mirror::Class::kStatusRetired, self); |
| 4537 | |
| 4538 | CHECK_EQ(new_class_h->GetStatus(), mirror::Class::kStatusResolving); |
| 4539 | // This will notify waiters on new_class that saw the not yet resolved |
| 4540 | // class in the class_table_ during EnsureResolved. |
| 4541 | new_class_h->SetStatus(mirror::Class::kStatusResolved, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4542 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4543 | return true; |
| 4544 | } |
| 4545 | |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4546 | static void CountMethodsAndFields(ClassDataItemIterator& dex_data, |
| 4547 | size_t* virtual_methods, |
| 4548 | size_t* direct_methods, |
| 4549 | size_t* static_fields, |
| 4550 | size_t* instance_fields) { |
| 4551 | *virtual_methods = *direct_methods = *static_fields = *instance_fields = 0; |
| 4552 | |
| 4553 | while (dex_data.HasNextStaticField()) { |
| 4554 | dex_data.Next(); |
| 4555 | (*static_fields)++; |
| 4556 | } |
| 4557 | while (dex_data.HasNextInstanceField()) { |
| 4558 | dex_data.Next(); |
| 4559 | (*instance_fields)++; |
| 4560 | } |
| 4561 | while (dex_data.HasNextDirectMethod()) { |
| 4562 | (*direct_methods)++; |
| 4563 | dex_data.Next(); |
| 4564 | } |
| 4565 | while (dex_data.HasNextVirtualMethod()) { |
| 4566 | (*virtual_methods)++; |
| 4567 | dex_data.Next(); |
| 4568 | } |
| 4569 | DCHECK(!dex_data.HasNext()); |
| 4570 | } |
| 4571 | |
| 4572 | static void DumpClass(std::ostream& os, |
| 4573 | const DexFile& dex_file, const DexFile::ClassDef& dex_class_def, |
| 4574 | const char* suffix) { |
| 4575 | ClassDataItemIterator dex_data(dex_file, dex_file.GetClassData(dex_class_def)); |
| 4576 | os << dex_file.GetClassDescriptor(dex_class_def) << suffix << ":\n"; |
| 4577 | os << " Static fields:\n"; |
| 4578 | while (dex_data.HasNextStaticField()) { |
| 4579 | const DexFile::FieldId& id = dex_file.GetFieldId(dex_data.GetMemberIndex()); |
| 4580 | os << " " << dex_file.GetFieldTypeDescriptor(id) << " " << dex_file.GetFieldName(id) << "\n"; |
| 4581 | dex_data.Next(); |
| 4582 | } |
| 4583 | os << " Instance fields:\n"; |
| 4584 | while (dex_data.HasNextInstanceField()) { |
| 4585 | const DexFile::FieldId& id = dex_file.GetFieldId(dex_data.GetMemberIndex()); |
| 4586 | os << " " << dex_file.GetFieldTypeDescriptor(id) << " " << dex_file.GetFieldName(id) << "\n"; |
| 4587 | dex_data.Next(); |
| 4588 | } |
| 4589 | os << " Direct methods:\n"; |
| 4590 | while (dex_data.HasNextDirectMethod()) { |
| 4591 | const DexFile::MethodId& id = dex_file.GetMethodId(dex_data.GetMemberIndex()); |
| 4592 | os << " " << dex_file.GetMethodName(id) << dex_file.GetMethodSignature(id).ToString() << "\n"; |
| 4593 | dex_data.Next(); |
| 4594 | } |
| 4595 | os << " Virtual methods:\n"; |
| 4596 | while (dex_data.HasNextVirtualMethod()) { |
| 4597 | const DexFile::MethodId& id = dex_file.GetMethodId(dex_data.GetMemberIndex()); |
| 4598 | os << " " << dex_file.GetMethodName(id) << dex_file.GetMethodSignature(id).ToString() << "\n"; |
| 4599 | dex_data.Next(); |
| 4600 | } |
| 4601 | } |
| 4602 | |
| 4603 | static std::string DumpClasses(const DexFile& dex_file1, const DexFile::ClassDef& dex_class_def1, |
| 4604 | const DexFile& dex_file2, const DexFile::ClassDef& dex_class_def2) { |
| 4605 | std::ostringstream os; |
| 4606 | DumpClass(os, dex_file1, dex_class_def1, " (Compile time)"); |
| 4607 | DumpClass(os, dex_file2, dex_class_def2, " (Runtime)"); |
| 4608 | return os.str(); |
| 4609 | } |
| 4610 | |
| 4611 | |
| 4612 | // Very simple structural check on whether the classes match. Only compares the number of |
| 4613 | // methods and fields. |
| 4614 | static bool SimpleStructuralCheck(const DexFile& dex_file1, const DexFile::ClassDef& dex_class_def1, |
| 4615 | const DexFile& dex_file2, const DexFile::ClassDef& dex_class_def2, |
| 4616 | std::string* error_msg) { |
| 4617 | ClassDataItemIterator dex_data1(dex_file1, dex_file1.GetClassData(dex_class_def1)); |
| 4618 | ClassDataItemIterator dex_data2(dex_file2, dex_file2.GetClassData(dex_class_def2)); |
| 4619 | |
| 4620 | // Counters for current dex file. |
| 4621 | size_t dex_virtual_methods1, dex_direct_methods1, dex_static_fields1, dex_instance_fields1; |
| 4622 | CountMethodsAndFields(dex_data1, &dex_virtual_methods1, &dex_direct_methods1, &dex_static_fields1, |
| 4623 | &dex_instance_fields1); |
| 4624 | // Counters for compile-time dex file. |
| 4625 | size_t dex_virtual_methods2, dex_direct_methods2, dex_static_fields2, dex_instance_fields2; |
| 4626 | CountMethodsAndFields(dex_data2, &dex_virtual_methods2, &dex_direct_methods2, &dex_static_fields2, |
| 4627 | &dex_instance_fields2); |
| 4628 | |
| 4629 | if (dex_virtual_methods1 != dex_virtual_methods2) { |
| 4630 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
| 4631 | *error_msg = StringPrintf("Virtual method count off: %zu vs %zu\n%s", dex_virtual_methods1, |
| 4632 | dex_virtual_methods2, class_dump.c_str()); |
| 4633 | return false; |
| 4634 | } |
| 4635 | if (dex_direct_methods1 != dex_direct_methods2) { |
| 4636 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
| 4637 | *error_msg = StringPrintf("Direct method count off: %zu vs %zu\n%s", dex_direct_methods1, |
| 4638 | dex_direct_methods2, class_dump.c_str()); |
| 4639 | return false; |
| 4640 | } |
| 4641 | if (dex_static_fields1 != dex_static_fields2) { |
| 4642 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
| 4643 | *error_msg = StringPrintf("Static field count off: %zu vs %zu\n%s", dex_static_fields1, |
| 4644 | dex_static_fields2, class_dump.c_str()); |
| 4645 | return false; |
| 4646 | } |
| 4647 | if (dex_instance_fields1 != dex_instance_fields2) { |
| 4648 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
| 4649 | *error_msg = StringPrintf("Instance field count off: %zu vs %zu\n%s", dex_instance_fields1, |
| 4650 | dex_instance_fields2, class_dump.c_str()); |
| 4651 | return false; |
| 4652 | } |
| 4653 | |
| 4654 | return true; |
| 4655 | } |
| 4656 | |
| 4657 | // Checks whether a the super-class changed from what we had at compile-time. This would |
| 4658 | // invalidate quickening. |
| 4659 | static bool CheckSuperClassChange(Handle<mirror::Class> klass, |
| 4660 | const DexFile& dex_file, |
| 4661 | const DexFile::ClassDef& class_def, |
| 4662 | mirror::Class* super_class) |
| 4663 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4664 | // Check for unexpected changes in the superclass. |
| 4665 | // Quick check 1) is the super_class class-loader the boot class loader? This always has |
| 4666 | // precedence. |
| 4667 | if (super_class->GetClassLoader() != nullptr && |
| 4668 | // Quick check 2) different dex cache? Breaks can only occur for different dex files, |
| 4669 | // which is implied by different dex cache. |
| 4670 | klass->GetDexCache() != super_class->GetDexCache()) { |
| 4671 | // Now comes the expensive part: things can be broken if (a) the klass' dex file has a |
| 4672 | // definition for the super-class, and (b) the files are in separate oat files. The oat files |
| 4673 | // are referenced from the dex file, so do (b) first. Only relevant if we have oat files. |
| 4674 | const OatFile* class_oat_file = dex_file.GetOatFile(); |
| 4675 | if (class_oat_file != nullptr) { |
| 4676 | const OatFile* loaded_super_oat_file = super_class->GetDexFile().GetOatFile(); |
| 4677 | if (loaded_super_oat_file != nullptr && class_oat_file != loaded_super_oat_file) { |
| 4678 | // Now check (a). |
| 4679 | const DexFile::ClassDef* super_class_def = dex_file.FindClassDef(class_def.superclass_idx_); |
| 4680 | if (super_class_def != nullptr) { |
| 4681 | // Uh-oh, we found something. Do our check. |
| 4682 | std::string error_msg; |
| 4683 | if (!SimpleStructuralCheck(dex_file, *super_class_def, |
| 4684 | super_class->GetDexFile(), *super_class->GetClassDef(), |
| 4685 | &error_msg)) { |
| 4686 | // Print a warning to the log. This exception might be caught, e.g., as common in test |
| 4687 | // drivers. When the class is later tried to be used, we re-throw a new instance, as we |
| 4688 | // only save the type of the exception. |
| 4689 | LOG(WARNING) << "Incompatible structural change detected: " << |
| 4690 | StringPrintf( |
| 4691 | "Structural change of %s is hazardous (%s at compile time, %s at runtime): %s", |
| 4692 | PrettyType(super_class_def->class_idx_, dex_file).c_str(), |
| 4693 | class_oat_file->GetLocation().c_str(), |
| 4694 | loaded_super_oat_file->GetLocation().c_str(), |
| 4695 | error_msg.c_str()); |
| 4696 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 4697 | "Structural change of %s is hazardous (%s at compile time, %s at runtime): %s", |
| 4698 | PrettyType(super_class_def->class_idx_, dex_file).c_str(), |
| 4699 | class_oat_file->GetLocation().c_str(), |
| 4700 | loaded_super_oat_file->GetLocation().c_str(), |
| 4701 | error_msg.c_str()); |
| 4702 | return false; |
| 4703 | } |
| 4704 | } |
| 4705 | } |
| 4706 | } |
| 4707 | } |
| 4708 | return true; |
| 4709 | } |
| 4710 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4711 | bool ClassLinker::LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4712 | CHECK_EQ(mirror::Class::kStatusIdx, klass->GetStatus()); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4713 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
| 4714 | uint16_t super_class_idx = class_def.superclass_idx_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4715 | if (super_class_idx != DexFile::kDexNoIndex16) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4716 | mirror::Class* super_class = ResolveType(dex_file, super_class_idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4717 | if (super_class == nullptr) { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 4718 | DCHECK(Thread::Current()->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4719 | return false; |
| 4720 | } |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 4721 | // Verify |
| 4722 | if (!klass->CanAccess(super_class)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4723 | ThrowIllegalAccessError(klass.Get(), "Class %s extended by class %s is inaccessible", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4724 | PrettyDescriptor(super_class).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4725 | PrettyDescriptor(klass.Get()).c_str()); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 4726 | return false; |
| 4727 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4728 | CHECK(super_class->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4729 | klass->SetSuperClass(super_class); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 4730 | |
| 4731 | if (!CheckSuperClassChange(klass, dex_file, class_def, super_class)) { |
| 4732 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4733 | return false; |
| 4734 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4735 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4736 | const DexFile::TypeList* interfaces = dex_file.GetInterfacesList(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4737 | if (interfaces != nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4738 | for (size_t i = 0; i < interfaces->Size(); i++) { |
| 4739 | uint16_t idx = interfaces->GetTypeItem(i).type_idx_; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4740 | mirror::Class* interface = ResolveType(dex_file, idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4741 | if (interface == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4742 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4743 | return false; |
| 4744 | } |
| 4745 | // Verify |
| 4746 | if (!klass->CanAccess(interface)) { |
| 4747 | // TODO: the RI seemed to ignore this in my testing. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4748 | ThrowIllegalAccessError(klass.Get(), "Interface %s implemented by class %s is inaccessible", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4749 | PrettyDescriptor(interface).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4750 | PrettyDescriptor(klass.Get()).c_str()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4751 | return false; |
| 4752 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4753 | } |
| 4754 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4755 | // Mark the class as loaded. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4756 | klass->SetStatus(mirror::Class::kStatusLoaded, nullptr); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4757 | return true; |
| 4758 | } |
| 4759 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4760 | bool ClassLinker::LinkSuperClass(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4761 | CHECK(!klass->IsPrimitive()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4762 | mirror::Class* super = klass->GetSuperClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4763 | if (klass.Get() == GetClassRoot(kJavaLangObject)) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4764 | if (super != nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4765 | ThrowClassFormatError(klass.Get(), "java.lang.Object must not have a superclass"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4766 | return false; |
| 4767 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4768 | return true; |
| 4769 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4770 | if (super == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4771 | ThrowLinkageError(klass.Get(), "No superclass defined for class %s", |
| 4772 | PrettyDescriptor(klass.Get()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4773 | return false; |
| 4774 | } |
| 4775 | // Verify |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 4776 | if (super->IsFinal() || super->IsInterface()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4777 | ThrowIncompatibleClassChangeError(klass.Get(), "Superclass %s of %s is %s", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4778 | PrettyDescriptor(super).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4779 | PrettyDescriptor(klass.Get()).c_str(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4780 | super->IsFinal() ? "declared final" : "an interface"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4781 | return false; |
| 4782 | } |
| 4783 | if (!klass->CanAccess(super)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4784 | ThrowIllegalAccessError(klass.Get(), "Superclass %s is inaccessible to class %s", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4785 | PrettyDescriptor(super).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4786 | PrettyDescriptor(klass.Get()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4787 | return false; |
| 4788 | } |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 4789 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4790 | // Inherit kAccClassIsFinalizable from the superclass in case this |
| 4791 | // class doesn't override finalize. |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 4792 | if (super->IsFinalizable()) { |
| 4793 | klass->SetFinalizable(); |
| 4794 | } |
| 4795 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 4796 | // Inherit reference flags (if any) from the superclass. |
| 4797 | int reference_flags = (super->GetAccessFlags() & kAccReferenceFlagsMask); |
| 4798 | if (reference_flags != 0) { |
| 4799 | klass->SetAccessFlags(klass->GetAccessFlags() | reference_flags); |
| 4800 | } |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 4801 | // Disallow custom direct subclasses of java.lang.ref.Reference. |
Elliott Hughes | bf61ba3 | 2011-10-11 10:53:09 -0700 | [diff] [blame] | 4802 | if (init_done_ && super == GetClassRoot(kJavaLangRefReference)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4803 | ThrowLinkageError(klass.Get(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4804 | "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] | 4805 | PrettyDescriptor(klass.Get()).c_str()); |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 4806 | return false; |
| 4807 | } |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 4808 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4809 | if (kIsDebugBuild) { |
| 4810 | // Ensure super classes are fully resolved prior to resolving fields.. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4811 | while (super != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4812 | CHECK(super->IsResolved()); |
| 4813 | super = super->GetSuperClass(); |
| 4814 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4815 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4816 | return true; |
| 4817 | } |
| 4818 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4819 | // Populate the class vtable and itable. Compute return type indices. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4820 | bool ClassLinker::LinkMethods(Thread* self, Handle<mirror::Class> klass, |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4821 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
| 4822 | StackHandleScope<mirror::Class::kImtSize>* out_imt) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4823 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4824 | if (klass->IsInterface()) { |
| 4825 | // No vtable. |
| 4826 | size_t count = klass->NumVirtualMethods(); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 4827 | if (!IsUint<16>(count)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4828 | ThrowClassFormatError(klass.Get(), "Too many methods on interface: %zd", count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4829 | return false; |
| 4830 | } |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 4831 | for (size_t i = 0; i < count; ++i) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4832 | klass->GetVirtualMethodDuringLinking(i)->SetMethodIndex(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4833 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4834 | } else if (!LinkVirtualMethods(self, klass)) { // Link virtual methods first. |
| 4835 | return false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4836 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4837 | return LinkInterfaceMethods(self, klass, interfaces, out_imt); // Link interface method last. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4838 | } |
| 4839 | |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 4840 | // Comparator for name and signature of a method, used in finding overriding methods. Implementation |
| 4841 | // avoids the use of handles, if it didn't then rather than compare dex files we could compare dex |
| 4842 | // caches in the implementation below. |
| 4843 | class MethodNameAndSignatureComparator FINAL : public ValueObject { |
| 4844 | public: |
| 4845 | explicit MethodNameAndSignatureComparator(mirror::ArtMethod* method) |
| 4846 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) : |
| 4847 | dex_file_(method->GetDexFile()), mid_(&dex_file_->GetMethodId(method->GetDexMethodIndex())), |
| 4848 | name_(nullptr), name_len_(0) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4849 | DCHECK(!method->IsProxyMethod()) << PrettyMethod(method); |
| 4850 | } |
| 4851 | |
| 4852 | const char* GetName() { |
| 4853 | if (name_ == nullptr) { |
| 4854 | name_ = dex_file_->StringDataAndUtf16LengthByIdx(mid_->name_idx_, &name_len_); |
| 4855 | } |
| 4856 | return name_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 4857 | } |
| 4858 | |
| 4859 | bool HasSameNameAndSignature(mirror::ArtMethod* other) |
| 4860 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4861 | DCHECK(!other->IsProxyMethod()) << PrettyMethod(other); |
| 4862 | const DexFile* other_dex_file = other->GetDexFile(); |
| 4863 | const DexFile::MethodId& other_mid = other_dex_file->GetMethodId(other->GetDexMethodIndex()); |
| 4864 | if (dex_file_ == other_dex_file) { |
| 4865 | return mid_->name_idx_ == other_mid.name_idx_ && mid_->proto_idx_ == other_mid.proto_idx_; |
| 4866 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4867 | GetName(); // Only used to make sure its calculated. |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 4868 | uint32_t other_name_len; |
| 4869 | const char* other_name = other_dex_file->StringDataAndUtf16LengthByIdx(other_mid.name_idx_, |
| 4870 | &other_name_len); |
| 4871 | if (name_len_ != other_name_len || strcmp(name_, other_name) != 0) { |
| 4872 | return false; |
| 4873 | } |
| 4874 | return dex_file_->GetMethodSignature(*mid_) == other_dex_file->GetMethodSignature(other_mid); |
| 4875 | } |
| 4876 | |
| 4877 | private: |
| 4878 | // Dex file for the method to compare against. |
| 4879 | const DexFile* const dex_file_; |
| 4880 | // MethodId for the method to compare against. |
| 4881 | const DexFile::MethodId* const mid_; |
| 4882 | // Lazily computed name from the dex file's strings. |
| 4883 | const char* name_; |
| 4884 | // Lazily computed name length. |
| 4885 | uint32_t name_len_; |
| 4886 | }; |
| 4887 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4888 | class LinkVirtualHashTable { |
| 4889 | public: |
| 4890 | LinkVirtualHashTable(Handle<mirror::Class> klass, size_t hash_size, uint32_t* hash_table) |
| 4891 | : klass_(klass), hash_size_(hash_size), hash_table_(hash_table) { |
| 4892 | std::fill(hash_table_, hash_table_ + hash_size_, invalid_index_); |
| 4893 | } |
| 4894 | void Add(uint32_t virtual_method_index) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4895 | mirror::ArtMethod* local_method = klass_->GetVirtualMethodDuringLinking(virtual_method_index); |
| 4896 | const char* name = local_method->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 4897 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4898 | uint32_t index = hash % hash_size_; |
| 4899 | // Linear probe until we have an empty slot. |
| 4900 | while (hash_table_[index] != invalid_index_) { |
| 4901 | if (++index == hash_size_) { |
| 4902 | index = 0; |
| 4903 | } |
| 4904 | } |
| 4905 | hash_table_[index] = virtual_method_index; |
| 4906 | } |
| 4907 | uint32_t FindAndRemove(MethodNameAndSignatureComparator* comparator) |
| 4908 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4909 | const char* name = comparator->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 4910 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4911 | size_t index = hash % hash_size_; |
| 4912 | while (true) { |
| 4913 | const uint32_t value = hash_table_[index]; |
| 4914 | // Since linear probe makes continuous blocks, hitting an invalid index means we are done |
| 4915 | // the block and can safely assume not found. |
| 4916 | if (value == invalid_index_) { |
| 4917 | break; |
| 4918 | } |
| 4919 | if (value != removed_index_) { // This signifies not already overriden. |
| 4920 | mirror::ArtMethod* virtual_method = |
| 4921 | klass_->GetVirtualMethodDuringLinking(value); |
| 4922 | if (comparator->HasSameNameAndSignature(virtual_method->GetInterfaceMethodIfProxy())) { |
| 4923 | hash_table_[index] = removed_index_; |
| 4924 | return value; |
| 4925 | } |
| 4926 | } |
| 4927 | if (++index == hash_size_) { |
| 4928 | index = 0; |
| 4929 | } |
| 4930 | } |
| 4931 | return GetNotFoundIndex(); |
| 4932 | } |
| 4933 | static uint32_t GetNotFoundIndex() { |
| 4934 | return invalid_index_; |
| 4935 | } |
| 4936 | |
| 4937 | private: |
| 4938 | static const uint32_t invalid_index_; |
| 4939 | static const uint32_t removed_index_; |
| 4940 | |
| 4941 | Handle<mirror::Class> klass_; |
| 4942 | const size_t hash_size_; |
| 4943 | uint32_t* const hash_table_; |
| 4944 | }; |
| 4945 | |
| 4946 | const uint32_t LinkVirtualHashTable::invalid_index_ = std::numeric_limits<uint32_t>::max(); |
| 4947 | const uint32_t LinkVirtualHashTable::removed_index_ = std::numeric_limits<uint32_t>::max() - 1; |
| 4948 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4949 | bool ClassLinker::LinkVirtualMethods(Thread* self, Handle<mirror::Class> klass) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4950 | const size_t num_virtual_methods = klass->NumVirtualMethods(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4951 | if (klass->HasSuperClass()) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4952 | const size_t super_vtable_length = klass->GetSuperClass()->GetVTableLength(); |
| 4953 | const size_t max_count = num_virtual_methods + super_vtable_length; |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4954 | StackHandleScope<2> hs(self); |
Mingyao Yang | 38eecb0 | 2014-08-13 14:51:03 -0700 | [diff] [blame] | 4955 | Handle<mirror::Class> super_class(hs.NewHandle(klass->GetSuperClass())); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4956 | MutableHandle<mirror::ObjectArray<mirror::ArtMethod>> vtable; |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4957 | if (super_class->ShouldHaveEmbeddedImtAndVTable()) { |
| 4958 | vtable = hs.NewHandle(AllocArtMethodArray(self, max_count)); |
| 4959 | if (UNLIKELY(vtable.Get() == nullptr)) { |
| 4960 | CHECK(self->IsExceptionPending()); // OOME. |
| 4961 | return false; |
| 4962 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 4963 | for (size_t i = 0; i < super_vtable_length; i++) { |
| 4964 | vtable->SetWithoutChecks<false>(i, super_class->GetEmbeddedVTableEntry(i)); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4965 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4966 | if (num_virtual_methods == 0) { |
| 4967 | klass->SetVTable(vtable.Get()); |
| 4968 | return true; |
| 4969 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4970 | } else { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4971 | mirror::ObjectArray<mirror::ArtMethod>* super_vtable = super_class->GetVTable(); |
| 4972 | CHECK(super_vtable != nullptr) << PrettyClass(super_class.Get()); |
| 4973 | if (num_virtual_methods == 0) { |
| 4974 | klass->SetVTable(super_vtable); |
| 4975 | return true; |
| 4976 | } |
| 4977 | vtable = hs.NewHandle(super_vtable->CopyOf(self, max_count)); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4978 | if (UNLIKELY(vtable.Get() == nullptr)) { |
| 4979 | CHECK(self->IsExceptionPending()); // OOME. |
| 4980 | return false; |
| 4981 | } |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4982 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4983 | // How the algorithm works: |
| 4984 | // 1. Populate hash table by adding num_virtual_methods from klass. The values in the hash |
| 4985 | // table are: invalid_index for unused slots, index super_vtable_length + i for a virtual |
| 4986 | // method which has not been matched to a vtable method, and j if the virtual method at the |
| 4987 | // index overrode the super virtual method at index j. |
| 4988 | // 2. Loop through super virtual methods, if they overwrite, update hash table to j |
| 4989 | // (j < super_vtable_length) to avoid redundant checks. (TODO maybe use this info for reducing |
| 4990 | // the need for the initial vtable which we later shrink back down). |
| 4991 | // 3. Add non overridden methods to the end of the vtable. |
| 4992 | static constexpr size_t kMaxStackHash = 250; |
| 4993 | const size_t hash_table_size = num_virtual_methods * 3; |
| 4994 | uint32_t* hash_table_ptr; |
| 4995 | std::unique_ptr<uint32_t[]> hash_heap_storage; |
| 4996 | if (hash_table_size <= kMaxStackHash) { |
| 4997 | hash_table_ptr = reinterpret_cast<uint32_t*>( |
| 4998 | alloca(hash_table_size * sizeof(*hash_table_ptr))); |
| 4999 | } else { |
| 5000 | hash_heap_storage.reset(new uint32_t[hash_table_size]); |
| 5001 | hash_table_ptr = hash_heap_storage.get(); |
| 5002 | } |
| 5003 | LinkVirtualHashTable hash_table(klass, hash_table_size, hash_table_ptr); |
| 5004 | // Add virtual methods to the hash table. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5005 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5006 | hash_table.Add(i); |
| 5007 | } |
| 5008 | // Loop through each super vtable method and see if they are overriden by a method we added to |
| 5009 | // the hash table. |
| 5010 | for (size_t j = 0; j < super_vtable_length; ++j) { |
| 5011 | // Search the hash table to see if we are overidden by any method. |
| 5012 | mirror::ArtMethod* super_method = vtable->GetWithoutChecks(j); |
| 5013 | MethodNameAndSignatureComparator super_method_name_comparator( |
| 5014 | super_method->GetInterfaceMethodIfProxy()); |
| 5015 | uint32_t hash_index = hash_table.FindAndRemove(&super_method_name_comparator); |
| 5016 | if (hash_index != hash_table.GetNotFoundIndex()) { |
| 5017 | mirror::ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(hash_index); |
| 5018 | if (klass->CanAccessMember(super_method->GetDeclaringClass(), |
| 5019 | super_method->GetAccessFlags())) { |
| 5020 | if (super_method->IsFinal()) { |
| 5021 | ThrowLinkageError(klass.Get(), "Method %s overrides final method in class %s", |
| 5022 | PrettyMethod(virtual_method).c_str(), |
| 5023 | super_method->GetDeclaringClassDescriptor()); |
| 5024 | return false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5025 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5026 | vtable->SetWithoutChecks<false>(j, virtual_method); |
| 5027 | virtual_method->SetMethodIndex(j); |
| 5028 | } else { |
| 5029 | LOG(WARNING) << "Before Android 4.1, method " << PrettyMethod(virtual_method) |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5030 | << " would have incorrectly overridden the package-private method in " |
| 5031 | << PrettyDescriptor(super_method->GetDeclaringClassDescriptor()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5032 | } |
| 5033 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5034 | } |
| 5035 | // Add the non overridden methods at the end. |
| 5036 | size_t actual_count = super_vtable_length; |
| 5037 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
| 5038 | mirror::ArtMethod* local_method = klass->GetVirtualMethodDuringLinking(i); |
| 5039 | size_t method_idx = local_method->GetMethodIndexDuringLinking(); |
| 5040 | if (method_idx < super_vtable_length && |
| 5041 | local_method == vtable->GetWithoutChecks(method_idx)) { |
| 5042 | continue; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5043 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5044 | vtable->SetWithoutChecks<false>(actual_count, local_method); |
| 5045 | local_method->SetMethodIndex(actual_count); |
| 5046 | ++actual_count; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5047 | } |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 5048 | if (!IsUint<16>(actual_count)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5049 | ThrowClassFormatError(klass.Get(), "Too many methods defined on class: %zd", actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5050 | return false; |
| 5051 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5052 | // Shrink vtable if possible |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5053 | CHECK_LE(actual_count, max_count); |
| 5054 | if (actual_count < max_count) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5055 | vtable.Assign(vtable->CopyOf(self, actual_count)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5056 | if (UNLIKELY(vtable.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5057 | CHECK(self->IsExceptionPending()); // OOME. |
| 5058 | return false; |
| 5059 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5060 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5061 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5062 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 5063 | CHECK_EQ(klass.Get(), GetClassRoot(kJavaLangObject)); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 5064 | if (!IsUint<16>(num_virtual_methods)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5065 | ThrowClassFormatError(klass.Get(), "Too many methods: %d", |
| 5066 | static_cast<int>(num_virtual_methods)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5067 | return false; |
| 5068 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5069 | mirror::ObjectArray<mirror::ArtMethod>* vtable = AllocArtMethodArray(self, num_virtual_methods); |
| 5070 | if (UNLIKELY(vtable == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5071 | CHECK(self->IsExceptionPending()); // OOME. |
| 5072 | return false; |
| 5073 | } |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 5074 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5075 | mirror::ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5076 | vtable->SetWithoutChecks<false>(i, virtual_method); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5077 | virtual_method->SetMethodIndex(i & 0xFFFF); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5078 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5079 | klass->SetVTable(vtable); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5080 | } |
| 5081 | return true; |
| 5082 | } |
| 5083 | |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5084 | bool ClassLinker::LinkInterfaceMethods(Thread* self, Handle<mirror::Class> klass, |
| 5085 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
| 5086 | StackHandleScope<mirror::Class::kImtSize>* out_imt) { |
Mathieu Chartier | d035c2d | 2014-10-27 17:30:20 -0700 | [diff] [blame] | 5087 | StackHandleScope<3> hs(self); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 5088 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5089 | const bool has_superclass = klass->HasSuperClass(); |
| 5090 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
| 5091 | const bool have_interfaces = interfaces.Get() != nullptr; |
| 5092 | const size_t num_interfaces = |
| 5093 | have_interfaces ? interfaces->GetLength() : klass->NumDirectInterfaces(); |
| 5094 | if (num_interfaces == 0) { |
| 5095 | if (super_ifcount == 0) { |
| 5096 | // Class implements no interfaces. |
| 5097 | DCHECK_EQ(klass->GetIfTableCount(), 0); |
| 5098 | DCHECK(klass->GetIfTable() == nullptr); |
| 5099 | return true; |
| 5100 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5101 | // Class implements same interfaces as parent, are any of these not marker interfaces? |
| 5102 | bool has_non_marker_interface = false; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5103 | mirror::IfTable* super_iftable = klass->GetSuperClass()->GetIfTable(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5104 | for (size_t i = 0; i < super_ifcount; ++i) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5105 | if (super_iftable->GetMethodArrayCount(i) > 0) { |
| 5106 | has_non_marker_interface = true; |
| 5107 | break; |
| 5108 | } |
| 5109 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5110 | // Class just inherits marker interfaces from parent so recycle parent's iftable. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5111 | if (!has_non_marker_interface) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5112 | klass->SetIfTable(super_iftable); |
| 5113 | return true; |
| 5114 | } |
| 5115 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5116 | size_t ifcount = super_ifcount + num_interfaces; |
| 5117 | for (size_t i = 0; i < num_interfaces; i++) { |
| 5118 | mirror::Class* interface = have_interfaces ? |
| 5119 | interfaces->GetWithoutChecks(i) : mirror::Class::GetDirectInterface(self, klass, i); |
| 5120 | DCHECK(interface != nullptr); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5121 | if (UNLIKELY(!interface->IsInterface())) { |
| 5122 | std::string temp; |
| 5123 | ThrowIncompatibleClassChangeError(klass.Get(), "Class %s implements non-interface class %s", |
| 5124 | PrettyDescriptor(klass.Get()).c_str(), |
| 5125 | PrettyDescriptor(interface->GetDescriptor(&temp)).c_str()); |
| 5126 | return false; |
| 5127 | } |
| 5128 | ifcount += interface->GetIfTableCount(); |
| 5129 | } |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5130 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(AllocIfTable(self, ifcount))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5131 | if (UNLIKELY(iftable.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5132 | CHECK(self->IsExceptionPending()); // OOME. |
| 5133 | return false; |
| 5134 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5135 | if (super_ifcount != 0) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5136 | mirror::IfTable* super_iftable = klass->GetSuperClass()->GetIfTable(); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 5137 | for (size_t i = 0; i < super_ifcount; i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5138 | mirror::Class* super_interface = super_iftable->GetInterface(i); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5139 | iftable->SetInterface(i, super_interface); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 5140 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5141 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5142 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5143 | // Flatten the interface inheritance hierarchy. |
| 5144 | size_t idx = super_ifcount; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5145 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | d035c2d | 2014-10-27 17:30:20 -0700 | [diff] [blame] | 5146 | mirror::Class* interface = have_interfaces ? interfaces->Get(i) : |
| 5147 | mirror::Class::GetDirectInterface(self, klass, i); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5148 | // Check if interface is already in iftable |
| 5149 | bool duplicate = false; |
| 5150 | for (size_t j = 0; j < idx; j++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5151 | mirror::Class* existing_interface = iftable->GetInterface(j); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5152 | if (existing_interface == interface) { |
| 5153 | duplicate = true; |
| 5154 | break; |
| 5155 | } |
| 5156 | } |
| 5157 | if (!duplicate) { |
| 5158 | // Add this non-duplicate interface. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5159 | iftable->SetInterface(idx++, interface); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5160 | // Add this interface's non-duplicate super-interfaces. |
| 5161 | for (int32_t j = 0; j < interface->GetIfTableCount(); j++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5162 | mirror::Class* super_interface = interface->GetIfTable()->GetInterface(j); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5163 | bool super_duplicate = false; |
| 5164 | for (size_t k = 0; k < idx; k++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5165 | mirror::Class* existing_interface = iftable->GetInterface(k); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5166 | if (existing_interface == super_interface) { |
| 5167 | super_duplicate = true; |
| 5168 | break; |
| 5169 | } |
| 5170 | } |
| 5171 | if (!super_duplicate) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5172 | iftable->SetInterface(idx++, super_interface); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5173 | } |
| 5174 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5175 | } |
| 5176 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5177 | self->AllowThreadSuspension(); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5178 | // Shrink iftable in case duplicates were found |
| 5179 | if (idx < ifcount) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5180 | DCHECK_NE(num_interfaces, 0U); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5181 | 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] | 5182 | if (UNLIKELY(iftable.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5183 | CHECK(self->IsExceptionPending()); // OOME. |
| 5184 | return false; |
| 5185 | } |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5186 | ifcount = idx; |
| 5187 | } else { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5188 | DCHECK_EQ(idx, ifcount); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 5189 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5190 | klass->SetIfTable(iftable.Get()); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 5191 | // 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] | 5192 | if (klass->IsInterface()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5193 | return true; |
| 5194 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5195 | size_t miranda_list_size = 0; |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5196 | size_t max_miranda_methods = 0; // The max size of miranda_list. |
| 5197 | for (size_t i = 0; i < ifcount; ++i) { |
| 5198 | max_miranda_methods += iftable->GetInterface(i)->NumVirtualMethods(); |
| 5199 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5200 | MutableHandle<mirror::ObjectArray<mirror::ArtMethod>> |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5201 | miranda_list(hs.NewHandle(AllocArtMethodArray(self, max_miranda_methods))); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5202 | MutableHandle<mirror::ObjectArray<mirror::ArtMethod>> vtable( |
| 5203 | hs.NewHandle(klass->GetVTableDuringLinking())); |
| 5204 | // Copy the IMT from the super class if possible. |
| 5205 | bool extend_super_iftable = false; |
| 5206 | if (has_superclass) { |
| 5207 | mirror::Class* super_class = klass->GetSuperClass(); |
| 5208 | extend_super_iftable = true; |
| 5209 | if (super_class->ShouldHaveEmbeddedImtAndVTable()) { |
| 5210 | for (size_t i = 0; i < mirror::Class::kImtSize; ++i) { |
| 5211 | out_imt->SetReference(i, super_class->GetEmbeddedImTableEntry(i)); |
| 5212 | } |
| 5213 | } else { |
| 5214 | // No imt in the super class, need to reconstruct from the iftable. |
| 5215 | mirror::IfTable* if_table = super_class->GetIfTable(); |
| 5216 | mirror::ArtMethod* conflict_method = runtime->GetImtConflictMethod(); |
| 5217 | const size_t length = super_class->GetIfTableCount(); |
| 5218 | for (size_t i = 0; i < length; ++i) { |
| 5219 | mirror::Class* interface = iftable->GetInterface(i); |
| 5220 | const size_t num_virtuals = interface->NumVirtualMethods(); |
| 5221 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 5222 | DCHECK_EQ(num_virtuals, method_array_count); |
| 5223 | if (method_array_count == 0) { |
| 5224 | continue; |
| 5225 | } |
| 5226 | mirror::ObjectArray<mirror::ArtMethod>* method_array = if_table->GetMethodArray(i); |
| 5227 | for (size_t j = 0; j < num_virtuals; ++j) { |
| 5228 | mirror::ArtMethod* method = method_array->GetWithoutChecks(j); |
| 5229 | if (method->IsMiranda()) { |
| 5230 | continue; |
| 5231 | } |
| 5232 | mirror::ArtMethod* interface_method = interface->GetVirtualMethod(j); |
| 5233 | uint32_t imt_index = interface_method->GetDexMethodIndex() % mirror::Class::kImtSize; |
| 5234 | mirror::ArtMethod* imt_ref = out_imt->GetReference(imt_index)->AsArtMethod(); |
| 5235 | if (imt_ref == runtime->GetImtUnimplementedMethod()) { |
| 5236 | out_imt->SetReference(imt_index, method); |
| 5237 | } else if (imt_ref != conflict_method) { |
| 5238 | out_imt->SetReference(imt_index, conflict_method); |
| 5239 | } |
| 5240 | } |
| 5241 | } |
| 5242 | } |
| 5243 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 5244 | for (size_t i = 0; i < ifcount; ++i) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5245 | self->AllowThreadSuspension(); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 5246 | size_t num_methods = iftable->GetInterface(i)->NumVirtualMethods(); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5247 | if (num_methods > 0) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 5248 | StackHandleScope<2> hs2(self); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5249 | const bool is_super = i < super_ifcount; |
| 5250 | const bool super_interface = is_super && extend_super_iftable; |
| 5251 | Handle<mirror::ObjectArray<mirror::ArtMethod>> method_array; |
| 5252 | Handle<mirror::ObjectArray<mirror::ArtMethod>> input_array; |
| 5253 | if (super_interface) { |
| 5254 | mirror::IfTable* if_table = klass->GetSuperClass()->GetIfTable(); |
| 5255 | DCHECK(if_table != nullptr); |
| 5256 | DCHECK(if_table->GetMethodArray(i) != nullptr); |
| 5257 | // 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] | 5258 | method_array = hs2.NewHandle(if_table->GetMethodArray(i)->Clone(self)-> |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5259 | AsObjectArray<mirror::ArtMethod>()); |
| 5260 | // We are overwriting a super class interface, try to only virtual methods instead of the |
| 5261 | // whole vtable. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 5262 | input_array = hs2.NewHandle(klass->GetVirtualMethods()); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5263 | } else { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 5264 | method_array = hs2.NewHandle(AllocArtMethodArray(self, num_methods)); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5265 | // A new interface, we need the whole vtable incase a new interface method is implemented |
| 5266 | // in the whole superclass. |
| 5267 | input_array = vtable; |
| 5268 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5269 | if (UNLIKELY(method_array.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5270 | CHECK(self->IsExceptionPending()); // OOME. |
| 5271 | return false; |
| 5272 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5273 | iftable->SetMethodArray(i, method_array.Get()); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5274 | if (input_array.Get() == nullptr) { |
| 5275 | // If the added virtual methods is empty, do nothing. |
| 5276 | DCHECK(super_interface); |
| 5277 | continue; |
| 5278 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5279 | for (size_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5280 | mirror::ArtMethod* interface_method = iftable->GetInterface(i)->GetVirtualMethod(j); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5281 | MethodNameAndSignatureComparator interface_name_comparator( |
| 5282 | interface_method->GetInterfaceMethodIfProxy()); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5283 | int32_t k; |
| 5284 | // For each method listed in the interface's method list, find the |
| 5285 | // matching method in our class's method list. We want to favor the |
| 5286 | // subclass over the superclass, which just requires walking |
| 5287 | // back from the end of the vtable. (This only matters if the |
| 5288 | // superclass defines a private method and this class redefines |
| 5289 | // it -- otherwise it would use the same vtable slot. In .dex files |
| 5290 | // those don't end up in the virtual method table, so it shouldn't |
| 5291 | // matter which direction we go. We walk it backward anyway.) |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5292 | for (k = input_array->GetLength() - 1; k >= 0; --k) { |
| 5293 | mirror::ArtMethod* vtable_method = input_array->GetWithoutChecks(k); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5294 | mirror::ArtMethod* vtable_method_for_name_comparison = |
| 5295 | vtable_method->GetInterfaceMethodIfProxy(); |
| 5296 | if (interface_name_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5297 | vtable_method_for_name_comparison)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5298 | if (!vtable_method->IsAbstract() && !vtable_method->IsPublic()) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5299 | ThrowIllegalAccessError( |
| 5300 | klass.Get(), |
| 5301 | "Method '%s' implementing interface method '%s' is not public", |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5302 | PrettyMethod(vtable_method).c_str(), |
| 5303 | PrettyMethod(interface_method).c_str()); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5304 | return false; |
| 5305 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5306 | method_array->SetWithoutChecks<false>(j, vtable_method); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 5307 | // Place method in imt if entry is empty, place conflict otherwise. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5308 | uint32_t imt_index = interface_method->GetDexMethodIndex() % mirror::Class::kImtSize; |
| 5309 | mirror::ArtMethod* imt_ref = out_imt->GetReference(imt_index)->AsArtMethod(); |
| 5310 | mirror::ArtMethod* conflict_method = runtime->GetImtConflictMethod(); |
| 5311 | if (imt_ref == runtime->GetImtUnimplementedMethod()) { |
| 5312 | out_imt->SetReference(imt_index, vtable_method); |
| 5313 | } else if (imt_ref != conflict_method) { |
| 5314 | // If we are not a conflict and we have the same signature and name as the imt entry, |
| 5315 | // it must be that we overwrote a superclass vtable entry. |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5316 | MethodNameAndSignatureComparator imt_ref_name_comparator( |
| 5317 | imt_ref->GetInterfaceMethodIfProxy()); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5318 | if (imt_ref_name_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5319 | vtable_method_for_name_comparison)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5320 | out_imt->SetReference(imt_index, vtable_method); |
| 5321 | } else { |
| 5322 | out_imt->SetReference(imt_index, conflict_method); |
| 5323 | } |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 5324 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5325 | break; |
| 5326 | } |
| 5327 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5328 | if (k < 0 && !super_interface) { |
| 5329 | mirror::ArtMethod* miranda_method = nullptr; |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5330 | for (size_t l = 0; l < miranda_list_size; ++l) { |
| 5331 | mirror::ArtMethod* mir_method = miranda_list->Get(l); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5332 | if (interface_name_comparator.HasSameNameAndSignature(mir_method)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5333 | miranda_method = mir_method; |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5334 | break; |
| 5335 | } |
| 5336 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5337 | if (miranda_method == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5338 | // Point the interface table at a phantom slot. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5339 | miranda_method = interface_method->Clone(self)->AsArtMethod(); |
| 5340 | if (UNLIKELY(miranda_method == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5341 | CHECK(self->IsExceptionPending()); // OOME. |
| 5342 | return false; |
| 5343 | } |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5344 | DCHECK_LT(miranda_list_size, max_miranda_methods); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5345 | miranda_list->Set<false>(miranda_list_size++, miranda_method); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 5346 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5347 | method_array->SetWithoutChecks<false>(j, miranda_method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5348 | } |
| 5349 | } |
| 5350 | } |
| 5351 | } |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5352 | if (miranda_list_size > 0) { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 5353 | int old_method_count = klass->NumVirtualMethods(); |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5354 | int new_method_count = old_method_count + miranda_list_size; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5355 | mirror::ObjectArray<mirror::ArtMethod>* virtuals; |
| 5356 | if (old_method_count == 0) { |
| 5357 | virtuals = AllocArtMethodArray(self, new_method_count); |
| 5358 | } else { |
| 5359 | virtuals = klass->GetVirtualMethods()->CopyOf(self, new_method_count); |
| 5360 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5361 | if (UNLIKELY(virtuals == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5362 | CHECK(self->IsExceptionPending()); // OOME. |
| 5363 | return false; |
| 5364 | } |
| 5365 | klass->SetVirtualMethods(virtuals); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5366 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5367 | int old_vtable_count = vtable->GetLength(); |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5368 | int new_vtable_count = old_vtable_count + miranda_list_size; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5369 | vtable.Assign(vtable->CopyOf(self, new_vtable_count)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5370 | if (UNLIKELY(vtable.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5371 | CHECK(self->IsExceptionPending()); // OOME. |
| 5372 | return false; |
| 5373 | } |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 5374 | for (size_t i = 0; i < miranda_list_size; ++i) { |
| 5375 | mirror::ArtMethod* method = miranda_list->Get(i); |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 5376 | // Leave the declaring class alone as type indices are relative to it |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 5377 | method->SetAccessFlags(method->GetAccessFlags() | kAccMiranda); |
| 5378 | method->SetMethodIndex(0xFFFF & (old_vtable_count + i)); |
| 5379 | klass->SetVirtualMethod(old_method_count + i, method); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5380 | vtable->SetWithoutChecks<false>(old_vtable_count + i, method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5381 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5382 | // 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] | 5383 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5384 | } |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 5385 | |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5386 | if (kIsDebugBuild) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 5387 | mirror::ObjectArray<mirror::ArtMethod>* check_vtable = klass->GetVTableDuringLinking(); |
| 5388 | for (int i = 0; i < check_vtable->GetLength(); ++i) { |
| 5389 | CHECK(check_vtable->GetWithoutChecks(i) != nullptr); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5390 | } |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 5391 | } |
| 5392 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5393 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5394 | return true; |
| 5395 | } |
| 5396 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5397 | bool ClassLinker::LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5398 | CHECK(klass.Get() != nullptr); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5399 | return LinkFields(self, klass, false, nullptr); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5400 | } |
| 5401 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5402 | 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] | 5403 | CHECK(klass.Get() != nullptr); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5404 | return LinkFields(self, klass, true, class_size); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5405 | } |
| 5406 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5407 | struct LinkFieldsComparator { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5408 | explicit LinkFieldsComparator() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 5409 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 5410 | // 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] | 5411 | bool operator()(mirror::ArtField* field1, mirror::ArtField* field2) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5412 | NO_THREAD_SAFETY_ANALYSIS { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 5413 | // 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] | 5414 | Primitive::Type type1 = field1->GetTypeAsPrimitiveType(); |
| 5415 | Primitive::Type type2 = field2->GetTypeAsPrimitiveType(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 5416 | if (type1 != type2) { |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 5417 | if (type1 == Primitive::kPrimNot) { |
| 5418 | // Reference always goes first. |
| 5419 | return true; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 5420 | } |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 5421 | if (type2 == Primitive::kPrimNot) { |
| 5422 | // Reference always goes first. |
| 5423 | return false; |
| 5424 | } |
| 5425 | size_t size1 = Primitive::ComponentSize(type1); |
| 5426 | size_t size2 = Primitive::ComponentSize(type2); |
| 5427 | if (size1 != size2) { |
| 5428 | // Larger primitive types go first. |
| 5429 | return size1 > size2; |
| 5430 | } |
| 5431 | // Primitive types differ but sizes match. Arbitrarily order by primitive type. |
| 5432 | return type1 < type2; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5433 | } |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 5434 | // Same basic group? Then sort by dex field index. This is guaranteed to be sorted |
| 5435 | // by name and for equal names by type id index. |
| 5436 | // NOTE: This works also for proxies. Their static fields are assigned appropriate indexes. |
| 5437 | return field1->GetDexFieldIndex() < field2->GetDexFieldIndex(); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5438 | } |
| 5439 | }; |
| 5440 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5441 | bool ClassLinker::LinkFields(Thread* self, Handle<mirror::Class> klass, bool is_static, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5442 | size_t* class_size) { |
| 5443 | self->AllowThreadSuspension(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5444 | size_t num_fields = |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5445 | is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5446 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5447 | mirror::ObjectArray<mirror::ArtField>* fields = |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5448 | is_static ? klass->GetSFields() : klass->GetIFields(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5449 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5450 | // Initialize field_offset |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 5451 | MemberOffset field_offset(0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5452 | if (is_static) { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5453 | field_offset = klass->GetFirstReferenceStaticFieldOffsetDuringLinking(); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5454 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5455 | mirror::Class* super_class = klass->GetSuperClass(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5456 | if (super_class != nullptr) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5457 | CHECK(super_class->IsResolved()) |
| 5458 | << PrettyClass(klass.Get()) << " " << PrettyClass(super_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5459 | field_offset = MemberOffset(super_class->GetObjectSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5460 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5461 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5462 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5463 | CHECK_EQ(num_fields == 0, fields == nullptr) << PrettyClass(klass.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5464 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5465 | // we want a relatively stable order so that adding new fields |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 5466 | // minimizes disruption of C++ version such as Class and Method. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5467 | std::deque<mirror::ArtField*> grouped_and_sorted_fields; |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 5468 | const char* old_no_suspend_cause = self->StartAssertNoThreadSuspension( |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 5469 | "Naked ArtField references in deque"); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5470 | for (size_t i = 0; i < num_fields; i++) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5471 | mirror::ArtField* f = fields->Get(i); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5472 | CHECK(f != nullptr) << PrettyClass(klass.Get()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5473 | grouped_and_sorted_fields.push_back(f); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5474 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5475 | std::sort(grouped_and_sorted_fields.begin(), grouped_and_sorted_fields.end(), |
| 5476 | LinkFieldsComparator()); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5477 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 5478 | // References should be at the front. |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5479 | size_t current_field = 0; |
| 5480 | size_t num_reference_fields = 0; |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 5481 | FieldGaps gaps; |
| 5482 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5483 | for (; current_field < num_fields; current_field++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5484 | mirror::ArtField* field = grouped_and_sorted_fields.front(); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 5485 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 5486 | bool isPrimitive = type != Primitive::kPrimNot; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5487 | if (isPrimitive) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 5488 | break; // past last reference, move on to the next phase |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5489 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5490 | if (UNLIKELY(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>( |
| 5491 | field_offset.Uint32Value()))) { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 5492 | MemberOffset old_offset = field_offset; |
| 5493 | field_offset = MemberOffset(RoundUp(field_offset.Uint32Value(), 4)); |
| 5494 | AddFieldGap(old_offset.Uint32Value(), field_offset.Uint32Value(), &gaps); |
| 5495 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5496 | DCHECK(IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(field_offset.Uint32Value())); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5497 | grouped_and_sorted_fields.pop_front(); |
| 5498 | num_reference_fields++; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5499 | field->SetOffset(field_offset); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5500 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 5501 | sizeof(mirror::HeapReference<mirror::Object>)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5502 | } |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 5503 | // Gaps are stored as a max heap which means that we must shuffle from largest to smallest |
| 5504 | // otherwise we could end up with suboptimal gap fills. |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5505 | ShuffleForward<8>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 5506 | ShuffleForward<4>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 5507 | ShuffleForward<2>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 5508 | ShuffleForward<1>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 5509 | CHECK(grouped_and_sorted_fields.empty()) << "Missed " << grouped_and_sorted_fields.size() << |
| 5510 | " fields."; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5511 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5512 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 5513 | // 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] | 5514 | if (!is_static && klass->DescriptorEquals("Ljava/lang/ref/Reference;")) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 5515 | // We know there are no non-reference fields in the Reference classes, and we know |
| 5516 | // that 'referent' is alphabetically last, so this is easy... |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5517 | CHECK_EQ(num_reference_fields, num_fields) << PrettyClass(klass.Get()); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 5518 | CHECK_STREQ(fields->Get(num_fields - 1)->GetName(), "referent") << PrettyClass(klass.Get()); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 5519 | --num_reference_fields; |
| 5520 | } |
| 5521 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5522 | size_t size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5523 | // Update klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5524 | if (is_static) { |
| 5525 | klass->SetNumReferenceStaticFields(num_reference_fields); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5526 | *class_size = size; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5527 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5528 | klass->SetNumReferenceInstanceFields(num_reference_fields); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5529 | if (!klass->IsVariableSize()) { |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 5530 | if (klass->DescriptorEquals("Ljava/lang/reflect/ArtMethod;")) { |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 5531 | size_t pointer_size = GetInstructionSetPointerSize(Runtime::Current()->GetInstructionSet()); |
| 5532 | klass->SetObjectSize(mirror::ArtMethod::InstanceSize(pointer_size)); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 5533 | } else { |
| 5534 | std::string temp; |
| 5535 | DCHECK_GE(size, sizeof(mirror::Object)) << klass->GetDescriptor(&temp); |
| 5536 | size_t previous_size = klass->GetObjectSize(); |
| 5537 | if (previous_size != 0) { |
| 5538 | // Make sure that we didn't originally have an incorrect size. |
| 5539 | CHECK_EQ(previous_size, size) << klass->GetDescriptor(&temp); |
| 5540 | } |
| 5541 | klass->SetObjectSize(size); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 5542 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5543 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5544 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5545 | |
| 5546 | if (kIsDebugBuild) { |
| 5547 | // Make sure that the fields array is ordered by name but all reference |
| 5548 | // offsets are at the beginning as far as alignment allows. |
| 5549 | MemberOffset start_ref_offset = is_static |
| 5550 | ? klass->GetFirstReferenceStaticFieldOffsetDuringLinking() |
| 5551 | : klass->GetFirstReferenceInstanceFieldOffset(); |
| 5552 | MemberOffset end_ref_offset(start_ref_offset.Uint32Value() + |
| 5553 | num_reference_fields * |
| 5554 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 5555 | MemberOffset current_ref_offset = start_ref_offset; |
| 5556 | for (size_t i = 0; i < num_fields; i++) { |
| 5557 | mirror::ArtField* field = fields->Get(i); |
| 5558 | if ((false)) { // enable to debug field layout |
| 5559 | LOG(INFO) << "LinkFields: " << (is_static ? "static" : "instance") |
| 5560 | << " class=" << PrettyClass(klass.Get()) |
| 5561 | << " field=" << PrettyField(field) |
| 5562 | << " offset=" |
| 5563 | << field->GetField32(mirror::ArtField::OffsetOffset()); |
| 5564 | } |
| 5565 | if (i != 0) { |
| 5566 | mirror::ArtField* prev_field = fields->Get(i - 1u); |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 5567 | // NOTE: The field names can be the same. This is not possible in the Java language |
| 5568 | // but it's valid Java/dex bytecode and for example proguard can generate such bytecode. |
| 5569 | CHECK_LE(strcmp(prev_field->GetName(), field->GetName()), 0); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5570 | } |
| 5571 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 5572 | bool is_primitive = type != Primitive::kPrimNot; |
| 5573 | if (klass->DescriptorEquals("Ljava/lang/ref/Reference;") && |
| 5574 | strcmp("referent", field->GetName()) == 0) { |
| 5575 | is_primitive = true; // We lied above, so we have to expect a lie here. |
| 5576 | } |
| 5577 | MemberOffset offset = field->GetOffsetDuringLinking(); |
| 5578 | if (is_primitive) { |
| 5579 | if (offset.Uint32Value() < end_ref_offset.Uint32Value()) { |
| 5580 | // Shuffled before references. |
| 5581 | size_t type_size = Primitive::ComponentSize(type); |
| 5582 | CHECK_LT(type_size, sizeof(mirror::HeapReference<mirror::Object>)); |
| 5583 | CHECK_LT(offset.Uint32Value(), start_ref_offset.Uint32Value()); |
| 5584 | CHECK_LE(offset.Uint32Value() + type_size, start_ref_offset.Uint32Value()); |
| 5585 | CHECK(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(offset.Uint32Value())); |
| 5586 | } |
| 5587 | } else { |
| 5588 | CHECK_EQ(current_ref_offset.Uint32Value(), offset.Uint32Value()); |
| 5589 | current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() + |
| 5590 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 5591 | } |
| 5592 | } |
| 5593 | CHECK_EQ(current_ref_offset.Uint32Value(), end_ref_offset.Uint32Value()); |
| 5594 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5595 | return true; |
| 5596 | } |
| 5597 | |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5598 | // Set the bitmap of reference instance field offsets. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5599 | void ClassLinker::CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5600 | uint32_t reference_offsets = 0; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5601 | mirror::Class* super_class = klass->GetSuperClass(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5602 | // 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] | 5603 | if (super_class != nullptr) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5604 | reference_offsets = super_class->GetReferenceInstanceOffsets(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5605 | // Compute reference offsets unless our superclass overflowed. |
| 5606 | if (reference_offsets != mirror::Class::kClassWalkSuper) { |
| 5607 | size_t num_reference_fields = klass->NumReferenceInstanceFieldsDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5608 | if (num_reference_fields != 0u) { |
| 5609 | // All of the fields that contain object references are guaranteed be grouped in memory |
| 5610 | // starting at an appropriately aligned address after super class object data. |
| 5611 | uint32_t start_offset = RoundUp(super_class->GetObjectSize(), |
| 5612 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 5613 | uint32_t start_bit = (start_offset - mirror::kObjectHeaderSize) / |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5614 | sizeof(mirror::HeapReference<mirror::Object>); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5615 | if (start_bit + num_reference_fields > 32) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5616 | reference_offsets = mirror::Class::kClassWalkSuper; |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5617 | } else { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 5618 | reference_offsets |= (0xffffffffu << start_bit) & |
| 5619 | (0xffffffffu >> (32 - (start_bit + num_reference_fields))); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5620 | } |
| 5621 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5622 | } |
| 5623 | } |
Mingyao Yang | faff0f0 | 2014-09-10 12:03:22 -0700 | [diff] [blame] | 5624 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5625 | } |
| 5626 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5627 | mirror::String* ClassLinker::ResolveString(const DexFile& dex_file, uint32_t string_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5628 | Handle<mirror::DexCache> dex_cache) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5629 | DCHECK(dex_cache.Get() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5630 | mirror::String* resolved = dex_cache->GetResolvedString(string_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5631 | if (resolved != nullptr) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5632 | return resolved; |
| 5633 | } |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 5634 | uint32_t utf16_length; |
| 5635 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5636 | mirror::String* string = intern_table_->InternStrong(utf16_length, utf8_data); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5637 | dex_cache->SetResolvedString(string_idx, string); |
| 5638 | return string; |
| 5639 | } |
| 5640 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5641 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, uint16_t type_idx, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 5642 | mirror::Class* referrer) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5643 | StackHandleScope<2> hs(Thread::Current()); |
| 5644 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 5645 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5646 | return ResolveType(dex_file, type_idx, dex_cache, class_loader); |
| 5647 | } |
| 5648 | |
| 5649 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, uint16_t type_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5650 | Handle<mirror::DexCache> dex_cache, |
| 5651 | Handle<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5652 | DCHECK(dex_cache.Get() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5653 | mirror::Class* resolved = dex_cache->GetResolvedType(type_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5654 | if (resolved == nullptr) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 5655 | Thread* self = Thread::Current(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 5656 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 5657 | resolved = FindClass(self, descriptor, class_loader); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5658 | if (resolved != nullptr) { |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 5659 | // TODO: we used to throw here if resolved's class loader was not the |
| 5660 | // boot class loader. This was to permit different classes with the |
| 5661 | // same name to be loaded simultaneously by different loaders |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5662 | dex_cache->SetResolvedType(type_idx, resolved); |
| 5663 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5664 | CHECK(self->IsExceptionPending()) |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 5665 | << "Expected pending exception for failed resolution of: " << descriptor; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5666 | // Convert a ClassNotFoundException to a NoClassDefFoundError. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5667 | StackHandleScope<1> hs(self); |
| 5668 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException(nullptr))); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5669 | if (cause->InstanceOf(GetClassRoot(kJavaLangClassNotFoundException))) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5670 | DCHECK(resolved == nullptr); // No Handle needed to preserve resolved. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 5671 | self->ClearException(); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 5672 | ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5673 | self->GetException(nullptr)->SetCause(cause.Get()); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 5674 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5675 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5676 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5677 | DCHECK((resolved == nullptr) || resolved->IsResolved() || resolved->IsErroneous()) |
Brian Carlstrom | abcf7ae | 2013-09-23 22:19:52 -0700 | [diff] [blame] | 5678 | << PrettyDescriptor(resolved) << " " << resolved->GetStatus(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5679 | return resolved; |
| 5680 | } |
| 5681 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 5682 | mirror::ArtMethod* ClassLinker::ResolveMethod(const DexFile& dex_file, uint32_t method_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5683 | Handle<mirror::DexCache> dex_cache, |
| 5684 | Handle<mirror::ClassLoader> class_loader, |
| 5685 | Handle<mirror::ArtMethod> referrer, |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 5686 | InvokeType type) { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5687 | DCHECK(dex_cache.Get() != nullptr); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5688 | // Check for hit in the dex cache. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5689 | mirror::ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 5690 | if (resolved != nullptr && !resolved->IsRuntimeMethod()) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5691 | return resolved; |
| 5692 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5693 | // Fail, get the declaring class. |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5694 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5695 | 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] | 5696 | if (klass == nullptr) { |
Elliott Hughes | cc5f9a9 | 2011-09-28 19:17:29 -0700 | [diff] [blame] | 5697 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5698 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5699 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5700 | // Scan using method_idx, this saves string compares but will only hit for matching dex |
| 5701 | // caches/files. |
| 5702 | switch (type) { |
| 5703 | case kDirect: // Fall-through. |
| 5704 | case kStatic: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5705 | resolved = klass->FindDirectMethod(dex_cache.Get(), method_idx); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5706 | break; |
| 5707 | case kInterface: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5708 | resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5709 | DCHECK(resolved == nullptr || resolved->GetDeclaringClass()->IsInterface()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5710 | break; |
| 5711 | case kSuper: // Fall-through. |
| 5712 | case kVirtual: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5713 | resolved = klass->FindVirtualMethod(dex_cache.Get(), method_idx); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5714 | break; |
| 5715 | default: |
| 5716 | LOG(FATAL) << "Unreachable - invocation type: " << type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 5717 | UNREACHABLE(); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 5718 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5719 | if (resolved == nullptr) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5720 | // Search by name, which works across dex files. |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5721 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 5722 | const Signature signature = dex_file.GetMethodSignature(method_id); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5723 | switch (type) { |
| 5724 | case kDirect: // Fall-through. |
| 5725 | case kStatic: |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 5726 | resolved = klass->FindDirectMethod(name, signature); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5727 | break; |
| 5728 | case kInterface: |
| 5729 | resolved = klass->FindInterfaceMethod(name, signature); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5730 | DCHECK(resolved == nullptr || resolved->GetDeclaringClass()->IsInterface()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5731 | break; |
| 5732 | case kSuper: // Fall-through. |
| 5733 | case kVirtual: |
| 5734 | resolved = klass->FindVirtualMethod(name, signature); |
| 5735 | break; |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5736 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5737 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5738 | // If we found a method, check for incompatible class changes. |
| 5739 | if (LIKELY(resolved != nullptr && !resolved->CheckIncompatibleClassChange(type))) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5740 | // Be a good citizen and update the dex cache to speed subsequent calls. |
| 5741 | dex_cache->SetResolvedMethod(method_idx, resolved); |
| 5742 | return resolved; |
| 5743 | } else { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5744 | // If we had a method, it's an incompatible-class-change error. |
| 5745 | if (resolved != nullptr) { |
| 5746 | ThrowIncompatibleClassChangeError(type, resolved->GetInvokeType(), resolved, referrer.Get()); |
| 5747 | } else { |
| 5748 | // We failed to find the method which means either an access error, an incompatible class |
| 5749 | // change, or no such method. First try to find the method among direct and virtual methods. |
| 5750 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
| 5751 | const Signature signature = dex_file.GetMethodSignature(method_id); |
| 5752 | switch (type) { |
| 5753 | case kDirect: |
| 5754 | case kStatic: |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5755 | resolved = klass->FindVirtualMethod(name, signature); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5756 | // Note: kDirect and kStatic are also mutually exclusive, but in that case we would |
| 5757 | // have had a resolved method before, which triggers the "true" branch above. |
| 5758 | break; |
| 5759 | case kInterface: |
| 5760 | case kVirtual: |
| 5761 | case kSuper: |
| 5762 | resolved = klass->FindDirectMethod(name, signature); |
| 5763 | break; |
| 5764 | } |
| 5765 | |
| 5766 | // 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] | 5767 | bool exception_generated = false; |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5768 | if (resolved != nullptr && referrer.Get() != nullptr) { |
| 5769 | mirror::Class* methods_class = resolved->GetDeclaringClass(); |
| 5770 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
| 5771 | if (!referring_class->CanAccess(methods_class)) { |
| 5772 | ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, methods_class, |
| 5773 | resolved, type); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5774 | exception_generated = true; |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5775 | } else if (!referring_class->CanAccessMember(methods_class, |
| 5776 | resolved->GetAccessFlags())) { |
| 5777 | ThrowIllegalAccessErrorMethod(referring_class, resolved); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5778 | exception_generated = true; |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5779 | } |
| 5780 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5781 | if (!exception_generated) { |
| 5782 | // Otherwise, throw an IncompatibleClassChangeError if we found something, and check |
| 5783 | // interface methods and throw if we find the method there. If we find nothing, throw a |
| 5784 | // NoSuchMethodError. |
| 5785 | switch (type) { |
| 5786 | case kDirect: |
| 5787 | case kStatic: |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5788 | if (resolved != nullptr) { |
| 5789 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer.Get()); |
| 5790 | } else { |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5791 | resolved = klass->FindInterfaceMethod(name, signature); |
| 5792 | if (resolved != nullptr) { |
| 5793 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer.Get()); |
| 5794 | } else { |
| 5795 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 5796 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5797 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5798 | break; |
| 5799 | case kInterface: |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5800 | if (resolved != nullptr) { |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5801 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer.Get()); |
| 5802 | } else { |
| 5803 | resolved = klass->FindVirtualMethod(name, signature); |
| 5804 | if (resolved != nullptr) { |
| 5805 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer.Get()); |
| 5806 | } else { |
| 5807 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 5808 | } |
| 5809 | } |
| 5810 | break; |
| 5811 | case kSuper: |
| 5812 | if (resolved != nullptr) { |
| 5813 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer.Get()); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5814 | } else { |
| 5815 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 5816 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5817 | break; |
| 5818 | case kVirtual: |
| 5819 | if (resolved != nullptr) { |
| 5820 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer.Get()); |
| 5821 | } else { |
| 5822 | resolved = klass->FindInterfaceMethod(name, signature); |
| 5823 | if (resolved != nullptr) { |
| 5824 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer.Get()); |
| 5825 | } else { |
| 5826 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 5827 | } |
| 5828 | } |
| 5829 | break; |
| 5830 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5831 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5832 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5833 | Thread::Current()->AssertPendingException(); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5834 | return nullptr; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5835 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5836 | } |
| 5837 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5838 | mirror::ArtField* ClassLinker::ResolveField(const DexFile& dex_file, uint32_t field_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5839 | Handle<mirror::DexCache> dex_cache, |
| 5840 | Handle<mirror::ClassLoader> class_loader, |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 5841 | bool is_static) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5842 | DCHECK(dex_cache.Get() != nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5843 | mirror::ArtField* resolved = dex_cache->GetResolvedField(field_idx); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5844 | if (resolved != nullptr) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5845 | return resolved; |
| 5846 | } |
| 5847 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5848 | Thread* const self = Thread::Current(); |
| 5849 | StackHandleScope<1> hs(self); |
| 5850 | Handle<mirror::Class> klass( |
| 5851 | 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] | 5852 | if (klass.Get() == nullptr) { |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 5853 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5854 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5855 | } |
| 5856 | |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 5857 | if (is_static) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5858 | resolved = mirror::Class::FindStaticField(self, klass, dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 5859 | } else { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5860 | resolved = klass->FindInstanceField(dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 5861 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5862 | |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5863 | if (resolved == nullptr) { |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5864 | const char* name = dex_file.GetFieldName(field_id); |
| 5865 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 5866 | if (is_static) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5867 | resolved = mirror::Class::FindStaticField(self, klass, name, type); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5868 | } else { |
| 5869 | resolved = klass->FindInstanceField(name, type); |
| 5870 | } |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5871 | if (resolved == nullptr) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5872 | ThrowNoSuchFieldError(is_static ? "static " : "instance ", klass.Get(), type, name); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5873 | return nullptr; |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5874 | } |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5875 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5876 | dex_cache->SetResolvedField(field_idx, resolved); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5877 | return resolved; |
| 5878 | } |
| 5879 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5880 | mirror::ArtField* ClassLinker::ResolveFieldJLS(const DexFile& dex_file, |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5881 | uint32_t field_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5882 | Handle<mirror::DexCache> dex_cache, |
| 5883 | Handle<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5884 | DCHECK(dex_cache.Get() != nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5885 | mirror::ArtField* resolved = dex_cache->GetResolvedField(field_idx); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5886 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5887 | return resolved; |
| 5888 | } |
| 5889 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5890 | Thread* self = Thread::Current(); |
| 5891 | StackHandleScope<1> hs(self); |
| 5892 | Handle<mirror::Class> klass( |
| 5893 | 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] | 5894 | if (klass.Get() == nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5895 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5896 | return nullptr; |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5897 | } |
| 5898 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 5899 | StringPiece name(dex_file.StringDataByIdx(field_id.name_idx_)); |
| 5900 | StringPiece type(dex_file.StringDataByIdx( |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5901 | dex_file.GetTypeId(field_id.type_idx_).descriptor_idx_)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5902 | resolved = mirror::Class::FindField(self, klass, name, type); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5903 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5904 | dex_cache->SetResolvedField(field_idx, resolved); |
| 5905 | } else { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5906 | ThrowNoSuchFieldError("", klass.Get(), type, name); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5907 | } |
| 5908 | return resolved; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 5909 | } |
| 5910 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5911 | const char* ClassLinker::MethodShorty(uint32_t method_idx, mirror::ArtMethod* referrer, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5912 | uint32_t* length) { |
| 5913 | mirror::Class* declaring_class = referrer->GetDeclaringClass(); |
| 5914 | mirror::DexCache* dex_cache = declaring_class->GetDexCache(); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 5915 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 5916 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 5917 | return dex_file.GetMethodShorty(method_id, length); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 5918 | } |
| 5919 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5920 | void ClassLinker::DumpAllClasses(int flags) { |
| 5921 | if (dex_cache_image_class_lookup_required_) { |
| 5922 | MoveImageClassesToClassTable(); |
| 5923 | } |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 5924 | // TODO: at the time this was written, it wasn't safe to call PrettyField with the ClassLinker |
| 5925 | // 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] | 5926 | std::vector<mirror::Class*> all_classes; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 5927 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 5928 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 5929 | for (GcRoot<mirror::Class>& it : class_table_) { |
| 5930 | all_classes.push_back(it.Read()); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 5931 | } |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 5932 | } |
| 5933 | |
| 5934 | for (size_t i = 0; i < all_classes.size(); ++i) { |
| 5935 | all_classes[i]->DumpClass(std::cerr, flags); |
| 5936 | } |
| 5937 | } |
| 5938 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 5939 | static OatFile::OatMethod CreateOatMethod(const void* code) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5940 | CHECK(code != nullptr); |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 5941 | const uint8_t* base = reinterpret_cast<const uint8_t*>(code); // Base of data points at code. |
| 5942 | base -= sizeof(void*); // Move backward so that code_offset != 0. |
| 5943 | const uint32_t code_offset = sizeof(void*); |
| 5944 | return OatFile::OatMethod(base, code_offset); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5945 | } |
| 5946 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5947 | bool ClassLinker::IsQuickResolutionStub(const void* entry_point) const { |
| 5948 | return (entry_point == GetQuickResolutionStub()) || |
| 5949 | (quick_resolution_trampoline_ == entry_point); |
| 5950 | } |
| 5951 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5952 | bool ClassLinker::IsQuickToInterpreterBridge(const void* entry_point) const { |
| 5953 | return (entry_point == GetQuickToInterpreterBridge()) || |
| 5954 | (quick_to_interpreter_bridge_trampoline_ == entry_point); |
| 5955 | } |
| 5956 | |
| 5957 | bool ClassLinker::IsQuickGenericJniStub(const void* entry_point) const { |
| 5958 | return (entry_point == GetQuickGenericJniStub()) || |
| 5959 | (quick_generic_jni_trampoline_ == entry_point); |
| 5960 | } |
| 5961 | |
| 5962 | const void* ClassLinker::GetRuntimeQuickGenericJniStub() const { |
| 5963 | return GetQuickGenericJniStub(); |
| 5964 | } |
| 5965 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 5966 | void ClassLinker::SetEntryPointsToCompiledCode(mirror::ArtMethod* method, |
| 5967 | const void* method_code) const { |
| 5968 | OatFile::OatMethod oat_method = CreateOatMethod(method_code); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5969 | oat_method.LinkMethod(method); |
| 5970 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5971 | } |
| 5972 | |
| 5973 | void ClassLinker::SetEntryPointsToInterpreter(mirror::ArtMethod* method) const { |
| 5974 | if (!method->IsNative()) { |
| 5975 | method->SetEntryPointFromInterpreter(artInterpreterToInterpreterBridge); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5976 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 5977 | } else { |
| 5978 | const void* quick_method_code = GetQuickGenericJniStub(); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 5979 | OatFile::OatMethod oat_method = CreateOatMethod(quick_method_code); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5980 | oat_method.LinkMethod(method); |
| 5981 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 5982 | } |
| 5983 | } |
| 5984 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5985 | void ClassLinker::DumpForSigQuit(std::ostream& os) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5986 | Thread* self = Thread::Current(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5987 | if (dex_cache_image_class_lookup_required_) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5988 | ScopedObjectAccess soa(self); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5989 | MoveImageClassesToClassTable(); |
| 5990 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5991 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 5992 | os << "Zygote loaded classes=" << pre_zygote_class_table_.Size() << " post zygote classes=" |
| 5993 | << class_table_.Size() << "\n"; |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 5994 | } |
| 5995 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5996 | size_t ClassLinker::NumLoadedClasses() { |
| 5997 | if (dex_cache_image_class_lookup_required_) { |
| 5998 | MoveImageClassesToClassTable(); |
| 5999 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 6000 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 6001 | // Only return non zygote classes since these are the ones which apps which care about. |
| 6002 | return class_table_.Size(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 6003 | } |
| 6004 | |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 6005 | pid_t ClassLinker::GetClassesLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 6006 | return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 6007 | } |
| 6008 | |
| 6009 | pid_t ClassLinker::GetDexLockOwner() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 6010 | return dex_lock_.GetExclusiveOwnerTid(); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 6011 | } |
| 6012 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 6013 | void ClassLinker::SetClassRoot(ClassRoot class_root, mirror::Class* klass) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 6014 | DCHECK(!init_done_); |
| 6015 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6016 | DCHECK(klass != nullptr); |
| 6017 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 6018 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 6019 | mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6020 | DCHECK(class_roots != nullptr); |
| 6021 | DCHECK(class_roots->Get(class_root) == nullptr); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 6022 | class_roots->Set<false>(class_root, klass); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 6023 | } |
| 6024 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 6025 | const char* ClassLinker::GetClassRootDescriptor(ClassRoot class_root) { |
| 6026 | static const char* class_roots_descriptors[] = { |
| 6027 | "Ljava/lang/Class;", |
| 6028 | "Ljava/lang/Object;", |
| 6029 | "[Ljava/lang/Class;", |
| 6030 | "[Ljava/lang/Object;", |
| 6031 | "Ljava/lang/String;", |
| 6032 | "Ljava/lang/DexCache;", |
| 6033 | "Ljava/lang/ref/Reference;", |
| 6034 | "Ljava/lang/reflect/ArtField;", |
| 6035 | "Ljava/lang/reflect/ArtMethod;", |
| 6036 | "Ljava/lang/reflect/Proxy;", |
| 6037 | "[Ljava/lang/String;", |
| 6038 | "[Ljava/lang/reflect/ArtField;", |
| 6039 | "[Ljava/lang/reflect/ArtMethod;", |
| 6040 | "Ljava/lang/ClassLoader;", |
| 6041 | "Ljava/lang/Throwable;", |
| 6042 | "Ljava/lang/ClassNotFoundException;", |
| 6043 | "Ljava/lang/StackTraceElement;", |
| 6044 | "Z", |
| 6045 | "B", |
| 6046 | "C", |
| 6047 | "D", |
| 6048 | "F", |
| 6049 | "I", |
| 6050 | "J", |
| 6051 | "S", |
| 6052 | "V", |
| 6053 | "[Z", |
| 6054 | "[B", |
| 6055 | "[C", |
| 6056 | "[D", |
| 6057 | "[F", |
| 6058 | "[I", |
| 6059 | "[J", |
| 6060 | "[S", |
| 6061 | "[Ljava/lang/StackTraceElement;", |
| 6062 | }; |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 6063 | static_assert(arraysize(class_roots_descriptors) == size_t(kClassRootsMax), |
| 6064 | "Mismatch between class descriptors and class-root enum"); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 6065 | |
| 6066 | const char* descriptor = class_roots_descriptors[class_root]; |
| 6067 | CHECK(descriptor != nullptr); |
| 6068 | return descriptor; |
| 6069 | } |
| 6070 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 6071 | std::size_t ClassLinker::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& root) |
| 6072 | const { |
| 6073 | std::string temp; |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 6074 | return ComputeModifiedUtf8Hash(root.Read()->GetDescriptor(&temp)); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 6075 | } |
| 6076 | |
| 6077 | bool ClassLinker::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a, |
| 6078 | const GcRoot<mirror::Class>& b) { |
| 6079 | if (a.Read()->GetClassLoader() != b.Read()->GetClassLoader()) { |
| 6080 | return false; |
| 6081 | } |
| 6082 | std::string temp; |
| 6083 | return a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp)); |
| 6084 | } |
| 6085 | |
| 6086 | std::size_t ClassLinker::ClassDescriptorHashEquals::operator()( |
| 6087 | const std::pair<const char*, mirror::ClassLoader*>& element) const { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 6088 | return ComputeModifiedUtf8Hash(element.first); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 6089 | } |
| 6090 | |
| 6091 | bool ClassLinker::ClassDescriptorHashEquals::operator()( |
| 6092 | const GcRoot<mirror::Class>& a, const std::pair<const char*, mirror::ClassLoader*>& b) { |
| 6093 | if (a.Read()->GetClassLoader() != b.second) { |
| 6094 | return false; |
| 6095 | } |
| 6096 | return a.Read()->DescriptorEquals(b.first); |
| 6097 | } |
| 6098 | |
| 6099 | bool ClassLinker::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a, |
| 6100 | const char* descriptor) { |
| 6101 | return a.Read()->DescriptorEquals(descriptor); |
| 6102 | } |
| 6103 | |
| 6104 | std::size_t ClassLinker::ClassDescriptorHashEquals::operator()(const char* descriptor) const { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 6105 | return ComputeModifiedUtf8Hash(descriptor); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 6106 | } |
| 6107 | |
Sebastien Hertz | 6963e44 | 2014-11-26 22:11:27 +0100 | [diff] [blame] | 6108 | bool ClassLinker::MayBeCalledWithDirectCodePointer(mirror::ArtMethod* m) { |
| 6109 | // Non-image methods don't use direct code pointer. |
| 6110 | if (!m->GetDeclaringClass()->IsBootStrapClassLoaded()) { |
| 6111 | return false; |
| 6112 | } |
| 6113 | if (m->IsPrivate()) { |
| 6114 | // The method can only be called inside its own oat file. Therefore it won't be called using |
| 6115 | // its direct code if the oat file has been compiled in PIC mode. |
| 6116 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 6117 | const DexFile& dex_file = m->GetDeclaringClass()->GetDexFile(); |
| 6118 | const OatFile::OatDexFile* oat_dex_file = class_linker->FindOpenedOatDexFileForDexFile(dex_file); |
| 6119 | if (oat_dex_file == nullptr) { |
| 6120 | // No oat file: the method has not been compiled. |
| 6121 | return false; |
| 6122 | } |
| 6123 | const OatFile* oat_file = oat_dex_file->GetOatFile(); |
| 6124 | return oat_file != nullptr && !oat_file->IsPic(); |
| 6125 | } else { |
| 6126 | // The method can be called outside its own oat file. Therefore it won't be called using its |
| 6127 | // direct code pointer only if all loaded oat files have been compiled in PIC mode. |
| 6128 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
| 6129 | for (const OatFile* oat_file : oat_files_) { |
| 6130 | if (!oat_file->IsPic()) { |
| 6131 | return true; |
| 6132 | } |
| 6133 | } |
| 6134 | return false; |
| 6135 | } |
| 6136 | } |
| 6137 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6138 | } // namespace art |